From fb9df0c269f36703909b8b259abbdbed29881ecd Mon Sep 17 00:00:00 2001 From: tcit Date: Thu, 24 Jul 2014 21:56:04 +0200 Subject: use directly MOBIClass --- inc/3rdparty/libraries/MOBIClass/MOBI.php | 192 ++++++++++++++++++++++++++++++ 1 file changed, 192 insertions(+) create mode 100644 inc/3rdparty/libraries/MOBIClass/MOBI.php (limited to 'inc/3rdparty/libraries/MOBIClass/MOBI.php') diff --git a/inc/3rdparty/libraries/MOBIClass/MOBI.php b/inc/3rdparty/libraries/MOBIClass/MOBI.php new file mode 100644 index 00000000..17e718c1 --- /dev/null +++ b/inc/3rdparty/libraries/MOBIClass/MOBI.php @@ -0,0 +1,192 @@ +setInternetSource($url); //Load URL, the result will be cleaned using a Readability port + * $mobi->setFileSource($file); //Load a local file without any extra changes + * $mobi->setData($data); //Load data + * + * //If you want, you can set some optional settings (see Settings.php for all recognized settings) + * $options = array( + * "title"=>"Insert title here", + * "author"=>"Author" + * ); + * $mobi->setOptions($options); + * + * //Then there are two ways to output it: + * $mobi->save($file); //Save the file locally + * $mobi->download($name); //Let the client download the file, make sure the page + * //that calls it doesn't output anything, otherwise it might + * //conflict with the download. $name contains the file name, + * //usually something like "title.mobi" (where the title should + * //be cleaned so as not to contain illegal characters). + * + * + * @author Sander Kromwijk + */ +class MOBI { + private $source = false; + private $images = array(); + private $optional = array(); + private $imgCounter = 0; + private $debug = false; + private $prc = false; + + public function __construct(){ + + } + + public function getTitle(){ + if(isset($this->optional["title"])){ + return $this->optional["title"]; + } + return false; + } + + /** + * Set a content provider as source + * @param ContentProvider $content Content Provider to use + */ + public function setContentProvider($content){ + $this->setOptions($content->getMetaData()); + $this->setImages($content->getImages()); + $this->setData($content->getTextData()); + } + + /** + * Set a local file as source + * @param string $file Path to the file + */ + public function setFileSource($file){ + $this->setData(file_get_contents($file)); + } + + /** + * Set the data to use + * @param string $data Data to put in the file + */ + public function setData($data){ + //$data = utf8_encode($data); + $data = CharacterEntities::convert($data); + //$data = utf8_decode($data); + //$this->source = iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $data); + $this->source = $data; + $this->prc = false; + } + + /** + * Set the images to use + * @param array $data Data to put in the file + */ + public function setImages($data){ + $this->images = $data; + $this->prc = false; + } + + /** + * Set options, usually for things like titles, authors, etc... + * @param array $options Options to set + */ + public function setOptions($options){ + $this->optional = $options; + $this->prc = false; + } + + /** + * Prepare the prc file + * @return Prc The file that can be used to be saved/downloaded + */ + private function preparePRC(){ + if($this->source === false){ + throw new Exception("No data set"); + } + if($this->prc !== false) return $this->prc; + + $data = $this->source; + $len = strlen($data); + + $settings = new Settings($this->optional); + $rec = new RecordFactory($settings); + $dataRecords = $rec->createRecords($data); + $nRecords = sizeof($dataRecords); + $mobiHeader = new PalmRecord($settings, $dataRecords, $nRecords, $len, sizeof($this->images)); + array_unshift($dataRecords, $mobiHeader); + $dataRecords = array_merge($dataRecords, $this->images); + $dataRecords[] = $rec->createFLISRecord(); + $dataRecords[] = $rec->createFCISRecord($len); + $dataRecords[] = $rec->createEOFRecord(); + $this->prc = new Prc($settings, $dataRecords); + return $this->prc; + } + + /** + * Save the file locally + * @param string $filename Path to save the file + */ + public function save($filename){ + $prc = $this->preparePRC(); + $prc->save($filename); + } + + /** + * Let the client download the file. Warning! No data should be + * outputted before or after. + * @param string $name Name used for download, usually "title.mobi" + */ + public function download($name){ + $prc = $this->preparePRC(); + $data = $prc->serialize(); + $length = strlen($data); + + if($this->debug) return; //In debug mode, don't start the download + + header("Content-Type: application/x-mobipocket-ebook"); + header("Content-Disposition: attachment; filename=\"".$name."\""); + header("Content-Transfer-Encoding: binary"); + header("Accept-Ranges: bytes"); + header("Cache-control: private"); + header('Pragma: private'); + header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); + header("Content-Length: ".$length); + + echo $data; + //Finished! + } + +} +?> \ No newline at end of file -- cgit v1.2.3