1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Extdb user sync script.
19 *
20 * This script is meant to be called from a system cronjob to
21 * sync moodle user accounts with external database.
22 * It is required when using internal passwords (== passwords not defined in external database).
23 *
24 * Sample cron entry:
25 * # 5 minutes past 4am
26 * 5 4 * * * sudo -u www-data /usr/bin/php /var/www/moodle/auth/db/cli/sync_users.php
27 *
28 * Notes:
29 *   - it is required to use the web server account when executing PHP CLI scripts
30 *   - you need to change the "www-data" to match the apache user account
31 *   - use "su" if "sudo" not available
32 *   - If you have a large number of users, you may want to raise the memory limits
33 *     by passing -d memory_limit=256M
34 *   - For debugging & better logging, you are encouraged to use in the command line:
35 *     -d log_errors=1 -d error_reporting=E_ALL -d display_errors=0 -d html_errors=0
36 *
37 * Performance notes:
38 * + The code is simpler, but not as optimized as its LDAP counterpart.
39 *
40 * @package    auth_db
41 * @copyright  2006 Martin Langhoff
42 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 */
44
45define('CLI_SCRIPT', true);
46
47require(__DIR__.'/../../../config.php');
48require_once("$CFG->libdir/clilib.php");
49
50// Now get cli options.
51list($options, $unrecognized) = cli_get_params(array('noupdate'=>false, 'verbose'=>false, 'help'=>false), array('n'=>'noupdate', 'v'=>'verbose', 'h'=>'help'));
52
53if ($unrecognized) {
54    $unrecognized = implode("\n  ", $unrecognized);
55    cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
56}
57
58if ($options['help']) {
59    $help =
60"Execute user account sync with external database.
61The auth_db plugin must be enabled and properly configured.
62
63Options:
64-n, --noupdate        Skip update of existing users
65-v, --verbose         Print verbose progress information
66-h, --help            Print out this help
67
68Example:
69\$ sudo -u www-data /usr/bin/php auth/db/cli/sync_users.php
70
71Sample cron entry:
72# 5 minutes past 4am
735 4 * * * sudo -u www-data /usr/bin/php /var/www/moodle/auth/db/cli/sync_users.php
74";
75
76    echo $help;
77    die;
78}
79
80if (!is_enabled_auth('db')) {
81    cli_error('auth_db plugin is disabled, synchronisation stopped', 2);
82}
83
84cli_problem('[AUTH DB] The sync users cron has been deprecated. Please use the scheduled task instead.');
85
86// Abort execution of the CLI script if the \auth_db\task\sync_users is enabled.
87$task = \core\task\manager::get_scheduled_task('auth_db\task\sync_users');
88if (!$task->get_disabled()) {
89    cli_error('[AUTH DB] The scheduled task sync_users is enabled, the cron execution has been aborted.');
90}
91
92if (empty($options['verbose'])) {
93    $trace = new null_progress_trace();
94} else {
95    $trace = new text_progress_trace();
96}
97
98$update = empty($options['noupdate']);
99
100/** @var auth_plugin_db $dbauth */
101$dbauth = get_auth_plugin('db');
102$result = $dbauth->sync_users($trace, $update);
103
104exit($result);
105