diff --git a/README.md b/README.md index 1f56019d1a03ecc016cfd671527c9782c550c4e4..f21ffcc83cfe425da95c37f8034924996ba3f1f6 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ | ---------- | ------------------------------------------------------------ | | 中文名称 | 腾讯云验证码(CAPTCHA)插件 | | 英文名称 | tencentcloud-captcha | -| 最新版本 | v1.0.1 (2020.12.11) | +| 最新版本 | v1.0.2 (2021.8.20) | | 适用平台 | [WordPress](https://wordpress.org/) | | 适用产品 | [腾讯云验证码](https://cloud.tencent.com/document/product/1110/36334) | | GitHub项目 | [tencentcloud-wordpress-plugin-captcha](https://github.com/Tencent-Cloud-Plugins/tencentcloud-wordpress-plugin-captcha) | @@ -30,7 +30,9 @@ - 支持找回密码表单增加验证码 - +- 支持配置页面验证码测试功能 + +- 支持本地调试日志功能 ## 3.安装指引 @@ -80,7 +82,9 @@ > 评论页面开启验证码效果。 - + + + > 配置页面测试验证码功能,并提供本地调试日志功能。 ### 4.2. 名词解释 @@ -106,12 +110,16 @@ ## 7.GitHub版本迭代记录 - +### 2021.8.20 tencentcloud-wordpress-plugin-captcha v1.0.2 +- 支持PHP8并兼容WordPress5.8 +- 支持配置页面进行验证码测试 +- 新增本地调试日志功能 ### 2020.12.11 tencentcloud-wordpress-plugin-captcha v1.0.1 - 支持在windows环境下运行 ### 2020.6.23 tencentcloud-wordpress-plugin-captcha v1.0.0 + - 支持在注册表单中增加验证码 - 支持在评论表单中增加验证码 - 支持用户自定义业务场景 diff --git a/images/captcha6.png b/images/captcha6.png new file mode 100644 index 0000000000000000000000000000000000000000..6f6f65c4456cb187f4d7f4d3bcd2f70ec3dd52a8 Binary files /dev/null and b/images/captcha6.png differ diff --git a/tencentcloud-captcha/TencentCloudCaptchaActions.php b/tencentcloud-captcha/TencentCloudCaptchaActions.php index fb8385d98525bd9a9a5cc4a37e66fd78e21ece9d..1ddd3f633937a8fcbf77812f824a3c4fad3c355b 100644 --- a/tencentcloud-captcha/TencentCloudCaptchaActions.php +++ b/tencentcloud-captcha/TencentCloudCaptchaActions.php @@ -15,6 +15,7 @@ * limitations under the License. */ if (!is_file(TENCENT_WORDPRESS_CAPTCHA_DIR . 'vendor/autoload.php')) { + CaptchaDebugLog::writeDebugLog('error', 'msg : ' . '缺少依赖文件,请先执行composer install', __FILE__, __LINE__); wp_die('缺少依赖文件,请先执行composer install', '缺少依赖文件', array('back_link' => true)); } require_once 'vendor/autoload.php'; @@ -28,9 +29,11 @@ use TencentCloud\Captcha\V20190722\CaptchaClient; use TencentCloud\Captcha\V20190722\Models\DescribeCaptchaResultRequest; -class TencentCloudCaptchaActions{ +class TencentCloudCaptchaActions +{ const TENCENT_WORDPRESS_CAPTCHA_OPTIONS = 'tencent_wordpress_captcha_options'; const TENCENT_WORDPRESS_CAPTCHA_LOGIN_NEED_CODE = 'login_need_code'; + const TENCENT_WORDPRESS_CAPTCHA_DEBUG_NEED_CODE = 'debug_need_code'; const TENCENT_WORDPRESS_CAPTCHA_CODE_FREE = 'code_free'; const TENCENT_WORDPRESS_CAPTCHA_APP_ID = 'captcha_app_id'; const TENCENT_WORDPRESS_CAPTCHA_REGISTER_APP_ID = 'captcha_register_app_id'; @@ -48,24 +51,38 @@ class TencentCloudCaptchaActions{ const TENCENT_WORDPRESS_CAPTCHA_SECRET_CUSTOM = 'secret_custom'; const TENCENT_WORDPRESS_CAPTCHA_PLUGIN_TYPE = 'captcha'; - - + public static function tencentCaptchaSetingNotice() + { + if (isset($GLOBALS['_REQUEST']) && isset($GLOBALS['_REQUEST']['page']) + && $GLOBALS['_REQUEST']['page'] === 'tencent_wordpress_plugin_captcha') { + $captchaOptions = get_option(self::TENCENT_WORDPRESS_CAPTCHA_OPTIONS); + if (isset($captchaOptions['activation']) && $captchaOptions['activation'] === true) { + $chosen = '腾讯云验证码插件启用生效中'; + } else { + $chosen = '腾讯云验证码插件启用中'; + } + echo '
' . $chosen . '
+@@ -81,7 +98,8 @@ class TencentCloudCaptchaActions{ /** * 注册表单增加验证码 */ - public function tencent_wordpress_captcha_registerForm(){ + public function tencentCaptchaRegisterForm() + { $CodeVerifyOptions = get_option(self::TENCENT_WORDPRESS_CAPTCHA_OPTIONS); @@ -108,10 +126,11 @@ class TencentCloudCaptchaActions{ /** * 评论表单增加验证码 - * @param $submit_button 评论按钮HTML + * @param $submitButton 评论按钮HTML * @return string */ - public function tencent_wordpress_captcha_commentForm($submit_button){ + public function tencentCaptchaCommentForm($submitButton) + { $CodeVerifyOptions = get_option(self::TENCENT_WORDPRESS_CAPTCHA_OPTIONS); $commentNeedCode = $CodeVerifyOptions[self::TENCENT_WORDPRESS_CAPTCHA_COMMENT_NEED_CODE]; if ($commentNeedCode == '2') { @@ -122,21 +141,22 @@ class TencentCloudCaptchaActions{ } else { $codAppId = sanitize_text_field($CodeVerifyOptions[self::TENCENT_WORDPRESS_CAPTCHA_COMMENT_APP_ID]) ?: $CodeVerifyOptions[self::TENCENT_WORDPRESS_CAPTCHA_APP_ID]; } - $submit_button = '
+ $submitButton = '
-
' . $submit_button; - return $submit_button; + ' . $submitButton; + return $submitButton; } else { - return $submit_button; + return $submitButton; } } /** * 找回密码增加验证码字段 */ - public function tencent_wordpress_captcha_lostpasswordForm(){ + public function tencentCaptchaLostpasswordForm() + { $CodeVerifyOptions = get_option(self::TENCENT_WORDPRESS_CAPTCHA_OPTIONS); $lostpasswordNeedCode = $CodeVerifyOptions[self::TENCENT_WORDPRESS_CAPTCHA_LOSTPASSWORD_NEED_CODE]; if ($lostpasswordNeedCode == '2') { @@ -160,16 +180,18 @@ class TencentCloudCaptchaActions{ /** * 插件菜单设置 */ - public function tencent_wordpress_captcha_pluginSettingPage(){ + public function tencentCaptchaPluginSettingPage() + { TencentWordpressPluginsSettingActions::AddTencentWordpressCommonSettingPage(); - $pagehook = add_submenu_page('TencentWordpressPluginsCommonSettingPage','验证码','验证码', 'manage_options', 'tencent_wordpress_plugin_captcha', array('TencentCloudCaptchaActions', 'tencent_wordpress_captcha_SettingPage')); - add_action( 'admin_print_styles-'.$pagehook, array(new TencentCloudCaptchaActions(), 'tencent_wordpress_captcha_loadCssForPage')); + $pagehook = add_submenu_page('TencentWordpressPluginsCommonSettingPage', '验证码', '验证码', 'manage_options', 'tencent_wordpress_plugin_captcha', array('TencentCloudCaptchaActions', 'tencentCaptchaSettingPage')); + add_action('admin_print_styles-' . $pagehook, array(new TencentCloudCaptchaActions(), 'tencentCaptchaLoadCssForPage')); } /** * 插件配置信息操作页面 */ - public static function tencent_wordpress_captcha_SettingPage(){ + public static function tencentCaptchaSettingPage() + { include TENCENT_WORDPRESS_CAPTCHA_DIR . 'tencentcloud-captcha-setting-page.php'; } @@ -179,7 +201,8 @@ class TencentCloudCaptchaActions{ * @param $file * @return mixed */ - public function tencent_wordpress_captcha_pluginSettingPageLinkButton($links, $file){ + public function tencentCaptchaPluginSettingPageLinkButton($links, $file) + { if ($file == plugin_basename(TENCENT_WORDPRESS_CAPTCHA_DIR . 'tencentcloud-captcha.php')) { $links[] = '设置'; } @@ -190,7 +213,8 @@ class TencentCloudCaptchaActions{ /** * 在文章页面加载JS脚本 */ - public function tencent_wordpress_captcha_loadScriptForPage(){ + public function tencentCaptchaLoadScriptForPage() + { if (is_single() || is_paged()) { wp_register_script('codeVerify_front_user_script', TENCENT_WORDPRESS_CAPTCHA_JS_DIR . 'tencent_cloud_captcha_user.js', array('jquery'), '2.1', true); wp_enqueue_script('codeVerify_front_user_script'); @@ -199,14 +223,16 @@ class TencentCloudCaptchaActions{ } } - public function tencent_wordpress_captcha_loadCssForPage(){ - wp_enqueue_style('codeVerify_admin_css',TENCENT_WORDPRESS_CAPTCHA_CSS_DIR.'bootstrap.min.css'); + public function tencentCaptchaLoadCssForPage() + { + wp_enqueue_style('codeVerify_admin_css', TENCENT_WORDPRESS_CAPTCHA_CSS_DIR . 'bootstrap.min.css'); } /** * 加载js脚本 */ - public function tencent_wordpress_captcha_loadMyScriptEnqueue(){ + public function tencentCaptchaLoadMyScriptEnqueue() + { wp_register_script('codeVerify_front_user_script', TENCENT_WORDPRESS_CAPTCHA_JS_DIR . 'tencent_cloud_captcha_user.js', array('jquery'), '2.1', true); wp_enqueue_script('codeVerify_front_user_script'); wp_register_script('codeVerify_back_admin_script', TENCENT_WORDPRESS_CAPTCHA_JS_DIR . 'tencent_cloud_captcha_admin.js', array('jquery'), '2.1', true); @@ -230,10 +256,11 @@ class TencentCloudCaptchaActions{ * @param $secretCustom 自定义密钥 * @return bool|string */ - public static function tencent_wordpress_captcha_checkMustParams($secretID, $secretKey, $codeAppId, $codeSecretKey, - $codeFree, $registerAppId, $registerAppKey, - $commentAppId, $commentAppKey, $lostpasswordAppId, - $lostpasswordAppKey, $secretCustom){ + public static function tencentCaptchaCheckMustParams($secretID, $secretKey, $codeAppId, $codeSecretKey, + $codeFree, $registerAppId, $registerAppKey, + $commentAppId, $commentAppKey, $lostpasswordAppId, + $lostpasswordAppKey, $secretCustom) + { if ($secretCustom == '2') { if (empty($secretID)) { return 'Secret Id未填写.'; @@ -266,8 +293,10 @@ class TencentCloudCaptchaActions{ /** * 保存插件配置 */ - public function tencent_wordpress_captcha_updateCaptchaSettings(){ + public function tencentCaptchaUpdateCaptchaSettings() + { if (!current_user_can('manage_options')) { + CaptchaDebugLog::writeDebugLog('error', 'msg : 当前用户无权限!', __FILE__, __LINE__); wp_send_json_error(array('msg' => '当前用户无权限.')); } $CodeVerifySettings[self::TENCENT_WORDPRESS_CAPTCHA_SECRET_ID] = sanitize_text_field($_POST['secret_id']); @@ -277,6 +306,7 @@ class TencentCloudCaptchaActions{ $CodeVerifySettings[self::TENCENT_WORDPRESS_CAPTCHA_REGISTER_NEED_CODE] = sanitize_text_field($_POST['registerNeedCode']); $CodeVerifySettings[self::TENCENT_WORDPRESS_CAPTCHA_COMMENT_NEED_CODE] = sanitize_text_field($_POST['commentNeedCode']); $CodeVerifySettings[self::TENCENT_WORDPRESS_CAPTCHA_LOGIN_NEED_CODE] = sanitize_text_field($_POST['loginNeedCode']); + $CodeVerifySettings[self::TENCENT_WORDPRESS_CAPTCHA_DEBUG_NEED_CODE] = sanitize_text_field($_POST['debugNeedCode']); $CodeVerifySettings[self::TENCENT_WORDPRESS_CAPTCHA_LOSTPASSWORD_NEED_CODE] = sanitize_text_field($_POST['lostpasswordNeedCode']); $CodeVerifySettings[self::TENCENT_WORDPRESS_CAPTCHA_CODE_FREE] = sanitize_text_field($_POST['codeFree']); $CodeVerifySettings[self::TENCENT_WORDPRESS_CAPTCHA_SECRET_CUSTOM] = sanitize_text_field($_POST['secretCustom']); @@ -303,17 +333,18 @@ class TencentCloudCaptchaActions{ } - $checkResult = self::tencent_wordpress_captcha_checkMustParams($CodeVerifySettings[self::TENCENT_WORDPRESS_CAPTCHA_SECRET_ID], $CodeVerifySettings[self::TENCENT_WORDPRESS_CAPTCHA_SECRET_KEY], $CodeVerifySettings[self::TENCENT_WORDPRESS_CAPTCHA_APP_ID], + $checkResult = self::tencentCaptchaCheckMustParams($CodeVerifySettings[self::TENCENT_WORDPRESS_CAPTCHA_SECRET_ID], $CodeVerifySettings[self::TENCENT_WORDPRESS_CAPTCHA_SECRET_KEY], $CodeVerifySettings[self::TENCENT_WORDPRESS_CAPTCHA_APP_ID], $CodeVerifySettings[self::TENCENT_WORDPRESS_CAPTCHA_APP_KEY], $CodeVerifySettings[self::TENCENT_WORDPRESS_CAPTCHA_CODE_FREE], $CodeVerifySettings[self::TENCENT_WORDPRESS_CAPTCHA_REGISTER_APP_ID], $CodeVerifySettings[self::TENCENT_WORDPRESS_CAPTCHA_REGISTER_APP_KEY], $CodeVerifySettings[self::TENCENT_WORDPRESS_CAPTCHA_COMMENT_APP_ID], $CodeVerifySettings[self::TENCENT_WORDPRESS_CAPTCHA_COMMENT_APP_KEY] , $CodeVerifySettings[self::TENCENT_WORDPRESS_CAPTCHA_LOSTPASSWORD_APP_ID], $CodeVerifySettings[self::TENCENT_WORDPRESS_CAPTCHA_LOSTPASSWORD_APP_KEY], $CodeVerifySettings[self::TENCENT_WORDPRESS_CAPTCHA_SECRET_CUSTOM]); if ($checkResult !== true) { + CaptchaDebugLog::writeDebugLog('error', 'msg : ' . $checkResult, __FILE__, __LINE__); wp_send_json_error(array('msg' => $checkResult)); } update_option(self::TENCENT_WORDPRESS_CAPTCHA_OPTIONS, $CodeVerifySettings, true); //发送用户体验数据 - $static_data = self::getTencentCloudWordPressStaticData('save_config'); - TencentWordpressPluginsSettingActions::sendUserExperienceInfo($static_data); + $staticData = self::getTencentCloudWordPressStaticData('save_config'); + TencentWordpressPluginsSettingActions::sendUserExperienceInfo($staticData); wp_send_json_success(array('msg' => '保存成功')); } @@ -323,7 +354,8 @@ class TencentCloudCaptchaActions{ * @param $users 用户 * @return WP_Error 验证错误 */ - public function tencent_wordpress_capthca_loginCodeVerify($users){ + public function tencentCapthcaLoginCodeVerify($users) + { if (!empty($_POST)) { $CodeVerifyOptions = get_option(self::TENCENT_WORDPRESS_CAPTCHA_OPTIONS); $loginNeedCode = $CodeVerifyOptions[self::TENCENT_WORDPRESS_CAPTCHA_LOGIN_NEED_CODE]; @@ -331,6 +363,7 @@ class TencentCloudCaptchaActions{ $ticket = sanitize_text_field($_POST['codeVerifyTicket']); $randStr = sanitize_text_field($_POST['codeVerifyRandstr']); if (empty($ticket) || empty($randStr)) { + CaptchaDebugLog::writeDebugLog('error', 'msg : ticket or randStr is null!', __FILE__, __LINE__); return new WP_Error( 'invalid_CodeVerify', __('未通过人机验证.') @@ -347,11 +380,12 @@ class TencentCloudCaptchaActions{ $codeAppKey = $CodeVerifyOptions[self::TENCENT_WORDPRESS_CAPTCHA_APP_KEY]; } $verifyCode = self::verifyCodeReal($ticket, $randStr, $codeAppId, $codeAppKey); - if ($verifyCode['CaptchaCode'] != 1) { + if (!$verifyCode || $verifyCode['CaptchaCode'] != 1) { $errormessage = '未通过人机验证.'; - if (!empty($verifyCode['errorMessage'])) { + if (isset($verifyCode['errorMessage']) && !empty($verifyCode['errorMessage'])) { $errormessage = $errormessage . $verifyCode['errorMessage']; } + CaptchaDebugLog::writeDebugLog('error', 'msg : ' . $errormessage, __FILE__, __LINE__); return new WP_Error( 'invalid_CodeVerify', __('未通过人机验证.') @@ -373,7 +407,8 @@ class TencentCloudCaptchaActions{ * @param $errors 异常 * @return mixed */ - public function tencent_wordpress_captcha_registerCodeVerify($login, $email, $errors){ + public function tencentCaptchaRegisterCodeVerify($login, $email, $errors) + { if (!empty($_POST)) { $CodeVerifyOptions = get_option(self::TENCENT_WORDPRESS_CAPTCHA_OPTIONS); $registerNeedCode = $CodeVerifyOptions[self::TENCENT_WORDPRESS_CAPTCHA_REGISTER_NEED_CODE]; @@ -381,6 +416,7 @@ class TencentCloudCaptchaActions{ $ticket = sanitize_text_field($_POST['codeVerifyTicket']); $randStr = sanitize_text_field($_POST['codeVerifyRandstr']); if (empty($ticket) || empty($randStr)) { + CaptchaDebugLog::writeDebugLog('error', 'msg : ticket or randStr is null!', __FILE__, __LINE__); $errors->add('未通过人机验证.', __('未通过人机验证.', 'wpcaptchadomain')); return $errors; } @@ -395,12 +431,13 @@ class TencentCloudCaptchaActions{ $codeAppId = $CodeVerifyOptions[self::TENCENT_WORDPRESS_CAPTCHA_APP_ID]; $codeAppKey = $CodeVerifyOptions[self::TENCENT_WORDPRESS_CAPTCHA_APP_KEY]; } - $verifyCode = self::verifyCodeReal( $ticket, $randStr, $codeAppId, $codeAppKey); - if ($verifyCode['CaptchaCode'] != 1) { + $verifyCode = self::verifyCodeReal($ticket, $randStr, $codeAppId, $codeAppKey); + if (!$verifyCode || $verifyCode['CaptchaCode'] != 1) { $errormessage = '未通过人机验证.'; - if (!empty($verifyCode['errorMessage'])) { + if (isset($verifyCode['errorMessage']) && !empty($verifyCode['errorMessage'])) { $errormessage = $errormessage . $verifyCode['errorMessage']; } + CaptchaDebugLog::writeDebugLog('error', 'msg : 未通过人机验证.' . $errormessage, __FILE__, __LINE__); $errors->add('未通过人机验证.', __($errormessage, 'wpcaptchadomain')); return $errors; } @@ -413,7 +450,8 @@ class TencentCloudCaptchaActions{ * 忘记密码时验证码验证 * */ - public function tencent_wordpress_captcha_lostpasswordCodeVerify(){ + public function tencentCaptchaLostpasswordCodeVerify() + { if (!empty($_POST)) { $CodeVerifyOptions = get_option(self::TENCENT_WORDPRESS_CAPTCHA_OPTIONS); $lostpasswordNeedCode = $CodeVerifyOptions[self::TENCENT_WORDPRESS_CAPTCHA_LOSTPASSWORD_NEED_CODE]; @@ -421,6 +459,7 @@ class TencentCloudCaptchaActions{ $ticket = sanitize_text_field($_POST['codeVerifyTicket']); $randStr = sanitize_text_field($_POST['codeVerifyRandstr']); if (empty($ticket) || empty($randStr)) { + CaptchaDebugLog::writeDebugLog('error', 'msg : ticket or randStr is null!', __FILE__, __LINE__); $error = new WP_Error( 'invalid_CodeVerify', __('未通过人机验证.') @@ -438,15 +477,16 @@ class TencentCloudCaptchaActions{ $codeAppKey = $CodeVerifyOptions[self::TENCENT_WORDPRESS_CAPTCHA_APP_KEY]; } $verifyCode = self::verifyCodeReal($ticket, $randStr, $codeAppId, $codeAppKey); - if ($verifyCode['CaptchaCode'] != 1) { + if (!$verifyCode || $verifyCode['CaptchaCode'] != 1) { $errormessage = '未通过人机验证.'; - if (!empty($verifyCode['errorMessage'])) { + if (isset($verifyCode['errorMessage']) && !empty($verifyCode['errorMessage'])) { $errormessage = $errormessage . $verifyCode['errorMessage']; } $error = new WP_Error( 'invalid_CodeVerify', __('未通过人机验证.') ); + CaptchaDebugLog::writeDebugLog('error', 'msg : ' . $errormessage, __FILE__, __LINE__); wp_die($error, '未通过人机验证.', array('back_link' => true)); } } @@ -459,11 +499,12 @@ class TencentCloudCaptchaActions{ * @param $comment 评论信息 * @return mixed */ - public function tencent_wordpress_captcha_commentCodeVerify($comment){ + public function tencentCaptchaCommentCodeVerify($comment) + { $user = wp_get_current_user(); // 管理员后台回复评论时无需验证 $allowed_roles = array('editor', 'administrator', 'author'); - if(array_intersect($allowed_roles, $user->roles ) ) { + if (array_intersect($allowed_roles, $user->roles)) { return $comment; } $CodeVerifyOptions = get_option(self::TENCENT_WORDPRESS_CAPTCHA_OPTIONS); @@ -476,6 +517,7 @@ class TencentCloudCaptchaActions{ 'need_authenticated_code', __('请先进行人机验证.') ); + CaptchaDebugLog::writeDebugLog('error', 'msg : ticket or randStr is null!', __FILE__, __LINE__); wp_die($error, '验证码不能为空', array('back_link' => true)); } @@ -490,15 +532,16 @@ class TencentCloudCaptchaActions{ $codeAppKey = $CodeVerifyOptions[self::TENCENT_WORDPRESS_CAPTCHA_APP_KEY]; } $verifyCode = self::verifyCodeReal($ticket, $randStr, $codeAppId, $codeAppKey); - if ($verifyCode['CaptchaCode'] != 1) { + if (!$verifyCode || $verifyCode['CaptchaCode'] != 1) { $errormessage = '验证码验证失败.'; - if (!empty($verifyCode['errorMessage'])) { + if (isset($verifyCode['errorMessage']) && !empty($verifyCode['errorMessage'])) { $errormessage = $errormessage . $verifyCode['errorMessage']; } $error = new WP_Error( 'authenticated_fail', __('验证码验证失败.') ); + CaptchaDebugLog::writeDebugLog('error', 'msg : ' . $errormessage, __FILE__, __LINE__); wp_die($error, '验证码验证失败,请重新验证', array('back_link' => true)); } return $comment; @@ -517,12 +560,12 @@ class TencentCloudCaptchaActions{ * @param $codeSecretKey 验证码应用蜜月 * @return array|mixed */ - public static function verifyCodeReal( $ticket, $randStr, $codeAppId, $codeSecretKey){ - + public static function verifyCodeReal($ticket, $randStr, $codeAppId, $codeSecretKey) + { try { $secretID = self::getSecretID(); $secretKey = self::getSecretKey(); - $remote_ip = preg_replace('/[^0-9a-fA-F:., ]/', '', $_SERVER['REMOTE_ADDR']); + $remoteIp = preg_replace('/[^0-9a-fA-F:., ]/', '', $_SERVER['REMOTE_ADDR']); $cred = new Credential($secretID, $secretKey); $httpProfile = new HttpProfile(); $httpProfile->setEndpoint("captcha.tencentcloudapi.com"); @@ -530,12 +573,13 @@ class TencentCloudCaptchaActions{ $clientProfile->setHttpProfile($httpProfile); $client = new CaptchaClient($cred, "", $clientProfile); $req = new DescribeCaptchaResultRequest(); - $params = array('CaptchaType' => 9, 'Ticket' => $ticket, 'Randstr' => $randStr, 'CaptchaAppId' => intval($codeAppId), 'AppSecretKey' => $codeSecretKey, 'UserIp' => $remote_ip); + $params = array('CaptchaType' => 9, 'Ticket' => $ticket, 'Randstr' => $randStr, 'CaptchaAppId' => intval($codeAppId), 'AppSecretKey' => $codeSecretKey, 'UserIp' => $remoteIp); $req->fromJsonString(json_encode($params)); $resp = $client->DescribeCaptchaResult($req); return json_decode($resp->toJsonString(), JSON_OBJECT_AS_ARRAY); } catch (TencentCloudSDKException $e) { - return array('requestId' => $e->getRequestId(), 'errorCode' => $e->getErrorCode(), 'errorMessage' => $e->getMessage()); + CaptchaDebugLog::writeDebugLog('error', 'msg : ' . $e->getMessage(), __FILE__, __LINE__); + return false; } } @@ -543,13 +587,18 @@ class TencentCloudCaptchaActions{ * 获取SecrtId * @return mixed */ - private static function getSecretID(){ + private static function getSecretID() + { $tecentCaptchaOptinos = get_option(self::TENCENT_WORDPRESS_CAPTCHA_OPTIONS); - if (sanitize_text_field($tecentCaptchaOptinos[self::TENCENT_WORDPRESS_CAPTCHA_SECRET_CUSTOM]) == '2'){ + if (sanitize_text_field($tecentCaptchaOptinos[self::TENCENT_WORDPRESS_CAPTCHA_SECRET_CUSTOM]) == '2') { return $tecentCaptchaOptinos[self::TENCENT_WORDPRESS_CAPTCHA_SECRET_ID]; - }else{ + } else { $commonOptinos = get_option('tencent_wordpress_common_options'); - return $commonOptinos[self::TENCENT_WORDPRESS_CAPTCHA_SECRET_ID]; + if (isset($commonOptinos[self::TENCENT_WORDPRESS_CAPTCHA_SECRET_ID])){ + return $commonOptinos[self::TENCENT_WORDPRESS_CAPTCHA_SECRET_ID]; + } + CaptchaDebugLog::writeDebugLog('error', 'msg : SecretID is null', __FILE__, __LINE__); + return ''; } } @@ -558,13 +607,18 @@ class TencentCloudCaptchaActions{ * 获取SecrtKey * @return mixed */ - private static function getSecretKey(){ + private static function getSecretKey() + { $tecentCaptchaOptinos = get_option(self::TENCENT_WORDPRESS_CAPTCHA_OPTIONS); - if (sanitize_text_field($tecentCaptchaOptinos[self::TENCENT_WORDPRESS_CAPTCHA_SECRET_CUSTOM]) == '2'){ + if (sanitize_text_field($tecentCaptchaOptinos[self::TENCENT_WORDPRESS_CAPTCHA_SECRET_CUSTOM]) == '2') { return $tecentCaptchaOptinos[self::TENCENT_WORDPRESS_CAPTCHA_SECRET_KEY]; - }else{ + } else { $commonOptinos = get_option('tencent_wordpress_common_options'); - return $commonOptinos[self::TENCENT_WORDPRESS_CAPTCHA_SECRET_KEY]; + if (isset($commonOptinos[self::TENCENT_WORDPRESS_CAPTCHA_SECRET_KEY])){ + return $commonOptinos[self::TENCENT_WORDPRESS_CAPTCHA_SECRET_KEY]; + } + CaptchaDebugLog::writeDebugLog('error', 'msg : SecretKey is null', __FILE__, __LINE__); + return ''; } } @@ -572,8 +626,9 @@ class TencentCloudCaptchaActions{ /** * 开启插件 */ - public static function tencent_wordpress_captcha_activatePlugin(){ - $init_options = array( + public static function tencentCaptchaActivatePlugin() + { + $initOptions = array( 'activation' => false, self::TENCENT_WORDPRESS_CAPTCHA_SECRET_ID => "", self::TENCENT_WORDPRESS_CAPTCHA_SECRET_KEY => "", @@ -581,6 +636,7 @@ class TencentCloudCaptchaActions{ self::TENCENT_WORDPRESS_CAPTCHA_APP_KEY => '', self::TENCENT_WORDPRESS_CAPTCHA_LOGIN_NEED_CODE => '', self::TENCENT_WORDPRESS_CAPTCHA_REGISTER_NEED_CODE => '', + self::TENCENT_WORDPRESS_CAPTCHA_DEBUG_NEED_CODE => '', self::TENCENT_WORDPRESS_CAPTCHA_COMMENT_NEED_CODE => '', self::TENCENT_WORDPRESS_CAPTCHA_LOSTPASSWORD_NEED_CODE => '', self::TENCENT_WORDPRESS_CAPTCHA_REGISTER_APP_ID => '', @@ -592,12 +648,12 @@ class TencentCloudCaptchaActions{ self::TENCENT_WORDPRESS_CAPTCHA_CODE_FREE => '', self::TENCENT_WORDPRESS_CAPTCHA_SECRET_CUSTOM => '' ); - $tencent_wordpress_captcha_options = get_option(self::TENCENT_WORDPRESS_CAPTCHA_OPTIONS); - if (empty($tencent_wordpress_captcha_options)) { - add_option(self::TENCENT_WORDPRESS_CAPTCHA_OPTIONS, $init_options); + $captchaOptions = get_option(self::TENCENT_WORDPRESS_CAPTCHA_OPTIONS); + if (empty($captchaOptions)) { + add_option(self::TENCENT_WORDPRESS_CAPTCHA_OPTIONS, $initOptions); } else { - $tencent_wordpress_captcha_options = array_merge($init_options, $tencent_wordpress_captcha_options); - update_option(self::TENCENT_WORDPRESS_CAPTCHA_OPTIONS, $tencent_wordpress_captcha_options); + $captchaOptions = array_merge($initOptions, $captchaOptions); + update_option(self::TENCENT_WORDPRESS_CAPTCHA_OPTIONS, $captchaOptions); } $plugin = array( @@ -614,28 +670,30 @@ class TencentCloudCaptchaActions{ // 第一次开启插件则生成一个全站唯一的站点id,保存在公共的option中 TencentWordpressPluginsSettingActions::setWordPressSiteID(); //发送用户体验数据 - $static_data = self::getTencentCloudWordPressStaticData('activate','','','',''); - TencentWordpressPluginsSettingActions::sendUserExperienceInfo($static_data); + $staticData = self::getTencentCloudWordPressStaticData('activate', '', '', '', ''); + TencentWordpressPluginsSettingActions::sendUserExperienceInfo($staticData); } /** * 禁止插件 */ - public static function tencent_wordpress_captcha_deactivePlugin(){ - $tencent_wordpress_captcha_options = get_option(self::TENCENT_WORDPRESS_CAPTCHA_OPTIONS); - if (!empty($tencent_wordpress_captcha_options) && isset($tencent_wordpress_captcha_options['activation'])) { - $tencent_wordpress_captcha_options['activation'] = false; - update_option(self::TENCENT_WORDPRESS_CAPTCHA_OPTIONS, $tencent_wordpress_captcha_options); + public static function tencentCaptchaDeactivePlugin() + { + $captchaOptions = get_option(self::TENCENT_WORDPRESS_CAPTCHA_OPTIONS); + if (!empty($captchaOptions) && isset($captchaOptions['activation'])) { + $captchaOptions['activation'] = false; + update_option(self::TENCENT_WORDPRESS_CAPTCHA_OPTIONS, $captchaOptions); } TencentWordpressPluginsSettingActions::disableTencentWordpressPlugin(TENCENT_WORDPRESS_CAPTCHA_SHOW_NAME); - $static_data = self::getTencentCloudWordPressStaticData('deactivate','','','',''); - TencentWordpressPluginsSettingActions::sendUserExperienceInfo($static_data); + $staticData = self::getTencentCloudWordPressStaticData('deactivate', '', '', '', ''); + TencentWordpressPluginsSettingActions::sendUserExperienceInfo($staticData); } /** * 插件初始化 */ - public static function tencent_wordpress_captcha_init(){ + public static function tencentCaptchaInit() + { if (class_exists('TencentWordpressPluginsSettingActions')) { TencentWordpressPluginsSettingActions::init(); } @@ -644,42 +702,131 @@ class TencentCloudCaptchaActions{ public static function getTencentCloudWordPressStaticData($action) { - $site_id = TencentWordpressPluginsSettingActions::getWordPressSiteID(); - $site_url = TencentWordpressPluginsSettingActions::getWordPressSiteUrl(); - $site_app = TencentWordpressPluginsSettingActions::getWordPressSiteApp(); - $static_data['action'] = $action; - $static_data['plugin_type'] = 'captcha'; - $static_data['data'] = array( - 'site_id' => $site_id, - 'site_url' => $site_url, - 'site_app' => $site_app + $siteId = TencentWordpressPluginsSettingActions::getWordPressSiteID(); + $siteUrl = TencentWordpressPluginsSettingActions::getWordPressSiteUrl(); + $siteApp = TencentWordpressPluginsSettingActions::getWordPressSiteApp(); + $staticData['action'] = $action; + $staticData['plugin_type'] = 'captcha'; + $staticData['data'] = array( + 'site_id' => $siteId, + 'site_url' => $siteUrl, + 'site_app' => $siteApp ); $common_option = get_option(TENCENT_WORDPRESS_COMMON_OPTIONS); - $tencent_wordpress_captcha_options = get_option(self::TENCENT_WORDPRESS_CAPTCHA_OPTIONS); - if ($tencent_wordpress_captcha_options[self::TENCENT_WORDPRESS_CAPTCHA_SECRET_CUSTOM] == '2' && isset($tencent_wordpress_captcha_options['secret_id']) && isset($tencent_wordpress_captcha_options['secret_key'])) { - $secret_id = $tencent_wordpress_captcha_options['secret_id']; - $secret_key = $tencent_wordpress_captcha_options['secret_key']; + $captchaOptions = get_option(self::TENCENT_WORDPRESS_CAPTCHA_OPTIONS); + if ($captchaOptions[self::TENCENT_WORDPRESS_CAPTCHA_SECRET_CUSTOM] == '2' && isset($captchaOptions['secret_id']) && isset($captchaOptions['secret_key'])) { + $secretId = $captchaOptions['secret_id']; + $secretKey = $captchaOptions['secret_key']; } elseif ($common_option['site_report_on'] === true && isset($common_option['secret_id']) && isset($common_option['secret_key'])) { - $secret_id = $common_option['secret_id']; - $secret_key = $common_option['secret_key']; + $secretId = $common_option['secret_id']; + $secretKey = $common_option['secret_key']; } - $static_data['data']['uin'] = TencentWordpressPluginsSettingActions::getUserUinBySecret($secret_id, $secret_key); - - $static_data['data']['cust_sec_on'] = $tencent_wordpress_captcha_options[self::TENCENT_WORDPRESS_CAPTCHA_SECRET_CUSTOM] == '2' ? 1 : 2; - $others =array( - 'captcha_appid' => $tencent_wordpress_captcha_options[self::TENCENT_WORDPRESS_CAPTCHA_APP_ID], - 'captcha_appid_login' => $tencent_wordpress_captcha_options[self::TENCENT_WORDPRESS_CAPTCHA_REGISTER_APP_ID], - 'captcha_appid_comment' => $tencent_wordpress_captcha_options[self::TENCENT_WORDPRESS_CAPTCHA_COMMENT_APP_ID], - 'captcha_appid_pwd' => $tencent_wordpress_captcha_options[self::TENCENT_WORDPRESS_CAPTCHA_LOSTPASSWORD_APP_ID], + $staticData['data']['uin'] = TencentWordpressPluginsSettingActions::getUserUinBySecret($secretId, $secretKey); + + $staticData['data']['cust_sec_on'] = $captchaOptions[self::TENCENT_WORDPRESS_CAPTCHA_SECRET_CUSTOM] == '2' ? 1 : 2; + $others = array( + 'captcha_appid' => $captchaOptions[self::TENCENT_WORDPRESS_CAPTCHA_APP_ID], + 'captcha_appid_login' => $captchaOptions[self::TENCENT_WORDPRESS_CAPTCHA_REGISTER_APP_ID], + 'captcha_appid_comment' => $captchaOptions[self::TENCENT_WORDPRESS_CAPTCHA_COMMENT_APP_ID], + 'captcha_appid_pwd' => $captchaOptions[self::TENCENT_WORDPRESS_CAPTCHA_LOSTPASSWORD_APP_ID], ); - $static_data['data']['others'] = json_encode($others); - return $static_data; + $staticData['data']['others'] = json_encode($others); + return $staticData; } + /** + * 配置页面测试验证码功能,先保存配置再测试 + * @throws Exception + */ + public static function tencentCaptchaCodeVerifyCheck() + { + $CodeVerifyOptions = get_option(self::TENCENT_WORDPRESS_CAPTCHA_OPTIONS); + $codeAppId = $CodeVerifyOptions[self::TENCENT_WORDPRESS_CAPTCHA_APP_ID]; + $codeAppKey = $CodeVerifyOptions[self::TENCENT_WORDPRESS_CAPTCHA_APP_KEY]; + $ticket = sanitize_text_field($_POST['codeVerifyTicketCheck']); + $randStr = sanitize_text_field($_POST['codeVerifyRandstrCheck']); -} + if (empty($ticket) || empty($randStr) || empty($codeAppId) || empty($codeAppKey)) { + $errormessage = 'ticket(' . $ticket . ') or randStr(' . $randStr . + ') or AppId(' . $codeAppId . ') or AppKey(' . $codeAppKey . ') is null'; + + CaptchaDebugLog::writeDebugLog('error', 'msg : ' . $errormessage, __FILE__, __LINE__); + wp_send_json_error(array('msg' => $errormessage)); + } + + try { + $verifyCode = self::verifyCodeReal($ticket, $randStr, $codeAppId, $codeAppKey); + if (!$verifyCode || isset($verifyCode['CaptchaCode']) && $verifyCode['CaptchaCode'] != 1) { + $errormessage = '验证码验证失败.'; + if (!empty($verifyCode['CaptchaMsg'])) { + $errormessage = $errormessage . $verifyCode['CaptchaMsg']; + } + CaptchaDebugLog::writeDebugLog('error', 'msg : ' . $errormessage, __FILE__, __LINE__); + wp_send_json_error(array('msg' => $errormessage)); + } + wp_send_json_success(array('msg' => '验证成功')); + } catch (\Exception $e) { + CaptchaDebugLog::writeDebugLog('error', 'msg : ' . $e->getMessage(), __FILE__, __LINE__); + wp_send_json_error(array('msg' => $e->getMessage())); + } + } + + public static function tencentCaptchaDeleteLogfile() + { + if (!file_exists(TENCENT_WORDPRESS_CAPTCHA_LOGS)) { + wp_send_json_success(); + } + + self::removeDir(TENCENT_WORDPRESS_CAPTCHA_LOGS); + wp_send_json_success(); + } + + /** + * 迭代删除目录及目录中的自文件 + * + * @param string $dir + * @access public + * @return bool + * @throws Exception + */ + public static function removeDir($dir) + { + if (empty($dir)) { + return true; + } + + $dir = realpath($dir) . '/'; + if ($dir == '/') { + CosDebugLog::writeDebugLog('notice', 'msg : can not remove root dir', __FILE__, __LINE__); + return false; + } + if (!is_writable($dir)) { + CosDebugLog::writeDebugLog('notice', 'msg : no delete permission ', __FILE__, __LINE__); + return false; + } + if (!is_dir($dir)) { + return true; + } + $entries = scandir($dir); + foreach ($entries as $entry) { + if ($entry == '.' or $entry == '..') + continue; + $fullEntry = $dir . $entry; + if (is_file($fullEntry)) { + @unlink($fullEntry); + } else { + return self::removeDir($fullEntry); + } + } + if (!@rmdir($dir)) { + CosDebugLog::writeDebugLog('notice', 'msg : remove log dir failed', __FILE__, __LINE__); + return false; + } + return true; + } +} diff --git a/tencentcloud-captcha/common/common.json b/tencentcloud-captcha/common/common.json index 9fe0c22b7af668c41bc34040cd555d0c5fbe3fa0..c6b93faecede6780007c884df6cfdb9749765769 100644 --- a/tencentcloud-captcha/common/common.json +++ b/tencentcloud-captcha/common/common.json @@ -1,4 +1,4 @@ { "version": "1.0.0", "log_server_url": "https://appdata.qq.com/upload" -} \ No newline at end of file +} diff --git a/tencentcloud-captcha/js/tencent_cloud_captcha_admin.js b/tencentcloud-captcha/js/tencent_cloud_captcha_admin.js index 8b50605d07f942335117f55126de603c783ab81c..80420bea3cf19abea16f8fc83fbc54247e8e9ba2 100644 --- a/tencentcloud-captcha/js/tencent_cloud_captcha_admin.js +++ b/tencentcloud-captcha/js/tencent_cloud_captcha_admin.js @@ -36,6 +36,7 @@ jQuery(function ($) { var loginCode = $("#codeVerify-option-login").is(":checked")?2:1;; var lostpasswordCode = $("#codeVerify-option-lostpassword").is(":checked")?2:1;; + var debugCode = $("#codeVerify-option-debug").is(":checked")?2:1;; var codeFree = $("#codeVerify-option-codeFree").is(":checked")?2:1;; var registerCodeAppId = $("#codeVerify_option_registerAPPID").val(); @@ -60,6 +61,7 @@ jQuery(function ($) { commentNeedCode: commentCode, loginNeedCode:loginCode, lostpasswordNeedCode:lostpasswordCode, + debugNeedCode:debugCode, codeFree: codeFree, registerCodeAppId: registerCodeAppId, registerCodeKey: registerCodeKey, @@ -78,6 +80,66 @@ jQuery(function ($) { } }); }); + + //验证码测试按钮的点击事件 + $('#codeVerifyButtonCheck').click(function () { + //重置ticket和随机字符串的值 + $('#codeVerifyTicketCheck').val(''); + $('#codeVerifyRandstrCheck').val(''); + //初始化验证码 + var captcha1 = new TencentCaptcha($('#codeVerifyButtonCheck').attr('data-appid'), function (res) { + //判断是否验证成功 + if (res.ret == 0) { + //将返回的ticket赋值给表单 + $('#codeVerifyTicketCheck').val(res.ticket); + $('#codeVerifyRandstrCheck').val(res.randstr); + //隐藏验证按钮 + $('#codeVerifyButtonCheck').hide(); + //展示通过按钮 + $('#codePassButtonNext').show(); + } + }); + //显示验证码 + captcha1.show(); + }); + + $('#codePassButtonNext').click(function () { + var codeVerifyTicketCheck = $("#codeVerifyTicketCheck").val(); + var codeVerifyRandstrCheck = $("#codeVerifyRandstrCheck").val(); + var codeAppId = $("#codeVerify-option_codeAppId").val(); + var codeSecretKey = $("#codeVerify-option_codeSecretKey").val(); + + $.ajax({ + type: "post", + url: ajaxUrl, + dataType:"json", + data: { + action: "codeVerify_check", + codeVerifyTicketCheck:codeVerifyTicketCheck, + codeVerifyRandstrCheck: codeVerifyRandstrCheck, + codeAppId: codeAppId, + codeSecretKey: codeSecretKey + }, + success: function(response) { + console.log(response); + if (response.success === true) { + //隐藏验证按钮 + $('#codePassButtonNext').hide(); + //展示通过按钮 + $('#codePassButtonOk').show(); + } else { + //隐藏验证按钮 + $('#codePassButtonNext').hide(); + //展示通过按钮 + $('#codeVerifyButtonCheck').show(); + $('#span_captch_verify')[0].innerHTML = response.data.msg; + } + + } + }); + }); + + //展示异步返回消息 function showAjaxReturnMsg(msg,success) { var parent = $('#codeVerify_show-ajax-return-msg').parent(); @@ -137,4 +199,30 @@ jQuery(function ($) { span_eye.addClass('shicons-hiddenda').removeClass('dashicons-visibility'); } } + + $('#button_delete_logfile').click(function () { + $.ajax({ + type: "post", + url: ajaxUrl, + dataType: "json", + data: { + action: "delete_captcha_logfile" + }, + success: function (response) { + if (response.success) { + $('#span_delete_logfile')[0].innerHTML = "删除成功!"; + $('#span_delete_logfile').show().delay(5000).fadeOut(); + } + } + }); + }); + + $('#codeVerify-option-debug').change(function () { + if ($('#codeVerify-option-debug')[0].checked) { + $('#tr_delete_logfile').css('display', ''); + } else { + $('#tr_delete_logfile').css('display', 'none'); + } + }); + }); \ No newline at end of file diff --git a/tencentcloud-captcha/js/tencent_cloud_captcha_user.js b/tencentcloud-captcha/js/tencent_cloud_captcha_user.js index 5203e33eb85c59237b84143a23142cac317c3f1e..8383e1d843d417da85409ec08c92b571e07a99ea 100644 --- a/tencentcloud-captcha/js/tencent_cloud_captcha_user.js +++ b/tencentcloud-captcha/js/tencent_cloud_captcha_user.js @@ -35,6 +35,4 @@ jQuery(function ($) { //显示验证码 captcha1.show(); }); - - }); \ No newline at end of file diff --git a/tencentcloud-captcha/readme.txt b/tencentcloud-captcha/readme.txt index 7627eefe1928d9b5dc747ca06521a2fb901463cd..ee05afb0ceb34645587c78164b43c50645771dc0 100644 --- a/tencentcloud-captcha/readme.txt +++ b/tencentcloud-captcha/readme.txt @@ -2,9 +2,9 @@ Contributors: Tencent Tags:tencent,tencentcloud,qcloud,春雨,腾讯云CAPTCHA,腾讯云验证码,腾讯云,验证码 -Requires at least: 5.0 -Tested up to: 5.4.1 -Stable tag: 1.0.1 +Requires at least: 5.5 +Tested up to: 5.8 +Stable tag: 1.0.2 License:Apache 2.0 License URI:http://www.apache.org/licenses/LICENSE-2.0 @@ -33,8 +33,6 @@ License URI:http://www.apache.org/licenses/LICENSE-2.0 == Frequently Asked Questions == * 1.当发现插件出错时,开启调试获取错误信息。 - - == Screenshots == 1. screenshot-1.png @@ -44,13 +42,17 @@ License URI:http://www.apache.org/licenses/LICENSE-2.0 5. screenshot-5.png == Changelog == += 1.0.2 = +* 1、最高支持PHP8版本,并兼容WordPress5.8 +* 2、新增本地调试日志功能 +* 3、配置页面新增验证码测试功能 + += 1.0.1 = +* 1、支持在windows环境下运行 = 1.0.0 = * 1、支持登录表单增加验证码; * 2、支持注册表单增加验证码; * 3、支持评论表单增加验证码; * 4、支持忘记密码表单增加验证码; -* 5、支持场景自定义 - -= 1.0.1 = -* 1、支持在windows环境下运行 \ No newline at end of file +* 5、支持场景自定义 \ No newline at end of file diff --git a/tencentcloud-captcha/tencent-captch-debuger.php b/tencentcloud-captcha/tencent-captch-debuger.php new file mode 100644 index 0000000000000000000000000000000000000000..dd55d13c1f0ada76d2d026ed866f67814cb51a81 --- /dev/null +++ b/tencentcloud-captcha/tencent-captch-debuger.php @@ -0,0 +1,155 @@ +_file_ext = 'log'; + self::$instance->_log_path = TENCENT_WORDPRESS_CAPTCHA_LOGS; + self::$instance->_file_permissions = 0644; + self::$instance->_date_fmt = 'Y-m-d H:i:s'; + self::$instance->_func_overload = false; + self::$instance->_enable = true; + } + } + + + /** + * 将消息输出到指定的文件 + * @param $level + * @param string $msg 消息内容 + * @param string $file 日志文件名称,默认是 qcloud_rain_php.log + * @return bool + * @throws Exception + */ + public static function writeDebugLog($level, $msg, $file_path = '', $line = '') + { + self::_init(); + $tencent_wordpress_captcha_options = get_option('tencent_wordpress_captcha_options'); + $loginNeedCode = $tencent_wordpress_captcha_options['debug_need_code']; + if ($loginNeedCode == '2') { + if (self::$instance->_enable === false) { + return FALSE; + } + if (!is_dir(self::$instance->_log_path)) { + @mkdir(self::$instance->_log_path, 0777); + } + $level = strtoupper($level); + $filepath = self::$instance->_log_path . '/log-' . date('Y-m-d') . '.' . self::$instance->_file_ext; + $message = ""; + if (!file_exists($filepath)) { + $newfile = TRUE; + } + + if (!$fp = @fopen($filepath, 'ab')) { + return FALSE; + } + + flock($fp, LOCK_EX); + if (strpos(self::$instance->_date_fmt, 'u') !== FALSE) { + $microtime_full = microtime(TRUE); + $microtime_short = sprintf("%06d", ($microtime_full - floor($microtime_full)) * 1000000); + $date = new DateTime(date('Y-m-d H:i:s.' . $microtime_short, $microtime_full)); + $date = $date->format(self::$instance->_date_fmt); + } else { + $date = date(self::$instance->_date_fmt); + } + $message .= self::$instance->_format_line($level, $date, $msg, $file_path, $line); + $result = 0; + for ($written = 0, $length = self::$instance->strlen($message); $written < $length; $written += $result) { + if (($result = fwrite($fp, self::$instance->substr($message, $written))) === FALSE) { + break; + } + } + + flock($fp, LOCK_UN); + fclose($fp); + + if (isset($newfile) && $newfile === TRUE) { + chmod($filepath, self::$instance->_file_permissions); + } + + return is_int($result); + } + + } + + /** + * 格式化输出字符串 + * @param $level + * @param $date + * @param $message + * @return string + */ + private function _format_line($level, $date, $message, $file_path, $line) + { + $format_line = $level . ', ' . $date . ', ' . $message; + if (isset($file_path) && !empty($file_path)) { + $format_line .= ", filepath:" . $file_path; + } + + if (isset($line) && !empty($line)) { + $format_line .= ", line:" . $line; + } + + $format_line .= "\n"; + return $format_line; + } + + /** + * 获取字符串长度,支持中文 + * @param $str + * @return false|int + */ + private static function strlen($str) + { + return (self::$instance->_func_overload) + ? mb_strlen($str, '8bit') + : strlen($str); + } + + /** + * 获取子字符串,支持中文 + * @param $str + * @param $start + * @param null $length + * @return false|string + */ + private static function substr($str, $start, $length = NULL) + { + if (self::$instance->_func_overload) { + isset($length) or $length = ($start >= 0 ? self::strlen($str) - $start : -$start); + return mb_substr($str, $start, $length, '8bit'); + } + + return isset($length) + ? substr($str, $start, $length) + : substr($str, $start); + } + } +} diff --git a/tencentcloud-captcha/tencentcloud-captcha-setting-page.php b/tencentcloud-captcha/tencentcloud-captcha-setting-page.php index e0d2b2f055086ffa67aa5862343c366521009ddd..780d3c25ec94473f3793d75960bd6575fee5f66e 100644 --- a/tencentcloud-captcha/tencentcloud-captcha-setting-page.php +++ b/tencentcloud-captcha/tencentcloud-captcha-setting-page.php @@ -19,213 +19,313 @@ if (!current_user_can('manage_options')) { } $ajaxUrl = admin_url('admin-ajax.php'); $codeVerifySettings = get_option('tencent_wordpress_captcha_options'); -$secretCustom = $codeVerifySettings['secret_custom']?:'1'; +$secretCustom = $codeVerifySettings['secret_custom'] ?: '1'; $secretID = ''; $secretKey = ''; -if ($secretCustom == '1'){ +if ($secretCustom == '1') { $commonOptions = get_option('tencent_wordpress_common_options'); - $secretID = esc_attr($commonOptions['secret_id']) ?: ''; - $secretKey = esc_attr($commonOptions['secret_key']) ?: ''; -}else{ + if (isset($commonOptions['secret_id'], $commonOptions['secret_key'])) { + $secretID = esc_attr($commonOptions['secret_id']) ?: ''; + $secretKey = esc_attr($commonOptions['secret_key']) ?: ''; + } +} else { $secretID = esc_attr($codeVerifySettings['secret_id']) ?: ''; $secretKey = esc_attr($codeVerifySettings['secret_key']) ?: ''; } $codeAppId = esc_attr($codeVerifySettings['captcha_app_id']) ?: ''; $codeSecretKey = esc_attr($codeVerifySettings['captcha_app_key']) ?: ''; -$commentCode = esc_attr($codeVerifySettings['comment_need_code'])?:'2'; -$registerCode = esc_attr($codeVerifySettings['register_need_code'])?:'2'; -$loginCode = esc_attr($codeVerifySettings['login_need_code'])?:'2'; -$lostpasswordCode = esc_attr($codeVerifySettings['lostpassword_need_code'])?:'2'; - -$codeFree = esc_attr($codeVerifySettings['code_free'])?:'1'; -$registerCodeAPPID = esc_attr($codeVerifySettings['captcha_register_app_id'])?:''; -$registerCodeKey = esc_attr($codeVerifySettings['captcha_register_app_key'])?:''; -$commentCodeAPPID = esc_attr($codeVerifySettings['captcha_comment_app_id'])?:''; -$commentCodeKey = esc_attr($codeVerifySettings['captcha_comment_app_key'])?:''; -$lostpasswordCodeAPPID = esc_attr($codeVerifySettings['captcha_lostpassword_app_id'])?:''; -$lostpasswordCodeKey = esc_attr($codeVerifySettings['captcha_lostpassword_app_key'])?:''; - ?> - - -腾讯云验证码插件启用生效中。
在登录、注册、找回密码、评论等场景下,增加人机校验