From 1f9024763d9224c4cd9a181bac27e6b9f12ad672 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Wed, 18 Sep 2024 14:14:53 -0400 Subject: refactor --- 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 ++++---- 6 files changed, 83 insertions(+), 202 deletions(-) delete mode 100644 src/web/_model/main.php (limited to 'src/web/_model') 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'); -- cgit v1.2.3-freya