]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/pluginlist.php
038b08a18fdc8e9a1fb625366fb92953b40cd20c
[quix0rs-gnu-social.git] / lib / pluginlist.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Plugins administration panel
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Settings
23  * @package   StatusNet
24  * @author    Brion Vibber <brion@status.net>
25  * @copyright 2010 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 require INSTALLDIR . '/lib/pluginenableform.php';
35 require INSTALLDIR . '/lib/plugindisableform.php';
36
37 /**
38  * Plugin list
39  *
40  * @category Admin
41  * @package  StatusNet
42  * @author   Brion Vibber <brion@status.net>
43  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44  * @link     http://status.net/
45  */
46 class PluginList extends Widget
47 {
48     var $plugins = array();
49
50     function __construct(array $plugins, Action $out=null)
51     {
52         parent::__construct($out);
53         $this->plugins = $plugins;
54     }
55
56     function show()
57     {
58         $this->startList();
59         $this->showPlugins();
60         $this->endList();
61     }
62
63     function startList()
64     {
65         $this->out->elementStart('table', 'plugin_list');
66     }
67
68     function endList()
69     {
70         $this->out->elementEnd('table');
71     }
72
73     function showPlugins()
74     {
75         foreach ($this->plugins as $plugin) {
76             // Some check on instance
77             assert($plugin instanceof Plugin);
78
79             $pli = $this->newListItem($plugin);
80             $pli->show();
81         }
82     }
83
84     function newListItem(Plugin $plugin)
85     {
86         return new PluginListItem($plugin, $this->out);
87     }
88 }
89
90 class PluginListItem extends Widget
91 {
92     /** Current plugin. */
93     var $plugin = null;
94
95     /** Local cache for plugin version info */
96     protected static $versions = false;
97
98     function __construct($plugin, Action $out=null)
99     {
100         parent::__construct($out);
101         $this->plugin = $plugin;
102     }
103
104     function show()
105     {
106         $meta = $this->metaInfo();
107
108         $this->out->elementStart('tr', array('id' => 'plugin-' . $this->plugin));
109
110         // Name and controls
111         $this->out->elementStart('td');
112         $this->out->elementStart('div');
113         if (!empty($meta['homepage'])) {
114             $this->out->elementStart('a', array('href' => $meta['homepage']));
115         }
116         $this->out->text($this->plugin);
117         if (!empty($meta['homepage'])) {
118             $this->out->elementEnd('a');
119         }
120         $this->out->elementEnd('div');
121
122         $form = $this->getControlForm();
123         $form->show();
124
125         $this->out->elementEnd('td');
126
127         // Version and authors
128         $this->out->elementStart('td');
129         if (!empty($meta['version'])) {
130             $this->out->elementStart('div');
131             $this->out->text($meta['version']);
132             $this->out->elementEnd('div');
133         }
134         if (!empty($meta['author'])) {
135             $this->out->elementStart('div');
136             $this->out->text($meta['author']);
137             $this->out->elementEnd('div');
138         }
139         $this->out->elementEnd('td');
140
141         // Description
142         $this->out->elementStart('td');
143         if (!empty($meta['rawdescription'])) {
144             $this->out->raw($meta['rawdescription']);
145         }
146         $this->out->elementEnd('td');
147
148         $this->out->elementEnd('tr');
149     }
150
151     /**
152      * Pull up the appropriate control form for this plugin, depending
153      * on its current state.
154      *
155      * @return Form
156      */
157     protected function getControlForm()
158     {
159         $key = 'disable-' . $this->plugin;
160         if (common_config('plugins', $key)) {
161             return new PluginEnableForm($this->out, $this->plugin);
162         } else {
163             return new PluginDisableForm($this->out, $this->plugin);
164         }
165     }
166
167     /**
168      * Grab metadata about this plugin...
169      * Warning: horribly inefficient and may explode!
170      * Doesn't work for disabled plugins either.
171      *
172      * @fixme pull structured data from plugin source
173      */
174     function metaInfo()
175     {
176         $versions = self::getPluginVersions();
177         $found = false;
178
179         foreach ($versions as $info) {
180             // hack for URL shorteners... "LilUrl (ur1.ca)" etc
181             list($name, ) = explode(' ', $info['name']);
182
183             if ($name == $this->plugin) {
184                 if ($found) {
185                     // hack for URL shorteners...
186                     $found['rawdescription'] .= "<br />\n" . $info['rawdescription'];
187                 } else {
188                     $found = $info;
189                 }
190             }
191         }
192
193         if ($found) {
194             return $found;
195         } else {
196             return array('name' => $this->plugin,
197                          // TRANS: Plugin description for a disabled plugin.
198                          'rawdescription' => _m('plugin-description','(The plugin description is unavailable when a plugin has been disabled.)'));
199         }
200     }
201
202     /**
203      * Lazy-load the set of active plugin version info
204      * @return array
205      */
206     protected static function getPluginVersions()
207     {
208         if (!is_array(self::$versions)) {
209             $versions = array();
210             Event::handle('PluginVersion', array(&$versions));
211             self::$versions = $versions;
212         }
213         return self::$versions;
214     }
215 }