<commit_before><?php

ini_set('display_errors', true);
require '_includes.php';

$janusConfig = SimpleSAML_Configuration::getConfig('module_janus.php');
$rootCertificatesFile = $janusConfig->getString('ca_bundle_file');

$server = new EntityCertificateServer();
$server->setTrustedRootCertificateAuthoritiesFile($rootCertificatesFile);
$server->serve($_GET['eid']);

class EntityCertificateServer
{
    protected $_response;

    /**
     * @var OpenSsl_Certificate
     */
    protected $_certificate;

    /**
     * @var OpenSsl_Certificate_Chain
     */
    protected $_certificateChain;

    protected $_trustedRootCertificateAuthoritiesFile;
    
    public function __construct()
    {
        $this->_initializeResponse();
    }

    public function setTrustedRootCertificateAuthoritiesFile($file)
    {
        $this->_trustedRootCertificateAuthoritiesFile = $file;
    }

    protected function _initializeResponse()
    {
        $response = new stdClass();
        $response->CertificateChain = array();
        $response->Warnings = array();
        $response->Errors = array();
        $this->_response = $response;
    }
    
    public function serve($entityId)
    {
        if (!$this->_loadEntityCertificate($entityId)) {
            return $this->_sendResponse();
        }

        $this->_checkCertificateValidity();
        $this->_loadCertificateChain();
        $this->_checkChainValidity();
        return $this->_sendResponse();
    }

    protected function _loadEntityCertificate($entityId)
    {
        $janusConfig = SimpleSAML_Configuration::getConfig('module_janus.php');
        $entityController = new sspmod_janus_EntityController($janusConfig);
        $entityController->setEntity($entityId);
        $entityController->loadEntity();
        try {
            $certificate = $entityController->getCertificate();
        } catch(Exception $e) {
            $this->_response->Errors[]   = "Certificate data invalid!";
            return false;
        }
        if (!$certificate) {
            $this->_response->Warnings[] = "No certificate data for this entity";
            return false;
        }
        return true;
    }

    protected function _checkCertificateValidity()
    {
        $validator = new sspmod_janus_OpenSsl_Certificate_Validator($this->_certificate);
        $validator->setIgnoreSelfSigned(true);
        $validator->validate();

        $this->_response->Warnings = array_merge($this->_response->Warnings, $validator->getWarnings());
        $this->_response->Errors   = array_merge($this->_response->Errors,   $validator->getErrors());
    }

    protected function _loadCertificateChain()<commit_msg>Fixes issue 284 There where an assignment between the temporary variable and the object variable.<commit_after><?php

ini_set('display_errors', true);
require '_includes.php';

$janusConfig = SimpleSAML_Configuration::getConfig('module_janus.php');
$rootCertificatesFile = $janusConfig->getString('ca_bundle_file');

$server = new EntityCertificateServer();
$server->setTrustedRootCertificateAuthoritiesFile($rootCertificatesFile);
$server->serve($_GET['eid']);

class EntityCertificateServer
{
    protected $_response;

    /**
     * @var OpenSsl_Certificate
     */
    protected $_certificate;

    /**
     * @var OpenSsl_Certificate_Chain
     */
    protected $_certificateChain;

    protected $_trustedRootCertificateAuthoritiesFile;
    
    public function __construct()
    {
        $this->_initializeResponse();
    }

    public function setTrustedRootCertificateAuthoritiesFile($file)
    {
        $this->_trustedRootCertificateAuthoritiesFile = $file;
    }

    protected function _initializeResponse()
    {
        $response = new stdClass();
        $response->CertificateChain = array();
        $response->Warnings = array();
        $response->Errors = array();
        $this->_response = $response;
    }
    
    public function serve($entityId)
    {
        if (!$this->_loadEntityCertificate($entityId)) {
            return $this->_sendResponse();
        }

        $this->_checkCertificateValidity();
        $this->_loadCertificateChain();
        $this->_checkChainValidity();
        return $this->_sendResponse();
    }

    protected function _loadEntityCertificate($entityId)
    {
        $janusConfig = SimpleSAML_Configuration::getConfig('module_janus.php');
        $entityController = new sspmod_janus_EntityController($janusConfig);
        $entityController->setEntity($entityId);
        $entityController->loadEntity();
        try {
            $certificate = $entityController->getCertificate();
        } catch(Exception $e) {
            $this->_response->Errors[]   = "Certificate data invalid!";
            return false;
        }
        if (!$certificate) {
            $this->_response->Warnings[] = "No certificate data for this entity";
            return false;
        }
        $this->_certificate = $certificate;
        return true;
    }

    protected function _checkCertificateValidity()
    {
        $validator = new sspmod_janus_OpenSsl_Certificate_Validator($this->_certificate);
        $validator->setIgnoreSelfSigned(true);
        $validator->validate();

        $this->_response->Warnings = array_merge($this->_response->Warnings, $validator->getWarnings());
        $this->_response->Errors   = array_merge($this->_response->Errors,   $validator->getErrors());
    }

    protected function _loadCertificateChain()
{'fraction_non_alphanumeric': 0.12252747252747252, 'fraction_numerical': 0.0005494505494505495, 'mean_word_length': 3.1433990895295905, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 74, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3689148', 'n_tokens_mistral': 1423, 'n_tokens_neox': 1421, 'n_words': 243}