* @copyright 2007-2021 ETS-Soft
* @license Valid for 1 website (or project) for each purchase of license
* International Registered Trademark & Property of ETS-Soft
*/
function ets_cfu_kses_no_null($string, $options = null)
{
if (!isset($options['slash_zero'])) {
$options = array('slash_zero' => 'remove');
}
$string = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F]/', '', $string);
if ('remove' == $options['slash_zero']) {
$string = preg_replace('/\\\\+0+/', '', $string);
}
return $string;
}
function ets_cfu_absint($maybeint)
{
return abs((int)$maybeint);
}
function ets_cfu_specialchars($string, $quote_style = ENT_NOQUOTES, $charset = false, $double_encode = false)
{
$string = (string)$string;
if (0 === Tools::strlen($string))
return '';
// Don't bother if there are no specialchars - saves some processing
if (!preg_match('/[&<>"\']/', $string))
return $string;
// Account for the previous behaviour of the function when the $quote_style is not an accepted value
if (empty($quote_style))
$quote_style = ENT_NOQUOTES;
elseif (!in_array($quote_style, array(0, 2, 3, 'single', 'double'), true))
$quote_style = ENT_QUOTES;
// Store the site charset as a static to avoid multiple calls to ets_load_alloptions()
if (in_array($charset, array('utf8', 'utf-8', 'UTF8')))
$charset = 'UTF-8';
$_quote_style = $quote_style;
if ($quote_style === 'double') {
$quote_style = ENT_COMPAT;
$_quote_style = ENT_COMPAT;
} elseif ($quote_style === 'single') {
$quote_style = ENT_NOQUOTES;
}
if (!$double_encode) {
// Guarantee every &entity; is valid, convert &garbage; into &garbage;
// This is required for PHP < 5.4.0 because ENT_HTML401 flag is unavailable.
$string = ets_cfu_kses_normalize_entities($string);
}
$string = @htmlspecialchars($string, $quote_style, $charset, $double_encode);
// Back-compat.
if ('single' === $_quote_style)
$string = str_replace("'", ''', $string);
return $string;
}
function ets_cfu_check_invalid_utf8($string, $strip = false)
{
$string = (string)$string;
if (0 === Tools::strlen($string)) {
return '';
}
// Store the site charset as a static to avoid multiple calls to get_option()
static $is_utf8 = null;
if (!isset($is_utf8)) {
$is_utf8 = true;
}
if (!$is_utf8) {
return $string;
}
// Check for support for utf8 in the installed PCRE library once and store the result in a static
static $utf8_pcre = null;
if (!isset($utf8_pcre)) {
$utf8_pcre = @preg_match('/^./u', 'a');
}
// We can't demand utf8 in the PCRE installation, so just return the string in those cases
if (!$utf8_pcre) {
return $string;
}
// preg_match fails when it encounters invalid UTF8 in $string
if (1 === @preg_match('/^./us', $string)) {
return $string;
}
// Attempt to strip the bad chars if requested (not recommended)
if ($strip && function_exists('iconv')) {
return iconv('utf-8', 'utf-8', $string);
}
return '';
}
function ets_cfu_esc_html($text)
{
$safe_text = ets_cfu_check_invalid_utf8($text);
$safe_text = ets_cfu_specialchars($safe_text, ENT_QUOTES);
return $safe_text;
}
function ets_cfu_autop_preserve_newline_callback($matches)
{
return str_replace("\n", '
\s*
|', "\n\n", $pee);
// Space things out a little
/* ets_cfu: remove select and input */
$allblocks = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)';
$pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee);
$pee = preg_replace('!(' . $allblocks . '>)!', "$1\n\n", $pee);
/* ets_cfu: take care of [response], [recaptcha], and [hidden] tags */
$form_tags_manager = ETS_CFU_Form_Tag_Manager::get_instance();
$block_hidden_form_tags = $form_tags_manager->collect_tag_types(
array('display-block', 'display-hidden'));
$block_hidden_form_tags = sprintf('(?:%s)',
implode('|', $block_hidden_form_tags));
$pee = preg_replace('!(\[' . $block_hidden_form_tags . '[^]]*\])!',
"\n$1\n\n", $pee);
$pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
if (strpos($pee, '
\s*(?' . $allblocks . '[^>]*>)!', "$1", $pee); $pee = preg_replace('!(?' . $allblocks . '[^>]*>)\s*
!', "$1", $pee); /* ets_cfu: take care of [response], [recaptcha], and [hidden] tag */ $pee = preg_replace('!\s*(\[' . $block_hidden_form_tags . '[^]]*\])!', "$1", $pee); $pee = preg_replace('!(\[' . $block_hidden_form_tags . '[^]]*\])\s*
!', "$1", $pee); if ($br) { /* ets_cfu: add textarea */ $pee = preg_replace_callback( '/<(script|style|textarea).*?<\/\\1>/s', 'ets_cfu_autop_preserve_newline_callback', $pee); $pee = preg_replace('|(?)\s*\n|', "]*>)(.*?)!is', 'clean_pre', $pee); } $pee = preg_replace("|\n$|", '', $pee); return $pee; } function ets_cfu_add_form_tag($tag, $func, $features = '') { $manager = ETS_CFU_Form_Tag_Manager::get_instance(); return $manager->add($tag, $func, $features); } function ets_cfu_remove_form_tag($tag) { $manager = ETS_CFU_Form_Tag_Manager::get_instance(); return $manager->remove($tag); } function ets_cfu_replace_all_form_tags($content) { $manager = ETS_CFU_Form_Tag_Manager::get_instance(); return $manager->replace_all($content); } function ets_cfu_scan_form_tags($cond = null) { $contact_form = ETS_CFU_Contact_Form::get_current(); if ($contact_form) { return $contact_form->scan_form_tags($cond); } return array(); } function ets_cfu_form_tag_supports($tag, $feature) { $manager = ETS_CFU_Form_Tag_Manager::get_instance(); return $manager->tag_type_supports($tag, $feature); } function ets_cfu_file_form_enctype_filter($enctype) { $multipart = (bool)ets_cfu_scan_form_tags( array('type' => array('file', 'file*'))); if ($multipart) { $enctype = 'multipart/form-data'; } return $enctype; } function ets_cfu_quiz_validation_filter($result, $tag) { $name = $tag->name; $answer = Tools::isSubmit($name) ? ets_cfu_canonicalize(Tools::getValue($name)) : ''; $answer = ets_cfu_unslash($answer); $answer_hash = ets_cfu_hash($answer, 'ets_cfu_quiz'); $expected_hash = Tools::isSubmit('_ets_cfu_quiz_answer_' . $name) ? (string)Tools::getValue('_ets_cfu_quiz_answer_' . $name) : ''; if ($answer_hash != $expected_hash && $expected_hash) { $result->invalidate($tag, ets_cfu_get_message('quiz_answer_not_correct')); } return $result; } function ets_cfu_file_validation_filter($result, $tag) { $name = $tag->name; //$id = $tag->get_id_option(); $file = isset($_FILES[$name]) ? $_FILES[$name] : null; if ($file['error'] && UPLOAD_ERR_NO_FILE != $file['error']) { $result->invalidate($tag, ets_cfu_get_message('upload_failed_php_error')); return $result; } if (empty($file['tmp_name']) && $tag->is_required()) { $result->invalidate($tag, ets_cfu_get_message('invalid_required')); return $result; } if (!is_uploaded_file($file['tmp_name'])) { return $result; } /* File type validation */ $file_type_pattern = ets_cfu_acceptable_filetypes($tag->get_option('filetypes'), 'regex'); $file_type_pattern = '/\.(' . $file_type_pattern . ')$/i'; if (!preg_match($file_type_pattern, $file['name'])) { $result->invalidate($tag, ets_cfu_get_message('upload_file_type_invalid')); return $result; } /* File size validation */ $allowed_size = 1048576; // default size 1 MB if ($file_size_a = $tag->get_option('limit')) { $limit_pattern = '/^([1-9][0-9]*)([kKmM]?[bB])?$/'; foreach ($file_size_a as $file_size) { if (preg_match($limit_pattern, $file_size, $matches)) { $allowed_size = (int)$matches[1]; if (!empty($matches[2])) { $kbmb = Tools::strtolower($matches[2]); if ('kb' == $kbmb) { $allowed_size *= 1024; } elseif ('mb' == $kbmb) { $allowed_size *= 1024 * 1024; } } break; } } } if ($file['size'] > $allowed_size) { $result->invalidate($tag, ets_cfu_get_message('upload_file_too_large')); return $result; } ets_cfu_init_uploads(); // Confirm upload dir $uploads_dir = ets_cfu_upload_tmp_dir(); $filename = $file['name']; $filename = ets_cfu_canonicalize($filename, 'as-is'); $filename = ets_cfu_antiscript_file_name($filename); $filename = ets_cfu_generateRandomString(7) . '-' . ets_cfu_unique_filename($uploads_dir, str_replace(' ', '-', $filename)); $new_file = ets_cfu_path_join($uploads_dir, $filename); $attachment = Tools::fileAttachment($name); if (false === move_uploaded_file($file['tmp_name'], $new_file)) { $result->invalidate($tag, ets_cfu_get_message('upload_failed')); return $result; } chmod($new_file, 0644); if ($submission = ETS_CFU_Submission::get_instance()) { $submission->add_uploaded_file($name, $new_file, $attachment); } return $result; } function ets_cfu_generateRandomString($length = 10) { $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $charactersLength = Tools::strlen($characters); $randomString = ''; for ($i = 0; $i < $length; $i++) { $randomString .= $characters[rand(0, $charactersLength - 1)]; } return $randomString; } /* File uploading functions */ function ets_cfu_acceptable_filetypes($types = 'default', $format = 'regex') { if ('default' === $types || empty($types)) { $types = array( 'jpg', 'jpeg', 'png', 'gif', 'pdf', 'doc', 'docx', 'ppt', 'pptx', 'odt', 'avi', 'ogg', 'm4a', 'mov', 'mp3', 'mp4', 'mpg', 'wav', 'wmv', 'zip', 'rar', 'txt' ); } else { $types_tmp = (array)$types; $types = array(); foreach ($types_tmp as $val) { if (is_string($val)) { $val = preg_split('/[\s|,]+/', $val); } $types = array_merge($types, (array)$val); } } $types = array_unique(array_filter($types)); $output = ''; foreach ($types as $type) { $type = trim($type, ' ,.|'); $type = str_replace( array('.', '+', '*', '?'), array('\.', '\+', '\*', '\?'), $type); if ('' === $type) { continue; } if ('attr' === $format || 'attribute' === $format) { $output .= sprintf('.%s', $type); $output .= ','; } else { $output .= $type; $output .= '|'; } } return trim($output, ' ,|'); } function ets_cfu_init_uploads() { $dir = ets_cfu_upload_tmp_dir(); ets_cfu_mkdir_p($dir); return true; } function ets_cfu_upload_tmp_dir() { return dirname(__FILE__) . '/../views/img/etscfu_upload'; } function ets_cfu_textarea_validation_filter($result, $tag) { //$type = $tag->type; $name = $tag->name; $value = Tools::isSubmit($name) ? (string)Tools::getValue($name) : ''; if ($tag->is_required() && '' == $value) { $result->invalidate($tag, ets_cfu_get_message('invalid_required')); } if ('' !== $value) { $maxlength = $tag->get_maxlength_option(); $minlength = $tag->get_minlength_option(); if ($maxlength && $minlength && $maxlength < $minlength) { $maxlength = $minlength = null; } $code_units = ets_cfu_count_code_units(Tools::stripslashes($value)); if (false !== $code_units) { if ($maxlength && $maxlength < $code_units) { $result->invalidate($tag, ets_cfu_get_message('invalid_too_long')); } elseif ($minlength && $code_units < $minlength) { $result->invalidate($tag, ets_cfu_get_message('invalid_too_short')); } } } return $result; } function ets_cfu_text_validation_filter($result, $tag) { $name = $tag->name; $value = Tools::getValue($name) && !is_array(Tools::getValue($name)) ? trim(ets_cfu_unslash(strtr((string)Tools::getValue($name), "\n", " "))) : Tools::getValue($name); if ('text' == $tag->basetype) { if ($tag->is_required() && '' == $value) { $result->invalidate($tag, ets_cfu_get_message('invalid_required')); } } if ('email' == $tag->basetype) { if ($tag->is_required() && '' == $value) { $result->invalidate($tag, ets_cfu_get_message('invalid_required')); } elseif ('' != $value && !ets_cfu_is_email($value)) { $result->invalidate($tag, ets_cfu_get_message('invalid_email')); } elseif (($msg = Ets_cfultimate::checkEmailBlackLists($value)) && $msg !== true) { $result->invalidate($tag, $msg); }elseif ('' != $value && ets_cfu_is_blacklist_email($value)){ $result->invalidate($tag, ets_cfu_get_message('email_black_list')); } } if ('url' == $tag->basetype) { if ($tag->is_required() && '' == $value) { $result->invalidate($tag, ets_cfu_get_message('invalid_required')); } elseif ('' != $value && !ets_cfu_is_url($value)) { $result->invalidate($tag, ets_cfu_get_message('invalid_url')); } } if ('tel' == $tag->basetype) { if ($tag->is_required() && '' == $value) { $result->invalidate($tag, ets_cfu_get_message('invalid_required')); } elseif ('' != $value && !ets_cfu_is_tel($value)) { $result->invalidate($tag, ets_cfu_get_message('invalid_tel')); } } if ('' !== $value && !is_array($value)) { $maxlength = $tag->get_maxlength_option(); $minlength = $tag->get_minlength_option(); if ($maxlength && $minlength && $maxlength < $minlength) { $maxlength = $minlength = null; } $code_units = ets_cfu_count_code_units(Tools::stripslashes($value)); if (false !== $code_units) { if ($maxlength && $maxlength < $code_units) { $result->invalidate($tag, ets_cfu_get_message('invalid_too_long')); } elseif ($minlength && $code_units < $minlength) { $result->invalidate($tag, ets_cfu_get_message('invalid_too_short')); } } } return $result; } function ets_cfu_checkbox_validation_filter($result, $tag) { $name = $tag->name; $is_required = $tag->is_required();// || 'radio' == $tag->type; $value = Tools::isSubmit($name) ? (array)Tools::getValue($name) : array(); if ($is_required && empty($value)) { $result->invalidate($tag, ets_cfu_get_message('invalid_required')); } return $result; } function ets_cfu_date_validation_filter($result, $tag) { $name = $tag->name; $min = $tag->get_date_option('min'); $max = $tag->get_date_option('max'); $value = Tools::isSubmit($name) ? trim(strtr((string)Tools::getValue($name), "\n", " ")) : ''; if ($tag->is_required() && '' == $value) { $result->invalidate($tag, ets_cfu_get_message('invalid_required')); } elseif ('' != $value && !ets_cfu_is_date($value)) { $result->invalidate($tag, ets_cfu_get_message('invalid_date')); } elseif ('' != $value && !empty($min) && $value < $min) { $result->invalidate($tag, ets_cfu_get_message('date_too_early')); } elseif ('' != $value && !empty($max) && $max < $value) { $result->invalidate($tag, ets_cfu_get_message('date_too_late')); } return $result; } function ets_cfu_number_validation_filter($result, $tag) { $name = $tag->name; $value = Tools::isSubmit($name) ? trim(strtr((string)Tools::getValue($name), "\n", " ")) : ''; $min = $tag->get_option('min', 'signed_int', true); $max = $tag->get_option('max', 'signed_int', true); if ($tag->is_required() && '' == $value) { $result->invalidate($tag, ets_cfu_get_message('invalid_required')); } elseif ('' != $value && !ets_cfu_is_number($value)) { $result->invalidate($tag, ets_cfu_get_message('invalid_number')); } elseif ('' != $value && '' != $min && (float)$value < (float)$min) { $result->invalidate($tag, ets_cfu_get_message('number_too_small')); } elseif ('' != $value && '' != $max && (float)$max < (float)$value) { $result->invalidate($tag, ets_cfu_get_message('number_too_large')); } return $result; } function ets_cfu_captcha_validation_filter($result, $tag) { //$type = $tag->type; $name = $tag->name; //$captchac = '_ets_cfu_captcha_challenge_' . $name; $prefix = isset(Context::getContext()->cookie->$name) ? (string)Context::getContext()->cookie->$name : ''; $response = Tools::isSubmit($name) ? (string)Tools::getValue($name) : ''; if (0 == Tools::strlen($prefix) || trim($prefix) != trim($response)) { $result->invalidate($tag, ets_cfu_get_message('captcha_not_match')); } Context::getContext()->cookie->$name = ''; Context::getContext()->cookie->write(); return $result; } function ets_cfu_select_validation_filter($result, $tag) { $name = $tag->name; $values = Tools::getValue($name); if (is_array($values)) { foreach ($values as $key => $value) { if ('' === $value) { unset($values[$key]); } } } $empty = empty($values); if ($tag->is_required() && $empty) { $result->invalidate($tag, ets_cfu_get_message('invalid_required')); } return $result; } function ets_cfu_acceptance_validation_filter($result, $tag) { if ($tag->has_option('optional')) { return $result; } $name = $tag->name; $value = (Tools::getValue($name) ? 1 : 0); $invert = $tag->has_option('invert'); if ($invert && $value || !$invert && !$value) { $result->invalidate($tag, ets_cfu_get_message('accept_terms')); } return $result; } function ets_cfu_esc_url($url, $protocols = null, $_context = 'display') { //$original_url = $url; if ('' == $url) return $url; $url = str_replace(' ', '%20', $url); $url = preg_replace('|[^a-z0-9-~+_.?#=!&;,/:%@$\|*\'()\[\]\\x80-\\xff]|i', '', $url); if ('' === $url) { return $url; } if (0 !== stripos($url, 'mailto:')) { $strip = array('%0d', '%0a', '%0D', '%0A'); $url = _ets_cfu_deep_replace($strip, $url); } $url = str_replace(';//', '://', $url); /* If the URL doesn't appear to contain a scheme, we * presume it needs http:// prepended (unless a relative * link starting with /, # or ? or a php file). */ if (strpos($url, ':') === false && !in_array($url[0], array('/', '#', '?')) && !preg_match('/^[a-z0-9-]+?\.php/i', $url)) $url = 'http://' . $url; // Replace ampersands and single quotes only when displaying. if ('display' == $_context) { $url = ets_cfu_kses_normalize_entities($url); $url = str_replace('&', '&', $url); $url = str_replace("'", ''', $url); } if ('/' === $url[0]) { $good_protocol_url = $url; } else { if (!is_array($protocols)) $protocols = ets_cfu_allowed_protocols(); $good_protocol_url = ets_cfu_kses_bad_protocol($url, $protocols); if (Tools::strtolower($good_protocol_url) != Tools::strtolower($url)) return ''; } return $good_protocol_url; } function ets_cfu_allowed_protocols() { static $protocols = array(); if (empty($protocols)) { $protocols = array('http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet', 'mms', 'rtsp', 'svn', 'tel', 'fax', 'xmpp', 'webcal', 'urn'); } return $protocols; } function ets_cfu_kses_normalize_entities($string) { // Disarm all entities by converting & to & $string = str_replace('&', '&', $string); // Change back the allowed entities in our entity whitelist $string = preg_replace_callback('/&#(0*[0-9]{1,7});/', 'ets_cfu_kses_normalize_entities2', $string); return $string; } function ets_cfu_kses_normalize_entities2($matches) { if (empty($matches[1])) return ''; $i = $matches[1]; $i = "&#$i;"; return $i; } function ets_cfu_kses_bad_protocol($string, $allowed_protocols) { $string = ets_cfu_kses_no_null($string); $iterations = 0; do { $original_string = $string; $string = ets_cfu_kses_bad_protocol_once($string, $allowed_protocols); } while ($original_string != $string && ++$iterations < 6); if ($original_string != $string) return ''; return $string; } function ets_cfu_kses_bad_protocol_once($string, $allowed_protocols, $count = 1) { $string2 = preg_split('/:|*58;|*3a;/i', $string, 2); if (isset($string2[1]) && !preg_match('%/\?%', $string2[0])) { $string = trim($string2[1]); $protocol = ets_cfu_kses_bad_protocol_once2($string2[0], $allowed_protocols); if ('feed:' == $protocol) { if ($count > 2) return ''; $string = ets_cfu_kses_bad_protocol_once($string, $allowed_protocols, ++$count); if (empty($string)) return $string; } $string = $protocol . $string; } return $string; } function ets_cfu_kses_bad_protocol_once2($string, $allowed_protocols) { $string2 = ets_cfu_kses_decode_entities($string); $string2 = preg_replace('/\s/', '', $string2); $string2 = ets_cfu_kses_no_null($string2); $string2 = Tools::strtolower($string2); $allowed = false; foreach ((array)$allowed_protocols as $one_protocol) if (Tools::strtolower($one_protocol) == $string2) { $allowed = true; break; } if ($allowed) return "$string2:"; else return ''; } function ets_cfu_kses_decode_entities($string) { $string = preg_replace_callback('/([0-9]+);/', '_ets_cfu_kses_decode_entities_chr', $string); $string = preg_replace_callback('/[Xx]([0-9A-Fa-f]+);/', '_ets_cfu_kses_decode_entities_chr_hexdec', $string); return $string; } function _ets_cfu_kses_decode_entities_chr_hexdec($match) { return chr(hexdec($match[1])); } function _ets_cfu_kses_decode_entities_chr($match) { return chr($match[1]); } function ets_cfu_untrailingslashit($string) { return rtrim($string, '/\\'); } function ets_cfu_esc_url_raw($url, $protocols = null) { return ets_cfu_esc_url($url, $protocols, 'db'); } function ets_cfu_add_query_arg() { $args = func_get_args(); if (is_array($args[0])) { if (count($args) < 2 || false === $args[1]) $uri = $_SERVER['REQUEST_URI']; else $uri = $args[1]; } else { if (count($args) < 3 || false === $args[2]) $uri = $_SERVER['REQUEST_URI']; else $uri = $args[2]; } if ($frag = strstr($uri, '#')) $uri = Tools::substr($uri, 0, -Tools::strlen($frag)); else $frag = ''; if (0 === stripos($uri, 'http://')) { $protocol = 'http://'; $uri = Tools::substr($uri, 7); } elseif (0 === stripos($uri, 'https://')) { $protocol = 'https://'; $uri = Tools::substr($uri, 8); } else { $protocol = ''; } if (strpos($uri, '?') !== false) { list($base, $query) = explode('?', $uri, 2); $base .= '?'; } elseif ($protocol || strpos($uri, '=') === false) { $base = $uri . '?'; $query = ''; } else { $base = ''; $query = $uri; } $qs = array(); ets_cfu_parse_str($query, $qs); $qs = ets_cfu_urlencode_deep($qs); // this re-URL-encodes things that were already in the query string if (is_array($args[0])) { foreach ($args[0] as $k => $v) { $qs[$k] = $v; } } else { $qs[$args[0]] = $args[1]; } foreach ($qs as $k => $v) { if ($v === false) unset($qs[$k]); } $ret = ets_cfu__build_query($qs); $ret = trim($ret, '?'); $ret = preg_replace('#=(&|$)#', '$1', $ret); $ret = $protocol . $base . $ret . $frag; $ret = rtrim($ret, '?'); return $ret; } function ets_cfu_urlencode_deep($value) { return ets_cfu_map_deep($value, 'urlencode'); } function ets_cfu_map_deep($value, $callback) { if (is_array($value)) { foreach ($value as $index => $item) { $value[$index] = ets_cfu_map_deep($item, $callback); } } elseif (is_object($value)) { $object_vars = get_object_vars($value); foreach ($object_vars as $property_name => $property_value) { $value->$property_name = ets_cfu_map_deep($property_value, $callback); } } else { $value = call_user_func($callback, $value); } return $value; } function ets_cfu__build_query($data) { return ets_cfu_http_build_query($data, null, '&', '', false); } function ets_cfu_http_build_query($data, $prefix = null, $sep = null, $key = '', $urlencode = true) { $ret = array(); foreach ((array)$data as $k => $v) { if ($urlencode) $k = urlencode($k); if (is_int($k) && $prefix != null) $k = $prefix . $k; if (!empty($key)) $k = $key . '%5B' . $k . '%5D'; if ($v === null) continue; elseif ($v === false) $v = '0'; if (is_array($v) || is_object($v)) array_push($ret, ets_cfu_http_build_query($v, '', $sep, $k, $urlencode)); elseif ($urlencode) array_push($ret, $k . '=' . urlencode($v)); else array_push($ret, $k . '=' . $v); } if (null === $sep) $sep = ini_get('arg_separator.output'); return implode($sep, $ret); } function _ets_cfu_deep_replace($search, $subject) { $subject = (string)$subject; $count = 1; while ($count) { $subject = str_replace($search, '', $subject, $count); } return $subject; } function ets_cfu_current_time($type, $gmt = 0) { switch ($type) { case 'mysql': return ($gmt) ? gmdate('Y-m-d H:i:s') : gmdate('Y-m-d H:i:s', time()); case 'timestamp': return ($gmt) ? time() : time(); default: return ($gmt) ? date($type) : date($type, time()); } } function ets_cfu_strip_all_tags($string, $remove_breaks = false) { $string = preg_replace('@<(script|style)[^>]*?>.*?\\1>@si', '', $string); $string = strip_tags($string); if ($remove_breaks) $string = preg_replace('/[\r\n\t ]+/', ' ', $string); return trim($string); } function ets_cfu_mail_replace_tags($content, $args = '', $body = false) { if ($args) { $args = array( 'html' => false, 'exclude_blank' => false, ); } if (is_array($content)) { foreach ($content as $key => $value) { $content[$key] = ets_cfu_mail_replace_tags($value, $args); } return $content; } $content = explode("\n", $content); foreach ($content as $num => $line) { $line = new ETS_CFU_MailTaggedText($line, $args); $replaced = $line->replace_tags(); if ($args['exclude_blank']) { $replaced_tags = $line->get_replaced_tags(); if (empty($replaced_tags) || array_filter($replaced_tags)) { $content[$num] = $replaced; } else { unset($content[$num]); // Remove a line. } } else { $content[$num] = $replaced; } } $content = implode("\n", $content); unset($body); return $content; } function ets_cfu_unslash($value) { return ets_cfu_stripslashes_deep($value); } function ets_cfu_stripslashes_deep($value) { return ets_cfu_map_deep($value, 'ets_cfu_stripslashes_from_strings_only'); } function ets_cfu_stripslashes_from_strings_only($value) { return is_string($value) ? Tools::stripslashes($value) : $value; } function ets_cfu_is_valid_locale($locale) { $pattern = '/^[a-z]{2,3}(?:_[a-zA-Z_]{2,})?$/'; return (bool)preg_match($pattern, $locale); } function ets_cfu_path_join($base, $path) { if (ets_cfu_path_is_absolute($path)) return $path; return rtrim($base, '/') . '/' . ltrim($path, '/'); } function ets_cfu_mkdir_p($target) { $wrapper = null; // Strip the protocol. if (ets_cfu_is_stream($target)) { list($wrapper, $target) = explode('://', $target, 2); } // From php.net/mkdir user contributed notes. $target = str_replace('//', '/', $target); // Put the wrapper back on the target. if ($wrapper !== null) { $target = $wrapper . '://' . $target; } /* * Safe mode fails with a trailing slash under certain PHP versions. * Use rtrim() instead of ets_cfu_untrailingslashit to avoid formatting.php dependency. */ $target = rtrim($target, '/'); if (empty($target)) $target = '/'; if (file_exists($target)) return @is_dir($target); // We need to find the permissions of the parent folder that exists and inherit that. $target_parent = dirname($target); while ('.' != $target_parent && !is_dir($target_parent)) { $target_parent = dirname($target_parent); } // Get the permission bits. if ($stat = @stat($target_parent)) { $dir_perms = $stat['mode'] & 0007777; } else { $dir_perms = 0777; } if (@mkdir($target, $dir_perms, true)) { /* * If a umask is set that modifies $dir_perms, we'll have to re-set * the $dir_perms correctly with chmod() */ if ($dir_perms != ($dir_perms & ~umask())) { $folder_parts = explode('/', Tools::substr($target, Tools::strlen($target_parent) + 1)); for ($i = 1, $c = count($folder_parts); $i <= $c; $i++) { @chmod($target_parent . '/' . implode('/', array_slice($folder_parts, 0, $i)), $dir_perms); } } return true; } return false; } function ets_cfu_is_stream($path) { $wrappers = stream_get_wrappers(); $wrappers_re = '(' . join('|', $wrappers) . ')'; return preg_match("!^$wrappers_re://!", $path) === 1; } function ets_cfu_path_is_absolute($path) { /* * This is definitive if true but fails if $path does not exist or contains * a symbolic link. */ if (realpath($path) == $path) return true; if (Tools::strlen($path) == 0 || $path[0] == '.') return false; // Windows allows absolute paths like this. if (preg_match('#^[a-zA-Z]:\\\\#', $path)) return true; // A path starting with / or \ is absolute; anything else is relative. return ($path[0] == '/' || $path[0] == '\\'); } function ets_cfu_zeroise($number, $threshold) { return sprintf('%0' . $threshold . 's', $number); } function ets_cfu_unique_filename($dir, $filename, $unique_filename_callback = null) { // Separate the filename into a name and extension. $ext = pathinfo($filename, PATHINFO_EXTENSION); $name = pathinfo($filename, PATHINFO_BASENAME); if ($ext) { $ext = '.' . $ext; } // Edge case: if file is named '.ext', treat as an empty name. if ($name === $ext) { $name = ''; } /* * Increment the file number until we have a unique file to save in $dir. * Use callback if supplied. */ if ($unique_filename_callback && is_callable($unique_filename_callback)) { $filename = call_user_func($unique_filename_callback, $dir, $name, $ext); } else { $number = ''; // Change '.ext' to lower case. if ($ext && Tools::strtolower($ext) != $ext) { $ext2 = Tools::strtolower($ext); $filename2 = preg_replace('|' . preg_quote($ext) . '$|', $ext2, $filename); // Check for both lower and upper case extension or image sub-sizes may be overwritten. while (file_exists($dir . "/$filename") || file_exists($dir . "/$filename2")) { $new_number = (int)$number + 1; $filename = str_replace(array("-$number$ext", "$number$ext"), "-$new_number$ext", $filename); $filename2 = str_replace(array("-$number$ext2", "$number$ext2"), "-$new_number$ext2", $filename2); $number = $new_number; } return $filename2; } while (file_exists($dir . "/$filename")) { $new_number = (int)$number + 1; if ('' == "$number$ext") { $filename = "$filename-" . $new_number; } else { $filename = str_replace(array("-$number$ext", "$number$ext"), "-" . $new_number . $ext, $filename); } $number = $new_number; } } /** This filter is documented in wp-includes/functions.php */ return $filename; } function ets_cfu_recaptcha_noscript($args = '') { $args = ets_cfu_parse_args($args, array( 'sitekey' => '', )); if (empty($args['sitekey'])) { return; } } function ets_cfu_recaptcha_check_with_google($spam) { $contact_form = ets_cfu_get_current_contact_form(); if (!$contact_form) { return $spam; } $tags = $contact_form->scan_form_tags(array('type' => 'recaptcha')); if (empty($tags)) { return $spam; } $recaptcha = ETS_CFU_Recaptcha::get_instance(); if (!$recaptcha->is_active()) { return $spam; } $response_token = ets_cfu_recaptcha_response(); $spam = !$recaptcha->verify($response_token); return $spam; } function ets_cfu_recaptcha_response() { if (Tools::isSubmit('g-recaptcha-response')) { return Tools::getValue('g-recaptcha-response'); } return false; } function ets_cfu_wpautop($pee, $br = true) { $pre_tags = array(); if (trim($pee) === '') return ''; // Just to make things a little easier, pad the end. $pee = $pee . "\n"; /* * Pre tags shouldn't be touched by autop. * Replace pre tags with placeholders and bring them back after autop. */ if (strpos($pee, '
', $pee);
$last_pee = array_pop($pee_parts);
$pee = '';
$i = 0;
foreach ($pee_parts as $pee_part) {
$start = strpos($pee_part, '";
$pre_tags[$name] = Tools::substr($pee_part, $start) . '';
$pee .= Tools::substr($pee_part, 0, $start) . $name;
$i++;
}
$pee .= $last_pee;
}
// Change multiple