pluginlist.php 10 KB

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