Action should always be displayed here
[phpmyadmin/dkf.git] / libraries / plugin_interface.lib.php
blob38e8153756769ebb71b49a24a442011d79c0894f
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Generic plugin interface.
6 * @version $Id$
7 * @package phpMyAdmin
8 */
10 /**
11 * array PMA_getPlugins(string $plugins_dir, mixed $plugin_param)
13 * Reads all plugin information from directory $plugins_dir.
15 * @uses ksort()
16 * @uses opendir()
17 * @uses readdir()
18 * @uses is_file()
19 * @uses preg_match()
20 * @param string $plugins_dir directrory with plugins
21 * @param mixed $plugin_param parameter to plugin by which they can decide whether they can work
22 * @return array list of plugins
24 function PMA_getPlugins($plugins_dir, $plugin_param)
26 /* Scan for plugins */
27 $plugin_list = array();
28 if ($handle = @opendir($plugins_dir)) {
29 $is_first = 0;
30 while ($file = @readdir($handle)) {
31 // In some situations, Mac OS creates a new file for each file
32 // (for example ._csv.php) so the following regexp
33 // matches a file which does not start with a dot but ends
34 // with ".php"
35 if (is_file($plugins_dir . $file) && preg_match('@^[^\.](.)*\.php$@i', $file)) {
36 include $plugins_dir . $file;
40 ksort($plugin_list);
41 return $plugin_list;
44 /**
45 * string PMA_getString(string $name)
47 * returns locale string for $name or $name if no locale is found
49 * @uses $GLOBALS
50 * @param string $name for local string
51 * @return string locale string for $name
53 function PMA_getString($name)
55 return isset($GLOBALS[$name]) ? $GLOBALS[$name] : $name;
58 /**
59 * string PMA_pluginCheckboxCheck(string $section, string $opt)
61 * returns html input tag option 'checked' if plugin $opt should be set by config or request
63 * @uses $_REQUEST
64 * @uses $GLOBALS['cfg']
65 * @uses $GLOBALS['timeout_passed']
66 * @param string $section name of config section in
67 * $GLOBALS['cfg'][$section] for plugin
68 * @param string $opt name of option
69 * @return string hmtl input tag option 'checked'
71 function PMA_pluginCheckboxCheck($section, $opt)
73 if ((isset($GLOBALS['timeout_passed']) && $GLOBALS['timeout_passed'] && isset($_REQUEST[$opt])) ||
74 (isset($GLOBALS['cfg'][$section][$opt]) && $GLOBALS['cfg'][$section][$opt])) {
75 return ' checked="checked"';
77 return '';
80 /**
81 * string PMA_pluginGetDefault(string $section, string $opt)
83 * returns default value for option $opt
85 * @uses htmlspecialchars()
86 * @uses $_REQUEST
87 * @uses $GLOBALS['cfg']
88 * @uses $GLOBALS['timeout_passed']
89 * @param string $section name of config section in
90 * $GLOBALS['cfg'][$section] for plugin
91 * @param string $opt name of option
92 * @return string default value for option $opt
94 function PMA_pluginGetDefault($section, $opt)
96 if (isset($GLOBALS['timeout_passed']) && $GLOBALS['timeout_passed'] && isset($_REQUEST[$opt])) {
97 return htmlspecialchars($_REQUEST[$opt]);
98 } elseif (isset($GLOBALS['cfg'][$section][$opt])) {
99 $matches = array();
100 /* Possibly replace localised texts */
101 if (preg_match_all('/(str[A-Z][A-Za-z0-9]*)/', $GLOBALS['cfg'][$section][$opt], $matches)) {
102 $val = $GLOBALS['cfg'][$section][$opt];
103 foreach($matches[0] as $match) {
104 if (isset($GLOBALS[$match])) {
105 $val = str_replace($match, $GLOBALS[$match], $val);
108 return htmlspecialchars($val);
109 } else {
110 return htmlspecialchars($GLOBALS['cfg'][$section][$opt]);
113 return '';
117 * string PMA_pluginIsActive(string $section, string $opt, string $val)
119 * returns html input tag option 'checked' if option $opt should be set by config or request
121 * @uses $_REQUEST
122 * @uses $GLOBALS['cfg']
123 * @uses $GLOBALS['timeout_passed']
124 * @param string $section name of config section in
125 * $GLOBALS['cfg'][$section] for plugin
126 * @param string $opt name of option
127 * @param string $val value of option to check against
128 * @return string html input tag option 'checked'
130 function PMA_pluginIsActive($section, $opt, $val)
132 if (! empty($GLOBALS['timeout_passed']) && isset($_REQUEST[$opt])) {
133 if ($_REQUEST[$opt] == $val) {
134 return ' checked="checked"';
136 } elseif (isset($GLOBALS['cfg'][$section][$opt]) && $GLOBALS['cfg'][$section][$opt] == $val) {
137 return ' checked="checked"';
139 return '';
143 * string PMA_pluginGetChoice(string $section, string $name, array &$list, string $cfgname)
145 * returns html radio form element for plugin choice
147 * @uses PMA_pluginIsActive()
148 * @uses PMA_getString()
149 * @param string $section name of config section in
150 * $GLOBALS['cfg'][$section] for plugin
151 * @param string $name name of radio element
152 * @param array &$list array with plugin configuration defined in plugin file
153 * @param string $cfgname name of config value, if none same as $name
154 * @return string html input radio tag
156 function PMA_pluginGetChoice($section, $name, &$list, $cfgname = NULL)
158 if (!isset($cfgname)) {
159 $cfgname = $name;
161 $ret = '';
162 foreach ($list as $plugin_name => $val) {
163 $ret .= '<!-- ' . $plugin_name . ' -->' . "\n";
164 $ret .= '<input type="radio" name="' . $name . '" value="' . $plugin_name . '"'
165 . ' id="radio_plugin_' . $plugin_name . '"'
166 . ' onclick="if(this.checked) { hide_them_all();';
167 if (isset($val['force_file'])) {
168 $ret .= 'document.getElementById(\'checkbox_dump_asfile\').checked = true;';
170 $ret .= ' document.getElementById(\'' . $plugin_name . '_options\').style.display = \'block\'; };'
171 .' return true"'
172 . PMA_pluginIsActive($section, $cfgname, $plugin_name) . '/>' . "\n";
173 $ret .= '<label for="radio_plugin_' . $plugin_name . '">'
174 . PMA_getString($val['text']) . '</label>' . "\n";
175 $ret .= '<br />' . "\n";
177 return $ret;
181 * string PMA_pluginGetOneOption(string $section, string $plugin_name, string $id, array &$opt)
183 * returns single option in a table row
185 * @uses PMA_getString()
186 * @uses PMA_pluginCheckboxCheck()
187 * @uses PMA_pluginGetDefault()
188 * @param string $section name of config section in
189 * $GLOBALS['cfg'][$section] for plugin
190 * @param string $plugin_name unique plugin name
191 * @param string $id option id
192 * @param array &$opt plugin option details
193 * @return string table row with option
195 function PMA_pluginGetOneOption($section, $plugin_name, $id, &$opt)
197 $ret = "\n";
198 if ($opt['type'] == 'bool') {
199 $ret .= '<div class="formelementrow">' . "\n";
200 $ret .= '<input type="checkbox" name="' . $plugin_name . '_' . $opt['name'] . '"'
201 . ' value="something" id="checkbox_' . $plugin_name . '_' . $opt['name'] . '"'
202 . ' ' . PMA_pluginCheckboxCheck($section, $plugin_name . '_' . $opt['name']);
203 if (isset($opt['force'])) {
204 /* Same code is also few lines lower, update both if needed */
205 $ret .= ' onclick="if (!this.checked &amp;&amp; '
206 . '(!document.getElementById(\'checkbox_' . $plugin_name . '_' .$opt['force'] . '\') '
207 . '|| !document.getElementById(\'checkbox_' . $plugin_name . '_' .$opt['force'] . '\').checked)) '
208 . 'return false; else return true;"';
210 $ret .= ' />';
211 $ret .= '<label for="checkbox_' . $plugin_name . '_' . $opt['name'] . '">'
212 . PMA_getString($opt['text']) . '</label>';
213 $ret .= '</div>' . "\n";
214 } elseif ($opt['type'] == 'text') {
215 $ret .= '<div class="formelementrow">' . "\n";
216 $ret .= '<label for="text_' . $plugin_name . '_' . $opt['name'] . '" class="desc">'
217 . PMA_getString($opt['text']) . '</label>';
218 $ret .= '<input type="text" name="' . $plugin_name . '_' . $opt['name'] . '"'
219 . ' value="' . PMA_pluginGetDefault($section, $plugin_name . '_' . $opt['name']) . '"'
220 . ' id="text_' . $plugin_name . '_' . $opt['name'] . '"'
221 . (isset($opt['size']) ? ' size="' . $opt['size'] . '"' : '')
222 . (isset($opt['len']) ? ' maxlength="' . $opt['len'] . '"' : '') . ' />';
223 $ret .= '</div>' . "\n";
224 } elseif ($opt['type'] == 'message_only') {
225 $ret .= '<div class="formelementrow">' . "\n";
226 $ret .= '<p class="desc">' . PMA_getString($opt['text']) . '</p>';
227 $ret .= '</div>' . "\n";
228 } elseif ($opt['type'] == 'select') {
229 $ret .= '<div class="formelementrow">' . "\n";
230 $ret .= '<label for="select_' . $plugin_name . '_' . $opt['name'] . '" class="desc">'
231 . PMA_getString($opt['text']) . '</label>';
232 $ret .= '<select name="' . $plugin_name . '_' . $opt['name'] . '"'
233 . ' id="select_' . $plugin_name . '_' . $opt['name'] . '">';
234 $default = PMA_pluginGetDefault($section, $plugin_name . '_' . $opt['name']);
235 foreach($opt['values'] as $key => $val) {
236 $ret .= '<option value="' . $key . '"';
237 if ($key == $default) {
238 $ret .= ' selected="selected"';
240 $ret .= '>' . PMA_getString($val) . '</option>';
242 $ret .= '</select>';
243 $ret .= '</div>' . "\n";
244 } elseif ($opt['type'] == 'hidden') {
245 $ret .= '<input type="hidden" name="' . $plugin_name . '_' . $opt['name'] . '"'
246 . ' value="' . PMA_pluginGetDefault($section, $plugin_name . '_' . $opt['name']) . '"' . ' />';
247 } elseif ($opt['type'] == 'bgroup') {
248 $ret .= '<fieldset><legend>';
249 /* No checkbox without name */
250 if (!empty($opt['name'])) {
251 $ret .= '<input type="checkbox" name="' . $plugin_name . '_' . $opt['name'] . '"'
252 . ' value="something" id="checkbox_' . $plugin_name . '_' . $opt['name'] . '"'
253 . ' ' . PMA_pluginCheckboxCheck($section, $plugin_name . '_' . $opt['name']);
254 if (isset($opt['force'])) {
255 /* Same code is also few lines higher, update both if needed */
256 $ret .= ' onclick="if (!this.checked &amp;&amp; '
257 . '(!document.getElementById(\'checkbox_' . $plugin_name . '_' .$opt['force'] . '\') '
258 . '|| !document.getElementById(\'checkbox_' . $plugin_name . '_' .$opt['force'] . '\').checked)) '
259 . 'return false; else return true;"';
261 $ret .= ' />';
262 $ret .= '<label for="checkbox_' . $plugin_name . '_' . $opt['name'] . '">'
263 . PMA_getString($opt['text']) . '</label>';
264 } else {
265 $ret .= PMA_getString($opt['text']);
267 $ret .= '</legend>';
268 } elseif ($opt['type'] == 'egroup') {
269 $ret .= '</fieldset>';
270 } else {
271 /* This should be seen only by plugin writers, so I do not thing this
272 * needs translation. */
273 $ret .= 'UNKNOWN OPTION ' . $opt['type'] . ' IN IMPORT PLUGIN ' . $plugin_name . '!';
275 if (isset($opt['doc'])) {
276 if (count($opt['doc']) == 3) {
277 $ret .= PMA_showMySQLDocu($opt['doc'][0], $opt['doc'][1], false, $opt['doc'][2]);
278 } else {
279 $ret .= PMA_showMySQLDocu($opt['doc'][0], $opt['doc'][1]);
282 $ret .= "\n";
283 return $ret;
287 * string PMA_pluginGetOptions(string $section, array &$list)
289 * return html fieldset with editable options for plugin
291 * @uses PMA_getString()
292 * @uses PMA_pluginGetOneOption()
293 * @param string $section name of config section in $GLOBALS['cfg'][$section]
294 * @param array &$list array with plugin configuration defined in plugin file
295 * @return string html fieldset with plugin options
297 function PMA_pluginGetOptions($section, &$list)
299 $ret = '';
300 // Options for plugins that support them
301 foreach ($list as $plugin_name => $val) {
302 $ret .= '<fieldset id="' . $plugin_name . '_options" class="options">';
303 $ret .= '<legend>' . PMA_getString($val['options_text']) . '</legend>';
304 $count = 0;
305 if (isset($val['options']) && count($val['options']) > 0) {
306 foreach ($val['options'] as $id => $opt) {
307 if ($opt['type'] != 'hidden') $count++;
308 $ret .= PMA_pluginGetOneOption($section, $plugin_name, $id, $opt);
311 if ($count == 0) {
312 $ret .= $GLOBALS['strNoOptions'];
314 $ret .= '</fieldset>';
316 return $ret;
320 * string PMA_pluginGetJavascript(array &$list)
322 * return html/javascript code which is needed for handling plugin stuff
324 * @param array &$list array with plugin configuration defined in plugin file
325 * @return string html fieldset with plugin options
327 function PMA_pluginGetJavascript(&$list) {
328 $ret = '
329 <script type="text/javascript">
330 //<![CDATA[
331 function hide_them_all() {
333 foreach ($list as $plugin_name => $val) {
334 $ret .= 'document.getElementById("' . $plugin_name . '_options").style.display = "none";' . "\n";
336 $ret .= '
339 function init_options() {
340 hide_them_all();
342 foreach ($list as $plugin_name => $val) {
343 $ret .= 'if (document.getElementById("radio_plugin_' . $plugin_name . '").checked) {' . "\n";
344 if (isset($val['force_file'])) {
345 $ret .= 'document.getElementById(\'checkbox_dump_asfile\').checked = true;' . "\n";
347 $ret .= 'document.getElementById("' . $plugin_name . '_options").style.display = "block";' . "\n";
348 $ret .= ' } else ' . "\n";
350 $ret .= '
356 function match_file(fname) {
357 farr = fname.toLowerCase().split(".");
358 if (farr.length != 0) {
359 len = farr.length
360 if (farr[len - 1] == "gz" || farr[len - 1] == "bz2" || farr[len -1] == "zip") len--;
361 switch (farr[len - 1]) {
363 foreach ($list as $plugin_name => $val) {
364 $ret .= 'case "' . $val['extension'] . '" :';
365 $ret .= 'document.getElementById("radio_plugin_' . $plugin_name . '").checked = true;';
366 $ret .= 'init_options();';
367 $ret .= 'break;' . "\n";
369 $ret .='
373 //]]>
374 </script>
376 return $ret;