diff --git a/classes/local/outagelib.php b/classes/local/outagelib.php index 27670f9..cf591c7 100644 --- a/classes/local/outagelib.php +++ b/classes/local/outagelib.php @@ -44,15 +44,15 @@ defined('MOODLE_INTERNAL') || die(); */ class outagelib { /** - * @var bool Flags in the page was already injected with the warning bar if needed. + * @var bool Flags in the injection function was already called. */ - private static $injected = false; + private static $injectcalled = false; /** * Calls inject even if it was already called before. */ public static function reinject() { - self::$injected = false; + self::$injectcalled = false; self::inject(); } @@ -62,15 +62,14 @@ class outagelib { public static function inject() { global $CFG; - // Many hooks can call it, execute only once. - if (self::$injected) { - return; - } - self::$injected = true; - // Ensure we do not kill the whole website in case of an error. try { - // Ensure no exceptions break the code. + // Check if we should inject the code. + if (!self::injection_allowed()) { + return; + } + + // Used to test the try block in case of errors. if (PHPUNIT_TEST && optional_param('auth_outage_break_code', false, PARAM_INT)) { (new stdClass())->invalidfield; } @@ -168,4 +167,26 @@ class outagelib { set_config('maintenance_later', $next->starttime); } } + + /** + * Checks if we should try to inject an warning bar. + * @return bool + */ + private static function injection_allowed() { + global $CFG; + + // Injection should only be called once, if called more times by other hooks ignore it. + if (self::$injectcalled) { + return false; + } + self::$injectcalled = true; + + // Do not inject into admin/settings.php, see Issue #65. + if ($_SERVER['SCRIPT_FILENAME'] === $CFG->dirroot.'/admin/settings.php') { + return false; + } + + // Nothing preventing the injection. + return true; + } } diff --git a/tests/phpunit/local/outagelib_test.php b/tests/phpunit/local/outagelib_test.php index c6b0a37..304a0e2 100644 --- a/tests/phpunit/local/outagelib_test.php +++ b/tests/phpunit/local/outagelib_test.php @@ -97,7 +97,7 @@ class outagelib_test extends advanced_testcase { self::assertContains('