/old/imdb/mdb_request.class.php

https://github.com/hetwieg/media_hdd · PHP · 117 lines · 74 code · 4 blank · 39 comment · 24 complexity · b2d462d1ff1ab9489e9324b8e0521d46 MD5 · raw file

  1. <?php
  2. #############################################################################
  3. # IMDBPHP (c) Giorgos Giagas & Itzchak Rehberg #
  4. # written by Giorgos Giagas #
  5. # extended & maintained by Itzchak Rehberg <izzysoft AT qumran DOT org> #
  6. # http://www.izzysoft.de/ #
  7. # ------------------------------------------------------------------------- #
  8. # This program is free software; you can redistribute and/or modify it #
  9. # under the terms of the GNU General Public License (see doc/LICENSE) #
  10. #############################################################################
  11. /* $Id: mdb_request.class.php 429 2010-12-01 16:20:49Z izzy $ */
  12. if ( isset($PEAR) && $PEAR ) { // Use the HTTP_Request class from the PEAR project.
  13. require_once("HTTP/Request.php");
  14. class MDB_Request extends HTTP_Request{
  15. function __construct($url){
  16. $this->HTTP_Request($url);
  17. if ( PROXY != ""){
  18. $this->setProxy(PROXY, PROXY_PORT);
  19. }
  20. $this->_allowRedirects = false;
  21. if ( in_array('HTTP_USER_AGENT',array_keys($_SERVER)) ) $user_agent = $_SERVER['HTTP_USER_AGENT'];
  22. else $user_agent = 'Mozilla/5.0 (X11; U; Linux i686; de; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3';
  23. $this->addHeader("User-Agent", $user_agent);
  24. }
  25. function getLastResponseHeaders($url) {
  26. $head = $this->head($url);
  27. return array($head["response"],$head["Date"],$head["Server"],"",$head["Connection"],$head["Content-Type"]);
  28. }
  29. }
  30. } else { // Use the browseremu class
  31. require_once (dirname(__FILE__)."/browseremulator.class.php");
  32. if (defined('IMDBPHP_CONFIG')) require_once (IMDBPHP_CONFIG);
  33. else require_once (dirname(__FILE__)."/mdb_config.class.php");
  34. /** The request class
  35. * Here we emulate a browser accessing the IMDB site. You don't need to
  36. * call any of its method directly - they are rather used by the IMDB classes.
  37. * @package MDBApi
  38. * @class MDB_Request
  39. */
  40. class MDB_Request extends BrowserEmulator{
  41. var $maxsize = 100000;
  42. /** Constructor: Initialize the BrowserEmulator
  43. * No need to call this.
  44. * @constructor MDB_Request
  45. * @param string url URL to open
  46. * @param optional string force_agent user agent string to use. Defaults to the one set in mdb_config.
  47. * @param optional string trigger_referer whether to trigger the referer. 'TRUE' = yes, 'FALSE' = no, '' = take value from mdb_config (default)
  48. */
  49. function __construct($url,$force_agent='',$trigger_referer=''){
  50. if (isset($GLOBALS['PEAR']) && $GLOBALS['PEAR']) parent::__construct();
  51. else $this->BrowserEmulator();
  52. $this->urltoopen = $url;
  53. $iconf = new mdb_config;
  54. if (!empty($force_agent)) $iconf->force_agent = $force_agent;
  55. if ($trigger_referer !== '' && is_bool($trigger_referer)) {
  56. $iconf->trigger_referer = $trigger_referer;
  57. } else {
  58. switch (strtolower($trigger_referer)) {
  59. case 'true' : $iconf->trigger_referer = TRUE; break;
  60. case 'false': $iconf->trigger_referer = FALSE; break;
  61. }
  62. }
  63. if ($iconf->trigger_referer) {
  64. if ( substr(get_class($this),0,4)=="imdb" ) $this->addHeaderLine('Referer','http://' . $this->imdbsite . '/');
  65. elseif ( in_array('HTTP_REFERER',array_keys($_SERVER)) ) $this->addHeaderLine('Referer',$_SERVER['HTTP_REFERER']);
  66. }
  67. if ($iconf->force_agent) $this->addHeaderLine('User-Agent', $iconf->force_agent);
  68. }
  69. /** Send a request to the movie site
  70. * @method sendRequest
  71. * @return boolean success
  72. */
  73. function sendRequest(){
  74. $this->fpopened = $this->fopen($this->urltoopen);
  75. if ($this->fpopened!==false) return true;
  76. return false;
  77. }
  78. /** Get the Response body
  79. * @method getResponseBody
  80. * @return string page
  81. */
  82. function getResponseBody(){
  83. $page = "";
  84. if ($this->fpopened===FALSE) return $page;
  85. while (!feof ($this->fpopened)) {
  86. $page .= fread ($this->fpopened, 1024);
  87. }
  88. return $page;
  89. }
  90. /** Set the URL we need to parse
  91. * @method setURL
  92. */
  93. function setURL($url){
  94. $this->urltoopen = $url;
  95. }
  96. /** Obtain the response header
  97. * @method getresponseheader
  98. * @param optional string header
  99. * @return string header
  100. */
  101. function getresponseheader($header = false){
  102. $headers = $this->getLastResponseHeaders();
  103. foreach ($headers as $head){
  104. if ( is_integer(strpos ($head, $header) )){
  105. $hstart = strpos ($head, ": ");
  106. $head = trim(substr($head,$hstart+2,100));
  107. return $head;
  108. }
  109. }
  110. }
  111. }
  112. }
  113. ?>