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 Action
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 // TRANS: Client error displayed when the session token does not match or is not given.
113 $this->clientError(_('There was a problem with your session token.'));
117 $cur = common_current_user();
119 if ($this->arg('cancel')) {
120 common_redirect(common_local_url('oauthappssettings'), 303);
121 } elseif ($this->arg('save')) {
124 // TRANS: Client error displayed when encountering an unexpected action on form submission.
125 $this->clientError(_('Unexpected form submission.'));
129 function showForm($msg=null)
135 function showContent()
137 $form = new ApplicationEditForm($this);
141 function showPageNotice()
144 $this->element('p', 'error', $this->msg);
146 $this->element('p', 'instructions',
147 // TRANS: Form instructions for registering a new application.
148 _('Use this form to register a new application.'));
154 $name = $this->trimmed('name');
155 $description = $this->trimmed('description');
156 $source_url = $this->trimmed('source_url');
157 $organization = $this->trimmed('organization');
158 $homepage = $this->trimmed('homepage');
159 $callback_url = $this->trimmed('callback_url');
160 $type = $this->arg('app_type');
161 $access_type = $this->arg('default_access_type');
164 // TRANS: Validation error shown when not providing a name in the "New application" form.
165 $this->showForm(_('Name is required.'));
167 } else if ($this->nameExists($name)) {
168 // TRANS: Validation error shown when providing a name for an application that already exists in the "New application" form.
169 $this->showForm(_('Name already in use. Try another one.'));
171 } elseif (mb_strlen($name) > 255) {
172 // TRANS: Validation error shown when providing too long a name in the "New application" form.
173 $this->showForm(_('Name is too long (maximum 255 characters).'));
175 } elseif (empty($description)) {
176 // TRANS: Validation error shown when not providing a description in the "New application" form.
177 $this->showForm(_('Description is required.'));
179 } elseif (Oauth_application::descriptionTooLong($description)) {
180 $this->showForm(sprintf(
181 // TRANS: Form validation error in New application form.
182 // TRANS: %d is the maximum number of characters for the description.
183 _m('Description is too long (maximum %d character).',
184 'Description is too long (maximum %d characters).',
185 Oauth_application::maxDesc()),
186 Oauth_application::maxDesc()));
188 } elseif (empty($source_url)) {
189 // TRANS: Validation error shown when not providing a source URL in the "New application" form.
190 $this->showForm(_('Source URL is required.'));
192 } elseif ((strlen($source_url) > 0)
195 array('allowed_schemes' => array('http', 'https'))
199 // TRANS: Validation error shown when providing an invalid source URL in the "New application" form.
200 $this->showForm(_('Source URL is not valid.'));
202 } elseif (empty($organization)) {
203 // TRANS: Validation error shown when not providing an organisation in the "New application" form.
204 $this->showForm(_('Organization is required.'));
206 } elseif (mb_strlen($organization) > 255) {
207 // TRANS: Validation error shown when providing too long an arganisation name in the "Edit application" form.
208 $this->showForm(_('Organization is too long (maximum 255 characters).'));
210 } elseif (empty($homepage)) {
211 // TRANS: Form validation error show when an organisation name has not been provided in the new application form.
212 $this->showForm(_('Organization homepage is required.'));
214 } elseif ((strlen($homepage) > 0)
217 array('allowed_schemes' => array('http', 'https'))
221 // TRANS: Validation error shown when providing an invalid homepage URL in the "New application" form.
222 $this->showForm(_('Homepage is not a valid URL.'));
224 } elseif (mb_strlen($callback_url) > 255) {
225 // TRANS: Validation error shown when providing too long a callback URL in the "New application" form.
226 $this->showForm(_('Callback is too long.'));
228 } elseif (strlen($callback_url) > 0
231 array('allowed_schemes' => array('http', 'https'))
235 // TRANS: Validation error shown when providing an invalid callback URL in the "New application" form.
236 $this->showForm(_('Callback URL is not valid.'));
240 $cur = common_current_user();
242 // Checked in prepare() above
244 assert(!is_null($cur));
246 $app = new Oauth_application();
248 $app->query('BEGIN');
251 $app->owner = $cur->id;
252 $app->description = $description;
253 $app->source_url = $source_url;
254 $app->organization = $organization;
255 $app->homepage = $homepage;
256 $app->callback_url = $callback_url;
259 // Yeah, I dunno why I chose bit flags. I guess so I could
260 // copy this value directly to Oauth_application_user
261 // access_type which I think does need bit flags -- Z
263 if ($access_type == 'r') {
264 $app->setAccessFlags(true, false);
266 $app->setAccessFlags(true, true);
269 $app->created = common_sql_now();
271 // generate consumer key and secret
273 $consumer = Consumer::generateNew();
275 $result = $consumer->insert();
278 common_log_db_error($consumer, 'INSERT', __FILE__);
279 // TRANS: Server error displayed when an application could not be registered in the database through the "New application" form.
280 $this->serverError(_('Could not create application.'));
283 $app->consumer_key = $consumer->consumer_key;
285 $this->app_id = $app->insert();
287 if (!$this->app_id) {
288 common_log_db_error($app, 'INSERT', __FILE__);
289 // TRANS: Server error displayed when an application could not be registered in the database through the "New application" form.
290 $this->serverError(_('Could not create application.'));
291 $app->query('ROLLBACK');
296 } catch (Exception $e) {
297 $app->query('ROLLBACK');
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);