View source
<?php
namespace Cleantalk\Common;
use Cleantalk\Common\Variables\Server;
class Helper {
const AGENT = 'Cleantalk-Helper/3.4';
public static $private_networks = array(
'v4' => array(
'10.0.0.0/8',
'100.64.0.0/10',
'172.16.0.0/12',
'192.168.0.0/16',
'127.0.0.1/32',
),
'v6' => array(
'0:0:0:0:0:0:0:1/128',
'0:0:0:0:0:0:a:1/128',
),
);
public static $cleantalks_servers = array(
'moderate1.cleantalk.org' => '162.243.144.175',
'moderate2.cleantalk.org' => '159.203.121.181',
'moderate3.cleantalk.org' => '88.198.153.60',
'moderate4.cleantalk.org' => '159.69.51.30',
'moderate5.cleantalk.org' => '95.216.200.119',
'moderate6.cleantalk.org' => '138.68.234.8',
'apix1.cleantalk.org' => '35.158.52.161',
'apix2.cleantalk.org' => '18.206.49.217',
'apix3.cleantalk.org' => '3.18.23.246',
'apix4.cleantalk.org' => '44.227.90.42',
'apix5.cleantalk.org' => '15.188.198.212',
'apix6.cleantalk.org' => '54.219.94.72',
'netserv2.cleantalk.org' => '178.63.60.214',
'netserv3.cleantalk.org' => '188.40.14.173',
);
public static function ip__get($ip_type_to_get = 'real', $v4_only = true, $headers = array()) {
$out = null;
switch ($ip_type_to_get) {
case 'cloud_flare':
$headers = $headers ?: self::http__get_headers();
if (isset($headers['Cf-Connecting-Ip'], $headers['Cf-Ipcountry'], $headers['Cf-Ray'])) {
$tmp = strpos($headers['Cf-Connecting-Ip'], ',') !== false ? explode(',', $headers['Cf-Connecting-Ip']) : (array) $headers['Cf-Connecting-Ip'];
$ip_version = self::ip__validate(trim($tmp[0]));
if ($ip_version) {
$out = $ip_version === 'v6' && !$v4_only ? self::ip__v6_normalize(trim($tmp[0])) : trim($tmp[0]);
}
}
break;
case 'gtranslate':
$headers = $headers ?: self::http__get_headers();
if (isset($headers['X-Gt-Clientip'], $headers['X-Gt-Viewer-Ip'])) {
$ip_version = self::ip__validate($headers['X-Gt-Viewer-Ip']);
if ($ip_version) {
$out = $ip_version === 'v6' && !$v4_only ? self::ip__v6_normalize($headers['X-Gt-Viewer-Ip']) : $headers['X-Gt-Viewer-Ip'];
}
}
break;
case 'ezoic':
$headers = $headers ?: self::http__get_headers();
if (isset($headers['X-Middleton'], $headers['X-Middleton-Ip'])) {
$ip_version = self::ip__validate($headers['X-Middleton-Ip']);
if ($ip_version) {
$out = $ip_version === 'v6' && !$v4_only ? self::ip__v6_normalize($headers['X-Middleton-Ip']) : $headers['X-Middleton-Ip'];
}
}
break;
case 'sucury':
$headers = $headers ?: self::http__get_headers();
if (isset($headers['X-Sucuri-Clientip'])) {
$ip_version = self::ip__validate($headers['X-Sucuri-Clientip']);
if ($ip_version) {
$out = $ip_version === 'v6' && !$v4_only ? self::ip__v6_normalize($headers['X-Sucuri-Clientip']) : $headers['X-Sucuri-Clientip'];
}
}
break;
case 'x_forwarded_by':
$headers = $headers ?: self::http__get_headers();
if (isset($headers['X-Forwarded-By'], $headers['X-Client-Ip'])) {
$ip_version = self::ip__validate($headers['X-Client-Ip']);
if ($ip_version) {
$out = $ip_version === 'v6' && !$v4_only ? self::ip__v6_normalize($headers['X-Client-Ip']) : $headers['X-Client-Ip'];
}
}
break;
case 'stackpath':
$headers = $headers ?: self::http__get_headers();
if (isset($headers['X-Sp-Edge-Host'], $headers['X-Sp-Forwarded-Ip'])) {
$ip_version = self::ip__validate($headers['X-Sp-Forwarded-Ip']);
if ($ip_version) {
$out = $ip_version === 'v6' && !$v4_only ? self::ip__v6_normalize($headers['X-Sp-Forwarded-Ip']) : $headers['X-Sp-Forwarded-Ip'];
}
}
break;
case 'ico_x_forwarded_for':
$headers = $headers ?: self::http__get_headers();
if (isset($headers['Ico-X-Forwarded-For'], $headers['X-Forwarded-Host'])) {
$ip_version = self::ip__validate($headers['Ico-X-Forwarded-For']);
if ($ip_version) {
$out = $ip_version === 'v6' && !$v4_only ? self::ip__v6_normalize($headers['Ico-X-Forwarded-For']) : $headers['Ico-X-Forwarded-For'];
}
}
break;
case 'ovh':
$headers = $headers ?: self::http__get_headers();
if (isset($headers['X-Cdn-Any-Ip'], $headers['Remote-Ip'])) {
$ip_version = self::ip__validate($headers['Remote-Ip']);
if ($ip_version) {
$out = $ip_version === 'v6' && !$v4_only ? self::ip__v6_normalize($headers['Remote-Ip']) : $headers['Remote-Ip'];
}
}
break;
case 'incapsula':
$headers = $headers ?: self::http__get_headers();
if (isset($headers['Incap-Client-Ip'], $headers['X-Forwarded-For'])) {
$ip_version = self::ip__validate($headers['Incap-Client-Ip']);
if ($ip_version) {
$out = $ip_version === 'v6' && !$v4_only ? self::ip__v6_normalize($headers['Incap-Client-Ip']) : $headers['Incap-Client-Ip'];
}
}
break;
case 'remote_addr':
$ip_version = self::ip__validate(Server::get('REMOTE_ADDR'));
if ($ip_version) {
$out = $ip_version === 'v6' && !$v4_only ? self::ip__v6_normalize(Server::get('REMOTE_ADDR')) : Server::get('REMOTE_ADDR');
}
break;
case 'x_forwarded_for':
$headers = $headers ?: self::http__get_headers();
if (isset($headers['X-Forwarded-For'])) {
$tmp = explode(',', trim($headers['X-Forwarded-For']));
$tmp = trim($tmp[0]);
$ip_version = self::ip__validate($tmp);
if ($ip_version) {
$out = $ip_version === 'v6' && !$v4_only ? self::ip__v6_normalize($tmp) : $tmp;
}
}
break;
case 'x_real_ip':
$headers = $headers ?: self::http__get_headers();
if (isset($headers['X-Real-Ip'])) {
$tmp = explode(",", trim($headers['X-Real-Ip']));
$tmp = trim($tmp[0]);
$ip_version = self::ip__validate($tmp);
if ($ip_version) {
$out = $ip_version === 'v6' && !$v4_only ? self::ip__v6_normalize($tmp) : $tmp;
}
}
break;
case 'real':
$out = self::ip__get('cloud_flare', $v4_only, $headers);
$out = $out ?: self::ip__get('sucury', $v4_only, $headers);
$out = $out ?: self::ip__get('gtranslate', $v4_only, $headers);
$out = $out ?: self::ip__get('ezoic', $v4_only, $headers);
$out = $out ?: self::ip__get('stackpath', $v4_only, $headers);
$out = $out ?: self::ip__get('x_forwarded_by', $v4_only, $headers);
$out = $out ?: self::ip__get('ico_x_forwarded_for', $v4_only, $headers);
$out = $out ?: self::ip__get('ovh', $v4_only, $headers);
$out = $out ?: self::ip__get('incapsula', $v4_only, $headers);
$ip_version = self::ip__validate($out);
if (!$out || $out && (self::ip__is_private_network($out, $ip_version) || self::ip__mask_match($out, Server::get('SERVER_ADDR') . '/24', $ip_version))) {
$out = $out ?: self::ip__get('x_forwarded_for', $v4_only, $headers);
$out = $out ?: self::ip__get('x_real_ip', $v4_only, $headers);
}
$out = $out ?: self::ip__get('remote_addr', $v4_only, $headers);
break;
default:
$out = self::ip__get('real', $v4_only, $headers);
}
$ip_version = self::ip__validate($out);
if (!$ip_version) {
return null;
}
elseif ($ip_version === 'v6' && $v4_only) {
return null;
}
else {
return $out;
}
}
static function ip__is_private_network($ip, $ip_type = 'v4') {
return self::ip__mask_match($ip, self::$private_networks[$ip_type], $ip_type);
}
public static function ip__mask_match($ip, $cidr, $ip_type = 'v4', $xtet_count = 0) {
if (is_array($cidr)) {
foreach ($cidr as $curr_mask) {
if (self::ip__mask_match($ip, $curr_mask, $ip_type)) {
return true;
}
}
unset($curr_mask);
return false;
}
if (!self::ip__validate($ip) || !self::cidr__validate($cidr)) {
return false;
}
$xtet_base = $ip_type == 'v4' ? 8 : 16;
$exploded = explode('/', $cidr);
$net_ip = $exploded[0];
$mask = $exploded[1];
$xtet_end = ceil($mask / $xtet_base);
if ($xtet_count == $xtet_end) {
return true;
}
$mask = $mask - $xtet_base * $xtet_count >= $xtet_base ? $xtet_base : $mask - $xtet_base * $xtet_count;
$net_ip_xtets = explode($ip_type == 'v4' ? '.' : ':', $net_ip);
$ip_xtets = explode($ip_type == 'v4' ? '.' : ':', $ip);
$net_xtet = str_pad(decbin($ip_type == 'v4' ? $net_ip_xtets[$xtet_count] : @hexdec($net_ip_xtets[$xtet_count])), $xtet_base, 0, STR_PAD_LEFT);
$ip_xtet = str_pad(decbin($ip_type == 'v4' ? $ip_xtets[$xtet_count] : @hexdec($ip_xtets[$xtet_count])), $xtet_base, 0, STR_PAD_LEFT);
for ($i = 0, $result = true; $mask != 0; $mask--, $i++) {
if ($ip_xtet[$i] != $net_xtet[$i]) {
$result = false;
break;
}
}
if ($result) {
$result = self::ip__mask_match($ip, $cidr, $ip_type, $xtet_count + 1);
}
return $result;
}
static function ip__mask__long_to_number($long_mask) {
$num_mask = strpos((string) decbin($long_mask), '0');
return $num_mask === false ? 32 : $num_mask;
}
public static function ip__validate($ip) {
if (!$ip) {
return false;
}
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) && $ip != '0.0.0.0') {
return 'v4';
}
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) && self::ip__v6_reduce($ip) != '0::0') {
return 'v6';
}
return false;
}
public static function cidr__validate($cidr) {
$cidr = explode('/', $cidr);
return isset($cidr[0], $cidr[1]) && self::ip__validate($cidr[0]) && preg_match('@\\d{1,2}@', $cidr[1]);
}
public static function ip__v6_normalize($ip) {
$ip = trim($ip);
if (preg_match('/^::ffff:([0-9]{1,3}\\.?){4}$/', $ip)) {
$ip = dechex(sprintf("%u", ip2long(substr($ip, 7))));
$ip = '0:0:0:0:0:0:' . (strlen($ip) > 4 ? substr('abcde', 0, -4) : '0') . ':' . substr($ip, -4, 4);
}
elseif (strpos($ip, '::') !== false) {
$ip = str_replace('::', str_repeat(':0', 8 - substr_count($ip, ':')) . ':', $ip);
$ip = strpos($ip, ':') === 0 ? '0' . $ip : $ip;
$ip = strpos(strrev($ip), ':') === 0 ? $ip . '0' : $ip;
}
if (preg_match('/:0(?=[a-z0-9]+)/', $ip)) {
$ip = preg_replace('/:0(?=[a-z0-9]+)/', ':', strtolower($ip));
$ip = self::ip__v6_normalize($ip);
}
return $ip;
}
public static function ip__v6_reduce($ip) {
if (strpos($ip, ':') !== false) {
$ip = preg_replace('/:0{1,4}/', ':', $ip);
$ip = preg_replace('/:{2,}/', '::', $ip);
$ip = strpos($ip, '0') === 0 ? substr($ip, 1) : $ip;
}
return $ip;
}
public static function ip__is_cleantalks($ip) {
if (self::ip__validate($ip)) {
$url = array_search($ip, self::$cleantalks_servers);
return $url ? true : false;
}
else {
return false;
}
}
public static function ip__resolve__cleantalks($ip) {
if (self::ip__validate($ip)) {
$url = array_search($ip, self::$cleantalks_servers);
return $url ? $url : self::ip__resolve($ip);
}
else {
return $ip;
}
}
public static function ip__resolve($ip) {
if (self::ip__validate($ip)) {
$url = gethostbyaddr($ip);
if ($url) {
return $url;
}
}
return $ip;
}
public static function dns__resolve($host, $out = false) {
if (function_exists('dns_get_record')) {
$records = dns_get_record($host, DNS_A);
if ($records !== false) {
$out = $records[0]['ip'];
}
}
if (!$out && function_exists('gethostbynamel')) {
$records = gethostbynamel($host);
if ($records !== false) {
$out = $records[0];
}
}
return $out;
}
public static function get_fields_any($arr, $fields_exclusions = '', $message = array(), $email = null, $nickname = array(
'nick' => '',
'first' => '',
'last' => '',
), $subject = null, $contact = true, $prev_name = '') {
$skip_params = array(
'ipn_track_id',
'txn_type',
'payment_status',
'ccbill_ipn',
'ct_checkjs',
'api_mode',
'loadLastCommentId',
);
$obfuscate_params = array(
'password',
'pass',
'pwd',
'pswd',
);
$skip_fields_with_strings = array(
'ct_checkjs',
'nonce',
'security',
'http_referer',
'timestamp',
'captcha',
'form_key',
'submit_entry',
'form_id',
'ccf_form',
'form_page',
'iphorm_uid',
'form_url',
'post_id',
'iphorm_ajax',
'iphorm_id',
'fs_postonce_1',
'fscf_submitted',
'mailto_id',
'si_contact_action',
'formData_id',
'formData_settings',
'formData_fields_\\d+_id',
'formData_fields_\\d+_files.*',
'recipient_signature',
'output_\\d+_\\w{0,2}',
'_formId',
'_returnLink',
'_save',
'_facebook',
'_social',
'user_login-',
'_wpcf7',
'avatar__file_image_data',
'task',
'page_url',
'page_title',
'Submit',
'formId',
'key',
'id',
'hiddenlists',
'ctrl',
'task',
'option',
'nextstep',
'acy_source',
'subid',
'ct_action',
'ct_method',
);
$skip_message_post = array(
'edd_action',
);
foreach ($skip_params as $value) {
if (@array_key_exists($value, $_GET) || @array_key_exists($value, $_POST)) {
$contact = false;
}
}
unset($value);
if (count($arr)) {
foreach ($arr as $key => $value) {
if (gettype($value) == 'string') {
$decoded_json_value = json_decode($value, true);
if ($decoded_json_value !== null) {
$value = $decoded_json_value;
}
}
if (!is_array($value) && !is_object($value)) {
if (is_string($fields_exclusions) && !empty($fields_exclusions)) {
$fields_exclusions = explode(",", $fields_exclusions);
if (is_array($fields_exclusions) && !empty($fields_exclusions)) {
foreach ($fields_exclusions as &$fields_exclusion) {
if (preg_match('/\\[*\\]/', $fields_exclusion)) {
$fields_exclusion = str_replace(array(
'[',
']',
), array(
'_',
'',
), $fields_exclusion);
}
}
$skip_fields_with_strings = array_merge($skip_fields_with_strings, $fields_exclusions);
}
}
if (in_array($key, $skip_params, true) && $key != 0 && $key != '' || preg_match("/^ct_checkjs/", $key)) {
$contact = false;
}
if ($value === '') {
continue;
}
foreach ($skip_fields_with_strings as $needle) {
if (preg_match("/" . $needle . "/", $prev_name . $key) == 1) {
continue 2;
}
}
unset($needle);
foreach ($obfuscate_params as $needle) {
if (strpos($key, $needle) !== false) {
$value = self::obfuscate_param($value);
continue 2;
}
}
unset($needle);
$value = urldecode(trim($value));
$value_for_email = trim($value);
if (!$email && preg_match("/^\\S+@\\S+\\.\\S+\$/", $value_for_email)) {
$email = $value_for_email;
}
elseif (preg_match("/name/i", $key)) {
preg_match("/((name.?)?(your|first|for)(.?name)?)\$/", $key, $match_forename);
preg_match("/((name.?)?(last|family|second|sur)(.?name)?)\$/", $key, $match_surname);
preg_match("/^(name.?)?(nick|user)(.?name)?\$/", $key, $match_nickname);
if (count($match_forename) > 1) {
$nickname['first'] = $value;
}
elseif (count($match_surname) > 1) {
$nickname['last'] = $value;
}
elseif (count($match_nickname) > 1) {
$nickname['nick'] = $value;
}
else {
$nickname[$prev_name . $key] = $value;
}
}
elseif ($subject === null && preg_match("/subject/i", $key)) {
$subject = $value;
}
else {
$message[$prev_name . $key] = $value;
}
}
elseif (!is_object($value)) {
$prev_name_original = $prev_name;
$prev_name = $prev_name === '' ? $key . '_' : $prev_name . $key . '_';
$temp = self::get_fields_any($value, $fields_exclusions, $message, $email, $nickname, $subject, $contact, $prev_name);
$message = $temp['message'];
$email = $temp['email'] ? $temp['email'] : null;
$nickname = $temp['nickname'] ? $temp['nickname'] : null;
$subject = $temp['subject'] ? $temp['subject'] : null;
if ($contact === true) {
$contact = $temp['contact'] === false ? false : true;
}
$prev_name = $prev_name_original;
}
}
unset($key, $value);
}
foreach ($skip_message_post as $v) {
if (isset($_POST[$v])) {
$message = null;
break;
}
}
unset($v);
if ($prev_name === '') {
if (!empty($nickname)) {
$nickname_str = '';
foreach ($nickname as $value) {
$nickname_str .= $value ? $value . " " : "";
}
unset($value);
}
$nickname = $nickname_str;
}
$return_param = array(
'email' => $email,
'nickname' => $nickname,
'subject' => $subject,
'contact' => $contact,
'message' => $message,
);
return $return_param;
}
public static function obfuscate_param($value = null) {
if ($value && (!is_object($value) || !is_array($value))) {
$length = strlen($value);
$value = str_repeat('*', $length);
}
return $value;
}
public static function print_form($arr, $k) {
foreach ($arr as $key => $value) {
if (!is_array($value)) {
if ($k == '') {
print '<textarea name="' . $key . '" style="display:none;">' . htmlspecialchars($value) . '</textarea>';
}
else {
print '<textarea name="' . $k . '[' . $key . ']" style="display:none;">' . htmlspecialchars($value) . '</textarea>';
}
}
}
}
public static function key_is_correct($api_key = '') {
return preg_match('/^[a-z\\d]{3,15}$|^$/', $api_key);
}
public static function http__request($url, $data = array(), $presets = null, $opts = array()) {
if (function_exists('curl_init')) {
$ch = curl_init();
if (!empty($data)) {
$data = is_string($data) || is_int($data) ? array(
$data => 1,
) : $data;
$opts[CURLOPT_POSTFIELDS] = $data;
}
$opts = self::array_merge__save_numeric_keys(array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT_MS => 6000,
CURLOPT_FORBID_REUSE => true,
CURLOPT_USERAGENT => self::AGENT . '; ' . (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'UNKNOWN_HOST'),
CURLOPT_POST => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_HTTPHEADER => array(
'Expect:',
),
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 5,
), $opts);
$presets = is_array($presets) ? $presets : explode(' ', $presets);
foreach ($presets as $preset) {
switch ($preset) {
case 'dont_follow_redirects':
$opts[CURLOPT_FOLLOWLOCATION] = false;
$opts[CURLOPT_MAXREDIRS] = 0;
break;
case 'get_code':
$opts[CURLOPT_HEADER] = true;
$opts[CURLOPT_NOBODY] = true;
break;
case 'async':
$opts[CURLOPT_CONNECTTIMEOUT_MS] = 1000;
$opts[CURLOPT_TIMEOUT_MS] = 1000;
break;
case 'get':
$opts[CURLOPT_URL] .= $data ? '?' . str_replace("&", "&", http_build_query($data)) : '';
$opts[CURLOPT_CUSTOMREQUEST] = 'GET';
$opts[CURLOPT_POST] = false;
$opts[CURLOPT_POSTFIELDS] = null;
break;
case 'ssl':
$opts[CURLOPT_SSL_VERIFYPEER] = true;
$opts[CURLOPT_SSL_VERIFYHOST] = 2;
if (defined('CLEANTALK_CASERT_PATH') && CLEANTALK_CASERT_PATH) {
$opts[CURLOPT_CAINFO] = CLEANTALK_CASERT_PATH;
}
break;
default:
break;
}
}
unset($preset);
curl_setopt_array($ch, $opts);
$result = curl_exec($ch);
if (in_array('async', $presets)) {
return true;
}
if ($result) {
if (strpos($result, PHP_EOL) !== false && !in_array('dont_split_to_array', $presets)) {
$result = explode(PHP_EOL, $result);
}
if (in_array('get_code', $presets)) {
$curl_info = curl_getinfo($ch);
$result = $curl_info['http_code'];
}
curl_close($ch);
$out = $result;
}
else {
$out = array(
'error' => curl_error($ch),
);
}
}
else {
$out = array(
'error' => 'CURL_NOT_INSTALLED',
);
}
if ($presets && ($presets == 'get_code' || is_array($presets) && in_array('get_code', $presets)) && isset($out['error']) && $out['error'] == 'CURL_NOT_INSTALLED') {
$headers = get_headers($url);
$out = (int) preg_replace('/.*(\\d{3}).*/', '$1', $headers[0]);
}
return $out;
}
public static function array_merge__save_numeric_keys($arr1, $arr2) {
foreach ($arr2 as $key => $val) {
$arr1[$key] = $val;
}
return $arr1;
}
public static function array_merge__save_numeric_keys__recursive($arr1, $arr2) {
foreach ($arr2 as $key => $val) {
if (isset($arr1[$key]) && is_array($arr1[$key]) && is_array($val)) {
$arr1[$key] = self::array_merge__save_numeric_keys__recursive($arr1[$key], $val);
}
elseif (isset($arr1[$key]) && !is_array($arr1[$key]) && is_array($val)) {
$tmp = $arr1[$key] = $arr1[$key] = $val;
$arr1[$key][] = $tmp;
}
elseif (isset($arr1[$key]) && is_array($arr1[$key]) && !is_array($val)) {
$arr1[$key][] = $val;
}
else {
$arr1[$key] = $val;
}
}
return $arr1;
}
public static function removeNonUTF8($data) {
if (is_array($data) || is_object($data)) {
foreach ($data as $key => &$val) {
$val = self::removeNonUTF8($val);
}
unset($key, $val);
}
else {
if (!preg_match('//u', $data)) {
$data = 'Nulled. Not UTF8 encoded or malformed.';
}
}
return $data;
}
public static function toUTF8($obj, $data_codepage = null) {
if (is_array($obj) || is_object($obj)) {
foreach ($obj as $key => &$val) {
$val = self::toUTF8($val, $data_codepage);
}
unset($key, $val);
}
else {
if (!preg_match('//u', $obj) && function_exists('mb_detect_encoding') && function_exists('mb_convert_encoding')) {
$encoding = mb_detect_encoding($obj);
$encoding = $encoding ? $encoding : $data_codepage;
if ($encoding) {
$obj = mb_convert_encoding($obj, 'UTF-8', $encoding);
}
}
}
return $obj;
}
public static function fromUTF8($obj, $data_codepage = null) {
if (is_array($obj) || is_object($obj)) {
foreach ($obj as $key => &$val) {
$val = self::fromUTF8($val, $data_codepage);
}
unset($key, $val);
}
else {
if (preg_match('u', $obj) && function_exists('mb_convert_encoding') && $data_codepage !== null) {
$obj = mb_convert_encoding($obj, $data_codepage, 'UTF-8');
}
}
return $obj;
}
public static function is_json($string) {
return is_string($string) && is_array(json_decode($string, true)) ? true : false;
}
public static function apbct_cookie__set($name, $value = '', $expires = 0, $path = '', $domain = null, $secure = false, $httponly = false, $samesite = 'Lax') {
if (version_compare(phpversion(), '7.3.0', '>=')) {
$params = array(
'expires' => $expires,
'path' => $path,
'domain' => $domain,
'secure' => $secure,
'httponly' => $httponly,
);
if ($samesite) {
$params['samesite'] = $samesite;
}
setcookie($name, $value, $params);
}
else {
setcookie($name, $value, $expires, $path, $domain, $secure, $httponly);
}
}
public static function time__get_interval_start($interval = 300) {
return time() - (time() - strtotime(date('d F Y'))) % $interval;
}
static function get_mime_type($data, $type = '') {
$data = str_replace(chr(0), '', $data);
if (!empty($data) && @file_exists($data)) {
$type = mime_content_type($data);
}
elseif (function_exists('finfo_open')) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$type = finfo_buffer($finfo, $data);
finfo_close($finfo);
}
return $type;
}
static function buffer__trim_and_clear_from_empty_lines($buffer) {
$buffer = (array) $buffer;
foreach ($buffer as $indx => &$line) {
$line = trim($line);
if ($line === '') {
unset($buffer[$indx]);
}
}
return $buffer;
}
static function buffer__parse__csv($buffer) {
$buffer = explode("\n", $buffer);
$buffer = self::buffer__trim_and_clear_from_empty_lines($buffer);
foreach ($buffer as &$line) {
$line = str_getcsv($line, ',', '\'');
}
return $buffer;
}
public static function buffer__csv__pop_line(&$csv) {
$pos = strpos($csv, "\n");
$line = substr($csv, 0, $pos);
$csv = substr_replace($csv, '', 0, $pos + 1);
return $line;
}
public static function buffer__csv__get_map(&$csv) {
$line = static::buffer__csv__pop_line($csv);
return explode(',', $line);
}
public static function buffer__csv__pop_line_to_array(&$csv, $map = array()) {
$line = trim(static::buffer__csv__pop_line($csv));
$line = strpos($line, '\'') === 0 ? str_getcsv($line, ',', '\'') : explode(',', $line);
if ($map) {
$line = array_combine($map, $line);
}
return $line;
}
public static function db__prepare_param($param, $quotes = '\'') {
if (is_array($param)) {
foreach ($param as &$par) {
$par = self::db__prepare_param($par);
}
}
switch (true) {
case is_numeric($param):
$param = intval($param);
break;
case is_string($param) && strtolower($param) == 'null':
$param = 'NULL';
break;
case is_string($param):
global $wpdb;
$param = $quotes . $wpdb
->_real_escape($param) . $quotes;
break;
}
return $param;
}
public static function http__get_headers() {
$headers = array();
foreach ($_SERVER as $key => $val) {
if (0 === stripos($key, 'http_')) {
$server_key = preg_replace('/^http_/i', '', $key);
$key_parts = explode('_', $server_key);
if (count($key_parts) > 0 and strlen($server_key) > 2) {
foreach ($key_parts as $part_index => $part) {
if (!empty($part)) {
$key_parts[$part_index] = function_exists('mb_strtolower') ? mb_strtolower($part) : strtolower($part);
$key_parts[$part_index][0] = strtoupper($key_parts[$part_index][0]);
}
}
$server_key = implode('-', $key_parts);
}
$headers[$server_key] = $val;
}
}
return $headers;
}
public static function http__request__get_response_code($url) {
return static::http__request($url, array(), 'get_code');
}
public static function http__request__get_content($url) {
return static::http__request($url, array(), 'get dont_split_to_array');
}
public static function http__request__rc_to_host($rc_action, $request_params, $patterns = array()) {
$request_params__default = array(
'spbc_remote_call_action' => $rc_action,
'plugin_name' => 'apbct',
);
$result__rc_check_website = static::http__request(static::getSiteUrl(), array_merge($request_params__default, $request_params, array(
'test' => 'test',
)), array(
'get',
));
if (empty($result__rc_check_website['error'])) {
if (is_string($result__rc_check_website) && preg_match('@^.*?OK$@', $result__rc_check_website)) {
static::http__request(static::getSiteUrl(), array_merge($request_params__default, $request_params), array_merge(array(
'get',
), $patterns));
}
else {
return array(
'error' => 'WRONG_SITE_RESPONSE ACTION: ' . $rc_action . ' RESPONSE: ' . htmlspecialchars(substr(!is_string($result__rc_check_website) ? print_r($result__rc_check_website, true) : $result__rc_check_website, 0, 400)),
);
}
}
else {
return array(
'error' => 'WRONG_SITE_RESPONSE TEST ACTION: ' . $rc_action . ' ERROR: ' . $result__rc_check_website['error'],
);
}
return true;
}
private static function getSiteUrl() {
return (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" . $_SERVER['HTTP_HOST'] . (isset($_SERVER['SCRIPT_URL']) ? $_SERVER['SCRIPT_URL'] : '');
}
public static function getFwStats() {
die(__METHOD__ . ' method must be overloaded in the CMS-based Helper class');
}
public static function setFwStats($fw_stats) {
die(__METHOD__ . ' method must be overloaded in the CMS-based Helper class');
}
public static function SfwUpdate_DoFinisnAction() {
}
public static function ip__long2ip($ipl32) {
$ip[0] = $ipl32 >> 24 & 255;
$ip[1] = $ipl32 >> 16 & 255;
$ip[2] = $ipl32 >> 8 & 255;
$ip[3] = $ipl32 & 255;
return implode('.', $ip);
}
}