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