password-hash.sh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * Drupal hash script - to generate a hash from a plaintext password
  5. *
  6. * Check for your PHP interpreter - on Windows you'll probably have to
  7. * replace line 1 with
  8. * #!c:/program files/php/php.exe
  9. *
  10. * @param password1 [password2 [password3 ...]]
  11. * Plain-text passwords in quotes (or with spaces backslash escaped).
  12. */
  13. if (version_compare(PHP_VERSION, "5.2.0", "<")) {
  14. $version = PHP_VERSION;
  15. echo <<<EOF
  16. ERROR: This script requires at least PHP version 5.2.0. You invoked it with
  17. PHP version {$version}.
  18. \n
  19. EOF;
  20. exit;
  21. }
  22. $script = basename(array_shift($_SERVER['argv']));
  23. if (in_array('--help', $_SERVER['argv']) || empty($_SERVER['argv'])) {
  24. echo <<<EOF
  25. Generate Drupal password hashes from the shell.
  26. Usage: {$script} [OPTIONS] "<plan-text password>"
  27. Example: {$script} "mynewpassword"
  28. All arguments are long options.
  29. --help Print this page.
  30. --root <path>
  31. Set the working directory for the script to the specified path.
  32. To execute this script this has to be the root directory of your
  33. Drupal installation, e.g. /home/www/foo/drupal (assuming Drupal
  34. running on Unix). Use surrounding quotation marks on Windows.
  35. "<password1>" ["<password2>" ["<password3>" ...]]
  36. One or more plan-text passwords enclosed by double quotes. The
  37. output hash may be manually entered into the {users}.pass field to
  38. change a password via SQL to a known value.
  39. To run this script without the --root argument invoke it from the root directory
  40. of your Drupal installation as
  41. ./scripts/{$script}
  42. \n
  43. EOF;
  44. exit;
  45. }
  46. $passwords = array();
  47. // Parse invocation arguments.
  48. while ($param = array_shift($_SERVER['argv'])) {
  49. switch ($param) {
  50. case '--root':
  51. // Change the working directory.
  52. $path = array_shift($_SERVER['argv']);
  53. if (is_dir($path)) {
  54. chdir($path);
  55. }
  56. break;
  57. default:
  58. // Add a password to the list to be processed.
  59. $passwords[] = $param;
  60. break;
  61. }
  62. }
  63. define('DRUPAL_ROOT', getcwd());
  64. include_once DRUPAL_ROOT . '/includes/password.inc';
  65. include_once DRUPAL_ROOT . '/includes/bootstrap.inc';
  66. foreach ($passwords as $password) {
  67. print("\npassword: $password \t\thash: ". user_hash_password($password) ."\n");
  68. }
  69. print("\n");