PageRenderTime 59ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/Doduo/WebKitSite/blog/wp-includes/js/tinymce/plugins/spellchecker/classes/HttpClient.class.php

https://github.com/weissms/owb-mirror
PHP | 339 lines | 304 code | 3 blank | 32 comment | 49 complexity | a76d00eacf8f07be342b41df83f0f062 MD5 | raw file
  1. <?php
  2. /* Version 0.9, 6th April 2003 - Simon Willison ( http://simon.incutio.com/ )
  3. Manual: http://scripts.incutio.com/httpclient/
  4. */
  5. class HttpClient {
  6. // Request vars
  7. var $host;
  8. var $port;
  9. var $path;
  10. var $method;
  11. var $postdata = '';
  12. var $cookies = array();
  13. var $referer;
  14. var $accept = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,image/jpeg,image/gif,*/*';
  15. var $accept_encoding = 'gzip';
  16. var $accept_language = 'en-us';
  17. var $user_agent = 'Incutio HttpClient v0.9';
  18. // Options
  19. var $timeout = 20;
  20. var $use_gzip = true;
  21. var $persist_cookies = true; // If true, received cookies are placed in the $this->cookies array ready for the next request
  22. // Note: This currently ignores the cookie path (and time) completely. Time is not important,
  23. // but path could possibly lead to security problems.
  24. var $persist_referers = true; // For each request, sends path of last request as referer
  25. var $debug = false;
  26. var $handle_redirects = true; // Auaomtically redirect if Location or URI header is found
  27. var $max_redirects = 5;
  28. var $headers_only = false; // If true, stops receiving once headers have been read.
  29. // Basic authorization variables
  30. var $username;
  31. var $password;
  32. // Response vars
  33. var $status;
  34. var $headers = array();
  35. var $content = '';
  36. var $errormsg;
  37. // Tracker variables
  38. var $redirect_count = 0;
  39. var $cookie_host = '';
  40. function HttpClient($host, $port=80) {
  41. $this->host = $host;
  42. $this->port = $port;
  43. }
  44. function get($path, $data = false) {
  45. $this->path = $path;
  46. $this->method = 'GET';
  47. if ($data) {
  48. $this->path .= '?'.$this->buildQueryString($data);
  49. }
  50. return $this->doRequest();
  51. }
  52. function post($path, $data) {
  53. $this->path = $path;
  54. $this->method = 'POST';
  55. $this->postdata = $this->buildQueryString($data);
  56. return $this->doRequest();
  57. }
  58. function buildQueryString($data) {
  59. $querystring = '';
  60. if (is_array($data)) {
  61. // Change data in to postable data
  62. foreach ($data as $key => $val) {
  63. if (is_array($val)) {
  64. foreach ($val as $val2) {
  65. $querystring .= urlencode($key).'='.urlencode($val2).'&';
  66. }
  67. } else {
  68. $querystring .= urlencode($key).'='.urlencode($val).'&';
  69. }
  70. }
  71. $querystring = substr($querystring, 0, -1); // Eliminate unnecessary &
  72. } else {
  73. $querystring = $data;
  74. }
  75. return $querystring;
  76. }
  77. function doRequest() {
  78. // Performs the actual HTTP request, returning true or false depending on outcome
  79. if (!$fp = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout)) {
  80. // Set error message
  81. switch($errno) {
  82. case -3:
  83. $this->errormsg = 'Socket creation failed (-3)';
  84. case -4:
  85. $this->errormsg = 'DNS lookup failure (-4)';
  86. case -5:
  87. $this->errormsg = 'Connection refused or timed out (-5)';
  88. default:
  89. $this->errormsg = 'Connection failed ('.$errno.')';
  90. $this->errormsg .= ' '.$errstr;
  91. $this->debug($this->errormsg);
  92. }
  93. return false;
  94. }
  95. socket_set_timeout($fp, $this->timeout);
  96. $request = $this->buildRequest();
  97. $this->debug('Request', $request);
  98. fwrite($fp, $request);
  99. // Reset all the variables that should not persist between requests
  100. $this->headers = array();
  101. $this->content = '';
  102. $this->errormsg = '';
  103. // Set a couple of flags
  104. $inHeaders = true;
  105. $atStart = true;
  106. // Now start reading back the response
  107. while (!feof($fp)) {
  108. $line = fgets($fp, 4096);
  109. if ($atStart) {
  110. // Deal with first line of returned data
  111. $atStart = false;
  112. if (!preg_match('/HTTP\/(\\d\\.\\d)\\s*(\\d+)\\s*(.*)/', $line, $m)) {
  113. $this->errormsg = "Status code line invalid: ".htmlentities($line);
  114. $this->debug($this->errormsg);
  115. return false;
  116. }
  117. $http_version = $m[1]; // not used
  118. $this->status = $m[2];
  119. $status_string = $m[3]; // not used
  120. $this->debug(trim($line));
  121. continue;
  122. }
  123. if ($inHeaders) {
  124. if (trim($line) == '') {
  125. $inHeaders = false;
  126. $this->debug('Received Headers', $this->headers);
  127. if ($this->headers_only) {
  128. break; // Skip the rest of the input
  129. }
  130. continue;
  131. }
  132. if (!preg_match('/([^:]+):\\s*(.*)/', $line, $m)) {
  133. // Skip to the next header
  134. continue;
  135. }
  136. $key = strtolower(trim($m[1]));
  137. $val = trim($m[2]);
  138. // Deal with the possibility of multiple headers of same name
  139. if (isset($this->headers[$key])) {
  140. if (is_array($this->headers[$key])) {
  141. $this->headers[$key][] = $val;
  142. } else {
  143. $this->headers[$key] = array($this->headers[$key], $val);
  144. }
  145. } else {
  146. $this->headers[$key] = $val;
  147. }
  148. continue;
  149. }
  150. // We're not in the headers, so append the line to the contents
  151. $this->content .= $line;
  152. }
  153. fclose($fp);
  154. // If data is compressed, uncompress it
  155. if (isset($this->headers['content-encoding']) && $this->headers['content-encoding'] == 'gzip') {
  156. $this->debug('Content is gzip encoded, unzipping it');
  157. $this->content = substr($this->content, 10); // See http://www.php.net/manual/en/function.gzencode.php
  158. $this->content = gzinflate($this->content);
  159. }
  160. // If $persist_cookies, deal with any cookies
  161. if ($this->persist_cookies && isset($this->headers['set-cookie']) && $this->host == $this->cookie_host) {
  162. $cookies = $this->headers['set-cookie'];
  163. if (!is_array($cookies)) {
  164. $cookies = array($cookies);
  165. }
  166. foreach ($cookies as $cookie) {
  167. if (preg_match('/([^=]+)=([^;]+);/', $cookie, $m)) {
  168. $this->cookies[$m[1]] = $m[2];
  169. }
  170. }
  171. // Record domain of cookies for security reasons
  172. $this->cookie_host = $this->host;
  173. }
  174. // If $persist_referers, set the referer ready for the next request
  175. if ($this->persist_referers) {
  176. $this->debug('Persisting referer: '.$this->getRequestURL());
  177. $this->referer = $this->getRequestURL();
  178. }
  179. // Finally, if handle_redirects and a redirect is sent, do that
  180. if ($this->handle_redirects) {
  181. if (++$this->redirect_count >= $this->max_redirects) {
  182. $this->errormsg = 'Number of redirects exceeded maximum ('.$this->max_redirects.')';
  183. $this->debug($this->errormsg);
  184. $this->redirect_count = 0;
  185. return false;
  186. }
  187. $location = isset($this->headers['location']) ? $this->headers['location'] : '';
  188. $uri = isset($this->headers['uri']) ? $this->headers['uri'] : '';
  189. if ($location || $uri) {
  190. $url = parse_url($location.$uri);
  191. // This will FAIL if redirect is to a different site
  192. return $this->get($url['path']);
  193. }
  194. }
  195. return true;
  196. }
  197. function buildRequest() {
  198. $headers = array();
  199. $headers[] = "{$this->method} {$this->path} HTTP/1.0"; // Using 1.1 leads to all manner of problems, such as "chunked" encoding
  200. $headers[] = "Host: {$this->host}";
  201. $headers[] = "User-Agent: {$this->user_agent}";
  202. $headers[] = "Accept: {$this->accept}";
  203. if ($this->use_gzip) {
  204. $headers[] = "Accept-encoding: {$this->accept_encoding}";
  205. }
  206. $headers[] = "Accept-language: {$this->accept_language}";
  207. if ($this->referer) {
  208. $headers[] = "Referer: {$this->referer}";
  209. }
  210. // Cookies
  211. if ($this->cookies) {
  212. $cookie = 'Cookie: ';
  213. foreach ($this->cookies as $key => $value) {
  214. $cookie .= "$key=$value; ";
  215. }
  216. $headers[] = $cookie;
  217. }
  218. // Basic authentication
  219. if ($this->username && $this->password) {
  220. $headers[] = 'Authorization: BASIC '.base64_encode($this->username.':'.$this->password);
  221. }
  222. // If this is a POST, set the content type and length
  223. if ($this->postdata) {
  224. $headers[] = 'Content-Type: application/x-www-form-urlencoded';
  225. $headers[] = 'Content-Length: '.strlen($this->postdata);
  226. }
  227. $request = implode("\r\n", $headers)."\r\n\r\n".$this->postdata;
  228. return $request;
  229. }
  230. function getStatus() {
  231. return $this->status;
  232. }
  233. function getContent() {
  234. return $this->content;
  235. }
  236. function getHeaders() {
  237. return $this->headers;
  238. }
  239. function getHeader($header) {
  240. $header = strtolower($header);
  241. if (isset($this->headers[$header])) {
  242. return $this->headers[$header];
  243. } else {
  244. return false;
  245. }
  246. }
  247. function getError() {
  248. return $this->errormsg;
  249. }
  250. function getCookies() {
  251. return $this->cookies;
  252. }
  253. function getRequestURL() {
  254. $url = 'http://'.$this->host;
  255. if ($this->port != 80) {
  256. $url .= ':'.$this->port;
  257. }
  258. $url .= $this->path;
  259. return $url;
  260. }
  261. // Setter methods
  262. function setUserAgent($string) {
  263. $this->user_agent = $string;
  264. }
  265. function setAuthorization($username, $password) {
  266. $this->username = $username;
  267. $this->password = $password;
  268. }
  269. function setCookies($array) {
  270. $this->cookies = $array;
  271. }
  272. // Option setting methods
  273. function useGzip($boolean) {
  274. $this->use_gzip = $boolean;
  275. }
  276. function setPersistCookies($boolean) {
  277. $this->persist_cookies = $boolean;
  278. }
  279. function setPersistReferers($boolean) {
  280. $this->persist_referers = $boolean;
  281. }
  282. function setHandleRedirects($boolean) {
  283. $this->handle_redirects = $boolean;
  284. }
  285. function setMaxRedirects($num) {
  286. $this->max_redirects = $num;
  287. }
  288. function setHeadersOnly($boolean) {
  289. $this->headers_only = $boolean;
  290. }
  291. function setDebug($boolean) {
  292. $this->debug = $boolean;
  293. }
  294. // "Quick" static methods
  295. function quickGet($url) {
  296. $bits = parse_url($url);
  297. $host = $bits['host'];
  298. $port = isset($bits['port']) ? $bits['port'] : 80;
  299. $path = isset($bits['path']) ? $bits['path'] : '/';
  300. if (isset($bits['query'])) {
  301. $path .= '?'.$bits['query'];
  302. }
  303. $client = new HttpClient($host, $port);
  304. if (!$client->get($path)) {
  305. return false;
  306. } else {
  307. return $client->getContent();
  308. }
  309. }
  310. function quickPost($url, $data) {
  311. $bits = parse_url($url);
  312. $host = $bits['host'];
  313. $port = isset($bits['port']) ? $bits['port'] : 80;
  314. $path = isset($bits['path']) ? $bits['path'] : '/';
  315. $client = new HttpClient($host, $port);
  316. if (!$client->post($path, $data)) {
  317. return false;
  318. } else {
  319. return $client->getContent();
  320. }
  321. }
  322. function debug($msg, $object = false) {
  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. }
  336. ?>