Simple Route

This commit is contained in:
2024-07-19 15:47:19 +02:00
parent 156d277e77
commit ab1df63788
156 changed files with 13136 additions and 0 deletions
+3
View File
@@ -0,0 +1,3 @@
Order deny,allow
Deny from all
Allow from 127.0.0.1
+28
View File
@@ -0,0 +1,28 @@
<?php
class Auth
{
public static function hasPermission(string $required)
{
$db = Database::getInstance();
if (!isset($_SERVER["HTTP_TOKEN"])) return false;
$token = $_SERVER["HTTP_TOKEN"];
$query =
"SELECT
UserPermissions.Permission
FROM
UserPermissions, Users
WHERE
Users.ID = UserPermissions.fkUserID AND
Users.Token = :TOKEN";
$stmt = $db->prepare($query);
$stmt->bindValue(":TOKEN", $token);
$stmt->execute();
$perms = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
return in_array($required, $perms);
}
}
+7
View File
@@ -0,0 +1,7 @@
<?php
spl_autoload_register(function ($name) {
$_name = lcfirst($name);
require_once __DIR__ . "/$_name/$_name.php";
});
+44
View File
@@ -0,0 +1,44 @@
<?php
class Config
{
private static array $instances = [];
private array $app;
private array $database;
protected function __construct()
{
$this->app = require_once __DIR__ . "/../../config/app.php";
$this->database = require_once __DIR__ . "/../../config/database.php";
}
protected function __clone()
{
}
public function __wakeup()
{
throw new \Exception("Cannot unserialize a singleton.");
}
private static function getInstance(): Config
{
$cls = static::class;
if (!isset(self::$instances[$cls])) {
self::$instances[$cls] = new static();
}
return self::$instances[$cls];
}
public static function getBasePath()
{
return Config::getInstance()->app["basePath"];
}
public static function getDatabase()
{
return Config::getInstance()->database;
}
}
+32
View File
@@ -0,0 +1,32 @@
<?php
class Database extends PDO
{
private static array $instances = [];
protected function __construct($dsn, $username = null, $password = null, array $options = null)
{
parent::__construct($dsn, $username, $password, $options);
}
public static function getInstance(): Database
{
$cls = static::class;
if (!isset(self::$instances[$cls])) {
$dataAccess = Config::getDatabase();
self::$instances[$cls] = new static(
"mysql:host={$dataAccess["host"]};dbname={$dataAccess["database"]};charset={$dataAccess["charset"]}",
$dataAccess["user"],
$dataAccess["passwd"],
[
PDO::ATTR_PERSISTENT => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
]
);
}
return self::$instances[$cls];
}
}
+22
View File
@@ -0,0 +1,22 @@
<?php
class Headers
{
public static function json()
{
header('Content-Type: text/html; charset=utf-8');
header("Content-Type: text/json");
}
public static function html()
{
header('Content-Type: text/html; charset=utf-8');
}
public static function redirect(string $newUrl, bool $permanent = FALSE)
{
header('Location: ' . $newUrl, true, $permanent ? 301 : 303);
exit();
}
}
+11
View File
@@ -0,0 +1,11 @@
<?php
class PathParams
{
public static function get()
{
$path = ltrim(str_replace(Config::getBasePath(), "", $_SERVER['REQUEST_URI']), "/");
$segs = explode("/", $path);
return $segs ? $segs : [];
}
}
+11
View File
@@ -0,0 +1,11 @@
<?php
class Response
{
public static function api($content, int $code = 200)
{
Headers::json();
http_response_code($code);
echo json_encode($content);
}
}