From 0c97b4be0145d11fc7155533c8bcc69c010d6bb3 Mon Sep 17 00:00:00 2001 From: Daniel Thee Roperto Date: Thu, 10 Nov 2016 19:29:43 +1100 Subject: [PATCH] Applying moodle coding standards. --- bootstrap.php | 7 +- classes/local/controllers/infopage.php | 10 +-- .../controllers/maintenance_static_page.php | 3 +- .../maintenance_static_page_generator.php | 22 +++-- file.php | 10 ++- .../maintenance_static_page_test.php | 37 ++++---- views/info/content.php | 87 ++++++++++--------- 7 files changed, 92 insertions(+), 84 deletions(-) diff --git a/bootstrap.php b/bootstrap.php index 093f7b6..566d843 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -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. diff --git a/classes/local/controllers/infopage.php b/classes/local/controllers/infopage.php index 11d4dd5..4c691d4 100644 --- a/classes/local/controllers/infopage.php +++ b/classes/local/controllers/infopage.php @@ -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. diff --git a/classes/local/controllers/maintenance_static_page.php b/classes/local/controllers/maintenance_static_page.php index 4c40f06..7c2257b 100644 --- a/classes/local/controllers/maintenance_static_page.php +++ b/classes/local/controllers/maintenance_static_page.php @@ -58,7 +58,8 @@ class maintenance_static_page { } else if (PHPUNIT_TEST) { $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']; } diff --git a/classes/local/controllers/maintenance_static_page_generator.php b/classes/local/controllers/maintenance_static_page_generator.php index e4efb00..1dd1426 100644 --- a/classes/local/controllers/maintenance_static_page_generator.php +++ b/classes/local/controllers/maintenance_static_page_generator.php @@ -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 == '') { diff --git a/file.php b/file.php index 725640c..799566c 100644 --- a/file.php +++ b/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); \ No newline at end of file diff --git a/tests/phpunit/local/controllers/maintenance_static_page_test.php b/tests/phpunit/local/controllers/maintenance_static_page_test.php index 4d3e479..637bb52 100644 --- a/tests/phpunit/local/controllers/maintenance_static_page_test.php +++ b/tests/phpunit/local/controllers/maintenance_static_page_test.php @@ -45,7 +45,8 @@ class maintenance_static_page_test extends auth_outage_base_testcase { $page = maintenance_static_page::create_from_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 = "\n". - 'Title'. - 'Content'; + 'Title'. + 'Content'; $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 = "\n". - 'Title'. + 'Title'. 'Content'; $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 = "\n". - 'Title'. + 'Title'. 'Content'; $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 = "\n". - 'Title'. + 'Title'. 'Content'; $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 = "\n". 'Title'. - 'Content'; + 'Content'; $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() { diff --git a/views/info/content.php b/views/info/content.php index 76a6767..ed6e0cd 100644 --- a/views/info/content.php +++ b/views/info/content.php @@ -21,52 +21,11 @@ * @author Daniel Thee Roperto * @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') - ); -} ?>
@@ -82,6 +41,48 @@ if ($viewbag['admin']) {
get_description(); ?>
+ -$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') + ); + ?>