oauthconnectionssettings.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * List a user's OAuth connected applications
  6. *
  7. * PHP version 5
  8. *
  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.
  13. *
  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.
  18. *
  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/>.
  21. *
  22. * @category Settings
  23. * @package StatusNet
  24. * @author Zach Copley <zach@status.net>
  25. * @copyright 2008-2010 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/
  28. */
  29. if (!defined('STATUSNET') && !defined('LACONICA')) {
  30. exit(1);
  31. }
  32. require_once INSTALLDIR . '/lib/applicationlist.php';
  33. /**
  34. * Show connected OAuth applications
  35. *
  36. * @category Settings
  37. * @package StatusNet
  38. * @author Zach Copley <zach@status.net>
  39. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  40. * @link http://status.net/
  41. *
  42. * @see SettingsAction
  43. */
  44. class OauthconnectionssettingsAction extends SettingsAction
  45. {
  46. var $page = null;
  47. var $oauth_token = null;
  48. function prepare($args)
  49. {
  50. parent::prepare($args);
  51. $this->oauth_token = $this->arg('oauth_token');
  52. $this->page = ($this->arg('page')) ? ($this->arg('page') + 0) : 1;
  53. return true;
  54. }
  55. /**
  56. * Title of the page
  57. *
  58. * @return string Title of the page
  59. */
  60. function title()
  61. {
  62. // TRANS: Title for OAuth connection settings.
  63. return _('Connected applications');
  64. }
  65. /**
  66. * Instructions for use
  67. *
  68. * @return instructions for use
  69. */
  70. function getInstructions()
  71. {
  72. // TRANS: Instructions for OAuth connection settings.
  73. return _('The following connections exist for your account.');
  74. }
  75. /**
  76. * Content area of the page
  77. *
  78. * @return void
  79. */
  80. function showContent()
  81. {
  82. $user = common_current_user();
  83. $profile = $user->getProfile();
  84. $offset = ($this->page - 1) * APPS_PER_PAGE;
  85. $limit = APPS_PER_PAGE + 1;
  86. $connection = $user->getConnectedApps($offset, $limit);
  87. $cnt = 0;
  88. if (!empty($connection)) {
  89. $cal = new ConnectedAppsList($connection, $user, $this);
  90. $cnt = $cal->show();
  91. }
  92. if ($cnt == 0) {
  93. $this->showEmptyListMessage();
  94. }
  95. $this->pagination(
  96. $this->page > 1,
  97. $cnt > APPS_PER_PAGE,
  98. $this->page,
  99. 'connectionssettings',
  100. array('nickname' => $user->nickname)
  101. );
  102. }
  103. /**
  104. * Handle posts to this form
  105. *
  106. * Based on the button that was pressed, muxes out to other functions
  107. * to do the actual task requested.
  108. *
  109. * All sub-functions reload the form with a message -- success or failure.
  110. *
  111. * @return void
  112. */
  113. function handlePost()
  114. {
  115. // CSRF protection
  116. $token = $this->trimmed('token');
  117. if (!$token || $token != common_session_token()) {
  118. // TRANS: Client error displayed when the session token does not match or is not given.
  119. $this->showForm(_('There was a problem with your session token. '.
  120. 'Try again, please.'));
  121. return;
  122. }
  123. if ($this->arg('revoke')) {
  124. $this->revokeAccess($this->oauth_token);
  125. } else {
  126. // TRANS: Client error when submitting a form with unexpected information.
  127. $this->clientError(_('Unexpected form submission.'), 401);
  128. }
  129. }
  130. /**
  131. * Revoke an access token
  132. *
  133. * XXX: Confirm revoke before doing it
  134. *
  135. * @param int $appId the ID of the application
  136. *
  137. */
  138. function revokeAccess($token)
  139. {
  140. $cur = common_current_user();
  141. $appUser = Oauth_application_user::getByUserAndToken($cur, $token);
  142. if (empty($appUser)) {
  143. // TRANS: Client error when trying to revoke access for an application while not being a user of it.
  144. $this->clientError(_('You are not a user of that application.'), 401);
  145. }
  146. $app = Oauth_application::getKV('id', $appUser->application_id);
  147. $datastore = new ApiGNUsocialOAuthDataStore();
  148. $datastore->revoke_token($appUser->token, 1);
  149. $result = $appUser->delete();
  150. if (!$result) {
  151. common_log_db_error($orig, 'DELETE', __FILE__);
  152. // TRANS: Client error when revoking access has failed for some reason.
  153. // TRANS: %s is the application ID revoking access failed for.
  154. $this->clientError(sprintf(_('Unable to revoke access for application: %s.'), $app->id));
  155. }
  156. $msg = 'API OAuth - user %s (id: %d) revoked access token %s for app id %d';
  157. common_log(
  158. LOG_INFO,
  159. sprintf(
  160. $msg,
  161. $cur->nickname,
  162. $cur->id,
  163. $appUser->token,
  164. $appUser->application_id
  165. )
  166. );
  167. $msg = sprintf(
  168. // TRANS: Success message after revoking access for an application.
  169. // TRANS: %1$s is the application name, %2$s is the first part of the user token.
  170. _('You have successfully revoked access for %1$s and the access token starting with %2$s.'),
  171. $app->name,
  172. substr($appUser->token, 0, 7)
  173. );
  174. $this->showForm($msg, true);
  175. }
  176. function showEmptyListMessage()
  177. {
  178. // TRANS: Empty list message when no applications have been authorised yet.
  179. $message = _('You have not authorized any applications to use your account.');
  180. $this->elementStart('div', 'guide');
  181. $this->raw(common_markup_to_html($message));
  182. $this->elementEnd('div');
  183. }
  184. function showSections()
  185. {
  186. $cur = common_current_user();
  187. $this->elementStart('div', array('id' => 'developer-help', 'class' => 'section'));
  188. $this->element('h2', null, 'Developers');
  189. $this->elementStart('p');
  190. $devMsg = sprintf(
  191. // TRANS: Note for developers in the OAuth connection settings form.
  192. // TRANS: This message contains a Markdown link. Do not separate "](".
  193. // TRANS: %s is the URL to the OAuth settings.
  194. _('Are you a developer? [Register an OAuth client application](%s) to use with this instance of StatusNet.'),
  195. common_local_url('oauthappssettings')
  196. );
  197. $output = common_markup_to_html($devMsg);
  198. $this->raw($output);
  199. $this->elementEnd('p');
  200. $this->elementEnd('section');
  201. }
  202. }