123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409 |
- <?php
- defined('GNUSOCIAL') || die();
- class ShowApplicationAction extends Action
- {
-
- public $application = null;
-
- public $owner = null;
- public $msg = null;
- public $success = null;
-
- public function prepare(array $args = [])
- {
- parent::prepare($args);
- $id = (int)$this->arg('id');
- $this->application = Oauth_application::getKV($id);
- $this->owner = User::getKV($this->application->owner);
- if (!common_logged_in()) {
-
- $this->clientError(_('You must be logged in to view an application.'));
- }
- if (empty($this->application)) {
-
- $this->clientError(_('No such application.'), 404);
- }
- $cur = common_current_user();
- if ($cur->id != $this->owner->id) {
-
- $this->clientError(_('You are not the owner of this application.'), 401);
- }
- return true;
- }
-
- public function handle()
- {
- parent::handle();
- if ($_SERVER['REQUEST_METHOD'] == 'POST') {
-
- $token = $this->trimmed('token');
- if (!$token || $token != common_session_token()) {
-
- $this->clientError(_('There was a problem with your session token.'));
- }
- if ($this->arg('reset')) {
- $this->resetKey();
- }
- } else {
- $this->showPage();
- }
- }
-
- public function title()
- {
- if (!empty($this->application->name)) {
- return 'Application: ' . $this->application->name;
- }
- }
- public function showPageNotice()
- {
- if (!empty($this->msg)) {
- $this->element('div', ($this->success) ? 'success' : 'error', $this->msg);
- }
- }
- public function showContent()
- {
- $cur = common_current_user();
- $consumer = $this->application->getConsumer();
- $this->elementStart('div', 'entity_profile h-card');
-
- $this->element('h2', null, _('Application profile'));
- if (!empty($this->application->icon)) {
- $this->element(
- 'img',
- [
- 'src' => $this->application->icon,
- 'class' => 'u-photo logo entity_depiction',
- ]
- );
- }
- $this->element(
- 'a',
- [
- 'href' => $this->application->source_url,
- 'class' => 'u-url p-name entity_fn',
- ],
- $this->application->name
- );
- $this->element(
- 'a',
- [
- 'href' => $this->application->homepage,
- 'class' => 'u-url entity_org',
- ],
- $this->application->organization
- );
- $this->element(
- 'div',
- 'note entity_note',
- $this->application->description
- );
- $this->elementStart('div', 'entity_statistics');
- $defaultAccess = ($this->application->access_type & Oauth_application::$writeAccess)
- ? 'read-write' : 'read-only';
- $profile = Profile::getKV($this->application->owner);
- $appUsers = new Oauth_application_user();
- $appUsers->application_id = $this->application->id;
- $userCnt = $appUsers->count();
- $this->raw(sprintf(
-
-
-
- _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),
- $profile->getBestName(),
- $defaultAccess,
- $userCnt
- ));
- $this->elementEnd('div');
- $this->elementEnd('div');
- $this->elementStart('div', 'entity_actions');
-
- $this->element('h2', null, _('Application actions'));
- $this->elementStart('ul');
- $this->elementStart('li', 'entity_edit');
- $this->element(
- 'a',
- [
- 'href' => common_local_url(
- 'editapplication',
- ['id' => $this->application->id]
- )
- ],
-
- _m('EDITAPP', 'Edit')
- );
- $this->elementEnd('li');
- $this->elementStart('li', 'entity_reset_keysecret');
- $this->elementStart(
- 'form',
- [
- 'id' => 'form_reset_key',
- 'class' => 'form_reset_key',
- 'method' => 'POST',
- 'action' => common_local_url(
- 'showapplication',
- ['id' => $this->application->id]
- ),
- ]
- );
- $this->elementStart('fieldset');
- $this->hidden('token', common_session_token());
- $this->element(
- 'input',
- [
- 'type' => 'submit',
- 'id' => 'reset',
- 'name' => 'reset',
- 'class' => 'submit',
-
-
- 'value' => _('Reset key & secret'),
- 'onClick' => 'return confirmReset()',
- ]
- );
- $this->elementEnd('fieldset');
- $this->elementEnd('form');
- $this->elementEnd('li');
- $this->elementStart('li', 'entity_delete');
- $this->elementStart(
- 'form',
- [
- 'id' => 'form_delete_application',
- 'class' => 'form_delete_application',
- 'method' => 'POST',
- 'action' => common_local_url(
- 'deleteapplication',
- ['id' => $this->application->id]
- ),
- ]
- );
- $this->elementStart('fieldset');
- $this->hidden('token', common_session_token());
-
- $this->submit('delete', _m('BUTTON', 'Delete'));
- $this->elementEnd('fieldset');
- $this->elementEnd('form');
- $this->elementEnd('li');
- $this->elementEnd('ul');
- $this->elementEnd('div');
- $this->elementStart('div', 'entity_data');
-
- $this->element('h2', null, _('Application info'));
- $this->elementStart('dl');
-
- $this->element('dt', null, _('Consumer key'));
- $this->element('dd', null, $consumer->consumer_key);
-
- $this->element('dt', null, _('Consumer secret'));
- $this->element('dd', null, $consumer->consumer_secret);
-
- $this->element('dt', null, _('Request token URL'));
- $this->element('dd', null, common_local_url('ApiOAuthRequestToken'));
-
- $this->element('dt', null, _('Access token URL'));
- $this->element('dd', null, common_local_url('ApiOAuthAccessToken'));
-
- $this->element('dt', null, _('Authorize URL'));
- $this->element('dd', null, common_local_url('ApiOAuthAuthorize'));
- $this->elementEnd('dl');
- $this->element(
- 'p',
- 'note',
-
- _('Note: HMAC-SHA1 signatures are supported. The plaintext signature method is not supported.')
- );
- $this->elementEnd('div');
- $this->elementStart('p', array('id' => 'application_action'));
- $this->element(
- 'a',
- [
- 'href' => common_local_url('oauthappssettings'),
- 'class' => 'more',
- ],
- 'View your applications'
- );
- $this->elementEnd('p');
- }
-
- public function showScripts()
- {
- parent::showScripts();
-
- $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);
- }
-
- public function resetKey()
- {
- $this->application->query('START TRANSACTION');
- $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;
- }
- $consumer = $this->application->getConsumer();
- $result = $consumer->delete();
- if ($result === false) {
- 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 (empty($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 === false) {
- 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();
- }
- }
|