pluginlist.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. defined('STATUSNET') || die();
  17. require INSTALLDIR . "/lib/pluginenableform.php";
  18. require INSTALLDIR . "/lib/plugindisableform.php";
  19. /**
  20. * Plugin list
  21. *
  22. * @category Admin
  23. * @package GNUsocial
  24. * @author Brion Vibber <brion@status.net>
  25. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  26. * @link http://status.net/
  27. */
  28. class PluginList extends Widget
  29. {
  30. public $plugins = [];
  31. /**
  32. * PluginList constructor.
  33. * @param Action $out
  34. * @param array|null $plugins
  35. */
  36. public function __construct(Action $out, ?array $plugins = null)
  37. {
  38. parent::__construct($out);
  39. $this->plugins = is_null($plugins) ? $this->grabAllPluginNames() : $plugins;
  40. }
  41. /**
  42. * List of names of all available plugins (distribution and third parties).
  43. * Warning: Plugin not modules, it doesn't include core modules.
  44. *
  45. * @return array
  46. */
  47. public static function grabAllPluginNames(): array
  48. {
  49. $plugins = [];
  50. $distribution_plugins = glob(INSTALLDIR . '/plugins/*');
  51. foreach ($distribution_plugins as $plugin) {
  52. $plugin_name = ltrim($plugin, INSTALLDIR . '/plugins/');
  53. if ($plugin_name == 'README.md') {
  54. continue;
  55. }
  56. $plugins[] = $plugin_name;
  57. }
  58. unset($distribution_plugins);
  59. $thirdparty_plugins = glob(INSTALLDIR . '/local/plugins/*');
  60. foreach ($thirdparty_plugins as $plugin) {
  61. $plugins[] = ltrim($plugin, INSTALLDIR . '/local/plugins/');
  62. }
  63. unset($thirdparty_plugins);
  64. natsort($plugins);
  65. return $plugins;
  66. }
  67. public function show()
  68. {
  69. if (!$this->plugins) {
  70. $this->out->element('p', null,
  71. // TRANS: Text displayed on plugin admin page when no plugin are enabled.
  72. _m('All plugins have been disabled from the ' .
  73. 'site\'s configuration file.'));
  74. }
  75. $this->startList();
  76. $this->showPlugins();
  77. $this->endList();
  78. }
  79. public function startList(): void
  80. {
  81. $this->out->elementStart('table', 'plugin_list');
  82. }
  83. public function endList(): void
  84. {
  85. $this->out->elementEnd('table');
  86. }
  87. public function showPlugins(): void
  88. {
  89. foreach ($this->plugins as $plugin) {
  90. $pli = $this->newListItem($plugin);
  91. $pli->show();
  92. }
  93. }
  94. public function newListItem($plugin): PluginListItem
  95. {
  96. return new PluginListItem($plugin, $this->out);
  97. }
  98. /** Local cache for plugin version info */
  99. protected static $versions = false;
  100. /**
  101. * Lazy-load the set of active plugin version info.
  102. * Warning: Plugin not modules, it doesn't include core modules.
  103. * @return array
  104. */
  105. public static function getPluginVersions(): array
  106. {
  107. if (!is_array(self::$versions)) {
  108. $plugin_versions = [];
  109. Event::handle('PluginVersion', [&$plugin_versions]);
  110. self::$versions = $plugin_versions;
  111. }
  112. return self::$versions;
  113. }
  114. /**
  115. * We need a proper name for comparison, that is, without spaces nor the `(section)`
  116. * Therefore, a plugin named "Diogo Cordeiro (diogo@fc.up.pt)" becomes "DiogoCordeiro"
  117. *
  118. * WARNING: You may have to use strtolower() in your situation
  119. *
  120. * @param string $plugin_name
  121. * @return string Name without spaces nor parentheses section
  122. */
  123. public static function internalizePluginName(string $plugin_name): string
  124. {
  125. $name_without_spaces = str_replace(' ', '', $plugin_name);
  126. $name_without_spaces_nor_parentheses_section = substr($name_without_spaces, 0, strpos($name_without_spaces . '(', '('));
  127. return $name_without_spaces_nor_parentheses_section;
  128. }
  129. /**
  130. * It calls self::getPluginVersions() and for each it builds an array with the self::internalizePluginName()
  131. *
  132. * @return array
  133. */
  134. public static function getActivePluginVersions(): array
  135. {
  136. $versions = self::getPluginVersions();
  137. $active_plugins = [];
  138. foreach ($versions as $info) {
  139. $internal_plugin_name = self::internalizePluginName($info['name']);
  140. // This is sensitive case
  141. $key = 'disable-' . $internal_plugin_name;
  142. if (common_config('plugins', $key)) {
  143. continue;
  144. }
  145. $active_plugins[] = $info;
  146. }
  147. return $active_plugins;
  148. }
  149. /**
  150. * Checks if a given plugin was loaded (added in config.php with addPlugin())
  151. *
  152. * @param string $plugin
  153. * @return bool
  154. * @see PluginListItem->metaInfo() Warning: horribly inefficient and may explode!
  155. */
  156. public static function isPluginLoaded(string $plugin): bool
  157. {
  158. $versions = self::getPluginVersions();
  159. foreach ($versions as $info) {
  160. $internal_plugin_name = self::internalizePluginName($info['name']);
  161. // For a proper comparison, we do it in lower case
  162. if (strtolower($internal_plugin_name) == strtolower($plugin)) {
  163. return true;
  164. }
  165. }
  166. return false;
  167. }
  168. /**
  169. * Checks if a given plugin is active (both loaded and not set as inactive in the database)
  170. *
  171. * @param string $plugin
  172. * @return bool
  173. * @see self::isPluginLoaded() Warning: horribly inefficient and may explode!
  174. */
  175. public static function isPluginActive(string $plugin): bool
  176. {
  177. $key = 'disable-' . $plugin;
  178. return self::isPluginLoaded($plugin) && !common_config('plugins', $key);
  179. }
  180. }
  181. /**
  182. * Class PluginListItem
  183. */
  184. class PluginListItem extends Widget
  185. {
  186. /** Current plugin. */
  187. public $plugin = null;
  188. /** Local cache for plugin version info */
  189. protected static $versions = false;
  190. public function __construct($plugin, $out)
  191. {
  192. parent::__construct($out);
  193. $this->plugin = $plugin;
  194. }
  195. public function show()
  196. {
  197. $meta = $this->metaInfo();
  198. $this->out->elementStart('tr', ['id' => 'plugin-' . $this->plugin]);
  199. // Name and controls
  200. $this->out->elementStart('td');
  201. $this->out->elementStart('div');
  202. if (!empty($meta['homepage'])) {
  203. $this->out->elementStart('a', ['href' => $meta['homepage']]);
  204. }
  205. $this->out->text($this->plugin);
  206. if (!empty($meta['homepage'])) {
  207. $this->out->elementEnd('a');
  208. }
  209. $this->out->elementEnd('div');
  210. $form = $this->getControlForm();
  211. $form->show();
  212. $delete_form = new PluginDeleteForm($this->out, $this->plugin);
  213. $delete_form->show();
  214. $this->out->elementEnd('td');
  215. // Version and authors
  216. $this->out->elementStart('td');
  217. if (!empty($meta['version'])) {
  218. $this->out->elementStart('div');
  219. $this->out->text($meta['version']);
  220. $this->out->elementEnd('div');
  221. }
  222. if (!empty($meta['author'])) {
  223. $this->out->elementStart('div');
  224. $this->out->text($meta['author']);
  225. $this->out->elementEnd('div');
  226. }
  227. $this->out->elementEnd('td');
  228. // Description
  229. $this->out->elementStart('td');
  230. if (!empty($meta['rawdescription'])) {
  231. $this->out->raw($meta['rawdescription']);
  232. } elseif (!empty($meta['description'])) {
  233. $this->out->text($meta['description']);
  234. }
  235. $this->out->elementEnd('td');
  236. $this->out->elementEnd('tr');
  237. }
  238. /**
  239. * Pull up the appropriate control form for this plugin, depending
  240. * on its current state.
  241. *
  242. * @return Form
  243. */
  244. protected function getControlForm()
  245. {
  246. if (PluginList::isPluginActive($this->plugin)) {
  247. return new PluginDisableForm($this->out, $this->plugin);
  248. } else {
  249. return new PluginEnableForm($this->out, $this->plugin);
  250. }
  251. }
  252. /**
  253. * Grab metadata about this plugin...
  254. * Warning: horribly inefficient and may explode!
  255. * Doesn't work for disabled plugins either.
  256. *
  257. * @fixme pull structured data from plugin source
  258. * ^ Maybe by introducing a ini file in each plugin's directory? But a typical instance doesn't have all that many
  259. * plugins anyway, no need for urgent action
  260. */
  261. public function metaInfo()
  262. {
  263. $versions = PluginList::getPluginVersions();
  264. $found = false;
  265. foreach ($versions as $info) {
  266. $internal_plugin_name = PluginList::internalizePluginName($info['name']);
  267. // For a proper comparison, we do it in lower case
  268. if (strtolower($internal_plugin_name) == strtolower($this->plugin)) {
  269. if ($found) {
  270. $found['rawdescription'] .= "<br />\n" . $info['rawdescription'];
  271. } else {
  272. $found = $info;
  273. }
  274. }
  275. }
  276. if ($found) {
  277. return $found;
  278. } else {
  279. return ['name' => $this->plugin,
  280. // TRANS: Plugin description for a disabled plugin.
  281. 'rawdescription' => _m('plugin-description', '(The plugin description is unavailable when a plugin hasn\'t been loaded.)')];
  282. }
  283. }
  284. }