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 * @copyright 2013 Free Software Foundation, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28 * @link http://status.net/
31 if (!defined('STATUSNET')) {
36 * Add a new application
38 * This is the form for adding a new application
40 * @category Application
42 * @author Zach Copley <zach@status.net>
43 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44 * @link http://status.net/
46 class NewApplicationAction extends FormAction
50 // TRANS: This is the title of the form for adding a new application.
51 return _('New application');
54 protected function handlePost()
58 if ($this->arg('cancel')) {
59 common_redirect(common_local_url('oauthappssettings'), 303);
60 } elseif ($this->arg('save')) {
64 // TRANS: Client error displayed when encountering an unexpected action on form submission.
65 $this->clientError(_('Unexpected form submission.'));
68 function showForm($msg=null)
74 function showContent()
76 $form = new ApplicationEditForm($this);
80 function showPageNotice()
83 $this->element('p', 'error', $this->msg);
85 $this->element('p', 'instructions',
86 // TRANS: Form instructions for registering a new application.
87 _('Use this form to register a new application.'));
91 private function trySave()
93 $name = $this->trimmed('name');
94 $description = $this->trimmed('description');
95 $source_url = $this->trimmed('source_url');
96 $organization = $this->trimmed('organization');
97 $homepage = $this->trimmed('homepage');
98 $callback_url = $this->trimmed('callback_url');
99 $type = $this->arg('app_type');
100 $access_type = $this->arg('default_access_type');
103 // TRANS: Validation error shown when not providing a name in the "New application" form.
104 $this->clientError(_('Name is required.'));
105 } else if ($this->nameExists($name)) {
106 // TRANS: Validation error shown when providing a name for an application that already exists in the "New application" form.
107 $this->clientError(_('Name already in use. Try another one.'));
108 } elseif (mb_strlen($name) > 255) {
109 // TRANS: Validation error shown when providing too long a name in the "New application" form.
110 $this->clientError(_('Name is too long (maximum 255 characters).'));
111 } elseif (empty($description)) {
112 // TRANS: Validation error shown when not providing a description in the "New application" form.
113 $this->clientError(_('Description is required.'));
114 } elseif (Oauth_application::descriptionTooLong($description)) {
115 $this->clientError(sprintf(
116 // TRANS: Form validation error in New application form.
117 // TRANS: %d is the maximum number of characters for the description.
118 _m('Description is too long (maximum %d character).',
119 'Description is too long (maximum %d characters).',
120 Oauth_application::maxDesc()),
121 Oauth_application::maxDesc()));
122 } elseif (empty($source_url)) {
123 // TRANS: Validation error shown when not providing a source URL in the "New application" form.
124 $this->clientError(_('Source URL is required.'));
125 } elseif ((strlen($source_url) > 0)
128 array('allowed_schemes' => array('http', 'https'))
131 // TRANS: Validation error shown when providing an invalid source URL in the "New application" form.
132 $this->clientError(_('Source URL is not valid.'));
133 } elseif (empty($organization)) {
134 // TRANS: Validation error shown when not providing an organisation in the "New application" form.
135 $this->clientError(_('Organization is required.'));
136 } elseif (mb_strlen($organization) > 255) {
137 // TRANS: Validation error shown when providing too long an arganisation name in the "Edit application" form.
138 $this->clientError(_('Organization is too long (maximum 255 characters).'));
139 } elseif (empty($homepage)) {
140 // TRANS: Form validation error show when an organisation name has not been provided in the new application form.
141 $this->clientError(_('Organization homepage is required.'));
142 } elseif ((strlen($homepage) > 0)
145 array('allowed_schemes' => array('http', 'https'))
148 // TRANS: Validation error shown when providing an invalid homepage URL in the "New application" form.
149 $this->clientError(_('Homepage is not a valid URL.'));
150 } elseif (mb_strlen($callback_url) > 255) {
151 // TRANS: Validation error shown when providing too long a callback URL in the "New application" form.
152 $this->clientError(_('Callback is too long.'));
153 } elseif (strlen($callback_url) > 0
156 array('allowed_schemes' => array('http', 'https'))
159 // TRANS: Validation error shown when providing an invalid callback URL in the "New application" form.
160 $this->clientError(_('Callback URL is not valid.'));
163 // Login is checked in parent::prepare()
164 assert(!is_null($this->scoped));
166 $app = new Oauth_application();
168 $app->query('BEGIN');
171 $app->owner = $this->scoped->id;
172 $app->description = $description;
173 $app->source_url = $source_url;
174 $app->organization = $organization;
175 $app->homepage = $homepage;
176 $app->callback_url = $callback_url;
179 // Yeah, I dunno why I chose bit flags. I guess so I could
180 // copy this value directly to Oauth_application_user
181 // access_type which I think does need bit flags -- Z
183 if ($access_type == 'r') {
184 $app->setAccessFlags(true, false);
186 $app->setAccessFlags(true, true);
189 $app->created = common_sql_now();
191 // generate consumer key and secret
193 $consumer = Consumer::generateNew();
195 $result = $consumer->insert();
198 common_log_db_error($consumer, 'INSERT', __FILE__);
199 // TRANS: Server error displayed when an application could not be registered in the database through the "New application" form.
200 $this->serverError(_('Could not create application.'));
203 $app->consumer_key = $consumer->consumer_key;
205 $this->app_id = $app->insert();
207 if (!$this->app_id) {
208 common_log_db_error($app, 'INSERT', __FILE__);
209 // TRANS: Server error displayed when an application could not be registered in the database through the "New application" form.
210 $this->serverError(_('Could not create application.'));
211 $app->query('ROLLBACK');
216 } catch (Exception $e) {
217 $app->query('ROLLBACK');
218 // TRANS: Form validation error messages displayed when uploading an invalid application logo.
219 $this->clientError(_('Invalid image.'));
222 $app->query('COMMIT');
224 common_redirect(common_local_url('oauthappssettings'), 303);
228 * Does the app name already exist?
230 * Checks the DB to see someone has already registered an app
231 * with the same name.
233 * @param string $name app name to check
235 * @return boolean true if the name already exists
237 function nameExists($name)
239 $app = Oauth_application::getKV('name', $name);