OSDN Git Service

htmlsc(): Just sugar for htmlspecialchars(), and a foundation
[pukiwiki/pukiwiki.git] / lib / plugin.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // $Id: plugin.php,v 1.20 2011/01/25 15:01:01 henoheno Exp $
4 // Copyright (C)
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
8 //
9 // Plugin related functions
10
11 define('PKWK_PLUGIN_CALL_TIME_LIMIT', 768);
12
13 // Set global variables for plugins
14 function set_plugin_messages($messages)
15 {
16         foreach ($messages as $name=>$val)
17                 if (! isset($GLOBALS[$name]))
18                         $GLOBALS[$name] = $val;
19 }
20
21 // Check plugin '$name' is here
22 function exist_plugin($name)
23 {
24         global $vars;
25         static $exist = array(), $count = array();
26
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&amp;page='.
34                         rawurlencode($vars['page']) . '">Try to edit this page</a><br />' . "\n" .
35                         '<a href="' . get_script_uri() . '">Return to frontpage</a>');
36                 return $exist[$name];
37         }
38
39         if (preg_match('/^\w{1,64}$/', $name) &&
40             file_exists(PLUGIN_DIR . $name . '.inc.php')) {
41                 $exist[$name] = TRUE;
42                 $count[$name] = 1;
43                 require_once(PLUGIN_DIR . $name . '.inc.php');
44                 return TRUE;
45         } else {
46                 $exist[$name] = FALSE;
47                 $count[$name] = 1;
48                 return FALSE;
49         }
50 }
51
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;
56 }
57
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;
62 }
63
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;
68 }
69
70 // Do init the plugin
71 function do_plugin_init($name)
72 {
73         static $checked = array();
74
75         if (isset($checked[$name])) return $checked[$name];
76
77         $func = 'plugin_' . $name . '_init';
78         if (function_exists($func)) {
79                 // TRUE or FALSE or NULL (return nothing)
80                 $checked[$name] = call_user_func($func);
81         } else {
82                 $checked[$name] = NULL; // Not exist
83         }
84
85         return $checked[$name];
86 }
87
88 // Call API 'action' of the plugin
89 function do_plugin_action($name)
90 {
91         if (! exist_plugin_action($name)) return array();
92
93         if(do_plugin_init($name) === FALSE)
94                 die_message('Plugin init failed: ' . $name);
95
96         $retvar = call_user_func('plugin_' . $name . '_action');
97
98         // Insert a hidden field, supports idenrtifying text enconding
99         if (PKWK_ENCODING_HINT != '')
100                 $retvar =  preg_replace('/(<form[^>]*>)/', '$1' . "\n" .
101                         '<div><input type="hidden" name="encode_hint" value="' .
102                         PKWK_ENCODING_HINT . '" /></div>', $retvar);
103
104         return $retvar;
105 }
106
107 // Call API 'convert' of the plugin
108 function do_plugin_convert($name, $args = '')
109 {
110         global $digest;
111
112         if(do_plugin_init($name) === FALSE)
113                 return '[Plugin init failed: ' . $name . ']';
114
115         if (! PKWKEXP_DISABLE_MULTILINE_PLUGIN_HACK) {
116                 // Multiline plugin?
117                 $pos  = strpos($args, "\r"); // "\r" is just a delimiter
118                 if ($pos !== FALSE) {
119                         $body = substr($args, $pos + 1);
120                         $args = substr($args, 0, $pos);
121                 }
122         }
123
124         if ($args === '') {
125                 $aryargs = array();                 // #plugin()
126         } else {
127                 $aryargs = csv_explode(',', $args); // #plugin(A,B,C,D)
128         }
129         if (! PKWKEXP_DISABLE_MULTILINE_PLUGIN_HACK) {
130                 if (isset($body)) $aryargs[] = & $body;     // #plugin(){{body}}
131         }
132
133         $_digest = $digest;
134         $retvar  = call_user_func_array('plugin_' . $name . '_convert', $aryargs);
135         $digest  = $_digest; // Revert
136
137         if ($retvar === FALSE) {
138                 return htmlsc('#' . $name .
139                         ($args != '' ? '(' . $args . ')' : ''));
140         } else if (PKWK_ENCODING_HINT != '') {
141                 // Insert a hidden field, supports idenrtifying text enconding
142                 return preg_replace('/(<form[^>]*>)/', '$1 ' . "\n" .
143                         '<div><input type="hidden" name="encode_hint" value="' .
144                         PKWK_ENCODING_HINT . '" /></div>', $retvar);
145         } else {
146                 return $retvar;
147         }
148 }
149
150 // Call API 'inline' of the plugin
151 function do_plugin_inline($name, $args, & $body)
152 {
153         global $digest;
154
155         if(do_plugin_init($name) === FALSE)
156                 return '[Plugin init failed: ' . $name . ']';
157
158         if ($args !== '') {
159                 $aryargs = csv_explode(',', $args);
160         } else {
161                 $aryargs = array();
162         }
163
164         // NOTE: A reference of $body is always the last argument
165         $aryargs[] = & $body; // func_num_args() != 0
166
167         $_digest = $digest;
168         $retvar  = call_user_func_array('plugin_' . $name . '_inline', $aryargs);
169         $digest  = $_digest; // Revert
170
171         if($retvar === FALSE) {
172                 // Do nothing
173                 return htmlsc('&' . $name . ($args ? '(' . $args . ')' : '') . ';');
174         } else {
175                 return $retvar;
176         }
177 }
178 ?>