Basic Framework

This commit is contained in:
Kilian Hofmann 2024-07-13 17:22:53 +02:00
parent c1fffec124
commit 4d25387a47
22 changed files with 250 additions and 0 deletions

37
exam/.htaccess Normal file
View File

@ -0,0 +1,37 @@
RewriteEngine On
##
## You may need to uncomment the following line for some hosting environments,
## if you have installed to a subdirectory, enter the name here also.
##
RewriteBase /phpCourse/exam
##
## Black listed folders
##
RewriteRule ^app/.* index.php [L,NC]
RewriteRule ^config/.* index.php [L,NC]
RewriteRule ^vendor/.* index.php [L,NC]
RewriteRule ^routes/.* index.php [L,NC]
RewriteRule ^pages/.* index.php [L,NC]
##
## White listed folders
##
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !/api/docs/.*
RewriteCond %{REQUEST_FILENAME} !/assets/.*
RewriteRule !^index.php index.php [L,NC]
##
## Block all PHP files, except index
##
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} \.php$
RewriteRule !^index.php index.php [L,NC]
##
## Standard routes
##
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

5
exam/api/.htaccess Normal file
View File

@ -0,0 +1,5 @@
Order deny,allow
Deny From All
<FilesMatch "^$">
Allow From All
</FilesMatch>

2
exam/api/docs/.htaccess Normal file
View File

@ -0,0 +1,2 @@
Order deny,allow
Allow from All

1
exam/api/docs/index.html Normal file
View File

@ -0,0 +1 @@
DOCUMENTATION

4
exam/api/index.php Normal file
View File

@ -0,0 +1,4 @@
<?php
foreach (glob(__DIR__ . '/*/index.php') as $filename) {
require_once($filename);
}

15
exam/api/users/index.php Normal file
View File

@ -0,0 +1,15 @@
<?php
$method = $_SERVER['REQUEST_METHOD'];
switch ($method) {
case "GET":
return get();
default:
return Response::api("$method not supported", 500);
}
function get()
{
Response::api("GET USERS");
}

3
exam/app/.htaccess Normal file
View File

@ -0,0 +1,3 @@
Order deny,allow
Deny from all
Allow from 127.0.0.1

13
exam/app/app.php Normal file
View File

@ -0,0 +1,13 @@
<?php
ini_set("display_errors", 1);
ini_set("default_charset", "utf-8");
ini_set('session.cookie_httponly', 1);
ini_set('session.cookie_secure', 1);
ini_set('session.use_only_cookies', 1);
error_reporting(E_ALL);
require_once __DIR__ . "/../vendor/autoloader.php";
Headers::html();
require_once __DIR__ . "/../routes/routes.php";

3
exam/config/.htaccess Normal file
View File

@ -0,0 +1,3 @@
Order deny,allow
Deny from all
Allow from 127.0.0.1

5
exam/config/app.php Normal file
View File

@ -0,0 +1,5 @@
<?php
return [
"basePath" => "phpCourse/exam/",
];

3
exam/index.php Normal file
View File

@ -0,0 +1,3 @@
<?php
require_once "./app/app.php";

3
exam/pages/.htaccess Normal file
View File

@ -0,0 +1,3 @@
Order deny,allow
Deny from all
Allow from 127.0.0.1

23
exam/pages/404.php Normal file
View File

@ -0,0 +1,23 @@
<!doctype html>
<html lang="de">
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<title>
Page not Found
</title>
</head>
<body>
<div id="site-content" class="site-content">
<div class="p-5 mb-4 bg-light rounded-3">
<div class="container-fluid py-5">
<h1 class="display-5 fw-bold">404 Page not Found</h1>
<p class="col-md-8 fs-4">Page does not exist.</p>
</div>
</div>
</div>
</body>
</html>

23
exam/pages/500.php Normal file
View File

@ -0,0 +1,23 @@
<!doctype html>
<html lang="de">
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<title>
Server Error
</title>
</head>
<body>
<div id="site-content" class="site-content">
<div class="p-5 mb-4 bg-light rounded-3">
<div class="container-fluid py-5">
<h1 class="display-5 fw-bold">500 Internal Server Error</h1>
<pre class="col-md-8 fs-4"><?= $e->getMessage() ?></pre>
</div>
</div>
</div>
</body>
</html>

14
exam/pages/index.html Normal file
View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + TS</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>

3
exam/routes/.htaccess Normal file
View File

@ -0,0 +1,3 @@
Order deny,allow
Deny from all
Allow from 127.0.0.1

13
exam/routes/routes.php Normal file
View File

@ -0,0 +1,13 @@
<?php
$path = ltrim(str_replace(Config::getBasePath(), "", $_SERVER['REQUEST_URI']), "/");
try {
if (substr($path, 0, 3) === "api") {
require_once __DIR__ . "/../api/index.php";
} else {
require_once __DIR__ . "/../pages/index.html";
}
} catch (Exception $e) {
require_once __DIR__ . "/../pages/500.php";
}

3
exam/vendor/.htaccess vendored Normal file
View File

@ -0,0 +1,3 @@
Order deny,allow
Deny from all
Allow from 127.0.0.1

7
exam/vendor/autoloader.php vendored Normal file
View File

@ -0,0 +1,7 @@
<?php
spl_autoload_register(function ($name) {
$_name = lcfirst($name);
require_once __DIR__ . "/$_name/$_name.php";
});

37
exam/vendor/config/config.php vendored Normal file
View File

@ -0,0 +1,37 @@
<?php
class Config
{
private static $instances = [];
private array $app;
protected function __construct()
{
$this->app = require_once __DIR__ . "/../../config/app.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"];
}
}

22
exam/vendor/headers/headers.php vendored Normal file
View File

@ -0,0 +1,22 @@
<?php
class Headers
{
static function json()
{
header('Content-Type: text/html; charset=utf-8');
header("Content-Type: text/json");
}
static function html()
{
header('Content-Type: text/html; charset=utf-8');
}
static function redirect(string $newUrl, bool $permanent = FALSE)
{
header('Location: ' . $newUrl, true, $permanent ? 301 : 303);
exit();
}
}

11
exam/vendor/response/response.php vendored Normal file
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);
}
}