|
| 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 | + * Form for scheduled tasks admin pages. |
| 19 | + * |
| 20 | + * @package tool_task |
| 21 | + * @copyright 2018 Toni Barbera <toni@moodle.com> |
| 22 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 23 | + */ |
| 24 | + |
| 25 | +namespace tool_task; |
| 26 | + |
| 27 | +defined('MOODLE_INTERNAL') || die(); |
| 28 | + |
| 29 | +/** |
| 30 | + * Running tasks from CLI. |
| 31 | + * |
| 32 | + * @copyright 2018 Toni Barbera <toni@moodle.com> |
| 33 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 34 | + */ |
| 35 | +class run_from_cli { |
| 36 | + |
| 37 | + /** |
| 38 | + * Find the path of PHP CLI binary. |
| 39 | + * |
| 40 | + * @return string|false The PHP CLI executable PATH |
| 41 | + */ |
| 42 | + protected static function find_php_cli_path() { |
| 43 | + global $CFG; |
| 44 | + |
| 45 | + if (!empty($CFG->pathtophp) && is_executable(trim($CFG->pathtophp))) { |
| 46 | + return $CFG->pathtophp; |
| 47 | + } |
| 48 | + |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Returns if Moodle have access to PHP CLI binary or not. |
| 54 | + * |
| 55 | + * @return bool |
| 56 | + */ |
| 57 | + public static function is_runnable():bool { |
| 58 | + return self::find_php_cli_path() !== false; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Executes a cron from web invocation using PHP CLI. |
| 63 | + * |
| 64 | + * @param \core\task\task_base $task Task that be executed via CLI. |
| 65 | + * @return bool |
| 66 | + * @throws \moodle_exception |
| 67 | + */ |
| 68 | + public static function execute(\core\task\task_base $task):bool { |
| 69 | + global $CFG; |
| 70 | + |
| 71 | + if (!self::is_runnable()) { |
| 72 | + $redirecturl = new \moodle_url('/admin/settings.php', ['section' => 'systempaths']); |
| 73 | + throw new \moodle_exception('cannotfindthepathtothecli', 'tool_task', $redirecturl->out()); |
| 74 | + } else { |
| 75 | + // Shell-escaped path to the PHP binary. |
| 76 | + $phpbinary = escapeshellarg(self::find_php_cli_path()); |
| 77 | + |
| 78 | + // Shell-escaped path CLI script. |
| 79 | + $pathcomponents = [$CFG->dirroot, $CFG->admin, 'tool', 'task', 'cli', 'schedule_task.php']; |
| 80 | + $scriptpath = escapeshellarg(implode(DIRECTORY_SEPARATOR, $pathcomponents)); |
| 81 | + |
| 82 | + // Shell-escaped task name. |
| 83 | + $classname = get_class($task); |
| 84 | + $taskarg = escapeshellarg("--execute={$classname}"); |
| 85 | + |
| 86 | + // Build the CLI command. |
| 87 | + $command = "{$phpbinary} {$scriptpath} {$taskarg}"; |
| 88 | + |
| 89 | + // Execute it. |
| 90 | + passthru($command); |
| 91 | + } |
| 92 | + |
| 93 | + return true; |
| 94 | + } |
| 95 | +} |
0 commit comments