mirror of
https://github.com/catalyst/moodle-auth_outage.git
synced 2026-05-17 05:48:43 +02:00
Applying moodle coding standards.
This commit is contained in:
@@ -29,6 +29,9 @@
|
||||
* @var stdClass $CFG
|
||||
*/
|
||||
|
||||
// This call is required by Moodle, but this script should have been called by config.php anyway.
|
||||
require_once(__DIR__.'/../../config.php');
|
||||
|
||||
// This file does nothing if running from CLI.
|
||||
if (defined('CLI_SCRIPT') && CLI_SCRIPT) {
|
||||
return;
|
||||
@@ -40,8 +43,8 @@ if (!isset($CFG->dataroot)) {
|
||||
}
|
||||
|
||||
// 1) Check and run the hook.
|
||||
if (isset($auth_outage_bootstrap_callback) && is_callable($auth_outage_bootstrap_callback)) {
|
||||
$auth_outage_bootstrap_callback();
|
||||
if (is_callable('auth_outage_bootstrap_callback')) {
|
||||
call_user_func('auth_outage_bootstrap_callback');
|
||||
}
|
||||
|
||||
// 2) Check for allowed IPs during outages.
|
||||
|
||||
@@ -88,6 +88,7 @@ class infopage {
|
||||
/**
|
||||
* Generates and outputs the HTML for the info page.
|
||||
* @uses redirect
|
||||
* @SuppressWarnings(PHPMD.UnusedLocalVariable) $viewbag is used inside 'require'.
|
||||
*/
|
||||
public function output() {
|
||||
global $PAGE, $CFG, $OUTPUT;
|
||||
@@ -96,11 +97,6 @@ class infopage {
|
||||
redirect(new moodle_url('/'));
|
||||
}
|
||||
|
||||
$viewbag = [
|
||||
'admin' => is_siteadmin(),
|
||||
'outage' => $this->outage,
|
||||
];
|
||||
|
||||
$PAGE->set_context(context_system::instance());
|
||||
$PAGE->set_title($this->outage->get_title());
|
||||
$PAGE->set_heading($this->outage->get_title());
|
||||
@@ -110,6 +106,10 @@ class infopage {
|
||||
outagelib::inject();
|
||||
|
||||
echo $OUTPUT->header();
|
||||
$viewbag = [
|
||||
'admin' => is_siteadmin(),
|
||||
'outage' => $this->outage,
|
||||
];
|
||||
require($CFG->dirroot.'/auth/outage/views/info/content.php');
|
||||
|
||||
// Moodle 2.7 did not check for CLI mode, which was fixed later.
|
||||
|
||||
@@ -58,7 +58,8 @@ class maintenance_static_page {
|
||||
} else if (PHPUNIT_TEST) {
|
||||
$html = '<html></html>';
|
||||
} else {
|
||||
$data = maintenance_static_page_io::file_get_data($CFG->wwwroot.'/auth/outage/info.php?auth_outage_hide_warning=1&id='.$outage->id);
|
||||
$data = maintenance_static_page_io::file_get_data(
|
||||
$CFG->wwwroot.'/auth/outage/info.php?auth_outage_hide_warning=1&id='.$outage->id);
|
||||
$html = $data['contents'];
|
||||
}
|
||||
|
||||
|
||||
@@ -116,7 +116,6 @@ class maintenance_static_page_generator {
|
||||
private function update_link_stylesheet() {
|
||||
$links = $this->dom->getElementsByTagName('link');
|
||||
|
||||
/** @var DOMElement $link */
|
||||
foreach ($links as $link) {
|
||||
$rel = $link->getAttribute("rel");
|
||||
$href = $link->getAttribute("href");
|
||||
@@ -137,6 +136,7 @@ class maintenance_static_page_generator {
|
||||
/**
|
||||
* Checks for urls inside filename.
|
||||
* @param string $filename
|
||||
* @param string $baseref
|
||||
*/
|
||||
private function update_link_stylesheet_parse($filename, $baseref) {
|
||||
global $CFG;
|
||||
@@ -145,20 +145,20 @@ class maintenance_static_page_generator {
|
||||
if (!preg_match_all('#url\([\'"]?([^\'"\)]+)#', $contents, $matches)) {
|
||||
return;
|
||||
}
|
||||
foreach ($matches[1] as $original_url) {
|
||||
foreach ($matches[1] as $originalurl) {
|
||||
// Allow incomplete URLs in CSS, assume it is from moodle root.
|
||||
if (maintenance_static_page_io::is_url($original_url)) {
|
||||
$full_url = $original_url;
|
||||
} else if ($original_url[0] == '/') {
|
||||
$full_url = $CFG->wwwroot.$original_url;
|
||||
if (maintenance_static_page_io::is_url($originalurl)) {
|
||||
$fullurl = $originalurl;
|
||||
} else if ($originalurl[0] == '/') {
|
||||
$fullurl = $CFG->wwwroot.$originalurl;
|
||||
} else {
|
||||
$full_url = $baseref.'/'.$original_url;
|
||||
$fullurl = $baseref.'/'.$originalurl;
|
||||
}
|
||||
|
||||
$saved = $this->io->save_url_file($full_url);
|
||||
$saved = $this->io->save_url_file($fullurl);
|
||||
if (!is_null($saved)) {
|
||||
$final_url = $this->io->get_url_for_file($saved['url']);
|
||||
$contents = str_replace($original_url, $final_url, $contents);
|
||||
$finalurl = $this->io->get_url_for_file($saved['url']);
|
||||
$contents = str_replace($originalurl, $finalurl, $contents);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -171,7 +171,6 @@ class maintenance_static_page_generator {
|
||||
private function update_link_favicon() {
|
||||
$links = $this->dom->getElementsByTagName('link');
|
||||
|
||||
/** @var DOMElement $link */
|
||||
foreach ($links as $link) {
|
||||
$rel = $link->getAttribute("rel");
|
||||
$href = $link->getAttribute("href");
|
||||
@@ -188,7 +187,6 @@ class maintenance_static_page_generator {
|
||||
private function update_images() {
|
||||
$links = $this->dom->getElementsByTagName('img');
|
||||
|
||||
/** @var DOMElement $link */
|
||||
foreach ($links as $link) {
|
||||
$src = $link->getAttribute("src");
|
||||
if ($src == '') {
|
||||
|
||||
10
file.php
10
file.php
@@ -27,6 +27,10 @@
|
||||
* @var stdClass $CFG
|
||||
*/
|
||||
|
||||
// This file does not use Moodle initialization as a requirement. Supress Warning.
|
||||
define('MOODLE_INTERNAL', true);
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
// File should have at least 3 characters as we will check the extension below.
|
||||
if (!isset($_GET['file'])) {
|
||||
http_response_code(400);
|
||||
@@ -50,7 +54,7 @@ header('Pragma: ');
|
||||
header('Cache-Control: public, max-age='.$lifetime);
|
||||
header('Accept-Ranges: none');
|
||||
|
||||
$auth_outage_bootstrap_callback = function () {
|
||||
function auth_outage_bootstrap_callback() {
|
||||
global $CFG;
|
||||
|
||||
// We are not using any external libraries or references in this file (cli maintenance is active).
|
||||
@@ -68,12 +72,12 @@ $auth_outage_bootstrap_callback = function () {
|
||||
}
|
||||
|
||||
readfile($file);
|
||||
exit(0);
|
||||
die();
|
||||
};
|
||||
|
||||
require_once(__DIR__.'/../../config.php');
|
||||
|
||||
// We should never reach here if config.php and auth/outage/bootstrap.php intercepted it correctly.
|
||||
// If config.php did not execute the callback function we can use the debugging function here.
|
||||
debugging('Your config.php is not properly configured for auth/outage plugin. '.
|
||||
'Please check the plugin settings for information.');
|
||||
exit(1);
|
||||
@@ -45,7 +45,8 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
|
||||
$page = maintenance_static_page::create_from_html('<html></html>');
|
||||
self::assertSame($CFG->dataroot.'/climaintenance.template.html', $page->get_io()->get_template_file());
|
||||
$page->get_io()->set_preview(true);
|
||||
self::assertSame($CFG->dataroot.'/auth_outage/climaintenance/preview/climaintenance.html', $page->get_io()->get_template_file());
|
||||
self::assertSame($CFG->dataroot.'/auth_outage/climaintenance/preview/climaintenance.html',
|
||||
$page->get_io()->get_template_file());
|
||||
}
|
||||
|
||||
public function test_resourcesfolder() {
|
||||
@@ -76,22 +77,22 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
|
||||
}
|
||||
|
||||
public function test_updatelinkstylesheet() {
|
||||
$local_css_link = $this->get_fixture_path('simple.css');
|
||||
$external_css_link = 'http://google.com/coolstuff.css';
|
||||
$localcsslink = $this->get_fixture_path('simple.css');
|
||||
$externalcsslink = 'http://google.com/coolstuff.css';
|
||||
$html = "<!DOCTYPE html>\n".
|
||||
'<html><head><link href="'.$local_css_link.'" rel="stylesheet" /><title>Title</title></head>'.
|
||||
'<body>Content<link rel="stylesheet" href="'.$external_css_link.'"></body></html>';
|
||||
'<html><head><link href="'.$localcsslink.'" rel="stylesheet" /><title>Title</title></head>'.
|
||||
'<body>Content<link rel="stylesheet" href="'.$externalcsslink.'"></body></html>';
|
||||
$generated = $this->generated_page_html($html);
|
||||
|
||||
self::assertContains('http://www.example.com/moodle/auth/outage/file.php?file=', $generated);
|
||||
self::assertNotContains($local_css_link, $generated);
|
||||
self::assertContains($external_css_link, $generated);
|
||||
self::assertNotContains($localcsslink, $generated);
|
||||
self::assertContains($externalcsslink, $generated);
|
||||
}
|
||||
|
||||
public function test_updatelinkstylesheet_urls() {
|
||||
$local_css_link = $this->get_fixture_path('withurls.css');
|
||||
$localcsslink = $this->get_fixture_path('withurls.css');
|
||||
$html = "<!DOCTYPE html>\n".
|
||||
'<html><head><link href="'.$local_css_link.'" rel="stylesheet" /><title>Title</title></head>'.
|
||||
'<html><head><link href="'.$localcsslink.'" rel="stylesheet" /><title>Title</title></head>'.
|
||||
'<body>Content</body></html>';
|
||||
$page = maintenance_static_page::create_from_html($html);
|
||||
$page->generate();
|
||||
@@ -104,9 +105,9 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
|
||||
}
|
||||
|
||||
public function test_updatelinkstylesheet_urls_quoted() {
|
||||
$local_css_link = $this->get_fixture_path('withurls-quoted.css');
|
||||
$localcsslink = $this->get_fixture_path('withurls-quoted.css');
|
||||
$html = "<!DOCTYPE html>\n".
|
||||
'<html><head><link href="'.$local_css_link.'" rel="stylesheet" /><title>Title</title></head>'.
|
||||
'<html><head><link href="'.$localcsslink.'" rel="stylesheet" /><title>Title</title></head>'.
|
||||
'<body>Content</body></html>';
|
||||
$page = maintenance_static_page::create_from_html($html);
|
||||
$page->generate();
|
||||
@@ -119,9 +120,9 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
|
||||
}
|
||||
|
||||
public function test_updatelinkstylesheet_urls_subdir() {
|
||||
$local_css_link = $this->get_fixture_path('subdir/withurls-subdir.css');
|
||||
$localcsslink = $this->get_fixture_path('subdir/withurls-subdir.css');
|
||||
$html = "<!DOCTYPE html>\n".
|
||||
'<html><head><link href="'.$local_css_link.'" rel="stylesheet" /><title>Title</title></head>'.
|
||||
'<html><head><link href="'.$localcsslink.'" rel="stylesheet" /><title>Title</title></head>'.
|
||||
'<body>Content</body></html>';
|
||||
$page = maintenance_static_page::create_from_html($html);
|
||||
$page->generate();
|
||||
@@ -134,16 +135,16 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
|
||||
}
|
||||
|
||||
public function test_updateimages() {
|
||||
$local_img_link = $this->get_fixture_path('catalyst.png');
|
||||
$external_img_link = 'http://google.com/coolstyle.css';
|
||||
$localimglink = $this->get_fixture_path('catalyst.png');
|
||||
$externalimglink = 'http://google.com/coolstyle.css';
|
||||
$html = "<!DOCTYPE html>\n".
|
||||
'<html><head><title>Title</title></head>'.
|
||||
'<body><img src="'.$local_img_link.'">Content<img src="'.$external_img_link.'" /></body></html>';
|
||||
'<body><img src="'.$localimglink.'">Content<img src="'.$externalimglink.'" /></body></html>';
|
||||
$generated = $this->generated_page_html($html);
|
||||
|
||||
self::assertContains('http://www.example.com/moodle/auth/outage/file.php?file=', $generated);
|
||||
self::assertNotContains($local_img_link, $generated);
|
||||
self::assertContains($external_img_link, $generated);
|
||||
self::assertNotContains($localimglink, $generated);
|
||||
self::assertContains($externalimglink, $generated);
|
||||
}
|
||||
|
||||
public function test_updatelinkfavicon() {
|
||||
|
||||
@@ -21,52 +21,11 @@
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*
|
||||
* @var array $viewbag
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
if ($viewbag['admin']) {
|
||||
$adminlinks = [];
|
||||
foreach ([
|
||||
'startofwarning' => -$viewbag['outage']->get_warning_duration(),
|
||||
'15secondsbefore' => -15,
|
||||
'start' => 0,
|
||||
'endofoutage' => $viewbag['outage']->get_duration_planned() - 1,
|
||||
] as $title => $delta) {
|
||||
$adminlinks[] = html_writer::link(
|
||||
new moodle_url(
|
||||
'/auth/outage/info.php',
|
||||
[
|
||||
'id' => $viewbag['outage']->id,
|
||||
'auth_outage_preview' => $viewbag['outage']->id,
|
||||
'auth_outage_delta' => $delta,
|
||||
]
|
||||
),
|
||||
get_string('info'.$title, 'auth_outage')
|
||||
);
|
||||
}
|
||||
$adminlinks[] = html_writer::link(
|
||||
new moodle_url(
|
||||
'/auth/outage/info.php',
|
||||
[
|
||||
'id' => $viewbag['outage']->id,
|
||||
'auth_outage_preview' => $viewbag['outage']->id,
|
||||
'auth_outage_delta' => 0,
|
||||
'auth_outage_hide_warning' => 1,
|
||||
]
|
||||
),
|
||||
get_string('infohidewarning', 'auth_outage')
|
||||
);
|
||||
$adminlinks[] = html_writer::link(
|
||||
new moodle_url('/auth/outage/preview.php', ['id' => $viewbag['outage']->id]),
|
||||
get_string('infostaticpage', 'auth_outage')
|
||||
);
|
||||
|
||||
$admineditlink = html_writer::link(
|
||||
new moodle_url('/auth/outage/edit.php', ['edit' => $viewbag['outage']->id]),
|
||||
get_string('outageedit', 'auth_outage')
|
||||
);
|
||||
}
|
||||
?>
|
||||
|
||||
<div class="auth_outage_info">
|
||||
@@ -82,6 +41,48 @@ if ($viewbag['admin']) {
|
||||
<div class="auth_outage_info_description"><?php echo $viewbag['outage']->get_description(); ?></div>
|
||||
|
||||
<?php if ($viewbag['admin']): ?>
|
||||
<?php
|
||||
$adminlinks = [];
|
||||
foreach ([
|
||||
'startofwarning' => -$viewbag['outage']->get_warning_duration(),
|
||||
'15secondsbefore' => -15,
|
||||
'start' => 0,
|
||||
'endofoutage' => $viewbag['outage']->get_duration_planned() - 1,
|
||||
] as $title => $delta) {
|
||||
$adminlinks[] = html_writer::link(
|
||||
new moodle_url(
|
||||
'/auth/outage/info.php',
|
||||
[
|
||||
'id' => $viewbag['outage']->id,
|
||||
'auth_outage_preview' => $viewbag['outage']->id,
|
||||
'auth_outage_delta' => $delta,
|
||||
]
|
||||
),
|
||||
get_string('info'.$title, 'auth_outage')
|
||||
);
|
||||
}
|
||||
$adminlinks[] = html_writer::link(
|
||||
new moodle_url(
|
||||
'/auth/outage/info.php',
|
||||
[
|
||||
'id' => $viewbag['outage']->id,
|
||||
'auth_outage_preview' => $viewbag['outage']->id,
|
||||
'auth_outage_delta' => 0,
|
||||
'auth_outage_hide_warning' => 1,
|
||||
]
|
||||
),
|
||||
get_string('infohidewarning', 'auth_outage')
|
||||
);
|
||||
$adminlinks[] = html_writer::link(
|
||||
new moodle_url('/auth/outage/preview.php', ['id' => $viewbag['outage']->id]),
|
||||
get_string('infostaticpage', 'auth_outage')
|
||||
);
|
||||
|
||||
$admineditlink = html_writer::link(
|
||||
new moodle_url('/auth/outage/edit.php', ['edit' => $viewbag['outage']->id]),
|
||||
get_string('outageedit', 'auth_outage')
|
||||
);
|
||||
?>
|
||||
<div class="auth_outage_info_adminlinks">
|
||||
<b><?php echo get_string('preview'); ?>:</b>
|
||||
<?php echo implode(' | ', $adminlinks); ?><br/>
|
||||
|
||||
Reference in New Issue
Block a user