aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorgi Nikolov <ggeorgi60@gmail.com>2025-01-25 18:15:00 +0200
committerGeorgi Nikolov <ggeorgi60@gmail.com>2025-01-25 18:15:00 +0200
commit2a9fdd8be41ace81dfedc0c5f19f0d3c09b1e74d (patch)
tree3301c127a3021c5fa86dcc3378639cfd26c39d2e
parent14a268ba7a7ec0127285f3cfdc253ce602da8170 (diff)
downloadnowayforward_human-2a9fdd8be41ace81dfedc0c5f19f0d3c09b1e74d.tar
nowayforward_human-2a9fdd8be41ace81dfedc0c5f19f0d3c09b1e74d.tar.gz
nowayforward_human-2a9fdd8be41ace81dfedc0c5f19f0d3c09b1e74d.zip
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
-rw-r--r--controllers/archive_page.php50
-rw-r--r--migrations/00-initial.sql2
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)
);