/**
  * Create Publisher Ad
  *
  * @param mixed $data
  * @return bool
  */
 public function savePublisherAd($userID, $data)
 {
     global $db;
     if (!$data['name']) {
         $this->last_message = MSG_INVALID_REQUEST;
         return false;
     }
     $sizeId = $data['size'];
     $classAds = new BuckysAds();
     $sizeDetail = $classAds->getAdSizeById($sizeId);
     if (!$sizeDetail) {
         $this->last_message = MSG_INVALID_REQUEST;
         return false;
     }
     $borderColor = !$data['border-color'] ? '006699' : $data['border-color'];
     $bgColor = !$data['bg-color'] ? '006699' : $data['bg-color'];
     $titleColor = !$data['title-color'] ? '006699' : $data['title-color'];
     $descriptionColor = !$data['description-color'] ? '006699' : $data['description-color'];
     $urlColor = !$data['url-color'] ? '006699' : $data['url-color'];
     $adType = $data['adType'];
     //Create token
     $token = sha1($userID . session_id() . $data['name'] . time() . buckys_generate_random_string(20));
     $insertData = ['publisherID' => $userID, 'size' => $sizeDetail['id'], 'name' => trim($data['name']), 'borderColor' => $borderColor, 'bgColor' => $bgColor, 'titleColor' => $titleColor, 'textColor' => $descriptionColor, 'urlColor' => $urlColor, 'createdDate' => date('Y-m-d H:i:s'), 'impressions' => 0, 'earnings' => 0.0, 'status' => TNB_PUBLISHER_AD_STATUS_ACTIVE, 'adType' => $adType, 'token' => $token];
     $newId = $db->insertFromArray(TABLE_PUBLISHER_ADS, $insertData);
     if (!$newId) {
         $this->last_message = $db->getLastError();
         return false;
     }
     $this->last_message = MSG_AD_NEW_AD_CREATED;
     return true;
 }
 /**
  * Create Wallet Address
  *
  * @param Int $userID
  * @return array|bool
  */
 public static function createWallet($userID, $userEmail)
 {
     global $db;
     $password = buckys_generate_random_string(10);
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, 'https://blockchain.info/api/v2/create_wallet?api_code=' . BLOCKCHAIN_INFO_API_KEY . '&password='******'userID' => $userID, 'bitcoin_guid' => $returnData->guid, 'bitcoin_address' => $returnData->address, 'bitcoin_link' => $returnData->link, 'bitcoin_password' => buckys_encrypt($password)];
     $db->insertFromArray(TABLE_USERS_BITCOIN, $data);
     return $data;
 }
 /**
  * @param $userID
  * @param $data
  * @return bool
  */
 public function saveAd($userID, $data)
 {
     global $db;
     //Validation Testing
     $adType = $data['type'];
     $adName = trim($data['name']);
     $adUrl = trim($data['url']);
     $budget = floatval($data['budget']);
     if (!$adName || !$adUrl || !$budget) {
         $this->last_message = MSG_INVALID_REQUEST;
         return false;
     }
     if ($budget < ADS_MINIMUM_PURCHASE_AMOUNT) {
         $this->last_message = 'Minimum budget must be at least ' . ADS_MINIMUM_PURCHASE_AMOUNT . ' BTC';
         return false;
     }
     if ($adType == 'Text') {
         $title = trim($data['title']);
         $description = trim($data['description']);
         $display_url = trim($data['display_url']);
         if (strlen($title) > 35) {
             $this->last_message = MSG_AD_TITLE_LENGTH_ERROR;
             return false;
         }
         if (strlen($description) > 70) {
             $this->last_message = MSG_AD_DESCRIPTION_LENGTH_ERROR;
             return false;
         }
         if (strlen($display_url) > 35) {
             $this->last_message = MSG_AD_DISPLAY_URL_LENGTH_ERROR;
             return false;
         }
     } else {
         if ($adType == 'Image') {
             $adSize = $data['size'];
             $fileName = $data['file_name'];
         } else {
             $this->last_message = MSG_INVALID_REQUEST;
             return false;
         }
     }
     //Check User Balance
     $bitcoinClass = new BuckysBitcoin();
     $userBalance = $bitcoinClass->getUserWalletBalance($userID);
     if ($userBalance < $budget) {
         $this->last_message = sprintf(MSG_AD_BITCOIN_BALANCE_NOT_ENOUGH_ERROR, $userBalance . ' BTC');
         return false;
     }
     $sendPayment = $bitcoinClass->sendBitcoin($userID, TNB_BITCOIN_ADDRESS, $budget);
     //they tried to send all the BTC in their wallet and didn't have enough for the fee
     if ($sendPayment === false) {
         $_SESSION['message'] = [];
         $tryPaymentAgain = $bitcoinClass->sendBitcoin($userID, TNB_BITCOIN_ADDRESS, $budget - BLOCKCHAIN_FEE);
         if ($tryPaymentAgain === false) {
             $this->last_message = MSG_INVALID_REQUEST;
             return false;
         }
     }
     $impressions = round($budget / ADS_PRICE_FOR_THOUSAND_IMPRESSIONS * 1000);
     $adKey = md5(buckys_generate_random_string(10) . $adName . $userID . time());
     if ($adType == 'Text') {
         if (!$title || !$description || !$display_url) {
             $this->last_message = MSG_INVALID_REQUEST;
             return false;
         }
         $newId = $db->insertFromArray(TABLE_ADS, ['adKey' => $adKey, 'status' => TNB_AD_STATUS_PENDING, 'createdDate' => date('Y-m-d H:i:s'), 'startedDate' => '0000-00-00 00:00:00', 'endedDate' => '0000-00-00 00:00:00', 'type' => 'Text', 'name' => $adName, 'title' => $title, 'budget' => $budget, 'ownerID' => $userID, 'description' => $description, 'url' => $adUrl, 'display_url' => $display_url, 'impressions' => $impressions]);
         if (!$newId) {
             $this->last_message = $db->last_error;
             return false;
         }
         $this->last_message = MSG_AD_NEW_AD_CREATED;
         return true;
     } else {
         if ($adType == 'Image') {
             if (!$adSize || !$fileName || !file_exists(DIR_FS_TMP . $fileName)) {
                 $this->last_message = MSG_INVALID_REQUEST;
                 return false;
             }
             //Move the image to the images/ads directory
             if (!is_dir(DIR_FS_AD_IMG)) {
                 mkdir(DIR_FS_AD_IMG, 0777);
                 //Create Index.html to prevent directory listing issue
                 $fp = fopen(DIR_FS_AD_IMG . "/index.html", "w");
                 fclose($fp);
             }
             $newFileName = md5(time() . $fileName) . "." . pathinfo($fileName, PATHINFO_EXTENSION);
             $fp = fopen(DIR_FS_TMP . $fileName, "r");
             $fp1 = fopen(DIR_FS_AD_IMG . $newFileName, "w");
             $imgContent = fread($fp, filesize(DIR_FS_TMP . $fileName));
             fwrite($fp1, $imgContent);
             fclose($fp1);
             fclose($fp);
             unlink(DIR_FS_TMP . $fileName);
             $newId = $db->insertFromArray(TABLE_ADS, ['adKey' => $adKey, 'status' => TNB_AD_STATUS_PENDING, 'createdDate' => date('Y-m-d H:i:s'), 'startedDate' => '0000-00-00 00:00:00', 'endedDate' => '0000-00-00 00:00:00', 'type' => 'Image', 'name' => $adName, 'url' => $adUrl, 'budget' => $budget, 'ownerID' => $userID, 'adSize' => $adSize, 'fileName' => $newFileName, 'impressions' => $impressions]);
             if (!$newId) {
                 $this->last_message = $db->last_error;
                 return false;
             }
             $this->last_message = MSG_AD_NEW_AD_CREATED;
             return true;
         }
     }
 }
     buckys_redirect('/register.php' . ($returnUrl ? "?return={$returnUrl}" : ""), MSG_INVALID_LOGIN_INFO, MSG_TYPE_ERROR);
 } else {
     if ($info['status'] == 0) {
         //Account Not Verified or Banned
         buckys_redirect('/index.php', !$info['token'] ? MSG_ACCOUNT_BANNED : MSG_ACCOUNT_NOT_VERIFIED, MSG_TYPE_ERROR);
     } else {
         //Login Success
         //Clear Login Attempts
         BuckysTracker::clearLoginAttemps();
         //Restart Session
         session_regenerate_id(true);
         $_SESSION['userID'] = $info['userID'];
         //Init Some Session Values
         $_SESSION['converation_list'] = [];
         //Create Login Cookie Token
         $login_token = hash('sha256', time() . buckys_generate_random_string(20, true) . time());
         $login_token_secure = md5($login_token);
         //Store Login Token
         BuckysUsersToken::removeUserToken($info['userID'], "auth");
         BuckysUsersToken::createNewToken($info['userID'], "auth", $login_token_secure);
         //Slice the login token to three pieces
         $login_token_piece1 = substr($login_token, 0, 20);
         $login_token_piece2 = substr($login_token, 20, 20);
         $login_token_piece3 = substr($login_token, 40);
         //If website is using SSL, use secure cookies
         if (SITE_USING_SSL == true) {
             setcookie('COOKIE_KEEP_ME_NAME1', base64_encode($login_token_piece1), time() + COOKIE_LIFETIME, "/", TNB_DOMAIN, true, true);
             setcookie('COOKIE_KEEP_ME_NAME2', base64_encode($login_token_piece3), time() + COOKIE_LIFETIME, "/", TNB_DOMAIN, true, true);
             setcookie('COOKIE_KEEP_ME_NAME3', base64_encode($login_token_piece2), time() + COOKIE_LIFETIME, "/", TNB_DOMAIN, true, true);
         } else {
             setcookie('COOKIE_KEEP_ME_NAME1', base64_encode($login_token_piece1), time() + COOKIE_LIFETIME, "/", TNB_DOMAIN);
/**
 * Get Secure Token for the site security
 *
 * @param mixed $forceNew
 * @return null|string
 */
function buckys_get_form_token($forceNew = false)
{
    $token = isset($_SESSION['form.token']) ? $_SESSION['form.token'] : null;
    if ($token === null || $forceNew) {
        $token = buckys_generate_random_string(12);
        $session_name = session_name();
        $token = md5($token . $session_name);
        $_SESSION['form.token'] = $token;
    }
    return $token;
}