]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/pluginlist.php
Merge remote-tracking branch 'upstream/master' into social-master
[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 $pluginNames = array();
49
50     function __construct(array $pluginNames, Action $out=null)
51     {
52         parent::__construct($out);
53         $this->pluginNames = $pluginNames;
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->pluginNames as $pluginName) {
76             $pli = $this->newListItem($pluginName);
77             $pli->show();
78         }
79     }
80
81     function newListItem($pluginName)
82     {
83         return new PluginListItem($pluginName, $this->out);
84     }
85 }
86
87 class PluginListItem extends Widget
88 {
89     /** Current plugin. */
90     private $pluginName = null;
91
92     /** Local cache for plugin version info */
93     protected static $versions = false;
94
95     function __construct($pluginName, Action $out=null)
96     {
97         parent::__construct($out);
98         $this->pluginName = $pluginName;
99     }
100
101     function show()
102     {
103         $meta = $this->metaInfo();
104
105         $this->out->elementStart('tr', array('id' => 'plugin-' . $this->pluginName));
106
107         // Name and controls
108         $this->out->elementStart('td');
109         $this->out->elementStart('div');
110         if (!empty($meta['homepage'])) {
111             $this->out->elementStart('a', array('href' => $meta['homepage']));
112         }
113         $this->out->text($this->pluginName);
114         if (!empty($meta['homepage'])) {
115             $this->out->elementEnd('a');
116         }
117         $this->out->elementEnd('div');
118
119         $form = $this->getControlForm();
120         $form->show();
121
122         $this->out->elementEnd('td');
123
124         // Version and authors
125         $this->out->elementStart('td');
126         if (!empty($meta['version'])) {
127             $this->out->elementStart('div');
128             $this->out->text($meta['version']);
129             $this->out->elementEnd('div');
130         }
131         if (!empty($meta['author'])) {
132             $this->out->elementStart('div');
133             $this->out->text($meta['author']);
134             $this->out->elementEnd('div');
135         }
136         $this->out->elementEnd('td');
137
138         // Description
139         $this->out->elementStart('td');
140         if (!empty($meta['rawdescription'])) {
141             $this->out->raw($meta['rawdescription']);
142         }
143         $this->out->elementEnd('td');
144
145         $this->out->elementEnd('tr');
146     }
147
148     /**
149      * Pull up the appropriate control form for this plugin, depending
150      * on its current state.
151      *
152      * @return Form
153      */
154     protected function getControlForm()
155     {
156         $key = 'disable-' . $this->pluginName;
157         if (common_config('plugins', $key)) {
158             return new PluginEnableForm($this->out, $this->pluginName);
159         } else {
160             return new PluginDisableForm($this->out, $this->pluginName);
161         }
162     }
163
164     /**
165      * Grab metadata about this plugin...
166      * Warning: horribly inefficient and may explode!
167      * Doesn't work for disabled plugins either.
168      *
169      * @fixme pull structured data from plugin source
170      */
171     function metaInfo()
172     {
173         $versions = self::getPluginVersions();
174         $found = false;
175
176         foreach ($versions as $info) {
177             // hack for URL shorteners... "LilUrl (ur1.ca)" etc
178             list($name, ) = explode(' ', $info['name']);
179
180             if ($name == $this->pluginName) {
181                 if ($found) {
182                     // hack for URL shorteners...
183                     $found['rawdescription'] .= "<br />\n" . $info['rawdescription'];
184                 } else {
185                     $found = $info;
186                 }
187             }
188         }
189
190         if ($found) {
191             return $found;
192         } else {
193             return array('name' => $this->pluginName,
194                          // TRANS: Plugin description for a disabled plugin.
195                          'rawdescription' => _m('plugin-description','(The plugin description is unavailable when a plugin has been disabled.)'));
196         }
197     }
198
199     /**
200      * Lazy-load the set of active plugin version info
201      * @return array
202      */
203     protected static function getPluginVersions()
204     {
205         if (!is_array(self::$versions)) {
206             $versions = array();
207             Event::handle('PluginVersion', array(&$versions));
208             self::$versions = $versions;
209         }
210         return self::$versions;
211     }
212 }