From 3b2df3b96c49086136efd77789bc15ba64ed7d50 Mon Sep 17 00:00:00 2001 From: flashwave Date: Mon, 28 Mar 2016 03:18:59 +0200 Subject: [PATCH] it's 3:15 am, why am i still down code *doing --- libraries/Comment.php | 112 ++++++++++++++++++ .../Settings/AccountController.php | 42 +++++++ .../Settings/AdvancedController.php | 27 +++++ .../Settings/AppearanceController.php | 42 +++++++ libraries/Controllers/Settings/Controller.php | 36 ++++++ .../Settings/FriendsController.php | 27 +++++ .../Settings/GeneralController.php | 32 +++++ .../Controllers/Settings/GroupsController.php | 27 +++++ .../Settings/NotificationsController.php | 22 ++++ libraries/News/Category.php | 1 + libraries/News/Post.php | 1 + libraries/Notification.php | 14 +++ libraries/Router.php | 14 ++- libraries/Urls.php | 21 +++- routes.php | 89 ++++++++++++++ 15 files changed, 502 insertions(+), 5 deletions(-) create mode 100644 libraries/Comment.php create mode 100644 libraries/Controllers/Settings/AccountController.php create mode 100644 libraries/Controllers/Settings/AdvancedController.php create mode 100644 libraries/Controllers/Settings/AppearanceController.php create mode 100644 libraries/Controllers/Settings/Controller.php create mode 100644 libraries/Controllers/Settings/FriendsController.php create mode 100644 libraries/Controllers/Settings/GeneralController.php create mode 100644 libraries/Controllers/Settings/GroupsController.php create mode 100644 libraries/Controllers/Settings/NotificationsController.php create mode 100644 libraries/News/Category.php create mode 100644 libraries/News/Post.php diff --git a/libraries/Comment.php b/libraries/Comment.php new file mode 100644 index 0000000..89396bf --- /dev/null +++ b/libraries/Comment.php @@ -0,0 +1,112 @@ + + */ +class Comment +{ + public $id = 0; + public $category = ""; + public $time = 0; + public $user = 0; + public $reply = 0; + public $text = ""; + public $upvotes = 0; + public $downvotes = 0; + private $replyCache = []; + + public function __construct($id = 0) + { + // Get comment data from the database + $data = DB::table('comments') + ->where('comment_id', $id) + ->get(); + + // Check if anything was returned and assign data + if ($data) { + $data = $data[0]; + + $this->id = $data->comment_id; + $this->category = $data->comment_category; + $this->time = $data->comment_timestamp; + $this->user = $data->comment_poster; + $this->reply = $data->comment_reply_to; + $this->text = $data->comment_text; + + $this->getVotes(); + } + } + + public function save() + { + // Create submission data, insert and update take the same format + $data = [ + 'comment_category' => $this->category, + 'comment_timestamp' => $this->time, + 'comment_poster' => $this->user, + 'comment_reply_to' => $this->reply, + 'comment_text' => $this->text, + ]; + + // Update if id isn't 0 + if ($this->id) { + DB::table('comments') + ->where('comment_id', $this->id) + ->update($data); + } else { + $this->id = DB::table('comments') + ->insertGetId($data); + } + } + + private function getVotes() + { + $votes = DB::table('comment_votes') + ->where('vote_comment', $this->id) + ->get(); + + foreach ($votes as $vote) { + if ($vote->vote_state) { + $upvotes += 1; + } else { + $downvotes += 1; + } + } + } + + public function replies() + { + if (!$this->replyCache) { + $commentIds = DB::table('comments') + ->where('comment_reply_to', $this->id) + ->get(['comment_id']); + $commentIds = array_column($comments, 'comment_id'); + + foreach ($commentIds as $comment) { + $this->replyCache[$comment] = new Comment($comment); + } + } + + return $this->replyCache; + } + + public function userData() + { + return User::construct($this->user); + } + + public function vote() + { + // can't be fucked to implement this right now + } +} diff --git a/libraries/Controllers/Settings/AccountController.php b/libraries/Controllers/Settings/AccountController.php new file mode 100644 index 0000000..35aaf19 --- /dev/null +++ b/libraries/Controllers/Settings/AccountController.php @@ -0,0 +1,42 @@ + + */ +class AccountController extends Controller +{ + public function email() + { + return $this->go('account.email'); + } + + public function username() + { + return $this->go('account.username'); + } + + public function title() + { + return $this->go('account.usertitle'); + } + + public function password() + { + return $this->go('account.password'); + } + + public function ranks() + { + return $this->go('account.ranks'); + } +} diff --git a/libraries/Controllers/Settings/AdvancedController.php b/libraries/Controllers/Settings/AdvancedController.php new file mode 100644 index 0000000..e0d0dff --- /dev/null +++ b/libraries/Controllers/Settings/AdvancedController.php @@ -0,0 +1,27 @@ + + */ +class AdvancedController extends Controller +{ + public function sessions() + { + return $this->go('advanced.sessions'); + } + + public function deactivate() + { + return $this->go('advanced.deactivate'); + } +} diff --git a/libraries/Controllers/Settings/AppearanceController.php b/libraries/Controllers/Settings/AppearanceController.php new file mode 100644 index 0000000..35e2e83 --- /dev/null +++ b/libraries/Controllers/Settings/AppearanceController.php @@ -0,0 +1,42 @@ + + */ +class AppearanceController extends Controller +{ + public function avatar() + { + return $this->go('appearance.avatar'); + } + + public function background() + { + return $this->go('appearance.background'); + } + + public function header() + { + return $this->go('appearance.header'); + } + + public function userpage() + { + return $this->go('appearance.userpage'); + } + + public function signature() + { + return $this->go('appearance.signature'); + } +} diff --git a/libraries/Controllers/Settings/Controller.php b/libraries/Controllers/Settings/Controller.php new file mode 100644 index 0000000..6f4acf9 --- /dev/null +++ b/libraries/Controllers/Settings/Controller.php @@ -0,0 +1,36 @@ + + */ +class Controller extends BaseController +{ + private $urls; + + public function __construct() + { + $this->urls = new Urls(); + } + + public function go($location) + { + $location = explode('.', $location); + + $url = $this->urls->format('SETTING_MODE', $location, null, false); + + return header("Location: {$url}"); + } +} diff --git a/libraries/Controllers/Settings/FriendsController.php b/libraries/Controllers/Settings/FriendsController.php new file mode 100644 index 0000000..11d4343 --- /dev/null +++ b/libraries/Controllers/Settings/FriendsController.php @@ -0,0 +1,27 @@ + + */ +class FriendsController extends Controller +{ + public function listing() + { + return $this->go('friends.listing'); + } + + public function requests() + { + return $this->go('friends.requests'); + } +} diff --git a/libraries/Controllers/Settings/GeneralController.php b/libraries/Controllers/Settings/GeneralController.php new file mode 100644 index 0000000..bd61184 --- /dev/null +++ b/libraries/Controllers/Settings/GeneralController.php @@ -0,0 +1,32 @@ + + */ +class GeneralController extends Controller +{ + public function home() + { + return $this->go('general.home'); + } + + public function profile() + { + return $this->go('general.profile'); + } + + public function options() + { + return $this->go('general.options'); + } +} diff --git a/libraries/Controllers/Settings/GroupsController.php b/libraries/Controllers/Settings/GroupsController.php new file mode 100644 index 0000000..05355ef --- /dev/null +++ b/libraries/Controllers/Settings/GroupsController.php @@ -0,0 +1,27 @@ + + */ +class GroupsController extends Controller +{ + public function listing() + { + return ""; + } + + public function invites() + { + return ""; + } +} diff --git a/libraries/Controllers/Settings/NotificationsController.php b/libraries/Controllers/Settings/NotificationsController.php new file mode 100644 index 0000000..2e677a9 --- /dev/null +++ b/libraries/Controllers/Settings/NotificationsController.php @@ -0,0 +1,22 @@ + + */ +class NotificationsController extends Controller +{ + public function history() + { + return $this->go('notifications.history'); + } +} diff --git a/libraries/News/Category.php b/libraries/News/Category.php new file mode 100644 index 0000000..66ce400 --- /dev/null +++ b/libraries/News/Category.php @@ -0,0 +1 @@ +donderdag diff --git a/libraries/News/Post.php b/libraries/News/Post.php new file mode 100644 index 0000000..746397b --- /dev/null +++ b/libraries/News/Post.php @@ -0,0 +1 @@ +vrijdag diff --git a/libraries/Notification.php b/libraries/Notification.php index 24f4ffa..15aca3f 100644 --- a/libraries/Notification.php +++ b/libraries/Notification.php @@ -73,9 +73,23 @@ class Notification } } + public function delete() + { + DB::table('comments') + ->where('comment_id', $this->id) + ->delete(); + + $this->id = 0; + } + public function toggleRead() { // Set read to the negative value of itself $this->read = !$this->read; } + + public function userData() + { + return User::construct($this->user); + } } diff --git a/libraries/Router.php b/libraries/Router.php index 5da4112..1dc0124 100644 --- a/libraries/Router.php +++ b/libraries/Router.php @@ -68,7 +68,19 @@ class Router // Check if the method exists if (in_array($name = strtoupper($name), self::$methods)) { $path = isset($args[2]) && $args !== null ? [$args[0], $args[2]] : $args[0]; - $handler = is_callable($args[1]) || is_array($args[1]) ? $args[1] : explode('@', ('Sakura\Controllers\\' . $args[1])); + $handler = is_callable($args[1]) || is_array($args[1]) + ? $args[1] + : explode( + '@', + ( + 'Sakura\\Controllers\\' + . str_replace( + '.', + '\\', + $args[1] + ) + ) + ); $filter = isset($args[3]) ? $args[3] : []; self::$router->addRoute($name, $path, $handler, $filter); diff --git a/libraries/Urls.php b/libraries/Urls.php index c60a741..994fe7f 100644 --- a/libraries/Urls.php +++ b/libraries/Urls.php @@ -301,7 +301,7 @@ class Urls * * @return null|string The formatted URL. */ - public function format($lid, $args = [], $rewrite = null) + public function format($lid, $args = [], $rewrite = null, $b = true) { // Check if the requested url exists @@ -309,11 +309,24 @@ class Urls return null; } - // Check if mod_rewrite is enabled - $rewrite = 0; // Pretty much disabled forever because of the router -- ($rewrite === null ? Config::get('url_rewrite') : $rewrite) ? 1 : 0; + if ($b && ($lid === 'SETTING_CAT' || $lid === 'SETTING_MODE')) { + if (in_array('messages', $args)) { + return null; + } + + if ($lid === 'SETTING_CAT') { + return Router::route("settings.{$args[0]}"); + } + + if ($lid === 'SETTING_MODE') { + $a = implode('.', $args); + $a = str_replace("usertitle", "title", $a); + return Router::route("settings.{$a}"); + } + } // Format urls - $formatted = vsprintf($this->urls[$lid][$rewrite], $args); + $formatted = vsprintf($this->urls[$lid][0], $args); // Return the formatted url return $formatted; diff --git a/routes.php b/routes.php index 42a184a..3f95bfb 100644 --- a/routes.php +++ b/routes.php @@ -122,6 +122,95 @@ Router::group(['prefix' => 'helper'], function () { }); // Settings +Router::group(['prefix' => 'settings', 'before' => 'loginCheck'], function () { + Router::get('/', function () { + $route = Router::route('settings.general.home'); + return header("Location: {$route}"); + }); + + // General section + Router::group(['prefix' => 'general'], function () { + Router::get('/', function () { + $route = Router::route('settings.general.home'); + return header("Location: {$route}"); + }); + + Router::get('/home', 'Settings.GeneralController@home', 'settings.general.home'); + Router::get('/profile', 'Settings.GeneralController@profile', 'settings.general.profile'); + Router::get('/options', 'Settings.GeneralController@options', 'settings.general.options'); + }); + + // Friends section + Router::group(['prefix' => 'friends'], function () { + Router::get('/', function () { + $route = Router::route('settings.friends.listing'); + return header("Location: {$route}"); + }); + + Router::get('/listing', 'Settings.FriendsController@listing', 'settings.friends.listing'); + Router::get('/requests', 'Settings.FriendsController@requests', 'settings.friends.requests'); + }); + + // Groups section + Router::group(['prefix' => 'groups'], function () { + Router::get('/', function () { + $route = Router::route('settings.groups.listing'); + return header("Location: {$route}"); + }); + + Router::get('/listing', 'Settings.GroupsController@listing', 'settings.groups.listing'); + Router::get('/invites', 'Settings.GroupsController@invites', 'settings.groups.invites'); + }); + + // Notifications section + Router::group(['prefix' => 'notifications'], function () { + Router::get('/', function () { + $route = Router::route('settings.notifications.history'); + return header("Location: {$route}"); + }); + + Router::get('/history', 'Settings.NotificationsController@history', 'settings.notifications.history'); + }); + + // Appearance section + Router::group(['prefix' => 'appearance'], function () { + Router::get('/', function () { + $route = Router::route('settings.appearance.avatar'); + return header("Location: {$route}"); + }); + + Router::get('/avatar', 'Settings.AppearanceController@avatar', 'settings.appearance.avatar'); + Router::get('/background', 'Settings.AppearanceController@background', 'settings.appearance.background'); + Router::get('/header', 'Settings.AppearanceController@header', 'settings.appearance.header'); + Router::get('/userpage', 'Settings.AppearanceController@userpage', 'settings.appearance.userpage'); + Router::get('/signature', 'Settings.AppearanceController@signature', 'settings.appearance.signature'); + }); + + // Account section + Router::group(['prefix' => 'account'], function () { + Router::get('/', function () { + $route = Router::route('settings.account.email'); + return header("Location: {$route}"); + }); + + Router::get('/email', 'Settings.AccountController@avatar', 'settings.account.email'); + Router::get('/username', 'Settings.AccountController@username', 'settings.account.username'); + Router::get('/title', 'Settings.AccountController@title', 'settings.account.title'); + Router::get('/password', 'Settings.AccountController@password', 'settings.account.password'); + Router::get('/ranks', 'Settings.AccountController@ranks', 'settings.account.ranks'); + }); + + // Advanced section + Router::group(['prefix' => 'advanced'], function () { + Router::get('/', function () { + $route = Router::route('settings.advanced.sessions'); + return header("Location: {$route}"); + }); + + Router::get('/sessions', 'Settings.AdvancedController@sessions', 'settings.advanced.sessions'); + Router::get('/deactivate', 'Settings.AdvancedController@deactivate', 'settings.advanced.deactivate'); + }); +}); /* * General * - Home (make this not worthless while you're at it)