]> git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/3rdparty/libraries/MOBIClass/PalmRecord.php
phpepub via composer
[github/wallabag/wallabag.git] / inc / 3rdparty / libraries / MOBIClass / PalmRecord.php
1 <?php
2 /**
3 * A Record of a PDB file
4 *
5 * @author Sander
6 */
7 class PalmRecord extends FileObject {
8 /**
9 * @var FileElement
10 */
11 private $elements;
12
13 public function __construct($settings, $records, $textRecords, $textLength, $images){
14 $this->elements = new FileElement(array(
15 "compression"=>new FileShort(),
16 "unused"=>new FileShort(),
17 "textLength"=>new FileInt(),
18 "recordCount"=>new FileShort(),
19 "recordSize"=>new FileShort(),
20 "encryptionType"=>new FileShort(),
21 "unused2"=>new FileShort(),
22 //MOBI Header
23 "mobiIdentifier"=>new FileString("MOBI", 4),
24 "mobiHeaderLength"=>new FileInt(),
25 "mobiType"=>new FileInt(),
26 "textEncoding"=>new FileInt(),
27 "uniqueID"=>new FileInt(),
28 "fileVersion"=>new FileInt(),
29 "reserved"=>new FileString(40),
30 "firstNonBookIndex"=>new FileInt(),
31 "fullNameOffset"=>new FileInt(),
32 "fullNameLength"=>new FileInt(),
33 "locale"=>new FileInt(),
34 "inputLanguage"=>new FileInt(),
35 "outputLanguage"=>new FileInt(),
36 "minimumVersion"=>new FileInt(),
37 "firstImageIndex"=>new FileInt(),
38 "huffmanRecordOffset"=>new FileInt(),
39 "huffmanRecordCount"=>new FileInt(),
40 "unused3"=>new FileString(8),
41 "exthFlags"=>new FileInt(0x40),
42 "unknown"=>new FileString(32),
43 "drmOffset"=>new FileInt(0xFFFFFFFF),
44 "drmCount"=>new FileShort(0xFFFFFFFF),
45 "drmSize"=>new FileShort(),
46 "drmFlags"=>new FileInt(),
47 "mobiFiller"=>new FileString(72),
48 //EXTH Header
49 "exthIdentifier"=>new FileString("EXTH", 4),
50 "exthHeaderLength"=>new FileInt(),
51 "exthRecordCount"=>new FileInt(),
52 "exthRecords"=>new FileElement(),
53 "exthPadding"=>new FileString(),
54 //"fullNamePadding"=>new FileString(100),
55 "fullName"=>new FileString()
56 ));
57
58 //Set values from the info block
59 foreach($settings->values as $name => $val){
60 //echo $name.", ";
61 if($this->elements->exists($name)){
62 $this->elements->get($name)->set($settings->get($name));
63 }
64 }
65
66 $els = $settings->values;
67
68 $exthElems = new FileElement();
69 $i = 0;
70 $l = 0;
71 foreach($els as $name=>$val){
72 $type = EXTHHelper::textToType($name);
73 if($type !== false){
74 $type = new FileInt($type);
75 $length = new FileInt(8+strlen($val));
76 $data = new FileString($val);
77 $l += 8+strlen($val);
78 $exthElems->add("type".$i, $type);
79 $exthElems->add("length".$i, $length);
80 $exthElems->add("data".$i, $data);
81 $i++;
82 }
83 }
84
85 if($images > 0){
86 $this->elements->get("firstImageIndex")->set($textRecords+1);
87 }
88 $this->elements->get("firstNonBookIndex")->set($textRecords+2+$images);
89 $this->elements->get("reserved")->set(str_pad("", 40, chr(255), STR_PAD_RIGHT));
90 $this->elements->get("exthRecordCount")->set($i);
91 $this->elements->set("exthRecords", $exthElems);
92 $pad = $l%4;
93 $pad = (4-$pad)%4;
94 $this->elements->get("exthPadding")->set(str_pad("", $pad, "\0", STR_PAD_RIGHT));
95 $this->elements->get("exthHeaderLength")->set(12+$l+$pad);
96
97
98 $this->elements->get("recordCount")->set($textRecords);
99
100 $this->elements->get("fullNameOffset")->set($this->elements->offsetToEntry("fullName"));
101 $this->elements->get("fullNameLength")->set(strlen($settings->get("title")));
102 $this->elements->get("fullName")->set($settings->get("title"));
103 $this->elements->get("textLength")->set($textLength);
104 }
105
106 public function getByteLength(){
107 return $this->getLength();
108 }
109
110 public function getLength(){
111 return $this->elements->getByteLength();
112 }
113
114 public function get(){
115 return $this;
116 }
117
118 public function set($elements){
119 throw new Exception("Unallowed set");
120 }
121
122 public function serialize() {
123 return $this->elements->serialize();
124 }
125
126 public function unserialize($data) {
127 $this->elements->unserialize($data);
128 }
129
130 public function __toString(){
131 $output = "PalmDoc Record (".$this->getByteLength()." bytes):\n";
132 $output .= $this->elements;
133 return $output;
134 }
135 }
136 ?>