123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- <?php
- defined('STATUSNET') || die();
- require INSTALLDIR . "/lib/admin/pluginenableform.php";
- require INSTALLDIR . "/lib/admin/plugindisableform.php";
- class PluginList extends Widget
- {
- public $plugins = [];
-
- public function __construct(Action $out, ?array $plugins = null)
- {
- parent::__construct($out);
- $this->plugins = is_null($plugins) ? $this->grabAllPluginNames() : $plugins;
- }
-
- public static function grabAllPluginNames(): array
- {
- $plugins = [];
- $distribution_plugins = glob(INSTALLDIR . '/plugins/*');
- foreach ($distribution_plugins as $plugin) {
- $plugin_name = ltrim($plugin, INSTALLDIR . '/plugins/');
- if ($plugin_name == 'README.md') {
- continue;
- }
- $plugins[] = $plugin_name;
- }
- unset($distribution_plugins);
- $thirdparty_plugins = glob(INSTALLDIR . '/local/plugins/*');
- foreach ($thirdparty_plugins as $plugin) {
- $plugins[] = ltrim($plugin, INSTALLDIR . '/local/plugins/');
- }
- unset($thirdparty_plugins);
- natsort($plugins);
- return $plugins;
- }
- public function show()
- {
- if (!$this->plugins) {
- $this->out->element('p', null,
-
- _m('All plugins have been disabled from the ' .
- 'site\'s configuration file.'));
- }
- $this->startList();
- $this->showPlugins();
- $this->endList();
- }
- public function startList(): void
- {
- $this->out->elementStart('table', 'plugin_list');
- }
- public function endList(): void
- {
- $this->out->elementEnd('table');
- }
- public function showPlugins(): void
- {
- foreach ($this->plugins as $plugin) {
- $pli = $this->newListItem($plugin);
- $pli->show();
- }
- }
- public function newListItem($plugin): PluginListItem
- {
- return new PluginListItem($plugin, $this->out);
- }
-
- protected static $versions = false;
-
- public static function getPluginVersions(): array
- {
- if (!is_array(self::$versions)) {
- $plugin_versions = [];
- Event::handle('PluginVersion', [&$plugin_versions]);
- self::$versions = $plugin_versions;
- }
- return self::$versions;
- }
-
- public static function internalizePluginName(string $plugin_name): string
- {
- $name_without_spaces = str_replace(' ', '', $plugin_name);
- $name_without_spaces_nor_parentheses_section = substr($name_without_spaces, 0, strpos($name_without_spaces . '(', '('));
- return $name_without_spaces_nor_parentheses_section;
- }
-
- public static function getActivePluginVersions(): array
- {
- $versions = self::getPluginVersions();
- $active_plugins = [];
- foreach ($versions as $info) {
- $internal_plugin_name = self::internalizePluginName($info['name']);
-
- $key = 'disable-' . $internal_plugin_name;
- if (common_config('plugins', $key)) {
- continue;
- }
- $active_plugins[] = $info;
- }
- return $active_plugins;
- }
-
- public static function isPluginLoaded(string $plugin): bool
- {
- $versions = self::getPluginVersions();
- foreach ($versions as $info) {
- $internal_plugin_name = self::internalizePluginName($info['name']);
-
-
- if ($internal_plugin_name == $plugin) {
- return true;
- }
- }
- return false;
- }
-
- public static function isPluginActive(string $plugin): bool
- {
- $key = 'disable-' . $plugin;
- return self::isPluginLoaded($plugin) && !common_config('plugins', $key);
- }
- }
- class PluginListItem extends Widget
- {
-
- public $plugin = null;
-
- protected static $versions = false;
- public function __construct($plugin, $out)
- {
- parent::__construct($out);
- $this->plugin = $plugin;
- }
- public function show()
- {
- $meta = $this->metaInfo();
- $this->out->elementStart('tr', ['id' => 'plugin-' . $this->plugin]);
-
- $this->out->elementStart('td');
- $this->out->elementStart('div');
- if (!empty($meta['homepage'])) {
- $this->out->elementStart('a', ['href' => $meta['homepage']]);
- }
- $this->out->text($this->plugin);
- if (!empty($meta['homepage'])) {
- $this->out->elementEnd('a');
- }
- $this->out->elementEnd('div');
- $form = $this->getControlForm();
- $form->show();
- $delete_form = new PluginDeleteForm($this->out, $this->plugin);
- $delete_form->show();
- $this->out->elementEnd('td');
-
- $this->out->elementStart('td');
- if (!empty($meta['version'])) {
- $this->out->elementStart('div');
- $this->out->text($meta['version']);
- $this->out->elementEnd('div');
- }
- if (!empty($meta['author'])) {
- $this->out->elementStart('div');
- $this->out->text($meta['author']);
- $this->out->elementEnd('div');
- }
- $this->out->elementEnd('td');
-
- $this->out->elementStart('td');
- if (!empty($meta['rawdescription'])) {
- $this->out->raw($meta['rawdescription']);
- } elseif (!empty($meta['description'])) {
- $this->out->text($meta['description']);
- }
- $this->out->elementEnd('td');
- $this->out->elementEnd('tr');
- }
-
- protected function getControlForm()
- {
- if (PluginList::isPluginActive($this->plugin)) {
- return new PluginDisableForm($this->out, $this->plugin);
- } else {
- return new PluginEnableForm($this->out, $this->plugin);
- }
- }
-
- public function metaInfo()
- {
- $versions = PluginList::getPluginVersions();
- $found = false;
- foreach ($versions as $info) {
- $internal_plugin_name = PluginList::internalizePluginName($info['name']);
-
- if (strtolower($internal_plugin_name) == strtolower($this->plugin)) {
- if ($found) {
- $found['rawdescription'] .= "<br />\n" . $info['rawdescription'];
- } else {
- $found = $info;
- }
- }
- }
- if ($found) {
- return $found;
- } else {
- return ['name' => $this->plugin,
-
- 'rawdescription' => _m('plugin-description', '(The plugin description is unavailable when a plugin hasn\'t been loaded.)')];
- }
- }
- }
|