From 20db2301cb4d33f9712f9be7aa5c688c855ff194 Mon Sep 17 00:00:00 2001 From: Kilian Hofmann Date: Mon, 22 Jul 2024 03:34:56 +0200 Subject: [PATCH] Rewrites and Formatting --- exam/.htaccess | 23 ++++----------------- exam/api/Logout/Logout.php | 2 +- exam/api/index.php | 3 ++- exam/classes/.htaccess | 33 +++--------------------------- exam/classes/Database/Database.php | 3 ++- exam/classes/Request/Request.php | 2 +- exam/config/Config.php | 4 +++- exam/dist/.htaccess | 7 +++++++ exam/react/.htaccess | 3 +++ exam/routes/routes.php | 4 ++-- 10 files changed, 28 insertions(+), 56 deletions(-) create mode 100644 exam/dist/.htaccess create mode 100644 exam/react/.htaccess diff --git a/exam/.htaccess b/exam/.htaccess index 6f932f6..5d415df 100644 --- a/exam/.htaccess +++ b/exam/.htaccess @@ -1,36 +1,21 @@ 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 +RewriteBase /phpCourse/exam/ -## -## Black listed folders -## -RewriteRule ^phpCourse/exam/config/.* index.php [L,NC] -RewriteRule ^phpCourse/exam/vendor/.* index.php [L,NC] -RewriteRule ^phpCourse/exam/routes/.* index.php [L,NC] -RewriteRule ^phpCourse/exam/react/.* index.php [L,NC] - -## ## API routes -## RewriteCond %{REQUEST_FILENAME} /api/.* RewriteCond %{REQUEST_FILENAME} !/api/docs RewriteRule ^ api/index.php [L,NC,QSA] -## -## Standard routes -## +## Rewrite all routes other than the specified paths RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !/api/docs RewriteCond %{REQUEST_FILENAME} !/dist RewriteRule ^ dist [L,NC,QSA] +## Rewrite all files unless in the directories specified RewriteCond %{REQUEST_FILENAME} -f RewriteCond %{REQUEST_FILENAME} !/storage/.* RewriteCond %{REQUEST_FILENAME} !/dist/.* RewriteCond %{REQUEST_FILENAME} !/api/docs -RewriteRule ^ dist [L,NC] \ No newline at end of file +RewriteRule ^ dist [L,NC,QSA] \ No newline at end of file diff --git a/exam/api/Logout/Logout.php b/exam/api/Logout/Logout.php index 04f2806..0a483ef 100644 --- a/exam/api/Logout/Logout.php +++ b/exam/api/Logout/Logout.php @@ -3,7 +3,7 @@ namespace Api\Logout; use Khofmann\Api\Api; -use \Khofmann\Models\User\User; +use Khofmann\Models\User\User; use Khofmann\Request\Request; use Khofmann\Response\Response; diff --git a/exam/api/index.php b/exam/api/index.php index aff5527..48d4cf4 100644 --- a/exam/api/index.php +++ b/exam/api/index.php @@ -16,8 +16,9 @@ use Pecee\SimpleRouter\Handlers\EventHandler; use Pecee\SimpleRouter\Event\EventArgument; use Pecee\SimpleRouter\Route\ILoadableRoute; use Pecee\SimpleRouter\Route\IGroupRoute; +use Config\Config; // Router base path -$basePath = Config\Config::getBasePath() . "api"; +$basePath = Config::getBasePath() . "api"; // Router event handler for prepending base path $eventHandler = new EventHandler(); $eventHandler->register(EventHandler::EVENT_ADD_ROUTE, function (EventArgument $event) use ($basePath) { diff --git a/exam/classes/.htaccess b/exam/classes/.htaccess index 767f212..872a3ce 100644 --- a/exam/classes/.htaccess +++ b/exam/classes/.htaccess @@ -1,30 +1,3 @@ -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 ^phpCourse/exam/config/.* index.php [L,NC] -RewriteRule ^phpCourse/exam/vendor/.* index.php [L,NC] -RewriteRule ^phpCourse/exam/routes/.* index.php [L,NC] -RewriteRule ^phpCourse/exam/react/.* index.php [L,NC] - -## -## API routes -## -RewriteCond %{REQUEST_FILENAME} /api/.* -RewriteCond %{REQUEST_FILENAME} !/api/docs -RewriteRule ^ api/index.php [L,NC,QSA] - -## -## Standard routes -## -RewriteCond %{REQUEST_FILENAME} !-f -RewriteCond %{REQUEST_FILENAME} !/api/docs -RewriteCond %{REQUEST_FILENAME} !/dist -RewriteRule ^ dist [L,NC,QSA] \ No newline at end of file +Order deny,allow +Deny from all +Allow from 127.0.0.1 \ No newline at end of file diff --git a/exam/classes/Database/Database.php b/exam/classes/Database/Database.php index c531687..714ee92 100644 --- a/exam/classes/Database/Database.php +++ b/exam/classes/Database/Database.php @@ -3,6 +3,7 @@ namespace Khofmann\Database; use PDO; +use Config\Config; class Database extends PDO { @@ -17,7 +18,7 @@ class Database extends PDO { $cls = static::class; if (!isset(self::$instances[$cls])) { - $dataAccess = \Config\Config::getDatabase(); + $dataAccess = Config::getDatabase(); self::$instances[$cls] = new static( "mysql:host={$dataAccess["host"]};dbname={$dataAccess["database"]};charset={$dataAccess["charset"]}", $dataAccess["user"], diff --git a/exam/classes/Request/Request.php b/exam/classes/Request/Request.php index 3b5e3ee..f83b047 100644 --- a/exam/classes/Request/Request.php +++ b/exam/classes/Request/Request.php @@ -14,6 +14,6 @@ class Request public static function header(string $name, $defaultValue = null, bool $tryParse = true): ?string { - return request()->getHeader($name, $defaultValue, $tryParse); + return Request::request()->getHeader($name, $defaultValue, $tryParse); } } diff --git a/exam/config/Config.php b/exam/config/Config.php index 768abda..2d866e2 100644 --- a/exam/config/Config.php +++ b/exam/config/Config.php @@ -2,6 +2,8 @@ namespace Config; +use Exception; + class Config { private static array $instances = []; @@ -21,7 +23,7 @@ class Config public function __wakeup() { - throw new \Exception("Cannot unserialize a singleton."); + throw new Exception("Cannot unserialize a singleton."); } private static function getInstance(): Config diff --git a/exam/dist/.htaccess b/exam/dist/.htaccess new file mode 100644 index 0000000..aadaa05 --- /dev/null +++ b/exam/dist/.htaccess @@ -0,0 +1,7 @@ +RewriteEngine On + +RewriteBase /phpCourse/exam/dist/ + +## Frontend routing required all non files to be rewritten to the entry point +RewriteCond %{REQUEST_FILENAME} !-f +RewriteRule ^ index.html [L,NC,QSA] \ No newline at end of file diff --git a/exam/react/.htaccess b/exam/react/.htaccess new file mode 100644 index 0000000..872a3ce --- /dev/null +++ b/exam/react/.htaccess @@ -0,0 +1,3 @@ +Order deny,allow +Deny from all +Allow from 127.0.0.1 \ No newline at end of file diff --git a/exam/routes/routes.php b/exam/routes/routes.php index ef205a9..1c6038e 100644 --- a/exam/routes/routes.php +++ b/exam/routes/routes.php @@ -22,7 +22,7 @@ SimpleRouter::post("/register", [Api\Register\Register::class, "post"]); /* * Normal Auth routes */ -SimpleRouter::group(["middleware" => \Khofmann\Auth\Auth::class], function () { +SimpleRouter::group(["middleware" => Khofmann\Auth\Auth::class], function () { // Login SimpleRouter::post("/logout", [Api\Logout\Logout::class, "post"]); // Get any user @@ -33,7 +33,7 @@ SimpleRouter::group(["middleware" => \Khofmann\Auth\Auth::class], function () { /* * Admin Auth routes */ -SimpleRouter::group(["middleware" => \Khofmann\Auth\AdminAuth::class], function () { +SimpleRouter::group(["middleware" => Khofmann\Auth\AdminAuth::class], function () { // Update any user SimpleRouter::patch("/user/{id}", [Api\User\User::class, "patch"]); // Delete any user