From bcb1d27e0e85299038af134ff027ff75d4bf1e58 Mon Sep 17 00:00:00 2001 From: Jacob Casper Date: Sun, 26 Jun 2022 14:29:29 -0500 Subject: [PATCH] Implement routing I want to have kind of a rest-like `/pic/{id}` structure, and so finally outgrew filesystem hierarchy based routing and needed to implement it in the app. --- README.md | 3 +++ app.php | 41 +++++++++++++++++++++++++++++++++++++++++ index.php | 52 +++++++++++++++------------------------------------- upload.php | 2 +- 4 files changed, 60 insertions(+), 38 deletions(-) create mode 100644 app.php diff --git a/README.md b/README.md index 20c3320..ac493ed 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,6 @@ Requirements: - php-imagick - php-xml - sqlite3 + +Run it behind something and `mod_rewrite` all requests to `index.php`. +Pinhole `style.css` and `rss.xml`. diff --git a/app.php b/app.php new file mode 100644 index 0000000..a3f2597 --- /dev/null +++ b/app.php @@ -0,0 +1,41 @@ + + + + + + + +
+Upload +query('SELECT + p.ID, p.TIMESTAMP, pi.MIME_TYPE, pi.PIC_B64, c.BODY + FROM posts p + JOIN pics pi on p.id = pi.post_id + JOIN comments c on p.id = c.post_id + ORDER BY p.TIMESTAMP DESC'); +while ($post = $posts->fetchArray()) { + $mime_type = $post['MIME_TYPE']; + $file_b64 = $post['PIC_B64']; + $post_id = $post['ID']; + $comment_text = $post['BODY']; + $img = new Imagick(); + $img->readImageBlob(base64_decode($file_b64)); + $img->setImageCompressionQuality(85); + $img->adaptiveResizeImage(480, 0); + $compressed_b64 = base64_encode($img->getImageBlob()); + + echo "
"; + + echo "$"; + echo "

{$comment_text}

"; + echo '
'; +} +?> +
+ + diff --git a/index.php b/index.php index c8f23f5..0928d93 100644 --- a/index.php +++ b/index.php @@ -1,41 +1,19 @@ - - - - - - - -
-Upload query('SELECT - p.ID, p.TIMESTAMP, pi.MIME_TYPE, pi.PIC_B64, c.BODY - FROM posts p - JOIN pics pi on p.id = pi.post_id - JOIN comments c on p.id = c.post_id - ORDER BY p.TIMESTAMP DESC'); -while ($post = $posts->fetchArray()) { - $mime_type = $post['MIME_TYPE']; - $file_b64 = $post['PIC_B64']; - $post_id = $post['ID']; - $comment_text = $post['BODY']; - $img = new Imagick(); - $img->readImageBlob(base64_decode($file_b64)); - $img->setImageCompressionQuality(85); - $img->adaptiveResizeImage(480, 0); - $compressed_b64 = base64_encode($img->getImageBlob()); - - echo "
"; +// May contain query params +$request_uri = explode('?', $_SERVER['REQUEST_URI'], 2); - echo "$"; - echo "

{$comment_text}

"; - echo '
'; +switch ($request_uri[0]) { + case '/': + require './app.php'; + break; + case '/upload': + require './upload.php'; + break; + case '/unauthorized': + require './unauthorized.php'; + break; + default: + header('HTTP/1.0 404 Not Found'); + die('404 Not Found'); } -?> -
- - diff --git a/upload.php b/upload.php index 65edac0..456cde7 100644 --- a/upload.php +++ b/upload.php @@ -76,7 +76,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { echo ' -
+
-- 2.20.1