summaryrefslogtreecommitdiff
path: root/src/web/core/router.php
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2026-02-23 22:57:27 -0500
committerFreya Murphy <freya@freyacat.org>2026-02-23 22:57:27 -0500
commitf373ead95fb5beb962c376b5b7b46dfde8ac4e57 (patch)
treec99df23521ff2a5e5e2e4627c525a5e99dc2e3ae /src/web/core/router.php
parentadd 96x96 logo (diff)
downloadwebsite-f373ead95fb5beb962c376b5b7b46dfde8ac4e57.tar.gz
website-f373ead95fb5beb962c376b5b7b46dfde8ac4e57.tar.bz2
website-f373ead95fb5beb962c376b5b7b46dfde8ac4e57.zip
update website to work with crimson framework
Diffstat (limited to 'src/web/core/router.php')
-rw-r--r--src/web/core/router.php208
1 files changed, 0 insertions, 208 deletions
diff --git a/src/web/core/router.php b/src/web/core/router.php
deleted file mode 100644
index 6a543aa..0000000
--- a/src/web/core/router.php
+++ /dev/null
@@ -1,208 +0,0 @@
-<?php /* Copyright (c) 2024 Freya Murphy */
-
-class Router extends Component {
-
- private bool $db_ready;
- private bool $recursed;
- private array $req;
-
- /**
- * Creates a router
- * @param Loader $load - the main laoder object
- */
- function __construct()
- {
- $this->db_ready = file_exists('/status/ready');
- $this->recursed = FALSE;
- $this->req = $this->get_req();
- }
-
- /**
- * @param string $path - the current request path
- * Gets the current route
- * @return array<string,mixed>
- */
- private function get_req_route($path): array
- {
- // trim the path
- $path = trim($path);
- // remove first '/'
- $path = substr($path, 1);
-
- // get modified route
- $routes = CONFIG['routes'];
- foreach ($routes as $key => $value) {
- $key = "/^{$key}$/";
- if (!preg_match($key, $path, $matches))
- continue;
-
- $path = $value;
-
- for ($i = 1; $i < count($matches); $i++) {
- $path = str_replace(
- "\\{$i}",
- $matches[$i],
- $path);
- }
-
- break;
- }
-
- // get path parts
- $parts = explode('/', $path);
- // get the length
- $len = count($parts);
- // get route info
- $route = array();
- $route['app'] = $len > 0 ? $parts[0] : 'index';
- $route['slug'] = $len > 1 ? $parts[1] : 'index';
- $route['args'] = array_slice($parts, 2);
-
- return $route;
- }
-
- /**
- * Gets the users ip
- */
- private function get_ip(): ?string
- {
- $headers = array (
- 'HTTP_CLIENT_IP',
- 'HTTP_X_FORWARDED_FOR',
- 'HTTP_X_FORWARDED',
- 'HTTP_FORWARDED_FOR',
- 'HTTP_FORWARDED',
- 'HTTP_X_REAL_IP',
- 'REMOTE_ADDR'
- );
- foreach ($headers as $header) {
- if (isset($_SERVER[$header]))
- return $_SERVER[$header];
- }
- return NULL;
- }
-
- /**
- * Gets the curret request info
- * @return array<string,mixed>
- */
- private function get_req(): array
- {
- $method = $_SERVER['REQUEST_METHOD'];
- $uri_str = $_SERVER['REQUEST_URI'];
- $uri = parse_url($uri_str);
-
- if (!$uri) {
- $uri = array('path' => '/error');
- }
-
- $path = '';
- if (array_key_exists('path', $uri)) {
- $path = $uri['path'];
- }
-
- return array_merge(
- array(
- 'uri' => $uri,
- 'uri_str' => $uri_str,
- 'method' => $method,
- 'ip' => $this->get_ip()
- ),
- $this->get_req_route($path),
- );
- }
-
- /**
- * Handles a router error code
- * @param int $code - the http error code
- */
- private function handle_error(int $code): void
- {
- if ($this->recursed)
- die($code . ' (recursed)');
-
- $this->recursed = TRUE;
- $this->req['app'] = 'error';
- $this->req['slug'] = 'code';
- $this->req['args'] = array($code);
- $this->handle_req();
- }
-
- /**
- * @param array<int,mixed> $req
- */
- private function load_htc(array $req): void
- {
- $parts = explode('/', $req['uri_str']);
- $file = end($parts);
- $path = PUBLIC_ROOT . '/polyfills/' . $file;
-
- if (file_exists($path)) {
- header('Content-type: text/x-component');
- include($path);
- } else {
- $this->handle_error(400);
- }
- }
-
- /**
- * @param array $req
- * @param array<int,mixed> $req
- */
- public function handle_req(): void
- {
- if ($this->recursed === FALSE) {
- // if we are in a recursing error handler
- // we dont want to trigger a db 503 forever
- // since its already active
- if ($this->db_ready === FALSE) {
- $this->handle_error(503);
- return;
- }
- }
-
- // server error if we cannot parse url
- if (!$this->req) {
- $this->handle_error(500);
- return;
- }
-
- // load htc if someone is requesting it (hi IE6 :3)
- if (str_ends_with($this->req['uri_str'], '.htc')) {
- $this->load_htc($this->req);
- return;
- }
-
- // load the controller
- $controller = $this->load_controller($this->req['app']);
- if ($controller === NULL) {
- $this->handle_error(404);
- return;
- }
-
- $ref = NULL;
- try {
- $cls = new ReflectionClass($controller);
- $mds = $cls->getMethods(ReflectionMethod::IS_PUBLIC);
- foreach ($mds as $md) {
- if ($md->name !== $this->req['slug'])
- continue;
- if (count($md->getParameters()) !=
- count($this->req['args']))
- continue;
- $ref = $md;
- break;
- }
- } catch (Exception $_e) {}
-
- if ($ref === NULL) {
- $this->handle_error(404);
- return;
- }
-
- define('CONTEXT', $this->req);
- Component::load_lang('common', $this->req['app']);
- $ref->invokeArgs($controller, $this->req['args']);
- }
-
-}