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/
46 class EditApplicationAction extends OwnerDesignAction
54 return _('Edit application');
61 function prepare($args)
63 parent::prepare($args);
65 if (!common_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 $this->clientError(_('You are not the owner of this application.'), 401);
81 $this->clientError(_('No such application.'));
91 * On GET, show the form. On POST, try to save the app.
93 * @param array $args unused
98 function handle($args)
100 parent::handle($args);
102 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
103 $this->handlePost($args);
109 function handlePost($args)
111 // Workaround for PHP returning empty $_POST and $_FILES when POST
112 // length > post_max_size in php.ini
116 && ($_SERVER['CONTENT_LENGTH'] > 0)
118 $msg = _('The server was unable to handle that much POST ' .
119 'data (%s bytes) due to its current configuration.');
120 $this->clientException(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
125 $token = $this->trimmed('token');
126 if (!$token || $token != common_session_token()) {
127 $this->clientError(_('There was a problem with your session token.'));
131 $cur = common_current_user();
133 if ($this->arg('cancel')) {
134 common_redirect(common_local_url('showapplication',
135 array('id' => $this->app->id)), 303);
136 } elseif ($this->arg('save')) {
139 $this->clientError(_('Unexpected form submission.'));
143 function showForm($msg=null)
149 function showContent()
151 $form = new ApplicationEditForm($this, $this->app);
155 function showPageNotice()
157 if (!empty($this->msg)) {
158 $this->element('p', 'error', $this->msg);
160 $this->element('p', 'instructions',
161 _('Use this form to edit your application.'));
167 $name = $this->trimmed('name');
168 $description = $this->trimmed('description');
169 $source_url = $this->trimmed('source_url');
170 $organization = $this->trimmed('organization');
171 $homepage = $this->trimmed('homepage');
172 $callback_url = $this->trimmed('callback_url');
173 $type = $this->arg('app_type');
174 $access_type = $this->arg('default_access_type');
177 $this->showForm(_('Name is required.'));
179 } elseif (mb_strlen($name) > 255) {
180 $this->showForm(_('Name is too long (max 255 chars).'));
182 } elseif (empty($description)) {
183 $this->showForm(_('Description is required.'));
185 } elseif (Oauth_application::descriptionTooLong($description)) {
186 $this->showForm(sprintf(
187 _('Description is too long (max %d chars).'),
188 Oauth_application::maxDescription()));
190 } elseif (mb_strlen($source_url) > 255) {
191 $this->showForm(_('Source URL is too long.'));
193 } elseif ((mb_strlen($source_url) > 0)
194 && !Validate::uri($source_url,
195 array('allowed_schemes' => array('http', 'https'))))
197 $this->showForm(_('Source URL is not valid.'));
199 } elseif (empty($organization)) {
200 $this->showForm(_('Organization is required.'));
202 } elseif (mb_strlen($organization) > 255) {
203 $this->showForm(_('Organization is too long (max 255 chars).'));
205 } elseif (empty($homepage)) {
206 $this->showForm(_('Organization homepage is required.'));
208 } elseif ((mb_strlen($homepage) > 0)
209 && !Validate::uri($homepage,
210 array('allowed_schemes' => array('http', 'https'))))
212 $this->showForm(_('Homepage is not a valid URL.'));
214 } elseif (mb_strlen($callback_url) > 255) {
215 $this->showForm(_('Callback is too long.'));
217 } elseif (mb_strlen($callback_url) > 0
218 && !Validate::uri($source_url,
219 array('allowed_schemes' => array('http', 'https'))
222 $this->showForm(_('Callback URL is not valid.'));
226 $cur = common_current_user();
228 // Checked in prepare() above
230 assert(!is_null($cur));
231 assert(!is_null($this->app));
233 $orig = clone($this->app);
235 $this->app->name = $name;
236 $this->app->description = $description;
237 $this->app->source_url = $source_url;
238 $this->app->organization = $organization;
239 $this->app->homepage = $homepage;
240 $this->app->callback_url = $callback_url;
241 $this->app->type = $type;
243 common_debug("access_type = $access_type");
245 if ($access_type == 'r') {
246 $this->app->access_type = 1;
248 $this->app->access_type = 3;
251 $result = $this->app->update($orig);
254 common_log_db_error($this->app, 'UPDATE', __FILE__);
255 $this->serverError(_('Could not update application.'));
258 $this->app->uploadLogo();
260 common_redirect(common_local_url('oauthappssettings'), 303);