id/index.php

74 lines
1.9 KiB
PHP
Raw Normal View History

2024-06-27 20:28:34 +01:00
<?php
2024-06-27 22:30:56 +01:00
2024-06-28 22:41:25 +01:00
$DOC_ROOT = $_SERVER['DOCUMENT_ROOT'];
2024-06-27 22:30:56 +01:00
// Includes
try {
2024-06-28 22:41:25 +01:00
require_once "config.php";
2024-06-27 22:30:56 +01:00
} catch (Error $e) {
echo "<b>Critical error:</b> " . $e->getMessage() . "<br />This isn't your fault. Please contact the developers.";
exit;
}
// Connect to database
try {
$pdo = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD, PDO_OPTIONS);
} catch (PDOException $e) {
echo "<b>Critical error:</b> " . $e->getMessage() . "<br />";
}
2024-06-27 22:30:56 +01:00
2024-06-28 22:41:25 +01:00
require_once 'strings/en.php'; // This ensures strings will fallback to English if one is missing.
require_once 'common/strings.php';
require_once 'common/account_utils.php';
require_once 'common/database.php';
2024-06-27 22:30:56 +01:00
// Starts the session
2024-06-28 22:41:25 +01:00
// TODO: write this to use the database to work across more than one server (e.g. don't use PHP sessions)
2024-06-27 22:30:56 +01:00
session_start();
2024-06-28 22:41:25 +01:00
$user = null;
if ($_SESSION['auth']) {
$user = get_user_by_id($_SESSION['id']);
}
2024-06-27 22:30:56 +01:00
$uri_string = $_SERVER['REQUEST_URI']; // `/foo/bar?bar=foo&foo=bar`
$uri_explode = explode('?', $uri_string);
2024-06-28 22:41:25 +01:00
$path_raw = $uri_explode[0]; // `/foo/bar`
$path = explode('/', $path_raw);
2024-06-27 22:30:56 +01:00
// Remove trailing slashes
2024-06-28 22:41:25 +01:00
if (str_ends_with($path_raw, '/') && $path_raw != '/') {
2024-06-27 22:30:56 +01:00
http_response_code(308);
2024-06-28 22:41:25 +01:00
header('Location: '.substr($path_raw,0, -1));
2024-06-27 22:30:56 +01:00
exit;
}
$routes = [
'' => function () { require 'views/home.php'; },
2024-06-28 22:41:25 +01:00
'api' => function () { require 'api.php'; /* Handoff further routing to API script. */ },
'profile' => function () {
global $path, $user, $profile_owner; // don't forget this lol
2024-06-27 22:30:56 +01:00
2024-06-28 22:41:25 +01:00
if (isset($path[2])) {
$profile_owner = $path[2];
$profile_owner = get_user_by_id($profile_owner);
} else {
$profile_owner = $user;
}
require 'views/profile.php';
},
];
2024-06-27 22:30:56 +01:00
2024-06-28 22:41:25 +01:00
if (array_key_exists($path[1], $routes)) {
$res = $routes[$path[1]]();
2024-06-27 22:30:56 +01:00
if ($res == 404) {
require "views/404.php";
}
} else {
require "views/404.php";
}