PluginProbe
E2Pdf – Export Pdf Tool for WordPress / trunk
E2Pdf – Export Pdf Tool for WordPress vtrunk
1.32.48 1.32.43 1.32.40 1.32.34 1.32.32 1.32.31 1.32.26 1.32.22 1.32.23 1.32.18 1.32.17 1.32.15 trunk 1.00.00 1.00.13 1.01.01 1.02.02 1.03.07 1.04.07 1.05.03 1.06.02 1.07.11 1.08.00 1.08.06 1.08.09 All 71 releases
e2pdf / classes / model / e2pdf-font.php

e2pdf-font.php in E2Pdf – Export Pdf Tool for WordPress trunk, at classes/model/e2pdf-font.php

263 lines 10.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * File: /model/e2pdf-font.php
5 *
6 * @package E2Pdf
7 * @license GPLv3
8 * @link https://e2pdf.com
9 */
10 if (!defined('ABSPATH')) {
11 die('Access denied.');
12 }
13
14 class Model_E2pdf_Font extends Model_E2pdf_Model {
15
16 protected $text;
17 protected $ntOffset;
18 protected $regex = '/font-family:\s*["\']?(.*?)["\']?;/';
19
20 public function get_font_info($font = false, $key = false, $font_path = false) {
21
22 $font_tags = array();
23 if (!$font_path) {
24 $font_path = $this->helper->get('fonts_dir') . $font;
25 }
26 if ($font_path && file_exists($font_path) && filesize($font_path) > 0) {
27
28 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fopen
29 $fd = fopen($font_path, 'r');
30 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fread
31 $this->text = fread($fd, filesize($font_path));
32 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fclose
33 fclose($fd);
34
35 $number_of_tables = hexdec($this->dec2ord($this->text[4]) . $this->dec2ord($this->text[5]));
36
37 for ($i = 0; $i < $number_of_tables; $i++) {
38 $tag = $this->text[12 + $i * 16] . $this->text[12 + $i * 16 + 1] . $this->text[12 + $i * 16 + 2] . $this->text[12 + $i * 16 + 3];
39
40 if ($tag == 'name') {
41 $this->ntOffset = hexdec(
42 $this->dec2ord($this->text[12 + $i * 16 + 8]) . $this->dec2ord($this->text[12 + $i * 16 + 8 + 1]) .
43 $this->dec2ord($this->text[12 + $i * 16 + 8 + 2]) . $this->dec2ord($this->text[12 + $i * 16 + 8 + 3])
44 );
45
46 $offset_storage_dec = hexdec($this->dec2ord($this->text[$this->ntOffset + 4]) . $this->dec2ord($this->text[$this->ntOffset + 5]));
47 $number_name_records_dec = hexdec($this->dec2ord($this->text[$this->ntOffset + 2]) . $this->dec2ord($this->text[$this->ntOffset + 3]));
48 }
49 }
50
51 $storage_dec = $offset_storage_dec + $this->ntOffset;
52 $storage_hex = strtoupper(dechex($storage_dec));
53
54 for ($j = 0; $j < $number_name_records_dec; $j++) {
55 $platform_id_dec = hexdec($this->dec2ord($this->text[$this->ntOffset + 6 + $j * 12 + 0]) . $this->dec2ord($this->text[$this->ntOffset + 6 + $j * 12 + 1]));
56 $name_id_dec = hexdec($this->dec2ord($this->text[$this->ntOffset + 6 + $j * 12 + 6]) . $this->dec2ord($this->text[$this->ntOffset + 6 + $j * 12 + 7]));
57 $string_length_dec = hexdec($this->dec2ord($this->text[$this->ntOffset + 6 + $j * 12 + 8]) . $this->dec2ord($this->text[$this->ntOffset + 6 + $j * 12 + 9]));
58 $string_offset_dec = hexdec($this->dec2ord($this->text[$this->ntOffset + 6 + $j * 12 + 10]) . $this->dec2ord($this->text[$this->ntOffset + 6 + $j * 12 + 11]));
59
60 if (!empty($name_id_dec) && empty($font_tags[$name_id_dec])) {
61 for ($l = 0; $l < $string_length_dec; $l++) {
62 if (ord($this->text[$storage_dec + $string_offset_dec + $l]) == '0') {
63 continue;
64 } else {
65
66 if (!isset($font_tags[$name_id_dec])) {
67 $font_tags[$name_id_dec] = '';
68 }
69
70 $font_tags[$name_id_dec] .= ($this->text[$storage_dec + $string_offset_dec + $l]);
71 }
72 }
73 }
74 }
75 }
76
77 if ($key) {
78 if (!empty($font_tags) && isset($font_tags[$key])) {
79 return $font_tags[$key];
80 } else {
81 return false;
82 }
83 } else {
84 return $font_tags;
85 }
86 }
87
88 public function get_font($font, $md5 = false) {
89 $font_path = $this->helper->get('fonts_dir') . $font;
90
91 if (file_exists($font_path)) {
92 if ($md5) {
93 return md5_file($font_path);
94 } else {
95 // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode, WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
96 return base64_encode(file_get_contents($font_path));
97 }
98 }
99 return false;
100 }
101
102 public function get_fonts() {
103 $fonts = array();
104 $files = glob($this->helper->get('fonts_dir') . '*');
105 if ($files) {
106 foreach ($files as $key => $value) {
107 if (in_array(strtolower(pathinfo($value, PATHINFO_EXTENSION)), $this->get_allowed_extensions(), true)) {
108 $font_name = $this->get_font_info(basename($value), 4);
109 if ($font_name) {
110 $fonts[basename($value)] = $font_name;
111 }
112 }
113 }
114 }
115 return $fonts;
116 }
117
118 public function get_font_path($font) {
119 $fonts = $this->get_fonts();
120 $c_font = array_search($font, $fonts, true);
121 if ($c_font) {
122 return $this->helper->get('fonts_dir') . $c_font;
123 }
124 return false;
125 }
126
127 public function delete_font($font) {
128 $font_path = $this->helper->get('fonts_dir') . $font;
129
130 if (file_exists($font_path)) {
131 unlink($font_path);
132 return true;
133 }
134 return false;
135 }
136
137 public function get_element_fonts($el_value, $all_fonts) {
138 $fonts = [];
139 if (($el_value['type'] == 'e2pdf-html' || $el_value['type'] == 'e2pdf-page-number') && isset($el_value['value']) && $el_value['value']) {
140 preg_match_all($this->regex, htmlspecialchars_decode($el_value['value']), $matches);
141 if (isset($matches[1]) && !empty($matches[1])) {
142 foreach ($matches[1] as $font_key => $font_value) {
143 $exist = array_search($font_value, $fonts, true);
144 if (!$exist) {
145 $c_font = array_search($font_value, $all_fonts, true);
146 if ($c_font) {
147 $fonts[$c_font] = $font_value;
148 }
149 }
150 }
151 }
152
153 if (isset($el_value['properties']['css']) && $el_value['properties']['css']) {
154 preg_match_all($this->regex, htmlspecialchars_decode($el_value['properties']['css']), $matches);
155 if (isset($matches[1]) && !empty($matches[1])) {
156 foreach ($matches[1] as $font_key => $font_value) {
157 $exist = array_search($font_value, $fonts, true);
158 if (!$exist) {
159 $c_font = array_search($font_value, $all_fonts, true);
160 if ($c_font) {
161 $fonts[$c_font] = $font_value;
162 }
163 }
164 }
165 }
166 }
167 }
168
169 if (isset($el_value['properties']['text_font']) && $el_value['properties']['text_font']) {
170 $font_value = $el_value['properties']['text_font'];
171 $exist = array_search($font_value, $fonts, true);
172 if (!$exist) {
173 $c_font = array_search($font_value, $all_fonts, true);
174 if ($c_font) {
175 $fonts[$c_font] = $font_value;
176 }
177 }
178 }
179
180 //action fonts
181 if (isset($el_value['actions']) && !empty($el_value['actions'])) {
182 foreach ($el_value['actions'] as $action) {
183 if (isset($action['property']) && $action['property'] == 'text_font' && isset($action['change']) && $action['change']) {
184 $font_value = $action['change'];
185 $exist = array_search($font_value, $fonts, true);
186 if (!$exist) {
187 $c_font = array_search($font_value, $all_fonts, true);
188 if ($c_font) {
189 $fonts[$c_font] = $font_value;
190 }
191 }
192 }
193
194 if (($el_value['type'] == 'e2pdf-html' || $el_value['type'] == 'e2pdf-page-number') && isset($action['property']) && $action['property'] == 'value' && isset($action['change']) && $action['change']) {
195 preg_match_all($this->regex, htmlspecialchars_decode($action['change']), $matches);
196 if (isset($matches[1]) && !empty($matches[1])) {
197 foreach ($matches[1] as $font_key => $font_value) {
198 $exist = array_search($font_value, $fonts, true);
199 if (!$exist) {
200 $c_font = array_search($font_value, $all_fonts, true);
201 if ($c_font) {
202 $fonts[$c_font] = $font_value;
203 }
204 }
205 }
206 }
207 }
208
209 if (($el_value['type'] == 'e2pdf-html' || $el_value['type'] == 'e2pdf-page-number') && isset($action['property']) && $action['property'] == 'css' && isset($action['change']) && $action['change']) {
210 preg_match_all($this->regex, htmlspecialchars_decode($action['change']), $matches);
211 if (isset($matches[1]) && !empty($matches[1])) {
212 foreach ($matches[1] as $font_key => $font_value) {
213 $exist = array_search($font_value, $fonts, true);
214 if (!$exist) {
215 $c_font = array_search($font_value, $all_fonts, true);
216 if ($c_font) {
217 $fonts[$c_font] = $font_value;
218 }
219 }
220 }
221 }
222 }
223 }
224 }
225
226 return $fonts;
227 }
228
229 public function get_css_fonts($css, $all_fonts) {
230 $fonts = [];
231 preg_match_all($this->regex, htmlspecialchars_decode($css), $matches);
232 if (isset($matches[1]) && !empty($matches[1])) {
233 foreach ($matches[1] as $font_key => $font_value) {
234 $exist = array_search($font_value, $fonts, true);
235 if (!$exist) {
236 $c_font = array_search($font_value, $all_fonts, true);
237 if ($c_font) {
238 $fonts[$c_font] = $font_value;
239 }
240 }
241 }
242 }
243
244 return $fonts;
245 }
246
247 public function get_allowed_extensions() {
248 return array(
249 'ttf',
250 'otf',
251 );
252 }
253
254 protected function dec2ord($dec) {
255 return $this->dec2hex(ord($dec));
256 }
257
258 protected function dec2hex($dec) {
259 // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.Found
260 return str_repeat('0', 2 - strlen(($hex = strtoupper(dechex($dec))))) . $hex;
261 }
262 }
263