/**
  * Create new password and send it to user
  * 
  * @param String $email
  */
 public function resetPassword($email)
 {
     global $db;
     $email = trim($email);
     if (!$email) {
         buckys_redirect('/register.php?forgotpwd=1', MSG_EMPTY_EMAIL, MSG_TYPE_ERROR);
         return;
     }
     //Check Email Address
     if (!preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\\._-]+)+\$/", $email)) {
         buckys_redirect('/register.php?forgotpwd=1', MSG_INVALID_EMAIL, MSG_TYPE_ERROR);
         return false;
     }
     $query = $db->prepare("SELECT userID FROM " . TABLE_USERS . " WHERE email=%s", $email);
     $userID = $db->getVar($query);
     if (!$userID) {
         buckys_redirect('/register.php?forgotpwd=1', MSG_EMAIL_NOT_FOUND, MSG_TYPE_ERROR);
         return false;
     }
     $data = BuckysUser::getUserData($userID);
     //Remove Old Token
     BuckysUsersToken::removeUserToken($userID, 'password');
     //Create New Token
     $token = BuckysUsersToken::createNewToken($userID, 'password');
     $link = "http://" . $_SERVER['HTTP_HOST'] . "/reset_password.php?token=" . $token;
     //Send an email to user with the link
     $title = "Reset your password.";
     $body = "Dear " . $data['firstName'] . " " . $data['lastName'] . "\n\n" . "Please reset your password by using the below link:\n" . $link . "\n\nBuckysroom.com";
     require_once DIR_FS_INCLUDES . "phpMailer/class.phpmailer.php";
     buckys_sendmail($data['email'], $data['firstName'] . " " . $data['lastName'], $title, $body);
     buckys_redirect('/register.php', MSG_RESET_PASSWORD_EMAIL_SENT, MSG_TYPE_SUCCESS);
     return;
 }
 /**
  * Remove Account
  */
 public static function deleteUserAccount($userID)
 {
     global $db;
     $userID = intval($userID);
     //Fix Comments Count
     $query = $db->prepare("SELECT count(commentID) AS c, postID FROM " . TABLE_POSTS_COMMENTS . " WHERE commenter=%d AND commentStatus=1 GROUP BY postID", $userID);
     $pcRows = $db->getResultsArray($query);
     foreach ($pcRows as $row) {
         $db->query("UPDATE " . TABLE_POSTS . " SET `comments` = `comments` - " . $row['c'] . " WHERE postID=" . $row['postID']);
     }
     //Fix Likes Count
     $query = $db->prepare("SELECT count(likeID) AS c, postID FROM " . TABLE_POSTS_LIKES . " WHERE userID=%d AND likeStatus=1 GROUP BY postID", $userID);
     $plRows = $db->getResultsArray($query);
     foreach ($plRows as $row) {
         $db->query("UPDATE " . TABLE_POSTS . " SET `likes` = `likes` - " . $row['c'] . " WHERE postID=" . $row['postID']);
     }
     //Block Votes for Moderator
     $query = $db->prepare("SELECT count(voteID) AS c, candidateID FROM " . TABLE_MODERATOR_VOTES . " WHERE voterID=%d AND voteStatus=1 GROUP BY candidateID", $userID);
     $vRows = $db->getResultsArray($query);
     foreach ($vRows as $row) {
         $db->query("UPDATE " . TABLE_MODERATOR_CANDIDATES . " SET `votes` = `votes` - " . $row['c'] . " WHERE candidateID=" . $row['candidateID']);
     }
     //Block Replies
     $query = $db->prepare("SELECT count(r.replyID), r.topicID, t.categoryID FROM " . TABLE_FORUM_REPLIES . " AS r LEFT JOIN " . TABLE_FORUM_TOPICS . " AS t ON t.topicID=r.topicID WHERE r.status='publish' AND r.creatorID=%d GROUP BY r.topicID", $userID);
     $rRows = $db->getResultsArray($query);
     $db->query("UPDATE " . TABLE_FORUM_REPLIES . " SET `status`='suspended' WHERE creatorID=" . $userID . " AND `status`='publish'");
     foreach ($rRows as $row) {
         $db->query("UPDATE " . TABLE_FORUM_TOPICS . " SET `replies` = `replies` - " . $row['c'] . " WHERE topicID=" . $row['topicID']);
         $db->query("UPDATE " . TABLE_FORUM_CATEGORIES . " SET `replies` = `replies` - " . $row['c'] . " WHERE categoryID=" . $row['categoryID']);
         BuckysForumTopic::updateTopicLastReplyID($row['topicID']);
     }
     //Block Topics
     $query = $db->prepare("SELECT count(topicID) AS tc, SUM(replies) AS rc, categoryID FROM " . TABLE_FORUM_TOPICS . " WHERE creatorID=%d AND `status`='publish' GROUP BY categoryID", $userID);
     $tRows = $db->getResultsArray($query);
     $db->query("UPDATE " . TABLE_FORUM_TOPICS . " SET `status`='suspended' WHERE creatorID=" . $userID . " AND `status`='publish'");
     foreach ($tRows as $row) {
         $db->query("UPDATE " . TABLE_FORUM_CATEGORIES . " SET `replies` = `replies` - " . $row['rc'] . ", `topics` = `topics` - " . $row['tc'] . " WHERE categoryID=" . $row['categoryID']);
         BuckysForumCategory::updateCategoryLastTopicID($row['categoryID']);
     }
     //Block Reply Votes
     $query = $db->prepare("SELECT count(voteID) AS c, objectID FROM " . TABLE_FORUM_VOTES . " WHERE voterID=%d AND voteStatus=1 GROUP BY objectID", $userID);
     $vRows = $db->getResultsArray($query);
     foreach ($vRows as $row) {
         $db->query("UPDATE " . TABLE_FORUM_REPLIES . " SET `votes` = `votes` - " . $row['c'] . " WHERE replyID=" . $row['objectID']);
     }
     //Delete Reported Objects
     $db->query("DELETE FROM " . TABLE_REPORTS . " WHERE objectID IN (SELECT postID FROM " . TABLE_POSTS . " WHERE poster=" . $userID . ")");
     $db->query("DELETE FROM " . TABLE_REPORTS . " WHERE objectID IN (SELECT topicID FROM " . TABLE_FORUM_TOPICS . " WHERE creatorID=" . $userID . ")");
     $db->query("DELETE FROM " . TABLE_REPORTS . " WHERE objectID IN (SELECT replyID FROM " . TABLE_FORUM_REPLIES . " WHERE creatorID=" . $userID . ")");
     //Delete From banned Users
     $db->query("DELETE FROM " . TABLE_BANNED_USERS . "  WHERE bannedUserID=" . $userID);
     //Delete Activities
     $db->query("DELETE FROM " . TABLE_MAIN_ACTIVITIES . " WHERE userID=" . $userID);
     //Delete Album Photos
     $db->query("DELETE FROM " . TABLE_ALBUMS_PHOTOS . " WHERE album_id IN (SELECT albumID FROM " . TABLE_ALBUMS . " WHERE OWNER=" . $userID . ")");
     //Delete ALbums
     $db->query("DELETE FROM " . TABLE_ALBUMS . " WHERE OWNER=" . $userID);
     //Delete Friends
     $db->query("DELETE FROM " . TABLE_FRIENDS . " WHERE userID=" . $userID . " OR userFriendID=" . $userID);
     //Delete Messages
     $db->query("DELETE FROM " . TABLE_MESSAGES . " WHERE userID=" . $userID . " OR sender=" . $userID);
     //Delete Private Messengers
     $db->query("DELETE FROM " . TABLE_MESSENGER_BLOCKLIST . " WHERE userID=" . $userID . " OR blockedID=" . $userID);
     $db->query("DELETE FROM " . TABLE_MESSENGER_BUDDYLIST . " WHERE userID=" . $userID . " OR buddyID=" . $userID);
     $db->query("DELETE FROM " . TABLE_MESSENGER_MESSAGES . " WHERE userID=" . $userID . " OR buddyID=" . $userID);
     //Delete Posts
     $posts = $db->getResultsArray("SELECT * FROM " . TABLE_POSTS . " WHERE poster=" . $userID);
     foreach ($posts as $post) {
         //Delete Comments
         $db->query("DELETE FROM " . TABLE_POSTS_COMMENTS . " WHERE postID=" . $post['postID']);
         //Delete Likes
         $db->query("DELETE FROM " . TABLE_POSTS_LIKES . " WHERE postID=" . $post['postID']);
         //Delete hits
         $db->query("DELETE FROM " . TABLE_POSTS_HITS . " WHERE postID=" . $post['postID']);
     }
     $db->query("DELETE FROM " . TABLE_POSTS . " WHERE poster=" . $userID);
     //Delete Pages
     $pageIns = new BuckysPage();
     $pageIns->deletePageByUserID($userID);
     //Delete Trade Section which are related to this user.
     $tradeIns = new BuckysTradeItem();
     $tradeIns->deleteItemsByUserID($userID);
     //Delete Shop Section which are related to this user
     $shopIns = new BuckysShopProduct();
     $shopIns->deleteProductsByUserID($userID);
     //Delete Comments
     $db->query("DELETE FROM " . TABLE_POSTS_COMMENTS . " WHERE commenter=" . $userID);
     //Delete Likes
     $db->query("DELETE FROM " . TABLE_POSTS_LIKES . " WHERE userID=" . $userID);
     //Delete Page Followers
     $db->query("DELETE FROM " . TABLE_PAGE_FOLLOWERS . " WHERE userID=" . $userID);
     //Getting Removed Topics
     $topicIDs = $db->getResultsArray("SELECT topicID FROM " . TABLE_FORUM_TOPICS . " WHERE creatorID=" . $userID);
     if (!$topicIDs) {
         $topicIDs = [0];
     }
     //Delete Reply Votes
     $db->query("DELETE FROM " . TABLE_FORUM_VOTES . " WHERE voterID=" . $userID);
     $db->query("DELETE FROM " . TABLE_FORUM_VOTES . " WHERE objectID IN ( SELECT replyID FROM " . TABLE_FORUM_REPLIES . " WHERE creatorID=" . $userID . " OR topicID IN (" . implode(", ", $topicIDs) . ") )");
     //Delete Replies
     $db->query("DELETE FROM " . TABLE_FORUM_REPLIES . " WHERE creatorID=" . $userID . " OR topicID IN (" . implode(", ", $topicIDs) . ")");
     //Delete Topics
     $db->query("DELETE FROM " . TABLE_FORUM_TOPICS . " WHERE creatorID=" . $userID);
     //Delete Users
     /*$db->query("DELETE FROM " . TABLE_USERS . " WHERE userID=" . $userID);
     		$db->query("DELETE FROM " . TABLE_USERS_CONTACT . " WHERE userID=" . $userID);
     		$db->query("DELETE FROM " . TABLE_USERS_EDUCATIONS . " WHERE userID=" . $userID);
     		$db->query("DELETE FROM " . TABLE_USERS_EMPLOYMENTS . " WHERE userID=" . $userID);
     		$db->query("DELETE FROM " . TABLE_USERS_LINKS . " WHERE userID=" . $userID);
     		$db->query("DELETE FROM " . TABLE_USERS_TOKEN . " WHERE userID=" . $userID);*/
     //Don't delete user from the database, just update the user's status
     $db->query("UPDATE " . TABLE_USERS . " SET `status`=" . BuckysUser::STATUS_USER_DELETED . " WHERE userID=" . $userID);
     //Send
     $bitCoinInfo = BuckysUser::getUserBitcoinInfo($userID);
     if ($bitCoinInfo) {
         $userInfo = BuckysUser::getUserBasicInfo($userID);
         $content = "Your " . TNB_SITE_NAME . " account has been deleted. However, you may still access your Bitcoin wallet at:\n" . "https://blockchain.info/wallet/login\n" . "Identifier: " . $bitCoinInfo['bitcoin_guid'] . "\n" . "Password: "******"\n";
         //Send Email to User
         buckys_sendmail($userInfo['email'], $userInfo['firstName'] . ' ' . $userInfo['lastName'], TNB_SITE_NAME . ' Account has been Deleted', $content);
     }
 }