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-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 * 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');
59 function prepare($args)
61 parent::prepare($args);
63 if (!common_logged_in()) {
64 // TRANS: Client error displayed trying to add a new application while not logged in.
65 $this->clientError(_('You must be logged in to register an application.'));
75 * On GET, show the form. On POST, try to save the app.
77 * @param array $args unused
82 function handle($args)
84 parent::handle($args);
86 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
87 $this->handlePost($args);
93 function handlePost($args)
95 // Workaround for PHP returning empty $_POST and $_FILES when POST
96 // length > post_max_size in php.ini
100 && ($_SERVER['CONTENT_LENGTH'] > 0)
102 // TRANS: Client error displayed when the number of bytes in a POST request exceeds a limit.
103 // TRANS: %s is the number of bytes of the CONTENT_LENGTH.
104 $msg = _m('The server was unable to handle that much POST data (%s byte) due to its current configuration.',
105 'The server was unable to handle that much POST data (%s bytes) due to its current configuration.',
106 intval($_SERVER['CONTENT_LENGTH']));
107 $this->clientException(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
112 $token = $this->trimmed('token');
113 if (!$token || $token != common_session_token()) {
114 $this->clientError(_('There was a problem with your session token.'));
118 $cur = common_current_user();
120 if ($this->arg('cancel')) {
121 common_redirect(common_local_url('oauthappssettings'), 303);
122 } elseif ($this->arg('save')) {
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 _('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 $this->showForm(_('Name is required.'));
165 } else if ($this->nameExists($name)) {
166 $this->showForm(_('Name already in use. Try another one.'));
168 } elseif (mb_strlen($name) > 255) {
169 $this->showForm(_('Name is too long (maximum 255 characters).'));
171 } elseif (empty($description)) {
172 $this->showForm(_('Description is required.'));
174 } elseif (Oauth_application::descriptionTooLong($description)) {
175 $this->showForm(sprintf(
176 // TRANS: Form validation error in New application form.
177 // TRANS: %d is the maximum number of characters for the description.
178 _m('Description is too long (maximum %d character).',
179 'Description is too long (maximum %d characters).',
180 Oauth_application::maxDesc()),
181 Oauth_application::maxDesc()));
183 } elseif (empty($source_url)) {
184 $this->showForm(_('Source URL is required.'));
186 } elseif ((strlen($source_url) > 0)
189 array('allowed_schemes' => array('http', 'https'))
193 $this->showForm(_('Source URL is not valid.'));
195 } elseif (empty($organization)) {
196 $this->showForm(_('Organization is required.'));
198 } elseif (mb_strlen($organization) > 255) {
199 $this->showForm(_('Organization is too long (maximum 255 characters).'));
201 } elseif (empty($homepage)) {
202 $this->showForm(_('Organization homepage is required.'));
204 } elseif ((strlen($homepage) > 0)
207 array('allowed_schemes' => array('http', 'https'))
211 $this->showForm(_('Homepage is not a valid URL.'));
213 } elseif (mb_strlen($callback_url) > 255) {
214 $this->showForm(_('Callback is too long.'));
216 } elseif (strlen($callback_url) > 0
219 array('allowed_schemes' => array('http', 'https'))
223 $this->showForm(_('Callback URL is not valid.'));
227 $cur = common_current_user();
229 // Checked in prepare() above
231 assert(!is_null($cur));
233 $app = new Oauth_application();
235 $app->query('BEGIN');
238 $app->owner = $cur->id;
239 $app->description = $description;
240 $app->source_url = $source_url;
241 $app->organization = $organization;
242 $app->homepage = $homepage;
243 $app->callback_url = $callback_url;
246 // Yeah, I dunno why I chose bit flags. I guess so I could
247 // copy this value directly to Oauth_application_user
248 // access_type which I think does need bit flags -- Z
250 if ($access_type == 'r') {
251 $app->setAccessFlags(true, false);
253 $app->setAccessFlags(true, true);
256 $app->created = common_sql_now();
258 // generate consumer key and secret
260 $consumer = Consumer::generateNew();
262 $result = $consumer->insert();
265 common_log_db_error($consumer, 'INSERT', __FILE__);
266 $this->serverError(_('Could not create application.'));
269 $app->consumer_key = $consumer->consumer_key;
271 $this->app_id = $app->insert();
273 if (!$this->app_id) {
274 common_log_db_error($app, 'INSERT', __FILE__);
275 $this->serverError(_('Could not create application.'));
276 $app->query('ROLLBACK');
281 $app->query('COMMIT');
283 common_redirect(common_local_url('oauthappssettings'), 303);
288 * Does the app name already exist?
290 * Checks the DB to see someone has already registered an app
291 * with the same name.
293 * @param string $name app name to check
295 * @return boolean true if the name already exists
298 function nameExists($name)
300 $app = Oauth_application::staticGet('name', $name);