diff --git a/_developer_data/structure.sql b/_developer_data/structure.sql index b326af8..8376ceb 100644 --- a/_developer_data/structure.sql +++ b/_developer_data/structure.sql @@ -16,7 +16,7 @@ CREATE TABLE `sakura_actioncodes` ( `actkey` varchar(255) COLLATE utf8_bin NOT NULL COMMENT 'The URL key for using this code.', `instruction` varchar(255) COLLATE utf8_bin NOT NULL COMMENT 'Things the backend should do upon using this code', PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; DROP TABLE IF EXISTS `sakura_apikeys`; @@ -52,7 +52,7 @@ CREATE TABLE `sakura_bbcodes` ( `replace` varchar(255) COLLATE utf8_bin NOT NULL COMMENT 'What to replace it with.', `description` varchar(512) COLLATE utf8_bin NOT NULL COMMENT 'Description of what this does.', PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; DROP TABLE IF EXISTS `sakura_config`; @@ -69,7 +69,7 @@ CREATE TABLE `sakura_faq` ( `question` varchar(255) COLLATE utf8_bin NOT NULL COMMENT 'The question.', `answer` text COLLATE utf8_bin NOT NULL COMMENT 'The answer.', PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; DROP TABLE IF EXISTS `sakura_forums`; @@ -86,7 +86,7 @@ CREATE TABLE `sakura_forums` ( `forum_last_poster_id` bigint(255) unsigned NOT NULL DEFAULT '0' COMMENT 'ID of last poster in forum.', `forum_icon` varchar(255) COLLATE utf8_bin NOT NULL COMMENT 'Display icon for the forum.', PRIMARY KEY (`forum_id`) -) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; DROP TABLE IF EXISTS `sakura_friends`; @@ -118,7 +118,7 @@ CREATE TABLE `sakura_logs` ( PRIMARY KEY (`id`), KEY `uid` (`uid`), CONSTRAINT `sakura_logs_ibfk_1` FOREIGN KEY (`uid`) REFERENCES `sakura_users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE -) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; DROP TABLE IF EXISTS `sakura_logtypes`; @@ -139,7 +139,7 @@ CREATE TABLE `sakura_messages` ( `subject` varchar(255) COLLATE utf8_bin NOT NULL COMMENT 'Title of the message', `content` text COLLATE utf8_bin NOT NULL COMMENT 'Contents of the message.', PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; DROP TABLE IF EXISTS `sakura_news`; @@ -150,7 +150,7 @@ CREATE TABLE `sakura_news` ( `title` varchar(255) COLLATE utf8_bin NOT NULL COMMENT 'Title of the post.', `content` text COLLATE utf8_bin NOT NULL COMMENT 'Contents of the post', PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; DROP TABLE IF EXISTS `sakura_notifications`; @@ -168,7 +168,7 @@ CREATE TABLE `sakura_notifications` ( PRIMARY KEY (`id`), KEY `uid` (`uid`), CONSTRAINT `sakura_notifications_ibfk_1` FOREIGN KEY (`uid`) REFERENCES `sakura_users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE -) ENGINE=InnoDB AUTO_INCREMENT=65 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; DROP TABLE IF EXISTS `sakura_permissions`; @@ -200,18 +200,21 @@ CREATE TABLE `sakura_posts` ( PRIMARY KEY (`post_id`), KEY `topic_id` (`topic_id`), KEY `forum_id` (`forum_id`), - CONSTRAINT `sakura_posts_ibfk_2` FOREIGN KEY (`forum_id`) REFERENCES `sakura_forums` (`forum_id`) ON DELETE CASCADE ON UPDATE CASCADE, - CONSTRAINT `sakura_posts_ibfk_1` FOREIGN KEY (`topic_id`) REFERENCES `sakura_topics` (`topic_id`) ON DELETE CASCADE ON UPDATE CASCADE -) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; + KEY `poster_id` (`poster_id`), + CONSTRAINT `sakura_posts_ibfk_3` FOREIGN KEY (`poster_id`) REFERENCES `sakura_users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT `sakura_posts_ibfk_1` FOREIGN KEY (`topic_id`) REFERENCES `sakura_topics` (`topic_id`) ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT `sakura_posts_ibfk_2` FOREIGN KEY (`forum_id`) REFERENCES `sakura_forums` (`forum_id`) ON DELETE CASCADE ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; DROP TABLE IF EXISTS `sakura_premium`; CREATE TABLE `sakura_premium` ( `id` bigint(255) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Automatically generated ID by MySQL for management.', - `expiredate` int(16) unsigned NOT NULL COMMENT 'Expiration timestamp.', `uid` bigint(255) unsigned NOT NULL COMMENT 'ID of the user that purchased Tenshi.', + `startdate` int(11) unsigned NOT NULL COMMENT 'Timestamp of first purchase.', + `expiredate` int(11) unsigned NOT NULL COMMENT 'Expiration timestamp.', PRIMARY KEY (`id`), - KEY `uid` (`uid`), + UNIQUE KEY `uid` (`uid`), CONSTRAINT `sakura_premium_ibfk_1` FOREIGN KEY (`uid`) REFERENCES `sakura_users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; @@ -226,7 +229,7 @@ CREATE TABLE `sakura_profilefields` ( `description` varchar(255) COLLATE utf8_bin NOT NULL COMMENT 'Description of the field displayed in the control panel.', `additional` varchar(255) COLLATE utf8_bin NOT NULL COMMENT 'Undocumented JSON array containing special options if needed (probably only going to be used for the YouTube field).', PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; DROP TABLE IF EXISTS `sakura_ranks`; @@ -238,7 +241,7 @@ CREATE TABLE `sakura_ranks` ( `description` text COLLATE utf8_bin NOT NULL COMMENT 'A description of what a user of this rank can do/is supposed to do.', `title` varchar(255) COLLATE utf8_bin NOT NULL COMMENT 'Default user title if user has none set.', PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; DROP TABLE IF EXISTS `sakura_regcodes`; @@ -251,7 +254,7 @@ CREATE TABLE `sakura_regcodes` ( PRIMARY KEY (`id`), KEY `created_by` (`created_by`), CONSTRAINT `sakura_regcodes_ibfk_1` FOREIGN KEY (`created_by`) REFERENCES `sakura_users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE -) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; DROP TABLE IF EXISTS `sakura_reports`; @@ -280,7 +283,7 @@ CREATE TABLE `sakura_sessions` ( PRIMARY KEY (`id`), KEY `userid` (`userid`), CONSTRAINT `sakura_sessions_ibfk_1` FOREIGN KEY (`userid`) REFERENCES `sakura_users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE -) ENGINE=InnoDB AUTO_INCREMENT=103 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; DROP TABLE IF EXISTS `sakura_sock_perms`; @@ -305,14 +308,16 @@ CREATE TABLE `sakura_topics` ( `topic_status` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT 'Status of topic.', `topic_status_change` int(11) unsigned NOT NULL DEFAULT '0' COMMENT 'Date the topic status was changed (used for deletion cooldown as well).', `topic_type` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT 'Type of the topic.', - `topic_first_post_id` bigint(255) unsigned NOT NULL DEFAULT '0' COMMENT 'ID of first post made in this topic.', - `topic_first_poster_id` bigint(255) unsigned NOT NULL DEFAULT '0' COMMENT 'User ID of person who made the first post.', - `topic_last_post_id` bigint(255) unsigned NOT NULL DEFAULT '0' COMMENT 'ID of last post made in this topic.', - `topic_last_poster_id` bigint(255) unsigned NOT NULL DEFAULT '0' COMMENT 'User ID of person who made the last post.', + `topic_first_post_id` bigint(255) unsigned DEFAULT NULL COMMENT 'ID of first post made in this topic.', + `topic_first_poster_id` bigint(255) unsigned DEFAULT NULL COMMENT 'User ID of person who made the first post.', + `topic_last_post_id` bigint(255) unsigned DEFAULT NULL COMMENT 'ID of last post made in this topic.', + `topic_last_poster_id` bigint(255) unsigned DEFAULT NULL COMMENT 'User ID of person who made the last post.', PRIMARY KEY (`topic_id`), KEY `forum_id` (`forum_id`), + KEY `topic_first_poster_id` (`topic_first_poster_id`), + KEY `topic_last_poster_id` (`topic_last_poster_id`), CONSTRAINT `sakura_topics_ibfk_1` FOREIGN KEY (`forum_id`) REFERENCES `sakura_forums` (`forum_id`) ON DELETE CASCADE ON UPDATE CASCADE -) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; DROP TABLE IF EXISTS `sakura_users`; @@ -341,7 +346,7 @@ CREATE TABLE `sakura_users` ( `userData` varchar(512) COLLATE utf8_bin NOT NULL DEFAULT '[]' COMMENT 'All additional profile data.', PRIMARY KEY (`id`), UNIQUE KEY `username_clean` (`username_clean`) -) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; DROP TABLE IF EXISTS `sakura_warnings`; @@ -357,7 +362,7 @@ CREATE TABLE `sakura_warnings` ( KEY `iid` (`iid`), CONSTRAINT `sakura_warnings_ibfk_2` FOREIGN KEY (`iid`) REFERENCES `sakura_users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `sakura_warnings_ibfk_1` FOREIGN KEY (`uid`) REFERENCES `sakura_users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE -) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; DROP TABLE IF EXISTS `sock_banned_users`; @@ -379,7 +384,7 @@ CREATE TABLE `sock_channels` ( `priv` int(11) DEFAULT '0', PRIMARY KEY (`id`), UNIQUE KEY `name` (`chname`) -) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1; +) ENGINE=InnoDB DEFAULT CHARSET=latin1; DROP TABLE IF EXISTS `sock_logs`; @@ -394,7 +399,7 @@ CREATE TABLE `sock_logs` ( `message` longtext NOT NULL, `flags` varchar(10) NOT NULL DEFAULT '10010', PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=49 DEFAULT CHARSET=latin1; +) ENGINE=InnoDB DEFAULT CHARSET=latin1; DROP TABLE IF EXISTS `sock_online_users`; @@ -407,4 +412,4 @@ CREATE TABLE `sock_online_users` ( ) ENGINE=InnoDB DEFAULT CHARSET=latin1; --- 2015-07-01 00:56:09 +-- 2015-07-01 14:27:30 diff --git a/_sakura/changelog.json b/_sakura/changelog.json index 420a284..ed3417d 100644 --- a/_sakura/changelog.json +++ b/_sakura/changelog.json @@ -1385,6 +1385,10 @@ { "type": "FIX", "change": "Fixed a multitude of fuckups." + }, + { + "type": "ADD", + "change": "Added automatic rank management for premium." } ] diff --git a/_sakura/components/Forum.php b/_sakura/components/Forum.php index 4c1828a..90a351e 100644 --- a/_sakura/components/Forum.php +++ b/_sakura/components/Forum.php @@ -214,7 +214,7 @@ class Forum { 'user' => ($_POSTER = Users::getUser($post['poster_id'])), 'rank' => Users::getRank($_POSTER['rank_main']), 'country' => Main::getCountryName($_POSTER['country']), - 'is_tenshi' => Users::checkUserTenshi($_POSTER['id']), + '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']), diff --git a/_sakura/components/Users.php b/_sakura/components/Users.php index 6bdb005..0a33f33 100644 --- a/_sakura/components/Users.php +++ b/_sakura/components/Users.php @@ -89,6 +89,9 @@ class Users { ] ]); + // Update the premium meta + Users::updatePremiumMeta($uid); + // Redirect people that need to change their password to the new format if(self::getUser($uid)['password_algo'] == 'legacy' && $_SERVER['PHP_SELF'] != '/authenticate.php' && $_SERVER['PHP_SELF'] != '/imageserve.php') header('Location: /authenticate.php?legacy=true'); @@ -130,7 +133,7 @@ class Users { $user['password_salt'], $user['password_hash'] ])) - return [0, 'INCORRECT_PASSWORD']; + return [0, 'INCORRECT_PASSWORD', $user['password_chan']]; } @@ -652,6 +655,106 @@ class Users { } + // Set the default rank of a user + public static function setDefaultRank($uid, $rid, $userIdIsUserData = false) { + + // Get the specified user + $user = $userIdIsUserData ? $uid : self::getUser($uid); + + // Decode the json + $ranks = json_decode($user['ranks'], true); + + // Check if the rank we're trying to set is actually there + if(!in_array($rid, $ranks)) + return false; + + // Update the row + Database::update('users', [ + [ + 'rank_main' => $rid + ], + [ + 'id' => [$uid, '='] + ] + ]); + + // Return true if everything was successful + return true; + + } + + // Add a rank to a user + public static function addRanksToUser($ranks, $uid, $userIdIsUserData = false) { + + // Get the specified user + $user = $userIdIsUserData ? $uid : self::getUser($uid); + + // Decode the array + $current = json_decode($user['ranks'], true); + + // Go over all the new ranks + foreach($ranks as $rank) { + + // Check if the user already has this rank and set it if not + if(!in_array($rank, $current)) + $current[] = (int)$rank; + + } + + // Encode the array + $current = json_encode($current); + + // Update the row + Database::update('users', [ + [ + 'ranks' => $current + ], + [ + 'id' => [$uid, '='] + ] + ]); + + // Return true because + return true; + + } + + // Removing ranks from a user + public static function removeRanksFromUser($ranks, $uid, $userIdIsUserData = false) { + + // Get the specified user + $user = $userIdIsUserData ? $uid : self::getUser($uid); + + // Get the ranks + $current = json_decode($user['ranks'], true); + + // Check the current ranks for ranks in the set array + foreach($current as $key => $rank) { + + // Unset the rank + if(in_array($rank, $ranks)) + unset($current[$key]); + + } + + // Encode the array + $current = json_encode($current); + + // Update the row + Database::update('users', [ + [ + 'ranks' => $current + ], + [ + 'id' => [$uid, '='] + ] + ]); + + // Return true + return true; + + } + // Check if a user has these ranks public static function checkIfUserHasRanks($ranks, $userid, $userIdIsUserData = false) { @@ -851,12 +954,105 @@ class Users { } - // Add premium to + // Add premium to a user + public static function addUserPremium($id, $seconds) { - // Check if user has Premium [ REWRITE THIS ] + // Check if there's already a record of premium for this user in the database + $getUser = Database::fetch('premium', false, [ + 'uid' => [$id, '='] + ]); + + // Calculate the (new) start and expiration timestamp + $start = isset($getUser['startdate']) ? $getUser['startdate'] : time(); + $expire = isset($getUser['expiredate']) ? $getUser['expiredate'] + $seconds : time() + $seconds; + + // If the user already exists do an update call, otherwise an insert call + if(empty($getUser)) { + + Database::insert('premium', [ + 'uid' => $id, + 'startdate' => $start, + 'expiredate' => $expire + ]); + + } else { + + Database::update('premium', [ + [ + 'expiredate' => $expire + ], + [ + 'uid' => [$id, '='] + ] + ]); + + } + + // Return the expiration timestamp + return $expire; + + } + + // Remove the premium status of a user + public static function removeUserPremium($id) { + + Database::delete('premium', [ + 'uid' => [$id, '='] + ]); + + } + + // Check if user has Premium public static function checkUserPremium($id) { - return false; + // Attempt to retrieve the premium record from the database + $getRecord = Database::fetch('premium', false, [ + 'uid' => [$id, '='] + ]); + + // If nothing was returned just return false + if(empty($getRecord)) + return [0]; + + // Check if the Tenshi hasn't expired + if($getRecord['expiredate'] < time()) { + + self::removeUserPremium($id); + self::updatePremiumMeta($id); + return [0, $getRecord['startdate'], $getRecord['expiredate']]; + + } + + // Else return the start and expiration date + return [1, $getRecord['startdate'], $getRecord['expiredate']]; + + } + + // Update the premium data + public static function updatePremiumMeta($id) { + + // Get the ID for the premium user rank from the database + $premiumRank = Configuration::getConfig('premium_rank_id'); + + // Run the check + $check = self::checkUserPremium($id); + + // Check if the user has premium + if($check[0] == 1) { + + // If so add the rank to them + self::addRanksToUser([$premiumRank], $id); + + // Check if the user's default rank is standard user and update it to premium + if(self::getUser($id)['rank_main'] == 2) + self::setDefaultRank($id, $premiumRank); + + } elseif($check[0] == 0 && count($check) > 1) { + + // Else remove the rank from them + self::removeRanksFromUser([$premiumRank], $id); + + } } diff --git a/_sakura/templates/yuuno/errors/premiumComplete.tpl b/_sakura/templates/yuuno/errors/premiumComplete.tpl index de1bfeb..4307066 100644 --- a/_sakura/templates/yuuno/errors/premiumComplete.tpl +++ b/_sakura/templates/yuuno/errors/premiumComplete.tpl @@ -2,5 +2,6 @@