2014-02-01 04:31:55 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class RecaptchaPlugin extends \RainLoop\Plugins\AbstractPlugin
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function Init()
|
|
|
|
{
|
|
|
|
$this->UseLangs(true);
|
2015-01-06 05:41:22 +08:00
|
|
|
|
2014-02-01 04:31:55 +08:00
|
|
|
$this->addJs('js/recaptcha.js');
|
2015-01-06 05:41:22 +08:00
|
|
|
|
2014-02-01 04:31:55 +08:00
|
|
|
$this->addHook('ajax.action-pre-call', 'AjaxActionPreCall');
|
|
|
|
$this->addHook('filter.ajax-response', 'FilterAjaxResponse');
|
|
|
|
}
|
2015-01-06 05:41:22 +08:00
|
|
|
|
2014-02-01 04:31:55 +08:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function configMapping()
|
|
|
|
{
|
|
|
|
return array(
|
2015-01-06 05:41:22 +08:00
|
|
|
\RainLoop\Plugins\Property::NewInstance('public_key')->SetLabel('Site key')
|
2014-02-01 04:31:55 +08:00
|
|
|
->SetAllowedInJs(true)
|
|
|
|
->SetDefaultValue(''),
|
2015-01-06 05:41:22 +08:00
|
|
|
\RainLoop\Plugins\Property::NewInstance('private_key')->SetLabel('Secret key')
|
2014-02-01 04:31:55 +08:00
|
|
|
->SetDefaultValue(''),
|
2015-01-06 05:41:22 +08:00
|
|
|
\RainLoop\Plugins\Property::NewInstance('theme')->SetLabel('Theme')
|
|
|
|
->SetAllowedInJs(true)
|
|
|
|
->SetType(\RainLoop\Enumerations\PluginPropertyType::SELECTION)
|
|
|
|
->SetDefaultValue(array('light', 'dark')),
|
2014-02-01 04:31:55 +08:00
|
|
|
\RainLoop\Plugins\Property::NewInstance('error_limit')->SetLabel('Limit')
|
|
|
|
->SetType(\RainLoop\Enumerations\PluginPropertyType::SELECTION)
|
|
|
|
->SetDefaultValue(array(0, 1, 2, 3, 4, 5))
|
|
|
|
->SetDescription('')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getCaptchaCacherKey()
|
|
|
|
{
|
2015-01-06 05:41:22 +08:00
|
|
|
return 'CaptchaNew/Login/'.\RainLoop\Utils::GetConnectionToken();
|
2014-02-01 04:31:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
private function getLimit()
|
|
|
|
{
|
|
|
|
$iConfigLimit = $this->Config()->Get('plugin', 'error_limit', 0);
|
|
|
|
if (0 < $iConfigLimit)
|
|
|
|
{
|
|
|
|
$oCacher = $this->Manager()->Actions()->Cacher();
|
|
|
|
$sLimit = $oCacher && $oCacher->IsInited() ? $oCacher->Get($this->getCaptchaCacherKey()) : '0';
|
2015-01-06 05:41:22 +08:00
|
|
|
|
|
|
|
if (0 < \strlen($sLimit) && \is_numeric($sLimit))
|
2014-02-01 04:31:55 +08:00
|
|
|
{
|
|
|
|
$iConfigLimit -= (int) $sLimit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $iConfigLimit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function FilterAppDataPluginSection($bAdmin, $bAuth, &$aData)
|
|
|
|
{
|
2015-01-06 05:41:22 +08:00
|
|
|
if (!$bAdmin && !$bAuth && \is_array($aData))
|
2014-02-01 04:31:55 +08:00
|
|
|
{
|
|
|
|
$aData['show_captcha_on_login'] = 1 > $this->getLimit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $sAction
|
|
|
|
*/
|
|
|
|
public function AjaxActionPreCall($sAction)
|
|
|
|
{
|
|
|
|
if ('Login' === $sAction && 0 >= $this->getLimit())
|
|
|
|
{
|
2015-01-06 05:41:22 +08:00
|
|
|
$bResult = false;
|
2014-02-01 04:31:55 +08:00
|
|
|
|
2015-01-06 05:41:22 +08:00
|
|
|
$sResult = $this->Manager()->Actions()->Http()->GetUrlAsString(
|
|
|
|
'https://www.google.com/recaptcha/api/siteverify?secret='.
|
|
|
|
\urlencode($this->Config()->Get('plugin', 'private_key', '')).'&response='.
|
|
|
|
\urlencode($this->Manager()->Actions()->GetActionParam('RecaptchaResponse', '')));
|
2014-02-01 04:31:55 +08:00
|
|
|
|
2015-01-06 05:41:22 +08:00
|
|
|
if ($sResult)
|
2014-02-01 04:31:55 +08:00
|
|
|
{
|
2015-01-06 05:41:22 +08:00
|
|
|
$aResp = @\json_decode($sResult, true);
|
|
|
|
if (\is_array($aResp) && isset($aResp['success']) && $aResp['success'])
|
|
|
|
{
|
|
|
|
$bResult = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$bResult)
|
|
|
|
{
|
|
|
|
$this->Manager()->Actions()->Logger()->Write('RecaptchaResponse:'.$sResult);
|
2014-02-01 04:31:55 +08:00
|
|
|
throw new \RainLoop\Exceptions\ClientException(\RainLoop\Notifications::CaptchaError);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $sAction
|
|
|
|
* @param array $aResponseItem
|
|
|
|
*/
|
|
|
|
public function FilterAjaxResponse($sAction, &$aResponseItem)
|
|
|
|
{
|
|
|
|
if ('Login' === $sAction && $aResponseItem && isset($aResponseItem['Result']))
|
|
|
|
{
|
|
|
|
$oCacher = $this->Manager()->Actions()->Cacher();
|
|
|
|
$iConfigLimit = (int) $this->Config()->Get('plugin', 'error_limit', 0);
|
2015-09-18 03:48:52 +08:00
|
|
|
|
2014-02-01 04:31:55 +08:00
|
|
|
$sKey = $this->getCaptchaCacherKey();
|
|
|
|
|
|
|
|
if (0 < $iConfigLimit && $oCacher && $oCacher->IsInited())
|
|
|
|
{
|
|
|
|
if (false === $aResponseItem['Result'])
|
|
|
|
{
|
|
|
|
$iLimit = 0;
|
|
|
|
$sLimut = $oCacher->Get($sKey);
|
2015-01-06 05:41:22 +08:00
|
|
|
if (0 < \strlen($sLimut) && \is_numeric($sLimut))
|
2014-02-01 04:31:55 +08:00
|
|
|
{
|
|
|
|
$iLimit = (int) $sLimut;
|
|
|
|
}
|
|
|
|
|
|
|
|
$oCacher->Set($sKey, ++$iLimit);
|
|
|
|
|
|
|
|
if ($iConfigLimit <= $iLimit)
|
|
|
|
{
|
|
|
|
$aResponseItem['Captcha'] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$oCacher->Delete($sKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|