a few fixes, just a few....

This commit is contained in:
Freya Murphy 2024-07-08 17:22:30 -04:00
parent 544c03c3ae
commit 8a2c577823
Signed by: freya
GPG key ID: 744AB800E383AE52
18 changed files with 189 additions and 119 deletions

View file

@ -9,7 +9,7 @@ class _comments_controller extends Controller {
} }
public function comments($page, $ref): void { public function comments(string $page, string $ref): void {
$data = $this->comments_model->get_comments($page); $data = $this->comments_model->get_comments($page);
$this->view('comments', array( $this->view('comments', array(
'comments' => $data, 'comments' => $data,

View file

@ -5,7 +5,7 @@ class _meta_controller extends Controller {
parent::__construct($load); parent::__construct($load);
} }
public function robots() { public function robots(): void {
header("Content-Type: text/plain"); header("Content-Type: text/plain");
$sitemap = $this->main->get_url_full('sitemap.xml'); $sitemap = $this->main->get_url_full('sitemap.xml');
@ -18,14 +18,14 @@ class _meta_controller extends Controller {
echo "Sitemap: {$sitemap}\n"; echo "Sitemap: {$sitemap}\n";
} }
private function sitemap_page($url, $priority) { private function sitemap_page(string $url, string $priority): void {
echo "<url>\n"; echo "<url>\n";
echo "<loc>{$this->main->get_url_full($url)}</loc>\n"; echo "<loc>{$this->main->get_url_full($url)}</loc>\n";
echo "<priority>{$priority}</priority>\n"; echo "<priority>{$priority}</priority>\n";
echo "</url>"; echo "</url>";
} }
public function sitemap() { public function sitemap(): void {
header("Content-Type: application/xml"); header("Content-Type: application/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
@ -46,7 +46,7 @@ class _meta_controller extends Controller {
echo "</urlset>\n"; echo "</urlset>\n";
} }
public function manifest() { public function manifest(): void {
$json = array( $json = array(
'short_name' => lang('domain'), 'short_name' => lang('domain'),
'name' => lang('domain'), 'name' => lang('domain'),

View file

@ -18,7 +18,7 @@ class Blog_controller extends Controller {
$this->view('footer', $data); $this->view('footer', $data);
} }
private function protect($folder) { private function protect(string $folder): void {
if (!array_key_exists('name', $_GET)) { if (!array_key_exists('name', $_GET)) {
$this->error(400); $this->error(400);
} }
@ -62,7 +62,7 @@ class Blog_controller extends Controller {
$this->view('footer', $data); $this->view('footer', $data);
} }
public function rss() { public function rss(): void {
$data = $this->blog_model->get_data(); $data = $this->blog_model->get_data();
header('Content-Type: application/xml'); header('Content-Type: application/xml');
$this->view('apps/blog_rss', $data); $this->view('apps/blog_rss', $data);

View file

@ -7,8 +7,11 @@ class Blog_model extends Model {
parent::__construct($load); parent::__construct($load);
$this->markdown = new MarkdownParser(); $this->markdown = new MarkdownParser();
} }
/**
private function load_blog(&$data) { * @param mixed $data
* @return void
*/
private function load_blog(&$data): void {
$blog = array(); $blog = array();
$dir = $GLOBALS['assetroot'] . '/blog'; $dir = $GLOBALS['assetroot'] . '/blog';
if ($handle = opendir($dir)) { if ($handle = opendir($dir)) {
@ -32,44 +35,55 @@ class Blog_model extends Model {
$data['desc'] = lang('blog_short_desc'); $data['desc'] = lang('blog_short_desc');
return $data; return $data;
} }
/**
private function load_post($name) { * @param mixed $name
* @return bool|<missing>
*/
private function load_post($name): ?array {
$dir = $GLOBALS['assetroot'] . '/blog'; $dir = $GLOBALS['assetroot'] . '/blog';
$path = $dir . '/' . $name; $path = $dir . '/' . $name;
if(!file_exists($path)) { if(!file_exists($path)) {
return FALSE; return NULL;
} }
$md = $this->markdown->parse($path); $md = $this->markdown->parse($path);
return $md; return $md;
} }
/**
public function get_post($name) { * @param mixed $name
* @return bool|null|array
*/
public function get_post($name): ?array {
$data = parent::get_data(); $data = parent::get_data();
$post = $this->load_post($name); $post = $this->load_post($name);
if (!$post) { if (!$post) {
return FALSE; return NULL;
} }
$data['title'] = $post['meta']['name']; $data['title'] = $post['meta']['name'];
$data['desc'] = $post['meta']['desc']; $data['desc'] = $post['meta']['desc'];
$data['post'] = $post; $data['post'] = $post;
return $data; return $data;
} }
/**
private function load_writeup($name) { * @param mixed $name
*/
private function load_writeup($name): ?string {
$dir = $GLOBALS['assetroot'] . '/writeup'; $dir = $GLOBALS['assetroot'] . '/writeup';
$path = $dir . '/' . $name; $path = $dir . '/' . $name;
if(!file_exists($path)) { if(!file_exists($path)) {
return FALSE; return NULL;
} }
$md = $this->markdown->parse($path); $md = $this->markdown->parse($path);
return $md; return $md;
} }
/**
public function get_writeup($name) { * @param mixed $name
* @return bool|null|array
*/
public function get_writeup($name): ?array {
$data = parent::get_data(); $data = parent::get_data();
$writeup = $this->load_writeup($name); $writeup = $this->load_writeup($name);
if (!$writeup) { if (!$writeup) {
return FALSE; return NULL;
} }
$data['title'] = $writeup['meta']['name']; $data['title'] = $writeup['meta']['name'];
$data['desc'] = $writeup['meta']['desc']; $data['desc'] = $writeup['meta']['desc'];

View file

@ -2,16 +2,16 @@
class Main_model extends Model { class Main_model extends Model {
// stores the current request info // stores the current request info
public $info; public mixed $info;
// the main loader // the main loader
public $load; public Loader $load;
/** /**
* Loads the main model * Loads the main model
* @param Loader $load - the main loader object * @param Loader $load - the main loader object
*/ */
function __construct($load) { function __construct(Loader $load) {
parent::__construct($load, TRUE); parent::__construct($load, TRUE);
$GLOBALS['main_model'] = $this; $GLOBALS['main_model'] = $this;
} }
@ -20,7 +20,7 @@ class Main_model extends Model {
* Gets the stamp for a asset path * Gets the stamp for a asset path
* @param string $path * @param string $path
*/ */
private function asset_stamp($path): int { private function asset_stamp(string $path): int {
$root = $GLOBALS['webroot']; $root = $GLOBALS['webroot'];
$path = $root . '/../public/' . $path; $path = $root . '/../public/' . $path;
return @filemtime($path); return @filemtime($path);
@ -41,25 +41,36 @@ class Main_model extends Model {
/** /**
* Gets the full url including the http scheme and host part * Gets the full url including the http scheme and host part
* Needed for IE 6 & 7 need. * Needed for IE 6 & 7 need.
*/ * @param string $path
public function get_url_full($path): string { * @param bool $timestamp
*/
public function get_url_full(string $path, bool $timestamp = FALSE): string {
$host = $_SERVER['HTTP_HOST']; $host = $_SERVER['HTTP_HOST'];
$base = lang('base_path'); $base = lang('base_path');
$time = @filemtime($GLOBALS['rootroot'] . '/' . $path);
$url = "http://{$host}{$base}{$path}?timestamp={$time}"; $url = "http://{$host}{$base}{$path}";
if ($timestamp) {
$time = @filemtime($GLOBALS['rootroot'] . '/' . $path);
$url .= "?timestamp={$time}";
}
return $url; return $url;
} }
/** /**
* Gets a full path url from a relative path * Gets a full path url from a relative path
*/ * @param string $path
public function get_url($path): string { * @param bool $timestamp
*/
public function get_url(string $path, bool $timestamp = FALSE): string {
if ($this->get_ie_version() <= 7) { if ($this->get_ie_version() <= 7) {
return $this->get_url_full($path); return $this->get_url_full($path, $timestamp);
} }
$base = lang('base_path'); $base = lang('base_path');
$time = @filemtime($GLOBALS['rootroot'] . '/' . $path); $url = "{$base}{$path}";
$url = "{$base}{$path}?timestamp={$time}"; if ($timestamp) {
$time = @filemtime($GLOBALS['rootroot'] . '/' . $path);
$url .= "?timestamp={$time}";
}
return $url; return $url;
} }
@ -67,7 +78,7 @@ class Main_model extends Model {
* Loads a css html link * Loads a css html link
* @param string $path - the path to the css file * @param string $path - the path to the css file
*/ */
public function link_css($path): string { public function link_css(string $path): string {
$stamp = $this->asset_stamp($path); $stamp = $this->asset_stamp($path);
$href = $this->get_url("public/{$path}?stamp={$stamp}"); $href = $this->get_url("public/{$path}?stamp={$stamp}");
return '<link rel="stylesheet" href="'. $href .'">'; return '<link rel="stylesheet" href="'. $href .'">';
@ -77,7 +88,7 @@ class Main_model extends Model {
* Loads a css html link * Loads a css html link
* @param string $path - the path to the css file * @param string $path - the path to the css file
*/ */
public function embed_css($path): string { public function embed_css(string $path): string {
$file = $GLOBALS['publicroot'] . '/' . $path; $file = $GLOBALS['publicroot'] . '/' . $path;
if (file_exists($file)) { if (file_exists($file)) {
$text = file_get_contents($file); $text = file_get_contents($file);
@ -91,7 +102,7 @@ class Main_model extends Model {
* Formats a ISO date * Formats a ISO date
* @param $iso_date the ISO date * @param $iso_date the ISO date
*/ */
public function format_date($iso_date): string { public function format_date(string $iso_date): string {
return date("Y-m-d D H:m", strtotime($iso_date)); return date("Y-m-d D H:m", strtotime($iso_date));
} }
} }

View file

@ -8,7 +8,10 @@ class Projects_model extends Model {
$this->markdown = new MarkdownParser(); $this->markdown = new MarkdownParser();
} }
private function load_projects(&$data) { /**
* @param array<string,mixed> $data
*/
private function load_projects(&$data): void {
$projects = array(); $projects = array();
$dir = $GLOBALS['assetroot'] . '/projects'; $dir = $GLOBALS['assetroot'] . '/projects';
if ($handle = opendir($dir)) { if ($handle = opendir($dir)) {

View file

@ -2,8 +2,7 @@
<?=aria_section('comments', lang('comments'))?> <?=aria_section('comments', lang('comments'))?>
<?php <?php
foreach($comments as $comment) { foreach($comments as $comment) {
$date = date_create($comment['created']); $date = $this->main->format_date($comment['created']);
$date = date_format($date, "Y-m-d H:i");
echo '<div class="comment">'; echo '<div class="comment">';
echo '<h3 class="header">' . esc($comment['author']) . '</h3>'; echo '<h3 class="header">' . esc($comment['author']) . '</h3>';

View file

@ -12,14 +12,14 @@
<meta property="og:description" content="<?=$desc?>"> <meta property="og:description" content="<?=$desc?>">
<meta property="og:title" content="<?=$title?>"> <meta property="og:title" content="<?=$title?>">
<meta property="og:site_name" content="<?=lang('domain')?>"> <meta property="og:site_name" content="<?=lang('domain')?>">
<meta property="og:image" content="<?=$this->main->get_url_full('public/icons/logo640.png')?>"> <meta property="og:image" content="<?=$this->main->get_url_full('public/icons/logo640.png', TRUE)?>">
<title><?=$title?></title> <title><?=$title?></title>
<link rel="icon" type="image/png" sizes="16x16" href="<?=$this->main->get_url("public/icons/logo16.png")?>"> <link rel="icon" type="image/png" sizes="16x16" href="<?=$this->main->get_url("public/icons/logo16.png", TRUE)?>">
<link rel="icon" type="image/png" sizes="32x32" href="<?=$this->main->get_url("public/icons/logo32.png")?>"> <link rel="icon" type="image/png" sizes="32x32" href="<?=$this->main->get_url("public/icons/logo32.png", TRUE)?>">
<link rel="icon" type="image/png" sizes="64x64" href="<?=$this->main->get_url("public/icons/logo64.png")?>"> <link rel="icon" type="image/png" sizes="64x64" href="<?=$this->main->get_url("public/icons/logo64.png", TRUE)?>">
<link rel="icon" type="image/png" sizes="320x320" href="<?=$this->main->get_url("public/icons/logo320.png")?>"> <link rel="icon" type="image/png" sizes="320x320" href="<?=$this->main->get_url("public/icons/logo320.png", TRUE)?>">
<link rel="icon" type="image/png" sizes="512x512" href="<?=$this->main->get_url("public/icons/logo512.png")?>"> <link rel="icon" type="image/png" sizes="512x512" href="<?=$this->main->get_url("public/icons/logo512.png", TRUE)?>">
<link rel="icon" type="image/png" sizes="640x640" href="<?=$this->main->get_url("public/icons/logo640.png")?>"> <link rel="icon" type="image/png" sizes="640x640" href="<?=$this->main->get_url("public/icons/logo640.png", TRUE)?>">
<link rel="manifest" href="/manifest.json"> <link rel="manifest" href="/manifest.json">
<?php if($this->main->get_ie_version() <= 7) <?php if($this->main->get_ie_version() <= 7)
echo $this->main->link_css('css/legacy.css'); echo $this->main->link_css('css/legacy.css');

View file

@ -2,10 +2,10 @@
abstract class Controller { abstract class Controller {
// the main model // the main model
public $main; public Main_model $main;
// the loader // the loader
public $load; public Loader $load;
/** /**
* Creates a constructor * Creates a constructor
@ -23,14 +23,17 @@ abstract class Controller {
} }
} }
public function index() {} public function index(): void {}
public function redirect($link) { public function redirect(string $link): void {
header('Location: '. $link, true, 301); header('Location: '. $link, true, 301);
die(); die();
} }
protected function view($__name, $data = array()) { /**
* @param array<int,mixed> $data
*/
protected function view(string $__name, array $data = array()): void {
$__root = $GLOBALS['webroot']; $__root = $GLOBALS['webroot'];
$__path = $__root . '/_views/' . $__name . '.php'; $__path = $__root . '/_views/' . $__name . '.php';
if (is_file($__path)) { if (is_file($__path)) {
@ -40,7 +43,7 @@ abstract class Controller {
} }
} }
protected function error($code): void { protected function error(int $code): void {
$_GET['code'] = $code; $_GET['code'] = $code;
$this->main->info['app'] = 'error'; $this->main->info['app'] = 'error';
$error_controller = $this->load->controller('error'); $error_controller = $this->load->controller('error');

View file

@ -2,19 +2,20 @@
abstract class Model { abstract class Model {
// the main model // the main model
// shared by all controllers and models // shared by all controllers and models
public $main; public Main_model $main;
public $load; public Loader $load;
// the database // the database
public $db; public DatabaseHelper $db;
private $config; private mixed $config;
/** /**
* Creates a model * Creates a model
* @param Loader $load - the main loader object * @param Loader $load - the main loader object
*/ * @param ?Main_model $main
function __construct($load, $main = FALSE) { */
function __construct(Loader $load, bool $main = FALSE) {
$this->load = $load; $this->load = $load;
if ($main) { if ($main) {
$this->main = $this; $this->main = $this;

View file

@ -2,13 +2,14 @@
class Loader { class Loader {
// keep track of what has been loaded // keep track of what has been loaded
private $loaded; private array $loaded;
// the database // the database
private $db; private ?DatabaseHelper $db;
function __construct() { function __construct() {
$this->loaded = array(); $this->loaded = array();
$this->db = NULL;
} }
/** /**
@ -99,7 +100,7 @@ class Loader {
} }
} }
public function db() { public function db(): DatabaseHelper {
if ($this->db) { if ($this->db) {
return $this->db; return $this->db;
} else { } else {

View file

@ -2,15 +2,15 @@
class Router { class Router {
// the loader // the loader
private $load; private Loader $load;
// the main model // the main model
private $main; private Main_model $main;
// the database // the database
private $db; private DatabaseHelper $db;
private $db_ready; private bool $db_ready;
/** /**
* Creates a router * Creates a router
@ -123,7 +123,7 @@ class Router {
* @param int $code - the http error code * @param int $code - the http error code
* @param bool $recursed * @param bool $recursed
*/ */
private function handle_error($code, $recursed): void { private function handle_error(int $code, bool $recursed): void {
if ($recursed) { if ($recursed) {
die($code . ' (recursed)'); die($code . ' (recursed)');
} }
@ -137,7 +137,10 @@ class Router {
$this->handle_req($req, TRUE); $this->handle_req($req, TRUE);
} }
private function load_htc($req, $recursed): void { /**
* @param array<int,mixed> $req
*/
private function load_htc(array $req, bool $recursed): void {
$parts = explode('/', $req['uri_str']); $parts = explode('/', $req['uri_str']);
$file = end($parts); $file = end($parts);
$path = $GLOBALS['publicroot'] . '/polyfills/' . $file; $path = $GLOBALS['publicroot'] . '/polyfills/' . $file;
@ -152,9 +155,10 @@ class Router {
/** /**
* @param array $req * @param array $req
* @param array<int,mixed> $req
* @param bool $recursed * @param bool $recursed
*/ */
private function handle_req($req, $recursed = FALSE): void { private function handle_req(array $req, bool $recursed = FALSE): void {
if ($recursed === false) { if ($recursed === false) {
if ( if (
@ -202,7 +206,10 @@ class Router {
$ref->invoke($controller); $ref->invoke($controller);
} }
private function log_request($req): void { /**
* @param array<int,mixed> $req
*/
private function log_request(array $req): void {
if ( if (
$req === FALSE || $req === FALSE ||
$this->db_ready === FALSE || $this->db_ready === FALSE ||
@ -220,7 +227,10 @@ class Router {
$query->execute(); $query->execute();
} }
private function check_banned($req) { /**
* @param array<int,mixed> $req
*/
private function check_banned(array $req): bool {
$ip = FALSE; $ip = FALSE;
if ($req) { if ($req) {
$ip = $req['ip']; $ip = $req['ip'];

View file

@ -1,6 +1,6 @@
<?php /* Copyright (c) 2024 Freya Murphy */ <?php /* Copyright (c) 2024 Freya Murphy */
function aria_section($id, $title = NULL): string { function aria_section(string $id, ?string $title = NULL): string {
$out = ''; $out = '';
if ($title) { if ($title) {
$idh = $id . '_heading'; $idh = $id . '_heading';

View file

@ -1,16 +1,24 @@
<?php /* Copyright (c) 2024 Freya Murphy */ <?php /* Copyright (c) 2024 Freya Murphy */
function __nullify(mixed $val): mixed {
if (!$val) {
return NULL;
} else {
return $val;
}
}
class DatabaseQuery { class DatabaseQuery {
private $conn; private \PDO $conn;
private $query; private string $query;
private $where; private bool $where;
private $set; private bool $set;
private $param; private array $param;
function __construct($conn) { function __construct(\PDO $conn) {
$this->conn = $conn; $this->conn = $conn;
$this->query = ''; $this->query = '';
@ -23,7 +31,7 @@ class DatabaseQuery {
/// ARBITRARY QUERY /// ARBITRARY QUERY
/// ///
public function query($query) { public function query(string $query): DatabaseQuery {
$this->query .= $query; $this->query .= $query;
return $this; return $this;
} }
@ -32,12 +40,12 @@ class DatabaseQuery {
/// SELECT /// SELECT
/// ///
public function select($select) { public function select(string $select): DatabaseQuery {
$this->query .= "SELECT $select\n"; $this->query .= "SELECT $select\n";
return $this; return $this;
} }
public function from($from) { public function from(string $from): DatabaseQuery {
$this->query .= "FROM $from\n"; $this->query .= "FROM $from\n";
return $this; return $this;
} }
@ -46,7 +54,7 @@ class DatabaseQuery {
/// INSERT /// INSERT
/// ///
public function insert_into($insert, ...$columns) { public function insert_into(string $insert, string ...$columns): DatabaseQuery {
$this->query .= "INSERT INTO $insert\n ("; $this->query .= "INSERT INTO $insert\n (";
foreach ($columns as $idx => $column) { foreach ($columns as $idx => $column) {
if ($idx !== 0) { if ($idx !== 0) {
@ -58,7 +66,7 @@ class DatabaseQuery {
return $this; return $this;
} }
public function values(...$values) { public function values(mixed ...$values): DatabaseQuery {
$this->query .= "VALUES ("; $this->query .= "VALUES (";
foreach ($values as $idx => $value) { foreach ($values as $idx => $value) {
if ($idx !== 0) { if ($idx !== 0) {
@ -75,7 +83,7 @@ class DatabaseQuery {
/// WHERE /// WHERE
/// ///
public function where($cond) { public function where(string $cond): DatabaseQuery {
if (!$this->where) { if (!$this->where) {
$this->where = TRUE; $this->where = TRUE;
$this->query .= "WHERE "; $this->query .= "WHERE ";
@ -86,7 +94,10 @@ class DatabaseQuery {
return $this; return $this;
} }
public function where_in($column, $array) { /**
* @param array<mixed> $array
*/
public function where_in(string $column, array $array): DatabaseQuery {
if (!$this->where) { if (!$this->where) {
$this->where = TRUE; $this->where = TRUE;
$this->query .= "WHERE "; $this->query .= "WHERE ";
@ -102,7 +113,10 @@ class DatabaseQuery {
return $this; return $this;
} }
private function in($array) { /**
* @param array<mixed> $array
*/
private function in(array $array): DatabaseQuery {
$in = 'IN ('; $in = 'IN (';
foreach ($array as $idx => $item) { foreach ($array as $idx => $item) {
if ($idx != 0) { if ($idx != 0) {
@ -119,31 +133,31 @@ class DatabaseQuery {
/// OPERATORS /// OPERATORS
/// ///
public function like($item) { public function like(mixed $item): DatabaseQuery {
$this->query .= "LIKE ?\n"; $this->query .= "LIKE ?\n";
array_push($this->param, $item); array_push($this->param, $item);
return $this; return $this;
} }
public function eq($item) { public function eq(mixed $item): DatabaseQuery {
$this->query .= "= ?\n"; $this->query .= "= ?\n";
array_push($this->param, $item); array_push($this->param, $item);
return $this; return $this;
} }
public function ne($item) { public function ne(mixed $item): DatabaseQuery {
$this->query .= "<> ?\n"; $this->query .= "<> ?\n";
array_push($this->param, $item); array_push($this->param, $item);
return $this; return $this;
} }
public function lt($item) { public function lt(mixed $item): DatabaseQuery {
$this->query .= "< ?\n"; $this->query .= "< ?\n";
array_push($this->param, $item); array_push($this->param, $item);
return $this; return $this;
} }
public function le($item) { public function le(mixed $item): DatabaseQuery {
$this->query .= "<= ?\n"; $this->query .= "<= ?\n";
array_push($this->param, $item); array_push($this->param, $item);
return $this; return $this;
@ -153,7 +167,7 @@ class DatabaseQuery {
/// JOINS /// JOINS
/// ///
public function join($table, $on, $type = 'LEFT') { public function join(string $table, string $on, string $type = 'LEFT'): DatabaseQuery {
$this->query .= "$type JOIN $table ON $on\n"; $this->query .= "$type JOIN $table ON $on\n";
return $this; return $this;
} }
@ -162,19 +176,19 @@ class DatabaseQuery {
/// LIMIT, OFFSET, ORDER /// LIMIT, OFFSET, ORDER
/// ///
public function limit($limit) { public function limit(int $limit): DatabaseQuery {
$this->query .= "LIMIT ?\n"; $this->query .= "LIMIT ?\n";
array_push($this->param, $limit); array_push($this->param, $limit);
return $this; return $this;
} }
public function offset($offset) { public function offset(int $offset): DatabaseQuery {
$this->query .= "OFFSET ?\n"; $this->query .= "OFFSET ?\n";
array_push($this->param, $offset); array_push($this->param, $offset);
return $this; return $this;
} }
public function order_by($column, $order = 'ASC') { public function order_by(string $column, string $order = 'ASC'): DatabaseQuery {
$this->query .= "ORDER BY " . $column . ' ' . $order . ' '; $this->query .= "ORDER BY " . $column . ' ' . $order . ' ';
return $this; return $this;
} }
@ -183,7 +197,7 @@ class DatabaseQuery {
/// COLLECT /// COLLECT
/// ///
public function rows(...$params) { public function rows(mixed ...$params): ?array {
$args = $this->param; $args = $this->param;
foreach ($params as $param) { foreach ($params as $param) {
array_push($args, $param); array_push($args, $param);
@ -196,20 +210,20 @@ class DatabaseQuery {
echo '<br> >> caused by <<<br>'; echo '<br> >> caused by <<<br>';
echo str_replace("\n", "<br>", $this->query); echo str_replace("\n", "<br>", $this->query);
} }
return $stmt->fetchAll(PDO::FETCH_ASSOC); return __nullify($stmt->fetchAll(PDO::FETCH_ASSOC));
} }
public function row(...$params) { public function row(mixed ...$params): ?array {
$args = $this->param; $args = $this->param;
foreach ($params as $param) { foreach ($params as $param) {
array_push($args, $param); array_push($args, $param);
} }
$stmt = $this->conn->prepare($this->query); $stmt = $this->conn->prepare($this->query);
$stmt->execute($args); $stmt->execute($args);
return $stmt->fetch(PDO::FETCH_ASSOC); return __nullify($stmt->fetch(PDO::FETCH_ASSOC));
} }
public function execute(...$params) { public function execute(mixed ...$params): bool {
$args = $this->param; $args = $this->param;
foreach ($params as $param) { foreach ($params as $param) {
array_push($args, $param); array_push($args, $param);
@ -234,13 +248,13 @@ class DatabaseQuery {
*/ */
class DatabaseHelper { class DatabaseHelper {
private $conn; private ?\PDO $conn;
function __construct() { function __construct() {
$this->conn = NULL; $this->conn = NULL;
} }
private function connect() { private function connect(): \PDO {
if ($this->conn === NULL) { if ($this->conn === NULL) {
$user = getenv("POSTGRES_USER"); $user = getenv("POSTGRES_USER");
$pass = getenv("POSTGRES_PASSWORD"); $pass = getenv("POSTGRES_PASSWORD");
@ -261,19 +275,19 @@ class DatabaseHelper {
return $this->conn; return $this->conn;
} }
public function select($select) { public function select(string $select): DatabaseQuery {
$conn = $this->connect(); $conn = $this->connect();
$query = new DatabaseQuery($conn); $query = new DatabaseQuery($conn);
return $query->select($select); return $query->select($select);
} }
public function insert_into($insert, ...$columns) { public function insert_into(string $insert, string ...$columns): DatabaseQuery {
$conn = $this->connect(); $conn = $this->connect();
$query = new DatabaseQuery($conn); $query = new DatabaseQuery($conn);
return $query->insert_into($insert, ...$columns); return $query->insert_into($insert, ...$columns);
} }
public function query($query_str) { public function query(string $query_str): DatabaseQuery {
$conn = $this->connect(); $conn = $this->connect();
$query = new DatabaseQuery($conn); $query = new DatabaseQuery($conn);
return $query->query($query_str); return $query->query($query_str);

View file

@ -30,7 +30,7 @@ function __make_source(
$media = ''; $media = '';
} }
$main = $GLOBALS['main_model']; $main = $GLOBALS['main_model'];
$path = $main->get_url('public/' . $name . '.' . $format); $path = $main->get_url('public/' . $name . '.' . $format, TRUE);
$mime = __get_mime($format); $mime = __get_mime($format);
return sprintf('<source type="%s" srcset="%s" %s>', return sprintf('<source type="%s" srcset="%s" %s>',
$mime, $path, $media); $mime, $path, $media);
@ -69,7 +69,7 @@ function image(
$format = end($formats); $format = end($formats);
$main = $GLOBALS['main_model']; $main = $GLOBALS['main_model'];
$path = $main->get_url('public/' . $name . '.' . $format); $path = $main->get_url('public/' . $name . '.' . $format, TRUE);
$out .= "<img src=\"$path\""; $out .= "<img src=\"$path\"";
if ($alt) { if ($alt) {
$alt = lang($alt); $alt = lang($alt);

View file

@ -1,7 +1,13 @@
<?php /* Copyright (c) 2024 Freya Murphy */ <?php /* Copyright (c) 2024 Freya Murphy */
$lang = array(); $lang = array();
function lang($key, $default = NULL, $sub = NULL) { /**
* @param ?array<string,mixed> $sub
*/
function lang(
string $key,
?string $default = NULL,
?array $sub = NULL) {
$lang = $GLOBALS['lang']; $lang = $GLOBALS['lang'];
if(array_key_exists($key, $lang)) { if(array_key_exists($key, $lang)) {
if ($sub) { if ($sub) {
@ -17,15 +23,20 @@ function lang($key, $default = NULL, $sub = NULL) {
} }
} }
function ilang($key, /**
$class = NULL, * @param array<string,string> $attrs
$id = NULL, * @param ?array<string,mixed> $sub
$href = NULL, */
$click = NULL, function ilang(
$attrs = array(), string $key,
$sub = NULL, string $class = NULL,
$button = FALSE, string $id = NULL,
$container = 'span' 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)); $text = ucfirst(lang($key . "_text", FALSE, sub: $sub));
$tip = lang($key . "_tip", FALSE, sub: $sub); $tip = lang($key . "_tip", FALSE, sub: $sub);

View file

@ -8,7 +8,10 @@ class MarkdownParser {
$this->parsedown = new ParsedownExtra(); $this->parsedown = new ParsedownExtra();
} }
function parse($path) { /**
* @return array<string,mixed>
*/
function parse(string $path): array {
$content = file_get_contents($path); $content = file_get_contents($path);
$data = array( $data = array(
'meta' => array(), 'meta' => array(),

View file

@ -1,6 +1,6 @@
<?php /* Copyright (c) 2024 Freya Murphy */ <?php /* Copyright (c) 2024 Freya Murphy */
function esc($data) { function esc(string $data): string {
$data = str_replace('&', '&amp;', $data); $data = str_replace('&', '&amp;', $data);
$data = str_replace('<', '&lt;', $data); $data = str_replace('<', '&lt;', $data);
$data = str_replace('>', '&gt;', $data); $data = str_replace('>', '&gt;', $data);