PageRenderTime 68ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/ReferenceCode/phpmyadmin/libraries/plugin_interface.lib.php

https://gitlab.com/ctheilman92/Aging-In-Place
PHP | 508 lines | 375 code | 27 blank | 106 comment | 83 complexity | cf8271fc9e1384a42e233b6664e1d04f 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. * Includes and instantiates the specified plugin type for a certain format
  10. *
  11. * @param string $plugin_type the type of the plugin (import, export, etc)
  12. * @param string $plugin_format the format of the plugin (sql, xml, et )
  13. * @param string $plugins_dir directrory with plugins
  14. * @param mixed $plugin_param parameter to plugin by which they can
  15. * decide whether they can work
  16. *
  17. * @return new plugin instance
  18. */
  19. function PMA_getPlugin(
  20. $plugin_type,
  21. $plugin_format,
  22. $plugins_dir,
  23. $plugin_param = false
  24. ) {
  25. $GLOBALS['plugin_param'] = $plugin_param;
  26. $class_name = strtoupper($plugin_type[0])
  27. . strtolower(substr($plugin_type, 1))
  28. . strtoupper($plugin_format[0])
  29. . strtolower(substr($plugin_format, 1));
  30. $file = $class_name . ".class.php";
  31. if (is_file($plugins_dir . $file)) {
  32. include_once $plugins_dir . $file;
  33. return new $class_name;
  34. }
  35. return null;
  36. }
  37. /**
  38. * Reads all plugin information from directory $plugins_dir
  39. *
  40. * @param string $plugin_type the type of the plugin (import, export, etc)
  41. * @param string $plugins_dir directrory with plugins
  42. * @param mixed $plugin_param parameter to plugin by which they can
  43. * decide whether they can work
  44. *
  45. * @return array list of plugin instances
  46. */
  47. function PMA_getPlugins($plugin_type, $plugins_dir, $plugin_param)
  48. {
  49. $GLOBALS['plugin_param'] = $plugin_param;
  50. /* Scan for plugins */
  51. $plugin_list = array();
  52. if ($handle = @opendir($plugins_dir)) {
  53. while ($file = @readdir($handle)) {
  54. // In some situations, Mac OS creates a new file for each file
  55. // (for example ._csv.php) so the following regexp
  56. // matches a file which does not start with a dot but ends
  57. // with ".php"
  58. $class_type = strtoupper($plugin_type[0])
  59. . strtolower(substr($plugin_type, 1));
  60. if (is_file($plugins_dir . $file)
  61. && preg_match(
  62. '@^' . $class_type . '(.+)\.class\.php$@i',
  63. $file,
  64. $matches
  65. )
  66. ) {
  67. $GLOBALS['skip_import'] = false;
  68. include_once $plugins_dir . $file;
  69. if (! $GLOBALS['skip_import']) {
  70. $class_name = $class_type . $matches[1];
  71. $plugin_list [] = new $class_name;
  72. }
  73. }
  74. }
  75. }
  76. ksort($plugin_list);
  77. return $plugin_list;
  78. }
  79. /**
  80. * Returns locale string for $name or $name if no locale is found
  81. *
  82. * @param string $name for local string
  83. *
  84. * @return string locale string for $name
  85. */
  86. function PMA_getString($name)
  87. {
  88. return isset($GLOBALS[$name]) ? $GLOBALS[$name] : $name;
  89. }
  90. /**
  91. * Returns html input tag option 'checked' if plugin $opt
  92. * should be set by config or request
  93. *
  94. * @param string $section name of config section in
  95. * $GLOBALS['cfg'][$section] for plugin
  96. * @param string $opt name of option
  97. *
  98. * @return string hmtl input tag option 'checked'
  99. */
  100. function PMA_pluginCheckboxCheck($section, $opt)
  101. {
  102. // If the form is being repopulated using $_GET data, that is priority
  103. if (isset($_GET[$opt])
  104. || ! isset($_GET['repopulate'])
  105. && ((isset($GLOBALS['timeout_passed'])
  106. && $GLOBALS['timeout_passed']
  107. && isset($_REQUEST[$opt]))
  108. || (isset($GLOBALS['cfg'][$section][$opt])
  109. && $GLOBALS['cfg'][$section][$opt]))
  110. ) {
  111. return ' checked="checked"';
  112. }
  113. return '';
  114. }
  115. /**
  116. * Returns default value for option $opt
  117. *
  118. * @param string $section name of config section in
  119. * $GLOBALS['cfg'][$section] for plugin
  120. * @param string $opt name of option
  121. *
  122. * @return string default value for option $opt
  123. */
  124. function PMA_pluginGetDefault($section, $opt)
  125. {
  126. if (isset($_GET[$opt])) {
  127. // If the form is being repopulated using $_GET data, that is priority
  128. return htmlspecialchars($_GET[$opt]);
  129. } elseif (isset($GLOBALS['timeout_passed'])
  130. && $GLOBALS['timeout_passed']
  131. && isset($_REQUEST[$opt])) {
  132. return htmlspecialchars($_REQUEST[$opt]);
  133. } elseif (isset($GLOBALS['cfg'][$section][$opt])) {
  134. $matches = array();
  135. /* Possibly replace localised texts */
  136. if (preg_match_all(
  137. '/(str[A-Z][A-Za-z0-9]*)/',
  138. $GLOBALS['cfg'][$section][$opt],
  139. $matches
  140. )) {
  141. $val = $GLOBALS['cfg'][$section][$opt];
  142. foreach ($matches[0] as $match) {
  143. if (isset($GLOBALS[$match])) {
  144. $val = str_replace($match, $GLOBALS[$match], $val);
  145. }
  146. }
  147. return htmlspecialchars($val);
  148. } else {
  149. return htmlspecialchars($GLOBALS['cfg'][$section][$opt]);
  150. }
  151. }
  152. return '';
  153. }
  154. /**
  155. * Returns html select form element for plugin choice
  156. * and hidden fields denoting whether each plugin must be exported as a file
  157. *
  158. * @param string $section name of config section in
  159. * $GLOBALS['cfg'][$section] for plugin
  160. * @param string $name name of select element
  161. * @param array &$list array with plugin instances
  162. * @param string $cfgname name of config value, if none same as $name
  163. *
  164. * @return string html select tag
  165. */
  166. function PMA_pluginGetChoice($section, $name, &$list, $cfgname = null)
  167. {
  168. if (! isset($cfgname)) {
  169. $cfgname = $name;
  170. }
  171. $ret = '<select id="plugins" name="' . $name . '">';
  172. $default = PMA_pluginGetDefault($section, $cfgname);
  173. foreach ($list as $plugin) {
  174. $plugin_name = strtolower(substr(get_class($plugin), strlen($section)));
  175. $ret .= '<option';
  176. // If the form is being repopulated using $_GET data, that is priority
  177. if (isset($_GET[$name])
  178. && $plugin_name == $_GET[$name]
  179. || ! isset($_GET[$name])
  180. && $plugin_name == $default
  181. ) {
  182. $ret .= ' selected="selected"';
  183. }
  184. $properties = $plugin->getProperties();
  185. $text = null;
  186. if ($properties != null) {
  187. $text = $properties->getText();
  188. }
  189. $ret .= ' value="' . $plugin_name . '">'
  190. . PMA_getString($text)
  191. . '</option>' . "\n";
  192. }
  193. $ret .= '</select>' . "\n";
  194. // Whether each plugin has to be saved as a file
  195. foreach ($list as $plugin) {
  196. $plugin_name = strtolower(substr(get_class($plugin), strlen($section)));
  197. $ret .= '<input type="hidden" id="force_file_' . $plugin_name
  198. . '" value="';
  199. $properties = $plugin->getProperties();
  200. if ( ! strcmp($section, 'Import')
  201. || ($properties != null && $properties->getForceFile() != null)
  202. ) {
  203. $ret .= 'true';
  204. } else {
  205. $ret .= 'false';
  206. }
  207. $ret .= '" />'. "\n";
  208. }
  209. return $ret;
  210. }
  211. /**
  212. * Returns single option in a list element
  213. *
  214. * @param string $section name of config section in
  215. * $GLOBALS['cfg'][$section] for plugin
  216. * @param string $plugin_name unique plugin name
  217. * @param array &$propertyGroup options property main group instance
  218. * @param boolean $is_subgroup if this group is a subgroup
  219. *
  220. * @return string table row with option
  221. */
  222. function PMA_pluginGetOneOption(
  223. $section,
  224. $plugin_name,
  225. &$propertyGroup,
  226. $is_subgroup = false
  227. ) {
  228. $ret = "\n";
  229. if (! $is_subgroup) {
  230. // for subgroup headers
  231. if (strpos(get_class($propertyGroup), "PropertyItem")) {
  232. $properties = array($propertyGroup);
  233. } else {
  234. // for main groups
  235. $ret .= '<div class="export_sub_options" id="' . $plugin_name . '_'
  236. . $propertyGroup->getName() . '">';
  237. if (method_exists($propertyGroup, 'getText')) {
  238. $text = $propertyGroup->getText();
  239. }
  240. if ($text != null) {
  241. $ret .= '<h4>' . PMA_getString($text) . '</h4>';
  242. }
  243. $ret .= '<ul>';
  244. }
  245. }
  246. if (! isset($properties)) {
  247. $not_subgroup_header = true;
  248. if (method_exists($propertyGroup, 'getProperties')) {
  249. $properties = $propertyGroup->getProperties();
  250. }
  251. }
  252. if (isset($properties)) {
  253. foreach ($properties as $propertyItem) {
  254. $property_class = get_class($propertyItem);
  255. // if the property is a subgroup, we deal with it recursively
  256. if (strpos($property_class, "Subgroup")) {
  257. // for subgroups
  258. // each subgroup can have a header, which may also be a form element
  259. $subgroup_header = $propertyItem->getSubgroupHeader();
  260. if (isset($subgroup_header)) {
  261. $ret .= PMA_pluginGetOneOption(
  262. $section,
  263. $plugin_name,
  264. $subgroup_header
  265. );
  266. }
  267. $ret .= '<li class="subgroup"><ul';
  268. if (isset($subgroup_header)) {
  269. $ret .= ' id="ul_' . $subgroup_header->getName() . '">';
  270. } else {
  271. $ret .= '>';
  272. }
  273. $ret .= PMA_pluginGetOneOption(
  274. $section,
  275. $plugin_name,
  276. $propertyItem,
  277. true
  278. );
  279. } else {
  280. // single property item
  281. switch ($property_class) {
  282. case "BoolPropertyItem":
  283. $ret .= '<li>' . "\n";
  284. $ret .= '<input type="checkbox" name="' . $plugin_name . '_'
  285. . $propertyItem->getName() . '"'
  286. . ' value="something" id="checkbox_' . $plugin_name . '_'
  287. . $propertyItem->getName() . '"'
  288. . ' '
  289. . PMA_pluginCheckboxCheck(
  290. $section,
  291. $plugin_name . '_' . $propertyItem->getName()
  292. );
  293. if ($propertyItem->getForce() != null) {
  294. // Same code is also few lines lower, update both if needed
  295. $ret .= ' onclick="if (!this.checked &amp;&amp; '
  296. . '(!document.getElementById(\'checkbox_' . $plugin_name
  297. . '_' . $propertyItem->getForce() . '\') '
  298. . '|| !document.getElementById(\'checkbox_'
  299. . $plugin_name . '_' . $propertyItem->getForce()
  300. . '\').checked)) '
  301. . 'return false; else return true;"';
  302. }
  303. $ret .= ' />';
  304. $ret .= '<label for="checkbox_' . $plugin_name . '_'
  305. . $propertyItem->getName() . '">'
  306. . PMA_getString($propertyItem->getText()) . '</label>';
  307. break;
  308. case "DocPropertyItem":
  309. echo "DocPropertyItem";
  310. break;
  311. case "HiddenPropertyItem":
  312. $ret .= '<li><input type="hidden" name="' . $plugin_name . '_'
  313. . $propertyItem->getName() . '"'
  314. . ' value="' . PMA_pluginGetDefault(
  315. $section,
  316. $plugin_name . '_' . $propertyItem->getName()
  317. )
  318. . '"' . ' /></li>';
  319. break;
  320. case "MessageOnlyPropertyItem":
  321. $ret .= '<li>' . "\n";
  322. $ret .= '<p>' . PMA_getString($propertyItem->getText()) . '</p>';
  323. break;
  324. case "RadioPropertyItem":
  325. $default = PMA_pluginGetDefault(
  326. $section,
  327. $plugin_name . '_' . $propertyItem->getName()
  328. );
  329. foreach ($propertyItem->getValues() as $key => $val) {
  330. $ret .= '<li><input type="radio" name="' . $plugin_name
  331. . '_' . $propertyItem->getName() . '" value="' . $key
  332. . '" id="radio_' . $plugin_name . '_'
  333. . $propertyItem->getName() . '_' . $key . '"';
  334. if ($key == $default) {
  335. $ret .= ' checked="checked"';
  336. }
  337. $ret .= ' />' . '<label for="radio_' . $plugin_name . '_'
  338. . $propertyItem->getName() . '_' . $key . '">'
  339. . PMA_getString($val) . '</label></li>';
  340. }
  341. break;
  342. case "SelectPropertyItem":
  343. $ret .= '<li>' . "\n";
  344. $ret .= '<label for="select_' . $plugin_name . '_'
  345. . $propertyItem->getName() . '" class="desc">'
  346. . PMA_getString($propertyItem->getText()) . '</label>';
  347. $ret .= '<select name="' . $plugin_name . '_'
  348. . $propertyItem->getName() . '"'
  349. . ' id="select_' . $plugin_name . '_'
  350. . $propertyItem->getName() . '">';
  351. $default = PMA_pluginGetDefault(
  352. $section,
  353. $plugin_name . '_' . $propertyItem->getName()
  354. );
  355. foreach ($propertyItem->getValues() as $key => $val) {
  356. $ret .= '<option value="' . $key . '"';
  357. if ($key == $default) {
  358. $ret .= ' selected="selected"';
  359. }
  360. $ret .= '>' . PMA_getString($val) . '</option>';
  361. }
  362. $ret .= '</select>';
  363. break;
  364. case "TextPropertyItem":
  365. $ret .= '<li>' . "\n";
  366. $ret .= '<label for="text_' . $plugin_name . '_'
  367. . $propertyItem->getName() . '" class="desc">'
  368. . PMA_getString($propertyItem->getText()) . '</label>';
  369. $ret .= '<input type="text" name="' . $plugin_name . '_'
  370. . $propertyItem->getName() . '"'
  371. . ' value="' . PMA_pluginGetDefault(
  372. $section,
  373. $plugin_name . '_' . $propertyItem->getName()
  374. ) . '"'
  375. . ' id="text_' . $plugin_name . '_'
  376. . $propertyItem->getName() . '"'
  377. . ($propertyItem->getSize() != null
  378. ? ' size="' . $propertyItem->getSize() . '"'
  379. : '')
  380. . ($propertyItem->getLen() != null
  381. ? ' maxlength="' . $propertyItem->getLen() . '"'
  382. : '')
  383. . ' />';
  384. break;
  385. default:;
  386. }
  387. }
  388. }
  389. }
  390. if ($is_subgroup) {
  391. // end subgroup
  392. $ret .= '</ul></li>';
  393. } else {
  394. // end main group
  395. if (! empty($not_subgroup_header)) {
  396. $ret .= '</ul></div>';
  397. }
  398. }
  399. if (method_exists($propertyGroup, "getDoc")) {
  400. $doc = $propertyGroup->getDoc();
  401. if ($doc != null) {
  402. if (count($doc) == 3) {
  403. $ret .= PMA_Util::showMySQLDocu(
  404. $doc[0],
  405. $doc[1],
  406. false,
  407. $doc[2]
  408. );
  409. } elseif (count($doc) == 1) {
  410. $ret .= PMA_Util::showDocu('faq', $doc[0]);
  411. } else {
  412. $ret .= PMA_Util::showMySQLDocu(
  413. $doc[0],
  414. $doc[1]
  415. );
  416. }
  417. }
  418. }
  419. // Close the list element after $doc link is displayed
  420. if (isset($property_class)) {
  421. if ($property_class == 'BoolPropertyItem'
  422. || $property_class == 'MessageOnlyPropertyItem'
  423. || $property_class == 'SelectPropertyItem'
  424. || $property_class == 'TextPropertyItem'
  425. ) {
  426. $ret .= '</li>';
  427. }
  428. }
  429. $ret .= "\n";
  430. return $ret;
  431. }
  432. /**
  433. * Returns html div with editable options for plugin
  434. *
  435. * @param string $section name of config section in $GLOBALS['cfg'][$section]
  436. * @param array &$list array with plugin instances
  437. *
  438. * @return string html fieldset with plugin options
  439. */
  440. function PMA_pluginGetOptions($section, &$list)
  441. {
  442. $ret = '';
  443. $default = PMA_pluginGetDefault('Export', 'format');
  444. // Options for plugins that support them
  445. foreach ($list as $plugin) {
  446. $properties = $plugin->getProperties();
  447. if ($properties != null) {
  448. $text = $properties->getText();
  449. $options = $properties->getOptions();
  450. }
  451. $plugin_name = strtolower(substr(get_class($plugin), strlen($section)));
  452. $ret .= '<div id="' . $plugin_name
  453. . '_options" class="format_specific_options">';
  454. $ret .= '<h3>' . PMA_getString($text) . '</h3>';
  455. $no_options = true;
  456. if ($options != null && count($options) > 0) {
  457. foreach ($options->getProperties()
  458. as $propertyMainGroup
  459. ) {
  460. // check for hidden properties
  461. $no_options = true;
  462. foreach ($propertyMainGroup->getProperties() as $propertyItem) {
  463. if (strcmp("HiddenPropertyItem", get_class($propertyItem))) {
  464. $no_options = false;
  465. break;
  466. }
  467. }
  468. $ret .= PMA_pluginGetOneOption(
  469. $section,
  470. $plugin_name,
  471. $propertyMainGroup
  472. );
  473. }
  474. }
  475. if ($no_options) {
  476. $ret .= '<p>' . __('This format has no options') . '</p>';
  477. }
  478. $ret .= '</div>';
  479. }
  480. return $ret;
  481. }