controller.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. class OC_Core_LostPassword_Controller {
  9. protected static function displayLostPasswordPage($error, $requested) {
  10. OC_Template::printGuestPage('core/lostpassword', 'lostpassword',
  11. array('error' => $error, 'requested' => $requested));
  12. }
  13. protected static function displayResetPasswordPage($success, $args) {
  14. $route_args = array();
  15. $route_args['token'] = $args['token'];
  16. $route_args['user'] = $args['user'];
  17. OC_Template::printGuestPage('core/lostpassword', 'resetpassword',
  18. array('success' => $success, 'args' => $route_args));
  19. }
  20. protected static function checkToken($user, $token) {
  21. return OC_Preferences::getValue($user, 'owncloud', 'lostpassword') === hash('sha256', $token);
  22. }
  23. public static function index($args) {
  24. self::displayLostPasswordPage(false, false);
  25. }
  26. public static function sendEmail($args) {
  27. if (OC_User::userExists($_POST['user'])) {
  28. $token = hash('sha256', OC_Util::generate_random_bytes(30).OC_Config::getValue('passwordsalt', ''));
  29. OC_Preferences::setValue($_POST['user'], 'owncloud', 'lostpassword',
  30. hash('sha256', $token)); // Hash the token again to prevent timing attacks
  31. $email = OC_Preferences::getValue($_POST['user'], 'settings', 'email', '');
  32. if (!empty($email)) {
  33. $link = OC_Helper::linkToRoute('core_lostpassword_reset',
  34. array('user' => $_POST['user'], 'token' => $token));
  35. $link = OC_Helper::makeURLAbsolute($link);
  36. $tmpl = new OC_Template('core/lostpassword', 'email');
  37. $tmpl->assign('link', $link, false);
  38. $msg = $tmpl->fetchPage();
  39. $l = OC_L10N::get('core');
  40. $from = OCP\Util::getDefaultEmailAddress('lostpassword-noreply');
  41. try {
  42. OC_Mail::send($email, $_POST['user'], $l->t('ownCloud password reset'), $msg, $from, 'ownCloud');
  43. } catch (Exception $e) {
  44. OC_Template::printErrorPage( 'A problem occurs during sending the e-mail please contact your administrator.');
  45. }
  46. self::displayLostPasswordPage(false, true);
  47. } else {
  48. self::displayLostPasswordPage(true, false);
  49. }
  50. } else {
  51. self::displayLostPasswordPage(true, false);
  52. }
  53. }
  54. public static function reset($args) {
  55. // Someone wants to reset their password:
  56. if(self::checkToken($args['user'], $args['token'])) {
  57. self::displayResetPasswordPage(false, $args);
  58. } else {
  59. // Someone lost their password
  60. self::displayLostPasswordPage(false, false);
  61. }
  62. }
  63. public static function resetPassword($args) {
  64. if (self::checkToken($args['user'], $args['token'])) {
  65. if (isset($_POST['password'])) {
  66. if (OC_User::setPassword($args['user'], $_POST['password'])) {
  67. OC_Preferences::deleteKey($args['user'], 'owncloud', 'lostpassword');
  68. OC_User::unsetMagicInCookie();
  69. self::displayResetPasswordPage(true, $args);
  70. } else {
  71. self::displayResetPasswordPage(false, $args);
  72. }
  73. } else {
  74. self::reset($args);
  75. }
  76. } else {
  77. // Someone lost their password
  78. self::displayLostPasswordPage(false, false);
  79. }
  80. }
  81. }