mysql.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. $mysql_saslcrypt, $mysql_unixcrypt, $cpw_mysql;
  16. // Initialize defaults
  17. $mysql_server = 'localhost';
  18. $mysql_database = 'email';
  19. $mysql_table = 'users';
  20. // The names of the user ID and password columns
  21. $mysql_userid_field = 'id';
  22. $mysql_password_field ='password';
  23. // The user to log into MySQL with (must have rights)
  24. $mysql_manager_id = 'email_admin';
  25. $mysql_manager_pw = 'xxxxxxx';
  26. // saslcrypt checked first - if it is 1, UNIX crypt is not used.
  27. $mysql_saslcrypt = 0; // use MySQL password() function
  28. $mysql_unixcrypt = 0; // use UNIX crypt() function
  29. // get overrides from config.
  30. if ( isset($cpw_mysql) && is_array($cpw_mysql) && !empty($cpw_mysql) )
  31. {
  32. foreach ( $cpw_mysql as $key => $value )
  33. {
  34. if ( isset(${'mysql_'.$key}) )
  35. ${'mysql_'.$key} = $value;
  36. }
  37. }
  38. global $squirrelmail_plugin_hooks;
  39. $squirrelmail_plugin_hooks['change_password_dochange']['mysql'] =
  40. 'cpw_mysql_dochange';
  41. /**
  42. * This is the function that is specific to your backend. It takes
  43. * the current password (as supplied by the user) and the desired
  44. * new password. It will return an array of messages. If everything
  45. * was successful, the array will be empty. Else, it will contain
  46. * the errormessage(s).
  47. * Constants to be used for these messages:
  48. * CPW_CURRENT_NOMATCH -> "Your current password is not correct."
  49. * CPW_INVALID_PW -> "Your new password contains invalid characters."
  50. *
  51. * @param array data The username/currentpw/newpw data.
  52. * @return array Array of error messages.
  53. */
  54. function cpw_mysql_dochange($data)
  55. {
  56. // unfortunately, we can only pass one parameter to a hook function,
  57. // so we have to pass it as an array.
  58. $username = $data['username'];
  59. $curpw = $data['curpw'];
  60. $newpw = $data['newpw'];
  61. $msgs = array();
  62. global $mysql_server, $mysql_database, $mysql_table, $mysql_userid_field,
  63. $mysql_password_field, $mysql_manager_id, $mysql_manager_pw,
  64. $mysql_saslcrypt, $mysql_unixcrypt;
  65. // TODO: allow to choose between mysql_connect() and mysql_pconnect() functions.
  66. $ds = mysql_pconnect($mysql_server, $mysql_manager_id, $mysql_manager_pw);
  67. if (! $ds) {
  68. array_push($msgs, _("Cannot connect to Database Server, please try later!"));
  69. return $msgs;
  70. }
  71. if (!mysql_select_db($mysql_database, $ds)) {
  72. array_push($msgs, _("Database not found on server"));
  73. return $msgs;
  74. }
  75. $query_string = 'SELECT ' . $mysql_userid_field . ',' . $mysql_password_field
  76. . ' FROM ' . $mysql_table
  77. . ' WHERE ' . $mysql_userid_field . '="' . mysql_escape_string($username) .'"'
  78. . ' AND ' . $mysql_password_field;
  79. if ($mysql_saslcrypt) {
  80. $query_string .= '=password("'.mysql_escape_string($curpw).'")';
  81. } elseif ($mysql_unixcrypt) {
  82. // FIXME: why password field name is used for salting
  83. $query_string .= '=encrypt("'.mysql_escape_string($curpw).'", '.$mysql_password_field . ')';
  84. } else {
  85. $query_string .= '="' . mysql_escape_string($curpw) . '"';
  86. }
  87. $select_result = mysql_query($query_string, $ds);
  88. if (!$select_result) {
  89. array_push($msgs, _("SQL call failed, try again later."));
  90. return $msgs;
  91. }
  92. if (mysql_num_rows($select_result) == 0) {
  93. array_push($msgs, CPW_CURRENT_NOMATCH);
  94. return $msgs;
  95. }
  96. if (mysql_num_rows($select_result) > 1) {
  97. //make sure we only have 1 uid
  98. array_push($msgs, _("Duplicate login entries detected, cannot change password!"));
  99. return $msgs;
  100. }
  101. $update_string = 'UPDATE '. $mysql_table . ' SET ' . $mysql_password_field;
  102. if ($mysql_saslcrypt) {
  103. $update_string .= '=password("'.mysql_escape_string($newpw).'")';
  104. } elseif ($mysql_unixcrypt) {
  105. // FIXME: use random salt when you create new password
  106. $update_string .= '=encrypt("'.mysql_escape_string($newpw).'", '.$mysql_password_field . ')';
  107. } else {
  108. $update_string .= '="' . mysql_escape_string($newpw) . '"';
  109. }
  110. $update_string .= ' WHERE ' . $mysql_userid_field . ' = "' . mysql_escape_string($username) . '"';
  111. if (!mysql_query($update_string, $ds)) {
  112. array_push($msgs, _("Password change was not successful!"));
  113. }
  114. return $msgs;
  115. }