Вход Регистрация
Файл: concrete5.7.5.6/concrete/src/Session/SessionValidator.php
Строк: 138
<?php

namespace ConcreteCoreSession;

use 
ConcreteCoreApplicationApplication;
use 
ConcreteCoreConfigRepositoryRepository;
use 
ConcreteCoreHttpRequest;
use 
ConcreteCoreUtilityIPAddress;
use 
PsrLogLoggerAwareInterface;
use 
PsrLogLoggerInterface;
use 
SymfonyComponentHttpFoundationSessionSession as SymfonySession;

/**
 * Class SessionValidator
 * Base concrete5 session validator, validates the IP and the agent across requests
 * @package ConcreteCoreSession
 */
class SessionValidator implements SessionValidatorInterfaceLoggerAwareInterface
{

    
/** @var ConcreteCoreApplicationApplication */
    
private $app;

    
/** @var ConcreteCoreConfigRepositoryRepository */
    
private $config;

    
/** @var ConcreteCoreHttpRequest */
    
private $request;

    
/** @var PsrLogLoggerInterface */
    
private $logger;

    public function 
__construct(Application $appRepository $configRequest $requestLoggerInterface $logger null)
    {
        
$this->app $app;
        
$this->config $config;
        
$this->request $request;
        
$this->logger $logger;
    }

    
/**
     * @param SymfonyComponentHttpFoundationSessionSession $session
     */
    
public function handleSessionValidation(SymfonySession $session)
    {
        
$ip_address = new IPAddress($this->request->getClientIp());
        
$request_ip $ip_address->getIp(IPAddress::FORMAT_IP_STRING);

        
$invalidate false;

        
$ip $session->get('CLIENT_REMOTE_ADDR');
        
$agent $session->get('CLIENT_HTTP_USER_AGENT');
        
$request_agent $this->request->server->get('HTTP_USER_AGENT');

        
// Validate the request IP
        
if ($this->shouldCompareIP() && $ip && $ip != $request_ip) {
            if (
$this->logger) {
                
$this->logger->debug('Session Invalidated. Session IP "{session}" did not match provided IP "{client}".',
                    array(
                        
'session' => $ip,
                        
'client' => $request_ip));
            }

            
$invalidate true;
        }

        
// Validate the request user agent
        
if ($this->shouldCompareAgent() && $agent && $agent != $request_agent) {
            if (
$this->logger) {
                
$this->logger->debug('Session Invalidated. Session user agent "{session}" did not match provided agent "{client}"',
                    array(
                        
'session' => $agent,
                        
'client' => $request_agent));
            }

            
$invalidate true;
        }

        if (
$invalidate) {
            
$session->invalidate();
        } else {
            if (!
$ip && $request_ip) {
                
$session->set('CLIENT_REMOTE_ADDR'$request_ip);
            }

            if (!
$agent && $request_agent) {
                
$session->set('CLIENT_HTTP_USER_AGENT'$request_agent);
            }
        }
    }

    
/**
     * @return bool
     */
    
private function shouldCompareIP()
    {
        return 
$this->config->get('concrete.security.session.invalidate_on_ip_mismatch'true);
    }

    
/**
     * @return bool
     */
    
private function shouldCompareAgent()
    {
        return 
$this->config->get('concrete.security.session.invalidate_on_user_agent_mismatch'true);
    }

    
/**
     * Sets a logger instance on the object
     *
     * @param LoggerInterface $logger
     * @return null
     */
    
public function setLogger(LoggerInterface $logger)
    {
        
$this->logger $logger;
    }

}
Онлайн: 2
Реклама