openidsettings.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Settings for OpenID
  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 Evan Prodromou <evan@status.net>
  25. * @copyright 2008-2009 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')) {
  30. exit(1);
  31. }
  32. require_once INSTALLDIR.'/plugins/OpenID/openid.php';
  33. /**
  34. * Settings for OpenID
  35. *
  36. * Lets users add, edit and delete OpenIDs from their account
  37. *
  38. * @category Settings
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  42. * @link http://status.net/
  43. */
  44. class OpenidsettingsAction extends SettingsAction
  45. {
  46. /**
  47. * Title of the page
  48. *
  49. * @return string Page title
  50. */
  51. function title()
  52. {
  53. // TRANS: Title of OpenID settings page for a user.
  54. return _m('TITLE','OpenID settings');
  55. }
  56. /**
  57. * Instructions for use
  58. *
  59. * @return string Instructions for use
  60. */
  61. function getInstructions()
  62. {
  63. // TRANS: Form instructions for OpenID settings.
  64. // TRANS: This message contains Markdown links in the form [description](link).
  65. return _m('[OpenID](%%doc.openid%%) lets you log into many sites ' .
  66. 'with the same user account. '.
  67. 'Manage your associated OpenIDs from here.');
  68. }
  69. function showScripts()
  70. {
  71. parent::showScripts();
  72. $this->autofocus('openid_url');
  73. }
  74. /**
  75. * Show the form for OpenID management
  76. *
  77. * We have one form with a few different submit buttons to do different things.
  78. *
  79. * @return void
  80. */
  81. function showContent()
  82. {
  83. $user = common_current_user();
  84. if (!common_config('openid', 'trusted_provider')) {
  85. $this->elementStart('form', array('method' => 'post',
  86. 'id' => 'form_settings_openid_add',
  87. 'class' => 'form_settings',
  88. 'action' =>
  89. common_local_url('openidsettings')));
  90. $this->elementStart('fieldset', array('id' => 'settings_openid_add'));
  91. // TRANS: Fieldset legend.
  92. $this->element('legend', null, _m('LEGEND','Add OpenID'));
  93. $this->hidden('token', common_session_token());
  94. $this->element('p', 'form_guide',
  95. // TRANS: Form guide.
  96. _m('If you want to add an OpenID to your account, ' .
  97. 'enter it in the box below and click "Add".'));
  98. $this->elementStart('ul', 'form_data');
  99. $this->elementStart('li');
  100. $this->element('label', array('for' => 'openid_url'),
  101. // TRANS: Field label.
  102. _m('OpenID URL'));
  103. $this->element('input', array('name' => 'openid_url',
  104. 'type' => 'text',
  105. 'id' => 'openid_url'));
  106. $this->elementEnd('li');
  107. $this->elementEnd('ul');
  108. $this->element('input', array('type' => 'submit',
  109. 'id' => 'settings_openid_add_action-submit',
  110. 'name' => 'add',
  111. 'class' => 'submit',
  112. // TRANS: Button text for adding an OpenID URL.
  113. 'value' => _m('BUTTON','Add')));
  114. $this->elementEnd('fieldset');
  115. $this->elementEnd('form');
  116. }
  117. $oid = new User_openid();
  118. $oid->user_id = $user->id;
  119. $cnt = $oid->find();
  120. if ($cnt > 0) {
  121. // TRANS: Header on OpenID settings page.
  122. $this->element('h2', null, _m('HEADER','Remove OpenID'));
  123. if ($cnt == 1 && !$user->password) {
  124. $this->element('p', 'form_guide',
  125. // TRANS: Form guide.
  126. _m('Removing your only OpenID '.
  127. 'would make it impossible to log in! ' .
  128. 'If you need to remove it, '.
  129. 'add another OpenID first.'));
  130. if ($oid->fetch()) {
  131. $this->elementStart('p');
  132. $this->element('a', array('href' => $oid->canonical),
  133. $oid->display);
  134. $this->elementEnd('p');
  135. }
  136. } else {
  137. $this->element('p', 'form_guide',
  138. // TRANS: Form guide.
  139. _m('You can remove an OpenID from your account '.
  140. 'by clicking the button marked "Remove".'));
  141. $idx = 0;
  142. while ($oid->fetch()) {
  143. $this->elementStart('form',
  144. array('method' => 'POST',
  145. 'id' => 'form_settings_openid_delete' . $idx,
  146. 'class' => 'form_settings',
  147. 'action' =>
  148. common_local_url('openidsettings')));
  149. $this->elementStart('fieldset');
  150. $this->hidden('token', common_session_token());
  151. $this->element('a', array('href' => $oid->canonical),
  152. $oid->display);
  153. $this->element('input', array('type' => 'hidden',
  154. 'id' => 'openid_url'.$idx,
  155. 'name' => 'openid_url',
  156. 'value' => $oid->canonical));
  157. $this->element('input', array('type' => 'submit',
  158. 'id' => 'remove'.$idx,
  159. 'name' => 'remove',
  160. 'class' => 'submit remove',
  161. // TRANS: Button text to remove an OpenID.
  162. 'value' => _m('BUTTON','Remove')));
  163. $this->elementEnd('fieldset');
  164. $this->elementEnd('form');
  165. $idx++;
  166. }
  167. }
  168. }
  169. $this->elementStart('form', array('method' => 'post',
  170. 'id' => 'form_settings_openid_trustroots',
  171. 'class' => 'form_settings',
  172. 'action' =>
  173. common_local_url('openidsettings')));
  174. $this->elementStart('fieldset', array('id' => 'settings_openid_trustroots'));
  175. // TRANS: Fieldset legend.
  176. $this->element('legend', null, _m('OpenID Trusted Sites'));
  177. $this->hidden('token', common_session_token());
  178. $this->element('p', 'form_guide',
  179. // TRANS: Form guide.
  180. _m('The following sites are allowed to access your ' .
  181. 'identity and log you in. You can remove a site from ' .
  182. 'this list to deny it access to your OpenID.'));
  183. $this->elementStart('ul', 'form_data');
  184. $user_openid_trustroot = new User_openid_trustroot();
  185. $user_openid_trustroot->user_id=$user->id;
  186. if($user_openid_trustroot->find()) {
  187. while($user_openid_trustroot->fetch()) {
  188. $this->elementStart('li');
  189. $this->element('input', array('name' => 'openid_trustroot[]',
  190. 'type' => 'checkbox',
  191. 'class' => 'checkbox',
  192. 'value' => $user_openid_trustroot->trustroot,
  193. 'id' => 'openid_trustroot_' . crc32($user_openid_trustroot->trustroot)));
  194. $this->element('label', array('class'=>'checkbox', 'for' => 'openid_trustroot_' . crc32($user_openid_trustroot->trustroot)),
  195. $user_openid_trustroot->trustroot);
  196. $this->elementEnd('li');
  197. }
  198. }
  199. $this->elementEnd('ul');
  200. $this->element('input', array('type' => 'submit',
  201. 'id' => 'settings_openid_trustroots_action-submit',
  202. 'name' => 'remove_trustroots',
  203. 'class' => 'submit',
  204. // TRANS: Button text to remove an OpenID trustroot.
  205. 'value' => _m('BUTTON','Remove')));
  206. $this->elementEnd('fieldset');
  207. $prefs = User_openid_prefs::getKV('user_id', $user->id);
  208. $this->elementStart('fieldset');
  209. $this->element('legend', null, _m('LEGEND','Preferences'));
  210. $this->elementStart('ul', 'form_data');
  211. $this->checkBox('hide_profile_link', "Hide OpenID links from my profile", !empty($prefs) && $prefs->hide_profile_link);
  212. $this->element('input', array('type' => 'submit',
  213. 'id' => 'settings_openid_prefs_save',
  214. 'name' => 'save_prefs',
  215. 'class' => 'submit',
  216. // TRANS: Button text to save OpenID prefs
  217. 'value' => _m('BUTTON','Save')));
  218. $this->elementEnd('ul');
  219. $this->elementEnd('fieldset');
  220. $this->elementEnd('form');
  221. }
  222. /**
  223. * Handle a POST request
  224. *
  225. * Muxes to different sub-functions based on which button was pushed
  226. *
  227. * @return void
  228. */
  229. function handlePost()
  230. {
  231. // CSRF protection
  232. $token = $this->trimmed('token');
  233. if (!$token || $token != common_session_token()) {
  234. // TRANS: Client error displayed when the session token does not match or is not given.
  235. $this->showForm(_m('There was a problem with your session token. '.
  236. 'Try again, please.'));
  237. return;
  238. }
  239. if ($this->arg('add')) {
  240. if (common_config('openid', 'trusted_provider')) {
  241. // TRANS: Form validation error if no OpenID providers can be added.
  242. $this->showForm(_m('Cannot add new providers.'));
  243. } else {
  244. $result = oid_authenticate($this->trimmed('openid_url'),
  245. 'finishaddopenid');
  246. if (is_string($result)) { // error message
  247. $this->showForm($result);
  248. }
  249. }
  250. } else if ($this->arg('remove')) {
  251. $this->removeOpenid();
  252. } else if($this->arg('remove_trustroots')) {
  253. $this->removeTrustroots();
  254. } else if($this->arg('save_prefs')) {
  255. $this->savePrefs();
  256. } else {
  257. // TRANS: Unexpected form validation error.
  258. $this->showForm(_m('Something weird happened.'));
  259. }
  260. }
  261. /**
  262. * Handles a request to remove OpenID trustroots from the user's account
  263. *
  264. * Validates input and, if everything is OK, deletes the trustroots.
  265. * Reloads the form with a success or error notification.
  266. *
  267. * @return void
  268. */
  269. function removeTrustroots()
  270. {
  271. $user = common_current_user();
  272. $trustroots = $this->arg('openid_trustroot');
  273. if($trustroots) {
  274. foreach($trustroots as $trustroot) {
  275. $user_openid_trustroot = User_openid_trustroot::pkeyGet(
  276. array('user_id'=>$user->id, 'trustroot'=>$trustroot));
  277. if($user_openid_trustroot) {
  278. $user_openid_trustroot->delete();
  279. } else {
  280. // TRANS: Form validation error when trying to remove a non-existing trustroot.
  281. $this->showForm(_m('No such OpenID trustroot.'));
  282. return;
  283. }
  284. }
  285. // TRANS: Success message after removing trustroots.
  286. $this->showForm(_m('Trustroots removed.'), true);
  287. } else {
  288. $this->showForm();
  289. }
  290. return;
  291. }
  292. /**
  293. * Handles a request to remove an OpenID from the user's account
  294. *
  295. * Validates input and, if everything is OK, deletes the OpenID.
  296. * Reloads the form with a success or error notification.
  297. *
  298. * @return void
  299. */
  300. function removeOpenid()
  301. {
  302. $openid_url = $this->trimmed('openid_url');
  303. $oid = User_openid::getKV('canonical', $openid_url);
  304. if (!$oid) {
  305. // TRANS: Form validation error for a non-existing OpenID.
  306. $this->showForm(_m('No such OpenID.'));
  307. return;
  308. }
  309. $cur = common_current_user();
  310. if (!$cur || $oid->user_id != $cur->id) {
  311. // TRANS: Form validation error if OpenID is connected to another user.
  312. $this->showForm(_m('That OpenID does not belong to you.'));
  313. return;
  314. }
  315. $oid->delete();
  316. // TRANS: Success message after removing an OpenID.
  317. $this->showForm(_m('OpenID removed.'), true);
  318. return;
  319. }
  320. /**
  321. * Handles a request to save preferences
  322. *
  323. * Validates input and, if everything is OK, deletes the OpenID.
  324. * Reloads the form with a success or error notification.
  325. *
  326. * @return void
  327. */
  328. function savePrefs()
  329. {
  330. $cur = common_current_user();
  331. if (empty($cur)) {
  332. throw new ClientException(_("Not logged in."));
  333. }
  334. $orig = null;
  335. $prefs = User_openid_prefs::getKV('user_id', $cur->id);
  336. if (empty($prefs)) {
  337. $prefs = new User_openid_prefs();
  338. $prefs->user_id = $cur->id;
  339. $prefs->created = common_sql_now();
  340. } else {
  341. $orig = clone($prefs);
  342. }
  343. $prefs->hide_profile_link = $this->boolean('hide_profile_link');
  344. if (empty($orig)) {
  345. $prefs->insert();
  346. } else {
  347. $prefs->update($orig);
  348. }
  349. $this->showForm(_m('OpenID preferences saved.'), true);
  350. return;
  351. }
  352. }