diff --git a/exam/.htaccess b/exam/.htaccess index 172d75e..767f212 100644 --- a/exam/.htaccess +++ b/exam/.htaccess @@ -12,24 +12,19 @@ RewriteBase /phpCourse/exam 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] ## -## White listed folders +## API routes ## -RewriteCond %{REQUEST_FILENAME} -f -RewriteCond %{REQUEST_FILENAME} !/api/docs/.* -RewriteCond %{REQUEST_FILENAME} !/pages/.* -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] +RewriteCond %{REQUEST_FILENAME} /api/.* +RewriteCond %{REQUEST_FILENAME} !/api/docs +RewriteRule ^ api/index.php [L,NC,QSA] ## ## Standard routes ## RewriteCond %{REQUEST_FILENAME} !-f -RewriteRule ^ index.php [L] \ No newline at end of file +RewriteCond %{REQUEST_FILENAME} !/api/docs +RewriteCond %{REQUEST_FILENAME} !/dist +RewriteRule ^ dist [L,NC,QSA] \ No newline at end of file diff --git a/exam/api/Auth/Auth.php b/exam/api/Auth/Auth.php new file mode 100644 index 0000000..4a3fb2b --- /dev/null +++ b/exam/api/Auth/Auth.php @@ -0,0 +1,21 @@ +getHeader("token"); + + //TODO: Auth user with token + + // If authentication failed + if ($request->token === null) { + response()->httpCode(401)->json(["message" => "Not Authorized"]); + } + } +} diff --git a/exam/api/Login/Login.php b/exam/api/Login/Login.php index 9eaa8b0..e0d43c2 100644 --- a/exam/api/Login/Login.php +++ b/exam/api/Login/Login.php @@ -7,5 +7,6 @@ class Login public function post() { echo "LOGIN HANDLER post"; + print_r(input()->all()); } } diff --git a/exam/api/Logout/Logout.php b/exam/api/Logout/Logout.php new file mode 100644 index 0000000..b325ee1 --- /dev/null +++ b/exam/api/Logout/Logout.php @@ -0,0 +1,12 @@ +all()); + } +} diff --git a/exam/api/docs/api.yaml b/exam/api/docs/api.yaml new file mode 100644 index 0000000..0d43bd8 --- /dev/null +++ b/exam/api/docs/api.yaml @@ -0,0 +1,73 @@ +openapi: 3.0.0 +info: + title: PHP Course Exam + version: 1.0.0 + contact: + name: Kilian Kurt Hofmann + email: khofmann@zedat.fu-berlin.de + description: PHP Course (ABV FU Berlin) 2024 Exam +paths: + /login: + summary: Login + description: Log in user + post: + summary: Login + description: Log in user + operationId: "" + responses: + "200": + description: Success + content: + application/json: + schema: + $ref: "#/components/schemas/BooleanResponse" + examples: + Success: + value: true + "401": + description: Invalid Username or Password + content: + application/json: + schema: + $ref: "#/components/schemas/ErrorResponse" + examples: + Invalid Username or Password: + value: { "message": "Invalid Username or Password" } + "404": + description: User not Found + content: + application/json: + schema: + $ref: "#/components/schemas/ErrorResponse" + examples: + User not Found: + value: { "message": "User not Found" } + tags: + - Login/Logout +externalDocs: + url: https://khofmann.userpage.fu-berlin.de/phpCourse/exam/api/docs/ +security: [] +servers: + - url: https://khofmann.userpage.fu-berlin.de/phpCourse/exam/api/ + description: "" + variables: {} +components: + links: {} + callbacks: {} + schemas: + BooleanResponse: + type: boolean + ErrorResponse: + type: object + properties: + message: + type: string + securitySchemes: + BasicAuth: + type: apiKey + name: token + in: header +tags: + - name: Login/Logout + - name: Users + - name: Posts diff --git a/exam/api/docs/index.html b/exam/api/docs/index.html new file mode 100644 index 0000000..1de6f28 --- /dev/null +++ b/exam/api/docs/index.html @@ -0,0 +1,328 @@ + + + + + + PHP Course Exam + + + + + + + + + +

PHP Course Exam (1.0.0)

Download OpenAPI specification:Download

Kilian Kurt Hofmann: khofmann@zedat.fu-berlin.de

PHP Course (ABV FU Berlin) 2024 Exam

+

Login/Logout

Login

Log in user

+

Responses

Response samples

Content type
application/json
"tr"

Users

Posts

+ + + + diff --git a/exam/index.php b/exam/api/index.php similarity index 78% rename from exam/index.php rename to exam/api/index.php index 514333b..de20644 100644 --- a/exam/index.php +++ b/exam/api/index.php @@ -2,14 +2,14 @@ // Enable Errors 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); +ini_set("session.cookie_httponly", 1); +ini_set("session.cookie_secure", 1); +ini_set("session.use_only_cookies", 1); error_reporting(E_ALL); // Load composer Autoloader -require_once __DIR__ . "/vendor/autoload.php"; +require_once __DIR__ . "/../vendor/autoload.php"; // Helpers -require_once __DIR__ . "/utils/helpers.php"; +require_once __DIR__ . "/../utils/helpers.php"; // Namespaces use Pecee\SimpleRouter\SimpleRouter; use Pecee\SimpleRouter\Handlers\EventHandler; @@ -17,7 +17,7 @@ use Pecee\SimpleRouter\Event\EventArgument; use Pecee\SimpleRouter\Route\ILoadableRoute; use Pecee\SimpleRouter\Route\IGroupRoute; // Router base path -$basePath = '/phpCourse/exam/'; +$basePath = Config\Config::getBasePath() . "/api"; // Router event handler for prepending base path $eventHandler = new EventHandler(); $eventHandler->register(EventHandler::EVENT_ADD_ROUTE, function (EventArgument $event) use ($basePath) { @@ -39,6 +39,6 @@ $eventHandler->register(EventHandler::EVENT_ADD_ROUTE, function (EventArgument $ // Add the handler SimpleRouter::addEventHandler($eventHandler); // Load external routes file -require_once __DIR__ . '/routes/routes.php'; +require_once __DIR__ . "/../routes/routes.php"; // Start the routing SimpleRouter::start(); diff --git a/exam/composer.json b/exam/composer.json index e9e0252..b05f569 100644 --- a/exam/composer.json +++ b/exam/composer.json @@ -5,7 +5,7 @@ "autoload": { "psr-4": { "Api\\": "api/", - "Pages\\": "pages/" + "Config\\": "config/" } } } diff --git a/exam/config/app.php b/exam/config/app.php new file mode 100644 index 0000000..5b59d04 --- /dev/null +++ b/exam/config/app.php @@ -0,0 +1,5 @@ + "phpCourse/exam/", +]; diff --git a/exam/config/config.php b/exam/config/config.php new file mode 100644 index 0000000..3e3c431 --- /dev/null +++ b/exam/config/config.php @@ -0,0 +1,46 @@ +app = require_once __DIR__ . "/app.php"; + $this->database = require_once __DIR__ . "/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; + } +} diff --git a/exam/config/database.php b/exam/config/database.php new file mode 100644 index 0000000..c70d477 --- /dev/null +++ b/exam/config/database.php @@ -0,0 +1,9 @@ + "usersql.zedat.fu-berlin.de", + "user" => "khofmann-sql", + "passwd" => "xz8c7m7p", + "database" => "khofmann-db1", + "charset" => "utf8", +]; diff --git a/exam/dist/index.php b/exam/dist/index.php new file mode 100644 index 0000000..9b6c0d4 --- /dev/null +++ b/exam/dist/index.php @@ -0,0 +1 @@ +

Redirect to React

\ No newline at end of file diff --git a/exam/pages/NotFound/NotFound.php b/exam/pages/NotFound/NotFound.php deleted file mode 100644 index a795723..0000000 --- a/exam/pages/NotFound/NotFound.php +++ /dev/null @@ -1,13 +0,0 @@ -Not Found - END; - } -} diff --git a/exam/routes/routes.php b/exam/routes/routes.php index 1e83cfd..d7b5a6c 100644 --- a/exam/routes/routes.php +++ b/exam/routes/routes.php @@ -2,19 +2,20 @@ // Namespaces use Pecee\SimpleRouter\SimpleRouter; use Pecee\Http\Request; -// Error handling redirects +// Error handling SimpleRouter::error(function (Request $request, \Exception $exception) { - switch ($exception->getCode()) { - // Page not found - case 404: - redirect('/phpCourse/exam/not-found', 404); - } + response()->httpCode($exception->getCode())->json(["message" => $exception->getMessage()]); }); -//404 -SimpleRouter::get('/not-found', [Pages\NotFound\NotFound::class, "render"]); -//Index -SimpleRouter::get('/', function () { - return 'Hello world'; +// Index +SimpleRouter::all("/", function () { + redirect("docs", 301); +}); +// Login/Logout +SimpleRouter::post("/login", [Api\Login\Login::class, "post"]); +SimpleRouter::post("/logout", [Api\Logout\Logout::class, "post"]); +// User +SimpleRouter::group(["middleware" => \Api\Auth\Auth::class], function () { + SimpleRouter::get("/user/{id}", function ($userID) { + echo "USER ENDP $userID"; + }); }); -//API -SimpleRouter::get("/api/login", [Api\Login\Login::class, 'post']);