feat: added first draft of Database based website rendering
Some checks failed
/ phplint (push) Failing after 3s

This commit is contained in:
xaseiresh 2023-10-22 12:59:20 +02:00
parent 5bcba0b689
commit fcc42bde20
4 changed files with 171 additions and 23 deletions

View file

@ -5,18 +5,59 @@ require_once 'vendor/autoload.php';
$sql = mysqli_connect('mysql', 'root', 'example', 'dragon_fire');
function process_post_data($post_data) {
if($post_data == null) {
$post_data = [
"found" => 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();
// $result = $stmt->get_result();
$post_data = $stmt->get_result()->fetch_assoc();
return $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)
@ -51,6 +92,19 @@ if($_SERVER['REQUEST_URI'] == '/') {
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'])) {