weekly release 3.5.8+
[moodle.git] / admin / cli / reset_password.php
blob9d4dc14bf687c7dc34ed199f8f879fd35e8f9f4c
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * This script allows you to reset any local user password.
21 * @package core
22 * @subpackage cli
23 * @copyright 2009 Petr Skoda (http://skodak.org)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 define('CLI_SCRIPT', true);
29 require(__DIR__.'/../../config.php');
30 require_once($CFG->libdir.'/clilib.php'); // cli only functions
32 // Define the input options.
33 $longparams = array(
34 'help' => false,
35 'username' => '',
36 'password' => '',
37 'ignore-password-policy' => false
40 $shortparams = array(
41 'h' => 'help',
42 'u' => 'username',
43 'p' => 'password',
44 'i' => 'ignore-password-policy'
47 // now get cli options
48 list($options, $unrecognized) = cli_get_params($longparams, $shortparams);
50 if ($unrecognized) {
51 $unrecognized = implode("\n ", $unrecognized);
52 cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
55 if ($options['help']) {
56 $help =
57 "Reset local user passwords, useful especially for admin acounts.
59 There are no security checks here because anybody who is able to
60 execute this file may execute any PHP too.
62 Options:
63 -h, --help Print out this help
64 -u, --username=username Specify username to change
65 -p, --password=newpassword Specify new password
66 --ignore-password-policy Ignore password policy when setting password
68 Example:
69 \$sudo -u www-data /usr/bin/php admin/cli/reset_password.php
70 \$sudo -u www-data /usr/bin/php admin/cli/reset_password.php --username=rosaura --password=jiu3jiu --ignore-password-policy
73 echo $help;
74 die;
76 if ($options['username'] == '' ) {
77 cli_heading('Password reset');
78 $prompt = "Enter username (manual authentication only)";
79 $username = cli_input($prompt);
80 } else {
81 $username = $options['username'];
84 if (!$user = $DB->get_record('user', array('auth'=>'manual', 'username'=>$username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
85 cli_error("Can not find user '$username'");
88 if ($options['password'] == '' ) {
89 $prompt = "Enter new password";
90 $password = cli_input($prompt);
91 } else {
92 $password = $options['password'];
95 $errmsg = '';//prevent eclipse warning
96 if (!$options['ignore-password-policy'] ) {
97 if (!check_password_policy($password, $errmsg)) {
98 cli_error(html_to_text($errmsg, 0));
102 $hashedpassword = hash_internal_user_password($password);
104 $DB->set_field('user', 'password', $hashedpassword, array('id'=>$user->id));
106 echo "Password changed\n";
108 exit(0); // 0 means success.