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 --- src/web/_model/apps/auth.php | 13 -- src/web/_model/apps/error.php | 31 ---- src/web/_model/apps/home.php | 22 --- src/web/_model/apps/people.php | 90 ----------- src/web/_model/apps/profile.php | 65 -------- src/web/_model/apps/settings.php | 16 -- src/web/_model/auth.php | 42 +++++ src/web/_model/cache.php | 37 ----- src/web/_model/error.php | 3 + src/web/_model/format.php | 36 ----- src/web/_model/home.php | 17 ++ src/web/_model/main.php | 119 -------------- src/web/_model/people.php | 72 +++++++++ src/web/_model/profile.php | 70 +++++++++ src/web/_model/request.php | 40 ----- src/web/_model/settings.php | 3 + src/web/_views/_modal/about.php | 9 ++ src/web/_views/_modal/new_post.php | 56 +++++++ src/web/_views/_modal/register.php | 178 +++++++++++++++++++++ src/web/_views/_template/comment.php | 12 ++ src/web/_views/_template/error.php | 12 ++ src/web/_views/_template/modal.php | 14 ++ src/web/_views/_template/post.php | 86 +++++++++++ src/web/_views/_template/posts.php | 32 ++++ src/web/_views/_template/toast.php | 22 +++ src/web/_views/apps/auth/login.php | 87 ----------- src/web/_views/apps/error/main.php | 6 - src/web/_views/apps/home/main.php | 27 ---- src/web/_views/apps/people/card.php | 15 -- src/web/_views/apps/people/footer.php | 3 - src/web/_views/apps/people/header.php | 6 - src/web/_views/apps/people/main.php | 72 --------- src/web/_views/apps/people/people.php | 7 - src/web/_views/apps/profile/main.php | 269 -------------------------------- src/web/_views/apps/settings/main.php | 200 ------------------------ src/web/_views/auth/main.php | 87 +++++++++++ src/web/_views/error/main.php | 6 + src/web/_views/head.php | 15 +- src/web/_views/header.php | 38 ++--- src/web/_views/home/main.php | 27 ++++ src/web/_views/modal/about.php | 9 -- src/web/_views/modal/new_post.php | 59 ------- src/web/_views/modal/register.php | 178 --------------------- src/web/_views/people/card.php | 15 ++ src/web/_views/people/footer.php | 3 + src/web/_views/people/header.php | 6 + src/web/_views/people/main.php | 76 +++++++++ src/web/_views/people/people.php | 5 + src/web/_views/profile/main.php | 269 ++++++++++++++++++++++++++++++++ src/web/_views/settings/main.php | 197 ++++++++++++++++++++++++ src/web/_views/template/comment.php | 15 -- src/web/_views/template/error.php | 12 -- src/web/_views/template/modal.php | 14 -- src/web/_views/template/post.php | 87 ----------- src/web/_views/template/posts.php | 25 --- src/web/_views/template/toast.php | 26 ---- src/web/config.php | 56 +++++++ src/web/config/aesthetic.php | 83 ---------- src/web/config/routes.php | 12 -- src/web/core/_controller.php | 69 --------- src/web/core/_model.php | 44 ------ src/web/core/database.php | 282 ---------------------------------- src/web/core/loader.php | 101 ------------ src/web/core/router.php | 159 ------------------- src/web/helper/error.php | 9 -- src/web/helper/image.php | 77 ---------- src/web/helper/lang.php | 81 ---------- src/web/index.php | 46 ------ src/web/lang/api.php | 31 ++++ src/web/lang/auth.php | 32 ++++ src/web/lang/common.php | 61 ++++++++ src/web/lang/en_US/api_lang.php | 33 ---- src/web/lang/en_US/apps/auth.php | 34 ---- src/web/lang/en_US/apps/home.php | 9 -- src/web/lang/en_US/apps/people.php | 17 -- src/web/lang/en_US/apps/profile.php | 46 ------ src/web/lang/en_US/apps/settings.php | 25 --- src/web/lang/en_US/common_lang.php | 74 --------- src/web/lang/en_US/error_lang.php | 8 - src/web/lang/home.php | 7 + src/web/lang/people.php | 17 ++ src/web/lang/post.php | 35 +++++ src/web/lang/profile.php | 43 ++++++ src/web/lang/settings.php | 25 +++ src/web/lib/_base.php | 61 ++++++++ src/web/lib/_controller.php | 49 ++++++ src/web/lib/_model.php | 61 ++++++++ src/web/lib/hooks.php | 31 ++++ src/web/lib/image.php | 77 ++++++++++ src/web/lib/utils.php | 6 + src/web/stamp.php | 46 ++++++ 112 files changed, 2475 insertions(+), 3371 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 delete mode 100644 src/web/_model/apps/auth.php delete mode 100644 src/web/_model/apps/error.php delete mode 100644 src/web/_model/apps/home.php delete mode 100644 src/web/_model/apps/people.php delete mode 100644 src/web/_model/apps/profile.php delete mode 100644 src/web/_model/apps/settings.php create mode 100644 src/web/_model/auth.php delete mode 100644 src/web/_model/cache.php create mode 100644 src/web/_model/error.php delete mode 100644 src/web/_model/format.php create mode 100644 src/web/_model/home.php delete mode 100644 src/web/_model/main.php create mode 100644 src/web/_model/people.php create mode 100644 src/web/_model/profile.php delete mode 100644 src/web/_model/request.php create mode 100644 src/web/_model/settings.php create mode 100644 src/web/_views/_modal/about.php create mode 100644 src/web/_views/_modal/new_post.php create mode 100644 src/web/_views/_modal/register.php create mode 100644 src/web/_views/_template/comment.php create mode 100644 src/web/_views/_template/error.php create mode 100644 src/web/_views/_template/modal.php create mode 100644 src/web/_views/_template/post.php create mode 100644 src/web/_views/_template/posts.php create mode 100644 src/web/_views/_template/toast.php delete mode 100644 src/web/_views/apps/auth/login.php delete mode 100644 src/web/_views/apps/error/main.php delete mode 100644 src/web/_views/apps/home/main.php delete mode 100644 src/web/_views/apps/people/card.php delete mode 100644 src/web/_views/apps/people/footer.php delete mode 100644 src/web/_views/apps/people/header.php delete mode 100644 src/web/_views/apps/people/main.php delete mode 100644 src/web/_views/apps/people/people.php delete mode 100644 src/web/_views/apps/profile/main.php delete mode 100644 src/web/_views/apps/settings/main.php create mode 100644 src/web/_views/auth/main.php create mode 100644 src/web/_views/error/main.php create mode 100644 src/web/_views/home/main.php delete mode 100644 src/web/_views/modal/about.php delete mode 100644 src/web/_views/modal/new_post.php delete mode 100644 src/web/_views/modal/register.php create mode 100644 src/web/_views/people/card.php create mode 100644 src/web/_views/people/footer.php create mode 100644 src/web/_views/people/header.php create mode 100644 src/web/_views/people/main.php create mode 100644 src/web/_views/people/people.php create mode 100644 src/web/_views/profile/main.php create mode 100644 src/web/_views/settings/main.php delete mode 100644 src/web/_views/template/comment.php delete mode 100644 src/web/_views/template/error.php delete mode 100644 src/web/_views/template/modal.php delete mode 100644 src/web/_views/template/post.php delete mode 100644 src/web/_views/template/posts.php delete mode 100644 src/web/_views/template/toast.php create mode 100644 src/web/config.php delete mode 100644 src/web/config/aesthetic.php delete mode 100644 src/web/config/routes.php delete mode 100644 src/web/core/_controller.php delete mode 100644 src/web/core/_model.php delete mode 100644 src/web/core/database.php delete mode 100644 src/web/core/loader.php delete mode 100644 src/web/core/router.php delete mode 100644 src/web/helper/error.php delete mode 100644 src/web/helper/image.php delete mode 100644 src/web/helper/lang.php delete mode 100644 src/web/index.php create mode 100644 src/web/lang/api.php create mode 100644 src/web/lang/auth.php create mode 100644 src/web/lang/common.php delete mode 100644 src/web/lang/en_US/api_lang.php delete mode 100644 src/web/lang/en_US/apps/auth.php delete mode 100644 src/web/lang/en_US/apps/home.php delete mode 100644 src/web/lang/en_US/apps/people.php delete mode 100644 src/web/lang/en_US/apps/profile.php delete mode 100644 src/web/lang/en_US/apps/settings.php delete mode 100644 src/web/lang/en_US/common_lang.php delete mode 100644 src/web/lang/en_US/error_lang.php create mode 100644 src/web/lang/home.php create mode 100644 src/web/lang/people.php create mode 100644 src/web/lang/post.php create mode 100644 src/web/lang/profile.php create mode 100644 src/web/lang/settings.php create mode 100644 src/web/lib/_base.php create mode 100644 src/web/lib/_controller.php create mode 100644 src/web/lib/_model.php create mode 100644 src/web/lib/hooks.php create mode 100644 src/web/lib/image.php create mode 100644 src/web/lib/utils.php create mode 100644 src/web/stamp.php (limited to 'src/web') 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); - } - -} - 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 @@ -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 @@ -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 @@ -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 - */ - 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 @@ -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 @@ -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 @@ +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 @@ -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 @@ +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 @@ -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 ''; - } - - /** - * 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 ''; - } - - /** - * 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 @@ +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 @@ +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 @@ - + + diff --git a/src/web/_views/_modal/new_post.php b/src/web/_views/_modal/new_post.php new file mode 100644 index 0000000..aff19a5 --- /dev/null +++ b/src/web/_views/_modal/new_post.php @@ -0,0 +1,56 @@ + + +
+
+
+ +
+ + +
+
+ +
+ +
+ diff --git a/src/web/_views/_modal/register.php b/src/web/_views/_modal/register.php new file mode 100644 index 0000000..2126d0d --- /dev/null +++ b/src/web/_views/_modal/register.php @@ -0,0 +1,178 @@ + + + +
+ + +
+ diff --git a/src/web/_views/_template/comment.php b/src/web/_views/_template/comment.php new file mode 100644 index 0000000..b947ff6 --- /dev/null +++ b/src/web/_views/_template/comment.php @@ -0,0 +1,12 @@ + + +
+ +
+
+ format_name($user)?> + format_date($comment['created'])?> +
+ +
+
diff --git a/src/web/_views/_template/error.php b/src/web/_views/_template/error.php new file mode 100644 index 0000000..2e02cb1 --- /dev/null +++ b/src/web/_views/_template/error.php @@ -0,0 +1,12 @@ + + + + <?=$code . ' - ' . $msg?> + + +
+

+
+
+ + diff --git a/src/web/_views/_template/modal.php b/src/web/_views/_template/modal.php new file mode 100644 index 0000000..8e4545d --- /dev/null +++ b/src/web/_views/_template/modal.php @@ -0,0 +1,14 @@ + + + diff --git a/src/web/_views/_template/post.php b/src/web/_views/_template/post.php new file mode 100644 index 0000000..f7f5de2 --- /dev/null +++ b/src/web/_views/_template/post.php @@ -0,0 +1,86 @@ + + +
+
+ +
+ + format_date($post['created'])?> +
+
+

+ +

+ $post['id'] + ); + if ($post['like_id'] !== NULL) { + $post_attrs['likeId'] = $post['like_id']; + } +?> + + +
+
+ + +
+
+ +
+ $post['id']); + $cdata = $this->comments(); + + $loaded = $cdata['loaded']; + $max = $cdata['max']; + $page_size = $cdata['page_size']; + $total = $post['comment_count']; + + if ($loaded >= $page_size && $page_size < $total) { + echo ilang('action_load_comments', + class: 'action-load-comments btn btn-blend mt', + attrs: array( + 'postId' => $post['id'], + 'loaded' => $loaded, + 'pageSize' => $page_size, + 'commentCount' => $total, + 'commentMax' => $max, + ) + ); + } + + ?> +
+ +
+ +
+ + +
+
+ +
+ + diff --git a/src/web/_views/_template/posts.php b/src/web/_views/_template/posts.php new file mode 100644 index 0000000..4202e67 --- /dev/null +++ b/src/web/_views/_template/posts.php @@ -0,0 +1,32 @@ +
+posts(); + + $loaded = $pdata['loaded']; + $page_size = $pdata['page_size']; + $total = $pdata['total']; + $max = $pdata['max']; + $filterUid = $pdata['filter_uid']; +?> + +
+ + +
+ += $page_size && $page_size < $total) { + echo ilang('action_load_posts', + id: 'action-load-posts', + class: 'btn btn-blend grow mb mt', + attrs: array( + 'loaded' => $loaded, + 'pageSize' => $page_size, + 'postCount' => $total, + 'postMax' => $max, + 'userId' => $filterUid ? json_encode($filterUid) : '' + ) + ); + } +?> +
diff --git a/src/web/_views/_template/toast.php b/src/web/_views/_template/toast.php new file mode 100644 index 0000000..902955c --- /dev/null +++ b/src/web/_views/_template/toast.php @@ -0,0 +1,22 @@ + + + +
+ + +
diff --git a/src/web/_views/apps/auth/login.php b/src/web/_views/apps/auth/login.php deleted file mode 100644 index ac4cd02..0000000 --- a/src/web/_views/apps/auth/login.php +++ /dev/null @@ -1,87 +0,0 @@ - - -
-
-

xssbook

- -
-
-
-
- - -
-
- - -
- 'submit') - )?> - -
-
- 'submit') - )?> -
- -
diff --git a/src/web/_views/apps/error/main.php b/src/web/_views/apps/error/main.php deleted file mode 100644 index bcc6f90..0000000 --- a/src/web/_views/apps/error/main.php +++ /dev/null @@ -1,6 +0,0 @@ - - -
-

- -
diff --git a/src/web/_views/apps/home/main.php b/src/web/_views/apps/home/main.php deleted file mode 100644 index 864034a..0000000 --- a/src/web/_views/apps/home/main.php +++ /dev/null @@ -1,27 +0,0 @@ - - -
- -
-
- - - - -
- -
- - post_controller->index(); ?> -
diff --git a/src/web/_views/apps/people/card.php b/src/web/_views/apps/people/card.php deleted file mode 100644 index 93b1350..0000000 --- a/src/web/_views/apps/people/card.php +++ /dev/null @@ -1,15 +0,0 @@ - - - -
- -
- format_model->name($user)?> - -
-
-
- - - diff --git a/src/web/_views/apps/people/header.php b/src/web/_views/apps/people/header.php deleted file mode 100644 index b1099ec..0000000 --- a/src/web/_views/apps/people/header.php +++ /dev/null @@ -1,6 +0,0 @@ - - -
-

-

-
diff --git a/src/web/_views/apps/people/main.php b/src/web/_views/apps/people/main.php deleted file mode 100644 index ec84ab9..0000000 --- a/src/web/_views/apps/people/main.php +++ /dev/null @@ -1,72 +0,0 @@ - - -
-people(); -?> -
- -= $page_size && $page_size < $total): ?> - $loaded, - 'pageSize' => $page_size, - 'userCount' => $total, - 'userMax' => $max, - 'filterUid' => $filter_uid, - 'filterType' => $filer_type - ) - )?> - - diff --git a/src/web/_views/apps/people/people.php b/src/web/_views/apps/people/people.php deleted file mode 100644 index 5fc0d17..0000000 --- a/src/web/_views/apps/people/people.php +++ /dev/null @@ -1,7 +0,0 @@ - - -view('apps/people/card', array('user' => $user)); - } -?> diff --git a/src/web/_views/apps/profile/main.php b/src/web/_views/apps/profile/main.php deleted file mode 100644 index b2ad496..0000000 --- a/src/web/_views/apps/profile/main.php +++ /dev/null @@ -1,269 +0,0 @@ - - -
-
-
- -
-
- -
-
-
-
- format_model->name($user)?> - -
- main->session && - (!isset($self) || $self['id'] != $user['id']) - ): ?> - - - -
- 0): ?> -
- - - -
-
-
-
- - - - -
-
-
-
-
- post_controller->index(); - ?> -
-
-

- - - - - - - - - - - - - - - - - - - - - -
main->date($user['birth_date'])?>
-

- - - - - - - - - - - - - - - - - - - - - - - - - -
main->date($user['created'])?>
main->date($user['seen'])?>
-
-
- people_controller->content(); - ?> -
-
- people_controller->content(); - ?> -
-
- -
diff --git a/src/web/_views/apps/settings/main.php b/src/web/_views/apps/settings/main.php deleted file mode 100644 index 2033acc..0000000 --- a/src/web/_views/apps/settings/main.php +++ /dev/null @@ -1,200 +0,0 @@ - - - -main->user(); - -function __create_form($user, $col) { - $ph = ucfirst(lang('ph_' . $col)); - $val = $user[$col]; - return "
-
- - -
- - - -
"; -} - -?> - - - -
-
-

-
-

- - - - - - - -
-

- -
-
- - -
-
- - -
- -
-
-

- -

-
- -
- - -
-
-

-
- -
- - -
-
-
-
diff --git a/src/web/_views/auth/main.php b/src/web/_views/auth/main.php new file mode 100644 index 0000000..9604770 --- /dev/null +++ b/src/web/_views/auth/main.php @@ -0,0 +1,87 @@ + + +
+
+

xssbook

+ +
+
+
+
+ + +
+
+ + +
+ 'submit') + )?> + +
+
+ 'submit') + )?> +
+ +
diff --git a/src/web/_views/error/main.php b/src/web/_views/error/main.php new file mode 100644 index 0000000..bcc6f90 --- /dev/null +++ b/src/web/_views/error/main.php @@ -0,0 +1,6 @@ + + +
+

+ +
diff --git a/src/web/_views/head.php b/src/web/_views/head.php index cca43a1..809ab36 100644 --- a/src/web/_views/head.php +++ b/src/web/_views/head.php @@ -4,23 +4,14 @@ - main->link_js($js); - } - foreach ($css_files as $css) { - echo $this->main->link_css($css); - } - ?> + <?=$title?> - -
diff --git a/src/web/_views/header.php b/src/web/_views/header.php index 7315afb..4ba15ed 100644 --- a/src/web/_views/header.php +++ b/src/web/_views/header.php @@ -1,7 +1,6 @@ main->user(); $this->view('head', $data); ?>
diff --git a/src/web/_views/modal/about.php b/src/web/_views/modal/about.php deleted file mode 100644 index 4bc7a4f..0000000 --- a/src/web/_views/modal/about.php +++ /dev/null @@ -1,9 +0,0 @@ - - - diff --git a/src/web/_views/modal/new_post.php b/src/web/_views/modal/new_post.php deleted file mode 100644 index 66e6561..0000000 --- a/src/web/_views/modal/new_post.php +++ /dev/null @@ -1,59 +0,0 @@ - - -main->user(); -?> -
-
-
- -
- - -
-
- -
- -
- diff --git a/src/web/_views/modal/register.php b/src/web/_views/modal/register.php deleted file mode 100644 index 5c76fe9..0000000 --- a/src/web/_views/modal/register.php +++ /dev/null @@ -1,178 +0,0 @@ - - - -
- - -
- diff --git a/src/web/_views/people/card.php b/src/web/_views/people/card.php new file mode 100644 index 0000000..7e040ba --- /dev/null +++ b/src/web/_views/people/card.php @@ -0,0 +1,15 @@ + + + +
+ +
+ format_name($user)?> + +
+
+
+ + + diff --git a/src/web/_views/people/header.php b/src/web/_views/people/header.php new file mode 100644 index 0000000..b1099ec --- /dev/null +++ b/src/web/_views/people/header.php @@ -0,0 +1,6 @@ + + +
+

+

+
diff --git a/src/web/_views/people/main.php b/src/web/_views/people/main.php new file mode 100644 index 0000000..c602b2c --- /dev/null +++ b/src/web/_views/people/main.php @@ -0,0 +1,76 @@ + +
+ people(); ?> +
+ + +
+ +
+ += $page_size && $page_size < $total): ?> + $loaded, + 'pageSize' => $page_size, + 'userCount' => $total, + 'userMax' => $max, + 'filterUid' => $filter_uid, + 'filterType' => $filer_type + ) + )?> + + diff --git a/src/web/_views/people/people.php b/src/web/_views/people/people.php new file mode 100644 index 0000000..8726f1d --- /dev/null +++ b/src/web/_views/people/people.php @@ -0,0 +1,5 @@ +view('people/card', array('user' => $user)); +} +?> diff --git a/src/web/_views/profile/main.php b/src/web/_views/profile/main.php new file mode 100644 index 0000000..3e7129c --- /dev/null +++ b/src/web/_views/profile/main.php @@ -0,0 +1,269 @@ + + +
+
+
+ +
+
+ +
+
+
+
+ format_name($user)?> + +
+ + + + +
+ 0): ?> +
+ + + +
+
+
+
+ + + + +
+
+
+
+
+ post_controller->index(); + ?> +
+
+

+ + + + + + + + + + + + + + + + + + + + + +
format_date($user['birth_date'])?>
+

+ + + + + + + + + + + + + + + + + + + + + + + + + +
format_date($user['created'])?>
format_date($user['seen'])?>
+
+
+ people_controller->content(); + ?> +
+
+ people_controller->content(); + ?> +
+
+ +
diff --git a/src/web/_views/settings/main.php b/src/web/_views/settings/main.php new file mode 100644 index 0000000..4401fe1 --- /dev/null +++ b/src/web/_views/settings/main.php @@ -0,0 +1,197 @@ + +
+ + +
+ + + + "; +} + +?> + + + +
+
+

+
+

+ + + + + + + +
+

+ +
+
+ + +
+
+ + +
+ +
+
+

+ +

+
+ +
+ + +
+
+

+
+ +
+ + +
+
+
+
diff --git a/src/web/_views/template/comment.php b/src/web/_views/template/comment.php deleted file mode 100644 index ceac588..0000000 --- a/src/web/_views/template/comment.php +++ /dev/null @@ -1,15 +0,0 @@ - - -load->model('format'); -?> -
- -
-
- name($user)?> - main->date($comment['created'])?> -
- -
-
diff --git a/src/web/_views/template/error.php b/src/web/_views/template/error.php deleted file mode 100644 index 2e02cb1..0000000 --- a/src/web/_views/template/error.php +++ /dev/null @@ -1,12 +0,0 @@ - - - - <?=$code . ' - ' . $msg?> - - -
-

-
-
- - diff --git a/src/web/_views/template/modal.php b/src/web/_views/template/modal.php deleted file mode 100644 index 2a041a7..0000000 --- a/src/web/_views/template/modal.php +++ /dev/null @@ -1,14 +0,0 @@ - - - diff --git a/src/web/_views/template/post.php b/src/web/_views/template/post.php deleted file mode 100644 index 48f8bb6..0000000 --- a/src/web/_views/template/post.php +++ /dev/null @@ -1,87 +0,0 @@ - - -
-
- -
- - main->date($post['created'])?> -
-
-

- -

-main->user(); - $liked = $post['like_id'] ? 'btn-primary' : ''; - $post_attrs = array( - 'postId' => $post['id'] - ); - if ($post['like_id'] !== NULL) { - $post_attrs['likeId'] = $post['like_id']; - } -?> - - -
-
- - -
-
- -
- $post['id']); - $cdata = $this->comments(); - - $loaded = $cdata['loaded']; - $max = $cdata['max']; - $page_size = $cdata['page_size']; - $total = $post['comment_count']; - - if ($loaded >= $page_size && $page_size < $total) { - ilang('action_load_comments', - class: 'action-load-comments btn btn-blend mt', - attrs: array( - 'postId' => $post['id'], - 'loaded' => $loaded, - 'pageSize' => $page_size, - 'commentCount' => $total, - 'commentMax' => $max, - ) - ); - } - - ?> -
- -
- -
- - -
-
- -
- - diff --git a/src/web/_views/template/posts.php b/src/web/_views/template/posts.php deleted file mode 100644 index ed79688..0000000 --- a/src/web/_views/template/posts.php +++ /dev/null @@ -1,25 +0,0 @@ -
-posts(); - - $loaded = $pdata['loaded']; - $page_size = $pdata['page_size']; - $total = $pdata['total']; - $max = $pdata['max']; - $filterUid = $pdata['filter_uid']; - - if ($loaded >= $page_size && $page_size < $total) { - ilang('action_load_posts', - id: 'action-load-posts', - class: 'btn btn-blend grow mb mt', - attrs: array( - 'loaded' => $loaded, - 'pageSize' => $page_size, - 'postCount' => $total, - 'postMax' => $max, - 'userId' => $filterUid ? json_encode($filterUid) : '' - ) - ); - } -?> -
diff --git a/src/web/_views/template/toast.php b/src/web/_views/template/toast.php deleted file mode 100644 index 0a6a82f..0000000 --- a/src/web/_views/template/toast.php +++ /dev/null @@ -1,26 +0,0 @@ - - - -
- - -
diff --git a/src/web/config.php b/src/web/config.php new file mode 100644 index 0000000..3ca2bbd --- /dev/null +++ b/src/web/config.php @@ -0,0 +1,56 @@ + 'home' sends / to /home +// +// style - single or list of css styles to load on specific routes +// +// js - single or list of js script to load on specific routes +// +// autoload - list of directories to autoload all PHP files in them +// +define('SITE_CONFIG', array( + /* core settings */ + 'domain' => 'xssbook.com', + 'allowed_hosts' => ['xssbook.com'], + 'base_path' => '/', + 'theme_color' => '#1778f2', + /* route overides */ + 'routes' => array( + 'manifest.json' => '_meta/manifest', + ), + /* css to load on each route */ + 'style' => array( + '' => 'css/common.css', + 'home' => ['css/home.css', 'css/post.css'], + 'auth' => 'css/auth.css', + 'people' => 'css/people.css', + 'profile' => ['css/profile.css', 'css/people.css', 'css/post.css'], + 'settings' => 'css/settings.css', + 'error' => 'css/error.css', + ), + /* js to load on each route */ + 'js' => array( + '' => ['js/thirdparty/jquery.min.js', 'js/lib.js', 'js/modal.js'], + 'home' => 'js/post.js', + 'profile' => 'js/post.js', + ), + /* directories to autoload php code */ + 'autoload' => array('/lib'), +)); + +define('POST_PAGE_SIZE', 10); +define('COMMENT_PAGE_SIZE', 5); +define('PEOPLE_PAGE_SIZE', 24); diff --git a/src/web/config/aesthetic.php b/src/web/config/aesthetic.php deleted file mode 100644 index 5a66660..0000000 --- a/src/web/config/aesthetic.php +++ /dev/null @@ -1,83 +0,0 @@ -config = array( - '_common' => array( - 'js' => [ - 'js/thirdparty/jquery.min.js', - 'js/lib.js', - 'js/modal.js', - ], - 'css' => [ - 'css/common.css' - ], - ), - 'error' => array( - 'css' => [ - 'css/error.css' - ], - ), - 'home' => array( - 'js' => [ - 'js/post.js', - ], - 'css' => [ - 'css/home.css', - 'css/post.css' - ], - ), - 'auth' => array( - 'css' => [ - 'css/auth.css' - ], - ), - 'people' => array( - 'css' => [ - 'css/people.css' - ], - ), - 'profile' => array( - 'js' => [ - 'js/post.js', - ], - 'css' => [ - 'css/profile.css', - 'css/people.css', - 'css/post.css' - ], - ), - 'settings' => array( - 'css' => [ - 'css/settings.css' - ] - ), - ); - } - /** - * @param mixed $route - * @return array - */ - function get_files($route): array { - $js_files = $this->config['_common']['js']; - $css_files = $this->config['_common']['css']; - - if (array_key_exists($route, $this->config)) { - $config = $this->config[$route]; - if (array_key_exists('js', $config)) { - $js_files = array_merge($js_files, $config['js']); - } - if (array_key_exists('css', $config)) { - $css_files = array_merge($css_files, $config['css']); - } - } - - return array( - 'js_files' => $js_files, - 'css_files' => $css_files, - ); - } - -} diff --git a/src/web/config/routes.php b/src/web/config/routes.php deleted file mode 100644 index 20b499a..0000000 --- a/src/web/config/routes.php +++ /dev/null @@ -1,12 +0,0 @@ -load = $load; - $this->main = $this->load->model('main'); - $this->db = $this->main->db; - - $info = $this->main->info; - $lang = $info['lang']; - $this->load->lang($lang); - $app = $info['app']; - if ($app) { - $this->load->app_lang($lang, $app); - } - - $this->format_model = $this->load->model('format'); - } - - public function index() {} - - public function redirect($link) { - header('Location: '. $link, true, 301); - die(); - } - - protected function view($__name, $data = array()) { - $__root = $GLOBALS['webroot']; - $__path = $__root . '/_views/' . $__name . '.php'; - if (is_file($__path)) { - extract($data); - require($__path); - return; - } - } - - protected function is_ajax(): bool { - $_POST = json_decode( - file_get_contents("php://input"), true - ); - return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest'; - } - - protected function error($code): void { - $_GET['code'] = $code; - $this->main->info['app'] = 'error'; - $error_controller = $this->load->controller('apps/error'); - $error_controller->index(); - die(); - } - -} -?> diff --git a/src/web/core/_model.php b/src/web/core/_model.php deleted file mode 100644 index dfc7163..0000000 --- a/src/web/core/_model.php +++ /dev/null @@ -1,44 +0,0 @@ -load = $load; - $this->main = $this->load->model('main'); - $this->db = $this->main->db; - $this->config = new Aesthetic(); - } - - /** - * @returns the base model data - */ - public function get_data(): ?array { - $data = array(); - $data['self'] = $this->main->user(); - - $info = $this->main->info; - $app = $info['app']; - - if ($app) { - $files = $this->config->get_files($app); - $data = array_merge($data, $files); - } else { - $files = $this->config->get_files(); - $data = array_merge($data, $files); - } - - return $data; - } -} diff --git a/src/web/core/database.php b/src/web/core/database.php deleted file mode 100644 index 25cb5ba..0000000 --- a/src/web/core/database.php +++ /dev/null @@ -1,282 +0,0 @@ -conn = $conn; - $this->query = ''; - - $this->set = FALSE; - $this->where = FALSE; - $this->param = array(); - } - - /// - /// ARBITRARY QUERY - /// - - public function query($query) { - $this->query .= $query; - return $this; - } - - /// - /// SELECT - /// - - public function select($select) { - $this->query .= "SELECT $select\n"; - return $this; - } - - public function from($from) { - $this->query .= "FROM $from\n"; - return $this; - } - - /// - /// INSERT - /// - - public function insert_into($insert, ...$columns) { - $this->query .= "INSERT INTO $insert\n ("; - foreach ($columns as $idx => $column) { - if ($idx !== 0) { - $this->query .= ","; - } - $this->query .= $column; - } - $this->query .= ")\n"; - return $this; - } - - public function values(...$values) { - $this->query .= "VALUES ("; - foreach ($values as $idx => $value) { - if ($idx !== 0) { - $this->query .= ","; - } - $this->query .= "?"; - array_push($this->param, $value); - } - $this->query .= ")\n"; - return $this; - } - - /// - /// WHERE - /// - - public function where($cond) { - if (!$this->where) { - $this->where = TRUE; - $this->query .= "WHERE "; - } else { - $this->query .= "AND "; - } - $this->query .= "$cond "; - return $this; - } - - public function where_in($column, $array) { - if (!$this->where) { - $this->where = TRUE; - $this->query .= "WHERE "; - } else { - $this->query .= "AND "; - } - if (empty($array)) { - $this->query .= "FALSE\n"; - return $this; - } - $in = $this->in($array); - $this->query .= "$column $in\n"; - return $this; - } - - private function in($array) { - $in = 'IN ('; - foreach ($array as $idx => $item) { - if ($idx != 0) { - $in .= ","; - } - $in .= "?"; - array_push($this->param, $item); - } - $in .= ")"; - return $in; - } - - /// - /// OPERATORS - /// - - public function like($item) { - $this->query .= "LIKE ?\n"; - array_push($this->param, $item); - return $this; - } - - public function eq($item) { - $this->query .= "= ?\n"; - array_push($this->param, $item); - return $this; - } - - public function ne($item) { - $this->query .= "<> ?\n"; - array_push($this->param, $item); - return $this; - } - - public function lt($item) { - $this->query .= "< ?\n"; - array_push($this->param, $item); - return $this; - } - - public function le($item) { - $this->query .= "<= ?\n"; - array_push($this->param, $item); - return $this; - } - - /// - /// JOINS - /// - - public function join($table, $on, $type = 'LEFT') { - $this->query .= "$type JOIN $table ON $on\n"; - return $this; - } - - /// - /// LIMIT, OFFSET, ORDER - /// - - public function limit($limit) { - $this->query .= "LIMIT ?\n"; - array_push($this->param, $limit); - return $this; - } - - public function offset($offset) { - $this->query .= "OFFSET ?\n"; - array_push($this->param, $offset); - return $this; - } - - public function order_by($column, $order = 'ASC') { - $this->query .= "ORDER BY " . $column . ' ' . $order . ' '; - return $this; - } - - /// - /// COLLECT - /// - - public function rows(...$params) { - $args = $this->param; - foreach ($params as $param) { - array_push($args, $param); - } - $stmt = $this->conn->prepare($this->query); - try { - $stmt->execute($args); - } catch (Exception $ex) { - echo $ex; - echo '
>> caused by <<
'; - echo str_replace("\n", "
", $this->query); - } - return $stmt->fetchAll(PDO::FETCH_ASSOC); - } - - public function row(...$params) { - $args = $this->param; - foreach ($params as $param) { - array_push($args, $param); - } - $stmt = $this->conn->prepare($this->query); - $stmt->execute($args); - return $stmt->fetch(PDO::FETCH_ASSOC); - } - - public function execute(...$params) { - $args = $this->param; - foreach ($params as $param) { - array_push($args, $param); - } - $stmt = $this->conn->prepare($this->query); - try { - $stmt->execute($args); - return TRUE; - } catch (Exception $_e) { - echo $_e; - echo '
>> caused by <<
'; - echo str_replace("\n", "
", $this->query); - return FALSE; - } - } -} - -/** - * DatabaseHelper - * allows queries on the - * postgres database - */ -class DatabaseHelper { - - private $conn; - - function __construct() { - $this->conn = NULL; - } - - private function connect() { - if ($this->conn === NULL) { - $user = getenv("POSTGRES_USER"); - $pass = getenv("POSTGRES_PASSWORD"); - $db = getenv("POSTGRES_DB"); - $host = 'db'; - $port = '5432'; - - $conn_str = sprintf("pgsql:host=%s;port=%d;dbname=%s;user=%s;password=%s", - $host, - $port, - $db, - $user, - $pass - ); - $this->conn = new \PDO($conn_str); - $this->conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); - } - return $this->conn; - } - - public function select($select) { - $conn = $this->connect(); - $query = new DatabaseQuery($conn); - return $query->select($select); - } - - public function insert_into($insert, ...$columns) { - $conn = $this->connect(); - $query = new DatabaseQuery($conn); - return $query->insert_into($insert, ...$columns); - } - - public function query($query_str) { - $conn = $this->connect(); - $query = new DatabaseQuery($conn); - return $query->query($query_str); - } -} - diff --git a/src/web/core/loader.php b/src/web/core/loader.php deleted file mode 100644 index 2091533..0000000 --- a/src/web/core/loader.php +++ /dev/null @@ -1,101 +0,0 @@ -loaded = array(); - } - - /** - * Loads a $type of object from a $dir with a given $name - * @param string $name - the name of the object to load - * @param string $dir - the directory theese objects are stored in - * @param string $type - the type of the object - */ - private function load_type($name, $dir, $type): object|NULL { - $path = $dir . '/' . $name . '.php'; - if (array_key_exists($path, $this->loaded)) { - return $this->loaded[$path]; - } - - if (!file_exists($path)) { - return NULL; - } - - $parts = explode('/', $name); - $part = end($parts); - $class = ucfirst($part) . '_' . $type; - require($path); - - $ref = NULL; - try { - $ref = new ReflectionClass($class); - } catch (Exception $_e) {} - - if ($ref === NULL) { - return NULL; - } - - $obj = $ref->newInstance($this); - $this->loaded[$path] = $obj; - - return $obj; - } - - /** - * Loads a model - * @param string $name - the name of the model to load - */ - public function model($name): object|NULL { - $root = $GLOBALS['webroot']; - $dir = $root . '/_model'; - return $this->load_type($name, $dir, 'model'); - } - - /** - * Loads a controller - * @param string $name - the name of the controller to load - */ - public function controller($name): Controller|NULL { - $root = $GLOBALS['webroot']; - $dir = $root . '/_controller'; - return $this->load_type($name, $dir, 'controller'); - } - - /** - * Loads the given common lang - * @param string $lang_code 0 the language code - */ - public function lang($lang_code): void { - $dir = $GLOBALS['webroot'] . '/lang/' . $lang_code . '/'; - $lang = $GLOBALS['lang']; - if ($handle = opendir($dir)) { - while (false !== ($entry = readdir($handle))) { - if ($entry === '.' || $entry === '..' || $entry === 'apps') { - continue; - } - $path = $dir . $entry; - require($path); - } - } - $GLOBALS['lang'] = $lang; - } - - /** - * Loads a given app specific lang - * @param string $lang_code - the language code - * @param string $name - the name of the app - */ - public function app_lang($lang_code, $name): void { - $dir = $GLOBALS['webroot'] . '/lang/' . $lang_code . '/apps/'; - $file = $dir . $name . '.php'; - if (file_exists($file)) { - $lang = $GLOBALS['lang']; - require($dir . $name . '.php'); - $GLOBALS['lang'] = $lang; - } - } - -} diff --git a/src/web/core/router.php b/src/web/core/router.php deleted file mode 100644 index 557665b..0000000 --- a/src/web/core/router.php +++ /dev/null @@ -1,159 +0,0 @@ -load = $load; - $this->main = $this->load->model('main'); - } - - /** - * @param string $path - the current request path - * Gets the current route - * @return array - */ - private function get_req_route($path): array { - // trim the path - $path = trim($path); - // remove first '/' - $path = substr($path, 1); - // get path parts - $parts = explode('/', $path); - - $len = count($parts); - - // get route info - $route = array(); - // e.g. / - if ($path === '') { - $route = array( - 'route' => '', - 'slug' => 'index', - ); - // e.g. /home /login - } else if ($len === 1) { - $route = array( - 'route' => $parts[0], - 'slug' => 'index', - ); - // e.g. /home/posts - } else { - $route = array ( - 'route' => implode('/', array_slice($parts, 0, -1)), - 'slug' => end($parts) - ); - }; - - $route['app'] = $route['route']; - $routes = $GLOBALS['routes']; - if (array_key_exists($route['route'], $routes)) { - $route['route'] = $routes[$route['route']]; - } - - return $route; - } - - /** - * Gets the curret request info - * @return array - */ - private function get_req(): array|bool { - $method = $_SERVER['REQUEST_METHOD']; - - $uri = parse_url($_SERVER['REQUEST_URI']); - if (!$uri) { - return FALSE; - } - - $path = $uri['path']; - - return array_merge( - array( - 'uri' => $uri, - 'method' => $method, - 'lang' => $this->get_lang(), - ), - $this->get_req_route($path), - ); - } - - /** - * Gets the current language - * @return string - */ - private function get_lang(): string { - return 'en_US'; - } - - /** - * Handles a router error code - * @param int $code - the http error code - * @param bool $recursed - */ - private function handle_error($code, $recursed): void { - if ($recursed) { - die($code . ' (recursed)'); - } - - $this->main->info = array(); - $this->main->info['slug'] = 'index'; - $this->main->info['app'] = 'error'; - $this->main->info['route'] = 'apps/error'; - $this->main->info['lang'] = $this->get_lang(); - $req = $this->main->info; - $_GET['code'] = $code; - - $this->handle_req($req, TRUE); - } - - /** - * @param array $req - * @param bool $recursed - */ - private function handle_req($req, $recursed = FALSE): void { - - if ($req === FALSE) { - $this->handle_error(500, $recursed); - return; - } - - $controller = $this->load->controller($req['route']); - - if ($controller === NULL) { - $this->handle_error(404, $recursed); - return; - } - - $ref = NULL; - try { - $ref = new ReflectionMethod($controller, $req['slug']); - } catch (Exception $_e) {} - - if ($ref === NULL || !$ref->isPublic()) { - $this->handle_error(404, $recursed); - return; - - } - - $ref->invoke($controller); - } - - /** - * Handels the incomming reuqest - */ - public function handle_request(): void { - $req = $this->get_req(); - $this->main->info = $req; - $this->handle_req($req); - } - -} diff --git a/src/web/helper/error.php b/src/web/helper/error.php deleted file mode 100644 index 2b6959e..0000000 --- a/src/web/helper/error.php +++ /dev/null @@ -1,9 +0,0 @@ -'; - } else if ($click) { - $content .= '