showapplication.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Show an OAuth application
  18. *
  19. * @category Application
  20. * @package GNUsocial
  21. * @author Zach Copley <zach@status.net>
  22. * @copyright 2008-2011 StatusNet, Inc.
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * Show an OAuth application
  28. *
  29. * @category Application
  30. * @package GNUsocial
  31. * @author Zach Copley <zach@status.net>
  32. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  33. */
  34. class ShowApplicationAction extends Action
  35. {
  36. /**
  37. * Application to show
  38. */
  39. public $application = null;
  40. /**
  41. * User who owns the app
  42. */
  43. public $owner = null;
  44. public $msg = null;
  45. public $success = null;
  46. /**
  47. * Load attributes based on database arguments
  48. *
  49. * Loads all the DB stuff
  50. *
  51. * @param array $args $_REQUEST array
  52. *
  53. * @return success flag
  54. */
  55. public function prepare(array $args = [])
  56. {
  57. parent::prepare($args);
  58. $id = (int)$this->arg('id');
  59. $this->application = Oauth_application::getKV($id);
  60. $this->owner = User::getKV($this->application->owner);
  61. if (!common_logged_in()) {
  62. // TRANS: Client error displayed trying to display an OAuth application while not logged in.
  63. $this->clientError(_('You must be logged in to view an application.'));
  64. }
  65. if (empty($this->application)) {
  66. // TRANS: Client error displayed trying to display a non-existing OAuth application.
  67. $this->clientError(_('No such application.'), 404);
  68. }
  69. $cur = common_current_user();
  70. if ($cur->id != $this->owner->id) {
  71. // TRANS: Client error displayed trying to display an OAuth application for which the logged in user is not the owner.
  72. $this->clientError(_('You are not the owner of this application.'), 401);
  73. }
  74. return true;
  75. }
  76. /**
  77. * Handle the request
  78. *
  79. * Shows info about the app
  80. *
  81. * @return void
  82. */
  83. public function handle()
  84. {
  85. parent::handle();
  86. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  87. // CSRF protection
  88. $token = $this->trimmed('token');
  89. if (!$token || $token != common_session_token()) {
  90. // TRANS: Client error displayed when the session token does not match or is not given.
  91. $this->clientError(_('There was a problem with your session token.'));
  92. }
  93. if ($this->arg('reset')) {
  94. $this->resetKey();
  95. }
  96. } else {
  97. $this->showPage();
  98. }
  99. }
  100. /**
  101. * Title of the page
  102. *
  103. * @return string title of the page
  104. */
  105. public function title()
  106. {
  107. if (!empty($this->application->name)) {
  108. return 'Application: ' . $this->application->name;
  109. }
  110. }
  111. public function showPageNotice()
  112. {
  113. if (!empty($this->msg)) {
  114. $this->element('div', ($this->success) ? 'success' : 'error', $this->msg);
  115. }
  116. }
  117. public function showContent()
  118. {
  119. $cur = common_current_user();
  120. $consumer = $this->application->getConsumer();
  121. $this->elementStart('div', 'entity_profile h-card');
  122. // TRANS: Header on the OAuth application page.
  123. $this->element('h2', null, _('Application profile'));
  124. if (!empty($this->application->icon)) {
  125. $this->element(
  126. 'img',
  127. [
  128. 'src' => $this->application->icon,
  129. 'class' => 'u-photo logo entity_depiction',
  130. ]
  131. );
  132. }
  133. $this->element(
  134. 'a',
  135. [
  136. 'href' => $this->application->source_url,
  137. 'class' => 'u-url p-name entity_fn',
  138. ],
  139. $this->application->name
  140. );
  141. $this->element(
  142. 'a',
  143. [
  144. 'href' => $this->application->homepage,
  145. 'class' => 'u-url entity_org',
  146. ],
  147. $this->application->organization
  148. );
  149. $this->element(
  150. 'div',
  151. 'note entity_note',
  152. $this->application->description
  153. );
  154. $this->elementStart('div', 'entity_statistics');
  155. $defaultAccess = ($this->application->access_type & Oauth_application::$writeAccess)
  156. ? 'read-write' : 'read-only';
  157. $profile = Profile::getKV($this->application->owner);
  158. $appUsers = new Oauth_application_user();
  159. $appUsers->application_id = $this->application->id;
  160. $userCnt = $appUsers->count();
  161. $this->raw(sprintf(
  162. // TRANS: Information output on an OAuth application page.
  163. // TRANS: %1$s is the application creator, %2$s is "read-only" or "read-write",
  164. // TRANS: %3$d is the number of users using the OAuth application.
  165. _m('Created by %1$s - %2$s access by default - %3$d user',
  166. 'Created by %1$s - %2$s access by default - %3$d users',
  167. $userCnt),
  168. $profile->getBestName(),
  169. $defaultAccess,
  170. $userCnt
  171. ));
  172. $this->elementEnd('div');
  173. $this->elementEnd('div');
  174. $this->elementStart('div', 'entity_actions');
  175. // TRANS: Header on the OAuth application page.
  176. $this->element('h2', null, _('Application actions'));
  177. $this->elementStart('ul');
  178. $this->elementStart('li', 'entity_edit');
  179. $this->element(
  180. 'a',
  181. [
  182. 'href' => common_local_url(
  183. 'editapplication',
  184. ['id' => $this->application->id]
  185. )
  186. ],
  187. // TRANS: Link text to edit application on the OAuth application page.
  188. _m('EDITAPP', 'Edit')
  189. );
  190. $this->elementEnd('li');
  191. $this->elementStart('li', 'entity_reset_keysecret');
  192. $this->elementStart(
  193. 'form',
  194. [
  195. 'id' => 'form_reset_key',
  196. 'class' => 'form_reset_key',
  197. 'method' => 'POST',
  198. 'action' => common_local_url(
  199. 'showapplication',
  200. ['id' => $this->application->id]
  201. ),
  202. ]
  203. );
  204. $this->elementStart('fieldset');
  205. $this->hidden('token', common_session_token());
  206. $this->element(
  207. 'input',
  208. [
  209. 'type' => 'submit',
  210. 'id' => 'reset',
  211. 'name' => 'reset',
  212. 'class' => 'submit',
  213. // TRANS: Button text on the OAuth application page.
  214. // TRANS: Resets the OAuth consumer key and secret.
  215. 'value' => _('Reset key & secret'),
  216. 'onClick' => 'return confirmReset()',
  217. ]
  218. );
  219. $this->elementEnd('fieldset');
  220. $this->elementEnd('form');
  221. $this->elementEnd('li');
  222. $this->elementStart('li', 'entity_delete');
  223. $this->elementStart(
  224. 'form',
  225. [
  226. 'id' => 'form_delete_application',
  227. 'class' => 'form_delete_application',
  228. 'method' => 'POST',
  229. 'action' => common_local_url(
  230. 'deleteapplication',
  231. ['id' => $this->application->id]
  232. ),
  233. ]
  234. );
  235. $this->elementStart('fieldset');
  236. $this->hidden('token', common_session_token());
  237. // TRANS: Submit button text the OAuth application page to delete an application.
  238. $this->submit('delete', _m('BUTTON', 'Delete'));
  239. $this->elementEnd('fieldset');
  240. $this->elementEnd('form');
  241. $this->elementEnd('li');
  242. $this->elementEnd('ul');
  243. $this->elementEnd('div');
  244. $this->elementStart('div', 'entity_data');
  245. // TRANS: Header on the OAuth application page.
  246. $this->element('h2', null, _('Application info'));
  247. $this->elementStart('dl');
  248. // TRANS: Field label on application page.
  249. $this->element('dt', null, _('Consumer key'));
  250. $this->element('dd', null, $consumer->consumer_key);
  251. // TRANS: Field label on application page.
  252. $this->element('dt', null, _('Consumer secret'));
  253. $this->element('dd', null, $consumer->consumer_secret);
  254. // TRANS: Field label on application page.
  255. $this->element('dt', null, _('Request token URL'));
  256. $this->element('dd', null, common_local_url('ApiOAuthRequestToken'));
  257. // TRANS: Field label on application page.
  258. $this->element('dt', null, _('Access token URL'));
  259. $this->element('dd', null, common_local_url('ApiOAuthAccessToken'));
  260. // TRANS: Field label on application page.
  261. $this->element('dt', null, _('Authorize URL'));
  262. $this->element('dd', null, common_local_url('ApiOAuthAuthorize'));
  263. $this->elementEnd('dl');
  264. $this->element(
  265. 'p',
  266. 'note',
  267. // TRANS: Note on the OAuth application page about signature support.
  268. _('Note: HMAC-SHA1 signatures are supported. The plaintext signature method is not supported.')
  269. );
  270. $this->elementEnd('div');
  271. $this->elementStart('p', array('id' => 'application_action'));
  272. $this->element(
  273. 'a',
  274. [
  275. 'href' => common_local_url('oauthappssettings'),
  276. 'class' => 'more',
  277. ],
  278. 'View your applications'
  279. );
  280. $this->elementEnd('p');
  281. }
  282. /**
  283. * Add a confirm script for Consumer key/secret reset
  284. *
  285. * @return void
  286. */
  287. public function showScripts()
  288. {
  289. parent::showScripts();
  290. // TRANS: Text in confirmation dialog to reset consumer key and secret for an OAuth application.
  291. $msg = _('Are you sure you want to reset your consumer key and secret?');
  292. $js = 'function confirmReset() { ';
  293. $js .= ' var agree = confirm("' . $msg . '"); ';
  294. $js .= ' return agree;';
  295. $js .= '}';
  296. $this->inlineScript($js);
  297. }
  298. /**
  299. * Reset an application's Consumer key and secret
  300. *
  301. * XXX: Should this be moved to its own page with a confirm?
  302. *
  303. */
  304. public function resetKey()
  305. {
  306. $this->application->query('START TRANSACTION');
  307. $oauser = new Oauth_application_user();
  308. $oauser->application_id = $this->application->id;
  309. $result = $oauser->delete();
  310. if ($result === false) {
  311. common_log_db_error($oauser, 'DELETE', __FILE__);
  312. $this->success = false;
  313. $this->msg = ('Unable to reset consumer key and secret.');
  314. $this->showPage();
  315. return;
  316. }
  317. $consumer = $this->application->getConsumer();
  318. $result = $consumer->delete();
  319. if ($result === false) {
  320. common_log_db_error($consumer, 'DELETE', __FILE__);
  321. $this->success = false;
  322. $this->msg = ('Unable to reset consumer key and secret.');
  323. $this->showPage();
  324. return;
  325. }
  326. $consumer = Consumer::generateNew();
  327. $result = $consumer->insert();
  328. if (empty($result)) {
  329. common_log_db_error($consumer, 'INSERT', __FILE__);
  330. $this->application->query('ROLLBACK');
  331. $this->success = false;
  332. $this->msg = ('Unable to reset consumer key and secret.');
  333. $this->showPage();
  334. return;
  335. }
  336. $orig = clone($this->application);
  337. $this->application->consumer_key = $consumer->consumer_key;
  338. $result = $this->application->update($orig);
  339. if ($result === false) {
  340. common_log_db_error($application, 'UPDATE', __FILE__);
  341. $this->application->query('ROLLBACK');
  342. $this->success = false;
  343. $this->msg = ('Unable to reset consumer key and secret.');
  344. $this->showPage();
  345. return;
  346. }
  347. $this->application->query('COMMIT');
  348. $this->success = true;
  349. $this->msg = ('Consumer key and secret reset.');
  350. $this->showPage();
  351. }
  352. }