OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / wiki_main / include / lib / plugin.php
1 <?php
2 /**
3  * プラグイン処理関係
4  *
5  * PHP versions 5
6  *
7  * LICENSE: This source file is licensed under the terms of the GNU General Public License.
8  *
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
15  */
16 // Copyright (C)
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
20
21 define('PKWK_PLUGIN_CALL_TIME_LIMIT', 768);
22
23 // Set global variables for plugins
24 function set_plugin_messages($messages)
25 {
26         foreach ($messages as $name=>$val)
27                 if (! isset($GLOBALS[$name]))
28                         $GLOBALS[$name] = $val;
29 }
30
31 // Check plugin '$name' is here
32 function exist_plugin($name)
33 {
34         //global $vars;
35         static $exist = array(), $count = array();
36
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&amp;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&amp;page='. rawurlencode($page)) . '">Try to edit this page</a><br />' . "\n" .
48                         '<a href="' . get_script_uri() . '">Return to frontpage</a>');
49                 }
50                 return $exist[$name];
51         }
52         if (preg_match('/^\w{1,64}$/', $name) &&
53             file_exists(PLUGIN_DIR . $name . '.inc.php')) {
54                 $exist[$name] = TRUE;
55                 $count[$name] = 1;
56                 require_once(PLUGIN_DIR . $name . '.inc.php');
57                 return TRUE;
58         } else {
59                 $exist[$name] = FALSE;
60                 $count[$name] = 1;
61                 return FALSE;
62         }
63 }
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;
69 }
70
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;
75 }
76
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;
81 }
82
83 // Do init the plugin
84 function do_plugin_init($name)
85 {
86         static $checked = array();
87
88         if (isset($checked[$name])) return $checked[$name];
89
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);
94         } else {
95                 $checked[$name] = NULL; // Not exist
96         }
97
98         return $checked[$name];
99 }
100
101 // Call API 'action' of the plugin
102 function do_plugin_action($name)
103 {
104         if (! exist_plugin_action($name)) return array();
105
106         if(do_plugin_init($name) === FALSE)
107                 die_message('Plugin init failed: ' . $name);
108
109         $retvar = call_user_func('plugin_' . $name . '_action');
110
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);
116
117         return $retvar;
118 }
119
120 // Call API 'convert' of the plugin
121 function do_plugin_convert($name, $args = '')
122 {
123         global $digest;
124
125         if(do_plugin_init($name) === FALSE)
126                 return '[Plugin init failed: ' . $name . ']';
127
128         if (! PKWKEXP_DISABLE_MULTILINE_PLUGIN_HACK) {
129                 // Multiline plugin?
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);
134                 }
135         }
136
137         if ($args === '') {
138                 $aryargs = array();                 // #plugin()
139         } else {
140                 $aryargs = csv_explode(',', $args); // #plugin(A,B,C,D)
141         }
142         if (! PKWKEXP_DISABLE_MULTILINE_PLUGIN_HACK) {
143                 if (isset($body)) $aryargs[] = $body;     // #plugin(){{body}}
144         }
145
146         $_digest = $digest;
147         $retvar  = call_user_func_array('plugin_' . $name . '_convert', $aryargs);
148         $digest  = $_digest; // Revert
149
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);
158         } else {
159                 return $retvar;
160         }
161 }
162
163 // Call API 'inline' of the plugin
164 function do_plugin_inline($name, $args, & $body)
165 {
166         global $digest;
167
168         if(do_plugin_init($name) === FALSE)
169                 return '[Plugin init failed: ' . $name . ']';
170
171         if ($args !== '') {
172                 $aryargs = csv_explode(',', $args);
173         } else {
174                 $aryargs = array();
175         }
176
177         // NOTE: A reference of $body is always the last argument
178         $aryargs[] = $body; // func_num_args() != 0
179
180         $_digest = $digest;
181         $retvar  = call_user_func_array('plugin_' . $name . '_inline', $aryargs);
182         $digest  = $_digest; // Revert
183
184         if($retvar === FALSE) {
185                 // Do nothing
186                 return htmlspecialchars('&' . $name . ($args ? '(' . $args . ')' : '') . ';');
187         } else {
188                 return $retvar;
189         }
190 }
191 ?>