AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

This commit is contained in:
William 2023-01-31 19:06:53 +01:00
parent f778c1f4a2
commit 7be1668f00
11 changed files with 226 additions and 140 deletions

View File

@ -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

View File

@ -1,6 +1,6 @@
<?php
namespace WillySoft\Http;
namespace WillySoft;
abstract class ErrorHandler {
private static array $error_messages = [];

View File

@ -1,6 +1,6 @@
<?php
namespace WillySoft\Http;
namespace WillySoft;
abstract class Route {
static array $groups = [
@ -19,7 +19,6 @@ abstract class Route {
));
}
$request_path_parts = $cache;
$path_parts = explode('/', $path);
$callback_args = [];
for ($i=0; $i < count($path_parts); $i++) {
@ -59,6 +58,7 @@ abstract class Route {
static function patch(string $path, callable $callback) { self::match('patch', $path, $callback); }
static function delete(string $path, callable $callback) { self::match('delete', $path, $callback); }
static function options(string $path, callable $callback) { self::match('options', $path, $callback); }
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); }
static function middleware(callable|array $middlewares) {

View File

@ -1,4 +1,14 @@
<?php
/**
* _____
* / \
* | () () |
* \ ^ /
* |||||
* |||||
*
* Tread carefully!
*/
// PSR-4 like autoloader
spl_autoload_register(
@ -76,5 +86,121 @@ function url(string $url, bool $full = false): string
return isset($_SERVER['HTTPS']) ? 'https' : 'http' . '://' . $_SERVER['HTTP_HOST'] . $dir . $url;
}
// now lets get routing!
require __DIR__ . '/../routes/start.php';
//-----------------------------------------------------
// 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');

View File

@ -1,33 +0,0 @@
<?php
use WillySoft\Http\ErrorHandler;
use WillySoft\Http\Route;
// enable debug here so we display config parse errors to the user
$debug = true;
ErrorHandler::register(function($error_messages) use (&$debug) {
if ($debug) {
view('errors/500', ['error_messages' => $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');

View File

@ -1,96 +0,0 @@
<?php
use WillySoft\Http\Route;
abstract class WillyChat {
static string $data_path;
static array $messages;
}
Route::middleware(function() {
WillyChat::$data_path = '/dev/shm/database.json';
if (!file_exists(WillyChat::$data_path)) {
file_put_contents(
WillyChat::$data_path,
json_encode([])
);
}
WillyChat::$messages = json_decode(
file_get_contents(WillyChat::$data_path),
true
);
});
Route::match('get|post','/willychat/', function() {
$default_nick = 'Willy';
$nick = $default_nick;
$just_sent_message = false;
$errmsg = false;
if (!empty($_POST)) {
$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.';
}
$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))
);
});

View File

@ -1,5 +1,14 @@
<?=view('templates/header', ['title' => 'Page not found'])?>
<h1>Page not found</h1>
<p>Sorry the page you requested could not be found on this server</p>
<a href="<?=url('/')?>">Go home</a>
<?=view('templates/footer')?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Page not found</title>
</head>
<body>
<h1>Page not found</h1>
<p>Sorry the page you requested could not be found on this server</p>
<a href="<?=url('/')?>">Go home</a>
</body>
</html>

View File

@ -0,0 +1,5 @@
<?=view('templates/blog/header')?>
<h1>What is there to life?</h1>
<a href="<?=url('./test')?>">This is a link</a>
<p>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. </p>
<?=view('templates/blog/footer')?>

View File

@ -0,0 +1,8 @@
<?=view('templates/blog/header')?>
<h1>This is test page</h1>
<p>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</p>
<h2>H2</h2>
<h3>H3</h3>
<pre><code>This is a code block</code></pre>
<a href="./">go back</a>
<?=view('templates/blog/footer')?>

View File

@ -0,0 +1,3 @@
</main>
</body>
</html>

View File

@ -0,0 +1,65 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Blog</title>
</head>
<body>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, avenir next, avenir, segoe ui, helvetica neue, helvetica, Cantarell, Ubuntu, roboto, noto, arial, sans-serif;
color: #fff;
background: #181818;
margin: 0;
line-height: 1.5;
}
a {
color: #457de6;
}
.container {
max-width: 50rem;
margin: auto;
padding-left: 1rem;
padding-right: 1rem;
}
main {
animation: enter .3s;
}
@keyframes enter {
from { opacity: 0; }
to { opacity: 1; }
}
</style>
<header style="
padding-top: 1rem;
padding-bottom: 1rem;
background: #181818;
">
<nav class="container">
<a href="./">MY FUCKING BLOG</a>
</nav>
</header>
<div style="
background: #000;
padding-top: 2rem;
padding-bottom: 2rem;
">
<div class="container">
<h1 style="
color: transparent;
background: linear-gradient(90deg, rgba(119,119,255,1) 0%, rgba(44,170,255,1) 65%, rgba(119,119,255,1) 100%);
background-clip: text;
-webkit-background-clip: text;
font-size: 3rem;
max-width: max-content;
">MY FUCKING BLOG</h1>
<p>Wasting your limited time by reading this</p>
</div>
</div>
<main class="container">