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 group.
76 * @param array $args unused
81 function handle($args)
83 parent::handle($args);
85 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
88 $token = $this->trimmed('token');
89 if (!$token || $token != common_session_token()) {
90 $this->clientError(_('There was a problem with your session token.'));
94 $cur = common_current_user();
96 if ($this->arg('cancel')) {
97 common_redirect(common_local_url('apps',
98 array('nickname' => $cur->nickname)), 303);
99 } elseif ($this->arg('save')) {
102 $this->clientError(_('Unexpected form submission.'));
109 function showForm($msg=null)
115 function showContent()
117 $form = new ApplicationEditForm($this);
121 function showPageNotice()
124 $this->element('p', 'error', $this->msg);
126 $this->element('p', 'instructions',
127 _('Use this form to register a new application.'));
133 $name = $this->trimmed('name');
134 $description = $this->trimmed('description');
135 $source_url = $this->trimmed('source_url');
136 $organization = $this->trimmed('organization');
137 $homepage = $this->trimmed('homepage');
138 $callback_url = $this->trimmed('callback_url');
139 $type = $this->arg('app_type');
140 $access_type = $this->arg('access_type');
143 $this->showForm(_('Name is required.'));
145 } elseif (mb_strlen($name) > 255) {
146 $this->showForm(_('Name is too long (max 255 chars).'));
148 } elseif (empty($description)) {
149 $this->showForm(_('Description is required.'));
151 } elseif (Oauth_application::descriptionTooLong($description)) {
152 $this->showForm(sprintf(
153 _('Description is too long (max %d chars).'),
154 Oauth_application::maxDescription()));
156 } elseif (empty($source_url)) {
157 $this->showForm(_('Source URL is required.'));
159 } elseif ((strlen($source_url) > 0)
162 array('allowed_schemes' => array('http', 'https'))
166 $this->showForm(_('Source URL is not valid.'));
168 } elseif (empty($organization)) {
169 $this->showForm(_('Organization is required.'));
171 } elseif (mb_strlen($organization) > 255) {
172 $this->showForm(_('Organization is too long (max 255 chars).'));
174 } elseif (empty($homepage)) {
175 $this->showForm(_('Organization homepage is required.'));
177 } elseif ((strlen($homepage) > 0)
180 array('allowed_schemes' => array('http', 'https'))
184 $this->showForm(_('Homepage is not a valid URL.'));
186 } elseif (empty($callback_url)) {
187 $this->showForm(_('Callback is required.'));
189 } elseif (strlen($callback_url) > 0
192 array('allowed_schemes' => array('http', 'https'))
196 $this->showForm(_('Callback URL is not valid.'));
200 $cur = common_current_user();
202 // Checked in prepare() above
204 assert(!is_null($cur));
206 $app = new Oauth_application();
208 $app->query('BEGIN');
211 $app->owner = $cur->id;
212 $app->description = $description;
213 $app->source_url = $source_url;
214 $app->organization = $organization;
215 $app->homepage = $homepage;
216 $app->callback_url = $callback_url;
219 // Yeah, I dunno why I chose bit flags. I guess so I could
220 // copy this value directly to Oauth_application_user
221 // access_type which I think does need bit flags -- Z
223 if ($access_type == 'r') {
224 $app->setAccessFlags(true, false);
226 $app->setAccessFlags(true, true);
229 $app->created = common_sql_now();
231 // generate consumer key and secret
233 $consumer = Consumer::generateNew();
235 $result = $consumer->insert();
238 common_log_db_error($consumer, 'INSERT', __FILE__);
239 $this->serverError(_('Could not create application.'));
242 $app->consumer_key = $consumer->consumer_key;
244 $result = $app->insert();
247 common_log_db_error($app, 'INSERT', __FILE__);
248 $this->serverError(_('Could not create application.'));
249 $app->query('ROLLBACK');
252 $app->query('COMMIT');
254 common_redirect(common_local_url('apps',
255 array('nickname' => $cur->nickname)), 303);