From 03341aea9f33784ba790cc5d0bdbce7b88447ab4 Mon Sep 17 00:00:00 2001 From: Alexander Bias Date: Sun, 28 Dec 2025 12:44:10 +0100 Subject: [PATCH] MDL-87559 core: Set $SCRIPT even outside public/ --- public/lib/setuplib.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/public/lib/setuplib.php b/public/lib/setuplib.php index 4dbccff172d..b3220e5bc83 100644 --- a/public/lib/setuplib.php +++ b/public/lib/setuplib.php @@ -764,14 +764,24 @@ function initialise_fullme_cli() { $topfile = realpath($topfile['file']); $dirroot = realpath($CFG->dirroot); - if (strpos($topfile, $dirroot) !== 0) { - // Probably some weird external script - $SCRIPT = $FULLSCRIPT = $FULLME = $ME = null; - } else { + if (strpos($topfile, $dirroot) === 0) { + // Normal case: Script is under dirroot (e.g., public/course/view.php). $relativefile = substr($topfile, strlen($dirroot)); - $relativefile = str_replace('\\', '/', $relativefile); // Win fix + $relativefile = str_replace('\\', '/', $relativefile); // Win fix. $SCRIPT = $FULLSCRIPT = $relativefile; $FULLME = $ME = null; + } else { + // Moodle 5.1+ structure: Admin CLI scripts are in parent directory of dirroot. + $root = dirname($dirroot); + if (strpos($topfile, $root) === 0) { + $relativefile = substr($topfile, strlen($root)); + $relativefile = str_replace('\\', '/', $relativefile); // Win fix. + $SCRIPT = $FULLSCRIPT = $relativefile; + $FULLME = $ME = null; + } else { + // Probably some weird external script. + $SCRIPT = $FULLSCRIPT = $FULLME = $ME = null; + } } }