summaryrefslogtreecommitdiff
path: root/src/web/_model
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2024-12-23 11:13:27 -0500
committerFreya Murphy <freya@freyacat.org>2024-12-23 11:13:27 -0500
commit5a2ba9c2e7605bb788bc406184547d22c6436867 (patch)
treecbd988d534e8a8593a31d70571222443f80da0b3 /src/web/_model
parentfix about modal (diff)
downloadxssbook2-5a2ba9c2e7605bb788bc406184547d22c6436867.tar.gz
xssbook2-5a2ba9c2e7605bb788bc406184547d22c6436867.tar.bz2
xssbook2-5a2ba9c2e7605bb788bc406184547d22c6436867.zip
v2.1.0, refactor w/ crimson
Diffstat (limited to 'src/web/_model')
-rw-r--r--src/web/_model/apps/auth.php13
-rw-r--r--src/web/_model/apps/error.php31
-rw-r--r--src/web/_model/apps/home.php22
-rw-r--r--src/web/_model/apps/people.php90
-rw-r--r--src/web/_model/apps/profile.php65
-rw-r--r--src/web/_model/apps/settings.php16
-rw-r--r--src/web/_model/auth.php42
-rw-r--r--src/web/_model/cache.php37
-rw-r--r--src/web/_model/error.php3
-rw-r--r--src/web/_model/format.php36
-rw-r--r--src/web/_model/home.php17
-rw-r--r--src/web/_model/main.php119
-rw-r--r--src/web/_model/people.php72
-rw-r--r--src/web/_model/profile.php70
-rw-r--r--src/web/_model/request.php40
-rw-r--r--src/web/_model/settings.php3
16 files changed, 207 insertions, 469 deletions
diff --git a/src/web/_model/apps/auth.php b/src/web/_model/apps/auth.php
deleted file mode 100644
index c528601..0000000
--- a/src/web/_model/apps/auth.php
+++ /dev/null
@@ -1,13 +0,0 @@
-<?php /* Copyright (c) 2024 Freya Murphy */
-class Auth_model extends Model {
-
- function __construct($load) {
- parent::__construct($load);
- }
-
- public function get_data(): ?array {
- $data = parent::get_data();
- $data['title'] = ucfirst(lang('login'));
- return $data;
- }
-}
diff --git a/src/web/_model/apps/error.php b/src/web/_model/apps/error.php
deleted file mode 100644
index 0a08fdd..0000000
--- a/src/web/_model/apps/error.php
+++ /dev/null
@@ -1,31 +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)) {
- http_response_code(500);
- $data['msg'] = ucfirst(lang('error'));
- $data['title'] = '500';
- } else {
- $code = $_GET['code'];
- http_response_code($code);
- $data['title'] = $code;
- $msg = ucfirst(lang('error_' . $code, FALSE));
- if (!$msg) {
- $msg = ucfirst(lang('error'));
- }
- $data['msg'] = $msg;
- }
- }
-
- public function get_data(): ?array {
- $data = parent::get_data();
- $this->get_msg($data);
- return $data;
- }
-}
-?>
diff --git a/src/web/_model/apps/home.php b/src/web/_model/apps/home.php
deleted file mode 100644
index 634bc67..0000000
--- a/src/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('xssbook.post')
- ->limit(20)
- ->rows();
- }
-
- public function get_data(): ?array {
- $data = parent::get_data();
- $data['title'] = ucfirst(lang('title'));
- $data['posts'] = $this->get_posts();
- return $data;
- }
-}
diff --git a/src/web/_model/apps/people.php b/src/web/_model/apps/people.php
deleted file mode 100644
index 08366a7..0000000
--- a/src/web/_model/apps/people.php
+++ /dev/null
@@ -1,90 +0,0 @@
-<?php /* Copyright (c) 2024 Freya Murphy */
-class People_model extends Model {
-
- private $request_model;
-
- function __construct($load) {
- parent::__construct($load);
- $this->request_model = $this->load->model('request');
- }
- /**
- * @param mixed $select
- */
- private function get_filted_query($select): DatabaseQuery {
- $filter_type = $this->request_model->get_str('filter', FALSE);
- $filter_uid = $this->request_model->get_int('uid', FALSE);
- $max = $this->request_model->get_int('max', FALSE);
-
- $query = $this->db
- ->select($select)
- ->from('api.user u');
-
- if ($filter_type && $filter_uid) {
- switch ($filter_type) {
- case 'follower': {
- $query = $query
- ->join('xssbook.follow f', 'f.follower_id = u.id AND f.followee_id', 'INNER')
- ->eq($filter_uid)
- ->where('f.value = TRUE');
- } break;
-
- case 'followee': {
- $query = $query
- ->join('xssbook.follow f', 'f.followee_id = u.id AND f.follower_id', 'INNER')
- ->eq($filter_uid)
- ->where('f.value = TRUE');
- } break;
- }
- }
-
- if ($max) {
- $query = $query
- ->where('u.id')
- ->le($max);
- }
-
- return $query;
- }
-
- /**
- * @return array<string,mixed>
- */
- public function get_users(): array {
- $page = $this->request_model->get_int('page', 0);
- $page_size = 24;
- $offset = $page_size * $page;
-
- $users = $this->get_filted_query('u.*')
- ->order_by('u.id', 'DESC')
- ->offset($offset)
- ->limit($page_size)
- ->rows();
-
- $count = $this->get_filted_query('COUNT(u.id) AS count')
- ->row()['count'];
-
- $max = 0;
-
- foreach ($users as $user) {
- $max = max($max, $user['id']);
- }
-
- $filter_type = $this->request_model->get_str('filter', FALSE);
- $filter_uid = $this->request_model->get_int('uid', FALSE);
-
- return array(
- 'users' => $users,
- 'count' => $count,
- 'page_size' => $page_size,
- 'max_id' => $max,
- 'filter_type' => $filter_type || '',
- 'filter_uid' => $filter_uid || ''
- );
- }
-
- public function get_data(): ?array {
- $data = parent::get_data();
- $data['title'] = ucfirst(lang('title'));
- return $data;
- }
-}
diff --git a/src/web/_model/apps/profile.php b/src/web/_model/apps/profile.php
deleted file mode 100644
index acec9c0..0000000
--- a/src/web/_model/apps/profile.php
+++ /dev/null
@@ -1,65 +0,0 @@
-<?php /* Copyright (c) 2024 Freya Murphy */
-class Profile_model extends Model {
-
- private $request_model;
-
- function __construct($load) {
- parent::__construct($load);
- $this->request_model = $this->load->model('request');
- }
-
- public function get_data(): ?array {
- $uid = $this->request_model->get_int('id', FALSE);
- if ($uid === FALSE) {
- if ($this->main->session) {
- $uid = $this->main->user()['id'];
- } else {
- return NULL;
- }
- }
-
- $user = $this->db
- ->select('*')
- ->from('api.user u')
- ->where('u.id')
- ->eq($uid)
- ->row();
-
- if (!$user) {
- return NULL;
- }
-
- $following = FALSE;
- $followed = FALSE;
- $follow_id = NULL;
-
- if ($this->main->session) {
- $sid = $this->main->user()['id'];
- $res = $this->db->select('f.value, f.id')
- ->from('xssbook.follow f')
- ->where('f.follower_id')
- ->eq($sid)
- ->where('f.followee_id')
- ->eq($uid)
- ->row();
- $following = $res ? $res['value'] : FALSE;
- $follow_id = $res ? $res['id'] : NULL;
- $res = $this->db->select('f.value')
- ->from('xssbook.follow f')
- ->where('f.follower_id')
- ->eq($uid)
- ->where('f.followee_id')
- ->eq($sid)
- ->row();
- $followed = $res ? $res['value'] : FALSE;
- }
-
- $data = parent::get_data();
- $data['user'] = $user;
- $data['following'] = $following;
- $data['followed'] = $followed;
- $data['follow_id'] = $follow_id;
- $data['title'] = ucfirst(lang('title', sub: [$user['first_name']]));
- return $data;
- }
-}
diff --git a/src/web/_model/apps/settings.php b/src/web/_model/apps/settings.php
deleted file mode 100644
index 1f1e3f9..0000000
--- a/src/web/_model/apps/settings.php
+++ /dev/null
@@ -1,16 +0,0 @@
-<?php /* Copyright (c) 2024 Freya Murphy */
-class Settings_model extends Model {
-
- private $request_model;
-
- function __construct($load) {
- parent::__construct($load);
- $this->request_model = $this->load->model('request');
- }
-
- public function get_data(): ?array {
- $data = parent::get_data();
- $data['title'] = ucfirst(lang('title'));
- return $data;
- }
-}
diff --git a/src/web/_model/auth.php b/src/web/_model/auth.php
new file mode 100644
index 0000000..50cb367
--- /dev/null
+++ b/src/web/_model/auth.php
@@ -0,0 +1,42 @@
+<?php /* Copyright (c) 2024 Freya Murphy */
+class Auth_model extends XSS_Model {
+
+ private static ?array $session = NULL;
+
+ /**
+ * Loads current session
+ * @param string $jwt - the user provided JWT
+ */
+ public function session(): ?array {
+ // check
+ if (self::$session)
+ return self::$session;
+ // get jwt
+ $jwt = $_SESSION['jwt'] ?? '';
+ if (!$jwt)
+ return NULL;
+ // get session
+ $result = $this->db()
+ ->select("_api.verify_jwt(?) AS user_id;")
+ ->row($jwt);
+ // invalid JWT
+ if (!$result)
+ return NULL;
+ // load user inside session
+ $user_id = $result['user_id'];
+ $user = $this->db()
+ ->select('*')
+ ->from('api.user')
+ ->where('id')
+ ->eq($user_id)
+ ->row();
+ // valid JWT, but invalid user
+ if (!$result)
+ return NULL;
+ // return session
+ self::$session = array_merge(
+ $user,
+ array('jwt' => $jwt));
+ return self::$session;
+ }
+}
diff --git a/src/web/_model/cache.php b/src/web/_model/cache.php
deleted file mode 100644
index 6cf9924..0000000
--- a/src/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/src/web/_model/error.php b/src/web/_model/error.php
new file mode 100644
index 0000000..ec376c7
--- /dev/null
+++ b/src/web/_model/error.php
@@ -0,0 +1,3 @@
+<?php /* Copyright (c) 2024 Freya Murphy */
+class Error_model extends XSS_Model {
+}
diff --git a/src/web/_model/format.php b/src/web/_model/format.php
deleted file mode 100644
index d2b7316..0000000
--- a/src/web/_model/format.php
+++ /dev/null
@@ -1,36 +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;
- }
-
-}
diff --git a/src/web/_model/home.php b/src/web/_model/home.php
new file mode 100644
index 0000000..f4a33e0
--- /dev/null
+++ b/src/web/_model/home.php
@@ -0,0 +1,17 @@
+<?php /* Copyright (c) 2024 Freya Murphy */
+class Home_model extends XSS_Model {
+
+ private function get_posts(): ?array {
+ return $this->db()
+ ->select('*')
+ ->from('api.post')
+ ->limit(POST_PAGE_SIZE)
+ ->rows();
+ }
+
+ public function get_data(): ?array {
+ $data = parent::get_data();
+ $data['posts'] = $this->get_posts();
+ return $data;
+ }
+}
diff --git a/src/web/_model/main.php b/src/web/_model/main.php
deleted file mode 100644
index cd34740..0000000
--- a/src/web/_model/main.php
+++ /dev/null
@@ -1,119 +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;
-
- // tthe logged in user
- private $user;
-
- /**
- * 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();
- $this->user = NULL;
- }
-
- /**
- * Loads current session
- * @param string $jwt - the user provided JWT
- */
- private function get_session($jwt) {
- $query = $this->db
- ->select("_api.verify_jwt(?) AS user_id;");
- $result = $query->row($jwt);
- $user_id = $result['user_id'];
- if ($user_id) {
- $this->session = array(
- 'id' => $user_id,
- 'jwt' => $jwt
- );
- $user = $this->user();
- if ($user === FALSE) {
- /// valid jwt for invalid user!!!
- $this->session = NULL;
- $this->user = NULL;
- }
- }
- }
-
- /**
- * 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->user) {
- return $this->user;
- }
- if ($this->session) {
- $this->user = $this->db
- ->select('*')
- ->from('api.user')
- ->where('id')
- ->eq($this->session['id'])
- ->row();
- return $this->user;
- }
- return NULL;
- }
-
- /**
- * Formats a date
- * @param string $date - the data in RFC3999 format
- * @returns the formatted date
- */
- public function date($date) {
- $date=date_create($date);
- return date_format($date, "Y-m-d D H:m");
- }
-
-}
-
-?>
diff --git a/src/web/_model/people.php b/src/web/_model/people.php
new file mode 100644
index 0000000..bf540cf
--- /dev/null
+++ b/src/web/_model/people.php
@@ -0,0 +1,72 @@
+<?php /* Copyright (c) 2024 Freya Murphy */
+
+class People_model extends XSS_Model {
+
+ private function get_filted_query($select): DatabaseQuery {
+ $filter_type = $this->get_string('filter');
+ $filter_uid = $this->get_int('uid');
+ $max = $this->get_int('max');
+ $query = $this->db()
+ ->select($select)
+ ->from('api.user u');
+
+ if ($filter_type && $filter_uid) {
+ switch ($filter_type) {
+ // only show followers
+ case 'follower':
+ $query = $query
+ ->join('xssbook.follow f', 'f.follower_id = u.id AND f.followee_id', 'INNER')
+ ->eq($filter_uid)
+ ->where('f.value = TRUE');
+ break;
+ // only show followees
+ case 'followee':
+ $query = $query
+ ->join('xssbook.follow f', 'f.followee_id = u.id AND f.follower_id', 'INNER')
+ ->eq($filter_uid)
+ ->where('f.value = TRUE');
+ break;
+ }
+ }
+
+ if ($max) {
+ $query = $query
+ ->where('u.id')
+ ->le($max);
+ }
+
+ return $query;
+ }
+
+ public function get_people(): array {
+ $filter_type = $this->get_string('filter');
+ $filter_uid = $this->get_int('uid');
+ $page = $this->get_int('page', 0);
+
+ $page_size = PEOPLE_PAGE_SIZE;
+ $offset = $page_size * $page;
+
+ $users = $this->get_filted_query('u.*')
+ ->order_by('u.id', 'DESC')
+ ->offset($offset)
+ ->limit($page_size)
+ ->rows();
+
+ $count = $this->get_filted_query('COUNT(u.id) AS count')
+ ->row()['count'];
+
+ $max = 0;
+
+ foreach ($users as $user)
+ $max = max($max, $user['id']);
+
+ return array(
+ 'users' => $users,
+ 'count' => $count,
+ 'page_size' => $page_size,
+ 'max_id' => $max,
+ 'filter_type' => $filter_type,
+ 'filter_uid' => $filter_uid,
+ );
+ }
+}
diff --git a/src/web/_model/profile.php b/src/web/_model/profile.php
new file mode 100644
index 0000000..d35cc46
--- /dev/null
+++ b/src/web/_model/profile.php
@@ -0,0 +1,70 @@
+<?php /* Copyright (c) 2024 Freya Murphy */
+class Profile_model extends XSS_Model {
+
+ private $auth_model;
+
+ function __construct() {
+ $this->auth_model = $this->load_model('auth');
+ }
+
+ public function get_data(): ?array {
+ $uid = $this->get_int('id');
+ $session = $this->auth_model->session();
+
+ if (!$uid && $session)
+ $uid = $session['id'];
+ if (!$uid)
+ return NULL;
+
+ $user = $this->db()
+ ->select('*')
+ ->from('api.user u')
+ ->where('u.id')
+ ->eq($uid)
+ ->row();
+
+ if (!$user)
+ return NULL;
+
+ // am i following $uid?
+ $following = FALSE;
+ $following_id = NULL;
+ // is $uid following me?
+ $followed = FALSE;
+
+ if ($session) {
+ $sid = $session['id'];
+ // am i following $uid?
+ $res = $this->db()
+ ->select('f.value, f.id')
+ ->from('xssbook.follow f')
+ ->where('f.follower_id')
+ ->eq($sid)
+ ->where('f.followee_id')
+ ->eq($uid)
+ ->row();
+ $following = $res ? $res['value'] : FALSE;
+ $following_id = $res ? $res['id'] : NULL;
+ // is $uid following me?
+ $res = $this->db()
+ ->select('f.value')
+ ->from('xssbook.follow f')
+ ->where('f.follower_id')
+ ->eq($uid)
+ ->where('f.followee_id')
+ ->eq($sid)
+ ->row();
+ $followed = $res ? $res['value'] : FALSE;
+ }
+
+ $data = parent::get_data();
+ $data['user'] = $user;
+ $data['following'] = $following;
+ $data['following_id'] = $following_id;
+ $data['followed'] = $followed;
+
+ $name = $this->format_name($user);
+ $data['title'] .= " - $name";
+ return $data;
+ }
+}
diff --git a/src/web/_model/request.php b/src/web/_model/request.php
deleted file mode 100644
index 4cce07a..0000000
--- a/src/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;
- }
- }
- }
-
-}
diff --git a/src/web/_model/settings.php b/src/web/_model/settings.php
new file mode 100644
index 0000000..9748f26
--- /dev/null
+++ b/src/web/_model/settings.php
@@ -0,0 +1,3 @@
+<?php /* Copyright (c) 2024 Freya Murphy */
+class Settings_model extends XSS_Model {
+}