annotate core/scripts/password-hash.sh @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
rev   line source
Chris@0 1 #!/usr/bin/env php
Chris@0 2 <?php
Chris@0 3
Chris@0 4 /**
Chris@0 5 * Drupal hash script - to generate a hash from a plaintext password
Chris@0 6 *
Chris@0 7 * @param password1 [password2 [password3 ...]]
Chris@0 8 * Plain-text passwords in quotes (or with spaces backslash escaped).
Chris@0 9 */
Chris@0 10
Chris@0 11 use Drupal\Core\DrupalKernel;
Chris@0 12 use Symfony\Component\HttpFoundation\Request;
Chris@0 13
Chris@0 14 if (PHP_SAPI !== 'cli') {
Chris@0 15 return;
Chris@0 16 }
Chris@0 17
Chris@0 18 if (version_compare(PHP_VERSION, '5.4.5') < 0) {
Chris@0 19 $version = PHP_VERSION;
Chris@0 20 echo <<<EOF
Chris@0 21
Chris@0 22 ERROR: This script requires at least PHP version 5.4.5. You invoked it with
Chris@0 23 PHP version {$version}.
Chris@0 24 \n
Chris@0 25 EOF;
Chris@0 26 exit;
Chris@0 27 }
Chris@0 28
Chris@0 29 $script = basename(array_shift($_SERVER['argv']));
Chris@0 30
Chris@0 31 if (in_array('--help', $_SERVER['argv']) || empty($_SERVER['argv'])) {
Chris@0 32 echo <<<EOF
Chris@0 33
Chris@0 34 Generate Drupal password hashes from the shell.
Chris@0 35
Chris@0 36 Usage: {$script} [OPTIONS] "<plan-text password>"
Chris@0 37 Example: {$script} "mynewpassword"
Chris@0 38
Chris@0 39 All arguments are long options.
Chris@0 40
Chris@0 41 --help Print this page.
Chris@0 42
Chris@0 43 "<password1>" ["<password2>" ["<password3>" ...]]
Chris@0 44
Chris@0 45 One or more plan-text passwords enclosed by double quotes. The
Chris@0 46 output hash may be manually entered into the
Chris@0 47 {users_field_data}.pass field to change a password via SQL to a
Chris@0 48 known value.
Chris@0 49
Chris@0 50
Chris@0 51 EOF;
Chris@0 52 exit;
Chris@0 53 }
Chris@0 54
Chris@0 55 // Password list to be processed.
Chris@0 56 $passwords = $_SERVER['argv'];
Chris@0 57
Chris@0 58 $autoloader = require __DIR__ . '/../../autoload.php';
Chris@0 59
Chris@0 60 $request = Request::createFromGlobals();
Chris@0 61 $kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod', FALSE);
Chris@0 62 $kernel->boot();
Chris@0 63
Chris@0 64 $password_hasher = $kernel->getContainer()->get('password');
Chris@0 65
Chris@0 66 foreach ($passwords as $password) {
Chris@0 67 print("\npassword: $password \t\thash: ". $password_hasher->hash($password) ."\n");
Chris@0 68 }
Chris@0 69 print("\n");
Chris@0 70