diff options
author | Freya Murphy <freya@freyacat.org> | 2024-04-01 11:09:25 -0400 |
---|---|---|
committer | Freya Murphy <freya@freyacat.org> | 2024-04-01 11:09:25 -0400 |
commit | 3a82baec9d793edf81ac2b151b0f4d4159641375 (patch) | |
tree | f9d50c296b078ac48c2a2391c172c3ccf37edb3f /src/web/_controller | |
parent | refactor asset dir, refactor oberver in lib (diff) | |
download | xssbook2-3a82baec9d793edf81ac2b151b0f4d4159641375.tar.gz xssbook2-3a82baec9d793edf81ac2b151b0f4d4159641375.tar.bz2 xssbook2-3a82baec9d793edf81ac2b151b0f4d4159641375.zip |
login and register, liking on homepage
Diffstat (limited to 'src/web/_controller')
-rw-r--r-- | src/web/_controller/_index.php | 23 | ||||
-rw-r--r-- | src/web/_controller/_util/post.php | 198 | ||||
-rw-r--r-- | src/web/_controller/apps/auth.php | 56 | ||||
-rw-r--r-- | src/web/_controller/apps/error.php | 21 | ||||
-rw-r--r-- | src/web/_controller/apps/home.php | 26 | ||||
-rw-r--r-- | src/web/_controller/modal.php | 34 | ||||
-rw-r--r-- | src/web/_controller/template.php | 22 |
7 files changed, 380 insertions, 0 deletions
diff --git a/src/web/_controller/_index.php b/src/web/_controller/_index.php new file mode 100644 index 0000000..2fd7db2 --- /dev/null +++ b/src/web/_controller/_index.php @@ -0,0 +1,23 @@ +<?php /* Copyright (c) 2024 Freya Murphy */ +class _index_controller extends Controller { + + // the home model + private $home_model; + + // the request model + private $request_model; + + // the caceh model + private $cache_model; + + public function index(): void { + if ($this->main->session) { + $this->redirect('/home'); + } else { + $this->redirect('/auth/login'); + } + } + +} + +?> diff --git a/src/web/_controller/_util/post.php b/src/web/_controller/_util/post.php new file mode 100644 index 0000000..b48816d --- /dev/null +++ b/src/web/_controller/_util/post.php @@ -0,0 +1,198 @@ +<?php /* Copyright (c) 2024 Freya Murphy */ +class Post_controller extends Controller { + + // the request model + private $request_model; + + // the caceh model + private $cache_model; + + // page size + private $page_size; + + function __construct($load) { + parent::__construct($load); + $this->request_model = $this->load->model('request'); + $this->cache_model = $this->load->model('cache'); + $this->page_size = 10; + } + + public function index(): void { + $this->view('template/posts'); + } + + public function post(): void { + $pid = $this->request_model->get_int('id', 0); + + $post = $this->db + ->select('p.*, l.id as like_id') + ->from('api.post p') + ->join('api.like l', 'p.id = l.post_id AND l.user_id') + ->eq($pid) + ->where('p.id') + ->eq($pid) + ->row(); + + if (!$post) { + return; + } + + $users = $this->cache_model->get_users([$post]); + $uid = $post['user_id']; + + if (!array_key_exists($uid, $users)) { + return; + } + + $user = $users[$uid]; + + $data = array( + 'user' => $user, + 'page_size' => $this->page_size, + 'post' => $post + ); + $this->view('template/post', $data); + } + + /** + * @return array<string,mixed> + */ + public function posts(): array { + $page = $this->request_model->get_int('page', 0); + $max = $this->request_model->get_int('max'); + $offset = $page * $this->page_size; + + $user = $this->main->user(); + $uid = isset($user) ? $user['id'] : NULL; + + $query = $this->db; + + $query = $this->db + ->select('p.*, l.id as like_id') + ->from('api.post p') + ->join('api.like l', 'p.id = l.post_id AND l.user_id') + ->eq($uid); + + if ($max) { + $query = $query + ->where('p.id')->le($max); + } + + $posts = $query + ->order_by('p.id', 'DESC') + ->limit($this->page_size) + ->offset($offset) + ->rows(); + + $users = $this->cache_model->get_users($posts); + $max = 0; + + foreach ($posts as $post) { + $max = max($max, $post['id']); + $data = array(); + $data['page_size'] = $this->page_size; + $data['user'] = $users[$post['user_id']]; + $data['post'] = $post; + $this->view('template/post', $data); + } + + $pc = $this->db + ->select('COUNT(p.id) as pc') + ->from('api.post p') + ->row()['pc']; + + return array( + 'loaded' => count($posts), + 'total' => $pc, + 'page_size' => $this->page_size, + 'max' => $max, + ); + } + + public function comment(): void { + $cid = $this->request_model->get_int('id', 0); + + $comment = $this->db + ->select('*') + ->from('api.comment') + ->where('id') + ->eq($cid) + ->row(); + + if (!$comment) { + return; + } + + $users = $this->cache_model->get_users([$comment]); + $uid = $comment['user_id']; + + if (!array_key_exists($uid, $users)) { + return; + } + + $user = $users[$uid]; + + $data = array( + 'user' => $user, + 'comment' => $comment + ); + $this->view('template/comment', $data); + } + + /** + * @return array<string,mixed> + */ + public function comments(): array { + $page = $this->request_model->get_int('page', 0); + $max = $this->request_model->get_int('max'); + $id = $this->request_model->get_int('id', 0); + $offset = $page * $this->page_size; + + $query = $this->db + ->select('*') + ->from('api.comment') + ->where('post_id') + ->eq($id); + + if ($max) { + $query = $query + ->and() + ->where('id') + ->le($max); + } + + $comments = $query + ->order_by('id', 'ASC') + ->limit($this->page_size) + ->offset($offset) + ->rows(); + + $users = $this->cache_model->get_users($comments); + $max = 0; + + // only add this hr when not logged in + // otherwise its added automatically by + // the like and comment buttons + if ( + count($comments) && + $page == 0 && + $this->main->session === NULL + ) { + echo '<hr>'; + } + + foreach ($comments as $comment) { + $max = max($max, $comment['id']); + $data = array(); + $data['user'] = $users[$comment['user_id']]; + $data['comment'] = $comment; + $this->view('template/comment', $data); + } + + return array( + 'loaded' => count($comments), + 'page_size' => $this->page_size, + 'max' => $max, + ); + } +} diff --git a/src/web/_controller/apps/auth.php b/src/web/_controller/apps/auth.php new file mode 100644 index 0000000..6b30cc9 --- /dev/null +++ b/src/web/_controller/apps/auth.php @@ -0,0 +1,56 @@ +<?php /* Copyright (c) 2024 Freya Murphy */ +class Auth_controller extends Controller { + + // the home model + private $auth_model; + + // the post controller + protected $post_controller; + + function __construct($load) { + parent::__construct($load); + $this->auth_model = $this->load->model('apps/auth'); + } + + public function index(): void { + if ($this->main->session) { + $this->redirect('/home'); + } else { + $this->redirect('/auth/login'); + } + } + + public function login(): void { + if ($this->main->session) { + $this->redirect('/home'); + } + + parent::index(); + $data = $this->auth_model->get_data(); + $this->view('header_empty', $data); + $this->view('apps/auth/login', $data); + $this->view('footer', $data); + } + + public function logout(): void { + if ($this->main->session) { + $_SESSION['jwt'] = NULL; + } + $this->redirect('/auth/login'); + } + + public function update(): void { + if (!$this->is_ajax()) { + $this->error(400); + } + if (!isset($_POST['key']) || !isset($_POST['value'])) { + $this->error(400); + } + $key = $_POST['key']; + $value = $_POST['value']; + $_SESSION[$key] = $value; + } + +} + +?> diff --git a/src/web/_controller/apps/error.php b/src/web/_controller/apps/error.php new file mode 100644 index 0000000..03bbd8d --- /dev/null +++ b/src/web/_controller/apps/error.php @@ -0,0 +1,21 @@ +<?php /* Copyright (c) 2024 Freya Murphy */ +class Error_controller extends Controller { + + private $error_model; + + function __construct($load) { + parent::__construct($load); + $this->error_model = $this->load->model('apps/error'); + } + + public function index(): void { + parent::index(); + $data = $this->error_model->get_data(); + $this->view('header', $data); + $this->view('apps/error/main', $data); + $this->view('footer', $data); + } + +} + +?> diff --git a/src/web/_controller/apps/home.php b/src/web/_controller/apps/home.php new file mode 100644 index 0000000..c9a116d --- /dev/null +++ b/src/web/_controller/apps/home.php @@ -0,0 +1,26 @@ +<?php /* Copyright (c) 2024 Freya Murphy */ +class Home_controller extends Controller { + + // the home model + private $home_model; + + // the post controller + protected $post_controller; + + function __construct($load) { + parent::__construct($load); + $this->home_model = $this->load->model('apps/home'); + $this->post_controller = $this->load->controller('_util/post'); + } + + public function index(): void { + parent::index(); + $data = $this->home_model->get_data(); + $this->view('header', $data); + $this->view('apps/home/main', $data); + $this->view('footer', $data); + } + +} + +?> diff --git a/src/web/_controller/modal.php b/src/web/_controller/modal.php new file mode 100644 index 0000000..03074d4 --- /dev/null +++ b/src/web/_controller/modal.php @@ -0,0 +1,34 @@ +<?php /* Copyright (c) 2024 Freya Murphy */ +class Modal_controller extends Controller { + + + function __construct($load) { + parent::__construct($load); + } + + /** + * @param string $name + * @param array $data + */ + private function modal($name, $data = array()): void { + $title = lang($name . '_modal_title'); + $data['title'] = $title; + $data['content'] = $name; + $this->view('template/modal', $data); + } + + public function new_post(): void { + $this->modal('new_post'); + } + + public function register(): void { + $this->load->app_lang( + $this->main->info['lang'], + 'auth' + ); + $this->modal('register'); + } +} + +?> + diff --git a/src/web/_controller/template.php b/src/web/_controller/template.php new file mode 100644 index 0000000..7a8cdf8 --- /dev/null +++ b/src/web/_controller/template.php @@ -0,0 +1,22 @@ +<?php /* Copyright (c) 2024 Freya Murphy */ +class Template_controller extends Controller { + + // the request model + private $request_model; + + function __construct($load) { + parent::__construct($load); + $this->request_model = $this->load->model('request'); + } + + public function toast(): void { + $data = array( + 'msg' => $this->request_model->get_str('msg', FALSE), + 'detail' => $this->request_model->get_str('detail', FALSE), + 'hint' => $this->request_model->get_str('hint', FALSE) + ); + $this->view('template/toast', $data); + } + +} + |