aboutsummaryrefslogtreecommitdiff
path: root/controllers
diff options
context:
space:
mode:
authorGeorgi Nikolov <ggeorgi60@gmail.com>2025-01-25 19:40:02 +0200
committerGeorgi Nikolov <ggeorgi60@gmail.com>2025-01-25 19:40:02 +0200
commit37ea5e37ec03dcd22a904579a877d23c9db99c95 (patch)
tree4b5ca274a91509ce6fe4564836c891d105fa3349 /controllers
parent39091b772b311172f5d20b98a26933aad1dbf2f5 (diff)
downloadnowayforward_human-37ea5e37ec03dcd22a904579a877d23c9db99c95.tar
nowayforward_human-37ea5e37ec03dcd22a904579a877d23c9db99c95.tar.gz
nowayforward_human-37ea5e37ec03dcd22a904579a877d23c9db99c95.zip
Added downloading and replacing of resources in the css styles
Diffstat (limited to 'controllers')
-rw-r--r--controllers/archive_page.php41
1 files changed, 41 insertions, 0 deletions
diff --git a/controllers/archive_page.php b/controllers/archive_page.php
index 9467eab..88b2b0c 100644
--- a/controllers/archive_page.php
+++ b/controllers/archive_page.php
@@ -147,6 +147,42 @@ class DownloadPage {
return rtrim($baseUrl, '/') . '/' . ltrim($relativeUrl, '/');
}
+ function handleCssUrls(&$content) : void {
+ if (preg_match_all('/url\((.*)\)/', $content, $matches, PREG_PATTERN_ORDER) > 0) {
+ $urls = $matches[1];
+
+ foreach ($urls as $url) {
+ $original_url = $url;
+ $url = ltrim($url, "'()");
+ $url = rtrim($url, "'()");
+ $url = substr($url, 0, strpos($url, "'"));
+
+ // Handle relative URLs
+ if (parse_url($url, PHP_URL_SCHEME) === null) {
+ $url = $this->page_url . $url;
+ }
+
+ if ($this->isResourceAccessible($url)) {
+ // Get the file name and local path
+ $file_name = basename($url);
+ $file_path = './' . $file_name;
+ $folder_path = $this->folder_location . '/' . $this->folder_name;
+ $urlContents = $this->downloadFile($url);
+ if ($urlContents) {
+ // Save the resource locally
+ $file = fopen($folder_path . '/' . $file_name, "w");
+ if ($file){
+ fwrite($file, $urlContents);
+ fclose($file);
+ }
+ // Replace the URL in the CSS content
+ $content = str_replace($original_url, "'" . $file_path . "'", $content);
+ }
+ }
+ }
+ }
+ }
+
function downloadSource(&$dom, $folder_path, $tagName, $attribute, $simular_pages) : void {
$links = $dom->getElementsByTagName($tagName);
foreach($links as $link) {
@@ -157,6 +193,11 @@ class DownloadPage {
$sourceContent = $this->downloadFile($sourceUrl);
if ($sourceContent) {
$found_resource = false;
+ if ($tagName == "link") {
+ // The resource is a css resource most likely
+ // Go trough the resource, download the urls and replace them with their local path
+ $this->handleCssUrls($sourceContent);
+ }
if (count($simular_pages) != 0) {
// Page is not unique so check if any other already downloaded resource is
// the same as the resource that is needed thus not actually needing to download it