Merge pull request #388 from catalyst/issue-387

Issue 387: Add metadata header to outage pages
This commit is contained in:
Matthew Hilton
2026-01-19 12:47:02 +10:00
committed by GitHub
11 changed files with 77 additions and 7 deletions

View File

@@ -90,6 +90,10 @@ class edit extends moodleform {
$mform->disabledIf('accesskey', 'useaccesskey');
$mform->addHelpButton('accesskey', 'accesskey', 'auth_outage');
$mform->addElement('text', 'metadata', get_string('metadata', 'auth_outage'));
$mform->setType('metadata', PARAM_TEXT);
$mform->addHelpButton('metadata', 'metadata', 'auth_outage');
$this->add_action_buttons();
}
@@ -144,6 +148,7 @@ class edit extends moodleform {
'title' => $data->title,
'description' => $data->description['text'],
'accesskey' => $data->useaccesskey ? $data->accesskey : null,
'metadata' => $data->metadata ?? null,
];
return new outage($outagedata);
}
@@ -169,6 +174,7 @@ class edit extends moodleform {
'description' => ['text' => $outage->description, 'format' => '1'],
'accesskey' => $outage->accesskey,
'useaccesskey' => !empty($outage->accesskey),
'metadata' => $outage->metadata,
]);
// If the default_autostart is configured in config, then force autostart to be the default value.

View File

@@ -109,6 +109,13 @@ class infopage {
// No hooks injecting into this page, do it manually.
echo outagelib::get_inject_code();
// Inject metadata into the header before output.
if (!empty($this->outage->metadata)) {
header('X-Outage-Metadata: ' . $this->outage->metadata);
header('X-Outage-StartTime: ' . $this->outage->starttime);
header('X-Outage-EndTime: ' . $this->outage->stoptime);
}
echo $OUTPUT->header();
$viewbag = [
'admin' => is_siteadmin(),

View File

@@ -49,6 +49,12 @@ class maintenance_static_page {
} else if (PHPUNIT_TEST || defined('BEHAT_SITE_RUNNING')) {
$html = '<html></html>';
} else {
// Inject metadata into the header before output.
if (!empty($outage->metadata)) {
header('X-Outage-Metadata: ' . $outage->metadata);
header('X-Outage-StartTime: ' . $outage->starttime);
header('X-Outage-EndTime: ' . $outage->stoptime);
}
$data = maintenance_static_page_io::file_get_data(
$CFG->wwwroot . '/auth/outage/info.php?auth_outage_hide_warning=1&static=1&id=' . $outage->id
);

View File

@@ -113,6 +113,11 @@ class outage {
*/
public $accesskey = null;
/**
* @var string|null metadata string, or null if not enabled.
*/
public $metadata = null;
/**
* outage constructor.
* @param stdClass|array|null $data The data for the outage.

View File

@@ -283,7 +283,7 @@ class outagelib {
* @return string
* @throws invalid_parameter_exception
*/
public static function create_climaintenancephp_code($starttime, $stoptime, $allowedips, $accesskey = null) {
public static function create_climaintenancephp_code($starttime, $stoptime, $allowedips, $accesskey = null, $metadata = null) {
global $CFG;
if (!is_int($starttime) || !is_int($stoptime)) {
throw new invalid_parameter_exception('Make sure $startime and $stoptime are integers.');
@@ -337,6 +337,11 @@ if ((time() >= {{STARTTIME}}) && (time() < {{STOPTIME}})) {
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Accept-Ranges: none');
header('X-Moodle-Maintenance: manager');
if (!empty({{METADATA}})) {
header('X-Outage-Metadata: ' . {{METADATA}});
}
header('X-Outage-StartTime: ' . '{{STARTTIME}}');
header('X-Outage-EndTime: ' . '{{STOPTIME}}');
}
if (!$isphpunit && ((defined('AJAX_SCRIPT') && AJAX_SCRIPT) || (defined('WS_SERVER') && WS_SERVER))) {
@@ -363,10 +368,10 @@ if ((time() >= {{STARTTIME}}) && (time() < {{STOPTIME}})) {
}
EOT;
$search = ['{{STARTTIME}}', '{{STOPTIME}}', '{{USEALLOWEDIPS}}', '{{ALLOWEDIPS}}', '{{USEACCESSKEY}}', '{{ACCESSKEY}}',
'{{YOURIP}}', '{{COOKIESECURE}}', '{{COOKIEHTTPONLY}}'];
'{{YOURIP}}', '{{COOKIESECURE}}', '{{COOKIEHTTPONLY}}', '{{METADATA}}'];
// Note that var_export is required because (string) false == '', not 'false'.
$replace = [$starttime, $stoptime, var_export(!empty($allowedips), true), $allowedips, var_export(!empty($accesskey), true),
$accesskey, getremoteaddr('n/a'), var_export($cookiesecure, true), var_export($cookiehttponly, true)];
$accesskey, getremoteaddr('n/a'), var_export($cookiesecure, true), var_export($cookiehttponly, true), var_export($metadata, true)];
return str_replace($search, $replace, $code);
}
@@ -389,6 +394,7 @@ EOT;
$config = self::get_config();
$allowedips = trim($config->allowedips);
$accesskey = $outage->accesskey ?? null;
$metadata = $outage->metadata ?? null;
// If no outage, or allowed ips is null and access key is null (i.e. no blocking required).
if (is_null($outage) || ($allowedips == '' && empty($accesskey))) {
@@ -396,7 +402,7 @@ EOT;
unlink($file);
}
} else {
$code = self::create_climaintenancephp_code($outage->starttime, $outage->stoptime, $allowedips, $accesskey);
$code = self::create_climaintenancephp_code($outage->starttime, $outage->stoptime, $allowedips, $accesskey, $metadata);
$dir = dirname($file);
if (!file_exists($dir) || !is_dir($dir)) {