3 * StatusNet, the distributed open-source microblogging tool
5 * Register a new 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 * Add a new application
37 * This is the form for adding a new 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 NewApplicationAction extends OwnerDesignAction
51 // TRANS: This is the title of the form for adding a new application.
52 return _('New application');
58 function prepare($args)
60 parent::prepare($args);
62 if (!common_logged_in()) {
63 // TRANS: Client error displayed trying to add a new application while not logged in.
64 $this->clientError(_('You must be logged in to register an application.'));
74 * On GET, show the form. On POST, try to save the app.
76 * @param array $args unused
80 function handle($args)
82 parent::handle($args);
84 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
85 $this->handlePost($args);
91 function handlePost($args)
93 // Workaround for PHP returning empty $_POST and $_FILES when POST
94 // length > post_max_size in php.ini
98 && ($_SERVER['CONTENT_LENGTH'] > 0)
100 // TRANS: Client error displayed when the number of bytes in a POST request exceeds a limit.
101 // TRANS: %s is the number of bytes of the CONTENT_LENGTH.
102 $msg = _m('The server was unable to handle that much POST data (%s byte) due to its current configuration.',
103 'The server was unable to handle that much POST data (%s bytes) due to its current configuration.',
104 intval($_SERVER['CONTENT_LENGTH']));
105 $this->clientException(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
110 $token = $this->trimmed('token');
111 if (!$token || $token != common_session_token()) {
112 $this->clientError(_('There was a problem with your session token.'));
116 $cur = common_current_user();
118 if ($this->arg('cancel')) {
119 common_redirect(common_local_url('oauthappssettings'), 303);
120 } elseif ($this->arg('save')) {
123 // TRANS: Client error displayed when encountering an unexpected action on form submission.
124 $this->clientError(_('Unexpected form submission.'));
128 function showForm($msg=null)
134 function showContent()
136 $form = new ApplicationEditForm($this);
140 function showPageNotice()
143 $this->element('p', 'error', $this->msg);
145 $this->element('p', 'instructions',
146 // TRANS: Form instructions for registering a new application.
147 _('Use this form to register a new application.'));
153 $name = $this->trimmed('name');
154 $description = $this->trimmed('description');
155 $source_url = $this->trimmed('source_url');
156 $organization = $this->trimmed('organization');
157 $homepage = $this->trimmed('homepage');
158 $callback_url = $this->trimmed('callback_url');
159 $type = $this->arg('app_type');
160 $access_type = $this->arg('default_access_type');
163 // TRANS: Validation error shown when not providing a name in the "New application" form.
164 $this->showForm(_('Name is required.'));
166 } else if ($this->nameExists($name)) {
167 // TRANS: Validation error shown when providing a name for an application that already exists in the "New application" form.
168 $this->showForm(_('Name already in use. Try another one.'));
170 } elseif (mb_strlen($name) > 255) {
171 // TRANS: Validation error shown when providing too long a name in the "New application" form.
172 $this->showForm(_('Name is too long (maximum 255 characters).'));
174 } elseif (empty($description)) {
175 // TRANS: Validation error shown when not providing a description in the "New application" form.
176 $this->showForm(_('Description is required.'));
178 } elseif (Oauth_application::descriptionTooLong($description)) {
179 $this->showForm(sprintf(
180 // TRANS: Form validation error in New application form.
181 // TRANS: %d is the maximum number of characters for the description.
182 _m('Description is too long (maximum %d character).',
183 'Description is too long (maximum %d characters).',
184 Oauth_application::maxDesc()),
185 Oauth_application::maxDesc()));
187 } elseif (empty($source_url)) {
188 // TRANS: Validation error shown when not providing a source URL in the "New application" form.
189 $this->showForm(_('Source URL is required.'));
191 } elseif ((strlen($source_url) > 0)
194 array('allowed_schemes' => array('http', 'https'))
198 // TRANS: Validation error shown when providing an invalid source URL in the "New application" form.
199 $this->showForm(_('Source URL is not valid.'));
201 } elseif (empty($organization)) {
202 // TRANS: Validation error shown when not providing an organisation in the "New application" form.
203 $this->showForm(_('Organization is required.'));
205 } elseif (mb_strlen($organization) > 255) {
206 // TRANS: Validation error shown when providing too long an arganisation name in the "Edit application" form.
207 $this->showForm(_('Organization is too long (maximum 255 characters).'));
209 } elseif (empty($homepage)) {
210 // TRANS: Form validation error show when an organisation name has not been provided in the new application form.
211 $this->showForm(_('Organization homepage is required.'));
213 } elseif ((strlen($homepage) > 0)
216 array('allowed_schemes' => array('http', 'https'))
220 // TRANS: Validation error shown when providing an invalid homepage URL in the "New application" form.
221 $this->showForm(_('Homepage is not a valid URL.'));
223 } elseif (mb_strlen($callback_url) > 255) {
224 // TRANS: Validation error shown when providing too long a callback URL in the "New application" form.
225 $this->showForm(_('Callback is too long.'));
227 } elseif (strlen($callback_url) > 0
230 array('allowed_schemes' => array('http', 'https'))
234 // TRANS: Validation error shown when providing an invalid callback URL in the "New application" form.
235 $this->showForm(_('Callback URL is not valid.'));
239 $cur = common_current_user();
241 // Checked in prepare() above
243 assert(!is_null($cur));
245 $app = new Oauth_application();
247 $app->query('BEGIN');
250 $app->owner = $cur->id;
251 $app->description = $description;
252 $app->source_url = $source_url;
253 $app->organization = $organization;
254 $app->homepage = $homepage;
255 $app->callback_url = $callback_url;
258 // Yeah, I dunno why I chose bit flags. I guess so I could
259 // copy this value directly to Oauth_application_user
260 // access_type which I think does need bit flags -- Z
262 if ($access_type == 'r') {
263 $app->setAccessFlags(true, false);
265 $app->setAccessFlags(true, true);
268 $app->created = common_sql_now();
270 // generate consumer key and secret
272 $consumer = Consumer::generateNew();
274 $result = $consumer->insert();
277 common_log_db_error($consumer, 'INSERT', __FILE__);
278 // TRANS: Server error displayed when an application could not be registered in the database through the "New application" form.
279 $this->serverError(_('Could not create application.'));
282 $app->consumer_key = $consumer->consumer_key;
284 $this->app_id = $app->insert();
286 if (!$this->app_id) {
287 common_log_db_error($app, 'INSERT', __FILE__);
288 // TRANS: Server error displayed when an application could not be registered in the database through the "New application" form.
289 $this->serverError(_('Could not create application.'));
290 $app->query('ROLLBACK');
295 } catch (Exception $e) {
296 $app->query('ROLLBACK');
297 // TRANS: Form validation error on New application page when providing an invalid image upload.
298 $this->showForm(_('Invalid image.'));
302 $app->query('COMMIT');
304 common_redirect(common_local_url('oauthappssettings'), 303);
308 * Does the app name already exist?
310 * Checks the DB to see someone has already registered an app
311 * with the same name.
313 * @param string $name app name to check
315 * @return boolean true if the name already exists
317 function nameExists($name)
319 $app = Oauth_application::staticGet('name', $name);