diff options
author | Freya Murphy <freya@freyacat.org> | 2024-03-30 12:14:42 -0400 |
---|---|---|
committer | Freya Murphy <freya@freyacat.org> | 2024-03-30 12:14:42 -0400 |
commit | 1f04b83be337cc91a3fabcf4e574e2306f3d2eaa (patch) | |
tree | 74d7d65a7047e60d1877384e3c7b0d70c7b0e49a /web/core | |
parent | start database (user and post), and initial barebones home page (diff) | |
download | xssbook2-1f04b83be337cc91a3fabcf4e574e2306f3d2eaa.tar.gz xssbook2-1f04b83be337cc91a3fabcf4e574e2306f3d2eaa.tar.bz2 xssbook2-1f04b83be337cc91a3fabcf4e574e2306f3d2eaa.zip |
refactor
Diffstat (limited to 'web/core')
-rw-r--r-- | web/core/_controller.php | 49 | ||||
-rw-r--r-- | web/core/_model.php | 44 | ||||
-rw-r--r-- | web/core/aesthetic.php | 55 | ||||
-rw-r--r-- | web/core/controller.php | 55 | ||||
-rw-r--r-- | web/core/database.php | 1 | ||||
-rw-r--r-- | web/core/error.php | 12 | ||||
-rw-r--r-- | web/core/helper.php | 0 | ||||
-rw-r--r-- | web/core/loader.php | 79 | ||||
-rw-r--r-- | web/core/main.php | 123 | ||||
-rw-r--r-- | web/core/model.php | 29 | ||||
-rw-r--r-- | web/core/router.php | 192 |
11 files changed, 270 insertions, 369 deletions
diff --git a/web/core/_controller.php b/web/core/_controller.php new file mode 100644 index 0000000..8a467d6 --- /dev/null +++ b/web/core/_controller.php @@ -0,0 +1,49 @@ +<?php /* Copyright (c) 2024 Freya Murphy */ +abstract class Controller { + + // the main model + public $main; + + // the loader + public $load; + + // the database + public $db; + + /** + * Creates a constructor + * @param Loader $load - the website loaded object + */ + function __construct($load) { + $this->load = $load; + $this->main = $this->load->model('main'); + $this->db = $this->main->db; + + $info = $this->main->info; + $lang = $info['lang']; + $this->load->lang($lang); + $app = $info['app']; + if ($app) { + $this->load->app_lang($lang, $app); + } + } + + public function index() {} + + public function redirect($link) { + header('Location: '. $link, true, 301); + die(); + } + + protected function view($__name, $data = array()) { + $__root = $GLOBALS['webroot']; + $__path = $__root . '/views/' . $__name . '.php'; + if (is_file($__path)) { + extract($data); + require($__path); + return; + } + } + +} +?> diff --git a/web/core/_model.php b/web/core/_model.php new file mode 100644 index 0000000..936fab4 --- /dev/null +++ b/web/core/_model.php @@ -0,0 +1,44 @@ +<?php /* Copyright (c) 2024 Freya Murphy */ +abstract class Model { + // the main model + // shared by all controllers and models + public $main; + public $load; + + // the database + public $db; + + private $config; + + /** + * Creates a model + * @param Loader $load - the main loader object + */ + function __construct($load) { + $this->load = $load; + $this->main = $this->load->model('main'); + $this->db = $this->main->db; + $this->config = new Aesthetic(); + } + + /** + * @returns the base model data + */ + public function get_data(): array { + $data = array(); + $data['self'] = $this->main->user(); + + $info = $this->main->info; + $app = $info['app']; + + if ($app) { + $files = $this->config->get_files($app); + $data = array_merge($data, $files); + } else { + $files = $this->config->get_files(); + $data = array_merge($data, $files); + } + + return $data; + } +} diff --git a/web/core/aesthetic.php b/web/core/aesthetic.php deleted file mode 100644 index 1180ad1..0000000 --- a/web/core/aesthetic.php +++ /dev/null @@ -1,55 +0,0 @@ -<?php /* Copyright (c) 2024 Freya Murphy */ -class Aesthetic { - - private $config; - - function __construct() { - $this->config = array( - '_common' => array( - 'js' => [ - 'js/jquery-3.7.1.min.js', - 'js/lib.js', - 'js/modal.js', - ], - 'css' => [ - 'css/common.css' - ], - ), - 'error' => array( - 'css' => [ - 'css/error.css' - ], - ), - 'home' => array( - 'js' => [ - 'js/post.js', - ], - 'css' => [ - 'css/home.css', - 'css/post.css' - ], - ), - ); - } - - function get_files($route) { - $js_files = $this->config['_common']['js']; - $css_files = $this->config['_common']['css']; - - if (array_key_exists($route, $this->config)) { - $config = $this->config[$route]; - if (array_key_exists('js', $config)) { - $js_files = array_merge($js_files, $config['js']); - } - if (array_key_exists('css', $config)) { - $css_files = array_merge($css_files, $config['css']); - } - } - - return array( - 'js_files' => $js_files, - 'css_files' => $css_files, - ); - } - -} diff --git a/web/core/controller.php b/web/core/controller.php deleted file mode 100644 index 946b460..0000000 --- a/web/core/controller.php +++ /dev/null @@ -1,55 +0,0 @@ -<?php /* Copyright (c) 2024 Freya Murphy */ -abstract class Controller { - - // the main model - public $main; - - // the loader - public $load; - - // the database - public $db; - - function __construct() { - $this->main = $GLOBALS['__vars']['main']; - $this->load = $GLOBALS['__vars']['load']; - $this->db = $this->main->db; - - $info = $this->main->info; - $lang_code = $info['lang']; - $route_name = $info['route']; - $this->load->lang($lang_code); - $this->load->route_lang($lang_code, $route_name); - } - - public function index() {} - - protected function view($__name, $data = array()) { - $__root = $GLOBALS['webroot']; - $__path = $__root . '/views/' . $__name . '.php'; - if (is_file($__path)) { - extract($data); - require($__path); - return; - } - } - - protected function app_view($__name, $data = array()) { - $__root = $GLOBALS['webroot']; - $__route = $this->main->info['route']; - $__path = $__root . '/routes/' . $__route . '/views/' . $__name . '.php'; - if (is_file($__path)) { - extract($data); - require($__path); - return; - } - } - - protected function modal($title, $content, $data = array()) { - $data['title'] = $title; - $data['content'] = $content; - $this->view('template/modal', $data); - } - -} -?> diff --git a/web/core/database.php b/web/core/database.php index b3a597b..4b44e86 100644 --- a/web/core/database.php +++ b/web/core/database.php @@ -170,4 +170,3 @@ class DatabaseHelper { } } -?> diff --git a/web/core/error.php b/web/core/error.php deleted file mode 100644 index 2e02cb1..0000000 --- a/web/core/error.php +++ /dev/null @@ -1,12 +0,0 @@ -<!DOCTYPE html> -<html> - <head> - <title><?=$code . ' - ' . $msg?></title> - </head> - <body> - <center> - <h1><?=$code . ' ' . $msg?></h1> - </center> - <hr> - </body> -</html> diff --git a/web/core/helper.php b/web/core/helper.php deleted file mode 100644 index e69de29..0000000 --- a/web/core/helper.php +++ /dev/null diff --git a/web/core/loader.php b/web/core/loader.php index 4d4526c..2091533 100644 --- a/web/core/loader.php +++ b/web/core/loader.php @@ -1,16 +1,79 @@ <?php /* Copyright (c) 2024 Freya Murphy */ class Loader { + // keep track of what has been loaded + private $loaded; + + function __construct() { + $this->loaded = array(); + } + + /** + * 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'; + if (array_key_exists($path, $this->loaded)) { + return $this->loaded[$path]; + } + + 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($this); + $this->loaded[$path] = $obj; + + return $obj; + } + + /** + * Loads a model + * @param string $name - the name of the model to load + */ + public function model($name): object|NULL { + $root = $GLOBALS['webroot']; + $dir = $root . '/_model'; + return $this->load_type($name, $dir, 'model'); + } + + /** + * Loads a controller + * @param string $name - the name of the controller to load + */ + public function controller($name): Controller|NULL { + $root = $GLOBALS['webroot']; + $dir = $root . '/_controller'; + return $this->load_type($name, $dir, 'controller'); + } + /** * Loads the given common lang - * @param lang_code - the language code + * @param string $lang_code 0 the language code */ - public function lang($lang_code) { + public function lang($lang_code): void { $dir = $GLOBALS['webroot'] . '/lang/' . $lang_code . '/'; $lang = $GLOBALS['lang']; if ($handle = opendir($dir)) { while (false !== ($entry = readdir($handle))) { - if ($entry === '.' || $entry === '..' || $entry === 'routes') { + if ($entry === '.' || $entry === '..' || $entry === 'apps') { continue; } $path = $dir . $entry; @@ -21,12 +84,12 @@ class Loader { } /** - * Loads a given route specific lang - * @param lang_coed - the language code - * #param name - the name of the route + * Loads a given app specific lang + * @param string $lang_code - the language code + * @param string $name - the name of the app */ - public function route_lang($lang_code, $name) { - $dir = $GLOBALS['webroot'] . '/lang/' . $lang_code . '/routes/'; + public function app_lang($lang_code, $name): void { + $dir = $GLOBALS['webroot'] . '/lang/' . $lang_code . '/apps/'; $file = $dir . $name . '.php'; if (file_exists($file)) { $lang = $GLOBALS['lang']; diff --git a/web/core/main.php b/web/core/main.php deleted file mode 100644 index c3c65dd..0000000 --- a/web/core/main.php +++ /dev/null @@ -1,123 +0,0 @@ -<?php /* Copyright (c) 2024 Freya Murphy */ -class MainModel { - - // loaded route infomation - public $info; - public $db; - public $user_id; - - private $users; - - function __construct() { - $this->info = NULL; - $this->db = new DatabaseHelper(); - $this->users = array(); - $_SESSION['jwt'] = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoicmVzdF91c2VyIiwidXNlcl9pZCI6MSwiZXhwIjoxNzExODUxMDUzfQ.FUcFO44SWV--YtVOy7NftTF8OeeOYGZDaDHigygQxsY'; - if (array_key_exists('jwt', $_SESSION)) { - $this->get_session($_SESSION['jwt']); - } else { - $this->user_id = NULL; - }; - } - - private function get_session($jwt) { - $query = $this->db - ->select("_api.verify_jwt('" . $jwt . "') AS user_id;"); - $result = $query->row(); - $user_id = $result['user_id']; - if ($user_id) { - $this->user_id = $user_id; - } - } - - public function link_css($path) { - return '<link rel="stylesheet" href="/public/' . $path . '">'; - } - - public function link_js($path) { - return '<script src="/public/'. $path . '"></script>'; - } - - public function user() { - if ($this->user_id) { - return $this->db - ->select('*') - ->from('api.user') - ->where('id') - ->eq($this->user_id) - ->row(); - } else { - return NULL; - } - } - - public function get_num($key, $default = NULL) { - if (!array_key_exists($key, $_GET)) { - if ($default !== NULL) { - return $default; - } else { - error_page(400, lang('error_400')); - } - } else { - $val = $_GET[$key]; - $val = intval($val); - if ($val < 0) { - return 0; - } else { - return $val; - } - } - } - - public function get_users($objs) { - $ids = array(); - foreach ($objs as $obj) { - $id = $obj['user_id']; - if (!array_key_exists($id, $this->users)) { - array_push($ids, intval($id)); - } - } - if (!empty($ids)) { - $result = $this->db - ->select('*') - ->from('api.user') - ->where_in('id', $ids) - ->rows(); - foreach ($result as $user) { - $id = $user['id']; - $this->users[$id] = $user; - } - } - return $this->users; - } - - public function display_name($user) { - $name = ''; - if ($user['first_name']) { - $name .= $user['first_name']; - } - if ($user['middle_name']) { - if ($name != '') { - $name .= ' '; - } - $name .= $user['middle_name']; - } - if ($user['last_name']) { - if ($name != '') { - $name .= ' '; - } - $name .= $user['last_name']; - } - if ($name == '') { - $name = '@' . $user['username']; - } - return $name; - } - - public function display_date($date) { - return $date; - } - -} - -?> diff --git a/web/core/model.php b/web/core/model.php deleted file mode 100644 index 039b138..0000000 --- a/web/core/model.php +++ /dev/null @@ -1,29 +0,0 @@ -<?php /* Copyright (c) 2024 Freya Murphy */ -abstract class Model { - // the main model - // shared by all controllers and models - public $main; - public $load; - - // the database - public $db; - - private $config; - - function __construct() { - $this->main = $GLOBALS['__vars']['main']; - $this->load = $GLOBALS['__vars']['load']; - $this->db = $this->main->db; - $this->config = new Aesthetic(); - } - - public function get_data() { - $data = array(); - $route = $this->main->info['route']; - $files = $this->config->get_files($route); - $data = array_merge($data, $files); - $data['self'] = $this->main->user(); - return $data; - } -} -?> diff --git a/web/core/router.php b/web/core/router.php index 6ee28a9..72c7674 100644 --- a/web/core/router.php +++ b/web/core/router.php @@ -1,127 +1,147 @@ <?php /* Copyright (c) 2024 Freya Murphy */ class Router { - private $main; + // the loader private $load; - private $routes; - - function load_route($route) { - $name = $route['name']; - $controller_cls = $route['controller']; - $model_cls = $route['model']; - - $root = $GLOBALS['webroot']; - $dir = $root . '/routes/' . $name; - require($dir . '/model.php'); - require($dir . '/controller.php'); - - $model_ref = new ReflectionClass($model_cls); - $model = $model_ref->newInstance(); - $controller_ref = new ReflectionClass($controller_cls); - $controller = $controller_ref->newInstance($model); + // the main model + private $main; - return $controller; + /** + * Creates a router + * @param Loader $load - the main laoder object + */ + function __construct($load) { + $this->load = $load; + $this->main = $this->load->model('main'); } - function __construct($main, $load) { + /** + * @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 path parts + $parts = explode('/', $path); - $routes = array( - 'home' => array( - 'slugs' => ['', 'home'], - 'model' => 'HomeModel', - 'controller' => 'HomeController', - ), - ); + $len = count($parts); - $this->routes = array(); - foreach ($routes as $name => $route) { - foreach ($route['slugs'] as $slug) { - $this->routes[$slug] = $route; - $this->routes[$slug]['name'] = $name; - } + // get route info + $route = array(); + // e.g. / + if ($path === '') { + $route = array( + 'route' => '', + 'slug' => 'index', + ); + // e.g. /home /login + } else if ($len === 1) { + $route = array( + 'route' => $parts[0], + 'slug' => 'index', + ); + // e.g. /home/posts + } else { + $route = array ( + 'route' => implode('/', array_slice($parts, 0, -1)), + 'slug' => end($parts) + ); + }; + + $route['app'] = $route['route']; + $routes = $GLOBALS['routes']; + if (array_key_exists($route['route'], $routes)) { + $route['route'] = $routes[$route['route']]; } - $this->main = $main; - $this->load = $load; + return $route; } - function get_info() { - $uri = parse_url($_SERVER['REQUEST_URI']); + /** + * Gets the curret request info + * @return array<string,mixed> + */ + private function get_req(): array { $method = $_SERVER['REQUEST_METHOD']; - $parts = explode('/', $uri['path']); - - $slug = sizeof($parts) > 1 ? $parts[1] : ''; - $path = sizeof($parts) > 2 ? $parts[2] : 'index'; - if (sizeof($parts) > 3) { - return NULL; - } - - return array( - 'method' => $method, - 'uri' => $uri, + $uri = parse_url($_SERVER['REQUEST_URI']); + $path = $uri['path']; - 'slug' => $slug, - 'path' => $path + return array_merge( + array( + 'uri' => $uri, + 'method' => $method, + 'lang' => $this->get_lang(), + ), + $this->get_req_route($path), ); } - function handle_error($code) { - $route = array( - 'name' => 'error', - 'model' => 'ErrorModel', - 'controller' => 'ErrorController' - ); - $this->main->info = array( - 'slug' => 'error', - 'lang' => 'en_US', - 'route' => 'error' - ); - $controller = $this->load_route($route); - $_GET['code'] = $code; - http_response_code($code); - $controller->index(); + /** + * Gets the current language + * @return string + */ + private function get_lang(): string { + return 'en_US'; } - public function handle_request() { - $request = $this->get_info(); - - if ($request === NULL) { - $this->handle_error(404); - return; + /** + * Handles a router error code + * @param int $code - the http error code + * @param bool $recursed + */ + private function handle_error($code, $recursed): void { + if ($recursed) { + die($code . ' (recursed)'); } - $slug = $request['slug']; - if (!array_key_exists($slug, $this->routes)) { - $this->handle_error(404); - return; - } + $this->main->info['slug'] = 'index'; + $this->main->info['app'] = 'error'; + $this->main->info['route'] = 'apps/error'; + $req = $this->main->info; + $_GET['code'] = $code; - $route = $this->routes[$slug]; - $this->main->info = array( - 'lang' => 'en_US', - 'slug' => $slug, - 'route' => $route['name'], - ); + $this->handle_req($req, TRUE); + } - $controller = $this->load_route($route); + /** + * @param array $req + * @param bool $recursed + */ + private function handle_req($req, $recursed = FALSE): void { + $controller = $this->load->controller($req['route']); - $path = $request['path']; - $ref = NULL; + if ($controller === NULL) { + $this->handle_error(404, $recursed); + return; + } + $ref = NULL; try { - $ref = new ReflectionMethod($controller, $path); + $ref = new ReflectionMethod($controller, $req['slug']); } catch (Exception $_e) {} if ($ref === NULL || !$ref->isPublic()) { - $this->handle_error(404); + $this->handle_error(404, $recursed); return; } $ref->invoke($controller); + } + /** + * Handels the incomming reuqest + */ + public function handle_request(): void { + $req = $this->get_req(); + $this->main->info = $req; + $this->handle_req($req); } } |