From 7be1668f006b145614580b45c6d5d7662c6e7110 Mon Sep 17 00:00:00 2001 From: William Date: Tue, 31 Jan 2023 19:06:53 +0100 Subject: [PATCH] AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA --- Dockerfile | 1 - app/WillySoft/{Http => }/ErrorHandler.php | 2 +- app/WillySoft/{Http => }/Route.php | 4 +- public/index.php | 130 +++++++++++++++++++++- routes/start.php | 33 ------ routes/willychat.php | 96 ---------------- views/errors/404.php | 19 +++- views/pages/blog/home.php | 5 + views/pages/blog/test.php | 8 ++ views/templates/blog/footer.php | 3 + views/templates/blog/header.php | 65 +++++++++++ 11 files changed, 226 insertions(+), 140 deletions(-) rename app/WillySoft/{Http => }/ErrorHandler.php (97%) rename app/WillySoft/{Http => }/Route.php (95%) delete mode 100644 routes/start.php delete mode 100644 routes/willychat.php create mode 100644 views/pages/blog/home.php create mode 100644 views/pages/blog/test.php create mode 100644 views/templates/blog/footer.php create mode 100644 views/templates/blog/header.php diff --git a/Dockerfile b/Dockerfile index 03c2839..50e3d6d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,7 +8,6 @@ RUN apk update && \ chown -R www:www /www COPY ./docker/nginx.conf /etc/nginx/nginx.conf -#COPY ./ /www COPY ./docker/run.sh /opt/run.sh RUN chmod +x /opt/run.sh diff --git a/app/WillySoft/Http/ErrorHandler.php b/app/WillySoft/ErrorHandler.php similarity index 97% rename from app/WillySoft/Http/ErrorHandler.php rename to app/WillySoft/ErrorHandler.php index 85a5c6d..3a5a243 100644 --- a/app/WillySoft/Http/ErrorHandler.php +++ b/app/WillySoft/ErrorHandler.php @@ -1,6 +1,6 @@ $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 diff --git a/routes/start.php b/routes/start.php deleted file mode 100644 index 5d7b155..0000000 --- a/routes/start.php +++ /dev/null @@ -1,33 +0,0 @@ - $error_messages]); - } else { - view('errors/500', ['error_messages' => []]); - } -}); -// if config loads successfully use that value instead -$debug = config('debug'); -unset($debug); - -Route::get('/', fn() => view('pages/home')); - -Route::group(function() { - require __DIR__ . '/willychat.php'; -}); - -Route::get('/test/$whatever?', function($whatever = 'Default Value') { - echo htmlspecialchars($whatever); -}); - -Route::get('/error', fn() => 1 / 0); - -// since no route was matched we show a page not found error -http_response_code(404); -view('errors/404'); \ No newline at end of file diff --git a/routes/willychat.php b/routes/willychat.php deleted file mode 100644 index aeca0fe..0000000 --- a/routes/willychat.php +++ /dev/null @@ -1,96 +0,0 @@ - $nick_max_chars) { - return 'Your nickname is too long.'; - } - - $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(WillyChat::$messages) > 100) { - array_pop(WillyChat::$messages); - } - - array_unshift(WillyChat::$messages, [ - 'nick' => $nick, - 'date' => time(), - 'text' => $text - ]); - - file_put_contents(WillyChat::$data_path, - json_encode( - WillyChat::$messages - ) - ); - } - $just_sent_message = true; - } - - view('pages/willychat/index', [ - 'nick' => $nick, - 'just_sent_message' => $just_sent_message, - 'errmsg' => $errmsg - ]); -}); - -Route::get('/willychat/messages', function() { - view('pages/willychat/messages', [ - 'messages' => WillyChat::$messages - ]); -}); - -Route::get('/willychat/sync', function() { - json_response( - hash('crc32', serialize(WillyChat::$messages)) - ); -}); \ No newline at end of file diff --git a/views/errors/404.php b/views/errors/404.php index 50dfa22..75fb13c 100644 --- a/views/errors/404.php +++ b/views/errors/404.php @@ -1,5 +1,14 @@ - 'Page not found'])?> -

Page not found

-

Sorry the page you requested could not be found on this server

-Go home - \ No newline at end of file + + + + + + + Page not found + + +

Page not found

+

Sorry the page you requested could not be found on this server

+ Go home + + \ No newline at end of file diff --git a/views/pages/blog/home.php b/views/pages/blog/home.php new file mode 100644 index 0000000..c25d3c6 --- /dev/null +++ b/views/pages/blog/home.php @@ -0,0 +1,5 @@ + +

What is there to life?

+This is a link +

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam commodo dignissim orci faucibus pellentesque. Cras sit amet leo libero. Morbi at maximus arcu, ac consectetur nunc. Nunc pharetra semper augue sed aliquet. Nunc pretium lorem quis nulla interdum, vestibulum sollicitudin lectus eleifend. Duis leo erat, consectetur nec convallis vitae, faucibus vel ipsum. Donec sed lectus ut lorem pellentesque posuere. Vivamus et iaculis ante, nec elementum sapien. Donec semper magna a nisi porttitor varius. Cras consequat pharetra sapien interdum finibus.

+ \ No newline at end of file diff --git a/views/pages/blog/test.php b/views/pages/blog/test.php new file mode 100644 index 0000000..7523174 --- /dev/null +++ b/views/pages/blog/test.php @@ -0,0 +1,8 @@ + +

This is test page

+

Or is it? Maybe. I'm just trying to fill this with some text right now using the noise from my brain but I'm not really that good at it.. FUCK

+

H2

+

H3

+
This is a code block
+go back + \ No newline at end of file diff --git a/views/templates/blog/footer.php b/views/templates/blog/footer.php new file mode 100644 index 0000000..d378fbe --- /dev/null +++ b/views/templates/blog/footer.php @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/views/templates/blog/header.php b/views/templates/blog/header.php new file mode 100644 index 0000000..3b9fd2c --- /dev/null +++ b/views/templates/blog/header.php @@ -0,0 +1,65 @@ + + + + + + + Blog + + + + + +
+ +
+ +
+
+

MY FUCKING BLOG

+

Wasting your limited time by reading this

+
+
+ +
\ No newline at end of file