diff --git a/admin/renderer.php b/admin/renderer.php index da59ef67fcb..c0683360dd2 100644 --- a/admin/renderer.php +++ b/admin/renderer.php @@ -823,8 +823,7 @@ class core_admin_renderer extends plugin_renderer_base { */ protected function registration_warning($registered) { - if (!$registered) { - + if (!$registered && site_is_public()) { if (has_capability('moodle/site:config', context_system::instance())) { $registerbutton = $this->single_button(new moodle_url('/admin/registration/index.php'), get_string('register', 'admin')); diff --git a/lib/classes/hub/registration.php b/lib/classes/hub/registration.php index 823e47f92c5..22196c0106c 100644 --- a/lib/classes/hub/registration.php +++ b/lib/classes/hub/registration.php @@ -512,13 +512,10 @@ class registration { $markasviewed = true; } else { $showregistration = !empty($CFG->registrationpending); - if ($showregistration) { - $host = parse_url($CFG->wwwroot, PHP_URL_HOST); - if ($host === 'localhost' || preg_match('|^127\.\d+\.\d+\.\d+$|', $host)) { - // If it's a localhost, don't redirect to registration, it won't work anyway. - $showregistration = false; - $markasviewed = true; - } + if ($showregistration && !site_is_public()) { + // If it's not a public site, don't redirect to registration, it won't work anyway. + $showregistration = false; + $markasviewed = true; } } if ($markasviewed !== null) { diff --git a/lib/moodlelib.php b/lib/moodlelib.php index abbc48d5363..8f412ae2c49 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -10206,3 +10206,28 @@ function get_callable_name($callable) { return $name; } } + +/** + * Tries to guess if $CFG->wwwroot is publicly accessible or not. + * Never put your faith on this function and rely on its accuracy as there might be false positives. + * It just performs some simple checks, and mainly is used for places where we want to hide some options + * such as site registration when $CFG->wwwroot is not publicly accessible. + * Good thing is there is no false negative. + * + * @return bool + */ +function site_is_public() { + global $CFG; + + $host = parse_url($CFG->wwwroot, PHP_URL_HOST); + + if ($host === 'localhost' || preg_match('|^127\.\d+\.\d+\.\d+$|', $host)) { + $ispublic = false; + } else if (\core\ip_utils::is_ip_address($host) && !ip_is_public($host)) { + $ispublic = false; + } else { + $ispublic = true; + } + + return $ispublic; +}