summaryrefslogtreecommitdiff
path: root/web/_model
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2024-04-01 11:09:25 -0400
committerFreya Murphy <freya@freyacat.org>2024-04-01 11:09:25 -0400
commit3a82baec9d793edf81ac2b151b0f4d4159641375 (patch)
treef9d50c296b078ac48c2a2391c172c3ccf37edb3f /web/_model
parentrefactor asset dir, refactor oberver in lib (diff)
downloadxssbook2-3a82baec9d793edf81ac2b151b0f4d4159641375.tar.gz
xssbook2-3a82baec9d793edf81ac2b151b0f4d4159641375.tar.bz2
xssbook2-3a82baec9d793edf81ac2b151b0f4d4159641375.zip
login and register, liking on homepage
Diffstat (limited to 'web/_model')
-rw-r--r--web/_model/apps/error.php35
-rw-r--r--web/_model/apps/home.php22
-rw-r--r--web/_model/cache.php37
-rw-r--r--web/_model/format.php45
-rw-r--r--web/_model/main.php96
-rw-r--r--web/_model/request.php40
6 files changed, 0 insertions, 275 deletions
diff --git a/web/_model/apps/error.php b/web/_model/apps/error.php
deleted file mode 100644
index ad72b28..0000000
--- a/web/_model/apps/error.php
+++ /dev/null
@@ -1,35 +0,0 @@
-<?php /* Copyright (c) 2024 Freya Murphy */
-class Error_model extends Model {
-
- function __construct($load) {
- parent::__construct($load);
- }
-
- private function get_msg(&$data) {
- if (!array_key_exists('code', $_GET)) {
- $data['msg'] = lang('error');
- $data['title'] = '500';
- } else {
- $code = $_GET['code'];
- $data['title'] = $code;
- switch ($code) {
- case '404':
- $data['msg'] = lang('error_404');
- break;
- case '500':
- $data['msg'] = lang('error_500');
- break;
- default:
- $data['msg'] = lang('error');
- break;
- }
- }
- }
-
- public function get_data(): array {
- $data = parent::get_data();
- $this->get_msg($data);
- return $data;
- }
-}
-?>
diff --git a/web/_model/apps/home.php b/web/_model/apps/home.php
deleted file mode 100644
index 82fbf26..0000000
--- a/web/_model/apps/home.php
+++ /dev/null
@@ -1,22 +0,0 @@
-<?php /* Copyright (c) 2024 Freya Murphy */
-class Home_model extends Model {
-
- function __construct($load) {
- parent::__construct($load);
- }
-
- private function get_posts(): array {
- return $this->db
- ->select('*')
- ->from('admin.post')
- ->limit(20)
- ->rows();
- }
-
- public function get_data(): array {
- $data = parent::get_data();
- $data['title'] = lang('title');
- $data['posts'] = $this->get_posts();
- return $data;
- }
-}
diff --git a/web/_model/cache.php b/web/_model/cache.php
deleted file mode 100644
index 6cf9924..0000000
--- a/web/_model/cache.php
+++ /dev/null
@@ -1,37 +0,0 @@
-<?php /* Copyright (c) 2024 Freya Murphy */
-class Cache_model extends Model {
-
- // the user cache
- private $users;
-
- function __construct($load) {
- parent::__construct($load);
- $this->users = array();
- }
-
- /**
- * Gets a array of users
- */
- public function get_users($objs) {
- $ids = array();
- foreach ($objs as $obj) {
- $id = $obj['user_id'];
- if (!array_key_exists($id, $this->users)) {
- array_push($ids, intval($id));
- }
- }
- if (!empty($ids)) {
- $result = $this->main->db
- ->select('*')
- ->from('api.user')
- ->where_in('id', $ids)
- ->rows();
- foreach ($result as $user) {
- $id = $user['id'];
- $this->users[$id] = $user;
- }
- }
- return $this->users;
- }
-
-}
diff --git a/web/_model/format.php b/web/_model/format.php
deleted file mode 100644
index 52b51be..0000000
--- a/web/_model/format.php
+++ /dev/null
@@ -1,45 +0,0 @@
-<?php /* Copyright (c) 2024 Freya Murphy */
-class Format_model extends Model {
-
- function __construct($load) {
- parent::__construct($load);
- }
-
- /**
- * Formats a users's name
- * @param array $user - the $user
- * @returns the user's formatted display name
- */
- public function name($user) {
- $name = '';
- if ($user['first_name']) {
- $name .= $user['first_name'];
- }
- if ($user['middle_name']) {
- if ($name != '') {
- $name .= ' ';
- }
- $name .= $user['middle_name'];
- }
- if ($user['last_name']) {
- if ($name != '') {
- $name .= ' ';
- }
- $name .= $user['last_name'];
- }
- if ($name == '') {
- $name = '@' . $user['username'];
- }
- return $name;
- }
-
- /**
- * Formats a date
- * @param string $date - the data in RFC3999 format
- * @returns the formatted date
- */
- public function date($date) {
- return $date;
- }
-
-}
diff --git a/web/_model/main.php b/web/_model/main.php
deleted file mode 100644
index ab964fd..0000000
--- a/web/_model/main.php
+++ /dev/null
@@ -1,96 +0,0 @@
-<?php /* Copyright (c) 2024 Freya Murphy */
-class Main_model {
-
- // the website database
- public $db;
-
- // the current user session (can be NULL)
- public $session;
-
- // current loaded users
- private $users;
-
- // stores the current request info
- public $info;
-
- /**
- * Loads the main model
- * @param Loader $load - the main loader object
- */
- function __construct($load) {
- /// load the database helper
- $this->db = new DatabaseHelper();
- /// load the current session
- if (array_key_exists('jwt', $_SESSION)) {
- $this->get_session($_SESSION['jwt']);
- } else {
- $this->session = NULL;
- };
- /// init other vars
- $this->users = array();
- }
-
- /**
- * Loads current session
- * @param string $jwt - the user provided JWT
- */
- private function get_session($jwt) {
- $query = $this->db
- ->select("_api.verify_jwt('" . $jwt . "') AS user_id;");
- $result = $query->row();
- $user_id = $result['user_id'];
- if ($user_id) {
- $this->session = array(
- 'id' => $user_id,
- 'jwt' => $jwt
- );
- }
- }
-
- /**
- * Gets the stamp for a asset path
- * @param string $path
- */
- private function asset_stamp($path): int {
- $root = $GLOBALS['webroot'];
- $path = $root . '/public/' . $path;
- return filemtime($path);
- }
-
- /**
- * Loads a css html link
- * @param string $path - the path to the css file
- */
- public function link_css($path) {
- $stamp = $this->asset_stamp($path);
- return '<link rel="stylesheet" href="/public/' . $path . '?stamp=' . $stamp . '">';
- }
-
- /**
- * Loads a js html link
- * @param string $path - the path to the js file
- */
- public function link_js($path) {
- $stamp = $this->asset_stamp($path);
- return '<script src="/public/'. $path . '?stamp=' . $stamp . '"></script>';
- }
-
- /**
- * Gets the current user
- */
- public function user() {
- if ($this->session) {
- return $this->db
- ->select('*')
- ->from('api.user')
- ->where('id')
- ->eq($this->session['id'])
- ->row();
- } else {
- return NULL;
- }
- }
-
-}
-
-?>
diff --git a/web/_model/request.php b/web/_model/request.php
deleted file mode 100644
index 4cce07a..0000000
--- a/web/_model/request.php
+++ /dev/null
@@ -1,40 +0,0 @@
-<?php /* Copyright (c) 2024 Freya Murphy */
-class Request_model extends Model {
-
- function __construct($load) {
- parent::__construct($load);
- }
-
- /**
- * Loads a string from the GET request
- * @param string $key - the name for the query param
- * @param string $default - the default value if not exists
- */
- public function get_str($key, $default = NULL): string | NULL {
- if (!array_key_exists($key, $_GET)) {
- return $default;
- } else {
- return $_GET[$key];
- }
- }
-
- /**
- * Loads a number from the GET request
- * @param string $key - the name for the query param
- * @param int $default - the default value if not exists
- */
- public function get_int($key, $default = NULL): int | NULL {
- if (!array_key_exists($key, $_GET)) {
- return $default;
- } else {
- $val = $_GET[$key];
- $val = intval($val);
- if ($val < 0) {
- return 0;
- } else {
- return $val;
- }
- }
- }
-
-}