mysql.php 3.2 KB

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