PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/manage/phpmyadminlite/libraries/plugin_interface.lib.php

https://gitlab.com/albert925/lading-ach
PHP | 378 lines | 238 code | 9 blank | 131 comment | 48 complexity | 4852436dfc5e5083793c873e2ab1a55d MD5 | raw file
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Generic plugin interface.
  5. *
  6. * @version $Id$
  7. * @package phpMyAdmin
  8. */
  9. /**
  10. * array PMA_getPlugins(string $plugins_dir, mixed $plugin_param)
  11. *
  12. * Reads all plugin information from directory $plugins_dir.
  13. *
  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
  22. */
  23. function PMA_getPlugins($plugins_dir, $plugin_param)
  24. {
  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;
  36. }
  37. }
  38. }
  39. ksort($plugin_list);
  40. return $plugin_list;
  41. }
  42. /**
  43. * string PMA_getString(string $name)
  44. *
  45. * returns locale string for $name or $name if no locale is found
  46. *
  47. * @uses $GLOBALS
  48. * @param string $name for local string
  49. * @return string locale string for $name
  50. */
  51. function PMA_getString($name)
  52. {
  53. return isset($GLOBALS[$name]) ? $GLOBALS[$name] : $name;
  54. }
  55. /**
  56. * string PMA_pluginCheckboxCheck(string $section, string $opt)
  57. *
  58. * returns html input tag option 'checked' if plugin $opt should be set by config or request
  59. *
  60. * @uses $_REQUEST
  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 ((isset($GLOBALS['timeout_passed']) && $GLOBALS['timeout_passed'] && isset($_REQUEST[$opt])) ||
  71. (isset($GLOBALS['cfg'][$section][$opt]) && $GLOBALS['cfg'][$section][$opt])) {
  72. return ' checked="checked"';
  73. }
  74. return '';
  75. }
  76. /**
  77. * string PMA_pluginGetDefault(string $section, string $opt)
  78. *
  79. * returns default value for option $opt
  80. *
  81. * @uses htmlspecialchars()
  82. * @uses $_REQUEST
  83. * @uses $GLOBALS['cfg']
  84. * @uses $GLOBALS['timeout_passed']
  85. * @param string $section name of config section in
  86. * $GLOBALS['cfg'][$section] for plugin
  87. * @param string $opt name of option
  88. * @return string default value for option $opt
  89. */
  90. function PMA_pluginGetDefault($section, $opt)
  91. {
  92. if (isset($GLOBALS['timeout_passed']) && $GLOBALS['timeout_passed'] && isset($_REQUEST[$opt])) {
  93. return htmlspecialchars($_REQUEST[$opt]);
  94. } elseif (isset($GLOBALS['cfg'][$section][$opt])) {
  95. $matches = array();
  96. /* Possibly replace localised texts */
  97. if (preg_match_all('/(str[A-Z][A-Za-z0-9]*)/', $GLOBALS['cfg'][$section][$opt], $matches)) {
  98. $val = $GLOBALS['cfg'][$section][$opt];
  99. foreach($matches[0] as $match) {
  100. if (isset($GLOBALS[$match])) {
  101. $val = str_replace($match, $GLOBALS[$match], $val);
  102. }
  103. }
  104. return htmlspecialchars($val);
  105. } else {
  106. return htmlspecialchars($GLOBALS['cfg'][$section][$opt]);
  107. }
  108. }
  109. return '';
  110. }
  111. /**
  112. * string PMA_pluginIsActive(string $section, string $opt, string $val)
  113. *
  114. * returns html input tag option 'checked' if option $opt should be set by config or request
  115. *
  116. * @uses $_REQUEST
  117. * @uses $GLOBALS['cfg']
  118. * @uses $GLOBALS['timeout_passed']
  119. * @param string $section name of config section in
  120. * $GLOBALS['cfg'][$section] for plugin
  121. * @param string $opt name of option
  122. * @param string $val value of option to check against
  123. * @return string html input tag option 'checked'
  124. */
  125. function PMA_pluginIsActive($section, $opt, $val)
  126. {
  127. if (! empty($GLOBALS['timeout_passed']) && isset($_REQUEST[$opt])) {
  128. if ($_REQUEST[$opt] == $val) {
  129. return ' checked="checked"';
  130. }
  131. } elseif (isset($GLOBALS['cfg'][$section][$opt]) && $GLOBALS['cfg'][$section][$opt] == $val) {
  132. return ' checked="checked"';
  133. }
  134. return '';
  135. }
  136. /**
  137. * string PMA_pluginGetChoice(string $section, string $name, array &$list, string $cfgname)
  138. *
  139. * returns html radio form element for plugin choice
  140. *
  141. * @uses PMA_pluginIsActive()
  142. * @uses PMA_getString()
  143. * @param string $section name of config section in
  144. * $GLOBALS['cfg'][$section] for plugin
  145. * @param string $name name of radio element
  146. * @param array &$list array with plugin configuration defined in plugin file
  147. * @param string $cfgname name of config value, if none same as $name
  148. * @return string html input radio tag
  149. */
  150. function PMA_pluginGetChoice($section, $name, &$list, $cfgname = NULL)
  151. {
  152. if (!isset($cfgname)) {
  153. $cfgname = $name;
  154. }
  155. $ret = '';
  156. foreach ($list as $plugin_name => $val) {
  157. $ret .= '<!-- ' . $plugin_name . ' -->' . "\n";
  158. $ret .= '<input type="radio" name="' . $name . '" value="' . $plugin_name . '"'
  159. . ' id="radio_plugin_' . $plugin_name . '"'
  160. . ' onclick="if(this.checked) { hide_them_all();';
  161. if (isset($val['force_file'])) {
  162. $ret .= 'document.getElementById(\'checkbox_dump_asfile\').checked = true;';
  163. }
  164. $ret .= ' document.getElementById(\'' . $plugin_name . '_options\').style.display = \'block\'; };'
  165. .' return true"'
  166. . PMA_pluginIsActive($section, $cfgname, $plugin_name) . '/>' . "\n";
  167. $ret .= '<label for="radio_plugin_' . $plugin_name . '">'
  168. . PMA_getString($val['text']) . '</label>' . "\n";
  169. $ret .= '<br />' . "\n";
  170. }
  171. return $ret;
  172. }
  173. /**
  174. * string PMA_pluginGetOneOption(string $section, string $plugin_name, string $id, array &$opt)
  175. *
  176. * returns single option in a table row
  177. *
  178. * @uses PMA_getString()
  179. * @uses PMA_pluginCheckboxCheck()
  180. * @uses PMA_pluginGetDefault()
  181. * @param string $section name of config section in
  182. * $GLOBALS['cfg'][$section] for plugin
  183. * @param string $plugin_name unique plugin name
  184. * @param string $id option id
  185. * @param array &$opt plugin option details
  186. * @return string table row with option
  187. */
  188. function PMA_pluginGetOneOption($section, $plugin_name, $id, &$opt)
  189. {
  190. $ret = "\n";
  191. if ($opt['type'] == 'bool') {
  192. $ret .= '<div class="formelementrow">' . "\n";
  193. $ret .= '<input type="checkbox" name="' . $plugin_name . '_' . $opt['name'] . '"'
  194. . ' value="something" id="checkbox_' . $plugin_name . '_' . $opt['name'] . '"'
  195. . ' ' . PMA_pluginCheckboxCheck($section, $plugin_name . '_' . $opt['name']);
  196. if (isset($opt['force'])) {
  197. /* Same code is also few lines lower, update both if needed */
  198. $ret .= ' onclick="if (!this.checked &amp;&amp; '
  199. . '(!document.getElementById(\'checkbox_' . $plugin_name . '_' .$opt['force'] . '\') '
  200. . '|| !document.getElementById(\'checkbox_' . $plugin_name . '_' .$opt['force'] . '\').checked)) '
  201. . 'return false; else return true;"';
  202. }
  203. $ret .= ' />';
  204. $ret .= '<label for="checkbox_' . $plugin_name . '_' . $opt['name'] . '">'
  205. . PMA_getString($opt['text']) . '</label>';
  206. $ret .= '</div>' . "\n";
  207. } elseif ($opt['type'] == 'text') {
  208. $ret .= '<div class="formelementrow">' . "\n";
  209. $ret .= '<label for="text_' . $plugin_name . '_' . $opt['name'] . '" class="desc">'
  210. . PMA_getString($opt['text']) . '</label>';
  211. $ret .= '<input type="text" name="' . $plugin_name . '_' . $opt['name'] . '"'
  212. . ' value="' . PMA_pluginGetDefault($section, $plugin_name . '_' . $opt['name']) . '"'
  213. . ' id="text_' . $plugin_name . '_' . $opt['name'] . '"'
  214. . (isset($opt['size']) ? ' size="' . $opt['size'] . '"' : '')
  215. . (isset($opt['len']) ? ' maxlength="' . $opt['len'] . '"' : '') . ' />';
  216. $ret .= '</div>' . "\n";
  217. } elseif ($opt['type'] == 'message_only') {
  218. $ret .= '<div class="formelementrow">' . "\n";
  219. $ret .= '<p class="desc">' . PMA_getString($opt['text']) . '</p>';
  220. $ret .= '</div>' . "\n";
  221. } elseif ($opt['type'] == 'select') {
  222. $ret .= '<div class="formelementrow">' . "\n";
  223. $ret .= '<label for="select_' . $plugin_name . '_' . $opt['name'] . '" class="desc">'
  224. . PMA_getString($opt['text']) . '</label>';
  225. $ret .= '<select name="' . $plugin_name . '_' . $opt['name'] . '"'
  226. . ' id="select_' . $plugin_name . '_' . $opt['name'] . '">';
  227. $default = PMA_pluginGetDefault($section, $plugin_name . '_' . $opt['name']);
  228. foreach($opt['values'] as $key => $val) {
  229. $ret .= '<option value="' . $key . '"';
  230. if ($key == $default) {
  231. $ret .= ' selected="selected"';
  232. }
  233. $ret .= '>' . PMA_getString($val) . '</option>';
  234. }
  235. $ret .= '</select>';
  236. $ret .= '</div>' . "\n";
  237. } elseif ($opt['type'] == 'hidden') {
  238. $ret .= '<input type="hidden" name="' . $plugin_name . '_' . $opt['name'] . '"'
  239. . ' value="' . PMA_pluginGetDefault($section, $plugin_name . '_' . $opt['name']) . '"' . ' />';
  240. } elseif ($opt['type'] == 'bgroup') {
  241. $ret .= '<fieldset><legend>';
  242. /* No checkbox without name */
  243. if (!empty($opt['name'])) {
  244. $ret .= '<input type="checkbox" name="' . $plugin_name . '_' . $opt['name'] . '"'
  245. . ' value="something" id="checkbox_' . $plugin_name . '_' . $opt['name'] . '"'
  246. . ' ' . PMA_pluginCheckboxCheck($section, $plugin_name . '_' . $opt['name']);
  247. if (isset($opt['force'])) {
  248. /* Same code is also few lines higher, update both if needed */
  249. $ret .= ' onclick="if (!this.checked &amp;&amp; '
  250. . '(!document.getElementById(\'checkbox_' . $plugin_name . '_' .$opt['force'] . '\') '
  251. . '|| !document.getElementById(\'checkbox_' . $plugin_name . '_' .$opt['force'] . '\').checked)) '
  252. . 'return false; else return true;"';
  253. }
  254. $ret .= ' />';
  255. $ret .= '<label for="checkbox_' . $plugin_name . '_' . $opt['name'] . '">'
  256. . PMA_getString($opt['text']) . '</label>';
  257. } else {
  258. $ret .= PMA_getString($opt['text']);
  259. }
  260. $ret .= '</legend>';
  261. } elseif ($opt['type'] == 'egroup') {
  262. $ret .= '</fieldset>';
  263. } else {
  264. /* This should be seen only by plugin writers, so I do not thing this
  265. * needs translation. */
  266. $ret .= 'UNKNOWN OPTION ' . $opt['type'] . ' IN IMPORT PLUGIN ' . $plugin_name . '!';
  267. }
  268. if (isset($opt['doc'])) {
  269. if (count($opt['doc']) == 3) {
  270. $ret .= PMA_showMySQLDocu($opt['doc'][0], $opt['doc'][1], false, $opt['doc'][2]);
  271. } else {
  272. $ret .= PMA_showMySQLDocu($opt['doc'][0], $opt['doc'][1]);
  273. }
  274. }
  275. $ret .= "\n";
  276. return $ret;
  277. }
  278. /**
  279. * string PMA_pluginGetOptions(string $section, array &$list)
  280. *
  281. * return html fieldset with editable options for plugin
  282. *
  283. * @uses PMA_getString()
  284. * @uses PMA_pluginGetOneOption()
  285. * @param string $section name of config section in $GLOBALS['cfg'][$section]
  286. * @param array &$list array with plugin configuration defined in plugin file
  287. * @return string html fieldset with plugin options
  288. */
  289. function PMA_pluginGetOptions($section, &$list)
  290. {
  291. $ret = '';
  292. // Options for plugins that support them
  293. foreach ($list as $plugin_name => $val) {
  294. $ret .= '<fieldset id="' . $plugin_name . '_options" class="options">';
  295. $ret .= '<legend>' . PMA_getString($val['options_text']) . '</legend>';
  296. $count = 0;
  297. if (isset($val['options']) && count($val['options']) > 0) {
  298. foreach ($val['options'] as $id => $opt) {
  299. if ($opt['type'] != 'hidden') $count++;
  300. $ret .= PMA_pluginGetOneOption($section, $plugin_name, $id, $opt);
  301. }
  302. }
  303. if ($count == 0) {
  304. $ret .= $GLOBALS['strNoOptions'];
  305. }
  306. $ret .= '</fieldset>';
  307. }
  308. return $ret;
  309. }
  310. /**
  311. * string PMA_pluginGetJavascript(array &$list)
  312. *
  313. * return html/javascript code which is needed for handling plugin stuff
  314. *
  315. * @param array &$list array with plugin configuration defined in plugin file
  316. * @return string html fieldset with plugin options
  317. */
  318. function PMA_pluginGetJavascript(&$list) {
  319. $ret = '
  320. <script type="text/javascript">
  321. //<![CDATA[
  322. function hide_them_all() {
  323. ';
  324. foreach ($list as $plugin_name => $val) {
  325. $ret .= 'document.getElementById("' . $plugin_name . '_options").style.display = "none";' . "\n";
  326. }
  327. $ret .= '
  328. }
  329. function init_options() {
  330. hide_them_all();
  331. ';
  332. foreach ($list as $plugin_name => $val) {
  333. $ret .= 'if (document.getElementById("radio_plugin_' . $plugin_name . '").checked) {' . "\n";
  334. if (isset($val['force_file'])) {
  335. $ret .= 'document.getElementById(\'checkbox_dump_asfile\').checked = true;' . "\n";
  336. }
  337. $ret .= 'document.getElementById("' . $plugin_name . '_options").style.display = "block";' . "\n";
  338. $ret .= ' } else ' . "\n";
  339. }
  340. $ret .= '
  341. {
  342. ;
  343. }
  344. }
  345. function match_file(fname) {
  346. farr = fname.toLowerCase().split(".");
  347. if (farr.length != 0) {
  348. len = farr.length
  349. if (farr[len - 1] == "gz" || farr[len - 1] == "bz2" || farr[len -1] == "zip") len--;
  350. switch (farr[len - 1]) {
  351. ';
  352. foreach ($list as $plugin_name => $val) {
  353. $ret .= 'case "' . $val['extension'] . '" :';
  354. $ret .= 'document.getElementById("radio_plugin_' . $plugin_name . '").checked = true;';
  355. $ret .= 'init_options();';
  356. $ret .= 'break;' . "\n";
  357. }
  358. $ret .='
  359. }
  360. }
  361. }
  362. //]]>
  363. </script>
  364. ';
  365. return $ret;
  366. }
  367. ?>