pluginlist.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 $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. if ($internal_plugin_name == $plugin) {
  164. return true;
  165. }
  166. }
  167. return false;
  168. }
  169. /**
  170. * Checks if a given plugin is active (both loaded and not set as inactive in the database)
  171. *
  172. * @param string $plugin
  173. * @return bool
  174. * @see self::isPluginLoaded() Warning: horribly inefficient and may explode!
  175. */
  176. public static function isPluginActive(string $plugin): bool
  177. {
  178. $key = 'disable-' . $plugin;
  179. return self::isPluginLoaded($plugin) && !common_config('plugins', $key);
  180. }
  181. }
  182. /**
  183. * Class PluginListItem
  184. */
  185. class PluginListItem extends Widget
  186. {
  187. /** Current plugin. */
  188. public $plugin = null;
  189. /** Local cache for plugin version info */
  190. protected static $versions = false;
  191. public function __construct($plugin, $out)
  192. {
  193. parent::__construct($out);
  194. $this->plugin = $plugin;
  195. }
  196. public function show()
  197. {
  198. $meta = $this->metaInfo();
  199. $this->out->elementStart('tr', ['id' => 'plugin-' . $this->plugin]);
  200. // Name and controls
  201. $this->out->elementStart('td');
  202. $this->out->elementStart('div');
  203. if (!empty($meta['homepage'])) {
  204. $this->out->elementStart('a', ['href' => $meta['homepage']]);
  205. }
  206. $this->out->text($this->plugin);
  207. if (!empty($meta['homepage'])) {
  208. $this->out->elementEnd('a');
  209. }
  210. $this->out->elementEnd('div');
  211. $form = $this->getControlForm();
  212. $form->show();
  213. $delete_form = new PluginDeleteForm($this->out, $this->plugin);
  214. $delete_form->show();
  215. $this->out->elementEnd('td');
  216. // Version and authors
  217. $this->out->elementStart('td');
  218. if (!empty($meta['version'])) {
  219. $this->out->elementStart('div');
  220. $this->out->text($meta['version']);
  221. $this->out->elementEnd('div');
  222. }
  223. if (!empty($meta['author'])) {
  224. $this->out->elementStart('div');
  225. $this->out->text($meta['author']);
  226. $this->out->elementEnd('div');
  227. }
  228. $this->out->elementEnd('td');
  229. // Description
  230. $this->out->elementStart('td');
  231. if (!empty($meta['rawdescription'])) {
  232. $this->out->raw($meta['rawdescription']);
  233. } elseif (!empty($meta['description'])) {
  234. $this->out->text($meta['description']);
  235. }
  236. $this->out->elementEnd('td');
  237. $this->out->elementEnd('tr');
  238. }
  239. /**
  240. * Pull up the appropriate control form for this plugin, depending
  241. * on its current state.
  242. *
  243. * @return Form
  244. */
  245. protected function getControlForm()
  246. {
  247. if (PluginList::isPluginActive($this->plugin)) {
  248. return new PluginDisableForm($this->out, $this->plugin);
  249. } else {
  250. return new PluginEnableForm($this->out, $this->plugin);
  251. }
  252. }
  253. /**
  254. * Grab metadata about this plugin...
  255. * Warning: horribly inefficient and may explode!
  256. * Doesn't work for disabled plugins either.
  257. *
  258. * @fixme pull structured data from plugin source
  259. * ^ Maybe by introducing a ini file in each plugin's directory? But a typical instance doesn't have all that many
  260. * plugins anyway, no need for urgent action
  261. */
  262. public function metaInfo()
  263. {
  264. $versions = PluginList::getPluginVersions();
  265. $found = false;
  266. foreach ($versions as $info) {
  267. $internal_plugin_name = PluginList::internalizePluginName($info['name']);
  268. // For a proper comparison, we do it in lower case
  269. if (strtolower($internal_plugin_name) == strtolower($this->plugin)) {
  270. if ($found) {
  271. $found['rawdescription'] .= "<br />\n" . $info['rawdescription'];
  272. } else {
  273. $found = $info;
  274. }
  275. }
  276. }
  277. if ($found) {
  278. return $found;
  279. } else {
  280. return ['name' => $this->plugin,
  281. // TRANS: Plugin description for a disabled plugin.
  282. 'rawdescription' => _m('plugin-description', '(The plugin description is unavailable when a plugin hasn\'t been loaded.)')];
  283. }
  284. }
  285. }