/** Save the photo to disk * @method savephoto * @param string path where to store the file * @param optional boolean thumb get the thumbnail (100x140, default) or the * bigger variant (400x600 - FALSE) * @return boolean success * @see IMDB person page / (Main page) */ public function savephoto($path, $thumb = true) { $req = new MDB_Request(""); $photo_url = $this->photo($thumb); if (!$photo_url) { return FALSE; } $req->setURL($photo_url); $req->sendRequest(); if (strpos($req->getResponseHeader("Content-Type"), 'image/jpeg') === 0 || strpos($req->getResponseHeader("Content-Type"), 'image/gif') === 0 || strpos($req->getResponseHeader("Content-Type"), 'image/bmp') === 0) { $fp = $req->getResponseBody(); } else { $this->debug_scalar("<BR>*photoerror* " . $photo_url . ": Content Type is '" . $req->getResponseHeader("Content-Type") . "'<BR>"); return false; } $fp2 = fopen($path, "w"); if (!$fp || !$fp2) { $this->debug_scalar("image error...<BR>"); return false; } fputs($fp2, $fp); return TRUE; }
/** Convert IMDB redirect-URLs of external sites to real URLs * @method convertIMDBtoRealURL * @param string url redirect-url * @return string url real-url */ protected function convertIMDBtoRealURL($url) { if (preg_match('/^http:\\/\\//', $url)) { return $url; } $req = new MDB_Request(""); $req->setURL("http://" . $this->imdbsite . $url); if ($req->sendRequest() !== FALSE) { $head = $req->getLastResponseHeaders(); foreach ($head as $header) { if (preg_match('/:/', $header)) { list($type, $value) = explode(':', $header, 2); if ($type == 'Location') { return preg_replace('/\\s/', '', $value); } } } } return false; }
/** Save the photo to disk * @method savephoto * @param string path where to store the file * @param optional boolean thumb get the thumbnail (100x140, default) or the * bigger variant (400x600 - FALSE) * @return boolean success * @see IMDB person page / (Main page) */ public function savephoto($path, $thumb = TRUE, $rerun = FALSE) { if ($rerun) { $req = new MDB_Request('', '', !$this->trigger_referer); } else { $req = new MDB_Request('', '', $this->trigger_referer); } $photo_url = $this->photo($thumb); if (!$photo_url) { return FALSE; } $req->setURL($photo_url); $req->sendRequest(); if (strpos($req->getResponseHeader("Content-Type"), 'image/jpeg') === 0 || strpos($req->getResponseHeader("Content-Type"), 'image/gif') === 0 || strpos($req->getResponseHeader("Content-Type"), 'image/bmp') === 0) { $fp = $req->getResponseBody(); } else { if ($rerun) { $this->debug_scalar("<BR>*photoerror* at " . __FILE__ . " line " . __LINE__ . ": " . $photo_url . ": Content Type is '" . $req->getResponseHeader("Content-Type") . "'<BR>"); return FALSE; } else { $this->debug_scalar("<BR>Initiate second run for photo '{$path}'<BR>"); return $this->savephoto($path, $thumb, TRUE); } } $fp2 = fopen($path, "w"); if (!$fp || !$fp2) { $this->debug_scalar("image error...<BR>"); return false; } fputs($fp2, $fp); return TRUE; }
/** Obtain page from web server * @method protected getWebPage * @param string wt internal name of the page * @param string url URL to open */ protected function getWebPage($wt, $url) { $req = new MDB_Request(""); $req->setURL($url); if ($req->sendRequest() !== FALSE) { $head = $req->getLastResponseHeaders(); } else { $head[0] = "HTTP/1.1 000"; } $response = explode(" ", $head[0]); $this->lastServerResponse = $response[1]; switch (substr($head[0], 0, 12)) { case "HTTP/1.1 000": $this->page[$wt] = "cannot open page"; $this->debug_scalar("cannot open page (could not connect to host): {$url}"); return false; break; case "HTTP/1.1 404": $this->page[$wt] = "cannot open page"; $this->debug_scalar("cannot open page (error 404): {$url}"); $this->debug_object($response); return false; break; case "HTTP/1.1 301": // permanent redirect // permanent redirect case "HTTP/1.1 302": // found // found case "HTTP/1.1 303": // see other // see other case "HTTP/1.1 307": // temporary redirect // in all these cases, the correct URL is to be found in the 'Location:' header foreach ($head as $headline) { if (strpos(trim(strtolower($headline)), 'location') !== 0) { continue; } $aline = explode(': ', $headline); $target = trim($aline[1]); $this->getWebPage($wt, $target); return; } // echo "<pre>";print_r($head);echo "</pre>\n"; // $this->debug_object($response); // echo "<pre>";print_r($head);echo "</pre>\n"; // $this->debug_object($response); case "HTTP/1.1 200": break; default: $this->debug_scalar("HTTP response code not handled explicitly: '" . $head[0] . "'"); break; } $this->page[$wt] = $req->getResponseBody(); if (strpos(get_class($this), 'imdb') !== FALSE && $this->imdb_utf8recode && function_exists('mb_detect_encoding')) { $cur_encoding = mb_detect_encoding($this->page[$wt]); if (!($cur_encoding == "UTF-8" && mb_check_encoding($this->page[$wt], "UTF-8"))) { $this->page[$wt] = utf8_encode($this->page[$wt]); } } }
/** Save the photo to disk * @method savephoto * @param string path where to store the file * @param optional boolean thumb get the thumbnail (100x140, default) or the * bigger variant (400x600 - FALSE) * @return boolean success * @see IMDB page / (TitlePage) */ public function savephoto($path, $thumb = true, $rerun = 0) { switch ($rerun) { case 2: $req = new MDB_Request(''); break; case 1: $req = new MDB_Request('', '', !$this->trigger_referer); break; default: $req = new MDB_Request('', '', $this->trigger_referer); break; } $photo_url = $this->photo($thumb); if (!$photo_url) { return FALSE; } $req->setURL($photo_url); $req->sendRequest(); if (strpos($req->getResponseHeader("Content-Type"), 'image/jpeg') === 0 || strpos($req->getResponseHeader("Content-Type"), 'image/gif') === 0 || strpos($req->getResponseHeader("Content-Type"), 'image/bmp') === 0) { $fp = $req->getResponseBody(); } else { switch ($rerun) { case 2: $ctype = $req->getResponseHeader("Content-Type"); $this->debug_scalar("<BR>*photoerror* at " . __FILE__ . " line " . __LINE__ . ": " . $photo_url . ": Content Type is '{$ctype}'<BR>"); if (substr($ctype, 0, 4) == 'text') { $this->debug_scalar("Details: <PRE>" . $req->getResponseBody() . "</PRE>\n"); } return FALSE; break; case 1: $this->debug_scalar("<BR>Initiate third run for savephoto({$path}) on IMDBID " . $this->imdbID . "<BR>"); unset($req); return $this->savephoto($path, $thumb, 2); break; default: $this->debug_scalar("<BR>Initiate second run for savephoto({$path}) on IMDBID " . $this->imdbID . "<BR>"); unset($req); return $this->savephoto($path, $thumb, 1); break; } } $fp2 = fopen($path, "w"); if (!$fp || !$fp2) { $this->debug_scalar("image error at " . __FILE__ . " line " . __LINE__ . "...<BR>"); return false; } fputs($fp2, $fp); return TRUE; }
/** Obtain page from web server * @method private getWebPage * @param string wt internal name of the page * @param string url URL to open */ function getWebPage($wt, $url) { $req = new MDB_Request(""); $req->setURL($url); if ($req->sendRequest() !== FALSE) { $head = $req->getLastResponseHeaders(); } else { $head[0] = "HTTP/1.1 000"; } $response = explode(" ", $head[0]); $this->lastServerResponse = $response[1]; switch (substr($head[0], 0, 12)) { case "HTTP/1.1 000": $this->page[$wt] = "cannot open page"; $this->debug_scalar("cannot open page (could not connect to host): {$url}"); return false; break; case "HTTP/1.1 404": $this->page[$wt] = "cannot open page"; $this->debug_scalar("cannot open page (error 404): {$url}"); return false; break; case "HTTP/1.1 301": case "HTTP/1.1 302": case "HTTP/1.1 200": break; default: $this->debug_scalar("HTTP response code not handled explicitly: '" . $head[0] . "'"); break; } $this->page[$wt] = $req->getResponseBody(); if (strpos(get_class($this), 'imdb') !== FALSE && $this->imdb_utf8recode && function_exists('mb_detect_encoding')) { $cur_encoding = mb_detect_encoding($this->page[$wt]); if (!($cur_encoding == "UTF-8" && mb_check_encoding($this->page[$wt], "UTF-8"))) { $this->page[$wt] = utf8_encode($this->page[$wt]); } } }