diff options
author | Thomas Citharel <tcit@tcit.fr> | 2014-12-14 00:16:39 +0100 |
---|---|---|
committer | Thomas Citharel <tcit@tcit.fr> | 2014-12-14 00:16:39 +0100 |
commit | 1256e4c645c86f4165d052bf8116b957b373b837 (patch) | |
tree | 3cc3bd960df403a6e0645afcd9e4f5bbb68bfc0e /inc/3rdparty/libraries/MOBIClass/RecordFactory.php | |
parent | 9c55ed0923273da886497cb62302a79622e34a74 (diff) | |
parent | dc69d3e8d895c87f9d26c2d1309e40d6090d4c8d (diff) | |
download | wallabag-1256e4c645c86f4165d052bf8116b957b373b837.tar.gz wallabag-1256e4c645c86f4165d052bf8116b957b373b837.tar.zst wallabag-1256e4c645c86f4165d052bf8116b957b373b837.zip |
Merge branch 'tcitworld-ebook' into dev
Diffstat (limited to 'inc/3rdparty/libraries/MOBIClass/RecordFactory.php')
-rw-r--r-- | inc/3rdparty/libraries/MOBIClass/RecordFactory.php | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/inc/3rdparty/libraries/MOBIClass/RecordFactory.php b/inc/3rdparty/libraries/MOBIClass/RecordFactory.php new file mode 100644 index 00000000..12806fe3 --- /dev/null +++ b/inc/3rdparty/libraries/MOBIClass/RecordFactory.php | |||
@@ -0,0 +1,115 @@ | |||
1 | <?php | ||
2 | |||
3 | /** | ||
4 | * Helper class to help with creating records from a | ||
5 | * long data stream | ||
6 | * | ||
7 | * @author Sander | ||
8 | */ | ||
9 | class RecordFactory { | ||
10 | /** | ||
11 | * Settings for the record factory | ||
12 | * @var Settings | ||
13 | */ | ||
14 | private $settings; | ||
15 | |||
16 | /** | ||
17 | * Create the helper class | ||
18 | * @param Settings $settings The Settings to be used for the records | ||
19 | */ | ||
20 | public function __construct($settings){ | ||
21 | $this->settings = $settings; | ||
22 | } | ||
23 | |||
24 | /** | ||
25 | * Create records from a data string | ||
26 | * @param string $data | ||
27 | * @return array(Record) | ||
28 | */ | ||
29 | public function createRecords($data){ | ||
30 | $records = array(); | ||
31 | $size = $this->settings->get("recordSize"); | ||
32 | $compression = $this->settings->get("compression"); | ||
33 | |||
34 | $dataEntries = mb_str_split($data, $size); | ||
35 | |||
36 | for($i = 0, $len = sizeof($dataEntries); $i < $len; $i++){ | ||
37 | $records[$i] = new Record($dataEntries[$i]); | ||
38 | $records[$i]->compress($compression); | ||
39 | } | ||
40 | |||
41 | return $records; | ||
42 | } | ||
43 | |||
44 | public function createEOFRecord(){ | ||
45 | return new Record(0xe98e0d0a); | ||
46 | } | ||
47 | |||
48 | public function createFCISRecord($textLength){ | ||
49 | $r = "FCIS"; | ||
50 | $r .= $this->asString(20, 4); | ||
51 | $r .= $this->asString(16, 4); | ||
52 | $r .= $this->asString(1, 4); | ||
53 | $r .= $this->asString(0, 4); | ||
54 | $r .= $this->asString($textLength, 4); | ||
55 | $r .= $this->asString(0, 4); | ||
56 | $r .= $this->asString(32, 4); | ||
57 | $r .= $this->asString(8, 4); | ||
58 | $r .= $this->asString(1, 2); | ||
59 | $r .= $this->asString(1, 2); | ||
60 | $r .= $this->asString(0, 4); | ||
61 | return new Record($r); | ||
62 | } | ||
63 | |||
64 | public function createFLISRecord(){ | ||
65 | $r = "FLIS"; | ||
66 | $r .= $this->asString(8, 4); | ||
67 | $r .= $this->asString(65, 2); | ||
68 | $r .= $this->asString(0, 2); | ||
69 | $r .= $this->asString(0, 4); | ||
70 | $r .= $this->asString(-1, 4); | ||
71 | $r .= $this->asString(1, 2); | ||
72 | $r .= $this->asString(3, 2); | ||
73 | $r .= $this->asString(3, 4); | ||
74 | $r .= $this->asString(1, 4); | ||
75 | $r .= $this->asString(-1, 4); | ||
76 | return new Record($r); | ||
77 | } | ||
78 | |||
79 | private function asString($int, $size){ | ||
80 | $out = ""; | ||
81 | for($i = 0; $i < $size; $i++){ | ||
82 | if($i > 0) $out = " ".$out; | ||
83 | $byte = dechex($int & 0xFF); | ||
84 | if(strlen($byte) == 1) $byte = "0".$byte; | ||
85 | $out = $byte.$out; | ||
86 | $int = $int >> 8; | ||
87 | } | ||
88 | return $out; | ||
89 | } | ||
90 | |||
91 | public function __toString() { | ||
92 | $out = "Record Factory: {\n"; | ||
93 | $out .= "\tRecord Size: ".$this->settings->get("recordSize")."\n"; | ||
94 | $out .= "\tCompression: ".$this->settings->get("compression")."\n"; | ||
95 | $out .= "}"; | ||
96 | return $out; | ||
97 | } | ||
98 | } | ||
99 | function mb_str_split($string, $split_length = 1){ | ||
100 | mb_internal_encoding('UTF-8'); | ||
101 | mb_regex_encoding('UTF-8'); | ||
102 | |||
103 | $split_length = ($split_length <= 0) ? 1 : $split_length; | ||
104 | |||
105 | $mb_strlen = mb_strlen($string, 'utf-8'); | ||
106 | |||
107 | $array = array(); | ||
108 | |||
109 | for($i = 0; $i < $mb_strlen; $i += $split_length){ | ||
110 | $array[] = mb_substr($string, $i, $split_length); | ||
111 | } | ||
112 | |||
113 | return $array; | ||
114 | } | ||
115 | ?> | ||