| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
die( 'You are not allowed to call this page directly.' ); |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* Class FrmAntiSpam. |
| 8 |
* |
| 9 |
* This token class generates tokens that are used in our Anti-Spam checking. |
| 10 |
* |
| 11 |
* @since 4.11 |
| 12 |
*/ |
| 13 |
class FrmAntiSpam extends FrmValidate { |
| 14 |
|
| 15 |
/** |
| 16 |
* @return string |
| 17 |
*/ |
| 18 |
protected function get_option_key() { |
| 19 |
return 'antispam'; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* @param int $form_id |
| 24 |
* |
| 25 |
* @return void |
| 26 |
*/ |
| 27 |
public static function maybe_init( $form_id ) { |
| 28 |
$antispam = new self( $form_id ); |
| 29 |
if ( $antispam->run_antispam() ) { |
| 30 |
$antispam->init(); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Initialise the actions for the Anti-spam. |
| 36 |
* |
| 37 |
* @since 4.11 |
| 38 |
* |
| 39 |
* @return void |
| 40 |
*/ |
| 41 |
public function init() { |
| 42 |
add_filter( 'frm_form_attributes', array( $this, 'add_token_to_form' ), 10, 1 ); |
| 43 |
add_filter( 'frm_form_div_attributes', array( $this, 'add_token_to_form' ), 10, 1 ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Return a valid token. |
| 48 |
* |
| 49 |
* @since 4.11 |
| 50 |
* |
| 51 |
* @param mixed $current True to use current time, otherwise a timestamp string. |
| 52 |
* |
| 53 |
* @return string Token. |
| 54 |
*/ |
| 55 |
private function get( $current = true ) { |
| 56 |
// If $current was not passed, or it is true, we use the current timestamp. |
| 57 |
// If $current was passed in as a string, we'll use that passed in timestamp. |
| 58 |
if ( $current !== true ) { |
| 59 |
$time = $current; |
| 60 |
} else { |
| 61 |
$time = time(); |
| 62 |
} |
| 63 |
|
| 64 |
// Format the timestamp to be less exact, as we want to deal in days. |
| 65 |
// June 19th, 2020 would get formatted as: 1906202017125. |
| 66 |
// Day of the month, month number, year, day number of the year, week number of the year. |
| 67 |
$token_date = gmdate( 'dmYzW', $time ); |
| 68 |
|
| 69 |
// Combine our token date and our token salt, and md5 it. |
| 70 |
$form_token_string = md5( $token_date . $this->get_antispam_secret_key() ); |
| 71 |
|
| 72 |
return $form_token_string; |
| 73 |
} |
| 74 |
|
| 75 |
private function get_antispam_secret_key() { |
| 76 |
$secret_key = get_option( 'frm_antispam_secret_key' ); |
| 77 |
|
| 78 |
// If we already have the secret, send it back. |
| 79 |
if ( false !== $secret_key ) { |
| 80 |
return base64_decode( $secret_key ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
| 81 |
} |
| 82 |
|
| 83 |
// We don't have a secret, so let's generate one. |
| 84 |
$secret_key = is_callable( 'sodium_crypto_secretbox_keygen' ) ? sodium_crypto_secretbox_keygen() : wp_generate_password( 32, true, true ); |
| 85 |
add_option( 'frm_antispam_secret_key', base64_encode( $secret_key ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
| 86 |
|
| 87 |
return $secret_key; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Generate the array of valid tokens to check for. These include two days |
| 92 |
* before the current date to account for long cache times. |
| 93 |
* |
| 94 |
* These two filters are available if a user wants to extend the times. |
| 95 |
* 'frm_form_token_check_before_today' |
| 96 |
* 'frm_form_token_check_after_today' |
| 97 |
* |
| 98 |
* @since 4.11 |
| 99 |
* |
| 100 |
* @return array Array of all valid tokens to check against. |
| 101 |
*/ |
| 102 |
private function get_valid_tokens() { |
| 103 |
$current_date = time(); |
| 104 |
|
| 105 |
// Create our array of times to check before today. A user with a longer |
| 106 |
// cache time can extend this. A user with a shorter cache time can remove times. |
| 107 |
$valid_token_times_before = apply_filters( |
| 108 |
'frm_form_token_check_before_today', |
| 109 |
array( |
| 110 |
// Two days ago. |
| 111 |
2 * DAY_IN_SECONDS, |
| 112 |
// One day ago. |
| 113 |
1 * DAY_IN_SECONDS, |
| 114 |
) |
| 115 |
); |
| 116 |
|
| 117 |
// Mostly to catch edge cases like the form page loading and submitting on two different days. |
| 118 |
// This probably won't be filtered by users too much, but they could extend it. |
| 119 |
$valid_token_times_after = apply_filters( |
| 120 |
'frm_form_token_check_after_today', |
| 121 |
array( |
| 122 |
// Add in 45 minutes past today to catch some midnight edge cases. |
| 123 |
45 * MINUTE_IN_SECONDS, |
| 124 |
) |
| 125 |
); |
| 126 |
|
| 127 |
// Built up our valid tokens. |
| 128 |
$valid_tokens = array(); |
| 129 |
|
| 130 |
// Add in all the previous times we check. |
| 131 |
foreach ( $valid_token_times_before as $time ) { |
| 132 |
$valid_tokens[] = $this->get( $current_date - $time ); |
| 133 |
} |
| 134 |
|
| 135 |
// Add in our current date. |
| 136 |
$valid_tokens[] = $this->get( $current_date ); |
| 137 |
|
| 138 |
// Add in the times after our check. |
| 139 |
foreach ( $valid_token_times_after as $time ) { |
| 140 |
$valid_tokens[] = $this->get( $current_date + $time ); |
| 141 |
} |
| 142 |
|
| 143 |
return $valid_tokens; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Check if the given token is valid or not. |
| 148 |
* |
| 149 |
* Tokens are valid for some period of time (see frm_token_validity_in_hours |
| 150 |
* and frm_token_validity_in_days to extend the validation period). |
| 151 |
* By default tokens are valid for day. |
| 152 |
* |
| 153 |
* @since 4.11 |
| 154 |
* |
| 155 |
* @param string $token Token to validate. |
| 156 |
* |
| 157 |
* @return bool Whether the token is valid or not. |
| 158 |
*/ |
| 159 |
private function verify( $token ) { |
| 160 |
// Check to see if our token is inside of the valid tokens. |
| 161 |
return in_array( $token, $this->get_valid_tokens(), true ); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Add the token field to the form. |
| 166 |
* |
| 167 |
* @since 4.11 |
| 168 |
* |
| 169 |
* @param string $attributes |
| 170 |
* |
| 171 |
* @return string |
| 172 |
*/ |
| 173 |
public function add_token_to_form( $attributes ) { |
| 174 |
$attributes .= ' data-token="' . esc_attr( $this->get() ) . '"'; |
| 175 |
return $attributes; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* @param int $form_id |
| 180 |
* |
| 181 |
* @return void |
| 182 |
*/ |
| 183 |
public static function maybe_echo_token( $form_id ) { |
| 184 |
$antispam = new self( $form_id ); |
| 185 |
if ( $antispam->run_antispam() ) { |
| 186 |
echo 'data-token="' . esc_attr( $antispam->get() ) . '"'; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* @return bool |
| 192 |
*/ |
| 193 |
public function run_antispam() { |
| 194 |
return $this->is_option_on() && apply_filters( 'frm_run_antispam', true, $this->form_id ); |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Validate Anti-spam if enabled. |
| 199 |
* |
| 200 |
* @since 4.11 |
| 201 |
* |
| 202 |
* @return bool|string True or a string with the error. |
| 203 |
*/ |
| 204 |
public function validate() { |
| 205 |
if ( ! $this->run_antispam() ) { |
| 206 |
return true; |
| 207 |
} |
| 208 |
|
| 209 |
$token = FrmAppHelper::get_param( 'antispam_token', '', 'post', 'sanitize_text_field' ); |
| 210 |
|
| 211 |
// If the antispam setting is enabled and we don't have a token, bail. |
| 212 |
if ( ! $token ) { |
| 213 |
if ( FrmAppHelper::is_admin_page( 'formidable-entries' ) ) { |
| 214 |
// add an exception for the entries page. |
| 215 |
return true; |
| 216 |
} |
| 217 |
return $this->process_antispam_filter( $this->get_missing_token_message() ); |
| 218 |
} |
| 219 |
|
| 220 |
// Verify the token. |
| 221 |
if ( ! $this->verify( $token ) ) { |
| 222 |
return $this->process_antispam_filter( $this->get_invalid_token_message() ); |
| 223 |
} |
| 224 |
|
| 225 |
return $this->process_antispam_filter( true ); |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Helper to run our filter on all the responses for the antispam checks. |
| 230 |
* |
| 231 |
* @since 4.11 |
| 232 |
* |
| 233 |
* @param bool|string $is_valid Is valid entry or not. |
| 234 |
* |
| 235 |
* @return bool|string Is valid or message. |
| 236 |
*/ |
| 237 |
private function process_antispam_filter( $is_valid ) { |
| 238 |
return apply_filters( 'frm_process_antispam', $is_valid ); |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Helper to get the missing token message. |
| 243 |
* |
| 244 |
* @since 4.11 |
| 245 |
* |
| 246 |
* @return string missing token message. |
| 247 |
*/ |
| 248 |
private function get_missing_token_message() { |
| 249 |
return esc_html__( 'This page isn\'t loading JavaScript properly, and the form will not be able to submit.', 'formidable' ) . $this->maybe_get_support_text(); |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Helper to get the invalid token message. |
| 254 |
* |
| 255 |
* @since 4.11 |
| 256 |
* |
| 257 |
* @return string Invalid token message. |
| 258 |
*/ |
| 259 |
private function get_invalid_token_message() { |
| 260 |
return esc_html__( 'Form token is invalid. Please refresh the page.', 'formidable' ) . $this->maybe_get_support_text(); |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* If a user is a super admin, add a support link to the message. |
| 265 |
* |
| 266 |
* @since 4.11 |
| 267 |
* |
| 268 |
* @return string Support text if super admin, empty string if not. |
| 269 |
*/ |
| 270 |
private function maybe_get_support_text() { |
| 271 |
// If user isn't a super admin, don't return any text. |
| 272 |
if ( ! is_super_admin() ) { |
| 273 |
return ''; |
| 274 |
} |
| 275 |
|
| 276 |
// If the user is an admin, return text with a link to support. |
| 277 |
// We add a space here to separate the sentences, but outside of the localized |
| 278 |
// text to avoid it being removed. |
| 279 |
return ' ' . sprintf( |
| 280 |
// translators: %1$s start link, %2$s end link. |
| 281 |
esc_html__( 'Please check out our %1$stroubleshooting guide%2$s for details on resolving this issue.', 'formidable' ), |
| 282 |
'<a href="https://formidableforms.com/knowledgebase/add-spam-protection/">', |
| 283 |
'</a>' |
| 284 |
); |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* Clear third party cache plugins to avoid data-tokens missing or appearing when the antispam setting is changed. |
| 289 |
* |
| 290 |
* @return void |
| 291 |
*/ |
| 292 |
public static function clear_caches() { |
| 293 |
self::clear_w3_total_cache(); |
| 294 |
self::clear_wp_fastest_cache(); |
| 295 |
self::clear_wp_super_cache(); |
| 296 |
self::clear_wp_optimize(); |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* @return void |
| 301 |
*/ |
| 302 |
private static function clear_w3_total_cache() { |
| 303 |
if ( is_callable( 'w3tc_flush_all' ) ) { |
| 304 |
w3tc_flush_all(); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* @return void |
| 310 |
*/ |
| 311 |
private static function clear_wp_fastest_cache() { |
| 312 |
do_action( 'wpfc_clear_all_cache' ); |
| 313 |
} |
| 314 |
|
| 315 |
/** |
| 316 |
* @return void |
| 317 |
*/ |
| 318 |
private static function clear_wp_super_cache() { |
| 319 |
if ( function_exists( 'wp_cache_clean_cache' ) ) { |
| 320 |
global $file_prefix; |
| 321 |
wp_cache_clean_cache( $file_prefix, true ); |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
/** |
| 326 |
* @return void |
| 327 |
*/ |
| 328 |
private static function clear_wp_optimize() { |
| 329 |
if ( class_exists( 'WP_Optimize' ) ) { |
| 330 |
WP_Optimize()->get_page_cache()->purge(); |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
|