]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/3rdparty/libraries/MOBIClass/MOBI.php
2 require_once(dirname(__FILE__
)."/../readability/Readability.php");
3 require_once(dirname(__FILE__
).'/CharacterEntities.php');
4 require_once(dirname(__FILE__
).'/constants.php');
5 require_once(dirname(__FILE__
).'/ContentProvider.php');
6 require_once(dirname(__FILE__
).'/MultipleFileHandler.php');
7 require_once(dirname(__FILE__
)."/downloaders/FanFictionNet.php");
8 require_once(dirname(__FILE__
).'/EXTHHelper.php');
9 require_once(dirname(__FILE__
).'/FileObject.php');
10 require_once(dirname(__FILE__
).'/FileByte.php');
11 require_once(dirname(__FILE__
).'/FileDate.php');
12 require_once(dirname(__FILE__
).'/FileElement.php');
13 require_once(dirname(__FILE__
).'/FileInt.php');
14 require_once(dirname(__FILE__
).'/FileRecord.php');
15 require_once(dirname(__FILE__
).'/FileShort.php');
16 require_once(dirname(__FILE__
).'/FileString.php');
17 require_once(dirname(__FILE__
).'/FileTri.php');
18 require_once(dirname(__FILE__
).'/Http.php');
19 require_once(dirname(__FILE__
).'/http_build_url.php');
20 require_once(dirname(__FILE__
).'/ImageHandler.php');
21 require_once(dirname(__FILE__
).'/MOBIFile.php');
22 require_once(dirname(__FILE__
).'/OnlineArticle.php');
23 require_once(dirname(__FILE__
).'/PalmRecord.php');
24 require_once(dirname(__FILE__
).'/Prc.php');
25 require_once(dirname(__FILE__
).'/PreprocessedArticle.php');
26 require_once(dirname(__FILE__
).'/RecognizeURL.php');
27 require_once(dirname(__FILE__
).'/Record.php');
28 require_once(dirname(__FILE__
).'/RecordFactory.php');
29 require_once(dirname(__FILE__
).'/Settings.php');
32 * Description of MOBI.
35 * include("MOBIClass/MOBI.php");
39 * //Then use one of the following ways to prepare information (it should be in the form of valid html)
40 * $mobi->setInternetSource($url); //Load URL, the result will be cleaned using a Readability port
41 * $mobi->setFileSource($file); //Load a local file without any extra changes
42 * $mobi->setData($data); //Load data
44 * //If you want, you can set some optional settings (see Settings.php for all recognized settings)
46 * "title"=>"Insert title here",
49 * $mobi->setOptions($options);
51 * //Then there are two ways to output it:
52 * $mobi->save($file); //Save the file locally
53 * $mobi->download($name); //Let the client download the file, make sure the page
54 * //that calls it doesn't output anything, otherwise it might
55 * //conflict with the download. $name contains the file name,
56 * //usually something like "title.mobi" (where the title should
57 * //be cleaned so as not to contain illegal characters).
60 * @author Sander Kromwijk
63 private $source = false;
64 private $images = array();
65 private $optional = array();
66 private $imgCounter = 0;
67 private $debug = false;
70 public function __construct(){
74 public function getTitle(){
75 if(isset($this->optional
["title"])){
76 return $this->optional
["title"];
82 * Set a content provider as source
83 * @param ContentProvider $content Content Provider to use
85 public function setContentProvider($content){
86 $this->setOptions($content->getMetaData());
87 $this->setImages($content->getImages());
88 $this->setData($content->getTextData());
92 * Set a local file as source
93 * @param string $file Path to the file
95 public function setFileSource($file){
96 $this->setData(file_get_contents($file));
100 * Set the data to use
101 * @param string $data Data to put in the file
103 public function setData($data){
104 //$data = utf8_encode($data);
105 $data = CharacterEntities
::convert($data);
106 //$data = utf8_decode($data);
107 //$this->source = iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $data);
108 $this->source
= $data;
113 * Set the images to use
114 * @param array $data Data to put in the file
116 public function setImages($data){
117 $this->images
= $data;
122 * Set options, usually for things like titles, authors, etc...
123 * @param array $options Options to set
125 public function setOptions($options){
126 $this->optional
= $options;
131 * Prepare the prc file
132 * @return Prc The file that can be used to be saved/downloaded
134 private function preparePRC(){
135 if($this->source
=== false){
136 throw new Exception("No data set");
138 if($this->prc
!== false) return $this->prc
;
140 $data = $this->source
;
141 $len = strlen($data);
143 $settings = new Settings($this->optional
);
144 $rec = new RecordFactory($settings);
145 $dataRecords = $rec->createRecords($data);
146 $nRecords = sizeof($dataRecords);
147 $mobiHeader = new PalmRecord($settings, $dataRecords, $nRecords, $len, sizeof($this->images
));
148 array_unshift($dataRecords, $mobiHeader);
149 $dataRecords = array_merge($dataRecords, $this->images
);
150 $dataRecords[] = $rec->createFLISRecord();
151 $dataRecords[] = $rec->createFCISRecord($len);
152 $dataRecords[] = $rec->createEOFRecord();
153 $this->prc
= new Prc($settings, $dataRecords);
158 * Save the file locally
159 * @param string $filename Path to save the file
161 public function save($filename){
162 $prc = $this->preparePRC();
163 $prc->save($filename);
167 * Let the client download the file. Warning! No data should be
168 * outputted before or after.
169 * @param string $name Name used for download, usually "title.mobi"
171 public function download($name){
172 $prc = $this->preparePRC();
173 $data = $prc->serialize();
174 $length = strlen($data);
176 if($this->debug
) return; //In debug mode, don't start the download
178 header("Content-Type: application/x-mobipocket-ebook");
179 header("Content-Disposition: attachment; filename=\"".$name."\"");
180 header("Content-Transfer-Encoding: binary");
181 header("Accept-Ranges: bytes");
182 header("Cache-control: private");
183 header('Pragma: private');
184 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
185 header("Content-Length: ".$length);