OSDN Git Service

Cleanup. Japanese => English
[pukiwiki/pukiwiki.git] / lib / plugin.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // $Id: plugin.php,v 1.6 2004/12/30 13:50:54 henoheno Exp $
4 //
5 // Plugin related functions
6
7 // ¥×¥é¥°¥¤¥óÍѤË̤ÄêµÁ¤Î¥°¥í¡¼¥Ð¥ëÊÑ¿ô¤òÀßÄê
8 function set_plugin_messages($messages)
9 {
10         foreach ($messages as $name=>$val) {
11                 if (! isset($GLOBALS[$name])) $GLOBALS[$name] = $val;
12         }
13 }
14
15 // Check plugin '$name' is here
16 function exist_plugin($name)
17 {
18         static $exists = array();
19
20         $name = strtolower($name);
21         if(isset($exists[$name])) return $exists[$name];
22
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');
27                 return TRUE;
28         } else {
29                 $exists[$name] = FALSE;
30                 return FALSE;
31         }
32 }
33
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;
38 }
39
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;
44 }
45
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;
50 }
51
52 // Do init the plugin
53 function do_plugin_init($name)
54 {
55         static $checked = array();
56
57         if (isset($checked[$name])) return $checked[$name];
58
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);
63         } else {
64                 $checked[$name] = NULL; // Not exist
65         }
66
67         return $checked[$name];
68 }
69
70 // Call API 'action' of the plugin
71 function do_plugin_action($name)
72 {
73         if (! exist_plugin_action($name)) return array();
74
75         if(do_plugin_init($name) === FALSE)
76                 die_message('Plugin init failed: ' . $name);
77
78         $retvar = call_user_func('plugin_' . $name . '_action');
79
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);
85
86         return $retvar;
87 }
88
89 // Call API 'convert' of the plugin
90 function do_plugin_convert($name, $args = '')
91 {
92         global $digest;
93
94         if(do_plugin_init($name) === FALSE)
95                 return '[Plugin init failed: ' . $name . ']';
96
97         if ($args !== '') {
98                 $aryargs = csv_explode(',', $args);
99         } else {
100                 $aryargs = array();
101         }
102
103         $_digest = $digest;
104         $retvar  = call_user_func_array('plugin_' . $name . '_convert', $aryargs);
105         $digest  = $_digest; // Revert
106
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);
115         }
116 }
117
118 // Call API 'inline' of the plugin
119 function do_plugin_inline($name, $args, & $body)
120 {
121         global $digest;
122
123         if(do_plugin_init($name) === FALSE)
124                 return '[Plugin init failed: ' . $name . ']';
125
126         if ($args !== '') {
127                 $aryargs = csv_explode(',', $args);
128         } else {
129                 $aryargs = array();
130         }
131
132         // NOTE: A reference of $body is always the last argument
133         $aryargs[] = & $body; // func_num_args() != 0
134
135         $_digest = $digest;
136         $retvar  = call_user_func_array('plugin_' . $name . '_inline', $aryargs);
137         $digest  = $_digest; // Revert
138
139         if($retvar === FALSE) {
140                 // Do nothing
141                 return htmlspecialchars('&' . $name . ($args ? '(' . $args . ')' : '') . ';');
142         } else {
143                 return $retvar;
144         }
145 }
146 ?>