PrinterFeieHttpClient.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <?php
  2. namespace app\model;
  3. class PrinterFeieHttpClient
  4. {
  5. // Request vars
  6. var $host;
  7. var $port;
  8. var $path;
  9. var $method;
  10. var $postdata = '';
  11. var $cookies = array();
  12. var $referer;
  13. var $accept = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,image/jpeg,image/gif,*/*';
  14. var $accept_encoding = 'gzip';
  15. var $accept_language = 'en-us';
  16. var $user_agent = 'Incutio HttpClient v0.9';
  17. var $timeout = 20;
  18. var $use_gzip = true;
  19. var $persist_cookies = true;
  20. var $persist_referers = true;
  21. var $debug = false;
  22. var $handle_redirects = true;
  23. var $max_redirects = 5;
  24. var $headers_only = false;
  25. var $username;
  26. var $password;
  27. var $status;
  28. var $headers = array();
  29. var $content = '';
  30. var $errormsg;
  31. var $redirect_count = 0;
  32. var $cookie_host = '';
  33. function __construct($host, $port = 80)
  34. {
  35. $this->host = $host;
  36. $this->port = $port;
  37. }
  38. function get($path, $data = false)
  39. {
  40. $this->path = $path;
  41. $this->method = 'GET';
  42. if ($data) {
  43. $this->path .= '?' . $this->buildQueryString($data);
  44. }
  45. return $this->doRequest();
  46. }
  47. function post($path, $data)
  48. {
  49. $this->path = $path;
  50. $this->method = 'POST';
  51. $this->postdata = $this->buildQueryString($data);
  52. return $this->doRequest();
  53. }
  54. function buildQueryString($data)
  55. {
  56. $querystring = '';
  57. if (is_array($data)) {
  58. foreach ($data as $key => $val) {
  59. if (is_array($val)) {
  60. foreach ($val as $val2) {
  61. $querystring .= urlencode($key) . '=' . urlencode($val2) . '&';
  62. }
  63. } else {
  64. $querystring .= urlencode($key) . '=' . urlencode($val) . '&';
  65. }
  66. }
  67. $querystring = substr($querystring, 0, -1); // Eliminate unnecessary &
  68. } else {
  69. $querystring = $data;
  70. }
  71. return $querystring;
  72. }
  73. function doRequest()
  74. {
  75. if (!$fp = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout)) {
  76. switch ($errno) {
  77. case -3:
  78. $this->errormsg = 'Socket creation failed (-3)';
  79. case -4:
  80. $this->errormsg = 'DNS lookup failure (-4)';
  81. case -5:
  82. $this->errormsg = 'Connection refused or timed out (-5)';
  83. default:
  84. $this->errormsg = 'Connection failed (' . $errno . ')';
  85. $this->errormsg .= ' ' . $errstr;
  86. $this->debug($this->errormsg);
  87. }
  88. return false;
  89. }
  90. socket_set_timeout($fp, $this->timeout);
  91. $request = $this->buildRequest();
  92. $this->debug('Request', $request);
  93. fwrite($fp, $request);
  94. $this->headers = array();
  95. $this->content = '';
  96. $this->errormsg = '';
  97. $inHeaders = true;
  98. $atStart = true;
  99. while (!feof($fp)) {
  100. $line = fgets($fp, 4096);
  101. if ($atStart) {
  102. $atStart = false;
  103. if (!preg_match('/HTTP\/(\\d\\.\\d)\\s*(\\d+)\\s*(.*)/', $line, $m)) {
  104. $this->errormsg = "Status code line invalid: " . htmlentities($line);
  105. $this->debug($this->errormsg);
  106. return false;
  107. }
  108. $http_version = $m[1];
  109. $this->status = $m[2];
  110. $status_string = $m[3];
  111. $this->debug(trim($line));
  112. continue;
  113. }
  114. if ($inHeaders) {
  115. if (trim($line) == '') {
  116. $inHeaders = false;
  117. $this->debug('Received Headers', $this->headers);
  118. if ($this->headers_only) {
  119. break;
  120. }
  121. continue;
  122. }
  123. if (!preg_match('/([^:]+):\\s*(.*)/', $line, $m)) {
  124. continue;
  125. }
  126. $key = strtolower(trim($m[1]));
  127. $val = trim($m[2]);
  128. if (isset($this->headers[$key])) {
  129. if (is_array($this->headers[$key])) {
  130. $this->headers[$key][] = $val;
  131. } else {
  132. $this->headers[$key] = array($this->headers[$key], $val);
  133. }
  134. } else {
  135. $this->headers[$key] = $val;
  136. }
  137. continue;
  138. }
  139. $this->content .= $line;
  140. }
  141. fclose($fp);
  142. if (isset($this->headers['content-encoding']) && $this->headers['content-encoding'] == 'gzip') {
  143. $this->debug('Content is gzip encoded, unzipping it');
  144. $this->content = substr($this->content, 10);
  145. $this->content = gzinflate($this->content);
  146. }
  147. if ($this->persist_cookies && isset($this->headers['set-cookie']) && $this->host == $this->cookie_host) {
  148. $cookies = $this->headers['set-cookie'];
  149. if (!is_array($cookies)) {
  150. $cookies = array($cookies);
  151. }
  152. foreach ($cookies as $cookie) {
  153. if (preg_match('/([^=]+)=([^;]+);/', $cookie, $m)) {
  154. $this->cookies[$m[1]] = $m[2];
  155. }
  156. }
  157. $this->cookie_host = $this->host;
  158. }
  159. if ($this->persist_referers) {
  160. $this->debug('Persisting referer: ' . $this->getRequestURL());
  161. $this->referer = $this->getRequestURL();
  162. }
  163. if ($this->handle_redirects) {
  164. if (++$this->redirect_count >= $this->max_redirects) {
  165. $this->errormsg = 'Number of redirects exceeded maximum (' . $this->max_redirects . ')';
  166. $this->debug($this->errormsg);
  167. $this->redirect_count = 0;
  168. return false;
  169. }
  170. $location = isset($this->headers['location']) ? $this->headers['location'] : '';
  171. $uri = isset($this->headers['uri']) ? $this->headers['uri'] : '';
  172. if ($location || $uri) {
  173. $url = parse_url($location . $uri);
  174. return $this->get($url['path']);
  175. }
  176. }
  177. return true;
  178. }
  179. function buildRequest()
  180. {
  181. $headers = array();
  182. $headers[] = "{$this->method} {$this->path} HTTP/1.0";
  183. $headers[] = "Host: {$this->host}";
  184. $headers[] = "User-Agent: {$this->user_agent}";
  185. $headers[] = "Accept: {$this->accept}";
  186. if ($this->use_gzip) {
  187. $headers[] = "Accept-encoding: {$this->accept_encoding}";
  188. }
  189. $headers[] = "Accept-language: {$this->accept_language}";
  190. if ($this->referer) {
  191. $headers[] = "Referer: {$this->referer}";
  192. }
  193. if ($this->cookies) {
  194. $cookie = 'Cookie: ';
  195. foreach ($this->cookies as $key => $value) {
  196. $cookie .= "$key=$value; ";
  197. }
  198. $headers[] = $cookie;
  199. }
  200. if ($this->username && $this->password) {
  201. $headers[] = 'Authorization: BASIC ' . base64_encode($this->username . ':' . $this->password);
  202. }
  203. if ($this->postdata) {
  204. $headers[] = 'Content-Type: application/x-www-form-urlencoded';
  205. $headers[] = 'Content-Length: ' . strlen($this->postdata);
  206. }
  207. if (!empty($headers)) {
  208. $headers = implode("\r\n", $headers);
  209. }
  210. $request = $headers . "\r\n\r\n" . $this->postdata;
  211. return $request;
  212. }
  213. function getStatus()
  214. {
  215. return $this->status;
  216. }
  217. function getContent()
  218. {
  219. return $this->content;
  220. }
  221. function getHeaders()
  222. {
  223. return $this->headers;
  224. }
  225. function getHeader($header)
  226. {
  227. $header = strtolower($header);
  228. if (isset($this->headers[$header])) {
  229. return $this->headers[$header];
  230. } else {
  231. return false;
  232. }
  233. }
  234. function getError()
  235. {
  236. return $this->errormsg;
  237. }
  238. function getCookies()
  239. {
  240. return $this->cookies;
  241. }
  242. function getRequestURL()
  243. {
  244. $url = 'http://' . $this->host;
  245. if ($this->port != 80) {
  246. $url .= ':' . $this->port;
  247. }
  248. $url .= $this->path;
  249. return $url;
  250. }
  251. function setUserAgent($string)
  252. {
  253. $this->user_agent = $string;
  254. }
  255. function setAuthorization($username, $password)
  256. {
  257. $this->username = $username;
  258. $this->password = $password;
  259. }
  260. function setCookies($array)
  261. {
  262. $this->cookies = $array;
  263. }
  264. function useGzip($boolean)
  265. {
  266. $this->use_gzip = $boolean;
  267. }
  268. function setPersistCookies($boolean)
  269. {
  270. $this->persist_cookies = $boolean;
  271. }
  272. function setPersistReferers($boolean)
  273. {
  274. $this->persist_referers = $boolean;
  275. }
  276. function setHandleRedirects($boolean)
  277. {
  278. $this->handle_redirects = $boolean;
  279. }
  280. function setMaxRedirects($num)
  281. {
  282. $this->max_redirects = $num;
  283. }
  284. function setHeadersOnly($boolean)
  285. {
  286. $this->headers_only = $boolean;
  287. }
  288. function setDebug($boolean)
  289. {
  290. $this->debug = $boolean;
  291. }
  292. function quickGet($url)
  293. {
  294. $bits = parse_url($url);
  295. $host = $bits['host'];
  296. $port = isset($bits['port']) ? $bits['port'] : 80;
  297. $path = isset($bits['path']) ? $bits['path'] : '/';
  298. if (isset($bits['query'])) {
  299. $path .= '?' . $bits['query'];
  300. }
  301. $client = new HttpClient($host, $port);
  302. if (!$client->get($path)) {
  303. return false;
  304. } else {
  305. return $client->getContent();
  306. }
  307. }
  308. function quickPost($url, $data)
  309. {
  310. $bits = parse_url($url);
  311. $host = $bits['host'];
  312. $port = isset($bits['port']) ? $bits['port'] : 80;
  313. $path = isset($bits['path']) ? $bits['path'] : '/';
  314. $client = new HttpClient($host, $port);
  315. if (!$client->post($path, $data)) {
  316. return false;
  317. } else {
  318. return $client->getContent();
  319. }
  320. }
  321. function debug($msg, $object = false)
  322. {
  323. if ($this->debug) {
  324. print '<div style="border: 1px solid red; padding: 0.5em; margin: 0.5em;"><strong>HttpClient Debug:</strong> ' . $msg;
  325. if ($object) {
  326. ob_start();
  327. print_r($object);
  328. $content = htmlentities(ob_get_contents());
  329. ob_end_clean();
  330. print '<pre>' . $content . '</pre>';
  331. }
  332. print '</div>';
  333. }
  334. }
  335. }