diff --git a/classes/local/controllers/maintenance_static_page.php b/classes/local/controllers/maintenance_static_page.php index c231b24..01c431f 100644 --- a/classes/local/controllers/maintenance_static_page.php +++ b/classes/local/controllers/maintenance_static_page.php @@ -28,7 +28,6 @@ namespace auth_outage\local\controllers; use auth_outage\local\outage; use coding_exception; use DOMDocument; -use DOMElement; use invalid_parameter_exception; use moodle_url; @@ -43,9 +42,6 @@ defined('MOODLE_INTERNAL') || die(); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class maintenance_static_page { - /** @var int */ - private static $nextfile = 1; - /** * Gets the cli maintenance template file location. * @return string @@ -199,18 +195,17 @@ class maintenance_static_page { return $url; // External URL, leave it. } - $file = self::$nextfile++; - if ($type != '') { - $file .= '.'.$type; - } - $path = self::get_resources_folder().'/'.$file; - // PHPUnit will use www.example.com as wwwroot and we don't to copy the file. - if (!PHPUNIT_TEST) { - copy($url, $path); + if (PHPUNIT_TEST) { + $contents = ''; + } else { + $contents = file_get_contents($url); } + $filename = sha1($contents).'.'.$type; + $filepath = self::get_resources_folder().'/'.$filename; + file_put_contents($filepath, $contents); - $url = (string)new moodle_url('/auth/outage/maintenance.php?file='.$file); + $url = (string)new moodle_url('/auth/outage/maintenance.php?file='.$filename); return $url; } } diff --git a/maintenance.php b/maintenance.php index 6db5925..902b193 100644 --- a/maintenance.php +++ b/maintenance.php @@ -44,12 +44,15 @@ if (isset($_GET['file'])) { } // Detect type, we only support css or PNG images. - $type = substr($file, -3); - if ($type == 'css') { - header('Content-type: text/css'); - } else { - header('Content-type: image/png'); - } + header('Content-Type: '.(substr($file, -3) == 'css' ? 'text/css' : 'image/png')); + + // Use cache. + $lifetime = 60 * 60 * 24; // 1 day. + 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'); + readfile($file); return; } diff --git a/tests/phpunit/local/controllers/maintenance_static_page_test.php b/tests/phpunit/local/controllers/maintenance_static_page_test.php index e5a8d35..815cc0a 100644 --- a/tests/phpunit/local/controllers/maintenance_static_page_test.php +++ b/tests/phpunit/local/controllers/maintenance_static_page_test.php @@ -88,7 +88,7 @@ class maintenance_static_page_test extends auth_outage_base_testcase { maintenance_static_page::create_from_html($html); $generated = file_get_contents(maintenance_static_page::get_template_file()); - self::assertContains('http://www.example.com/moodle/auth/outage/maintenance.php/', $generated); + self::assertContains('http://www.example.com/moodle/auth/outage/maintenance.php?file=', $generated); self::assertNotContains($link1, $generated); self::assertNotContains($link2, $generated); self::assertContains($link3, $generated); @@ -104,7 +104,7 @@ class maintenance_static_page_test extends auth_outage_base_testcase { maintenance_static_page::create_from_html($html); $generated = file_get_contents(maintenance_static_page::get_template_file()); - self::assertContains('http://www.example.com/moodle/auth/outage/maintenance.php/', $generated); + self::assertContains('http://www.example.com/moodle/auth/outage/maintenance.php?file=', $generated); self::assertNotContains($link1, $generated); self::assertNotContains($link2, $generated); self::assertContains($link3, $generated); @@ -119,6 +119,6 @@ class maintenance_static_page_test extends auth_outage_base_testcase { $generated = file_get_contents(maintenance_static_page::get_template_file()); self::assertNotContains($link, $generated); - self::assertContains('http://www.example.com/moodle/auth/outage/maintenance.php/', $generated); + self::assertContains('http://www.example.com/moodle/auth/outage/maintenance.php?file=', $generated); } }