]>
Commit | Line | Data |
---|---|---|
4188f38a | 1 | <?php |
8ae45e7f | 2 | require_once(dirname(__FILE__)."/../readability/Readability.php"); |
4188f38a | 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'); | |
fb9df0c2 | 21 | require_once(dirname(__FILE__).'/MOBIFile.php'); |
4188f38a | 22 | require_once(dirname(__FILE__).'/OnlineArticle.php'); |
23 | require_once(dirname(__FILE__).'/PalmRecord.php'); | |
4188f38a | 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'); | |
30 | ||
31 | /** | |
32 | * Description of MOBI. | |
33 | * | |
34 | * Usage: | |
35 | * include("MOBIClass/MOBI.php"); | |
36 | * | |
37 | * $mobi = new MOBI(); | |
38 | * | |
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 | |
43 | * | |
fb9df0c2 | 44 | * //If you want, you can set some optional settings (see Settings.php for all recognized settings) |
4188f38a | 45 | * $options = array( |
46 | * "title"=>"Insert title here", | |
47 | * "author"=>"Author" | |
48 | * ); | |
49 | * $mobi->setOptions($options); | |
50 | * | |
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). | |
58 | * | |
59 | * | |
60 | * @author Sander Kromwijk | |
61 | */ | |
62 | class MOBI { | |
63 | private $source = false; | |
64 | private $images = array(); | |
65 | private $optional = array(); | |
66 | private $imgCounter = 0; | |
67 | private $debug = false; | |
68 | private $prc = false; | |
69 | ||
70 | public function __construct(){ | |
71 | ||
72 | } | |
73 | ||
74 | public function getTitle(){ | |
75 | if(isset($this->optional["title"])){ | |
76 | return $this->optional["title"]; | |
77 | } | |
78 | return false; | |
79 | } | |
80 | ||
81 | /** | |
82 | * Set a content provider as source | |
83 | * @param ContentProvider $content Content Provider to use | |
84 | */ | |
85 | public function setContentProvider($content){ | |
86 | $this->setOptions($content->getMetaData()); | |
fb9df0c2 | 87 | $this->setImages($content->getImages()); |
4188f38a | 88 | $this->setData($content->getTextData()); |
89 | } | |
90 | ||
91 | /** | |
92 | * Set a local file as source | |
93 | * @param string $file Path to the file | |
94 | */ | |
95 | public function setFileSource($file){ | |
96 | $this->setData(file_get_contents($file)); | |
97 | } | |
98 | ||
99 | /** | |
100 | * Set the data to use | |
101 | * @param string $data Data to put in the file | |
102 | */ | |
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; | |
109 | $this->prc = false; | |
110 | } | |
111 | ||
112 | /** | |
113 | * Set the images to use | |
114 | * @param array $data Data to put in the file | |
115 | */ | |
116 | public function setImages($data){ | |
117 | $this->images = $data; | |
118 | $this->prc = false; | |
119 | } | |
120 | ||
121 | /** | |
122 | * Set options, usually for things like titles, authors, etc... | |
123 | * @param array $options Options to set | |
124 | */ | |
125 | public function setOptions($options){ | |
126 | $this->optional = $options; | |
127 | $this->prc = false; | |
128 | } | |
129 | ||
130 | /** | |
131 | * Prepare the prc file | |
132 | * @return Prc The file that can be used to be saved/downloaded | |
133 | */ | |
134 | private function preparePRC(){ | |
135 | if($this->source === false){ | |
136 | throw new Exception("No data set"); | |
137 | } | |
138 | if($this->prc !== false) return $this->prc; | |
139 | ||
140 | $data = $this->source; | |
141 | $len = strlen($data); | |
142 | ||
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); | |
fb9df0c2 | 150 | $dataRecords[] = $rec->createFLISRecord(); |
4188f38a | 151 | $dataRecords[] = $rec->createFCISRecord($len); |
fb9df0c2 | 152 | $dataRecords[] = $rec->createEOFRecord(); |
4188f38a | 153 | $this->prc = new Prc($settings, $dataRecords); |
154 | return $this->prc; | |
155 | } | |
156 | ||
157 | /** | |
158 | * Save the file locally | |
159 | * @param string $filename Path to save the file | |
160 | */ | |
161 | public function save($filename){ | |
162 | $prc = $this->preparePRC(); | |
163 | $prc->save($filename); | |
164 | } | |
165 | ||
166 | /** | |
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" | |
170 | */ | |
171 | public function download($name){ | |
172 | $prc = $this->preparePRC(); | |
173 | $data = $prc->serialize(); | |
174 | $length = strlen($data); | |
175 | ||
176 | if($this->debug) return; //In debug mode, don't start the download | |
fb9df0c2 | 177 | |
4188f38a | 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); | |
4188f38a | 186 | |
fb9df0c2 | 187 | echo $data; |
4188f38a | 188 | //Finished! |
189 | } | |
190 | ||
191 | } | |
8ae45e7f | 192 | ?> |