diff options
author | Freya Murphy <freya@freyacat.org> | 2024-05-24 09:05:42 -0400 |
---|---|---|
committer | Freya Murphy <freya@freyacat.org> | 2024-05-24 09:05:42 -0400 |
commit | c5f39ea2cd7cf02246705ea8872d3b350526165c (patch) | |
tree | 2694f9fdc5d83b529a01f2997c1d89c271c86592 /src/web/_model | |
download | website-c5f39ea2cd7cf02246705ea8872d3b350526165c.tar.gz website-c5f39ea2cd7cf02246705ea8872d3b350526165c.tar.bz2 website-c5f39ea2cd7cf02246705ea8872d3b350526165c.zip |
initial
Diffstat (limited to 'src/web/_model')
-rw-r--r-- | src/web/_model/_comments.php | 66 | ||||
-rw-r--r-- | src/web/_model/blog.php | 80 | ||||
-rw-r--r-- | src/web/_model/bucket.php | 26 | ||||
-rw-r--r-- | src/web/_model/error.php | 31 | ||||
-rw-r--r-- | src/web/_model/main.php | 97 | ||||
-rw-r--r-- | src/web/_model/projects.php | 36 |
6 files changed, 336 insertions, 0 deletions
diff --git a/src/web/_model/_comments.php b/src/web/_model/_comments.php new file mode 100644 index 0000000..73c1fc7 --- /dev/null +++ b/src/web/_model/_comments.php @@ -0,0 +1,66 @@ +<?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'; + $str = file_get_contents($path); + $lines = explode("\n", $str); + + $regex = '/('; + foreach ($lines as $idx => $line) { + if ($line == '') { + continue; + } + if ($idx != 0) { + $regex .= '|'; + } + $regex .= $line; + } + $regex .= ')/'; + + return $regex; + } + + 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 + ->select('*') + ->from('admin.comment c') + ->where('c.page') + ->eq($page) + ->query('AND ( + (c.vulgar IS FALSE) OR + (c.vulgar IS TRUE and c.ip = ?) + )') + ->order_by('c.id', 'DESC'); + $result = $query->rows($ip); + return $result; + } + + public function ban_user() { + $ip = $this->main->info['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 + ->insert_into('admin.comment', + 'author', 'content', 'page', 'ip', 'vulgar') + ->values($author, $content, $page, $ip, $vulgar) + ->execute(); + } + +} diff --git a/src/web/_model/blog.php b/src/web/_model/blog.php new file mode 100644 index 0000000..42cee97 --- /dev/null +++ b/src/web/_model/blog.php @@ -0,0 +1,80 @@ +<?php /* Copyright (c) 2024 Freya Murphy */ +class Blog_model extends Model { + + private $markdown; + + function __construct($load) { + parent::__construct($load); + $this->markdown = new MarkdownParser(); + } + + private function load_blog(&$data) { + $blog = array(); + $dir = $GLOBALS['assetroot'] . '/blog'; + if ($handle = opendir($dir)) { + while (false !== ($entry = readdir($handle))) { + if (str_starts_with($entry, ".")) { + continue; + } + $path = $dir . '/' . $entry; + $md = $this->markdown->parse($path); + $blog[$entry] = $md; + } + } + krsort($blog); + $data['blog'] = $blog; + } + + public function get_data(): ?array { + $data = parent::get_data(); + $this->load_blog($data); + $data['title'] = lang('title'); + $data['desc'] = lang('blog_short_desc'); + return $data; + } + + private function load_post($name) { + $dir = $GLOBALS['assetroot'] . '/blog'; + $path = $dir . '/' . $name; + if(!file_exists($path)) { + return FALSE; + } + $md = $this->markdown->parse($path); + return $md; + } + + public function get_post($name) { + $data = parent::get_data(); + $post = $this->load_post($name); + if (!$post) { + return FALSE; + } + $data['title'] = $post['meta']['name']; + $data['desc'] = $post['meta']['desc']; + $data['post'] = $post; + return $data; + } + + private function load_writeup($name) { + $dir = $GLOBALS['assetroot'] . '/writeup'; + $path = $dir . '/' . $name; + if(!file_exists($path)) { + return FALSE; + } + $md = $this->markdown->parse($path); + return $md; + } + + public function get_writeup($name) { + $data = parent::get_data(); + $writeup = $this->load_writeup($name); + if (!$writeup) { + return FALSE; + } + $data['title'] = $writeup['meta']['name']; + $data['desc'] = $writeup['meta']['desc']; + $data['post'] = $writeup; + return $data; + } +} +?> diff --git a/src/web/_model/bucket.php b/src/web/_model/bucket.php new file mode 100644 index 0000000..f38bebe --- /dev/null +++ b/src/web/_model/bucket.php @@ -0,0 +1,26 @@ +<?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(); + + if (array_key_exists('name', $_GET)) { + $data['name'] = $_GET['name']; + } else { + return NULL; + } + + if (array_key_exists('lightmode', $_GET)) { + $data['lightmode'] = $_GET['lightmode']; + } else { + $data['lightmode'] = 'false'; + } + + return $data; + } +} +?> diff --git a/src/web/_model/error.php b/src/web/_model/error.php new file mode 100644 index 0000000..0a08fdd --- /dev/null +++ b/src/web/_model/error.php @@ -0,0 +1,31 @@ +<?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; + } + } + + public function get_data(): ?array { + $data = parent::get_data(); + $this->get_msg($data); + return $data; + } +} +?> diff --git a/src/web/_model/main.php b/src/web/_model/main.php new file mode 100644 index 0000000..6767010 --- /dev/null +++ b/src/web/_model/main.php @@ -0,0 +1,97 @@ +<?php /* Copyright (c) 2024 Freya Murphy */ +class Main_model extends Model { + + // stores the current request info + public $info; + + // the main loader + public $load; + + /** + * Loads the main model + * @param Loader $load - the main loader object + */ + function __construct($load) { + parent::__construct($load, TRUE); + $GLOBALS['main_model'] = $this; + } + + /** + * Gets the stamp for a asset path + * @param string $path + */ + private function asset_stamp($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. + */ + public function get_url_full($path): string { + $host = $_SERVER['HTTP_HOST']; + $base = lang('base_path'); + $url = "http://{$host}{$base}{$path}"; + return $url; + } + + /** + * Gets a full path url from a relative path + */ + public function get_url($path): string { + if ($this->get_ie_version() <= 7) { + return $this->get_url_full($path); + } + $base = lang('base_path'); + $url = "{$base}{$path}"; + return $url; + } + + /** + * Loads a css html link + * @param string $path - the path to the css file + */ + public function link_css($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($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($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 new file mode 100644 index 0000000..5373a78 --- /dev/null +++ b/src/web/_model/projects.php @@ -0,0 +1,36 @@ +<?php /* Copyright (c) 2024 Freya Murphy */ +class Projects_model extends Model { + + private $markdown; + + function __construct($load) { + parent::__construct($load); + $this->markdown = new MarkdownParser(); + } + + private function load_projects(&$data) { + $projects = array(); + $dir = $GLOBALS['assetroot'] . '/projects'; + if ($handle = opendir($dir)) { + while (false !== ($entry = readdir($handle))) { + if (str_starts_with($entry, ".")) { + continue; + } + $path = $dir . '/' . $entry; + $md = $this->markdown->parse($path); + $projects[$entry] = $md; + } + } + krsort($projects); + $data['projects'] = $projects; + } + + public function get_data(): ?array { + $data = parent::get_data(); + $this->load_projects($data); + $data['title'] = lang('title'); + $data['desc'] = lang('short_desc'); + return $data; + } +} +?> |