functions.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * functions.php - Change Password plugin
  4. *
  5. * Copyright (c) 2003-2004 The SquirrelMail Project Team
  6. * Licensed under the GNU GPL. For full terms see the file COPYING.
  7. *
  8. * @version $Id$
  9. * @package plugins
  10. * @subpackage change_password
  11. */
  12. /**
  13. * Will verify the input against a set of criteria:
  14. * is every field supplied, does verify password match,
  15. * does current password validate, ..
  16. * These criteria are (for now) backend-independant.
  17. *
  18. * @return array Array with zero or more error messages.
  19. */
  20. function cpw_check_input()
  21. {
  22. global $cpw_pass_min_length, $cpw_pass_max_length;
  23. // formdata
  24. sqgetGlobalVar('cpw_curpass', $currentpw, SQ_POST);
  25. sqgetGlobalVar('cpw_newpass', $newpw, SQ_POST);
  26. sqgetGlobalVar('cpw_verify', $verifypw, SQ_POST);
  27. // for decrypting current password
  28. sqgetGlobalVar('key', $key, SQ_COOKIE);
  29. sqgetGlobalVar('onetimepad', $onetimepad,SQ_SESSION);
  30. $msg = array();
  31. if(!$currentpw) {
  32. $msg[] = _("You must type in your current password.");
  33. } elseif($currentpw != OneTimePadDecrypt($key, $onetimepad)) {
  34. $msg[] = _("Your current password is not correct.");
  35. }
  36. if(!$newpw) {
  37. $msg[] = _("You must type in a new password.");
  38. }
  39. if(!$verifypw) {
  40. $msg[] = _("You must also type in your new password in the verify box.");
  41. } elseif ($verifypw != $newpw) {
  42. $msg[] = _("Your new password does not match the verify password.");
  43. }
  44. if($newpw && (strlen($newpw) < $cpw_pass_min_length ||
  45. strlen($newpw) > $cpw_pass_max_length ) ) {
  46. $msg[] = sprintf(_("Your new password should be %s to %s characters long."),
  47. $cpw_pass_min_length, $cpw_pass_max_length);
  48. }
  49. // do we need to do checks that are backend-specific and should
  50. // be handled by a hook? I know of none now, bnd those checks can
  51. // also be done in the backend dochange() function. If there turns
  52. // out to be a need for it we can add a hook for that here.
  53. return $msg;
  54. }
  55. define('CPW_CURRENT_NOMATCH', _("Your current password is not correct."));
  56. define('CPW_INVALID_PW', _("Your new password contains invalid characters."));
  57. /**
  58. * Does the actual password changing (meaning it calls the hook function
  59. * from the backend that does this. If something goes wrong, return error
  60. * message(s). If everything ok, change the password in the session so the
  61. * user doesn't have to log out, and redirect back to the options screen.
  62. */
  63. function cpw_do_change()
  64. {
  65. global $cpw_backend;
  66. sqgetGlobalVar('cpw_curpass', $curpw, SQ_POST);
  67. sqgetGlobalVar('cpw_newpass', $newpw, SQ_POST);
  68. sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
  69. sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
  70. sqgetGlobalVar('key', $key, SQ_COOKIE);
  71. sqgetGlobalVar('username', $username, SQ_SESSION);
  72. require_once(SM_PATH . 'plugins/change_password/backend/'.$cpw_backend.'.php');
  73. $msgs = do_hook_function('change_password_dochange',
  74. array (
  75. 'username' => $username,
  76. 'curpw' => $curpw,
  77. 'newpw' => $newpw
  78. ) );
  79. /* something bad happened, return */
  80. if(count($msgs) > 0) {
  81. return $msgs;
  82. }
  83. /* update our password stored in the session */
  84. $onetimepad = OneTimePadCreate(strlen($newpw));
  85. $_SESSION['onetimepad'] = $onetimepad;
  86. $key = OneTimePadEncrypt($newpw, $onetimepad);
  87. setcookie('key', $key, 0, $base_uri);
  88. /* make sure we write the session data before we redirect */
  89. session_write_close();
  90. header('Location: '.SM_PATH. 'src/options.php?optmode=submit&plugin_change_password=1');
  91. exit;
  92. }