diff options
Diffstat (limited to 'src/web/core')
| -rw-r--r-- | src/web/core/component.php | 119 | ||||
| -rw-r--r-- | src/web/core/controller.php | 65 | ||||
| -rw-r--r-- | src/web/core/core.php | 109 | ||||
| -rw-r--r-- | src/web/core/model.php | 39 | ||||
| -rw-r--r-- | src/web/core/router.php | 208 |
5 files changed, 0 insertions, 540 deletions
diff --git a/src/web/core/component.php b/src/web/core/component.php deleted file mode 100644 index 376e24d..0000000 --- a/src/web/core/component.php +++ /dev/null @@ -1,119 +0,0 @@ -<?php /* Copyright (c) 2024 Freya Murphy */ - -/** - * Gives access to imporant - * needed utility functions for - * accessing everything else! - */ -abstract class Component extends Core { - - // keep track of what has been loaded - private static array $loaded = array(); - -// ============================= LOADABLE OBJECTS == - - /** - * Loads a $type of object from a $dir with a given $name - * @param string $name - the name of the object to load - * @param string $dir - the directory theese objects are stored in - * @param string $type - the type of the object - */ - private function load_type($name, $dir, $type): object|NULL - { - - $path = $dir . '/' . $name . '.php'; - - // dont reload an ohject - if (array_key_exists($path, Component::$loaded)) - return Component::$loaded[$path]; - - // only load a object if it exists - if (!file_exists($path)) - return NULL; - - - $parts = explode('/', $name); - $part = end($parts); - $class = ucfirst($part) . '_' . $type; - require($path); - - $ref = NULL; - try { - $ref = new ReflectionClass($class); - } catch (Exception $_e) {} - - if ($ref === NULL) - return NULL; - - $obj = $ref->newInstance(); - Component::$loaded[$path] = $obj; - - return $obj; - } - - /** - * Loads a model - * @param string $name - the name of the model to load - */ - protected function load_model($name): Model|NULL - { - $dir = WEB_ROOT . '/_model'; - return $this->load_type($name, $dir, 'model'); - } - - /** - * Loads a controller - * @param string $name - the name of the controller to load - */ - public function load_controller($name): Controller|NULL - { - $dir = WEB_ROOT . '/_controller'; - return $this->load_type($name, $dir, 'controller'); - } - -// ========================================= LANG == - - /** - * Loads a php lang file into the lang array - */ - private static function load_lang_file(string $file): void - { - $lang = $GLOBALS['__lang']; - require($file); - $GLOBALS['__lang'] = $lang; - } - - /** - * Loads each php file lang strings in a directory - */ - private static function load_lang_dir(string $dir): void - { - if ($handle = opendir($dir)) { - while (false !== ($entry = readdir($handle))) { - if ($entry === '.' || $entry === '..') - continue; - Component::load_lang_file($entry); - } - } - } - - /** - * Loads the given common lang - */ - protected static function load_lang(string ...$langs): void - { - $root = WEB_ROOT . '/lang'; - - foreach ($langs as $lang) { - $file = "{$root}/{$lang}.php"; - $dir = "{$root}/{$lang}"; - - if (file_exists($file)) - Component::load_lang_file($file); - else if (is_dir($dir)) - Component::load_lang_dir($dir); - - } - } - -} diff --git a/src/web/core/controller.php b/src/web/core/controller.php deleted file mode 100644 index ca892e2..0000000 --- a/src/web/core/controller.php +++ /dev/null @@ -1,65 +0,0 @@ -<?php /* Copyright (c) 2024 Freya Murphy */ - -abstract class Controller extends Component { - - /** - * Default index for a app, empty - */ - public function index(): void {} - - /** - * Redirectes to a link - */ - public function redirect(string $link): void - { - header('Location: '. $link, true, 301); - die(); - } - - /** - * Lodas a view - */ - protected function view(string $__name, array $data = array()): void - { - $__path = WEB_ROOT . '/_views/' . $__name . '.php'; - if (is_file($__path)) { - extract($data); - require($__path); - } - } - - /** - * Loads a erorr page with a given - * error code - */ - protected function error(int $code): void - { - $error_controller = $this->load_controller('error'); - $error_controller->code($code); - die(); - } - - /** - * Returns HTTP POST information if POST request. - * Returns 405 Method Not Allowed if not. - * - * If $key is specified, returns only that key. otherwise - * returns HTTP 400 Bad Request; - */ - protected function post_data(?string $key = NULL): array|string - { - // only post requests allowed - if ($_SERVER['REQUEST_METHOD'] != 'POST') - $this->error(405); - - // return entire $_POST array - if (!$key) - return $_POST; - - if (!isset($_POST[$key])) - $this->error(400); - - return $_POST[$key]; - } - -} diff --git a/src/web/core/core.php b/src/web/core/core.php deleted file mode 100644 index 4c341c2..0000000 --- a/src/web/core/core.php +++ /dev/null @@ -1,109 +0,0 @@ -<?php /* Copyright (c) 2024 Freya Murphy */ - -/** - * Core functions needed everywhere - */ -abstract class Core { - - private static ?DatabaseHelper $db = NULL; - - /** - * Loads the database - */ - public static function db(): DatabaseHelper - { - if (!Component::$db) - Component::$db = new DatabaseHelper(); - return Component::$db; - } - - /** - * Gets the stamp for a asset path - * @param string $path - */ - public static function asset_stamp(string $path): int - { - if (ENVIRONMENT == 'devlopment') - return time(); - if (isset(FILE_TIMES[$path])) - return FILE_TIMES[$path]; - return 0; - } - - /** - * Gets a full path url from a relative path - * @param string $path - * @param bool $timestamp - */ - public static function get_url(string $path, bool $timestamp = FALSE): string - { - $scheme = 'http'; - if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) - $scheme = $_SERVER['HTTP_X_FORWARDED_PROTO']; - - $host = $_SERVER['HTTP_HOST']; - - if (ENVIRONMENT == 'production') { - $default = CONFIG['domain']; - $allowed = CONFIG['allowed_hosts']; - if (!is_array($allowed)) - $allowed = [$allowed]; - if (!in_array($host, $allowed)) - $host = $default; - } - - $base = CONFIG['base_path']; - $url = "{$scheme}://{$host}{$base}{$path}"; - if ($timestamp) { - $time = Core::asset_stamp($path); - $url .= "?timestamp={$time}"; - } - return $url; - } - - /** - * Loads a js html link - * @param string $path - the path to the js file - */ - public static function link_js(string $path): string - { - $stamp = Core::asset_stamp("public/$path"); - $href = Core::get_url("public/{$path}?timestamp={$stamp}"); - return '<script src="'. $href .'"></script>'; - } - - /** - * Loads a css html link - * @param string $path - the path to the css file - */ - public static function link_css(string $path): string - { - $stamp = Core::asset_stamp("public/$path"); - $href = Core::get_url("public/{$path}?timestamp={$stamp}"); - return '<link rel="stylesheet" href="'. $href .'">'; - } - - /** - * Loads a css html link - * @param string $path - the path to the css file - */ - public static function embed_css(string $path): string - { - $file = PUBLIC_ROOT . '/' . $path; - if (file_exists($file)) { - $text = file_get_contents($file); - return "<style>{$text}</style>"; - } else { - return ""; - } - } - - /** - * Formats a ISO date - * @param $iso_date the ISO date - */ - public static function format_date(string $iso_date): string - { - return date("Y-m-d D H:i", strtotime($iso_date)) . ' EST'; - } -} diff --git a/src/web/core/model.php b/src/web/core/model.php deleted file mode 100644 index 6a42a98..0000000 --- a/src/web/core/model.php +++ /dev/null @@ -1,39 +0,0 @@ -<?php /* Copyright (c) 2024 Freya Murphy */ - -abstract class Model extends Component { - - /** - * @return array<string,mixed> - */ - public static function get_base_data(?string $app = NULL): array - { - $data = array(); - $data['title'] = lang('first_name'); - $data['desc'] = lang('default_short_desc'); - $data['css'] = array(); - $data['js'] = array(); - - $style = CONFIG['style']; - $js = CONFIG['js']; - - if (!$app) - $app = CONTEXT['app']; - - if (isset($style[$app])) { - $css = $style[$app]; - if (!is_array($css)) - $css = array($css); - $data['css'] = $css; - } - - if (isset($js[$app])) { - $js = $js[$app]; - if (!is_array($js)) - $js = array($js); - $data['js'] = $js; - } - - return $data; - } - -} 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']); - } - -} |