mysql.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * MySQL change password backend
  4. *
  5. * @author Thijs Kinkhorst <kink@squirrelmail.org>
  6. * @version $Id$
  7. * @package plugins
  8. * @subpackage change_password
  9. */
  10. /**
  11. * Config vars
  12. */
  13. global $mysql_server, $mysql_database, $mysql_table, $mysql_userid_field,
  14. $mysql_password_field, $mysql_manager_id, $mysql_manager_pw;
  15. // The MySQL Server
  16. $mysql_server = 'localhost';
  17. $mysql_database = 'email';
  18. $mysql_table = 'users';
  19. // The names of the user ID and password columns
  20. $mysql_userid_field = 'id';
  21. $mysql_password_field ='password';
  22. // The user to log into MySQL with (must have rights)
  23. $mysql_manager_id = 'email_admin';
  24. $mysql_manager_pw = 'xxxxxxx';
  25. // NO NEED TO CHANGE ANYTHING BELOW THIS LINE
  26. global $squirrelmail_plugin_hooks;
  27. $squirrelmail_plugin_hooks['change_password_dochange']['mysql'] =
  28. 'cpw_mysql_dochange';
  29. /**
  30. * This is the function that is specific to your backend. It takes
  31. * the current password (as supplied by the user) and the desired
  32. * new password. It will return an array of messages. If everything
  33. * was successful, the array will be empty. Else, it will contain
  34. * the errormessage(s).
  35. * Constants to be used for these messages:
  36. * CPW_CURRENT_NOMATCH -> "Your current password is not correct."
  37. * CPW_INVALID_PW -> "Your new password contains invalid characters."
  38. *
  39. * @param array data The username/currentpw/newpw data.
  40. * @return array Array of error messages.
  41. */
  42. function cpw_mysql_dochange($data)
  43. {
  44. // unfortunately, we can only pass one parameter to a hook function,
  45. // so we have to pass it as an array.
  46. $username = $data['username'];
  47. $curpw = $data['curpw'];
  48. $newpw = $data['newpw'];
  49. $msgs = array();
  50. global $mysql_server, $mysql_database, $mysql_table, $mysql_userid_field,
  51. $mysql_password_field, $mysql_manager_id, $mysql_manager_pw;
  52. $ds = mysql_pconnect($mysql_server, $mysql_manager_id, $mysql_manager_pw);
  53. if (! $ds) {
  54. array_push($msgs, _("Cannot connect to Database Server, please try later!"));
  55. return $msgs;
  56. }
  57. if (!mysql_select_db($mysql_database, $ds)) {
  58. array_push($msgs, _("Database not found on server"));
  59. return $msgs;
  60. }
  61. $query_string = 'SELECT ' . $mysql_userid_field . ',' . $mysql_password_field
  62. . ' FROM ' . $mysql_table
  63. . ' WHERE ' . $mysql_userid_field . '="' . mysql_escape_string($username) .'"'
  64. . ' AND ' . $mysql_password_field . '="' . mysql_escape_string($curpw) . '"';
  65. $select_result = mysql_query($query_string, $ds);
  66. if (!$select_result) {
  67. array_push($msgs, _("SQL call failed, try again later."));
  68. return $msgs;
  69. }
  70. if (mysql_num_rows($select_result) == 0) {
  71. array_push($msgs, CPW_CURRENT_NOMATCH);
  72. return $msgs;
  73. }
  74. if (mysql_num_rows($select_result) > 1) {
  75. //make sure we only have 1 uid
  76. array_push($msgs, _("Duplicate login entries detected, cannot change password!"));
  77. return $msgs;
  78. }
  79. $update_string = 'UPDATE '. $mysql_table . ' SET ' . $mysql_password_field
  80. . ' = "' . mysql_escape_string($cp_newpass) . '"'
  81. . ' WHERE ' . $mysql_userid_field . ' = "' . mysql_escape_string($username) . '"';
  82. if (!mysql_query($update_string, $ds)) {
  83. array_push($msgs, _("Password change was not successful!"));
  84. }
  85. return $msgs;
  86. }