| @@ -3,9 +3,9 @@ | ||
| 3 | 3 | /** |
| 4 | 4 | * E2pdf Convert Helper |
| 5 | 5 | * |
| 6 | 6 | * @copyright Copyright 2017 https://e2pdf.com |
| 7 | - * @license GPL v2 | |
| 7 | + * @license GPLv3 | |
| 8 | 8 | * @version 1 |
| 9 | 9 | * @link https://e2pdf.com |
| 10 | 10 | * @since 1.01.02 |
| 11 | 11 | */ |
| @@ -14,25 +14,31 @@ | ||
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | class Helper_E2pdf_Convert { |
| 17 | 17 | |
| 18 | + private $helper; | |
| 19 | + | |
| 20 | + public function __construct() { | |
| 21 | + $this->helper = Helper_E2pdf_Helper::instance(); | |
| 22 | + } | |
| 23 | + | |
| 18 | 24 | public function to_hex_color($hex = '') { |
| 19 | - $color = array( | |
| 20 | - 0x00, 0x00, 0x00 | |
| 21 | - ); | |
| 25 | + $color = [ | |
| 26 | + 0x00, 0x00, 0x00, | |
| 27 | + ]; | |
| 22 | 28 | $hex = ltrim($hex, '#'); |
| 23 | 29 | if (strlen($hex) === 3) { |
| 24 | - $color = array( | |
| 30 | + $color = [ | |
| 25 | 31 | hexdec(substr($hex, 0, 1)), |
| 26 | 32 | hexdec(substr($hex, 1, 1)), |
| 27 | - hexdec(substr($hex, 2, 1)) | |
| 28 | - ); | |
| 33 | + hexdec(substr($hex, 2, 1)), | |
| 34 | + ]; | |
| 29 | 35 | } elseif (strlen($hex) === 6) { |
| 30 | - $color = array( | |
| 36 | + $color = [ | |
| 31 | 37 | hexdec(substr($hex, 0, 2)), |
| 32 | 38 | hexdec(substr($hex, 2, 2)), |
| 33 | - hexdec(substr($hex, 4, 2)) | |
| 34 | - ); | |
| 39 | + hexdec(substr($hex, 4, 2)), | |
| 40 | + ]; | |
| 35 | 41 | } |
| 36 | 42 | return $color; |
| 37 | 43 | } |
| 38 | 44 | |
| @@ -37,32 +43,63 @@ | ||
| 37 | 43 | } |
| 38 | 44 | |
| 39 | 45 | /** |
| 40 | 46 | * Convert from bytes to Human View |
| 41 | - * | |
| 42 | 47 | * @param int $size - Size in Bytes |
| 43 | - * | |
| 44 | 48 | * @return string - Converted size |
| 45 | 49 | */ |
| 50 | + public function from_bytes($size) { | |
| 51 | + $unit = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']; | |
| 52 | + return @round($size / pow(1024, ($i = floor(log($size, 1024)))), 2) . '' . $unit[$i]; | |
| 53 | + } | |
| 54 | + | |
| 46 | 55 | public function to_bytes($size) { |
| 47 | - $unit = array('B', 'KB', 'MB', 'GB', 'TB', 'PB'); | |
| 48 | - return @round($size / pow(1024, ($i = floor(log($size, 1024)))), 2) . '' . $unit[$i]; | |
| 56 | + $unit = preg_replace('/[^bkmgtpezy]/i', '', $size); | |
| 57 | + $size = preg_replace('/[^0-9\.]/', '', $size); | |
| 58 | + if ($unit) { | |
| 59 | + return round($size * pow(1024, stripos('bkmgtpezy', $unit[0]))); | |
| 60 | + } else { | |
| 61 | + return round($size); | |
| 62 | + } | |
| 49 | 63 | } |
| 50 | 64 | |
| 65 | + public function to_file_dir($file_dir = '') { | |
| 66 | + if ($file_dir) { | |
| 67 | + $file_dir = str_replace(['\\', './'], ['/', '.'], $file_dir); | |
| 68 | + } | |
| 69 | + return $file_dir; | |
| 70 | + } | |
| 71 | + | |
| 51 | 72 | public function to_file_name($file_name = '') { |
| 52 | 73 | if ($file_name) { |
| 53 | - $search = array( | |
| 74 | + $search = [ | |
| 54 | 75 | '/', |
| 55 | - '\\' | |
| 56 | - ); | |
| 57 | - $replace = array( | |
| 76 | + '\\', | |
| 77 | + '"', | |
| 78 | + '[', | |
| 79 | + ']', | |
| 80 | + ''', | |
| 81 | + '"', | |
| 82 | + '[', | |
| 83 | + ']', | |
| 84 | + '&', | |
| 85 | + ';', | |
| 86 | + ]; | |
| 87 | + $replace = [ | |
| 58 | 88 | '', |
| 59 | - '_' | |
| 60 | - ); | |
| 61 | - | |
| 89 | + '_', | |
| 90 | + '', | |
| 91 | + '[', | |
| 92 | + ']', | |
| 93 | + '\'', | |
| 94 | + '', | |
| 95 | + '[', | |
| 96 | + ']', | |
| 97 | + '&', | |
| 98 | + '', | |
| 99 | + ]; | |
| 62 | 100 | $file_name = str_replace($search, $replace, $file_name); |
| 63 | 101 | } |
| 64 | - | |
| 65 | 102 | return $file_name; |
| 66 | 103 | } |
| 67 | 104 | |
| 68 | 105 | /* |
| @@ -75,11 +112,11 @@ | ||
| 75 | 112 | if (is_array($needle)) { |
| 76 | 113 | $pos1 = 0; |
| 77 | 114 | $result = $haystack; |
| 78 | 115 | while (count($needle) > 0) { |
| 79 | - $positions = array(); | |
| 116 | + $positions = []; | |
| 80 | 117 | foreach ($needle as $from => $to) { |
| 81 | - if (( $pos2 = stripos($result, $from, $pos1) ) === FALSE) { | |
| 118 | + if (($pos2 = stripos($result, $from, $pos1)) === FALSE) { | |
| 82 | 119 | unset($needle[$from]); |
| 83 | 120 | } else { |
| 84 | 121 | $positions[$from] = $pos2; |
| 85 | 122 | } |
| @@ -89,10 +126,10 @@ | ||
| 89 | 126 | } |
| 90 | 127 | |
| 91 | 128 | $winner = min($positions); |
| 92 | 129 | $key = array_search($winner, $positions); |
| 93 | - $result = ( substr($result, 0, $winner) . $needle[$key] . substr($result, ( $winner + strlen($key))) ); | |
| 94 | - $pos1 = ( $winner + strlen($needle[$key]) ); | |
| 130 | + $result = (substr($result, 0, $winner) . $needle[$key] . substr($result, ($winner + strlen($key)))); | |
| 131 | + $pos1 = ($winner + strlen($needle[$key])); | |
| 95 | 132 | } |
| 96 | 133 | return $result; |
| 97 | 134 | } else { |
| 98 | 135 | return $haystack; |
| @@ -98,17 +135,8 @@ | ||
| 98 | 135 | return $haystack; |
| 99 | 136 | } |
| 100 | 137 | } |
| 101 | 138 | |
| 102 | - public function to_shortcode_array($value, $fields = array()) { | |
| 103 | - $list = explode(',', $value); | |
| 104 | - $data = array(); | |
| 105 | - foreach ($list as $item) { | |
| 106 | - $data[] = $this->parse_fields($item); | |
| 107 | - } | |
| 108 | - return $data; | |
| 109 | - } | |
| 110 | - | |
| 111 | 139 | public function path_to_url($path = '') { |
| 112 | 140 | $url = str_replace( |
| 113 | 141 | wp_normalize_path(untrailingslashit(ABSPATH)), site_url(), wp_normalize_path($path) |
| 114 | 142 | ); |
| @@ -114,34 +142,33 @@ | ||
| 114 | 142 | ); |
| 115 | 143 | return esc_url_raw($url); |
| 116 | 144 | } |
| 117 | 145 | |
| 118 | - public function to_content_key($content_key = false, $value = '') { | |
| 146 | + public function unserialize($value) { | |
| 147 | + /* Compatibility fix with Docket Cache */ | |
| 148 | + if (is_array($value)) { | |
| 149 | + return $value; | |
| 150 | + } | |
| 151 | + if (version_compare(PHP_VERSION, '7.0.0', '<')) { | |
| 152 | + return @unserialize($value); | |
| 153 | + } else { | |
| 154 | + return @unserialize($value, ['allowed_classes' => false]); | |
| 155 | + } | |
| 156 | + } | |
| 157 | + | |
| 158 | + public function is_content_key($content_key = false, $value = '') { | |
| 119 | 159 | $response = ''; |
| 120 | - | |
| 121 | 160 | if ($content_key) { |
| 122 | - $shortcode_tags = array( | |
| 123 | - 'e2pdf-content' | |
| 124 | - ); | |
| 161 | + $shortcode_tags = [ | |
| 162 | + 'e2pdf-content', | |
| 163 | + ]; | |
| 125 | 164 | preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $value, $matches); |
| 126 | 165 | $tagnames = array_intersect($shortcode_tags, $matches[1]); |
| 127 | - | |
| 128 | 166 | if (!empty($tagnames)) { |
| 129 | - $pattern = get_shortcode_regex($tagnames); | |
| 130 | - | |
| 131 | - preg_match_all("/$pattern/", $value, $shortcodes); | |
| 132 | - | |
| 167 | + preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $value, $shortcodes); | |
| 133 | 168 | foreach ($shortcodes[0] as $key => $shortcode_value) { |
| 134 | - $shortcode = array(); | |
| 135 | - $shortcode[1] = $shortcodes[1][$key]; | |
| 136 | - $shortcode[2] = $shortcodes[2][$key]; | |
| 137 | - $shortcode[3] = $shortcodes[3][$key]; | |
| 138 | - $shortcode[4] = $shortcodes[4][$key]; | |
| 139 | - $shortcode[5] = $shortcodes[5][$key]; | |
| 140 | - $shortcode[6] = $shortcodes[6][$key]; | |
| 141 | - | |
| 169 | + $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key); | |
| 142 | 170 | $atts = shortcode_parse_atts($shortcode[3]); |
| 143 | - | |
| 144 | 171 | if (isset($atts['key']) && $atts['key'] == $content_key) { |
| 145 | 172 | $response = $shortcode[5]; |
| 146 | 173 | } |
| 147 | 174 | } |
| @@ -146,30 +173,61 @@ | ||
| 146 | 173 | } |
| 147 | 174 | } |
| 148 | 175 | } |
| 149 | 176 | } |
| 177 | + return $response; | |
| 178 | + } | |
| 150 | 179 | |
| 151 | - return $response; | |
| 180 | + public function is_string_array($value) { | |
| 181 | + if (!is_array($value)) { | |
| 182 | + $value = !empty($value) ? (array) $value : []; | |
| 183 | + } | |
| 184 | + return $value; | |
| 152 | 185 | } |
| 153 | 186 | |
| 154 | - private function parse_fields($value) { | |
| 155 | - $data = array(); | |
| 156 | - $fields = explode("|", $value); | |
| 157 | - foreach ($fields as $field) { | |
| 158 | - $field_data = explode(":", $field); | |
| 159 | - $field_key = $field_data['0']; | |
| 160 | - unset($field_data['0']); | |
| 161 | - $field_value = implode(":", $field_data); | |
| 162 | - $data = array_merge_recursive($data, $this->parse_field($field_key, $field_value)); | |
| 187 | + public function is_unserialized_array($value) { | |
| 188 | + if (!is_array($value)) { | |
| 189 | + if (is_serialized($value)) { | |
| 190 | + $value = $this->unserialize($value); | |
| 191 | + } | |
| 163 | 192 | } |
| 164 | - return $data; | |
| 193 | + return is_array($value) ? $value : []; | |
| 165 | 194 | } |
| 166 | 195 | |
| 167 | - private function parse_field($field_key, $field_value) { | |
| 168 | - $keys = array_reverse(explode('_', $field_key)); | |
| 169 | - foreach ($keys as $key) { | |
| 170 | - $field_value = [$key => $field_value]; | |
| 196 | + public function load_html($source, $dom, $form = false) { | |
| 197 | + libxml_use_internal_errors(true); | |
| 198 | + $options = defined('LIBXML_HTML_NOIMPLIED') && defined('LIBXML_HTML_NODEFDTD') ? LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD : 0; | |
| 199 | + if ($form && $options) { | |
| 200 | + $source = '<html>' . $source . '</html>'; | |
| 171 | 201 | } |
| 172 | - return $field_value; | |
| 202 | + if (function_exists('mb_encode_numericentity')) { | |
| 203 | + $html = $dom->loadHTML($this->html_entities($source), $options); | |
| 204 | + } else { | |
| 205 | + $html = $dom->loadHTML('<?xml encoding="UTF-8">' . $source, $options); | |
| 206 | + } | |
| 207 | + libxml_clear_errors(); | |
| 208 | + return $html; | |
| 173 | 209 | } |
| 174 | 210 | |
| 211 | + public function html_entities($source) { | |
| 212 | + if (!function_exists('mb_encode_numericentity')) { | |
| 213 | + return $source; | |
| 214 | + } | |
| 215 | + $map = [ | |
| 216 | + 0x80, 0xFFFF, 0, 0xFFFF, | |
| 217 | + ]; | |
| 218 | + try { | |
| 219 | + return mb_encode_numericentity($source, $map, 'UTF-8'); | |
| 220 | + } catch (Exception $e) { | |
| 221 | + if (function_exists('mb_detect_encoding') && function_exists('mb_detect_order') && function_exists('iconv')) { | |
| 222 | + $charset = mb_detect_encoding($source, mb_detect_order(), true); | |
| 223 | + $source = iconv($charset, 'UTF-8//IGNORE', $source); | |
| 224 | + return mb_encode_numericentity($source, $map, 'UTF-8'); | |
| 225 | + } | |
| 226 | + return $source; | |
| 227 | + } | |
| 228 | + } | |
| 229 | + | |
| 230 | + public function html_entities_decode($source) { | |
| 231 | + return html_entity_decode($source, ENT_QUOTES | ENT_HTML5, 'UTF-8'); | |
| 232 | + } | |
| 175 | 233 | } |