Include PHP Plugin

AuthorJan Wessely
Version0.0.1
Required DokuWiki Versiondevel
Downloadthere
Main PageInclude PHP Plugin at jawe.net

Description

A DokuWiki plugin to include approved PHP files in pages.

Syntax

{{php>filename}}

where filename is the PHP file's name without the .php extension

Configuration

place the files you want to be included into the dokuwiki/lib/plugins/php/php directory.

Source code

syntax.php
<?php
/**
 * Plugin Include PHP: Includes an approved PHP file into a page.
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Jan Wessely <info@jawe.net>
 */
 
if(!defined('DOKU_INC')) {
    define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
}
if(!defined('DOKU_PLUGIN')) {
    define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
}
 
require_once(DOKU_PLUGIN.'syntax.php');
 
/**
 * All DokuWiki plugins to extend the parser/rendering mechanism
 * need to inherit from this class
 */
class syntax_plugin_php extends DokuWiki_Syntax_Plugin {
 
    /**
     * return some info
     */
    function getInfo(){
        return array(
            'author' => 'Jan Wessely',
            'email'  => 'info@jawe.net',
            'date'   => '2009-09-13',
            'name'   => 'Include PHP',
            'desc'   => 'Includes an approved PHP file into a page.',
            'url'    => 'http://jawe.net/wiki/dev/dokuwiki/php',
        );
    }
 
    /**
     * What kind of syntax are we?
     */
    function getType(){
        return 'substition';
    }
 
    // Just before build in links
    function getSort(){ return 299; }
 
    /**
     * What about paragraphs?
     */
    function getPType(){
        return 'block';
    }
 
    function connectTo($mode) {
       $this->Lexer->addSpecialPattern('\{\{php>[^\}]*?\}\}',$mode,'plugin_php');
    }
 
 
    /**
     * Handle the match
     */
    function handle($match, $state, $pos, &$handler){
    $match = preg_replace("%\\{\\{php(?:\\>(.*))?\\}\\}%u", "\\1", $match);
        return $match;
    }
 
    /**
     * Create output
     */
    function render($mode, &$renderer, $data) {
        if($mode == 'xhtml'){
            $text=$this->_inc_form($data);
            $renderer->doc .= $text;
            return true;
        }
        return false;
    }
 
    //Translate an ID into a form file name
    //path is relative to the Dokuwiki root (I use data/forms)
    function formFN($id,$rev=''){
        global $conf;
        if(empty($rev)) {
            $id = cleanID($id);
            $id = str_replace(':','/',$id);
            $fn = 'php/'.utf8_encodeFN($id).'.php';
        }
        return $fn;
    } // formFN()
 
    function _inc_form($form) {
        global $ID;
        if(strlen($form) < 1) {
            $form = $ID;   // default to the current page ID
    }
    $path =  $this->formFN($form);
        ob_start();
        echo "\n\t<!-- Begin '$form' -->\n";
        include_once($path);
        echo "\n\t<!-- End '$form' -->\n";
        $text = ob_get_contents();
        ob_end_clean();
        if (empty($text))
            $text = "\n\t<!-- No file found for '$form'-->\n";
        return $text;
    } // _inc_form()
} // syntax_plugin_php
?>

Demo

Discussion

your complaints here please: