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