2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // $Id: plugin.php,v 1.20 2011/01/25 15:01:01 henoheno Exp $
5 // 2002-2005 PukiWiki Developers Team
6 // 2001-2002 Originally written by yu-ji
7 // License: GPL v2 or (at your option) any later version
9 // Plugin related functions
11 define('PKWK_PLUGIN_CALL_TIME_LIMIT', 768);
13 // Set global variables for plugins
14 function set_plugin_messages($messages)
16 foreach ($messages as $name=>$val)
17 if (! isset($GLOBALS[$name]))
18 $GLOBALS[$name] = $val;
21 // Check plugin '$name' is here
22 function exist_plugin($name)
25 static $exist = array(), $count = array();
27 $name = strtolower($name);
28 if(isset($exist[$name])) {
29 if (++$count[$name] > PKWK_PLUGIN_CALL_TIME_LIMIT)
30 die('Alert: plugin "' . htmlsc($name) .
31 '" was called over ' . PKWK_PLUGIN_CALL_TIME_LIMIT .
32 ' times. SPAM or someting?<br />' . "\n" .
33 '<a href="' . get_script_uri() . '?cmd=edit&page='.
34 rawurlencode($vars['page']) . '">Try to edit this page</a><br />' . "\n" .
35 '<a href="' . get_script_uri() . '">Return to frontpage</a>');
39 if (preg_match('/^\w{1,64}$/', $name) &&
40 file_exists(PLUGIN_DIR . $name . '.inc.php')) {
43 require_once(PLUGIN_DIR . $name . '.inc.php');
46 $exist[$name] = FALSE;
52 // Check if plugin API 'action' exists
53 function exist_plugin_action($name) {
54 return function_exists('plugin_' . $name . '_action') ? TRUE : exist_plugin($name) ?
55 function_exists('plugin_' . $name . '_action') : FALSE;
58 // Check if plugin API 'convert' exists
59 function exist_plugin_convert($name) {
60 return function_exists('plugin_' . $name . '_convert') ? TRUE : exist_plugin($name) ?
61 function_exists('plugin_' . $name . '_convert') : FALSE;
64 // Check if plugin API 'inline' exists
65 function exist_plugin_inline($name) {
66 return function_exists('plugin_' . $name . '_inline') ? TRUE : exist_plugin($name) ?
67 function_exists('plugin_' . $name . '_inline') : FALSE;
71 function do_plugin_init($name)
73 static $checked = array();
75 // TRUE or FALSE or NULL (Return nothing / Not exists)
76 if (array_key_exists($name, $checked)) return $checked[$name];
78 $func = 'plugin_' . $name . '_init';
79 if (function_exists($func)) {
80 $result = call_user_func($func);
81 $checked[$name] = ($result === NULL) ? NULL : (bool)$result;
83 $checked[$name] = NULL;
86 return $checked[$name];
89 // Call API 'action' of the plugin
90 function do_plugin_action($name)
92 if (! exist_plugin_action($name)) return array();
94 if(do_plugin_init($name) === FALSE)
95 die_message('Plugin init failed: ' . $name);
97 $retvar = call_user_func('plugin_' . $name . '_action');
99 // Insert a hidden field, supports idenrtifying text enconding
100 if (PKWK_ENCODING_HINT != '')
101 $retvar = preg_replace('/(<form[^>]*>)/', '$1' . "\n" .
102 '<div><input type="hidden" name="encode_hint" value="' .
103 PKWK_ENCODING_HINT . '" /></div>', $retvar);
108 // Call API 'convert' of the plugin
109 function do_plugin_convert($name, $args = '')
113 if(do_plugin_init($name) === FALSE)
114 return '[Plugin init failed: ' . $name . ']';
116 if (! PKWKEXP_DISABLE_MULTILINE_PLUGIN_HACK) {
118 $pos = strpos($args, "\r"); // "\r" is just a delimiter
119 if ($pos !== FALSE) {
120 $body = substr($args, $pos + 1);
121 $args = substr($args, 0, $pos);
126 $aryargs = array(); // #plugin()
128 $aryargs = csv_explode(',', $args); // #plugin(A,B,C,D)
130 if (! PKWKEXP_DISABLE_MULTILINE_PLUGIN_HACK) {
131 if (isset($body)) $aryargs[] = & $body; // #plugin(){{body}}
135 $retvar = call_user_func_array('plugin_' . $name . '_convert', $aryargs);
136 $digest = $_digest; // Revert
138 if ($retvar === FALSE) {
139 return htmlsc('#' . $name .
140 ($args != '' ? '(' . $args . ')' : ''));
141 } else if (PKWK_ENCODING_HINT != '') {
142 // Insert a hidden field, supports idenrtifying text enconding
143 return preg_replace('/(<form[^>]*>)/', '$1 ' . "\n" .
144 '<div><input type="hidden" name="encode_hint" value="' .
145 PKWK_ENCODING_HINT . '" /></div>', $retvar);
151 // Call API 'inline' of the plugin
152 function do_plugin_inline($name, $args, & $body)
156 if(do_plugin_init($name) === FALSE)
157 return '[Plugin init failed: ' . $name . ']';
160 $aryargs = csv_explode(',', $args);
165 // NOTE: A reference of $body is always the last argument
166 $aryargs[] = & $body; // func_num_args() != 0
169 $retvar = call_user_func_array('plugin_' . $name . '_inline', $aryargs);
170 $digest = $_digest; // Revert
172 if($retvar === FALSE) {
174 return htmlsc('&' . $name . ($args ? '(' . $args . ')' : '') . ';');