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('GNUSOCIAL')) { exit(1); }
34 * Add a new application
36 * This is the form for adding a new application
38 * @category Application
40 * @author Zach Copley <zach@status.net>
41 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
42 * @link http://status.net/
44 class NewApplicationAction extends SettingsAction
48 // TRANS: This is the title of the form for adding a new application.
49 return _('New application');
52 protected function doPost()
54 if ($this->arg('cancel')) {
55 common_redirect(common_local_url('oauthappssettings'), 303);
56 } elseif ($this->arg('save')) {
57 //trySave will never return, just throw exception or redirect
61 // TRANS: Client error displayed when encountering an unexpected action on form submission.
62 $this->clientError(_('Unexpected form submission.'));
65 protected function getForm()
67 return new ApplicationEditForm($this);
70 protected function getInstructions()
72 // TRANS: Form instructions for registering a new application.
73 return _('Use this form to register a new application.');
76 protected function trySave()
78 $name = $this->trimmed('name');
79 $description = $this->trimmed('description');
80 $source_url = $this->trimmed('source_url');
81 $organization = $this->trimmed('organization');
82 $homepage = $this->trimmed('homepage');
83 $callback_url = $this->trimmed('callback_url');
84 $type = $this->arg('app_type');
85 $access_type = $this->arg('default_access_type');
88 // TRANS: Validation error shown when not providing a name in the "New application" form.
89 $this->clientError(_('Name is required.'));
90 } else if ($this->nameExists($name)) {
91 // TRANS: Validation error shown when providing a name for an application that already exists in the "New application" form.
92 $this->clientError(_('Name already in use. Try another one.'));
93 } elseif (mb_strlen($name) > 255) {
94 // TRANS: Validation error shown when providing too long a name in the "New application" form.
95 $this->clientError(_('Name is too long (maximum 255 characters).'));
96 } elseif (empty($description)) {
97 // TRANS: Validation error shown when not providing a description in the "New application" form.
98 $this->clientError(_('Description is required.'));
99 } elseif (Oauth_application::descriptionTooLong($description)) {
100 $this->clientError(sprintf(
101 // TRANS: Form validation error in New application form.
102 // TRANS: %d is the maximum number of characters for the description.
103 _m('Description is too long (maximum %d character).',
104 'Description is too long (maximum %d characters).',
105 Oauth_application::maxDesc()),
106 Oauth_application::maxDesc()));
107 } elseif (empty($source_url)) {
108 // TRANS: Validation error shown when not providing a source URL in the "New application" form.
109 $this->clientError(_('Source URL is required.'));
110 } elseif ((strlen($source_url) > 0) && !common_valid_http_url($source_url)) {
111 // TRANS: Validation error shown when providing an invalid source URL in the "New application" form.
112 $this->clientError(_('Source URL is not valid.'));
113 } elseif (empty($organization)) {
114 // TRANS: Validation error shown when not providing an organisation in the "New application" form.
115 $this->clientError(_('Organization is required.'));
116 } elseif (mb_strlen($organization) > 255) {
117 // TRANS: Validation error shown when providing too long an arganisation name in the "Edit application" form.
118 $this->clientError(_('Organization is too long (maximum 255 characters).'));
119 } elseif (empty($homepage)) {
120 // TRANS: Form validation error show when an organisation name has not been provided in the new application form.
121 $this->clientError(_('Organization homepage is required.'));
122 } elseif ((strlen($homepage) > 0) && !common_valid_http_url($homepage)) {
123 // TRANS: Validation error shown when providing an invalid homepage URL in the "New application" form.
124 $this->clientError(_('Homepage is not a valid URL.'));
125 } elseif (mb_strlen($callback_url) > 255) {
126 // TRANS: Validation error shown when providing too long a callback URL in the "New application" form.
127 $this->clientError(_('Callback is too long.'));
128 } elseif (strlen($callback_url) > 0 && !common_valid_http_url($callback_url)) {
129 // TRANS: Validation error shown when providing an invalid callback URL in the "New application" form.
130 $this->clientError(_('Callback URL is not valid.'));
133 // Login is checked in parent::prepare()
134 assert(!is_null($this->scoped));
136 $app = new Oauth_application();
138 $app->query('BEGIN');
141 $app->owner = $this->scoped->getID();
142 $app->description = $description;
143 $app->source_url = $source_url;
144 $app->organization = $organization;
145 $app->homepage = $homepage;
146 $app->callback_url = $callback_url;
149 // Yeah, I dunno why I chose bit flags. I guess so I could
150 // copy this value directly to Oauth_application_user
151 // access_type which I think does need bit flags -- Z
153 if ($access_type == 'r') {
154 $app->setAccessFlags(true, false);
156 $app->setAccessFlags(true, true);
159 $app->created = common_sql_now();
161 // generate consumer key and secret
163 $consumer = Consumer::generateNew();
165 $result = $consumer->insert();
168 common_log_db_error($consumer, 'INSERT', __FILE__);
169 $app->query('ROLLBACK');
170 // TRANS: Server error displayed when an application could not be registered in the database through the "New application" form.
171 $this->serverError(_('Could not create application.'));
174 $app->consumer_key = $consumer->consumer_key;
176 $this->app_id = $app->insert();
178 if (!$this->app_id) {
179 common_log_db_error($app, 'INSERT', __FILE__);
180 $app->query('ROLLBACK');
181 // TRANS: Server error displayed when an application could not be registered in the database through the "New application" form.
182 $this->serverError(_('Could not create application.'));
187 } catch (Exception $e) {
188 $app->query('ROLLBACK');
189 // TRANS: Form validation error messages displayed when uploading an invalid application logo.
190 $this->clientError(_('Invalid image.'));
193 $app->query('COMMIT');
195 common_redirect(common_local_url('oauthappssettings'), 303);
199 * Does the app name already exist?
201 * Checks the DB to see someone has already registered an app
202 * with the same name.
204 * @param string $name app name to check
206 * @return boolean true if the name already exists
208 function nameExists($name)
210 $app = Oauth_application::getKV('name', $name);