cron.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. * Web cron
  18. *
  19. * This script looks through all the module directories for cron.php files
  20. * and runs them. These files can contain cleanup functions, email functions
  21. * or anything that needs to be run on a regular basis.
  22. *
  23. * This file is best run from cron on the host system (ie outside PHP).
  24. * It is strongly recommended to add password protection via admin settings.
  25. *
  26. * eg wget -q -O /dev/null 'http: *moodle.somewhere.edu/admin/cron.php?password=SeCreT666'
  27. *
  28. * It is also possible to use CLI script admin/cli/cron.php instead,
  29. * you can not call this script from command line any more.
  30. *
  31. * @package core
  32. * @subpackage admin
  33. * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
  34. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35. */
  36. if (defined('STDIN')) {
  37. fwrite(STDERR, "ERROR: This script no longer supports CLI, please use admin/cli/cron.php instead\n");
  38. exit(1);
  39. }
  40. // This is a fake CLI script, it is a really ugly hack which emulates
  41. // CLI via web interface, please do not use this hack elsewhere
  42. define('CLI_SCRIPT', true);
  43. define('WEB_CRON_EMULATED_CLI', 'defined'); // ugly ugly hack, do not use elsewhere please
  44. define('NO_OUTPUT_BUFFERING', true);
  45. require('../config.php');
  46. require_once($CFG->libdir.'/clilib.php');
  47. require_once($CFG->libdir.'/cronlib.php');
  48. // extra safety
  49. \core\session\manager::write_close();
  50. // check if execution allowed
  51. if (!empty($CFG->cronclionly)) {
  52. // This script can only be run via the cli.
  53. print_error('cronerrorclionly', 'admin');
  54. exit;
  55. }
  56. // This script is being called via the web, so check the password if there is one.
  57. if (!empty($CFG->cronremotepassword)) {
  58. $pass = optional_param('password', '', PARAM_RAW);
  59. if ($pass != $CFG->cronremotepassword) {
  60. // wrong password.
  61. print_error('cronerrorpassword', 'admin');
  62. exit;
  63. }
  64. }
  65. // send mime type and encoding
  66. @header('Content-Type: text/plain; charset=utf-8');
  67. // we do not want html markup in emulated CLI
  68. @ini_set('html_errors', 'off');
  69. // execute the cron
  70. cron_run();