diff options
Diffstat (limited to 'src/web/helpers')
| -rw-r--r-- | src/web/helpers/aria.php | 17 | ||||
| -rw-r--r-- | src/web/helpers/database.php | 323 | ||||
| -rw-r--r-- | src/web/helpers/ie.php | 32 | ||||
| -rw-r--r-- | src/web/helpers/image.php | 94 | ||||
| -rw-r--r-- | src/web/helpers/lang.php | 90 | ||||
| -rw-r--r-- | src/web/helpers/markdown.php | 36 | ||||
| -rw-r--r-- | src/web/helpers/sanitize.php | 8 |
7 files changed, 0 insertions, 600 deletions
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 @@ -<?php /* Copyright (c) 2024 Freya Murphy */ - -function aria_section(string $id, ?string $title = NULL): string -{ - $out = ''; - if ($title) { - $idh = $id . '_heading'; - $out .= sprintf('<div id="%s" class="section" role="region" aria-labelledby="%s">', - $id, $idh); - $out .= sprintf('<h2 class="heading" id="%s">%s</h2>', - $idh, $title); - } else { - $out .= sprintf('<div id="%s" class="section" role="region">', - $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 @@ -<?php /* Copyright (c) 2024 Freya Murphy */ - -function __nullify(mixed $val): mixed -{ - if (!$val) { - return NULL; - } else { - return $val; - } -} - -class DatabaseQuery { - - private \PDO $conn; - private string $query; - - private bool $where; - private bool $set; - - private array $param; - - function __construct(\PDO $conn) - { - $this->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<mixed> $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<mixed> $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 '<br> >> caused by <<<br>'; - echo str_replace("\n", "<br>", $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 '<br> >> caused by <<<br>'; - echo str_replace("\n", "<br>", $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 @@ -<?php /* Copyright (c) 2024 Freya Murphy */ - -/// IE 5 though 8 support IE conditional comments -/// IE 4 does not (need to fall back to user agent) - -{ - if (preg_match('/MSIE\s(?P<v>\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', "<!--[if lt IE 8 ]>"); - define('IE_END', "<![endif]-->"); - } 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 @@ -<?php /* Copyright (c) 2024 Freya Murphy */ - -function __get_mime($type) -{ - switch ($type) { - case 'mp4': - return 'video/mp4'; - case 'webm': - return 'video/webm'; - case 'gif': - return 'image/gif'; - case 'png': - return 'image/png'; - case 'jpg': - return 'image/jpeg'; - case 'webp': - return 'image/webp'; - default: - return NULL; - } -} - -function __make_source( - $name, - $format, - $media) -{ - if ($media) { - $media = "media=\"$media\""; - } else { - $media = ''; - } - $path = Core::get_url('public/' . $name . '.' . $format, TRUE); - $mime = __get_mime($format); - return sprintf('<source type="%s" srcset="%s" %s>', - $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 = "<picture>"; - - 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 .= "<img src=\"$path\""; - if ($alt) { - $alt = lang($alt); - $attrs['alt'] = $alt; - $attrs['title'] = $alt; - } - if ($width) { - $attrs['width'] = $width; - } - if ($height) { - $attrs['height'] = $height; - } - if ($size) { - $attrs['width'] = $size; - $attrs['height'] = $size; - } - foreach ($attrs as $key => $value) { - $out .= " $key=\"$value\""; - } - $out .= '></picture>'; - - 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 @@ -<?php /* Copyright (c) 2024 Freya Murphy */ -$__lang = array(); - -/** - * @param ?array<string,mixed> $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<string,string> $attrs - * @param ?array<string,mixed> $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 '<button '; - } else { - echo '<a '; - } - if ($tip) { - echo 'title="' . $tip . '" '; - echo 'aria-label="' . $tip . '" '; - } - if ($class) { - echo 'class="' . $class . '" '; - } - if ($id) { - echo 'id="' . $id . '" '; - } - if ($click) { - echo 'onclick="' . $click . '" '; - } - if ($href) { - echo 'href="' . $href . '" '; - } - foreach ($attrs as $key => $attr) { - echo $key . '="' . $attr . '" '; - } - echo '> '; - if ($icon) { - echo '<i class="' . $icon . '">'; - if ($content) { - echo $content; - } - echo '</i>'; - } - if ($text) { - echo '<' . $container; - if ($icon) { - echo ' class="ml-sm"'; - } - echo '>' . $text . '</' . $container . '>'; - } - if ($click || $button) { - echo '</button>'; - } else { - echo '</a>'; - } -} 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 @@ -<?php /* Copyright (c) 2024 Freya Murphy */ - -class MarkdownParser { - - private $parsedown; - - function __construct() { - $this->parsedown = new ParsedownExtra(); - } - - /** - * @return array<string,mixed> - */ - 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 @@ -<?php /* Copyright (c) 2024 Freya Murphy */ - -function esc(string $data): string { - $data = str_replace('&', '&', $data); - $data = str_replace('<', '<', $data); - $data = str_replace('>', '>', $data); - return $data; -} |