PageRenderTime 54ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/application/libraries/admin/http/httpRequestIt.php

https://bitbucket.org/sammousa/valuematchbv-ls2
PHP | 1982 lines | 1932 code | 41 blank | 9 comment | 396 complexity | dc235e7b6d332d658dd2fa567a13cf57 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause, GPL-3.0, LGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /*
  3. * http.php
  4. *
  5. * @(#) $Header: /home/mlemos/cvsroot/http/http.php,v 1.79 2009/09/03 00:09:37 mlemos Exp $
  6. *
  7. */
  8. class httpRequestIt
  9. {
  10. var $host_name="";
  11. var $host_port=0;
  12. var $proxy_host_name="";
  13. var $proxy_host_port=80;
  14. var $socks_host_name = '';
  15. var $socks_host_port = 1080;
  16. var $socks_version = '5';
  17. var $protocol="http";
  18. var $request_method="GET";
  19. var $user_agent='httpclient (http://www.phpclasses.org/httpclient $Revision: 1.79 $)';
  20. var $authentication_mechanism="";
  21. var $user;
  22. var $password;
  23. var $realm;
  24. var $workstation;
  25. var $proxy_authentication_mechanism="";
  26. var $proxy_user;
  27. var $proxy_password;
  28. var $proxy_realm;
  29. var $proxy_workstation;
  30. var $request_uri="";
  31. var $request="";
  32. var $request_headers=array();
  33. var $request_user;
  34. var $request_password;
  35. var $request_realm;
  36. var $request_workstation;
  37. var $proxy_request_user;
  38. var $proxy_request_password;
  39. var $proxy_request_realm;
  40. var $proxy_request_workstation;
  41. var $request_body="";
  42. var $request_arguments=array();
  43. var $protocol_version="1.1";
  44. var $timeout=0;
  45. var $data_timeout=0;
  46. var $debug=0;
  47. var $debug_response_body=1;
  48. var $html_debug=0;
  49. var $support_cookies=1;
  50. var $cookies=array();
  51. var $error="";
  52. var $exclude_address="";
  53. var $follow_redirect=0;
  54. var $redirection_limit=5;
  55. var $response_status="";
  56. var $response_message="";
  57. var $file_buffer_length=8000;
  58. var $force_multipart_form_post=0;
  59. var $prefer_curl = 0;
  60. /* private variables - DO NOT ACCESS */
  61. var $state="Disconnected";
  62. var $use_curl=0;
  63. var $connection=0;
  64. var $content_length=0;
  65. var $response="";
  66. var $read_response=0;
  67. var $read_length=0;
  68. var $request_host="";
  69. var $next_token="";
  70. var $redirection_level=0;
  71. var $chunked=0;
  72. var $remaining_chunk=0;
  73. var $last_chunk_read=0;
  74. var $months=array(
  75. "Jan"=>"01",
  76. "Feb"=>"02",
  77. "Mar"=>"03",
  78. "Apr"=>"04",
  79. "May"=>"05",
  80. "Jun"=>"06",
  81. "Jul"=>"07",
  82. "Aug"=>"08",
  83. "Sep"=>"09",
  84. "Oct"=>"10",
  85. "Nov"=>"11",
  86. "Dec"=>"12");
  87. var $session='';
  88. var $connection_close=0;
  89. /* Private methods - DO NOT CALL */
  90. Function Tokenize($string,$separator="")
  91. {
  92. if(!strcmp($separator,""))
  93. {
  94. $separator=$string;
  95. $string=$this->next_token;
  96. }
  97. for($character=0;$character<strlen($separator);$character++)
  98. {
  99. if(GetType($position=strpos($string,$separator[$character]))=="integer")
  100. $found=(IsSet($found) ? min($found,$position) : $position);
  101. }
  102. if(IsSet($found))
  103. {
  104. $this->next_token=substr($string,$found+1);
  105. return(substr($string,0,$found));
  106. }
  107. else
  108. {
  109. $this->next_token="";
  110. return($string);
  111. }
  112. }
  113. Function CookieEncode($value, $name)
  114. {
  115. return($name ? str_replace("=", "%25", $value) : str_replace(";", "%3B", $value));
  116. }
  117. Function SetError($error)
  118. {
  119. return($this->error=$error);
  120. }
  121. Function SetPHPError($error, &$php_error_message)
  122. {
  123. if(IsSet($php_error_message)
  124. && strlen($php_error_message))
  125. $error.=": ".$php_error_message;
  126. return($this->SetError($error));
  127. }
  128. Function SetDataAccessError($error,$check_connection=0)
  129. {
  130. $this->error=$error;
  131. if(!$this->use_curl
  132. && function_exists("socket_get_status"))
  133. {
  134. $status=socket_get_status($this->connection);
  135. if($status["timed_out"])
  136. $this->error.=": data access time out";
  137. elseif($status["eof"])
  138. {
  139. if($check_connection)
  140. $this->error="";
  141. else
  142. $this->error.=": the server disconnected";
  143. }
  144. }
  145. }
  146. Function OutputDebug($message)
  147. {
  148. $message.="\n";
  149. if($this->html_debug)
  150. $message=str_replace("\n","<br />\n",HtmlEntities($message));
  151. echo $message;
  152. flush();
  153. }
  154. Function GetLine()
  155. {
  156. for($line="";;)
  157. {
  158. if($this->use_curl)
  159. {
  160. $eol=strpos($this->response,"\n",$this->read_response);
  161. $data=($eol ? substr($this->response,$this->read_response,$eol+1-$this->read_response) : "");
  162. $this->read_response+=strlen($data);
  163. }
  164. else
  165. {
  166. if(feof($this->connection))
  167. {
  168. $this->SetDataAccessError("reached the end of data while reading from the HTTP server connection");
  169. return(0);
  170. }
  171. $data=fgets($this->connection,100);
  172. }
  173. if(GetType($data)!="string"
  174. || strlen($data)==0)
  175. {
  176. $this->SetDataAccessError("it was not possible to read line from the HTTP server");
  177. return(0);
  178. }
  179. $line.=$data;
  180. $length=strlen($line);
  181. if($length
  182. && !strcmp(substr($line,$length-1,1),"\n"))
  183. {
  184. $length-=(($length>=2 && !strcmp(substr($line,$length-2,1),"\r")) ? 2 : 1);
  185. $line=substr($line,0,$length);
  186. if($this->debug)
  187. $this->OutputDebug("S $line");
  188. return($line);
  189. }
  190. }
  191. }
  192. Function PutLine($line)
  193. {
  194. if($this->debug)
  195. $this->OutputDebug("C $line");
  196. if(!fputs($this->connection,$line."\r\n"))
  197. {
  198. $this->SetDataAccessError("it was not possible to send a line to the HTTP server");
  199. return(0);
  200. }
  201. return(1);
  202. }
  203. Function PutData($data)
  204. {
  205. if(strlen($data))
  206. {
  207. if($this->debug)
  208. $this->OutputDebug('C '.$data);
  209. if(!fputs($this->connection,$data))
  210. {
  211. $this->SetDataAccessError("it was not possible to send data to the HTTP server");
  212. return(0);
  213. }
  214. }
  215. return(1);
  216. }
  217. Function FlushData()
  218. {
  219. if(!fflush($this->connection))
  220. {
  221. $this->SetDataAccessError("it was not possible to send data to the HTTP server");
  222. return(0);
  223. }
  224. return(1);
  225. }
  226. Function ReadChunkSize()
  227. {
  228. if($this->remaining_chunk==0)
  229. {
  230. $debug=$this->debug;
  231. if(!$this->debug_response_body)
  232. $this->debug=0;
  233. $line=$this->GetLine();
  234. $this->debug=$debug;
  235. if(GetType($line)!="string")
  236. return($this->SetError("4 could not read chunk start: ".$this->error));
  237. $this->remaining_chunk=hexdec($line);
  238. }
  239. return("");
  240. }
  241. Function ReadBytes($length)
  242. {
  243. if($this->use_curl)
  244. {
  245. $bytes=substr($this->response,$this->read_response,min($length,strlen($this->response)-$this->read_response));
  246. $this->read_response+=strlen($bytes);
  247. if($this->debug
  248. && $this->debug_response_body
  249. && strlen($bytes))
  250. $this->OutputDebug("S ".$bytes);
  251. }
  252. else
  253. {
  254. if($this->chunked)
  255. {
  256. for($bytes="",$remaining=$length;$remaining;)
  257. {
  258. if(strlen($this->ReadChunkSize()))
  259. return("");
  260. if($this->remaining_chunk==0)
  261. {
  262. $this->last_chunk_read=1;
  263. break;
  264. }
  265. $ask=min($this->remaining_chunk,$remaining);
  266. $chunk=@fread($this->connection,$ask);
  267. $read=strlen($chunk);
  268. if($read==0)
  269. {
  270. $this->SetDataAccessError("it was not possible to read data chunk from the HTTP server");
  271. return("");
  272. }
  273. if($this->debug
  274. && $this->debug_response_body)
  275. $this->OutputDebug("S ".$chunk);
  276. $bytes.=$chunk;
  277. $this->remaining_chunk-=$read;
  278. $remaining-=$read;
  279. if($this->remaining_chunk==0)
  280. {
  281. if(feof($this->connection))
  282. return($this->SetError("reached the end of data while reading the end of data chunk mark from the HTTP server"));
  283. $data=@fread($this->connection,2);
  284. if(strcmp($data,"\r\n"))
  285. {
  286. $this->SetDataAccessError("it was not possible to read end of data chunk from the HTTP server");
  287. return("");
  288. }
  289. }
  290. }
  291. }
  292. else
  293. {
  294. $bytes=@fread($this->connection,$length);
  295. if(strlen($bytes))
  296. {
  297. if($this->debug
  298. && $this->debug_response_body)
  299. $this->OutputDebug("S ".$bytes);
  300. }
  301. else
  302. $this->SetDataAccessError("it was not possible to read data from the HTTP server", $this->connection_close);
  303. }
  304. }
  305. return($bytes);
  306. }
  307. Function EndOfInput()
  308. {
  309. if($this->use_curl)
  310. return($this->read_response>=strlen($this->response));
  311. if($this->chunked)
  312. return($this->last_chunk_read);
  313. return(feof($this->connection));
  314. }
  315. Function Resolve($domain, &$ip, $server_type)
  316. {
  317. if(preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/',$domain))
  318. $ip=$domain;
  319. else
  320. {
  321. if($this->debug)
  322. $this->OutputDebug('Resolving '.$server_type.' server domain "'.$domain.'"...');
  323. if(!strcmp($ip=@gethostbyname($domain),$domain))
  324. $ip="";
  325. }
  326. if(strlen($ip)==0
  327. || (strlen($this->exclude_address)
  328. && !strcmp(@gethostbyname($this->exclude_address),$ip)))
  329. return($this->SetError("could not resolve the host domain \"".$domain."\""));
  330. return('');
  331. }
  332. Function Connect($host_name, $host_port, $ssl, $server_type = 'HTTP')
  333. {
  334. $domain=$host_name;
  335. $port = $host_port;
  336. if(strlen($error = $this->Resolve($domain, $ip, $server_type)))
  337. return($error);
  338. if(strlen($this->socks_host_name))
  339. {
  340. switch($this->socks_version)
  341. {
  342. case '4':
  343. $version = 4;
  344. break;
  345. case '5':
  346. $version = 5;
  347. break;
  348. default:
  349. return('it was not specified a supported SOCKS protocol version');
  350. break;
  351. }
  352. $host_ip = $ip;
  353. $port = $this->socks_host_port;
  354. $host_server_type = $server_type;
  355. $server_type = 'SOCKS';
  356. if(strlen($error = $this->Resolve($this->socks_host_name, $ip, $server_type)))
  357. return($error);
  358. }
  359. if($this->debug)
  360. $this->OutputDebug('Connecting to '.$server_type.' server IP '.$ip.' port '.$port.'...');
  361. if($ssl)
  362. $ip="ssl://".$ip;
  363. if(($this->connection=($this->timeout ? @fsockopen($ip, $port, $errno, $error, $this->timeout) : @fsockopen($ip, $port, $errno)))==0)
  364. {
  365. switch($errno)
  366. {
  367. case -3:
  368. return($this->SetError("-3 socket could not be created"));
  369. case -4:
  370. return($this->SetError("-4 dns lookup on hostname \"".$host_name."\" failed"));
  371. case -5:
  372. return($this->SetError("-5 connection refused or timed out"));
  373. case -6:
  374. return($this->SetError("-6 fdopen() call failed"));
  375. case -7:
  376. return($this->SetError("-7 setvbuf() call failed"));
  377. default:
  378. return($this->SetPHPError($errno." could not connect to the host \"".$host_name."\"",$php_errormsg));
  379. }
  380. }
  381. else
  382. {
  383. if($this->data_timeout
  384. && function_exists("socket_set_timeout"))
  385. socket_set_timeout($this->connection,$this->data_timeout,0);
  386. if(strlen($this->socks_host_name))
  387. {
  388. if($this->debug)
  389. $this->OutputDebug('Connected to the SOCKS server '.$this->socks_host_name);
  390. $send_error = 'it was not possible to send data to the SOCKS server';
  391. $receive_error = 'it was not possible to receive data from the SOCKS server';
  392. switch($version)
  393. {
  394. case 4:
  395. $command = 1;
  396. if(!fputs($this->connection, chr($version).chr($command).pack('nN', $host_port, ip2long($host_ip)).$this->user.Chr(0)))
  397. $error = $this->SetDataAccessError($send_error);
  398. else
  399. {
  400. $response = fgets($this->connection, 9);
  401. if(strlen($response) != 8)
  402. $error = $this->SetDataAccessError($receive_error);
  403. else
  404. {
  405. $socks_errors = array(
  406. "\x5a"=>'',
  407. "\x5b"=>'request rejected',
  408. "\x5c"=>'request failed because client is not running identd (or not reachable from the server)',
  409. "\x5d"=>'request failed because client\'s identd could not confirm the user ID string in the request',
  410. );
  411. $error_code = $response[1];
  412. $error = (IsSet($socks_errors[$error_code]) ? $socks_errors[$error_code] : 'unknown');
  413. if(strlen($error))
  414. $error = 'SOCKS error: '.$error;
  415. }
  416. }
  417. break;
  418. case 5:
  419. if($this->debug)
  420. $this->OutputDebug('Negotiating the authentication method ...');
  421. $methods = 1;
  422. $method = 0;
  423. if(!fputs($this->connection, chr($version).chr($methods).chr($method)))
  424. $error = $this->SetDataAccessError($send_error);
  425. else
  426. {
  427. $response = fgets($this->connection, 3);
  428. if(strlen($response) != 2)
  429. $error = $this->SetDataAccessError($receive_error);
  430. elseif(Ord($response[1]) != $method)
  431. $error = 'the SOCKS server requires an authentication method that is not yet supported';
  432. else
  433. {
  434. if($this->debug)
  435. $this->OutputDebug('Connecting to '.$host_server_type.' server IP '.$host_ip.' port '.$host_port.'...');
  436. $command = 1;
  437. $address_type = 1;
  438. if(!fputs($this->connection, chr($version).chr($command)."\x00".chr($address_type).pack('Nn', ip2long($host_ip), $host_port)))
  439. $error = $this->SetDataAccessError($send_error);
  440. else
  441. {
  442. $response = fgets($this->connection, 11);
  443. if(strlen($response) != 10)
  444. $error = $this->SetDataAccessError($receive_error);
  445. else
  446. {
  447. $socks_errors = array(
  448. "\x00"=>'',
  449. "\x01"=>'general SOCKS server failure',
  450. "\x02"=>'connection not allowed by ruleset',
  451. "\x03"=>'Network unreachable',
  452. "\x04"=>'Host unreachable',
  453. "\x05"=>'Connection refused',
  454. "\x06"=>'TTL expired',
  455. "\x07"=>'Command not supported',
  456. "\x08"=>'Address type not supported'
  457. );
  458. $error_code = $response[1];
  459. $error = (IsSet($socks_errors[$error_code]) ? $socks_errors[$error_code] : 'unknown');
  460. if(strlen($error))
  461. $error = 'SOCKS error: '.$error;
  462. }
  463. }
  464. }
  465. }
  466. break;
  467. default:
  468. $error = 'support for SOCKS protocol version '.$this->socks_version.' is not yet implemented';
  469. break;
  470. }
  471. if(strlen($error))
  472. {
  473. fclose($this->connection);
  474. return($error);
  475. }
  476. }
  477. if($this->debug)
  478. $this->OutputDebug("Connected to $host_name");
  479. if(strlen($this->proxy_host_name)
  480. && !strcmp(strtolower($this->protocol), 'https'))
  481. {
  482. if(function_exists('stream_socket_enable_crypto')
  483. && in_array('ssl', stream_get_transports()))
  484. $this->state = "ConnectedToProxy";
  485. else
  486. {
  487. $this->OutputDebug("It is not possible to start SSL after connecting to the proxy server. If the proxy refuses to forward the SSL request, you may need to upgrade to PHP 5.1 or later with OpenSSL support enabled.");
  488. $this->state="Connected";
  489. }
  490. }
  491. else
  492. $this->state="Connected";
  493. return("");
  494. }
  495. }
  496. Function Disconnect()
  497. {
  498. if($this->debug)
  499. $this->OutputDebug("Disconnected from ".$this->host_name);
  500. if($this->use_curl)
  501. {
  502. curl_close($this->connection);
  503. $this->response="";
  504. }
  505. else
  506. fclose($this->connection);
  507. $this->state="Disconnected";
  508. return("");
  509. }
  510. /* Public methods */
  511. Function GetRequestArguments($url, &$arguments)
  512. {
  513. $this->error = '';
  514. $arguments=array();
  515. $url = str_replace(' ', '%20', $url);
  516. $parameters=@parse_url($url);
  517. if(!$parameters)
  518. return($this->SetError("it was not specified a valid URL"));
  519. if(!IsSet($parameters["scheme"]))
  520. return($this->SetError("it was not specified the protocol type argument"));
  521. switch(strtolower($parameters["scheme"]))
  522. {
  523. case "http":
  524. case "https":
  525. $arguments["Protocol"]=$parameters["scheme"];
  526. break;
  527. default:
  528. return($parameters["scheme"]." connection scheme is not yet supported");
  529. }
  530. if(!IsSet($parameters["host"]))
  531. return($this->SetError("it was not specified the connection host argument"));
  532. $arguments["HostName"]=$parameters["host"];
  533. $arguments["Headers"]=array("Host"=>$parameters["host"].(IsSet($parameters["port"]) ? ":".$parameters["port"] : ""));
  534. if(IsSet($parameters["user"]))
  535. {
  536. $arguments["AuthUser"]=UrlDecode($parameters["user"]);
  537. if(!IsSet($parameters["pass"]))
  538. $arguments["AuthPassword"]="";
  539. }
  540. if(IsSet($parameters["pass"]))
  541. {
  542. if(!IsSet($parameters["user"]))
  543. $arguments["AuthUser"]="";
  544. $arguments["AuthPassword"]=UrlDecode($parameters["pass"]);
  545. }
  546. if(IsSet($parameters["port"]))
  547. {
  548. if(strcmp($parameters["port"],strval(intval($parameters["port"]))))
  549. return($this->SetError("it was not specified a valid connection host argument"));
  550. $arguments["HostPort"]=intval($parameters["port"]);
  551. }
  552. else
  553. $arguments["HostPort"]=0;
  554. $arguments["RequestURI"]=(IsSet($parameters["path"]) ? $parameters["path"] : "/").(IsSet($parameters["query"]) ? "?".$parameters["query"] : "");
  555. if(strlen($this->user_agent))
  556. $arguments["Headers"]["User-Agent"]=$this->user_agent;
  557. return("");
  558. }
  559. Function Open($arguments)
  560. {
  561. if(strlen($this->error))
  562. return($this->error);
  563. if($this->state!="Disconnected")
  564. return("1 already connected");
  565. if(IsSet($arguments["HostName"]))
  566. $this->host_name=$arguments["HostName"];
  567. if(IsSet($arguments["HostPort"]))
  568. $this->host_port=$arguments["HostPort"];
  569. if(IsSet($arguments["ProxyHostName"]))
  570. $this->proxy_host_name=$arguments["ProxyHostName"];
  571. if(IsSet($arguments["ProxyHostPort"]))
  572. $this->proxy_host_port=$arguments["ProxyHostPort"];
  573. if(IsSet($arguments["SOCKSHostName"]))
  574. $this->socks_host_name=$arguments["SOCKSHostName"];
  575. if(IsSet($arguments["SOCKSHostPort"]))
  576. $this->socks_host_port=$arguments["SOCKSHostPort"];
  577. if(IsSet($arguments["SOCKSVersion"]))
  578. $this->socks_version=$arguments["SOCKSVersion"];
  579. if(IsSet($arguments["Protocol"]))
  580. $this->protocol=$arguments["Protocol"];
  581. switch(strtolower($this->protocol))
  582. {
  583. case "http":
  584. $default_port=80;
  585. break;
  586. case "https":
  587. $default_port=443;
  588. break;
  589. default:
  590. return($this->SetError("2 it was not specified a valid connection protocol"));
  591. }
  592. if(strlen($this->proxy_host_name)==0)
  593. {
  594. if(strlen($this->host_name)==0)
  595. return($this->SetError("2 it was not specified a valid hostname"));
  596. $host_name=$this->host_name;
  597. $host_port=($this->host_port ? $this->host_port : $default_port);
  598. $server_type = 'HTTP';
  599. }
  600. else
  601. {
  602. $host_name=$this->proxy_host_name;
  603. $host_port=$this->proxy_host_port;
  604. $server_type = 'HTTP proxy';
  605. }
  606. $ssl=(strtolower($this->protocol)=="https" && strlen($this->proxy_host_name)==0);
  607. if($ssl
  608. && strlen($this->socks_host_name))
  609. return($this->SetError('establishing SSL connections via a SOCKS server is not yet supported'));
  610. $this->use_curl=($ssl && $this->prefer_curl && function_exists("curl_init"));
  611. if($this->debug)
  612. $this->OutputDebug("Connecting to ".$this->host_name);
  613. if($this->use_curl)
  614. {
  615. $error=(($this->connection=curl_init($this->protocol."://".$this->host_name.($host_port==$default_port ? "" : ":".strval($host_port))."/")) ? "" : "Could not initialize a CURL session");
  616. if(strlen($error)==0)
  617. {
  618. if(IsSet($arguments["SSLCertificateFile"]))
  619. curl_setopt($this->connection,CURLOPT_SSLCERT,$arguments["SSLCertificateFile"]);
  620. if(IsSet($arguments["SSLCertificatePassword"]))
  621. curl_setopt($this->connection,CURLOPT_SSLCERTPASSWD,$arguments["SSLCertificatePassword"]);
  622. if(IsSet($arguments["SSLKeyFile"]))
  623. curl_setopt($this->connection,CURLOPT_SSLKEY,$arguments["SSLKeyFile"]);
  624. if(IsSet($arguments["SSLKeyPassword"]))
  625. curl_setopt($this->connection,CURLOPT_SSLKEYPASSWD,$arguments["SSLKeyPassword"]);
  626. }
  627. $this->state="Connected";
  628. }
  629. else
  630. {
  631. $error="";
  632. if(strlen($this->proxy_host_name)
  633. && (IsSet($arguments["SSLCertificateFile"])
  634. || IsSet($arguments["SSLCertificateFile"])))
  635. $error="establishing SSL connections using certificates or private keys via non-SSL proxies is not supported";
  636. else
  637. {
  638. if($ssl)
  639. {
  640. if(IsSet($arguments["SSLCertificateFile"]))
  641. $error="establishing SSL connections using certificates is only supported when the cURL extension is enabled";
  642. elseif(IsSet($arguments["SSLKeyFile"]))
  643. $error="establishing SSL connections using a private key is only supported when the cURL extension is enabled";
  644. else
  645. {
  646. $version=explode(".",function_exists("phpversion") ? phpversion() : "3.0.7");
  647. $php_version=intval($version[0])*1000000+intval($version[1])*1000+intval($version[2]);
  648. if($php_version<4003000)
  649. $error="establishing SSL connections requires at least PHP version 4.3.0 or having the cURL extension enabled";
  650. elseif(!function_exists("extension_loaded")
  651. || !extension_loaded("openssl"))
  652. $error="establishing SSL connections requires the OpenSSL extension enabled";
  653. }
  654. }
  655. if(strlen($error)==0)
  656. $error=$this->Connect($host_name, $host_port, $ssl, $server_type);
  657. }
  658. }
  659. if(strlen($error))
  660. return($this->SetError($error));
  661. $this->session=md5(uniqid(""));
  662. return("");
  663. }
  664. Function Close()
  665. {
  666. if($this->state=="Disconnected")
  667. return("1 already disconnected");
  668. $error=$this->Disconnect();
  669. if(strlen($error)==0)
  670. $this->state="Disconnected";
  671. return($error);
  672. }
  673. Function PickCookies(&$cookies,$secure)
  674. {
  675. if(IsSet($this->cookies[$secure]))
  676. {
  677. $now=gmdate("Y-m-d H-i-s");
  678. for($domain=0,Reset($this->cookies[$secure]);$domain<count($this->cookies[$secure]);Next($this->cookies[$secure]),$domain++)
  679. {
  680. $domain_pattern=Key($this->cookies[$secure]);
  681. $match=strlen($this->request_host)-strlen($domain_pattern);
  682. if($match>=0
  683. && !strcmp($domain_pattern,substr($this->request_host,$match))
  684. && ($match==0
  685. || $domain_pattern[0]=="."
  686. || $this->request_host[$match-1]=="."))
  687. {
  688. for(Reset($this->cookies[$secure][$domain_pattern]),$path_part=0;$path_part<count($this->cookies[$secure][$domain_pattern]);Next($this->cookies[$secure][$domain_pattern]),$path_part++)
  689. {
  690. $path=Key($this->cookies[$secure][$domain_pattern]);
  691. if(strlen($this->request_uri)>=strlen($path)
  692. && substr($this->request_uri,0,strlen($path))==$path)
  693. {
  694. for(Reset($this->cookies[$secure][$domain_pattern][$path]),$cookie=0;$cookie<count($this->cookies[$secure][$domain_pattern][$path]);Next($this->cookies[$secure][$domain_pattern][$path]),$cookie++)
  695. {
  696. $cookie_name=Key($this->cookies[$secure][$domain_pattern][$path]);
  697. $expires=$this->cookies[$secure][$domain_pattern][$path][$cookie_name]["expires"];
  698. if($expires==""
  699. || strcmp($now,$expires)<0)
  700. $cookies[$cookie_name]=$this->cookies[$secure][$domain_pattern][$path][$cookie_name];
  701. }
  702. }
  703. }
  704. }
  705. }
  706. }
  707. }
  708. Function GetFileDefinition($file, &$definition)
  709. {
  710. $name="";
  711. if(IsSet($file["FileName"]))
  712. $name=basename($file["FileName"]);
  713. if(IsSet($file["Name"]))
  714. $name=$file["Name"];
  715. if(strlen($name)==0)
  716. return("it was not specified the file part name");
  717. if(IsSet($file["Content-Type"]))
  718. {
  719. $content_type=$file["Content-Type"];
  720. $type=$this->Tokenize(strtolower($content_type),"/");
  721. $sub_type=$this->Tokenize("");
  722. switch($type)
  723. {
  724. case "text":
  725. case "image":
  726. case "audio":
  727. case "video":
  728. case "application":
  729. case "message":
  730. break;
  731. case "automatic":
  732. switch($sub_type)
  733. {
  734. case "name":
  735. switch(GetType($dot=strrpos($name,"."))=="integer" ? strtolower(substr($name,$dot)) : "")
  736. {
  737. case ".xls":
  738. $content_type="application/excel";
  739. break;
  740. case ".hqx":
  741. $content_type="application/macbinhex40";
  742. break;
  743. case ".doc":
  744. case ".dot":
  745. case ".wrd":
  746. $content_type="application/msword";
  747. break;
  748. case ".pdf":
  749. $content_type="application/pdf";
  750. break;
  751. case ".pgp":
  752. $content_type="application/pgp";
  753. break;
  754. case ".ps":
  755. case ".eps":
  756. case ".ai":
  757. $content_type="application/postscript";
  758. break;
  759. case ".ppt":
  760. $content_type="application/powerpoint";
  761. break;
  762. case ".rtf":
  763. $content_type="application/rtf";
  764. break;
  765. case ".tgz":
  766. case ".gtar":
  767. $content_type="application/x-gtar";
  768. break;
  769. case ".gz":
  770. $content_type="application/x-gzip";
  771. break;
  772. case ".php":
  773. case ".php3":
  774. $content_type="application/x-httpd-php";
  775. break;
  776. case ".js":
  777. $content_type="application/x-javascript";
  778. break;
  779. case ".ppd":
  780. case ".psd":
  781. $content_type="application/x-photoshop";
  782. break;
  783. case ".swf":
  784. case ".swc":
  785. case ".rf":
  786. $content_type="application/x-shockwave-flash";
  787. break;
  788. case ".tar":
  789. $content_type="application/x-tar";
  790. break;
  791. case ".zip":
  792. $content_type="application/zip";
  793. break;
  794. case ".mid":
  795. case ".midi":
  796. case ".kar":
  797. $content_type="audio/midi";
  798. break;
  799. case ".mp2":
  800. case ".mp3":
  801. case ".mpga":
  802. $content_type="audio/mpeg";
  803. break;
  804. case ".ra":
  805. $content_type="audio/x-realaudio";
  806. break;
  807. case ".wav":
  808. $content_type="audio/wav";
  809. break;
  810. case ".bmp":
  811. $content_type="image/bitmap";
  812. break;
  813. case ".gif":
  814. $content_type="image/gif";
  815. break;
  816. case ".iff":
  817. $content_type="image/iff";
  818. break;
  819. case ".jb2":
  820. $content_type="image/jb2";
  821. break;
  822. case ".jpg":
  823. case ".jpe":
  824. case ".jpeg":
  825. $content_type="image/jpeg";
  826. break;
  827. case ".jpx":
  828. $content_type="image/jpx";
  829. break;
  830. case ".png":
  831. $content_type="image/png";
  832. break;
  833. case ".tif":
  834. case ".tiff":
  835. $content_type="image/tiff";
  836. break;
  837. case ".wbmp":
  838. $content_type="image/vnd.wap.wbmp";
  839. break;
  840. case ".xbm":
  841. $content_type="image/xbm";
  842. break;
  843. case ".css":
  844. $content_type="text/css";
  845. break;
  846. case ".txt":
  847. $content_type="text/plain";
  848. break;
  849. case ".htm":
  850. case ".html":
  851. $content_type="text/html";
  852. break;
  853. case ".xml":
  854. $content_type="text/xml";
  855. break;
  856. case ".mpg":
  857. case ".mpe":
  858. case ".mpeg":
  859. $content_type="video/mpeg";
  860. break;
  861. case ".qt":
  862. case ".mov":
  863. $content_type="video/quicktime";
  864. break;
  865. case ".avi":
  866. $content_type="video/x-ms-video";
  867. break;
  868. case ".eml":
  869. $content_type="message/rfc822";
  870. break;
  871. default:
  872. $content_type="application/octet-stream";
  873. break;
  874. }
  875. break;
  876. default:
  877. return($content_type." is not a supported automatic content type detection method");
  878. }
  879. break;
  880. default:
  881. return($content_type." is not a supported file content type");
  882. }
  883. }
  884. else
  885. $content_type="application/octet-stream";
  886. $definition=array(
  887. "Content-Type"=>$content_type,
  888. "NAME"=>$name
  889. );
  890. if(IsSet($file["FileName"]))
  891. {
  892. if(GetType($length=@filesize($file["FileName"]))!="integer")
  893. {
  894. $error="it was not possible to determine the length of the file ".$file["FileName"];
  895. if(IsSet($php_errormsg)
  896. && strlen($php_errormsg))
  897. $error.=": ".$php_errormsg;
  898. if(!file_exists($file["FileName"]))
  899. $error="it was not possible to access the file ".$file["FileName"];
  900. return($error);
  901. }
  902. $definition["FILENAME"]=$file["FileName"];
  903. $definition["Content-Length"]=$length;
  904. }
  905. elseif(IsSet($file["Data"]))
  906. $definition["Content-Length"]=strlen($definition["DATA"]=$file["Data"]);
  907. else
  908. return("it was not specified a valid file name");
  909. return("");
  910. }
  911. Function ConnectFromProxy($arguments, &$headers)
  912. {
  913. if(!$this->PutLine('CONNECT '.$this->host_name.':'.($this->host_port ? $this->host_port : 443).' HTTP/1.0')
  914. || (strlen($this->user_agent)
  915. && !$this->PutLine('User-Agent: '.$this->user_agent))
  916. || (IsSet($arguments['Headers']['Proxy-Authorization'])
  917. && !$this->PutLine('Proxy-Authorization: '.$arguments['Headers']['Proxy-Authorization']))
  918. || !$this->PutLine(''))
  919. {
  920. $this->Disconnect();
  921. return($this->error);
  922. }
  923. $this->state = "ConnectSent";
  924. if(strlen($error=$this->ReadReplyHeadersResponse($headers)))
  925. return($error);
  926. $proxy_authorization="";
  927. while(!strcmp($this->response_status, "100"))
  928. {
  929. $this->state="ConnectSent";
  930. if(strlen($error=$this->ReadReplyHeadersResponse($headers)))
  931. return($error);
  932. }
  933. switch($this->response_status)
  934. {
  935. case "200":
  936. if(!@stream_socket_enable_crypto($this->connection, 1, STREAM_CRYPTO_METHOD_SSLv23_CLIENT))
  937. {
  938. $this->SetPHPError('it was not possible to start a SSL encrypted connection via this proxy', $php_errormsg);
  939. $this->Disconnect();
  940. return($this->error);
  941. }
  942. $this->state = "Connected";
  943. break;
  944. case "407":
  945. if(strlen($error=$this->Authenticate($headers, -1, $proxy_authorization, $this->proxy_request_user, $this->proxy_request_password, $this->proxy_request_realm, $this->proxy_request_workstation)))
  946. return($error);
  947. break;
  948. default:
  949. return($this->SetError("unable to send request via proxy"));
  950. }
  951. return("");
  952. }
  953. Function SendRequest($arguments)
  954. {
  955. if(strlen($this->error))
  956. return($this->error);
  957. if(IsSet($arguments["ProxyUser"]))
  958. $this->proxy_request_user=$arguments["ProxyUser"];
  959. elseif(IsSet($this->proxy_user))
  960. $this->proxy_request_user=$this->proxy_user;
  961. if(IsSet($arguments["ProxyPassword"]))
  962. $this->proxy_request_password=$arguments["ProxyPassword"];
  963. elseif(IsSet($this->proxy_password))
  964. $this->proxy_request_password=$this->proxy_password;
  965. if(IsSet($arguments["ProxyRealm"]))
  966. $this->proxy_request_realm=$arguments["ProxyRealm"];
  967. elseif(IsSet($this->proxy_realm))
  968. $this->proxy_request_realm=$this->proxy_realm;
  969. if(IsSet($arguments["ProxyWorkstation"]))
  970. $this->proxy_request_workstation=$arguments["ProxyWorkstation"];
  971. elseif(IsSet($this->proxy_workstation))
  972. $this->proxy_request_workstation=$this->proxy_workstation;
  973. switch($this->state)
  974. {
  975. case "Disconnected":
  976. return($this->SetError("1 connection was not yet established"));
  977. case "Connected":
  978. $connect = 0;
  979. break;
  980. case "ConnectedToProxy":
  981. if(strlen($error = $this->ConnectFromProxy($arguments, $headers)))
  982. return($error);
  983. $connect = 1;
  984. break;
  985. default:
  986. return($this->SetError("2 can not send request in the current connection state"));
  987. }
  988. if(IsSet($arguments["RequestMethod"]))
  989. $this->request_method=$arguments["RequestMethod"];
  990. if(IsSet($arguments["User-Agent"]))
  991. $this->user_agent=$arguments["User-Agent"];
  992. if(!IsSet($arguments["Headers"]["User-Agent"])
  993. && strlen($this->user_agent))
  994. $arguments["Headers"]["User-Agent"]=$this->user_agent;
  995. if(strlen($this->request_method)==0)
  996. return($this->SetError("3 it was not specified a valid request method"));
  997. if(IsSet($arguments["RequestURI"]))
  998. $this->request_uri=$arguments["RequestURI"];
  999. if(strlen($this->request_uri)==0
  1000. || substr($this->request_uri,0,1)!="/")
  1001. return($this->SetError("4 it was not specified a valid request URI"));
  1002. $this->request_arguments=$arguments;
  1003. $this->request_headers=(IsSet($arguments["Headers"]) ? $arguments["Headers"] : array());
  1004. $body_length=0;
  1005. $this->request_body="";
  1006. $get_body=1;
  1007. if($this->request_method=="POST"
  1008. || $this->request_method=="PUT")
  1009. {
  1010. if(IsSet($arguments['StreamRequest']))
  1011. {
  1012. $get_body = 0;
  1013. $this->request_headers["Transfer-Encoding"]="chunked";
  1014. }
  1015. elseif(IsSet($arguments["PostFiles"])
  1016. || ($this->force_multipart_form_post
  1017. && IsSet($arguments["PostValues"])))
  1018. {
  1019. $boundary="--".md5(uniqid(time()));
  1020. $this->request_headers["Content-Type"]="multipart/form-data; boundary=".$boundary.(IsSet($arguments["CharSet"]) ? "; charset=".$arguments["CharSet"] : "");
  1021. $post_parts=array();
  1022. if(IsSet($arguments["PostValues"]))
  1023. {
  1024. $values=$arguments["PostValues"];
  1025. if(GetType($values)!="array")
  1026. return($this->SetError("5 it was not specified a valid POST method values array"));
  1027. for(Reset($values),$value=0;$value<count($values);Next($values),$value++)
  1028. {
  1029. $input=Key($values);
  1030. $headers="--".$boundary."\r\nContent-Disposition: form-data; name=\"".$input."\"\r\n\r\n";
  1031. $data=$values[$input];
  1032. $post_parts[]=array("HEADERS"=>$headers,"DATA"=>$data);
  1033. $body_length+=strlen($headers)+strlen($data)+strlen("\r\n");
  1034. }
  1035. }
  1036. $body_length+=strlen("--".$boundary."--\r\n");
  1037. $files=(IsSet($arguments["PostFiles"]) ? $arguments["PostFiles"] : array());
  1038. Reset($files);
  1039. $end=(GetType($input=Key($files))!="string");
  1040. for(;!$end;)
  1041. {
  1042. if(strlen($error=$this->GetFileDefinition($files[$input],$definition)))
  1043. return("3 ".$error);
  1044. $headers="--".$boundary."\r\nContent-Disposition: form-data; name=\"".$input."\"; filename=\"".$definition["NAME"]."\"\r\nContent-Type: ".$definition["Content-Type"]."\r\n\r\n";
  1045. $part=count($post_parts);
  1046. $post_parts[$part]=array("HEADERS"=>$headers);
  1047. if(IsSet($definition["FILENAME"]))
  1048. {
  1049. $post_parts[$part]["FILENAME"]=$definition["FILENAME"];
  1050. $data="";
  1051. }
  1052. else
  1053. $data=$definition["DATA"];
  1054. $post_parts[$part]["DATA"]=$data;
  1055. $body_length+=strlen($headers)+$definition["Content-Length"]+strlen("\r\n");
  1056. Next($files);
  1057. $end=(GetType($input=Key($files))!="string");
  1058. }
  1059. $get_body=0;
  1060. }
  1061. elseif(IsSet($arguments["PostValues"]))
  1062. {
  1063. $values=$arguments["PostValues"];
  1064. if(GetType($values)!="array")
  1065. return($this->SetError("5 it was not specified a valid POST method values array"));
  1066. for(Reset($values),$value=0;$value<count($values);Next($values),$value++)
  1067. {
  1068. $k=Key($values);
  1069. if(GetType($values[$k])=="array")
  1070. {
  1071. for($v = 0; $v < count($values[$k]); $v++)
  1072. {
  1073. if($value+$v>0)
  1074. $this->request_body.="&";
  1075. $this->request_body.=UrlEncode($k)."=".UrlEncode($values[$k][$v]);
  1076. }
  1077. }
  1078. else
  1079. {
  1080. if($value>0)
  1081. $this->request_body.="&";
  1082. $this->request_body.=UrlEncode($k)."=".UrlEncode($values[$k]);
  1083. }
  1084. }
  1085. $this->request_headers["Content-Type"]="application/x-www-form-urlencoded".(IsSet($arguments["CharSet"]) ? "; charset=".$arguments["CharSet"] : "");
  1086. $get_body=0;
  1087. }
  1088. }
  1089. if($get_body
  1090. && (IsSet($arguments["Body"])
  1091. || IsSet($arguments["BodyStream"])))
  1092. {
  1093. if(IsSet($arguments["Body"]))
  1094. $this->request_body=$arguments["Body"];
  1095. else
  1096. {
  1097. $stream=$arguments["BodyStream"];
  1098. $this->request_body="";
  1099. for($part=0; $part<count($stream); $part++)
  1100. {
  1101. if(IsSet($stream[$part]["Data"]))
  1102. $this->request_body.=$stream[$part]["Data"];
  1103. elseif(IsSet($stream[$part]["File"]))
  1104. {
  1105. if(!($file=@fopen($stream[$part]["File"],"rb")))
  1106. return($this->SetPHPError("could not open upload file ".$stream[$part]["File"], $php_errormsg));
  1107. while(!feof($file))
  1108. {
  1109. if(GetType($block=@fread($file,$this->file_buffer_length))!="string")
  1110. {
  1111. $error=$this->SetPHPError("could not read body stream file ".$stream[$part]["File"], $php_errormsg);
  1112. fclose($file);
  1113. return($error);
  1114. }
  1115. $this->request_body.=$block;
  1116. }
  1117. fclose($file);
  1118. }
  1119. else
  1120. return("5 it was not specified a valid file or data body stream element at position ".$part);
  1121. }
  1122. }
  1123. if(!IsSet($this->request_headers["Content-Type"]))
  1124. $this->request_headers["Content-Type"]="application/octet-stream".(IsSet($arguments["CharSet"]) ? "; charset=".$arguments["CharSet"] : "");
  1125. }
  1126. if(IsSet($arguments["AuthUser"]))
  1127. $this->request_user=$arguments["AuthUser"];
  1128. elseif(IsSet($this->user))
  1129. $this->request_user=$this->user;
  1130. if(IsSet($arguments["AuthPassword"]))
  1131. $this->request_password=$arguments["AuthPassword"];
  1132. elseif(IsSet($this->password))
  1133. $this->request_password=$this->password;
  1134. if(IsSet($arguments["AuthRealm"]))
  1135. $this->request_realm=$arguments["AuthRealm"];
  1136. elseif(IsSet($this->realm))
  1137. $this->request_realm=$this->realm;
  1138. if(IsSet($arguments["AuthWorkstation"]))
  1139. $this->request_workstation=$arguments["AuthWorkstation"];
  1140. elseif(IsSet($this->workstation))
  1141. $this->request_workstation=$this->workstation;
  1142. if(strlen($this->proxy_host_name)==0
  1143. || $connect)
  1144. $request_uri=$this->request_uri;
  1145. else
  1146. {
  1147. switch(strtolower($this->protocol))
  1148. {
  1149. case "http":
  1150. $default_port=80;
  1151. break;
  1152. case "https":
  1153. $default_port=443;
  1154. break;
  1155. }
  1156. $request_uri=strtolower($this->protocol)."://".$this->host_name.(($this->host_port==0 || $this->host_port==$default_port) ? "" : ":".$this->host_port).$this->request_uri;
  1157. }
  1158. if($this->use_curl)
  1159. {
  1160. $version=(GetType($v=curl_version())=="array" ? (IsSet($v["version"]) ? $v["version"] : "0.0.0") : (preg_match("/^libcurl\\/([0-9]+\\.[0-9]+\\.[0-9]+)/",$v,$m) ? $m[1] : "0.0.0"));
  1161. $curl_version=100000*intval($this->Tokenize($version,"."))+1000*intval($this->Tokenize("."))+intval($this->Tokenize(""));
  1162. $protocol_version=($curl_version<713002 ? "1.0" : $this->protocol_version);
  1163. }
  1164. else
  1165. $protocol_version=$this->protocol_version;
  1166. $this->request=$this->request_method." ".$request_uri." HTTP/".$protocol_version;
  1167. if($body_length
  1168. || ($body_length=strlen($this->request_body)))
  1169. $this->request_headers["Content-Length"]=$body_length;
  1170. for($headers=array(),$host_set=0,Reset($this->request_headers),$header=0;$header<count($this->request_headers);Next($this->request_headers),$header++)
  1171. {
  1172. $header_name=Key($this->req

Large files files are truncated, but you can click here to view the full file