Get and check the token values.

@param string $captcha

@return string|bool
19044
private static function getToken($captcha)
    {
        // Get the token values
        try {
            $token = Crypt::decrypt($captcha);
        } catch (\Exception $exception) {
            return false;
        }

        $token = @unserialize($token);

        // Token is null or unserializable
        if (!$token || !is_array($token) || empty($token)) {
            return false;
        }

        // Check token values
        if (empty($token['session_id']) ||
            empty($token['ip']) ||
            empty($token['user_agent']) ||
            $token['session_id'] !== session()->getId() ||
            $token['ip'] !== request()->ip() ||
            $token['user_agent'] !== request()->header('User-Agent')
        ) {
            return false;
        }

        return $token;
    }
418554
public function parseToken(string $tokenString)
    {
        // try to parse a token string.
        try {
            return $this->parser()->parse($tokenString);
        } catch (\Exception $e) {
            // if it was not possible to, return null.
            return null;
        }
    }
656962
public function validateCaptchaMinTime($value, $context) {
		$captcha = $this->_getCaptcha($context['data']);
		if (!$captcha) {
			return false;
		}
		if ($captcha->created >= new Time('- ' . $this->getConfig('minTime') . ' seconds')) {
			return false;
		}

		return true;
	}
385317
private function getCaptchaImage()
    {
        $image = $this->client->get(self::URL_CAPTCHA);

        if (false === $image) {
            $this->error = 'No se pudo cargar el captcha image';

            return false;
        }

        return $image;
    }
874987
static public function captcha($token_name='_form_token', $apiurl=null, $settings=[])
    {
        if (is_array($token_name)) {
            $settings = $token_name;
            $token_name = null;
        }
        $token_name || $token_name = '_form_token';
        
        if (is_array($apiurl)) {
            $settings = $apiurl;
            $apiurl = null;
        }
        $apiurl || $apiurl = Ioc::url('api:common/getCaptchaImage');
        
        $str = '';
        $class = isset($settings['class']) ? $settings['class'] : 'captcha-image';
        $settings['style'] = isset($settings['style']) ? $settings['style'] : '';
        $settings['style'] .= ';cursor:pointer;';
        foreach ($settings as $k => $v) {
            if (in_array($k, ['width', 'height', 'id', 'style'])) {
                $str .= $k . '="' . $v . '" ';
            }
        }
        $str = '<img src="" class="' . $class . '" title="点击刷新" ' . $str . '/>';
        $isLoad = isset(static::$isLoaded['captcha']) && static::$isLoaded['captcha'] ? true : false;
        if (!$isLoad) {
            $str .= <<<EOF
            <script>
            $(function (){
                var captcha_image_load = function(){
                    var self = $(".${class}");
                    var data = {};
                    var token = self.parents("form").find("input[name=${token_name}]");
                    if (token.length) {
                    	data['_form_token'] = token.val();
                    }
                    
        	    	$.get("{$apiurl}", data, function(data){
                        if (data) {
                            self.attr("src", "data:image/jpg;base64," + data.base64);
                        }
                    });
        	    };
                $(".${class}").on("click", captcha_image_load);
                setTimeout(function () {
                    $(".${class}").first().click();
                }, 1000);
            });
            </script>
EOF;
        }
        
        return $str;
    }
1007084
public function generateCaptchaAction($key)
    {
        $options = $this->container->getParameter('gregwar_captcha.config');
        $session = $this->get('session');
        $whitelistKey = $options['whitelist_key'];
        $isOk = false;

        if ($session->has($whitelistKey)) {
            $keys = $session->get($whitelistKey);
            if (is_array($keys) && in_array($key, $keys)) {
                $isOk = true;
            }
        }

        if (!$isOk) {
            return $this->error($options);
        }

        /* @var \Gregwar\CaptchaBundle\Generator\CaptchaGenerator $generator */
        $generator = $this->container->get('gregwar_captcha.generator');

        $persistedOptions = $session->get($key, array());
        $options = array_merge($options, $persistedOptions);

        $phrase = $generator->getPhrase($options);
        $generator->setPhrase($phrase);
        $persistedOptions['phrase'] = $phrase;
        $session->set($key, $persistedOptions);

        $response = new Response($generator->generate($options));
        $response->headers->set('Content-type', 'image/jpeg');
        $response->headers->set('Pragma', 'no-cache');
        $response->headers->set('Cache-Control', 'no-cache');

        return $response;
    }
604658
protected function getCrcValue(string $crc) : ?string
    {
        return isset($this->crcCache[$crc]) ? $this->crcCache[$crc] : null;
    }
1302420
public function checkToken($method = 'post') {
        $token = $this->getFormToken();

        if (!$this->input->$method->get($token, '', 'alnum')) {
            if ($this->getSession()->isNew()) {
                // Redirect to login screen.
                $this->redirect('/login');
                $this->close();
            }

            return false;
        }

        return true;
    }
859191
public static function getTokenName($token)
    {
        if (is_int($token[self::T_KEY])) {
            return token_name($token[self::T_KEY]);
        } else {
            return $token[self::T_KEY];
        }
    }
495998
public function validateCaptchaResult($value, $context) {
		$captcha = $this->_getCaptcha($context['data']);
		if (!$captcha) {
			return false;
		}
		if ((string)$value !== $captcha->result) {
			return false;
		}

		$this->_captchasTable->markUsed($captcha);
		return true;
	}
851846
public function validateReCaptcha()
    {
        if (is_user_logged_in()) {
            return;
        }

        $response = isset($_POST['g-recaptcha-response']) ? esc_attr($_POST['g-recaptcha-response']) : '';
        $reCaptcha = \Municipio\Helper\ReCaptcha::controlReCaptcha($response);

        if (!$reCaptcha) {
            wp_die(sprintf('<strong>%s</strong>:&nbsp;%s', __('Error', 'municipio'), __('reCaptcha verification failed', 'municipio')));
        }

        return;
    }
870148
public function findByToken($token)
    {
        /** @var Query $query */
        $query = $this->isValid($token, true);
        if ($query === false) {
            return $query;
        }
        return $query->contain(['Users'])->first();
    }
413633
protected function captcha()
	{
		echo $this->getLabel();
		echo '<div class="controls"><div class="captcha">';
		echo '<div class="widget">' . $this->widget('CCaptcha', $this->captchaOptions, true) . '</div>';
		echo $this->form->textField($this->model, $this->attribute, $this->htmlOptions);
		echo $this->getError() . $this->getHint();
		echo '</div></div>';
	}
8305
public function getTokens($string)
    {
        $tokens      = [];
        $scanResults = $this->engine->getTokenizer()->scan($string);

        foreach ($scanResults as $scanResult) {
            if (
                isset($scanResult['type'])
                && isset($scanResult['name'])
                && \Mustache_Tokenizer::T_ESCAPED === $scanResult['type']
            ) {
                $tokens[$scanResult['name']] = true;
            }
        }

        return array_keys($tokens);
    }
960429
protected function getRecaptchaResult()
    {
        $response = $this->guzzle->request(
            'POST',
            'https://www.google.com/recaptcha/api/siteverify',
            [
                'timeout'   => 2.0,
                'query'     => [
                    'secret'    => $this->config->get('antispam.recaptcha.secret', ''),
                    'response'  => $this->request->get('g-recaptcha-response', ''),
                    'remoteip'  => $this->request->getClientIp(),
                ],
            ]
        );

        return json_decode($response->getBody()->getContents());
    }
922227
public function displayCaptchaPicture()
    {
        $app = Application::getFacadeApplication();
        $responseFactory = $app->make(ResponseFactoryInterface::class);

        return $responseFactory->notFound('Captcha without image');
    }
964000
public function getHash($text = null)
    {
        // inserting captcha record
        $time = time() + $this->timeout;
        $textHash = $this->getTextHash($text);

        // if session is started - storing captcha info here
        $session = $this->getSession();
        if ($session->isSessionStarted()) {
            $hash = oxUtilsObject::getInstance()->generateUID();
            $hashArray = $session->getVariable('captchaHashes');
            $hashArray[$hash] = array($textHash => $time);
            $session->setVariable('captchaHashes', $hashArray);
        } else {
            $database = DatabaseProvider::getDb();
            $query = "insert into oecaptcha (oxhash, oxtime) values (" .
                      $database->quote($textHash) . ", " . $database->quote($time) . ")";
            $database->execute($query);
            $hash = $database->getOne('select LAST_INSERT_ID()', false, false);
        }

        return $hash;
    }