From 20b34878c60626ba6f572de4f21d2e4c8641882d Mon Sep 17 00:00:00 2001 From: themohooks <81331307+themohooks@users.noreply.github.com> Date: Tue, 25 Feb 2025 23:02:05 +0300 Subject: [PATCH] models & services --- app/Core/Page.php | 84 +++++++++++++++++++++--------------- app/Core/Routes.php | 1 + app/Models/Photo.php | 12 ++++-- app/Models/User.php | 4 +- app/Services/APIResponse.php | 45 +++++++++++++++++++ app/Services/DB.php | 2 +- 6 files changed, 109 insertions(+), 39 deletions(-) create mode 100644 app/Services/APIResponse.php diff --git a/app/Core/Page.php b/app/Core/Page.php index ab7ac2c..dc885c6 100644 --- a/app/Core/Page.php +++ b/app/Core/Page.php @@ -2,45 +2,61 @@ namespace App\Core; +use Latte\Engine; +use Exception; +use App\Services\Auth; +use App\Models\User; +use App\Services\DB; + class Page { - private static $cache = []; + private $latte; + private $layouts = [ + 'dashboard' => 'dashboard.latte', + 'login' => null, + ]; + private $defaultLayout = '@layout.latte'; + private $user; - public static function component($name) + public function __construct() { - if (!isset(self::$cache[$name])) { - self::$cache[$name] = require_once($_SERVER['DOCUMENT_ROOT'] . '/views/components/' . $name . '.php'); + $this->latte = new Engine; + $this->latte->setTempDirectory($_SERVER['DOCUMENT_ROOT'] . '/cdn/lattecache'); + $this->user = new User(Auth::userid()); + } + + + + private function get_current_git_commit($branch = 'main') + { + if ($hash = file_get_contents(sprintf($_SERVER['DOCUMENT_ROOT'] . '/.git/refs/heads/%s', $branch))) { + return mb_strimwidth($hash, 0, 7, ""); + } else { + return false; + } + } + + + + public function render($template, $params = []) + { + $templatePath = $_SERVER['DOCUMENT_ROOT'] . "/views/pages/" . $template . ".latte"; + if (!file_exists($templatePath)) { + throw new Exception("Template not found: " . $templatePath); + } + $params['ngallery'] = NGALLERY; + $params['user_id'] = Auth::userid(); + $params['user'] = $this->user; + $params['nonreviewedimgs'] = DB::query('SELECT COUNT(*) FROM photos WHERE moderated=0')[0]['COUNT(*)']; + $params['lastcommit'] = self::get_current_git_commit(); + $params['mysqlversion'] = DB::query('SELECT VERSION()')[0]['VERSION()']; + $params['db'] = \App\Services\DB::class; + + $layout = $this->layouts[$template] ?? $this->defaultLayout; + if ($layout) { + $params['layout'] = $_SERVER['DOCUMENT_ROOT'] . "/views/components/" . $layout; } - return self::$cache[$name]; - } - - public static function rewrite($search, $replace, $rootUrl) - { - return str_ireplace($search, $replace, $rootUrl); - } - - public static function set($name) - { - if (!isset(self::$cache[$name])) { - self::$cache[$name] = require_once($_SERVER['DOCUMENT_ROOT'] . '/views/pages/' . $name . '.php'); - } - - return self::$cache[$name]; - } - - public static function render($name) - { - return self::set($name)(); - } - - public static function method() - { - return $_SERVER['REQUEST_METHOD']; - } - - public static function exists($name) - { - return file_exists($_SERVER['DOCUMENT_ROOT'] . '/views/pages/' . $name . '.php'); + $this->latte->render($templatePath, $params); } } diff --git a/app/Core/Routes.php b/app/Core/Routes.php index 7afc185..fc05ada 100644 --- a/app/Core/Routes.php +++ b/app/Core/Routes.php @@ -40,6 +40,7 @@ class Routes Router::get('/voting/results', 'ContestsController@results'); Router::get('/voting/waiting', 'ContestsController@waiting'); Router::get('/comments', 'MainController@comments'); + Router::get('/messages', 'MessagesController@i'); if (Auth::userid() > 0) { $user = new \App\Models\User(Auth::userid()); diff --git a/app/Models/Photo.php b/app/Models/Photo.php index 596a888..7c6b90f 100644 --- a/app/Models/Photo.php +++ b/app/Models/Photo.php @@ -5,11 +5,17 @@ use \App\Services\DB; class Photo { public $photoid; - function __construct($user_id) { - $this->photoid = $user_id; + private $photo; + function __construct($photo_id) { + $this->photoid = $photo_id; + $this->photo = DB::query("SELECT * FROM photos WHERE id=:id", array(':id'=>$this->photoid))[0]; } public function i($table) { - return DB::query("SELECT * FROM photos WHERE id=:id", array(':id'=>$this->photoid))[0][$table]; + return $this->photo[$table]; + } + public function exists(): bool + { + return $this->i('id') !== null; } public static function fetchAll($user_id = NULL) { if ($user_id != NULL) { diff --git a/app/Models/User.php b/app/Models/User.php index a0115d4..e3bd6e4 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -5,11 +5,13 @@ use \App\Services\DB; class User { public $userid; + private $user; function __construct($user_id) { $this->userid = $user_id; + $this->user = DB::query("SELECT * FROM users WHERE id=:id", array(':id'=>$this->userid))[0]; } public function i($table) { - return DB::query("SELECT * FROM users WHERE id=:id", array(':id'=>$this->userid))[0][$table]; + return $this->user[$table]; } public function content($table) { $content = json_decode(self::i('content'), true); diff --git a/app/Services/APIResponse.php b/app/Services/APIResponse.php new file mode 100644 index 0000000..5b0e3fe --- /dev/null +++ b/app/Services/APIResponse.php @@ -0,0 +1,45 @@ +data = []; + $this->httpCode = 200; + } + + public static function data(array $data, int $httpCode = 200): self { + $instance = new self(); + $instance->data = $data; + $instance->httpCode = $httpCode; + return $instance; + } + + public function setData(array $data): self { + $this->data = $data; + return $this; + } + + public function setHttpCode(int $httpCode): self { + $this->httpCode = $httpCode; + return $this; + } + + public function addField(string $key, mixed $value): self { + $this->data[$key] = $value; + return $this; + } + + public function toJson(): string { + return json_encode($this->data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); + } + + public function send(): void { + http_response_code($this->httpCode); + header('Content-Type: application/json'); + echo $this->toJson(); + exit; + } +} + diff --git a/app/Services/DB.php b/app/Services/DB.php index 5d53106..9077be9 100644 --- a/app/Services/DB.php +++ b/app/Services/DB.php @@ -16,7 +16,7 @@ class DB { try { self::$pdoInstance = new PDO($dsn, $username, $password); self::$pdoInstance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - } catch (PDOException $ex) { + } catch (\PDOException $ex) { die("Connection failed: " . $ex->getMessage()); } }