openidsettings.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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')) {
  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. public 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. public 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. public 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. public function showContent()
  82. {
  83. if (!common_config('openid', 'trusted_provider')) {
  84. $this->elementStart('form', ['method' => 'post',
  85. 'id' => 'form_settings_openid_add',
  86. 'class' => 'form_settings',
  87. 'action' =>
  88. common_local_url('openidsettings')]);
  89. $this->elementStart('fieldset', ['id' => 'settings_openid_add']);
  90. // TRANS: Fieldset legend.
  91. $this->element('legend', null, _m('LEGEND', 'Add OpenID'));
  92. $this->hidden('token', common_session_token());
  93. $this->elementStart('ul', 'form_data');
  94. $this->elementStart('li');
  95. // TRANS: Field label.
  96. $this->input('openid_url', _m('OpenID URL'), null,
  97. // TRANS: Form guide.
  98. _m('An OpenID URL which identifies you.'),
  99. null, true,
  100. ['placeholder'=>'https://example.com/you']);
  101. $this->elementEnd('li');
  102. $this->elementStart('li');
  103. // TRANS: Field label.
  104. $this->checkbox('openid-synch', _m('Synchronize Account'), false,
  105. // TRANS: Form guide.
  106. _m('Synchronize GNU social profile with this OpenID identity.'));
  107. $this->elementEnd('li');
  108. $this->elementEnd('ul');
  109. // TRANS: Button text for adding an OpenID URL.
  110. $this->submit('settings_openid_add_action-submit', _m('BUTTON', 'Add'), 'submit', 'add');
  111. $this->elementEnd('fieldset');
  112. $this->elementEnd('form');
  113. }
  114. $oid = new User_openid();
  115. $oid->user_id = $this->scoped->getID();
  116. $cnt = $oid->find();
  117. if ($cnt > 0) {
  118. // TRANS: Header on OpenID settings page.
  119. $this->element('h2', null, _m('HEADER', 'OpenID Actions'));
  120. if ($cnt == 1 && !$this->scoped->hasPassword()) {
  121. $this->element('p', 'form_guide',
  122. // TRANS: Form guide.
  123. _m('You can\'t remove your main OpenID account ' .
  124. 'without either adding a password to your ' .
  125. 'GNU social account or another OpenID account. ' .
  126. 'You can synchronize your profile with your ' .
  127. 'OpenID by clicking the button labeled "Synchronize".'));
  128. if ($oid->fetch()) {
  129. $this->elementStart('form', ['method' => 'POST',
  130. 'id' => 'form_settings_openid_actions' . $idx,
  131. 'class' => 'form_settings',
  132. 'action' => common_local_url('openidsettings')]);
  133. $this->elementStart('fieldset');
  134. $this->hidden('token', common_session_token());
  135. $this->element('a', ['href' => $oid->canonical], $oid->display);
  136. $this->hidden("openid_url", $oid->canonical);
  137. // TRANS: Button text to synchronize OpenID with the GS profile.
  138. $this->submit("synch", _m('BUTTON', 'Synchronize'), 'submit synch');
  139. $this->elementEnd('fieldset');
  140. $this->elementEnd('form');
  141. }
  142. } else {
  143. $this->element('p', 'form_guide',
  144. // TRANS: Form guide.
  145. _m('You can remove an OpenID from your account ' .
  146. 'by clicking the button labeled "Remove". ' .
  147. 'You can synchronize your profile with an OpenID ' .
  148. 'by clicking the button labeled "Synchronize".'));
  149. $idx = 0;
  150. while ($oid->fetch()) {
  151. $this->elementStart('form', ['method' => 'POST',
  152. 'id' => 'form_settings_openid_actions' . $idx,
  153. 'class' => 'form_settings',
  154. 'action' => common_local_url('openidsettings')]);
  155. $this->elementStart('fieldset');
  156. $this->hidden('token', common_session_token());
  157. $this->element('a', ['href' => $oid->canonical], $oid->display);
  158. $this->hidden("openid_url{$idx}", $oid->canonical, 'openid_url');
  159. $this->elementStart('span', ['class' => 'element_actions']);
  160. // TRANS: Button text to sync an OpenID with the GS profile.
  161. $this->submit("synch{$idx}", _m('BUTTON', 'Synchronize'), 'submit', 'synch');
  162. // TRANS: Button text to remove an OpenID.
  163. $this->submit("remove{$idx}", _m('BUTTON', 'Remove'), 'submit', 'remove');
  164. $this->elementEnd('span');
  165. $this->elementEnd('fieldset');
  166. $this->elementEnd('form');
  167. $idx++;
  168. }
  169. }
  170. }
  171. $this->elementStart('form', ['method' => 'post',
  172. 'id' => 'form_settings_openid_trustroots',
  173. 'class' => 'form_settings',
  174. 'action' =>
  175. common_local_url('openidsettings')]);
  176. $this->elementStart('fieldset', ['id' => 'settings_openid_trustroots']);
  177. // TRANS: Fieldset legend.
  178. $this->element('legend', null, _m('OpenID Trusted Sites'));
  179. $this->hidden('token', common_session_token());
  180. $this->element('p', 'form_guide',
  181. // TRANS: Form guide.
  182. _m('The following sites are allowed to access your ' .
  183. 'identity and log you in. You can remove a site from ' .
  184. 'this list to deny it access to your OpenID.'));
  185. $this->elementStart('ul', 'form_data');
  186. $user_openid_trustroot = new User_openid_trustroot();
  187. $user_openid_trustroot->user_id = $this->scoped->getID();
  188. if ($user_openid_trustroot->find()) {
  189. while ($user_openid_trustroot->fetch()) {
  190. $this->elementStart('li');
  191. $this->element('input', ['name' => 'openid_trustroot[]',
  192. 'type' => 'checkbox',
  193. 'class' => 'checkbox',
  194. 'value' => $user_openid_trustroot->trustroot,
  195. 'id' => 'openid_trustroot_' . crc32($user_openid_trustroot->trustroot)]);
  196. $this->element('label',
  197. ['class'=>'checkbox',
  198. 'for' => 'openid_trustroot_' . crc32($user_openid_trustroot->trustroot)],
  199. $user_openid_trustroot->trustroot);
  200. $this->elementEnd('li');
  201. }
  202. }
  203. $this->elementEnd('ul');
  204. // TRANS: Button text to remove an OpenID trustroot.
  205. $this->submit('settings_openid_trustroots_action-submit', _m('BUTTON', 'Remove'), 'submit', 'remove_trustroots');
  206. $this->elementEnd('fieldset');
  207. $prefs = User_openid_prefs::getKV('user_id', $this->scoped->getID());
  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. // TRANS: Button text to save OpenID prefs
  213. $this->submit('settings_openid_prefs_save', _m('BUTTON', 'Save'), 'submit', 'save_prefs');
  214. $this->elementEnd('ul');
  215. $this->elementEnd('fieldset');
  216. $this->elementEnd('form');
  217. }
  218. /**
  219. * Handle a POST request
  220. *
  221. * Muxes to different sub-functions based on which button was pushed
  222. *
  223. * @return void
  224. */
  225. protected function doPost()
  226. {
  227. if ($this->arg('add')) {
  228. if (common_config('openid', 'trusted_provider')) {
  229. // TRANS: Form validation error if no OpenID providers can be added.
  230. throw new ServerException(_m('Cannot add new providers.'));
  231. } else {
  232. $_SESSION['openid_synch'] = $this->boolean('openid-synch');
  233. $result = oid_authenticate($this->trimmed('openid_url'), 'finishaddopenid');
  234. if (is_string($result)) { // error message
  235. unset($_SESSION['openid-synch']);
  236. throw new ServerException($result);
  237. }
  238. return _('Added new provider.');
  239. }
  240. } elseif ($this->arg('remove')) {
  241. return $this->removeOpenid();
  242. } elseif ($this->arg('synch')) {
  243. return $this->synchOpenid();
  244. } elseif ($this->arg('remove_trustroots')) {
  245. return $this->removeTrustroots();
  246. } elseif ($this->arg('save_prefs')) {
  247. return $this->savePrefs();
  248. }
  249. // TRANS: Unexpected form validation error.
  250. throw new ServerException(_m('No known action for POST.'));
  251. }
  252. /**
  253. * Handles a request to remove OpenID trustroots from the user's account
  254. *
  255. * Validates input and, if everything is OK, deletes the trustroots.
  256. * Reloads the form with a success or error notification.
  257. *
  258. * @return void
  259. */
  260. public function removeTrustroots()
  261. {
  262. $trustroots = $this->arg('openid_trustroot', []);
  263. foreach ($trustroots as $trustroot) {
  264. $user_openid_trustroot = User_openid_trustroot::pkeyGet(
  265. ['user_id'=>$this->scoped->getID(), 'trustroot'=>$trustroot]
  266. );
  267. if ($user_openid_trustroot) {
  268. $user_openid_trustroot->delete();
  269. } else {
  270. // TRANS: Form validation error when trying to remove a non-existing trustroot.
  271. throw new ClientException(_m('No such OpenID trustroot.'));
  272. }
  273. }
  274. // TRANS: Success message after removing trustroots.
  275. return _m('Trustroots removed.');
  276. }
  277. /**
  278. * Handles a request to remove an OpenID from the user's account
  279. *
  280. * Validates input and, if everything is OK, deletes the OpenID.
  281. * Reloads the form with a success or error notification.
  282. *
  283. * @return void
  284. */
  285. public function removeOpenid()
  286. {
  287. $oid = User_openid::getKV('canonical', $this->trimmed('openid_url'));
  288. if (!$oid instanceof User_openid) {
  289. // TRANS: Form validation error for a non-existing OpenID.
  290. throw new ClientException(_m('No such OpenID.'));
  291. }
  292. if ($this->scoped->getID() != $oid->user_id) {
  293. // TRANS: Form validation error if OpenID is connected to another user.
  294. throw new ClientException(_m('That OpenID does not belong to you.'));
  295. }
  296. $oid->delete();
  297. // TRANS: Success message after removing an OpenID.
  298. return _m('OpenID removed.');
  299. }
  300. /**
  301. * Handles a request to synch an OpenID to the user's profile
  302. *
  303. * @return void
  304. */
  305. public function synchOpenid()
  306. {
  307. $oid = User_openid::getKV('canonical', $this->trimmed('openid_url'));
  308. if (!$oid instanceof User_openid) {
  309. throw new ClientException(_m('No such OpenID.'));
  310. }
  311. $result = oid_authenticate($this->trimmed('openid_url'), 'finishsynchopenid');
  312. if (is_string($result)) { // error message
  313. throw new ServerException($result);
  314. }
  315. return _m('Synchronized OpenID.');
  316. }
  317. /**
  318. * Handles a request to save preferences
  319. *
  320. * Validates input and, if everything is OK, deletes the OpenID.
  321. * Reloads the form with a success or error notification.
  322. *
  323. * @return void
  324. */
  325. public function savePrefs()
  326. {
  327. $orig = null;
  328. $prefs = User_openid_prefs::getKV('user_id', $this->scoped->getID());
  329. if (!$prefs instanceof User_openid_prefs) {
  330. $prefs = new User_openid_prefs();
  331. $prefs->user_id = $this->scoped->getID();
  332. $prefs->created = common_sql_now();
  333. } else {
  334. $orig = clone($prefs);
  335. }
  336. $prefs->hide_profile_link = $this->booleanintstring('hide_profile_link');
  337. if ($orig instanceof User_openid_prefs) {
  338. $prefs->update($orig);
  339. } else {
  340. $prefs->insert();
  341. }
  342. return _m('OpenID preferences saved.');
  343. }
  344. }