From f373ead95fb5beb962c376b5b7b46dfde8ac4e57 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Mon, 23 Feb 2026 22:57:27 -0500 Subject: update website to work with crimson framework --- src/web/_controller/_comments.php | 4 +- src/web/_controller/_meta.php | 4 + src/web/_controller/blog.php | 1 + src/web/_controller/bucket.php | 1 + src/web/_controller/error.php | 3 +- src/web/_controller/home.php | 10 +- src/web/_controller/projects.php | 1 + src/web/_model/_comments.php | 26 ++- src/web/_model/blog.php | 7 +- src/web/_model/bucket.php | 5 +- src/web/_model/error.php | 11 +- src/web/_model/home.php | 10 ++ src/web/_model/projects.php | 3 +- src/web/_views/head.php | 2 +- src/web/_views/header.php | 6 +- src/web/config.php | 41 +++-- src/web/core/component.php | 119 -------------- src/web/core/controller.php | 65 -------- src/web/core/core.php | 109 ------------- src/web/core/model.php | 39 ----- src/web/core/router.php | 208 ------------------------ src/web/helpers/aria.php | 17 -- src/web/helpers/database.php | 323 -------------------------------------- src/web/helpers/ie.php | 32 ---- src/web/helpers/image.php | 94 ----------- src/web/helpers/lang.php | 90 ----------- src/web/helpers/markdown.php | 36 ----- src/web/helpers/sanitize.php | 8 - src/web/index.php | 48 ------ src/web/lang/error.php | 58 ------- src/web/lib/aria.php | 17 ++ src/web/lib/hooks.php | 10 ++ src/web/lib/ie.php | 32 ++++ src/web/lib/image.php | 96 +++++++++++ src/web/lib/markdown.php | 36 +++++ 35 files changed, 287 insertions(+), 1285 deletions(-) create mode 100644 src/web/_model/home.php delete mode 100644 src/web/core/component.php delete mode 100644 src/web/core/controller.php delete mode 100644 src/web/core/core.php delete mode 100644 src/web/core/model.php delete mode 100644 src/web/core/router.php delete mode 100644 src/web/helpers/aria.php delete mode 100644 src/web/helpers/database.php delete mode 100644 src/web/helpers/ie.php delete mode 100644 src/web/helpers/image.php delete mode 100644 src/web/helpers/lang.php delete mode 100644 src/web/helpers/markdown.php delete mode 100644 src/web/helpers/sanitize.php delete mode 100644 src/web/index.php create mode 100644 src/web/lib/aria.php create mode 100644 src/web/lib/hooks.php create mode 100644 src/web/lib/ie.php create mode 100644 src/web/lib/image.php create mode 100644 src/web/lib/markdown.php (limited to 'src/web') diff --git a/src/web/_controller/_comments.php b/src/web/_controller/_comments.php index 3b8fe35..c81ae9d 100644 --- a/src/web/_controller/_comments.php +++ b/src/web/_controller/_comments.php @@ -5,6 +5,7 @@ class _comments_controller extends Controller { function __construct() { $this->comments_model = $this->load_model('_comments'); + $this->load_lang('common'); } public function comments(string $page, string $ref): void { @@ -12,7 +13,8 @@ class _comments_controller extends Controller { $this->view('comments', array( 'comments' => $data, 'ref' => $ref, - 'page' => $page + 'page' => $page, + 'ip', $this->comments_model->get_ip(), )); } diff --git a/src/web/_controller/_meta.php b/src/web/_controller/_meta.php index 9ea3f47..ac9f682 100644 --- a/src/web/_controller/_meta.php +++ b/src/web/_controller/_meta.php @@ -1,6 +1,10 @@ load_lang('common'); + } + public function robots(): void { header("Content-Type: text/plain"); diff --git a/src/web/_controller/blog.php b/src/web/_controller/blog.php index 2ea45df..9cb93e5 100644 --- a/src/web/_controller/blog.php +++ b/src/web/_controller/blog.php @@ -8,6 +8,7 @@ class Blog_controller extends Controller { { $this->blog_model = $this->load_model('blog'); $this->comments_controller = $this->load_controller('_comments'); + $this->load_lang('common', 'blog'); } public function index(): void diff --git a/src/web/_controller/bucket.php b/src/web/_controller/bucket.php index e14f9b6..460f862 100644 --- a/src/web/_controller/bucket.php +++ b/src/web/_controller/bucket.php @@ -6,6 +6,7 @@ class Bucket_controller extends Controller { function __construct() { $this->bucket_model = $this->load_model('bucket'); + $this->load_lang('common', 'bucket'); } public function index(): void diff --git a/src/web/_controller/error.php b/src/web/_controller/error.php index 9efbb7e..ed89a89 100644 --- a/src/web/_controller/error.php +++ b/src/web/_controller/error.php @@ -6,6 +6,7 @@ class Error_controller extends Controller { function __construct() { $this->error_model = $this->load_model('error'); + $this->load_lang('common', 'error'); } public function code(int $code): void @@ -18,7 +19,7 @@ class Error_controller extends Controller { parent::index(); $this->load_lang('error'); - $data = $this->error_model->get_data($code); + $data = $this->error_model->get_error_data($code); $this->view('header', $data); $this->view('error/main', $data); $this->view('footer', $data); diff --git a/src/web/_controller/home.php b/src/web/_controller/home.php index 89e67e8..28ccd1e 100644 --- a/src/web/_controller/home.php +++ b/src/web/_controller/home.php @@ -1,11 +1,19 @@ home_model = $this->load_model('home'); + $this->load_lang('common', 'home'); + } + public function index(): void { parent::index(); - $data = Model::get_base_data(); + $data = $this->home_model->get_data(); $this->view('header', $data); $this->view('home/main', $data); $this->view('footer', $data); diff --git a/src/web/_controller/projects.php b/src/web/_controller/projects.php index fea4664..35274b5 100644 --- a/src/web/_controller/projects.php +++ b/src/web/_controller/projects.php @@ -6,6 +6,7 @@ class Projects_controller extends Controller { function __construct() { $this->projects_model = $this->load_model('projects'); + $this->load_lang('common', 'projects'); } public function index(): void diff --git a/src/web/_model/_comments.php b/src/web/_model/_comments.php index 3518508..765fda8 100644 --- a/src/web/_model/_comments.php +++ b/src/web/_model/_comments.php @@ -36,10 +36,10 @@ class _comments_model extends Model { public function get_comments($page) { - $ip = CONTEXT['ip']; + $ip = $this->get_ip(); $query = $this->db() ->select('*') - ->from('admin.comment c') + ->from('website.comment c') ->where('c.page') ->eq($page) ->query('AND ( @@ -53,12 +53,30 @@ class _comments_model extends Model { public function post_comment($author, $content, $page, $vulgar) { - $ip = CONTEXT['ip']; + $ip = $this->get_ip(); return $this->db() - ->insert_into('admin.comment', + ->insert_into('website.comment', 'author', 'content', 'page', 'ip', 'vulgar') ->values($author, $content, $page, $ip, $vulgar) ->execute(); } + public function get_ip(): ?string + { + $headers = array ( + 'HTTP_CLIENT_IP', + 'HTTP_X_FORWARDED_FOR', + 'HTTP_X_FORWARDED', + 'HTTP_FORWARDED_FOR', + 'HTTP_FORWARDED', + 'HTTP_X_REAL_IP', + 'REMOTE_ADDR' + ); + foreach ($headers as $header) { + if (isset($_SERVER[$header])) + return $_SERVER[$header]; + } + return NULL; + } + } diff --git a/src/web/_model/blog.php b/src/web/_model/blog.php index 94ca049..6446564 100644 --- a/src/web/_model/blog.php +++ b/src/web/_model/blog.php @@ -30,7 +30,7 @@ class Blog_model extends Model { } public function get_data(): array { - $data = parent::get_base_data('blog'); + $data = parent::get_data(); $this->load_blog($data); $data['title'] = lang('title'); $data['desc'] = lang('blog_short_desc'); @@ -68,7 +68,7 @@ class Blog_model extends Model { * @return bool|null|array */ public function get_post($name): ?array { - $data = parent::get_base_data(); + $data = parent::get_data(); $post = $this->render_post($name); if (!$post) { return NULL; @@ -97,7 +97,7 @@ class Blog_model extends Model { * @return bool|null|array */ public function get_writeup($name): ?array { - $data = parent::get_base_data(); + $data = parent::get_data(); $writeup = $this->render_writeup($name); if (!$writeup) { return NULL; @@ -108,4 +108,3 @@ class Blog_model extends Model { return $data; } } -?> diff --git a/src/web/_model/bucket.php b/src/web/_model/bucket.php index 374836e..7c426b7 100644 --- a/src/web/_model/bucket.php +++ b/src/web/_model/bucket.php @@ -3,7 +3,9 @@ class Bucket_model extends Model { public function get_data(): ?array { - $data = parent::get_base_data(); + $data = parent::get_data(); + $data['title'] = lang('first_name'); + $data['desc'] = lang('default_short_desc'); if (array_key_exists('name', $_GET)) $data['name'] = $_GET['name']; @@ -18,4 +20,3 @@ class Bucket_model extends Model { return $data; } } -?> diff --git a/src/web/_model/error.php b/src/web/_model/error.php index 08046f3..74bbd20 100644 --- a/src/web/_model/error.php +++ b/src/web/_model/error.php @@ -5,21 +5,22 @@ class Error_model extends Model { { http_response_code($code); $data['title'] = $code; - $msg = ucfirst(lang('error_' . $code, FALSE)); + $msg = status_code_msg($code); + if (!$msg && $code == 0) + $msg = lang('error_0'); if (!$msg) { $error_text = lang('error', NULL); $msg = $error_text[array_rand($error_text)]; $msg = sprintf($msg, $code); } $data['msg'] = $msg; - + $data['desc'] = $msg; } - public function get_data(int $code): array + public function get_error_data(int $code): array { - $data = parent::get_base_data('error'); + $data = parent::get_data(); $this->get_msg($data, $code); return $data; } } -?> diff --git a/src/web/_model/home.php b/src/web/_model/home.php new file mode 100644 index 0000000..d1041db --- /dev/null +++ b/src/web/_model/home.php @@ -0,0 +1,10 @@ +load_projects($data); $data['title'] = lang('title'); $data['desc'] = lang('short_desc'); return $data; } } -?> diff --git a/src/web/_views/head.php b/src/web/_views/head.php index ab7bb1b..1ca6954 100644 --- a/src/web/_views/head.php +++ b/src/web/_views/head.php @@ -23,7 +23,7 @@ req['app'] != 'bucket') echo $this->link_css('css/main.css'); /* IE 4-7 Styles */ diff --git a/src/web/_views/header.php b/src/web/_views/header.php index 90a31ea..f6b4740 100644 --- a/src/web/_views/header.php +++ b/src/web/_views/header.php @@ -17,15 +17,15 @@
  • get_url('home'), - container: 'h2' + subtype: 'h2' )?>
  • get_url('projects'), - container: 'h2' + subtype: 'h2' )?>
  • get_url('blog'), - container: 'h2' + subtype: 'h2' )?>
  • diff --git a/src/web/config.php b/src/web/config.php index 8689b8e..4a4e830 100644 --- a/src/web/config.php +++ b/src/web/config.php @@ -1,26 +1,33 @@ 'home' sends / to /home +// +// style - single or list of css styles to load on specific routes +// +// js - single or list of js script to load on specific routes +// +// autoload - list of directories to autoload all PHP files in them +// +define('SITE_CONFIG', array( /* core config settings */ - 'domain' => 'freya.cat', - 'allowed_hosts' => ['freya.cat', 'www.freya.cat'], - 'base_path' => '/', + 'domain' => getenv("WEBSITE_HOST"), + 'allowed_hosts' => explode(" ", getenv("WEBSITE_ALLOWED_HOSTS")), + 'base_path' => getenv("WEBSITE_BASE_PATH"), 'theme_color' => '#181818', - 'git_url' => 'https://g.freya.cat/freya', + 'git_url' => 'https://g.freya.cat', /* route overides */ 'routes' => array( '' => 'home', @@ -39,4 +46,8 @@ define('CONFIG', array( 'js' => array( 'blog' => 'js/prism.js', ), + /* directories to autoload php code */ + 'autoload' => array('/third_party', '/lib'), )); + +define('ASSET_ROOT', PHP_ROOT . '/assets'); diff --git a/src/web/core/component.php b/src/web/core/component.php deleted file mode 100644 index 376e24d..0000000 --- a/src/web/core/component.php +++ /dev/null @@ -1,119 +0,0 @@ -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 deleted file mode 100644 index ca892e2..0000000 --- a/src/web/core/controller.php +++ /dev/null @@ -1,65 +0,0 @@ -load_controller('error'); - $error_controller->code($code); - die(); - } - - /** - * Returns HTTP POST information if POST request. - * Returns 405 Method Not Allowed if not. - * - * If $key is specified, returns only that key. otherwise - * returns HTTP 400 Bad Request; - */ - protected function post_data(?string $key = NULL): array|string - { - // only post requests allowed - if ($_SERVER['REQUEST_METHOD'] != 'POST') - $this->error(405); - - // return entire $_POST array - if (!$key) - return $_POST; - - if (!isset($_POST[$key])) - $this->error(400); - - return $_POST[$key]; - } - -} diff --git a/src/web/core/core.php b/src/web/core/core.php deleted file mode 100644 index 4c341c2..0000000 --- a/src/web/core/core.php +++ /dev/null @@ -1,109 +0,0 @@ -'; - } - - /** - * Loads a css html link - * @param string $path - the path to the css file - */ - public static function link_css(string $path): string - { - $stamp = Core::asset_stamp("public/$path"); - $href = Core::get_url("public/{$path}?timestamp={$stamp}"); - return ''; - } - - /** - * 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:i", strtotime($iso_date)) . ' EST'; - } -} diff --git a/src/web/core/model.php b/src/web/core/model.php deleted file mode 100644 index 6a42a98..0000000 --- a/src/web/core/model.php +++ /dev/null @@ -1,39 +0,0 @@ - - */ - public static function get_base_data(?string $app = NULL): array - { - $data = array(); - $data['title'] = lang('first_name'); - $data['desc'] = lang('default_short_desc'); - $data['css'] = array(); - $data['js'] = array(); - - $style = CONFIG['style']; - $js = CONFIG['js']; - - if (!$app) - $app = CONTEXT['app']; - - if (isset($style[$app])) { - $css = $style[$app]; - if (!is_array($css)) - $css = array($css); - $data['css'] = $css; - } - - if (isset($js[$app])) { - $js = $js[$app]; - if (!is_array($js)) - $js = array($js); - $data['js'] = $js; - } - - return $data; - } - -} diff --git a/src/web/core/router.php b/src/web/core/router.php deleted file mode 100644 index 6a543aa..0000000 --- a/src/web/core/router.php +++ /dev/null @@ -1,208 +0,0 @@ -db_ready = file_exists('/status/ready'); - $this->recursed = FALSE; - $this->req = $this->get_req(); - } - - /** - * @param string $path - the current request path - * Gets the current route - * @return array - */ - private function get_req_route($path): array - { - // trim the path - $path = trim($path); - // remove first '/' - $path = substr($path, 1); - - // get modified route - $routes = CONFIG['routes']; - foreach ($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(); - $route['app'] = $len > 0 ? $parts[0] : 'index'; - $route['slug'] = $len > 1 ? $parts[1] : 'index'; - $route['args'] = array_slice($parts, 2); - - return $route; - } - - /** - * Gets the users ip - */ - private function get_ip(): ?string - { - $headers = array ( - 'HTTP_CLIENT_IP', - 'HTTP_X_FORWARDED_FOR', - 'HTTP_X_FORWARDED', - 'HTTP_FORWARDED_FOR', - 'HTTP_FORWARDED', - 'HTTP_X_REAL_IP', - 'REMOTE_ADDR' - ); - foreach ($headers as $header) { - if (isset($_SERVER[$header])) - return $_SERVER[$header]; - } - return NULL; - } - - /** - * Gets the curret request info - * @return array - */ - private function get_req(): array - { - $method = $_SERVER['REQUEST_METHOD']; - $uri_str = $_SERVER['REQUEST_URI']; - $uri = parse_url($uri_str); - - if (!$uri) { - $uri = array('path' => '/error'); - } - - $path = ''; - if (array_key_exists('path', $uri)) { - $path = $uri['path']; - } - - return array_merge( - array( - 'uri' => $uri, - 'uri_str' => $uri_str, - 'method' => $method, - 'ip' => $this->get_ip() - ), - $this->get_req_route($path), - ); - } - - /** - * Handles a router error code - * @param int $code - the http error code - */ - private function handle_error(int $code): void - { - if ($this->recursed) - die($code . ' (recursed)'); - - $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): void - { - $parts = explode('/', $req['uri_str']); - $file = end($parts); - $path = PUBLIC_ROOT . '/polyfills/' . $file; - - if (file_exists($path)) { - header('Content-type: text/x-component'); - include($path); - } else { - $this->handle_error(400); - } - } - - /** - * @param array $req - * @param array $req - */ - 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; - } - } - - // server error if we cannot parse url - if (!$this->req) { - $this->handle_error(500); - return; - } - - // 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; - } - - // load the controller - $controller = $this->load_controller($this->req['app']); - if ($controller === NULL) { - $this->handle_error(404); - return; - } - - $ref = NULL; - try { - $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) { - $this->handle_error(404); - return; - } - - 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 deleted file mode 100644 index 0e06b97..0000000 --- a/src/web/helpers/aria.php +++ /dev/null @@ -1,17 +0,0 @@ -', - $id, $idh); - $out .= sprintf('

    %s

    ', - $idh, $title); - } else { - $out .= sprintf('
    ', - $id); - } - return $out; -} diff --git a/src/web/helpers/database.php b/src/web/helpers/database.php deleted file mode 100644 index 82c711d..0000000 --- a/src/web/helpers/database.php +++ /dev/null @@ -1,323 +0,0 @@ -conn = $conn; - $this->query = ''; - - $this->set = FALSE; - $this->where = FALSE; - $this->param = array(); - } - - /// - /// ARBITRARY QUERY - /// - - public function query(string $query): DatabaseQuery - { - $this->query .= $query; - return $this; - } - - /// - /// SELECT - /// - - public function select(string $select): DatabaseQuery - { - $this->query .= "SELECT $select\n"; - return $this; - } - - public function from(string $from): DatabaseQuery - { - $this->query .= "FROM $from\n"; - return $this; - } - - /// - /// INSERT - /// - - public function insert_into(string $insert, string ...$columns): DatabaseQuery - { - $this->query .= "INSERT INTO $insert\n ("; - foreach ($columns as $idx => $column) { - if ($idx !== 0) { - $this->query .= ","; - } - $this->query .= $column; - } - $this->query .= ")\n"; - return $this; - } - - public function values(mixed ...$values): DatabaseQuery - { - $this->query .= "VALUES ("; - foreach ($values as $idx => $value) { - if ($idx !== 0) { - $this->query .= ","; - } - $this->query .= "?"; - array_push($this->param, $value); - } - $this->query .= ")\n"; - return $this; - } - - /// - /// WHERE - /// - - public function where(string $cond): DatabaseQuery - { - if (!$this->where) { - $this->where = TRUE; - $this->query .= "WHERE "; - } else { - $this->query .= "AND "; - } - $this->query .= "$cond "; - return $this; - } - - /** - * @param array $array - */ - public function where_in(string $column, array $array): DatabaseQuery - { - if (!$this->where) { - $this->where = TRUE; - $this->query .= "WHERE "; - } else { - $this->query .= "AND "; - } - if (empty($array)) { - $this->query .= "FALSE\n"; - return $this; - } - $in = $this->in($array); - $this->query .= "$column $in\n"; - return $this; - } - - /** - * @param array $array - */ - private function in(array $array): DatabaseQuery - { - $in = 'IN ('; - foreach ($array as $idx => $item) { - if ($idx != 0) { - $in .= ","; - } - $in .= "?"; - array_push($this->param, $item); - } - $in .= ")"; - return $in; - } - - /// - /// OPERATORS - /// - - public function like(mixed $item): DatabaseQuery - { - $this->query .= "LIKE ?\n"; - array_push($this->param, $item); - return $this; - } - - public function eq(mixed $item): DatabaseQuery - { - $this->query .= "= ?\n"; - array_push($this->param, $item); - return $this; - } - - public function ne(mixed $item): DatabaseQuery - { - $this->query .= "<> ?\n"; - array_push($this->param, $item); - return $this; - } - - public function lt(mixed $item): DatabaseQuery - { - $this->query .= "< ?\n"; - array_push($this->param, $item); - return $this; - } - - public function le(mixed $item): DatabaseQuery - { - $this->query .= "<= ?\n"; - array_push($this->param, $item); - return $this; - } - - /// - /// JOINS - /// - - public function join(string $table, string $on, string $type = 'LEFT'): DatabaseQuery - { - $this->query .= "$type JOIN $table ON $on\n"; - return $this; - } - - /// - /// LIMIT, OFFSET, ORDER - /// - - public function limit(int $limit): DatabaseQuery - { - $this->query .= "LIMIT ?\n"; - array_push($this->param, $limit); - return $this; - } - - 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 - { - $this->query .= "ORDER BY " . $column . ' ' . $order . ' '; - return $this; - } - - /// - /// COLLECT - /// - - public function rows(mixed ...$params): ?array - { - $args = $this->param; - foreach ($params as $param) { - array_push($args, $param); - } - $stmt = $this->conn->prepare($this->query); - try { - $stmt->execute($args); - } catch (Exception $ex) { - echo $ex; - echo '
    >> caused by <<
    '; - echo str_replace("\n", "
    ", $this->query); - } - return __nullify($stmt->fetchAll(PDO::FETCH_ASSOC)); - } - - public function row(mixed ...$params): ?array - { - $args = $this->param; - foreach ($params as $param) { - array_push($args, $param); - } - $stmt = $this->conn->prepare($this->query); - $stmt->execute($args); - return __nullify($stmt->fetch(PDO::FETCH_ASSOC)); - } - - public function execute(mixed ...$params): bool - { - $args = $this->param; - foreach ($params as $param) { - array_push($args, $param); - } - $stmt = $this->conn->prepare($this->query); - try { - $stmt->execute($args); - return TRUE; - } catch (Exception $_e) { - echo $_e; - echo '
    >> caused by <<
    '; - echo str_replace("\n", "
    ", $this->query); - return FALSE; - } - } -} - -/** - * DatabaseHelper - * allows queries on the - * postgres database - */ -class DatabaseHelper { - - private ?\PDO $conn; - - function __construct() - { - $this->conn = NULL; - } - - private function connect(): \PDO - { - if ($this->conn === NULL) { - $user = getenv("POSTGRES_USER"); - $pass = getenv("POSTGRES_PASSWORD"); - $db = getenv("POSTGRES_DB"); - $host = 'db'; - $port = '5432'; - - $conn_str = sprintf("pgsql:host=%s;port=%d;dbname=%s;user=%s;password=%s", - $host, - $port, - $db, - $user, - $pass - ); - $this->conn = new \PDO($conn_str); - $this->conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); - } - return $this->conn; - } - - 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 - { - $conn = $this->connect(); - $query = new DatabaseQuery($conn); - return $query->insert_into($insert, ...$columns); - } - - 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/ie.php b/src/web/helpers/ie.php deleted file mode 100644 index ead6c1a..0000000 --- a/src/web/helpers/ie.php +++ /dev/null @@ -1,32 +0,0 @@ -\d+)/i', @$_SERVER['HTTP_USER_AGENT'], $B)) { - define('IE_VERSION', $B['v']); - } else { - define('IE_VERSION', FALSE); - } - - if (IE_VERSION == FALSE || IE_VERSION > 4) { - // ADD COND COMMENTS - define('IE_START', ""); - } else { - // IE4 DETECTED, DO NOT ADD COMMENTS - define('IE_START', ''); - define('IE_END', ''); - } -} - -function ie(string $inner) { - return IE_START . $inner . IE_END; -} - -function ie_ua(string $inner, int $ver) { - if (IE_VERSION == $ver) - return $inner; - return ''; -} diff --git a/src/web/helpers/image.php b/src/web/helpers/image.php deleted file mode 100644 index d4683fe..0000000 --- a/src/web/helpers/image.php +++ /dev/null @@ -1,94 +0,0 @@ -', - $mime, $path, $media); -} - -function image( - $name, - $alt, - $formats = array('webp', 'png'), - $animated = FALSE, - $attrs = array(), - - $height = NULL, - $width = NULL, - $size = NULL) :string -{ - - if ($animated === TRUE) { - $animated = array('gif'); - } - - if (!$animated) { - $animated = array(); - } - - $out = ""; - - foreach ($formats as $format) { - $media = count($animated) ? '(prefers-reduced-motion: reduce)' : NULL; - $out .= __make_source($name, $format, $media); - } - - foreach ($animated as $format) { - $out .= __make_source($name, $format, NULL); - } - - $format = end($formats); - $path = Core::get_url('public/' . $name . '.' . $format, TRUE); - $out .= " $value) { - $out .= " $key=\"$value\""; - } - $out .= '>'; - - return $out; -} diff --git a/src/web/helpers/lang.php b/src/web/helpers/lang.php deleted file mode 100644 index 72167fc..0000000 --- a/src/web/helpers/lang.php +++ /dev/null @@ -1,90 +0,0 @@ - $sub - */ -function lang( - string $key, - ?string $default = NULL, - ?array $sub = NULL) { - $lang = $GLOBALS['__lang']; - if(array_key_exists($key, $lang)) { - if ($sub) { - return sprintf($lang[$key], ...$sub); - } else { - return $lang[$key]; - } - } else if ($default !== NULL) { - return $default; - } else { - trigger_error('Undefined lang string: ' . $key, E_USER_WARNING); - return $key; - } -} - -/** - * @param array $attrs - * @param ?array $sub - */ -function ilang( - string $key, - ?string $class = NULL, - ?string $id = NULL, - ?string $href = NULL, - ?string $click = NULL, - array $attrs = array(), - ?array $sub = NULL, - bool $button = FALSE, - string $container = 'span' -) { - $text = ucfirst(lang($key . "_text", FALSE, sub: $sub)); - $tip = lang($key . "_tip", FALSE, sub: $sub); - $icon = lang($key . "_icon", FALSE); - $content = lang($key . "_content", FALSE); - - if ($click || $button) { - echo ''; - } else { - echo ''; - } -} diff --git a/src/web/helpers/markdown.php b/src/web/helpers/markdown.php deleted file mode 100644 index 5279a1f..0000000 --- a/src/web/helpers/markdown.php +++ /dev/null @@ -1,36 +0,0 @@ -parsedown = new ParsedownExtra(); - } - - /** - * @return array - */ - function parse(string $path): array { - $content = file_get_contents($path); - $data = array( - 'meta' => array(), - 'content' => $content - ); - if (str_starts_with($content, '---')) { - $parts = explode('---', $content); - $data['content'] = trim(implode('---', array_slice($parts, 2))); - $meta = array_filter(explode("\n", $parts[1]), fn($x) => $x != ''); - foreach ($meta as $set) { - $parts = explode(": ", $set); - $key = trim($parts[0]); - $value = trim($parts[1]); - $data['meta'][$key] = $value; - } - - } - $data['content'] = $this->parsedown->text($data['content']); - return $data; - } - -} diff --git a/src/web/helpers/sanitize.php b/src/web/helpers/sanitize.php deleted file mode 100644 index 495c249..0000000 --- a/src/web/helpers/sanitize.php +++ /dev/null @@ -1,8 +0,0 @@ -', '>', $data); - return $data; -} diff --git a/src/web/index.php b/src/web/index.php deleted file mode 100644 index 9958b1b..0000000 --- a/src/web/index.php +++ /dev/null @@ -1,48 +0,0 @@ -handle_req(); diff --git a/src/web/lang/error.php b/src/web/lang/error.php index 549700d..449d18f 100644 --- a/src/web/lang/error.php +++ b/src/web/lang/error.php @@ -1,64 +1,6 @@ ', + $id, $idh); + $out .= sprintf('

    %s

    ', + $idh, $title); + } else { + $out .= sprintf('
    ', + $id); + } + return $out; +} diff --git a/src/web/lib/hooks.php b/src/web/lib/hooks.php new file mode 100644 index 0000000..70853e1 --- /dev/null +++ b/src/web/lib/hooks.php @@ -0,0 +1,10 @@ +load_controller('error'); + $error_controller->code($code); + CRIMSON_DIE(); +} diff --git a/src/web/lib/ie.php b/src/web/lib/ie.php new file mode 100644 index 0000000..ead6c1a --- /dev/null +++ b/src/web/lib/ie.php @@ -0,0 +1,32 @@ +\d+)/i', @$_SERVER['HTTP_USER_AGENT'], $B)) { + define('IE_VERSION', $B['v']); + } else { + define('IE_VERSION', FALSE); + } + + if (IE_VERSION == FALSE || IE_VERSION > 4) { + // ADD COND COMMENTS + define('IE_START', ""); + } else { + // IE4 DETECTED, DO NOT ADD COMMENTS + define('IE_START', ''); + define('IE_END', ''); + } +} + +function ie(string $inner) { + return IE_START . $inner . IE_END; +} + +function ie_ua(string $inner, int $ver) { + if (IE_VERSION == $ver) + return $inner; + return ''; +} diff --git a/src/web/lib/image.php b/src/web/lib/image.php new file mode 100644 index 0000000..7b6ec0e --- /dev/null +++ b/src/web/lib/image.php @@ -0,0 +1,96 @@ +', + $mime, $path, $media); +} + +function image( + $name, + $alt, + $formats = array('webp', 'png'), + $animated = FALSE, + $attrs = array(), + + $height = NULL, + $width = NULL, + $size = NULL) :string +{ + + if ($animated === TRUE) { + $animated = array('gif'); + } + + if (!$animated) { + $animated = array(); + } + + $out = ""; + + foreach ($formats as $format) { + $media = count($animated) ? '(prefers-reduced-motion: reduce)' : NULL; + $out .= __make_source($name, $format, $media); + } + + foreach ($animated as $format) { + $out .= __make_source($name, $format, NULL); + } + + $format = end($formats); + $path = Base::get_url('public/' . $name . '.' . $format, TRUE); + $out .= " $value) { + $out .= " $key=\"$value\""; + } + $out .= '>'; + + return $out; +} diff --git a/src/web/lib/markdown.php b/src/web/lib/markdown.php new file mode 100644 index 0000000..5279a1f --- /dev/null +++ b/src/web/lib/markdown.php @@ -0,0 +1,36 @@ +parsedown = new ParsedownExtra(); + } + + /** + * @return array + */ + function parse(string $path): array { + $content = file_get_contents($path); + $data = array( + 'meta' => array(), + 'content' => $content + ); + if (str_starts_with($content, '---')) { + $parts = explode('---', $content); + $data['content'] = trim(implode('---', array_slice($parts, 2))); + $meta = array_filter(explode("\n", $parts[1]), fn($x) => $x != ''); + foreach ($meta as $set) { + $parts = explode(": ", $set); + $key = trim($parts[0]); + $value = trim($parts[1]); + $data['meta'][$key] = $value; + } + + } + $data['content'] = $this->parsedown->text($data['content']); + return $data; + } + +} -- cgit v1.2.3-freya