From b089bfc551a5fa23c59ed0c70b1fac1c4fc4d228 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Sat, 23 Dec 2023 10:52:29 +0100 Subject: [PATCH] feat: add wrapper function to log page access and exit --- www/router.php | 76 +++++++++++++++++-------------- www/templates/pathed_content.html | 2 +- 2 files changed, 44 insertions(+), 34 deletions(-) diff --git a/www/router.php b/www/router.php index 84b30d8..4d0a219 100644 --- a/www/router.php +++ b/www/router.php @@ -5,18 +5,9 @@ $data_time_start = microtime(true); require_once 'vendor/autoload.php'; require_once 'post_adapter.php'; -// $sql = mysqli_connect('mysql', 'root', 'example', 'dragon_fire'); $adapter = new PostHandler(); -//if (!$sql) -// { -// echo 'Connection failed
'; -// echo 'Error number: ' . mysqli_connect_errno() . '
'; -// echo 'Error message: ' . mysqli_connect_error() . '
'; -// die(); -// } - $loader = new \Twig\Loader\FilesystemLoader(['./templates', './user_content']); $twig = new \Twig\Environment($loader,['debug' => true]); @@ -26,6 +17,42 @@ use Twig\Extra\Markdown\DefaultMarkdown; use Twig\Extra\Markdown\MarkdownRuntime; use Twig\RuntimeLoader\RuntimeLoaderInterface; +function deduce_user_agent() { + $real_agent=$_SERVER['HTTP_USER_AGENT']; + + if(preg_match('/(Googlebot|\w*Google\w*)/', $real_agent, $match)) { + return "bot/google/" . $match[1]; + } + elseif(preg_match('/(Mozilla|Chrome|Chromium)/', $real_agent, $match)) { + return "user/" . $match[1]; + } + else { + return "unidentified"; + } +} + +function log_and_die($path, $die_code = 0, $referrer = null) { + global $data_time_start; + global $adapter; + + $data_time_end = microtime(true); + + if(!isset($referrer)) { + $referrer = 'magic'; + + if(isset($_SERVER['HTTP_REFERER'])) { + $referrer = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST); + } + } + + $adapter->log_post_access($path, + deduce_user_agent(), + $referrer, + $data_time_end - $data_time_start); + + die($die_code); +} + $twig->addRuntimeLoader(new class implements RuntimeLoaderInterface { public function load($class) { if (MarkdownRuntime::class === $class) { @@ -34,22 +61,6 @@ $twig->addRuntimeLoader(new class implements RuntimeLoaderInterface { } }); -function deduce_user_agent() { - $real_agent=$_SERVER['HTTP_USER_AGENT']; - - if(preg_match('/(?:Mozilla|Chrome|Chromium)/', $real_agent)) { - return "web"; - } - elseif(preg_match('/(?:google)/', $real_agent)) { - return "bot/google"; - } - else { - return "unidentified"; - } -} - -$user_agent = deduce_user_agent(); - function generate_website($SURI) { global $twig; global $adapter; @@ -72,7 +83,7 @@ function generate_website($SURI) { "authorized" => false ]); - die(); + log_and_die('/api/401'); } if($SURI = '/api/admin/upload') { @@ -84,6 +95,9 @@ function generate_website($SURI) { if($SURI == '/api/post_counters') { header('Content-Type: application/json'); echo json_encode($adapter->get_post_access_counters()); + } elseif($SURI == '/api/metrics') { + header('Content-Type: application/line'); + echo $adapter->get_post_access_counters_line(); } elseif(preg_match('/^\/api\/posts(.*)$/', $SURI, $match)) { header('Content-Type: application/json'); @@ -112,14 +126,14 @@ function generate_website($SURI) { "post" => $post ]); - die(); + log_and_die('/404', referrer: ($_SERVER['HTTP_REFERER'] ?? 'magic')); } if($post['post_metadata']['type'] == 'directory') { if(preg_match('/^(.*[^\/])((?:#.*)?)$/', $SURI, $match)) { header('Location: ' . $match[1] . '/' . $match[2]); - return; + die(); } echo $twig->render('post_types/directory.html', [ @@ -150,10 +164,6 @@ function generate_website($SURI) { generate_website($_SERVER['REQUEST_URI']); -$data_time_end = microtime(true); - -$adapter->log_post_access($_SERVER['REQUEST_URI'], - $user_agent, - $data_time_end - $data_time_start) +log_and_die($_SERVER['REQUEST_URI']); ?> diff --git a/www/templates/pathed_content.html b/www/templates/pathed_content.html index 337c711..cdcb753 100644 --- a/www/templates/pathed_content.html +++ b/www/templates/pathed_content.html @@ -15,7 +15,7 @@ {%endblock%} - This article was created on {{ post.post_create_time }}, last edited {{ post.post_update_time }}, and was viewed {{ post.post_access_counter }} times~ + This article was created on {{ post.post_create_time }}, last edited {{ post.post_update_time }}, and was viewed {{ post.post_access_count }} times~