PageRenderTime 55ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/phpmyadmin/libraries/plugin_interface.lib.php

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