3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * Plugin enable action.
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.
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.
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/>.
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/
31 if (!defined('STATUSNET')) {
36 * Plugin enable action.
38 * (Re)-enables a plugin from the default plugins list.
42 * - plugin: plugin name
43 * - token: session token to prevent CSRF attacks
44 * - ajax: boolean; whether to return Ajax or full-browser results
46 * Only works if the current user is logged in.
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/
55 class PluginEnableAction extends Action
61 * Check pre-requisites and instantiate attributes
63 * @param Array $args array of arguments (URL, GET, POST)
65 * @return boolean success flag
67 function prepare($args)
69 parent::prepare($args);
71 // @fixme these are pretty common, should a parent class factor these out?
73 // Only allow POST requests
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.'));
84 $token = $this->trimmed('token');
86 if (!$token || $token != common_session_token()) {
87 // TRANS: Client error displayed when the session token does not match or is not given.
88 $this->clientError(_('There was a problem with your session token.'.
89 ' Try again, please.'));
93 // Only for logged-in users
95 $this->user = common_current_user();
97 if (empty($this->user)) {
98 // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
99 $this->clientError(_('Not logged in.'));
103 if (!AdminPanelAction::canAdmin('plugins')) {
104 // TRANS: Client error displayed when trying to enable or disable a plugin without access rights.
105 $this->clientError(_('You cannot administer plugins.'));
109 $this->plugin = $this->arg('plugin');
110 $defaultPlugins = common_config('plugins', 'default');
111 if (!array_key_exists($this->plugin, $defaultPlugins)) {
112 // TRANS: Client error displayed when trying to enable or disable a non-existing plugin.
113 $this->clientError(_('No such plugin.'));
123 * Does the subscription and returns results.
125 * @param Array $args unused.
129 function handle($args)
131 $key = 'disable-' . $this->plugin;
132 Config::save('plugins', $key, $this->overrideValue());
134 // @fixme this is a pretty common pattern and should be refactored down
135 if ($this->boolean('ajax')) {
136 $this->startHTML('text/xml;charset=utf-8');
137 $this->elementStart('head');
138 $this->element('title', null, $this->successShortTitle());
139 $this->elementEnd('head');
140 $this->elementStart('body');
141 $form = $this->successNextForm();
143 $this->elementEnd('body');
144 $this->elementEnd('html');
146 $url = common_local_url('pluginsadminpanel');
147 common_redirect($url, 303);
152 * Value to save into $config['plugins']['disable-<name>']
154 protected function overrideValue()
159 protected function successShortTitle()
161 // TRANS: Page title for AJAX form return when enabling a plugin.
162 return _m('plugin', 'Enabled');
165 protected function successNextForm()
167 return new DisablePluginForm($this, $this->plugin);