bug#3212720 Show error message on error.
[phpmyadmin/ayax.git] / libraries / plugin_interface.lib.php
blob48dc97577e37f629d39638f7dfb01c5c6086db4e
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Generic plugin interface.
6 * @package phpMyAdmin
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 preg_match()
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 // In some situations, Mac OS creates a new file for each file
31 // (for example ._csv.php) so the following regexp
32 // matches a file which does not start with a dot but ends
33 // with ".php"
34 if (is_file($plugins_dir . $file) && preg_match('@^[^\.](.)*\.php$@i', $file)) {
35 include $plugins_dir . $file;
39 ksort($plugin_list);
40 return $plugin_list;
43 /**
44 * string PMA_getString(string $name)
46 * returns locale string for $name or $name if no locale is found
48 * @uses $GLOBALS
49 * @param string $name for local string
50 * @return string locale string for $name
52 function PMA_getString($name)
54 return isset($GLOBALS[$name]) ? $GLOBALS[$name] : $name;
57 /**
58 * string PMA_pluginCheckboxCheck(string $section, string $opt)
60 * returns html input tag option 'checked' if plugin $opt should be set by config or request
62 * @uses $_REQUEST
63 * @uses $_GET
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 the form is being repopulated using $_GET data, that is priority
74 if (isset($_GET[$opt]) || !isset($_GET['repopulate']) && ((isset($GLOBALS['timeout_passed']) && $GLOBALS['timeout_passed'] && isset($_REQUEST[$opt])) ||
75 (isset($GLOBALS['cfg'][$section][$opt]) && $GLOBALS['cfg'][$section][$opt]))) {
76 return ' checked="checked"';
78 return '';
81 /**
82 * string PMA_pluginGetDefault(string $section, string $opt)
84 * returns default value for option $opt
86 * @uses htmlspecialchars()
87 * @uses $_REQUEST
88 * @uses $_GET
89 * @uses $GLOBALS['cfg']
90 * @uses $GLOBALS['timeout_passed']
91 * @param string $section name of config section in
92 * $GLOBALS['cfg'][$section] for plugin
93 * @param string $opt name of option
94 * @return string default value for option $opt
96 function PMA_pluginGetDefault($section, $opt)
98 if(isset($_GET[$opt])) { // If the form is being repopulated using $_GET data, that is priority
99 return htmlspecialchars($_GET[$opt]);
100 } elseif (isset($GLOBALS['timeout_passed']) && $GLOBALS['timeout_passed'] && isset($_REQUEST[$opt])) {
101 return htmlspecialchars($_REQUEST[$opt]);
102 } elseif (isset($GLOBALS['cfg'][$section][$opt])) {
103 $matches = array();
104 /* Possibly replace localised texts */
105 if (preg_match_all('/(str[A-Z][A-Za-z0-9]*)/', $GLOBALS['cfg'][$section][$opt], $matches)) {
106 $val = $GLOBALS['cfg'][$section][$opt];
107 foreach($matches[0] as $match) {
108 if (isset($GLOBALS[$match])) {
109 $val = str_replace($match, $GLOBALS[$match], $val);
112 return htmlspecialchars($val);
113 } else {
114 return htmlspecialchars($GLOBALS['cfg'][$section][$opt]);
117 return '';
121 * string PMA_pluginIsActive(string $section, string $opt, string $val)
123 * returns html input tag option 'checked' if option $opt should be set by config or request
125 * @uses $_REQUEST
126 * @uses $GLOBALS['cfg']
127 * @uses $GLOBALS['timeout_passed']
128 * @param string $section name of config section in
129 * $GLOBALS['cfg'][$section] for plugin
130 * @param string $opt name of option
131 * @param string $val value of option to check against
132 * @return string html input tag option 'checked'
134 function PMA_pluginIsActive($section, $opt, $val)
136 if (! empty($GLOBALS['timeout_passed']) && isset($_REQUEST[$opt])) {
137 if ($_REQUEST[$opt] == $val) {
138 return ' checked="checked"';
140 } elseif (isset($GLOBALS['cfg'][$section][$opt]) && $GLOBALS['cfg'][$section][$opt] == $val) {
141 return ' checked="checked"';
143 return '';
147 * string PMA_pluginGetChoice(string $section, string $name, array &$list, string $cfgname)
149 * returns html select form element for plugin choice
150 * and hidden fields denoting whether each plugin must be exported as a file
152 * @uses PMA_pluginGetDefault()
153 * @uses PMA_getString()
154 * @param string $section name of config section in
155 * $GLOBALS['cfg'][$section] for plugin
156 * @param string $name name of select element
157 * @param array &$list array with plugin configuration defined in plugin file
158 * @param string $cfgname name of config value, if none same as $name
159 * @return string html select tag
161 function PMA_pluginGetChoice($section, $name, &$list, $cfgname = NULL)
163 if (!isset($cfgname)) {
164 $cfgname = $name;
166 $ret = '<select id="plugins" name="' . $name . '">';
167 $default = PMA_pluginGetDefault($section, $cfgname);
168 foreach ($list as $plugin_name => $val) {
169 $ret .= '<option';
170 // If the form is being repopulated using $_GET data, that is priority
171 if(isset($_GET[$name]) && $plugin_name == $_GET[$name] || !isset($_GET[$name]) && $plugin_name == $default) {
172 $ret .= ' selected="selected"';
174 $ret .= ' value="' . $plugin_name . '">' . PMA_getString($val['text']) . '</option>' . "\n";
176 $ret .= '</select>' . "\n";
178 // Whether each plugin has to be saved as a file
179 foreach ($list as $plugin_name => $val) {
180 $ret .= '<input type="hidden" id="force_file_' . $plugin_name . '" value="';
181 if(isset($val['force_file'])) {
182 $ret .= 'true';
183 } else {
184 $ret .= 'false';
186 $ret .= '" />'. "\n";
188 return $ret;
192 * string PMA_pluginGetOneOption(string $section, string $plugin_name, string $id, array &$opt)
194 * returns single option in a list element
196 * @uses PMA_getString()
197 * @uses PMA_pluginCheckboxCheck()
198 * @uses PMA_pluginGetDefault()
199 * @param string $section name of config section in
200 * $GLOBALS['cfg'][$section] for plugin
201 * @param string $plugin_name unique plugin name
202 * @param string $id option id
203 * @param array &$opt plugin option details
204 * @return string table row with option
206 function PMA_pluginGetOneOption($section, $plugin_name, $id, &$opt)
208 $ret = "\n";
209 if ($opt['type'] == 'bool') {
210 $ret .= '<li>' . "\n";
211 $ret .= '<input type="checkbox" name="' . $plugin_name . '_' . $opt['name'] . '"'
212 . ' value="something" id="checkbox_' . $plugin_name . '_' . $opt['name'] . '"'
213 . ' ' . PMA_pluginCheckboxCheck($section, $plugin_name . '_' . $opt['name']);
214 if (isset($opt['force'])) {
215 /* Same code is also few lines lower, update both if needed */
216 $ret .= ' onclick="if (!this.checked &amp;&amp; '
217 . '(!document.getElementById(\'checkbox_' . $plugin_name . '_' .$opt['force'] . '\') '
218 . '|| !document.getElementById(\'checkbox_' . $plugin_name . '_' .$opt['force'] . '\').checked)) '
219 . 'return false; else return true;"';
221 $ret .= ' />';
222 $ret .= '<label for="checkbox_' . $plugin_name . '_' . $opt['name'] . '">'
223 . PMA_getString($opt['text']) . '</label>';
224 } elseif ($opt['type'] == 'text') {
225 $ret .= '<li>' . "\n";
226 $ret .= '<label for="text_' . $plugin_name . '_' . $opt['name'] . '" class="desc">'
227 . PMA_getString($opt['text']) . '</label>';
228 $ret .= '<input type="text" name="' . $plugin_name . '_' . $opt['name'] . '"'
229 . ' value="' . PMA_pluginGetDefault($section, $plugin_name . '_' . $opt['name']) . '"'
230 . ' id="text_' . $plugin_name . '_' . $opt['name'] . '"'
231 . (isset($opt['size']) ? ' size="' . $opt['size'] . '"' : '')
232 . (isset($opt['len']) ? ' maxlength="' . $opt['len'] . '"' : '') . ' />';
233 } elseif ($opt['type'] == 'message_only') {
234 $ret .= '<li>' . "\n";
235 $ret .= '<p>' . PMA_getString($opt['text']) . '</p>';
236 } elseif ($opt['type'] == 'select') {
237 $ret .= '<li>' . "\n";
238 $ret .= '<label for="select_' . $plugin_name . '_' . $opt['name'] . '" class="desc">'
239 . PMA_getString($opt['text']) . '</label>';
240 $ret .= '<select name="' . $plugin_name . '_' . $opt['name'] . '"'
241 . ' id="select_' . $plugin_name . '_' . $opt['name'] . '">';
242 $default = PMA_pluginGetDefault($section, $plugin_name . '_' . $opt['name']);
243 foreach($opt['values'] as $key => $val) {
244 $ret .= '<option value="' . $key . '"';
245 if ($key == $default) {
246 $ret .= ' selected="selected"';
248 $ret .= '>' . PMA_getString($val) . '</option>';
250 $ret .= '</select>';
251 } elseif ($opt['type'] == 'radio') {
252 $default = PMA_pluginGetDefault($section, $plugin_name . '_' . $opt['name']);
253 foreach($opt['values'] as $key => $val) {
254 $ret .= '<li><input type="radio" name="' . $plugin_name . '_' . $opt['name'] . '" value="' . $key
255 . '" id="radio_' . $plugin_name . '_' . $opt['name'] . '_' . $key . '"';
256 if($key == $default) {
257 $ret .= 'checked="checked"';
259 $ret .= ' />' . '<label for="radio_' . $plugin_name . '_' . $opt['name'] . '_' . $key . '">'
260 . PMA_getString($val) . '</label></li>';
262 } elseif ($opt['type'] == 'hidden') {
263 $ret .= '<li><input type="hidden" name="' . $plugin_name . '_' . $opt['name'] . '"'
264 . ' value="' . PMA_pluginGetDefault($section, $plugin_name . '_' . $opt['name']) . '"' . ' /></li>';
265 } elseif ($opt['type'] == 'begin_group') {
266 $ret .= '<div class="export_sub_options" id="' . $plugin_name . '_' . $opt['name'] . '">';
267 if (isset($opt['text'])) {
268 $ret .= '<h4>' . PMA_getString($opt['text']) . '</h4>';
270 $ret .= '<ul>';
271 } elseif ($opt['type'] == 'end_group') {
272 $ret .= '</ul></div>';
273 } elseif ($opt['type'] == 'begin_subgroup') {
274 /* each subgroup can have a header, which may also be a form element */
275 $ret .= PMA_pluginGetOneOption($section, $plugin_name, $id, $opt['subgroup_header']) . '<li class="subgroup"><ul';
276 if(isset($opt['subgroup_header']['name'])) {
277 $ret .= ' id="ul_' . $opt['subgroup_header']['name'] . '">';
278 } else {
279 $ret .= '>';
281 } elseif ($opt['type'] == 'end_subgroup') {
282 $ret .= '</ul></li>';
283 } else {
284 /* This should be seen only by plugin writers, so I do not thing this
285 * needs translation. */
286 $ret .= 'UNKNOWN OPTION ' . $opt['type'] . ' IN IMPORT PLUGIN ' . $plugin_name . '!';
288 if (isset($opt['doc'])) {
289 if (count($opt['doc']) == 3) {
290 $ret .= PMA_showMySQLDocu($opt['doc'][0], $opt['doc'][1], false, $opt['doc'][2]);
291 } elseif (count($opt['doc']) == 1) {
292 $ret .= PMA_showDocu($opt['doc'][0]);
293 } else {
294 $ret .= PMA_showMySQLDocu($opt['doc'][0], $opt['doc'][1]);
298 // Close the list element after $opt['doc'] link is displayed
299 if($opt['type'] == 'bool' || $opt['type'] == 'text' || $opt['type'] == 'message_only' || $opt['type'] == 'select') {
300 $ret .= '</li>';
302 $ret .= "\n";
303 return $ret;
307 * string PMA_pluginGetOptions(string $section, array &$list)
309 * return html div with editable options for plugin
311 * @uses PMA_getString()
312 * @uses PMA_pluginGetOneOption()
313 * @uses PMA_pluginGetDefault();
314 * @param string $section name of config section in $GLOBALS['cfg'][$section]
315 * @param array &$list array with plugin configuration defined in plugin file
316 * @return string html fieldset with plugin options
318 function PMA_pluginGetOptions($section, &$list)
320 $ret = '';
321 $default = PMA_pluginGetDefault('Export', 'format');
322 // Options for plugins that support them
323 foreach ($list as $plugin_name => $val) {
324 $ret .= '<div id="' . $plugin_name . '_options" class="format_specific_options">';
325 $count = 0;
326 $ret .= '<h3>' . PMA_getString($val['text']) . '</h3>';
327 if (isset($val['options']) && count($val['options']) > 0) {
328 foreach ($val['options'] as $id => $opt) {
329 if ($opt['type'] != 'hidden' && $opt['type'] != 'begin_group' && $opt['type'] != 'end_group' && $opt['type'] != 'begin_subgroup' && $opt['type'] != 'end_subgroup') {
330 $count++;
332 $ret .= PMA_pluginGetOneOption($section, $plugin_name, $id, $opt);
335 if ($count == 0) {
336 $ret .= '<p>' . __('This format has no options') . '</p>';
338 $ret .= '</div>';
340 return $ret;