We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

showapplication.php 10.2 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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
<?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>
 * @copyright 2008-2009 StatusNet, Inc.
 * @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/
 */

class ShowApplicationAction extends OwnerDesignAction
{
    /**
     * 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');

        $this->application  = Oauth_application::staticGet($id);
        $this->owner        = User::staticGet($this->application->owner);

        if (!common_logged_in()) {
            $this->clientError(_('You must be logged in to view an application.'));
            return false;
        }

        if (empty($this->application)) {
            $this->clientError(_('No such application.'), 404);
            return false;
        }

        $cur = common_current_user();

        if ($cur->id != $this->owner->id) {
            $this->clientError(_('You are not the owner of this application.'), 401);
Zach Copley's avatar
Zach Copley committed
95
            return false;
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 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 150 151 152 153 154 155 156
        }

        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()) {
                $this->clientError(_('There was a problem with your session token.'));
                return;
            }

            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();

157
        $this->elementStart('div', 'entity_profile vcard');
158 159 160 161 162
        $this->element('h2', null, _('Application profile'));
        $this->elementStart('dl', 'entity_depiction');
        $this->element('dt', null, _('Icon'));
        $this->elementStart('dd');
        if (!empty($this->application->icon)) {
163 164
            $this->element('img', array('src' => $this->application->icon,
                                        'class' => 'photo logo'));
165 166 167
        }
        $this->elementEnd('dd');
        $this->elementEnd('dl');
168

169 170
        $this->elementStart('dl', 'entity_fn');
        $this->element('dt', null, _('Name'));
171 172 173 174 175
        $this->elementStart('dd');
        $this->element('a', array('href' =>  $this->application->source_url,
                                  'class' => 'url fn'),
                            $this->application->name);
        $this->elementEnd('dd');
176 177 178 179
        $this->elementEnd('dl');

        $this->elementStart('dl', 'entity_org');
        $this->element('dt', null, _('Organization'));
180 181 182 183 184
        $this->elementStart('dd');
        $this->element('a', array('href' =>  $this->application->homepage,
                                  'class' => 'url'),
                            $this->application->organization);
        $this->elementEnd('dd');
185
        $this->elementEnd('dl');
186

187 188
        $this->elementStart('dl', 'entity_note');
        $this->element('dt', null, _('Description'));
189
        $this->element('dd', 'note', $this->application->description);
190
        $this->elementEnd('dl');
191

192 193 194
        $this->elementStart('dl', 'entity_statistics');
        $this->element('dt', null, _('Statistics'));
        $this->elementStart('dd');
195 196 197
        $defaultAccess = ($this->application->access_type & Oauth_application::$writeAccess)
            ? 'read-write' : 'read-only';
        $profile = Profile::staticGet($this->application->owner);
Zach Copley's avatar
Zach Copley committed
198 199 200 201

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

        $this->raw(sprintf(
Zach Copley's avatar
Zach Copley committed
204
            _('created by %1$s - %2$s access by default - %3$d users'),
205 206 207 208
              $profile->getBestName(),
              $defaultAccess,
              $userCnt
            ));
209 210 211
        $this->elementEnd('dd');
        $this->elementEnd('dl');
        $this->elementEnd('div');
212

213 214 215 216 217
        $this->elementStart('div', 'entity_actions');
        $this->element('h2', null, _('Application actions'));
        $this->elementStart('ul');
        $this->elementStart('li', 'entity_edit');
        $this->element('a',
218 219 220
                       array('href' => common_local_url('editapplication',
                                                        array('id' => $this->application->id))),
                       'Edit');
221 222 223 224 225 226 227 228
        $this->elementEnd('li');

        $this->elementStart('li', 'entity_reset_keysecret');
        $this->elementStart('form', array(
            'id' => 'forma_reset_key',
            'class' => 'form_reset_key',
            'method' => 'POST',
            'action' => common_local_url('showapplication',
Zach Copley's avatar
Zach Copley committed
229
                                         array('id' => $this->application->id))));
230 231 232 233 234 235 236 237 238 239 240

        $this->elementStart('fieldset');
        $this->hidden('token', common_session_token());
        $this->submit('reset', _('Reset key & secret'));
        $this->elementEnd('fieldset');
        $this->elementEnd('form');
        $this->elementEnd('li');
        $this->elementEnd('ul');
        $this->elementEnd('div');

        $this->elementStart('div', 'entity_data');
241
        $this->element('h2', null, _('Application info'));
242 243
        $this->elementStart('dl', 'entity_consumer_key');
        $this->element('dt', null, _('Consumer key'));
244
        $this->element('dd', null, $consumer->consumer_key);
245 246 247 248
        $this->elementEnd('dl');

        $this->elementStart('dl', 'entity_consumer_secret');
        $this->element('dt', null, _('Consumer secret'));
249
        $this->element('dd', null, $consumer->consumer_secret);
250 251 252 253
        $this->elementEnd('dl');

        $this->elementStart('dl', 'entity_request_token_url');
        $this->element('dt', null, _('Request token URL'));
254
        $this->element('dd', null, common_local_url('apioauthrequesttoken'));
255 256 257 258
        $this->elementEnd('dl');

        $this->elementStart('dl', 'entity_access_token_url');
        $this->element('dt', null, _('Access token URL'));
259
        $this->element('dd', null, common_local_url('apioauthaccesstoken'));
260 261 262 263
        $this->elementEnd('dl');

        $this->elementStart('dl', 'entity_authorize_url');
        $this->element('dt', null, _('Authorize URL'));
264
        $this->element('dd', null, common_local_url('apioauthauthorize'));
265 266
        $this->elementEnd('dl');

267
        $this->element('p', 'note',
Siebrand Mazeland's avatar
Siebrand Mazeland committed
268
            _('Note: We support HMAC-SHA1 signatures. We do not support the plaintext signature method.'));
269 270
        $this->elementEnd('div');

271
        $this->elementStart('p', array('id' => 'application_action'));
272
        $this->element('a',
273 274 275
            array('href' => common_local_url('oauthappssettings'),
                  'class' => 'more'),
                  'View your applications');
276
        $this->elementEnd('p');
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
    }

    function resetKey()
    {
        $this->application->query('BEGIN');

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

        if (!$result) {
            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();

        if (!$result) {
            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);

        if (!$result) {
            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();
    }

}