xref: /plugin/inclform/syntax.php (revision 23c24f12a58f42590ef80652268d275fa279fa5a)
11db3bf9aScziehr<?php
21db3bf9aScziehr/**
31db3bf9aScziehr * Plugin Include Form: include external, approved PHP forms
41db3bf9aScziehr * Updated April 19, 2007 by Kite <Kite@puzzlers.org>
51db3bf9aScziehr *
61db3bf9aScziehr * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
71db3bf9aScziehr * @author     Kite <Kite@puzzlers.org>
81db3bf9aScziehr * @based_on   "externallink" plugin by Otto Vainio <plugins@valjakko.net>
91db3bf9aScziehr */
101db3bf9aScziehr
111db3bf9aScziehrif(!defined('DOKU_INC')) {
121db3bf9aScziehr	define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
131db3bf9aScziehr}
141db3bf9aScziehrif(!defined('DOKU_PLUGIN')) {
151db3bf9aScziehr	define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
161db3bf9aScziehr}
171db3bf9aScziehr
181db3bf9aScziehrrequire_once(DOKU_PLUGIN.'syntax.php');
191db3bf9aScziehr
201db3bf9aScziehr
211db3bf9aScziehrfunction eval_form_php($arr) {
221db3bf9aScziehr	global $INFO;
231db3bf9aScziehr
241db3bf9aScziehr	if(0 and ($INFO['perm'] == AUTH_ADMIN)) { // Use debug output??
251db3bf9aScziehr		ob_start();
261db3bf9aScziehr		$content = "<!-- Content: ";
271db3bf9aScziehr		print_r( $arr );
281db3bf9aScziehr		$content .= ob_get_contents();
291db3bf9aScziehr		ob_end_clean();
301db3bf9aScziehr		$content .= " -->\n";
311db3bf9aScziehr		echo $content;  // can't return this
321db3bf9aScziehr	}
331db3bf9aScziehr	ob_start();
341db3bf9aScziehr	if($INFO['perm'] == AUTH_ADMIN) {
351db3bf9aScziehr		eval("?>$arr");              //  not $arr -- now allows parsing all PHP blocks
361db3bf9aScziehr	} else {
371db3bf9aScziehr		@eval("?>$arr");             //  not $arr -- now allows parsing all PHP blocks
381db3bf9aScziehr	}
391db3bf9aScziehr	$content = ob_get_contents();
401db3bf9aScziehr	ob_end_clean();
411db3bf9aScziehr	return $content;
421db3bf9aScziehr}
431db3bf9aScziehr
441db3bf9aScziehr/**
451db3bf9aScziehr * All DokuWiki plugins to extend the parser/rendering mechanism
461db3bf9aScziehr * need to inherit from this class
471db3bf9aScziehr */
481db3bf9aScziehrclass syntax_plugin_inclform extends DokuWiki_Syntax_Plugin {
491db3bf9aScziehr
501db3bf9aScziehr    /**
511db3bf9aScziehr     * What kind of syntax are we?
521db3bf9aScziehr     */
531db3bf9aScziehr    function getType(){
541db3bf9aScziehr        return 'substition';
551db3bf9aScziehr    }
561db3bf9aScziehr
571db3bf9aScziehr    // Just before build in links
581db3bf9aScziehr    function getSort(){ return 299; }
591db3bf9aScziehr
601db3bf9aScziehr    /**
611db3bf9aScziehr     * What about paragraphs?
621db3bf9aScziehr     */
631db3bf9aScziehr    function getPType(){
641db3bf9aScziehr        return 'block';
651db3bf9aScziehr    }
661db3bf9aScziehr
671db3bf9aScziehr    function connectTo($mode) {
681db3bf9aScziehr       $this->Lexer->addSpecialPattern('~~INCLFORM[^~]*~~',$mode,'plugin_inclform');
691db3bf9aScziehr    }
701db3bf9aScziehr
711db3bf9aScziehr
721db3bf9aScziehr    /**
731db3bf9aScziehr     * Handle the match
741db3bf9aScziehr     */
751db3bf9aScziehr    function handle($match, $state, $pos, Doku_Handler $handler){
761db3bf9aScziehr	    // Remove the tag itself, and then separate the form name
771db3bf9aScziehr	    // from the parameter set.
781db3bf9aScziehr	    $match = preg_replace("%~~INCLFORM(=(.*))?~~%u", "\\2", $match);
791db3bf9aScziehr	    //echo "\n\t<!-- syntax_plugin_INCLFORM.handle() found >> $match << -->\n";
801db3bf9aScziehr        return $match;
811db3bf9aScziehr    }
821db3bf9aScziehr
831db3bf9aScziehr    /**
841db3bf9aScziehr     * Create output
851db3bf9aScziehr     */
861db3bf9aScziehr    function render($mode, Doku_Renderer $renderer, $data) {
871db3bf9aScziehr        if($mode == 'xhtml'){
881db3bf9aScziehr	    $FORM_PARAMS = explode(';', $data);
891db3bf9aScziehr            $text=$this->_inc_form($FORM_PARAMS[0], $FORM_PARAMS);
901db3bf9aScziehr            $renderer->doc .= $text;
911db3bf9aScziehr            return true;
921db3bf9aScziehr        }
931db3bf9aScziehr        return false;
941db3bf9aScziehr    }
951db3bf9aScziehr
961db3bf9aScziehr
971db3bf9aScziehr	//Translate an ID into a form file name
981db3bf9aScziehr	//path is relative to the DokuWiki root (I use data/forms)
991db3bf9aScziehr	function formFN($id,$rev=''){
1001db3bf9aScziehr	  global $conf;
1011db3bf9aScziehr	  if(empty($rev)){
1021db3bf9aScziehr	    $id = cleanID($id);
1031db3bf9aScziehr	    $id = str_replace(':','/',$id);
104*23c24f12Scziehr	    $fn = $this->getConf('formdir').'/'.utf8_encodeFN($id).'.php';
1051db3bf9aScziehr	  }
1061db3bf9aScziehr	  return $fn;
1071db3bf9aScziehr	} // formFN()
1081db3bf9aScziehr
1091db3bf9aScziehr    function _inc_form($form, $PARAMS) {
1101db3bf9aScziehr	global $FORM_PARAMS;
1111db3bf9aScziehr	global $ID;
1121db3bf9aScziehr        if(strlen($form) < 1) {
1131db3bf9aScziehr	    $form = $ID;   // default to the current page ID
1141db3bf9aScziehr	}
1151db3bf9aScziehr        // Break the form parameters (if any) into name/values
1161db3bf9aScziehr	$path =  $this->formFN($form);
1171db3bf9aScziehr	foreach($PARAMS as $item) {
1181db3bf9aScziehr	    // split the pair
1191db3bf9aScziehr	    $parts = explode('=', $item);
1201db3bf9aScziehr	    // Skip the $ID parameter
1211db3bf9aScziehr	    if(strlen($parts[0])>0) {
1221db3bf9aScziehr		$FORM_PARAMS[$parts[0]] = $parts[1];
1231db3bf9aScziehr	    }
1241db3bf9aScziehr	}
1251db3bf9aScziehr        if(file_exists(DOKU_INC . $path)) {
1261db3bf9aScziehr	    //echo "<!-- form was found -->\n";
1271db3bf9aScziehr	    $text = io_readFile($path);
1281db3bf9aScziehr	    $pattern = "/(<\?php)(.*?)(\?>)/is";
1291db3bf9aScziehr	    $text = eval_form_php($text);
1301db3bf9aScziehr	} else {
1311db3bf9aScziehr	    $text = "\n\t<!-- No form found for '$form'-->\n";
1321db3bf9aScziehr	}
1331db3bf9aScziehr	return $text;
1341db3bf9aScziehr    } // _inc_form()
1351db3bf9aScziehr} // syntax_plugin_INCLFORM
1361db3bf9aScziehr?>
137