Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / plugin_interface.lib.php
blobb4fe7f6d67930b87da5a6814a38aafe91013b469
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Generic plugin interface.
6 * @package PhpMyAdmin
7 */
9 /**
10 * Includes and instantiates the specified plugin type for a certain format
12 * @param string $plugin_type the type of the plugin (import, export, etc)
13 * @param string $plugin_format the format of the plugin (sql, xml, et )
14 * @param string $plugins_dir directrory with plugins
15 * @param mixed $plugin_param parameter to plugin by which they can
16 * decide whether they can work
18 * @return new plugin instance
20 function PMA_getPlugin(
21 $plugin_type,
22 $plugin_format,
23 $plugins_dir,
24 $plugin_param = false
25 ) {
26 $GLOBALS['plugin_param'] = $plugin_param;
27 $class_name = strtoupper($plugin_type[0])
28 . strtolower(substr($plugin_type, 1))
29 . strtoupper($plugin_format[0])
30 . strtolower(substr($plugin_format, 1));
31 $file = $class_name . ".class.php";
32 if (is_file($plugins_dir . $file)) {
33 include_once $plugins_dir . $file;
34 return new $class_name;
37 return null;
40 /**
41 * Reads all plugin information from directory $plugins_dir
43 * @param string $plugin_type the type of the plugin (import, export, etc)
44 * @param string $plugins_dir directrory with plugins
45 * @param mixed $plugin_param parameter to plugin by which they can
46 * decide whether they can work
48 * @return array list of plugin instances
50 function PMA_getPlugins($plugin_type, $plugins_dir, $plugin_param)
52 $GLOBALS['plugin_param'] = $plugin_param;
53 /* Scan for plugins */
54 $plugin_list = array();
55 if ($handle = @opendir($plugins_dir)) {
56 while ($file = @readdir($handle)) {
57 // In some situations, Mac OS creates a new file for each file
58 // (for example ._csv.php) so the following regexp
59 // matches a file which does not start with a dot but ends
60 // with ".php"
61 $class_type = strtoupper($plugin_type[0])
62 . strtolower(substr($plugin_type, 1));
63 if (is_file($plugins_dir . $file)
64 && preg_match(
65 '@^' . $class_type . '(.+)\.class\.php$@i',
66 $file,
67 $matches
69 ) {
70 $GLOBALS['skip_import'] = false;
71 include_once $plugins_dir . $file;
72 if (! $GLOBALS['skip_import']) {
73 $class_name = $class_type . $matches[1];
74 $plugin_list [] = new $class_name;
79 ksort($plugin_list);
80 return $plugin_list;
83 /**
84 * Returns locale string for $name or $name if no locale is found
86 * @param string $name for local string
88 * @return string locale string for $name
90 function PMA_getString($name)
92 return isset($GLOBALS[$name]) ? $GLOBALS[$name] : $name;
95 /**
96 * Returns html input tag option 'checked' if plugin $opt
97 * should be set by config or request
99 * @param string $section name of config section in
100 * $GLOBALS['cfg'][$section] for plugin
101 * @param string $opt name of option
103 * @return string hmtl input tag option 'checked'
105 function PMA_pluginCheckboxCheck($section, $opt)
107 // If the form is being repopulated using $_GET data, that is priority
108 if (isset($_GET[$opt])
109 || ! isset($_GET['repopulate'])
110 && ((isset($GLOBALS['timeout_passed'])
111 && $GLOBALS['timeout_passed']
112 && isset($_REQUEST[$opt]))
113 || (isset($GLOBALS['cfg'][$section][$opt])
114 && $GLOBALS['cfg'][$section][$opt]))
116 return ' checked="checked"';
118 return '';
122 * Returns default value for option $opt
124 * @param string $section name of config section in
125 * $GLOBALS['cfg'][$section] for plugin
126 * @param string $opt name of option
128 * @return string default value for option $opt
130 function PMA_pluginGetDefault($section, $opt)
132 if (isset($_GET[$opt])) {
133 // If the form is being repopulated using $_GET data, that is priority
134 return htmlspecialchars($_GET[$opt]);
135 } elseif (isset($GLOBALS['timeout_passed'])
136 && $GLOBALS['timeout_passed']
137 && isset($_REQUEST[$opt])) {
138 return htmlspecialchars($_REQUEST[$opt]);
139 } elseif (isset($GLOBALS['cfg'][$section][$opt])) {
140 $matches = array();
141 /* Possibly replace localised texts */
142 if (preg_match_all(
143 '/(str[A-Z][A-Za-z0-9]*)/',
144 $GLOBALS['cfg'][$section][$opt],
145 $matches
146 )) {
147 $val = $GLOBALS['cfg'][$section][$opt];
148 foreach ($matches[0] as $match) {
149 if (isset($GLOBALS[$match])) {
150 $val = str_replace($match, $GLOBALS[$match], $val);
153 return htmlspecialchars($val);
154 } else {
155 return htmlspecialchars($GLOBALS['cfg'][$section][$opt]);
158 return '';
162 * Returns html select form element for plugin choice
163 * and hidden fields denoting whether each plugin must be exported as a file
165 * @param string $section name of config section in
166 * $GLOBALS['cfg'][$section] for plugin
167 * @param string $name name of select element
168 * @param array &$list array with plugin instances
169 * @param string $cfgname name of config value, if none same as $name
171 * @return string html select tag
173 function PMA_pluginGetChoice($section, $name, &$list, $cfgname = null)
175 if (! isset($cfgname)) {
176 $cfgname = $name;
178 $ret = '<select id="plugins" name="' . $name . '">';
179 $default = PMA_pluginGetDefault($section, $cfgname);
180 foreach ($list as $plugin) {
181 $plugin_name = strtolower(substr(get_class($plugin), strlen($section)));
182 $ret .= '<option';
183 // If the form is being repopulated using $_GET data, that is priority
184 if (isset($_GET[$name])
185 && $plugin_name == $_GET[$name]
186 || ! isset($_GET[$name])
187 && $plugin_name == $default
189 $ret .= ' selected="selected"';
192 $properties = $plugin->getProperties();
193 $text = null;
194 if ($properties != null) {
195 $text = $properties->getText();
197 $ret .= ' value="' . $plugin_name . '">'
198 . PMA_getString($text)
199 . '</option>' . "\n";
201 $ret .= '</select>' . "\n";
203 // Whether each plugin has to be saved as a file
204 foreach ($list as $plugin) {
205 $plugin_name = strtolower(substr(get_class($plugin), strlen($section)));
206 $ret .= '<input type="hidden" id="force_file_' . $plugin_name
207 . '" value="';
208 $properties = $plugin->getProperties();
209 if ( ! strcmp($section, 'Import')
210 || ($properties != null && $properties->getForceFile() != null)
212 $ret .= 'true';
213 } else {
214 $ret .= 'false';
216 $ret .= '" />'. "\n";
219 return $ret;
223 * Returns single option in a list element
225 * @param string $section name of config section in
226 * $GLOBALS['cfg'][$section] for plugin
227 * @param string $plugin_name unique plugin name
228 * @param array &$propertyGroup options property main group instance
229 * @param boolean $is_subgroup if this group is a subgroup
231 * @return string table row with option
233 function PMA_pluginGetOneOption(
234 $section,
235 $plugin_name,
236 &$propertyGroup,
237 $is_subgroup = false
239 $ret = "\n";
241 if (! $is_subgroup) {
242 // for subgroup headers
243 if (strpos(get_class($propertyGroup), "PropertyItem")) {
244 $properties = array($propertyGroup);
245 } else {
246 // for main groups
247 $ret .= '<div class="export_sub_options" id="' . $plugin_name . '_'
248 . $propertyGroup->getName() . '">';
250 if (method_exists($propertyGroup, 'getText')) {
251 $text = $propertyGroup->getText();
254 if ($text != null) {
255 $ret .= '<h4>' . PMA_getString($text) . '</h4>';
257 $ret .= '<ul>';
261 if (! isset($properties)) {
262 $not_subgroup_header = true;
263 if (method_exists($propertyGroup, 'getProperties')) {
264 $properties = $propertyGroup->getProperties();
268 if (isset($properties)) {
269 foreach ($properties as $propertyItem) {
270 $property_class = get_class($propertyItem);
271 // if the property is a subgroup, we deal with it recursively
272 if (strpos($property_class, "Subgroup")) {
273 // for subgroups
274 // each subgroup can have a header, which may also be a form element
275 $subgroup_header = $propertyItem->getSubgroupHeader();
276 if (isset($subgroup_header)) {
277 $ret .= PMA_pluginGetOneOption(
278 $section,
279 $plugin_name,
280 $subgroup_header
284 $ret .= '<li class="subgroup"><ul';
285 if (isset($subgroup_header)) {
286 $ret .= ' id="ul_' . $subgroup_header->getName() . '">';
287 } else {
288 $ret .= '>';
291 $ret .= PMA_pluginGetOneOption(
292 $section,
293 $plugin_name,
294 $propertyItem,
295 true
297 } else {
298 // single property item
299 switch ($property_class) {
300 case "BoolPropertyItem":
301 $ret .= '<li>' . "\n";
302 $ret .= '<input type="checkbox" name="' . $plugin_name . '_'
303 . $propertyItem->getName() . '"'
304 . ' value="something" id="checkbox_' . $plugin_name . '_'
305 . $propertyItem->getName() . '"'
306 . ' '
307 . PMA_pluginCheckboxCheck(
308 $section,
309 $plugin_name . '_' . $propertyItem->getName()
312 if ($propertyItem->getForce() != null) {
313 // Same code is also few lines lower, update both if needed
314 $ret .= ' onclick="if (!this.checked &amp;&amp; '
315 . '(!document.getElementById(\'checkbox_' . $plugin_name
316 . '_' . $propertyItem->getForce() . '\') '
317 . '|| !document.getElementById(\'checkbox_'
318 . $plugin_name . '_' . $propertyItem->getForce()
319 . '\').checked)) '
320 . 'return false; else return true;"';
322 $ret .= ' />';
323 $ret .= '<label for="checkbox_' . $plugin_name . '_'
324 . $propertyItem->getName() . '">'
325 . PMA_getString($propertyItem->getText()) . '</label>';
326 break;
327 case "DocPropertyItem":
328 echo "DocPropertyItem";
329 break;
330 case "HiddenPropertyItem":
331 $ret .= '<li><input type="hidden" name="' . $plugin_name . '_'
332 . $propertyItem->getName() . '"'
333 . ' value="' . PMA_pluginGetDefault(
334 $section,
335 $plugin_name . '_' . $propertyItem->getName()
337 . '"' . ' /></li>';
338 break;
339 case "MessageOnlyPropertyItem":
340 $ret .= '<li>' . "\n";
341 $ret .= '<p>' . PMA_getString($propertyItem->getText()) . '</p>';
342 break;
343 case "RadioPropertyItem":
344 $default = PMA_pluginGetDefault(
345 $section,
346 $plugin_name . '_' . $propertyItem->getName()
348 foreach ($propertyItem->getValues() as $key => $val) {
349 $ret .= '<li><input type="radio" name="' . $plugin_name
350 . '_' . $propertyItem->getName() . '" value="' . $key
351 . '" id="radio_' . $plugin_name . '_'
352 . $propertyItem->getName() . '_' . $key . '"';
353 if ($key == $default) {
354 $ret .= ' checked="checked"';
356 $ret .= ' />' . '<label for="radio_' . $plugin_name . '_'
357 . $propertyItem->getName() . '_' . $key . '">'
358 . PMA_getString($val) . '</label></li>';
360 break;
361 case "SelectPropertyItem":
362 $ret .= '<li>' . "\n";
363 $ret .= '<label for="select_' . $plugin_name . '_'
364 . $propertyItem->getName() . '" class="desc">'
365 . PMA_getString($propertyItem->getText()) . '</label>';
366 $ret .= '<select name="' . $plugin_name . '_'
367 . $propertyItem->getName() . '"'
368 . ' id="select_' . $plugin_name . '_'
369 . $propertyItem->getName() . '">';
370 $default = PMA_pluginGetDefault(
371 $section,
372 $plugin_name . '_' . $propertyItem->getName()
374 foreach ($propertyItem->getValues() as $key => $val) {
375 $ret .= '<option value="' . $key . '"';
376 if ($key == $default) {
377 $ret .= ' selected="selected"';
379 $ret .= '>' . PMA_getString($val) . '</option>';
381 $ret .= '</select>';
382 break;
383 case "TextPropertyItem":
384 $ret .= '<li>' . "\n";
385 $ret .= '<label for="text_' . $plugin_name . '_'
386 . $propertyItem->getName() . '" class="desc">'
387 . PMA_getString($propertyItem->getText()) . '</label>';
388 $ret .= '<input type="text" name="' . $plugin_name . '_'
389 . $propertyItem->getName() . '"'
390 . ' value="' . PMA_pluginGetDefault(
391 $section,
392 $plugin_name . '_' . $propertyItem->getName()
393 ) . '"'
394 . ' id="text_' . $plugin_name . '_'
395 . $propertyItem->getName() . '"'
396 . ($propertyItem->getSize() != null
397 ? ' size="' . $propertyItem->getSize() . '"'
398 : '')
399 . ($propertyItem->getLen() != null
400 ? ' maxlength="' . $propertyItem->getLen() . '"'
401 : '')
402 . ' />';
403 break;
404 default:;
410 if ($is_subgroup) {
411 // end subgroup
412 $ret .= '</ul></li>';
413 } else {
414 // end main group
415 if (! empty($not_subgroup_header)) {
416 $ret .= '</ul></div>';
420 if (method_exists($propertyGroup, "getDoc")) {
421 $doc = $propertyGroup->getDoc();
422 if ($doc != null) {
423 if (count($doc) == 3) {
424 $ret .= PMA_Util::showMySQLDocu(
425 $doc[0],
426 $doc[1],
427 false,
428 $doc[2]
430 } elseif (count($doc) == 1) {
431 $ret .= PMA_Util::showDocu('faq', $doc[0]);
432 } else {
433 $ret .= PMA_Util::showMySQLDocu(
434 $doc[0],
435 $doc[1]
441 // Close the list element after $doc link is displayed
442 if (isset($property_class)) {
443 if ($property_class == 'BoolPropertyItem'
444 || $property_class == 'MessageOnlyPropertyItem'
445 || $property_class == 'SelectPropertyItem'
446 || $property_class == 'TextPropertyItem'
448 $ret .= '</li>';
451 $ret .= "\n";
452 return $ret;
456 * Returns html div with editable options for plugin
458 * @param string $section name of config section in $GLOBALS['cfg'][$section]
459 * @param array &$list array with plugin instances
461 * @return string html fieldset with plugin options
463 function PMA_pluginGetOptions($section, &$list)
465 $ret = '';
466 $default = PMA_pluginGetDefault('Export', 'format');
467 // Options for plugins that support them
468 foreach ($list as $plugin) {
469 $properties = $plugin->getProperties();
470 if ($properties != null) {
471 $text = $properties->getText();
472 $options = $properties->getOptions();
475 $plugin_name = strtolower(substr(get_class($plugin), strlen($section)));
476 $ret .= '<div id="' . $plugin_name
477 . '_options" class="format_specific_options">';
478 $ret .= '<h3>' . PMA_getString($text) . '</h3>';
480 $no_options = true;
481 if ($options != null && count($options) > 0) {
482 foreach ($options->getProperties()
483 as $propertyMainGroup
485 // check for hidden properties
486 $no_options = true;
487 foreach ($propertyMainGroup->getProperties() as $propertyItem) {
488 if (strcmp("HiddenPropertyItem", get_class($propertyItem))) {
489 $no_options = false;
490 break;
494 $ret .= PMA_pluginGetOneOption(
495 $section,
496 $plugin_name,
497 $propertyMainGroup
502 if ($no_options) {
503 $ret .= '<p>' . __('This format has no options') . '</p>';
505 $ret .= '</div>';
507 return $ret;