vlw.se/api/endpoints/notes/GET.php

46 lines
No EOL
1 KiB
PHP

<?php
use Reflect\Path;
use Reflect\Response;
use ReflectRules\Type;
use ReflectRules\Rules;
use ReflectRules\Ruleset;
const GET_PARAM_NOTE = "id";
class GET_Notes {
protected readonly Ruleset $ruleset;
public function __construct() {
$this->ruleset = new Ruleset(strict: true);
$this->ruleset->GET([
(new Rules(GET_PARAM_NOTE))
->required()
->type(Type::STRING)
->min(1)
]);
$this->ruleset->validate_or_exit();
}
private function get_path(): string {
$base_path = substr($_ENV["notes"]["md_file_dir"], -1, 1) === "/" ? $_ENV["notes"]["md_file_dir"] : $_ENV["notes"]["md_file_dir"] . "/";
return "{$base_path}{$_GET[GET_PARAM_NOTE]}.md";
}
public function exists(): bool {
return is_readable($this->get_path());
}
public function read(): string {
return file_get_contents($this->get_path());
}
public function main(): Response {
return $this->exists()
? new Response($this->read(), 200, "text/plain")
: new Response("Note not found", 404);
}
}