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);
$this->view('comments', array(
'comments' => $data,

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -2,10 +2,10 @@
abstract class Controller {
// the main model
public $main;
public Main_model $main;
// the loader
public $load;
public Loader $load;
/**
* 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);
die();
}
protected function view($__name, $data = array()) {
/**
* @param array<int,mixed> $data
*/
protected function view(string $__name, array $data = array()): void {
$__root = $GLOBALS['webroot'];
$__path = $__root . '/_views/' . $__name . '.php';
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;
$this->main->info['app'] = 'error';
$error_controller = $this->load->controller('error');

View file

@ -2,19 +2,20 @@
abstract class Model {
// the main model
// shared by all controllers and models
public $main;
public $load;
public Main_model $main;
public Loader $load;
// the database
public $db;
public DatabaseHelper $db;
private $config;
private mixed $config;
/**
* Creates a model
* @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;
if ($main) {
$this->main = $this;

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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