| 1 |
<?php |
| 2 |
// @codingStandardsIgnoreFile |
| 3 |
/** |
| 4 |
* Captcha utilities. |
| 5 |
* |
| 6 |
* Copyright: © 2009-2011 |
| 7 |
* {@link http://websharks-inc.com/ WebSharks, Inc.} |
| 8 |
* (coded in the USA) |
| 9 |
* |
| 10 |
* Released under the terms of the GNU General Public License. |
| 11 |
* You should have received a copy of the GNU General Public License, |
| 12 |
* along with this software. In the main directory, see: /licensing/ |
| 13 |
* If not, see: {@link http://www.gnu.org/licenses/}. |
| 14 |
* |
| 15 |
* @package s2Member\Utilities |
| 16 |
* @since 3.5 |
| 17 |
*/ |
| 18 |
if(!defined('WPINC')) // MUST have WordPress. |
| 19 |
exit ('Do not access this file directly.'); |
| 20 |
|
| 21 |
if (!class_exists ('c_ws_plugin__s2member_utils_captchas')) |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Captcha utilities. |
| 25 |
* |
| 26 |
* @package s2Member\Utilities |
| 27 |
* @since 3.5 |
| 28 |
*/ |
| 29 |
class c_ws_plugin__s2member_utils_captchas |
| 30 |
{ |
| 31 |
/** |
| 32 |
* Which reCAPTCHA™ version. |
| 33 |
* |
| 34 |
* @package s2Member\Utilities |
| 35 |
* @since 150717 |
| 36 |
* |
| 37 |
* @return string The version number. |
| 38 |
*/ |
| 39 |
public static function recaptcha_version() |
| 40 |
{ |
| 41 |
return apply_filters('ws_plugin__s2member_recaptcha_version', '1', get_defined_vars()); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Public/private keys to use for reCAPTCHA™. |
| 46 |
* |
| 47 |
* @package s2Member\Utilities |
| 48 |
* @since 111203 |
| 49 |
* |
| 50 |
* @return array An array with with two elements: `public` and `private`. |
| 51 |
*/ |
| 52 |
public static function recaptcha_keys() |
| 53 |
{ |
| 54 |
// NOTE: Version 2 keys are only possible w/ s2Member Pro filters. |
| 55 |
|
| 56 |
$public = $GLOBALS['WS_PLUGIN__']['s2member']['c']['recaptcha']['public_key']; |
| 57 |
$private = $GLOBALS['WS_PLUGIN__']['s2member']['c']['recaptcha']['private_key']; |
| 58 |
|
| 59 |
return apply_filters('ws_plugin__s2member_recaptcha_keys', array('public' => $public, 'private' => $private), get_defined_vars ()); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* reCAPTCHA™ post vars. |
| 64 |
* |
| 65 |
* @package s2Member\Utilities |
| 66 |
* @since 150717 |
| 67 |
* |
| 68 |
* @param array $post_vars Existing post vars array. |
| 69 |
* |
| 70 |
* @return array Post vars array, with reCAPTCHA™ challenge/response. |
| 71 |
*/ |
| 72 |
public static function recaptcha_post_vars($post_vars = array()) |
| 73 |
{ |
| 74 |
$post_vars = (array)$post_vars; // Force array. |
| 75 |
|
| 76 |
if(self::recaptcha_version() === '2') |
| 77 |
{ |
| 78 |
$post_vars['g-recaptcha-response'] = isset($_POST['g-recaptcha-response']) ? trim(stripslashes((string)$_POST['g-recaptcha-response'])) : ''; |
| 79 |
$post_vars['recaptcha_challenge_field'] = $post_vars['recaptcha_response_field'] = $post_vars['g-recaptcha-response']; // Compatibility. |
| 80 |
|
| 81 |
return apply_filters('ws_plugin__s2member_recaptcha_post_vars', $post_vars, get_defined_vars()); |
| 82 |
} |
| 83 |
$post_vars['recaptcha_challenge_field'] = isset($_POST['recaptcha_challenge_field']) ? trim(stripslashes((string)$_POST['recaptcha_challenge_field'])) : ''; |
| 84 |
$post_vars['recaptcha_response_field'] = isset($_POST['recaptcha_response_field']) ? trim(stripslashes((string)$_POST['recaptcha_response_field'])) : ''; |
| 85 |
|
| 86 |
return apply_filters('ws_plugin__s2member_recaptcha_post_vars', $post_vars, get_defined_vars()); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Verifies a reCAPTCHA™ code via Google. |
| 91 |
* |
| 92 |
* @package s2Member\Utilities |
| 93 |
* @since 3.5 |
| 94 |
* |
| 95 |
* @param string $challenge The value of `recaptcha_challenge_field` during form submisson. |
| 96 |
* @param string $response The value of `recaptcha_response_field` during form submission. |
| 97 |
* @return bool True if ``$response`` is valid, else false. |
| 98 |
*/ |
| 99 |
public static function recaptcha_code_validates($challenge = '', $response = '') |
| 100 |
{ |
| 101 |
$keys = c_ws_plugin__s2member_utils_captchas::recaptcha_keys(); |
| 102 |
|
| 103 |
if(self::recaptcha_version() === '2') // New API verifier. |
| 104 |
{ |
| 105 |
$api_post_vars = array('secret' => $keys['private'], 'response' => $response, 'remoteip' => c_ws_plugin__s2member_utils_ip::current()); |
| 106 |
$api_response = c_ws_plugin__s2member_utils_urls::remote('https://www.google.com/recaptcha/api/siteverify', $api_post_vars); |
| 107 |
$api_response = json_decode($api_response); |
| 108 |
|
| 109 |
return is_object($api_response) && !empty($api_response->success); |
| 110 |
} |
| 111 |
else // Old API call; note that this is NOT over SSL for some reason. |
| 112 |
{ |
| 113 |
$api_post_vars = array('privatekey' => $keys['private'], 'challenge' => $challenge, 'response' => $response, 'remoteip' => c_ws_plugin__s2member_utils_ip::current()); |
| 114 |
$api_response = c_ws_plugin__s2member_utils_urls::remote ('http://www.google.com/recaptcha/api/verify', $api_post_vars); |
| 115 |
|
| 116 |
return preg_match('/^true/i', trim($api_response)); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Builds a reCAPTCHA™ JavaScript `script` tag for display. |
| 122 |
* |
| 123 |
* @package s2Member\Utilities |
| 124 |
* @since 3.5 |
| 125 |
* |
| 126 |
* @param string $theme Optional. The theme used in display. Defaults to `clean`. |
| 127 |
* @param string $tabindex Optional. Value of `tabindex=""` attribute. Defaults to `-1`. |
| 128 |
* @param string $error Optional. An error message to display. |
| 129 |
* @return string HTML markup for JavaScript tag. |
| 130 |
*/ |
| 131 |
public static function recaptcha_script_tag($theme = '', $tabindex = '', $error = '') |
| 132 |
{ |
| 133 |
$theme = $theme ? $theme : 'clean'; |
| 134 |
$tabindex = strlen($tabindex) ? (int)$tabindex : -1; |
| 135 |
$keys = c_ws_plugin__s2member_utils_captchas::recaptcha_keys(); |
| 136 |
|
| 137 |
if(self::recaptcha_version() === '2') // New API verifier. |
| 138 |
{ |
| 139 |
$theme = !$theme || in_array($theme, array('red', 'white', 'clean', 'blackglass'), TRUE) ? 'light' : $theme; |
| 140 |
$locale = get_locale(); |
| 141 |
$lang_code = substr($locale, 0, 2); |
| 142 |
return '<div class="g-recaptcha" data-sitekey="'.esc_attr($keys['public']).'" data-size="normal" data-theme="'.esc_attr($theme).'" data-tabindex="'.esc_attr($tabindex).'"></div>'. |
| 143 |
'<script src="https://www.google.com/recaptcha/api.js?hl='.esc_attr($lang_code).'"></script>'; //250606 Added the site's language. |
| 144 |
} |
| 145 |
$options = '<script type="text/javascript">'."if(typeof RecaptchaOptions !== 'object'){ var RecaptchaOptions = {theme: '".c_ws_plugin__s2member_utils_strings::esc_js_sq($theme)."', lang: '".c_ws_plugin__s2member_utils_strings::esc_js_sq($GLOBALS['WS_PLUGIN__']['s2member']['c']['recaptcha']['lang'])."', tabindex: ".$tabindex." }; }".'</script>'."\n"; |
| 146 |
$no_tabindex_icons = '<script type="text/javascript">'."if(typeof jQuery === 'function'){ jQuery('td a[id^=\"recaptcha\"]').removeAttr('tabindex'); }".'</script>'; |
| 147 |
$adjustments = !apply_filters('c_ws_plugin__s2member_utils_tabindex_recaptcha_icons', false, get_defined_vars ()) ? $no_tabindex_icons : ''; |
| 148 |
|
| 149 |
return $options.'<script type="text/javascript" src="'.esc_attr('https://www.google.com/recaptcha/api/challenge?k='.urlencode($keys['public'])).($error ? '&error='.urlencode($error) : '').'"></script>'.$adjustments; |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|