git.gnu.io has moved to IP address 209.51.188.249 -- please double check where you are logging in.

showapplication.php 12.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
<?php
/**
 * StatusNet, the distributed open-source microblogging tool
 *
 * Show an OAuth application
 *
 * PHP version 5
 *
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category  Application
 * @package   StatusNet
 * @author    Zach Copley <zach@status.net>
25
 * @copyright 2008-2011 StatusNet, Inc.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link      http://status.net/
 */

if (!defined('STATUSNET') && !defined('LACONICA')) {
    exit(1);
}

/**
 * Show an OAuth application
 *
 * @category Application
 * @package  StatusNet
 * @author   Zach Copley <zach@status.net>
 * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link     http://status.net/
 */
43
class ShowApplicationAction extends Action
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
{
    /**
     * Application to show
     */
    var $application = null;

    /**
     * User who owns the app
     */
    var $owner = null;

    var $msg = null;

    var $success = null;

    /**
     * Load attributes based on database arguments
     *
     * Loads all the DB stuff
     *
     * @param array $args $_REQUEST array
     *
     * @return success flag
     */
    function prepare($args)
    {
        parent::prepare($args);

        $id = (int)$this->arg('id');

74 75
        $this->application  = Oauth_application::getKV($id);
        $this->owner        = User::getKV($this->application->owner);
76 77

        if (!common_logged_in()) {
78
            // TRANS: Client error displayed trying to display an OAuth application while not logged in.
79 80 81 82
            $this->clientError(_('You must be logged in to view an application.'));
        }

        if (empty($this->application)) {
83
            // TRANS: Client error displayed trying to display a non-existing OAuth application.
84 85 86 87 88 89
            $this->clientError(_('No such application.'), 404);
        }

        $cur = common_current_user();

        if ($cur->id != $this->owner->id) {
90
            // TRANS: Client error displayed trying to display an OAuth application for which the logged in user is not the owner.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
            $this->clientError(_('You are not the owner of this application.'), 401);
        }

        return true;
    }

    /**
     * Handle the request
     *
     * Shows info about the app
     *
     * @return void
     */
    function handle($args)
    {
        parent::handle($args);

        if ($_SERVER['REQUEST_METHOD'] == 'POST') {

            // CSRF protection
            $token = $this->trimmed('token');
            if (!$token || $token != common_session_token()) {
113
                // TRANS: Client error displayed when the session token does not match or is not given.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
                $this->clientError(_('There was a problem with your session token.'));
            }

            if ($this->arg('reset')) {
                $this->resetKey();
            }
        } else {
            $this->showPage();
        }
    }

    /**
     * Title of the page
     *
     * @return string title of the page
     */
    function title()
    {
        if (!empty($this->application->name)) {
            return 'Application: ' . $this->application->name;
        }
    }

    function showPageNotice()
    {
        if (!empty($this->msg)) {
            $this->element('div', ($this->success) ? 'success' : 'error', $this->msg);
        }
    }

    function showContent()
    {
        $cur = common_current_user();

        $consumer = $this->application->getConsumer();

mmn's avatar
mmn committed
150
        $this->elementStart('div', 'entity_profile h-card');
151
        // TRANS: Header on the OAuth application page.
152 153
        $this->element('h2', null, _('Application profile'));
        if (!empty($this->application->icon)) {
154
            $this->element('img', array('src' => $this->application->icon,
mmn's avatar
mmn committed
155
                                        'class' => 'u-photo logo entity_depiction'));
156
        }
157

158
        $this->element('a', array('href' =>  $this->application->source_url,
mmn's avatar
mmn committed
159
                                  'class' => 'u-url p-name entity_fn'),
160
                            $this->application->name);
161

162
        $this->element('a', array('href' =>  $this->application->homepage,
mmn's avatar
mmn committed
163
                                  'class' => 'u-url entity_org'),
164
                            $this->application->organization);
165

166 167 168
        $this->element('div',
                       'note entity_note',
                       $this->application->description);
169

170
        $this->elementStart('div', 'entity_statistics');
171 172
        $defaultAccess = ($this->application->access_type & Oauth_application::$writeAccess)
            ? 'read-write' : 'read-only';
173
        $profile = Profile::getKV($this->application->owner);
Zach Copley's avatar
Zach Copley committed
174 175 176 177

        $appUsers = new Oauth_application_user();
        $appUsers->application_id = $this->application->id;
        $userCnt = $appUsers->count();
178 179

        $this->raw(sprintf(
180 181 182 183 184 185
            // TRANS: Information output on an OAuth application page.
            // TRANS: %1$s is the application creator, %2$s is "read-only" or "read-write",
            // TRANS: %3$d is the number of users using the OAuth application.
            _m('Created by %1$s - %2$s access by default - %3$d user',
               'Created by %1$s - %2$s access by default - %3$d users',
               $userCnt),
186 187 188 189
              $profile->getBestName(),
              $defaultAccess,
              $userCnt
            ));
190 191
        $this->elementEnd('div');

192
        $this->elementEnd('div');
193

194
        $this->elementStart('div', 'entity_actions');
195
        // TRANS: Header on the OAuth application page.
196 197 198 199
        $this->element('h2', null, _('Application actions'));
        $this->elementStart('ul');
        $this->elementStart('li', 'entity_edit');
        $this->element('a',
200 201
                       array('href' => common_local_url('editapplication',
                                                        array('id' => $this->application->id))),
202 203
                       // TRANS: Link text to edit application on the OAuth application page.
                       _m('EDITAPP','Edit'));
204 205 206 207
        $this->elementEnd('li');

        $this->elementStart('li', 'entity_reset_keysecret');
        $this->elementStart('form', array(
208
            'id' => 'form_reset_key',
209 210 211
            'class' => 'form_reset_key',
            'method' => 'POST',
            'action' => common_local_url('showapplication',
Zach Copley's avatar
Zach Copley committed
212
                                         array('id' => $this->application->id))));
213 214
        $this->elementStart('fieldset');
        $this->hidden('token', common_session_token());
215 216 217 218 219

        $this->element('input', array('type' => 'submit',
                                      'id' => 'reset',
                                      'name' => 'reset',
                                      'class' => 'submit',
220 221
                                      // TRANS: Button text on the OAuth application page.
                                      // TRANS: Resets the OAuth consumer key and secret.
222 223
                                      'value' => _('Reset key & secret'),
                                      'onClick' => 'return confirmReset()'));
224 225 226
        $this->elementEnd('fieldset');
        $this->elementEnd('form');
        $this->elementEnd('li');
227 228 229 230 231 232 233 234 235 236 237

        $this->elementStart('li', 'entity_delete');
        $this->elementStart('form', array(
                                          'id' => 'form_delete_application',
                                          'class' => 'form_delete_application',
                                          'method' => 'POST',
                                          'action' => common_local_url('deleteapplication',
                                                                       array('id' => $this->application->id))));

        $this->elementStart('fieldset');
        $this->hidden('token', common_session_token());
238 239
        // TRANS: Submit button text the OAuth application page to delete an application.
        $this->submit('delete', _m('BUTTON','Delete'));
240 241 242 243
        $this->elementEnd('fieldset');
        $this->elementEnd('form');
        $this->elementEnd('li');

244 245 246 247
        $this->elementEnd('ul');
        $this->elementEnd('div');

        $this->elementStart('div', 'entity_data');
248
        // TRANS: Header on the OAuth application page.
249
        $this->element('h2', null, _('Application info'));
250

251
        $this->elementStart('dl');
252
        // TRANS: Field label on application page.
253 254
        $this->element('dt', null, _('Consumer key'));
        $this->element('dd', null, $consumer->consumer_key);
255
        // TRANS: Field label on application page.
256 257
        $this->element('dt', null, _('Consumer secret'));
        $this->element('dd', null, $consumer->consumer_secret);
258
        // TRANS: Field label on application page.
259
        $this->element('dt', null, _('Request token URL'));
260
        $this->element('dd', null, common_local_url('ApiOAuthRequestToken'));
261
        // TRANS: Field label on application page.
262
        $this->element('dt', null, _('Access token URL'));
263
        $this->element('dd', null, common_local_url('ApiOAuthAccessToken'));
264
        // TRANS: Field label on application page.
265
        $this->element('dt', null, _('Authorize URL'));
266
        $this->element('dd', null, common_local_url('ApiOAuthAuthorize'));
267
        $this->elementEnd('dl');
268

269
        $this->element('p', 'note',
270 271
            // TRANS: Note on the OAuth application page about signature support.
            _('Note: HMAC-SHA1 signatures are supported. The plaintext signature method is not supported.'));
272 273
        $this->elementEnd('div');

274
        $this->elementStart('p', array('id' => 'application_action'));
275
        $this->element('a',
276 277 278
            array('href' => common_local_url('oauthappssettings'),
                  'class' => 'more'),
                  'View your applications');
279
        $this->elementEnd('p');
280 281
    }

282 283 284 285 286 287 288 289 290
    /**
     * Add a confirm script for Consumer key/secret reset
     *
     * @return void
     */
    function showScripts()
    {
        parent::showScripts();

291
        // TRANS: Text in confirmation dialog to reset consumer key and secret for an OAuth application.
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
        $msg = _('Are you sure you want to reset your consumer key and secret?');

        $js  = 'function confirmReset() { ';
        $js .= '    var agree = confirm("' . $msg . '"); ';
        $js .= '    return agree;';
        $js .= '}';

        $this->inlineScript($js);
    }

    /**
     * Reset an application's Consumer key and secret
     *
     * XXX: Should this be moved to its own page with a confirm?
     *
     */
308 309 310 311
    function resetKey()
    {
        $this->application->query('BEGIN');

312 313 314 315 316 317 318 319 320 321 322 323
        $oauser = new Oauth_application_user();
        $oauser->application_id = $this->application->id;
        $result = $oauser->delete();

        if ($result === false) {
            common_log_db_error($oauser, 'DELETE', __FILE__);
            $this->success = false;
            $this->msg = ('Unable to reset consumer key and secret.');
            $this->showPage();
            return;
        }

324 325 326
        $consumer = $this->application->getConsumer();
        $result = $consumer->delete();

327
        if ($result === false) {
328 329 330 331 332 333 334 335 336 337 338
            common_log_db_error($consumer, 'DELETE', __FILE__);
            $this->success = false;
            $this->msg = ('Unable to reset consumer key and secret.');
            $this->showPage();
            return;
        }

        $consumer = Consumer::generateNew();

        $result = $consumer->insert();

339
        if (empty($result)) {
340 341 342 343 344 345 346 347 348 349 350 351
            common_log_db_error($consumer, 'INSERT', __FILE__);
            $this->application->query('ROLLBACK');
            $this->success = false;
            $this->msg = ('Unable to reset consumer key and secret.');
            $this->showPage();
            return;
        }

        $orig = clone($this->application);
        $this->application->consumer_key = $consumer->consumer_key;
        $result = $this->application->update($orig);

352
        if ($result === false) {
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
            common_log_db_error($application, 'UPDATE', __FILE__);
            $this->application->query('ROLLBACK');
            $this->success = false;
            $this->msg = ('Unable to reset consumer key and secret.');
            $this->showPage();
            return;
        }

        $this->application->query('COMMIT');

        $this->success = true;
        $this->msg = ('Consumer key and secret reset.');
        $this->showPage();
    }
}