1<?php
2/**
3 * Plugin Include Form: include external, approved PHP forms
4 * Updated April 19, 2007 by Kite <Kite@puzzlers.org>
5 *
6 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author     Kite <Kite@puzzlers.org>
8 * @based_on   "externallink" plugin by Otto Vainio <plugins@valjakko.net>
9 */
10
11if(!defined('DOKU_INC')) {
12	define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
13}
14if(!defined('DOKU_PLUGIN')) {
15	define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
16}
17
18require_once(DOKU_PLUGIN.'syntax.php');
19
20
21function eval_form_php($arr) {
22	global $INFO;
23
24	if(0 and ($INFO['perm'] == AUTH_ADMIN)) { // Use debug output??
25		ob_start();
26		$content = "<!-- Content: ";
27		print_r( $arr );
28		$content .= ob_get_contents();
29		ob_end_clean();
30		$content .= " -->\n";
31		echo $content;  // can't return this
32	}
33	ob_start();
34	if($INFO['perm'] == AUTH_ADMIN) {
35		eval("?>$arr");              //  not $arr -- now allows parsing all PHP blocks
36	} else {
37		@eval("?>$arr");             //  not $arr -- now allows parsing all PHP blocks
38	}
39	$content = ob_get_contents();
40	ob_end_clean();
41	return $content;
42}
43
44/**
45 * All DokuWiki plugins to extend the parser/rendering mechanism
46 * need to inherit from this class
47 */
48class syntax_plugin_inclform extends DokuWiki_Syntax_Plugin {
49
50    /**
51     * What kind of syntax are we?
52     */
53    function getType(){
54        return 'substition';
55    }
56
57    // Just before build in links
58    function getSort(){ return 299; }
59
60    /**
61     * What about paragraphs?
62     */
63    function getPType(){
64        return 'block';
65    }
66
67    function connectTo($mode) {
68       $this->Lexer->addSpecialPattern('~~INCLFORM[^~]*~~',$mode,'plugin_inclform');
69    }
70
71
72    /**
73     * Handle the match
74     */
75    function handle($match, $state, $pos, Doku_Handler $handler){
76	    // Remove the tag itself, and then separate the form name
77	    // from the parameter set.
78	    $match = preg_replace("%~~INCLFORM(=(.*))?~~%u", "\\2", $match);
79	    //echo "\n\t<!-- syntax_plugin_INCLFORM.handle() found >> $match << -->\n";
80        return $match;
81    }
82
83    /**
84     * Create output
85     */
86    function render($mode, Doku_Renderer $renderer, $data) {
87        if($mode == 'xhtml'){
88	    $FORM_PARAMS = explode(';', $data);
89            $text=$this->_inc_form($FORM_PARAMS[0], $FORM_PARAMS);
90            $renderer->doc .= $text;
91            return true;
92        }
93        return false;
94    }
95
96
97	//Translate an ID into a form file name
98	//path is relative to the DokuWiki root (I use data/forms)
99	function formFN($id,$rev=''){
100	  global $conf;
101	  if(empty($rev)){
102	    $id = cleanID($id);
103	    $id = str_replace(':','/',$id);
104	    $fn = $this->getConf('formdir').'/'.utf8_encodeFN($id).'.php';
105	  }
106	  return $fn;
107	} // formFN()
108
109    function _inc_form($form, $PARAMS) {
110	global $FORM_PARAMS;
111	global $ID;
112        if(strlen($form) < 1) {
113	    $form = $ID;   // default to the current page ID
114	}
115        // Break the form parameters (if any) into name/values
116	$path =  $this->formFN($form);
117	foreach($PARAMS as $item) {
118	    // split the pair
119	    $parts = explode('=', $item);
120	    // Skip the $ID parameter
121	    if(strlen($parts[0])>0) {
122		$FORM_PARAMS[$parts[0]] = $parts[1];
123	    }
124	}
125        if(file_exists(DOKU_INC . $path)) {
126	    //echo "<!-- form was found -->\n";
127	    $text = io_readFile($path);
128	    $pattern = "/(<\?php)(.*?)(\?>)/is";
129	    $text = eval_form_php($text);
130	} else {
131	    $text = "\n\t<!-- No form found for '$form'-->\n";
132	}
133	return $text;
134    } // _inc_form()
135} // syntax_plugin_INCLFORM
136?>
137