PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/pma/libraries/plugin_interface.lib.php

https://bitbucket.org/pavolve/masterskayaludmila
PHP | 314 lines | 201 code | 10 blank | 103 comment | 78 complexity | 2dc0a68f7c9e252d6b62c0d3186aa3fe MD5 | raw file
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Generic plugin interface.
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. /**
  9. * array PMA_getPlugins(string $plugins_dir, mixed $plugin_param)
  10. *
  11. * Reads all plugin information from directory $plugins_dir.
  12. *
  13. * @param string $plugins_dir directrory with plugins
  14. * @param mixed $plugin_param parameter to plugin by which they can decide whether they can work
  15. * @return array list of plugins
  16. */
  17. function PMA_getPlugins($plugins_dir, $plugin_param)
  18. {
  19. /* Scan for plugins */
  20. $plugin_list = array();
  21. if ($handle = @opendir($plugins_dir)) {
  22. while ($file = @readdir($handle)) {
  23. // In some situations, Mac OS creates a new file for each file
  24. // (for example ._csv.php) so the following regexp
  25. // matches a file which does not start with a dot but ends
  26. // with ".php"
  27. if (is_file($plugins_dir . $file) && preg_match('@^[^\.](.)*\.php$@i', $file)) {
  28. include $plugins_dir . $file;
  29. }
  30. }
  31. }
  32. ksort($plugin_list);
  33. return $plugin_list;
  34. }
  35. /**
  36. * string PMA_getString(string $name)
  37. *
  38. * returns locale string for $name or $name if no locale is found
  39. *
  40. * @param string $name for local string
  41. * @return string locale string for $name
  42. */
  43. function PMA_getString($name)
  44. {
  45. return isset($GLOBALS[$name]) ? $GLOBALS[$name] : $name;
  46. }
  47. /**
  48. * string PMA_pluginCheckboxCheck(string $section, string $opt)
  49. *
  50. * returns html input tag option 'checked' if plugin $opt should be set by config or request
  51. *
  52. * @param string $section name of config section in
  53. * $GLOBALS['cfg'][$section] for plugin
  54. * @param string $opt name of option
  55. * @return string hmtl input tag option 'checked'
  56. */
  57. function PMA_pluginCheckboxCheck($section, $opt)
  58. {
  59. // If the form is being repopulated using $_GET data, that is priority
  60. if (isset($_GET[$opt]) || ! isset($_GET['repopulate']) && ((isset($GLOBALS['timeout_passed']) && $GLOBALS['timeout_passed'] && isset($_REQUEST[$opt])) ||
  61. (isset($GLOBALS['cfg'][$section][$opt]) && $GLOBALS['cfg'][$section][$opt]))) {
  62. return ' checked="checked"';
  63. }
  64. return '';
  65. }
  66. /**
  67. * string PMA_pluginGetDefault(string $section, string $opt)
  68. *
  69. * returns default value for option $opt
  70. *
  71. * @param string $section name of config section in
  72. * $GLOBALS['cfg'][$section] for plugin
  73. * @param string $opt name of option
  74. * @return string default value for option $opt
  75. */
  76. function PMA_pluginGetDefault($section, $opt)
  77. {
  78. if (isset($_GET[$opt])) { // If the form is being repopulated using $_GET data, that is priority
  79. return htmlspecialchars($_GET[$opt]);
  80. } elseif (isset($GLOBALS['timeout_passed']) && $GLOBALS['timeout_passed'] && isset($_REQUEST[$opt])) {
  81. return htmlspecialchars($_REQUEST[$opt]);
  82. } elseif (isset($GLOBALS['cfg'][$section][$opt])) {
  83. $matches = array();
  84. /* Possibly replace localised texts */
  85. if (preg_match_all('/(str[A-Z][A-Za-z0-9]*)/', $GLOBALS['cfg'][$section][$opt], $matches)) {
  86. $val = $GLOBALS['cfg'][$section][$opt];
  87. foreach ($matches[0] as $match) {
  88. if (isset($GLOBALS[$match])) {
  89. $val = str_replace($match, $GLOBALS[$match], $val);
  90. }
  91. }
  92. return htmlspecialchars($val);
  93. } else {
  94. return htmlspecialchars($GLOBALS['cfg'][$section][$opt]);
  95. }
  96. }
  97. return '';
  98. }
  99. /**
  100. * string PMA_pluginIsActive(string $section, string $opt, string $val)
  101. *
  102. * returns html input tag option 'checked' if option $opt should be set by config or request
  103. *
  104. * @param string $section name of config section in
  105. * $GLOBALS['cfg'][$section] for plugin
  106. * @param string $opt name of option
  107. * @param string $val value of option to check against
  108. * @return string html input tag option 'checked'
  109. */
  110. function PMA_pluginIsActive($section, $opt, $val)
  111. {
  112. if (! empty($GLOBALS['timeout_passed']) && isset($_REQUEST[$opt])) {
  113. if ($_REQUEST[$opt] == $val) {
  114. return ' checked="checked"';
  115. }
  116. } elseif (isset($GLOBALS['cfg'][$section][$opt]) && $GLOBALS['cfg'][$section][$opt] == $val) {
  117. return ' checked="checked"';
  118. }
  119. return '';
  120. }
  121. /**
  122. * string PMA_pluginGetChoice(string $section, string $name, array &$list, string $cfgname)
  123. *
  124. * returns html select form element for plugin choice
  125. * and hidden fields denoting whether each plugin must be exported as a file
  126. *
  127. * @param string $section name of config section in
  128. * $GLOBALS['cfg'][$section] for plugin
  129. * @param string $name name of select element
  130. * @param array &$list array with plugin configuration defined in plugin file
  131. * @param string $cfgname name of config value, if none same as $name
  132. * @return string html select tag
  133. */
  134. function PMA_pluginGetChoice($section, $name, &$list, $cfgname = null)
  135. {
  136. if (! isset($cfgname)) {
  137. $cfgname = $name;
  138. }
  139. $ret = '<select id="plugins" name="' . $name . '">';
  140. $default = PMA_pluginGetDefault($section, $cfgname);
  141. foreach ($list as $plugin_name => $val) {
  142. $ret .= '<option';
  143. // If the form is being repopulated using $_GET data, that is priority
  144. if (isset($_GET[$name]) && $plugin_name == $_GET[$name] || ! isset($_GET[$name]) && $plugin_name == $default) {
  145. $ret .= ' selected="selected"';
  146. }
  147. $ret .= ' value="' . $plugin_name . '">' . PMA_getString($val['text']) . '</option>' . "\n";
  148. }
  149. $ret .= '</select>' . "\n";
  150. // Whether each plugin has to be saved as a file
  151. foreach ($list as $plugin_name => $val) {
  152. $ret .= '<input type="hidden" id="force_file_' . $plugin_name . '" value="';
  153. if (isset($val['force_file'])) {
  154. $ret .= 'true';
  155. } else {
  156. $ret .= 'false';
  157. }
  158. $ret .= '" />'. "\n";
  159. }
  160. return $ret;
  161. }
  162. /**
  163. * string PMA_pluginGetOneOption(string $section, string $plugin_name, string $id, array &$opt)
  164. *
  165. * returns single option in a list element
  166. *
  167. * @param string $section name of config section in
  168. * $GLOBALS['cfg'][$section] for plugin
  169. * @param string $plugin_name unique plugin name
  170. * @param string $id option id
  171. * @param array &$opt plugin option details
  172. * @return string table row with option
  173. */
  174. function PMA_pluginGetOneOption($section, $plugin_name, $id, &$opt)
  175. {
  176. $ret = "\n";
  177. if ($opt['type'] == 'bool') {
  178. $ret .= '<li>' . "\n";
  179. $ret .= '<input type="checkbox" name="' . $plugin_name . '_' . $opt['name'] . '"'
  180. . ' value="something" id="checkbox_' . $plugin_name . '_' . $opt['name'] . '"'
  181. . ' ' . PMA_pluginCheckboxCheck($section, $plugin_name . '_' . $opt['name']);
  182. if (isset($opt['force'])) {
  183. /* Same code is also few lines lower, update both if needed */
  184. $ret .= ' onclick="if (!this.checked &amp;&amp; '
  185. . '(!document.getElementById(\'checkbox_' . $plugin_name . '_' .$opt['force'] . '\') '
  186. . '|| !document.getElementById(\'checkbox_' . $plugin_name . '_' .$opt['force'] . '\').checked)) '
  187. . 'return false; else return true;"';
  188. }
  189. $ret .= ' />';
  190. $ret .= '<label for="checkbox_' . $plugin_name . '_' . $opt['name'] . '">'
  191. . PMA_getString($opt['text']) . '</label>';
  192. } elseif ($opt['type'] == 'text') {
  193. $ret .= '<li>' . "\n";
  194. $ret .= '<label for="text_' . $plugin_name . '_' . $opt['name'] . '" class="desc">'
  195. . PMA_getString($opt['text']) . '</label>';
  196. $ret .= '<input type="text" name="' . $plugin_name . '_' . $opt['name'] . '"'
  197. . ' value="' . PMA_pluginGetDefault($section, $plugin_name . '_' . $opt['name']) . '"'
  198. . ' id="text_' . $plugin_name . '_' . $opt['name'] . '"'
  199. . (isset($opt['size']) ? ' size="' . $opt['size'] . '"' : '')
  200. . (isset($opt['len']) ? ' maxlength="' . $opt['len'] . '"' : '') . ' />';
  201. } elseif ($opt['type'] == 'message_only') {
  202. $ret .= '<li>' . "\n";
  203. $ret .= '<p>' . PMA_getString($opt['text']) . '</p>';
  204. } elseif ($opt['type'] == 'select') {
  205. $ret .= '<li>' . "\n";
  206. $ret .= '<label for="select_' . $plugin_name . '_' . $opt['name'] . '" class="desc">'
  207. . PMA_getString($opt['text']) . '</label>';
  208. $ret .= '<select name="' . $plugin_name . '_' . $opt['name'] . '"'
  209. . ' id="select_' . $plugin_name . '_' . $opt['name'] . '">';
  210. $default = PMA_pluginGetDefault($section, $plugin_name . '_' . $opt['name']);
  211. foreach ($opt['values'] as $key => $val) {
  212. $ret .= '<option value="' . $key . '"';
  213. if ($key == $default) {
  214. $ret .= ' selected="selected"';
  215. }
  216. $ret .= '>' . PMA_getString($val) . '</option>';
  217. }
  218. $ret .= '</select>';
  219. } elseif ($opt['type'] == 'radio') {
  220. $default = PMA_pluginGetDefault($section, $plugin_name . '_' . $opt['name']);
  221. foreach ($opt['values'] as $key => $val) {
  222. $ret .= '<li><input type="radio" name="' . $plugin_name . '_' . $opt['name'] . '" value="' . $key
  223. . '" id="radio_' . $plugin_name . '_' . $opt['name'] . '_' . $key . '"';
  224. if ($key == $default) {
  225. $ret .= 'checked="checked"';
  226. }
  227. $ret .= ' />' . '<label for="radio_' . $plugin_name . '_' . $opt['name'] . '_' . $key . '">'
  228. . PMA_getString($val) . '</label></li>';
  229. }
  230. } elseif ($opt['type'] == 'hidden') {
  231. $ret .= '<li><input type="hidden" name="' . $plugin_name . '_' . $opt['name'] . '"'
  232. . ' value="' . PMA_pluginGetDefault($section, $plugin_name . '_' . $opt['name']) . '"' . ' /></li>';
  233. } elseif ($opt['type'] == 'begin_group') {
  234. $ret .= '<div class="export_sub_options" id="' . $plugin_name . '_' . $opt['name'] . '">';
  235. if (isset($opt['text'])) {
  236. $ret .= '<h4>' . PMA_getString($opt['text']) . '</h4>';
  237. }
  238. $ret .= '<ul>';
  239. } elseif ($opt['type'] == 'end_group') {
  240. $ret .= '</ul></div>';
  241. } elseif ($opt['type'] == 'begin_subgroup') {
  242. /* each subgroup can have a header, which may also be a form element */
  243. $ret .= PMA_pluginGetOneOption($section, $plugin_name, $id, $opt['subgroup_header']) . '<li class="subgroup"><ul';
  244. if (isset($opt['subgroup_header']['name'])) {
  245. $ret .= ' id="ul_' . $opt['subgroup_header']['name'] . '">';
  246. } else {
  247. $ret .= '>';
  248. }
  249. } elseif ($opt['type'] == 'end_subgroup') {
  250. $ret .= '</ul></li>';
  251. } else {
  252. /* This should be seen only by plugin writers, so I do not thing this
  253. * needs translation. */
  254. $ret .= 'UNKNOWN OPTION ' . $opt['type'] . ' IN IMPORT PLUGIN ' . $plugin_name . '!';
  255. }
  256. if (isset($opt['doc'])) {
  257. if (count($opt['doc']) == 3) {
  258. $ret .= PMA_showMySQLDocu($opt['doc'][0], $opt['doc'][1], false, $opt['doc'][2]);
  259. } elseif (count($opt['doc']) == 1) {
  260. $ret .= PMA_showDocu($opt['doc'][0]);
  261. } else {
  262. $ret .= PMA_showMySQLDocu($opt['doc'][0], $opt['doc'][1]);
  263. }
  264. }
  265. // Close the list element after $opt['doc'] link is displayed
  266. if ($opt['type'] == 'bool' || $opt['type'] == 'text' || $opt['type'] == 'message_only' || $opt['type'] == 'select') {
  267. $ret .= '</li>';
  268. }
  269. $ret .= "\n";
  270. return $ret;
  271. }
  272. /**
  273. * string PMA_pluginGetOptions(string $section, array &$list)
  274. *
  275. * return html div with editable options for plugin
  276. *
  277. * @param string $section name of config section in $GLOBALS['cfg'][$section]
  278. * @param array &$list array with plugin configuration defined in plugin file
  279. * @return string html fieldset with plugin options
  280. */
  281. function PMA_pluginGetOptions($section, &$list)
  282. {
  283. $ret = '';
  284. $default = PMA_pluginGetDefault('Export', 'format');
  285. // Options for plugins that support them
  286. foreach ($list as $plugin_name => $val) {
  287. $ret .= '<div id="' . $plugin_name . '_options" class="format_specific_options">';
  288. $count = 0;
  289. $ret .= '<h3>' . PMA_getString($val['text']) . '</h3>';
  290. if (isset($val['options']) && count($val['options']) > 0) {
  291. foreach ($val['options'] as $id => $opt) {
  292. if ($opt['type'] != 'hidden' && $opt['type'] != 'begin_group' && $opt['type'] != 'end_group' && $opt['type'] != 'begin_subgroup' && $opt['type'] != 'end_subgroup') {
  293. $count++;
  294. }
  295. $ret .= PMA_pluginGetOneOption($section, $plugin_name, $id, $opt);
  296. }
  297. }
  298. if ($count == 0) {
  299. $ret .= '<p>' . __('This format has no options') . '</p>';
  300. }
  301. $ret .= '</div>';
  302. }
  303. return $ret;
  304. }