function __construct($db)
 {
     $this->db = $db;
     $this->request_cache = array();
     $this->params_cache = array();
     $this->params_cache['game_data_version'] = PgrmConfigDAO::getConfigProperty($this->db, 'value1', 'GAME_INFO', 'DATA_VERSION');
     $this->params_cache['game_name'] = PgrmConfigDAO::getConfigProperty($this->db, 'value1', 'GAME_INFO', 'NAME');
     $this->params_cache['client_build'] = PgrmConfigDAO::getConfigProperty($this->db, 'value1', 'CLIENT', 'BUILD');
     $this->params_cache['client_version'] = PgrmConfigDAO::getConfigProperty($this->db, 'value1', 'CLIENT', 'VERSION');
     $this->params_cache['api_version'] = PgrmConfigDAO::getConfigProperty($this->db, 'value1', 'API', 'VERSION');
     $url_base = PgrmConfigDAO::getConfigProperty($this->db, 'value1', 'URL_BASE');
     $hmac_key = PgrmConfigDAO::getConfigProperty($this->db, 'value1', 'TOKENS', 'HMAC_KEY');
     $proxies = ProxyDAO::getActiveProxies($this->db);
     $this->ws = new WarOfnationsWS($this->db, $proxies, $url_base, $hmac_key);
 }
 public function MakeRequest($endpoint, $data_string, $method, $retry_count = 0)
 {
     $log_msg = "Attempt #" . ($retry_count + 1) . "\r\n\r\n" . $data_string;
     $log_seq = 0;
     $func_args = func_get_args();
     $func_log_id = DataLoadLogDAO::startFunction($this->db, $this->data_load_id, __CLASS__, __FUNCTION__, $func_args);
     if ($retry_count > 0) {
         echo "Retry Attempt #{$retry_count}\r\n";
     }
     if ($retry_count >= self::$min_retry_delay && self::$min_retry_delay > 0) {
         $waittime = ($retry_count - self::$min_retry_delay + 1) * 5;
         echo "Waiting {$waittime} seconds before retry.\r\n";
         DataLoadLogDAO::logEvent2($this->db, $func_log_id, $log_seq++, 'INFO', "Retry Attempt #{$retry_count}: Waiting {$waittime} seconds before retry.");
         usleep($waittime * 1000000);
     }
     // Assemble our URL
     $url = $this->url_base . $endpoint;
     // Initialize our Curl request
     $headers = $this->get_headers($endpoint, $data_string);
     $proxy = false;
     if ($this->proxies != false) {
         $proxy = $this->proxies[array_rand($this->proxies)];
     }
     $ch = $this->init_request($url, $proxy);
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
     $log_msg = "Proxy: {$proxy['ip_address']}:{$proxy['port']}\r\n\r\n" . print_r($headers, true);
     $request_id = DataLoadLogDAO::logWebserviceRequest($this->db, $func_log_id, $url, $method, $proxy, $headers, $data_string);
     // Execute our request
     $start = microtime(true);
     $response_string = curl_exec($ch);
     $end = microtime(true);
     if (!$response_string) {
         $curl_error = curl_error($ch);
     }
     // cleans up the curl request
     curl_close($ch);
     $request_time = $end - $start;
     //echo "Request completed in ".$request_time." seconds\r\n";
     // If our call failed
     if (!$response_string) {
         $log_msg = "Error Description: {$curl_error}\r\n\r\nProxy: {$proxy['ip_address']}:{$proxy['port']}<br/>\r\nURL: {$url}<br/>\r\nData: {$data_string}<br/>\r\n";
         DataLoadLogDAO::logEvent2($this->db, $func_log_id, $log_seq++, 'ERROR', "Error occurred during curl request", $log_msg, 1);
         echo "Error occurred while getting Curl response.\r\n";
         //echo "Proxy: {$proxy['ip_address']}:{$proxy['port']}<br/>\r\n";
         //echo "URL: $url<br/>\r\n";
         //echo "Data: $data_string<br/>\r\n\r\n";
         ProxyDAO::countFailure($this->db, $proxy['id'], $request_time);
         // Retry if we haven't reached our max
         if (!$this->max_attempts_reached($retry_count)) {
             return $this->MakeRequest($endpoint, $data_string, $method, $retry_count + 1);
         } else {
             return false;
         }
     }
     // We send a header requesting a gzip encoded response, so try to decode it
     $decoded = gzdecode($response_string);
     // If we encountered an error in decoding, see what we can do with it
     if (!$decoded) {
         // Some of our proxies decode the gzip for us, so check to see if we've been decoded
         // If we have UTF-8 encoding already, then we might be OK after all
         if (mb_check_encoding($response_string, 'UTF-8')) {
             DataLoadLogDAO::logEvent2($this->db, $func_log_id, $log_seq++, 'WARNING', "Gzip decoding failed, but response is utf8 encoded.  We'll try to use it.", $response_string);
             echo "Error occurred while decoding CURL response, but let's assume this is OK!\r\n";
             // If we think we got a proxy error, then log it, and disable the proxy server.  Otherwise return the response
             if (stripos($response_string, 'The maximum web proxy user limit has been reached') > 0) {
                 DataLoadLogDAO::logEvent2($this->db, $func_log_id, $log_seq++, 'ERROR', "Proxy Limit Reached", null, 1);
                 ProxyDAO::disableProxy($this->db, $proxy['id'], 'The maximum web proxy user limit has been reached');
             } else {
                 if (stripos($response_string, '<title>Access Den') > 0) {
                     DataLoadLogDAO::logEvent2($this->db, $func_log_id, $log_seq++, 'ERROR', "Access Denied", null, 1);
                     ProxyDAO::disableProxy($this->db, $proxy['id'], 'Access Denied Received');
                 } else {
                     ProxyDAO::countSuccess($this->db, $proxy['id'], $request_time);
                     // Since we have a valid string, try to decode the JSON response
                     $json_array = json_decode($response_string, true);
                     if (!$json_array) {
                         DataLoadLogDAO::logWebserviceResponse($this->db, $request_id, $decoded, $request_time);
                         DataLoadLogDAO::completeFunction($this->db, $func_log_id, 'Failed to Decode JSON Response');
                         return false;
                     }
                     DataLoadLogDAO::logWebserviceResponse($this->db, $request_id, $response_string, $request_time, $json_array);
                     // Validate that the request completed successfully
                     $this->ValidateResponse($json_array, $func_log_id, $request_id);
                     return $json_array;
                 }
             }
         } else {
             DataLoadLogDAO::logEvent2($this->db, $func_log_id, $log_seq++, 'ERROR', "Gzip decoding failed, and we don't know what to do with it.  Retry this request.", $response_string, 1);
             echo "Error occurred while decoding CURL response, and we think this is a problem!\r\n";
             ProxyDAO::countFailure($this->db, $proxy['id'], $request_time);
         }
         // If we made it this far then we need to retry
         if (!$this->max_attempts_reached($retry_count)) {
             DataLoadLogDAO::completeFunction($this->db, $func_log_id, 'Failed to Decode Response. Retrying.', 1);
             return $this->MakeRequest($endpoint, $data_string, $method, $retry_count + 1);
         } else {
             DataLoadLogDAO::completeFunction($this->db, $func_log_id, "Request Failed. Retry limit hit [{$retry_count}]. Quitting.", 1);
             return false;
         }
     }
     // If we made it this far, then we were successful, to let's log it as a success to our proxy
     ProxyDAO::countSuccess($this->db, $proxy['id'], $request_time);
     // Since we have a valid string, try to decode the JSON response
     $json_array = json_decode($decoded, true);
     if ($json_array === null) {
         DataLoadLogDAO::logWebserviceResponse($this->db, $request_id, $decoded, $request_time);
         DataLoadLogDAO::completeFunction($this->db, $func_log_id, 'Failed to Decode JSON Response', 1);
         return false;
     }
     DataLoadLogDAO::logWebserviceResponse($this->db, $request_id, $decoded, $request_time, $json_array);
     // Validate that the request completed successfully
     $status = $this->ValidateResponse($json_array, $func_log_id, $request_id);
     if ($status === 'retry') {
         DataLoadLogDAO::logEvent2($this->db, $func_log_id, $log_seq++, 'ERROR', "Validation returned [{$status}].  Retrying request...", $response_string, 1);
         echo "Validation returned [{$status}].  Retrying request...\n";
         return $this->MakeRequest($endpoint, $data_string, $method, $retry_count + 1);
     }
     // Return the decoded string
     return $json_array;
 }
        if ($proxy['type'] == 'SOCKS') {
            curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        //timeout in seconds
        // makes the curl call do it's work based on what we've set previously and
        //returns that fetched page to $page
        $page = curl_exec($ch);
        // cleans up the curl set
        curl_close($ch);
        // this will check that there was some html returned, now some sites might block some
        //proxies so you'd want to set for that specific site in the $testpage var and then
        //find something on that page to look for with the below function.
        $check = stripos($page, $checktext);
        // if there was a match in the stripos (string postion) function echo that the
        //proxy got the data and works
        if ($check > 0) {
            echo $proxy_str . " Works!\r\n";
            ProxyDAO::countSuccess($db, $proxy['id']);
            // or else echo it doesn't work
        } else {
            echo $proxy_str . " Is Dead!\r\n";
            ProxyDAO::countFailure($db, $proxy['id']);
        }
    }
}
?>

?>