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-2009 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 OwnerDesignAction
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 exception. %s is CONTENT_LENGTH (in bytes).
120 $msg = _('The server was unable to handle that much POST ' .
121 'data (%s bytes) due to its current configuration.');
122 $this->clientException(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
127 $token = $this->trimmed('token');
128 if (!$token || $token != common_session_token()) {
129 $this->clientError(_('There was a problem with your session token.'));
133 $cur = common_current_user();
135 if ($this->arg('cancel')) {
136 common_redirect(common_local_url('showapplication',
137 array('id' => $this->app->id)), 303);
138 } elseif ($this->arg('save')) {
141 // TRANS: Client error displayed submitting invalid form data for edit application.
142 $this->clientError(_('Unexpected form submission.'));
146 function showForm($msg=null)
152 function showContent()
154 $form = new ApplicationEditForm($this, $this->app);
158 function showPageNotice()
160 if (!empty($this->msg)) {
161 $this->element('p', 'error', $this->msg);
163 $this->element('p', 'instructions',
164 // TRANS: Instructions for "Edit application" form.
165 _('Use this form to edit your application.'));
171 $name = $this->trimmed('name');
172 $description = $this->trimmed('description');
173 $source_url = $this->trimmed('source_url');
174 $organization = $this->trimmed('organization');
175 $homepage = $this->trimmed('homepage');
176 $callback_url = $this->trimmed('callback_url');
177 $type = $this->arg('app_type');
178 $access_type = $this->arg('default_access_type');
181 // TRANS: Validation error shown when not providing a name in the "Edit application" form.
182 $this->showForm(_('Name is required.'));
184 } elseif (mb_strlen($name) > 255) {
185 // TRANS: Validation error shown when providing too long a name in the "Edit application" form.
186 $this->showForm(_('Name is too long (max 255 characters).'));
188 } else if ($this->nameExists($name)) {
189 // TRANS: Validation error shown when providing a name for an application that already exists in the "Edit application" form.
190 $this->showForm(_('Name already in use. Try another one.'));
192 } elseif (empty($description)) {
193 // TRANS: Validation error shown when not providing a description in the "Edit application" form.
194 $this->showForm(_('Description is required.'));
196 } elseif (Oauth_application::descriptionTooLong($description)) {
197 $this->showForm(sprintf(
198 // TRANS: Validation error shown when providing too long a description in the "Edit application" form.
199 _m('Description is too long (maximum %d character).',
200 'Description is too long (maximum %d characters).',
201 Oauth_application::maxDesc()),
202 Oauth_application::maxDesc()));
204 } elseif (mb_strlen($source_url) > 255) {
205 // TRANS: Validation error shown when providing too long a source URL in the "Edit application" form.
206 $this->showForm(_('Source URL is too long.'));
208 } elseif ((mb_strlen($source_url) > 0)
209 && !Validate::uri($source_url,
210 array('allowed_schemes' => array('http', 'https'))))
212 // TRANS: Validation error shown when providing an invalid source URL in the "Edit application" form.
213 $this->showForm(_('Source URL is not valid.'));
215 } elseif (empty($organization)) {
216 // TRANS: Validation error shown when not providing an organisation in the "Edit application" form.
217 $this->showForm(_('Organization is required.'));
219 } elseif (mb_strlen($organization) > 255) {
220 // TRANS: Validation error shown when providing too long an arganisation name in the "Edit application" form.
221 $this->showForm(_('Organization is too long (maximum 255 characters).'));
223 } elseif (empty($homepage)) {
224 $this->showForm(_('Organization homepage is required.'));
226 } elseif ((mb_strlen($homepage) > 0)
227 && !Validate::uri($homepage,
228 array('allowed_schemes' => array('http', 'https'))))
230 // TRANS: Validation error shown when providing an invalid homepage URL in the "Edit application" form.
231 $this->showForm(_('Homepage is not a valid URL.'));
233 } elseif (mb_strlen($callback_url) > 255) {
234 // TRANS: Validation error shown when providing too long a callback URL in the "Edit application" form.
235 $this->showForm(_('Callback is too long.'));
237 } elseif (mb_strlen($callback_url) > 0
238 && !Validate::uri($source_url,
239 array('allowed_schemes' => array('http', 'https'))
242 // TRANS: Validation error shown when providing an invalid callback URL in the "Edit application" form.
243 $this->showForm(_('Callback URL is not valid.'));
247 $cur = common_current_user();
249 // Checked in prepare() above
251 assert(!is_null($cur));
252 assert(!is_null($this->app));
254 $orig = clone($this->app);
256 $this->app->name = $name;
257 $this->app->description = $description;
258 $this->app->source_url = $source_url;
259 $this->app->organization = $organization;
260 $this->app->homepage = $homepage;
261 $this->app->callback_url = $callback_url;
262 $this->app->type = $type;
264 common_debug("access_type = $access_type");
266 if ($access_type == 'r') {
267 $this->app->access_type = 1;
269 $this->app->access_type = 3;
272 $result = $this->app->update($orig);
274 // Note: 0 means no rows changed, which can happen if the only
275 // thing we changed was the icon, since it's not altered until
277 if ($result === false) {
278 common_log_db_error($this->app, 'UPDATE', __FILE__);
279 // TRANS: Server error occuring when an application could not be updated from the "Edit application" form.
280 $this->serverError(_('Could not update application.'));
283 $this->app->uploadLogo();
285 common_redirect(common_local_url('oauthappssettings'), 303);
289 * Does the app name already exist?
291 * Checks the DB to see someone has already registered an app
292 * with the same name.
294 * @param string $name app name to check
296 * @return boolean true if the name already exists
298 function nameExists($name)
300 $newapp = Oauth_application::staticGet('name', $name);
301 if (empty($newapp)) {
304 return $newapp->id != $this->app->id;