xref: /plugin/inclform/syntax.php (revision 1db3bf9af0e77fe8902fc0695648e36af5d7fa27)
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     * return some info
52     */
53    function getInfo(){
54        return array(
55            'author' => 'Kite',
56            'email'  => 'kite@puzzlers.org',
57            'date'   => '2006-09-02',
58            'name'   => 'Include Form',
59            'desc'   => 'Includes an approved form into a page.',
60            'url'    => 'http://www.dokuwiki.org/wiki:plugins:incl_form',
61        );
62    }
63
64    /**
65     * What kind of syntax are we?
66     */
67    function getType(){
68        return 'substition';
69    }
70
71    // Just before build in links
72    function getSort(){ return 299; }
73
74    /**
75     * What about paragraphs?
76     */
77    function getPType(){
78        return 'block';
79    }
80
81    function connectTo($mode) {
82       $this->Lexer->addSpecialPattern('~~INCLFORM[^~]*~~',$mode,'plugin_inclform');
83    }
84
85
86    /**
87     * Handle the match
88     */
89    function handle($match, $state, $pos, Doku_Handler $handler){
90	    // Remove the tag itself, and then separate the form name
91	    // from the parameter set.
92	    $match = preg_replace("%~~INCLFORM(=(.*))?~~%u", "\\2", $match);
93	    //echo "\n\t<!-- syntax_plugin_INCLFORM.handle() found >> $match << -->\n";
94        return $match;
95    }
96
97    /**
98     * Create output
99     */
100    function render($mode, Doku_Renderer $renderer, $data) {
101        if($mode == 'xhtml'){
102	    $FORM_PARAMS = explode(';', $data);
103            $text=$this->_inc_form($FORM_PARAMS[0], $FORM_PARAMS);
104            $renderer->doc .= $text;
105            return true;
106        }
107        return false;
108    }
109
110
111	//Translate an ID into a form file name
112	//path is relative to the DokuWiki root (I use data/forms)
113	function formFN($id,$rev=''){
114	  global $conf;
115	  if(empty($rev)){
116	    $id = cleanID($id);
117	    $id = str_replace(':','/',$id);
118	    $fn = $conf['formdir'].'/'.utf8_encodeFN($id).'.php';
119	  }
120	  return $fn;
121	} // formFN()
122
123    function _inc_form($form, $PARAMS) {
124	global $FORM_PARAMS;
125	global $ID;
126        if(strlen($form) < 1) {
127	    $form = $ID;   // default to the current page ID
128	}
129        // Break the form parameters (if any) into name/values
130	$path =  $this->formFN($form);
131	foreach($PARAMS as $item) {
132	    // split the pair
133	    $parts = explode('=', $item);
134	    // Skip the $ID parameter
135	    if(strlen($parts[0])>0) {
136		$FORM_PARAMS[$parts[0]] = $parts[1];
137	    }
138	}
139        if(file_exists(DOKU_INC . $path)) {
140	    //echo "<!-- form was found -->\n";
141	    $text = io_readFile($path);
142	    $pattern = "/(<\?php)(.*?)(\?>)/is";
143	    $text = eval_form_php($text);
144	} else {
145	    $text = "\n\t<!-- No form found for '$form'-->\n";
146	}
147	return $text;
148    } // _inc_form()
149} // syntax_plugin_INCLFORM
150?>
151