diff --git a/src/web/_controller/_comments.php b/src/web/_controller/_comments.php index 4b87a94..059b926 100644 --- a/src/web/_controller/_comments.php +++ b/src/web/_controller/_comments.php @@ -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, diff --git a/src/web/_controller/_meta.php b/src/web/_controller/_meta.php index 801d254..e78f8b3 100644 --- a/src/web/_controller/_meta.php +++ b/src/web/_controller/_meta.php @@ -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 "\n"; echo "{$this->main->get_url_full($url)}\n"; echo "{$priority}\n"; echo ""; } - public function sitemap() { + public function sitemap(): void { header("Content-Type: application/xml"); echo "\n"; @@ -46,7 +46,7 @@ class _meta_controller extends Controller { echo "\n"; } - public function manifest() { + public function manifest(): void { $json = array( 'short_name' => lang('domain'), 'name' => lang('domain'), diff --git a/src/web/_controller/blog.php b/src/web/_controller/blog.php index f13ffd1..4a961e1 100644 --- a/src/web/_controller/blog.php +++ b/src/web/_controller/blog.php @@ -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); diff --git a/src/web/_model/blog.php b/src/web/_model/blog.php index 42cee97..52fb97c 100644 --- a/src/web/_model/blog.php +++ b/src/web/_model/blog.php @@ -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| + */ + 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']; diff --git a/src/web/_model/main.php b/src/web/_model/main.php index 5728932..cbcf498 100644 --- a/src/web/_model/main.php +++ b/src/web/_model/main.php @@ -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. - */ - public function get_url_full($path): string { + * @param string $path + * @param bool $timestamp + */ + public function get_url_full(string $path, bool $timestamp = FALSE): string { $host = $_SERVER['HTTP_HOST']; $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; } /** * Gets a full path url from a relative path - */ - public function get_url($path): string { + * @param string $path + * @param bool $timestamp + */ + 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'); - $time = @filemtime($GLOBALS['rootroot'] . '/' . $path); - $url = "{$base}{$path}?timestamp={$time}"; + $url = "{$base}{$path}"; + if ($timestamp) { + $time = @filemtime($GLOBALS['rootroot'] . '/' . $path); + $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 ''; @@ -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)); } } diff --git a/src/web/_model/projects.php b/src/web/_model/projects.php index 5373a78..784e12a 100644 --- a/src/web/_model/projects.php +++ b/src/web/_model/projects.php @@ -8,7 +8,10 @@ class Projects_model extends Model { $this->markdown = new MarkdownParser(); } - private function load_projects(&$data) { + /** + * @param array $data + */ + private function load_projects(&$data): void { $projects = array(); $dir = $GLOBALS['assetroot'] . '/projects'; if ($handle = opendir($dir)) { diff --git a/src/web/_views/comments.php b/src/web/_views/comments.php index d72afd6..7f8acf2 100644 --- a/src/web/_views/comments.php +++ b/src/web/_views/comments.php @@ -2,8 +2,7 @@ main->format_date($comment['created']); echo '
'; echo '

' . esc($comment['author']) . '

'; diff --git a/src/web/_views/head.php b/src/web/_views/head.php index ef0d86c..5534070 100644 --- a/src/web/_views/head.php +++ b/src/web/_views/head.php @@ -12,14 +12,14 @@ - + <?=$title?> - "> - "> - "> - "> - "> - "> + "> + "> + "> + "> + "> + "> main->get_ie_version() <= 7) echo $this->main->link_css('css/legacy.css'); diff --git a/src/web/core/_controller.php b/src/web/core/_controller.php index 0dbb5b8..1da5a96 100644 --- a/src/web/core/_controller.php +++ b/src/web/core/_controller.php @@ -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 $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'); diff --git a/src/web/core/_model.php b/src/web/core/_model.php index 4c27b1b..57127de 100644 --- a/src/web/core/_model.php +++ b/src/web/core/_model.php @@ -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 - */ - function __construct($load, $main = FALSE) { + * @param ?Main_model $main + */ + function __construct(Loader $load, bool $main = FALSE) { $this->load = $load; if ($main) { $this->main = $this; diff --git a/src/web/core/loader.php b/src/web/core/loader.php index b0a1cbd..101abef 100644 --- a/src/web/core/loader.php +++ b/src/web/core/loader.php @@ -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 { diff --git a/src/web/core/router.php b/src/web/core/router.php index c8fb142..1ad6cb5 100644 --- a/src/web/core/router.php +++ b/src/web/core/router.php @@ -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 $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 $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 $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 $req + */ + private function check_banned(array $req): bool { $ip = FALSE; if ($req) { $ip = $req['ip']; diff --git a/src/web/helpers/aria.php b/src/web/helpers/aria.php index 8ebfcc5..9782299 100644 --- a/src/web/helpers/aria.php +++ b/src/web/helpers/aria.php @@ -1,6 +1,6 @@ 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 $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 $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 '
>> caused by <<
'; echo str_replace("\n", "
", $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); diff --git a/src/web/helpers/image.php b/src/web/helpers/image.php index c18154a..fd395b5 100644 --- a/src/web/helpers/image.php +++ b/src/web/helpers/image.php @@ -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('', $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 .= " $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 $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); diff --git a/src/web/helpers/markdown.php b/src/web/helpers/markdown.php index 39b430c..5279a1f 100644 --- a/src/web/helpers/markdown.php +++ b/src/web/helpers/markdown.php @@ -8,7 +8,10 @@ class MarkdownParser { $this->parsedown = new ParsedownExtra(); } - function parse($path) { + /** + * @return array + */ + function parse(string $path): array { $content = file_get_contents($path); $data = array( 'meta' => array(), diff --git a/src/web/helpers/sanitize.php b/src/web/helpers/sanitize.php index 5d37852..495c249 100644 --- a/src/web/helpers/sanitize.php +++ b/src/web/helpers/sanitize.php @@ -1,6 +1,6 @@ ', '>', $data);