| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* E2pdf Convert Helper |
| 5 |
* |
| 6 |
* @copyright Copyright 2017 https://e2pdf.com |
| 7 |
* @license GPLv3 |
| 8 |
* @version 1 |
| 9 |
* @link https://e2pdf.com |
| 10 |
* @since 1.01.02 |
| 11 |
*/ |
| 12 |
if (!defined('ABSPATH')) { |
| 13 |
die('Access denied.'); |
| 14 |
} |
| 15 |
|
| 16 |
class Helper_E2pdf_Convert { |
| 17 |
|
| 18 |
private $helper; |
| 19 |
|
| 20 |
public function __construct() { |
| 21 |
$this->helper = Helper_E2pdf_Helper::instance(); |
| 22 |
} |
| 23 |
|
| 24 |
public function to_hex_color($hex = '') { |
| 25 |
$color = [ |
| 26 |
0x00, 0x00, 0x00, |
| 27 |
]; |
| 28 |
$hex = ltrim($hex, '#'); |
| 29 |
if (strlen($hex) === 3) { |
| 30 |
$color = [ |
| 31 |
hexdec(substr($hex, 0, 1)), |
| 32 |
hexdec(substr($hex, 1, 1)), |
| 33 |
hexdec(substr($hex, 2, 1)), |
| 34 |
]; |
| 35 |
} elseif (strlen($hex) === 6) { |
| 36 |
$color = [ |
| 37 |
hexdec(substr($hex, 0, 2)), |
| 38 |
hexdec(substr($hex, 2, 2)), |
| 39 |
hexdec(substr($hex, 4, 2)), |
| 40 |
]; |
| 41 |
} |
| 42 |
return $color; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Convert from bytes to Human View |
| 47 |
* @param int $size - Size in Bytes |
| 48 |
* @return string - Converted size |
| 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 |
|
| 55 |
public function to_bytes($size) { |
| 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 |
} |
| 63 |
} |
| 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 |
|
| 72 |
public function to_file_name($file_name = '') { |
| 73 |
if ($file_name) { |
| 74 |
$search = [ |
| 75 |
'/', |
| 76 |
'\\', |
| 77 |
'"', |
| 78 |
'[', |
| 79 |
']', |
| 80 |
''', |
| 81 |
'"', |
| 82 |
'[', |
| 83 |
']', |
| 84 |
'&', |
| 85 |
';', |
| 86 |
]; |
| 87 |
$replace = [ |
| 88 |
'', |
| 89 |
'_', |
| 90 |
'', |
| 91 |
'[', |
| 92 |
']', |
| 93 |
'\'', |
| 94 |
'', |
| 95 |
'[', |
| 96 |
']', |
| 97 |
'&', |
| 98 |
'', |
| 99 |
]; |
| 100 |
$file_name = str_replace($search, $replace, $file_name); |
| 101 |
} |
| 102 |
return $file_name; |
| 103 |
} |
| 104 |
|
| 105 |
/* |
| 106 |
stritr - case insensitive version of strtr |
| 107 |
Author: Alexander Peev |
| 108 |
Posted in PHP.NET |
| 109 |
*/ |
| 110 |
|
| 111 |
public function stritr($haystack, $needle) { |
| 112 |
if (is_array($needle)) { |
| 113 |
$pos1 = 0; |
| 114 |
$result = $haystack; |
| 115 |
while (count($needle) > 0) { |
| 116 |
$positions = []; |
| 117 |
foreach ($needle as $from => $to) { |
| 118 |
if (($pos2 = stripos($result, $from, $pos1)) === FALSE) { |
| 119 |
unset($needle[$from]); |
| 120 |
} else { |
| 121 |
$positions[$from] = $pos2; |
| 122 |
} |
| 123 |
} |
| 124 |
if (count($needle) <= 0) { |
| 125 |
break; |
| 126 |
} |
| 127 |
|
| 128 |
$winner = min($positions); |
| 129 |
$key = array_search($winner, $positions); |
| 130 |
$result = (substr($result, 0, $winner) . $needle[$key] . substr($result, ($winner + strlen($key)))); |
| 131 |
$pos1 = ($winner + strlen($needle[$key])); |
| 132 |
} |
| 133 |
return $result; |
| 134 |
} else { |
| 135 |
return $haystack; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
public function path_to_url($path = '') { |
| 140 |
$url = str_replace( |
| 141 |
wp_normalize_path(untrailingslashit(ABSPATH)), site_url(), wp_normalize_path($path) |
| 142 |
); |
| 143 |
return esc_url_raw($url); |
| 144 |
} |
| 145 |
|
| 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 = '') { |
| 159 |
$response = ''; |
| 160 |
if ($content_key) { |
| 161 |
$shortcode_tags = [ |
| 162 |
'e2pdf-content', |
| 163 |
]; |
| 164 |
preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $value, $matches); |
| 165 |
$tagnames = array_intersect($shortcode_tags, $matches[1]); |
| 166 |
if (!empty($tagnames)) { |
| 167 |
preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $value, $shortcodes); |
| 168 |
foreach ($shortcodes[0] as $key => $shortcode_value) { |
| 169 |
$shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key); |
| 170 |
$atts = shortcode_parse_atts($shortcode[3]); |
| 171 |
if (isset($atts['key']) && $atts['key'] == $content_key) { |
| 172 |
$response = $shortcode[5]; |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
return $response; |
| 178 |
} |
| 179 |
|
| 180 |
public function is_string_array($value) { |
| 181 |
if (!is_array($value)) { |
| 182 |
$value = !empty($value) ? (array) $value : []; |
| 183 |
} |
| 184 |
return $value; |
| 185 |
} |
| 186 |
|
| 187 |
public function is_unserialized_array($value) { |
| 188 |
if (!is_array($value)) { |
| 189 |
if (is_serialized($value)) { |
| 190 |
$value = $this->unserialize($value); |
| 191 |
} |
| 192 |
} |
| 193 |
return is_array($value) ? $value : []; |
| 194 |
} |
| 195 |
|
| 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>'; |
| 201 |
} |
| 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; |
| 209 |
} |
| 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 |
} |
| 233 |
} |
| 234 |
|