aboutsummaryrefslogtreecommitdiff
path: root/src/Handlers
diff options
context:
space:
mode:
Diffstat (limited to 'src/Handlers')
-rw-r--r--src/Handlers/ApiTagsHandler.php16
-rw-r--r--src/Handlers/Index.php20
-rw-r--r--src/Handlers/JsonAPIHandler.php18
-rw-r--r--src/Handlers/JsonAPIResult.php24
-rw-r--r--src/Handlers/MethodNotAllowedHandler.php14
-rw-r--r--src/Handlers/NewNote.php40
-rw-r--r--src/Handlers/NotFoundHandler.php15
-rw-r--r--src/Handlers/NoteHandler.php39
-rw-r--r--src/Handlers/Search.php33
9 files changed, 219 insertions, 0 deletions
diff --git a/src/Handlers/ApiTagsHandler.php b/src/Handlers/ApiTagsHandler.php
new file mode 100644
index 0000000..af9fb7b
--- /dev/null
+++ b/src/Handlers/ApiTagsHandler.php
@@ -0,0 +1,16 @@
+<?php
+
+
+namespace Micropoly\Handlers;
+
+
+use Micropoly\Env;
+use Micropoly\Models\Tag;
+
+class ApiTagsHandler extends JsonAPIHandler
+{
+ protected function handleAPIRequest(Env $env, array $variables): JsonAPIResult
+ {
+ return new JsonAPIResult(array_keys(Tag::getTagCounts($env->db())));
+ }
+} \ No newline at end of file
diff --git a/src/Handlers/Index.php b/src/Handlers/Index.php
new file mode 100644
index 0000000..8d0896b
--- /dev/null
+++ b/src/Handlers/Index.php
@@ -0,0 +1,20 @@
+<?php
+
+namespace Micropoly\Handlers;
+
+use Micropoly\Env;
+use Micropoly\Handler;
+use Micropoly\Models\Tag;
+
+class Index implements Handler
+{
+
+ public function handle(Env $env, array $variables)
+ {
+ echo $env->twig()->render("/index.twig", [
+ "title" => "hello",
+ "msg" => "Johoo <script>alert(1)</script>",
+ "tagcloud" => Tag::calcTagCloud(Tag::getTagCounts($env->db())),
+ ]);
+ }
+}
diff --git a/src/Handlers/JsonAPIHandler.php b/src/Handlers/JsonAPIHandler.php
new file mode 100644
index 0000000..cc6aa61
--- /dev/null
+++ b/src/Handlers/JsonAPIHandler.php
@@ -0,0 +1,18 @@
+<?php
+
+
+namespace Micropoly\Handlers;
+
+
+use Micropoly\Env;
+use Micropoly\Handler;
+
+abstract class JsonAPIHandler implements Handler
+{
+ abstract protected function handleAPIRequest(Env $env, array $variables): JsonAPIResult;
+
+ public function handle(Env $env, array $variables)
+ {
+ $this->handleAPIRequest($env, $variables)->send();
+ }
+} \ No newline at end of file
diff --git a/src/Handlers/JsonAPIResult.php b/src/Handlers/JsonAPIResult.php
new file mode 100644
index 0000000..905599c
--- /dev/null
+++ b/src/Handlers/JsonAPIResult.php
@@ -0,0 +1,24 @@
+<?php
+
+
+namespace Micropoly\Handlers;
+
+
+class JsonAPIResult
+{
+ public $data;
+ public int $statuscode = 200;
+
+ public function __construct($data, int $statuscode = 200)
+ {
+ $this->data = $data;
+ $this->statuscode = $statuscode;
+ }
+
+ public function send(): void
+ {
+ http_response_code($this->statuscode);
+ header("Content-Type: application/json; charset=UTF-8");
+ echo json_encode($this->data);
+ }
+} \ No newline at end of file
diff --git a/src/Handlers/MethodNotAllowedHandler.php b/src/Handlers/MethodNotAllowedHandler.php
new file mode 100644
index 0000000..53ddb0e
--- /dev/null
+++ b/src/Handlers/MethodNotAllowedHandler.php
@@ -0,0 +1,14 @@
+<?php
+
+namespace Micropoly\Handlers;
+
+use Micropoly\Env;
+use Micropoly\Handler;
+
+class MethodNotAllowedHandler implements Handler
+{
+ public function handle(\Micropoly\Env $env, array $variables)
+ {
+
+ }
+}
diff --git a/src/Handlers/NewNote.php b/src/Handlers/NewNote.php
new file mode 100644
index 0000000..9c60757
--- /dev/null
+++ b/src/Handlers/NewNote.php
@@ -0,0 +1,40 @@
+<?php
+
+
+namespace Micropoly\Handlers;
+
+
+use Micropoly\Env;
+use Micropoly\Esc;
+use Micropoly\Handler;
+use Micropoly\Models\Note;
+
+class NewNote implements Handler
+{
+ private static function getPostedContent(): ?string
+ {
+ if (empty($_POST["content"]))
+ return null;
+
+ $content = trim((string)$_POST["content"]);
+ return empty($content) ? null : $content;
+ }
+
+ public function handle(Env $env, array $variables)
+ {
+ $content = self::getPostedContent();
+ if ($content !== null) {
+ $note = new Note();
+ $note->setContent($content);
+ $note->setTags($_POST["tag"]);
+ $note->save($env->db());
+
+ $url = $env->documentRoot() . "n/" . $note->getId();
+ http_response_code(303);
+ header("Location: {$url}");
+ echo 'Note created: <a href="' . Esc::e($url) . '">';
+ }
+
+ echo $env->twig()->render("/new_note.twig", []);
+ }
+} \ No newline at end of file
diff --git a/src/Handlers/NotFoundHandler.php b/src/Handlers/NotFoundHandler.php
new file mode 100644
index 0000000..1827995
--- /dev/null
+++ b/src/Handlers/NotFoundHandler.php
@@ -0,0 +1,15 @@
+<?php
+
+namespace Micropoly\Handlers;
+
+use Micropoly\Env;
+use Micropoly\Handler;
+
+class NotFoundHandler implements Handler
+{
+ public function handle(Env $env, array $variables)
+ {
+ http_response_code(404);
+ echo "404";
+ }
+}
diff --git a/src/Handlers/NoteHandler.php b/src/Handlers/NoteHandler.php
new file mode 100644
index 0000000..afdabb5
--- /dev/null
+++ b/src/Handlers/NoteHandler.php
@@ -0,0 +1,39 @@
+<?php
+
+
+namespace Micropoly\Handlers;
+
+
+use Micropoly\Env;
+use Micropoly\Handler;
+use Micropoly\Models\Note;
+
+class NoteHandler implements Handler
+{
+ public function handle(Env $env, array $variables)
+ {
+ $db = $env->db();
+
+ $note = Note::byId($db, $variables["id"]);
+ if ($note === null) {
+ (new NotFoundHandler())->handle($env, []);
+ return;
+ }
+
+ if ($_SERVER["REQUEST_METHOD"] === "POST") {
+ if ($_POST["delete"] === "delete") {
+ $note->delete($db);
+ http_response_code(303);
+ $url = $env->documentRoot();
+ header("Location: {$url}");
+ return;
+ }
+
+ $note->setContent($_POST["content"]);
+ $note->setTags($_POST["tag"]);
+ $note->save($db);
+ }
+
+ echo $env->twig()->render("/note.twig", ["note" => $note]);
+ }
+} \ No newline at end of file
diff --git a/src/Handlers/Search.php b/src/Handlers/Search.php
new file mode 100644
index 0000000..30311ea
--- /dev/null
+++ b/src/Handlers/Search.php
@@ -0,0 +1,33 @@
+<?php
+
+
+namespace Micropoly\Handlers;
+
+use Micropoly\Env;
+use Micropoly\Handler;
+use Micropoly\Models\Note;
+use Micropoly\Search\ParseError;
+use Micropoly\Search\Parser;
+use Micropoly\Search\SearchResult;
+use Micropoly\Search\TrueExpr;
+
+class Search implements Handler
+{
+ public function handle(Env $env, array $variables)
+ {
+ $vars = ["query" => $_GET["q"] ?? ""];
+
+ try {
+ $expr = isset($_GET["q"])
+ ? (Parser::parse($_GET["q"]) ?? new TrueExpr())
+ : new TrueExpr();
+
+ $results = SearchResult::search($env->db(), $expr);
+ $vars["results"] = $results;
+ } catch (ParseError $e) {
+ $vars["error"] = $e->getMessage();
+ }
+
+ echo $env->twig()->render("/search.twig", $vars);
+ }
+} \ No newline at end of file