]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/pluginenable.php
Merge branch '1.0.x' into feedsub-wizard
[quix0rs-gnu-social.git] / actions / pluginenable.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * Plugin enable action.
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Affero General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Affero General Public License for more details.
17  *
18  * You should have received a copy of the GNU Affero General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  *
21  * PHP version 5
22  *
23  * @category  Action
24  * @package   StatusNet
25  * @author    Brion Vibber <brion@status.net>
26  * @copyright 2010 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 /**
36  * Plugin enable action.
37  *
38  * (Re)-enables a plugin from the default plugins list.
39  *
40  * Takes parameters:
41  *
42  *    - plugin: plugin name
43  *    - token: session token to prevent CSRF attacks
44  *    - ajax: boolean; whether to return Ajax or full-browser results
45  *
46  * Only works if the current user is logged in.
47  *
48  * @category  Action
49  * @package   StatusNet
50  * @author    Brion Vibber <brion@status.net>
51  * @copyright 2010 StatusNet, Inc.
52  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
53  * @link      http://status.net/
54  */
55 class PluginEnableAction extends Action
56 {
57     var $user;
58     var $plugin;
59
60     /**
61      * Check pre-requisites and instantiate attributes
62      *
63      * @param Array $args array of arguments (URL, GET, POST)
64      *
65      * @return boolean success flag
66      */
67     function prepare($args)
68     {
69         parent::prepare($args);
70
71         // @fixme these are pretty common, should a parent class factor these out?
72
73         // Only allow POST requests
74
75         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
76             // TRANS: Client error displayed when trying to use another method than POST.
77             // TRANS: Do not translate POST.
78             $this->clientError(_('This action only accepts POST requests.'));
79             return false;
80         }
81
82         // CSRF protection
83
84         $token = $this->trimmed('token');
85
86         if (!$token || $token != common_session_token()) {
87             $this->clientError(_('There was a problem with your session token.'.
88                                  ' Try again, please.'));
89             return false;
90         }
91
92         // Only for logged-in users
93
94         $this->user = common_current_user();
95
96         if (empty($this->user)) {
97             // TRANS: Client error displayed when trying to enable or disable a plugin while not logged in.
98             $this->clientError(_('Not logged in.'));
99             return false;
100         }
101
102         if (!AdminPanelAction::canAdmin('plugins')) {
103             // TRANS: Client error displayed when trying to enable or disable a plugin without access rights.
104             $this->clientError(_('You cannot administer plugins.'));
105             return false;
106         }
107
108         $this->plugin = $this->arg('plugin');
109         $defaultPlugins = common_config('plugins', 'default');
110         if (!array_key_exists($this->plugin, $defaultPlugins)) {
111             // TRANS: Client error displayed when trying to enable or disable a non-existing plugin.
112             $this->clientError(_('No such plugin.'));
113             return false;
114         }
115
116         return true;
117     }
118
119     /**
120      * Handle request
121      *
122      * Does the subscription and returns results.
123      *
124      * @param Array $args unused.
125      *
126      * @return void
127      */
128     function handle($args)
129     {
130         $key = 'disable-' . $this->plugin;
131         Config::save('plugins', $key, $this->overrideValue());
132
133         // @fixme this is a pretty common pattern and should be refactored down
134         if ($this->boolean('ajax')) {
135             $this->startHTML('text/xml;charset=utf-8');
136             $this->elementStart('head');
137             $this->element('title', null, $this->successShortTitle());
138             $this->elementEnd('head');
139             $this->elementStart('body');
140             $form = $this->successNextForm();
141             $form->show();
142             $this->elementEnd('body');
143             $this->elementEnd('html');
144         } else {
145             $url = common_local_url('pluginsadminpanel');
146             common_redirect($url, 303);
147         }
148     }
149
150     /**
151      * Value to save into $config['plugins']['disable-<name>']
152      */
153     protected function overrideValue()
154     {
155         return 0;
156     }
157
158     protected function successShortTitle()
159     {
160         // TRANS: Page title for AJAX form return when enabling a plugin.
161         return _m('plugin', 'Enabled');
162     }
163
164     protected function successNextForm()
165     {
166         return new DisablePluginForm($this, $this->plugin);
167     }
168 }