From 1f9024763d9224c4cd9a181bac27e6b9f12ad672 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Wed, 18 Sep 2024 14:14:53 -0400 Subject: [PATCH] refactor --- src/assets/projects/03-brainfucked.md | 2 +- .../writeup/{proof.md => brainfucked.md} | 0 src/scss/legacy.scss | 8 +- src/web/_controller/_comments.php | 8 +- src/web/_controller/_meta.php | 30 +-- src/web/_controller/blog.php | 48 ++-- src/web/_controller/bucket.php | 9 +- src/web/_controller/error.php | 17 +- src/web/_controller/home.php | 9 +- src/web/_controller/projects.php | 10 +- src/web/_model/_comments.php | 33 +-- src/web/_model/blog.php | 69 +++--- src/web/_model/bucket.php | 19 +- src/web/_model/error.php | 34 +-- src/web/_model/main.php | 110 --------- src/web/_model/projects.php | 20 +- src/web/_views/apps/blog.php | 6 +- src/web/_views/apps/blog_post.php | 2 +- src/web/_views/apps/blog_rss.php | 5 +- src/web/_views/apps/bucket.php | 4 +- src/web/_views/comments.php | 2 +- src/web/_views/footer.php | 11 +- src/web/_views/head.php | 22 +- src/web/_views/header.php | 20 +- src/web/config/routes.php | 4 - src/web/config/style.php | 2 - src/web/core/_controller.php | 54 ----- src/web/core/_model.php | 51 ---- src/web/core/component.php | 119 +++++++++ src/web/core/controller.php | 43 ++++ src/web/core/core.php | 85 +++++++ src/web/core/loader.php | 112 --------- src/web/core/model.php | 29 +++ src/web/core/router.php | 227 ++++++++---------- src/web/helpers/aria.php | 3 +- src/web/helpers/database.php | 81 ++++--- src/web/helpers/image.php | 17 +- src/web/helpers/lang.php | 4 +- src/web/index.php | 64 +++-- src/web/lang/{apps => }/blog.php | 0 src/web/lang/{apps => }/error.php | 0 src/web/lang/{apps => }/home.php | 0 src/web/lang/{apps => }/projects.php | 0 43 files changed, 681 insertions(+), 712 deletions(-) rename src/assets/writeup/{proof.md => brainfucked.md} (100%) delete mode 100644 src/web/_model/main.php delete mode 100644 src/web/core/_controller.php delete mode 100644 src/web/core/_model.php create mode 100644 src/web/core/component.php create mode 100644 src/web/core/controller.php create mode 100644 src/web/core/core.php delete mode 100644 src/web/core/loader.php create mode 100644 src/web/core/model.php rename src/web/lang/{apps => }/blog.php (100%) rename src/web/lang/{apps => }/error.php (100%) rename src/web/lang/{apps => }/home.php (100%) rename src/web/lang/{apps => }/projects.php (100%) diff --git a/src/assets/projects/03-brainfucked.md b/src/assets/projects/03-brainfucked.md index f45c157..e8ffb5e 100644 --- a/src/assets/projects/03-brainfucked.md +++ b/src/assets/projects/03-brainfucked.md @@ -7,4 +7,4 @@ brainfucked is a brainfuck dialect that makes you manage your memory manually! The original interpreter gives you a tape of 30,000 cells, while i only give you as much as a pointer takes up on your system: 4 or 8 depending if your on a 32bit or 64bit system. You can than allocate a new tape and then use that! Also brainfucked is tuing complete proven by -this amzing [proof](/blog/writeup?name=proof.md) that my friend [trimill](https://trimill.xyz) made. +this amzing [proof](/blog/writeup/brainfucked) that my friend [trimill](https://trimill.xyz) made. diff --git a/src/assets/writeup/proof.md b/src/assets/writeup/brainfucked.md similarity index 100% rename from src/assets/writeup/proof.md rename to src/assets/writeup/brainfucked.md diff --git a/src/scss/legacy.scss b/src/scss/legacy.scss index bc56f9f..dbb7bf3 100644 --- a/src/scss/legacy.scss +++ b/src/scss/legacy.scss @@ -6,12 +6,12 @@ behavior: url(boxsizing.htc); } -#main.legacy .col { +#main .col { display: block !important; width: 100% !important; } -#main.legacy .left { +#main .left { padding-right: 0 !important; padding-bottom: $outer-gap; } @@ -22,4 +22,8 @@ #new_comment { width: 400px; + + .input { + width: 400px; + } } diff --git a/src/web/_controller/_comments.php b/src/web/_controller/_comments.php index 059b926..eea792f 100644 --- a/src/web/_controller/_comments.php +++ b/src/web/_controller/_comments.php @@ -3,12 +3,10 @@ class _comments_controller extends Controller { private $comments_model; - function __construct($load) { - parent::__construct($load); - $this->comments_model = $this->load->model('_comments'); + function __construct() { + $this->comments_model = $this->load_model('_comments'); } - public function comments(string $page, string $ref): void { $data = $this->comments_model->get_comments($page); $this->view('comments', array( @@ -79,7 +77,7 @@ class _comments_controller extends Controller { ->post_comment($author, $content, $page, $vulgar); if ($result) { - header('Location: ' . $this->main->get_url($ref) . '#comments'); + header('Location: ' . $this->get_url($ref) . '#comments'); } else { $this->error(500); } diff --git a/src/web/_controller/_meta.php b/src/web/_controller/_meta.php index e78f8b3..891bf69 100644 --- a/src/web/_controller/_meta.php +++ b/src/web/_controller/_meta.php @@ -1,13 +1,10 @@ main->get_url_full('sitemap.xml'); + $sitemap = $this->get_url('sitemap.xml'); echo "User-agent: *\n"; echo "Disallow:\n"; @@ -18,14 +15,16 @@ class _meta_controller extends Controller { echo "Sitemap: {$sitemap}\n"; } - private function sitemap_page(string $url, string $priority): void { + private function sitemap_page(string $url, string $priority): void + { echo "\n"; - echo "{$this->main->get_url_full($url)}\n"; + echo "{$this->get_url($url)}\n"; echo "{$priority}\n"; echo ""; } - public function sitemap(): void { + public function sitemap(): void + { header("Content-Type: application/xml"); echo "\n"; @@ -35,8 +34,8 @@ class _meta_controller extends Controller { $this->sitemap_page('projects', 0.8); $this->sitemap_page('blog', 0.8); - $this->load->app_lang('blog'); - $blog_modal = $this->load->model('blog'); + $this->load_lang('blog'); + $blog_modal = $this->load_model('blog'); $blog = $blog_modal->get_data()['blog']; foreach ($blog as $name => $_) { @@ -46,20 +45,21 @@ class _meta_controller extends Controller { echo "\n"; } - public function manifest(): void { + public function manifest(): void + { $json = array( 'short_name' => lang('domain'), 'name' => lang('domain'), 'icons' => [ array( - 'src' => $this->main->get_url('public/icons/logo512.png'), + 'src' => $this->get_url('public/icons/logo512.png'), 'type' => 'image/png', 'sizes' => '512x512', 'purpose' => 'any maskable' ) ], - 'id' => $this->main->get_url('home'), - 'start_url' => $this->main->get_url('home'), + 'id' => $this->get_url('home'), + 'start_url' => $this->get_url('home'), 'background_color' => lang('theme_color'), 'display' => 'standalone', 'scope' => lang('base_path'), diff --git a/src/web/_controller/blog.php b/src/web/_controller/blog.php index 4a961e1..b2ddbcc 100644 --- a/src/web/_controller/blog.php +++ b/src/web/_controller/blog.php @@ -4,29 +4,28 @@ class Blog_controller extends Controller { public $comments_controller; private $blog_model; - function __construct($load) { - parent::__construct($load); - $this->blog_model = $this->load->model('blog'); - $this->comments_controller = $this->load->controller('_comments'); + function __construct() + { + $this->blog_model = $this->load_model('blog'); + $this->comments_controller = $this->load_controller('_comments'); } - public function index(): void { + public function index(): void + { parent::index(); + $data = $this->blog_model->get_data(); $this->view('header', $data); $this->view('apps/blog', $data); $this->view('footer', $data); } - private function protect(string $folder): void { - if (!array_key_exists('name', $_GET)) { - $this->error(400); - } - - $basepath = $GLOBALS['assetroot'] . '/' . $folder . '/'; + private function protect(string $folder, string $name): void + { + $basepath = ASSET_ROOT . '/' . $folder . '/'; $realBase = realpath($basepath); - $userpath = $basepath . $_GET['name']; + $userpath = $basepath . $name . '.md'; $realUserPath = realpath($userpath); if ($realUserPath === false || strpos($realUserPath, $realBase) !== 0) { @@ -34,35 +33,42 @@ class Blog_controller extends Controller { } } - public function post(): void { - $this->protect('blog'); + public function post($name): void + { + $this->protect('blog', $name); + parent::index(); - $data = $this->blog_model->get_post($_GET['name']); + + $data = $this->blog_model->get_post($name); if ($data === FALSE) { $this->error(404); } $this->view('header', $data); $this->view('apps/blog_post', $data); - $ref = 'blog/post?name=' . $_GET['name']; + $ref = "blog/post{$name}"; $this->comments_controller->comments($data['post']['meta']['name'], $ref); $this->view('footer', $data); } - public function writeup(): void { - $this->protect('writeup'); + public function writeup($name): void + { + $this->protect('writeup', $name); + parent::index(); - $data = $this->blog_model->get_writeup($_GET['name']); + + $data = $this->blog_model->get_writeup($name); if ($data === FALSE) { $this->error(404); } $this->view('header', $data); $this->view('apps/blog_writeup', $data); - $ref = 'blog/writeup?name=' . $_GET['name']; + $ref = "blog/writeup/{$name}"; $this->comments_controller->comments($data['post']['meta']['name'], $ref); $this->view('footer', $data); } - public function rss(): void { + public function rss(): void + { $data = $this->blog_model->get_data(); header('Content-Type: application/xml'); $this->view('apps/blog_rss', $data); diff --git a/src/web/_controller/bucket.php b/src/web/_controller/bucket.php index ed15ef8..a7d1023 100644 --- a/src/web/_controller/bucket.php +++ b/src/web/_controller/bucket.php @@ -3,12 +3,13 @@ class Bucket_controller extends Controller { private $bucket_model; - function __construct($load) { - parent::__construct($load); - $this->bucket_model = $this->load->model('bucket'); + function __construct() + { + $this->bucket_model = $this->load_model('bucket'); } - public function index(): void { + public function index(): void + { parent::index(); $data = $this->bucket_model->get_data(); if ($data === NULL) { diff --git a/src/web/_controller/error.php b/src/web/_controller/error.php index d24308b..30bd797 100644 --- a/src/web/_controller/error.php +++ b/src/web/_controller/error.php @@ -3,19 +3,26 @@ class Error_controller extends Controller { private $error_model; - function __construct($load) { - parent::__construct($load); - $this->error_model = $this->load->model('error'); + function __construct() + { + $this->error_model = $this->load_model('error'); } - public function index(): void { + public function code(int $code): void + { parent::index(); - $data = $this->error_model->get_data(); + $this->load_lang('error'); + + $data = $this->error_model->get_data($code); $this->view('header', $data); $this->view('apps/error', $data); $this->view('footer', $data); } + public function index(): void + { + $this->code(500); + } } ?> diff --git a/src/web/_controller/home.php b/src/web/_controller/home.php index 12dff64..a673e60 100644 --- a/src/web/_controller/home.php +++ b/src/web/_controller/home.php @@ -1,12 +1,11 @@ main->get_data(); + + $data = Model::get_base_data(); $this->view('header', $data); $this->view('apps/home', $data); $this->view('footer', $data); diff --git a/src/web/_controller/projects.php b/src/web/_controller/projects.php index 9ee2136..cfa28b5 100644 --- a/src/web/_controller/projects.php +++ b/src/web/_controller/projects.php @@ -3,13 +3,15 @@ class Projects_controller extends Controller { private $projects_model; - function __construct($load) { - parent::__construct($load); - $this->projects_model = $this->load->model('projects'); + function __construct() + { + $this->projects_model = $this->load_model('projects'); } - public function index(): void { + public function index(): void + { parent::index(); + $data = $this->projects_model->get_data(); $this->view('header', $data); $this->view('apps/projects', $data); diff --git a/src/web/_model/_comments.php b/src/web/_model/_comments.php index 73c1fc7..f36c642 100644 --- a/src/web/_model/_comments.php +++ b/src/web/_model/_comments.php @@ -1,12 +1,9 @@ load_profanity(); return preg_match($profanity, $text); } - public function get_comments($page) { - $ip = $this->main->info['ip']; - $query = $this->db + public function get_comments($page) + { + $ip = CONTEXT['ip']; + $query = $this->db() ->select('*') ->from('admin.comment c') ->where('c.page') @@ -46,17 +45,19 @@ class _comments_model extends Model { return $result; } - public function ban_user() { - $ip = $this->main->info['ip']; - $this->db + public function ban_user() + { + $ip = CONTEXT['ip']; + $this->db() ->insert_into('admin.banned', 'ip', 'reason') ->values($ip, 'vulgar language') ->execute(); } - public function post_comment($author, $content, $page, $vulgar) { - $ip = $this->main->info['ip']; - return $this->db + public function post_comment($author, $content, $page, $vulgar) + { + $ip = CONTEXT['ip']; + return $this->db() ->insert_into('admin.comment', 'author', 'content', 'page', 'ip', 'vulgar') ->values($author, $content, $page, $ip, $vulgar) diff --git a/src/web/_model/blog.php b/src/web/_model/blog.php index 0df3959..6dc1316 100644 --- a/src/web/_model/blog.php +++ b/src/web/_model/blog.php @@ -3,17 +3,18 @@ class Blog_model extends Model { private $markdown; - function __construct($load) { - parent::__construct($load); + function __construct() + { $this->markdown = new MarkdownParser(); } - /** - * @param mixed $data - * @return void - */ - private function load_blog(&$data): void { + /** + * @param mixed $data + * @return void + */ + private function load_blog(&$data): void + { $blog = array(); - $dir = $GLOBALS['assetroot'] . '/blog'; + $dir = ASSET_ROOT . '/blog'; if ($handle = opendir($dir)) { while (false !== ($entry = readdir($handle))) { if (str_starts_with($entry, ".")) { @@ -29,31 +30,31 @@ class Blog_model extends Model { } public function get_data(): ?array { - $data = parent::get_data(); + $data = parent::get_base_data('blog'); $this->load_blog($data); $data['title'] = lang('title'); $data['desc'] = lang('blog_short_desc'); return $data; } - /** - * @param mixed $name - * @return bool| - */ - private function load_post($name): ?array { - $dir = $GLOBALS['assetroot'] . '/blog'; - $path = $dir . '/' . $name; + /** + * @param mixed $name + * @return bool| + */ + private function load_post($name): ?array { + $dir = ASSET_ROOT . '/blog'; + $path = $dir . '/' . $name . '.md'; if(!file_exists($path)) { return NULL; } $md = $this->markdown->parse($path); return $md; } - /** - * @param mixed $name - * @return bool|null|array - */ - public function get_post($name): ?array { - $data = parent::get_data(); + /** + * @param mixed $name + * @return bool|null|array + */ + public function get_post($name): ?array { + $data = parent::get_base_data(); $post = $this->load_post($name); if (!$post) { return NULL; @@ -63,24 +64,24 @@ class Blog_model extends Model { $data['post'] = $post; return $data; } - /** - * @param mixed $name - */ - private function load_writeup($name): ?array { - $dir = $GLOBALS['assetroot'] . '/writeup'; - $path = $dir . '/' . $name; + /** + * @param mixed $name + */ + private function load_writeup($name): ?array { + $dir = ASSET_ROOT . '/writeup'; + $path = $dir . '/' . $name . '.md'; if(!file_exists($path)) { return NULL; } $md = $this->markdown->parse($path); return $md; } - /** - * @param mixed $name - * @return bool|null|array - */ - public function get_writeup($name): ?array { - $data = parent::get_data(); + /** + * @param mixed $name + * @return bool|null|array + */ + public function get_writeup($name): ?array { + $data = parent::get_base_data(); $writeup = $this->load_writeup($name); if (!$writeup) { return NULL; diff --git a/src/web/_model/bucket.php b/src/web/_model/bucket.php index f38bebe..374836e 100644 --- a/src/web/_model/bucket.php +++ b/src/web/_model/bucket.php @@ -1,24 +1,19 @@ get_msg($data); + public function get_data(int $code): array + { + $data = parent::get_base_data('error'); + $this->get_msg($data, $code); return $data; } } diff --git a/src/web/_model/main.php b/src/web/_model/main.php deleted file mode 100644 index cbcf498..0000000 --- a/src/web/_model/main.php +++ /dev/null @@ -1,110 +0,0 @@ -\d+)/i', @$_SERVER['HTTP_USER_AGENT'], $B)) { - return $B['v']; - } else { - return PHP_INT_MAX; - } - } - - /** - * Gets the full url including the http scheme and host part - * Needed for IE 6 & 7 need. - * @param string $path - * @param bool $timestamp - */ - public function get_url_full(string $path, bool $timestamp = FALSE): string { - $host = $_SERVER['HTTP_HOST']; - $base = lang('base_path'); - - $url = "http://{$host}{$base}{$path}"; - if ($timestamp) { - $time = @filemtime($GLOBALS['rootroot'] . '/' . $path); - $url .= "?timestamp={$time}"; - } - return $url; - } - - /** - * Gets a full path url from a relative path - * @param string $path - * @param bool $timestamp - */ - public function get_url(string $path, bool $timestamp = FALSE): string { - if ($this->get_ie_version() <= 7) { - return $this->get_url_full($path, $timestamp); - } - $base = lang('base_path'); - $url = "{$base}{$path}"; - if ($timestamp) { - $time = @filemtime($GLOBALS['rootroot'] . '/' . $path); - $url .= "?timestamp={$time}"; - } - return $url; - } - - /** - * Loads a css html link - * @param string $path - the path to the css file - */ - public function link_css(string $path): string { - $stamp = $this->asset_stamp($path); - $href = $this->get_url("public/{$path}?stamp={$stamp}"); - return ''; - } - - /** - * Loads a css html link - * @param string $path - the path to the css file - */ - public function embed_css(string $path): string { - $file = $GLOBALS['publicroot'] . '/' . $path; - if (file_exists($file)) { - $text = file_get_contents($file); - return ""; - } else { - return ""; - } - } - - /** - * Formats a ISO date - * @param $iso_date the ISO date - */ - public function format_date(string $iso_date): string { - return date("Y-m-d D H:m", strtotime($iso_date)); - } -} - -?> diff --git a/src/web/_model/projects.php b/src/web/_model/projects.php index 784e12a..537bce5 100644 --- a/src/web/_model/projects.php +++ b/src/web/_model/projects.php @@ -3,17 +3,18 @@ class Projects_model extends Model { private $markdown; - function __construct($load) { - parent::__construct($load); + function __construct() + { $this->markdown = new MarkdownParser(); } - /** - * @param array $data - */ - private function load_projects(&$data): void { + /** + * @param array $data + */ + private function load_projects(&$data): void + { $projects = array(); - $dir = $GLOBALS['assetroot'] . '/projects'; + $dir = ASSET_ROOT . '/projects'; if ($handle = opendir($dir)) { while (false !== ($entry = readdir($handle))) { if (str_starts_with($entry, ".")) { @@ -28,8 +29,9 @@ class Projects_model extends Model { $data['projects'] = $projects; } - public function get_data(): ?array { - $data = parent::get_data(); + public function get_data(): ?array + { + $data = parent::get_base_data(); $this->load_projects($data); $data['title'] = lang('title'); $data['desc'] = lang('short_desc'); diff --git a/src/web/_views/apps/blog.php b/src/web/_views/apps/blog.php index 7290f0e..f64453c 100644 --- a/src/web/_views/apps/blog.php +++ b/src/web/_views/apps/blog.php @@ -1,13 +1,13 @@ -

main->get_url('rss.xml')])?>

+

get_url('rss.xml')])?>

$post) { $meta = $post['meta']; - $link = $this->main->get_url('blog/post?name=' . $name); + $link = $this->get_url('blog/post/' . substr($name, 0, -3)); echo '

' . $meta['name'] . '

'; echo '' . $meta['desc'] . '
'; - echo ''; + echo ''; } ?> diff --git a/src/web/_views/apps/blog_post.php b/src/web/_views/apps/blog_post.php index d5ad255..8b45a62 100644 --- a/src/web/_views/apps/blog_post.php +++ b/src/web/_views/apps/blog_post.php @@ -1,6 +1,6 @@ - : + :
diff --git a/src/web/_views/apps/blog_rss.php b/src/web/_views/apps/blog_rss.php index c3b1c84..e112389 100644 --- a/src/web/_views/apps/blog_rss.php +++ b/src/web/_views/apps/blog_rss.php @@ -7,12 +7,13 @@ $post) { + $name = substr($name, 0, -3); echo ''; echo '' . $post['meta']['name'] . ''; echo '' . $post['meta']['desc'] . ''; echo '' . $post['meta']['date'] . ''; - echo '' . lang('root_url') . 'blog/post?name=' . $name . ''; - echo '' . lang('root_url') . 'blog/post?name=' . $name . ''; + echo '' . lang('root_url') . 'blog/post/' . $name . ''; + echo '' . lang('root_url') . 'blog/post/' . $name . ''; echo ''; } ?> diff --git a/src/web/_views/apps/bucket.php b/src/web/_views/apps/bucket.php index 72c349b..aeb6cf3 100644 --- a/src/web/_views/apps/bucket.php +++ b/src/web/_views/apps/bucket.php @@ -3,9 +3,9 @@ $root='https://webring.bucketfish.me/redirect.html?to=%s&name=' . $name; $this->view('head', $data); if ($lightmode === 'true') { - echo $this->main->link_css('css/bucket_light.css'); + echo $this->link_css('css/bucket_light.css'); } else { - echo $this->main->link_css('css/bucket.css'); + echo $this->link_css('css/bucket.css'); } ?> diff --git a/src/web/_views/comments.php b/src/web/_views/comments.php index 0544f84..78cc1cb 100644 --- a/src/web/_views/comments.php +++ b/src/web/_views/comments.php @@ -3,7 +3,7 @@ main->format_date($comment['created']); + $date = $this->format_date($comment['created']); echo '
'; echo '

' . esc($comment['author']) . '

'; diff --git a/src/web/_views/footer.php b/src/web/_views/footer.php index 1eac625..f451bfd 100644 --- a/src/web/_views/footer.php +++ b/src/web/_views/footer.php @@ -6,8 +6,6 @@ } else { $footer_text = ''; } - - $legacy = $this->main->get_ie_version() <= 7; ?>
@@ -39,13 +37,12 @@ height="40" class="bucket" title="" - src="main->get_url('bucket?name=freya')?>" + src="get_url('bucket?name=freya')?>" > - - - - + diff --git a/src/web/_views/head.php b/src/web/_views/head.php index 5534070..27613ff 100644 --- a/src/web/_views/head.php +++ b/src/web/_views/head.php @@ -12,18 +12,18 @@ - + <?=$title?> - "> - "> - "> - "> - "> - "> + "> + "> + "> + "> + "> + "> - main->get_ie_version() <= 7) - echo $this->main->link_css('css/legacy.css'); - ?> + main->embed_css($file); + echo $this->embed_css($file); ?> diff --git a/src/web/_views/header.php b/src/web/_views/header.php index b037038..07c26b8 100644 --- a/src/web/_views/header.php +++ b/src/web/_views/header.php @@ -1,16 +1,14 @@ view('head', $data); - echo $this->main->link_css('css/main.css'); - $legacy = $this->main->get_ie_version() <= 7; + echo $this->link_css('css/main.css'); ?> - -
- +
- - -
-
-
diff --git a/src/web/config/routes.php b/src/web/config/routes.php index 0b1cc3a..1fc3b67 100644 --- a/src/web/config/routes.php +++ b/src/web/config/routes.php @@ -1,11 +1,7 @@ load = $load; - $this->main = $this->load->model('main'); - - $this->load->lang(); - $info = $this->main->info; - $app = $info['app']; - if ($app) { - $this->load->app_lang($app); - } - } - - public function index(): void {} - - public function redirect(string $link): void { - header('Location: '. $link, true, 301); - die(); - } - - /** - * @param array $data - */ - protected function view(string $__name, array $data = array()): void { - $__root = $GLOBALS['webroot']; - $__path = $__root . '/_views/' . $__name . '.php'; - if (is_file($__path)) { - extract($data); - require($__path); - return; - } - } - - protected function error(int $code): void { - $_GET['code'] = $code; - $this->main->info['app'] = 'error'; - $error_controller = $this->load->controller('error'); - $error_controller->index(); - die(); - } - -} diff --git a/src/web/core/_model.php b/src/web/core/_model.php deleted file mode 100644 index 57127de..0000000 --- a/src/web/core/_model.php +++ /dev/null @@ -1,51 +0,0 @@ -load = $load; - if ($main) { - $this->main = $this; - } else { - $this->main = $this->load->model('main'); - } - $this->db = $this->load->db(); - } - - /** - * @returns the base model data - */ - public function get_data(): ?array { - $data = array(); - - $info = $this->main->info; - $app = $info['app']; - - $data['title'] = lang('first_name'); - $data['desc'] = lang('default_short_desc'); - $data['css'] = array(); - - $style = $GLOBALS['style']; - if (isset($style[$app])) { - $css = $style[$app]; - if (!is_array($css)) - $css = array($css); - $data['css'] = $css; - } - - return $data; - } -} diff --git a/src/web/core/component.php b/src/web/core/component.php new file mode 100644 index 0000000..376e24d --- /dev/null +++ b/src/web/core/component.php @@ -0,0 +1,119 @@ +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 new file mode 100644 index 0000000..340bbb1 --- /dev/null +++ b/src/web/core/controller.php @@ -0,0 +1,43 @@ +load_controller('error'); + $error_controller->code($code); + die(); + } + +} diff --git a/src/web/core/core.php b/src/web/core/core.php new file mode 100644 index 0000000..d71870e --- /dev/null +++ b/src/web/core/core.php @@ -0,0 +1,85 @@ +'; + } + + /** + * 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 ""; + } 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:m", strtotime($iso_date)); + } +} diff --git a/src/web/core/loader.php b/src/web/core/loader.php deleted file mode 100644 index 101abef..0000000 --- a/src/web/core/loader.php +++ /dev/null @@ -1,112 +0,0 @@ -loaded = array(); - $this->db = NULL; - } - - /** - * 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 - */ - public function lang(): void { - $dir = $GLOBALS['webroot'] . '/lang/'; - $lang = $GLOBALS['lang']; - if ($handle = opendir($dir)) { - while (false !== ($entry = readdir($handle))) { - if ($entry === '.' || $entry === '..' || $entry === 'apps') { - continue; - } - $path = $dir . $entry; - require($path); - } - } - $GLOBALS['lang'] = $lang; - } - - /** - * Loads a given app specific lang - * @param string $name - the name of the app - */ - public function app_lang($name): void { - $dir = $GLOBALS['webroot'] . '/lang/apps/'; - $file = $dir . $name . '.php'; - if (file_exists($file)) { - $lang = $GLOBALS['lang']; - require($dir . $name . '.php'); - $GLOBALS['lang'] = $lang; - } - } - - public function db(): DatabaseHelper { - if ($this->db) { - return $this->db; - } else { - $this->db = new DatabaseHelper(); - return $this->db; - } - } - -} diff --git a/src/web/core/model.php b/src/web/core/model.php new file mode 100644 index 0000000..8e105da --- /dev/null +++ b/src/web/core/model.php @@ -0,0 +1,29 @@ +load = $load; - $this->db = $load->db(); - $this->main = $this->load->model('main'); + function __construct() + { $this->db_ready = file_exists('/status/ready'); + $this->recursed = FALSE; + $this->req = $this->get_req(); } /** - * @param string $path - the current request path + * @param string $path - the current request path * Gets the current route * @return array */ - private function get_req_route($path): array { + private function get_req_route($path): array + { // trim the path $path = trim($path); // remove first '/' $path = substr($path, 1); + + // get modified route + foreach ($GLOBALS['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(); // e.g. / - if ($path === '') { + if ($path === '') $route = array( - 'app' => '', + 'app' => 'index', 'slug' => 'index', + 'args' => array(), ); // e.g. /home /login - } else if ($len === 1) { + else if ($len === 1) $route = array( 'app' => $parts[0], 'slug' => 'index', + 'args' => array(), ); // e.g. /home/posts - } else { + else if ($len === 2) $route = array ( - 'app' => implode('/', array_slice($parts, 0, -1)), - 'slug' => end($parts) + 'app' => $parts[0], + 'slug' => $parts[1], + 'args' => array(), + ); + // e.g. /home/posts/joe + else + $route = array ( + 'app' => $parts[0], + 'slug' => $parts[1], + 'args' => array_slice($parts, 2), ); - }; $routes = $GLOBALS['routes']; - if (array_key_exists($route['app'], $routes)) { - $parts = explode('/', $routes[$route['app']]); + if (isset($routes[$path])) { + $parts = explode('/', $routes[$path]); if (count($parts) == 1) { $route['app'] = $parts[0]; } else { @@ -77,7 +100,8 @@ class Router { /** * Gets the users ip */ - private function get_ip(): string { + private function get_ip(): string + { $ip = ''; if (!empty($_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; @@ -93,13 +117,14 @@ class Router { * Gets the curret request info * @return array */ - private function get_req(): array|bool { + private function get_req(): array + { $method = $_SERVER['REQUEST_METHOD']; - $uri_str = $_SERVER['REQUEST_URI']; $uri = parse_url($uri_str); + if (!$uri) { - return FALSE; + $uri = array('path' => '/error'); } $path = ''; @@ -121,138 +146,94 @@ class Router { /** * Handles a router error code * @param int $code - the http error code - * @param bool $recursed */ - private function handle_error(int $code, bool $recursed): void { - if ($recursed) { + private function handle_error(int $code): void + { + if ($this->recursed) die($code . ' (recursed)'); - } - $uri_str = $_SERVER['REQUEST_URI']; - $req = array(); - $req['slug'] = 'index'; - $req['app'] = 'error'; - $req['uri_str'] = $uri_str; - $this->main->info = $req; - $_GET['code'] = $code; - $this->handle_req($req, TRUE); + + $this->recursed = TRUE; + $this->req['app'] = 'error'; + $this->req['slug'] = 'code'; + $this->req['args'] = array($code); + $this->handle_req(); } - /** - * @param array $req - */ - private function load_htc(array $req, bool $recursed): void { + /** + * @param array $req + */ + private function load_htc(array $req): void + { $parts = explode('/', $req['uri_str']); $file = end($parts); - $path = $GLOBALS['publicroot'] . '/polyfills/' . $file; + $path = PUBLIC_ROOT . '/polyfills/' . $file; if (file_exists($path)) { header('Content-type: text/x-component'); include($path); } else { - $this->handle_error(400, $recursed); + $this->handle_error(400); } } /** * @param array $req - * @param array $req - * @param bool $recursed + * @param array $req */ - private function handle_req(array $req, bool $recursed = FALSE): void { - - if ($recursed === false) { - if ( - $this->db_ready === false && - in_array($req['app'], $GLOBALS['serviceable']) === false - ) { - $this->handle_error(503, $recursed); - return; - } - - if ($this->check_banned($req)) { - $this->handle_error(401, $recursed); + 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; } } - if (!$req) { - $this->handle_error(500, $recursed); + // server error if we cannot parse url + if (!$this->req) { + $this->handle_error(500); return; } - if (str_ends_with($req['uri_str'], '.htc')) { - $this->load_htc($req, $recursed); + // 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; } - $controller = $this->load->controller($req['app']); - + // load the controller + $controller = $this->load_controller($this->req['app']); if ($controller === NULL) { - $this->handle_error(404, $recursed); + $this->handle_error(404); return; } $ref = NULL; try { - $ref = new ReflectionMethod($controller, $req['slug']); + $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 || !$ref->isPublic()) { - $this->handle_error(404, $recursed); - return; - - } - - $ref->invoke($controller); - } - - /** - * @param array $req - */ - private function log_request(array $req): void { - if ( - $req === FALSE || - $this->db_ready === FALSE || - in_array($req['app'], $GLOBALS['serviceable']) - ) { + if ($ref === NULL) { + $this->handle_error(404); return; } - $query = $this->db - ->insert_into('admin.request_log', - 'ip', 'method', 'uri') - ->values( - $req['ip'], $req['method'], $req['uri_str']); - - $query->execute(); - } - - /** - * @param array $req - */ - private function check_banned(array $req): bool { - $ip = FALSE; - if ($req) { - $ip = $req['ip']; - } else { - $ip = $this->get_ip(); - } - $query = $this->db - ->select('TRUE') - ->from('admin.banned') - ->where('ip')->eq($ip); - - return !!($query->row()); - } - - /** - * Handels the incomming reuqest - */ - public function handle_request(): void { - $req = $this->get_req(); - $this->log_request($req); - $this->main->info = $req; - $this->handle_req($req); + define('CONTEXT', $this->req); + Component::load_lang('common', $this->req['app']); + $ref->invokeArgs($controller, $this->req['args']); } } diff --git a/src/web/helpers/aria.php b/src/web/helpers/aria.php index 9782299..0e06b97 100644 --- a/src/web/helpers/aria.php +++ b/src/web/helpers/aria.php @@ -1,6 +1,7 @@ conn = $conn; $this->query = ''; @@ -31,7 +33,8 @@ class DatabaseQuery { /// ARBITRARY QUERY /// - public function query(string $query): DatabaseQuery { + public function query(string $query): DatabaseQuery + { $this->query .= $query; return $this; } @@ -40,12 +43,14 @@ class DatabaseQuery { /// SELECT /// - public function select(string $select): DatabaseQuery { + public function select(string $select): DatabaseQuery + { $this->query .= "SELECT $select\n"; return $this; } - public function from(string $from): DatabaseQuery { + public function from(string $from): DatabaseQuery + { $this->query .= "FROM $from\n"; return $this; } @@ -54,7 +59,8 @@ class DatabaseQuery { /// INSERT /// - public function insert_into(string $insert, string ...$columns): DatabaseQuery { + public function insert_into(string $insert, string ...$columns): DatabaseQuery + { $this->query .= "INSERT INTO $insert\n ("; foreach ($columns as $idx => $column) { if ($idx !== 0) { @@ -66,7 +72,8 @@ class DatabaseQuery { return $this; } - public function values(mixed ...$values): DatabaseQuery { + public function values(mixed ...$values): DatabaseQuery + { $this->query .= "VALUES ("; foreach ($values as $idx => $value) { if ($idx !== 0) { @@ -83,7 +90,8 @@ class DatabaseQuery { /// WHERE /// - public function where(string $cond): DatabaseQuery { + public function where(string $cond): DatabaseQuery + { if (!$this->where) { $this->where = TRUE; $this->query .= "WHERE "; @@ -97,7 +105,8 @@ class DatabaseQuery { /** * @param array $array */ - public function where_in(string $column, array $array): DatabaseQuery { + public function where_in(string $column, array $array): DatabaseQuery + { if (!$this->where) { $this->where = TRUE; $this->query .= "WHERE "; @@ -116,7 +125,8 @@ class DatabaseQuery { /** * @param array $array */ - private function in(array $array): DatabaseQuery { + private function in(array $array): DatabaseQuery + { $in = 'IN ('; foreach ($array as $idx => $item) { if ($idx != 0) { @@ -133,31 +143,36 @@ class DatabaseQuery { /// OPERATORS /// - public function like(mixed $item): DatabaseQuery { + public function like(mixed $item): DatabaseQuery + { $this->query .= "LIKE ?\n"; array_push($this->param, $item); return $this; } - public function eq(mixed $item): DatabaseQuery { + public function eq(mixed $item): DatabaseQuery + { $this->query .= "= ?\n"; array_push($this->param, $item); return $this; } - public function ne(mixed $item): DatabaseQuery { + public function ne(mixed $item): DatabaseQuery + { $this->query .= "<> ?\n"; array_push($this->param, $item); return $this; } - public function lt(mixed $item): DatabaseQuery { + public function lt(mixed $item): DatabaseQuery + { $this->query .= "< ?\n"; array_push($this->param, $item); return $this; } - public function le(mixed $item): DatabaseQuery { + public function le(mixed $item): DatabaseQuery + { $this->query .= "<= ?\n"; array_push($this->param, $item); return $this; @@ -167,7 +182,8 @@ class DatabaseQuery { /// JOINS /// - public function join(string $table, string $on, string $type = 'LEFT'): DatabaseQuery { + public function join(string $table, string $on, string $type = 'LEFT'): DatabaseQuery + { $this->query .= "$type JOIN $table ON $on\n"; return $this; } @@ -176,19 +192,22 @@ class DatabaseQuery { /// LIMIT, OFFSET, ORDER /// - public function limit(int $limit): DatabaseQuery { + public function limit(int $limit): DatabaseQuery + { $this->query .= "LIMIT ?\n"; array_push($this->param, $limit); return $this; } - public function offset(int $offset): DatabaseQuery { + public function offset(int $offset): DatabaseQuery + { $this->query .= "OFFSET ?\n"; array_push($this->param, $offset); return $this; } - public function order_by(string $column, string $order = 'ASC'): DatabaseQuery { + public function order_by(string $column, string $order = 'ASC'): DatabaseQuery + { $this->query .= "ORDER BY " . $column . ' ' . $order . ' '; return $this; } @@ -197,7 +216,8 @@ class DatabaseQuery { /// COLLECT /// - public function rows(mixed ...$params): ?array { + public function rows(mixed ...$params): ?array + { $args = $this->param; foreach ($params as $param) { array_push($args, $param); @@ -213,7 +233,8 @@ class DatabaseQuery { return __nullify($stmt->fetchAll(PDO::FETCH_ASSOC)); } - public function row(mixed ...$params): ?array { + public function row(mixed ...$params): ?array + { $args = $this->param; foreach ($params as $param) { array_push($args, $param); @@ -223,7 +244,8 @@ class DatabaseQuery { return __nullify($stmt->fetch(PDO::FETCH_ASSOC)); } - public function execute(mixed ...$params): bool { + public function execute(mixed ...$params): bool + { $args = $this->param; foreach ($params as $param) { array_push($args, $param); @@ -250,11 +272,13 @@ class DatabaseHelper { private ?\PDO $conn; - function __construct() { + function __construct() + { $this->conn = NULL; } - private function connect(): \PDO { + private function connect(): \PDO + { if ($this->conn === NULL) { $user = getenv("POSTGRES_USER"); $pass = getenv("POSTGRES_PASSWORD"); @@ -275,19 +299,22 @@ class DatabaseHelper { return $this->conn; } - public function select(string $select): DatabaseQuery { + public function select(string $select): DatabaseQuery + { $conn = $this->connect(); $query = new DatabaseQuery($conn); return $query->select($select); } - public function insert_into(string $insert, string ...$columns): DatabaseQuery { + public function insert_into(string $insert, string ...$columns): DatabaseQuery + { $conn = $this->connect(); $query = new DatabaseQuery($conn); return $query->insert_into($insert, ...$columns); } - public function query(string $query_str): DatabaseQuery { + public function query(string $query_str): DatabaseQuery + { $conn = $this->connect(); $query = new DatabaseQuery($conn); return $query->query($query_str); diff --git a/src/web/helpers/image.php b/src/web/helpers/image.php index fd395b5..ec867b8 100644 --- a/src/web/helpers/image.php +++ b/src/web/helpers/image.php @@ -1,6 +1,7 @@ get_url('public/' . $name . '.' . $format, TRUE); + $path = Core::get_url('public/' . $name . '.' . $format, TRUE); $mime = __get_mime($format); return sprintf('', $mime, $path, $media); @@ -45,8 +45,8 @@ function image( $height = NULL, $width = NULL, - $size = NULL, -) :string { + $size = NULL) :string +{ if ($animated === TRUE) { $animated = array('gif'); @@ -68,8 +68,7 @@ function image( } $format = end($formats); - $main = $GLOBALS['main_model']; - $path = $main->get_url('public/' . $name . '.' . $format, TRUE); + $path = Core::get_url('public/' . $name . '.' . $format, TRUE); $out .= " $sub @@ -8,7 +8,7 @@ function lang( string $key, ?string $default = NULL, ?array $sub = NULL) { - $lang = $GLOBALS['lang']; + $lang = $GLOBALS['__lang']; if(array_key_exists($key, $lang)) { if ($sub) { return sprintf($lang[$key], ...$sub); diff --git a/src/web/index.php b/src/web/index.php index eedf913..51ec652 100644 --- a/src/web/index.php +++ b/src/web/index.php @@ -1,39 +1,57 @@ handle_request(); + $router = new Router(); + $router->handle_req(); }; __init(); diff --git a/src/web/lang/apps/blog.php b/src/web/lang/blog.php similarity index 100% rename from src/web/lang/apps/blog.php rename to src/web/lang/blog.php diff --git a/src/web/lang/apps/error.php b/src/web/lang/error.php similarity index 100% rename from src/web/lang/apps/error.php rename to src/web/lang/error.php diff --git a/src/web/lang/apps/home.php b/src/web/lang/home.php similarity index 100% rename from src/web/lang/apps/home.php rename to src/web/lang/home.php diff --git a/src/web/lang/apps/projects.php b/src/web/lang/projects.php similarity index 100% rename from src/web/lang/apps/projects.php rename to src/web/lang/projects.php