diff options
| author | Freya Murphy <freya@freyacat.org> | 2026-02-23 22:57:27 -0500 |
|---|---|---|
| committer | Freya Murphy <freya@freyacat.org> | 2026-02-23 22:57:27 -0500 |
| commit | f373ead95fb5beb962c376b5b7b46dfde8ac4e57 (patch) | |
| tree | c99df23521ff2a5e5e2e4627c525a5e99dc2e3ae /src/web/helpers | |
| parent | add 96x96 logo (diff) | |
| download | website-f373ead95fb5beb962c376b5b7b46dfde8ac4e57.tar.gz website-f373ead95fb5beb962c376b5b7b46dfde8ac4e57.tar.bz2 website-f373ead95fb5beb962c376b5b7b46dfde8ac4e57.zip | |
update website to work with crimson framework
Diffstat (limited to '')
| -rw-r--r-- | src/web/helpers/database.php | 323 | ||||
| -rw-r--r-- | src/web/helpers/lang.php | 90 | ||||
| -rw-r--r-- | src/web/helpers/sanitize.php | 8 | ||||
| -rw-r--r-- | src/web/lib/aria.php (renamed from src/web/helpers/aria.php) | 0 | ||||
| -rw-r--r-- | src/web/lib/ie.php (renamed from src/web/helpers/ie.php) | 0 | ||||
| -rw-r--r-- | src/web/lib/image.php (renamed from src/web/helpers/image.php) | 6 | ||||
| -rw-r--r-- | src/web/lib/markdown.php (renamed from src/web/helpers/markdown.php) | 0 |
7 files changed, 4 insertions, 423 deletions
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/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/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; -} diff --git a/src/web/helpers/aria.php b/src/web/lib/aria.php index 0e06b97..0e06b97 100644 --- a/src/web/helpers/aria.php +++ b/src/web/lib/aria.php diff --git a/src/web/helpers/ie.php b/src/web/lib/ie.php index ead6c1a..ead6c1a 100644 --- a/src/web/helpers/ie.php +++ b/src/web/lib/ie.php diff --git a/src/web/helpers/image.php b/src/web/lib/image.php index d4683fe..7b6ec0e 100644 --- a/src/web/helpers/image.php +++ b/src/web/lib/image.php @@ -1,5 +1,7 @@ <?php /* Copyright (c) 2024 Freya Murphy */ + + function __get_mime($type) { switch ($type) { @@ -30,7 +32,7 @@ function __make_source( } else { $media = ''; } - $path = Core::get_url('public/' . $name . '.' . $format, TRUE); + $path = Base::get_url('public/' . $name . '.' . $format, TRUE); $mime = __get_mime($format); return sprintf('<source type="%s" srcset="%s" %s>', $mime, $path, $media); @@ -68,7 +70,7 @@ function image( } $format = end($formats); - $path = Core::get_url('public/' . $name . '.' . $format, TRUE); + $path = Base::get_url('public/' . $name . '.' . $format, TRUE); $out .= "<img src=\"$path\""; if ($alt) { $alt = lang($alt); diff --git a/src/web/helpers/markdown.php b/src/web/lib/markdown.php index 5279a1f..5279a1f 100644 --- a/src/web/helpers/markdown.php +++ b/src/web/lib/markdown.php |