recoverpassword.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2008, 2009, StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
  20. // You have 24 hours to claim your password
  21. define('MAX_RECOVERY_TIME', 24 * 60 * 60);
  22. class RecoverpasswordAction extends Action
  23. {
  24. var $mode = null;
  25. var $msg = null;
  26. var $success = null;
  27. function handle($args)
  28. {
  29. parent::handle($args);
  30. if (common_logged_in()) {
  31. // TRANS: Client error displayed trying to recover password while already logged in.
  32. $this->clientError(_('You are already logged in!'));
  33. } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  34. if ($this->arg('recover')) {
  35. $this->recoverPassword();
  36. } else if ($this->arg('reset')) {
  37. $this->resetPassword();
  38. } else {
  39. // TRANS: Client error displayed when unexpected data is posted in the password recovery form.
  40. $this->clientError(_('Unexpected form submission.'));
  41. }
  42. } else {
  43. if ($this->trimmed('code')) {
  44. $this->checkCode();
  45. } else {
  46. $this->showForm();
  47. }
  48. }
  49. }
  50. function checkCode()
  51. {
  52. $code = $this->trimmed('code');
  53. $confirm = Confirm_address::getKV('code', $code);
  54. if (!$confirm) {
  55. // TRANS: Client error displayed when password recovery code is not correct.
  56. $this->clientError(_('No such recovery code.'));
  57. }
  58. if ($confirm->address_type != 'recover') {
  59. // TRANS: Client error displayed when no proper password recovery code was submitted.
  60. $this->clientError(_('Not a recovery code.'));
  61. }
  62. $user = User::getKV($confirm->user_id);
  63. if (!$user) {
  64. // TRANS: Server error displayed trying to recover password without providing a user.
  65. $this->serverError(_('Recovery code for unknown user.'));
  66. }
  67. $touched = strtotime($confirm->modified);
  68. $email = $confirm->address;
  69. // Burn this code
  70. $result = $confirm->delete();
  71. if (!$result) {
  72. common_log_db_error($confirm, 'DELETE', __FILE__);
  73. // TRANS: Server error displayed removing a password recovery code from the database.
  74. $this->serverError(_('Error with confirmation code.'));
  75. }
  76. // These should be reaped, but for now we just check mod time
  77. // Note: it's still deleted; let's avoid a second attempt!
  78. if ((time() - $touched) > MAX_RECOVERY_TIME) {
  79. common_log(LOG_WARNING,
  80. 'Attempted redemption on recovery code ' .
  81. 'that is ' . $touched . ' seconds old. ');
  82. // TRANS: Client error displayed trying to recover password with too old a recovery code.
  83. $this->clientError(_('This confirmation code is too old. ' .
  84. 'Please start again.'));
  85. }
  86. // If we used an outstanding confirmation to send the email,
  87. // it's been confirmed at this point.
  88. if (!$user->email) {
  89. $orig = clone($user);
  90. $user->email = $email;
  91. // Throws exception on failure.
  92. $user->updateWithKeys($orig);
  93. }
  94. // Success!
  95. $this->setTempUser($user);
  96. $this->showPasswordForm();
  97. }
  98. function setTempUser(&$user)
  99. {
  100. common_ensure_session();
  101. $_SESSION['tempuser'] = $user->id;
  102. }
  103. function getTempUser()
  104. {
  105. common_ensure_session();
  106. $user_id = $_SESSION['tempuser'];
  107. if ($user_id) {
  108. $user = User::getKV($user_id);
  109. }
  110. return $user;
  111. }
  112. function clearTempUser()
  113. {
  114. common_ensure_session();
  115. unset($_SESSION['tempuser']);
  116. }
  117. function showPageNotice()
  118. {
  119. if ($this->msg) {
  120. $this->element('div', ($this->success) ? 'success' : 'error', $this->msg);
  121. } else {
  122. $this->elementStart('div', 'instructions');
  123. if ($this->mode == 'recover') {
  124. $this->element('p', null,
  125. // TRANS: Page notice for password recovery page.
  126. _('If you have forgotten or lost your' .
  127. ' password, you can get a new one sent to' .
  128. ' the email address you have stored' .
  129. ' in your account.'));
  130. } else if ($this->mode == 'reset') {
  131. $this->element('p', null,
  132. // TRANS: Page notice for password change page.
  133. _('You have been identified. Enter a' .
  134. ' new password below.'));
  135. }
  136. $this->elementEnd('div');
  137. }
  138. }
  139. function showForm($msg=null)
  140. {
  141. $this->msg = $msg;
  142. $this->mode = 'recover';
  143. $this->showPage();
  144. }
  145. function showContent()
  146. {
  147. if ($this->mode == 'recover') {
  148. $this->showRecoverForm();
  149. } else if ($this->mode == 'reset') {
  150. $this->showResetForm();
  151. }
  152. }
  153. function showRecoverForm()
  154. {
  155. $this->elementStart('form', array('method' => 'post',
  156. 'id' => 'form_password_recover',
  157. 'class' => 'form_settings',
  158. 'action' => common_local_url('recoverpassword')));
  159. $this->elementStart('fieldset');
  160. // TRANS: Fieldset legend for password recovery page.
  161. $this->element('legend', null, _('Password recovery'));
  162. $this->elementStart('ul', 'form_data');
  163. $this->elementStart('li');
  164. // TRANS: Field label on password recovery page.
  165. $this->input('nicknameoremail', _('Nickname or email address'),
  166. $this->trimmed('nicknameoremail'),
  167. // TRANS: Title for field label on password recovery page.
  168. _('Your nickname on this server, ' .
  169. 'or your registered email address.'));
  170. $this->elementEnd('li');
  171. $this->elementEnd('ul');
  172. $this->element('input', array('name' => 'recover',
  173. 'type' => 'hidden',
  174. // TRANS: Field label on password recovery page.
  175. 'value' => _('Recover')));
  176. // TRANS: Button text on password recovery page.
  177. $this->submit('recover', _m('BUTTON','Recover'));
  178. $this->elementEnd('fieldset');
  179. $this->elementEnd('form');
  180. }
  181. function title()
  182. {
  183. switch ($this->mode) {
  184. // TRANS: Title for password recovery page in password reset mode.
  185. case 'reset': return _('Reset password');
  186. // TRANS: Title for password recovery page in password recover mode.
  187. case 'recover': return _('Recover password');
  188. // TRANS: Title for password recovery page in email sent mode.
  189. case 'sent': return _('Password recovery requested');
  190. // TRANS: Title for password recovery page in password saved mode.
  191. case 'saved': return _('Password saved');
  192. default:
  193. // TRANS: Title for password recovery page when an unknown action has been specified.
  194. return _('Unknown action');
  195. }
  196. }
  197. function showPasswordForm($msg=null)
  198. {
  199. $this->msg = $msg;
  200. $this->mode = 'reset';
  201. $this->showPage();
  202. }
  203. function showResetForm()
  204. {
  205. $this->elementStart('form', array('method' => 'post',
  206. 'id' => 'form_password_change',
  207. 'class' => 'form_settings',
  208. 'action' => common_local_url('recoverpassword')));
  209. $this->elementStart('fieldset');
  210. // TRANS: Fieldset legend for password reset form.
  211. $this->element('legend', null, _('Password change'));
  212. $this->hidden('token', common_session_token());
  213. $this->elementStart('ul', 'form_data');
  214. $this->elementStart('li');
  215. // TRANS: Field label for password reset form.
  216. $this->password('newpassword', _('New password'),
  217. // TRANS: Title for field label for password reset form.
  218. _('6 or more characters, and do not forget it!'));
  219. $this->elementEnd('li');
  220. $this->elementStart('li');
  221. // TRANS: Field label for password reset form where the password has to be typed again.
  222. $this->password('confirm', _('Confirm'),
  223. // TRANS: Title for field label for password reset form where the password has to be typed again.
  224. _('Same as password above.'));
  225. $this->elementEnd('li');
  226. $this->elementEnd('ul');
  227. // TRANS: Button text for password reset form.
  228. $this->submit('reset', _m('BUTTON','Reset'));
  229. $this->elementEnd('fieldset');
  230. $this->elementEnd('form');
  231. }
  232. function recoverPassword()
  233. {
  234. $nore = $this->trimmed('nicknameoremail');
  235. if (!$nore) {
  236. // TRANS: Form instructions for password recovery form.
  237. $this->showForm(_('Enter a nickname or email address.'));
  238. return;
  239. }
  240. try {
  241. User::recoverPassword($nore);
  242. $this->mode = 'sent';
  243. // TRANS: User notification after an e-mail with instructions was sent from the password recovery form.
  244. $this->msg = _('Instructions for recovering your password ' .
  245. 'have been sent to the email address registered to your ' .
  246. 'account.');
  247. $this->success = true;
  248. } catch (Exception $e) {
  249. $this->success = false;
  250. $this->msg = $e->getMessage();
  251. }
  252. $this->showPage();
  253. }
  254. function resetPassword()
  255. {
  256. // CSRF protection
  257. $token = $this->trimmed('token');
  258. if (!$token || $token != common_session_token()) {
  259. // TRANS: Form validation error message.
  260. $this->showForm(_('There was a problem with your session token. Try again, please.'));
  261. return;
  262. }
  263. $user = $this->getTempUser();
  264. if (!$user) {
  265. // TRANS: Client error displayed when trying to reset as password without providing a user.
  266. $this->clientError(_('Unexpected password reset.'));
  267. }
  268. $newpassword = $this->trimmed('newpassword');
  269. $confirm = $this->trimmed('confirm');
  270. if (!$newpassword || strlen($newpassword) < 6) {
  271. // TRANS: Reset password form validation error message.
  272. $this->showPasswordForm(_('Password must be 6 characters or more.'));
  273. return;
  274. }
  275. if ($newpassword != $confirm) {
  276. // TRANS: Reset password form validation error message.
  277. $this->showPasswordForm(_('Password and confirmation do not match.'));
  278. return;
  279. }
  280. // OK, we're ready to go
  281. $original = clone($user);
  282. $user->password = common_munge_password($newpassword, $user->id);
  283. if (!$user->update($original)) {
  284. common_log_db_error($user, 'UPDATE', __FILE__);
  285. // TRANS: Reset password form validation error message.
  286. $this->serverError(_('Cannot save new password.'));
  287. }
  288. $this->clearTempUser();
  289. if (!common_set_user($user->nickname)) {
  290. // TRANS: Server error displayed when something does wrong with the user object during password reset.
  291. $this->serverError(_('Error setting user.'));
  292. }
  293. common_real_login(true);
  294. $this->mode = 'saved';
  295. // TRANS: Success message for user after password reset.
  296. $this->msg = _('New password successfully saved. ' .
  297. 'You are now logged in.');
  298. $this->success = true;
  299. $this->showPage();
  300. }
  301. /**
  302. * A local menu
  303. *
  304. * Shows different login/register actions.
  305. *
  306. * @return void
  307. */
  308. function showLocalNav()
  309. {
  310. $nav = new LoginGroupNav($this);
  311. $nav->show();
  312. }
  313. }