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/
46 class NewApplicationAction extends OwnerDesignAction
52 return _('New Application');
59 function prepare($args)
61 parent::prepare($args);
63 if (!common_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
81 function handle($args)
83 parent::handle($args);
85 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
86 $this->handlePost($args);
92 function handlePost($args)
94 // Workaround for PHP returning empty $_POST and $_FILES when POST
95 // length > post_max_size in php.ini
99 && ($_SERVER['CONTENT_LENGTH'] > 0)
101 $msg = _('The server was unable to handle that much POST ' .
102 'data (%s bytes) due to its current configuration.');
103 $this->clientException(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
108 $token = $this->trimmed('token');
109 if (!$token || $token != common_session_token()) {
110 $this->clientError(_('There was a problem with your session token.'));
114 $cur = common_current_user();
116 if ($this->arg('cancel')) {
117 common_redirect(common_local_url('apps',
118 array('nickname' => $cur->nickname)), 303);
119 } elseif ($this->arg('save')) {
122 $this->clientError(_('Unexpected form submission.'));
126 function showForm($msg=null)
132 function showContent()
134 $form = new ApplicationEditForm($this);
138 function showPageNotice()
141 $this->element('p', 'error', $this->msg);
143 $this->element('p', 'instructions',
144 _('Use this form to register a new application.'));
150 $name = $this->trimmed('name');
151 $description = $this->trimmed('description');
152 $source_url = $this->trimmed('source_url');
153 $organization = $this->trimmed('organization');
154 $homepage = $this->trimmed('homepage');
155 $callback_url = $this->trimmed('callback_url');
156 $type = $this->arg('app_type');
157 $access_type = $this->arg('default_access_type');
160 $this->showForm(_('Name is required.'));
162 } elseif (mb_strlen($name) > 255) {
163 $this->showForm(_('Name is too long (max 255 chars).'));
165 } elseif (empty($description)) {
166 $this->showForm(_('Description is required.'));
168 } elseif (Oauth_application::descriptionTooLong($description)) {
169 $this->showForm(sprintf(
170 _('Description is too long (max %d chars).'),
171 Oauth_application::maxDescription()));
173 } elseif (empty($source_url)) {
174 $this->showForm(_('Source URL is required.'));
176 } elseif ((strlen($source_url) > 0)
179 array('allowed_schemes' => array('http', 'https'))
183 $this->showForm(_('Source URL is not valid.'));
185 } elseif (empty($organization)) {
186 $this->showForm(_('Organization is required.'));
188 } elseif (mb_strlen($organization) > 255) {
189 $this->showForm(_('Organization is too long (max 255 chars).'));
191 } elseif (empty($homepage)) {
192 $this->showForm(_('Organization homepage is required.'));
194 } elseif ((strlen($homepage) > 0)
197 array('allowed_schemes' => array('http', 'https'))
201 $this->showForm(_('Homepage is not a valid URL.'));
203 } elseif (mb_strlen($callback_url) > 255) {
204 $this->showForm(_('Callback is too long.'));
206 } elseif (strlen($callback_url) > 0
209 array('allowed_schemes' => array('http', 'https'))
213 $this->showForm(_('Callback URL is not valid.'));
217 $cur = common_current_user();
219 // Checked in prepare() above
221 assert(!is_null($cur));
223 $app = new Oauth_application();
225 $app->query('BEGIN');
228 $app->owner = $cur->id;
229 $app->description = $description;
230 $app->source_url = $source_url;
231 $app->organization = $organization;
232 $app->homepage = $homepage;
233 $app->callback_url = $callback_url;
236 // Yeah, I dunno why I chose bit flags. I guess so I could
237 // copy this value directly to Oauth_application_user
238 // access_type which I think does need bit flags -- Z
240 if ($access_type == 'r') {
241 $app->setAccessFlags(true, false);
243 $app->setAccessFlags(true, true);
246 $app->created = common_sql_now();
248 // generate consumer key and secret
250 $consumer = Consumer::generateNew();
252 $result = $consumer->insert();
255 common_log_db_error($consumer, 'INSERT', __FILE__);
256 $this->serverError(_('Could not create application.'));
259 $app->consumer_key = $consumer->consumer_key;
261 $this->app_id = $app->insert();
263 if (!$this->app_id) {
264 common_log_db_error($app, 'INSERT', __FILE__);
265 $this->serverError(_('Could not create application.'));
266 $app->query('ROLLBACK');
269 $this->app->uploadLogo();
271 $app->query('COMMIT');
273 common_redirect(common_local_url('apps',
274 array('nickname' => $cur->nickname)), 303);