]> git.immae.eu Git - github/wallabag/wallabag.git/blame - inc/3rdparty/libraries/send2kindle/MOBIClass/MOBI.php
add mobi and pdf to routing
[github/wallabag/wallabag.git] / inc / 3rdparty / libraries / send2kindle / MOBIClass / MOBI.php
CommitLineData
4188f38a 1<?php
2require_once(dirname(__FILE__)."/readability/Readability.php");
3require_once(dirname(__FILE__).'/CharacterEntities.php');
4require_once(dirname(__FILE__).'/constants.php');
5require_once(dirname(__FILE__).'/ContentProvider.php');
6require_once(dirname(__FILE__).'/MultipleFileHandler.php');
7require_once(dirname(__FILE__)."/downloaders/FanFictionNet.php");
8require_once(dirname(__FILE__).'/EXTHHelper.php');
9require_once(dirname(__FILE__).'/FileObject.php');
10require_once(dirname(__FILE__).'/FileByte.php');
11require_once(dirname(__FILE__).'/FileDate.php');
12require_once(dirname(__FILE__).'/FileElement.php');
13require_once(dirname(__FILE__).'/FileInt.php');
14require_once(dirname(__FILE__).'/FileRecord.php');
15require_once(dirname(__FILE__).'/FileShort.php');
16require_once(dirname(__FILE__).'/FileString.php');
17require_once(dirname(__FILE__).'/FileTri.php');
18require_once(dirname(__FILE__).'/Http.php');
19require_once(dirname(__FILE__).'/http_build_url.php');
20require_once(dirname(__FILE__).'/ImageHandler.php');
21require_once(dirname(__FILE__).'/OnlineArticle.php');
22require_once(dirname(__FILE__).'/PalmRecord.php');
23require_once(dirname(__FILE__).'/PEOFRecord.php');
24require_once(dirname(__FILE__).'/PFCISRecord.php');
25require_once(dirname(__FILE__).'/PFLISRecord.php');
26require_once(dirname(__FILE__).'/Prc.php');
27require_once(dirname(__FILE__).'/PreprocessedArticle.php');
28require_once(dirname(__FILE__).'/RecognizeURL.php');
29require_once(dirname(__FILE__).'/Record.php');
30require_once(dirname(__FILE__).'/RecordFactory.php');
31require_once(dirname(__FILE__).'/Settings.php');
32
33/**
34 * Description of MOBI.
35 *
36 * Usage:
37 * include("MOBIClass/MOBI.php");
38 *
39 * $mobi = new MOBI();
40 *
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
45 *
46 * //If you want, you can set some optional settings
47 * $options = array(
48 * "title"=>"Insert title here",
49 * "author"=>"Author"
50 * );
51 * $mobi->setOptions($options);
52 *
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).
60 *
61 *
62 * @author Sander Kromwijk
63 */
64class MOBI {
65 private $source = false;
66 private $images = array();
67 private $optional = array();
68 private $imgCounter = 0;
69 private $debug = false;
70 private $prc = false;
71
72 public function __construct(){
73
74 }
75
76 public function getTitle(){
77 if(isset($this->optional["title"])){
78 return $this->optional["title"];
79 }
80 return false;
81 }
82
83 /**
84 * Set a content provider as source
85 * @param ContentProvider $content Content Provider to use
86 */
87 public function setContentProvider($content){
88 $this->setOptions($content->getMetaData());
89 $this->images = $content->getImages();
90 $this->setData($content->getTextData());
91 }
92
93 /**
94 * Set a local file as source
95 * @param string $file Path to the file
96 */
97 public function setFileSource($file){
98 $this->setData(file_get_contents($file));
99 }
100
101 /**
102 * Set the data to use
103 * @param string $data Data to put in the file
104 */
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;
111 $this->prc = false;
112 }
113
114 /**
115 * Set the images to use
116 * @param array $data Data to put in the file
117 */
118 public function setImages($data){
119 $this->images = $data;
120 $this->prc = false;
121 }
122
123 /**
124 * Set options, usually for things like titles, authors, etc...
125 * @param array $options Options to set
126 */
127 public function setOptions($options){
128 $this->optional = $options;
129 $this->prc = false;
130 }
131
132 /**
133 * Prepare the prc file
134 * @return Prc The file that can be used to be saved/downloaded
135 */
136 private function preparePRC(){
137 if($this->source === false){
138 throw new Exception("No data set");
139 }
140 if($this->prc !== false) return $this->prc;
141
142 $data = $this->source;
143 $len = strlen($data);
144
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);
163 return $this->prc;
164 }
165
166 /**
167 * Save the file locally
168 * @param string $filename Path to save the file
169 */
170 public function save($filename){
171 $prc = $this->preparePRC();
172 $prc->save($filename);
173 }
174
175 /**
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"
179 */
180 public function download($name){
181 $prc = $this->preparePRC();
182 $data = $prc->serialize();
183 $length = strlen($data);
184
185 if($this->debug) return; //In debug mode, don't start the download
186/*
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);
195 echo $data;*/
196
197
198 $hh = fopen("D:\hakuna.mobi", "w");
199 fwrite($hh, $data);
200 fclose($hh);
201 //Finished!
202 }
203
204}
205?>