PageRenderTime 30ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/mailz/classes/http.class.php

https://bitbucket.org/antonyravel/cape-resorts
PHP | 438 lines | 336 code | 47 blank | 55 comment | 95 complexity | 79c2c17d966149cf825b813aaae15dc4 MD5 | raw file
  1. <?php
  2. //v2.02.06
  3. //removed cc_whmcs_log call
  4. //need wpabspath for mailz
  5. //mailz returns full URL in case of redirection!!
  6. //made redirect generic, detect if location string contains protocol and server or not
  7. //added option to enable repost of $_POST variables
  8. //fixed issue with redirection location string
  9. //added support for content-type
  10. //fixed issue with $this->$headers wrong, should be $this->headers
  11. //fixed issue with handling of $repost
  12. //added check on $headers['location'] existence
  13. //added initialisation of $post and $apost
  14. //fixed issue with checkConnection()
  15. //added support for multiple cookies
  16. //check if session exists before starting a new one
  17. //changed return test value of checkConnection()
  18. //replaced display of error and notice by triggering PHP error or notice
  19. //added mime type info to uploaded files
  20. //added option to disable following redirect links
  21. //fixed issue with HTTP 417 errors on some web servers
  22. //fixed redirect link parsing issue
  23. //removed check on cainfo
  24. //added redirection fix for Windows
  25. //removed _params variable
  26. //fixed issue with redirect urls duplicating url path
  27. //added http return code
  28. //improvide error management
  29. if (!class_exists('zHttpRequest')) {
  30. class zHttpRequest
  31. {
  32. var $_fp; // HTTP socket
  33. var $_url; // full URL
  34. var $_host; // HTTP host
  35. var $_protocol; // protocol (HTTP/HTTPS)
  36. var $_uri; // request URI
  37. var $_port; // port
  38. var $_path;
  39. var $error=false;
  40. var $errno=false;
  41. var $post=array(); //post variables, defaults to $_POST
  42. var $redirect=false;
  43. var $forceWithRedirect=array('wpabspath' => 0);
  44. var $errors=array();
  45. var $countRedirects=0;
  46. var $sid;
  47. var $httpCode;
  48. var $repost=false;
  49. var $type; //content-type
  50. var $follow=true; //whether to follow redirect links or not
  51. var $noErrors=false; //whether to trigger an error in case of a curl error
  52. var $errorMessage;
  53. var $httpHeaders=array('Expect:');
  54. var $debugFunction;
  55. var $time;
  56. // constructor
  57. function __construct($url="",$sid='', $repost=false)
  58. {
  59. if (!$url) return;
  60. $this->sid=$sid;
  61. $this->_url = $url;
  62. $this->_scan_url();
  63. $this->post=$_POST;
  64. $this->repost=$repost;
  65. }
  66. private function time($action) {
  67. $t=function_exists('microtime') ? 'microtime' :'time';
  68. if ($action=='reset') $this->time=$t(true);
  69. elseif ($action=='delta') return round(($t(true)-$this->time)*100,0);
  70. }
  71. private function forceWithRedirectToString() {
  72. $s='';
  73. foreach ($this->forceWithRedirect as $n => $v) {
  74. if ($s) $s.='&';
  75. $s.=$n.'='.$v;
  76. }
  77. return $s;
  78. }
  79. private function debug($type=0,$msg='',$filename="",$linenum=0) {
  80. if ($f=$this->debugFunction) $f($type,$msg,$filename,$linenum);
  81. }
  82. private function os() {
  83. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') return 'WINDOWS';
  84. else return 'LINUX';
  85. }
  86. private function processHeaders($headers) {
  87. // split headers, one per array element
  88. if ( is_string($headers) ) {
  89. // tolerate line terminator: CRLF = LF (RFC 2616 19.3)
  90. $headers = str_replace("\r\n", "\n", $headers);
  91. // unfold folded header fields. LWS = [CRLF] 1*( SP | HT ) <US-ASCII SP, space (32)>, <US-ASCII HT, horizontal-tab (9)> (RFC 2616 2.2)
  92. $headers = preg_replace('/\n[ \t]/', ' ', $headers);
  93. // create the headers array
  94. $headers = explode("\n", $headers);
  95. }
  96. $response = array('code' => 0, 'message' => '');
  97. // If a redirection has taken place, The headers for each page request may have been passed.
  98. // In this case, determine the final HTTP header and parse from there.
  99. for ( $i = count($headers)-1; $i >= 0; $i-- ) {
  100. if ( !empty($headers[$i]) && false === strpos($headers[$i], ':') ) {
  101. $headers = array_splice($headers, $i);
  102. break;
  103. }
  104. }
  105. $cookies = '';
  106. $newheaders = array();
  107. //echo '<br /><br />HEADERS<br />'.print_r($headers,true).'===<br /><br />';
  108. foreach ( $headers as $tempheader ) {
  109. if ( empty($tempheader) )
  110. continue;
  111. if ( false === strpos($tempheader, ':') ) {
  112. list( , $response['code'], $response['message']) = explode(' ', $tempheader, 3);
  113. continue;
  114. }
  115. list($key, $value) = explode(':', $tempheader, 2);
  116. if ( !empty( $value ) ) {
  117. $key = strtolower( $key );
  118. if ( isset( $newheaders[$key] ) ) {
  119. if ( !is_array($newheaders[$key]) )
  120. $newheaders[$key] = array($newheaders[$key]);
  121. $newheaders[$key][] = trim( $value );
  122. } else {
  123. $newheaders[$key] = trim( $value );
  124. }
  125. if ( 'set-cookie' == $key ) {
  126. if ($cookies) $cookies.=' ;';
  127. $cookies .= $value;
  128. }
  129. }
  130. }
  131. //echo '<br /><br />COOKIES:'.$cookies.'===<br /><br />';
  132. return array('response' => $response, 'headers' => $newheaders, 'cookies' => $cookies);
  133. }
  134. // scan url
  135. private function _scan_url()
  136. {
  137. $req = $this->_url;
  138. $pos = strpos($req, '://');
  139. $this->_protocol = strtolower(substr($req, 0, $pos));
  140. $req = substr($req, $pos+3);
  141. $pos = strpos($req, '/');
  142. if($pos === false)
  143. $pos = strlen($req);
  144. $host = substr($req, 0, $pos);
  145. if(strpos($host, ':') !== false)
  146. {
  147. list($this->_host, $this->_port) = explode(':', $host);
  148. }
  149. else
  150. {
  151. $this->_host = $host;
  152. $this->_port = ($this->_protocol == 'https') ? 443 : 80;
  153. }
  154. $this->_uri = substr($req, $pos);
  155. if($this->_uri == '') {
  156. $this->_uri = '/';
  157. } else {
  158. $params=substr(strrchr($this->_uri,'/'),1);
  159. $this->_path=str_replace($params,'',$this->_uri);
  160. }
  161. }
  162. //check if server is live
  163. function live() {
  164. if (ip2long($this->_host)) return true; //in case using an IP instead of a host name
  165. $url=$this->_host;
  166. if (gethostbyname($url) == $url) return false;
  167. else return true;
  168. }
  169. //get mime type of uploaded file
  170. function mimeType($file) {
  171. $mime='';
  172. if (function_exists('finfo_open')) {
  173. if ($finfo = finfo_open(FILEINFO_MIME_TYPE)) {
  174. $mime=finfo_file($finfo, $file);
  175. finfo_close($finfo);
  176. }
  177. }
  178. if ($mime) return ';type='.$mime;
  179. else return '';
  180. }
  181. //check if cURL installed
  182. function curlInstalled() {
  183. if (!function_exists('curl_init')) return false;
  184. else return true;
  185. }
  186. //check destination is reachable
  187. function checkConnection() {
  188. $this->post['checkconnection']=1;
  189. $output=$this->connect($this->_protocol.'://'.$this->_host.$this->_uri);
  190. if ($output=='zingiri' || $output=='connected') return true;
  191. else return false;
  192. }
  193. //error logging
  194. function error($msg) {
  195. $this->errorMsg=$msg;
  196. $this->error=true;
  197. if (!$this->noErrors) trigger_error($msg,E_USER_WARNING);
  198. }
  199. //notification logging
  200. function notify($msg) {
  201. $this->errorMsg=$msg;
  202. $this->error=true;
  203. if (!$this->noErrors) trigger_error($msg,E_USER_NOTICE);
  204. }
  205. // download URL to string
  206. function DownloadToString($withHeaders=true,$withCookies=false)
  207. {
  208. return $this->connect($this->_protocol.'://'.$this->_host.$this->_uri,$withHeaders,$withCookies);
  209. }
  210. function connect($url,$withHeaders=true,$withCookies=false)
  211. {
  212. $this->time('reset');
  213. $newfiles=array();
  214. if (!session_id()) @session_start();
  215. $ch = curl_init(); // initialize curl handle
  216. //echo '<br />call:'.$url;echo '<br />post='.print_r($this->post,true).'=<br />headers='.print_r($this->httpHeaders,true).'<br />';
  217. $this->debug(0,'http call: '.$url.' with '.print_r($this->post,true));
  218. curl_setopt($ch, CURLOPT_URL,$url); // set url to post to
  219. curl_setopt($ch, CURLOPT_FAILONERROR, 1);
  220. if ($withHeaders) curl_setopt($ch, CURLOPT_HEADER, 1);
  221. curl_setopt($ch, CURLOPT_HTTPHEADER, $this->httpHeaders); //avoid 417 errors
  222. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
  223. curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  224. curl_setopt($ch, CURLOPT_TIMEOUT, 30); // times out after 10s
  225. if ($this->_protocol == "https") {
  226. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  227. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  228. curl_setopt($ch, CURLOPT_CAINFO, NULL);
  229. curl_setopt($ch, CURLOPT_CAPATH, NULL);
  230. }
  231. $cookies="";
  232. if ($withCookies && isset($_COOKIE)) {
  233. foreach ($_COOKIE as $i => $v) {
  234. if ($i=='WHMCSUID' || $i=="WHMCSPW") {
  235. if ($cookies) $cookies.=';';
  236. $cookies.=$i.'='.$v;
  237. }
  238. }
  239. }
  240. $cookies=apply_filters('zHttpRequest_pre',$cookies);
  241. if (isset($_SESSION[$this->sid]['cookies'])) {
  242. //curl_setopt($ch, CURLOPT_COOKIE, $_SESSION[$this->sid]['cookies']);
  243. if ($cookies) $cookies.=';';
  244. $cookies.=$_SESSION[$this->sid]['cookies'];
  245. }
  246. //echo '<br />cookie before='.$cookies.'=';
  247. if (is_array($cookies)) $this->debug(0,'Cookie before:'.print_r($cookies,true));
  248. if ($cookies) {
  249. curl_setopt($ch, CURLOPT_COOKIE, $cookies);
  250. }
  251. if (count($_FILES) > 0) {
  252. foreach ($_FILES as $name => $file) {
  253. if (is_array($file['tmp_name']) && count($file['tmp_name']) > 0) {
  254. $c=count($file['tmp_name']);
  255. for ($i=0;$i<$c;$i++) {
  256. if ($file['tmp_name'][$i]) {
  257. $newfile=BLOGUPLOADDIR.$file['name'][$i];
  258. $newfiles[]=$newfile;
  259. copy($file['tmp_name'][$i],$newfile);
  260. if ($file['tmp_name'][$i]) $this->post[$name][$i]='@'.$newfile.$this->mimeType($newfile);
  261. }
  262. }
  263. } elseif ($file['tmp_name']) {
  264. $newfile=BLOGUPLOADDIR.$file['name'];
  265. $newfiles[]=$newfile;
  266. copy($file['tmp_name'],$newfile);
  267. if ($file['tmp_name']) $this->post[$name]='@'.$newfile.$this->mimeType($newfile);
  268. }
  269. }
  270. }
  271. $post='';
  272. $apost=array();
  273. if (count($this->post) > 0) {
  274. curl_setopt($ch, CURLOPT_POST, 1); // set POST method
  275. $post="";
  276. $apost=array();
  277. foreach ($this->post as $k => $v) {
  278. if (is_array($v)) {
  279. foreach ($v as $k2 => $v2) {
  280. if (is_array($v2)) {
  281. foreach ($v2 as $k3 => $v3) {
  282. if ($post) $post.='&';
  283. $post.=$k.'['.$k2.']'.'['.$k3.']'.'='.urlencode(stripslashes($v3));
  284. $apost[$k.'['.$k2.']'.'['.$k3.']']=stripslashes($v3);
  285. }
  286. } else {
  287. if ($post) $post.='&';
  288. $post.=$k.'['.$k2.']'.'='.urlencode(stripslashes($v2));
  289. $key='['.$k.']['.$k2.']';
  290. $apost[$k.'['.$k2.']']=stripslashes($v2);
  291. }
  292. }
  293. } else {
  294. if ($post) $post.='&';
  295. $post.=$k.'='.urlencode(stripslashes($v));
  296. $apost[$k]=stripslashes($v);
  297. }
  298. }
  299. }
  300. if (count($apost) > 0) {
  301. curl_setopt($ch, CURLOPT_POSTFIELDS, $apost); // add POST fields
  302. }
  303. $data = curl_exec($ch); // run the whole process
  304. if (curl_errno($ch)) {
  305. $this->errno=curl_errno($ch);
  306. $this->error=curl_error($ch);
  307. $this->error('HTTP Error:'.$this->errno.'/'.$this->error.' at '.$this->_url);
  308. return false;
  309. }
  310. $info=curl_getinfo($ch);
  311. if ( !empty($data) ) {
  312. $headerLength = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  313. $head = trim( substr($data, 0, $headerLength) );
  314. if ( strlen($data) > $headerLength ) $body = substr( $data, $headerLength );
  315. else $body = '';
  316. if ( false !== strpos($head, "\r\n\r\n") ) {
  317. $headerParts = explode("\r\n\r\n", $head);
  318. $head = $headerParts[ count($headerParts) -1 ];
  319. }
  320. $head = $this->processHeaders($head);
  321. $headers=$head['headers'];
  322. $cookies=$head['cookies'];
  323. } else {
  324. $headers=array();
  325. $cookies='';
  326. $body = '';
  327. $this->error('An undefined error occured');
  328. return false;
  329. }
  330. if ($cookies) {
  331. if (!isset($_SESSION[$this->sid])) $_SESSION[$this->sid]=array();
  332. $_SESSION[$this->sid]['cookies']=$cookies;
  333. }
  334. //echo '<br />cookie after='.print_r($_SESSION[$this->sid]['cookies'],true).'=';
  335. if (is_array($cookies)) $this->debug(0,'Cookie after:'.print_r($cookies,true));
  336. curl_close($ch);
  337. //remove temporary upload files
  338. if (count($newfiles) > 0) {
  339. foreach ($newfiles as $file) {
  340. unlink($file);
  341. }
  342. }
  343. $this->headers=$headers;
  344. $this->data=$data;
  345. $this->cookies=$cookies;
  346. $this->body=$body;
  347. if ($headers['content-type']) {
  348. $this->type=$headers['content-type'];
  349. }
  350. $this->cookies=apply_filters('zHttpRequest_post',$this->cookies);
  351. $this->debug(0,'Call completed in '.$this->time('delta').' microseconds');
  352. if ($this->follow && isset ($headers['location']) && $headers['location']) {
  353. //echo '<br />redirect to:'.print_r($headers,true);
  354. //echo '<br />protocol='.$this->_protocol;
  355. //echo '<br />path='.$this->_path;
  356. $redir=$headers['location'];
  357. if ($this->os()=='WINDOWS') {
  358. if (strstr($this->_protocol.'://'.$this->_host.$redir,$this->_protocol.'://'.$this->_host.$this->_path)) $redir=$this->_protocol.'://'.$this->_host.$this->_path;
  359. elseif (!strstr($redir,$this->_host)) $redir=$this->_protocol.'://'.$this->_host.$this->_path.$redir;
  360. } else {
  361. if (strstr($redir,$this->_protocol.'://'.$this->_host.$this->_path)) {
  362. //do nothing
  363. } elseif (strstr($this->_protocol.'://'.$this->_host.$redir,$this->_protocol.'://'.$this->_host.$this->_path)) {
  364. $redir=$this->_protocol.'://'.$this->_host.$redir;
  365. } elseif (!strstr($redir,$this->_host)) {
  366. $redir=$this->_protocol.'://'.$this->_host.$this->_path.$redir;
  367. }
  368. }
  369. //echo '<br />redir='.$redir;
  370. if (strstr($redir,'&')) $redir.='&';
  371. elseif (strstr($redir,'?')) $redir.='&';
  372. else $redir.='?';
  373. //$redir.='wpabspath=0';
  374. $redir.=$this->forceWithRedirectToString();
  375. $this->debug(0,'Redirect to: '.$redir);
  376. if (!$this->repost) $this->post=array();
  377. $this->countRedirects++;
  378. if ($this->countRedirects < 10) {
  379. if ($redir != $url) {
  380. return $this->connect($redir,$withHeaders,$withCookies);
  381. }
  382. } else {
  383. $this->error('ERROR: Too many redirects '.$url.' > '.$headers['location'],E_USER_ERROR);
  384. return false;
  385. }
  386. }
  387. return $body;
  388. }
  389. }
  390. }
  391. ?>