diff --git a/admin/settings/server.php b/admin/settings/server.php index c0d57c3fcd7..f4c24c0f3f0 100644 --- a/admin/settings/server.php +++ b/admin/settings/server.php @@ -7,7 +7,8 @@ if ($hassiteconfig) { // speedup for non-admins, add all caps used on this page // "systempaths" settingpage $temp = new admin_settingpage('systempaths', new lang_string('systempaths','admin')); - +$temp->add(new admin_setting_configexecutable('pathtophp', new lang_string('pathtophp', 'admin'), + new lang_string('configpathtophp', 'admin'), '')); $temp->add(new admin_setting_configexecutable('pathtodu', new lang_string('pathtodu', 'admin'), new lang_string('configpathtodu', 'admin'), '')); $temp->add(new admin_setting_configexecutable('aspellpath', new lang_string('aspellpath', 'admin'), new lang_string('edhelpaspellpath'), '')); $temp->add(new admin_setting_configexecutable('pathtodot', new lang_string('pathtodot', 'admin'), new lang_string('pathtodot_help', 'admin'), '')); diff --git a/admin/tool/task/classes/run_from_cli.php b/admin/tool/task/classes/run_from_cli.php new file mode 100644 index 00000000000..06e24ef9cba --- /dev/null +++ b/admin/tool/task/classes/run_from_cli.php @@ -0,0 +1,95 @@ +. + +/** + * Form for scheduled tasks admin pages. + * + * @package tool_task + * @copyright 2018 Toni Barbera + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace tool_task; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Running tasks from CLI. + * + * @copyright 2018 Toni Barbera + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class run_from_cli { + + /** + * Find the path of PHP CLI binary. + * + * @return string|false The PHP CLI executable PATH + */ + protected static function find_php_cli_path() { + global $CFG; + + if (!empty($CFG->pathtophp) && is_executable(trim($CFG->pathtophp))) { + return $CFG->pathtophp; + } + + return false; + } + + /** + * Returns if Moodle have access to PHP CLI binary or not. + * + * @return bool + */ + public static function is_runnable():bool { + return self::find_php_cli_path() !== false; + } + + /** + * Executes a cron from web invocation using PHP CLI. + * + * @param \core\task\task_base $task Task that be executed via CLI. + * @return bool + * @throws \moodle_exception + */ + public static function execute(\core\task\task_base $task):bool { + global $CFG; + + if (!self::is_runnable()) { + $redirecturl = new \moodle_url('/admin/settings.php', ['section' => 'systempaths']); + throw new \moodle_exception('cannotfindthepathtothecli', 'tool_task', $redirecturl->out()); + } else { + // Shell-escaped path to the PHP binary. + $phpbinary = escapeshellarg(self::find_php_cli_path()); + + // Shell-escaped path CLI script. + $pathcomponents = [$CFG->dirroot, $CFG->admin, 'tool', 'task', 'cli', 'schedule_task.php']; + $scriptpath = escapeshellarg(implode(DIRECTORY_SEPARATOR, $pathcomponents)); + + // Shell-escaped task name. + $classname = get_class($task); + $taskarg = escapeshellarg("--execute={$classname}"); + + // Build the CLI command. + $command = "{$phpbinary} {$scriptpath} {$taskarg}"; + + // Execute it. + passthru($command); + } + + return true; + } +} diff --git a/admin/tool/task/lang/en/tool_task.php b/admin/tool/task/lang/en/tool_task.php index 43c1d9f55fd..2e2e20a6309 100644 --- a/admin/tool/task/lang/en/tool_task.php +++ b/admin/tool/task/lang/en/tool_task.php @@ -25,6 +25,7 @@ $string['asap'] = 'ASAP'; $string['backtoscheduledtasks'] = 'Back to scheduled tasks'; $string['blocking'] = 'Blocking'; +$string['cannotfindthepathtothecli'] = 'Cannot find the path to the PHP CLI executable so task execution aborted. Set the "Path to PHP CLI" setting in "Site administration / Server / System paths"'; $string['clearfaildelay_confirm'] = 'Are you sure you want to clear the fail delay for task \'{$a}\'? After clearing the delay, the task will run according to its normal schedule.'; $string['component'] = 'Component'; $string['corecomponent'] = 'Core'; @@ -58,3 +59,4 @@ $string['taskscheduleminute_help'] = 'Minute field for task schedule. The field $string['taskschedulemonth'] = 'Month'; $string['taskschedulemonth_help'] = 'Month field for task schedule. The field uses the same format as unix cron. Some examples are:
'; $string['privacy:metadata'] = 'The Scheduled task configuration plugin does not store any personal data.'; + diff --git a/admin/tool/task/renderer.php b/admin/tool/task/renderer.php index d612355dc9b..88dbdd4971d 100644 --- a/admin/tool/task/renderer.php +++ b/admin/tool/task/renderer.php @@ -62,6 +62,7 @@ class tool_task_renderer extends plugin_renderer_base { $asap = get_string('asap', 'tool_task'); $disabledstr = get_string('taskdisabled', 'tool_task'); $plugindisabledstr = get_string('plugindisabled', 'tool_task'); + $runnabletasks = tool_task\run_from_cli::is_runnable(); foreach ($tasks as $task) { $customised = $task->is_customised() ? $no : $yes; if (empty($CFG->preventscheduledtaskchanges)) { @@ -105,7 +106,7 @@ class tool_task_renderer extends plugin_renderer_base { } $runnow = ''; - if (!$disabled && get_config('tool_task', 'enablerunnow')) { + if ( ! $disabled && get_config('tool_task', 'enablerunnow') && $runnabletasks ) { $runnow = html_writer::div(html_writer::link( new moodle_url('/admin/tool/task/schedule_task.php', array('task' => get_class($task))), diff --git a/admin/tool/task/schedule_task.php b/admin/tool/task/schedule_task.php index afb20b6d269..13cf1cef1d2 100644 --- a/admin/tool/task/schedule_task.php +++ b/admin/tool/task/schedule_task.php @@ -88,7 +88,8 @@ echo html_writer::start_tag('pre'); $CFG->mtrace_wrapper = 'tool_task_mtrace_wrapper'; // Run the specified task (this will output an error if it doesn't exist). -cron_run_single_task($task); +\tool_task\run_from_cli::execute($task); + echo html_writer::end_tag('pre'); $output = $PAGE->get_renderer('tool_task'); diff --git a/config-dist.php b/config-dist.php index f2437cb63d1..28976f7fda1 100644 --- a/config-dist.php +++ b/config-dist.php @@ -867,6 +867,11 @@ $CFG->admin = 'admin'; // and 'gsdll32.dll' to a new folder without a space in the path) // $CFG->pathtogs = '/usr/bin/gs'; // +// Path to PHP CLI. +// Probably something like /usr/bin/php. If you enter this, cron scripts can be +// executed from admin web interface. +// $CFG->pathtophp = ''; +// // Path to du. // Probably something like /usr/bin/du. If you enter this, pages that display // directory contents will run much faster for directories with a lot of files. diff --git a/lang/en/admin.php b/lang/en/admin.php index ad1b1a9a0f0..df98ddd00ac 100644 --- a/lang/en/admin.php +++ b/lang/en/admin.php @@ -299,6 +299,7 @@ $string['configoverride'] = 'Defined in config.php'; $string['configpasswordpolicy'] = 'Turning this on will make Moodle check user passwords against a valid password policy. Use the settings below to specify your policy (they will be ignored if you set this to \'No\').'; $string['configpasswordresettime'] = 'This specifies the amount of time people have to validate a password reset request before it expires. Usually 30 minutes is a good value.'; $string['configpathtodu'] = 'Path to du. Probably something like /usr/bin/du. If you enter this, pages that display directory contents will run much faster for directories with a lot of files.'; +$string['configpathtophp'] = 'Path to PHP CLI. Probably something like /usr/bin/php. If you enter this, cron scripts can be executed from admin web interface.'; $string['configperfdebug'] = 'If you turn this on, performance info will be printed in the footer of the standard theme'; $string['configprofileroles'] = 'List of roles that are visible on user profiles and participation page.'; $string['configprofilesforenrolledusersonly'] = 'To prevent misuse by spammers, profile descriptions of users who are not yet enrolled in any course are hidden. New users must enrol in at least one course before they can add a profile description.'; @@ -843,6 +844,7 @@ $string['passwordreuselimit'] = 'Password rotation limit'; $string['passwordreuselimit_desc'] = 'Number of times a user must change their password before they are allowed to reuse a password. Hashes of previously used passwords are stored in local database table. This feature might not be compatible with some external authentication plugins.'; $string['pathtodot'] = 'Path to dot'; $string['pathtodot_help'] = 'Path to dot. On Linux it is something like /usr/bin/dot. On Windows it is something like C:\Program Files (x86)\Graphviz2.38\bin\dot.exe. On Mac it is something like /opt/local/bin/dot. To be able to generate graphics from DOT files, you must have installed the dot executable and point to it here.'; +$string['pathtophp'] = 'Path to PHP CLI'; $string['pathtodu'] = 'Path to du'; $string['pathtogs'] = 'Path to ghostscript'; $string['pathtogs_help'] = 'On most Linux installs, this can be left as \'/usr/bin/gs\'. On Windows it will be something like \'c:\\gs\\bin\\gswin32c.exe\' (make sure there are no spaces in the path - if necessary copy the files \'gswin32c.exe\' and \'gsdll32.dll\' to a new folder without a space in the path)'; diff --git a/lib/behat/lib.php b/lib/behat/lib.php index 8f6fb064b3d..af03103ad25 100644 --- a/lib/behat/lib.php +++ b/lib/behat/lib.php @@ -219,7 +219,7 @@ function behat_clean_init_config() { 'wwwroot', 'dataroot', 'dirroot', 'admin', 'directorypermissions', 'filepermissions', 'umaskpermissions', 'dbtype', 'dblibrary', 'dbhost', 'dbname', 'dbuser', 'dbpass', 'prefix', 'dboptions', 'proxyhost', 'proxyport', 'proxytype', 'proxyuser', 'proxypassword', - 'proxybypass', 'theme', 'pathtogs', 'pathtodu', 'aspellpath', 'pathtodot', 'skiplangupgrade', + 'proxybypass', 'theme', 'pathtogs', 'pathtophp', 'pathtodu', 'aspellpath', 'pathtodot', 'skiplangupgrade', 'altcacheconfigpath', 'pathtounoconv', 'alternative_file_system_class', 'pathtopython' )); diff --git a/lib/phpunit/bootstrap.php b/lib/phpunit/bootstrap.php index 3e88f506f83..1f4ba0a83c7 100644 --- a/lib/phpunit/bootstrap.php +++ b/lib/phpunit/bootstrap.php @@ -181,7 +181,7 @@ $CFG->dboptions = isset($CFG->phpunit_dboptions) ? $CFG->phpunit_dboptions : $CF $allowed = array('wwwroot', 'dataroot', 'dirroot', 'admin', 'directorypermissions', 'filepermissions', 'dbtype', 'dblibrary', 'dbhost', 'dbname', 'dbuser', 'dbpass', 'prefix', 'dboptions', 'proxyhost', 'proxyport', 'proxytype', 'proxyuser', 'proxypassword', 'proxybypass', // keep proxy settings from config.php - 'altcacheconfigpath', 'pathtogs', 'pathtodu', 'aspellpath', 'pathtodot', + 'altcacheconfigpath', 'pathtogs', 'pathtphp', 'pathtodu', 'aspellpath', 'pathtodot', 'pathtounoconv', 'alternative_file_system_class', 'pathtopython' ); $productioncfg = (array)$CFG;