2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
20 * This script looks through all the module directories for cron.php files
21 * and runs them. These files can contain cleanup functions, email functions
22 * or anything that needs to be run on a regular basis.
24 * This file is best run from cron on the host system (ie outside PHP).
25 * It is strongly recommended to add password protection via admin settings.
27 * eg wget -q -O /dev/null 'http: *moodle.somewhere.edu/admin/cron.php?password=SeCreT666'
29 * It is also possible to use CLI script admin/cli/cron.php instead,
30 * you can not call this script from command line any more.
34 * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 // phpcs:ignoreFile moodle.Files.MoodleInternal.MoodleInternalGlobalState
40 if (defined('STDIN')) {
41 fwrite(STDERR
, "ERROR: This script no longer supports CLI, please use admin/cli/cron.php instead\n");
45 // This is a fake CLI script, it is a really ugly hack which emulates
46 // CLI via web interface, please do not use this hack elsewhere
47 define('CLI_SCRIPT', true);
48 define('WEB_CRON_EMULATED_CLI', 'defined'); // ugly ugly hack, do not use elsewhere please
49 define('NO_OUTPUT_BUFFERING', true);
51 require('../config.php');
52 require_once($CFG->libdir
. '/clilib.php');
55 \core\session\manager
::write_close();
57 // check if execution allowed
58 if (!empty($CFG->cronclionly
)) {
59 // This script can only be run via the cli.
60 throw new \
moodle_exception('cronerrorclionly', 'admin');
63 // This script is being called via the web, so check the password if there is one.
64 if (!empty($CFG->cronremotepassword
)) {
65 $pass = optional_param('password', '', PARAM_RAW
);
66 if ($pass != $CFG->cronremotepassword
) {
68 throw new \
moodle_exception('cronerrorpassword', 'admin');
73 // Send mime type and encoding.
74 @header
('Content-Type: text/plain; charset=utf-8');
76 // We do not want html markup in emulated CLI.
77 @ini_set
('html_errors', 'off');
79 // Execute the cron, disabling keepalive.
80 \core\cron
::run_main_process(0);