true]); $twig->addExtension(new Twig\Extra\Markdown\MarkdownExtension()); 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) { return new MarkdownRuntime(new DefaultMarkdown()); } } }); function generate_website($SURI) { global $twig; global $adapter; if(preg_match('/^\/api\/admin/', $SURI)) { header('Content-Type: application/json'); $user_api_key = ''; if(isset($_GET['api_key'])) { $user_api_key = $_GET['api_key']; } if(isset($_POST['api_key'])) { $user_api_key = $_POST['api_key']; } if($user_api_key != file_get_contents('secrets/api_admin_key')) { http_response_code(401); echo json_encode([ "authorized" => false ]); log_and_die('/api/401'); } if($SURI = '/api/admin/upload') { $adapter->handle_upload($_POST['post_path'], $_FILES['post_data']['tmp_name']); echo json_encode(["ok" => true]); } } elseif(preg_match('/^\/api/', $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'); echo json_encode($adapter->get_post_by_path($match[1])); } elseif(preg_match('/^\/api\/subposts(.*)$/', $SURI, $match)) { header('Content-Type: application/json'); echo json_encode(get_subposts($match[1])); } elseif($SURI == '/api/upload') { echo $twig->render('upload.html'); } } elseif(preg_match('/^\s*image/', $_SERVER['HTTP_ACCEPT'])) { header('Location: /raw' . $SURI); exit(0); } elseif(true) { $post = $adapter->get_post_by_path($SURI); if(!$post['found']) { echo $twig->render('post_types/rrror.html',[ "error_code" => '404 Hoard not found!', "error_description" => "Well, we searched far and wide for `" . $SURI . "` but somehow it must have gotten lost... Sorry!", "post" => $post ]); 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]); die(); } echo $twig->render('post_types/directory.html', [ "post" => $post, "subposts" => $adapter->get_subposts_by_path($SURI) ]); } elseif($post['post_metadata']['type'] == 'text/markdown') { echo $twig->render('post_types/markdown.html', [ "post" => $post ]); } elseif($post['post_metadata']['type'] == 'image') { echo $twig->render('post_types/image.html', [ "post" => $post ]); } } else { echo $twig->render('rrror.html',[ "error_code" => '404 Hoard not found!', "error_description" => "Well, we searched far and wide for `" . $SURI . "` but somehow it must have gotten lost... Sorry!" ]); } } generate_website($_SERVER['REQUEST_URI']); log_and_die($_SERVER['REQUEST_URI']); ?>