2.11.1.2 release
[phpmyadmin/arisferyanto.git] / libraries / plugin_interface.lib.php
blob31f3aab33c08e76b3b82acdb987ce09083a832e8
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Generic plugin interface.
6 * @version $Id$
7 */
9 /**
10 * array PMA_getPlugins(string $plugins_dir, mixed $plugin_param)
12 * Reads all plugin information from directory $plugins_dir.
14 * @uses ksort()
15 * @uses opendir()
16 * @uses readdir()
17 * @uses is_file()
18 * @uses eregi()
19 * @param string $plugins_dir directrory with plugins
20 * @param mixed $plugin_param parameter to plugin by which they can decide whether they can work
21 * @return array list of plugins
23 function PMA_getPlugins($plugins_dir, $plugin_param)
25 /* Scan for plugins */
26 $plugin_list = array();
27 if ($handle = @opendir($plugins_dir)) {
28 $is_first = 0;
29 while ($file = @readdir($handle)) {
30 if (is_file($plugins_dir . $file) && eregi('\.php$', $file)) {
31 include $plugins_dir . $file;
35 ksort($plugin_list);
36 return $plugin_list;
39 /**
40 * string PMA_getString(string $name)
42 * returns locale string for $name or $name if no locale is found
44 * @uses $GLOBALS
45 * @param string $name for local string
46 * @return string locale string for $name
48 function PMA_getString($name)
50 return isset($GLOBALS[$name]) ? $GLOBALS[$name] : $name;
53 /**
54 * string PMA_pluginCheckboxCheck(string $section, string $opt)
56 * returns html input tag option 'checked' if plugin $opt should be set by config or request
58 * @uses $_REQUEST
59 * @uses $GLOBALS['cfg']
60 * @uses $GLOBALS['timeout_passed']
61 * @param string $section name of config section in
62 * $GLOBALS['cfg'][$section] for plugin
63 * @param string $opt name of option
64 * @return string hmtl input tag option 'checked'
66 function PMA_pluginCheckboxCheck($section, $opt)
68 if ((isset($GLOBALS['timeout_passed']) && $GLOBALS['timeout_passed'] && isset($_REQUEST[$opt])) ||
69 (isset($GLOBALS['cfg'][$section][$opt]) && $GLOBALS['cfg'][$section][$opt])) {
70 return ' checked="checked"';
72 return '';
75 /**
76 * string PMA_pluginGetDefault(string $section, string $opt)
78 * returns default value for option $opt
80 * @uses htmlspecialchars()
81 * @uses $_REQUEST
82 * @uses $GLOBALS['cfg']
83 * @uses $GLOBALS['timeout_passed']
84 * @param string $section name of config section in
85 * $GLOBALS['cfg'][$section] for plugin
86 * @param string $opt name of option
87 * @return string default value for option $opt
89 function PMA_pluginGetDefault($section, $opt)
91 if (isset($GLOBALS['timeout_passed']) && $GLOBALS['timeout_passed'] && isset($_REQUEST[$opt])) {
92 return htmlspecialchars($_REQUEST[$opt]);
93 } elseif (isset($GLOBALS['cfg'][$section][$opt])) {
94 $matches = array();
95 /* Possibly replace localised texts */
96 if (preg_match_all('/(str[A-Z][A-Za-z0-9]*)/', $GLOBALS['cfg'][$section][$opt], $matches)) {
97 $val = $GLOBALS['cfg'][$section][$opt];
98 foreach($matches[0] as $match) {
99 if (isset($GLOBALS[$match])) {
100 $val = str_replace($match, $GLOBALS[$match], $val);
103 return htmlspecialchars($val);
104 } else {
105 return htmlspecialchars($GLOBALS['cfg'][$section][$opt]);
108 return '';
112 * string PMA_pluginIsActive(string $section, string $opt, string $val)
114 * returns html input tag option 'checked' if option $opt should be set by config or request
116 * @uses $_REQUEST
117 * @uses $GLOBALS['cfg']
118 * @uses $GLOBALS['timeout_passed']
119 * @param string $section name of config section in
120 * $GLOBALS['cfg'][$section] for plugin
121 * @param string $opt name of option
122 * @param string $val value of option to check against
123 * @return string html input tag option 'checked'
125 function PMA_pluginIsActive($section, $opt, $val)
127 if (! empty($GLOBALS['timeout_passed']) && isset($_REQUEST[$opt])) {
128 if ($_REQUEST[$opt] == $val) {
129 return ' checked="checked"';
131 } elseif (isset($GLOBALS['cfg'][$section][$opt]) && $GLOBALS['cfg'][$section][$opt] == $val) {
132 return ' checked="checked"';
134 return '';
138 * string PMA_pluginGetChoice(string $section, string $name, array &$list, string $cfgname)
140 * returns html radio form element for plugin choice
142 * @uses PMA_pluginIsActive()
143 * @uses PMA_getString()
144 * @param string $section name of config section in
145 * $GLOBALS['cfg'][$section] for plugin
146 * @param string $name name of radio element
147 * @param array &$list array with plugin configuration defined in plugin file
148 * @param string $cfgname name of config value, if none same as $name
149 * @return string html input radio tag
151 function PMA_pluginGetChoice($section, $name, &$list, $cfgname = NULL)
153 if (!isset($cfgname)) {
154 $cfgname = $name;
156 $ret = '';
157 foreach ($list as $plugin_name => $val) {
158 $ret .= '<!-- ' . $plugin_name . ' -->' . "\n";
159 $ret .= '<input type="radio" name="' . $name . '" value="' . $plugin_name . '"'
160 . ' id="radio_plugin_' . $plugin_name . '"'
161 . ' onclick="if(this.checked) { hide_them_all();';
162 if (isset($val['force_file'])) {
163 $ret .= 'document.getElementById(\'checkbox_dump_asfile\').checked = true;';
165 $ret .= ' document.getElementById(\'' . $plugin_name . '_options\').style.display = \'block\'; };'
166 .' return true"'
167 . PMA_pluginIsActive($section, $cfgname, $plugin_name) . '/>' . "\n";
168 $ret .= '<label for="radio_plugin_' . $plugin_name . '">'
169 . PMA_getString($val['text']) . '</label>' . "\n";
170 $ret .= '<br /><br />' . "\n";
172 return $ret;
176 * string PMA_pluginGetOneOption(string $section, string $plugin_name, string $id, array &$opt)
178 * returns single option in a table row
180 * @uses PMA_getString()
181 * @uses PMA_pluginCheckboxCheck()
182 * @uses PMA_pluginGetDefault()
183 * @param string $section name of config section in
184 * $GLOBALS['cfg'][$section] for plugin
185 * @param string $plugin_name unique plugin name
186 * @param string $id option id
187 * @param array &$opt plugin option details
188 * @return string table row with option
190 function PMA_pluginGetOneOption($section, $plugin_name, $id, &$opt)
192 $ret = "\n";
193 if ($opt['type'] == 'bool') {
194 $ret .= '<div class="formelementrow">' . "\n";
195 $ret .= '<input type="checkbox" name="' . $plugin_name . '_' . $opt['name'] . '"'
196 . ' value="something" id="checkbox_' . $plugin_name . '_' . $opt['name'] . '"'
197 . ' ' . PMA_pluginCheckboxCheck($section, $plugin_name . '_' . $opt['name']);
198 if (isset($opt['force'])) {
199 /* Same code is also few lines lower, update both if needed */
200 $ret .= ' onclick="if (!this.checked &amp;&amp; '
201 . '(!document.getElementById(\'checkbox_' . $plugin_name . '_' .$opt['force'] . '\') '
202 . '|| !document.getElementById(\'checkbox_' . $plugin_name . '_' .$opt['force'] . '\').checked)) '
203 . 'return false; else return true;"';
205 $ret .= ' />';
206 $ret .= '<label for="checkbox_' . $plugin_name . '_' . $opt['name'] . '">'
207 . PMA_getString($opt['text']) . '</label>';
208 $ret .= '</div>' . "\n";
209 } elseif ($opt['type'] == 'text') {
210 $ret .= '<div class="formelementrow">' . "\n";
211 $ret .= '<label for="text_' . $plugin_name . '_' . $opt['name'] . '" class="desc">'
212 . PMA_getString($opt['text']) . '</label>';
213 $ret .= '<input type="text" name="' . $plugin_name . '_' . $opt['name'] . '"'
214 . ' value="' . PMA_pluginGetDefault($section, $plugin_name . '_' . $opt['name']) . '"'
215 . ' id="text_' . $plugin_name . '_' . $opt['name'] . '"'
216 . (isset($opt['size']) ? ' size="' . $opt['size'] . '"' : '')
217 . (isset($opt['len']) ? ' maxlength="' . $opt['len'] . '"' : '') . ' />';
218 $ret .= '</div>' . "\n";
219 } elseif ($opt['type'] == 'message_only') {
220 $ret .= '<div class="formelementrow">' . "\n";
221 $ret .= '<label for="text_' . $plugin_name . '_' . $opt['name'] . '" class="desc">'
222 . PMA_getString($opt['text']) . '</label>';
223 $ret .= '</div>' . "\n";
224 } elseif ($opt['type'] == 'select') {
225 $ret .= '<div class="formelementrow">' . "\n";
226 $ret .= '<label for="select_' . $plugin_name . '_' . $opt['name'] . '" class="desc">'
227 . PMA_getString($opt['text']) . '</label>';
228 $ret .= '<select name="' . $plugin_name . '_' . $opt['name'] . '"'
229 . ' id="select_' . $plugin_name . '_' . $opt['name'] . '">';
230 $default = PMA_pluginGetDefault($section, $plugin_name . '_' . $opt['name']);
231 foreach($opt['values'] as $key => $val) {
232 $ret .= '<option name="' . $key . '"';
233 if ($key == $default) {
234 $ret .= ' selected="selected"';
236 $ret .= '>' . PMA_getString($val) . '</option>';
238 $ret .= '</select>';
239 $ret .= '</div>' . "\n";
240 } elseif ($opt['type'] == 'hidden') {
241 $ret .= '<input type="hidden" name="' . $plugin_name . '_' . $opt['name'] . '"'
242 . ' value="' . PMA_pluginGetDefault($section, $plugin_name . '_' . $opt['name']) . '"' . ' />';
243 } elseif ($opt['type'] == 'bgroup') {
244 $ret .= '<fieldset><legend>';
245 /* No checkbox without name */
246 if (!empty($opt['name'])) {
247 $ret .= '<input type="checkbox" name="' . $plugin_name . '_' . $opt['name'] . '"'
248 . ' value="something" id="checkbox_' . $plugin_name . '_' . $opt['name'] . '"'
249 . ' ' . PMA_pluginCheckboxCheck($section, $plugin_name . '_' . $opt['name']);
250 if (isset($opt['force'])) {
251 /* Same code is also few lines higher, update both if needed */
252 $ret .= ' onclick="if (!this.checked &amp;&amp; '
253 . '(!document.getElementById(\'checkbox_' . $plugin_name . '_' .$opt['force'] . '\') '
254 . '|| !document.getElementById(\'checkbox_' . $plugin_name . '_' .$opt['force'] . '\').checked)) '
255 . 'return false; else return true;"';
257 $ret .= ' />';
258 $ret .= '<label for="checkbox_' . $plugin_name . '_' . $opt['name'] . '">'
259 . PMA_getString($opt['text']) . '</label>';
260 } else {
261 $ret .= PMA_getString($opt['text']);
263 $ret .= '</legend>';
264 } elseif ($opt['type'] == 'egroup') {
265 $ret .= '</fieldset>';
266 } else {
267 /* This should be seen only by plugin writers, so I do not thing this
268 * needs translation. */
269 $ret .= 'UNKNOWN OPTION ' . $opt['type'] . ' IN IMPORT PLUGIN ' . $plugin_name . '!';
271 if (isset($opt['doc'])) {
272 $ret .= PMA_showMySQLDocu($opt['doc'][0], $opt['doc'][1]);
274 $ret .= "\n";
275 return $ret;
279 * string PMA_pluginGetOptions(string $section, array &$list)
281 * return html fieldset with editable options for plugin
283 * @uses PMA_getString()
284 * @uses PMA_pluginGetOneOption()
285 * @param string $section name of config section in $GLOBALS['cfg'][$section]
286 * @param array &$list array with plugin configuration defined in plugin file
287 * @return string html fieldset with plugin options
289 function PMA_pluginGetOptions($section, &$list)
291 $ret = '';
292 // Options for plugins that support them
293 foreach ($list as $plugin_name => $val) {
294 $ret .= '<fieldset id="' . $plugin_name . '_options" class="options">';
295 $ret .= '<legend>' . PMA_getString($val['options_text']) . '</legend>';
296 $count = 0;
297 if (isset($val['options']) && count($val['options']) > 0) {
298 foreach ($val['options'] as $id => $opt) {
299 if ($opt['type'] != 'hidden') $count++;
300 $ret .= PMA_pluginGetOneOption($section, $plugin_name, $id, $opt);
303 if ($count == 0) {
304 $ret .= $GLOBALS['strNoOptions'];
306 $ret .= '</fieldset>';
308 return $ret;
312 * string PMA_pluginGetJavascript(array &$list)
314 * return html/javascript code which is needed for handling plugin stuff
316 * @param array &$list array with plugin configuration defined in plugin file
317 * @return string html fieldset with plugin options
319 function PMA_pluginGetJavascript(&$list) {
320 $ret = '
321 <script type="text/javascript">
322 //<![CDATA[
323 function hide_them_all() {
325 foreach ($list as $plugin_name => $val) {
326 $ret .= 'document.getElementById("' . $plugin_name . '_options").style.display = "none";' . "\n";
328 $ret .= '
331 function init_options() {
332 hide_them_all();
334 foreach ($list as $plugin_name => $val) {
335 $ret .= 'if (document.getElementById("radio_plugin_' . $plugin_name . '").checked) {' . "\n";
336 if (isset($val['force_file'])) {
337 $ret .= 'document.getElementById(\'checkbox_dump_asfile\').checked = true;' . "\n";
339 $ret .= 'document.getElementById("' . $plugin_name . '_options").style.display = "block";' . "\n";
340 $ret .= ' } else ' . "\n";
342 $ret .= '
348 function match_file(fname) {
349 farr = fname.toLowerCase().split(".");
350 if (farr.length != 0) {
351 len = farr.length
352 if (farr[len - 1] == "gz" || farr[len - 1] == "bz2" || farr[len -1] == "zip") len--;
353 switch (farr[len - 1]) {
355 foreach ($list as $plugin_name => $val) {
356 $ret .= 'case "' . $val['extension'] . '" :';
357 $ret .= 'document.getElementById("radio_plugin_' . $plugin_name . '").checked = true;';
358 $ret .= 'init_options();';
359 $ret .= 'break;' . "\n";
361 $ret .='
365 //]]>
366 </script>
368 return $ret;