diff --git a/_sakura/changelog.json b/_sakura/changelog.json index 75cd474..4a173bc 100644 --- a/_sakura/changelog.json +++ b/_sakura/changelog.json @@ -2720,6 +2720,22 @@ "type": "ADD", "change": "Added Linux no cron task method.", "user": "Flashwave" + }, + { + "type": "ADD", + "change": "Remove old notifications after a month.", + "user": "Flashwave" + } + + ], + + "20150914": [ + + "eminence", + { + "type": "UPD", + "change": "Make indentation style PSR2 standard compliant.", + "user": "Flashwave" } ] diff --git a/_sakura/components/Bans.php b/_sakura/components/Bans.php index d2b1c91..c341616 100644 --- a/_sakura/components/Bans.php +++ b/_sakura/components/Bans.php @@ -5,37 +5,35 @@ namespace Sakura; -class Bans { - +class Bans +{ // Check if a user is banned - public static function checkBan($id) { + public static function checkBan($uid) + { // Attempt to get a ban from this user - $bans = Database::fetch('bans', true, ['uid' => [$id, '=']]); + $bans = Database::fetch('bans', true, ['uid' => [$uid, '=']]); // Reverse the array so new bans are listed first $bans = array_reverse($bans); // Go over each ban - foreach($bans as $ban) { - + foreach ($bans as $ban) { // Check if it hasn't expired - if($ban['ban_end'] != 0 && $ban['ban_end'] < time()) { - + if ($ban['ban_end'] != 0 && $ban['ban_end'] < time()) { // If it has delete the entry and continue - Database::delete('bans', ['id' => [$ban['id'], '=']]); + Database::delete('bans', ['id' => [$ban['uid'], '=']]); continue; - } // Return the ban if all checks were passed return [ - 'user' => $ban['uid'], - 'issuer' => $ban['mod_id'], - 'issued' => $ban['ban_begin'], - 'expires' => $ban['ban_end'], - 'reason' => $ban['ban_reason'] + 'user' => $ban['uid'], + 'issuer' => $ban['mod_uid'], + 'issued' => $ban['ban_begin'], + 'expires' => $ban['ban_end'], + 'reason' => $ban['ban_reason'], ]; @@ -45,5 +43,4 @@ class Bans { return false; } - } diff --git a/_sakura/components/Comments.php b/_sakura/components/Comments.php index cfee4af..13ec03b 100644 --- a/_sakura/components/Comments.php +++ b/_sakura/components/Comments.php @@ -5,8 +5,7 @@ namespace Sakura; -class Comments { - - +class Comments +{ } diff --git a/_sakura/components/Configuration.php b/_sakura/components/Configuration.php index 9b2b5fd..71685c3 100644 --- a/_sakura/components/Configuration.php +++ b/_sakura/components/Configuration.php @@ -5,35 +5,30 @@ namespace Sakura; -class Configuration { - +class Configuration +{ // Configuration data - private static $_LCNF = []; - private static $_DCNF = []; + private static $local = []; + private static $database = []; // Initialise configuration, does not contain database initialisation because explained below - public static function init($local) { + public static function init($local) + { // Check if the configuration file exists - if(!file_exists($local)) { - + if (!file_exists($local)) { trigger_error('Local configuration file does not exist', E_USER_ERROR); - } // Attempt to load the configuration file $local = parse_ini_file($local, true); - // Check if $local is an array and then store it in $_LCNF - if(is_array($local)) { - - self::$_LCNF = $local; - + // Check if $local is an array and then store it in $local + if (is_array($local)) { + self::$local = $local; } else { - // Otherwise trigger an error trigger_error('Failed to load local configuration file, check the structure of the file to see if you made mistake somewhere', E_USER_ERROR); - } } @@ -43,7 +38,8 @@ class Configuration { * Different from init as that is called before the database connection is initially * established. */ - public static function initDB() { + public static function initDB() + { // Get config table from the database $_DATA = Database::fetch('config', true); @@ -52,89 +48,70 @@ class Configuration { $_DBCN = array(); // Properly sort the values - foreach($_DATA as $_CONF) { - + foreach ($_DATA as $_CONF) { $_DBCN[$_CONF['config_name']] = $_CONF['config_value']; - } // Assign the temporary array to the static one - self::$_DCNF = $_DBCN; + self::$database = $_DBCN; } // Get values from the configuration on the file system - public static function getLocalConfig($key, $subkey = null) { + public static function getLocalConfig($key, $subkey = null) + { // Check if the key that we're looking for exists - if(array_key_exists($key, self::$_LCNF)) { - - if($subkey) { - + if (array_key_exists($key, self::$local)) { + if ($subkey) { // If we also have a subkey return the proper data - return self::$_LCNF[$key][$subkey]; - - } else { - - // else we just return the default value - return self::$_LCNF[$key]; - + return self::$local[$key][$subkey]; } - } else {// If it doesn't exist trigger an error to avoid explosions - - trigger_error('Unable to get local configuration value "'. $key .'"', E_USER_ERROR); - + // else we just return the default value + return self::$local[$key]; } + // If it doesn't exist trigger an error to avoid explosions + trigger_error('Unable to get local configuration value "' . $key . '"', E_USER_ERROR); + } // Dynamically set local configuration values, does not update the configuration file - public static function setLocalConfig($key, $subkey, $value) { + public static function setLocalConfig($key, $subkey, $value) + { // Check if we also do a subkey - if($subkey) { - + if ($subkey) { // If we do we make sure that the parent key is an array - if(!isset(self::$_LCNF[$key])) { - - self::$_LCNF[$key] = array(); - + if (!isset(self::$local[$key])) { + self::$local[$key] = array(); } // And then assign the value - self::$_LCNF[$key][$subkey] = $value; - - } else { - - // Otherwise we just straight up assign it - self::$_LCNF[$key] = $value; - + self::$local[$key][$subkey] = $value; } + // Otherwise we just straight up assign it + self::$local[$key] = $value; + } // Get values from the configuration in the database - public static function getConfig($key, $returnNull = false) { + public static function getConfig($key, $returnNull = false) + { // Check if the key that we're looking for exists - if(array_key_exists($key, self::$_DCNF)) { - + if (array_key_exists($key, self::$database)) { // Then return the value - return self::$_DCNF[$key]; - - } elseif($returnNull) { - + return self::$database[$key]; + } elseif ($returnNull) { // Avoid the error trigger if requested return null; - - } else { - - // Then return the value - trigger_error('Unable to get configuration value "'. $key .'"', E_USER_ERROR); - } - } + // Then return the value + trigger_error('Unable to get configuration value "' . $key . '"', E_USER_ERROR); + } } diff --git a/_sakura/components/Database.php b/_sakura/components/Database.php index a33643a..768d84e 100644 --- a/_sakura/components/Database.php +++ b/_sakura/components/Database.php @@ -5,69 +5,73 @@ namespace Sakura; -class Database { - +class Database +{ // Database container - public static $_DATABASE; + public static $database; // Initialisation function - public static function init($wrapper) { + public static function init($wrapper) + { // Make the wrapper class name lowercase - $wrapper = __NAMESPACE__ .'\DBWrapper\\'. strtolower($wrapper); + $wrapper = __NAMESPACE__ . '\DBWrapper\\' . strtolower($wrapper); // Check if the class exists - if(!class_exists($wrapper)) { - + if (!class_exists($wrapper)) { trigger_error('Failed to load database wrapper', E_USER_ERROR); - } // Initialise SQL wrapper - self::$_DATABASE = new $wrapper; + self::$database = new $wrapper; } // Select from database - public static function select($table, $data = null, $order = null, $limit = null, $group = null, $distinct = false, $column = '*', $prefix = null) { + public static function select($table, $data = null, $order = null, $limit = null, $group = null, $distinct = false, $column = '*', $prefix = null) + { - return self::$_DATABASE->select($table, $data, $order, $limit, $group, $distinct, $column, $prefix); + return self::$database->select($table, $data, $order, $limit, $group, $distinct, $column, $prefix); } // Fetch from database - public static function fetch($table, $fetchAll = true, $data = null, $order = null, $limit = null, $group = null, $distinct = false, $column = '*', $prefix = null) { + public static function fetch($table, $fetchAll = true, $data = null, $order = null, $limit = null, $group = null, $distinct = false, $column = '*', $prefix = null) + { - return self::$_DATABASE->fetch($table, $fetchAll, $data, $order, $limit, $group, $distinct, $column, $prefix); + return self::$database->fetch($table, $fetchAll, $data, $order, $limit, $group, $distinct, $column, $prefix); } // Insert into database - public static function insert($table, $data, $prefix = null) { + public static function insert($table, $data, $prefix = null) + { - return self::$_DATABASE->insert($table, $data, $prefix); + return self::$database->insert($table, $data, $prefix); } // Update in database - public static function update($table, $data, $prefix = null) { + public static function update($table, $data, $prefix = null) + { - return self::$_DATABASE->update($table, $data, $prefix); + return self::$database->update($table, $data, $prefix); } // Delete from database - public static function delete($table, $data, $prefix = null) { + public static function delete($table, $data, $prefix = null) + { - return self::$_DATABASE->delete($table, $data, $prefix); + return self::$database->delete($table, $data, $prefix); } // Count from database - public static function count($table, $data = null, $prefix = null) { + public static function count($table, $data = null, $prefix = null) + { - return self::$_DATABASE->count($table, $data, $prefix); + return self::$database->count($table, $data, $prefix); } - } diff --git a/_sakura/components/Forum.php b/_sakura/components/Forum.php index 2ec105d..236481b 100644 --- a/_sakura/components/Forum.php +++ b/_sakura/components/Forum.php @@ -5,22 +5,23 @@ namespace Sakura; -class Forum { - +class Forum +{ // Empty forum template public static $emptyForum = [ - 'forum_id' => 0, - 'forum_name' => 'Forum', - 'forum_desc' => '', - 'forum_link' => '', - 'forum_category' => 0, - 'forum_type' => 1, - 'forum_posts' => 0, - 'forum_topics' => 0 + 'forum_id' => 0, + 'forum_name' => 'Forum', + 'forum_desc' => '', + 'forum_link' => '', + 'forum_category' => 0, + 'forum_type' => 1, + 'forum_posts' => 0, + 'forum_topics' => 0, ]; // Getting the forum list - public static function getForumList() { + public static function getForumList() + { // Get the content from the database $forums = Database::fetch('forums'); @@ -29,36 +30,32 @@ class Forum { $return = [ 0 => [ 'forum' => self::$emptyForum, - 'forums' => [] - ] + 'forums' => [], + ], ]; // Resort the forums - foreach($forums as $forum) { - + foreach ($forums as $forum) { // If the forum type is a category create a new one - if($forum['forum_type'] == 1) { - + if ($forum['forum_type'] == 1) { $return[$forum['forum_id']]['forum'] = $forum; - } else { - // For link and reg. forum add it to the category $return[$forum['forum_category']]['forums'][$forum['forum_id']] = $forum; // Get the topic count $return[$forum['forum_category']]['forums'][$forum['forum_id']]['topic_count'] = Database::count('topics', [ - 'forum_id' => [$forum['forum_id'], '='] + 'forum_id' => [$forum['forum_id'], '='], ])[0]; // Get the post count $return[$forum['forum_category']]['forums'][$forum['forum_id']]['post_count'] = Database::count('posts', [ - 'forum_id' => [$forum['forum_id'], '='] + 'forum_id' => [$forum['forum_id'], '='], ])[0]; // Get last post in forum $lastPost = Database::fetch('posts', false, [ - 'forum_id' => [$forum['forum_id'], '='] + 'forum_id' => [$forum['forum_id'], '='], ], ['post_id', true]); // Add last poster data and the details about the post as well @@ -66,11 +63,9 @@ class Forum { 'post' => $lastPost, 'user' => ($_LAST_POSTER = Users::getUser($lastPost['poster_id'])), 'rank' => Users::getRank($_LAST_POSTER['rank_main']), - 'elap' => Main::timeElapsed($lastPost['post_time']) + 'elap' => Main::timeElapsed($lastPost['post_time']), ]; - } - } // Return the resorted data @@ -79,7 +74,8 @@ class Forum { } // Get a forum or category - public static function getForum($id) { + public static function getForum($id) + { // Get the forumlist from the database $forums = Database::fetch('forums'); @@ -91,46 +87,42 @@ class Forum { $forum = []; // Try to find the requested forum - foreach($forums as $list) { - + foreach ($forums as $list) { // Once found set $forum to $list and break the loop - if($list['forum_id'] == $id) { - + if ($list['forum_id'] == $id) { $forum['forum'] = $list; break; - } - } // If $forum is still empty after the foreach return false - if(empty($forum)) + if (empty($forum)) { return false; + } // Create conditions for fetching the forums $conditions['forum_category'] = [$id, '=']; // If the current category is 0 (the built in fallback) prevent getting categories - if($id == 0) + if ($id == 0) { $conditions['forum_type'] = ['1', '!=']; + } // Check if this forum/category has any subforums $forum['forums'] = Database::fetch('forums', true, $conditions); // Get the userdata related to last posts - foreach($forum['forums'] as $key => $sub) { - + foreach ($forum['forums'] as $key => $sub) { // Get last post in forum $lastPost = Database::fetch('posts', false, [ - 'forum_id' => [$sub['forum_id'], '='] + 'forum_id' => [$sub['forum_id'], '='], ], ['post_id', true]); $forum['forums'][$key]['last_poster'] = [ 'post' => $lastPost, - 'user' => ($_LAST_POSTER = Users::getUser($lastPost['poster_id'])), - 'rank' => Users::getRank($_LAST_POSTER['rank_main']) + 'user' => ($lastPoster = Users::getUser($lastPost['poster_id'])), + 'rank' => Users::getRank($lastPoster['rank_main']), ]; - } // Lastly grab the topics for this forum @@ -142,45 +134,44 @@ class Forum { } // Getting all topics from a forum - public static function getTopics($id) { + public static function getTopics($id) + { // Get the topics from the database $topics = Database::fetch('topics', true, [ - 'forum_id' => [$id, '='] + 'forum_id' => [$id, '='], ]); // Get the userdata related to last posts - foreach($topics as $key => $topic) { - + foreach ($topics as $key => $topic) { // Get the reply count $topics[$key]['reply_count'] = Database::count('posts', [ - 'topic_id' => [$topic['topic_id'], '='] + 'topic_id' => [$topic['topic_id'], '='], ])[0]; // Get first post in topics $firstPost = Database::fetch('posts', false, [ - 'topic_id' => [$topic['topic_id'], '='] + 'topic_id' => [$topic['topic_id'], '='], ]); $topics[$key]['first_poster'] = [ 'post' => $firstPost, 'user' => ($_FIRST_POSTER = Users::getUser($firstPost['poster_id'])), 'rank' => Users::getRank($_FIRST_POSTER['rank_main']), - 'elap' => Main::timeElapsed($firstPost['post_time']) + 'elap' => Main::timeElapsed($firstPost['post_time']), ]; // Get last post in topics $lastPost = Database::fetch('posts', false, [ - 'topic_id' => [$topic['topic_id'], '='] + 'topic_id' => [$topic['topic_id'], '='], ], ['post_id', true]); $topics[$key]['last_poster'] = [ 'post' => $lastPost, 'user' => ($_LAST_POSTER = Users::getUser($lastPost['poster_id'])), 'rank' => Users::getRank($_LAST_POSTER['rank_main']), - 'elap' => Main::timeElapsed($lastPost['post_time']) + 'elap' => Main::timeElapsed($lastPost['post_time']), ]; - } return $topics; @@ -188,38 +179,38 @@ class Forum { } // Get posts of a thread - public static function getTopic($id, $ignoreView = false) { + public static function getTopic($id, $ignoreView = false) + { // Get the topic data from the database $topicInfo = Database::fetch('topics', false, [ - 'topic_id' => [$id, '='] + 'topic_id' => [$id, '='], ]); // Check if there actually is anything - if(empty($topicInfo)) + if (empty($topicInfo)) { return false; + } // Up the view count - if(!$ignoreView) { - + if (!$ignoreView) { // Get the new count $topicInfo['topic_views'] = $topicInfo['topic_views'] + 1; // Update the count Database::update('topics', [ [ - 'topic_views' => $topicInfo['topic_views'] + 'topic_views' => $topicInfo['topic_views'], ], [ - 'topic_id' => [$id, '='] - ] + 'topic_id' => [$id, '='], + ], ]); - } // Get the posts from the database $rawPosts = Database::fetch('posts', true, [ - 'topic_id' => [$id, '='] + 'topic_id' => [$id, '='], ]); // Create storage array @@ -233,7 +224,7 @@ class Forum { // Get first post in topics $firstPost = Database::fetch('posts', false, [ - 'topic_id' => [$topic['topic']['topic_id'], '='] + 'topic_id' => [$topic['topic']['topic_id'], '='], ]); // Get the data of the first poster @@ -241,12 +232,12 @@ class Forum { 'post' => $firstPost, 'user' => ($_FIRST_POSTER = Users::getUser($firstPost['poster_id'])), 'rank' => Users::getRank($_FIRST_POSTER['rank_main']), - 'elap' => Main::timeElapsed($firstPost['post_time']) + 'elap' => Main::timeElapsed($firstPost['post_time']), ]; // Get last post in topics $lastPost = Database::fetch('posts', false, [ - 'topic_id' => [$topic['topic']['topic_id'], '='] + 'topic_id' => [$topic['topic']['topic_id'], '='], ], ['post_id', true]); // Get the data of the last poster @@ -254,32 +245,30 @@ class Forum { 'post' => $lastPost, 'user' => ($_LAST_POSTER = Users::getUser($lastPost['poster_id'])), 'rank' => Users::getRank($_LAST_POSTER['rank_main']), - 'elap' => Main::timeElapsed($lastPost['post_time']) + 'elap' => Main::timeElapsed($lastPost['post_time']), ]; // Create space for posts $topic['posts'] = []; // Parse the data of every post - foreach($rawPosts as $post) { - + foreach ($rawPosts as $post) { // Add post and metadata to the global storage array $topic['posts'][$post['post_id']] = array_merge($post, [ - 'is_op' => ($post['poster_id'] == $firstPost['poster_id'] ? '1' : '0'), - 'user' => ($_POSTER = Users::getUser($post['poster_id'])), - 'rank' => Users::getRank($_POSTER['rank_main']), - 'time_elapsed' => Main::timeElapsed($post['post_time']), - 'country' => Main::getCountryName($_POSTER['country']), - 'is_premium' => Users::checkUserPremium($_POSTER['id'])[0], - 'is_online' => Users::checkUserOnline($_POSTER['id']), - 'is_friend' => Users::checkFriend($_POSTER['id']), - 'parsed_post' => self::parseMarkUp($post['post_text'], $post['parse_mode'], $post['enable_emotes']), - 'signature' => empty($_POSTER['userData']['signature']) ? '' : self::parseMarkUp($_POSTER['userData']['signature']['text'], $_POSTER['userData']['signature']['mode']) + 'is_op' => ($post['poster_id'] == $firstPost['poster_id'] ? '1' : '0'), + 'user' => ($_POSTER = Users::getUser($post['poster_id'])), + 'rank' => Users::getRank($_POSTER['rank_main']), + 'time_elapsed' => Main::timeElapsed($post['post_time']), + 'country' => Main::getCountryName($_POSTER['country']), + 'is_premium' => Users::checkUserPremium($_POSTER['id'])[0], + 'is_online' => Users::checkUserOnline($_POSTER['id']), + 'is_friend' => Users::checkFriend($_POSTER['id']), + 'parsed_post' => self::parseMarkUp($post['post_text'], $post['parse_mode'], $post['enable_emotes']), + 'signature' => empty($_POSTER['userData']['signature']) ? '' : self::parseMarkUp($_POSTER['userData']['signature']['text'], $_POSTER['userData']['signature']['mode']), ]); // Just in case unset($_POSTER); - } // Return the compiled topic data @@ -288,16 +277,18 @@ class Forum { } // Get a topic ID from a post ID - public static function getTopicIdFromPostId($id) { + public static function getTopicIdFromPostId($id) + { // Get the post $post = Database::fetch('posts', false, [ - 'post_id' => [$id, '='] + 'post_id' => [$id, '='], ]); // Return false if nothing was returned - if(empty($post)) + if (empty($post)) { return false; + } // Return the topic id return $post['topic_id']; @@ -305,68 +296,67 @@ class Forum { } // Parse different markup flavours - public static function parseMarkUp($text, $mode, $emotes = 1) { + public static function parseMarkUp($text, $mode, $emotes = 1) + { // Clean string $text = Main::cleanString($text); // Parse emotes - if($emotes) + if ($emotes) { $text = Main::parseEmotes($text); + } // Switch between modes - switch($mode) { - + switch ($mode) { case 1: return Main::bbParse($text); - + case 2: return Main::mdParse($text); case 0: default: return $text; - } } // Get forum statistics of a user - public static function getUserStats($uid) { + public static function getUserStats($uid) + { // Collect the stats return [ - 'posts' => Database::count('posts', ['poster_id' => [$uid, '=']])[0], - 'topics' => count(Database::fetch('posts', true, ['poster_id' => [$uid, '=']], ['post_time'], null, ['topic_id'])) + 'posts' => Database::count('posts', ['poster_id' => [$uid, '=']])[0], + 'topics' => count(Database::fetch('posts', true, ['poster_id' => [$uid, '=']], ['post_time'], null, ['topic_id'])), ]; } // Creating a new post - public static function createPost($subject, $text, $enableMD, $enableSig, $forum, $type = 0, $status = 0, $topic = 0) { + public static function createPost($subject, $text, $enableMD, $enableSig, $forum, $type = 0, $status = 0, $topic = 0) + { // Check if this post is OP - if(!$topic) { - + if (!$topic) { // If so create a new topic Database::insert('topics', [ - 'forum_id' => $forum, - 'topic_hidden' => 0, - 'topic_title' => $subject, - 'topic_time' => time(), - 'topic_time_limit' => 0, - 'topic_last_reply' => 0, - 'topic_views' => 0, - 'topic_replies' => 0, - 'topic_status' => $status, - 'topic_status_change' => 0, - 'topic_type' => $type, - 'topic_first_post_id' => 0, - 'topic_first_poster_id' => Session::$userId + 'forum_id' => $forum, + 'topic_hidden' => 0, + 'topic_title' => $subject, + 'topic_time' => time(), + 'topic_time_limit' => 0, + 'topic_last_reply' => 0, + 'topic_views' => 0, + 'topic_replies' => 0, + 'topic_status' => $status, + 'topic_status_change' => 0, + 'topic_type' => $type, + 'topic_first_post_id' => 0, + 'topic_first_poster_id' => Session::$userId, ]); - } } - } diff --git a/_sakura/components/Hashing.php b/_sakura/components/Hashing.php index 2b46bd7..a6fbfdb 100644 --- a/_sakura/components/Hashing.php +++ b/_sakura/components/Hashing.php @@ -31,23 +31,24 @@ namespace Sakura; -class Hashing { - +class Hashing +{ // These variables can be changed without break the existing hashes - private static $_PBKDF2_HASH_ALGORITHM = 'sha256'; - private static $_PBKDF2_ITERATIONS = 1000; - private static $_PBKDF2_SALT_BYTES = 24; - private static $_PBKDF2_HASH_BYTES = 24; + private static $_PBKDF2_HASH_ALGORITHM = 'sha256'; + private static $_PBKDF2_ITERATIONS = 1000; + private static $_PBKDF2_SALT_BYTES = 24; + private static $_PBKDF2_HASH_BYTES = 24; // Changing these will break them though - private static $_HASH_ALGORITHM_INDEX = 0; - private static $_HASH_ITERATION_INDEX = 1; - private static $_HASH_SALT_INDEX = 2; - private static $_HASH_PBKDF2_INDEX = 3; - private static $_HASH_SECTIONS = 4; + private static $_HASH_ALGORITHM_INDEX = 0; + private static $_HASH_ITERATION_INDEX = 1; + private static $_HASH_SALT_INDEX = 2; + private static $_HASH_PBKDF2_INDEX = 3; + private static $_HASH_SECTIONS = 4; // Returns an array formatted like: [algorithm, iterations, salt, hash] - public static function create_hash($pass) { + public static function create_hash($pass) + { $salt = base64_encode( \mcrypt_create_iv( @@ -71,7 +72,7 @@ class Hashing { self::$_PBKDF2_HASH_ALGORITHM, self::$_PBKDF2_ITERATIONS, $salt, - $hash + $hash, ); return $passwordData; @@ -79,10 +80,12 @@ class Hashing { } // Validates hashed password - public static function validate_password($password, $params) { + public static function validate_password($password, $params) + { - if(count($params) < self::$_HASH_SECTIONS) + if (count($params) < self::$_HASH_SECTIONS) { return false; + } $pbkdf2 = base64_decode($params[self::$_HASH_PBKDF2_INDEX]); @@ -92,7 +95,7 @@ class Hashing { $params[self::$_HASH_ALGORITHM_INDEX], $password, $params[self::$_HASH_SALT_INDEX], - (int)$params[self::$_HASH_ITERATION_INDEX], + (int) $params[self::$_HASH_ITERATION_INDEX], strlen($pbkdf2), true ) @@ -103,12 +106,14 @@ class Hashing { } // Compares two strings $a and $b in length-constant time. - public static function slow_equals($a, $b) { + public static function slow_equals($a, $b) + { $diff = strlen($a) ^ strlen($b); - for($i = 0; $i < strlen($a) && $i < strlen($b); $i++) + for ($i = 0; $i < strlen($a) && $i < strlen($b); $i++) { $diff |= ord($a[$i]) ^ ord($b[$i]); + } return $diff === 0; @@ -130,24 +135,26 @@ class Hashing { * With improvements by http://www.variations-of-shadow.com */ - private static function pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output = false) { + private static function pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output = false) + { $algorithm = strtolower($algorithm); - if(!in_array($algorithm, hash_algos(), true)) + if (!in_array($algorithm, hash_algos(), true)) { trigger_error('PBKDF2 ERROR: Invalid hash algorithm.', E_USER_ERROR); + } - if($count <= 0 || $key_length <= 0) + if ($count <= 0 || $key_length <= 0) { trigger_error('PBKDF2 ERROR: Invalid parameters.', E_USER_ERROR); + } - if(function_exists('hash_pbkdf2')) { - + if (function_exists('hash_pbkdf2')) { // The output length is in NIBBLES (4-bits) if $raw_output is false! - if(!$raw_output) + if (!$raw_output) { $key_length = $key_length * 2; + } return hash_pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output); - } $hash_length = strlen(hash($algorithm, '', true)); @@ -155,8 +162,7 @@ class Hashing { $output = ''; - for($i = 1; $i <= $block_count; $i++) { - + for ($i = 1; $i <= $block_count; $i++) { // $i encoded as 4 bytes, big endian. $last = $salt . pack('N', $i); @@ -164,18 +170,18 @@ class Hashing { $last = $xorsum = hash_hmac($algorithm, $last, $password, true); // Perform the other $count - 1 interations - for($j = 1; $j < $count; $j++) + for ($j = 1; $j < $count; $j++) { $xorsum ^= ($last = hash_hmac($algorithm, $last, $password, true)); + } $output .= $xorsum; - if($raw_output) + if ($raw_output) { return substr($output, 0, $key_length); - else - return bin2hex(substr($output, 0, $key_length)); + } + return bin2hex(substr($output, 0, $key_length)); } } - } diff --git a/_sakura/components/Main.php b/_sakura/components/Main.php index 9af6b63..8028aea 100644 --- a/_sakura/components/Main.php +++ b/_sakura/components/Main.php @@ -8,10 +8,11 @@ namespace Sakura; use Parsedown; use PHPMailer; -class Main { - +class Main +{ // Constructor - public static function init($config) { + public static function init($config) + { // Configuration Management and local configuration Configuration::init($config); @@ -28,32 +29,35 @@ class Main { } // Parse markdown - public static function mdParse($text) { + public static function mdParse($text) + { return (new Parsedown())->text($text); } // Get bbcodes - public static function getBBcodes() { + public static function getBBcodes() + { return Database::fetch('bbcodes'); } // Parse bbcodes - public static function bbParse($text) { + public static function bbParse($text) + { // Get bbcode regex from the database $bbcodes = Database::fetch('bbcodes'); // Split the regex - $regex = array_map(function($arr) { + $regex = array_map(function ($arr) { return $arr['regex']; }, $bbcodes); // Split the replacement - $replace = array_map(function($arr) { + $replace = array_map(function ($arr) { return $arr['replace']; }, $bbcodes); @@ -66,23 +70,23 @@ class Main { } // Get emoticons - public static function getEmotes() { + public static function getEmotes() + { return Database::fetch('emoticons'); } // Parsing emoticons - public static function parseEmotes($text) { + public static function parseEmotes($text) + { // Get emoticons from the database $emotes = Database::fetch('emoticons'); // Do the replacements - foreach($emotes as $emote) { - - $text = str_replace($emote['emote_string'], '', $text); - + foreach ($emotes as $emote) { + $text = str_replace($emote['emote_string'], '', $text); } // Return the parsed text @@ -91,16 +95,15 @@ class Main { } // Verify ReCAPTCHA - public static function verifyCaptcha($response) { + public static function verifyCaptcha($response) + { // Attempt to get the response - $resp = @file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='. Configuration::getConfig('recaptcha_private') .'&response='. $response); + $resp = @file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=' . Configuration::getConfig('recaptcha_private') . '&response=' . $response); // In the highly unlikely case that it failed to get anything forge a false - if(!$resp) { - + if (!$resp) { return false; - } // Decode the response JSON from the servers @@ -112,49 +115,44 @@ class Main { } // Error Handler - public static function errorHandler($errno, $errstr, $errfile, $errline) { + public static function errorHandler($errno, $errstr, $errfile, $errline) + { // Remove ROOT path from the error string and file location - $errstr = str_replace(ROOT, '', $errstr); - $errfile = str_replace(ROOT, '', $errfile); + $errstr = str_replace(ROOT, '', $errstr); + $errfile = str_replace(ROOT, '', $errfile); // Attempt to log the error to the database - if(Database::$_DATABASE !== null) { - + if (Database::$database !== null) { // Encode backtrace data $backtrace = base64_encode(json_encode(debug_backtrace())); // Check if this error has already been logged in the past - if($past = Database::fetch('error_log', false, ['backtrace' => [$backtrace, '=', true], 'error_string' => [$errstr, '=']])) { - + if ($past = Database::fetch('error_log', false, ['backtrace' => [$backtrace, '=', true], 'error_string' => [$errstr, '=']])) { // If so assign the errid $errid = $past['id']; - } else { - // Create an error ID $errid = substr(md5(microtime()), rand(0, 22), 10); // Log the error Database::insert('error_log', [ - 'id' => $errid, - 'timestamp' => date("r"), - 'revision' => SAKURA_VERSION, - 'error_type' => $errno, - 'error_line' => $errline, - 'error_string' => $errstr, - 'error_file' => $errfile, - 'backtrace' => $backtrace + 'id' => $errid, + 'timestamp' => date("r"), + 'revision' => SAKURA_VERSION, + 'error_type' => $errno, + 'error_line' => $errline, + 'error_string' => $errstr, + 'error_file' => $errfile, + 'backtrace' => $backtrace, ]); - } } switch ($errno) { - case E_ERROR: case E_USER_ERROR: $error = 'FATAL ERROR: ' . $errstr . ' on line ' . $errline . ' in ' . $errfile; @@ -172,7 +170,6 @@ class Main { default: $error = 'Unknown error type [' . $errno . ']: ' . $errstr . ' on line ' . $errline . ' in ' . $errfile; - } // Truncate all previous outputs @@ -205,41 +202,33 @@ class Main {
To prevent potential security risks or data loss Sakura has stopped execution of the script.
'; -if(isset($errid)) { + if (isset($errid)) { + $errorPage .= 'The error and surrounding data has been logged.
+' . $errid . ''; + } else { + $errorPage .= '
Sakura was not able to log this error which could mean that there was an error with the database connection. If you\'re the system administrator check the database credentials and make sure the server is running and if you\'re not please let the system administrator know about this error if it occurs again.
'; + } - $errorPage .= 'The error and surrounding data has been logged.
-'. $errid .''; - -} else { - - $errorPage .= '
Sakura was not able to log this error which could mean that there was an error with the database connection. If you\'re the system administrator check the database credentials and make sure the server is running and if you\'re not please let the system administrator know about this error if it occurs again.
'; - -} - -if(!SAKURA_STABLE) { - $errorPage .= ''. $error .'+ if (!SAKURA_STABLE) { + $errorPage .= '
' . $error . '
'; - $errorPage .= ''; - - } - -} - -$errorPage .= '#'. $num .'
'; + foreach ($trace as $key => $val) { + $errorPage .= str_pad('[' . $key . ']', 12) . '=> ' . (is_array($val) || is_object($val) ? json_encode($val) : $val) . "\r\n"; + } - foreach($trace as $key => $val) { - - $errorPage .= str_pad('['. $key .']', 12) .'=> '. (is_array($val) || is_object($val) ? json_encode($val) : $val) ."\r\n"; + $errorPage .= ''; + } } - $errorPage .= '