PageRenderTime 110ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/WebCalendar-1.2.5/tools/convert_passwords.php

#
PHP | 95 lines | 62 code | 11 blank | 22 comment | 14 complexity | b049cc559cf1c949a28b7bf5769c291f MD5 | raw file
Possible License(s): LGPL-2.1
  1. #!/usr/local/bin/php -q
  2. <?php
  3. /*
  4. * $Id: convert_passwords.php,v 1.8.2.6 2011/04/27 00:27:35 rjones6061 Exp $
  5. *
  6. * This script will alter the webcal_user table to allow 32 character passwords
  7. * and convert user passwords to PHP md5 passwords.
  8. *
  9. * It is necessary to run this to upgrade to version 0.9.43 from any version.
  10. *
  11. *
  12. * ** NOTE: This script should only be run ONCE and then be deleted!!
  13. *
  14. */
  15. /********************************************************************/
  16. define ( '__WC_BASEDIR', '..' ); // Points to the base WebCalendar directory
  17. // relative to current working directory.
  18. define ( '__WC_INCLUDEDIR', '../includes' );
  19. require_once __WC_INCLUDEDIR . '/classes/WebCalendar.class';
  20. $WebCalendar = new WebCalendar ( __FILE__ );
  21. include __WC_INCLUDEDIR . '/config.php';
  22. include __WC_INCLUDEDIR . '/dbi4php.php';
  23. $WebCalendar->initializeFirstPhase();
  24. $WebCalendar->initializeSecondPhase();
  25. $c = dbi_connect ( $db_host, $db_login, $db_password, $db_database );
  26. if ( ! $c ) {
  27. echo "Error connecting to database: " . dbi_error ();
  28. exit;
  29. }
  30. // First, look at the passwords. If we find and md5 hash in there,
  31. // (it will have 32 chars instead of < 25 like in the old version),
  32. // then we know this script was already run.
  33. $sql = "SELECT cal_passwd FROM webcal_user";
  34. $res = dbi_execute ( $sql );
  35. $doneBefore = false;
  36. if ( $res ) {
  37. if ( $row = dbi_fetch_row ( $res ) ) {
  38. if ( strlen ( $row[0] ) > 30 )
  39. $doneBefore = true;
  40. }
  41. dbi_free_result ( $res );
  42. } else {
  43. echo "Database error: " . dbi_error ();
  44. exit;
  45. }
  46. if ( $doneBefore ) {
  47. echo "Passwords were already converted to md5!\n<br />\n";
  48. exit;
  49. }
  50. // See if webcal_user.cal_passwd will allow 32 characters
  51. $sql = "DESC webcal_user";
  52. $res = dbi_execute ( $sql );
  53. while ( $row = dbi_fetch_row ( $res ) ) {
  54. if ($row[Field] == 'cal_passwd') {
  55. preg_match ( "/([0-9]+)/", $row[Type], $match );
  56. if ($match[1] < 32) {
  57. $sql = "ALTER TABLE webcal_user MODIFY cal_passwd VARCHAR(32) NULL";
  58. // Use the following on older MySQL versions
  59. //$sql = "ALTER TABLE webcal_user CHANGE cal_passwd cal_passwd VARCHAR(32) NULL";
  60. $res = dbi_execute ( $sql );
  61. if ($res) {
  62. echo "Table webcal_user altered to allow 32 character passwords.\n" .
  63. "<br />Converting passwords...\n<br /><br />\n";
  64. }
  65. }
  66. }
  67. }
  68. dbi_free_result ( $res );
  69. // Convert the passwords
  70. $sql = "SELECT cal_login, cal_passwd FROM webcal_user";
  71. $res = dbi_execute ( $sql );
  72. if ( $res ) {
  73. while ( $row = dbi_fetch_row ( $res ) ) {
  74. $sql2 = "UPDATE webcal_user SET cal_passwd = ? WHERE cal_login = ?";
  75. $res2 = dbi_execute ( $sql2, array ( md5 ( $row[1] ), $row[0] ) );
  76. if ($res2)
  77. echo "Password updated for: ".$row[0]."<br />\n";
  78. }
  79. dbi_free_result ( $res );
  80. echo "Finished converting passwords\n<br />\n";
  81. echo "<br /><br />\n<h1>DO NOT Run this script again!!!</h1>\n<br />\n";
  82. echo '<h1>Delete this script if it ran successfully!!!</h1>';
  83. }
  84. ?>