summaryrefslogtreecommitdiff
path: root/src/web/core/core.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/web/core/core.php')
-rw-r--r--src/web/core/core.php85
1 files changed, 85 insertions, 0 deletions
diff --git a/src/web/core/core.php b/src/web/core/core.php
new file mode 100644
index 0000000..d71870e
--- /dev/null
+++ b/src/web/core/core.php
@@ -0,0 +1,85 @@
+<?php /* Copyright (c) 2024 Freya Murphy */
+
+/**
+ * Core functions needed everywhere
+ */
+abstract class Core {
+
+ private static ?DatabaseHelper $db = NULL;
+
+ /**
+ * Loads the database
+ */
+ public static function db(): DatabaseHelper
+ {
+ if (!Component::$db)
+ Component::$db = new DatabaseHelper();
+ return Component::$db;
+ }
+
+ /**
+ * Gets the stamp for a asset path
+ * @param string $path
+ */
+ public static function asset_stamp(string $path): int
+ {
+ $path = PUBLIC_ROOT . '/' . $path;
+ return @filemtime($path);
+ }
+
+ /**
+ * Gets a full path url from a relative path
+ * @param string $path
+ * @param bool $timestamp
+ */
+ public static function get_url(string $path, bool $timestamp = FALSE): string
+ {
+ $host = $_SERVER['HTTP_HOST'];
+
+ if (ENVIRONMENT == 'production')
+ $host = lang('domain');
+
+ $base = lang('base_path');
+ $url = "http://{$host}{$base}{$path}";
+ if ($timestamp) {
+ $time = @filemtime(PHP_ROOT . '/' . $path);
+ $url .= "?timestamp={$time}";
+ }
+ return $url;
+ }
+
+ /**
+ * Loads a css html link
+ * @param string $path - the path to the css file
+ */
+ public static function link_css(string $path): string
+ {
+ $stamp = Core::asset_stamp($path);
+ $href = Core::get_url("public/{$path}?stamp={$stamp}");
+ return '<link rel="stylesheet" href="'. $href .'">';
+ }
+
+ /**
+ * Loads a css html link
+ * @param string $path - the path to the css file
+ */
+ public static function embed_css(string $path): string
+ {
+ $file = PUBLIC_ROOT . '/' . $path;
+ if (file_exists($file)) {
+ $text = file_get_contents($file);
+ return "<style>{$text}</style>";
+ } else {
+ return "";
+ }
+ }
+
+ /**
+ * Formats a ISO date
+ * @param $iso_date the ISO date
+ */
+ public static function format_date(string $iso_date): string
+ {
+ return date("Y-m-d D H:m", strtotime($iso_date));
+ }
+}