admin.php
4 months ago
ajax.php
4 months ago
functions.php
1 month ago
setup.php
3 months ago
stats.php
4 months ago
utility.php
4 months ago
utility.php
595 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * WP Captcha |
| 5 | * https://getwpcaptcha.com/ |
| 6 | * (c) WebFactory Ltd, 2022 - 2026, www.webfactoryltd.com |
| 7 | */ |
| 8 | |
| 9 | use WFMaxMind\Db\Reader; |
| 10 | |
| 11 | class WPCaptcha_Utility extends WPCaptcha |
| 12 | { |
| 13 | /** |
| 14 | * Display settings notice |
| 15 | * |
| 16 | * @param $redirect |
| 17 | * @return bool |
| 18 | */ |
| 19 | static function display_notice($message, $type = 'error', $code = 'advanced-google-recaptcha') |
| 20 | { |
| 21 | global $wp_settings_errors; |
| 22 | |
| 23 | $wp_settings_errors[] = array( |
| 24 | 'setting' => WPCAPTCHA_OPTIONS_KEY, |
| 25 | 'code' => $code, |
| 26 | 'message' => $message, |
| 27 | 'type' => $type |
| 28 | ); |
| 29 | set_transient('settings_errors', $wp_settings_errors); |
| 30 | } // display_notice |
| 31 | |
| 32 | /** |
| 33 | * Empty cache in various 3rd party plugins |
| 34 | * |
| 35 | * @since 5.0 |
| 36 | * |
| 37 | * @return null |
| 38 | * |
| 39 | */ |
| 40 | static function clear_3rdparty_cache() |
| 41 | { |
| 42 | if (function_exists('w3tc_pgcache_flush')) { |
| 43 | w3tc_pgcache_flush(); |
| 44 | } |
| 45 | if (function_exists('wp_cache_clean_cache')) { |
| 46 | global $file_prefix; |
| 47 | wp_cache_clean_cache($file_prefix); |
| 48 | } |
| 49 | if (function_exists('wp_cache_clear_cache')) { |
| 50 | wp_cache_clear_cache(); |
| 51 | } |
| 52 | if (class_exists('Endurance_Page_Cache')) { |
| 53 | $epc = new Endurance_Page_Cache; |
| 54 | $epc->purge_all(); |
| 55 | } |
| 56 | if (method_exists('SG_CachePress_Supercacher', 'purge_cache')) { |
| 57 | SG_CachePress_Supercacher::purge_cache(true); |
| 58 | } |
| 59 | |
| 60 | if (class_exists('SiteGround_Optimizer\Supercacher\Supercacher')) { |
| 61 | SiteGround_Optimizer\Supercacher\Supercacher::purge_cache(); |
| 62 | } |
| 63 | } // empty_3rdparty_cache |
| 64 | |
| 65 | |
| 66 | /** |
| 67 | * Dismiss pointer |
| 68 | * |
| 69 | * @since 5.0 |
| 70 | * |
| 71 | * @return null |
| 72 | * |
| 73 | */ |
| 74 | static function dismiss_pointer_ajax() |
| 75 | { |
| 76 | delete_option(WPCAPTCHA_POINTERS_KEY); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * checkbox helper function |
| 81 | * |
| 82 | * @since 5.0 |
| 83 | * |
| 84 | * @return string checked HTML |
| 85 | * |
| 86 | */ |
| 87 | static function checked($value, $current, $echo = false) |
| 88 | { |
| 89 | $out = ''; |
| 90 | |
| 91 | if (!is_array($current)) { |
| 92 | $current = (array) $current; |
| 93 | } |
| 94 | |
| 95 | if (in_array($value, $current)) { |
| 96 | $out = ' checked="checked" '; |
| 97 | } |
| 98 | |
| 99 | if ($echo) { |
| 100 | WPCaptcha_Utility::wp_kses_wf($out); |
| 101 | } else { |
| 102 | return $out; |
| 103 | } |
| 104 | } // checked |
| 105 | |
| 106 | /** |
| 107 | * Create toggle switch |
| 108 | * |
| 109 | * @since 5.0 |
| 110 | * |
| 111 | * @return string Switch HTML |
| 112 | * |
| 113 | */ |
| 114 | static function create_toggle_switch($name, $options = array(), $output = true, $class = '') |
| 115 | { |
| 116 | $default_options = array('value' => '1', 'saved_value' => '', 'option_key' => $name); |
| 117 | $options = array_merge($default_options, $options); |
| 118 | |
| 119 | $out = "\n"; |
| 120 | $out .= '<div class="toggle-wrapper">'; |
| 121 | $out .= '<input class="' . $class . '" type="checkbox" id="' . $name . '" ' . self::checked($options['value'], $options['saved_value']) . ' type="checkbox" value="' . $options['value'] . '" name="' . $options['option_key'] . '">'; |
| 122 | $out .= '<label for="' . $name . '" class="toggle"><span class="toggle_handler"></span></label>'; |
| 123 | $out .= '</div>'; |
| 124 | |
| 125 | if ($output) { |
| 126 | WPCaptcha_Utility::wp_kses_wf($out); |
| 127 | } else { |
| 128 | return $out; |
| 129 | } |
| 130 | } // create_toggle_switch |
| 131 | |
| 132 | /** |
| 133 | * Get user IP |
| 134 | * |
| 135 | * @since 5.0 |
| 136 | * |
| 137 | * @return string userip |
| 138 | * |
| 139 | */ |
| 140 | static function getUserIP($force_clear = false) |
| 141 | { |
| 142 | $options = WPCaptcha_Setup::get_options(); |
| 143 | $ip = ''; |
| 144 | |
| 145 | if (!empty($_SERVER['REMOTE_ADDR'])) { |
| 146 | $ip = sanitize_url(wp_unslash($_SERVER['REMOTE_ADDR'])); |
| 147 | } |
| 148 | |
| 149 | if ($options['anonymous_logging'] == '1' && !$force_clear) { |
| 150 | $ip = md5($ip); |
| 151 | } |
| 152 | |
| 153 | return $ip; |
| 154 | } // getUserIP |
| 155 | |
| 156 | /** |
| 157 | * Create select options for select |
| 158 | * |
| 159 | * @since 5.0 |
| 160 | * |
| 161 | * @param array $options options |
| 162 | * @param string $selected selected value |
| 163 | * @param bool $output echo, if false return html as string |
| 164 | * @return string html with options |
| 165 | */ |
| 166 | static function create_select_options($options, $selected = null, $output = true) |
| 167 | { |
| 168 | $out = "\n"; |
| 169 | |
| 170 | foreach ($options as $tmp) { |
| 171 | if ((is_array($selected) && in_array($tmp['val'], $selected)) || $selected == $tmp['val']) { |
| 172 | $out .= "<option selected=\"selected\" value=\"{$tmp['val']}\" " . (isset($tmp['class']) ? "class=\"{$tmp['class']}\"" : "") . ">{$tmp['label']} </option>\n"; |
| 173 | } else { |
| 174 | $out .= "<option value=\"{$tmp['val']}\" " . (isset($tmp['class']) ? "class=\"{$tmp['class']}\"" : "") . ">{$tmp['label']} </option>\n"; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | if ($output) { |
| 179 | WPCaptcha_Utility::wp_kses_wf($out); |
| 180 | } else { |
| 181 | return $out; |
| 182 | } |
| 183 | } // create_select_options |
| 184 | |
| 185 | |
| 186 | static function create_radio_group($name, $options, $selected = null, $output = true) |
| 187 | { |
| 188 | $out = "\n"; |
| 189 | |
| 190 | foreach ($options as $tmp) { |
| 191 | if ($selected == $tmp['val']) { |
| 192 | $out .= "<label for=\"{$name}_{$tmp['val']}\" class=\"radio_wrapper\"><input id=\"{$name}_{$tmp['val']}\" name=\"{$name}\" type=\"radio\" checked=\"checked\" value=\"{$tmp['val']}\">{$tmp['label']} </option></label>\n"; |
| 193 | } else { |
| 194 | $out .= "<label for=\"{$name}_{$tmp['val']}\" class=\"radio_wrapper\"><input id=\"{$name}_{$tmp['val']}\" name=\"{$name}\" type=\"radio\" value=\"{$tmp['val']}\">{$tmp['label']} </option></label>\n"; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | if ($output) { |
| 199 | WPCaptcha_Utility::wp_kses_wf($out); |
| 200 | } else { |
| 201 | return $out; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Parse user agent to add device icon and clean text |
| 207 | * |
| 208 | * @since 5.0 |
| 209 | * |
| 210 | * @param string $user_agent |
| 211 | * @return string $user_agent |
| 212 | */ |
| 213 | static function parse_user_agent($user_agent = false) |
| 214 | { |
| 215 | if (!$user_agent) { |
| 216 | $user_agent = array(); |
| 217 | foreach ($_SERVER as $name => $value) { |
| 218 | if (substr($name, 0, 5) == 'HTTP_') { |
| 219 | $user_agent[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | $user_agent = new WhichBrowser\Parser($user_agent); |
| 225 | |
| 226 | $user_agent_string = ''; |
| 227 | if ($user_agent->isType('mobile')) { |
| 228 | $user_agent_string .= '<i class="tooltip fas fa-mobile-alt" title="Phone"></i>'; |
| 229 | } else if ($user_agent->isType('tablet')) { |
| 230 | $user_agent_string .= '<i class="tooltip fas fa-tablet-alt" title="Table"></i>'; |
| 231 | } else if ($user_agent->isType('desktop')) { |
| 232 | $user_agent_string .= '<i class="tooltip fas fa-desktop" title="Desktop"></i>'; |
| 233 | } else { |
| 234 | $user_agent_string .= '<i class="tooltip fas fa-robot" title="Bot"></i>'; |
| 235 | } |
| 236 | |
| 237 | if (isset($user_agent->browser) && isset($user_agent->browser->version)) { |
| 238 | $browser_version = explode('.', $user_agent->browser->version->toString()); |
| 239 | } else { |
| 240 | $browser_version = array('unknown'); |
| 241 | } |
| 242 | |
| 243 | if ($user_agent->os) { |
| 244 | $os = $user_agent->os->toString(); |
| 245 | } else { |
| 246 | $os = 'unknown'; |
| 247 | } |
| 248 | |
| 249 | if (isset($user_agent->browser) && isset($user_agent->browser->name)) { |
| 250 | $browser_name = $user_agent->browser->name; |
| 251 | } else { |
| 252 | $browser_name = 'unknown'; |
| 253 | } |
| 254 | |
| 255 | $user_agent_string .= ' ' . $browser_name . ' ' . $browser_version[0] . ' on ' . $os; |
| 256 | |
| 257 | |
| 258 | return $user_agent_string; |
| 259 | } // parse_user_agent |
| 260 | |
| 261 | static function get_home_path() |
| 262 | { |
| 263 | |
| 264 | if (!function_exists('get_home_path')) { |
| 265 | |
| 266 | require_once(ABSPATH . 'wp-admin/includes/file.php'); |
| 267 | } |
| 268 | |
| 269 | return get_home_path(); |
| 270 | } |
| 271 | |
| 272 | static function wp_kses_wf($html) |
| 273 | { |
| 274 | add_filter('safe_style_css', function ($styles) { |
| 275 | $styles_wf = array( |
| 276 | 'text-align', |
| 277 | 'margin', |
| 278 | 'color', |
| 279 | 'float', |
| 280 | 'border', |
| 281 | 'background', |
| 282 | 'background-color', |
| 283 | 'border-bottom', |
| 284 | 'border-bottom-color', |
| 285 | 'border-bottom-style', |
| 286 | 'border-bottom-width', |
| 287 | 'border-collapse', |
| 288 | 'border-color', |
| 289 | 'border-left', |
| 290 | 'border-left-color', |
| 291 | 'border-left-style', |
| 292 | 'border-left-width', |
| 293 | 'border-right', |
| 294 | 'border-right-color', |
| 295 | 'border-right-style', |
| 296 | 'border-right-width', |
| 297 | 'border-spacing', |
| 298 | 'border-style', |
| 299 | 'border-top', |
| 300 | 'border-top-color', |
| 301 | 'border-top-style', |
| 302 | 'border-top-width', |
| 303 | 'border-width', |
| 304 | 'caption-side', |
| 305 | 'clear', |
| 306 | 'cursor', |
| 307 | 'direction', |
| 308 | 'font', |
| 309 | 'font-family', |
| 310 | 'font-size', |
| 311 | 'font-style', |
| 312 | 'font-variant', |
| 313 | 'font-weight', |
| 314 | 'height', |
| 315 | 'letter-spacing', |
| 316 | 'line-height', |
| 317 | 'margin-bottom', |
| 318 | 'margin-left', |
| 319 | 'margin-right', |
| 320 | 'margin-top', |
| 321 | 'overflow', |
| 322 | 'padding', |
| 323 | 'padding-bottom', |
| 324 | 'padding-left', |
| 325 | 'padding-right', |
| 326 | 'padding-top', |
| 327 | 'text-decoration', |
| 328 | 'text-indent', |
| 329 | 'vertical-align', |
| 330 | 'width', |
| 331 | 'display', |
| 332 | ); |
| 333 | |
| 334 | foreach ($styles_wf as $style_wf) { |
| 335 | $styles[] = $style_wf; |
| 336 | } |
| 337 | return $styles; |
| 338 | }); |
| 339 | |
| 340 | $allowed_tags = wp_kses_allowed_html('post'); |
| 341 | $allowed_tags['input'] = array( |
| 342 | 'type' => true, |
| 343 | 'style' => true, |
| 344 | 'class' => true, |
| 345 | 'id' => true, |
| 346 | 'checked' => true, |
| 347 | 'disabled' => true, |
| 348 | 'name' => true, |
| 349 | 'size' => true, |
| 350 | 'placeholder' => true, |
| 351 | 'value' => true, |
| 352 | 'data-*' => true, |
| 353 | 'size' => true, |
| 354 | 'disabled' => true |
| 355 | ); |
| 356 | |
| 357 | $allowed_tags['textarea'] = array( |
| 358 | 'type' => true, |
| 359 | 'style' => true, |
| 360 | 'class' => true, |
| 361 | 'id' => true, |
| 362 | 'checked' => true, |
| 363 | 'disabled' => true, |
| 364 | 'name' => true, |
| 365 | 'size' => true, |
| 366 | 'placeholder' => true, |
| 367 | 'value' => true, |
| 368 | 'data-*' => true, |
| 369 | 'cols' => true, |
| 370 | 'rows' => true, |
| 371 | 'disabled' => true, |
| 372 | 'autocomplete' => true |
| 373 | ); |
| 374 | |
| 375 | $allowed_tags['select'] = array( |
| 376 | 'type' => true, |
| 377 | 'style' => true, |
| 378 | 'class' => true, |
| 379 | 'id' => true, |
| 380 | 'checked' => true, |
| 381 | 'disabled' => true, |
| 382 | 'name' => true, |
| 383 | 'size' => true, |
| 384 | 'placeholder' => true, |
| 385 | 'value' => true, |
| 386 | 'data-*' => true, |
| 387 | 'multiple' => true, |
| 388 | 'disabled' => true |
| 389 | ); |
| 390 | |
| 391 | $allowed_tags['option'] = array( |
| 392 | 'type' => true, |
| 393 | 'style' => true, |
| 394 | 'class' => true, |
| 395 | 'id' => true, |
| 396 | 'checked' => true, |
| 397 | 'disabled' => true, |
| 398 | 'name' => true, |
| 399 | 'size' => true, |
| 400 | 'placeholder' => true, |
| 401 | 'value' => true, |
| 402 | 'selected' => true, |
| 403 | 'data-*' => true |
| 404 | ); |
| 405 | $allowed_tags['optgroup'] = array( |
| 406 | 'type' => true, |
| 407 | 'style' => true, |
| 408 | 'class' => true, |
| 409 | 'id' => true, |
| 410 | 'checked' => true, |
| 411 | 'disabled' => true, |
| 412 | 'name' => true, |
| 413 | 'size' => true, |
| 414 | 'placeholder' => true, |
| 415 | 'value' => true, |
| 416 | 'selected' => true, |
| 417 | 'data-*' => true, |
| 418 | 'label' => true |
| 419 | ); |
| 420 | |
| 421 | $allowed_tags['a'] = array( |
| 422 | 'href' => true, |
| 423 | 'data-*' => true, |
| 424 | 'class' => true, |
| 425 | 'style' => true, |
| 426 | 'id' => true, |
| 427 | 'target' => true, |
| 428 | 'data-*' => true, |
| 429 | 'role' => true, |
| 430 | 'aria-controls' => true, |
| 431 | 'aria-selected' => true, |
| 432 | 'disabled' => true |
| 433 | ); |
| 434 | |
| 435 | $allowed_tags['div'] = array( |
| 436 | 'style' => true, |
| 437 | 'class' => true, |
| 438 | 'id' => true, |
| 439 | 'data-*' => true, |
| 440 | 'role' => true, |
| 441 | 'aria-labelledby' => true, |
| 442 | 'value' => true, |
| 443 | 'aria-modal' => true, |
| 444 | 'tabindex' => true |
| 445 | ); |
| 446 | |
| 447 | $allowed_tags['li'] = array( |
| 448 | 'style' => true, |
| 449 | 'class' => true, |
| 450 | 'id' => true, |
| 451 | 'data-*' => true, |
| 452 | 'role' => true, |
| 453 | 'aria-labelledby' => true, |
| 454 | 'value' => true, |
| 455 | 'aria-modal' => true, |
| 456 | 'tabindex' => true |
| 457 | ); |
| 458 | |
| 459 | $allowed_tags['span'] = array( |
| 460 | 'style' => true, |
| 461 | 'class' => true, |
| 462 | 'id' => true, |
| 463 | 'data-*' => true, |
| 464 | 'aria-hidden' => true |
| 465 | ); |
| 466 | |
| 467 | $allowed_tags['style'] = array( |
| 468 | 'class' => true, |
| 469 | 'id' => true, |
| 470 | 'type' => true, |
| 471 | 'style' => true |
| 472 | ); |
| 473 | |
| 474 | $allowed_tags['fieldset'] = array( |
| 475 | 'class' => true, |
| 476 | 'id' => true, |
| 477 | 'type' => true, |
| 478 | 'style' => true |
| 479 | ); |
| 480 | |
| 481 | $allowed_tags['link'] = array( |
| 482 | 'class' => true, |
| 483 | 'id' => true, |
| 484 | 'type' => true, |
| 485 | 'rel' => true, |
| 486 | 'href' => true, |
| 487 | 'media' => true, |
| 488 | 'style' => true |
| 489 | ); |
| 490 | |
| 491 | $allowed_tags['form'] = array( |
| 492 | 'style' => true, |
| 493 | 'class' => true, |
| 494 | 'id' => true, |
| 495 | 'method' => true, |
| 496 | 'action' => true, |
| 497 | 'data-*' => true, |
| 498 | 'style' => true |
| 499 | ); |
| 500 | |
| 501 | $allowed_tags['script'] = array( |
| 502 | 'class' => true, |
| 503 | 'id' => true, |
| 504 | 'type' => true, |
| 505 | 'src' => true, |
| 506 | 'style' => true |
| 507 | ); |
| 508 | |
| 509 | $allowed_tags['table'] = array( |
| 510 | 'class' => true, |
| 511 | 'id' => true, |
| 512 | 'type' => true, |
| 513 | 'cellpadding' => true, |
| 514 | 'cellspacing' => true, |
| 515 | 'border' => true, |
| 516 | 'style' => true |
| 517 | ); |
| 518 | |
| 519 | $allowed_tags['canvas'] = array( |
| 520 | 'class' => true, |
| 521 | 'id' => true, |
| 522 | 'style' => true |
| 523 | ); |
| 524 | |
| 525 | echo wp_kses($html, $allowed_tags); |
| 526 | |
| 527 | add_filter('safe_style_css', function ($styles) { |
| 528 | $styles_wf = array( |
| 529 | 'text-align', |
| 530 | 'margin', |
| 531 | 'color', |
| 532 | 'float', |
| 533 | 'border', |
| 534 | 'background', |
| 535 | 'background-color', |
| 536 | 'border-bottom', |
| 537 | 'border-bottom-color', |
| 538 | 'border-bottom-style', |
| 539 | 'border-bottom-width', |
| 540 | 'border-collapse', |
| 541 | 'border-color', |
| 542 | 'border-left', |
| 543 | 'border-left-color', |
| 544 | 'border-left-style', |
| 545 | 'border-left-width', |
| 546 | 'border-right', |
| 547 | 'border-right-color', |
| 548 | 'border-right-style', |
| 549 | 'border-right-width', |
| 550 | 'border-spacing', |
| 551 | 'border-style', |
| 552 | 'border-top', |
| 553 | 'border-top-color', |
| 554 | 'border-top-style', |
| 555 | 'border-top-width', |
| 556 | 'border-width', |
| 557 | 'caption-side', |
| 558 | 'clear', |
| 559 | 'cursor', |
| 560 | 'direction', |
| 561 | 'font', |
| 562 | 'font-family', |
| 563 | 'font-size', |
| 564 | 'font-style', |
| 565 | 'font-variant', |
| 566 | 'font-weight', |
| 567 | 'height', |
| 568 | 'letter-spacing', |
| 569 | 'line-height', |
| 570 | 'margin-bottom', |
| 571 | 'margin-left', |
| 572 | 'margin-right', |
| 573 | 'margin-top', |
| 574 | 'overflow', |
| 575 | 'padding', |
| 576 | 'padding-bottom', |
| 577 | 'padding-left', |
| 578 | 'padding-right', |
| 579 | 'padding-top', |
| 580 | 'text-decoration', |
| 581 | 'text-indent', |
| 582 | 'vertical-align', |
| 583 | 'width' |
| 584 | ); |
| 585 | |
| 586 | foreach ($styles_wf as $style_wf) { |
| 587 | if (($key = array_search($style_wf, $styles)) !== false) { |
| 588 | unset($styles[$key]); |
| 589 | } |
| 590 | } |
| 591 | return $styles; |
| 592 | }); |
| 593 | } |
| 594 | } // class |
| 595 |