From fb9df0c269f36703909b8b259abbdbed29881ecd Mon Sep 17 00:00:00 2001 From: tcit Date: Thu, 24 Jul 2014 21:56:04 +0200 Subject: use directly MOBIClass --- .../libraries/MOBIClass/MultipleFileHandler.php | 136 +++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 inc/3rdparty/libraries/MOBIClass/MultipleFileHandler.php (limited to 'inc/3rdparty/libraries/MOBIClass/MultipleFileHandler.php') diff --git a/inc/3rdparty/libraries/MOBIClass/MultipleFileHandler.php b/inc/3rdparty/libraries/MOBIClass/MultipleFileHandler.php new file mode 100644 index 00000000..e9792816 --- /dev/null +++ b/inc/3rdparty/libraries/MOBIClass/MultipleFileHandler.php @@ -0,0 +1,136 @@ +".$title."".$contents.""; + } + $pos = 0; + + if(sizeof($this->toc) > 0){ + $lastToc = $this->toc[sizeof($this->toc)-1]; + $lastFile = $this->files[sizeof($this->files)-1]; + $pos = $lastToc["pos"] + strlen($lastFile) + 1; + } + + $this->files[] = $contents; + $this->toc[] = array("title"=>$title, "pos"=>$pos); + } + + /** + * Add an image to the file + * @param string $imageContents Data string containing the binary data of the image + * @return int The reference of the image + */ + public function addImage($imageContents){ + $this->images[] = $imageContents; + return sizeof($this->images)-1; + } + + /** + * Add an image to the file + * @param string $url Url to the image + * @return int The reference of the image, false if the image couldn't be downloaded + */ + public function addImageFromUrl($url){ + $image = ImageHandler::DownloadImage($url); + + if($image === false) return false; + return $this->addImage($image); + } + + /** + * Set the metadata + * @param string $key Key + * @param string $value Value + */ + public function setMetadata($key, $value){ + $this->metadata[$key] = $value; + } + + /** + * Get the text data to be integrated in the MOBI file + * @return string + */ + public function getTextData(){ + $data = implode("\n", $this->files); + $begin = ""; + $beforeTOC = $begin.$data; + + $tocPos = strlen($beforeTOC); + + $toc = $this->generateTOC(strlen($begin)); + + $customBegin = ""; + $data = $customBegin.$data.$toc.""; + return $data; + } + + public function forceLength($n, $l){ + $str = $n.""; + $cur = strlen($str); + while($cur < $l){ + $str = "0".$str; + $cur++; + } + return $str; + } + + public function generateTOC($base = 0){ + $toc = "

Contents

"; + $toc .= "
"; + for($i = 0, $len = sizeof($this->toc); $i < $len; $i++){ + $entry = $this->toc[$i]; + $position = $entry["pos"]+$base; + $toc .= ""; + } + $toc .= "
".($i+1).".".$entry["title"]."
"; + + return $toc; + } + /** + * Get the images (an array containing the jpeg data). Array entry 0 will + * correspond to image record 0. + * @return array + */ + public function getImages(){ + return $this->images; + } + + /** + * Get the metadata in the form of a hashtable (for example, title or author). + * @return array + */ + public function getMetaData(){ + return $this->metadata; + } + +} +?> -- cgit v1.2.3