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

/qa-include/pages/admin/admin-plugins.php

http://github.com/q2a/question2answer
PHP | 272 lines | 182 code | 67 blank | 23 comment | 38 complexity | edd54cc7573bec3c806b5ca6c630c661 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /*
  3. Question2Answer by Gideon Greenspan and contributors
  4. http://www.question2answer.org/
  5. Description: Controller for admin page listing plugins and showing their options
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. More about this license: http://www.question2answer.org/license.php
  15. */
  16. if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
  17. header('Location: ../../../');
  18. exit;
  19. }
  20. require_once QA_INCLUDE_DIR . 'app/admin.php';
  21. // Check admin privileges
  22. if (!qa_admin_check_privileges($qa_content))
  23. return $qa_content;
  24. // Prepare content for theme
  25. $qa_content = qa_content_prepare();
  26. $qa_content['title'] = qa_lang_html('admin/admin_title') . ' - ' . qa_lang_html('admin/plugins_title');
  27. $qa_content['error'] = qa_admin_page_error();
  28. $qa_content['script_rel'][] = 'qa-content/qa-admin.js?' . QA_VERSION;
  29. $pluginManager = new Q2A_Plugin_PluginManager();
  30. $pluginManager->cleanRemovedPlugins();
  31. $enabledPlugins = $pluginManager->getEnabledPlugins();
  32. $fileSystemPlugins = $pluginManager->getFilesystemPlugins();
  33. $pluginHashes = $pluginManager->getHashesForPlugins($fileSystemPlugins);
  34. $showpluginforms = true;
  35. if (qa_is_http_post()) {
  36. if (!qa_check_form_security_code('admin/plugins', qa_post_text('qa_form_security_code'))) {
  37. $qa_content['error'] = qa_lang_html('misc/form_security_reload');
  38. $showpluginforms = false;
  39. } else {
  40. if (qa_clicked('dosave')) {
  41. $enabledPluginHashes = qa_post_text('enabled_plugins_hashes');
  42. $enabledPluginHashesArray = explode(';', $enabledPluginHashes);
  43. $pluginDirectories = array_keys(array_intersect($pluginHashes, $enabledPluginHashesArray));
  44. $pluginManager->setEnabledPlugins($pluginDirectories);
  45. qa_redirect('admin/plugins');
  46. }
  47. }
  48. }
  49. // Map modules with options to their containing plugins
  50. $pluginoptionmodules = array();
  51. $tables = qa_db_list_tables();
  52. $moduletypes = qa_list_module_types();
  53. foreach ($moduletypes as $type) {
  54. $modules = qa_list_modules($type);
  55. foreach ($modules as $name) {
  56. $module = qa_load_module($type, $name);
  57. if (method_exists($module, 'admin_form')) {
  58. $info = qa_get_module_info($type, $name);
  59. $dir = rtrim($info['directory'], '/');
  60. $pluginoptionmodules[$dir][] = array(
  61. 'type' => $type,
  62. 'name' => $name,
  63. );
  64. }
  65. }
  66. }
  67. foreach ($moduletypes as $type) {
  68. $modules = qa_load_modules_with($type, 'init_queries');
  69. foreach ($modules as $name => $module) {
  70. $queries = $module->init_queries($tables);
  71. if (!empty($queries)) {
  72. if (qa_is_http_post())
  73. qa_redirect('install');
  74. else {
  75. $qa_content['error'] = strtr(qa_lang_html('admin/module_x_database_init'), array(
  76. '^1' => qa_html($name),
  77. '^2' => qa_html($type),
  78. '^3' => '<a href="' . qa_path_html('install') . '">',
  79. '^4' => '</a>',
  80. ));
  81. }
  82. }
  83. }
  84. }
  85. if (!empty($fileSystemPlugins)) {
  86. $metadataUtil = new Q2A_Util_Metadata();
  87. $sortedPluginFiles = array();
  88. foreach ($fileSystemPlugins as $pluginDirectory) {
  89. $pluginDirectoryPath = QA_PLUGIN_DIR . $pluginDirectory;
  90. $metadata = $metadataUtil->fetchFromAddonPath($pluginDirectoryPath);
  91. if (empty($metadata)) {
  92. $pluginFile = $pluginDirectoryPath . '/qa-plugin.php';
  93. // limit plugin parsing to first 8kB
  94. $contents = file_get_contents($pluginFile, false, null, 0, 8192);
  95. $metadata = qa_addon_metadata($contents, 'Plugin');
  96. }
  97. $metadata['name'] = isset($metadata['name']) && !empty($metadata['name'])
  98. ? qa_html($metadata['name'])
  99. : qa_lang_html('admin/unnamed_plugin');
  100. $sortedPluginFiles[$pluginDirectory] = $metadata;
  101. }
  102. qa_sort_by($sortedPluginFiles, 'name');
  103. $pluginIndex = -1;
  104. foreach ($sortedPluginFiles as $pluginDirectory => $metadata) {
  105. $pluginIndex++;
  106. $pluginDirectoryPath = QA_PLUGIN_DIR . $pluginDirectory;
  107. $hash = $pluginHashes[$pluginDirectory];
  108. $showthisform = $showpluginforms && (qa_get('show') == $hash);
  109. $namehtml = $metadata['name'];
  110. if (isset($metadata['uri']) && strlen($metadata['uri']))
  111. $namehtml = '<a href="' . qa_html($metadata['uri']) . '">' . $namehtml . '</a>';
  112. $namehtml = '<b>' . $namehtml . '</b>';
  113. $metaver = isset($metadata['version']) && strlen($metadata['version']);
  114. if ($metaver)
  115. $namehtml .= ' v' . qa_html($metadata['version']);
  116. if (isset($metadata['author']) && strlen($metadata['author'])) {
  117. $authorhtml = qa_html($metadata['author']);
  118. if (isset($metadata['author_uri']) && strlen($metadata['author_uri']))
  119. $authorhtml = '<a href="' . qa_html($metadata['author_uri']) . '">' . $authorhtml . '</a>';
  120. $authorhtml = qa_lang_html_sub('main/by_x', $authorhtml);
  121. } else
  122. $authorhtml = '';
  123. if ($metaver && isset($metadata['update_uri']) && strlen($metadata['update_uri'])) {
  124. $elementid = 'version_check_' . md5($pluginDirectory);
  125. $updatehtml = '(<span id="' . $elementid . '">...</span>)';
  126. $qa_content['script_onloads'][] = array(
  127. "qa_version_check(" . qa_js($metadata['update_uri']) . ", " . qa_js($metadata['version'], true) . ", " . qa_js($elementid) . ", false);"
  128. );
  129. }
  130. else
  131. $updatehtml = '';
  132. if (isset($metadata['description']))
  133. $deschtml = qa_html($metadata['description']);
  134. else
  135. $deschtml = '';
  136. if (isset($pluginoptionmodules[$pluginDirectoryPath]) && !$showthisform) {
  137. $deschtml .= (strlen($deschtml) ? ' - ' : '') . '<a href="' . qa_admin_plugin_options_path($pluginDirectory) . '">' .
  138. qa_lang_html('admin/options') . '</a>';
  139. }
  140. $allowDisable = isset($metadata['load_order']) && $metadata['load_order'] === 'after_db_init';
  141. $beforeDbInit = isset($metadata['load_order']) && $metadata['load_order'] === 'before_db_init';
  142. $enabled = $beforeDbInit || !$allowDisable || in_array($pluginDirectory, $enabledPlugins);
  143. $pluginhtml = $namehtml . ' ' . $authorhtml . ' ' . $updatehtml . '<br>';
  144. $pluginhtml .= $deschtml . (strlen($deschtml) > 0 ? '<br>' : '');
  145. $pluginhtml .= '<small style="color:#666">' . qa_html($pluginDirectoryPath) . '/</small>';
  146. if (qa_qa_version_below(@$metadata['min_q2a']))
  147. $pluginhtml = '<s style="color:#999">'.$pluginhtml.'</s><br><span style="color:#f00">'.
  148. qa_lang_html_sub('admin/requires_q2a_version', qa_html($metadata['min_q2a'])).'</span>';
  149. elseif (qa_php_version_below(@$metadata['min_php']))
  150. $pluginhtml = '<s style="color:#999">'.$pluginhtml.'</s><br><span style="color:#f00">'.
  151. qa_lang_html_sub('admin/requires_php_version', qa_html($metadata['min_php'])).'</span>';
  152. $qa_content['form_plugin_'.$pluginIndex] = array(
  153. 'tags' => 'id="'.qa_html($hash).'"',
  154. 'style' => 'tall',
  155. 'fields' => array(
  156. array(
  157. 'type' => 'checkbox',
  158. 'label' => qa_lang_html('admin/enabled'),
  159. 'value' => $enabled,
  160. 'tags' => sprintf('id="plugin_enabled_%s"%s', $hash, $allowDisable ? '' : ' disabled'),
  161. ),
  162. array(
  163. 'type' => 'custom',
  164. 'html' => $pluginhtml,
  165. ),
  166. ),
  167. );
  168. if ($showthisform && isset($pluginoptionmodules[$pluginDirectoryPath])) {
  169. foreach ($pluginoptionmodules[$pluginDirectoryPath] as $pluginoptionmodule) {
  170. $type = $pluginoptionmodule['type'];
  171. $name = $pluginoptionmodule['name'];
  172. $module = qa_load_module($type, $name);
  173. $form = $module->admin_form($qa_content);
  174. if (!isset($form['tags']))
  175. $form['tags'] = 'method="post" action="' . qa_admin_plugin_options_path($pluginDirectory) . '"';
  176. if (!isset($form['style']))
  177. $form['style'] = 'tall';
  178. $form['boxed'] = true;
  179. $form['hidden']['qa_form_security_code'] = qa_get_form_security_code('admin/plugins');
  180. $qa_content['form_plugin_options'] = $form;
  181. }
  182. }
  183. }
  184. }
  185. $qa_content['navigation']['sub'] = qa_admin_sub_navigation();
  186. $qa_content['form'] = array(
  187. 'tags' => 'method="post" action="' . qa_self_html() . '" name="plugins_form" onsubmit="qa_get_enabled_plugins_hashes(); return true;"',
  188. 'style' => 'wide',
  189. 'buttons' => array(
  190. 'dosave' => array(
  191. 'tags' => 'name="dosave"',
  192. 'label' => qa_lang_html('admin/save_options_button'),
  193. ),
  194. ),
  195. 'hidden' => array(
  196. 'qa_form_security_code' => qa_get_form_security_code('admin/plugins'),
  197. 'enabled_plugins_hashes' => '',
  198. ),
  199. );
  200. return $qa_content;