pluginlist.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. public function __construct($plugins, $out)
  32. {
  33. parent::__construct($out);
  34. $this->plugins = $plugins;
  35. }
  36. public function show()
  37. {
  38. $this->startList();
  39. $this->showPlugins();
  40. $this->endList();
  41. }
  42. public function startList()
  43. {
  44. $this->out->elementStart('table', 'plugin_list');
  45. }
  46. public function endList()
  47. {
  48. $this->out->elementEnd('table');
  49. }
  50. public function showPlugins()
  51. {
  52. foreach ($this->plugins as $plugin) {
  53. $pli = $this->newListItem($plugin);
  54. $pli->show();
  55. }
  56. }
  57. public function newListItem($plugin)
  58. {
  59. return new PluginListItem($plugin, $this->out);
  60. }
  61. }
  62. class PluginListItem extends Widget
  63. {
  64. /** Current plugin. */
  65. public $plugin = null;
  66. /** Local cache for plugin version info */
  67. protected static $versions = false;
  68. public function __construct($plugin, $out)
  69. {
  70. parent::__construct($out);
  71. $this->plugin = $plugin;
  72. }
  73. public function show()
  74. {
  75. $meta = $this->metaInfo();
  76. $this->out->elementStart('tr', array('id' => 'plugin-' . $this->plugin));
  77. // Name and controls
  78. $this->out->elementStart('td');
  79. $this->out->elementStart('div');
  80. if (!empty($meta['homepage'])) {
  81. $this->out->elementStart('a', array('href' => $meta['homepage']));
  82. }
  83. $this->out->text($this->plugin);
  84. if (!empty($meta['homepage'])) {
  85. $this->out->elementEnd('a');
  86. }
  87. $this->out->elementEnd('div');
  88. $form = $this->getControlForm();
  89. $form->show();
  90. $this->out->elementEnd('td');
  91. // Version and authors
  92. $this->out->elementStart('td');
  93. if (!empty($meta['version'])) {
  94. $this->out->elementStart('div');
  95. $this->out->text($meta['version']);
  96. $this->out->elementEnd('div');
  97. }
  98. if (!empty($meta['author'])) {
  99. $this->out->elementStart('div');
  100. $this->out->text($meta['author']);
  101. $this->out->elementEnd('div');
  102. }
  103. $this->out->elementEnd('td');
  104. // Description
  105. $this->out->elementStart('td');
  106. if (!empty($meta['rawdescription'])) {
  107. $this->out->raw($meta['rawdescription']);
  108. } elseif (!empty($meta['description'])) {
  109. $this->out->raw($meta['description']);
  110. }
  111. $this->out->elementEnd('td');
  112. $this->out->elementEnd('tr');
  113. }
  114. /**
  115. * Pull up the appropriate control form for this plugin, depending
  116. * on its current state.
  117. *
  118. * @return Form
  119. */
  120. protected function getControlForm()
  121. {
  122. $key = 'disable-' . $this->plugin;
  123. if (common_config('plugins', $key)) {
  124. return new PluginEnableForm($this->out, $this->plugin);
  125. } else {
  126. return new PluginDisableForm($this->out, $this->plugin);
  127. }
  128. }
  129. /**
  130. * Grab metadata about this plugin...
  131. * Warning: horribly inefficient and may explode!
  132. * Doesn't work for disabled plugins either.
  133. *
  134. * @fixme pull structured data from plugin source
  135. */
  136. public function metaInfo()
  137. {
  138. $versions = self::getPluginVersions();
  139. $found = false;
  140. foreach ($versions as $info) {
  141. // We need a proper name for comparison, that is, without spaces nor the `(section)`
  142. // Therefore, a plugin named "Diogo Cordeiro (diogo@fc.up.pt)" becomes "DiogoCordeiro"
  143. $name_without_spaces = str_replace(' ', '', $info['name']);
  144. $name_without_spaces_nor_parentheses_section = substr($name_without_spaces, 0, strpos($name_without_spaces.'(', '('));
  145. if (strtolower($name_without_spaces_nor_parentheses_section) == strtolower($this->plugin)) {
  146. if ($found) {
  147. $found['rawdescription'] .= "<br />\n" . $info['rawdescription'];
  148. } else {
  149. $found = $info;
  150. }
  151. }
  152. }
  153. if ($found) {
  154. return $found;
  155. } else {
  156. return ['name' => $this->plugin,
  157. // TRANS: Plugin description for a disabled plugin.
  158. 'rawdescription' => _m('plugin-description', '(The plugin description is unavailable when a plugin has been disabled.)')];
  159. }
  160. }
  161. /**
  162. * Lazy-load the set of active plugin version info
  163. * @return array
  164. */
  165. protected static function getPluginVersions()
  166. {
  167. if (!is_array(self::$versions)) {
  168. $versions = [];
  169. Event::handle('ModuleVersion', [&$versions]);
  170. self::$versions = $versions;
  171. }
  172. return self::$versions;
  173. }
  174. }