2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // $Id: plugin.php,v 1.6 2004/12/30 13:50:54 henoheno Exp $
5 // Plugin related functions
7 // ¥×¥é¥°¥¤¥óÍѤË̤ÄêµÁ¤Î¥°¥í¡¼¥Ð¥ëÊÑ¿ô¤òÀßÄê
8 function set_plugin_messages($messages)
10 foreach ($messages as $name=>$val) {
11 if (! isset($GLOBALS[$name])) $GLOBALS[$name] = $val;
15 // Check plugin '$name' is here
16 function exist_plugin($name)
18 static $exists = array();
20 $name = strtolower($name);
21 if(isset($exists[$name])) return $exists[$name];
23 if (preg_match('/^\w{1,64}$/', $name) &&
24 file_exists(PLUGIN_DIR . $name . '.inc.php')) {
25 $exists[$name] = TRUE;
26 require_once(PLUGIN_DIR . $name . '.inc.php');
29 $exists[$name] = FALSE;
34 // Check if plugin API 'action' exists
35 function exist_plugin_action($name) {
36 return function_exists('plugin_' . $name . '_action') ? TRUE : exist_plugin($name) ?
37 function_exists('plugin_' . $name . '_action') : FALSE;
40 // Check if plugin API 'convert' exists
41 function exist_plugin_convert($name) {
42 return function_exists('plugin_' . $name . '_convert') ? TRUE : exist_plugin($name) ?
43 function_exists('plugin_' . $name . '_convert') : FALSE;
46 // Check if plugin API 'inline' exists
47 function exist_plugin_inline($name) {
48 return function_exists('plugin_' . $name . '_inline') ? TRUE : exist_plugin($name) ?
49 function_exists('plugin_' . $name . '_inline') : FALSE;
53 function do_plugin_init($name)
55 static $checked = array();
57 if (isset($checked[$name])) return $checked[$name];
59 $func = 'plugin_' . $name . '_init';
60 if (function_exists($func)) {
61 // TRUE or FALSE or NULL (return nothing)
62 $checked[$name] = call_user_func($func);
64 $checked[$name] = NULL; // Not exist
67 return $checked[$name];
70 // Call API 'action' of the plugin
71 function do_plugin_action($name)
73 if (! exist_plugin_action($name)) return array();
75 if(do_plugin_init($name) === FALSE)
76 die_message('Plugin init failed: ' . $name);
78 $retvar = call_user_func('plugin_' . $name . '_action');
80 // Insert a hidden field, supports idenrtifying text enconding
81 if (PKWK_ENCODING_HINT != '')
82 $retvar = preg_replace('/(<form[^>]*>)/', '$1' . "\n" .
83 '<div><input type="hidden" name="encode_hint" value="' .
84 PKWK_ENCODING_HINT . '" /></div>', $retvar);
89 // Call API 'convert' of the plugin
90 function do_plugin_convert($name, $args = '')
94 if(do_plugin_init($name) === FALSE)
95 return '[Plugin init failed: ' . $name . ']';
98 $aryargs = csv_explode(',', $args);
104 $retvar = call_user_func_array('plugin_' . $name . '_convert', $aryargs);
105 $digest = $_digest; // Revert
107 if ($retvar === FALSE) {
108 return htmlspecialchars('#' . $name .
109 ($args != '' ? '(' . $args . ')' : ''));
110 } else if (PKWK_ENCODING_HINT != '') {
111 // Insert a hidden field, supports idenrtifying text enconding
112 return preg_replace('/(<form[^>]*>)/', '$1 ' . "\n" .
113 '<div><input type="hidden" name="encode_hint" value="' .
114 PKWK_ENCODING_HINT . '" /></div>', $retvar);
118 // Call API 'inline' of the plugin
119 function do_plugin_inline($name, $args, & $body)
123 if(do_plugin_init($name) === FALSE)
124 return '[Plugin init failed: ' . $name . ']';
127 $aryargs = csv_explode(',', $args);
132 // NOTE: A reference of $body is always the last argument
133 $aryargs[] = & $body; // func_num_args() != 0
136 $retvar = call_user_func_array('plugin_' . $name . '_inline', $aryargs);
137 $digest = $_digest; // Revert
139 if($retvar === FALSE) {
141 return htmlspecialchars('&' . $name . ($args ? '(' . $args . ')' : '') . ';');