From ebb30781cc36b38473bb308c213cf8f8ca51f147 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Mon, 27 Jun 2016 12:55:05 +0000 Subject: [PATCH] * outputhtml : add a timestamp after the internal resources when creating the page. Like this, when updating a script, image or css file, it is updated by the client automatically (the browser don't use its cache) git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2771 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- outputhtml.php | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/outputhtml.php b/outputhtml.php index 895b280..c0a35db 100644 --- a/outputhtml.php +++ b/outputhtml.php @@ -83,7 +83,6 @@ class outputhtml extends output EOT; $resView = str_replace ("{content}", $resView, $layoutPage); } - // Do the title replacement in the replacement structure if (! isset ($replacement["{title}"])) $replacement["{title}"] = $title; @@ -93,6 +92,40 @@ EOT; foreach ($replacement as $key=>$val) $resView = str_replace ($key, $val, $resView); + // Manage the timestamp/md5sum for the external files managed by this + // server. + $dom = new DOMDocument (); + @$dom->loadHTML ($resView); + foreach (array ("script"=>"src", "link"=>"href", "img"=>"src") + as $key=>$val) + { + $files = $dom->getElementsByTagName ($key); + for ($i = 0 ; $i < $files->length ; $i++) + { + if ($files[$i]->hasAttribute ($val) === false) + continue; + $src = $files[$i]->getAttribute ($val); + if ($src === "") + continue; + if (substr ($src, 0, 7) === "http://" || + substr ($src, 0, 8) === "https://" || + substr ($src, 0, 2) === "//") + continue; + // If there is already a ? with some variables, don't add the timestamp + if (strpos ($src, "?")) + continue; + if (! array_key_exists ("{baseurl}", $replacement)) + continue; + $srcFile = substr ($src, strlen ($replacement["{baseurl}"])); + if (file_exists ($srcFile)) + $srcHash = filemtime ($srcFile); + else + $srcHash = time(); + $dom->getElementsByTagName ($key)[$i]->setAttribute ($val, + "$src?$srcHash"); + } + } + echo $dom->saveHTML();exit; echo $resView; if (!defined ("PHPUNIT")) exit;