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

/imdb/imdb/browseremulator.class.php

https://github.com/Bigjoos/U-232-V3
PHP | 210 lines | 129 code | 18 blank | 63 comment | 28 complexity | 00967abfda466387dbf8258e147b41a3 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. # -----------------------------------------------------------------------------
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License
  10. # as published by the Free Software Foundation; either version 2
  11. # of the License, or (at your option) any later version.
  12. #
  13. # This program 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
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. ###############################################################################
  22. /** BrowserEmulator class. Provides methods for opening urls and emulating
  23. * a web browser request.
  24. * @package MDBApi
  25. * @class BrowserEmulator
  26. * @author Kai Blankenhorn (kai AT bitfolge DOT de)
  27. */
  28. class BrowserEmulator {
  29. var $headerLines = Array ();
  30. var $postData = Array ();
  31. var $authUser = "";
  32. var $authPass = "";
  33. var $port;
  34. var $lastResponse = Array ();
  35. function BrowserEmulator () {
  36. $this->resetHeaderLines ();
  37. $this->resetPort ();
  38. }
  39. /** Add a single header field to the HTTP request header. The resulting
  40. * header line will have the format "$name: $value\n"
  41. * @method addHeaderLine
  42. * @param string name
  43. * @param string value
  44. */
  45. function addHeaderLine ($name, $value) {
  46. $this->headerLines[$name] = $value;
  47. }
  48. /** Delete all custom header lines. This will not remove the User-Agent
  49. * header field, which is necessary for correct operation.
  50. * @method resetHeaderLines
  51. */
  52. function resetHeaderLines () {
  53. $this->headerLines = Array ();
  54. if ( in_array('HTTP_USER_AGENT',array_keys($_SERVER)) ) $user_agent = $_SERVER['HTTP_USER_AGENT'];
  55. else $user_agent = 'Mozilla/5.0 (X11; U; Linux i686; de; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3';
  56. $this->headerLines["User-Agent"] = $user_agent;
  57. }
  58. /** Add a post parameter. Post parameters are sent in the body of an HTTP POST request.
  59. * @method addPostData
  60. * @param string name
  61. * @param string value
  62. */
  63. function addPostData ($name, $value) {
  64. $this->postData[$name] = $value;
  65. }
  66. /** Delete all custom post parameters.
  67. * @method resetPostData
  68. */
  69. function resetPostData () {
  70. $this->postData = Array ();
  71. }
  72. /** Set an auth user and password to use for the request.
  73. * Set both as empty strings to disable authentication.
  74. * @method setAuth
  75. * @param string user
  76. * @param string pass
  77. */
  78. function setAuth ($user, $pass) {
  79. $this->authUser = $user;
  80. $this->authPass = $pass;
  81. }
  82. /** Select a custom port to use for the request.
  83. * @method setPort
  84. * @param integer portNumber
  85. */
  86. function setPort ($portNumber) {
  87. $this->port = $portNumber;
  88. }
  89. /** Reset the port used for request to the HTTP default (80).
  90. * @method resetPort
  91. */
  92. function resetPort () {
  93. $this->port = 80;
  94. }
  95. /** Make an fopen call to $url with the parameters set by previous member
  96. * method calls. Send all set headers, post data and user authentication data.
  97. * @method fopen
  98. * @param string url
  99. * @return mixed file handle on success, FALSE otherwise
  100. */
  101. function fopen ($url) {
  102. $debug = false;
  103. $this->lastResponse = Array ();
  104. preg_match ("~([a-z]*://)?([^:^/]*)(:([0-9]{1,5}))?(/.*)?~i", $url, $matches);
  105. if ($debug) var_dump ($matches);
  106. $protocol = $matches[1];
  107. $server = $matches[2];
  108. $port = $matches[4];
  109. $path = $matches[5];
  110. if ($port != "") $this->setPort ($port);
  111. if ($path == "") $path = "/";
  112. $socket = false;
  113. $socket = @fsockopen ($server, $this->port);
  114. if ($socket) {
  115. $this->headerLines["Host"] = $server;
  116. if ($this->authUser != "" AND $this->authPass != "")
  117. $headers["Authorization"] = "Basic ".base64_encode ($this->authUser.":".$this->authPass);
  118. if (count ($this->postData) == 0) $request = "GET $path HTTP/1.0\r\n";
  119. else $request = "POST $path HTTP/1.0\r\n";
  120. if ($debug) echo $request;
  121. fputs ($socket, $request);
  122. if (count ($this->postData) > 0) {
  123. $PostStringArray = Array ();
  124. foreach ($this->postData AS $key => $value) $PostStringArray[] = "$key=$value";
  125. $PostString = join ("&", $PostStringArray);
  126. $this->headerLines["Content-Length"] = strlen ($PostString);
  127. }
  128. foreach ($this->headerLines AS $key => $value) {
  129. if ($debug)
  130. echo "$key: $value\n";
  131. fputs ($socket, "$key: $value\r\n");
  132. }
  133. if ($debug) echo "\n";
  134. fputs ($socket, "\r\n");
  135. if (count ($this->postData) > 0) {
  136. if ($debug)
  137. echo "$PostString";
  138. fputs ($socket, $PostString."\r\n");
  139. }
  140. }
  141. if ($debug) echo "\n";
  142. if ($socket) {
  143. $line = fgets ($socket, 1000);
  144. if ($debug) echo $line;
  145. $this->lastResponse[] = $line;
  146. $status = substr ($line, 9, 3);
  147. while (trim ($line = fgets ($socket, 1000)) != "") {
  148. if ($debug) echo "$line";
  149. $this->lastResponse[] = $line;
  150. if ($status == "401" AND strpos ($line, "WWW-Authenticate: Basic realm=\"") === 0) {
  151. fclose ($socket);
  152. return FALSE;
  153. }
  154. }
  155. }
  156. return $socket;
  157. }
  158. /** Make an file call to $url with the parameters set by previous member
  159. * method calls. Send all set headers, post data and user authentication data.
  160. * @method file
  161. * @param string url
  162. * @return mixed array file on success, FALSE otherwise
  163. */
  164. function file ($url) {
  165. $file = Array ();
  166. $socket = $this->fopen ($url);
  167. if ($socket) {
  168. $file = Array ();
  169. while (!feof ($socket)) $file[] = fgets ($socket, 10000);
  170. } else {
  171. return FALSE;
  172. }
  173. return $file;
  174. }
  175. /** Get the latest server response
  176. * @method getLastResponseHeaders
  177. * @return array lastResponse <ul>
  178. * <li>0: HTTP response (e.g. "HTTP/1.1 404 Not Found")</li>
  179. * <li>1: Date (e.g. "Date: Sun, 08 Jun 2008 16:36:37 GMT")</li>
  180. * <li>2: ServerInfo (e.g. "Server: Apache/2.2.3 (Ubuntu) PHP/5.2.1"</li>
  181. * <li>3: Content length (e.g. "Content-Length: 214"</li>
  182. * <li>4: Connection (e.g. "Connection: close")</li>
  183. * <li>5: Content type (e.g. "Content-Type: text/html; charset=iso-8859-1")</li></ul>
  184. */
  185. function getLastResponseHeaders () {
  186. return $this->lastResponse;
  187. }
  188. } // end class
  189. ?>