openidsettings.php 13 KB

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