added japanese language
[openemr.git] / phpmyadmin / libraries / plugin_interface.lib.php
blob619d5ff9d40a159d7409f8d2e659a19e09ee692a
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 object 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 // by default, return SQL plugin
38 return PMA_getPlugin($plugin_type, 'sql', $plugins_dir, $plugin_param);
41 /**
42 * Reads all plugin information from directory $plugins_dir
44 * @param string $plugin_type the type of the plugin (import, export, etc)
45 * @param string $plugins_dir directrory with plugins
46 * @param mixed $plugin_param parameter to plugin by which they can
47 * decide whether they can work
49 * @return array list of plugin instances
51 function PMA_getPlugins($plugin_type, $plugins_dir, $plugin_param)
53 $GLOBALS['plugin_param'] = $plugin_param;
54 /* Scan for plugins */
55 $plugin_list = array();
56 if (!($handle = @opendir($plugins_dir))) {
57 ksort($plugin_list);
58 return $plugin_list;
61 while ($file = @readdir($handle)) {
62 // In some situations, Mac OS creates a new file for each file
63 // (for example ._csv.php) so the following regexp
64 // matches a file which does not start with a dot but ends
65 // with ".php"
66 $class_type = mb_strtoupper($plugin_type[0], 'UTF-8')
67 . mb_strtolower(substr($plugin_type, 1), 'UTF-8');
68 if (is_file($plugins_dir . $file)
69 && preg_match(
70 '@^' . $class_type . '(.+)\.class\.php$@i',
71 $file,
72 $matches
74 ) {
75 $GLOBALS['skip_import'] = false;
76 include_once $plugins_dir . $file;
77 if (! $GLOBALS['skip_import']) {
78 $class_name = $class_type . $matches[1];
79 $plugin = new $class_name;
80 if (null !== $plugin->getProperties()) {
81 $plugin_list[] = $plugin;
87 ksort($plugin_list);
88 return $plugin_list;
91 /**
92 * Returns locale string for $name or $name if no locale is found
94 * @param string $name for local string
96 * @return string locale string for $name
98 function PMA_getString($name)
100 return isset($GLOBALS[$name]) ? $GLOBALS[$name] : $name;
104 * Returns html input tag option 'checked' if plugin $opt
105 * should be set by config or request
107 * @param string $section name of config section in
108 * $GLOBALS['cfg'][$section] for plugin
109 * @param string $opt name of option
111 * @return string hmtl input tag option 'checked'
113 function PMA_pluginCheckboxCheck($section, $opt)
115 // If the form is being repopulated using $_GET data, that is priority
116 if (isset($_GET[$opt])
117 || ! isset($_GET['repopulate'])
118 && ((isset($GLOBALS['timeout_passed'])
119 && $GLOBALS['timeout_passed']
120 && isset($_REQUEST[$opt]))
121 || (isset($GLOBALS['cfg'][$section][$opt])
122 && $GLOBALS['cfg'][$section][$opt]))
124 return ' checked="checked"';
126 return '';
130 * Returns default value for option $opt
132 * @param string $section name of config section in
133 * $GLOBALS['cfg'][$section] for plugin
134 * @param string $opt name of option
136 * @return string default value for option $opt
138 function PMA_pluginGetDefault($section, $opt)
140 if (isset($_GET[$opt])) {
141 // If the form is being repopulated using $_GET data, that is priority
142 return htmlspecialchars($_GET[$opt]);
145 if (isset($GLOBALS['timeout_passed'])
146 && $GLOBALS['timeout_passed']
147 && isset($_REQUEST[$opt])
149 return htmlspecialchars($_REQUEST[$opt]);
152 if (!isset($GLOBALS['cfg'][$section][$opt])) {
153 return '';
156 $matches = array();
157 /* Possibly replace localised texts */
158 if (!preg_match_all(
159 '/(str[A-Z][A-Za-z0-9]*)/',
160 $GLOBALS['cfg'][$section][$opt],
161 $matches
162 )) {
163 return htmlspecialchars($GLOBALS['cfg'][$section][$opt]);
166 $val = $GLOBALS['cfg'][$section][$opt];
167 foreach ($matches[0] as $match) {
168 if (isset($GLOBALS[$match])) {
169 $val = str_replace($match, $GLOBALS[$match], $val);
172 return htmlspecialchars($val);
176 * Returns html select form element for plugin choice
177 * and hidden fields denoting whether each plugin must be exported as a file
179 * @param string $section name of config section in
180 * $GLOBALS['cfg'][$section] for plugin
181 * @param string $name name of select element
182 * @param array &$list array with plugin instances
183 * @param string $cfgname name of config value, if none same as $name
185 * @return string html select tag
187 function PMA_pluginGetChoice($section, $name, &$list, $cfgname = null)
189 if (! isset($cfgname)) {
190 $cfgname = $name;
192 $ret = '<select id="plugins" name="' . $name . '">';
193 $default = PMA_pluginGetDefault($section, $cfgname);
194 foreach ($list as $plugin) {
195 $plugin_name = strtolower(substr(get_class($plugin), strlen($section)));
196 $ret .= '<option';
197 // If the form is being repopulated using $_GET data, that is priority
198 if (isset($_GET[$name])
199 && $plugin_name == $_GET[$name]
200 || ! isset($_GET[$name])
201 && $plugin_name == $default
203 $ret .= ' selected="selected"';
206 $properties = $plugin->getProperties();
207 $text = null;
208 if ($properties != null) {
209 $text = $properties->getText();
211 $ret .= ' value="' . $plugin_name . '">'
212 . PMA_getString($text)
213 . '</option>' . "\n";
215 $ret .= '</select>' . "\n";
217 // Whether each plugin has to be saved as a file
218 foreach ($list as $plugin) {
219 $plugin_name = strtolower(substr(get_class($plugin), strlen($section)));
220 $ret .= '<input type="hidden" id="force_file_' . $plugin_name
221 . '" value="';
222 $properties = $plugin->getProperties();
223 if ( ! strcmp($section, 'Import')
224 || ($properties != null && $properties->getForceFile() != null)
226 $ret .= 'true';
227 } else {
228 $ret .= 'false';
230 $ret .= '" />' . "\n";
233 return $ret;
237 * Returns single option in a list element
239 * @param string $section name of config section in
240 * $GLOBALS['cfg'][$section] for plugin
241 * @param string $plugin_name unique plugin name
242 * @param array &$propertyGroup options property main group instance
243 * @param boolean $is_subgroup if this group is a subgroup
245 * @return string table row with option
247 function PMA_pluginGetOneOption(
248 $section,
249 $plugin_name,
250 &$propertyGroup,
251 $is_subgroup = false
253 $ret = "\n";
255 if (! $is_subgroup) {
256 // for subgroup headers
257 if (strpos(get_class($propertyGroup), "PropertyItem")) {
258 $properties = array($propertyGroup);
259 } else {
260 // for main groups
261 $ret .= '<div class="export_sub_options" id="' . $plugin_name . '_'
262 . $propertyGroup->getName() . '">';
264 if (method_exists($propertyGroup, 'getText')) {
265 $text = $propertyGroup->getText();
268 if ($text != null) {
269 $ret .= '<h4>' . PMA_getString($text) . '</h4>';
271 $ret .= '<ul>';
275 if (! isset($properties)) {
276 $not_subgroup_header = true;
277 if (method_exists($propertyGroup, 'getProperties')) {
278 $properties = $propertyGroup->getProperties();
282 if (isset($properties)) {
283 foreach ($properties as $propertyItem) {
284 $property_class = get_class($propertyItem);
285 // if the property is a subgroup, we deal with it recursively
286 if (strpos($property_class, "Subgroup")) {
287 // for subgroups
288 // each subgroup can have a header, which may also be a form element
289 $subgroup_header = $propertyItem->getSubgroupHeader();
290 if (isset($subgroup_header)) {
291 $ret .= PMA_pluginGetOneOption(
292 $section,
293 $plugin_name,
294 $subgroup_header
298 $ret .= '<li class="subgroup"><ul';
299 if (isset($subgroup_header)) {
300 $ret .= ' id="ul_' . $subgroup_header->getName() . '">';
301 } else {
302 $ret .= '>';
305 $ret .= PMA_pluginGetOneOption(
306 $section,
307 $plugin_name,
308 $propertyItem,
309 true
311 continue;
314 // single property item
315 switch ($property_class) {
316 case "BoolPropertyItem":
317 $ret .= '<li>' . "\n";
318 $ret .= '<input type="checkbox" name="' . $plugin_name . '_'
319 . $propertyItem->getName() . '"'
320 . ' value="something" id="checkbox_' . $plugin_name . '_'
321 . $propertyItem->getName() . '"'
322 . ' '
323 . PMA_pluginCheckboxCheck(
324 $section,
325 $plugin_name . '_' . $propertyItem->getName()
328 if ($propertyItem->getForce() != null) {
329 // Same code is also few lines lower, update both if needed
330 $ret .= ' onclick="if (!this.checked &amp;&amp; '
331 . '(!document.getElementById(\'checkbox_' . $plugin_name
332 . '_' . $propertyItem->getForce() . '\') '
333 . '|| !document.getElementById(\'checkbox_'
334 . $plugin_name . '_' . $propertyItem->getForce()
335 . '\').checked)) '
336 . 'return false; else return true;"';
338 $ret .= ' />';
339 $ret .= '<label for="checkbox_' . $plugin_name . '_'
340 . $propertyItem->getName() . '">'
341 . PMA_getString($propertyItem->getText()) . '</label>';
342 break;
343 case "DocPropertyItem":
344 echo "DocPropertyItem";
345 break;
346 case "HiddenPropertyItem":
347 $ret .= '<li><input type="hidden" name="' . $plugin_name . '_'
348 . $propertyItem->getName() . '"'
349 . ' value="' . PMA_pluginGetDefault(
350 $section,
351 $plugin_name . '_' . $propertyItem->getName()
353 . '"' . ' /></li>';
354 break;
355 case "MessageOnlyPropertyItem":
356 $ret .= '<li>' . "\n";
357 $ret .= '<p>' . PMA_getString($propertyItem->getText()) . '</p>';
358 break;
359 case "RadioPropertyItem":
360 $default = PMA_pluginGetDefault(
361 $section,
362 $plugin_name . '_' . $propertyItem->getName()
364 foreach ($propertyItem->getValues() as $key => $val) {
365 $ret .= '<li><input type="radio" name="' . $plugin_name
366 . '_' . $propertyItem->getName() . '" value="' . $key
367 . '" id="radio_' . $plugin_name . '_'
368 . $propertyItem->getName() . '_' . $key . '"';
369 if ($key == $default) {
370 $ret .= ' checked="checked"';
372 $ret .= ' />' . '<label for="radio_' . $plugin_name . '_'
373 . $propertyItem->getName() . '_' . $key . '">'
374 . PMA_getString($val) . '</label></li>';
376 break;
377 case "SelectPropertyItem":
378 $ret .= '<li>' . "\n";
379 $ret .= '<label for="select_' . $plugin_name . '_'
380 . $propertyItem->getName() . '" class="desc">'
381 . PMA_getString($propertyItem->getText()) . '</label>';
382 $ret .= '<select name="' . $plugin_name . '_'
383 . $propertyItem->getName() . '"'
384 . ' id="select_' . $plugin_name . '_'
385 . $propertyItem->getName() . '">';
386 $default = PMA_pluginGetDefault(
387 $section,
388 $plugin_name . '_' . $propertyItem->getName()
390 foreach ($propertyItem->getValues() as $key => $val) {
391 $ret .= '<option value="' . $key . '"';
392 if ($key == $default) {
393 $ret .= ' selected="selected"';
395 $ret .= '>' . PMA_getString($val) . '</option>';
397 $ret .= '</select>';
398 break;
399 case "TextPropertyItem":
400 case "NumberPropertyItem":
401 $ret .= '<li>' . "\n";
402 $ret .= '<label for="text_' . $plugin_name . '_'
403 . $propertyItem->getName() . '" class="desc">'
404 . PMA_getString($propertyItem->getText()) . '</label>';
405 $ret .= '<input type="text" name="' . $plugin_name . '_'
406 . $propertyItem->getName() . '"'
407 . ' value="' . PMA_pluginGetDefault(
408 $section,
409 $plugin_name . '_' . $propertyItem->getName()
410 ) . '"'
411 . ' id="text_' . $plugin_name . '_'
412 . $propertyItem->getName() . '"'
413 . ($propertyItem->getSize() != null
414 ? ' size="' . $propertyItem->getSize() . '"'
415 : '')
416 . ($propertyItem->getLen() != null
417 ? ' maxlength="' . $propertyItem->getLen() . '"'
418 : '')
419 . ' />';
420 break;
421 default:;
426 if ($is_subgroup) {
427 // end subgroup
428 $ret .= '</ul></li>';
429 } else {
430 // end main group
431 if (! empty($not_subgroup_header)) {
432 $ret .= '</ul></div>';
436 if (method_exists($propertyGroup, "getDoc")) {
437 $doc = $propertyGroup->getDoc();
438 if ($doc != null) {
439 if (count($doc) == 3) {
440 $ret .= PMA_Util::showMySQLDocu(
441 $doc[1],
442 false,
443 $doc[2]
445 } elseif (count($doc) == 1) {
446 $ret .= PMA_Util::showDocu('faq', $doc[0]);
447 } else {
448 $ret .= PMA_Util::showMySQLDocu(
449 $doc[1]
455 // Close the list element after $doc link is displayed
456 if (isset($property_class)) {
457 if ($property_class == 'BoolPropertyItem'
458 || $property_class == 'MessageOnlyPropertyItem'
459 || $property_class == 'SelectPropertyItem'
460 || $property_class == 'TextPropertyItem'
462 $ret .= '</li>';
465 $ret .= "\n";
466 return $ret;
470 * Returns html div with editable options for plugin
472 * @param string $section name of config section in $GLOBALS['cfg'][$section]
473 * @param array &$list array with plugin instances
475 * @return string html fieldset with plugin options
477 function PMA_pluginGetOptions($section, &$list)
479 $ret = '';
480 // Options for plugins that support them
481 foreach ($list as $plugin) {
482 $properties = $plugin->getProperties();
483 if ($properties != null) {
484 $text = $properties->getText();
485 $options = $properties->getOptions();
488 $plugin_name = strtolower(substr(get_class($plugin), strlen($section)));
489 $ret .= '<div id="' . $plugin_name
490 . '_options" class="format_specific_options">';
491 $ret .= '<h3>' . PMA_getString($text) . '</h3>';
493 $no_options = true;
494 if ($options != null && count($options) > 0) {
495 foreach ($options->getProperties()
496 as $propertyMainGroup
498 // check for hidden properties
499 $no_options = true;
500 foreach ($propertyMainGroup->getProperties() as $propertyItem) {
501 if (strcmp("HiddenPropertyItem", get_class($propertyItem))) {
502 $no_options = false;
503 break;
507 $ret .= PMA_pluginGetOneOption(
508 $section,
509 $plugin_name,
510 $propertyMainGroup
515 if ($no_options) {
516 $ret .= '<p>' . __('This format has no options') . '</p>';
518 $ret .= '</div>';
520 return $ret;