rhymix/addons/recaptcha/recaptcha.class.php
Kijin Sung 84e5542d77 Remove unnecessary use of BaseObject
- 트리거 등 반환값이 필요하지 않은 곳에서 new BaseObject()를 반환하는 것 삭제
- 모듈 설치, 업데이트 후 무의미한 new BaseObject()를 반환하는 것 삭제
- 사용자에게 에러 메시지를 돌려주는 용도로 new BaseObject(-1, '에러메시지')를
  사용하는 경우는 대부분 $this->setError()로 변경함. 언어 변환과 sprintf()
  처리까지 한 번에 이루어지므로 이쪽이 더 편리함.
2017-12-01 00:54:51 +09:00

70 lines
2 KiB
PHP

<?php
class reCAPTCHA
{
protected static $verify = 'https://www.google.com/recaptcha/api/siteverify';
protected static $config = null;
protected static $scripts_added = false;
protected static $instances_inserted = 0;
protected static $sequence = 1;
public static function init($config)
{
self::$config = $config;
}
public static function check()
{
$response = Context::get('g-recaptcha-response');
if (!$response)
{
return new BaseObject(-1, 'recaptcha.msg_recaptcha_invalid_response');
}
try
{
$verify_request = \Requests::post(self::$verify, array(), array(
'secret' => self::$config->secret_key,
'response' => $response,
'remoteip' => \RX_CLIENT_IP,
));
}
catch (\Requests_Exception $e)
{
return new BaseObject(-1, 'recaptcha.msg_recaptcha_connection_error');
}
$verify = @json_decode($verify_request->body, true);
if ($verify && isset($verify['error-codes']) && in_array('invalid-input-response', $verify['error-codes']))
{
return new BaseObject(-1, 'recaptcha.msg_recaptcha_invalid_response');
}
elseif (!$verify || !$verify['success'] || (isset($verify['error-codes']) && $verify['error-codes']))
{
return new BaseObject(-1, 'recaptcha.msg_recaptcha_server_error');
}
else
{
$_SESSION['recaptcha_authenticated'] = true;
return true;
}
}
public function __construct()
{
if (!self::$scripts_added)
{
self::$scripts_added = true;
Context::loadFile(array('./addons/recaptcha/recaptcha.js', 'body'));
Context::addHtmlFooter('<script src="https://www.google.com/recaptcha/api.js?render=explicit&amp;onload=reCaptchaCallback" async defer></script>');
$html = '<div id="recaptcha-config" data-sitekey="%s" data-theme="%s" data-size="%s"></div>';
$html = sprintf($html, escape(self::$config->site_key), self::$config->theme ?: 'light', self::$config->size ?: 'normal');
Context::addHtmlFooter($html);
}
}
public function __toString()
{
return sprintf('<div id="recaptcha-instance-%d" class="g-recaptcha"></div>', self::$instances_inserted++);
}
}