From 5491c98563901c664505e9a6e7afc74dddb991b8 Mon Sep 17 00:00:00 2001 From: William Date: Fri, 3 Feb 2023 12:00:13 +0100 Subject: [PATCH] OAOAUOEUAOEUAOUEOAOOOOGAABOOOGAOGABOGA --- app/App/Config.php | 49 ++++++ app/App/Controller/ChatController.php | 94 +++++++++++ app/App/Controller/DefaultController.php | 21 +++ app/App/Database.php | 21 +++ app/WillySoft/ErrorHandler.php | 44 +++--- app/WillySoft/Route.php | 10 -- config/default.config.php | 4 +- public/index.php | 155 +------------------ routes/start.php | 37 +++++ views/errors/500.php | 25 +-- views/pages/{willychat => chat}/index.php | 2 +- views/pages/{willychat => chat}/messages.php | 0 views/templates/footer.php | 2 +- 13 files changed, 255 insertions(+), 209 deletions(-) create mode 100644 app/App/Config.php create mode 100644 app/App/Controller/ChatController.php create mode 100644 app/App/Controller/DefaultController.php create mode 100644 app/App/Database.php create mode 100644 routes/start.php rename views/pages/{willychat => chat}/index.php (93%) rename views/pages/{willychat => chat}/messages.php (100%) diff --git a/app/App/Config.php b/app/App/Config.php new file mode 100644 index 0000000..e6b8cbc --- /dev/null +++ b/app/App/Config.php @@ -0,0 +1,49 @@ + $override_value) { + if (!array_key_exists($override_key, self::$config)) { + trigger_error( + 'Failed to validate config: ' . + "Undefined option '$override_key'" + , E_USER_ERROR); + } + if (gettype($override_value) !== gettype(self::$config[$override_key])) { + trigger_error( + 'Failed to validate config: ' . + "Type mismatch in config file: '$override_key' should be of type " + . gettype(self::$config[$override_key]) . + ' not ' + . gettype($override_value) + , E_USER_ERROR); + } + self::$config[$override_key] = $override_value; + } + return self::$config[$key]; + } +} \ No newline at end of file diff --git a/app/App/Controller/ChatController.php b/app/App/Controller/ChatController.php new file mode 100644 index 0000000..9881982 --- /dev/null +++ b/app/App/Controller/ChatController.php @@ -0,0 +1,94 @@ + 'Willy', + 'just_sent_message' => false, + 'errmsg' => false + ]); + } + + $nick = filter_input(INPUT_POST, 'nick'); + $text = filter_input(INPUT_POST, 'text'); + $errmsg = (function() use (&$text, &$nick): string|false { + if (empty(trim($nick, ' '))) { + return 'You must choose a nickname.'; + } + + $nick_max_chars = 128; + if (strlen($nick) > $nick_max_chars) { + return 'Your nickname is TOO LONG! ' . strlen($nick) . ' out of ' . $nick_max_chars. ' characters.'; + } + + $text_max_chars = 4096; + if (strlen($text) > $text_max_chars) { + return 'Your message is TOO LONG!!!! ' . strlen($text) . ' out of ' . $text_max_chars . ' characters.'; + } + + if (empty(trim($text, ' '))) { + return 'Message body cannot be empty.'; + } + + return false; + })(); + + if (!$errmsg) { + if (count(self::$messages) > 100) { + array_pop(self::$messages); + } + + array_unshift(self::$messages, [ + 'nick' => $nick, + 'date' => time(), + 'text' => $text + ]); + + file_put_contents(Config::get('path_to_chat_json_file'), + json_encode( + self::$messages + ) + ); + } + view('pages/chat/index', [ + 'nick' => $nick, + 'just_sent_message' => true, + 'errmsg' => $errmsg + ]); + } + + static function messages() + { + view('pages/chat/messages', ['messages' => self::$messages]); + } + + static function sync() + { + json_response(hash('crc32', serialize(self::$messages))); + } +} \ No newline at end of file diff --git a/app/App/Controller/DefaultController.php b/app/App/Controller/DefaultController.php new file mode 100644 index 0000000..cdf6337 --- /dev/null +++ b/app/App/Controller/DefaultController.php @@ -0,0 +1,21 @@ +Error[$errno]: $errstr in $errfile at line $errline"; + self::oof($callback, "Error[$errno]: $errstr in $errfile at line $errline"); }); - set_exception_handler(function($exception) { + set_exception_handler(function($exception) use (&$callback) { error_log("Uncaught Exception: $exception\n"); - self::$error_messages[] = "Uncaught Exception: {$exception}"; - }); - - register_shutdown_function(function() use(&$callback) { - if (!self::$error_messages) { - return; - } - - ob_end_clean(); - - http_response_code(500); - header_remove(); - - $callback(self::$error_messages); + self::oof($callback, "Uncaught Exception: {$exception}"); }); } + + private static function oof(callable $callback, string $error_message) + { + ob_end_clean(); + + http_response_code(500); + header_remove(); + + $callback($error_message); + die(); + } } \ No newline at end of file diff --git a/app/WillySoft/Route.php b/app/WillySoft/Route.php index 4ae84b5..ef355bb 100644 --- a/app/WillySoft/Route.php +++ b/app/WillySoft/Route.php @@ -10,11 +10,6 @@ abstract class Route static string $prefix = ''; - /** - * Check to see if the methods and path satisfy the request. - * and do some magic to retrieve the parameters before calling - * the callback passing those as arguments - */ static function match(string $methods, string $path, callable $callback) { if (!in_array($_SERVER['REQUEST_METHOD'], @@ -70,10 +65,6 @@ abstract class Route static function form(string $path, callable $callback) { self::match('get|post', $path, $callback); } static function any(string $path, callable $callback) { self::match('get|post|put|patch|delete|options', $path, $callback); } - /** - * Require that a callable or an array of callables have to run - * if a route is matched within the current or all child groups - */ static function middleware(callable|array $middlewares) { if (!is_array($middlewares)) { @@ -86,7 +77,6 @@ abstract class Route } } - static function group(string $prefix = '', ?callable $callback = null) { if (!str_starts_with($_SERVER['REQUEST_URI'], self::$prefix . $prefix)) { diff --git a/config/default.config.php b/config/default.config.php index 993fff8..0b604c7 100644 --- a/config/default.config.php +++ b/config/default.config.php @@ -1,8 +1,8 @@ false + 'path_to_chat_json_file' => '/dev/shm/database.json' ]; \ No newline at end of file diff --git a/public/index.php b/public/index.php index 2dd17c1..2230b20 100644 --- a/public/index.php +++ b/public/index.php @@ -1,15 +1,4 @@ $override_value) { - if (!array_key_exists($override_key, $config)) { - trigger_error('Undefined key in config file', E_USER_ERROR); - } - if (gettype($override_value) !== gettype($config[$override_key])) { - trigger_error('Type mismatch in config file', E_USER_ERROR); - } - $config[$override_key] = $override_value; - } - return $config[$key]; -} - /** * Helper for evaluating/including views */ @@ -86,121 +50,4 @@ function url(string $url, bool $full = false): string return isset($_SERVER['HTTPS']) ? 'https' : 'http' . '://' . $_SERVER['HTTP_HOST'] . $dir . $url; } -//----------------------------------------------------- -// Let's get routing! -//----------------------------------------------------- - -use WillySoft\ErrorHandler; -use WillySoft\Route; - -Route::get('/', function() { - view('pages/home'); -}); - -ErrorHandler::register(function($error_messages) { - if (config('debug')) { - view('errors/500', ['error_messages' => $error_messages]); - } else { - view('errors/500', ['error_messages' => []]); - } -}); - -Route::group(function() { - Route::get('/', fn() => view('pages/home')); - Route::get('/echo/$text?', fn($text= 'You sent nothing...') => json_response($text)); - Route::get('/error', fn() => 1 / 0); -}); - -Route::group(function() { - if (!config('debug')) { - return; - } - Route::get('/blog/', fn() => view('pages/blog/home')); - Route::get('/blog/test', fn() => view('pages/blog/test')); -}); - -Route::group(function() { - $data_path = '/dev/shm/database.json'; - $messages = []; - - Route::middleware(function() use (&$data_path, &$messages) { - if (!file_exists($data_path)) { - file_put_contents( - $data_path, - json_encode([]) - ); - } - $messages = json_decode( - file_get_contents($data_path), - true - ); - }); - - Route::form('/willychat/', function() use (&$data_path, &$messages) { - if (empty($_POST)) { - return view('pages/willychat/index', [ - 'nick' => 'Willy', - 'just_sent_message' => false, - 'errmsg' => false - ]); - } - - $nick = filter_input(INPUT_POST, 'nick'); - $text = filter_input(INPUT_POST, 'text'); - $errmsg = (function() use (&$text, &$nick): string|false { - if (empty(trim($nick, ' '))) { - return 'You must choose a nickname.'; - } - - $nick_max_chars = 128; - if (strlen($nick) > $nick_max_chars) { - return 'Your nickname is TOO LONG! ' . strlen($nick) . ' out of ' . $nick_max_chars. ' characters.'; - } - - $text_max_chars = 4096; - if (strlen($text) > $text_max_chars) { - return 'Your message is TOO LONG!!!! ' . strlen($text) . ' out of ' . $text_max_chars . ' characters.'; - } - - if (empty(trim($text, ' '))) { - return 'Message body cannot be empty.'; - } - - return false; - })(); - - if (!$errmsg) { - if (count($messages) > 100) { - array_pop($messages); - } - - array_unshift($messages, [ - 'nick' => $nick, - 'date' => time(), - 'text' => $text - ]); - - file_put_contents($data_path, - json_encode( - $messages - ) - ); - } - view('pages/willychat/index', [ - 'nick' => $nick, - 'just_sent_message' => true, - 'errmsg' => $errmsg - ]); - }); - - Route::get('/willychat/messages', function() use (&$messages) { - view('pages/willychat/messages', ['messages' => $messages]); - }); - - Route::get('/willychat/sync', function() use (&$messages) { - json_response(hash('crc32', serialize($messages))); - }); -}); - -http_response_code(404); -view('errors/404'); \ No newline at end of file +require '../routes/start.php'; \ No newline at end of file diff --git a/routes/start.php b/routes/start.php new file mode 100644 index 0000000..8d979b9 --- /dev/null +++ b/routes/start.php @@ -0,0 +1,37 @@ + $error_message + ]); +}); + +use App\Controller\DefaultController; + +App::get('/', + DefaultController::index(...)); +App::get('/echo/$text?', + DefaultController::echo(...)); +App::get('/error', + DefaultController::error(...)); + +use App\Controller\ChatController; + +App::group('/chat', function() { + App::form('/', + ChatController::index(...)); + App::get('/messages', + ChatController::messages(...)); +}); + +http_response_code(404); +view('errors/404'); \ No newline at end of file diff --git a/views/errors/500.php b/views/errors/500.php index 1a10269..f03451f 100644 --- a/views/errors/500.php +++ b/views/errors/500.php @@ -2,27 +2,14 @@ + - 500 Internal Server Error + Internal Server Error - -

Error!!1 (×﹏×)

- -
- +

Internal Server Error (×﹏×)

+ +
+ \ No newline at end of file diff --git a/views/pages/willychat/index.php b/views/pages/chat/index.php similarity index 93% rename from views/pages/willychat/index.php rename to views/pages/chat/index.php index 4807742..0cd82d2 100644 --- a/views/pages/willychat/index.php +++ b/views/pages/chat/index.php @@ -68,7 +68,7 @@
- +