| @@ -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 | } |
| @@ -116,8 +114,17 @@ | ||
| 116 | 114 | } |
| 117 | 115 | return $fonts; |
| 118 | 116 | } |
| 119 | 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 | + | |
| 120 | 127 | public function delete_font($font) { |
| 121 | 128 | $font_path = $this->helper->get('fonts_dir') . $font; |
| 122 | 129 | |
| 123 | 130 | if (file_exists($font_path)) { |
| @@ -126,17 +133,17 @@ | ||
| 126 | 133 | } |
| 127 | 134 | return false; |
| 128 | 135 | } |
| 129 | 136 | |
| 130 | - function get_element_fonts($el_value, $all_fonts) { | |
| 131 | - $fonts = array(); | |
| 132 | - if ($el_value['type'] == 'e2pdf-html' && isset($el_value['value']) && $el_value['value']) { | |
| 133 | - 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); | |
| 134 | 141 | if (isset($matches[1]) && !empty($matches[1])) { |
| 135 | 142 | foreach ($matches[1] as $font_key => $font_value) { |
| 136 | - $exist = array_search($font_value, $fonts); | |
| 143 | + $exist = array_search($font_value, $fonts, true); | |
| 137 | 144 | if (!$exist) { |
| 138 | - $c_font = array_search($font_value, $all_fonts); | |
| 145 | + $c_font = array_search($font_value, $all_fonts, true); | |
| 139 | 146 | if ($c_font) { |
| 140 | 147 | $fonts[$c_font] = $font_value; |
| 141 | 148 | } |
| 142 | 149 | } |
| @@ -141,15 +148,30 @@ | ||
| 141 | 148 | } |
| 142 | 149 | } |
| 143 | 150 | } |
| 144 | 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 | + } | |
| 145 | 167 | } |
| 146 | 168 | |
| 147 | 169 | if (isset($el_value['properties']['text_font']) && $el_value['properties']['text_font']) { |
| 148 | 170 | $font_value = $el_value['properties']['text_font']; |
| 149 | - $exist = array_search($font_value, $fonts); | |
| 171 | + $exist = array_search($font_value, $fonts, true); | |
| 150 | 172 | if (!$exist) { |
| 151 | - $c_font = array_search($font_value, $all_fonts); | |
| 173 | + $c_font = array_search($font_value, $all_fonts, true); | |
| 152 | 174 | if ($c_font) { |
| 153 | 175 | $fonts[$c_font] = $font_value; |
| 154 | 176 | } |
| 155 | 177 | } |
| @@ -159,11 +181,11 @@ | ||
| 159 | 181 | if (isset($el_value['actions']) && !empty($el_value['actions'])) { |
| 160 | 182 | foreach ($el_value['actions'] as $action) { |
| 161 | 183 | if (isset($action['property']) && $action['property'] == 'text_font' && isset($action['change']) && $action['change']) { |
| 162 | 184 | $font_value = $action['change']; |
| 163 | - $exist = array_search($font_value, $fonts); | |
| 185 | + $exist = array_search($font_value, $fonts, true); | |
| 164 | 186 | if (!$exist) { |
| 165 | - $c_font = array_search($font_value, $all_fonts); | |
| 187 | + $c_font = array_search($font_value, $all_fonts, true); | |
| 166 | 188 | if ($c_font) { |
| 167 | 189 | $fonts[$c_font] = $font_value; |
| 168 | 190 | } |
| 169 | 191 | } |
| @@ -168,15 +190,15 @@ | ||
| 168 | 190 | } |
| 169 | 191 | } |
| 170 | 192 | } |
| 171 | 193 | |
| 172 | - if ($el_value['type'] == 'e2pdf-html' && isset($action['property']) && $action['property'] == 'value' && isset($action['change']) && $action['change']) { | |
| 173 | - 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); | |
| 174 | 196 | if (isset($matches[1]) && !empty($matches[1])) { |
| 175 | 197 | foreach ($matches[1] as $font_key => $font_value) { |
| 176 | - $exist = array_search($font_value, $fonts); | |
| 198 | + $exist = array_search($font_value, $fonts, true); | |
| 177 | 199 | if (!$exist) { |
| 178 | - $c_font = array_search($font_value, $all_fonts); | |
| 200 | + $c_font = array_search($font_value, $all_fonts, true); | |
| 179 | 201 | if ($c_font) { |
| 180 | 202 | $fonts[$c_font] = $font_value; |
| 181 | 203 | } |
| 182 | 204 | } |
| @@ -182,8 +204,23 @@ | ||
| 182 | 204 | } |
| 183 | 205 | } |
| 184 | 206 | } |
| 185 | 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 | + } | |
| 186 | 223 | } |
| 187 | 224 | } |
| 188 | 225 | |
| 189 | 226 | return $fonts; |
| @@ -188,13 +225,38 @@ | ||
| 188 | 225 | |
| 189 | 226 | return $fonts; |
| 190 | 227 | } |
| 191 | 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 | + | |
| 192 | 254 | protected function dec2ord($dec) { |
| 193 | 255 | return $this->dec2hex(ord($dec)); |
| 194 | 256 | } |
| 195 | 257 | |
| 196 | 258 | protected function dec2hex($dec) { |
| 259 | + // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.Found | |
| 197 | 260 | return str_repeat('0', 2 - strlen(($hex = strtoupper(dechex($dec))))) . $hex; |
| 198 | 261 | } |
| 199 | - | |
| 200 | 262 | } |