Skip to content

Commit

Permalink
chore, phpcs
Browse files Browse the repository at this point in the history
  • Loading branch information
gardenboi committed Dec 15, 2023
1 parent d1b0c2f commit 5b1af7d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 55 deletions.
8 changes: 4 additions & 4 deletions admin/CF7_AntiSpam_Admin_Customizations.php
Original file line number Diff line number Diff line change
Expand Up @@ -779,16 +779,16 @@ public function cf7a_check_geoip() {

/** It prints the language info text */
public function cf7a_check_language() {
$iso_639 = '<a href="' . esc_url('https://www.loc.gov/standards/iso639-2/php/code_list.php') . '">ISO 639-1</a>';
$iso_3166 = '<a href="' . esc_url('https://www.iso.org/obp/ui/#search') . '">ISO 3166-2</a>';
$iso_639 = '<a href="' . esc_url( 'https://www.loc.gov/standards/iso639-2/php/code_list.php' ) . '">ISO 639-1</a>';
$iso_3166 = '<a href="' . esc_url( 'https://www.iso.org/obp/ui/#search' ) . '">ISO 3166-2</a>';
printf(
'<p>%s</p><p>%s<br>%s</p><p>%s<br>%s<br>%s</p>',
esc_html__( 'Check the user browser language / user keyboard. Add a country code / locale (US), language (en) or both (en-US) each line, in the latter case you must delimit with a "-" (Hyphen) character , you can insert them comma separated and when you save they will be formatted with the standard one per line.', 'cf7-antispam' ),
esc_html__( 'The browser language detection and country detection are separated, you can choose one of them each line or both.', 'cf7-antispam' ),
esc_html__( 'The language detection must be lower case, while the country detection upper case.', 'cf7-antispam' ),
esc_html__( 'If you are unsure please consult these ISO standards:', 'cf7-antispam' ),
esc_html__( '- Language codes (use ctrl+f for search) ', 'cf7-antispam') . wp_kses( $iso_639, array( 'a' => array( 'href' => array() ) ) ),
esc_html__( '- Country codes (refer to "Alpha-2 code"), ', 'cf7-antispam') . wp_kses( $iso_3166, array( 'a' => array( 'href' => array() ) ) )
esc_html__( '- Language codes (use ctrl+f for search) ', 'cf7-antispam' ) . wp_kses( $iso_639, array( 'a' => array( 'href' => array() ) ) ),
esc_html__( '- Country codes (refer to "Alpha-2 code"), ', 'cf7-antispam' ) . wp_kses( $iso_3166, array( 'a' => array( 'href' => array() ) ) )
);
}

Expand Down
80 changes: 44 additions & 36 deletions core/CF7_AntiSpam_Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ public function cf7a_blacklist_get_id( $id ) {
* It adds an IP address to the blacklist.
*
* @param string $ip The IP address to ban.
* @param array $reason The reason why the IP is being banned.
* @param float $spam_score This is the number of points that will be added to the IP's spam score.
* @param array $reason The reason why the IP is being banned.
* @param float $spam_score This is the number of points that will be added to the IP's spam score.
*
* @return bool true if the given id was banned
*/
Expand Down Expand Up @@ -244,7 +244,7 @@ public function cf7a_cron_unban() {
/**
* Retrieves the list of languages or locales from the given options array by key.
*
* @param array $option An array of options.
* @param array $option An array of options.
* @param string $key The key of the option to retrieve.
*
* @return array The list of languages extracted from the options array.
Expand All @@ -259,7 +259,7 @@ public function cf7a_get_language_locales( $option, $key ) {
} elseif ( $n == 1 ) {
return strtoupper( $l[1] );
}
} else if ( strlen( $el ) === 2 && ctype_alpha( $el ) ) {
} elseif ( strlen( $el ) === 2 && ctype_alpha( $el ) ) {
if ( $n == 0 && ctype_lower( $el ) ) {
return $el;
} elseif ( $n == 1 && ctype_upper( $el ) ) {
Expand All @@ -270,19 +270,26 @@ public function cf7a_get_language_locales( $option, $key ) {
return '';
};

return array_values( array_unique( array_reduce( $option, function ( $carry, $item ) use ( $key, $check_len ) {
$carry = is_array($carry) ? $carry : array();
if ( $key == 'languages' ) {
$l = $check_len( $item, 0 );
} elseif ( $key == 'locales' ) {
$l = $check_len( $item, 1 );
}
if ( ! empty( $l ) ) {
$carry[] = $l;
}
return array_values(
array_unique(
array_reduce(
$option,
function ( $carry, $item ) use ( $key, $check_len ) {
$carry = is_array( $carry ) ? $carry : array();
if ( $key == 'languages' ) {
$l = $check_len( $item, 0 );
} elseif ( $key == 'locales' ) {
$l = $check_len( $item, 1 );
}
if ( ! empty( $l ) ) {
$carry[] = $l;
}

return $carry;
} ) ) );
return $carry;
}
)
)
);
}


Expand Down Expand Up @@ -567,7 +574,7 @@ public function cf7a_spam_filter( $spam ) {
if ( intval( $options['check_refer'] ) === 1 ) {
if ( ! $cf7a_referer ) {

$spam_score += $score_warn;
$spam_score += $score_warn;
$reason['no_referrer'] = 'client has referrer address';

cf7a_log( "the $remote_ip has reached the contact form page without any referrer", 1 );
Expand All @@ -577,7 +584,7 @@ public function cf7a_spam_filter( $spam ) {
if ( $cf7a_protocol ) {
if ( in_array( $cf7a_protocol, array( 'HTTP/1.0', 'HTTP/1.1', 'HTTP/1.2' ) ) ) {

$spam_score += $score_warn;
$spam_score += $score_warn;
$reason['no_protocol'] = 'client has a bot-like connection protocol';

cf7a_log( "the $remote_ip has a bot-like connection protocol (HTTP/1.X)", 1 );
Expand All @@ -589,7 +596,7 @@ public function cf7a_spam_filter( $spam ) {
*/
if ( ! $cf7a_version ) {

$spam_score += $score_fingerprinting;
$spam_score += $score_fingerprinting;
$reason['data_mismatch'] = "Version mismatch '$cf7a_version' != '" . CF7ANTISPAM_VERSION . "'";

cf7a_log( "Incorrect data submitted by $remote_ip in the hidden field _version, may have been modified, removed or hacked", 1 );
Expand Down Expand Up @@ -656,7 +663,7 @@ public function cf7a_spam_filter( $spam ) {

/* increment the spam score if needed, then log the result */
if ( ! empty( $fails ) ) {
$spam_score += count( $fails ) * $score_fingerprinting;
$spam_score += count( $fails ) * $score_fingerprinting;
$reason['bot_fingerprint'] = implode( ', ', $fails );

cf7a_log( "The $remote_ip ip hasn't passed " . count( $fails ) . ' / ' . count( $bot_fingerprint ) . " of the bot fingerprint test ({$reason['bot_fingerprint']})", 1 );
Expand Down Expand Up @@ -701,7 +708,7 @@ public function cf7a_spam_filter( $spam ) {

if ( ! empty( $fails ) ) {

$spam_score += count( $fails ) * $score_fingerprinting;
$spam_score += count( $fails ) * $score_fingerprinting;
$reason['bot_fingerprint_extras'] = implode( ', ', $fails );

cf7a_log( "The $remote_ip ip hasn't passed " . count( $fails ) . ' / ' . count( $bot_fingerprint_extras ) . " of the bot fingerprint extra test ({$reason['bot_fingerprint_extras']})", 1 );
Expand All @@ -723,15 +730,15 @@ public function cf7a_spam_filter( $spam ) {
* Language checks
*/
if ( empty( $languages['browser_language'] ) ) {
$spam_score += $score_detection;
$spam_score += $score_detection;
$reason['browser_language'] = 'missing browser language';
} else {
$languages_locales = cf7a_get_browser_languages_locales_array( $languages['browser_language'] );
$languages['browser'] = $languages_locales['languages'];
}

if ( empty( $languages['accept_language'] ) ) {
$spam_score += $score_detection;
$spam_score += $score_detection;
$reason['language_field'] = 'missing language field';
} else {
$languages['accept'] = cf7a_get_accept_language_array( $languages['accept_language'] );
Expand All @@ -757,7 +764,7 @@ public function cf7a_spam_filter( $spam ) {
$language_disallowed = $this->cf7a_check_languages_locales_allowed( $client_languages, $languages_disallowed, $languages_allowed );

if ( false === $language_disallowed ) {
$spam_score += $score_detection;
$spam_score += $score_detection;
$reason['browser_language'] = implode( ', ', $client_languages );
}
}
Expand All @@ -784,11 +791,12 @@ public function cf7a_spam_filter( $spam ) {
$geo_data = array_filter( array( $geoip_continent, $geoip_country ) );

if ( ! empty( $geo_data ) ) {
/* then check if the detected country is among the allowed and disallowed languages */
/*
then check if the detected country is among the allowed and disallowed languages */
// Check if the country is allowed by country by splitting browser headers 2nd arg since ISO is coherent
if ( false === $this->cf7a_check_languages_locales_allowed( $geo_data, $locales_disallowed, $locales_allowed ) ) {
$reason['geo_ip'] = $geoip_continent . '-' . $geoip_country;
$spam_score += $score_warn;
$spam_score += $score_warn;

cf7a_log( "The $remote_ip is not allowed by geoip" . $reason['geo_ip'], 1 );
}
Expand All @@ -808,7 +816,7 @@ public function cf7a_spam_filter( $spam ) {

if ( ! $timestamp ) {

$spam_score += $score_detection;
$spam_score += $score_detection;
$reason['timestamp'] = 'undefined';

cf7a_log( "The $remote_ip ip _timestamp field is missing, probable form hacking attempt from $remote_ip", 1 );
Expand All @@ -822,7 +830,7 @@ public function cf7a_spam_filter( $spam ) {
*/
if ( 0 !== $time_elapsed_min && $time_elapsed < $time_elapsed_min ) {

$spam_score += $score_time;
$spam_score += $score_time;
$reason['min_time_elapsed'] = $time_elapsed;

cf7a_log( "The $remote_ip ip took too little time to fill in the form - elapsed $time_elapsed seconds < $time_elapsed_min seconds expected", 1 );
Expand All @@ -833,7 +841,7 @@ public function cf7a_spam_filter( $spam ) {
*/
if ( 0 !== $time_elapsed_max && $time_elapsed > $time_elapsed_max ) {

$spam_score += $score_time;
$spam_score += $score_time;
$reason['max_time_elapsed'] = $time_elapsed;

cf7a_log( "The $remote_ip ip took too much time to fill in the form - elapsed $time_elapsed seconds > $time_elapsed_max seconds expected", 1 );
Expand All @@ -851,7 +859,7 @@ public function cf7a_spam_filter( $spam ) {
foreach ( $emails as $email ) {
foreach ( $bad_email_strings as $bad_email_string ) {
if ( false !== stripos( strtolower( $email ), strtolower( $bad_email_string ) ) ) {
$spam_score += $score_bad_string;
$spam_score += $score_bad_string;
$reason['email_blacklisted'][] = $bad_email_string;
}
}
Expand All @@ -872,7 +880,7 @@ public function cf7a_spam_filter( $spam ) {

if ( ! $user_agent ) {

$spam_score += $score_detection;
$spam_score += $score_detection;
$reason['user_agent'] = 'empty';

cf7a_log( "The $remote_ip ip user agent is empty, look like a spambot", 1 );
Expand All @@ -881,7 +889,7 @@ public function cf7a_spam_filter( $spam ) {
foreach ( $bad_user_agent_list as $bad_user_agent ) {

if ( false !== stripos( strtolower( $user_agent ), strtolower( $bad_user_agent ) ) ) {
$spam_score += $score_bad_string;
$spam_score += $score_bad_string;
$reason['user_agent'] = $bad_user_agent;
}
}
Expand All @@ -904,7 +912,7 @@ public function cf7a_spam_filter( $spam ) {
foreach ( $bad_words as $bad_word ) {
if ( false !== stripos( $message_compressed, str_replace( ' ', '', strtolower( $bad_word ) ) ) ) {

$spam_score += $score_bad_string;
$spam_score += $score_bad_string;
$reason['bad_word'][] = $bad_word;
}
}
Expand Down Expand Up @@ -937,7 +945,7 @@ public function cf7a_spam_filter( $spam ) {
foreach ( $options['dnsbl_list'] as $dnsbl ) {
if ( $this->cf7a_check_dnsbl( $reverse_ip, $dnsbl ) ) {
$reason['dsnbl'][] = $dnsbl;
$spam_score += $score_dnsbl;
$spam_score += $score_dnsbl;
}
// if ( $this->cf7a_check_emailbl( $dnsbl ) ) {
// $reason['dsnbl'][] = $dnsbl;
Expand Down Expand Up @@ -981,7 +989,7 @@ public function cf7a_spam_filter( $spam ) {

/* check only if it's set and if it is different from "" */
if ( $has_honeypot ) {
$spam_score += $score_honeypot;
$spam_score += $score_honeypot;
$reason['honeypot'][] = $input_names[ $i ];
}
}
Expand Down Expand Up @@ -1020,7 +1028,7 @@ public function cf7a_spam_filter( $spam ) {
if ( $rating >= $b8_threshold ) {

$reason['b8'] = $rating;
$spam_score += $score_detection;
$spam_score += $score_detection;

cf7a_log( "B8 rating $rating / 1", 1 );
}
Expand Down
26 changes: 13 additions & 13 deletions core/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,25 @@ function( $res, $el ) {
}
if ( strlen( $el ) >= 5 ) {
/* split into key: language , value: locale */
$l = explode( '-', $el );
$res[ 'languages' ][] = $l[0] ;
$res[ 'locales' ][] = $l[1] ;
} else if ( strlen( $el ) === 2 && ctype_alpha( $el ) ) {
$l = explode( '-', $el );
$res['languages'][] = $l[0];
$res['locales'][] = $l[1];
} elseif ( strlen( $el ) === 2 && ctype_alpha( $el ) ) {
/* otherwise keep key:language, value: '' (any locale) */
if ( ctype_lower( $el ) ) {
$res[ 'languages' ][] = $el ;
$res['languages'][] = $el;
} elseif ( ctype_upper( $el ) ) {
$res[ 'locales' ][] = $el ;
$res['locales'][] = $el;
}
}
return $res;
},
array()
);

if (! empty($result)) {
$result['languages'] = array_values(array_unique($result['languages'])) ;
$result['locales'] = array_values(array_unique($result['locales'])) ;
if ( ! empty( $result ) ) {
$result['languages'] = array_values( array_unique( $result['languages'] ) );
$result['locales'] = array_values( array_unique( $result['locales'] ) );
}

return $result;
Expand Down Expand Up @@ -203,7 +203,7 @@ function get_honeypot_input_names( $custom_names = array() ) {
* It encrypts a string using the WordPress salt as the key
*
* @param string|int $value The value to encrypt.
* @param string $cipher The cipher method to use.
* @param string $cipher The cipher method to use.
*
* @return string The encrypted value.
*/
Expand Down Expand Up @@ -314,7 +314,7 @@ function cf7a_format_status( $rank ) {
* key/value pair separated by a semicolon and a space
*
* @param array $array - the array of reasons to ban.
* @param bool $is_html - true to return a html string.
* @param bool $is_html - true to return a html string.
*
* @return false|string Compress arrays into "key:value; " pair
*/
Expand Down Expand Up @@ -346,7 +346,7 @@ function ( $v, $k ) use ( $is_html ) {
* on, then log the string
*
* @param string|array $log_data - The string/array to log.
* @param numeric $log_level 0 = log always, 1 = logging, 2 = only extended logging.
* @param numeric $log_level 0 = log always, 1 = logging, 2 = only extended logging.
*
* @return void
*/
Expand Down Expand Up @@ -381,7 +381,7 @@ function cf7a_get_mail_meta( $tag ) {
* If the message tag contains a space, it's a multiple meta tag,
* so split it up and return the value of the meta tag
*
* @param array $posted_data The form data array.
* @param array $posted_data The form data array.
* @param string $message_tag The tag of the field you want to retrieve.
* @param string $explode_pattern Used to split multiple cf7 user tags .
*
Expand Down
4 changes: 2 additions & 2 deletions engine/CF7_AntiSpam_Activator.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static function init_vars() {
'dnsbl_list' => array(),
'honeypot_input_names' => array(),
'honeyform_excluded_pages' => array(),
'languages_locales' => array(
'languages_locales' => array(
'allowed' => array(),
'disallowed' => array(),
),
Expand Down Expand Up @@ -142,7 +142,7 @@ public static function init_vars() {
'billing_country',
'email-address',
),
'languages_locales' => array(
'languages_locales' => array(
'allowed' => isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] )
? cf7a_get_browser_languages_locales_array( sanitize_text_field( wp_unslash( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ) )
: array(),
Expand Down

0 comments on commit 5b1af7d

Please sign in to comment.