where('status', Status::ENABLE)->first(); return $reCaptcha ? $reCaptcha->generateScript() : null; } /** * Custom captcha script * * @return string */ public static function customCaptcha($width = '100%', $height = 46, $bgColor = '#003'){ $textColor = '#'.gs('base_color'); $captcha = Extension::where('act', 'custom-captcha')->where('status', Status::ENABLE)->first(); if (!$captcha) { return 0; } $code = rand(100000, 999999); $char = str_split($code); $ret = ''; $ret .= '
'; foreach ($char as $value) { $ret .= '' . $value . ''; } $ret .= '
'; $captchaSecret = hash_hmac('sha256', $code, $captcha->shortcode->random_key->value); $ret .= ''; return $ret; } /** * Verify all captcha * * @return boolean */ public static function verify(){ $gCaptchaPass = self::verifyGoogleCaptcha(); $cCaptchaPass = self::verifyCustomCaptcha(); if ($gCaptchaPass && $cCaptchaPass) { return true; } return false; } /** * Verify google recaptcha2 * * @return boolean */ public static function verifyGoogleCaptcha(){ $pass = true; $googleCaptcha = Extension::where('act', 'google-recaptcha2')->where('status', Status::ENABLE)->first(); if ($googleCaptcha) { $resp = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$googleCaptcha->shortcode->secret_key->value."&response=".request()['g-recaptcha-response']."&remoteip=".getRealIP()), true); if (!$resp['success']) { $pass = false; } } return $pass; } /** * Verify custom captcha * * @return boolean */ public static function verifyCustomCaptcha(){ $pass = true; $customCaptcha = Extension::where('act', 'custom-captcha')->where('status', Status::ENABLE)->first(); if ($customCaptcha) { $captchaSecret = hash_hmac('sha256', request()->captcha, $customCaptcha->shortcode->random_key->value); if ($captchaSecret != request()->captcha_secret) { $pass = false; } } return $pass; } }