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 $this->clientError(_('You must be logged in to view an application.'));
82 if (empty($this->application)) {
83 $this->clientError(_('No such application.'), 404);
87 $cur = common_current_user();
89 if ($cur->id != $this->owner->id) {
90 $this->clientError(_('You are not the owner of this application.'), 401);
100 * Shows info about the app
104 function handle($args)
106 parent::handle($args);
108 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
111 $token = $this->trimmed('token');
112 if (!$token || $token != common_session_token()) {
113 $this->clientError(_('There was a problem with your session token.'));
117 if ($this->arg('reset')) {
128 * @return string title of the page
132 if (!empty($this->application->name)) {
133 return 'Application: ' . $this->application->name;
137 function showPageNotice()
139 if (!empty($this->msg)) {
140 $this->element('div', ($this->success) ? 'success' : 'error', $this->msg);
144 function showContent()
146 $cur = common_current_user();
148 $consumer = $this->application->getConsumer();
150 $this->elementStart('div', 'entity_profile vcard');
151 $this->element('h2', null, _('Application profile'));
152 if (!empty($this->application->icon)) {
153 $this->element('img', array('src' => $this->application->icon,
154 'class' => 'photo logo entity_depiction'));
157 $this->element('a', array('href' => $this->application->source_url,
158 'class' => 'url fn entity_fn'),
159 $this->application->name);
161 $this->element('a', array('href' => $this->application->homepage,
162 'class' => 'url entity_org'),
163 $this->application->organization);
165 $this->element('div',
167 $this->application->description);
169 $this->elementStart('div', 'entity_statistics');
170 $defaultAccess = ($this->application->access_type & Oauth_application::$writeAccess)
171 ? 'read-write' : 'read-only';
172 $profile = Profile::staticGet($this->application->owner);
174 $appUsers = new Oauth_application_user();
175 $appUsers->application_id = $this->application->id;
176 $userCnt = $appUsers->count();
179 _('Created by %1$s - %2$s access by default - %3$d users'),
180 $profile->getBestName(),
184 $this->elementEnd('div');
186 $this->elementEnd('div');
188 $this->elementStart('div', 'entity_actions');
189 $this->element('h2', null, _('Application actions'));
190 $this->elementStart('ul');
191 $this->elementStart('li', 'entity_edit');
193 array('href' => common_local_url('editapplication',
194 array('id' => $this->application->id))),
196 $this->elementEnd('li');
198 $this->elementStart('li', 'entity_reset_keysecret');
199 $this->elementStart('form', array(
200 'id' => 'form_reset_key',
201 'class' => 'form_reset_key',
203 'action' => common_local_url('showapplication',
204 array('id' => $this->application->id))));
205 $this->elementStart('fieldset');
206 $this->hidden('token', common_session_token());
208 $this->element('input', array('type' => 'submit',
212 'value' => _('Reset key & secret'),
213 'onClick' => 'return confirmReset()'));
214 $this->elementEnd('fieldset');
215 $this->elementEnd('form');
216 $this->elementEnd('li');
218 $this->elementStart('li', 'entity_delete');
219 $this->elementStart('form', array(
220 'id' => 'form_delete_application',
221 'class' => 'form_delete_application',
223 'action' => common_local_url('deleteapplication',
224 array('id' => $this->application->id))));
226 $this->elementStart('fieldset');
227 $this->hidden('token', common_session_token());
228 $this->submit('delete', _('Delete'));
229 $this->elementEnd('fieldset');
230 $this->elementEnd('form');
231 $this->elementEnd('li');
233 $this->elementEnd('ul');
234 $this->elementEnd('div');
236 $this->elementStart('div', 'entity_data');
237 $this->element('h2', null, _('Application info'));
238 $this->element('div',
239 'entity_consumer_key',
240 $consumer->consumer_key);
242 $this->element('div',
243 'entity_consumer_secret',
244 $consumer->consumer_secret);
246 $this->element('div',
247 'entity_request_token_url',
248 common_local_url('ApiOauthRequestToken'));
250 $this->element('div', 'entity_access_token_url', common_local_url('ApiOauthAccessToken'));
252 $this->element('div', 'entity_authorize_url', common_local_url('ApiOauthAuthorize'));
254 $this->element('p', 'note',
255 _('Note: We support HMAC-SHA1 signatures. We do not support the plaintext signature method.'));
256 $this->elementEnd('div');
258 $this->elementStart('p', array('id' => 'application_action'));
260 array('href' => common_local_url('oauthappssettings'),
262 'View your applications');
263 $this->elementEnd('p');
267 * Add a confirm script for Consumer key/secret reset
271 function showScripts()
273 parent::showScripts();
275 $msg = _('Are you sure you want to reset your consumer key and secret?');
277 $js = 'function confirmReset() { ';
278 $js .= ' var agree = confirm("' . $msg . '"); ';
279 $js .= ' return agree;';
282 $this->inlineScript($js);
286 * Reset an application's Consumer key and secret
288 * XXX: Should this be moved to its own page with a confirm?
293 $this->application->query('BEGIN');
295 $oauser = new Oauth_application_user();
296 $oauser->application_id = $this->application->id;
297 $result = $oauser->delete();
299 if ($result === false) {
300 common_log_db_error($oauser, 'DELETE', __FILE__);
301 $this->success = false;
302 $this->msg = ('Unable to reset consumer key and secret.');
307 $consumer = $this->application->getConsumer();
308 $result = $consumer->delete();
310 if ($result === false) {
311 common_log_db_error($consumer, 'DELETE', __FILE__);
312 $this->success = false;
313 $this->msg = ('Unable to reset consumer key and secret.');
318 $consumer = Consumer::generateNew();
320 $result = $consumer->insert();
322 if (empty($result)) {
323 common_log_db_error($consumer, 'INSERT', __FILE__);
324 $this->application->query('ROLLBACK');
325 $this->success = false;
326 $this->msg = ('Unable to reset consumer key and secret.');
331 $orig = clone($this->application);
332 $this->application->consumer_key = $consumer->consumer_key;
333 $result = $this->application->update($orig);
335 if ($result === false) {
336 common_log_db_error($application, 'UPDATE', __FILE__);
337 $this->application->query('ROLLBACK');
338 $this->success = false;
339 $this->msg = ('Unable to reset consumer key and secret.');
344 $this->application->query('COMMIT');
346 $this->success = true;
347 $this->msg = ('Consumer key and secret reset.');