PageRenderTime 26ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/imdb/browseremulator.class.php

https://github.com/itspriddle/itt-capstone
PHP | 270 lines | 164 code | 21 blank | 85 comment | 26 complexity | 0040da795d45911f0fcd9f00b15395b0 MD5 | raw file
  1. <?php
  2. /***************************************************************************
  3. Browser Emulating file functions v2.0
  4. (c) Kai Blankenhorn
  5. www.bitfolge.de/en
  6. kaib@bitfolge.de
  7. This program is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU General Public License
  9. as published by the Free Software Foundation; either version 2
  10. of the License, or (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. ****************************************************************************
  19. Changelog:
  20. v2.0 03-09-03
  21. added a wrapper class; this has the advantage that you no longer need
  22. to specify a lot of parameters, just call the methods to set
  23. each option
  24. added option to use a special port number, may be given by setPort or
  25. as part of the URL (e.g. server.com:80)
  26. added getLastResponseHeaders()
  27. v1.5
  28. added Basic HTTP user authorization
  29. minor optimizations
  30. v1.0
  31. initial release
  32. ***************************************************************************/
  33. /**
  34. * BrowserEmulator class. Provides methods for opening urls and emulating
  35. * a web browser request.
  36. **/
  37. class BrowserEmulator {
  38. var $headerLines = Array ();
  39. var $postData = Array ();
  40. var $authUser = "";
  41. var $authPass = "";
  42. var $port;
  43. var $lastResponse = Array ();
  44. function BrowserEmulator () {
  45. $this->resetHeaderLines ();
  46. $this->resetPort ();
  47. }
  48. /**
  49. * Adds a single header field to the HTTP request header. The resulting header
  50. * line will have the format
  51. * $name: $value\n
  52. **/
  53. function addHeaderLine ($name, $value) {
  54. $this->headerLines[$name] = $value;
  55. }
  56. /**
  57. * Deletes all custom header lines. This will not remove the User-Agent header field,
  58. * which is necessary for correct operation.
  59. **/
  60. function resetHeaderLines () {
  61. $this->headerLines = Array ();
  62. /*******************************************************************************/
  63. /************** YOU MAX SET THE USER AGENT STRING HERE *******************/
  64. /* */
  65. /* default is "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)", */
  66. /* which means Internet Explorer 6.0 on WinXP */
  67. $this->headerLines["User-Agent"] =
  68. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
  69. /*******************************************************************************/
  70. }
  71. /**
  72. * Add a post parameter. Post parameters are sent in the body of an HTTP POST request.
  73. **/
  74. function addPostData ($name, $value) {
  75. $this->postData[$name] = $value;
  76. }
  77. /**
  78. * Deletes all custom post parameters.
  79. **/
  80. function resetPostData () {
  81. $this->postData = Array ();
  82. }
  83. /**
  84. * Sets an auth user and password to use for the request.
  85. * Set both as empty strings to disable authentication.
  86. **/
  87. function setAuth ($user, $pass) {
  88. $this->authUser = $user;
  89. $this->authPass = $pass;
  90. }
  91. /**
  92. * Selects a custom port to use for the request.
  93. **/
  94. function setPort ($portNumber) {
  95. $this->port = $portNumber;
  96. }
  97. /**
  98. * Resets the port used for request to the HTTP default (80).
  99. **/
  100. function resetPort () {
  101. $this->port = 80;
  102. }
  103. /**
  104. * Make an fopen call to $url with the parameters set by previous member
  105. * method calls. Send all set headers, post data and user authentication data.
  106. * Returns a file handle on success, or false on failure.
  107. **/
  108. function fopen ($url) {
  109. $debug = false;
  110. $this->lastResponse = Array ();
  111. preg_match ("~([a-z]*://)?([^:^/]*)(:([0-9]{1,5}))?(/.*)?~i", $url,
  112. $matches);
  113. if ($debug)
  114. var_dump ($matches);
  115. $protocol = $matches[1];
  116. $server = $matches[2];
  117. $port = $matches[4];
  118. $path = $matches[5];
  119. if ($port != "") {
  120. $this->setPort ($port);
  121. }
  122. if ($path == "")
  123. $path = "/";
  124. $socket = false;
  125. $socket = fsockopen ($server, $this->port);
  126. if ($socket) {
  127. $this->headerLines["Host"] = $server;
  128. if ($this->authUser != "" AND $this->authPass != "") {
  129. $headers["Authorization"] =
  130. "Basic ".base64_encode ($this->authUser.":".$this->
  131. authPass);
  132. }
  133. if (count ($this->postData) == 0) {
  134. $request = "GET $path HTTP/1.0\r\n";
  135. }
  136. else {
  137. $request = "POST $path HTTP/1.0\r\n";
  138. }
  139. if ($debug)
  140. echo $request;
  141. fputs ($socket, $request);
  142. if (count ($this->postData) > 0) {
  143. $PostStringArray = Array ();
  144. foreach ($this->postData AS $key => $value) {
  145. $PostStringArray[] = "$key=$value";
  146. }
  147. $PostString = join ("&", $PostStringArray);
  148. $this->headerLines["Content-Length"] =
  149. strlen ($PostString);
  150. }
  151. foreach ($this->headerLines AS $key => $value) {
  152. if ($debug)
  153. echo "$key: $value\n";
  154. fputs ($socket, "$key: $value\r\n");
  155. }
  156. if ($debug)
  157. echo "\n";
  158. fputs ($socket, "\r\n");
  159. if (count ($this->postData) > 0) {
  160. if ($debug)
  161. echo "$PostString";
  162. fputs ($socket, $PostString."\r\n");
  163. }
  164. }
  165. if ($debug)
  166. echo "\n";
  167. if ($socket) {
  168. $line = fgets ($socket, 1000);
  169. if ($debug)
  170. echo $line;
  171. $this->lastResponse[] = $line;
  172. $status = substr ($line, 9, 3);
  173. while (trim ($line = fgets ($socket, 1000)) != "") {
  174. if ($debug)
  175. echo "$line";
  176. $this->lastResponse[] = $line;
  177. if ($status == "401" AND strpos ($line, "WWW-Authenticate: Basic realm=\"") === 0) {
  178. fclose ($socket);
  179. return FALSE;
  180. }
  181. }
  182. }
  183. return $socket;
  184. }
  185. /**
  186. * Make an file call to $url with the parameters set by previous member
  187. * method calls. Send all set headers, post data and user authentication data.
  188. * Returns the requested file as an array on success, or false on failure.
  189. **/
  190. function file ($url) {
  191. $file = Array ();
  192. $socket = $this->fopen ($url);
  193. if ($socket) {
  194. $file = Array ();
  195. while (!feof ($socket)) {
  196. $file[] = fgets ($socket, 10000);
  197. }
  198. }
  199. else {
  200. return FALSE;
  201. }
  202. return $file;
  203. }
  204. function getLastResponseHeaders () {
  205. return $this->lastResponse;
  206. }
  207. }
  208. // example code
  209. /*
  210. $be = new BrowserEmulator();
  211. //$be->addHeaderLine("Referer", "http://previous.server.com/");
  212. //$be->addHeaderLine("Accept-Encoding", "x-compress; x-zip");
  213. //$be->addPostData("Submit", "OK");
  214. //$be->addPostData("item", "42");
  215. //$be->setAuth("admin", "secretpass");
  216. // also possible:
  217. // $be->setPort(10080);
  218. $file = $be->fopen("http://us.imdb.com/Title?0209144");
  219. $response = $be->getLastResponseHeaders();
  220. while ($line = fgets($file, 1024)) {
  221. // do something with the file
  222. echo $line;
  223. }
  224. fclose($file);
  225. */
  226. ?>