2016-08-20 23:24:22 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace CustomValidations;
|
|
|
|
|
|
|
|
use Respect\Validation\Rules\AbstractRule;
|
|
|
|
|
|
|
|
class Captcha extends AbstractRule {
|
2019-11-16 20:07:02 +01:00
|
|
|
private $dataStoreName;
|
2020-10-14 20:08:14 +02:00
|
|
|
private $apiKeyPermissionType;
|
2019-11-16 20:07:02 +01:00
|
|
|
|
2020-10-14 20:08:14 +02:00
|
|
|
public function __construct($apiKeyPermissionType = '') {
|
|
|
|
$this->apiKeyPermissionType = $apiKeyPermissionType;
|
|
|
|
if (in_array($apiKeyPermissionType, \APIKey::TYPES)) {
|
|
|
|
$this->apiKeyType = $apiKeyPermissionType;
|
|
|
|
} else if($apiKeyPermissionType) {
|
2019-11-16 20:07:02 +01:00
|
|
|
throw new \Exception(\ERRORS::INVALID_API_KEY_TYPE);
|
|
|
|
}
|
|
|
|
}
|
2020-10-14 20:08:14 +02:00
|
|
|
|
2016-08-20 23:24:22 +02:00
|
|
|
public function validate($reCaptchaResponse) {
|
|
|
|
$reCaptchaPrivateKey = \Setting::getSetting('recaptcha-private')->getValue();
|
2017-01-13 21:06:49 +01:00
|
|
|
$apiKey = \APIKey::getDataStore(\Controller::request('apiKey'), 'token');
|
2016-08-20 23:24:22 +02:00
|
|
|
|
2019-11-16 20:07:02 +01:00
|
|
|
if (!$reCaptchaPrivateKey) return true;
|
2020-10-14 20:08:14 +02:00
|
|
|
|
|
|
|
if (!$apiKey->isNull()){
|
|
|
|
switch ($this->apiKeyPermissionType) {
|
|
|
|
case 'TICKET_CREATE_PERMISSION':
|
|
|
|
return $apiKey->canCreateTickets;
|
|
|
|
case 'USER_CREATE_PERMISSION':
|
|
|
|
return $apiKey->canCreateUsers;
|
|
|
|
case 'TICKET_CHECK_PERMISSION':
|
|
|
|
return $apiKey->canCheckTickets;
|
|
|
|
}
|
|
|
|
}
|
2016-08-20 23:24:22 +02:00
|
|
|
|
|
|
|
$reCaptcha = new \ReCaptcha\ReCaptcha($reCaptchaPrivateKey);
|
|
|
|
$reCaptchaValidation = $reCaptcha->verify($reCaptchaResponse, $_SERVER['REMOTE_ADDR']);
|
|
|
|
return $reCaptchaValidation->isSuccess();
|
|
|
|
}
|
|
|
|
}
|