]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/3rdparty/libraries/send2kindle/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__
).'/OnlineArticle.php');
22 require_once(dirname(__FILE__
).'/PalmRecord.php');
23 require_once(dirname(__FILE__
).'/PEOFRecord.php');
24 require_once(dirname(__FILE__
).'/PFCISRecord.php');
25 require_once(dirname(__FILE__
).'/PFLISRecord.php');
26 require_once(dirname(__FILE__
).'/Prc.php');
27 require_once(dirname(__FILE__
).'/PreprocessedArticle.php');
28 require_once(dirname(__FILE__
).'/RecognizeURL.php');
29 require_once(dirname(__FILE__
).'/Record.php');
30 require_once(dirname(__FILE__
).'/RecordFactory.php');
31 require_once(dirname(__FILE__
).'/Settings.php');
34 * Description of MOBI.
37 * include("MOBIClass/MOBI.php");
41 * //Then use one of the following ways to prepare information (it should be in the form of valid html)
42 * $mobi->setInternetSource($url); //Load URL, the result will be cleaned using a Readability port
43 * $mobi->setFileSource($file); //Load a local file without any extra changes
44 * $mobi->setData($data); //Load data
46 * //If you want, you can set some optional settings
48 * "title"=>"Insert title here",
51 * $mobi->setOptions($options);
53 * //Then there are two ways to output it:
54 * $mobi->save($file); //Save the file locally
55 * $mobi->download($name); //Let the client download the file, make sure the page
56 * //that calls it doesn't output anything, otherwise it might
57 * //conflict with the download. $name contains the file name,
58 * //usually something like "title.mobi" (where the title should
59 * //be cleaned so as not to contain illegal characters).
62 * @author Sander Kromwijk
65 private $source = false;
66 private $images = array();
67 private $optional = array();
68 private $imgCounter = 0;
69 private $debug = false;
72 public function __construct(){
76 public function getTitle(){
77 if(isset($this->optional
["title"])){
78 return $this->optional
["title"];
84 * Set a content provider as source
85 * @param ContentProvider $content Content Provider to use
87 public function setContentProvider($content){
88 $this->setOptions($content->getMetaData());
89 $this->images
= $content->getImages();
90 $this->setData($content->getTextData());
94 * Set a local file as source
95 * @param string $file Path to the file
97 public function setFileSource($file){
98 $this->setData(file_get_contents($file));
102 * Set the data to use
103 * @param string $data Data to put in the file
105 public function setData($data){
106 //$data = utf8_encode($data);
107 $data = CharacterEntities
::convert($data);
108 //$data = utf8_decode($data);
109 //$this->source = iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $data);
110 $this->source
= $data;
115 * Set the images to use
116 * @param array $data Data to put in the file
118 public function setImages($data){
119 $this->images
= $data;
124 * Set options, usually for things like titles, authors, etc...
125 * @param array $options Options to set
127 public function setOptions($options){
128 $this->optional
= $options;
133 * Prepare the prc file
134 * @return Prc The file that can be used to be saved/downloaded
136 private function preparePRC(){
137 if($this->source
=== false){
138 throw new Exception("No data set");
140 if($this->prc
!== false) return $this->prc
;
142 $data = $this->source
;
143 $len = strlen($data);
145 $settings = new Settings($this->optional
);
146 $rec = new RecordFactory($settings);
147 $dataRecords = $rec->createRecords($data);
148 $nRecords = sizeof($dataRecords);
149 $mobiHeader = new PalmRecord($settings, $dataRecords, $nRecords, $len, sizeof($this->images
));
150 array_unshift($dataRecords, $mobiHeader);
151 $dataRecords = array_merge($dataRecords, $this->images
);
152 $mobiFooter1 = new PFLISRecord($len);
153 $mobiFooter2 = new PFCISRecord($len);
154 $mobiFooter3 = new PEOFRecord($len);
155 $dataRecords[] = $mobiFooter1;
156 $dataRecords[] = $mobiFooter2;
157 $dataRecords[] = $mobiFooter3;
158 /*$dataRecords = array_merge($dataRecords, $mobiFooter);
159 *$dataRecords[] = $rec->createFLISRecord();*
160 $dataRecords[] = $rec->createFCISRecord($len);
161 $dataRecords[] = $rec->createEOFRecord();*/
162 $this->prc
= new Prc($settings, $dataRecords);
167 * Save the file locally
168 * @param string $filename Path to save the file
170 public function save($filename){
171 $prc = $this->preparePRC();
172 $prc->save($filename);
176 * Let the client download the file. Warning! No data should be
177 * outputted before or after.
178 * @param string $name Name used for download, usually "title.mobi"
180 public function download($name){
181 $prc = $this->preparePRC();
182 $data = $prc->serialize();
183 $length = strlen($data);
185 if($this->debug
) return; //In debug mode, don't start the download
187 header("Content-Type: application/x-mobipocket-ebook");
188 header("Content-Disposition: attachment; filename=\"".$name."\"");
189 header("Content-Transfer-Encoding: binary");
190 header("Accept-Ranges: bytes");
191 header("Cache-control: private");
192 header('Pragma: private');
193 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
194 header("Content-Length: ".$length);
198 $hh = fopen("D:\hakuna.mobi", "w");