7 * LICENSE: This source file is licensed under the terms of the GNU General Public License.
9 * @package Magic3 Framework
10 * @author 平田直毅(Naoki Hirata) <naoki@aplo.co.jp>
11 * @copyright Copyright 2006-2010 Magic3 Project.
12 * @license http://www.gnu.org/copyleft/gpl.html GPL License
13 * @version SVN: $Id: plugin.php 3474 2010-08-13 10:36:48Z fishbone $
14 * @link http://www.magic3.org
17 // 2002-2005 PukiWiki Developers Team
18 // 2001-2002 Originally written by yu-ji
19 // License: GPL v2 or (at your option) any later version
21 define('PKWK_PLUGIN_CALL_TIME_LIMIT', 768);
23 // Set global variables for plugins
24 function set_plugin_messages($messages)
26 foreach ($messages as $name=>$val)
27 if (! isset($GLOBALS[$name]))
28 $GLOBALS[$name] = $val;
31 // Check plugin '$name' is here
32 function exist_plugin($name)
35 static $exist = array(), $count = array();
37 $page = WikiParam::getPage();
38 $name = strtolower($name);
39 if(isset($exist[$name])) {
40 if (++$count[$name] > PKWK_PLUGIN_CALL_TIME_LIMIT){
41 /*die('Alert: plugin "' . htmlspecialchars($name) . '" was called over ' . PKWK_PLUGIN_CALL_TIME_LIMIT .
42 ' times. SPAM or someting?<br />' . "\n" .
43 '<a href="' . get_script_uri() . '?cmd=edit&page='. rawurlencode($vars['page']) . '">Try to edit this page</a><br />' . "\n" .
44 '<a href="' . get_script_uri() . '">Return to frontpage</a>');*/
45 die('Alert: plugin "' . htmlspecialchars($name) . '" was called over ' . PKWK_PLUGIN_CALL_TIME_LIMIT .
46 ' times. SPAM or someting?<br />' . "\n" .
47 '<a href="' . get_script_uri() . WikiParam::convQuery('?cmd=edit&page='. rawurlencode($page)) . '">Try to edit this page</a><br />' . "\n" .
48 '<a href="' . get_script_uri() . '">Return to frontpage</a>');
52 if (preg_match('/^\w{1,64}$/', $name) &&
53 file_exists(PLUGIN_DIR . $name . '.inc.php')) {
56 require_once(PLUGIN_DIR . $name . '.inc.php');
59 $exist[$name] = FALSE;
64 // プラグイン処理のための関数名は、「_action」,「_convert」,「_inline」,「_init」
65 // Check if plugin API 'action' exists
66 function exist_plugin_action($name) {
67 return function_exists('plugin_' . $name . '_action') ? TRUE : exist_plugin($name) ?
68 function_exists('plugin_' . $name . '_action') : FALSE;
71 // Check if plugin API 'convert' exists
72 function exist_plugin_convert($name) {
73 return function_exists('plugin_' . $name . '_convert') ? TRUE : exist_plugin($name) ?
74 function_exists('plugin_' . $name . '_convert') : FALSE;
77 // Check if plugin API 'inline' exists
78 function exist_plugin_inline($name) {
79 return function_exists('plugin_' . $name . '_inline') ? TRUE : exist_plugin($name) ?
80 function_exists('plugin_' . $name . '_inline') : FALSE;
84 function do_plugin_init($name)
86 static $checked = array();
88 if (isset($checked[$name])) return $checked[$name];
90 $func = 'plugin_' . $name . '_init';
91 if (function_exists($func)) {
92 // TRUE or FALSE or NULL (return nothing)
93 $checked[$name] = call_user_func($func);
95 $checked[$name] = NULL; // Not exist
98 return $checked[$name];
101 // Call API 'action' of the plugin
102 function do_plugin_action($name)
104 if (! exist_plugin_action($name)) return array();
106 if(do_plugin_init($name) === FALSE)
107 die_message('Plugin init failed: ' . $name);
109 $retvar = call_user_func('plugin_' . $name . '_action');
111 // Insert a hidden field, supports idenrtifying text enconding
112 if (PKWK_ENCODING_HINT != '')
113 $retvar = preg_replace('/(<form[^>]*>)/', '$1' . "\n" .
114 '<div><input type="hidden" name="encode_hint" value="' .
115 PKWK_ENCODING_HINT . '" /></div>', $retvar);
120 // Call API 'convert' of the plugin
121 function do_plugin_convert($name, $args = '')
125 if(do_plugin_init($name) === FALSE)
126 return '[Plugin init failed: ' . $name . ']';
128 if (! PKWKEXP_DISABLE_MULTILINE_PLUGIN_HACK) {
130 $pos = strpos($args, "\r"); // "\r" is just a delimiter
131 if ($pos !== FALSE) {
132 $body = substr($args, $pos + 1);
133 $args = substr($args, 0, $pos);
138 $aryargs = array(); // #plugin()
140 $aryargs = csv_explode(',', $args); // #plugin(A,B,C,D)
142 if (! PKWKEXP_DISABLE_MULTILINE_PLUGIN_HACK) {
143 if (isset($body)) $aryargs[] = $body; // #plugin(){{body}}
147 $retvar = call_user_func_array('plugin_' . $name . '_convert', $aryargs);
148 $digest = $_digest; // Revert
150 if ($retvar === FALSE) {
151 return htmlspecialchars('#' . $name .
152 ($args != '' ? '(' . $args . ')' : ''));
153 } else if (PKWK_ENCODING_HINT != '') {
154 // Insert a hidden field, supports idenrtifying text enconding
155 return preg_replace('/(<form[^>]*>)/', '$1 ' . "\n" .
156 '<div><input type="hidden" name="encode_hint" value="' .
157 PKWK_ENCODING_HINT . '" /></div>', $retvar);
163 // Call API 'inline' of the plugin
164 function do_plugin_inline($name, $args, & $body)
168 if(do_plugin_init($name) === FALSE)
169 return '[Plugin init failed: ' . $name . ']';
172 $aryargs = csv_explode(',', $args);
177 // NOTE: A reference of $body is always the last argument
178 $aryargs[] = $body; // func_num_args() != 0
181 $retvar = call_user_func_array('plugin_' . $name . '_inline', $aryargs);
182 $digest = $_digest; // Revert
184 if($retvar === FALSE) {
186 return htmlspecialchars('&' . $name . ($args ? '(' . $args . ')' : '') . ';');