From 2a9fdd8be41ace81dfedc0c5f19f0d3c09b1e74d Mon Sep 17 00:00:00 2001 From: Georgi Nikolov Date: Sat, 25 Jan 2025 18:15:00 +0200 Subject: Fixed the icons not being able to be Null Also added a fallback to the icons so they can be downloaded from the servers directly --- controllers/archive_page.php | 50 ++++++++++++++++++++++++++++++++++++++++---- migrations/00-initial.sql | 2 +- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/controllers/archive_page.php b/controllers/archive_page.php index 7372681..37ce4ee 100644 --- a/controllers/archive_page.php +++ b/controllers/archive_page.php @@ -33,12 +33,48 @@ class DownloadPage { $this->folder_name = Database\Webpage::getPagesCount() + 1; $this->page_contents = $this->downloadFile($this->page_url); $this->createArchive($simular_pages); + if (!$this->favicon_path) { + // No favicons were found in the normal links + // Fallback and try to download them from the server directly + $this->tryDownloadFavicon(); + } Database\Webpage::create($folder_location, $page_url, 1, $this->favicon_path); } else { echo "Website does not exist"; } } + function tryDownloadFavicon() : void { + // Tries to download an icon from the server directly + // The tried names are favicon.png/ico/jpeg/jpg/svg + + foreach(["png", "ico", "jpeg", "jpg", "svg"] as $ending) { + $currentName = "/favicon." . $ending; + $currentLink = $this->page_url . $currentName; + if ($this->downloadFavicon($currentLink, $currentName)) { + break; + } + } + } + + function downloadFavicon(string $currentLink, string $currentName) : bool { + if ($this->isResourceAccessible($currentLink)) { + $sourceContent = $this->downloadFile($currentLink); + if ($sourceContent) { + $resourceName = basename($currentName); + $folder_path = $this->folder_location . '/' . $this->folder_name; + $file = fopen($folder_path . '/' . $resourceName, "w"); + if ($file){ + fwrite($file, $sourceContent); + fclose($file); + $this->favicon_path = $this->folder_name . $currentName; + return true; + } + } + } + return false; + } + function getCorrectLinkPattern($page_url) : string { // NOTE: Offset by 2 because of the '//' of the protocol $page_url = substr($page_url, strpos($page_url, "//") + 2, strlen($page_url)); @@ -134,8 +170,11 @@ class DownloadPage { // change the link to point to the source of the previous archive instead of downloading a news source $link->setAttribute($attribute, "../" . $page->WID . "/" . $resourceName); $found_resource = true; - if (str_contains($resourceName, "favicon")) { - $this->favicon_path = $page->WID . "/" . $resourceName; + if ($tagName == "link") { + $faviconTry = $link->getAttribute("rel"); + if ($faviconTry && ($faviconTry == "icon" || $faviconTry == "icon shortcut")) { + $this->favicon_path = $page->WID . "/" . $resourceName; + } } break; } @@ -151,8 +190,11 @@ class DownloadPage { fwrite($file, $sourceContent); fclose($file); } - if (str_contains($resourceName, "favicon")) { - $this->favicon_path = $this->folder_name . "/" . $resourceName; + if ($tagName == "link") { + $faviconTry = $link->getAttribute("rel"); + if ($faviconTry && ($faviconTry == "icon" || $faviconTry == "icon shortcut")) { + $this->favicon_path = $this->folder_name . "/" . $resourceName; + } } } } diff --git a/migrations/00-initial.sql b/migrations/00-initial.sql index 0edf29b..a072863 100644 --- a/migrations/00-initial.sql +++ b/migrations/00-initial.sql @@ -24,7 +24,7 @@ CREATE TABLE IF NOT EXISTS Webpages ( Date DATETIME NOT NULL, Visits INT NOT NULL, RequesterUID INT NOT NULL, - FaviconPath VARCHAR(512) NOT NULL, + FaviconPath VARCHAR(512), PRIMARY KEY (WID), FOREIGN KEY (RequesterUID) REFERENCES Users(UID) ); -- cgit v1.2.3