Fix recaptcha api request (#891)

This commit is contained in:
RainLoop Team 2017-04-15 23:54:56 +03:00
parent 18b39315e7
commit 509f81d2c8
2 changed files with 152 additions and 149 deletions

View file

@ -1 +1 @@
2.1 2.2

View file

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