diff options
| author | Syndamia <kamen@syndamia.com> | 2025-01-26 14:04:11 +0200 |
|---|---|---|
| committer | Syndamia <kamen@syndamia.com> | 2025-01-26 14:04:11 +0200 |
| commit | bfa238c056f31f36c00faad1c5995cbb8af3bd26 (patch) | |
| tree | e35d6b33fff3e79ae905d7dad699134553a1292d | |
| parent | c36f74386bcbb19c13915fe3796cb757aa7d1845 (diff) | |
| download | nowayforward_human-bfa238c056f31f36c00faad1c5995cbb8af3bd26.tar nowayforward_human-bfa238c056f31f36c00faad1c5995cbb8af3bd26.tar.gz nowayforward_human-bfa238c056f31f36c00faad1c5995cbb8af3bd26.zip | |
feat!: Rework all views to be used with the router
| -rw-r--r-- | controllers/archive.php | 360 | ||||
| -rw-r--r-- | controllers/login.php | 11 | ||||
| -rw-r--r-- | controllers/meta.php | 13 | ||||
| -rw-r--r-- | controllers/register.php | 12 | ||||
| -rw-r--r-- | views/404/index.php | 1 | ||||
| -rw-r--r-- | views/404/meta.php | 3 | ||||
| -rw-r--r-- | views/archive/index.php | 29 | ||||
| -rw-r--r-- | views/archive/meta.php | 5 | ||||
| -rw-r--r-- | views/authenticate.js | 2 | ||||
| -rw-r--r-- | views/footer.php | 16 | ||||
| -rw-r--r-- | views/header.php | 36 | ||||
| -rw-r--r-- | views/home/index.php | 26 | ||||
| -rw-r--r-- | views/home/meta.php | 3 | ||||
| -rw-r--r-- | views/login/index.php | 19 | ||||
| -rw-r--r-- | views/login/meta.php | 4 | ||||
| -rw-r--r-- | views/meta.php | 19 | ||||
| -rw-r--r-- | views/profile/authenticate.php | 10 | ||||
| -rw-r--r-- | views/profile/index.php | 12 | ||||
| -rw-r--r-- | views/profile/meta.php | 4 | ||||
| -rw-r--r-- | views/register/index.php | 16 | ||||
| -rw-r--r-- | views/register/meta.php | 4 |
21 files changed, 446 insertions, 159 deletions
diff --git a/controllers/archive.php b/controllers/archive.php index dc72045..939b133 100644 --- a/controllers/archive.php +++ b/controllers/archive.php @@ -1,12 +1,360 @@ <?php +namespace Controller; +use Database; +use DOMDocument; -function on_get() { - global $page; - try { - $page = Database\Webpage::fromDB($_GET["page_url"]); - $page->incrementVisits(); +function on_post() { + $WEBSITE_CATEGORY = 'page_url'; + $DOWNLOADS_FOLDER = getenv('ARCHIVES_DIR'); + $website_url = $_POST[$WEBSITE_CATEGORY]; + $currentPage = new DownloadPage($website_url, $DOWNLOADS_FOLDER); +} + +class DownloadPage { + private $folder_location; + private $folder_name; + private $page_url; + private $page_contents; + private $favicon_path; + private $page_title; + + function __construct($page_url, $folder_location) { + $this->folder_location = $folder_location; + $this->page_url = $page_url; + list($website_exists, $this->page_url) = $this->doesWebsiteExist($this->page_url); + // Search for all the regexes that fit the *url* pattern where the pattern is the requested url but without the protocol + $page_url_pattern = $this->getCorrectLinkPattern($page_url); + $simular_pages = Database\Webpage::getArchivePathsByPattern('%' . $page_url_pattern . '%'); + if ($website_exists) { + $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, $this->page_title); + } 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)); + return $page_url; + } + + function setFolderLocation($folder_location) : void { + $this->folder_location = $folder_location; + } + function setFolderName($folder_name) : void { + $this->folder_name = $folder_name; + } + function setPageUrl($page_url) : void { + $this->page_url = $page_url; + } + function applyCorrectProtocol($url, $protocol) : string { + if (str_contains($url, $protocol)) { + return $url; + } + + return $protocol . $url; + } + + function downloadFile($url) : string { + $curl_func = curl_init($url); + curl_setopt($curl_func, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl_func, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($curl_func, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko; compatible; pageburst) Chrome/131.0.6778.204 Safari/537.36"); + $page_contents = curl_exec($curl_func); + curl_close($curl_func); + return $page_contents; + } + + function doesWebsiteExist($url) : array { + // Check if the site exists with https + $https_url = $this->applyCorrectProtocol($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->applyCorrectProtocol($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 resolveUrl($relativeUrl, $baseUrl) : string { + // If the url is already absolute return it + if (parse_url($relativeUrl, PHP_URL_SCHEME)) { + return $relativeUrl; + } + // Otherwise resolve it agains the base url + 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 handleJsImports(&$content) : void { + if (preg_match_all("/import .*'(.*)'/", $content, $matches, PREG_PATTERN_ORDER) > 0) { + $urls = $matches[1]; + + foreach ($urls as $url) { + $original_url = $url; + $url = ltrim($url, "./"); + $url = rtrim($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) { + $source = $link->getAttribute($attribute); + if ($source) { + $sourceUrl = $this->resolveUrl($source, $this->page_url); + if ($this->isResourceAccessible($sourceUrl)) { + $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); + } elseif ($tagName == "script") { + // The resource is a script resource most likely + // Go trough the resource, download the imports and replace them with their local path + $this->handleJsImports($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 + foreach($simular_pages as $page) { + $resourceName = basename($source); + if (!file_exists($this->folder_location . "/" . $page->WID . "/" . $resourceName)) { + continue; + } + $resourceContents = file_get_contents($this->folder_location . "/" . $page->WID . "/" . $resourceName); + if (strlen($resourceContents) == strlen($sourceContent) && md5($resourceContents) == md5($sourceContent)) { + // They are the same resource + // 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 ($tagName == "link") { + $faviconTry = $link->getAttribute("rel"); + if ($faviconTry && ($faviconTry == "icon" || $faviconTry == "icon shortcut")) { + $this->favicon_path = $page->WID . "/" . $resourceName; + } + } + break; + } + } + } + + if (!$found_resource) { + // Page is unique so there will be no resource that can be cached + $resourceName = basename($source); + $link->setAttribute($attribute, './' . $resourceName); + $file = fopen($folder_path . '/' . $resourceName, "w"); + if ($file){ + fwrite($file, $sourceContent); + fclose($file); + } + if ($tagName == "link") { + $faviconTry = $link->getAttribute("rel"); + if ($faviconTry && ($faviconTry == "icon" || $faviconTry == "icon shortcut")) { + $this->favicon_path = $this->folder_name . "/" . $resourceName; + } + } + } + } + } + } + } + } + + // Changes the hyperlinks in the site to ones that are local for the site + // or to the landing page when a page is not archived if the hyperlink of the + // other page is not archived + function changeHyperlinkToLocal(&$dom, $tagName, $attribute) : void { + $tags = $dom->getElementsByTagName($tagName); + foreach($tags as $tag) { + $link = $tag->getAttribute($attribute); + // Make a request to the db and check if any URLs like the 'link' + // exist in it and are presently donwloaded + //$link_url = $this->resolveUrl($link); + $page_url_pattern = $this->getCorrectLinkPattern($link); + // TODO: The link should depend on whether there is a domain in the front or not + $correct_results = Database\Webpage::getArchivePathsByPattern('%' . $page_url_pattern . '%'); + + if (count($correct_results) != 0) { + // If there are any links that are the same as the urls make the $dom attribute point + // to the latest version of that page + $tag->setAttribute($attribute, "../" . $correct_results[0]->WID . "/index.html"); + } else { + // If there are no pages that are like that url point to the landing page of the site + // that says that this page was not yet archived + $tag->setAttribute($attribute, "../../archive/index.php?page_url=" . $this->baseToFullUrlForGet($this->page_url, $link)); + } + } + } + + function baseToFullUrlForGet($url, $base) : string { + $replaced = rtrim($url, '/') . '/' . ltrim($base, '/'); + $replaced = str_replace('/', '%2F', $replaced); + $replaced = str_replace(':', '%3A', $replaced); + return $replaced; + } + + function isResourceAccessible($url) : bool { + $curl_func = curl_init($url); + curl_setopt($curl_func, CURLOPT_NOBODY, true); // Gives only the headers + curl_setopt($curl_func, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl_func, CURLOPT_FOLLOWLOCATION, true); + curl_exec($curl_func); + $code = curl_getinfo($curl_func, CURLINFO_HTTP_CODE); + curl_close($curl_func); + return ($code >= 200 && $code < 400); + } + + function updatePageTitle(&$dom) { + $titles = $dom->getElementsByTagName("title"); + if ($titles->length > 0) { + $this->page_title = $titles->item(0)->textContent; + } + } + + + function createArchive($simular_pages) : void { + // Creates the folder with the correct resources and the main html page in a index.html tag + $dom = new DOMDocument(); + $contentType = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">'; // Ensures the encoding is UTF-8 + @$dom->loadHTML($contentType . $this->page_contents); // This suppresses warnings for invalid HTML + + $folder_path = $this->folder_location . '/' . $this->folder_name; + if (!file_exists($folder_path)) { + mkdir($folder_path, 0777, true); + } + + $this->downloadSource($dom, $folder_path, 'link', 'href', $simular_pages); + $this->downloadSource($dom, $folder_path, 'script', 'src', $simular_pages); + $this->downloadSource($dom, $folder_path, 'img', 'src', $simular_pages); + + $this->updatePageTitle($dom); + + $this->changeHyperlinkToLocal($dom, 'a', 'href'); + + $this->page_contents = $dom->saveHTML(); + $indexFile = fopen($folder_path . '/index.html', "w"); + fwrite($indexFile, $this->page_contents); + fclose($indexFile); } - catch(Exception $e) {} } function applyCorrectProtocol($url, $protocol) : string { diff --git a/controllers/login.php b/controllers/login.php index 75c62ac..7502b03 100644 --- a/controllers/login.php +++ b/controllers/login.php @@ -1,9 +1,12 @@ <?php +namespace Controller; +use Database; +use Exception; function on_post() { - global $status; + global $user_status; global $token; - $status = ""; + $user_status = ""; try { $user = Database\User::fromDB($_POST["username"]); @@ -11,10 +14,10 @@ function on_post() { $token = Database\Cookie::create($user->UID); } else { - $status = "Incorrect password!"; + $user_status = "Incorrect password!"; } } catch(Exception $e) { - $status = "User \"" . $_POST["username"] . "\" doesn't exist!"; + $user_status = "User \"" . $_POST["username"] . "\" doesn't exist!"; } } diff --git a/controllers/meta.php b/controllers/meta.php index e567ac3..e20bbfc 100644 --- a/controllers/meta.php +++ b/controllers/meta.php @@ -1,18 +1,13 @@ <?php -foreach (glob("../models/*.php") as $filename) { - include $filename; -} - function call_handler(string $name) { if (function_exists($name)) { call_user_func($name); } } -match ($_SERVER['REQUEST_METHOD']) { - 'POST' => call_handler('on_post'), - 'GET' => call_handler('on_get'), - 'PUT' => call_handler('on_put'), - 'DELETE' => call_handler('on_delete'), +switch ($_SERVER['REQUEST_METHOD']) { + case 'POST': call_handler('Controller\on_post'); break; + case 'PUT': call_handler('Controller\on_put'); break; + case 'DELETE': call_handler('Controller\on_delete'); break; }; diff --git a/controllers/register.php b/controllers/register.php index 3e8d416..6c0c105 100644 --- a/controllers/register.php +++ b/controllers/register.php @@ -1,11 +1,15 @@ <?php +namespace Controller; +use Database; +use Exception; function on_post() { - global $status; - $status = ""; + global $user_status; + $user_status = ""; + try { Database\User::fromDB($_POST["username"]); - $status = "User \"" . $_POST["username"] . "\" already exists!"; + $user_status = "User \"" . $_POST["username"] . "\" already exists!"; return; } catch(Exception $e) {} @@ -14,6 +18,6 @@ function on_post() { Database\User::create($_POST["username"], $_POST["password"], "User"); } catch(Exception $e) { - $status = $e; + $user_status = $e; } } diff --git a/views/404/index.php b/views/404/index.php new file mode 100644 index 0000000..72d5267 --- /dev/null +++ b/views/404/index.php @@ -0,0 +1 @@ +<h1>Error 404: Page not found!</h1> diff --git a/views/404/meta.php b/views/404/meta.php new file mode 100644 index 0000000..0d1676e --- /dev/null +++ b/views/404/meta.php @@ -0,0 +1,3 @@ +<?php + +$title = 'Page not found!'; diff --git a/views/archive/index.php b/views/archive/index.php index 64d6a77..4195eb2 100644 --- a/views/archive/index.php +++ b/views/archive/index.php @@ -1,16 +1,21 @@ <?php - $title = $_GET["page_url"] . ' archive'; - include '../meta.php'; - + $exists = null; $page = null; - runController('archive'); + + try { + $page = Database\Webpage::fromDB($url); + $page->incrementVisits(); + } + catch(Exception $e) { + $exists = Controller\doesWebsiteExist($url); + } ?> <?php if ($page !== null): ?> <iframe src="<?= "/archives/{$page->WID}" ?>" scrolling="no"></iframe> - <form action="/sample_archive/index.php" method="POST"> - <input type="hidden" name="page_url" value="<?= $_GET["page_url"] ?>"> + <form action="#" method="POST"> + <input type="hidden" name="page_url" value="<?= $url ?>"> <input type="submit" value="Archive Now!"> </form> <!-- Button to add to list --> @@ -39,16 +44,16 @@ </section> <?php endforeach; ?> -<?php elseif(!doesWebsiteExist($_GET["page_url"])): ?> - <h2>"<?= $_GET["page_url"] ?>" Does not exist!</h2> +<?php elseif(!$exists): ?> + <h2>"<?= $url ?>" Does not exist!</h2> <p>Submit another request or check the spelling of the site and try again</p> - <a href="/home/index.php">Go back!</a> + <a href="/">Go back!</a> <?php else: ?> - <h2>"<?= $_GET["page_url"] ?>" hasn't been archived yet!</h2> - <form action="/sample_archive/index.php" method="POST"> - <input type="hidden" name="page_url" value="<?= $_GET["page_url"] ?>"> + <h2>"<?= $url ?>" hasn't been archived yet!</h2> + <form action="#" method="POST"> + <input type="hidden" name="page_url" value="<?= $url ?>"> <input type="submit" value="Archive Now!"> </form> diff --git a/views/archive/meta.php b/views/archive/meta.php new file mode 100644 index 0000000..001d07c --- /dev/null +++ b/views/archive/meta.php @@ -0,0 +1,5 @@ +<?php + +$url = $_GET['url']; +$title = $url . ' archive'; +$controller = 'archive'; diff --git a/views/authenticate.js b/views/authenticate.js index 5e1371a..8b158ce 100644 --- a/views/authenticate.js +++ b/views/authenticate.js @@ -8,7 +8,7 @@ function requestAuthentication() { authentication_response = (request.status == 200) ? request.responseText : ""; } - request.open("POST", "/profile/authenticate.php", true); + request.open("POST", "/authenticate", true); request.setRequestHeader("Authorization", sessionStorage.getItem("token")); request.send(null); } diff --git a/views/footer.php b/views/footer.php deleted file mode 100644 index 22c4631..0000000 --- a/views/footer.php +++ /dev/null @@ -1,16 +0,0 @@ - </article> -</body> -<script type="text/javascript"> - function eval_callbacks() { - if (authentication_response === null) { - setTimeout(eval_callbacks, 50); - } - else if (authentication_response !== "") { - for (callback of authentication_callbacks) { - callback(authentication_response); - } - } - } - eval_callbacks(); -</script> -</html> diff --git a/views/header.php b/views/header.php deleted file mode 100644 index 3835dfb..0000000 --- a/views/header.php +++ /dev/null @@ -1,36 +0,0 @@ -<!DOCTYPE html> -<html lang="en"> -<head> - <meta charset="UTF-8"> - <meta name="robots" content="index, follow"> - <meta name="theme-color" content="#2b2b2e"> - <meta name="viewport" content="width=device-width, initial-scale=1.0"> - <link rel="stylesheet" href="/styles.css"> - <title><?= htmlspecialchars($title ?? "No Way Forward Human");?></title> -</head> -<body> - <script type="text/javascript" src="/authenticate.js"></script> - <header> - <nav> - <div class="fadeout-left"></div> - <a href="/home/index.php">Home</a> - <a href="/sample_archive/index.php">Sample Archive</a> - <div class="flex-expand"></div> - <a id="login" href="/login/index.php">Login</a> - <a id="register" href="/register/index.php">Register</a> - <a id="profile" href="/profile/index.php" hidden>Profile</a> - <div class="fadeout-right"></div> - </nav> - <script type="text/javascript"> - function updateNavbar(response) { - document.getElementById('login').hidden = true; - document.getElementById('register').hidden = true; - - const profile = document.getElementById('profile'); - profile.hidden = false; - profile.href += '?user=' + response; - } - authenticated(updateNavbar); - </script> - </header> - <article> diff --git a/views/home/index.php b/views/home/index.php index f567744..abc872b 100644 --- a/views/home/index.php +++ b/views/home/index.php @@ -1,12 +1,8 @@ -<?php - $title = 'Home'; - include '../meta.php'; -?> - <section class="highlight separate-margin"> <h2>Explore the archives or add a new page</h2> - <form action="/archive/index.php" method="GET" class="font-125 flex-row width-100 center-margin"> - <input type="text" name="page_url" placeholder="Enter a URL" class="flex-expand"> + + <form action="/archive" method="GET" class="font-125 flex-row width-100 center-margin"> + <input type="text" name="url" placeholder="Enter a URL" class="flex-expand"> <input type="submit" value="Search"> </form> </section> @@ -16,7 +12,7 @@ <h1>Most popular archives</h1> <?php foreach(Database\Webpage::mostVisited(10) as $page): ?> - <section class="card" onclick="open_archive('<?= $page->URL ?>')"> + <section class="card" onclick="goto_archive('<?= $page->URL ?>')"> <section class="quickinfo"> <a href="<?= $page->URL ?>"><?= $page->URL ?></a> <span class="float-right"><?= $page->Date ?></span> @@ -29,16 +25,20 @@ <strong>Visits: <?= $page->Visits ?></strong> <strong><!-- Archives count --></strong> </section> + <script type="text/javascript"> + function open_archive(url) { + window.location.href = '/archive/' + url; + } + </script> </section> <?php endforeach; ?> + <h1>...</h1> <div class="card-blank-afterspace"></div> <script type="text/javascript"> -function open_archive(url) { - window.location.href = '/archive/index.php?page_url=' + url; -} + function goto_archive(uri) { + window.location.href = '/archive/?url=' + uri; + } </script> - -<?php end_page(); ?> diff --git a/views/home/meta.php b/views/home/meta.php new file mode 100644 index 0000000..4d1a472 --- /dev/null +++ b/views/home/meta.php @@ -0,0 +1,3 @@ +<?php + +$title = "Home"; diff --git a/views/login/index.php b/views/login/index.php index e538319..1013874 100644 --- a/views/login/index.php +++ b/views/login/index.php @@ -1,16 +1,7 @@ -<?php - $title = 'Login to your account'; - include '../meta.php'; - - $status = null; - $token = null; - runController('login'); -?> - -<?php if ($status !== null): ?> - <?php if ($status !== ""): ?> +<?php if (isset($user_status)): ?> + <?php if ($user_status !== ""): ?> <p> - Fail: <?= $status ?> + Fail: <?= $user_status ?> </p> <?php else: ?> <p> @@ -18,12 +9,12 @@ </p> <script type="text/javascript"> sessionStorage.setItem("token", "<?= $token ?>"); - window.location.href = "/home/index.php"; + window.location.href = "/"; </script> <?php endif; ?> <?php endif; ?> -<form action="./index.php" method="POST"> +<form action="#" method="POST"> <input type="text" name="username" placeholder="Username" minlength="1" pattern="[A-Za-z][A-Za-z_0-9]*"> <input type="password" name="password" placeholder="Password" minlength="4"> <input type="submit" value="Login"> diff --git a/views/login/meta.php b/views/login/meta.php new file mode 100644 index 0000000..06be3e8 --- /dev/null +++ b/views/login/meta.php @@ -0,0 +1,4 @@ +<?php + +$title = 'Login to your account'; +$controller = 'login'; diff --git a/views/meta.php b/views/meta.php deleted file mode 100644 index 4aa7b8f..0000000 --- a/views/meta.php +++ /dev/null @@ -1,19 +0,0 @@ -<?php - -if (isset($title)) { - include_once "../header.php"; -} - -function end_page() { - include_once "../footer.php"; -} - -include_once "../../models/database.php"; -foreach (glob("../../models/*.php") as $filename) { - include_once $filename; -} - -function runController(string $name) { - include_once "../../controllers/$name.php"; - include_once '../../controllers/meta.php'; -} diff --git a/views/profile/authenticate.php b/views/profile/authenticate.php index 540d4a3..afe1ca7 100644 --- a/views/profile/authenticate.php +++ b/views/profile/authenticate.php @@ -8,17 +8,15 @@ if ($_SERVER['REQUEST_METHOD'] !== 'POST') { exit; } -include '../meta.php'; +try { + $headers = apache_request_headers(); + $user = Database\Cookie::fromDB($headers["Authorization"]); -$user = null; -runController('user'); - -if ($user !== null) { http_response_code(200); header('Content-Type: text/plain'); echo $user->Username; } -else { +catch(Exception $e) { http_response_code(401); header('Content-Type: text/plain'); echo 'Bad token!'; diff --git a/views/profile/index.php b/views/profile/index.php index 80b0c4a..2334c1b 100644 --- a/views/profile/index.php +++ b/views/profile/index.php @@ -1,9 +1,9 @@ <?php - $title = $_GET["user"] . ' - Profile'; - include '../meta.php'; - $user = null; - runController('user'); + try { + $user = Database\User::fromDB($username); + } + catch(Exception $e) {} ?> <?php if ($user !== null): ?> @@ -12,7 +12,5 @@ <?= $user->Role ?> </section> <?php else: ?> - <h2>User "<?= $_GET["user"] ?>" doesn't exist!</h2> + <h2>User "<?= $username ?>" doesn't exist!</h2> <?php endif; ?> - -<?php end_page(); ?> diff --git a/views/profile/meta.php b/views/profile/meta.php new file mode 100644 index 0000000..52764ef --- /dev/null +++ b/views/profile/meta.php @@ -0,0 +1,4 @@ +<?php + +$username = explode('/', $uri, 4)[2]; +$title = "$username's profile"; diff --git a/views/register/index.php b/views/register/index.php index 1dc7bea..86f4aa7 100644 --- a/views/register/index.php +++ b/views/register/index.php @@ -1,15 +1,7 @@ -<?php - $title = 'Register a new user'; - include '../meta.php'; - - $status = null; - runController('register'); -?> - -<?php if ($status !== null): ?> - <?php if ($status !== ""): ?> +<?php if (isset($user_status)): ?> + <?php if ($user_status !== ""): ?> <p> - Fail: <?= $status ?> + Fail: <?= $user_status ?> </p> <?php else: ?> <p> @@ -18,7 +10,7 @@ <?php endif; ?> <?php endif; ?> -<form action="./index.php" method="POST"> +<form action="#" method="POST"> <input type="text" name="username" placeholder="Username" minlength="1" pattern="[A-Za-z][A-Za-z_0-9]*"> <input type="password" name="password" placeholder="Password" minlength="4"> <input type="submit" value="Register"> diff --git a/views/register/meta.php b/views/register/meta.php new file mode 100644 index 0000000..0cf11ce --- /dev/null +++ b/views/register/meta.php @@ -0,0 +1,4 @@ +<?php + +$title = 'Register a new account'; +$controller = 'register'; |
