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/
43 class ShowApplicationAction extends OwnerDesignAction
48 var $application = null;
51 * User who owns the app
60 * Load attributes based on database arguments
62 * Loads all the DB stuff
64 * @param array $args $_REQUEST array
66 * @return success flag
68 function prepare($args)
70 parent::prepare($args);
72 $id = (int)$this->arg('id');
74 $this->application = Oauth_application::staticGet($id);
75 $this->owner = User::staticGet($this->application->owner);
77 if (!common_logged_in()) {
78 // TRANS: Client error displayed trying to display an OAuth application while not logged in.
79 $this->clientError(_('You must be logged in to view an application.'));
83 if (empty($this->application)) {
84 // TRANS: Client error displayed trying to display a non-existing OAuth application.
85 $this->clientError(_('No such application.'), 404);
89 $cur = common_current_user();
91 if ($cur->id != $this->owner->id) {
92 // TRANS: Client error displayed trying to display an OAuth application for which the logged in user is not the owner.
93 $this->clientError(_('You are not the owner of this application.'), 401);
103 * Shows info about the app
107 function handle($args)
109 parent::handle($args);
111 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
114 $token = $this->trimmed('token');
115 if (!$token || $token != common_session_token()) {
116 $this->clientError(_('There was a problem with your session token.'));
120 if ($this->arg('reset')) {
131 * @return string title of the page
135 if (!empty($this->application->name)) {
136 return 'Application: ' . $this->application->name;
140 function showPageNotice()
142 if (!empty($this->msg)) {
143 $this->element('div', ($this->success) ? 'success' : 'error', $this->msg);
147 function showContent()
149 $cur = common_current_user();
151 $consumer = $this->application->getConsumer();
153 $this->elementStart('div', 'entity_profile vcard');
154 // TRANS: Header on the OAuth application page.
155 $this->element('h2', null, _('Application profile'));
156 if (!empty($this->application->icon)) {
157 $this->element('img', array('src' => $this->application->icon,
158 'class' => 'photo logo entity_depiction'));
161 $this->element('a', array('href' => $this->application->source_url,
162 'class' => 'url fn entity_fn'),
163 $this->application->name);
165 $this->element('a', array('href' => $this->application->homepage,
166 'class' => 'url entity_org'),
167 $this->application->organization);
169 $this->element('div',
171 $this->application->description);
173 $this->elementStart('div', 'entity_statistics');
174 $defaultAccess = ($this->application->access_type & Oauth_application::$writeAccess)
175 ? 'read-write' : 'read-only';
176 $profile = Profile::staticGet($this->application->owner);
178 $appUsers = new Oauth_application_user();
179 $appUsers->application_id = $this->application->id;
180 $userCnt = $appUsers->count();
183 // TRANS: Information output on an OAuth application page.
184 // TRANS: %1$s is the application creator, %2$s is "read-only" or "read-write",
185 // TRANS: %3$d is the number of users using the OAuth application.
186 _m('Created by %1$s - %2$s access by default - %3$d user',
187 'Created by %1$s - %2$s access by default - %3$d users',
189 $profile->getBestName(),
193 $this->elementEnd('div');
195 $this->elementEnd('div');
197 $this->elementStart('div', 'entity_actions');
198 // TRANS: Header on the OAuth application page.
199 $this->element('h2', null, _('Application actions'));
200 $this->elementStart('ul');
201 $this->elementStart('li', 'entity_edit');
203 array('href' => common_local_url('editapplication',
204 array('id' => $this->application->id))),
205 // TRANS: Link text to edit application on the OAuth application page.
206 _m('EDITAPP','Edit'));
207 $this->elementEnd('li');
209 $this->elementStart('li', 'entity_reset_keysecret');
210 $this->elementStart('form', array(
211 'id' => 'form_reset_key',
212 'class' => 'form_reset_key',
214 'action' => common_local_url('showapplication',
215 array('id' => $this->application->id))));
216 $this->elementStart('fieldset');
217 $this->hidden('token', common_session_token());
219 $this->element('input', array('type' => 'submit',
223 // TRANS: Button text on the OAuth application page.
224 // TRANS: Resets the OAuth consumer key and secret.
225 'value' => _('Reset key & secret'),
226 'onClick' => 'return confirmReset()'));
227 $this->elementEnd('fieldset');
228 $this->elementEnd('form');
229 $this->elementEnd('li');
231 $this->elementStart('li', 'entity_delete');
232 $this->elementStart('form', array(
233 'id' => 'form_delete_application',
234 'class' => 'form_delete_application',
236 'action' => common_local_url('deleteapplication',
237 array('id' => $this->application->id))));
239 $this->elementStart('fieldset');
240 $this->hidden('token', common_session_token());
241 // TRANS: Submit button text the OAuth application page to delete an application.
242 $this->submit('delete', _m('BUTTON','Delete'));
243 $this->elementEnd('fieldset');
244 $this->elementEnd('form');
245 $this->elementEnd('li');
247 $this->elementEnd('ul');
248 $this->elementEnd('div');
250 $this->elementStart('div', 'entity_data');
251 // TRANS: Header on the OAuth application page.
252 $this->element('h2', null, _('Application info'));
253 $this->element('div',
254 'entity_consumer_key',
255 $consumer->consumer_key);
257 $this->element('div',
258 'entity_consumer_secret',
259 $consumer->consumer_secret);
261 $this->element('div',
262 'entity_request_token_url',
263 common_local_url('ApiOauthRequestToken'));
265 $this->element('div', 'entity_access_token_url', common_local_url('ApiOauthAccessToken'));
267 $this->element('div', 'entity_authorize_url', common_local_url('ApiOauthAuthorize'));
269 $this->element('p', 'note',
270 // TRANS: Note on the OAuth application page about signature support.
271 _('Note: HMAC-SHA1 signatures are supported. The plaintext signature method is not supported.'));
272 $this->elementEnd('div');
274 $this->elementStart('p', array('id' => 'application_action'));
276 array('href' => common_local_url('oauthappssettings'),
278 'View your applications');
279 $this->elementEnd('p');
283 * Add a confirm script for Consumer key/secret reset
287 function showScripts()
289 parent::showScripts();
291 // TRANS: Text in confirmation dialog to reset consumer key and secret for an OAuth application.
292 $msg = _('Are you sure you want to reset your consumer key and secret?');
294 $js = 'function confirmReset() { ';
295 $js .= ' var agree = confirm("' . $msg . '"); ';
296 $js .= ' return agree;';
299 $this->inlineScript($js);
303 * Reset an application's Consumer key and secret
305 * XXX: Should this be moved to its own page with a confirm?
310 $this->application->query('BEGIN');
312 $oauser = new Oauth_application_user();
313 $oauser->application_id = $this->application->id;
314 $result = $oauser->delete();
316 if ($result === false) {
317 common_log_db_error($oauser, 'DELETE', __FILE__);
318 $this->success = false;
319 $this->msg = ('Unable to reset consumer key and secret.');
324 $consumer = $this->application->getConsumer();
325 $result = $consumer->delete();
327 if ($result === false) {
328 common_log_db_error($consumer, 'DELETE', __FILE__);
329 $this->success = false;
330 $this->msg = ('Unable to reset consumer key and secret.');
335 $consumer = Consumer::generateNew();
337 $result = $consumer->insert();
339 if (empty($result)) {
340 common_log_db_error($consumer, 'INSERT', __FILE__);
341 $this->application->query('ROLLBACK');
342 $this->success = false;
343 $this->msg = ('Unable to reset consumer key and secret.');
348 $orig = clone($this->application);
349 $this->application->consumer_key = $consumer->consumer_key;
350 $result = $this->application->update($orig);
352 if ($result === false) {
353 common_log_db_error($application, 'UPDATE', __FILE__);
354 $this->application->query('ROLLBACK');
355 $this->success = false;
356 $this->msg = ('Unable to reset consumer key and secret.');
361 $this->application->query('COMMIT');
363 $this->success = true;
364 $this->msg = ('Consumer key and secret reset.');