diff options
Diffstat (limited to '')
-rw-r--r-- | src/web/_controller/_index.php (renamed from web/_controller/_index.php) | 2 | ||||
-rw-r--r-- | src/web/_controller/_util/post.php (renamed from web/_controller/_util/post.php) | 97 | ||||
-rw-r--r-- | src/web/_controller/apps/auth.php | 56 | ||||
-rw-r--r-- | src/web/_controller/apps/error.php (renamed from web/_controller/apps/error.php) | 3 | ||||
-rw-r--r-- | src/web/_controller/apps/home.php (renamed from web/_controller/apps/home.php) | 1 | ||||
-rw-r--r-- | src/web/_controller/modal.php (renamed from web/_controller/modal.php) | 8 | ||||
-rw-r--r-- | src/web/_controller/template.php (renamed from web/_controller/template.php) | 0 |
7 files changed, 151 insertions, 16 deletions
diff --git a/web/_controller/_index.php b/src/web/_controller/_index.php index fdf9440..2fd7db2 100644 --- a/web/_controller/_index.php +++ b/src/web/_controller/_index.php @@ -14,7 +14,7 @@ class _index_controller extends Controller { if ($this->main->session) { $this->redirect('/home'); } else { - $this->redirect('/login'); + $this->redirect('/auth/login'); } } diff --git a/web/_controller/_util/post.php b/src/web/_controller/_util/post.php index b128d67..b48816d 100644 --- a/web/_controller/_util/post.php +++ b/src/web/_controller/_util/post.php @@ -21,6 +21,39 @@ class Post_controller extends Controller { $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> */ @@ -30,28 +63,23 @@ class Post_controller extends Controller { $offset = $page * $this->page_size; $user = $this->main->user(); + $uid = isset($user) ? $user['id'] : NULL; $query = $this->db; - if ($user) { - $query = $query->select('p.*, l.post_id IS NOT NULL as liked'); - } else { - $query = $query->select('p.*, FALSE as liked'); - } - - $query = $query->from('api.post p'); - - if ($user) { - $query = $query->join('admin.like l', 'p.id = l.post_id AND l.user_id') - ->eq($user['id']); - } + $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('id')->le($max); + ->where('p.id')->le($max); } $posts = $query + ->order_by('p.id', 'DESC') ->limit($this->page_size) ->offset($offset) ->rows(); @@ -73,7 +101,6 @@ class Post_controller extends Controller { ->from('api.post p') ->row()['pc']; - return array( 'loaded' => count($posts), 'total' => $pc, @@ -82,6 +109,36 @@ class Post_controller extends Controller { ); } + 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> */ @@ -105,6 +162,7 @@ class Post_controller extends Controller { } $comments = $query + ->order_by('id', 'ASC') ->limit($this->page_size) ->offset($offset) ->rows(); @@ -112,6 +170,17 @@ class Post_controller extends Controller { $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(); 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/web/_controller/apps/error.php b/src/web/_controller/apps/error.php index 5ce9ec4..03bbd8d 100644 --- a/web/_controller/apps/error.php +++ b/src/web/_controller/apps/error.php @@ -8,11 +8,12 @@ class Error_controller extends Controller { $this->error_model = $this->load->model('apps/error'); } - public function index() { + 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/web/_controller/apps/home.php b/src/web/_controller/apps/home.php index edf7e2b..c9a116d 100644 --- a/web/_controller/apps/home.php +++ b/src/web/_controller/apps/home.php @@ -18,6 +18,7 @@ class Home_controller extends Controller { $data = $this->home_model->get_data(); $this->view('header', $data); $this->view('apps/home/main', $data); + $this->view('footer', $data); } } diff --git a/web/_controller/modal.php b/src/web/_controller/modal.php index 9ae4ca8..03074d4 100644 --- a/web/_controller/modal.php +++ b/src/web/_controller/modal.php @@ -20,6 +20,14 @@ class Modal_controller extends Controller { 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/web/_controller/template.php b/src/web/_controller/template.php index 7a8cdf8..7a8cdf8 100644 --- a/web/_controller/template.php +++ b/src/web/_controller/template.php |