X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=lib%2Fplugin.php;h=1bfcc3ded993978d56b97a5d8a6d5e1874a5446e;hb=a6cac21c76c2e94d8b90a26a59764b7e0e4cfb81;hp=6e994a641eab2469387ea506f37b3fa52ce3a240;hpb=e923786be4e4997d8508c9e8ec0465c44b5e1c00;p=pukiwiki%2Fpukiwiki.git diff --git a/lib/plugin.php b/lib/plugin.php index 6e994a6..1bfcc3d 100644 --- a/lib/plugin.php +++ b/lib/plugin.php @@ -1,134 +1,191 @@ $val) { - if (! isset($GLOBALS[$name])) $GLOBALS[$name] = $val; - } + foreach ($messages as $name=>$val) + if (! isset($GLOBALS[$name])) + $GLOBALS[$name] = $val; } -//¥×¥é¥°¥¤¥ó¤¬Â¸ºß¤¹¤ë¤« +// Check plugin '$name' is here function exist_plugin($name) { - $name = strtolower($name); // Âçʸ»ú¤È¾®Ê¸»ú¤ò¶èÊ̤·¤Ê¤¤¥Õ¥¡¥¤¥ë¥·¥¹¥Æ¥àÂкö + global $vars; + static $exist = array(), $count = array(); + + $name = strtolower($name); + if(isset($exist[$name])) { + if (++$count[$name] > PKWK_PLUGIN_CALL_TIME_LIMIT) + die('Alert: plugin "' . htmlsc($name) . + '" was called over ' . PKWK_PLUGIN_CALL_TIME_LIMIT . + ' times. SPAM or someting?
' . "\n" . + 'Try to edit this page
' . "\n" . + 'Return to frontpage'); + return $exist[$name]; + } + if (preg_match('/^\w{1,64}$/', $name) && file_exists(PLUGIN_DIR . $name . '.inc.php')) { + $exist[$name] = TRUE; + $count[$name] = 1; require_once(PLUGIN_DIR . $name . '.inc.php'); return TRUE; } else { + $exist[$name] = FALSE; + $count[$name] = 1; return FALSE; } } -//¥×¥é¥°¥¤¥ó´Ø¿ô(action)¤¬Â¸ºß¤¹¤ë¤« +// Check if plugin API 'action' exists function exist_plugin_action($name) { - return function_exists('plugin_' . $name . '_action') ? TRUE : exist_plugin($name) ? - function_exists('plugin_' . $name . '_action') : FALSE; + if (function_exists('plugin_' . $name . '_action')) { + return TRUE; + } + if (exist_plugin($name)) { + return function_exists('plugin_' . $name . '_action'); + } + return FALSE; } -//¥×¥é¥°¥¤¥ó´Ø¿ô(convert)¤¬Â¸ºß¤¹¤ë¤« +// Check if plugin API 'convert' exists function exist_plugin_convert($name) { - return function_exists('plugin_' . $name . '_convert') ? TRUE : exist_plugin($name) ? - function_exists('plugin_' . $name . '_convert') : FALSE; + if (function_exists('plugin_' . $name . '_convert')) { + return TRUE; + } + if (exist_plugin($name)) { + return function_exists('plugin_' . $name . '_convert'); + } + return FALSE; } -//¥×¥é¥°¥¤¥ó´Ø¿ô(inline)¤¬Â¸ºß¤¹¤ë¤« +// Check if plugin API 'inline' exists function exist_plugin_inline($name) { - return function_exists('plugin_' . $name . '_inline') ? TRUE : exist_plugin($name) ? - function_exists('plugin_' . $name . '_inline') : FALSE; + if (function_exists('plugin_' . $name . '_inline')) { + return TRUE; + } + if (exist_plugin($name)) { + return function_exists('plugin_' . $name . '_inline'); + } + return FALSE; } -//¥×¥é¥°¥¤¥ó¤Î½é´ü²½¤ò¼Â¹Ô +// Call 'init' function for the plugin +// NOTE: Returning FALSE means "An erorr occurerd" function do_plugin_init($name) { - static $checked = array(); + static $done = array(); - if (! isset($checked[$name])) { + if (! isset($done[$name])) { $func = 'plugin_' . $name . '_init'; - if (function_exists($func)) { - // TRUE or FALSE or NULL (return nothing) - $checked[$name] = call_user_func($func); - } else { - // Not exists - $checked[$name] = null; - } + $done[$name] = (! function_exists($func) || call_user_func($func) !== FALSE); } - return $checked[$name]; + + return $done[$name]; } -//¥×¥é¥°¥¤¥ó(action)¤ò¼Â¹Ô +// Call API 'action' of the plugin function do_plugin_action($name) { if (! exist_plugin_action($name)) return array(); - if(do_plugin_init($name) === FALSE) - die_message("Plugin init failed: $name"); + if (do_plugin_init($name) === FALSE) { + die_message('Plugin init failed: ' . htmlsc($name)); + } $retvar = call_user_func('plugin_' . $name . '_action'); - // ʸ»ú¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¸¡½ÐÍÑ hidden ¥Õ¥£¡¼¥ë¥É¤òÁÞÆþ - return preg_replace('/(]*>)/', - "$1\n" . '
', - $retvar); + // Insert a hidden field, supports idenrtifying text enconding + if (PKWK_ENCODING_HINT != '') + $retvar = preg_replace('/(]*>)/', '$1' . "\n" . + '
', $retvar); + + return $retvar; } -//¥×¥é¥°¥¤¥ó(convert)¤ò¼Â¹Ô +// Call API 'convert' of the plugin function do_plugin_convert($name, $args = '') { global $digest; - if(do_plugin_init($name) === FALSE) - die_message("Plugin init failed: $name"); + if (do_plugin_init($name) === FALSE) { + return '[Plugin init failed: ' . htmlsc($name) . ']'; + } - if ($args !== '') { - $aryargs = csv_explode(',', $args); + if (! PKWKEXP_DISABLE_MULTILINE_PLUGIN_HACK) { + // Multiline plugin? + $pos = strpos($args, "\r"); // "\r" is just a delimiter + if ($pos !== FALSE) { + $body = substr($args, $pos + 1); + $args = substr($args, 0, $pos); + } + } + + if ($args === '') { + $aryargs = array(); // #plugin() } else { - $aryargs = array(); + $aryargs = csv_explode(',', $args); // #plugin(A,B,C,D) + } + if (! PKWKEXP_DISABLE_MULTILINE_PLUGIN_HACK) { + if (isset($body)) $aryargs[] = & $body; // #plugin(){{body}} } - $_digest = $digest; // ÂàÈò + $_digest = $digest; $retvar = call_user_func_array('plugin_' . $name . '_convert', $aryargs); - $digest = $_digest; // Éü¸µ + $digest = $_digest; // Revert if ($retvar === FALSE) { - return htmlspecialchars('#' . $name . ($args ? "($args)" : '')); + return htmlsc('#' . $name . + ($args != '' ? '(' . $args . ')' : '')); + } else if (PKWK_ENCODING_HINT != '') { + // Insert a hidden field, supports idenrtifying text enconding + return preg_replace('/(]*>)/', '$1 ' . "\n" . + '
', $retvar); } else { - // ʸ»ú¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¸¡½ÐÍÑ hidden ¥Õ¥£¡¼¥ë¥É¤òÁÞÆþ - return preg_replace('/(]*>)/', - "$1\n" . '
', - $retvar); + return $retvar; } } -//¥×¥é¥°¥¤¥ó(inline)¤ò¼Â¹Ô +// Call API 'inline' of the plugin function do_plugin_inline($name, $args, & $body) { global $digest; - if(do_plugin_init($name) === FALSE) - die_message("Plugin init failed: $name"); + if (do_plugin_init($name) === FALSE) { + return '[Plugin init failed: ' . htmlsc($name) . ']'; + } - if ($args !== '') { - $aryargs = csv_explode(',', $args); - } else { + if ($args === '') { $aryargs = array(); + } else { + $aryargs = csv_explode(',', $args); } - $aryargs[] = & $body; // Added reference of $body - $_digest = $digest; // ÂàÈò + // NOTE: A reference of $body is always the last argument + $aryargs[] = & $body; // func_num_args() != 0 + + $_digest = $digest; $retvar = call_user_func_array('plugin_' . $name . '_inline', $aryargs); - $digest = $_digest; // Éü¸µ + $digest = $_digest; // Revert if($retvar === FALSE) { - return htmlspecialchars("&${name}" . ($args ? "($args)" : '') . ';'); + // Do nothing + return htmlsc('&' . $name . ($args ? '(' . $args . ')' : '') . ';'); } else { return $retvar; } } -?>