Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/wpforms-.../src/Forms
File: Token.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace WPForms\Forms;
[2] Fix | Delete
[3] Fix | Delete
/**
[4] Fix | Delete
* Class Token.
[5] Fix | Delete
*
[6] Fix | Delete
* This token class generates tokens that are used in our Anti-Spam checking mechanism.
[7] Fix | Delete
*
[8] Fix | Delete
* @since 1.6.2
[9] Fix | Delete
*/
[10] Fix | Delete
class Token {
[11] Fix | Delete
[12] Fix | Delete
/**
[13] Fix | Delete
* Initialise the actions for the Anti-spam.
[14] Fix | Delete
*
[15] Fix | Delete
* @since 1.6.2
[16] Fix | Delete
*/
[17] Fix | Delete
public function init() {
[18] Fix | Delete
[19] Fix | Delete
$this->hooks();
[20] Fix | Delete
}
[21] Fix | Delete
[22] Fix | Delete
/**
[23] Fix | Delete
* Register hooks.
[24] Fix | Delete
*
[25] Fix | Delete
* @since 1.6.2
[26] Fix | Delete
*/
[27] Fix | Delete
public function hooks() {
[28] Fix | Delete
[29] Fix | Delete
add_filter( 'wpforms_frontend_form_atts', [ $this, 'add_token_to_form_atts' ], 10, 2 );
[30] Fix | Delete
add_filter( 'wpforms_frontend_strings', [ $this, 'add_frontend_strings' ] );
[31] Fix | Delete
add_action( 'wp_ajax_nopriv_wpforms_get_token', [ $this, 'ajax_get_token' ] );
[32] Fix | Delete
add_action( 'wp_ajax_wpforms_get_token', [ $this, 'ajax_get_token' ] );
[33] Fix | Delete
}
[34] Fix | Delete
[35] Fix | Delete
/**
[36] Fix | Delete
* Return a valid token.
[37] Fix | Delete
*
[38] Fix | Delete
* @since 1.6.2
[39] Fix | Delete
* @since 1.7.1 Added the $form_data argument.
[40] Fix | Delete
*
[41] Fix | Delete
* @param mixed $current True to use current time, otherwise a timestamp string.
[42] Fix | Delete
* @param array $form_data Form data and settings.
[43] Fix | Delete
*
[44] Fix | Delete
* @return string Token.
[45] Fix | Delete
*/
[46] Fix | Delete
public function get( $current = true, $form_data = [] ) {
[47] Fix | Delete
[48] Fix | Delete
// If $current was not passed, or it is true, we use the current timestamp.
[49] Fix | Delete
// If $current was passed in as a string, we'll use that passed in timestamp.
[50] Fix | Delete
if ( $current !== true ) {
[51] Fix | Delete
$time = $current;
[52] Fix | Delete
} else {
[53] Fix | Delete
$time = time();
[54] Fix | Delete
}
[55] Fix | Delete
[56] Fix | Delete
// Format the timestamp to be less exact, as we want to deal in days.
[57] Fix | Delete
// June 19th, 2020 would get formatted as: 1906202017125.
[58] Fix | Delete
// Day of the month, month number, year, day number of the year, week number of the year.
[59] Fix | Delete
$token_data = gmdate( 'dmYzW', $time );
[60] Fix | Delete
[61] Fix | Delete
if ( ! empty( $form_data['id'] ) ) {
[62] Fix | Delete
$token_data .= "::{$form_data['id']}";
[63] Fix | Delete
}
[64] Fix | Delete
[65] Fix | Delete
// Combine our token date and our token salt, and md5 it.
[66] Fix | Delete
return md5( $token_data . \WPForms\Helpers\Crypto::get_secret_key() );
[67] Fix | Delete
}
[68] Fix | Delete
[69] Fix | Delete
/**
[70] Fix | Delete
* Generate the array of valid tokens to check for. These include two days
[71] Fix | Delete
* before the current date to account for long cache times.
[72] Fix | Delete
*
[73] Fix | Delete
* These two filters are available if a user wants to extend the times.
[74] Fix | Delete
* 'wpforms_form_token_check_before_today'
[75] Fix | Delete
* 'wpforms_form_token_check_after_today'
[76] Fix | Delete
*
[77] Fix | Delete
* @since 1.6.2
[78] Fix | Delete
* @since 1.7.1 Added the $form_data argument.
[79] Fix | Delete
*
[80] Fix | Delete
* @param array $form_data Form data and settings.
[81] Fix | Delete
*
[82] Fix | Delete
* @return array Array of all valid tokens to check against.
[83] Fix | Delete
*/
[84] Fix | Delete
public function get_valid_tokens( $form_data = [] ) {
[85] Fix | Delete
[86] Fix | Delete
$current_date = time();
[87] Fix | Delete
[88] Fix | Delete
$valid_token_times_before = [];
[89] Fix | Delete
[90] Fix | Delete
$days_in_5_years = 5 * 365;
[91] Fix | Delete
[92] Fix | Delete
// Create an array of 5 years worth of days.
[93] Fix | Delete
for ( $i = 1; $i <= $days_in_5_years; $i++ ) {
[94] Fix | Delete
$valid_token_times_before[] = $i * DAY_IN_SECONDS;
[95] Fix | Delete
}
[96] Fix | Delete
[97] Fix | Delete
// Create our array of times to check before today. A user with a longer
[98] Fix | Delete
// cache time can extend this. A user with a shorter cache time can remove times.
[99] Fix | Delete
$valid_token_times_before = apply_filters(
[100] Fix | Delete
'wpforms_form_token_check_before_today',
[101] Fix | Delete
$valid_token_times_before
[102] Fix | Delete
);
[103] Fix | Delete
[104] Fix | Delete
// Mostly to catch edge cases like the form page loading and submitting on two different days.
[105] Fix | Delete
// This probably won't be filtered by users too much, but they could extend it.
[106] Fix | Delete
$valid_token_times_after = apply_filters(
[107] Fix | Delete
'wpforms_form_token_check_after_today',
[108] Fix | Delete
[
[109] Fix | Delete
( 45 * MINUTE_IN_SECONDS ), // Add in 45 minutes past today to catch some midnight edge cases.
[110] Fix | Delete
]
[111] Fix | Delete
);
[112] Fix | Delete
[113] Fix | Delete
// Built up our valid tokens.
[114] Fix | Delete
$valid_tokens = [];
[115] Fix | Delete
[116] Fix | Delete
// Add in all the previous times we check.
[117] Fix | Delete
foreach ( $valid_token_times_before as $time ) {
[118] Fix | Delete
$valid_tokens[] = $this->get( $current_date - $time, $form_data );
[119] Fix | Delete
}
[120] Fix | Delete
[121] Fix | Delete
// Add in our current date.
[122] Fix | Delete
$valid_tokens[] = $this->get( $current_date, $form_data );
[123] Fix | Delete
[124] Fix | Delete
// Add in the times after our check.
[125] Fix | Delete
foreach ( $valid_token_times_after as $time ) {
[126] Fix | Delete
$valid_tokens[] = $this->get( $current_date + $time, $form_data );
[127] Fix | Delete
}
[128] Fix | Delete
[129] Fix | Delete
return $valid_tokens;
[130] Fix | Delete
}
[131] Fix | Delete
[132] Fix | Delete
/**
[133] Fix | Delete
* Check if the given token is valid or not.
[134] Fix | Delete
*
[135] Fix | Delete
* Tokens are valid for some period of time (see wpforms_token_validity_in_hours
[136] Fix | Delete
* and wpforms_token_validity_in_days to extend the validation period).
[137] Fix | Delete
* By default tokens are valid for day.
[138] Fix | Delete
*
[139] Fix | Delete
* @since 1.6.2
[140] Fix | Delete
* @since 1.7.1 Added the $form_data argument.
[141] Fix | Delete
*
[142] Fix | Delete
* @param string $token Token to validate.
[143] Fix | Delete
* @param array $form_data Form data and settings.
[144] Fix | Delete
*
[145] Fix | Delete
* @return bool Whether the token is valid or not.
[146] Fix | Delete
*/
[147] Fix | Delete
public function verify( string $token, array $form_data = [] ): bool {
[148] Fix | Delete
[149] Fix | Delete
// Check to see if our token is inside the valid tokens.
[150] Fix | Delete
return in_array( $token, $this->get_valid_tokens( $form_data ), true );
[151] Fix | Delete
}
[152] Fix | Delete
[153] Fix | Delete
/**
[154] Fix | Delete
* Add the token to the form attributes.
[155] Fix | Delete
*
[156] Fix | Delete
* @since 1.6.2
[157] Fix | Delete
* @since 1.7.1 Added the $form_data argument.
[158] Fix | Delete
*
[159] Fix | Delete
* @param array $attrs Form attributes.
[160] Fix | Delete
* @param array $form_data Form data and settings.
[161] Fix | Delete
*
[162] Fix | Delete
* @return array Form attributes.
[163] Fix | Delete
*/
[164] Fix | Delete
public function add_token_to_form_atts( array $attrs, array $form_data ) {
[165] Fix | Delete
[166] Fix | Delete
$attrs['atts']['data-token'] = $this->get( true, $form_data );
[167] Fix | Delete
$attrs['atts']['data-token-time'] = time();
[168] Fix | Delete
[169] Fix | Delete
return $attrs;
[170] Fix | Delete
}
[171] Fix | Delete
[172] Fix | Delete
/**
[173] Fix | Delete
* Validate Anti-spam if enabled.
[174] Fix | Delete
*
[175] Fix | Delete
* @since 1.6.2
[176] Fix | Delete
*
[177] Fix | Delete
* @param array $form_data Form data.
[178] Fix | Delete
* @param array $fields Fields.
[179] Fix | Delete
* @param array $entry Form entry.
[180] Fix | Delete
*
[181] Fix | Delete
* @return bool|string True or a string with the error.
[182] Fix | Delete
*/
[183] Fix | Delete
public function validate( array $form_data, array $fields, array $entry ) {
[184] Fix | Delete
[185] Fix | Delete
// Bail out if we don't have the antispam setting.
[186] Fix | Delete
if ( empty( $form_data['settings']['antispam'] ) ) {
[187] Fix | Delete
return true;
[188] Fix | Delete
}
[189] Fix | Delete
[190] Fix | Delete
// Bail out if the antispam setting isn't enabled.
[191] Fix | Delete
if ( $form_data['settings']['antispam'] !== '1' ) {
[192] Fix | Delete
return true;
[193] Fix | Delete
}
[194] Fix | Delete
[195] Fix | Delete
$is_valid_token = isset( $entry['token'] ) && $this->verify( (string) $entry['token'], $form_data );
[196] Fix | Delete
[197] Fix | Delete
if ( $this->process_antispam_filter_wrapper( $is_valid_token, $fields, $entry, $form_data ) ) {
[198] Fix | Delete
return true;
[199] Fix | Delete
}
[200] Fix | Delete
[201] Fix | Delete
// Prepare the log data.
[202] Fix | Delete
$form_title = $form_data['settings']['form_title'] ?? '';
[203] Fix | Delete
$form_id = $form_data['id'] ?? 'unknown';
[204] Fix | Delete
[205] Fix | Delete
if ( $is_valid_token ) {
[206] Fix | Delete
// Token is OK, but antispam filter is not passed.
[207] Fix | Delete
$log_message = 'Filter is not passed';
[208] Fix | Delete
$error_message = $this->get_antispam_filter_message();
[209] Fix | Delete
} else {
[210] Fix | Delete
// Invalid token.
[211] Fix | Delete
$log_message = 'Token is invalid';
[212] Fix | Delete
$error_message = $this->get_invalid_token_message();
[213] Fix | Delete
}
[214] Fix | Delete
[215] Fix | Delete
wpforms_log(
[216] Fix | Delete
'Antispam: ' . $log_message,
[217] Fix | Delete
[
[218] Fix | Delete
'message' => $error_message,
[219] Fix | Delete
'referer' => esc_url_raw( (string) wp_get_referer() ),
[220] Fix | Delete
'form' => ! empty( $form_title ) ? $form_title . ' (ID: ' . $form_id . ')' : 'ID: ' . $form_id,
[221] Fix | Delete
'token' => $entry['token'] ?? '',
[222] Fix | Delete
'user_ip' => wpforms_get_ip(),
[223] Fix | Delete
'entry_data' => ! wpforms_setting( 'gdpr' ) ? $entry : 'Not logged',
[224] Fix | Delete
],
[225] Fix | Delete
[
[226] Fix | Delete
'type' => [ 'spam', 'error' ],
[227] Fix | Delete
'form_id' => $form_data['id'],
[228] Fix | Delete
'force' => true,
[229] Fix | Delete
]
[230] Fix | Delete
);
[231] Fix | Delete
[232] Fix | Delete
return $error_message;
[233] Fix | Delete
}
[234] Fix | Delete
[235] Fix | Delete
/**
[236] Fix | Delete
* Helper to run our filter on all the responses for the antispam checks.
[237] Fix | Delete
*
[238] Fix | Delete
* @since 1.6.2
[239] Fix | Delete
*
[240] Fix | Delete
* @param bool $is_valid_not_spam Is valid entry or not.
[241] Fix | Delete
* @param array $fields Form Fields.
[242] Fix | Delete
* @param array $entry Form entry.
[243] Fix | Delete
* @param array $form_data Form Data.
[244] Fix | Delete
*
[245] Fix | Delete
* @return bool Is valid or not.
[246] Fix | Delete
*/
[247] Fix | Delete
public function process_antispam_filter_wrapper( bool $is_valid_not_spam, array $fields, array $entry, array $form_data ): bool {
[248] Fix | Delete
[249] Fix | Delete
/**
[250] Fix | Delete
* Allows developers to filter the antispam check result.
[251] Fix | Delete
*
[252] Fix | Delete
* @since 1.6.2
[253] Fix | Delete
*
[254] Fix | Delete
* @param bool $is_valid_not_spam True if entry valid, false otherwise.
[255] Fix | Delete
* @param array $fields Fields data.
[256] Fix | Delete
* @param array $entry Entry data.
[257] Fix | Delete
* @param array $form_data Form data.
[258] Fix | Delete
*/
[259] Fix | Delete
return (bool) apply_filters( 'wpforms_process_antispam', $is_valid_not_spam, $fields, $entry, $form_data ); // phpcs:ignore WPForms.PHP.ValidateHooks.InvalidHookName
[260] Fix | Delete
}
[261] Fix | Delete
[262] Fix | Delete
/**
[263] Fix | Delete
* Helper to get the invalid token message.
[264] Fix | Delete
*
[265] Fix | Delete
* @since 1.6.2.1
[266] Fix | Delete
*
[267] Fix | Delete
* @return string Invalid token message.
[268] Fix | Delete
*/
[269] Fix | Delete
private function get_invalid_token_message(): string {
[270] Fix | Delete
[271] Fix | Delete
return $this->get_error_message( esc_html__( 'Antispam token is invalid.', 'wpforms-lite' ) );
[272] Fix | Delete
}
[273] Fix | Delete
[274] Fix | Delete
/**
[275] Fix | Delete
* Helper to get the antispam filter error message.
[276] Fix | Delete
*
[277] Fix | Delete
* @since 1.8.9
[278] Fix | Delete
*
[279] Fix | Delete
* @return string Missing token message.
[280] Fix | Delete
*/
[281] Fix | Delete
private function get_antispam_filter_message(): string {
[282] Fix | Delete
[283] Fix | Delete
return $this->get_error_message( esc_html__( 'Antispam filter did not allow your data to pass through.', 'wpforms-lite' ) );
[284] Fix | Delete
}
[285] Fix | Delete
[286] Fix | Delete
/**
[287] Fix | Delete
* Get error message depends on user.
[288] Fix | Delete
*
[289] Fix | Delete
* @since 1.6.4.1
[290] Fix | Delete
*
[291] Fix | Delete
* @param string $text Message text.
[292] Fix | Delete
*
[293] Fix | Delete
* @return string
[294] Fix | Delete
*/
[295] Fix | Delete
private function get_error_message( string $text ): string {
[296] Fix | Delete
[297] Fix | Delete
$text .= ' ' . esc_html__( 'Please reload the page and try submitting the form again.', 'wpforms-lite' );
[298] Fix | Delete
[299] Fix | Delete
return wpforms_current_user_can() ? $text . $this->maybe_get_support_text() : $text;
[300] Fix | Delete
}
[301] Fix | Delete
[302] Fix | Delete
/**
[303] Fix | Delete
* If a user is a super admin, add a support link to the message.
[304] Fix | Delete
*
[305] Fix | Delete
* @since 1.6.2.1
[306] Fix | Delete
*
[307] Fix | Delete
* @return string Support text if super admin, empty string if not.
[308] Fix | Delete
*/
[309] Fix | Delete
private function maybe_get_support_text(): string {
[310] Fix | Delete
[311] Fix | Delete
// If a user isn't a super admin, don't return any text.
[312] Fix | Delete
if ( ! is_super_admin() ) {
[313] Fix | Delete
return '';
[314] Fix | Delete
}
[315] Fix | Delete
[316] Fix | Delete
// If the user is an admin, return text with a link to support.
[317] Fix | Delete
// We add a space here to separate the sentences, but outside the localized text to avoid it being removed.
[318] Fix | Delete
return ' ' . sprintf(
[319] Fix | Delete
/* translators: placeholders are links. */
[320] Fix | Delete
esc_html__( 'Please check out our %1$stroubleshooting guide%2$s for details on resolving this issue.', 'wpforms-lite' ),
[321] Fix | Delete
'<a href="https://wpforms.com/docs/getting-support-wpforms/">',
[322] Fix | Delete
'</a>'
[323] Fix | Delete
);
[324] Fix | Delete
}
[325] Fix | Delete
[326] Fix | Delete
/**
[327] Fix | Delete
* Add token related strings to the frontend.
[328] Fix | Delete
*
[329] Fix | Delete
* @since 1.8.8
[330] Fix | Delete
*
[331] Fix | Delete
* @param array|mixed $strings Frontend strings.
[332] Fix | Delete
*
[333] Fix | Delete
* @return array Frontend strings.
[334] Fix | Delete
*/
[335] Fix | Delete
public function add_frontend_strings( $strings ): array {
[336] Fix | Delete
[337] Fix | Delete
$strings = (array) $strings;
[338] Fix | Delete
[339] Fix | Delete
$strings['error_updating_token'] = esc_html__(
[340] Fix | Delete
'Error updating token. Please try again or contact support if the issue persists.',
[341] Fix | Delete
'wpforms-lite'
[342] Fix | Delete
);
[343] Fix | Delete
$strings['network_error'] = esc_html__(
[344] Fix | Delete
'Network error or server is unreachable. Check your connection or try again later.',
[345] Fix | Delete
'wpforms-lite'
[346] Fix | Delete
);
[347] Fix | Delete
[348] Fix | Delete
// Default token lifetime is 24 hours in seconds.
[349] Fix | Delete
$token_lifetime = DAY_IN_SECONDS;
[350] Fix | Delete
[351] Fix | Delete
/**
[352] Fix | Delete
* Filter token cache lifetime in seconds.
[353] Fix | Delete
*
[354] Fix | Delete
* @since 1.8.8
[355] Fix | Delete
*
[356] Fix | Delete
* @param integer $token_lifetime Token lifetime in seconds.
[357] Fix | Delete
*/
[358] Fix | Delete
$strings['token_cache_lifetime'] = apply_filters( 'wpforms_forms_token_cache_lifetime', $token_lifetime );
[359] Fix | Delete
[360] Fix | Delete
return $strings;
[361] Fix | Delete
}
[362] Fix | Delete
[363] Fix | Delete
/**
[364] Fix | Delete
* Update token via ajax handler.
[365] Fix | Delete
*
[366] Fix | Delete
* @since 1.8.8
[367] Fix | Delete
*/
[368] Fix | Delete
public function ajax_get_token() {
[369] Fix | Delete
[370] Fix | Delete
$form_data = [];
[371] Fix | Delete
$form_data['id'] = filter_input( INPUT_POST, 'formId', FILTER_VALIDATE_INT );
[372] Fix | Delete
[373] Fix | Delete
$response = [
[374] Fix | Delete
'token' => $this->get( true, $form_data ),
[375] Fix | Delete
];
[376] Fix | Delete
[377] Fix | Delete
wp_send_json_success( $response );
[378] Fix | Delete
}
[379] Fix | Delete
}
[380] Fix | Delete
[381] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function