timeout=$timeout; // check if curl is choosen and available and initiate cURL-Session if ($connect=='curl' and function_exists('curl_init')) { if ($this->startCurl($url,$ssl,$port)===true) { $this->connect='curl'; } // Use and or fallback to fsockopen if possible and create socket } else if (($connect=='fsockopen' or !function_exists('curl_init')) and function_exists('fsockopen')) { if ($this->startSocket($url,$ssl,$port)===true) { $this->connect='fsockopen'; } } // If connection was successfull, go on and set values if ($this->connect!==false) { $this->user=$user; $this->pwd=$pwd; $this->ssl=$ssl; $this->port=$port; $this->url=$url; // Use json, or xml to communicate if ($method=='json') { $this->method='json'; } else { $this->method='xml'; } } else { $this->throwException(10); } } // False usage of the object needs to be handled and execution stopped private function throwException ($rawError,$extraText=false) { // If an exception is caught from imbedded class use the raw error if (is_object($rawError)) { $errorcode=$rawError->getMessage(); // else use the custom messages } else { // default custom messages $errorArray=array( 1=>'Bad data: Only Strings and Integers are allowed!', 2=>'Bad data: Only Strings are allowed!', 3=>'Bad data: Only Integers are allowed!', 4=>'Bad data: Only arrays are allowed!', 5=>'Bad data: Unknown Error!', 6=>'Bad data: Empty values!', 10=>'Connection Error: Could not connect to!'.$this->url ); // if the message is not predifined use the raw input if (array_key_exists($rawError,$errorArray)) { $errorcode=$errorArray["${rawError}"]; } else { $errorcode=$rawError; } } // Add some extra info if given if ($extraText!==false) { $errorcode.=$extraText; } throw new Exception('
'.$errorcode.'
'); die; } // private function startCurl ($url,$ssl,$port) { // create the URL to call if (substr($url,-1)=='/') { $url=substr($url,0,-1); } $url=str_replace(array('http://','https://',':8080',':80',':443'),'',$url); if ($ssl==true) { $url='https://'.$url; } else { $url='http://'.$url; } $url=$url.'/api.php'; // create cURL-Handle $this->handle=curl_init($url); // check success if ($this->handle===false) { return false; } else { // Set options $this->setbasicCurlOpts(); return true; } } // in case of curl setopts private function setbasicCurlOpts () { curl_setopt($this->handle,CURLOPT_CONNECTTIMEOUT,$this->timeout); curl_setopt($this->handle,CURLOPT_USERAGENT,"cURL (Easy-WI; 1.0; Linux)"); curl_setopt($this->handle,CURLOPT_RETURNTRANSFER,true); curl_setopt($this->handle,CURLOPT_SSL_VERIFYPEER,false); curl_setopt($this->handle,CURLOPT_FOLLOWLOCATION,1); curl_setopt($this->handle,CURLOPT_HEADER,1); //curl_setopt($this->handle,CURLOPT_ENCODING,'deflate'); if (($this->ssl===true and $this->port!=443) or ($this->ssl===false and $this->port!=80)) { curl_setopt($this->handle,CURLOPT_PORT,$this->port); } } // method to execute a curl request private function execCurl($type,$send) { // Setting up POST data and add it to the opts $postArray['user']=$this->user; $postArray['pwd']=$this->pwd; $postArray['type']=$type; $postArray['xmlstring']=$send; curl_setopt($this->handle,CURLOPT_POSTFIELDS,$postArray); // Execute request, get the response and return it. $this->response=curl_exec($this->handle); $this->header=curl_getinfo($this->handle); return $this->response; } // Ioncube obfuscated files add sometimes data to the REST responses. // This will be picked up if fsockopen is used. // So there is a need to strip this data. private function convertRawData ($rawdata) { if ($this->method=='json') { $checkStart='{'; $checkStop='}'; } else { $checkStart='<'; $checkStop='>'; } $response=$rawdata; while (substr($response,0,1)!=$checkStart and strlen($response)>0) { $response=substr($response,1); } while (substr($response,-1)!=$checkStop and strlen($response)>0) { $response=substr($response,0,-1); } // Decode the rest of the response string into an object. if ($this->method=='json') { $decoded=@json_decode($response); } else { $decoded=@simplexml_load_string($response); } // If decoding was not possible return the raw response, else return the object. if ($decoded) { unset($rawdata); return $decoded; } else if ($this->connect=='fsockopen') { return substr($rawdata,4,-3); } else { return $rawdata; } unset($decoded); } // create the JSON that will be send to the API private function JSONPostValue ($paramArray,$action,$params) { $jsonArray=array(); foreach ($paramArray as $param) { if (array_key_exists($param,$params)) { if (is_array($params[$param])) { $jsonArray[$param]=array(); foreach ($params[$param] as $val) { $jsonArray[$param][]=$params[$param]; } } else { $jsonArray[$param]=$params[$param]; } } else { $jsonArray[$param]=''; } } $json=json_encode($jsonArray); unset($type,$params,$paramArray,$jsonArray); return $json; } // create the XML that will be send to the API private function XMLPostValue ($paramArray,$action,$params) { $xml=new SimpleXMLElement(<<