aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSyndamia <kamen@syndamia.com>2025-01-19 07:44:57 +0200
committerKamen Mladenov <kamen@syndamia.com>2025-01-19 07:46:17 +0200
commitc36724bb6cb37e03d72b0d6f0995e0a91b871edf (patch)
treedde3aed035dff7b5438fef6b5dce6f6ec9462ebe
parent3ca7dba628b780ea9ee98eab649f5c9dec058726 (diff)
downloadnowayforward_human-c36724bb6cb37e03d72b0d6f0995e0a91b871edf.tar
nowayforward_human-c36724bb6cb37e03d72b0d6f0995e0a91b871edf.tar.gz
nowayforward_human-c36724bb6cb37e03d72b0d6f0995e0a91b871edf.zip
feat: Move archive_page logic from src to controllers
-rw-r--r--.gitignore2
-rw-r--r--apache/httpd.conf.tpl1
-rw-r--r--controllers/archive_page.php98
-rw-r--r--shell.nix9
-rw-r--r--src/archive_page.php98
5 files changed, 84 insertions, 124 deletions
diff --git a/.gitignore b/.gitignore
index 10dd2e8..9ccc3cf 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,2 @@
-archives/*.zip
+.archives
.config
diff --git a/apache/httpd.conf.tpl b/apache/httpd.conf.tpl
index 0a63292..3c87dab 100644
--- a/apache/httpd.conf.tpl
+++ b/apache/httpd.conf.tpl
@@ -32,3 +32,4 @@ AddType application/javascript .js
SetEnv MYSQL_UNIX_SOCKET ${MYSQL_UNIX_SOCKET}
SetEnv USER ${USER}
+SetEnv ARCHIVES_DIR ${ARCHIVES_DIR}
diff --git a/controllers/archive_page.php b/controllers/archive_page.php
index 593076f..ff766dd 100644
--- a/controllers/archive_page.php
+++ b/controllers/archive_page.php
@@ -2,40 +2,88 @@
function on_post() {
$WEBSITE_CATEGORY = 'page_url';
+ $DOWNLOADS_FOLDER = getenv('ARCHIVES_DIR');
$website_url = $_POST[$WEBSITE_CATEGORY];
- $website_exists = does_website_exist($website_url) ? "true" : "false";
- echo "Website exists: $website_exists" . "<br/>";
+ $currentPage = new DownloadPage($website_url, "test2.zip", $DOWNLOADS_FOLDER);
}
-function apply_correct_protocol($url, $protocol) {
- if (str_contains($url, $protocol)) {
- return $url;
- }
+class DownloadPage {
+ private $zip_location;
+ private $zip_name;
+ private $page_url;
+ private $page_contents;
- return $protocol . $url;
-}
+ 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";
+ }
+ }
-function does_website_exist($url) {
- $result = false;
+ 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 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';
+ return $protocol . $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';
+ function does_website_exist($url) {
+
+ // 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);
+ }
+ }
+
+ // 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);
}
- // 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 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();
+ echo "Archived {$this->page_url}";
+ } else {
+ echo "Zip archive could not be open";
+ }
- return $result;
+ return $zip;
+ }
}
diff --git a/shell.nix b/shell.nix
index 5d18df2..5198659 100644
--- a/shell.nix
+++ b/shell.nix
@@ -36,10 +36,19 @@ let
: ''${PHP_FPM_SOCKET:=$ROOT_DIR/php-fpm.sock}
export SERVER_ROOT SERVER_PORT ROOT_DIR PHP_FPM_SOCKET
+ : ''${ARCHIVES_DIR:=$REPOSITORY/.archives}
+ export ARCHIVES_DIR
+
#
# Apache2
#
+ if [ ! -d "$ARCHIVES_DIR" ]
+ then
+ echo 'Creating the archives directory...'
+ mkdir -p "$ARCHIVES_DIR"
+ fi
+
if [ ! -d "$ROOT_DIR" ]
then
echo 'Installing apache config...'
diff --git a/src/archive_page.php b/src/archive_page.php
deleted file mode 100644
index 16aac8a..0000000
--- a/src/archive_page.php
+++ /dev/null
@@ -1,98 +0,0 @@
-<!DOCTYPE html>
-<html lang="en">
-<head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>PHP answer to the request</title>
-</head>
-<body>
-<p>
-<?php
- $WEBSITE_CATEGORY = 'page_url';
- $DOWNLOADS_FOLDER = '../archives/';
- $website_url = $_POST[$WEBSITE_CATEGORY];
- $currentPage = new DownloadPage($website_url, "test2.zip", $DOWNLOADS_FOLDER);
-
- class DownloadPage {
- private $zip_location;
- private $zip_name;
- private $page_url;
- private $page_contents;
-
- 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";
- }
- }
-
- 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;
- }
-
- return $protocol . $url;
- }
-
- function does_website_exist($url) {
-
- // 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);
- }
- }
-
- // 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>
-</body>
-</html>