PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.32.49
E2Pdf – Export Pdf Tool for WordPress v1.32.49
1.32.49 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 All 72 releases
← All changes | classes/model/e2pdf-font.php +99 -47 1.08.001.32.49 View file →
@@ -1,14 +1,12 @@
1 1 <?php
2 2
3 3 /**
4 - * E2pdf Font Model
5 - *
6 - * @copyright Copyright 2017 https://e2pdf.com
7 - * @license GPL v2
8 - * @version 1
9 - * @link https://e2pdf.com
10 - * @since 0.00.01
4 + * File: /model/e2pdf-font.php
5 + *
6 + * @package E2Pdf
7 + * @license GPLv3
8 + * @link https://e2pdf.com
11 9 */
12 10 if (!defined('ABSPATH')) {
13 11 die('Access denied.');
14 12 }
@@ -15,29 +13,24 @@
15 13
16 14 class Model_E2pdf_Font extends Model_E2pdf_Model {
17 15
18 16 protected $text;
17 + protected $ntOffset;
18 + protected $regex = '/font-family:\s*["\']?(.*?)["\']?;/';
19 19
20 - /**
21 - * Initialize functions
22 - */
23 - function __construct() {
24 - parent::__construct();
25 - }
26 -
27 20 public function get_font_info($font = false, $key = false, $font_path = false) {
28 21
29 22 $font_tags = array();
30 -
31 23 if (!$font_path) {
32 - $font_path = $this->helper->get("fonts_dir") . $font;
24 + $font_path = $this->helper->get('fonts_dir') . $font;
33 25 }
34 -
35 -
36 26 if ($font_path && file_exists($font_path) && filesize($font_path) > 0) {
37 27
38 - $fd = fopen($font_path, "r");
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
39 31 $this->text = fread($fd, filesize($font_path));
32 + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fclose
40 33 fclose($fd);
41 34
42 35 $number_of_tables = hexdec($this->dec2ord($this->text[4]) . $this->dec2ord($this->text[5]));
43 36
@@ -46,9 +39,10 @@
46 39
47 40 if ($tag == 'name') {
48 41 $this->ntOffset = hexdec(
49 42 $this->dec2ord($this->text[12 + $i * 16 + 8]) . $this->dec2ord($this->text[12 + $i * 16 + 8 + 1]) .
50 - $this->dec2ord($this->text[12 + $i * 16 + 8 + 2]) . $this->dec2ord($this->text[12 + $i * 16 + 8 + 3]));
43 + $this->dec2ord($this->text[12 + $i * 16 + 8 + 2]) . $this->dec2ord($this->text[12 + $i * 16 + 8 + 3])
44 + );
51 45
52 46 $offset_storage_dec = hexdec($this->dec2ord($this->text[$this->ntOffset + 4]) . $this->dec2ord($this->text[$this->ntOffset + 5]));
53 47 $number_name_records_dec = hexdec($this->dec2ord($this->text[$this->ntOffset + 2]) . $this->dec2ord($this->text[$this->ntOffset + 3]));
54 48 }
@@ -62,9 +56,9 @@
62 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]));
63 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]));
64 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]));
65 59
66 - if (!empty($name_id_dec) and empty($font_tags[$name_id_dec])) {
60 + if (!empty($name_id_dec) && empty($font_tags[$name_id_dec])) {
67 61 for ($l = 0; $l < $string_length_dec; $l++) {
68 62 if (ord($this->text[$storage_dec + $string_offset_dec + $l]) == '0') {
69 63 continue;
70 64 } else {
@@ -69,9 +63,9 @@
69 63 continue;
70 64 } else {
71 65
72 66 if (!isset($font_tags[$name_id_dec])) {
73 - $font_tags[$name_id_dec] = "";
67 + $font_tags[$name_id_dec] = '';
74 68 }
75 69
76 70 $font_tags[$name_id_dec] .= ($this->text[$storage_dec + $string_offset_dec + $l]);
77 71 }
@@ -90,27 +84,31 @@
90 84 return $font_tags;
91 85 }
92 86 }
93 87
94 - public function get_font($font) {
88 + public function get_font($font, $md5 = false) {
95 89 $font_path = $this->helper->get('fonts_dir') . $font;
96 90
97 91 if (file_exists($font_path)) {
98 - return base64_encode(file_get_contents($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 + }
99 98 }
100 99 return false;
101 100 }
102 101
103 102 public function get_fonts() {
104 -
105 103 $fonts = array();
106 - $files = glob($this->helper->get('fonts_dir') . "*");
104 + $files = glob($this->helper->get('fonts_dir') . '*');
107 105 if ($files) {
108 106 foreach ($files as $key => $value) {
109 - if (pathinfo($value, PATHINFO_EXTENSION) == 'ttf') {
110 - $font_info = $this->get_font_info(basename($value), 4);
111 - if ($font_info) {
112 - $fonts[basename($value)] = $font_info;
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;
113 111 }
114 112 }
115 113 }
116 114 }
@@ -118,10 +116,9 @@
118 116 }
119 117
120 118 public function get_font_path($font) {
121 119 $fonts = $this->get_fonts();
122 - $c_font = array_search($font, $fonts);
123 -
120 + $c_font = array_search($font, $fonts, true);
124 121 if ($c_font) {
125 122 return $this->helper->get('fonts_dir') . $c_font;
126 123 }
127 124 return false;
@@ -136,17 +133,17 @@
136 133 }
137 134 return false;
138 135 }
139 136
140 - function get_element_fonts($el_value, $all_fonts) {
141 - $fonts = array();
142 - if ($el_value['type'] == 'e2pdf-html' && isset($el_value['value']) && $el_value['value']) {
143 - preg_match_all('/font-family:[\s]+?"(.*?)";/', htmlspecialchars_decode($el_value['value']), $matches);
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);
144 141 if (isset($matches[1]) && !empty($matches[1])) {
145 142 foreach ($matches[1] as $font_key => $font_value) {
146 - $exist = array_search($font_value, $fonts);
143 + $exist = array_search($font_value, $fonts, true);
147 144 if (!$exist) {
148 - $c_font = array_search($font_value, $all_fonts);
145 + $c_font = array_search($font_value, $all_fonts, true);
149 146 if ($c_font) {
150 147 $fonts[$c_font] = $font_value;
151 148 }
152 149 }
@@ -151,15 +148,30 @@
151 148 }
152 149 }
153 150 }
154 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 + }
155 167 }
156 168
157 169 if (isset($el_value['properties']['text_font']) && $el_value['properties']['text_font']) {
158 170 $font_value = $el_value['properties']['text_font'];
159 - $exist = array_search($font_value, $fonts);
171 + $exist = array_search($font_value, $fonts, true);
160 172 if (!$exist) {
161 - $c_font = array_search($font_value, $all_fonts);
173 + $c_font = array_search($font_value, $all_fonts, true);
162 174 if ($c_font) {
163 175 $fonts[$c_font] = $font_value;
164 176 }
165 177 }
@@ -169,11 +181,11 @@
169 181 if (isset($el_value['actions']) && !empty($el_value['actions'])) {
170 182 foreach ($el_value['actions'] as $action) {
171 183 if (isset($action['property']) && $action['property'] == 'text_font' && isset($action['change']) && $action['change']) {
172 184 $font_value = $action['change'];
173 - $exist = array_search($font_value, $fonts);
185 + $exist = array_search($font_value, $fonts, true);
174 186 if (!$exist) {
175 - $c_font = array_search($font_value, $all_fonts);
187 + $c_font = array_search($font_value, $all_fonts, true);
176 188 if ($c_font) {
177 189 $fonts[$c_font] = $font_value;
178 190 }
179 191 }
@@ -178,15 +190,15 @@
178 190 }
179 191 }
180 192 }
181 193
182 - if ($el_value['type'] == 'e2pdf-html' && isset($action['property']) && $action['property'] == 'value' && isset($action['change']) && $action['change']) {
183 - preg_match_all('/font-family:[\s]+?"(.*?)";/', htmlspecialchars_decode($action['change']), $matches);
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);
184 196 if (isset($matches[1]) && !empty($matches[1])) {
185 197 foreach ($matches[1] as $font_key => $font_value) {
186 - $exist = array_search($font_value, $fonts);
198 + $exist = array_search($font_value, $fonts, true);
187 199 if (!$exist) {
188 - $c_font = array_search($font_value, $all_fonts);
200 + $c_font = array_search($font_value, $all_fonts, true);
189 201 if ($c_font) {
190 202 $fonts[$c_font] = $font_value;
191 203 }
192 204 }
@@ -192,8 +204,23 @@
192 204 }
193 205 }
194 206 }
195 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 + }
196 223 }
197 224 }
198 225
199 226 return $fonts;
@@ -198,13 +225,38 @@
198 225
199 226 return $fonts;
200 227 }
201 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 +
202 254 protected function dec2ord($dec) {
203 255 return $this->dec2hex(ord($dec));
204 256 }
205 257
206 258 protected function dec2hex($dec) {
259 + // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.Found
207 260 return str_repeat('0', 2 - strlen(($hex = strtoupper(dechex($dec))))) . $hex;
208 261 }
209 -
210 262 }