diff --git a/classes/local/controllers/maintenance_static_page.php b/classes/local/controllers/maintenance_static_page.php index 690538f..cc0b511 100644 --- a/classes/local/controllers/maintenance_static_page.php +++ b/classes/local/controllers/maintenance_static_page.php @@ -28,6 +28,7 @@ namespace auth_outage\local\controllers; use auth_outage\local\outage; use coding_exception; use DOMDocument; +use DOMElement; use invalid_parameter_exception; use moodle_url; @@ -42,12 +43,23 @@ defined('MOODLE_INTERNAL') || die(); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class maintenance_static_page { + /** + * Creates a page based on the given outage. + * @param outage $outage + * @return maintenance_static_page + */ public static function create_from_outage(outage $outage) { global $CFG; $html = file_get_contents($CFG->wwwroot.'/auth/outage/info.php?auth_outage_hide_warning=1&id='.$outage->id); return self::create_from_html($html); } + /** + * Creates a page based on the given HTML. + * @param string $html + * @return maintenance_static_page + * @throws coding_exception + */ public static function create_from_html($html) { if (!is_string($html)) { throw new coding_exception('$html is not valid.'); @@ -69,6 +81,10 @@ class maintenance_static_page { /** @var bool */ protected $preview = false; + /** + * maintenance_static_page constructor. + * @param DOMDocument $dom + */ public function __construct(DOMDocument $dom) { $this->dom = $dom; } @@ -103,6 +119,9 @@ class maintenance_static_page { } } + /** + * Generates the page. + */ public function generate() { self::prepare_dataroot(); self::remove_script_tags(); @@ -121,6 +140,9 @@ class maintenance_static_page { return $this; } + /** + * Remove script tags from DOM. + */ private function remove_script_tags() { $scripts = $this->dom->getElementsByTagName('script'); // List items to remove without changing the DOM. @@ -134,6 +156,9 @@ class maintenance_static_page { } } + /** + * Clean up the dataroot as needed. + */ private function prepare_dataroot() { $dir = self::get_resources_folder(); if (is_dir($dir)) { @@ -142,6 +167,12 @@ class maintenance_static_page { mkdir($dir, 0775, true); } + /** + * Deletes the given directory with all its files and subdirectories. + * @param string $dir Directory to delete. + * @throws coding_exception + * @throws invalid_parameter_exception + */ private function delete_directory_recursively($dir) { // It should never come from user, but protect against possible attacks anyway. $dir = realpath($dir); @@ -172,6 +203,9 @@ class maintenance_static_page { rmdir($dir); } + /** + * Fetch and fixes all link rel="stylesheet" tags. + */ private function update_link_stylesheet() { $links = $this->dom->getElementsByTagName('link'); @@ -185,6 +219,9 @@ class maintenance_static_page { } } + /** + * Fetch and fixes the favicon link tag. + */ private function update_link_favicon() { $links = $this->dom->getElementsByTagName('link'); @@ -198,6 +235,9 @@ class maintenance_static_page { } } + /** + * Fetch and fixes all img tags. + */ private function update_images() { $links = $this->dom->getElementsByTagName('img'); @@ -210,6 +250,12 @@ class maintenance_static_page { } } + /** + * Saves the content of the URL into a file, returning the new URL. + * @param string $url Input URL. + * @param string $type Type of file. + * @return string Output URL. + */ private function prepare_url($url, $type) { global $CFG; diff --git a/maintenance.php b/maintenance.php index cb9187b..dc13028 100644 --- a/maintenance.php +++ b/maintenance.php @@ -21,6 +21,8 @@ * @author Daniel Thee Roperto * @copyright 2016 Catalyst IT * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * + * @var stdClass $CFG */ use auth_outage\dml\outagedb; @@ -50,7 +52,7 @@ if (isset($_GET['file'])) { // Use cache. $lifetime = 60 * 60 * 24; // 1 day. - header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT'); + header('Expires: '.gmdate('D, d M Y H:i:s', time() + $lifetime).' GMT'); header('Pragma: '); header('Cache-Control: public, max-age='.$lifetime); header('Accept-Ranges: none'); diff --git a/tests/phpunit/local/controllers/maintenance_static_page_test.php b/tests/phpunit/local/controllers/maintenance_static_page_test.php index 38b0537..86a66d3 100644 --- a/tests/phpunit/local/controllers/maintenance_static_page_test.php +++ b/tests/phpunit/local/controllers/maintenance_static_page_test.php @@ -128,6 +128,11 @@ class maintenance_static_page_test extends auth_outage_base_testcase { self::assertContains('http://www.example.com/moodle/auth/outage/maintenance.php?file=preview%2F', $generated); } + /** + * Generates the maintenance page (not using preview mode). + * @param string $html Input HTML. + * @return string Output HTML. + */ private function generated_page_html($html) { $page = maintenance_static_page::create_from_html($html); $page->generate();