".$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; } } ?>