snoopy.class.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  1. <?php
  2. /*************************************************
  3. *
  4. * Snoopy - the PHP net client
  5. * Author: Monte Ohrt <monte@ohrt.com>
  6. * Copyright (c): 1999-2014, all rights reserved
  7. * Version: 2.0.0
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. * You may contact the author of Snoopy by e-mail at:
  23. * monte@ohrt.com
  24. *
  25. * The latest version of Snoopy can be obtained from:
  26. * http://snoopy.sourceforge.net/
  27. *************************************************/
  28. class Snoopy
  29. {
  30. /**** Public variables ****/
  31. /* user definable vars */
  32. var $scheme = 'http'; // http or https
  33. var $host = "www.php.net"; // host name we are connecting to
  34. var $port = 80; // port we are connecting to
  35. var $proxy_host = ""; // proxy host to use
  36. var $proxy_port = ""; // proxy port to use
  37. var $proxy_user = ""; // proxy user to use
  38. var $proxy_pass = ""; // proxy password to use
  39. var $agent = "Snoopy v2.0.0"; // agent we masquerade as
  40. var $referer = ""; // referer info to pass
  41. var $cookies = array(); // array of cookies to pass
  42. // $cookies["username"]="joe";
  43. var $rawheaders = array(); // array of raw headers to send
  44. // $rawheaders["Content-type"]="text/html";
  45. var $maxredirs = 5; // http redirection depth maximum. 0 = disallow
  46. var $lastredirectaddr = ""; // contains address of last redirected address
  47. var $offsiteok = true; // allows redirection off-site
  48. var $maxframes = 0; // frame content depth maximum. 0 = disallow
  49. var $expandlinks = true; // expand links to fully qualified URLs.
  50. // this only applies to fetchlinks()
  51. // submitlinks(), and submittext()
  52. var $passcookies = true; // pass set cookies back through redirects
  53. // NOTE: this currently does not respect
  54. // dates, domains or paths.
  55. var $user = ""; // user for http authentication
  56. var $pass = ""; // password for http authentication
  57. // http accept types
  58. var $accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
  59. var $results = ""; // where the content is put
  60. var $error = ""; // error messages sent here
  61. var $response_code = ""; // response code returned from server
  62. var $headers = array(); // headers returned from server sent here
  63. var $maxlength = 500000; // max return data length (body)
  64. var $read_timeout = 0; // timeout on read operations, in seconds
  65. // supported only since PHP 4 Beta 4
  66. // set to 0 to disallow timeouts
  67. var $timed_out = false; // if a read operation timed out
  68. var $status = 0; // http request status
  69. var $temp_dir = "/tmp"; // temporary directory that the webserver
  70. // has permission to write to.
  71. // under Windows, this should be C:\temp
  72. var $curl_path = false;
  73. // deprecated, snoopy no longer uses curl for https requests,
  74. // but instead requires the openssl extension.
  75. // send Accept-encoding: gzip?
  76. var $use_gzip = true;
  77. // file or directory with CA certificates to verify remote host with
  78. var $cafile;
  79. var $capath;
  80. /**** Private variables ****/
  81. var $_maxlinelen = 4096; // max line length (headers)
  82. var $_httpmethod = "GET"; // default http request method
  83. var $_httpversion = "HTTP/1.0"; // default http request version
  84. var $_submit_method = "POST"; // default submit method
  85. var $_submit_type = "application/x-www-form-urlencoded"; // default submit type
  86. var $_mime_boundary = ""; // MIME boundary for multipart/form-data submit type
  87. var $_redirectaddr = false; // will be set if page fetched is a redirect
  88. var $_redirectdepth = 0; // increments on an http redirect
  89. var $_frameurls = array(); // frame src urls
  90. var $_framedepth = 0; // increments on frame depth
  91. var $_isproxy = false; // set if using a proxy server
  92. var $_fp_timeout = 30; // timeout for socket connection
  93. /*======================================================================*\
  94. Function: fetch
  95. Purpose: fetch the contents of a web page
  96. (and possibly other protocols in the
  97. future like ftp, nntp, gopher, etc.)
  98. Input: $URI the location of the page to fetch
  99. Output: $this->results the output text from the fetch
  100. \*======================================================================*/
  101. function fetch($URI)
  102. {
  103. $URI_PARTS = parse_url($URI);
  104. if (!empty($URI_PARTS["user"]))
  105. $this->user = $URI_PARTS["user"];
  106. if (!empty($URI_PARTS["pass"]))
  107. $this->pass = $URI_PARTS["pass"];
  108. if (empty($URI_PARTS["query"]))
  109. $URI_PARTS["query"] = '';
  110. if (empty($URI_PARTS["path"]))
  111. $URI_PARTS["path"] = '';
  112. $fp = null;
  113. switch (strtolower($URI_PARTS["scheme"])) {
  114. case "https":
  115. if (!extension_loaded('openssl')) {
  116. trigger_error("openssl extension required for HTTPS", E_USER_ERROR);
  117. exit;
  118. }
  119. $this->port = 443;
  120. case "http":
  121. $this->scheme = strtolower($URI_PARTS["scheme"]);
  122. $this->host = $URI_PARTS["host"];
  123. if (!empty($URI_PARTS["port"]))
  124. $this->port = $URI_PARTS["port"];
  125. if ($this->_connect($fp)) {
  126. if ($this->_isproxy) {
  127. // using proxy, send entire URI
  128. $this->_httprequest($URI, $fp, $URI, $this->_httpmethod);
  129. } else {
  130. $path = $URI_PARTS["path"] . ($URI_PARTS["query"] ? "?" . $URI_PARTS["query"] : "");
  131. // no proxy, send only the path
  132. $this->_httprequest($path, $fp, $URI, $this->_httpmethod);
  133. }
  134. $this->_disconnect($fp);
  135. if ($this->_redirectaddr) {
  136. /* url was redirected, check if we've hit the max depth */
  137. if ($this->maxredirs > $this->_redirectdepth) {
  138. // only follow redirect if it's on this site, or offsiteok is true
  139. if (preg_match("|^https?://" . preg_quote($this->host) . "|i", $this->_redirectaddr) || $this->offsiteok) {
  140. /* follow the redirect */
  141. $this->_redirectdepth++;
  142. $this->lastredirectaddr = $this->_redirectaddr;
  143. $this->fetch($this->_redirectaddr);
  144. }
  145. }
  146. }
  147. if ($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0) {
  148. $frameurls = $this->_frameurls;
  149. $this->_frameurls = array();
  150. foreach ($frameurls as $frameurl) {
  151. if ($this->_framedepth < $this->maxframes) {
  152. $this->fetch($frameurl);
  153. $this->_framedepth++;
  154. } else
  155. break;
  156. }
  157. }
  158. } else {
  159. return false;
  160. }
  161. return $this;
  162. break;
  163. default:
  164. // not a valid protocol
  165. $this->error = 'Invalid protocol "' . $URI_PARTS["scheme"] . '"\n';
  166. return false;
  167. break;
  168. }
  169. return $this;
  170. }
  171. /*======================================================================*\
  172. Function: submit
  173. Purpose: submit an http(s) form
  174. Input: $URI the location to post the data
  175. $formvars the formvars to use.
  176. format: $formvars["var"] = "val";
  177. $formfiles an array of files to submit
  178. format: $formfiles["var"] = "/dir/filename.ext";
  179. Output: $this->results the text output from the post
  180. \*======================================================================*/
  181. function submit($URI, $formvars = "", $formfiles = "")
  182. {
  183. unset($postdata);
  184. $postdata = $this->_prepare_post_body($formvars, $formfiles);
  185. $URI_PARTS = parse_url($URI);
  186. if (!empty($URI_PARTS["user"]))
  187. $this->user = $URI_PARTS["user"];
  188. if (!empty($URI_PARTS["pass"]))
  189. $this->pass = $URI_PARTS["pass"];
  190. if (empty($URI_PARTS["query"]))
  191. $URI_PARTS["query"] = '';
  192. if (empty($URI_PARTS["path"]))
  193. $URI_PARTS["path"] = '';
  194. switch (strtolower($URI_PARTS["scheme"])) {
  195. case "https":
  196. if (!extension_loaded('openssl')) {
  197. trigger_error("openssl extension required for HTTPS", E_USER_ERROR);
  198. exit;
  199. }
  200. $this->port = 443;
  201. case "http":
  202. $this->scheme = strtolower($URI_PARTS["scheme"]);
  203. $this->host = $URI_PARTS["host"];
  204. if (!empty($URI_PARTS["port"]))
  205. $this->port = $URI_PARTS["port"];
  206. if ($this->_connect($fp)) {
  207. if ($this->_isproxy) {
  208. // using proxy, send entire URI
  209. $this->_httprequest($URI, $fp, $URI, $this->_submit_method, $this->_submit_type, $postdata);
  210. } else {
  211. $path = $URI_PARTS["path"] . ($URI_PARTS["query"] ? "?" . $URI_PARTS["query"] : "");
  212. // no proxy, send only the path
  213. $this->_httprequest($path, $fp, $URI, $this->_submit_method, $this->_submit_type, $postdata);
  214. }
  215. $this->_disconnect($fp);
  216. if ($this->_redirectaddr) {
  217. /* url was redirected, check if we've hit the max depth */
  218. if ($this->maxredirs > $this->_redirectdepth) {
  219. if (!preg_match("|^" . $URI_PARTS["scheme"] . "://|", $this->_redirectaddr))
  220. $this->_redirectaddr = $this->_expandlinks($this->_redirectaddr, $URI_PARTS["scheme"] . "://" . $URI_PARTS["host"]);
  221. // only follow redirect if it's on this site, or offsiteok is true
  222. if (preg_match("|^https?://" . preg_quote($this->host) . "|i", $this->_redirectaddr) || $this->offsiteok) {
  223. /* follow the redirect */
  224. $this->_redirectdepth++;
  225. $this->lastredirectaddr = $this->_redirectaddr;
  226. if (strpos($this->_redirectaddr, "?") > 0)
  227. $this->fetch($this->_redirectaddr); // the redirect has changed the request method from post to get
  228. else
  229. $this->submit($this->_redirectaddr, $formvars, $formfiles);
  230. }
  231. }
  232. }
  233. if ($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0) {
  234. $frameurls = $this->_frameurls;
  235. $this->_frameurls = array();
  236. foreach ($frameurls as $frameurl) {
  237. if ($this->_framedepth < $this->maxframes) {
  238. $this->fetch($frameurl);
  239. $this->_framedepth++;
  240. } else
  241. break;
  242. }
  243. }
  244. } else {
  245. return false;
  246. }
  247. return $this;
  248. break;
  249. default:
  250. // not a valid protocol
  251. $this->error = 'Invalid protocol "' . $URI_PARTS["scheme"] . '"\n';
  252. return false;
  253. break;
  254. }
  255. return $this;
  256. }
  257. /*======================================================================*\
  258. Function: fetchlinks
  259. Purpose: fetch the links from a web page
  260. Input: $URI where you are fetching from
  261. Output: $this->results an array of the URLs
  262. \*======================================================================*/
  263. function fetchlinks($URI)
  264. {
  265. if ($this->fetch($URI) !== false) {
  266. if ($this->lastredirectaddr)
  267. $URI = $this->lastredirectaddr;
  268. if (is_array($this->results)) {
  269. for ($x = 0; $x < count($this->results); $x++)
  270. $this->results[$x] = $this->_striplinks($this->results[$x]);
  271. } else
  272. $this->results = $this->_striplinks($this->results);
  273. if ($this->expandlinks)
  274. $this->results = $this->_expandlinks($this->results, $URI);
  275. return $this;
  276. } else
  277. return false;
  278. }
  279. /*======================================================================*\
  280. Function: fetchform
  281. Purpose: fetch the form elements from a web page
  282. Input: $URI where you are fetching from
  283. Output: $this->results the resulting html form
  284. \*======================================================================*/
  285. function fetchform($URI)
  286. {
  287. if ($this->fetch($URI) !== false) {
  288. if (is_array($this->results)) {
  289. for ($x = 0; $x < count($this->results); $x++)
  290. $this->results[$x] = $this->_stripform($this->results[$x]);
  291. } else
  292. $this->results = $this->_stripform($this->results);
  293. return $this;
  294. } else
  295. return false;
  296. }
  297. /*======================================================================*\
  298. Function: fetchtext
  299. Purpose: fetch the text from a web page, stripping the links
  300. Input: $URI where you are fetching from
  301. Output: $this->results the text from the web page
  302. \*======================================================================*/
  303. function fetchtext($URI)
  304. {
  305. if ($this->fetch($URI) !== false) {
  306. if (is_array($this->results)) {
  307. for ($x = 0; $x < count($this->results); $x++)
  308. $this->results[$x] = $this->_striptext($this->results[$x]);
  309. } else
  310. $this->results = $this->_striptext($this->results);
  311. return $this;
  312. } else
  313. return false;
  314. }
  315. /*======================================================================*\
  316. Function: submitlinks
  317. Purpose: grab links from a form submission
  318. Input: $URI where you are submitting from
  319. Output: $this->results an array of the links from the post
  320. \*======================================================================*/
  321. function submitlinks($URI, $formvars = "", $formfiles = "")
  322. {
  323. if ($this->submit($URI, $formvars, $formfiles) !== false) {
  324. if ($this->lastredirectaddr)
  325. $URI = $this->lastredirectaddr;
  326. if (is_array($this->results)) {
  327. for ($x = 0; $x < count($this->results); $x++) {
  328. $this->results[$x] = $this->_striplinks($this->results[$x]);
  329. if ($this->expandlinks)
  330. $this->results[$x] = $this->_expandlinks($this->results[$x], $URI);
  331. }
  332. } else {
  333. $this->results = $this->_striplinks($this->results);
  334. if ($this->expandlinks)
  335. $this->results = $this->_expandlinks($this->results, $URI);
  336. }
  337. return $this;
  338. } else
  339. return false;
  340. }
  341. /*======================================================================*\
  342. Function: submittext
  343. Purpose: grab text from a form submission
  344. Input: $URI where you are submitting from
  345. Output: $this->results the text from the web page
  346. \*======================================================================*/
  347. function submittext($URI, $formvars = "", $formfiles = "")
  348. {
  349. if ($this->submit($URI, $formvars, $formfiles) !== false) {
  350. if ($this->lastredirectaddr)
  351. $URI = $this->lastredirectaddr;
  352. if (is_array($this->results)) {
  353. for ($x = 0; $x < count($this->results); $x++) {
  354. $this->results[$x] = $this->_striptext($this->results[$x]);
  355. if ($this->expandlinks)
  356. $this->results[$x] = $this->_expandlinks($this->results[$x], $URI);
  357. }
  358. } else {
  359. $this->results = $this->_striptext($this->results);
  360. if ($this->expandlinks)
  361. $this->results = $this->_expandlinks($this->results, $URI);
  362. }
  363. return $this;
  364. } else
  365. return false;
  366. }
  367. /*======================================================================*\
  368. Function: set_submit_multipart
  369. Purpose: Set the form submission content type to
  370. multipart/form-data
  371. \*======================================================================*/
  372. function set_submit_multipart()
  373. {
  374. $this->_submit_type = "multipart/form-data";
  375. return $this;
  376. }
  377. /*======================================================================*\
  378. Function: set_submit_normal
  379. Purpose: Set the form submission content type to
  380. application/x-www-form-urlencoded
  381. \*======================================================================*/
  382. function set_submit_normal()
  383. {
  384. $this->_submit_type = "application/x-www-form-urlencoded";
  385. return $this;
  386. }
  387. /*======================================================================*\
  388. Private functions
  389. \*======================================================================*/
  390. /*======================================================================*\
  391. Function: _striplinks
  392. Purpose: strip the hyperlinks from an html document
  393. Input: $document document to strip.
  394. Output: $match an array of the links
  395. \*======================================================================*/
  396. function _striplinks($document)
  397. {
  398. preg_match_all("'<\s*a\s.*?href\s*=\s* # find <a href=
  399. ([\"\'])? # find single or double quote
  400. (?(1) (.*?)\\1 | ([^\s\>]+)) # if quote found, match up to next matching
  401. # quote, otherwise match up to next space
  402. 'isx", $document, $links);
  403. // catenate the non-empty matches from the conditional subpattern
  404. foreach ($links[2] as $key => $val) {
  405. if (!empty($val))
  406. $match[] = $val;
  407. }
  408. foreach ($links[3] as $key => $val) {
  409. if (!empty($val))
  410. $match[] = $val;
  411. }
  412. // return the links
  413. return $match;
  414. }
  415. /*======================================================================*\
  416. Function: _stripform
  417. Purpose: strip the form elements from an html document
  418. Input: $document document to strip.
  419. Output: $match an array of the links
  420. \*======================================================================*/
  421. function _stripform($document)
  422. {
  423. preg_match_all("'<\/?(FORM|INPUT|SELECT|TEXTAREA|(OPTION))[^<>]*>(?(2)(.*(?=<\/?(option|select)[^<>]*>[\r\n]*)|(?=[\r\n]*))|(?=[\r\n]*))'Usi", $document, $elements);
  424. // catenate the matches
  425. $match = implode("\r\n", $elements[0]);
  426. // return the links
  427. return $match;
  428. }
  429. /*======================================================================*\
  430. Function: _striptext
  431. Purpose: strip the text from an html document
  432. Input: $document document to strip.
  433. Output: $text the resulting text
  434. \*======================================================================*/
  435. function _striptext($document)
  436. {
  437. // I didn't use preg eval (//e) since that is only available in PHP 4.0.
  438. // so, list your entities one by one here. I included some of the
  439. // more common ones.
  440. $search = array("'<script[^>]*?>.*?</script>'si", // strip out javascript
  441. "'<[\/\!]*?[^<>]*?>'si", // strip out html tags
  442. "'([\r\n])[\s]+'", // strip out white space
  443. "'&(quot|#34|#034|#x22);'i", // replace html entities
  444. "'&(amp|#38|#038|#x26);'i", // added hexadecimal values
  445. "'&(lt|#60|#060|#x3c);'i",
  446. "'&(gt|#62|#062|#x3e);'i",
  447. "'&(nbsp|#160|#xa0);'i",
  448. "'&(iexcl|#161);'i",
  449. "'&(cent|#162);'i",
  450. "'&(pound|#163);'i",
  451. "'&(copy|#169);'i",
  452. "'&(reg|#174);'i",
  453. "'&(deg|#176);'i",
  454. "'&(#39|#039|#x27);'",
  455. "'&(euro|#8364);'i", // europe
  456. "'&a(uml|UML);'", // german
  457. "'&o(uml|UML);'",
  458. "'&u(uml|UML);'",
  459. "'&A(uml|UML);'",
  460. "'&O(uml|UML);'",
  461. "'&U(uml|UML);'",
  462. "'&szlig;'i",
  463. );
  464. $replace = array("",
  465. "",
  466. "\\1",
  467. "\"",
  468. "&",
  469. "<",
  470. ">",
  471. " ",
  472. chr(161),
  473. chr(162),
  474. chr(163),
  475. chr(169),
  476. chr(174),
  477. chr(176),
  478. chr(39),
  479. chr(128),
  480. "ä",
  481. "ö",
  482. "ü",
  483. "Ä",
  484. "Ö",
  485. "Ü",
  486. "ß",
  487. );
  488. $text = preg_replace($search, $replace, $document);
  489. return $text;
  490. }
  491. /*======================================================================*\
  492. Function: _expandlinks
  493. Purpose: expand each link into a fully qualified URL
  494. Input: $links the links to qualify
  495. $URI the full URI to get the base from
  496. Output: $expandedLinks the expanded links
  497. \*======================================================================*/
  498. function _expandlinks($links, $URI)
  499. {
  500. preg_match("/^[^\?]+/", $URI, $match);
  501. $match = preg_replace("|/[^\/\.]+\.[^\/\.]+$|", "", $match[0]);
  502. $match = preg_replace("|/$|", "", $match);
  503. $match_part = parse_url($match);
  504. $match_root =
  505. $match_part["scheme"] . "://" . $match_part["host"];
  506. $search = array("|^http://" . preg_quote($this->host) . "|i",
  507. "|^(\/)|i",
  508. "|^(?!http://)(?!mailto:)|i",
  509. "|/\./|",
  510. "|/[^\/]+/\.\./|"
  511. );
  512. $replace = array("",
  513. $match_root . "/",
  514. $match . "/",
  515. "/",
  516. "/"
  517. );
  518. $expandedLinks = preg_replace($search, $replace, $links);
  519. return $expandedLinks;
  520. }
  521. /*======================================================================*\
  522. Function: _httprequest
  523. Purpose: go get the http(s) data from the server
  524. Input: $url the url to fetch
  525. $fp the current open file pointer
  526. $URI the full URI
  527. $body body contents to send if any (POST)
  528. Output:
  529. \*======================================================================*/
  530. function _httprequest($url, $fp, $URI, $http_method, $content_type = "", $body = "")
  531. {
  532. $cookie_headers = '';
  533. if ($this->passcookies && $this->_redirectaddr)
  534. $this->setcookies();
  535. $URI_PARTS = parse_url($URI);
  536. if (empty($url))
  537. $url = "/";
  538. $headers = $http_method . " " . $url . " " . $this->_httpversion . "\r\n";
  539. if (!empty($this->host) && !isset($this->rawheaders['Host'])) {
  540. $headers .= "Host: " . $this->host;
  541. if (!empty($this->port) && $this->port != '80')
  542. $headers .= ":" . $this->port;
  543. $headers .= "\r\n";
  544. }
  545. if (!empty($this->agent))
  546. $headers .= "User-Agent: " . $this->agent . "\r\n";
  547. if (!empty($this->accept))
  548. $headers .= "Accept: " . $this->accept . "\r\n";
  549. if ($this->use_gzip) {
  550. // make sure PHP was built with --with-zlib
  551. // and we can handle gzipp'ed data
  552. if (function_exists('gzinflate')) {
  553. $headers .= "Accept-encoding: gzip\r\n";
  554. } else {
  555. trigger_error(
  556. "use_gzip is on, but PHP was built without zlib support." .
  557. " Requesting file(s) without gzip encoding.",
  558. E_USER_NOTICE);
  559. }
  560. }
  561. if (!empty($this->referer))
  562. $headers .= "Referer: " . $this->referer . "\r\n";
  563. if (!empty($this->cookies)) {
  564. if (!is_array($this->cookies))
  565. $this->cookies = (array)$this->cookies;
  566. reset($this->cookies);
  567. if (count($this->cookies) > 0) {
  568. $cookie_headers .= 'Cookie: ';
  569. foreach ($this->cookies as $cookieKey => $cookieVal) {
  570. $cookie_headers .= $cookieKey . "=" . urlencode($cookieVal) . "; ";
  571. }
  572. $headers .= substr($cookie_headers, 0, -2) . "\r\n";
  573. }
  574. }
  575. if (!empty($this->rawheaders)) {
  576. if (!is_array($this->rawheaders))
  577. $this->rawheaders = (array)$this->rawheaders;
  578. foreach ($this->rawheaders as $headerKey => $headerVal)
  579. $headers .= $headerKey . ": " . $headerVal . "\r\n";
  580. }
  581. if (!empty($content_type)) {
  582. $headers .= "Content-type: $content_type";
  583. if ($content_type == "multipart/form-data")
  584. $headers .= "; boundary=" . $this->_mime_boundary;
  585. $headers .= "\r\n";
  586. }
  587. if (!empty($body))
  588. $headers .= "Content-length: " . strlen($body) . "\r\n";
  589. if (!empty($this->user) || !empty($this->pass))
  590. $headers .= "Authorization: Basic " . base64_encode($this->user . ":" . $this->pass) . "\r\n";
  591. //add proxy auth headers
  592. if (!empty($this->proxy_user))
  593. $headers .= 'Proxy-Authorization: ' . 'Basic ' . base64_encode($this->proxy_user . ':' . $this->proxy_pass) . "\r\n";
  594. $headers .= "\r\n";
  595. // set the read timeout if needed
  596. if ($this->read_timeout > 0)
  597. socket_set_timeout($fp, $this->read_timeout);
  598. $this->timed_out = false;
  599. fwrite($fp, $headers . $body, strlen($headers . $body));
  600. $this->_redirectaddr = false;
  601. unset($this->headers);
  602. // content was returned gzip encoded?
  603. $is_gzipped = false;
  604. while ($currentHeader = fgets($fp, $this->_maxlinelen)) {
  605. if ($this->read_timeout > 0 && $this->_check_timeout($fp)) {
  606. $this->status = -100;
  607. return false;
  608. }
  609. if ($currentHeader == "\r\n")
  610. break;
  611. // if a header begins with Location: or URI:, set the redirect
  612. if (preg_match("/^(Location:|URI:)/i", $currentHeader)) {
  613. // get URL portion of the redirect
  614. preg_match("/^(Location:|URI:)[ ]+(.*)/i", chop($currentHeader), $matches);
  615. // look for :// in the Location header to see if hostname is included
  616. if (!preg_match("|\:\/\/|", $matches[2])) {
  617. // no host in the path, so prepend
  618. $this->_redirectaddr = $URI_PARTS["scheme"] . "://" . $this->host . ":" . $this->port;
  619. // eliminate double slash
  620. if (!preg_match("|^/|", $matches[2]))
  621. $this->_redirectaddr .= "/" . $matches[2];
  622. else
  623. $this->_redirectaddr .= $matches[2];
  624. } else
  625. $this->_redirectaddr = $matches[2];
  626. }
  627. if (preg_match("|^HTTP/|", $currentHeader)) {
  628. if (preg_match("|^HTTP/[^\s]*\s(.*?)\s|", $currentHeader, $status)) {
  629. $this->status = $status[1];
  630. }
  631. $this->response_code = $currentHeader;
  632. }
  633. if (preg_match("/Content-Encoding: gzip/", $currentHeader)) {
  634. $is_gzipped = true;
  635. }
  636. $this->headers[] = $currentHeader;
  637. }
  638. $results = '';
  639. do {
  640. $_data = fread($fp, $this->maxlength);
  641. if (strlen($_data) == 0) {
  642. break;
  643. }
  644. $results .= $_data;
  645. } while (true);
  646. // gunzip
  647. if ($is_gzipped) {
  648. // per http://www.php.net/manual/en/function.gzencode.php
  649. $results = substr($results, 10);
  650. $results = gzinflate($results);
  651. }
  652. if ($this->read_timeout > 0 && $this->_check_timeout($fp)) {
  653. $this->status = -100;
  654. return false;
  655. }
  656. // check if there is a a redirect meta tag
  657. if (preg_match("'<meta[\s]*http-equiv[^>]*?content[\s]*=[\s]*[\"\']?\d+;[\s]*URL[\s]*=[\s]*([^\"\']*?)[\"\']?>'i", $results, $match)) {
  658. $this->_redirectaddr = $this->_expandlinks($match[1], $URI);
  659. }
  660. // have we hit our frame depth and is there frame src to fetch?
  661. if (($this->_framedepth < $this->maxframes) && preg_match_all("'<frame\s+.*src[\s]*=[\'\"]?([^\'\"\>]+)'i", $results, $match)) {
  662. $this->results[] = $results;
  663. for ($x = 0; $x < count($match[1]); $x++)
  664. $this->_frameurls[] = $this->_expandlinks($match[1][$x], $URI_PARTS["scheme"] . "://" . $this->host);
  665. } // have we already fetched framed content?
  666. elseif (is_array($this->results))
  667. $this->results[] = $results;
  668. // no framed content
  669. else
  670. $this->results = $results;
  671. return $this;
  672. }
  673. /*======================================================================*\
  674. Function: setcookies()
  675. Purpose: set cookies for a redirection
  676. \*======================================================================*/
  677. function setcookies()
  678. {
  679. for ($x = 0; $x < count($this->headers); $x++) {
  680. if (preg_match('/^set-cookie:[\s]+([^=]+)=([^;]+)/i', $this->headers[$x], $match))
  681. $this->cookies[$match[1]] = urldecode($match[2]);
  682. }
  683. return $this;
  684. }
  685. /*======================================================================*\
  686. Function: _check_timeout
  687. Purpose: checks whether timeout has occurred
  688. Input: $fp file pointer
  689. \*======================================================================*/
  690. function _check_timeout($fp)
  691. {
  692. if ($this->read_timeout > 0) {
  693. $fp_status = socket_get_status($fp);
  694. if ($fp_status["timed_out"]) {
  695. $this->timed_out = true;
  696. return true;
  697. }
  698. }
  699. return false;
  700. }
  701. /*======================================================================*\
  702. Function: _connect
  703. Purpose: make a socket connection
  704. Input: $fp file pointer
  705. \*======================================================================*/
  706. function _connect(&$fp)
  707. {
  708. if (!empty($this->proxy_host) && !empty($this->proxy_port)) {
  709. $this->_isproxy = true;
  710. $host = $this->proxy_host;
  711. $port = $this->proxy_port;
  712. if ($this->scheme == 'https') {
  713. trigger_error("HTTPS connections over proxy are currently not supported", E_USER_ERROR);
  714. exit;
  715. }
  716. } else {
  717. $host = $this->host;
  718. $port = $this->port;
  719. }
  720. $this->status = 0;
  721. $context_opts = array();
  722. if ($this->scheme == 'https') {
  723. // if cafile or capath is specified, enable certificate
  724. // verification (including name checks)
  725. if (isset($this->cafile) || isset($this->capath)) {
  726. $context_opts['ssl'] = array(
  727. 'verify_peer' => true,
  728. 'CN_match' => $this->host,
  729. 'disable_compression' => true,
  730. );
  731. if (isset($this->cafile))
  732. $context_opts['ssl']['cafile'] = $this->cafile;
  733. if (isset($this->capath))
  734. $context_opts['ssl']['capath'] = $this->capath;
  735. }
  736. $host = 'ssl://' . $host;
  737. }
  738. $context = stream_context_create($context_opts);
  739. if (version_compare(PHP_VERSION, '5.0.0', '>')) {
  740. if($this->scheme == 'http')
  741. $host = "tcp://" . $host;
  742. $fp = stream_socket_client(
  743. "$host:$port",
  744. $errno,
  745. $errmsg,
  746. $this->_fp_timeout,
  747. STREAM_CLIENT_CONNECT,
  748. $context);
  749. } else {
  750. $fp = fsockopen(
  751. $host,
  752. $port,
  753. $errno,
  754. $errstr,
  755. $this->_fp_timeout,
  756. $context);
  757. }
  758. if ($fp) {
  759. // socket connection succeeded
  760. return true;
  761. } else {
  762. // socket connection failed
  763. $this->status = $errno;
  764. switch ($errno) {
  765. case -3:
  766. $this->error = "socket creation failed (-3)";
  767. case -4:
  768. $this->error = "dns lookup failure (-4)";
  769. case -5:
  770. $this->error = "connection refused or timed out (-5)";
  771. default:
  772. $this->error = "connection failed (" . $errno . ")";
  773. }
  774. return false;
  775. }
  776. }
  777. /*======================================================================*\
  778. Function: _disconnect
  779. Purpose: disconnect a socket connection
  780. Input: $fp file pointer
  781. \*======================================================================*/
  782. function _disconnect($fp)
  783. {
  784. return (fclose($fp));
  785. }
  786. /*======================================================================*\
  787. Function: _prepare_post_body
  788. Purpose: Prepare post body according to encoding type
  789. Input: $formvars - form variables
  790. $formfiles - form upload files
  791. Output: post body
  792. \*======================================================================*/
  793. function _prepare_post_body($formvars, $formfiles)
  794. {
  795. settype($formvars, "array");
  796. settype($formfiles, "array");
  797. $postdata = '';
  798. if (count($formvars) == 0 && count($formfiles) == 0)
  799. return;
  800. switch ($this->_submit_type) {
  801. case "application/x-www-form-urlencoded":
  802. foreach ($formvars as $key => $val) {
  803. if (is_array($val) || is_object($val)) {
  804. foreach ($val as $cur_key => $cur_val) {
  805. $postdata .= urlencode($key) . "[]=" . urlencode($cur_val) . "&";
  806. }
  807. } else
  808. $postdata .= urlencode($key) . "=" . urlencode($val) . "&";
  809. }
  810. break;
  811. case "multipart/form-data":
  812. $this->_mime_boundary = "Snoopy" . md5(uniqid(microtime()));
  813. foreach ($formvars as $key => $val) {
  814. if (is_array($val) || is_object($val)) {
  815. foreach ($val as $cur_key => $cur_val) {
  816. $postdata .= "--" . $this->_mime_boundary . "\r\n";
  817. $postdata .= "Content-Disposition: form-data; name=\"$key\[\]\"\r\n\r\n";
  818. $postdata .= "$cur_val\r\n";
  819. }
  820. } else {
  821. $postdata .= "--" . $this->_mime_boundary . "\r\n";
  822. $postdata .= "Content-Disposition: form-data; name=\"$key\"\r\n\r\n";
  823. $postdata .= "$val\r\n";
  824. }
  825. }
  826. foreach ($formfiles as $field_name => $file_names) {
  827. settype($file_names, "array");
  828. foreach ($file_names as $file_name) {
  829. if (!is_readable($file_name)) continue;
  830. $fp = fopen($file_name, "r");
  831. $file_content = fread($fp, filesize($file_name));
  832. fclose($fp);
  833. $base_name = basename($file_name);
  834. $postdata .= "--" . $this->_mime_boundary . "\r\n";
  835. $postdata .= "Content-Disposition: form-data; name=\"$field_name\"; filename=\"$base_name\"\r\n\r\n";
  836. $postdata .= "$file_content\r\n";
  837. }
  838. }
  839. $postdata .= "--" . $this->_mime_boundary . "--\r\n";
  840. break;
  841. }
  842. return $postdata;
  843. }
  844. /*======================================================================*\
  845. Function: getResults
  846. Purpose: return the results of a request
  847. Output: string results
  848. \*======================================================================*/
  849. function getResults()
  850. {
  851. return $this->results;
  852. }
  853. }
  854. ?>