aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorgi Nikolov <ggeorgi60@gmail.com>2025-01-18 18:34:06 +0200
committerGeorgi Nikolov <ggeorgi60@gmail.com>2025-01-18 18:34:06 +0200
commit950f1febd6028bb3317783ae4628a10392a2db7f (patch)
tree5e06fcb831da76f0ae1eb98f177044489d0b2143
parentd3833d2a50371b5ebc989ca9b4a999a5f7f74c98 (diff)
downloadnowayforward_human-950f1febd6028bb3317783ae4628a10392a2db7f.tar
nowayforward_human-950f1febd6028bb3317783ae4628a10392a2db7f.tar.gz
nowayforward_human-950f1febd6028bb3317783ae4628a10392a2db7f.zip
Added archiving of existing pages and saving them in the main repo
-rw-r--r--.gitignore1
-rw-r--r--src/archive_page.php111
2 files changed, 75 insertions, 37 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..632602c
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+archives/*.zip
diff --git a/src/archive_page.php b/src/archive_page.php
index 9c37fb0..16aac8a 100644
--- a/src/archive_page.php
+++ b/src/archive_page.php
@@ -8,52 +8,89 @@
<body>
<p>
<?php
- function apply_correct_protocol($url, $protocol) {
- if (str_contains($url, $protocol)) {
- return $url;
- }
+ $WEBSITE_CATEGORY = 'page_url';
+ $DOWNLOADS_FOLDER = '../archives/';
+ $website_url = $_POST[$WEBSITE_CATEGORY];
+ $currentPage = new DownloadPage($website_url, "test2.zip", $DOWNLOADS_FOLDER);
- return $protocol . $url;
- }
+ class DownloadPage {
+ private $zip_location;
+ private $zip_name;
+ private $page_url;
+ private $page_contents;
- function does_website_exist($url) {
- $result = false;
+ function __construct($page_url, $zip_name, $zip_location) {
+ $this->zip_location = $zip_location;
+ $this->zip_name = $zip_name;
+ $this->page_url = $page_url;
+ list($website_exists, $this->page_url) = $this->does_website_exist($this->page_url);
+ if ($website_exists) {
+ $this->page_contents = file_get_contents($this->page_url);
+ $zip = $this->create_zip_archive();
+ } else {
+ echo "Website does not exist";
+ }
+ }
- // Check if the site exists with https
- $https_url = apply_correct_protocol($url, "https://");
- if ($https_url != $url) {
- $url_headers = @get_headers($https_url);
- $result |= $url_headers && $url_headers[0] != 'HTTP/1.1 404 Not Found';
+ function set_zip_location($zip_location) {
+ $this->zip_location = $zip_location;
+ }
+ function set_zip_name($zip_name) {
+ $this->zip_name = $zip_name;
}
+ function set_page_url($page_url) {
+ $this->page_url = $page_url;
+ }
+ function apply_correct_protocol($url, $protocol) {
+ if (str_contains($url, $protocol)) {
+ return $url;
+ }
- // Check if the site exists with http
- $http_url = apply_correct_protocol($url, "http://");
- if ($http_url != $url) {
- $url_headers = @get_headers($http_url);
- $result |= $url_headers && $url_headers[0] != 'HTTP/1.1 404 Not Found';
+ return $protocol . $url;
}
- // Check if the site exists as is
- // Will take effect when the user has entered the https/http protocol with the site
- $url_headers = @get_headers($url);
- $result |= $url_headers && $url_headers[0] != 'HTTP/1.1 404 Not Found';
+ function does_website_exist($url) {
- return $result;
- }
+ // Check if the site exists with https
+ $https_url = $this->apply_correct_protocol($url, "https://");
+ if ($https_url != $url) {
+ $url_headers = @get_headers($https_url);
+ if ($url_headers && $url_headers[0] != 'HTTP/1.1 404 Not Found') {
+ return array(true, $https_url);
+ }
+ }
- $WEBSITE_CATEGORY = 'page_url';
- $DATABASE_NAME = 'db';
- $TABLE_NAME = 'users';
- $website_url = $_POST[$WEBSITE_CATEGORY];
- $website_exists = does_website_exist($website_url) ? "true" : "false";
- echo "Website exists: $website_exists" . "<br/>";
-
- try {
- $db = new PDO("mysql:host=localhost;dbname=$DATABASE_NAME", $user, $password);
- echo $db->query("DESCRIBE $TABLE_NAME");
- } catch (PDOException $e) {
- print "Error!: " . $e->getMessage() . "<br/>";
- die();
+ // Check if the site exists with http
+ $http_url = $this->apply_correct_protocol($url, "http://");
+ if ($http_url != $url) {
+ $url_headers = @get_headers($http_url);
+ if ($url_headers && $url_headers[0] != 'HTTP/1.1 404 Not Found') {
+ return array(true, $http_url);
+ }
+ }
+
+ // Check if the site exists as is
+ // Will take effect when the user has entered the https/http protocol with the site
+ $url_headers = @get_headers($url);
+ if ($url_headers && $url_headers[0] != 'HTTP/1.1 404 Not Found') {
+ return array(true, $url);
+ }
+
+ return array(false, $url);
+ }
+
+ function create_zip_archive() {
+ // Creates and returns a zip object resulted from zipping the page that was downloaded
+ $zip = new ZipArchive();
+ if ($zip->open($this->zip_location . $this->zip_name, ZipArchive::CREATE | ZipArchive::OVERWRITE) == TRUE) {
+ $zip->addFromString('index.html', $this->page_contents);
+ $zip->close();
+ } else {
+ echo "Zip archive could not be open";
+ }
+
+ return $zip;
+ }
}
?>
</p>