Current path: home/webdevt/www/demo2/wp-content/plugins/divi-contact-form-helper/d5/server/Helpers/
?? Go up: /home/webdevt/www/demo2/wp-content/plugins/divi-contact-form-helper/d5/server
<?php
namespace KS_PAC_DCFH\Helpers;
if (!defined('ABSPATH')) {
exit;
}
class MiscUtils
{
private static $_instance;
public static function instance(): self
{
if (self::$_instance === null) {
self::$_instance = new self();
}
return self::$_instance;
}
public static function get_post_id(): int
{
$id = get_the_ID();
wp_reset_postdata();
return (int)$id;
}
public static function get_post_types(): array
{
$posts_types = get_post_types(['public' => true]);
if (isset($posts_types['attachment'])) {
unset($posts_types['attachment']);
}
return $posts_types;
}
public static function get_formatted_datetime($datetime): string
{
if (empty($datetime)) {
return '';
}
$timestamp = strtotime($datetime);
$date_format = et_get_option('pwh_dcfh_entries_date_format', 'Y/m/d');
$date_format = empty($date_format) ? get_option('date_format') : $date_format;
$time_format = et_get_option('pwh_dcfh_entries_time_format', 'h:i:sa');
$date_output = date_i18n($date_format, $timestamp);
if (empty($time_format)) {
return sprintf('%s', $date_output);
}
$time_output = date_i18n($time_format, $timestamp);
return sprintf(__('%1$s at %2$s', 'divi-contact-form-helper'), $date_output, $time_output);
}
public static function get_image_mimes(): array
{
$mimes = wp_get_mime_types();
$image_mimes = [];
foreach ($mimes as $mime) {
if (strpos($mime, 'image/') !== false) {
$image_mimes[] = $mime;
}
}
return $image_mimes;
}
public static function get_wp_locale()
{
$locale_arr = explode('_', get_locale());
return $locale_arr[0] ?? 'en';
}
public static function get_referrer_page(): string
{
$referrer = wp_get_referer();
$page = '';
if ($referrer) {
$parsed_url = wp_parse_url($referrer);
if (!empty($parsed_url['query'])) {
$query_args = [];
wp_parse_str($parsed_url['query'], $query_args);
if (!empty($query_args['page'])) {
$page = $query_args['page'];
}
}
}
return $page;
}
public static function sanitize_string($string, $case = 'ucwords')
{
// Remove Accents
$string = remove_accents($string);
// Replace Underscore and Hyphen
$string = preg_replace('/[\-_]/', ' ', $string);
// Removes special chars with space
$string = preg_replace('/\W/', ' ', $string);
// Replacing multiple spaces with a single space
$string = preg_replace('!\s+!', ' ', $string);
// Trim Spaces
$string = trim($string);
if (empty($case) || ' ' === $case) {
$case = 'strtolower';
}
return call_user_func($case, $string);
}
public static function sanitize_key($string, $replace_with = '_'): string
{
if (empty($string)) {
return $string;
}
$string = remove_accents($string);
// Removes special chars with space
$string = preg_replace('/\W/', ' ', $string);
// Replacing multiple spaces with a single space
$string = preg_replace('!\s+!', ' ', $string);
// Trim Spaces
$string = trim($string);
// Replace space
$string = str_replace(' ', $replace_with, $string);
return strtolower($string);
}
public static function encrypt_decrypt($string = '', $encrypt_decrypt = 'e')
{
$output = null;
$secret_key = ',y(yZu+i7nG.^(pSW00rQjpHgMvpxkn-ux{H*]bL7GzOOB=-Q]6`=Kw&Rh%u+7!p';
$secret_iv = '!8y&3TdE:| 1k7;3!V#AQDelCb2/.ZTIod;m!jZ~EuI[#BFAU_xKnwQRugn+ iq}';
$key = hash('sha256', $secret_key);
$iv = substr(hash('sha256', $secret_iv), 0, 16);
if ($encrypt_decrypt === 'e') {
$output = base64_encode(openssl_encrypt($string, "AES-256-CBC", $key, 0, $iv)); // phpcs:ignore
} elseif ($encrypt_decrypt === 'd') {
$output = openssl_decrypt(base64_decode($string), "AES-256-CBC", $key, 0, $iv); // phpcs:ignore
}
return $output;
}
public static function is_robot()
{
$user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? sanitize_text_field($_SERVER['HTTP_USER_AGENT']) : '';
if (!empty($user_agent)) {
return preg_match('/abacho|accona|AddThis|AdsBot|ahoy|AhrefsBot|AISearchBot|alexa|altavista|anthill|appie|applebot|arale|araneo|AraybOt|ariadne|arks|aspseek|ATN_Worldwide|Atomz|baiduspider|baidu|bbot|bingbot|bing|Bjaaland|BlackWidow|BotLink|bot|boxseabot|bspider|calif|CCBot|ChinaClaw|christcrawler|CMC\/0\.01|combine|confuzzledbot|contaxe|CoolBot|cosmos|crawler|crawlpaper|crawl|curl|cusco|cyberspyder|cydralspider|dataprovider|digger|DIIbot|DotBot|downloadexpress|DragonBot|DuckDuckBot|dwcp|EasouSpider|ebiness|ecollector|elfinbot|esculapio|ESI|esther|eStyle|Ezooms|facebookexternalhit|facebook|facebot|fastcrawler|FatBot|FDSE|FELIX IDE|fetch|fido|find|Firefly|fouineur|Freecrawl|froogle|gammaSpider|gazz|gcreep|geona|Getterrobo-Plus|get|girafabot|golem|googlebot|-google|grabber|GrabNet|griffon|Gromit|gulliver|gulper|hambot|havIndex|hotwired|htdig|HTTrack|ia_archiver|iajabot|IDBot|Informant|InfoSeek|InfoSpiders|INGRID\/0\.1|inktomi|inspectorwww|Internet Cruiser Robot|irobot|Iron33|JBot|jcrawler|Jeeves|jobo|KDD-Explorer|KIT-Fireball|ko_yappo_robot|label-grabber|larbin|legs|libwww-perl|linkedin|Linkidator|linkwalker|Lockon|logo_gif_crawler|Lycos|m2e|majesticsEO|marvin|mattie|mediafox|mediapartners|MerzScope|MindCrawler|MJ12bot|mod_pagespeed|moget|Motor|msnbot|muncher|muninn|MuscatFerret|MwdSearch|NationalDirectory|naverbot|NEC-MeshExplorer|NetcraftSurveyAgent|NetScoop|NetSeer|newscan-online|nil|none|Nutch|ObjectsSearch|Occam|openstat.ru\/Bot|packrat|pageboy|ParaSite|patric|pegasus|perlcrawler|phpdig|piltdownman|Pimptrain|pingdom|pinterest|pjspider|PlumtreeWebAccessor|PortalBSpider|psbot|rambler|Raven|RHCS|RixBot|roadrunner|Robbie|robi|RoboCrawl|robofox|Scooter|Scrubby|Search-AU|searchprocess|search|SemrushBot|Senrigan|seznambot|Shagseeker|sharp-info-agent|sift|SimBot|Site Valet|SiteSucker|skymob|SLCrawler\/2\.0|slurp|snooper|solbot|speedy|spider_monkey|SpiderBot\/1\.0|spiderline|spider|suke|tach_bw|TechBOT|TechnoratiSnoop|templeton|teoma|titin|topiclink|twitterbot|twitter|UdmSearch|Ukonline|UnwindFetchor|URL_Spider_SQL|urlck|urlresolver|Valkyrie libwww-perl|verticrawl|Victoria|void-bot|Voyager|VWbot_K|wapspider|WebBandit\/1\.0|webcatcher|WebCopier|WebFindBot|WebLeacher|WebMechanic|WebMoose|webquest|webreaper|webspider|webs|WebWalker|WebZip|wget|whowhere|winona|wlm|WOLP|woriobot|WWWC|XGET|xing|yahoo|YandexBot|YandexMobileBot|yandex|yeti|Zeus/i', $user_agent);
}
return false;
}
public static function get_ip_address(): string
{
$headers = [
'HTTP_CLIENT_IP',
'HTTP_X_FORWARDED_FOR',
'HTTP_X_FORWARDED',
'HTTP_FORWARDED_FOR',
'HTTP_FORWARDED',
'REMOTE_ADDR'
];
foreach ($headers as $header) {
if (!empty($_SERVER[$header])) {
return sanitize_text_field($_SERVER[$header]);
}
}
return '';
}
public static function get_user_agent($agent = '')
{
$user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? sanitize_text_field($_SERVER['HTTP_USER_AGENT']) : '';
if (empty($user_agent)) {
return null;
}
// Detect platform
$platforms = [
'/linux/i' => 'Linux',
'/macintosh|mac os x/i' => 'Mac',
'/windows|win32/i' => 'Windows'
];
$platform = 'Unknown';
foreach ($platforms as $regex => $value) {
if (preg_match($regex, $user_agent)) {
$platform = $value;
break;
}
}
// Detect browser
$browsers = [
'MSIE' => 'Internet Explorer',
'Firefox' => 'Mozilla Firefox',
'Chrome' => 'Google Chrome',
'Safari' => 'Apple Safari',
'Opera' => 'Opera',
'Netscape' => 'Netscape',
'Edge' => 'Microsoft Edge'
];
$browser = 'Unknown';
$browser_key = '';
foreach ($browsers as $key => $value) {
if (stripos($user_agent, $key) !== false) {
$browser = $value;
$browser_key = $key;
break;
}
}
// Get version
$version = 'Unknown';
if ($browser_key && preg_match('#('.$browser_key.'|Version)[/ ]+([0-9.]+)#i', $user_agent, $matches)) {
$version = $matches[2] ?? 'Unknown';
}
$data = [
'agent' => sanitize_text_field($user_agent),
'browser' => esc_html($browser),
'version' => esc_html($version),
'platform' => esc_html($platform),
];
return $agent ? ($data[$agent] ?? null) : $data;
}
public static function maybe_unserialize($string)
{
$string = wp_specialchars_decode($string);
$unserialize_data = maybe_unserialize($string);
if (false === $unserialize_data) {
$string = preg_replace_callback('/s:(\d+):\"(.*?)\";/', static function ($matches) use ($string) {
if (isset($matches[2])) {
return "s:".strlen($matches[2]).':"'.$matches[2].'";';
}
return $string;
}, $string);
$unserialize_data = maybe_unserialize($string);
}
return $unserialize_data;
}
}