PageRenderTime 70ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/HTTPQuery.php

https://bitbucket.org/0xDB/text2speach
PHP | 922 lines | 481 code | 134 blank | 307 comment | 139 complexity | 689545d92b434bc7748248cec54cbef1 MD5 | raw file
  1. <?php
  2. /**
  3. * @name: HTTPQuery.php
  4. * @description: Thing to work with HTTP protocol
  5. * @requires: WhoIS class by NULL_byte >= ver 1.1
  6. * @author: NULL_byte
  7. * @contacts: www.null-byte.info
  8. * @version: 2.1
  9. */
  10. # http://ru.php.net/manual/en/ref.url.php
  11. # TODO: refactor HTTPSocket class using <http://ru.php.net/manual/en/ref.http.php>
  12. # & discover <http://ru.php.net/manual/en/class.httpresponse.php>
  13. # The better HTTP Client interface will always work via HTTPQuery class
  14. # usage example:
  15. # $q = new HTTPQuery;
  16. # $q->Get('http://www.yandex.ru/');
  17. # Import WhoIS class
  18. if (!class_exists('WhoIS'))
  19. @require_once dirname(__FILE__).'/WhoIS.php';
  20. if (!class_exists('WhoIS'))
  21. die("The required class `WhoIS` is not found!");
  22. abstract class HTTPClient extends WhoIS {
  23. # System operated variables
  24. public $URI = 'http://localhost/';
  25. public $Scheme = 'http';
  26. public $Host = null;
  27. public $IDNA = null; # Punycode
  28. public $Port = '80';
  29. public $Path = null;
  30. public $Method = 'get';
  31. public $Query = null;
  32. public $Cookies = null;
  33. public $Engine = null; # 'cURL' or 'Sockets'
  34. # Toggles
  35. public $Proxy = false; # Proxy to use. Format: Array('proxy_host', 'port') or false or 'proxy:port', yeah
  36. #public $UserAgent = 'Opera/8.01 (J2ME/MIDP; Opera Mini/2.0.4719; en; U; ssr)';
  37. public $UserAgent = false;
  38. public $Referer = null;
  39. public $AutoCookies = true;
  40. public $AutoReferer = true;
  41. public $AutoRedirect = true; # TODO: fix header('Location: /redirect.html'); # Fixed?
  42. public $FlushCookies = false; # WTF?! will flush cookies after each request if $this->AutoCookies = false;
  43. public $Verbose = false; # If true, would print many interesting messages
  44. public $Timeout = 30; # Request timeout
  45. # Output
  46. public $Result = null; # Will contain result with page headers
  47. public $ResultClean = null; # Will contain result without page headers
  48. # Headers
  49. public $HTTPHeaders = Array(); # Example: $this->HTTPHeaders = Array('Location: /newpage.html', ...);
  50. public $PageHeaders = Array(); # Example: $this->PageHeaders['location'];
  51. # Aliases
  52. public $ResponseHeaders = Array(); # alias of $this->PageHeaders
  53. public $Headers = Array(); # alias of $this->PageHeaders
  54. public $RequestHeaders = Array(); # alias of $this->HTTPHeaders
  55. public $RawHeaders = Array(); # alias of $this->HTTPHeaders
  56. public $maxRedirects = 20;
  57. public $maxRedirects_i = 0;
  58. public $Redirect_i = 0; # should be reset manualy... (updates only on request!)
  59. #private $gotCookies_ = null;
  60. public $gotCookies_ = null;
  61. public $gotCookies__ = null;
  62. abstract public function Get($url);
  63. abstract public function Post($url);
  64. #abstract public function GetCookies();
  65. abstract protected function SendRequest();
  66. abstract protected function ConstructQuery();
  67. public function __construct ($dump_engine = false) {
  68. $this->Engine = get_parent_class($this);
  69. #if (strtolower(substr($this->Engine, 0, 4)) == 'http')
  70. # $this->Engine = substr($this->Engine, 4);
  71. if ($dump_engine)
  72. echo " # `".get_class($this)."` currently uses `<{$this->Engine}>` as HTTP engine\r\n";
  73. }
  74. public function rndUserAgent() { # needs to be improved
  75. if(mt_rand(0,1)) $this->UserAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)';
  76. if(mt_rand(0,1)) $this->UserAgent = 'Mozilla/5.0 (compatible; Opera 9.80; Windows NT 6)';
  77. if(mt_rand(0,1)) $this->UserAgent = 'Safari Mozilla 4.6';
  78. if(mt_rand(0,1)) $this->UserAgent = 'Mozilla/3.0 Googlebot';
  79. if(mt_rand(0,1)) $this->UserAgent = 'Mozilla/5.0 (X11; U; Linux x86_64;) Gecko Ubuntu/9.04 (jaunty) Shiretoko/3.5';
  80. if(mt_rand(0,1)) $this->UserAgent = 'Mozilla/4.61 [en] (OS/2; U)';
  81. if(mt_rand(0,1)) $this->UserAgent = 'Mozilla/4.8 [en] (Windows NT 5.0; U)';
  82. if(mt_rand(0,1)) $this->UserAgent = 'Opera/8.0 (X11; Linux i686; U; cs)';
  83. if(mt_rand(0,1)) $this->UserAgent = 'Opera/10.00 (Windows NT 6.0; U; en) Presto/2.2.0';
  84. #if(mt_rand(0,1)) $this->UserAgent = 'Opera/8.01 (J2ME/MIDP; Opera Mini/2.0.4719; en; U; ssr)';
  85. }
  86. protected function MakeAliases() {
  87. if (!$this->PageHeaders && $this->ResponseHeaders) $this->PageHeaders = $this->ResponseHeaders;
  88. if (!$this->PageHeaders && $this->Headers) $this->PageHeaders = $this->Headers;
  89. if (!$this->HTTPHeaders && $this->RequestHeaders) $this->HTTPHeaders = $this->RequestHeaders;
  90. if (!$this->HTTPHeaders && $this->RawHeaders) $this->HTTPHeaders = $this->RawHeaders;
  91. }
  92. protected function PreConstructQuery() {
  93. $this->getUrlParts();
  94. list($this->IDNA, $this->Host) = $this->IDNA($this->Host);
  95. if ($this->IDNA) $this->Host = $this->IDNA;
  96. }
  97. protected function getUrlParts($url = false) {
  98. if (!$url && !$this->URI) return false;
  99. if (!$url) $url = $this->URI;
  100. $url_parts = parse_url($url);
  101. if (!isset($url_parts['scheme'])) $url_parts['scheme'] = 'http';
  102. if (!isset($url_parts['port']))
  103. $url_parts['port'] = ($url_parts['scheme'] == 'https') ? 443 : 80;
  104. if (!isset($url_parts['path'])) $url_parts['path'] = null;
  105. if (!isset($url_parts['query']))
  106. $url_parts['query'] = null;
  107. else
  108. $url_parts['query'] = "?{$url_parts['query']}";
  109. $this->Scheme = strtolower($url_parts['scheme']);
  110. $this->Host = strtolower($url_parts['host']);
  111. $this->Port = $url_parts['port'];
  112. $this->Path = $url_parts['path'];
  113. $this->Query = $url_parts['query'];
  114. return $url_parts;
  115. }
  116. protected function ParseHeaders() {
  117. if (!$this->Result) return false;
  118. $server_response = explode("\r\n\r\n", $this->Result);
  119. $server_response = $server_response[0];
  120. $server_response = str_replace("\r", '', $server_response);
  121. $server_response = explode("\n", $server_response);
  122. $this->ResponseStatus = $server_response[0];
  123. preg_match('#\d{3}#', $this->ResponseStatus, $this->ResponseCode);
  124. if (isset($this->ResponseCode[0]))
  125. $this->ResponseCode = $this->ResponseCode[0];
  126. $result = Array();
  127. foreach($server_response as $mixed_headers) {
  128. if ((strpos($mixed_headers, ': ') >= 0)) {
  129. $header = explode(': ', trim($mixed_headers));
  130. $result[$header[0]] = isset($header[1]) ? $header[1] : NULL;
  131. $result[strtolower($header[0])] = isset($header[1]) ? $header[1] : NULL;
  132. }
  133. }
  134. $this->ResponseHeaders = $result;
  135. $this->PageHeaders = $result;
  136. $this->Headers = $result;
  137. return $result;
  138. }
  139. protected function CookiesToString($cookie = false) {
  140. if (!$cookie) $cookie = $this->Cookies;
  141. if (!$cookie) return $cookie;
  142. if (is_string($cookie)) return $cookie;
  143. #if (!is_array($cookie)) var_dump($cookie);
  144. $cookie_string = '';
  145. foreach ($cookie as $key => $value)
  146. $cookie_string .= " {$key}={$value};";
  147. $cookie = trim($cookie_string, '; ');
  148. return $cookie;
  149. }
  150. protected function CookiesToArray($cookie = false) {
  151. if (!$cookie) $cookie = $this->Cookies;
  152. if (is_array($cookie)) return $cookie;
  153. $cookie = explode('; ', $cookie);
  154. $cookie_array = Array();
  155. for ($i = 0; $i < count($cookie); $i++) {
  156. $c = explode('=', $cookie[$i]);
  157. $cookie_array[$c[0]] = $c[1];
  158. }
  159. return $cookie_array;
  160. }
  161. public function GetCookies($as_string = false) {
  162. if (!$this->Result && $as_string) return null;
  163. if (!$this->Result) return Array();
  164. $data = explode("\r\n\r\n", $this->Result);
  165. $data = $data[0];
  166. $data = str_replace("\r", '', $data);
  167. $data = explode("\n", $data);
  168. $raw_cookies = Array();
  169. foreach ($data as $d) {
  170. $d = explode(' ', $d);
  171. if (strtolower($d[0]) == 'set-cookie:') {
  172. $raw_cookies[] = $d[1];
  173. }
  174. }
  175. $cookies = Array();
  176. foreach ($raw_cookies as $cookie) {
  177. $cookie_name = substr($cookie, 0, strpos($cookie, '='));
  178. $cookie_value = substr($cookie, strpos($cookie, '=')+1, strpos($cookie, ';')-2);
  179. $cookies[$cookie_name] = trim($cookie_value, '; ');
  180. }
  181. if ($as_string)
  182. $cookies = $this->CookiesToString($cookies);
  183. #$this->Cookies = $cookies;
  184. return $cookies;
  185. }
  186. public function ClearCookies() {
  187. $this->Cookies = null;
  188. }
  189. # Pa-paa-ram!
  190. # Checks if string is UTF-8 encoded
  191. function is_utf8($string, $utf8_split = 5000) { // v1.01
  192. if (strlen($string) > $utf8_split) {
  193. for ($i=0,$s=$utf8_split,$j=ceil(strlen($string)/$utf8_split);$i < $j;$i++,$s+=$utf8_split) {
  194. if ($this->is_utf8(substr($string,$s,$utf8_split)))
  195. return true;
  196. }
  197. return false;
  198. } else {
  199. return preg_match('%^(?:
  200. [\x09\x0A\x0D\x20-\x7E] # ASCII
  201. | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
  202. | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
  203. | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
  204. | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
  205. | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
  206. | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
  207. | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
  208. )*$%xs', $string);
  209. }
  210. }
  211. }
  212. # If cURL extension is available, we will use it...
  213. # else we'll be lived without HTTP/1.1 & HTTPS support
  214. ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### #####
  215. ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### #####
  216. ##### ##### ##### ##### ##### #####
  217. ##### Working with HTTP via cURL... #####
  218. ##### ##### ##### ##### ##### #####
  219. ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### #####
  220. ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### #####
  221. # cURL based interface
  222. class HTTPcURL extends HTTPClient {
  223. var $cURLHandler = null;
  224. function ConstructQuery() {
  225. if ($this->Verbose) {
  226. file_put_contents("./_request_#{$this->Redirect_i}.log", null);
  227. echo " # Client request log created (`./_request_#{$this->Redirect_i}.log`)\n";
  228. }
  229. $this->MakeAliases();
  230. $this->cURLHandler = curl_init();
  231. curl_setopt($this->cURLHandler, CURLOPT_FRESH_CONNECT, true);
  232. curl_setopt($this->cURLHandler, CURLOPT_FORBID_REUSE, true);
  233. curl_setopt($this->cURLHandler, CURLOPT_USERAGENT, $this->UserAgent);
  234. if ($this->Verbose) {
  235. file_put_contents("./_request_#{$this->Redirect_i}.log",
  236. "UserAgent: `".print_r($this->UserAgent, true)."`\r\n", FILE_APPEND);
  237. echo " # Client request log [`./_request_#{$this->Redirect_i}.log`] updated with `UserAgent`\r\n";
  238. }
  239. curl_setopt($this->cURLHandler, CURLOPT_TIMEOUT, $this->Timeout);
  240. curl_setopt($this->cURLHandler, CURLOPT_HEADER, true);
  241. #curl_setopt($this->cURLHandler, CURLINFO_HEADER_OUT, true);
  242. curl_setopt($this->cURLHandler, CURLOPT_SSL_VERIFYPEER, false);
  243. curl_setopt($this->cURLHandler, CURLOPT_SSL_VERIFYHOST, false);
  244. curl_setopt($this->cURLHandler, CURLOPT_AUTOREFERER, $this->AutoReferer);
  245. #curl_setopt($this->cURLHandler, CURLOPT_COOKIESESSION, true);
  246. curl_setopt($this->cURLHandler, CURLOPT_FOLLOWLOCATION, false);
  247. curl_setopt($this->cURLHandler, CURLOPT_VERBOSE, $this->Verbose);
  248. curl_setopt($this->cURLHandler, CURLOPT_NOPROGRESS, !$this->Verbose);
  249. curl_setopt($this->cURLHandler, CURLOPT_FAILONERROR, false);
  250. if ($this->Proxy) {
  251. if (is_string($this->Proxy))
  252. $this->Proxy = explode(':', $this->Proxy);
  253. curl_setopt($this->cURLHandler, CURLOPT_PROXY, $this->Proxy[0]);
  254. curl_setopt($this->cURLHandler, CURLOPT_PROXYPORT, $this->Proxy[1]);
  255. curl_setopt($this->cURLHandler, CURLOPT_HTTPPROXYTUNNEL, true);
  256. if ($this->Verbose) {
  257. file_put_contents("./_request_#{$this->Redirect_i}.log",
  258. "Proxy: `".trim(print_r($this->Proxy, true))."`\r\n", FILE_APPEND);
  259. echo " # Client request log [`./_request_#{$this->Redirect_i}.log`] updated with `Proxy`\r\n";
  260. }
  261. } else {
  262. curl_setopt($this->cURLHandler, CURLOPT_HTTPPROXYTUNNEL, false);
  263. }
  264. # CURLOPT_COOKIE
  265. if ($this->AutoReferer && !$this->Referer && $this->Host)
  266. $this->Referer = "{$this->Scheme}://{$this->Host}/";
  267. if ($this->Referer) {
  268. curl_setopt($this->cURLHandler, CURLOPT_REFERER, $this->Referer);
  269. if ($this->Verbose) {
  270. file_put_contents("./_request_#{$this->Redirect_i}.log",
  271. "Referer: `".trim(print_r($this->Referer, true))."`\r\n", FILE_APPEND);
  272. echo " # Client request log [`./_request_#{$this->Redirect_i}.log`] updated with `Referer`\r\n";
  273. }
  274. }
  275. #if ($this->AutoCookies && $this->Cookies) {
  276. if ($this->Cookies) {
  277. curl_setopt($this->cURLHandler, CURLOPT_COOKIE, $this->CookiesToString());
  278. if ($this->Verbose) {
  279. file_put_contents("./_request_#{$this->Redirect_i}.log",
  280. "Cookies: `".$this->CookiesToString()."`\r\n", FILE_APPEND);
  281. echo " # Client request log [`./_request_#{$this->Redirect_i}.log`] updated with `Cookies`\r\n";
  282. }
  283. }
  284. if ($this->HTTPHeaders) {
  285. curl_setopt($this->cURLHandler, CURLOPT_HTTPHEADER, $this->HTTPHeaders);
  286. if ($this->Verbose) {
  287. file_put_contents("./_request_#{$this->Redirect_i}.log",
  288. "HTTP Headers: `".trim(print_r($this->HTTPHeaders, true))."`\r\n", FILE_APPEND);
  289. echo " # Client request log [`./_request_#{$this->Redirect_i}.log`] updated with `HTTPHeaders`\r\n";
  290. }
  291. }
  292. #curl_setopt($this->cURLHandler, CURLOPT_POST, true);
  293. #curl_setopt($this->cURLHandler, CURLOPT_HTTPGET, false);
  294. #curl_setopt($this->cURLHandler, CURLOPT_POSTFIELDS, $this->Query);
  295. if ($this->Verbose) {
  296. file_put_contents("./_request_#{$this->Redirect_i}.log", "\r\n", FILE_APPEND);
  297. echo " # Client request log [`./_request_#{$this->Redirect_i}.log`] updated with `new line (".'\r\n'.")`\r\n";
  298. }
  299. return $this->cURLHandler;
  300. }
  301. function SendRequest($url = false, $ch = false) {
  302. if (!$url && !$this->URI) return false;
  303. if (!$url) $url = $this->URI;
  304. else $this->URI = $url;
  305. $this->PreConstructQuery();
  306. if (!$ch) $this->cURLHandler = $this->ConstructQuery();
  307. else $this->cURLHandler = $ch;
  308. #var_dump("{$this->Scheme}://{$this->Host}{$this->Path}{$this->Query}");
  309. #var_dump($this);
  310. curl_setopt($this->cURLHandler, CURLOPT_URL,
  311. "{$this->Scheme}://{$this->Host}{$this->Path}{$this->Query}");
  312. curl_setopt($this->cURLHandler, CURLOPT_PORT, $this->Port);
  313. curl_setopt($this->cURLHandler, CURLOPT_RETURNTRANSFER, true);
  314. $this->Result = curl_exec($this->cURLHandler);
  315. curl_close($this->cURLHandler);
  316. # clean 100 continue waste
  317. while (preg_match('#^http/1\.[01] [^\r\n]+\r?\n\r?\n#si', $this->Result))
  318. $this->Result = preg_replace("#^http/1\.[01] [^\r\n]+\r?\n\r?\n#si", '', $this->Result);
  319. #if (preg_match('#^http/1\.[01] [^\r\n]+\r\n\r\n#i', $this->Result))
  320. # $this->Result = preg_replace("#^http/1\.[01] [^\r\n]+\r\n\r\n#i", '', $this->Result);
  321. if ($this->Verbose) {
  322. file_put_contents("./_responce_#{$this->Redirect_i}.log", null);
  323. echo " # Server responce log created (`./_responce_#{$this->Redirect_i}.log`)\n";
  324. #var_dump($this->Cookies);
  325. #var_dump($this->GetCookies());
  326. }
  327. $this->ParseHeaders();
  328. $this->gotCookies_ = $this->GetCookies();
  329. if ($this->AutoCookies) {
  330. if ($this->Verbose) {
  331. file_put_contents("./_responce_#{$this->Redirect_i}.log",
  332. "HTTP Page request (source) Cookies: `".trim($this->CookiesToString())."`\r\n", FILE_APPEND);
  333. echo " # Server responce log [`./_responce_#{$this->Redirect_i}.log`] updated with `HTTP Page request (source) Cookies`\r\n";
  334. echo "Trying to set AutoCookies, responce Cookies are: \n";
  335. var_dump(trim($this->CookiesToString($this->gotCookies_)));
  336. echo "\n";
  337. }
  338. if (!$this->Cookies) $this->Cookies = $this->gotCookies_; #mad_cow O_o
  339. elseif (is_array($this->Cookies)) $this->Cookies = array_merge($this->Cookies, $this->gotCookies_);
  340. elseif (is_string($this->Cookies)) $this->Cookies .= $this->CookiesToString($this->gotCookies_);
  341. if ($this->Verbose) {
  342. file_put_contents("./_responce_#{$this->Redirect_i}.log",
  343. "HTTP Page responce (current) Cookies: `".trim(print_r($this->gotCookies_, true))."`\r\n", FILE_APPEND);
  344. echo " # Server total log [`./_responce_#{$this->Redirect_i}.log`] updated with `HTTP Page total (current) Cookies`\r\n";
  345. file_put_contents("./_responce_#{$this->Redirect_i}.log",
  346. "HTTP Page total (result) Cookies: `".trim($this->CookiesToString($this->Cookies))."`\r\n", FILE_APPEND);
  347. echo " # Server responce log [`./_responce_#{$this->Redirect_i}.log`] updated with `HTTP Page total (result) Cookies`\r\n";
  348. echo "\n";
  349. }
  350. }
  351. if ($this->Verbose) {
  352. file_put_contents("./_responce_#{$this->Redirect_i}.log",
  353. "HTTP Page Headers: `".trim(print_r($this->PageHeaders, true))."`\r\n", FILE_APPEND);
  354. echo " # Server responce log [`./_responce_#{$this->Redirect_i}.log`] updated with `HTTP Page Headers`\r\n";
  355. }
  356. /*
  357. if (!$this->Cookies) $this->Cookies = $this->gotCookies_; #mad_cow O_o
  358. elseif (is_array($this->Cookies)) $this->Cookies = array_merge($this->Cookies, $this->gotCookies_);
  359. elseif (is_string($this->Cookies)) $this->Cookies .= $this->CookiesToString($this->gotCookies_);
  360. if ($this->Verbose) {
  361. file_put_contents("./_responce_#{$this->Redirect_i}.log",
  362. "HTTP Page total Cookies: `".trim(print_r($this->Cookies, true))."`\r\n", FILE_APPEND);
  363. echo " # Server total log [`./_responce_#{$this->Redirect_i}.log`] updated with `HTTP Page total (current) Cookies`\r\n";
  364. #echo "So, resulting Cookies are: \n";
  365. #var_dump($this->Cookies);
  366. }
  367. # }
  368. */
  369. /*
  370. #curl_setopt($ch, CURLOPT_ENCODING, '');
  371. #if ($this->gotCookies_) {
  372. if ($this->Cookies) {
  373. var_dump($this->CookiesToString());
  374. curl_setopt($this->cURLHandler, CURLOPT_COOKIE, $this->CookiesToString());
  375. if ($this->Verbose) {
  376. file_put_contents("./_request_#{$this->Redirect_i}.log",
  377. "Cookies: `".trim(print_r($this->CookiesToString()), true)."`\r\n", FILE_APPEND);
  378. var_dump($this->CookiesToString());
  379. echo " # Client request log [`./_request_#{$this->Redirect_i}.log`] updated with `Cookies`\r\n";
  380. }
  381. }
  382. */
  383. /*
  384. #$this->gotCookies__ = $this->Cookies;
  385. $this->gotCookies_ = $this->GetCookies();
  386. if ($this->AutoCookies) {
  387. if ($this->Verbose) {
  388. file_put_contents("./_responce_#{$this->Redirect_i}.log",
  389. #"HTTP Page request Cookies: `".trim(print_r($this->gotCookies__, true))."`\r\n", FILE_APPEND);
  390. "HTTP Page request Cookies: `".trim(print_r($this->Cookies, true))."`\r\n", FILE_APPEND);
  391. echo " # Server responce log [`./_responce_#{$this->Redirect_i}.log`] updated with `HTTP Page request (source) Cookies`\r\n";
  392. #echo "Trying to set AutoCookies, source Cookies are: \n";
  393. #var_dump($this->Cookies);
  394. file_put_contents("./_responce_#{$this->Redirect_i}.log",
  395. "HTTP Page responce Cookies: `".trim(print_r($this->gotCookies_, true))."`\r\n", FILE_APPEND);
  396. echo " # Server responce log [`./_responce_#{$this->Redirect_i}.log`] updated with `HTTP Page responce (current) Cookies`\r\n";
  397. #echo "Trying to set AutoCookies, current Cookies are: \n";
  398. #var_dump($this->gotCookies_);
  399. }
  400. */
  401. /*
  402. if ($this->Verbose) {
  403. echo "Current Cookies are: \n";
  404. var_dump($this->gotCookies_);
  405. }
  406. if (!$this->Cookies) $this->Cookies = $this->gotCookies_; #mad_cow O_o
  407. elseif (is_array($this->Cookies)) $this->Cookies = array_merge($this->Cookies, $this->gotCookies_);
  408. elseif (is_string($this->Cookies)) $this->Cookies .= $this->CookiesToString($this->gotCookies_);
  409. if ($this->Verbose) {
  410. echo "So, resulting Cookies are: \n";
  411. var_dump($this->Cookies);
  412. }
  413. #if (is_array($this->Cookies)) $this->Cookies += $this->GetCookies();
  414. #$gotCookies = $this->GetCookies();
  415. /*
  416. $gotCookies = $this->Cookies;
  417. if ($this->Verbose)
  418. echo "Above page Cookies: \n".print_r($gotCookies, true)."\n";
  419. */
  420. # }
  421. #$this->GetCookies();
  422. #$this->gotCookies_ = $this->GetCookies(); # should be declared as `private`
  423. #}
  424. /*
  425. if ($this->Verbose && $this->Cookies) {
  426. file_put_contents("./_responce_#{$this->Redirect_i}.log",
  427. "HTTP Page total Cookies: `".trim(print_r($this->Cookies, true))."`\r\n", FILE_APPEND);
  428. echo " # Server responce log [`./_responce_#{$this->Redirect_i}.log`] updated with `HTTP Page total Cookies`\r\n";
  429. }
  430. */
  431. if ($this->Verbose && $this->AutoRedirect && isset($this->Headers['location']) &&
  432. $this->maxRedirects_i < $this->maxRedirects) {
  433. file_put_contents("./_responce_#{$this->Redirect_i}.log",
  434. "HTTP is now redirecting us: ({$this->maxRedirects_i}/{$this->maxRedirects}) to `{$this->Headers['location']}`\r\n", FILE_APPEND);
  435. echo " # Server responce log [`./_responce_#{$this->Redirect_i}.log`] updated with `HTTP Redirect`\r\n";
  436. }
  437. if ($this->Verbose) {
  438. file_put_contents("./_responce_#{$this->Redirect_i}.log",
  439. "HTTP Page Contents: `".trim(print_r($this->Result, true))."`\r\n", FILE_APPEND);
  440. echo " # Server responce log [`./_responce_#{$this->Redirect_i}.log`] updated with `HTTP Page Contents`\r\n";
  441. }
  442. #while (preg_match('#^http/1\.[01] [^\r\n]+\r\n\r\n#i', $this->Result)) {
  443. #if (preg_match('#^http/1\.[01] [^\r\n]+\r\n\r\n#i', $this->Result))
  444. # $this->Result = preg_replace("#^http/1\.[01] [^\r\n]+\r\n\r\n#i", '', $this->Result);
  445. /*
  446. if (!preg_match('#^HTTP/1\.[01] 100#', $this->Result)) {
  447. if ($this->Verbose)
  448. echo "Above page Headers: ".print_r($this->PageHeaders, true)."\n";
  449. if ($this->Verbose) echo "\n\n";
  450. }
  451. */
  452. if (preg_match('#^HTTP/1\.[01] 100#', $this->Result)) {
  453. if ($this->Verbose)
  454. echo "Above page Headers: ".print_r($this->PageHeaders, true)."\n";
  455. if ($this->Verbose) echo "\n\n";
  456. }
  457. #var_dump("Loc\n");
  458. #var_dump($this->Headers);
  459. #var_dump("{$this->maxRedirects_i} < {$this->maxRedirects}");
  460. #if ($this->Verbose && $this->AutoRedirect && isset($this->Headers['location']))
  461. # echo " Redirect {$this->maxRedirects_i}/{$this->maxRedirects} -> \n";
  462. if ($this->AutoRedirect && isset($this->Headers['location']) &&
  463. $this->maxRedirects_i < $this->maxRedirects) {
  464. $this->maxRedirects_i++;
  465. #if (substr($this->Headers['location'],0,7) == 'http://') {
  466. if ($this->Verbose)
  467. echo "Above page redirected ({$this->maxRedirects_i}/{$this->maxRedirects}) to <{$this->Headers['location']}>\n";
  468. if (preg_match('#^https?://#i', $this->Headers['location'])) {
  469. return $this->SendRequest("{$this->Headers['location']}");
  470. } elseif ($this->Headers['location'][0] == '/') {
  471. return $this->SendRequest("{$this->Scheme}://{$this->Host}{$this->Headers['location']}");
  472. } else {
  473. return $this->SendRequest("{$this->Scheme}://{$this->Host}".
  474. substr($this->Path,0,strrpos($this->Path,'/')+1)."{$this->Headers['location']}");
  475. }
  476. }
  477. #$this->ResultClean = ltrim(substr($this->Result, strrpos($this->Result, "\r\n\r\n")));
  478. #$this->ResultClean = preg_match("#Content-Length: [0-9]+\r\n\r\n(.*?)$#si", $this->Result, $clean);
  479. #preg_match("#[^<>]+:[^<>]+\r\n\r\n(.*?)$#si", $this->Result, $clean);
  480. #$this->ResultClean = isset($clean[1]) ? $clean[1] : $this->Result;
  481. $this->ResultClean = ltrim(substr($this->Result, strpos($this->Result, "\r\n\r\n")));
  482. $this->Referer = false;
  483. $this->Query = Array();
  484. $this->Redirect_i++;
  485. #var_dump($this->Cookies);
  486. return $this->Result;
  487. }
  488. function Get($url) {
  489. $this->Method = 'get';
  490. $this->cURLHandler = $this->ConstructQuery();
  491. curl_setopt($this->cURLHandler, CURLOPT_HTTPGET, true);
  492. return $this->SendRequest($url, $this->cURLHandler);
  493. }
  494. function Post($url, $postfields = false, $force_fields_str = false) {
  495. $this->Method = 'post';
  496. if (!$postfields) $postfields = $this->Query;
  497. if ($force_fields_str) {
  498. #if (is_array($postfields))
  499. # $postfields = http_build_query($postfields);
  500. # $postfields = str_replace('+', '%20', $postfields);
  501. if (is_array($postfields)) {
  502. $postfields_ = '';
  503. if (is_array($postfields))
  504. foreach ($postfields as $key => $value) {
  505. if (isset($value[0]) && $value[0] != '@')
  506. $value = urlencode($value);
  507. #var_dump("{$key}={$value}&");
  508. $postfields_ .= "{$key}={$value}&";
  509. }
  510. $postfields_ = rtrim($postfields_, '&');
  511. $postfields = $postfields_;
  512. }
  513. }
  514. #if (is_array($postfields))
  515. # $postfields = http_build_query($postfields);
  516. # $postfields = str_replace('+', '%20', $postfields);
  517. # a-la http_build_query()
  518. /*
  519. if (is_array($postfields))
  520. foreach ($postfields as &$field)
  521. if ($field[0] != '@')
  522. $field = urlencode(($field));
  523. $postfields_ = '';
  524. if (is_array($postfields))
  525. foreach ($postfields as $key => $value)
  526. $postfields_ .= "{$key}={$value}&";
  527. $postfields_ = trim($postfields_, '&');
  528. $postfields = $postfields_;
  529. #*#/
  530. echo "\n\n------------------\n\n";var_dump($postfields);
  531. if (is_array($postfields))
  532. foreach ($postfields as &$field)
  533. if ($field[0] != '@')
  534. $field = rawurlencode($field);
  535. #$field = str_replace('+', '%2B', $field);
  536. echo "\n\n------------------\n\n";var_dump($postfields);
  537. */
  538. $this->cURLHandler = $this->ConstructQuery();
  539. if ($this->Verbose) {
  540. echo "Below Post data: ".print_r($postfields, true)."\n";
  541. file_put_contents("./_request_#{$this->Redirect_i}.log",
  542. "Post Vars: `".trim(print_r($postfields, true))."`\r\n", FILE_APPEND);
  543. echo " # Client request log [`./_request_#{$this->Redirect_i}.log`] updated with `Post Vars`\r\n";
  544. }
  545. curl_setopt($this->cURLHandler, CURLOPT_POST, true);
  546. curl_setopt($this->cURLHandler, CURLOPT_HTTPGET, false);
  547. curl_setopt($this->cURLHandler, CURLOPT_POSTFIELDS, $postfields);
  548. return $this->SendRequest($url, $this->cURLHandler);
  549. }
  550. /*
  551. function GetCookies() {
  552. #return true;
  553. preg_match_all('#\sSet-Cookie: *([^ ]+); #si', $this->Result, $cookies, PREG_SET_ORDER);
  554. foreach ($cookies as $cookie) {
  555. if ($this->AutoCookies) $this->Cookies .= " {$cookie[1]};";
  556. #elseif ($this->FlushCookies) $this->Cookies = "{$cookie[1]};";
  557. }
  558. }
  559. */
  560. }
  561. # If the above methods are not available, but we still need to do the work...
  562. ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### #####
  563. ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### #####
  564. ##### ##### ##### ##### ##### #####
  565. ##### Working with HTTP via sockets... #####
  566. ##### ##### ##### ##### ##### #####
  567. ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### #####
  568. ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### #####
  569. # Socket based interface
  570. class HTTPSockets extends HTTPClient {
  571. var $ContentType = null;
  572. var $Boundary = '-----NULL-BYTE----NULL-BYTE----NULL-BYTE';
  573. var $Silent = false; # If it true, wouldn't print any messages
  574. var $ResponseStatus = false; # First line of the response header
  575. var $ResponseCode = 0; # Response HTTP code
  576. var $Retries = 3; # Max number of request retries
  577. function ConstructQuery() {
  578. $this->MakeAliases();
  579. if (is_array($this->Query)) {
  580. if (strtolower($this->ContentType) != 'multipart/form-data') {
  581. $query_string = '';
  582. foreach ($this->Query as $key => $value) {
  583. $query_string .= "$key=$value&";
  584. //$query_string .= $key.'='.urlencode($value);
  585. }
  586. $this->Query = trim($query_string, '&');
  587. } else {
  588. $query_string = '';
  589. foreach ($this->Query as $key => $value) {
  590. $query_string .= "--{$this->Boundary}\r\n";
  591. if (!is_array($value)) {
  592. $query_string .= "Content-Disposition: form-data; name=\"$key\"\r\n\r\n$value\r\n";
  593. } else {
  594. /*
  595. if (!isset($value[1]) && !isset($value[2])) {
  596. $value[1] = $value[0];
  597. if (!is_file($value[1])) {
  598. echo "HTTPQuery: No such file!\n";
  599. //return false;
  600. }
  601. $value[0] = @file_get_contents($value[1]);
  602. file_put_contents("./!!!{$value[1]}", '');
  603. }*/
  604. if (!isset($value[1])) $value[1] = 'somefile';
  605. if (!isset($value[2])) $value[2] = 'application/octet-stream';
  606. //if (!isset($value[2])) $value[2] = 'undefined/undefined';
  607. $query_string .= "Content-Disposition: form-data; name=\"$key\"; ";
  608. $query_string .= "filename=\"{$value[1]}\"\r\n";
  609. $query_string .= "Content-Type: {$value[2]}\r\n\r\n{$value[0]}\r\n";
  610. }
  611. }
  612. $query_string .= "--{$this->Boundary}--\r\n";
  613. $this->Query = $query_string;
  614. }
  615. }
  616. }
  617. function Filter() {
  618. $this->Result = null;
  619. $this->ResultClean = null;
  620. $this->Method = strtolower($this->Method);
  621. $this->URI = preg_replace('#^https?://#', '', $this->URI);
  622. //$this->URI = preg_replace('#[\\]]#', '/', $this->URI);
  623. //$this->URI = preg_replace('#/{2,}#', '/', $this->URI);
  624. if (is_string($this->Proxy)) $this->Proxy = explode(':', $this->Proxy);
  625. $this->ConstructQuery();
  626. # Erasing data from old request (if current will fail)
  627. $this->ResponseStatus = false;
  628. $this->ResponseCode = 0;
  629. }
  630. function SendRequest($url = false) {
  631. if (!$url && !$this->URI) return false;
  632. if (!$url) $url = $this->URI;
  633. else $this->URI = $url;
  634. $this->Filter();
  635. $url_parts = explode('/', $this->URI);
  636. $url_parts[0] = explode(':', $url_parts[0]);
  637. $this->Host = $url_parts[0][0];
  638. if (isset($url_parts[0][1]) && is_numeric($url_parts[0][1]))
  639. $this->Port = $url_parts[0][1];
  640. $this->Path = '';
  641. for ($i = 1; $i < count($url_parts); $i++)
  642. $this->Path .= '/'.$url_parts[$i];
  643. if ($this->Path == '') $this->Path = '/';
  644. // Creating socket
  645. $cur_try = 0; // Current try
  646. $max_tries = $this->Retries; // Max allowed tries
  647. while (true) {
  648. if ($this->Proxy) {
  649. if (is_string($this->Proxy))
  650. $this->Proxy = explode(':', $this->Proxy);
  651. $fp = @fsockopen($this->Proxy[0], $this->Proxy[1], $errno, $errstr, $this->Timeout);
  652. } else {
  653. $fp = @fsockopen($this->Host, $this->Port, $errno, $errstr, $this->Timeout);
  654. }
  655. if ($fp) break; // If everything is ok, going on without stopping
  656. // Else retrying...
  657. $cur_try++;
  658. if (!$this->Silent) echo "Cannot create socket, will now try again ({$cur_try}/{$max_tries})...\n";
  659. if ($cur_try >= $max_tries) {
  660. if (!$this->Silent) echo "Too many errors, please check your internet connection!\n";
  661. return false;
  662. }
  663. sleep(0);
  664. }
  665. if (($this->Method == 'get') && (substr(0, 1, $this->Query) != '?') && ($this->Query != null))
  666. $this->Query = '?'.$this->Query;
  667. $query_length = strlen($this->Query);
  668. if (!$this->UserAgent)
  669. $this->UserAgent = 'NULL_byte\'s PHP Browser Mozilla/4.0 (compatible; MSIE 3.0; Windows NT 4.0)';
  670. if ($this->AutoReferer && !$this->Referer)
  671. $this->Referer = $this->URI;
  672. $_cookies = $this->CookiesToString($this->Cookies);
  673. $PostContentType = 'application/x-www-form-urlencoded';
  674. if (strtolower($this->ContentType) == 'multipart/form-data')
  675. $PostContentType = "multipart/form-data; boundary={$this->Boundary}";
  676. $HTTP_Version = 'HTTP/1.0';
  677. $out = Array();
  678. switch ($this->Method) {
  679. case 'get':
  680. $out[] = "GET {$this->Path}{$this->Query} {$HTTP_Version}";
  681. break;
  682. case 'post':
  683. $out[] = "POST {$this->Path} {$HTTP_Version}";
  684. if (isset($this->ContentType))
  685. $out[] = "Content-Type: {$PostContentType}";
  686. $out[] = "Content-Length: {$query_length}";
  687. break;
  688. default:
  689. $out[] = strtoupper($this->Method)." {$this->Path}{$this->Query} {$HTTP_Version}";
  690. break;
  691. }
  692. $out[] = "Host: {$this->Host}:{$this->Port}";
  693. $out[] = "Accept: text/html;q=0.9,application/xhtml+xml;q=0.8,*/*;q=0.5";
  694. $out[] = "Accept-Language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7";
  695. $out[] = "User-Agent: {$this->UserAgent}";
  696. if ($this->Cookies) $out[] = "Cookie: {$_cookies}";
  697. if ($this->Referer) $out[] = "Referer: {$this->Referer}";
  698. $out = array_merge($out, $this->HTTPHeaders);
  699. $out[] = "Connection: Close";
  700. //$out[] = "Connection: Keep-Alive";
  701. $out = implode("\r\n", $out);
  702. $out = str_replace("\n", "\r\n", $out);
  703. $out = preg_replace("#[\n]+#", "\n", $out);
  704. $out = preg_replace("#[\r]+#", "\r", $out);
  705. $out .= "\r\n\r\n";
  706. if ($this->Method == 'post') $out .= $this->Query;
  707. fwrite($fp, $out);
  708. $this->Result = '';
  709. while (!feof($fp))
  710. $this->Result .= fgets($fp, 1024);
  711. fclose($fp);
  712. $this->ResultClean = ltrim(substr($this->Result, strpos($this->Result, "\r\n\r\n")));
  713. $this->ParseHeaders();
  714. if ($this->Verbose) {
  715. echo "----->\n$out\n\n";
  716. echo "<-----\n{$this->Result}\n\n";
  717. }
  718. if ($this->AutoCookies) {
  719. #if (is_array($this->Cookies)) $this->Cookies += $this->GetCookies();
  720. if (!$this->Cookies) $this->Cookies = $this->GetCookies();
  721. elseif (is_array($this->Cookies)) $this->Cookies = array_merge($this->Cookies, $this->GetCookies());
  722. elseif (is_string($this->Cookies)) $this->Cookies .= $this->GetCookies(true);
  723. }
  724. if ($this->AutoRedirect && isset($this->Headers['location']) &&
  725. $this->maxRedirects_i < $this->maxRedirects) {
  726. $this->maxRedirects_i++;
  727. if (substr($this->Headers['location'],0,7) == 'http://') {
  728. return $this->SendRequest("{$this->Headers['location']}");
  729. } elseif ($this->Headers['location'][0] == '/') {
  730. return $this->SendRequest("http://{$this->Host}{$this->Headers['location']}");
  731. } else {
  732. return $this->SendRequest("http://{$this->Host}".
  733. substr($this->Path,0,strrpos($this->Path,'/')+1)."{$this->Headers['location']}");
  734. }
  735. }
  736. $this->Query = NULL;
  737. $this->Referer = $url;
  738. $this->ContentType = NULL;
  739. $this->RawHeaders = Array();
  740. $this->HTTPHeaders = Array();
  741. $this->RequestHeaders = Array();
  742. $this->Redirect_i++;
  743. return $this->Result;
  744. }
  745. function Get($url, $vars = false) {
  746. if ($vars)
  747. $this->Query = $vars;
  748. $this->Method = 'get';
  749. return $this->SendRequest($url);
  750. }
  751. function Post($url, $vars = false, $force_fields_str = false) {
  752. if ($vars)
  753. $this->Query = $vars;
  754. $this->Method = 'post';
  755. return $this->SendRequest($url);
  756. }
  757. }
  758. /*
  759. if (!function_exists('class_alias')) {
  760. function class_alias($from, $to) {
  761. eval("class {$to} extends {$from} {}");
  762. #eval("abstract class {$to} extends {$from} {}");
  763. }}
  764. if (function_exists('curl_init')) {
  765. class_alias('HTTPcURL', 'HTTPQuery');
  766. } else {
  767. class_alias('HTTPSockets', 'HTTPQuery');
  768. }
  769. */
  770. eval('class HTTPQuery extends '.
  771. (function_exists('curl_init') ? 'HTTPcURL' : 'HTTPSockets').' {}');
  772. ?>