From 585b0604622fa3be71a0a106c59fca2176273bad Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Thu, 13 Jun 2024 00:33:03 +0800 Subject: [PATCH] MDL-82183 core: Do not load subsystem db/legacyclasses.php Subsystems do not support the notion of a `db` folder. Instead we must conflate the lib/db/legacyclasses.php syntax to allow it to support paths as either a string, or an array with [subsystem, path]. --- lib/classes/component.php | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/lib/classes/component.php b/lib/classes/component.php index e7f3eb4b324..26b83933d71 100644 --- a/lib/classes/component.php +++ b/lib/classes/component.php @@ -727,14 +727,13 @@ $cache = ' . var_export($cache, true) . '; self::$classmap = []; self::load_classes('core', "$CFG->dirroot/lib/classes"); - self::load_legacy_classes($CFG->libdir); + self::load_legacy_classes($CFG->libdir, true); foreach (self::$subsystems as $subsystem => $fulldir) { if (!$fulldir) { continue; } self::load_classes('core_' . $subsystem, "$fulldir/classes"); - self::load_legacy_classes($fulldir); } foreach (self::$plugins as $plugintype => $plugins) { @@ -1448,8 +1447,12 @@ $cache = ' . var_export($cache, true) . '; * and the value is the path to the class file within the relative ../classes/ directory. * * @param string|null $fulldir The directory to the legacy classes. + * @param bool $allowsubsystems Whether to allow the specification of alternative subsystems for this path. */ - protected static function load_legacy_classes(?string $fulldir): void { + protected static function load_legacy_classes( + ?string $fulldir, + bool $allowsubsystems = false, + ): void { if (is_null($fulldir)) { return; } @@ -1460,7 +1463,32 @@ $cache = ' . var_export($cache, true) . '; require($file); if (is_array($legacyclasses)) { foreach ($legacyclasses as $classname => $path) { - self::$classmap[$classname] = "{$fulldir}/classes/{$path}"; + if (is_array($path)) { + if (!$allowsubsystems) { + throw new \Exception( + "Invalid legacy classes path entry for {$classname}. " . + "Only files within the component can be specified.", + ); + } + if (count($path) !== 2) { + throw new \Exception( + "Invalid legacy classes path entry for {$classname}. " . + "Entries must be in the format [subsystem, path].", + ); + } + [$subsystem, $path] = $path; + $subsystem = substr($subsystem, 5); + if (!array_key_exists($subsystem, self::$subsystems)) { + throw new \Exception( + "Unknown subsystem '{$subsystem}' for legacy classes entry of '{$classname}'", + ); + } + + $subsystemfulldir = self::$subsystems[$subsystem]; + self::$classmap[$classname] = "{$subsystemfulldir}/classes/{$path}"; + } else { + self::$classmap[$classname] = "{$fulldir}/classes/{$path}"; + } } } }