/** 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 IMDB_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 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(); }
/** 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 */ function savephoto($path, $thumb = true) { $req = new IMDB_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; }
/** Open an IMDB page * @method openpage * @param string wt */ function openpage($wt) { if (strlen($this->imdbID) != 7) { echo "not valid imdbID: " . $this->imdbID . "<BR>" . strlen($this->imdbID); $this->page[$wt] = "cannot open page"; return; } // try to block all new request /*$this->page[$wt] = "cannot open page"; return;*/ switch ($wt) { case "Title": $urlname = "/"; break; case "Credits": $urlname = "/fullcredits"; break; case "Plot": $urlname = "/plotsummary"; break; case "Taglines": $urlname = "/taglines"; break; } if ($this->usecache) { @($fp = fopen("{$this->cachedir}/{$this->imdbID}.{$wt}", "r")); if ($fp) { $temp = ""; while (!feof($fp)) { $temp .= fread($fp, 1024); } if ($temp) { $this->page[$wt] = $temp; return; } } } // end cache $req = new IMDB_Request(""); $req->setURL("http://" . $this->imdbsite . "/title/tt" . $this->imdbID . $urlname); $response = $req->send(); $responseBody = $response->getBody(); if ($responseBody) { $this->page[$wt] = utf8_encode($responseBody); } if ($this->page[$wt]) { //storecache if ($this->storecache) { $fp = fopen("{$this->cachedir}/{$this->imdbID}.{$wt}", "w"); fputs($fp, $this->page[$wt]); fclose($fp); } return; } $this->page[$wt] = "cannot open page"; //echo "page not found"; }
/** Load an IMDB page into the corresponding property (variable) * @method private openpage * @param string wt internal name of the page * @param optional string type whether its a "movie" (default) or a "person" */ function openpage($wt, $type = "movie") { if (strlen($this->imdbID) != 7) { $this->debug_scalar("not valid imdbID: " . $this->imdbID . "<BR>" . strlen($this->imdbID)); $this->page[$wt] = "cannot open page"; return; } $urlname = $this->set_pagename($wt); if ($urlname === false) { return; } if ($this->usecache) { $fname = "{$this->cachedir}/{$this->imdbID}.{$wt}"; if ($this->usezip) { if ($this->page[$wt] = @join("", @gzfile($fname))) { if ($this->converttozip) { @($fp = fopen($fname, "r")); $zipchk = fread($fp, 2); fclose($fp); if (!($zipchk[0] == chr(31) && $zipchk[1] == chr(139))) { //checking for zip header /* converting on access */ $fp = @gzopen($fname, "w"); @gzputs($fp, $this->page[$wt]); @gzclose($fp); } } return; } } else { // no zip @($fp = fopen($fname, "r")); if ($fp) { $temp = ""; while (!feof($fp)) { $temp .= fread($fp, 1024); $this->page[$wt] = $temp; } return; } } } // end cache $req = new IMDB_Request(""); switch ($type) { case "person": $url = "http://" . $this->imdbsite . "/name/nm" . $this->imdbID . $urlname; break; default: $url = "http://" . $this->imdbsite . "/title/tt" . $this->imdbID . $urlname; } $req->setURL($url); $req->sendRequest(); $this->page[$wt] = $req->getResponseBody(); if ($this->page[$wt]) { //storecache if ($this->storecache) { if (!is_dir($this->cachedir)) { $this->debug_scalar("<BR>***ERROR*** Configured cache directory does not exist!<BR>"); return; } if (!is_writable($this->cachedir)) { $this->debug_scalar("<BR>***ERROR*** Configured cache directory lacks write permission!<BR>"); return; } $fname = "{$this->cachedir}/{$this->imdbID}.{$wt}"; if ($this->usezip) { $fp = gzopen($fname, "w"); gzputs($fp, $this->page[$wt]); gzclose($fp); } else { // no zip $fp = fopen($fname, "w"); fputs($fp, $this->page[$wt]); fclose($fp); } } return; } $this->page[$wt] = "cannot open page"; $this->debug_scalar("cannot open page: {$url}"); }