public function __construct($url) { if (!preg_match('!^https?://!i', $url)) { $url = 'http://' . $url; } $data = Http::Request($url); //$enc = mb_detect_encoding($str, "UTF-8,ISO-8859-1,ASCII"); $html = mb_convert_encoding($data, "UTF-8", "UTF-8,ISO-8859-1,ASCII"); //$html = utf8_encode($html); $r = new Readability($html, $url); $r->init(); if (!isset($this->metadata["title"])) { $this->metadata["title"] = CharacterEntities::convert(strip_tags($r->getTitle()->innerHTML)); } if (!isset($this->metadata["author"])) { $parts = parse_url($url); $this->metadata["author"] = $parts["host"]; } $article = $r->getContent()->innerHTML; if (substr($article, 0, 5) == "<body") { $article = "<html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/></head>" . $article . "</html>"; } else { $article = "<html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/></head><body>" . $article . "</body></html>"; } $doc = new DOMDocument(); @$doc->loadHTML($article) or die($article); $doc->normalizeDocument(); $this->images = $this->handleImages($doc, $url); $this->text = $doc->saveHTML(); }
/** * Download an image. * * @param string $url Url to the image * * @return false|string False if failed, else the data of the image (converted to grayscale jpeg) */ public static function DownloadImage($url) { $data = Http::Request($url); $imgFile = @imagecreatefromstring($data); if ($imgFile !== false) { $result = self::CreateImage($imgFile); imagedestroy($imgFile); return $result; } return false; }
private function addChapter($n) { $doc = new DOMDocument(); $file = Http::Request(self::$prefix . $this->id . "/" . $n . "/"); @$doc->loadHTML($file) or die($file); if (!$this->downloadedMetadata) { $this->loadMetadata($doc); $this->downloadedMetadata = true; } if ($this->chapterCount < 0) { $this->chapterCount = $this->getNumberChapters($doc); if ($this->chapterCount > 4) { die("Too many files to download, don't use php for this!"); } } $textEl = $doc->getElementById("storytext"); if ($textEl == null) { die("Error: " . $doc->saveHTML()); } $horizontalRulebars = $doc->getElementsByTagName('hr'); /** * @var DOMNode */ $hr; foreach ($horizontalRulebars as $hr) { $hr->setAttribute("size", null); $hr->setAttribute("noshade", null); } $text = $this->innerHtml($textEl); $title = ""; $selects = $doc->getElementsByTagName('select'); foreach ($selects as $select) { if ($select->hasAttribute("name") && $select->getAttribute("name") == "chapter") { $options = $select->getElementsByTagName("option"); $test = $n . ". "; foreach ($options as $option) { $val = $option->nodeValue; if (substr($val, 0, strlen($test)) == $test) { $title = substr($val, strlen($test)); break; } } break; } } $this->addPage($text, $title); }
/** * @param DOMElement $dom * * @return array */ private function downloadImages($links) { $images = array(); foreach ($links as $link) { $imgFile = @imagecreatefromstring(Http::Request($link)); if ($imgFile === false) { $imgFile = @imagecreate(1, 1); $black = @imagecolorallocate($imgFile, 255, 255, 255); } if ($imgFile !== false) { @imagefilter($imgFile, IMG_FILTER_GRAYSCALE); ob_start(); @imagejpeg($imgFile); $image = ob_get_contents(); ob_end_clean(); $images[$this->imgCounter] = new FileRecord(new Record($image)); imagedestroy($imgFile); ++$this->imgCounter; } } return $images; }