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 $this->elementStart('dl', 'entity_depiction');
153 $this->element('dt', null, _('Icon'));
154 $this->elementStart('dd');
155 if (!empty($this->application->icon)) {
156 $this->element('img', array('src' => $this->application->icon,
157 'class' => 'photo logo'));
159 $this->elementEnd('dd');
160 $this->elementEnd('dl');
162 $this->elementStart('dl', 'entity_fn');
163 $this->element('dt', null, _('Name'));
164 $this->elementStart('dd');
165 $this->element('a', array('href' => $this->application->source_url,
166 'class' => 'url fn'),
167 $this->application->name);
168 $this->elementEnd('dd');
169 $this->elementEnd('dl');
171 $this->elementStart('dl', 'entity_org');
172 $this->element('dt', null, _('Organization'));
173 $this->elementStart('dd');
174 $this->element('a', array('href' => $this->application->homepage,
176 $this->application->organization);
177 $this->elementEnd('dd');
178 $this->elementEnd('dl');
180 $this->elementStart('dl', 'entity_note');
181 $this->element('dt', null, _('Description'));
182 $this->element('dd', 'note', $this->application->description);
183 $this->elementEnd('dl');
185 $this->elementStart('dl', 'entity_statistics');
186 $this->element('dt', null, _('Statistics'));
187 $this->elementStart('dd');
188 $defaultAccess = ($this->application->access_type & Oauth_application::$writeAccess)
189 ? 'read-write' : 'read-only';
190 $profile = Profile::staticGet($this->application->owner);
192 $appUsers = new Oauth_application_user();
193 $appUsers->application_id = $this->application->id;
194 $userCnt = $appUsers->count();
197 _('Created by %1$s - %2$s access by default - %3$d users'),
198 $profile->getBestName(),
202 $this->elementEnd('dd');
203 $this->elementEnd('dl');
204 $this->elementEnd('div');
206 $this->elementStart('div', 'entity_actions');
207 $this->element('h2', null, _('Application actions'));
208 $this->elementStart('ul');
209 $this->elementStart('li', 'entity_edit');
211 array('href' => common_local_url('editapplication',
212 array('id' => $this->application->id))),
214 $this->elementEnd('li');
216 $this->elementStart('li', 'entity_reset_keysecret');
217 $this->elementStart('form', array(
218 'id' => 'form_reset_key',
219 'class' => 'form_reset_key',
221 'action' => common_local_url('showapplication',
222 array('id' => $this->application->id))));
223 $this->elementStart('fieldset');
224 $this->hidden('token', common_session_token());
226 $this->element('input', array('type' => 'submit',
230 'value' => _('Reset key & secret'),
231 'onClick' => 'return confirmReset()'));
232 $this->elementEnd('fieldset');
233 $this->elementEnd('form');
234 $this->elementEnd('li');
236 $this->elementStart('li', 'entity_delete');
237 $this->elementStart('form', array(
238 'id' => 'form_delete_application',
239 'class' => 'form_delete_application',
241 'action' => common_local_url('deleteapplication',
242 array('id' => $this->application->id))));
244 $this->elementStart('fieldset');
245 $this->hidden('token', common_session_token());
246 $this->submit('delete', _('Delete'));
247 $this->elementEnd('fieldset');
248 $this->elementEnd('form');
249 $this->elementEnd('li');
251 $this->elementEnd('ul');
252 $this->elementEnd('div');
254 $this->elementStart('div', 'entity_data');
255 $this->element('h2', null, _('Application info'));
256 $this->elementStart('dl', 'entity_consumer_key');
257 $this->element('dt', null, _('Consumer key'));
258 $this->element('dd', null, $consumer->consumer_key);
259 $this->elementEnd('dl');
261 $this->elementStart('dl', 'entity_consumer_secret');
262 $this->element('dt', null, _('Consumer secret'));
263 $this->element('dd', null, $consumer->consumer_secret);
264 $this->elementEnd('dl');
266 $this->elementStart('dl', 'entity_request_token_url');
267 $this->element('dt', null, _('Request token URL'));
268 $this->element('dd', null, common_local_url('ApiOauthRequestToken'));
269 $this->elementEnd('dl');
271 $this->elementStart('dl', 'entity_access_token_url');
272 $this->element('dt', null, _('Access token URL'));
273 $this->element('dd', null, common_local_url('ApiOauthAccessToken'));
274 $this->elementEnd('dl');
276 $this->elementStart('dl', 'entity_authorize_url');
277 $this->element('dt', null, _('Authorize URL'));
278 $this->element('dd', null, common_local_url('ApiOauthAuthorize'));
279 $this->elementEnd('dl');
281 $this->element('p', 'note',
282 _('Note: We support HMAC-SHA1 signatures. We do not support the plaintext signature method.'));
283 $this->elementEnd('div');
285 $this->elementStart('p', array('id' => 'application_action'));
287 array('href' => common_local_url('oauthappssettings'),
289 'View your applications');
290 $this->elementEnd('p');
294 * Add a confirm script for Consumer key/secret reset
298 function showScripts()
300 parent::showScripts();
302 $msg = _('Are you sure you want to reset your consumer key and secret?');
304 $js = 'function confirmReset() { ';
305 $js .= ' var agree = confirm("' . $msg . '"); ';
306 $js .= ' return agree;';
309 $this->inlineScript($js);
313 * Reset an application's Consumer key and secret
315 * XXX: Should this be moved to its own page with a confirm?
320 $this->application->query('BEGIN');
322 $oauser = new Oauth_application_user();
323 $oauser->application_id = $this->application->id;
324 $result = $oauser->delete();
326 if ($result === false) {
327 common_log_db_error($oauser, 'DELETE', __FILE__);
328 $this->success = false;
329 $this->msg = ('Unable to reset consumer key and secret.');
334 $consumer = $this->application->getConsumer();
335 $result = $consumer->delete();
337 if ($result === false) {
338 common_log_db_error($consumer, 'DELETE', __FILE__);
339 $this->success = false;
340 $this->msg = ('Unable to reset consumer key and secret.');
345 $consumer = Consumer::generateNew();
347 $result = $consumer->insert();
349 if (empty($result)) {
350 common_log_db_error($consumer, 'INSERT', __FILE__);
351 $this->application->query('ROLLBACK');
352 $this->success = false;
353 $this->msg = ('Unable to reset consumer key and secret.');
358 $orig = clone($this->application);
359 $this->application->consumer_key = $consumer->consumer_key;
360 $result = $this->application->update($orig);
362 if ($result === false) {
363 common_log_db_error($application, 'UPDATE', __FILE__);
364 $this->application->query('ROLLBACK');
365 $this->success = false;
366 $this->msg = ('Unable to reset consumer key and secret.');
371 $this->application->query('COMMIT');
373 $this->success = true;
374 $this->msg = ('Consumer key and secret reset.');