HTML
1 year ago
views
5 months ago
Apply.php
6 months ago
Cron.php
1 year ago
CronJob.php
7 months ago
CronJobs.php
2 months ago
Crypt.php
1 month ago
DownloadStats.php
5 months ago
Email.php
5 days ago
EmailCron.php
1 year ago
FileSystem.php
1 year ago
Installer.php
20 hours ago
Messages.php
1 year ago
Query.php
4 months ago
Session.php
20 hours ago
Settings.php
4 years ago
SimpleMath.php
4 years ago
TempStorage.php
20 hours ago
Template.php
5 months ago
UI.php
6 months ago
Updater.php
4 years ago
UserAgent.php
2 years ago
__.php
1 month ago
__MailUI.php
3 years ago
__.php
873 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace WPDM\__; |
| 5 | |
| 6 | |
| 7 | use function Symfony\Component\VarDumper\Dumper\esc; |
| 8 | |
| 9 | class __ { |
| 10 | /** |
| 11 | * Get the client's IP address |
| 12 | * |
| 13 | */ |
| 14 | static function get_client_ip() { |
| 15 | $proxy_allowed = defined( 'WPDM_PROXY_IP_ALLOWED' ) && WPDM_PROXY_IP_ALLOWED === true; |
| 16 | $ipaddress = ''; |
| 17 | if ( $proxy_allowed && getenv( 'HTTP_CLIENT_IP' ) ) { |
| 18 | $ipaddress = getenv( 'HTTP_CLIENT_IP' ); |
| 19 | } else if ( $proxy_allowed && getenv( 'HTTP_X_FORWARDED_FOR' ) ) { |
| 20 | $ipaddress = getenv( 'HTTP_X_FORWARDED_FOR' ); |
| 21 | } else if ( $proxy_allowed && getenv( 'HTTP_X_FORWARDED' ) ) { |
| 22 | $ipaddress = getenv( 'HTTP_X_FORWARDED' ); |
| 23 | } else if ( $proxy_allowed && getenv( 'HTTP_FORWARDED_FOR' ) ) { |
| 24 | $ipaddress = getenv( 'HTTP_FORWARDED_FOR' ); |
| 25 | } else if ( $proxy_allowed && getenv( 'HTTP_FORWARDED' ) ) { |
| 26 | $ipaddress = getenv( 'HTTP_FORWARDED' ); |
| 27 | } else if ( getenv( 'REMOTE_ADDR' ) ) { |
| 28 | $ipaddress = getenv( 'REMOTE_ADDR' ); |
| 29 | } else { |
| 30 | $ipaddress = 'UNKNOWN'; |
| 31 | } |
| 32 | |
| 33 | $ipaddress = explode( ",", $ipaddress ); |
| 34 | |
| 35 | return esc_attr( $ipaddress[0] ); |
| 36 | } |
| 37 | |
| 38 | |
| 39 | static function media_field( $data ) { |
| 40 | ob_start(); |
| 41 | $id = isset( $data['id'] ) ? sanitize_text_field( $data['id'] ) : uniqid(); |
| 42 | ?> |
| 43 | <div class="input-group"> |
| 44 | <input placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" type="url" |
| 45 | name="<?php echo esc_attr( $data['name'] ); ?>" |
| 46 | id="<?php echo esc_attr( $id ); ?>" class="form-control" |
| 47 | value="<?php echo isset( $data['value'] ) ? esc_attr( $data['value'] ) : ''; ?>"/> |
| 48 | <span class="input-group-btn"> |
| 49 | <button class="btn btn-default btn-media-upload" type="button" |
| 50 | rel="#<?php echo esc_attr( $id ); ?>"><i |
| 51 | class="fa fa-picture-o"></i></button> |
| 52 | </span> |
| 53 | </div> |
| 54 | <?php |
| 55 | return ob_get_clean(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @usage Genrate option fields |
| 60 | * |
| 61 | * @param $data |
| 62 | * |
| 63 | * @return mixed|string |
| 64 | */ |
| 65 | static function option_field( $data, $fieldprefix ) { |
| 66 | $desc = isset( $data['description'] ) ? "<em class='note'>{$data['description']}</em>" : ""; |
| 67 | $class = isset( $data['class'] ) ? $data['class'] : ""; |
| 68 | $data['placeholder'] = isset( $data['placeholder'] ) ? $data['placeholder'] : ''; |
| 69 | switch ( $data['type'] ): |
| 70 | case 'text': |
| 71 | return "<input type='text' name='{$fieldprefix}[{$data['name']}]' class='form-control {$class}' id='$data[id]' value='$data[value]' placeholder='{$data['placeholder']}' />$desc"; |
| 72 | break; |
| 73 | case 'select': |
| 74 | case 'dropdown': |
| 75 | $html = "<select name='{$fieldprefix}[{$data['name']}]' id='{$data['id']}' class='form-control {$class}' style='width:100%;min-width:150px;' >"; |
| 76 | foreach ( $data['options'] as $value => $label ) { |
| 77 | |
| 78 | $html .= "<option value='{$value}' " . selected( $data['selected'], $value, false ) . ">$label</option>"; |
| 79 | } |
| 80 | $html .= "</select>"; |
| 81 | |
| 82 | return $html . $desc; |
| 83 | break; |
| 84 | case 'notice': |
| 85 | return "<div class='alert alert-info' style='margin: 0'>$data[notice]</div>" . $desc; |
| 86 | case 'textarea': |
| 87 | return "<textarea name='{$fieldprefix}[{$data['name']}]' id='$data[id]' class='form-control {$class}' style='min-height: 100px'>$data[value]</textarea>$desc"; |
| 88 | break; |
| 89 | case 'checkbox': |
| 90 | return "<input type='hidden' name='{$fieldprefix}[{$data['name']}]' value='0' /><input type='checkbox' class='{$class}' name='$data[name]' id='$data[id]' value='$data[value]' " . checked( $data['checked'], $data['value'], false ) . " />" . $desc; |
| 91 | break; |
| 92 | case 'callback': |
| 93 | return call_user_func( $data['dom_callback'], $data['dom_callback_params'] ) . $desc; |
| 94 | break; |
| 95 | case 'heading': |
| 96 | return "<h3>" . $data['label'] . "</h3>"; |
| 97 | break; |
| 98 | case 'media': |
| 99 | return __::media_field( $data ); |
| 100 | break; |
| 101 | default: |
| 102 | return "<input type='{$data['type']}' name='{$fieldprefix}[{$data['name']}]' class='form-control {$class}' id='$data[id]' value='$data[value]' placeholder='{$data['placeholder']}' />$desc"; |
| 103 | break; |
| 104 | endswitch; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @param $options |
| 109 | * |
| 110 | * @return string |
| 111 | */ |
| 112 | static function option_page( $options, $fieldprefix = '' ) { |
| 113 | $html = "<div class='wpdm-settings-fields'>"; |
| 114 | foreach ( $options as $id => $option ) { |
| 115 | $option['name'] = $id; |
| 116 | if ( ! isset( $option['id'] ) ) { |
| 117 | $option['id'] = $id; |
| 118 | } |
| 119 | if ( in_array( $option['type'], array( 'checkbox', 'radio' ) ) ) { |
| 120 | $html .= "<div class='form-group'><label>" . option_field( $option ) . " {$option['label']}</label></div>"; |
| 121 | } else if ( $option['type'] == 'heading' ) { |
| 122 | $html .= "<h3>{$option['label']}</h3>"; |
| 123 | } else { |
| 124 | $html .= "<div class='form-group'><label>{$option['label']}</label>" . option_field( $option, $fieldprefix ) . "</div>"; |
| 125 | } |
| 126 | } |
| 127 | $html .= "</div>"; |
| 128 | |
| 129 | return $html; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | /** |
| 134 | * @param $name |
| 135 | * @param $options |
| 136 | * |
| 137 | * @return string |
| 138 | */ |
| 139 | static function settings_section( $name, $options ) { |
| 140 | return "<div class='panel panel-default'><div class='panel-heading'>{$name}</div><div class='panel-body'>" . option_page( $options ) . "</div></div>"; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | /** |
| 145 | * @param $var |
| 146 | * @param $index |
| 147 | * @param $params |
| 148 | * @param $validate |
| 149 | * |
| 150 | * @return array|float|int|mixed|string|string[]|null |
| 151 | */ |
| 152 | static function valueof( $var, $index, $params = [], $validate = null ) { |
| 153 | |
| 154 | $index = substr_count($index, '->') ? explode( "->", $index ) : explode( "/", $index ); |
| 155 | $default = is_string( $params ) ? $params : ''; |
| 156 | if ( is_object( $var ) ) { |
| 157 | $var = json_decode( json_encode( $var ), true ); |
| 158 | } |
| 159 | $default = is_array( $params ) && isset( $params['default'] ) ? $params['default'] : $default; |
| 160 | if ( count( $index ) > 1 ) { |
| 161 | $val = $var; |
| 162 | foreach ( $index as $key ) { |
| 163 | if ( is_object( $val ) ) { |
| 164 | $val = (array) $val; |
| 165 | } |
| 166 | $val = is_array( $val ) && isset( $val[ $key ] ) ? $val[ $key ] : '__not__set__'; |
| 167 | if ( $val === '__not__set__' ) { |
| 168 | return $default; |
| 169 | } |
| 170 | } |
| 171 | } else { |
| 172 | $val = isset( $var[ $index[0] ] ) ? $var[ $index[0] ] : $default; |
| 173 | } |
| 174 | $validate = is_array( $params ) && isset( $params['validate'] ) && ! $validate ? $params['validate'] : $validate; |
| 175 | if ( $validate ) { |
| 176 | if ( ! is_array( $val ) ) { |
| 177 | $val = __::sanitize_var( $val, $validate ); |
| 178 | } else { |
| 179 | $val = __::sanitize_array( $val, $validate ); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | return $val; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * @usage Validate and sanitize input data |
| 188 | * |
| 189 | * @param $var |
| 190 | * @param array $params |
| 191 | * |
| 192 | * @return int|null|string |
| 193 | */ |
| 194 | static function query_var( $var, $params_or_validate = array(), $default = null ) { |
| 195 | global $wp_query; |
| 196 | |
| 197 | $params = $params_or_validate; |
| 198 | |
| 199 | $_var = explode( "/", $var ); |
| 200 | if ( count( $_var ) > 1 ) { |
| 201 | $val = $_REQUEST; |
| 202 | foreach ( $_var as $key ) { |
| 203 | $val = is_array( $val ) && isset( $val[ $key ] ) ? $val[ $key ] : false; |
| 204 | } |
| 205 | } else { |
| 206 | $default = $default ?: ( isset( $params['default'] ) ? $params['default'] : null ); |
| 207 | $val = isset( $_REQUEST[ $var ] ) ? $_REQUEST[ $var ] : null; |
| 208 | if ( ! $val ) { |
| 209 | $val = isset( $wp_query->query_vars[ $var ] ) ? $wp_query->query_vars[ $var ] : $default; |
| 210 | } |
| 211 | } |
| 212 | $validate = is_string( $params ) ? $params : ''; |
| 213 | $validate = is_array( $params ) && isset( $params['validate'] ) ? $params['validate'] : $validate; |
| 214 | |
| 215 | if ( ! is_array( $val ) ) { |
| 216 | $val = __::sanitize_var( $val, $validate ); |
| 217 | } else { |
| 218 | $val = __::sanitize_array( $val, $validate ); |
| 219 | } |
| 220 | |
| 221 | return $val; |
| 222 | } |
| 223 | |
| 224 | public static function decode_html($html){ |
| 225 | $html = htmlspecialchars_decode($html); |
| 226 | $html = html_entity_decode($html, ENT_QUOTES); |
| 227 | $html = stripslashes_deep($html); |
| 228 | return $html; |
| 229 | } |
| 230 | |
| 231 | static function timeStamp( $date, $format = true ) { |
| 232 | if ( $format === false ) { |
| 233 | return strtotime( $date ); |
| 234 | } |
| 235 | if ( $format === true ) { |
| 236 | $format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' ); |
| 237 | } |
| 238 | $dateobj = \DateTime::createFromFormat( $format, $date ); |
| 239 | |
| 240 | return $dateobj->getTimestamp(); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Sanitize an array or any single value |
| 245 | * |
| 246 | * @param $array |
| 247 | * |
| 248 | * @return mixed |
| 249 | */ |
| 250 | static function sanitize_array( $array, $sanitize = '' ) { |
| 251 | |
| 252 | if (!is_array($array)) return __::sanitize_var($array, $sanitize); |
| 253 | |
| 254 | foreach ( $array as $key => &$value ) { |
| 255 | $validate = is_array($sanitize) ? __::valueof($sanitize, $key) : $sanitize; |
| 256 | if ( is_array( $value ) ) { |
| 257 | __::sanitize_array( $value, $validate ); |
| 258 | } else { |
| 259 | $value = __::sanitize_var( $value, $validate ); |
| 260 | } |
| 261 | $array[ $key ] = &$value; |
| 262 | } |
| 263 | |
| 264 | return $array; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * @param $value |
| 269 | * @param string $sanitize |
| 270 | * |
| 271 | * @return array|float|int|mixed|string|string[]|null |
| 272 | */ |
| 273 | static function sanitize_var( $value, $sanitize = '' ) { |
| 274 | if ( is_array( $value ) ) { |
| 275 | return __::sanitize_array( $value, $sanitize ); |
| 276 | } else { |
| 277 | switch ( $sanitize ) { |
| 278 | |
| 279 | case 'int': |
| 280 | case 'num': |
| 281 | return (int) $value; |
| 282 | |
| 283 | case 'double': |
| 284 | case 'float': |
| 285 | return (float) ( $value ); |
| 286 | |
| 287 | case 'esc_html': |
| 288 | $value = esc_sql( esc_html( $value ) ); |
| 289 | break; |
| 290 | |
| 291 | case 'txt': |
| 292 | case 'str': |
| 293 | $value = sanitize_text_field( $value ); |
| 294 | break; |
| 295 | |
| 296 | case 'esc_attr': |
| 297 | $value = esc_attr( $value ); |
| 298 | break; |
| 299 | |
| 300 | case 'kses': |
| 301 | $allowedtags = wp_kses_allowed_html( 'post' ); |
| 302 | $value = __::decode_html( $value ); |
| 303 | $value = wp_kses( $value, $allowedtags ); |
| 304 | break; |
| 305 | |
| 306 | case 'serverpath': |
| 307 | $value = realpath( $value ); |
| 308 | $value = str_replace( "\\", "/", $value ); |
| 309 | break; |
| 310 | |
| 311 | case 'txts': |
| 312 | $value = sanitize_textarea_field( $value ); |
| 313 | break; |
| 314 | |
| 315 | case 'url': |
| 316 | $value = sanitize_url( urldecode( $value ) ); |
| 317 | break; |
| 318 | |
| 319 | case 'noscript': |
| 320 | case 'escs': |
| 321 | $value = wpdm_escs( $value ); |
| 322 | break; |
| 323 | |
| 324 | case 'filename': |
| 325 | $value = sanitize_file_name( $value ); |
| 326 | break; |
| 327 | |
| 328 | case 'alpha': |
| 329 | $value = preg_replace( "/([^a-zA-Z])/", '', $value ); |
| 330 | break; |
| 331 | |
| 332 | case 'alphanum': |
| 333 | $value = preg_replace( "/([^a-zA-Z0-9])/", '', $value ); |
| 334 | break; |
| 335 | |
| 336 | case 'safetxt': |
| 337 | $value = preg_replace("/([^a-zA-Z0-9.,_|\-\s])/", '', $value); |
| 338 | break; |
| 339 | |
| 340 | case 'username': |
| 341 | $value = preg_replace("/([^a-zA-Z0-9_\-\s])/", '', $value); |
| 342 | break; |
| 343 | |
| 344 | case 'html': |
| 345 | |
| 346 | break; |
| 347 | |
| 348 | default: |
| 349 | $value = esc_sql( esc_attr( $value ) ); |
| 350 | if ( $sanitize !== '' && self::isRegexPattern( $sanitize ) ) { |
| 351 | $result = @preg_replace( $sanitize, '', $value ); |
| 352 | if ( $result !== null ) { |
| 353 | $value = $result; |
| 354 | } |
| 355 | } |
| 356 | break; |
| 357 | } |
| 358 | $value = __::escs( $value ); |
| 359 | } |
| 360 | |
| 361 | return $value; |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Check whether a string looks like a valid PCRE pattern. |
| 366 | * |
| 367 | * A PCRE pattern must start with a delimiter that is not alphanumeric |
| 368 | * or backslash, and the same delimiter (or its bracket pair) must close |
| 369 | * the pattern. Without this guard, calling preg_match() on bare strings |
| 370 | * like "array" emits a PHP warning that monitoring tools still capture |
| 371 | * even when prefixed with @. |
| 372 | */ |
| 373 | static function isRegexPattern( $pattern ) { |
| 374 | if ( ! is_string( $pattern ) || strlen( $pattern ) < 2 ) { |
| 375 | return false; |
| 376 | } |
| 377 | $delim = $pattern[0]; |
| 378 | if ( ctype_alnum( $delim ) || $delim === '\\' ) { |
| 379 | return false; |
| 380 | } |
| 381 | $pairs = [ '(' => ')', '[' => ']', '{' => '}', '<' => '>' ]; |
| 382 | $closing = $pairs[ $delim ] ?? $delim; |
| 383 | $end = strrpos( $pattern, $closing ); |
| 384 | return $end !== false && $end > 0; |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * @usage Escape script tag |
| 389 | * |
| 390 | * @param $html |
| 391 | * |
| 392 | * @return null|string|string[] |
| 393 | */ |
| 394 | static function escs( $html ) { |
| 395 | return preg_replace( '#<script(.*?)>(.*?)</script>#is', '', $html ); |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * @return bool |
| 400 | */ |
| 401 | static function is_ajax() { |
| 402 | return ( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) == 'xmlhttprequest' ); |
| 403 | } |
| 404 | |
| 405 | static function rebuildUrl(array $parts): string |
| 406 | { |
| 407 | $url = ''; |
| 408 | |
| 409 | if (isset($parts['scheme'])) { |
| 410 | $url .= $parts['scheme'] . '://'; |
| 411 | } |
| 412 | |
| 413 | if (isset($parts['user'])) { |
| 414 | $url .= $parts['user']; |
| 415 | if (isset($parts['pass'])) { |
| 416 | $url .= ':' . $parts['pass']; |
| 417 | } |
| 418 | $url .= '@'; |
| 419 | } |
| 420 | |
| 421 | if (isset($parts['host'])) { |
| 422 | $url .= $parts['host']; |
| 423 | } |
| 424 | |
| 425 | if (isset($parts['port'])) { |
| 426 | $url .= ':' . $parts['port']; |
| 427 | } |
| 428 | |
| 429 | if (isset($parts['path'])) { |
| 430 | $url .= $parts['path']; |
| 431 | } |
| 432 | |
| 433 | if (isset($parts['query'])) { |
| 434 | $url .= '?' . $parts['query']; |
| 435 | } |
| 436 | |
| 437 | if (isset($parts['fragment'])) { |
| 438 | $url .= '#' . $parts['fragment']; |
| 439 | } |
| 440 | |
| 441 | return $url; |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * @param $file |
| 446 | * |
| 447 | * @return bool |
| 448 | */ |
| 449 | static function isMediaLibFile($file) { |
| 450 | return substr_count($file, 'wp-content/uploads/') > 0; |
| 451 | } |
| 452 | |
| 453 | static function is_url( $url ) { |
| 454 | $isValid = false; |
| 455 | |
| 456 | // Define supported schemes as a constant for reusability and clarity |
| 457 | $allowedSchemes = [ |
| 458 | 'http', 'https', 'ftp', 'ftps', |
| 459 | 'magnet', 'tor', 'bitcoin', 'ipfs', |
| 460 | 'mailto', 'tel', 'file', 'data', |
| 461 | 'custom' |
| 462 | ]; |
| 463 | |
| 464 | // Parse and normalize the URL |
| 465 | $parsedUrl = parse_url($url); |
| 466 | if (!isset($parsedUrl['scheme'])) { |
| 467 | return apply_filters( '__is_url', false, $url ); |
| 468 | } |
| 469 | |
| 470 | $scheme = strtolower($parsedUrl['scheme']); |
| 471 | |
| 472 | if (isset($parsedUrl['path'])) { |
| 473 | $segments = explode('/', $parsedUrl['path']); |
| 474 | $parsedUrl['path'] = implode('/', array_map('rawurlencode', $segments)); |
| 475 | } |
| 476 | |
| 477 | // Normalize fragment by encoding it (to pass filter_var and comply with RFCs) |
| 478 | if (isset($parsedUrl['fragment'])) { |
| 479 | $parsedUrl['fragment'] = rawurlencode($parsedUrl['fragment']); |
| 480 | } |
| 481 | |
| 482 | $url = self::rebuildUrl($parsedUrl); |
| 483 | |
| 484 | |
| 485 | // Scheme-specific validation |
| 486 | if (!in_array($scheme, $allowedSchemes, true)) { |
| 487 | $isValid = false; |
| 488 | } elseif (in_array($scheme, ['http', 'https', 'ftp', 'ftps'], true)) { |
| 489 | $isValid = filter_var($url, FILTER_VALIDATE_URL) !== false; |
| 490 | } elseif ($scheme === 'magnet') { |
| 491 | $isValid = preg_match('/^magnet:\?xt=urn:[a-z0-9]+:[a-zA-Z0-9]{32,}/i', $url) === 1; |
| 492 | } elseif ($scheme === 'tor') { |
| 493 | $isValid = preg_match('/^tor:\/\/[a-z2-7]{16,56}\.onion/i', $url) === 1; |
| 494 | } elseif (preg_match('/\.onion(:[0-9]+)?(\/.*)?$/i', $url)) { |
| 495 | $isValid = true; |
| 496 | } else { |
| 497 | // Fallback: generic URI format validation for other schemes |
| 498 | $isValid = preg_match('/^[a-z][a-z0-9+\-.]*:[^\s]+$/i', $url) === 1; |
| 499 | } |
| 500 | //wpdmdd( (int)$isValid, $url); |
| 501 | return apply_filters( '__is_url', $isValid, $url ); |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * @usage Post with cURL |
| 506 | * |
| 507 | * @param $url |
| 508 | * @param $data (array) |
| 509 | * @param $headers (array) |
| 510 | * |
| 511 | * @return bool|mixed|string |
| 512 | */ |
| 513 | static function remote_post( $url, $data, $headers = [] ) { |
| 514 | |
| 515 | $response = wp_remote_post( $url, array( |
| 516 | 'method' => 'POST', |
| 517 | 'sslverify' => false, |
| 518 | 'timeout' => 5, |
| 519 | 'redirection' => 5, |
| 520 | 'httpversion' => '1.0', |
| 521 | 'blocking' => true, |
| 522 | 'headers' => $headers, |
| 523 | 'body' => $data, |
| 524 | 'cookies' => array() |
| 525 | ) |
| 526 | ); |
| 527 | $body = wp_remote_retrieve_body( $response ); |
| 528 | |
| 529 | return $body; |
| 530 | } |
| 531 | |
| 532 | /** |
| 533 | * @usage Get with cURL |
| 534 | * |
| 535 | * @param $url |
| 536 | * @param $headers (array) |
| 537 | * |
| 538 | * @return bool|mixed|string |
| 539 | */ |
| 540 | static function remote_get( $url, $headers = [] ) { |
| 541 | $content = ""; |
| 542 | $response = wp_remote_get( $url, array( 'timeout' => 5, 'sslverify' => false, 'headers' => $headers ) ); |
| 543 | if ( is_array( $response ) ) { |
| 544 | $content = $response['body']; |
| 545 | } else { |
| 546 | $content = Messages::error( $response->get_error_message(), - 1 ); |
| 547 | } |
| 548 | |
| 549 | return $content; |
| 550 | } |
| 551 | |
| 552 | static function hex2rgb( $hex, $array = false ) { |
| 553 | list( $r, $g, $b ) = sscanf( $hex, "#%02x%02x%02x" ); |
| 554 | |
| 555 | return $array ? [ 'r' => $r, 'g' => $g, 'b' => $b ] : "$r, $g, $b"; |
| 556 | } |
| 557 | |
| 558 | /** |
| 559 | * @usage Quote all elements in an array |
| 560 | * |
| 561 | * @param $values |
| 562 | * |
| 563 | * @return mixed |
| 564 | */ |
| 565 | static function quote_all_array( $values ) { |
| 566 | foreach ( $values as $key => $value ) { |
| 567 | if ( is_array( $value ) ) { |
| 568 | $values[ $key ] = self::quote_all_array( $value ); |
| 569 | } else { |
| 570 | $values[ $key ] = self:: quote_it( $value ); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | return $values; |
| 575 | } |
| 576 | |
| 577 | /** |
| 578 | * @usage Quoate a value |
| 579 | * |
| 580 | * @param $value |
| 581 | * |
| 582 | * @return array|string |
| 583 | */ |
| 584 | static function quote_it( $value ) { |
| 585 | if ( is_null( $value ) ) { |
| 586 | return "NULL"; |
| 587 | } |
| 588 | $value = '"' . esc_sql( $value ) . '"'; |
| 589 | |
| 590 | return $value; |
| 591 | } |
| 592 | |
| 593 | |
| 594 | /** |
| 595 | * Splice associative array |
| 596 | * |
| 597 | * @param $input |
| 598 | * @param $offset |
| 599 | * @param $length |
| 600 | * @param $replacement |
| 601 | */ |
| 602 | static function array_splice_assoc( &$input, $offset, $length, $replacement ) { |
| 603 | $replacement = (array) $replacement; |
| 604 | $key_indices = array_flip( array_keys( $input ) ); |
| 605 | if ( isset( $input[ $offset ] ) && is_string( $offset ) ) { |
| 606 | $offset = $key_indices[ $offset ]; |
| 607 | } |
| 608 | if ( isset( $input[ $length ] ) && is_string( $length ) ) { |
| 609 | $length = $key_indices[ $length ] - $offset; |
| 610 | } |
| 611 | |
| 612 | $input = array_slice( $input, 0, $offset, true ) |
| 613 | + $replacement |
| 614 | + array_slice( $input, $offset + $length, null, true ); |
| 615 | } |
| 616 | |
| 617 | /** |
| 618 | * @param $email |
| 619 | * @param $skipDNS |
| 620 | * |
| 621 | * @return bool |
| 622 | */ |
| 623 | static function isValidEmail( $email, $skipDNS = true ) { |
| 624 | $isValid = true; |
| 625 | if ( ! is_string( $email ) ) { |
| 626 | return false; |
| 627 | } |
| 628 | $atIndex = strrpos( $email, "@" ); |
| 629 | if ( is_bool( $atIndex ) && ! $atIndex ) { |
| 630 | $isValid = false; |
| 631 | } else { |
| 632 | $domain = substr( $email, $atIndex + 1 ); |
| 633 | $local = substr( $email, 0, $atIndex ); |
| 634 | $localLen = strlen( $local ); |
| 635 | $domainLen = strlen( $domain ); |
| 636 | if ( $localLen < 1 || $localLen > 64 ) { |
| 637 | // local part length exceeded |
| 638 | $isValid = false; |
| 639 | } else if ( $domainLen < 1 || $domainLen > 255 ) { |
| 640 | // domain part length exceeded |
| 641 | $isValid = false; |
| 642 | } else if ( $local[0] == '.' || $local[ $localLen - 1 ] == '.' ) { |
| 643 | // local part starts or ends with '.' |
| 644 | $isValid = false; |
| 645 | } else if ( preg_match( '/\\.\\./', $local ) ) { |
| 646 | // local part has two consecutive dots |
| 647 | $isValid = false; |
| 648 | } else if ( ! preg_match( '/^[A-Za-z0-9\\-\\.]+$/', $domain ) ) { |
| 649 | // character not valid in domain part |
| 650 | $isValid = false; |
| 651 | } else if ( preg_match( '/\\.\\./', $domain ) ) { |
| 652 | // domain part has two consecutive dots |
| 653 | $isValid = false; |
| 654 | } else if ( ! preg_match( '/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace( "\\\\", "", $local ) ) ) { |
| 655 | // character not valid in local part unless |
| 656 | // local part is quoted |
| 657 | if ( ! preg_match( '/^"(\\\\"|[^"])+"$/', str_replace( "\\\\", "", $local ) ) ) { |
| 658 | $isValid = false; |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | if ( ! $skipDNS ) { |
| 663 | if ( $isValid && ! ( checkdnsrr( $domain, "MX" ) || checkdnsrr( $domain, "A" ) ) ) { |
| 664 | // domain not found in DNS |
| 665 | $isValid = false; |
| 666 | } |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | return $isValid; |
| 671 | } |
| 672 | |
| 673 | |
| 674 | static function mnemonicNumberFormat( $number, $plus = true ) { |
| 675 | if ( $number > 1000000 ) { |
| 676 | $number = number_format( ( $number / 1000000 ), 1 ); |
| 677 | $number = $number > (int) $number && $plus ? (int) $number . 'M+' : (int) $number . 'M'; |
| 678 | |
| 679 | return $number; |
| 680 | } |
| 681 | if ( $number > 1000 ) { |
| 682 | $number = number_format( ( $number / 1000 ), 1 ); |
| 683 | $number = $number > (int) $number && $plus ? (int) $number . 'K+' : (int) $number . 'K'; |
| 684 | |
| 685 | return $number; |
| 686 | } |
| 687 | |
| 688 | return $number; |
| 689 | } |
| 690 | |
| 691 | static function timezoneOffset() { |
| 692 | $offet = get_option( 'gmt_offset' ); |
| 693 | if ( $offet ) { |
| 694 | return $offet * 3600; |
| 695 | } |
| 696 | $timezone_string = get_option( 'timezone_string' ); |
| 697 | if ( ! $timezone_string ) { |
| 698 | return 0; |
| 699 | } |
| 700 | $timezone = timezone_open( $timezone_string ); |
| 701 | $datetime = date_create( "now", timezone_open( get_option( 'timezone_string' ) ) ); |
| 702 | |
| 703 | return timezone_offset_get( $timezone, $datetime ); |
| 704 | } |
| 705 | |
| 706 | /** |
| 707 | * @param $bytes |
| 708 | * @param $precision |
| 709 | * |
| 710 | * @return string |
| 711 | */ |
| 712 | static function formatBytes( $bytes, $precision = 2 ) { |
| 713 | $base = log( $bytes, 1024 ); |
| 714 | $suffixes = array( 'B', 'KB', 'MB', 'GB', 'TB' ); |
| 715 | |
| 716 | return round( pow( 1024, $base - floor( $base ) ), $precision ) . ' ' . $suffixes[ floor( $base ) ]; |
| 717 | |
| 718 | } |
| 719 | |
| 720 | static function convertToBytes( $sizeString ) { |
| 721 | $sizeUnits = [ 'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' ]; |
| 722 | |
| 723 | // Extract numeric value and unit from the input string |
| 724 | preg_match( '/^([\d.]+)\s*(' . implode( '|', $sizeUnits ) . ')$/i', $sizeString, $matches ); |
| 725 | |
| 726 | if ( ! $matches ) { |
| 727 | // Invalid input format |
| 728 | return false; |
| 729 | } |
| 730 | |
| 731 | $size = (float) $matches[1]; |
| 732 | $unit = strtoupper( $matches[2] ); |
| 733 | |
| 734 | // Convert to bytes based on the unit |
| 735 | $power = array_search( $unit, $sizeUnits ); |
| 736 | $bytes = $size * ( 1024 ** $power ); |
| 737 | |
| 738 | return $bytes; |
| 739 | } |
| 740 | |
| 741 | static function isAuthentic( $nonce_var, $nonce_key, $access_level, $is_ajax = true ) { |
| 742 | $nonce_var = __::sanitize_var( $nonce_var, 'txt' ); |
| 743 | if ( $is_ajax ) { |
| 744 | if ( ! check_ajax_referer( $nonce_key, $nonce_var, false ) ) { |
| 745 | wp_send_json( [ |
| 746 | 'success' => false, |
| 747 | 'type' => 'error', |
| 748 | 'message' => esc_attr__( 'Referer verification failed', WPDM_TEXT_DOMAIN ) |
| 749 | ] ); |
| 750 | } |
| 751 | } |
| 752 | if ( ! wp_verify_nonce( wpdm_query_var( $nonce_var ), $nonce_key ) ) { |
| 753 | wp_send_json( [ |
| 754 | 'success' => false, |
| 755 | 'type' => 'error', |
| 756 | 'message' => __( 'Security token is expired! Refresh the page and try again.', 'download-manager' ) |
| 757 | ] ); |
| 758 | } |
| 759 | |
| 760 | if ( in_array( $access_level, [ 'visitor', 'read', 'guest' ] ) ) { |
| 761 | return true; |
| 762 | } |
| 763 | |
| 764 | if ( ! current_user_can( $access_level ) ) { |
| 765 | wp_send_json( [ |
| 766 | 'success' => false, |
| 767 | 'type' => 'error', |
| 768 | 'message' => __( "You are not allowed to execute this action!", "download-manager" ) |
| 769 | ] ); |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | static function requestURI() { |
| 774 | $uri = ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] === 'on' ? "https" : "http" ) . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; |
| 775 | $uri = esc_url_raw( $uri ); |
| 776 | |
| 777 | return $uri; |
| 778 | } |
| 779 | |
| 780 | |
| 781 | static function a( $array, $default = [] ) { |
| 782 | if ( ! is_array( $default ) ) { |
| 783 | $default = []; |
| 784 | } |
| 785 | if ( ! is_array( $array ) ) { |
| 786 | $array = $default; |
| 787 | } |
| 788 | |
| 789 | return $array; |
| 790 | } |
| 791 | |
| 792 | static function push( &$array, $index, $item, $overwrite = false ) { |
| 793 | if ( ! is_array( $array ) ) { |
| 794 | $array = []; |
| 795 | } |
| 796 | if ( ! isset( $array[ $index ] ) || $overwrite ) { |
| 797 | $array[ $index ] = $item; |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | static function explodes($separator, $string, $sanitize = 'safetxt') { |
| 802 | $array = explode($separator, $string); |
| 803 | foreach ( $array as &$item ) { |
| 804 | $item = __::sanitize_var($item, $sanitize); |
| 805 | } |
| 806 | return $array; |
| 807 | } |
| 808 | |
| 809 | static function deepimplode($glue, $array) { |
| 810 | $result = []; |
| 811 | |
| 812 | foreach ($array as $subArray) { |
| 813 | if (is_array($subArray)) { |
| 814 | $result[] = implode($glue, $subArray); |
| 815 | } |
| 816 | } |
| 817 | return implode($glue, $result); |
| 818 | } |
| 819 | |
| 820 | /** |
| 821 | * Mask any part of string |
| 822 | * @param $string |
| 823 | * @param $mask |
| 824 | * @param $maskChars |
| 825 | * @param $repeatMask |
| 826 | * |
| 827 | * @return array|mixed|string|string[] |
| 828 | */ |
| 829 | static function mask( $string, $mask = '*', $maskChars = 5, $repeatMask = true ) { |
| 830 | if ( ! $string ) { |
| 831 | return $string; |
| 832 | } |
| 833 | $length = strlen( $string ); |
| 834 | if ( $maskChars < 0 ) { |
| 835 | $maskChars = $length + ( $maskChars * 2 ); |
| 836 | } |
| 837 | if ( $maskChars < 0 ) { |
| 838 | $maskChars = 0; |
| 839 | } |
| 840 | $start = floor( ( ( $length - $maskChars ) / 2 ) + 1 ); |
| 841 | $fullMask = $repeatMask ? str_repeat( $mask, $maskChars ) : $mask; |
| 842 | $string = substr_replace( $string, $fullMask, $start, $maskChars ); |
| 843 | |
| 844 | return $string; |
| 845 | } |
| 846 | |
| 847 | static function p( ...$args ) { |
| 848 | foreach ( $args as $arg ) { |
| 849 | echo "<pre>" . print_r( $arg, 1 ) . "</pre>"; |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | static function d( ...$args ) { |
| 854 | foreach ( $args as $arg ) { |
| 855 | echo "<pre>" . print_r( $arg, 1 ) . "</pre>"; |
| 856 | } |
| 857 | die(); |
| 858 | } |
| 859 | |
| 860 | static function qrCode( $url, $w = 250 ) { |
| 861 | $qrimg = md5( $url.$w ).".png"; |
| 862 | $dir = wp_upload_dir()['basedir'] . "/wpdm-assets/qrcodes/"; |
| 863 | if(file_exists($dir.$qrimg)) return wp_upload_dir()['baseurl']."/wpdm-assets/qrcodes/".$qrimg; |
| 864 | if(!file_exists($dir)){ |
| 865 | mkdir($dir, 0755, true); |
| 866 | } |
| 867 | $img = __::remote_get("https://api.qrserver.com/v1/create-qr-code/?size={$w}x{$w}&data={$url}"); |
| 868 | file_put_contents( $dir.$qrimg, $img ); |
| 869 | return wp_upload_dir()['baseurl']."/wpdm-assets/qrcodes/".$qrimg; |
| 870 | } |
| 871 | |
| 872 | } |
| 873 |