3 * StatusNet, the distributed open-source microblogging tool
5 * Show an 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 Application
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 * Show an OAuth application
37 * @category Application
39 * @author Zach Copley <zach@status.net>
40 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41 * @link http://status.net/
44 class ShowApplicationAction extends OwnerDesignAction
50 var $application = null;
53 * User who owns the app
63 * Load attributes based on database arguments
65 * Loads all the DB stuff
67 * @param array $args $_REQUEST array
69 * @return success flag
72 function prepare($args)
74 parent::prepare($args);
76 $id = (int)$this->arg('id');
78 $this->application = Oauth_application::staticGet($id);
79 $this->owner = User::staticGet($this->application->owner);
81 if (!common_logged_in()) {
82 $this->clientError(_('You must be logged in to view an application.'));
86 if (empty($this->application)) {
87 $this->clientError(_('No such application.'), 404);
91 $cur = common_current_user();
93 if ($cur->id != $this->owner->id) {
94 $this->clientError(_('You are not the owner of this application.'), 401);
104 * Shows info about the app
109 function handle($args)
111 parent::handle($args);
113 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
116 $token = $this->trimmed('token');
117 if (!$token || $token != common_session_token()) {
118 $this->clientError(_('There was a problem with your session token.'));
122 if ($this->arg('reset')) {
133 * @return string title of the page
138 if (!empty($this->application->name)) {
139 return 'Application: ' . $this->application->name;
143 function showPageNotice()
145 if (!empty($this->msg)) {
146 $this->element('div', ($this->success) ? 'success' : 'error', $this->msg);
150 function showContent()
152 $cur = common_current_user();
154 $consumer = $this->application->getConsumer();
156 $this->elementStart('div', 'entity_profile vcard');
157 $this->element('h2', null, _('Application profile'));
158 if (!empty($this->application->icon)) {
159 $this->element('img', array('src' => $this->application->icon,
160 'class' => 'photo logo entity_depiction'));
163 $this->element('a', array('href' => $this->application->source_url,
164 'class' => 'url fn entity_fn'),
165 $this->application->name);
167 $this->element('a', array('href' => $this->application->homepage,
168 'class' => 'url entity_org'),
169 $this->application->organization);
171 $this->element('div',
173 $this->application->description);
175 $this->elementStart('div', 'entity_statistics');
176 $defaultAccess = ($this->application->access_type & Oauth_application::$writeAccess)
177 ? 'read-write' : 'read-only';
178 $profile = Profile::staticGet($this->application->owner);
180 $appUsers = new Oauth_application_user();
181 $appUsers->application_id = $this->application->id;
182 $userCnt = $appUsers->count();
185 _('Created by %1$s - %2$s access by default - %3$d users'),
186 $profile->getBestName(),
190 $this->elementEnd('div');
192 $this->elementEnd('div');
194 $this->elementStart('div', 'entity_actions');
195 $this->element('h2', null, _('Application actions'));
196 $this->elementStart('ul');
197 $this->elementStart('li', 'entity_edit');
199 array('href' => common_local_url('editapplication',
200 array('id' => $this->application->id))),
202 $this->elementEnd('li');
204 $this->elementStart('li', 'entity_reset_keysecret');
205 $this->elementStart('form', array(
206 'id' => 'form_reset_key',
207 'class' => 'form_reset_key',
209 'action' => common_local_url('showapplication',
210 array('id' => $this->application->id))));
211 $this->elementStart('fieldset');
212 $this->hidden('token', common_session_token());
214 $this->element('input', array('type' => 'submit',
218 'value' => _('Reset key & secret'),
219 'onClick' => 'return confirmReset()'));
220 $this->elementEnd('fieldset');
221 $this->elementEnd('form');
222 $this->elementEnd('li');
224 $this->elementStart('li', 'entity_delete');
225 $this->elementStart('form', array(
226 'id' => 'form_delete_application',
227 'class' => 'form_delete_application',
229 'action' => common_local_url('deleteapplication',
230 array('id' => $this->application->id))));
232 $this->elementStart('fieldset');
233 $this->hidden('token', common_session_token());
234 $this->submit('delete', _('Delete'));
235 $this->elementEnd('fieldset');
236 $this->elementEnd('form');
237 $this->elementEnd('li');
239 $this->elementEnd('ul');
240 $this->elementEnd('div');
242 $this->elementStart('div', 'entity_data');
243 $this->element('h2', null, _('Application info'));
244 $this->element('div',
245 'entity_consumer_key',
246 $consumer->consumer_key);
248 $this->element('div',
249 'entity_consumer_secret',
250 $consumer->consumer_secret);
252 $this->element('div',
253 'entity_request_token_url',
254 common_local_url('ApiOauthRequestToken'));
256 $this->element('div', 'entity_access_token_url', common_local_url('ApiOauthAccessToken'));
258 $this->element('div', 'entity_authorize_url', common_local_url('ApiOauthAuthorize'));
260 $this->element('p', 'note',
261 _('Note: We support HMAC-SHA1 signatures. We do not support the plaintext signature method.'));
262 $this->elementEnd('div');
264 $this->elementStart('p', array('id' => 'application_action'));
266 array('href' => common_local_url('oauthappssettings'),
268 'View your applications');
269 $this->elementEnd('p');
273 * Add a confirm script for Consumer key/secret reset
278 function showScripts()
280 parent::showScripts();
282 $msg = _('Are you sure you want to reset your consumer key and secret?');
284 $js = 'function confirmReset() { ';
285 $js .= ' var agree = confirm("' . $msg . '"); ';
286 $js .= ' return agree;';
289 $this->inlineScript($js);
293 * Reset an application's Consumer key and secret
295 * XXX: Should this be moved to its own page with a confirm?
301 $this->application->query('BEGIN');
303 $oauser = new Oauth_application_user();
304 $oauser->application_id = $this->application->id;
305 $result = $oauser->delete();
307 if ($result === false) {
308 common_log_db_error($oauser, 'DELETE', __FILE__);
309 $this->success = false;
310 $this->msg = ('Unable to reset consumer key and secret.');
315 $consumer = $this->application->getConsumer();
316 $result = $consumer->delete();
318 if ($result === false) {
319 common_log_db_error($consumer, 'DELETE', __FILE__);
320 $this->success = false;
321 $this->msg = ('Unable to reset consumer key and secret.');
326 $consumer = Consumer::generateNew();
328 $result = $consumer->insert();
330 if (empty($result)) {
331 common_log_db_error($consumer, 'INSERT', __FILE__);
332 $this->application->query('ROLLBACK');
333 $this->success = false;
334 $this->msg = ('Unable to reset consumer key and secret.');
339 $orig = clone($this->application);
340 $this->application->consumer_key = $consumer->consumer_key;
341 $result = $this->application->update($orig);
343 if ($result === false) {
344 common_log_db_error($application, 'UPDATE', __FILE__);
345 $this->application->query('ROLLBACK');
346 $this->success = false;
347 $this->msg = ('Unable to reset consumer key and secret.');
352 $this->application->query('COMMIT');
354 $this->success = true;
355 $this->msg = ('Consumer key and secret reset.');