mysql.php 4.7 KB

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