3 * StatusNet, the distributed open-source microblogging tool
5 * Edit an OAuth Application
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.
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.
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/>.
22 * @category Applications
24 * @author Zach Copley <zach@status.net>
25 * @copyright 2008-2011 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/
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
35 * Edit the details of an OAuth application
37 * This is the form for editing an application
39 * @category Application
41 * @author Zach Copley <zach@status.net>
42 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43 * @link http://status.net/
45 class EditApplicationAction extends Action
53 // TRANS: Title for "Edit application" form.
54 return _('Edit application');
60 function prepare($args)
62 parent::prepare($args);
64 if (!common_logged_in()) {
65 // TRANS: Client error displayed trying to edit an application while not logged in.
66 $this->clientError(_('You must be logged in to edit an application.'));
70 $id = (int)$this->arg('id');
72 $this->app = Oauth_application::staticGet($id);
73 $this->owner = User::staticGet($this->app->owner);
74 $cur = common_current_user();
76 if ($cur->id != $this->owner->id) {
77 // TRANS: Client error displayed trying to edit an application while not being its owner.
78 $this->clientError(_('You are not the owner of this application.'), 401);
82 // TRANS: Client error displayed trying to edit an application that does not exist.
83 $this->clientError(_('No such application.'));
93 * On GET, show the form. On POST, try to save the app.
95 * @param array $args unused
99 function handle($args)
101 parent::handle($args);
103 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
104 $this->handlePost($args);
110 function handlePost($args)
112 // Workaround for PHP returning empty $_POST and $_FILES when POST
113 // length > post_max_size in php.ini
117 && ($_SERVER['CONTENT_LENGTH'] > 0)
119 // TRANS: Client error displayed when the number of bytes in a POST request exceeds a limit.
120 // TRANS: %s is the number of bytes of the CONTENT_LENGTH.
121 $msg = _m('The server was unable to handle that much POST data (%s byte) due to its current configuration.',
122 'The server was unable to handle that much POST data (%s bytes) due to its current configuration.',
123 intval($_SERVER['CONTENT_LENGTH']));
124 $this->clientException(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
129 $token = $this->trimmed('token');
130 if (!$token || $token != common_session_token()) {
131 // TRANS: Client error displayed when the session token does not match or is not given.
132 $this->clientError(_('There was a problem with your session token.'));
136 $cur = common_current_user();
138 if ($this->arg('cancel')) {
139 common_redirect(common_local_url('showapplication',
140 array('id' => $this->app->id)), 303);
141 } elseif ($this->arg('save')) {
144 // TRANS: Client error displayed submitting invalid form data for edit application.
145 $this->clientError(_('Unexpected form submission.'));
149 function showForm($msg=null)
155 function showContent()
157 $form = new ApplicationEditForm($this, $this->app);
161 function showPageNotice()
163 if (!empty($this->msg)) {
164 $this->element('p', 'error', $this->msg);
166 $this->element('p', 'instructions',
167 // TRANS: Instructions for "Edit application" form.
168 _('Use this form to edit your application.'));
174 $name = $this->trimmed('name');
175 $description = $this->trimmed('description');
176 $source_url = $this->trimmed('source_url');
177 $organization = $this->trimmed('organization');
178 $homepage = $this->trimmed('homepage');
179 $callback_url = $this->trimmed('callback_url');
180 $type = $this->arg('app_type');
181 $access_type = $this->arg('default_access_type');
184 // TRANS: Validation error shown when not providing a name in the "Edit application" form.
185 $this->showForm(_('Name is required.'));
187 } elseif (mb_strlen($name) > 255) {
188 // TRANS: Validation error shown when providing too long a name in the "Edit application" form.
189 $this->showForm(_('Name is too long (maximum 255 characters).'));
191 } else if ($this->nameExists($name)) {
192 // TRANS: Validation error shown when providing a name for an application that already exists in the "Edit application" form.
193 $this->showForm(_('Name already in use. Try another one.'));
195 } elseif (empty($description)) {
196 // TRANS: Validation error shown when not providing a description in the "Edit application" form.
197 $this->showForm(_('Description is required.'));
199 } elseif (Oauth_application::descriptionTooLong($description)) {
200 $this->showForm(sprintf(
201 // TRANS: Validation error shown when providing too long a description in the "Edit application" form.
202 // TRANS: %d is the maximum number of allowed characters.
203 _m('Description is too long (maximum %d character).',
204 'Description is too long (maximum %d characters).',
205 Oauth_application::maxDesc()),
206 Oauth_application::maxDesc()));
208 } elseif (mb_strlen($source_url) > 255) {
209 // TRANS: Validation error shown when providing too long a source URL in the "Edit application" form.
210 $this->showForm(_('Source URL is too long.'));
212 } elseif ((mb_strlen($source_url) > 0)
213 && !Validate::uri($source_url,
214 array('allowed_schemes' => array('http', 'https'))))
216 // TRANS: Validation error shown when providing an invalid source URL in the "Edit application" form.
217 $this->showForm(_('Source URL is not valid.'));
219 } elseif (empty($organization)) {
220 // TRANS: Validation error shown when not providing an organisation in the "Edit application" form.
221 $this->showForm(_('Organization is required.'));
223 } elseif (mb_strlen($organization) > 255) {
224 // TRANS: Validation error shown when providing too long an arganisation name in the "Edit application" form.
225 $this->showForm(_('Organization is too long (maximum 255 characters).'));
227 } elseif (empty($homepage)) {
228 // TRANS: Form validation error show when an organisation name has not been provided in the edit application form.
229 $this->showForm(_('Organization homepage is required.'));
231 } elseif ((mb_strlen($homepage) > 0)
232 && !Validate::uri($homepage,
233 array('allowed_schemes' => array('http', 'https'))))
235 // TRANS: Validation error shown when providing an invalid homepage URL in the "Edit application" form.
236 $this->showForm(_('Homepage is not a valid URL.'));
238 } elseif (mb_strlen($callback_url) > 255) {
239 // TRANS: Validation error shown when providing too long a callback URL in the "Edit application" form.
240 $this->showForm(_('Callback is too long.'));
242 } elseif (mb_strlen($callback_url) > 0
243 && !Validate::uri($source_url,
244 array('allowed_schemes' => array('http', 'https'))
247 // TRANS: Validation error shown when providing an invalid callback URL in the "Edit application" form.
248 $this->showForm(_('Callback URL is not valid.'));
252 $cur = common_current_user();
254 // Checked in prepare() above
256 assert(!is_null($cur));
257 assert(!is_null($this->app));
259 $orig = clone($this->app);
261 $this->app->name = $name;
262 $this->app->description = $description;
263 $this->app->source_url = $source_url;
264 $this->app->organization = $organization;
265 $this->app->homepage = $homepage;
266 $this->app->callback_url = $callback_url;
267 $this->app->type = $type;
269 common_debug("access_type = $access_type");
271 if ($access_type == 'r') {
272 $this->app->access_type = 1;
274 $this->app->access_type = 3;
277 $result = $this->app->update($orig);
279 // Note: 0 means no rows changed, which can happen if the only
280 // thing we changed was the icon, since it's not altered until
282 if ($result === false) {
283 common_log_db_error($this->app, 'UPDATE', __FILE__);
284 // TRANS: Server error occuring when an application could not be updated from the "Edit application" form.
285 $this->serverError(_('Could not update application.'));
288 $this->app->uploadLogo();
290 common_redirect(common_local_url('oauthappssettings'), 303);
294 * Does the app name already exist?
296 * Checks the DB to see someone has already registered an app
297 * with the same name.
299 * @param string $name app name to check
301 * @return boolean true if the name already exists
303 function nameExists($name)
305 $newapp = Oauth_application::staticGet('name', $name);
306 if (empty($newapp)) {
309 return $newapp->id != $this->app->id;