diff options
author | Freya Murphy <freya@freyacat.org> | 2024-09-18 14:14:53 -0400 |
---|---|---|
committer | Freya Murphy <freya@freyacat.org> | 2024-09-18 14:48:54 -0400 |
commit | 1f9024763d9224c4cd9a181bac27e6b9f12ad672 (patch) | |
tree | 00f827470dad9aa2692483acbdef9502c1a464d3 /src/web/_model | |
parent | fix rss (diff) | |
download | website-1f9024763d9224c4cd9a181bac27e6b9f12ad672.tar.gz website-1f9024763d9224c4cd9a181bac27e6b9f12ad672.tar.bz2 website-1f9024763d9224c4cd9a181bac27e6b9f12ad672.zip |
refactor
Diffstat (limited to 'src/web/_model')
-rw-r--r-- | src/web/_model/_comments.php | 33 | ||||
-rw-r--r-- | src/web/_model/blog.php | 69 | ||||
-rw-r--r-- | src/web/_model/bucket.php | 19 | ||||
-rw-r--r-- | src/web/_model/error.php | 34 | ||||
-rw-r--r-- | src/web/_model/main.php | 110 | ||||
-rw-r--r-- | src/web/_model/projects.php | 20 |
6 files changed, 83 insertions, 202 deletions
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 @@ <?php /* Copyright (c) 2024 Freya Murphy */ class _comments_model extends Model { - function __construct($load) { - parent::__construct($load); - } - - private function load_profanity() { - $path = $GLOBALS['assetroot'] . '/profanity.txt'; + private function load_profanity() + { + $path = ASSET_ROOT . '/profanity.txt'; $str = file_get_contents($path); $lines = explode("\n", $str); @@ -25,14 +22,16 @@ class _comments_model extends Model { return $regex; } - public function is_vulgar($text) { + public function is_vulgar($text) + { $profanity = $this->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|<missing> - */ - private function load_post($name): ?array { - $dir = $GLOBALS['assetroot'] . '/blog'; - $path = $dir . '/' . $name; + /** + * @param mixed $name + * @return bool|<missing> + */ + 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 @@ <?php /* Copyright (c) 2024 Freya Murphy */ class Bucket_model extends Model { - function __construct($load) { - parent::__construct($load); - } - - public function get_data(): ?array { - $data = parent::get_data(); + public function get_data(): ?array + { + $data = parent::get_base_data(); - if (array_key_exists('name', $_GET)) { + if (array_key_exists('name', $_GET)) $data['name'] = $_GET['name']; - } else { + else return NULL; - } - if (array_key_exists('lightmode', $_GET)) { + if (array_key_exists('lightmode', $_GET)) $data['lightmode'] = $_GET['lightmode']; - } else { + else $data['lightmode'] = 'false'; - } return $data; } diff --git a/src/web/_model/error.php b/src/web/_model/error.php index 0a08fdd..11b56f9 100644 --- a/src/web/_model/error.php +++ b/src/web/_model/error.php @@ -1,30 +1,22 @@ <?php /* Copyright (c) 2024 Freya Murphy */ class Error_model extends Model { - function __construct($load) { - parent::__construct($load); - } - - private function get_msg(&$data) { - if (!array_key_exists('code', $_GET)) { - http_response_code(500); - $data['msg'] = ucfirst(lang('error')); - $data['title'] = '500'; - } else { - $code = $_GET['code']; - http_response_code($code); - $data['title'] = $code; - $msg = ucfirst(lang('error_' . $code, FALSE)); - if (!$msg) { - $msg = ucfirst(lang('error')); - } - $data['msg'] = $msg; + private function get_msg(&$data, int $code) + { + http_response_code($code); + $data['title'] = $code; + $msg = ucfirst(lang('error_' . $code, FALSE)); + if (!$msg) { + $msg = ucfirst(lang('error')); } + $data['msg'] = $msg; + } - public function get_data(): ?array { - $data = parent::get_data(); - $this->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 @@ -<?php /* Copyright (c) 2024 Freya Murphy */ -class Main_model extends Model { - - // stores the current request info - public mixed $info; - - // the main loader - public Loader $load; - - /** - * Loads the main model - * @param Loader $load - the main loader object - */ - function __construct(Loader $load) { - parent::__construct($load, TRUE); - $GLOBALS['main_model'] = $this; - } - - /** - * Gets the stamp for a asset path - * @param string $path - */ - private function asset_stamp(string $path): int { - $root = $GLOBALS['webroot']; - $path = $root . '/../public/' . $path; - return @filemtime($path); - } - - /** - * Get the current IE version - * @returns the IE version if valid IE user agent, INT_MAX if not - */ - public function get_ie_version(): int { - if (preg_match('/MSIE\s(?P<v>\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 '<link rel="stylesheet" href="'. $href .'">'; - } - - /** - * 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 "<style>{$text}</style>"; - } 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<string,mixed> $data - */ - private function load_projects(&$data): void { + /** + * @param array<string,mixed> $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'); |