false ]; } else { $post_data["found"] = true; $post_data["post_metadata"] = json_decode($post_data["post_metadata"]); } return $post_data; } function get_post_by_path($path) { global $sql; $path = chop($path, '/'); $qry = "SELECT * FROM posts WHERE post_path = ?"; $stmt = $sql->prepare($qry); $stmt->bind_param("s", $path); $stmt->execute(); $post_data = $stmt->get_result()->fetch_assoc(); return process_post_data($post_data); } function get_subposts($path) { global $sql; $path = chop($path, '/'); $path_depth = substr_count($path, "/"); $qry = "SELECT post_path, post_metadata FROM posts WHERE (post_path LIKE CONCAT(?,'/%')) AND post_path_depth = ? ORDER BY post_create_time DESC LIMIT 10"; $stmt = $sql->prepare($qry); $stmt->bind_param("si", $path, $path_depth); $stmt->execute(); $post_data = $stmt->get_result()->fetch_all(MYSQLI_ASSOC); $post_data = array_map('process_post_data', $post_data); return $post_data; } //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]); $twig->addExtension(new Twig\Extra\Markdown\MarkdownExtension()); use Twig\Extra\Markdown\DefaultMarkdown; use Twig\Extra\Markdown\MarkdownRuntime; use Twig\RuntimeLoader\RuntimeLoaderInterface; $twig->addRuntimeLoader(new class implements RuntimeLoaderInterface { public function load($class) { if (MarkdownRuntime::class === $class) { return new MarkdownRuntime(new DefaultMarkdown()); } } }); if($_SERVER['REQUEST_URI'] == '/') { echo $twig->render('root.html'); } elseif(preg_match('/^\/api\/posts(.*)$/', $_SERVER['REQUEST_URI'], $match)) { header('Content-Type: application/json'); echo json_encode(get_post_by_path($match[1])); } elseif(preg_match('/^\/api\/subposts(.*)$/', $_SERVER['REQUEST_URI'], $match)) { header('Content-Type: application/json'); echo json_encode(get_subposts($match[1])); } elseif(true) { $post = get_post_by_path($_SERVER['REQUEST_URI']); echo $twig->render('about.html', [ "post" => $post, "subposts" => get_subposts($_SERVER['REQUEST_URI']) ]); } elseif(preg_match('/^\/about(.html)?$/', $_SERVER['REQUEST_URI'])) { echo $twig->render('about.html'); } elseif(preg_match('/^\/gallery\/([^\?]+)/', $_SERVER['REQUEST_URI'])) { echo $twig->render('/gallery/gallery_entry.html', [ 'image_url' => '/static/banner/0.png', 'image_title' => 'Test!', 'image_desc' => 'A soft piece made by a dear friend', 'artist_name' => 'Doggonaut', 'artist_src_link' => 'https://twitter.com/doggonaut' ]); } else { echo $twig->render('rrror.html',[ "error_code" => '404 Hoard not found!', "error_description" => "Well, we searched far and wide for `" . $_SERVER['REQUEST_URI'] . "` but somehow it must have gotten lost... Sorry!" ]); } ?>