From 5a2ba9c2e7605bb788bc406184547d22c6436867 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Mon, 23 Dec 2024 11:13:27 -0500 Subject: v2.1.0, refactor w/ crimson --- src/web/_controller/_index.php | 23 ---- src/web/_controller/_meta.php | 12 +- src/web/_controller/_modal.php | 28 +++++ src/web/_controller/_post.php | 201 ++++++++++++++++++++++++++++++++ src/web/_controller/_template.php | 21 ++++ src/web/_controller/_util/post.php | 212 ---------------------------------- src/web/_controller/apps/auth.php | 56 --------- src/web/_controller/apps/error.php | 21 ---- src/web/_controller/apps/home.php | 26 ----- src/web/_controller/apps/people.php | 48 -------- src/web/_controller/apps/profile.php | 44 ------- src/web/_controller/apps/settings.php | 41 ------- src/web/_controller/auth.php | 45 ++++++++ src/web/_controller/error.php | 36 ++++++ src/web/_controller/home.php | 27 +++++ src/web/_controller/index.php | 16 +++ src/web/_controller/modal.php | 38 ------ src/web/_controller/people.php | 45 ++++++++ src/web/_controller/profile.php | 44 +++++++ src/web/_controller/settings.php | 34 ++++++ src/web/_controller/template.php | 23 ---- 21 files changed, 503 insertions(+), 538 deletions(-) delete mode 100644 src/web/_controller/_index.php create mode 100644 src/web/_controller/_modal.php create mode 100644 src/web/_controller/_post.php create mode 100644 src/web/_controller/_template.php delete mode 100644 src/web/_controller/_util/post.php delete mode 100644 src/web/_controller/apps/auth.php delete mode 100644 src/web/_controller/apps/error.php delete mode 100644 src/web/_controller/apps/home.php delete mode 100644 src/web/_controller/apps/people.php delete mode 100644 src/web/_controller/apps/profile.php delete mode 100644 src/web/_controller/apps/settings.php create mode 100644 src/web/_controller/auth.php create mode 100644 src/web/_controller/error.php create mode 100644 src/web/_controller/home.php create mode 100644 src/web/_controller/index.php delete mode 100644 src/web/_controller/modal.php create mode 100644 src/web/_controller/people.php create mode 100644 src/web/_controller/profile.php create mode 100644 src/web/_controller/settings.php delete mode 100644 src/web/_controller/template.php (limited to 'src/web/_controller') diff --git a/src/web/_controller/_index.php b/src/web/_controller/_index.php deleted file mode 100644 index 2fd7db2..0000000 --- a/src/web/_controller/_index.php +++ /dev/null @@ -1,23 +0,0 @@ -main->session) { - $this->redirect('/home'); - } else { - $this->redirect('/auth/login'); - } - } - -} - -?> diff --git a/src/web/_controller/_meta.php b/src/web/_controller/_meta.php index bec3c65..06c7c0a 100644 --- a/src/web/_controller/_meta.php +++ b/src/web/_controller/_meta.php @@ -1,21 +1,21 @@ 'xssbook.com', - 'name' => 'xssbook.com', + 'short_name' => CONFIG['domain'], + 'name' => CONFIG['domain'], 'icons' => [ array( - 'src' => 'https://xssbook.com/public/icons/logo512.png', + 'src' => $this->get_url('public/icons/logo512.png'), 'type' => 'image/png', 'sizes' => '512x512', 'purpose' => 'any maskable' ) ], - 'id' => 'https://xssbook.com/home', - 'start_url' => 'https://xssbook.com/home', + 'id' => $this->get_url('home'), + 'start_url' => $this->get_url('home'), 'background_color' => '#181818', 'display' => 'standalone', 'scope' => '/', diff --git a/src/web/_controller/_modal.php b/src/web/_controller/_modal.php new file mode 100644 index 0000000..0447ca8 --- /dev/null +++ b/src/web/_controller/_modal.php @@ -0,0 +1,28 @@ +model->get_data(); + $data['title'] = ucwords(lang($name . '_modal_title')); + $data['content'] = $name; + $this->view('_template/modal', $data); + } + + public function new_post(): void { + $this->load_lang('post'); + $this->modal('new_post'); + } + + public function about(): void { + $this->modal('about'); + } + + public function register(): void { + $this->load_lang('auth'); + $this->modal('register'); + } +} diff --git a/src/web/_controller/_post.php b/src/web/_controller/_post.php new file mode 100644 index 0000000..56c997f --- /dev/null +++ b/src/web/_controller/_post.php @@ -0,0 +1,201 @@ +auth_model = $this->load_model('auth'); + $this->load_lang('post'); + } + + public function index(): void { + $this->view('_template/posts'); + } + + public function post(): void { + $pid = $this->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->model->get_users([$post]); + $uid = $post['user_id']; + + if (!isset($users[$uid])) + return; + + $user = $users[$uid]; + + $data = $this->model->get_data(); + $data['user'] = $user; + $data['page_size'] = POST_PAGE_SIZE; + $data['post'] = $post; + $this->view('_template/post', $data); + } + + /** + * @return array + */ + public function posts(): array { + $page = $this->get_int('page', 0); + $max = $this->get_int('max'); + $offset = $page * POST_PAGE_SIZE; + $filter_uid = $this->get_int('user_id', FALSE); + + $user = $this->auth_model->session(); + $uid = isset($user) ? $user['id'] : NULL; + + $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); + } + + if ($filter_uid) { + $query = $query + ->where('p.user_id')->eq($filter_uid); + } + + $posts = $query + ->order_by('p.id', 'DESC') + ->limit(POST_PAGE_SIZE) + ->offset($offset) + ->rows(); + + $users = $this->model->get_users($posts); + $max = 0; + + foreach ($posts as $post) { + $max = max($max, $post['id']); + $data = $this->model->get_data(); + $data['page_size'] = POST_PAGE_SIZE; + $data['user'] = $users[$post['user_id']]; + $data['post'] = $post; + $this->view('_template/post', $data); + } + + $query = $this->db() + ->select('COUNT(p.id) as pc') + ->from('api.post p'); + + if ($filter_uid) { + $query = $query + ->where('p.user_id')->eq($filter_uid); + } + + $pc = $query + ->row()['pc']; + + return array( + 'loaded' => count($posts), + 'total' => $pc, + 'page_size' => POST_PAGE_SIZE, + + 'max' => $max, + 'filter_uid' => $filter_uid + ); + } + + public function comment(): void { + $cid = $this->get_int('id', 0); + + $comment = $this->db() + ->select('*') + ->from('api.comment') + ->where('id') + ->eq($cid) + ->row(); + + if (!$comment) { + return; + } + + $users = $this->model->get_users([$comment]); + $uid = $comment['user_id']; + + if (!array_key_exists($uid, $users)) { + return; + } + + $user = $users[$uid]; + + $data = $this->model->get_data(); + $data['user'] = $user; + $data['comment'] = $comment; + $this->view('_template/comment', $data); + } + + /** + * @return array + */ + public function comments(): array { + $page = $this->get_int('page', 0); + $max = $this->get_int('max'); + $id = $this->get_int('id', 0); + $offset = $page * COMMENT_PAGE_SIZE; + + $user = $this->auth_model->session(); + + $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(COMMENT_PAGE_SIZE) + ->offset($offset) + ->rows(); + + $users = $this->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 && + $user === NULL + ) { + echo '
'; + } + + foreach ($comments as $comment) { + $max = max($max, $comment['id']); + $data = $this->model->get_data(); + $data['user'] = $users[$comment['user_id']]; + $data['comment'] = $comment; + $this->view('_template/comment', $data); + } + + return array( + 'loaded' => count($comments), + 'page_size' => COMMENT_PAGE_SIZE, + 'max' => $max, + ); + } +} diff --git a/src/web/_controller/_template.php b/src/web/_controller/_template.php new file mode 100644 index 0000000..9c82956 --- /dev/null +++ b/src/web/_controller/_template.php @@ -0,0 +1,21 @@ +get_string('msg') ?? ''; + $detail = $this->get_string('detail'); + $hint = $this->get_string('hint'); + $type = $this->get_string('type', 'error'); + + $data = array( + 'msg' => $msg, + 'detail' => $detail, + 'hint' => $hint, + 'type' => $type, + ); + + $this->view('_template/toast', $data); + } + +} + diff --git a/src/web/_controller/_util/post.php b/src/web/_controller/_util/post.php deleted file mode 100644 index 5346497..0000000 --- a/src/web/_controller/_util/post.php +++ /dev/null @@ -1,212 +0,0 @@ -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 - */ - public function posts(): array { - $page = $this->request_model->get_int('page', 0); - $max = $this->request_model->get_int('max'); - $offset = $page * $this->page_size; - $filter_uid = $this->request_model->get_int('user_id', FALSE); - - $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); - } - - if ($filter_uid) { - $query = $query - ->where('p.user_id')->eq($filter_uid); - } - - $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); - } - - $query = $this->db - ->select('COUNT(p.id) as pc') - ->from('api.post p'); - - if ($filter_uid) { - $query = $query - ->where('p.user_id')->eq($filter_uid); - } - - $pc = $query - ->row()['pc']; - - return array( - 'loaded' => count($posts), - 'total' => $pc, - 'page_size' => $this->page_size, - 'max' => $max, - 'filter_uid' => $filter_uid - ); - } - - 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 - */ - 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 '
'; - } - - 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 deleted file mode 100644 index 1df74da..0000000 --- a/src/web/_controller/apps/auth.php +++ /dev/null @@ -1,56 +0,0 @@ -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('head', $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 deleted file mode 100644 index 03bbd8d..0000000 --- a/src/web/_controller/apps/error.php +++ /dev/null @@ -1,21 +0,0 @@ -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 deleted file mode 100644 index c9a116d..0000000 --- a/src/web/_controller/apps/home.php +++ /dev/null @@ -1,26 +0,0 @@ -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/apps/people.php b/src/web/_controller/apps/people.php deleted file mode 100644 index 86da3b3..0000000 --- a/src/web/_controller/apps/people.php +++ /dev/null @@ -1,48 +0,0 @@ -people_model = $this->load->model('apps/people'); - $this->format_model = $this->load->model('format'); - } - - public function index(): void { - parent::index(); - $data = $this->people_model->get_data(); - $this->view('header', $data); - $this->view('apps/people/header', $data); - $this->view('apps/people/main', $data); - $this->view('apps/people/footer', $data); - $this->view('footer', $data); - } - - public function content(): void { - $data = $this->people_model->get_data(); - $this->view('apps/people/main', $data); - } - - /** - * @return array - */ - public function people(): array { - $data = $this->people_model->get_users(); - - $this->view('apps/people/people', $data); - - $max = 0; - foreach ($data['users'] as $user) { - $max = max($max, $user['id']); - } - - return $data; - } -} - -?> diff --git a/src/web/_controller/apps/profile.php b/src/web/_controller/apps/profile.php deleted file mode 100644 index 9e9fca6..0000000 --- a/src/web/_controller/apps/profile.php +++ /dev/null @@ -1,44 +0,0 @@ -profile_model = $this->load->model('apps/profile'); - $this->people_controller = $this->load->controller('apps/people'); - $this->format_model = $this->load->model('format'); - $this->post_controller = $this->load->controller('_util/post'); - } - - public function index(): void { - - if ($this->main->user() && !isset($_GET['id'])) { - $this->redirect('/profile?id=' . $this->main->user()['id']); - } - - parent::index(); - $data = $this->profile_model->get_data(); - - if (!$data) { - $this->error(404); - } - - $this->view('header', $data); - $this->view('apps/profile/main', $data); - $this->view('footer', $data); - } - -} - -?> diff --git a/src/web/_controller/apps/settings.php b/src/web/_controller/apps/settings.php deleted file mode 100644 index 8a409cc..0000000 --- a/src/web/_controller/apps/settings.php +++ /dev/null @@ -1,41 +0,0 @@ -settings_model = $this->load->model('apps/settings'); - } - - public function index(): void { - if (!$this->main->session) { - $this->redirect('/auth/login'); - } - - parent::index(); - $data = $this->settings_model->get_data(); - - if (!$data) { - $this->error(404); - } - - $this->load->app_lang($this->main->info['lang'], 'auth'); - $this->view('header', $data); - $this->view('apps/settings/main', $data); - $this->view('footer', $data); - } - -} - -?> diff --git a/src/web/_controller/auth.php b/src/web/_controller/auth.php new file mode 100644 index 0000000..fd1931c --- /dev/null +++ b/src/web/_controller/auth.php @@ -0,0 +1,45 @@ +auth_model = $this->load_model('auth'); + $this->load_lang('auth'); + } + + public function index(): void { + $this->load_controller('index')->index(); + } + + public function login(): void { + if ($this->auth_model->session()) + $this->redirect('/home'); + + parent::index(); + $data = $this->auth_model->get_data(); + $this->view('head', $data); + $this->view('auth/main', $data); + $this->view('footer', $data); + } + + public function logout(): void { + if ($this->auth_model->session()) + $_SESSION['jwt'] = NULL; + $this->redirect('/auth/login'); + } + + public function update(): void { + $key = $this->post_data('key'); + $value = $this->post_data('value'); + + if (!$key || !$value) + $this->error(400); + + $_SESSION[$key] = $value; + } + +} + +?> diff --git a/src/web/_controller/error.php b/src/web/_controller/error.php new file mode 100644 index 0000000..55034ba --- /dev/null +++ b/src/web/_controller/error.php @@ -0,0 +1,36 @@ +error_model = $this->load_model('error'); + } + + public function index(): void { + $this->code(404); + } + + public function code($code): void { + parent::index(); + + $code = intval($code); + if ($code == 404 && rand(0, 100) > 95) + $code = 451; + if (!is_valid_status_code($code)) + $code = 404; + $msg = status_code_msg($code); + + $data = $this->error_model->get_data(); + $data['title'] = $code; + $data['msg'] = $msg; + + $this->view('header', $data); + $this->view('error/main', $data); + $this->view('footer', $data); + } + +} + +?> diff --git a/src/web/_controller/home.php b/src/web/_controller/home.php new file mode 100644 index 0000000..dc9da4d --- /dev/null +++ b/src/web/_controller/home.php @@ -0,0 +1,27 @@ +home_model = $this->load_model('home'); + $this->post_controller = $this->load_controller('_post'); + $this->load_lang('post', 'home'); + } + + public function index(): void { + parent::index(); + $data = $this->home_model->get_data(); + $this->view('header', $data); + $this->view('home/main', $data); + $this->view('footer', $data); + } + +} + +?> diff --git a/src/web/_controller/index.php b/src/web/_controller/index.php new file mode 100644 index 0000000..0822a22 --- /dev/null +++ b/src/web/_controller/index.php @@ -0,0 +1,16 @@ +load_model('auth'); + $session = $auth_model->session(); + + $home = $this->get_url('home'); + $login = $this->get_url('auth/login'); + + $this->redirect($session ? $home : $login); + } + +} + +?> diff --git a/src/web/_controller/modal.php b/src/web/_controller/modal.php deleted file mode 100644 index da17cca..0000000 --- a/src/web/_controller/modal.php +++ /dev/null @@ -1,38 +0,0 @@ -view('template/modal', $data); - } - - public function new_post(): void { - $this->modal('new_post'); - } - - public function about(): void { - $this->modal('about'); - } - - public function register(): void { - $this->load->app_lang( - $this->main->info['lang'], - 'auth' - ); - $this->modal('register'); - } -} - -?> - diff --git a/src/web/_controller/people.php b/src/web/_controller/people.php new file mode 100644 index 0000000..bb2db2c --- /dev/null +++ b/src/web/_controller/people.php @@ -0,0 +1,45 @@ +people_model = $this->load_model('people'); + $this->load_lang('people'); + } + + public function index(): void { + parent::index(); + $data = $this->people_model->get_data(); + $this->view('header', $data); + $this->view('people/header', $data); + $this->view('people/main', $data); + $this->view('people/footer', $data); + $this->view('footer', $data); + } + + public function content(): void { + $data = $this->people_model->get_data(); + $this->view('people/main', $data); + } + + /** + * @return array + */ + public function people(): array { + $data = $this->people_model->get_people(); + + $this->view('people/people', $data); + + $max = 0; + foreach ($data['users'] as $user) { + $max = max($max, $user['id']); + } + + return $data; + } +} + +?> diff --git a/src/web/_controller/profile.php b/src/web/_controller/profile.php new file mode 100644 index 0000000..dd02ed2 --- /dev/null +++ b/src/web/_controller/profile.php @@ -0,0 +1,44 @@ +profile_model = $this->load_model('profile'); + $this->people_controller = $this->load_controller('people'); + $this->post_controller = $this->load_controller('_post'); + $this->load_lang('profile'); + } + + public function index(): void { + $id = $this->get_int('id'); + + parent::index(); + $data = $this->profile_model->get_data(); + + // profile does not exist + if (!$data) { + // not logged in and trying to access own profile + if (!$id) + $this->redirect('/auth/login'); + // directly accessing unknown user id => 404 + else + $this->error(404); + } + + $this->view('header', $data); + $this->view('profile/main', $data); + $this->view('footer', $data); + } + +} + +?> diff --git a/src/web/_controller/settings.php b/src/web/_controller/settings.php new file mode 100644 index 0000000..e42389f --- /dev/null +++ b/src/web/_controller/settings.php @@ -0,0 +1,34 @@ +settings_model = $this->load_model('settings'); + $this->auth_model = $this->load_model('auth'); + $this->load_lang('auth', 'settings'); + } + + public function index(): void { + if (!$this->auth_model->session()) + $this->redirect('/auth/login'); + + parent::index(); + $data = $this->settings_model->get_data(); + + if (!$data) + $this->error(404); + + $this->view('header', $data); + $this->view('settings/main', $data); + $this->view('footer', $data); + } + +} + +?> diff --git a/src/web/_controller/template.php b/src/web/_controller/template.php deleted file mode 100644 index 879eadc..0000000 --- a/src/web/_controller/template.php +++ /dev/null @@ -1,23 +0,0 @@ -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), - 'type' => $this->request_model->get_str('type', 'error') - ); - $this->view('template/toast', $data); - } - -} - -- cgit v1.2.3-freya