From bbfd22d14dac723830ab333bbedcf0b4c99dcfcd Mon Sep 17 00:00:00 2001 From: flashwave Date: Mon, 15 Feb 2016 22:20:46 +0100 Subject: [PATCH] r20160215 --- libraries/BBcode.php | 2 +- libraries/Config.php | 32 +-- libraries/Controllers/Auth.php | 2 +- libraries/Controllers/Controller.php | 18 ++ libraries/Controllers/Files.php | 154 +++++++++++++ libraries/Controllers/Forums.php | 2 +- libraries/Controllers/Meta.php | 4 +- libraries/Controllers/Premium.php | 5 +- libraries/Controllers/User.php | 2 +- libraries/Router.php | 6 +- public/imageserve.php | 191 ---------------- public/manage.php | 313 --------------------------- routes.php | 58 ++++- sakura.php | 7 +- 14 files changed, 248 insertions(+), 548 deletions(-) create mode 100644 libraries/Controllers/Controller.php create mode 100644 libraries/Controllers/Files.php delete mode 100644 public/imageserve.php delete mode 100644 public/manage.php diff --git a/libraries/BBcode.php b/libraries/BBcode.php index 9dddf80..8287957 100644 --- a/libraries/BBcode.php +++ b/libraries/BBcode.php @@ -51,7 +51,7 @@ class BBcode $emotes = Database::fetch('emoticons'); // Parse all emoticons - foreach($emotes as $emote) { + foreach ($emotes as $emote) { $image = "{$emote["; $icon = preg_quote($emote['emote_string'], '#'); $text = preg_replace("#$icon#", $image, $text); diff --git a/libraries/Config.php b/libraries/Config.php index d416832..e035377 100644 --- a/libraries/Config.php +++ b/libraries/Config.php @@ -23,7 +23,7 @@ class Config private static $local = []; /** - * Container for the configuration stored in the database. + * Cache for the configuration stored in the database. * * @var array */ @@ -36,7 +36,6 @@ class Config */ public static function init($local) { - // Check if the configuration file exists if (!file_exists($local)) { trigger_error('Local configuration file does not exist', E_USER_ERROR); @@ -58,27 +57,6 @@ class Config } } - /** - * Fetch configuration values from the database. - */ - public static function initDB() - { - - // Get config table from the database - $_DATA = Database::fetch('config', true); - - // Create variable to temporarily store values in - $_DBCN = []; - - // Properly sort the values - foreach ($_DATA as $_CONF) { - $_DBCN[$_CONF['config_name']] = $_CONF['config_value']; - } - - // Assign the temporary array to the static one - self::$database = $_DBCN; - } - /** * Get a value from the local configuration file. * @@ -89,7 +67,6 @@ class Config */ public static function local($key, $subkey = null) { - // Check if the key that we're looking for exists if (array_key_exists($key, self::$local)) { if ($subkey) { @@ -119,11 +96,16 @@ class Config */ public static function get($key, $returnNull = false) { - // Check if the key that we're looking for exists if (array_key_exists($key, self::$database)) { // Then return the value return self::$database[$key]; + } else { + $value = Database::fetch('config', false, ['config_name' => [$key, '=']]); + if ($value) { + self::$database[$key] = $value['config_value']; + return self::$database[$key]; + } } // Then return the value diff --git a/libraries/Controllers/Auth.php b/libraries/Controllers/Auth.php index 7b8bd65..d270d29 100644 --- a/libraries/Controllers/Auth.php +++ b/libraries/Controllers/Auth.php @@ -15,7 +15,7 @@ use Sakura\Template; * @package Sakura * @author Julian van de Groep */ -class Auth +class Auth extends Controller { public function login() { diff --git a/libraries/Controllers/Controller.php b/libraries/Controllers/Controller.php new file mode 100644 index 0000000..a84f964 --- /dev/null +++ b/libraries/Controllers/Controller.php @@ -0,0 +1,18 @@ + + */ +class Controller +{ +} diff --git a/libraries/Controllers/Files.php b/libraries/Controllers/Files.php new file mode 100644 index 0000000..3be83f8 --- /dev/null +++ b/libraries/Controllers/Files.php @@ -0,0 +1,154 @@ + + */ +class Files extends Controller +{ + private function serveImage($data, $mime, $name) + { + // Add original filename + header('Content-Disposition: inline; filename="' . $name . '"'); + + // Set content type + header('Content-Type: ' . $mime); + + // Return image data + return $data; + } + + public function avatar($id = 0) + { + global $templateName; + + $noAvatar = ROOT . str_replace( + '{{ TPL }}', + $templateName, + Config::get('no_avatar_img') + ); + $none = [ + 'name' => basename($noAvatar), + 'data' => file_get_contents($noAvatar), + 'mime' => getimagesizefromstring($noAvatar)['mime'], + ]; + + $deactivePath = ROOT . str_replace( + '{{ TPL }}', + $templateName, + Config::get('deactivated_avatar_img') + ); + $deactive = [ + 'name' => basename($deactivePath), + 'data' => file_get_contents($deactivePath), + 'mime' => getimagesizefromstring($deactivePath)['mime'], + ]; + + $bannedPath = ROOT . str_replace( + '{{ TPL }}', + $templateName, + Config::get('banned_avatar_img') + ); + $banned = [ + 'name' => basename($bannedPath), + 'data' => file_get_contents($bannedPath), + 'mime' => getimagesizefromstring($bannedPath)['mime'], + ]; + + $user = User::construct($id); + + if ($user->permission(Site::DEACTIVATED)) { + return $this->serveImage($deactive['data'], $deactive['mime'], $deactive['name']); + } + + if ($user->checkBan() || $user->permission(Site::RESTRICTED)) { + return $this->serveImage($banned['data'], $banned['mime'], $banned['name']); + } + + if (!$user->avatar) { + return $this->serveImage($none['data'], $none['mime'], $none['name']); + } + + $serve = new File($user->avatar); + + if (!$serve->id) { + return $this->serveImage($none['data'], $none['mime'], $none['name']); + } + + return $this->serveImage($serve->data, $serve->mime, $serve->name); + } + + public function background($id = 0) + { + global $templateName; + + $noBg = ROOT . Config::get('no_background_img'); + $none = [ + 'name' => basename($noBg), + 'data' => file_get_contents($noBg), + 'mime' => getimagesizefromstring($noBg)['mime'], + ]; + + if (!$id) { + return $this->serveImage($none['data'], $none['mime'], $none['name']); + } + + $user = User::construct($id); + + if ($user->permission(Site::DEACTIVATED) || $user->checkBan() || $user->permission(Site::RESTRICTED) || !$user->background) { + return $this->serveImage($none['data'], $none['mime'], $none['name']); + } + + $serve = new File($user->background); + + if (!$serve->id) { + return $this->serveImage($none['data'], $none['mime'], $none['name']); + } + + return $this->serveImage($serve->data, $serve->mime, $serve->name); + } + + public function header($id = 0) + { + global $templateName; + + $noHeader = ROOT . Config::get('no_header_img'); + $none = [ + 'name' => basename($noHeader), + 'data' => file_get_contents($noHeader), + 'mime' => getimagesizefromstring($noHeader)['mime'], + ]; + + if (!$id) { + return $this->serveImage($none['data'], $none['mime'], $none['name']); + } + + $user = User::construct($id); + + if ($user->permission(Site::DEACTIVATED) || $user->checkBan() || $user->permission(Site::RESTRICTED) || !$user->header) { + return $this->serveImage($none['data'], $none['mime'], $none['name']); + } + + $serve = new File($user->header); + + if (!$serve->id) { + return $this->serveImage($none['data'], $none['mime'], $none['name']); + } + + return $this->serveImage($serve->data, $serve->mime, $serve->name); + } +} diff --git a/libraries/Controllers/Forums.php b/libraries/Controllers/Forums.php index 8836103..5791014 100644 --- a/libraries/Controllers/Forums.php +++ b/libraries/Controllers/Forums.php @@ -22,7 +22,7 @@ use Sakura\Utils; * @package Sakura * @author Julian van de Groep */ -class Forums +class Forums extends Controller { /** * Serves the forum index. diff --git a/libraries/Controllers/Meta.php b/libraries/Controllers/Meta.php index 31e05f8..9f7edb2 100644 --- a/libraries/Controllers/Meta.php +++ b/libraries/Controllers/Meta.php @@ -16,12 +16,12 @@ use Sakura\Users; use Sakura\Utils; /** - * Meta page controllers (sections that aren't big enough to warrant a dedicated controller class). + * Meta page controllers (sections that aren't big enough to warrant a dedicated controller). * * @package Sakura * @author Julian van de Groep */ -class Meta +class Meta extends Controller { /** * Serves the site index. diff --git a/libraries/Controllers/Premium.php b/libraries/Controllers/Premium.php index 43aed3e..49775f7 100644 --- a/libraries/Controllers/Premium.php +++ b/libraries/Controllers/Premium.php @@ -23,7 +23,7 @@ use Sakura\Perms\Site; * @package Sakura * @author Julian van de Groep */ -class Premium +class Premium extends Controller { public function index() { @@ -96,8 +96,7 @@ class Premium // Attempt to complete the transaction try { $finalise = Payments::completeTransaction($_GET['paymentId'], $_GET['PayerID']); - } - catch (Exception $e) { + } catch (Exception $e) { return trigger_error('Something went horribly wrong.', E_USER_ERROR); } diff --git a/libraries/Controllers/User.php b/libraries/Controllers/User.php index 4521ac6..89b78da 100644 --- a/libraries/Controllers/User.php +++ b/libraries/Controllers/User.php @@ -20,7 +20,7 @@ use Sakura\Utils; * @package Sakura * @author Julian van de Groep */ -class User +class User extends Controller { /** * Display the profile of a user. diff --git a/libraries/Router.php b/libraries/Router.php index 2921572..8850090 100644 --- a/libraries/Router.php +++ b/libraries/Router.php @@ -143,10 +143,8 @@ class Router // Handle the request try { - try { - return self::$dispatcher->dispatch($method, $url); - } catch (HttpMethodNotAllowedException $e) { - } + return self::$dispatcher->dispatch($method, $url); + } catch (HttpMethodNotAllowedException $e) { } catch (HttpRouteNotFoundException $e) { } diff --git a/public/imageserve.php b/public/imageserve.php deleted file mode 100644 index 4cad769..0000000 --- a/public/imageserve.php +++ /dev/null @@ -1,191 +0,0 @@ -permission(Perms\Site::DEACTIVATED)) { - $serveImage = $deactiveAvatar; - break; - } - - // Check if user is banned - if ($user->checkBan() || $user->permission(Perms\Site::RESTRICTED)) { - $serveImage = $bannedAvatar; - break; - } - - // Check if user has an avatar set - if (!$user->avatar) { - $serveImage = $noAvatar; - break; - } - - // Attempt to get the file - $serve = new File($user->avatar); - - // Check if the file exists - if (!$serve->id) { - $serveImage = $noAvatar; - break; - } - - // Check if the avatar exist and assign it to a value - $serveImage = $serve->data; - $serveMime = $serve->mime; - $serveName = $serve->name; - break; - - case 'background': - // Set paths - $noBackground = ROOT . Config::get('no_background_img'); - - // If ?u= isn't set or if it isn't numeric - if (!isset($_GET['u']) || !is_numeric($_GET['u'])) { - $serveImage = $noBackground; - break; - } - - // Get user data - $user = User::construct($_GET['u']); - - // If user is deactivated use deactive avatar - if ($user->permission(Perms\Site::DEACTIVATED)) { - $serveImage = $noBackground; - break; - } - - // Check if user is banned - if (Bans::checkBan($_GET['u']) || $user->permission(Perms\Site::RESTRICTED)) { - $serveImage = $noBackground; - break; - } - - // Check if user has a background set - if (!$user->background) { - $serveImage = $noBackground; - break; - } - - // Attempt to get the file - $serve = new File($user->background); - - // Check if the file exists - if (!$serve->id) { - $serveImage = $noBackground; - break; - } - - // Check if the avatar exist and assign it to a value - $serveImage = $serve->data; - $serveMime = $serve->mime; - $serveName = $serve->name; - break; - - case 'header': - // Set paths - $noHeader = ROOT . Config::get('no_header_img'); - - // If ?u= isn't set or if it isn't numeric - if (!isset($_GET['u']) || !is_numeric($_GET['u'])) { - $serveImage = $noHeader; - break; - } - - // Get user data - $user = User::construct($_GET['u']); - - // If user is deactivated use deactive avatar - if ($user->permission(Perms\Site::DEACTIVATED)) { - $serveImage = $noHeader; - break; - } - - // Check if user is banned - if (Bans::checkBan($_GET['u']) || $user->permission(Perms\Site::RESTRICTED)) { - $serveImage = $noHeader; - break; - } - - // Check if user has a header set - if (!$user->header) { - $serveImage = $noHeader; - break; - } - - // Attempt to get the file - $serve = new File($user->header); - - // Check if the file exists - if (!$serve->id) { - $serveImage = $noHeader; - break; - } - - // Check if the avatar exist and assign it to a value - $serveImage = $serve->data; - $serveMime = $serve->mime; - $serveName = $serve->name; - break; - - default: - $serveImage = ROOT . Config::get('pixel_img'); - - } -} else { - $serveImage = ROOT . Config::get('pixel_img'); -} - -// Do some more checks -if (!isset($serveName) || !isset($serveMime)) { - $serveName = basename($serveImage); - $serveImage = file_get_contents($serveImage); - $serveMime = getimagesizefromstring($serveImage)['mime']; -} - -// Add original filename -header('Content-Disposition: inline; filename="' . $serveName . '"'); - -// Set content type -header('Content-Type: ' . $serveMime); - -echo $serveImage; diff --git a/public/manage.php b/public/manage.php deleted file mode 100644 index 58ba6f4..0000000 --- a/public/manage.php +++ /dev/null @@ -1,313 +0,0 @@ -permission(Manage::USE_MANAGE, Perms::MANAGE)) { - // Set parse variables - Template::vars($renderData); - - // Print page contents - echo Template::render('global/restricted'); - exit; -} - -// Modes -$pages = [ - 'general' => [ - 'title' => 'General', - 'modes' => [ - 'dashboard' => [ - 'title' => 'Dashboard', - 'description' => [ - 'Welcome to the Broomcloset! Here\'s a quick overview of the site.', - ], - 'access' => !$currentUser->permission(Site::DEACTIVATED), - 'menu' => true, - ], - 'info' => [ - 'title' => 'Info pages', - 'description' => [ - 'Manage and edit the info pages.', - ], - 'access' => !$currentUser->permission(Site::DEACTIVATED), - 'menu' => true, - ], - ], - ], - 'config' => [ - 'title' => 'Configuration', - 'modes' => [ - 'general' => [ - 'title' => 'General', - 'description' => [ - 'Manages the appearance of the site and most other options that don\'t need their own category.', - ], - 'access' => !$currentUser->permission(Site::DEACTIVATED), - 'menu' => true, - ], - 'uploads' => [ - 'title' => 'Uploads', - 'description' => [ - 'Settings regarding uploads like avatars and backgrounds.', - ], - 'access' => !$currentUser->permission(Site::DEACTIVATED), - 'menu' => true, - ], - 'premium' => [ - 'title' => 'Premium', - 'description' => [ - 'Alters the way the premium system works.', - ], - 'access' => !$currentUser->permission(Site::DEACTIVATED), - 'menu' => true, - ], - 'user' => [ - 'title' => 'User', - 'description' => [ - 'Settings regarding users such as registration.', - ], - 'access' => !$currentUser->permission(Site::DEACTIVATED), - 'menu' => true, - ], - 'mail' => [ - 'title' => 'Mail', - 'description' => [ - 'How will Sakura send e-mails.', - ], - 'access' => !$currentUser->permission(Site::DEACTIVATED), - 'menu' => true, - ], - ], - ], - 'forums' => [ - 'title' => 'Forums', - 'modes' => [ - 'manage' => [ - 'title' => 'Manage', - 'description' => [ - 'Change the forums.', - ], - 'access' => !$currentUser->permission(Site::DEACTIVATED), - 'menu' => true, - ], - 'settings' => [ - 'title' => 'Settings', - 'description' => [ - 'Alter settings specific to the forum.', - ], - 'access' => !$currentUser->permission(Site::DEACTIVATED), - 'menu' => true, - ], - ], - ], - 'comments' => [ - 'title' => 'Comments', - 'modes' => [ - 'manage' => [ - 'title' => 'Manage', - 'description' => [ - 'View all the comment categories.', - ], - 'access' => !$currentUser->permission(Site::DEACTIVATED), - 'menu' => true, - ], - ], - ], - 'users' => [ - 'title' => 'Users', - 'modes' => [ - 'manage-users' => [ - 'title' => 'Manage users', - 'description' => [ - 'View and change users.', - ], - 'access' => !$currentUser->permission(Site::DEACTIVATED), - 'menu' => true, - ], - 'manage-ranks' => [ - 'title' => 'Manage ranks', - 'description' => [ - 'View and change ranks.', - ], - 'access' => !$currentUser->permission(Site::DEACTIVATED), - 'menu' => true, - ], - 'profile-fields' => [ - 'title' => 'Profile fields', - 'description' => [ - 'Manage the custom profile fields.', - ], - 'access' => !$currentUser->permission(Site::DEACTIVATED), - 'menu' => true, - ], - 'option-fields' => [ - 'title' => 'Option fields', - 'description' => [ - 'Manage the custom option fields.', - ], - 'access' => !$currentUser->permission(Site::DEACTIVATED), - 'menu' => true, - ], - 'bans' => [ - 'title' => 'Bans', - 'description' => [ - 'Banning users.', - ], - 'access' => !$currentUser->permission(Site::DEACTIVATED), - 'menu' => true, - ], - 'warnings' => [ - 'title' => 'Warnings', - 'description' => [ - 'Warn users.', - ], - 'access' => !$currentUser->permission(Site::DEACTIVATED), - 'menu' => true, - ], - ], - ], - 'permissions' => [ - 'title' => 'Permissions', - 'modes' => [ - 'site' => [ - 'title' => 'Manage site', - 'description' => [ - 'Alter the global site perms.', - ], - 'access' => !$currentUser->permission(Site::DEACTIVATED), - 'menu' => true, - ], - 'management' => [ - 'title' => 'Manage management', - 'description' => [ - 'Alter the management/moderation perms.', - ], - 'access' => !$currentUser->permission(Site::DEACTIVATED), - 'menu' => true, - ], - 'forum' => [ - 'title' => 'Manage forums', - 'description' => [ - 'Alter the perms of the forums.', - ], - 'access' => !$currentUser->permission(Site::DEACTIVATED), - 'menu' => true, - ], - ], - ], - 'logs' => [ - 'title' => 'Logs', - 'modes' => [ - 'actions' => [ - 'title' => 'Actions', - 'description' => [ - 'Viewing the global action logs.', - ], - 'access' => !$currentUser->permission(Site::DEACTIVATED), - 'menu' => true, - ], - 'management' => [ - 'title' => 'Management', - 'description' => [ - 'Viewing the management actions taken by staff.', - ], - 'access' => !$currentUser->permission(Site::DEACTIVATED), - 'menu' => true, - ], - 'errors' => [ - 'title' => 'Errors', - 'description' => [ - 'Viewing the PHP error logs Sakura was able to log.', - ], - 'access' => !$currentUser->permission(Site::DEACTIVATED), - 'menu' => true, - ], - ], - ], -]; - -// Current settings page -$category = isset($_GET['cat']) ? ( - array_key_exists($_GET['cat'], $pages) ? $_GET['cat'] : false -) : array_keys($pages)[0]; -$mode = false; - -// Only continue setting mode if $category is true -if ($category) { - $mode = isset($_GET['mode']) && $category ? ( - array_key_exists($_GET['mode'], $pages[$category]['modes']) ? $_GET['mode'] : false - ) : array_keys($pages[$category]['modes'])[0]; -} - -// Not found -if (!$category - || empty($category) - || !$mode - || empty($mode) - || !$pages[$category]['modes'][$mode]['access']) { - header('HTTP/1.0 404 Not Found'); - - // Set parse variables - Template::vars($renderData); - - // Print page contents - echo Template::render('global/notfound'); - exit; -} - -// Set templates directory -$renderData['templates'] = 'manage'; - -// Render data -$renderData['current'] = $category . '.' . $mode; - -// Settings pages -$renderData['pages'] = $pages; - -// Page data -$renderData['page'] = [ - 'category' => $pages[$category]['title'], - 'mode' => $pages[$category]['modes'][$mode]['title'], - 'description' => $pages[$category]['modes'][$mode]['description'], -]; - -// Add special variables -switch ($category . '.' . $mode) { - case 'general.dashboard': - $renderData = array_merge($renderData, [ - 'stats' => [ - 'postcount' => Database::count('posts')[0], - 'threadcount' => Database::count('topics')[0], - 'commentcount' => Database::count('comments')[0], - 'userscount' => Database::count('users')[0], - 'bancount' => Database::count('bans')[0], - 'uploadcount' => count(glob(ROOT . Config::get('user_uploads') . '/*')) - 1, - ], - ]); - break; - - case 'logs.errors': - $errorLog = Database::fetch('error_log', true, null, ['error_id', true]); - $renderData = array_merge($renderData, ['errors' => $errorLog]); - break; -} - -// Set parse variables -Template::vars($renderData); - -// Print page contents -echo Template::render('main/settings'); diff --git a/routes.php b/routes.php index f24dcb8..aeb7b0a 100644 --- a/routes.php +++ b/routes.php @@ -25,16 +25,53 @@ Router::get('/forum', 'Forums@index', 'forums.index'); Router::get('/forum/{id}', 'Forums@forum', 'forums.forum'); // Members -Router::get('/members', 'User@members', 'members.all'); +Router::get('/members', 'User@members', 'members.index'); Router::get('/members/{rank}', 'User@members', 'members.rank'); // User Router::get('/u/{id}', 'User@profile', 'user.profile'); +Router::get('/u/{id}/header', 'Files@header', 'user.header'); + +// Files +Router::get('/a/{id}', 'Files@avatar', 'file.avatar'); +Router::get('/bg/{id}', 'Files@background', 'file.background'); // Premium Router::get('/support', 'Premium@index', 'premium.index'); Router::get('/support/tracker', 'Premium@tracker', 'premium.tracker'); +// Management +/* + * General + * - Dashboard + * - Info pages (possibly deprecate with wiki) + * Configuration + * - General + * - Files + * - User + * - Mail + * Forums + * - Manage + * - Settings + * Comments + * - Manage + * Users + * - Manage users + * - Manage ranks + * - Profile fields + * - Option fields + * - Bans and restrictions + * - Warnings + * Permissions + * - Site + * - Management + * - Forum + * Logs + * - Actions + * - Management + * - Errors + */ + // Redirections Router::any('/index.php', function () { // Info pages @@ -130,6 +167,25 @@ Router::any('/support.php', function () { header('Location: /support'); }); +Router::any('/imageserve.php', function () { + // Category + post + if (isset($_REQUEST['u']) && isset($_REQUEST['m'])) { + switch ($_REQUEST['m']) { + case 'avatar': + header('Location: /a/' . $_REQUEST['u']); + return; + case 'background': + header('Location: /bg/' . $_REQUEST['u']); + return; + case 'header': + header('Location: /u/' . $_REQUEST['u'] . '/header'); + return; + } + } + + header('Location: /bg/0'); +}); + Router::any('/faq.php', function () { header('Location: /faq'); }); diff --git a/sakura.php b/sakura.php index 8c2ccce..12b6f6b 100644 --- a/sakura.php +++ b/sakura.php @@ -8,7 +8,7 @@ namespace Sakura; // Define Sakura version -define('SAKURA_VERSION', '20160214'); +define('SAKURA_VERSION', '20160215'); define('SAKURA_VLABEL', 'Amethyst'); define('SAKURA_COLOUR', '#9966CC'); @@ -25,7 +25,7 @@ set_time_limit(0); // Set internal encoding method mb_internal_encoding('utf-8'); -// Stop the execution if the PHP Version is older than 5.4.0 +// Stop the execution if the PHP Version is older than 5.5.0 if (version_compare(phpversion(), '5.5.0', '<')) { die('Sakura requires at least PHP 5.5.0, please upgrade to a newer PHP version.'); } @@ -64,9 +64,6 @@ error_reporting(Config::local('dev', 'show_errors') ? -1 : 0); // Make the database connection Database::init(Config::local('database', 'driver')); -// Load the configuration stored in the database -Config::initDB(); - // Check if we're using console if (php_sapi_name() === 'cli' && !defined('SAKURA_CRON')) { $console = new Console\Application;