<commit_before><?php
if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

/**
 * @package   Jagger
 * @author    Middleware Team HEAnet
 * @author    <NAME> <<EMAIL>>
 * @copyright 2018 HEAnet Limited (http://www.heanet.ie)
 * @license   MIT http://www.opensource.org/licenses/mit-license.php
 */
class Mdq extends MY_Controller
{
    protected static $metaPaths;
    protected static $prefixPath = "/opt/Jagger/";
    public function __construct() {
        parent::__construct();
        $this->load->library('mdqsigner');
        self::$metaPaths = array(
            'circle' => 'signedmetadata/mdq/circle/',
            'entity' => 'signedmetadata/mdq/entity/',
            'federation' => 'signedmetadata/mdq/federation/'
        );
    }

    /**
     * @return bool
     */
    private function isFeatureEnabled(){
        $isEnabled = $this->config->item('mdq')?: false;
        return $isEnabled;
    }


    /**
     * @param \models\Provider $entity
     * @return CI_Output
     */
    private function genMetada(\models\Provider $entity) {
        if (null === $entity) {
            return $this->output->set_status_header(404)->set_output("Not found");
        }
        $entityInSha = sha1($entity->getEntityId());
        $this->load->library(array('providertoxml','mdqsigner'));
        $options['attrs'] = 1;
        $isStatic = $entity->isStaticMetadata();
        $unsignedMetadata = null;
        /**
         * @var \XMLWriter $xmlOut
         */
        if ($isStatic) {

            $unsignedMetadata = $this->regenerateStatic($entity);
            $mem = memory_get_usage();
            $mem = round($mem / 1048576, 2);
            log_message('info', 'Memory usage: ' . $mem . 'M');
            //   $this->load->view('metadata_view', $data);
        } else {
            $xmlOut = $this->providertoxml->entityConvertNewDocument($entity, $options);
            if (!empty($xmlOut)) {
                $unsignedMetadata = $xmlOut->outputMemory();
                $mem = memory_get_usage();
                $mem = round($mem / 1048576, 2);
                log_message('info', 'Memory usage: ' . $mem . 'M');
                //    $this->load->view('metadata_view', $data);
            } else {
                log_message('error', __METHOD__ . ' empty xml has been generated');
                show_error('Internal server error', 500);
            }
        }


        if ($unsignedMetadata !== null) {

            try {
                $signeMetadata = $this->mdqsigner->signXML($unsignedMetadata, null);
            } catch (Exception $e) {
                log_message('error', __METHOD__ . ' ' . $e);

                return $this->output->set_status_header(500)->set_output("Problem with signing");
            }
            try {
                $this->mdqsigner->storeMetadada($entityInSha, $signeMetadata);
            }
            catch (Exception $e){
                log_message('ERROR', __METHOD__.' '.$e);
            }

            return $this->output->set_content_type('application/samlmetadata+xml')->set_output($signeMetadata);


        }

        return $this->output->set_status_header(404)->set_output("Not found");

    }


    private function trustgraph() {
        $this->load->library(array('j_ncache','trustgraph'));
        $cached = $this->j_ncache->getTrustGraph();
        if (is_array($cached)) {
            return $cached;
        }
        $result = $this->trustgraph->genTrustLight();
        $this->j_ncache->saveTrustGraph($result);
        return $result;
    }

    public function trustgraphjson() {

        $result = $this->trustgraph();

        return $this->output->set_content_type('application/json')->set_output(json_encode(array('providers' => $result)));

    }

    private function validForInSecs($days){
        return (86400 * $days);
    }

    private function getmdq($entityid) {

        $this->load->library('mdqsigner');
        $metadata = $this->mdqsigner->getStoredMetadata(sha1($entityid));
        if(null !== $metadata){
            $now = new \DateTime('now', new \DateTimezone('UTC'));
            $diff = (int) $now->format('U') - (int) $metadata['modified'];
            if($diff < $this->validForInSecs(2)) {
                return $this->output->set_content_type('application/samlmetadata+xml')->set_output($metadata['metadata']);
            }
        }

        try {
            $entity = $this->em->getRepository("models\Provider")->findOneBy(array('entityid' => $entityid));
        }
        catch (Exception $e){
            log_message('ERROR',__METHOD__.': '.$e);
            return $this->output->set_status_header(500)->set_output('Internal server error');
        }

        $this->genMetada($entity);
    }<commit_msg>cleaning<commit_after><?php
if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

/**
 * @package   Jagger
 * @author    Middleware Team HEAnet
 * @author    <NAME> <<EMAIL>>
 * @copyright 2018 HEAnet Limited (http://www.heanet.ie)
 * @license   MIT http://www.opensource.org/licenses/mit-license.php
 */
class Mdq extends MY_Controller
{
    public function __construct() {
        parent::__construct();
        $this->load->library('mdqsigner');
    }

    /**
     * @return bool
     */
    private function isFeatureEnabled(){
        $isEnabled = $this->config->item('mdq')?: false;
        return $isEnabled;
    }


    /**
     * @param \models\Provider $entity
     * @return CI_Output
     */
    private function genMetada(\models\Provider $entity) {
        if (null === $entity) {
            return $this->output->set_status_header(404)->set_output("Not found");
        }
        $entityInSha = sha1($entity->getEntityId());
        $this->load->library(array('providertoxml'));
        $options['attrs'] = 1;
        $isStatic = $entity->isStaticMetadata();
        $unsignedMetadata = null;
        /**
         * @var \XMLWriter $xmlOut
         */
        if ($isStatic) {

            $unsignedMetadata = $this->regenerateStatic($entity);
            $mem = memory_get_usage();
            $mem = round($mem / 1048576, 2);
            log_message('info', 'Memory usage: ' . $mem . 'M');
            //   $this->load->view('metadata_view', $data);
        } else {
            $xmlOut = $this->providertoxml->entityConvertNewDocument($entity, $options);
            if (!empty($xmlOut)) {
                $unsignedMetadata = $xmlOut->outputMemory();
                $mem = memory_get_usage();
                $mem = round($mem / 1048576, 2);
                log_message('info', 'Memory usage: ' . $mem . 'M');
                //    $this->load->view('metadata_view', $data);
            } else {
                log_message('error', __METHOD__ . ' empty xml has been generated');
                show_error('Internal server error', 500);
            }
        }


        if ($unsignedMetadata !== null) {

            try {
                $signeMetadata = $this->mdqsigner->signXML($unsignedMetadata, null);
            } catch (Exception $e) {
                log_message('error', __METHOD__ . ' ' . $e);

                return $this->output->set_status_header(500)->set_output("Problem with signing");
            }
            try {
                $this->mdqsigner->storeMetadada($entityInSha, $signeMetadata);
            }
            catch (Exception $e){
                log_message('ERROR', __METHOD__.' '.$e);
            }

            return $this->output->set_content_type('application/samlmetadata+xml')->set_output($signeMetadata);


        }

        return $this->output->set_status_header(404)->set_output("Not found");

    }


    private function trustgraph() {
        $this->load->library(array('j_ncache','trustgraph'));
        $cached = $this->j_ncache->getTrustGraph();
        if (is_array($cached)) {
            return $cached;
        }
        $result = $this->trustgraph->genTrustLight();
        $this->j_ncache->saveTrustGraph($result);
        return $result;
    }

    public function trustgraphjson() {

        $result = $this->trustgraph();

        return $this->output->set_content_type('application/json')->set_output(json_encode(array('providers' => $result)));

    }

    private function validForInSecs($days){
        return (86400 * $days);
    }

    private function getmdq($entityid) {


        $metadata = $this->mdqsigner->getStoredMetadata(sha1($entityid));
        if(null !== $metadata){
            $now = new \DateTime('now', new \DateTimezone('UTC'));
            $diff = (int) $now->format('U') - (int) $metadata['modified'];
            if($diff < $this->validForInSecs(2)) {
                return $this->output->set_content_type('application/samlmetadata+xml')->set_output($metadata['metadata']);
            }
        }

        try {
            $entity = $this->em->getRepository("models\Provider")->findOneBy(array('entityid' => $entityid));
        }
        catch (Exception $e){
            log_message('ERROR',__METHOD__.': '.$e);
            return $this->output->set_status_header(500)->set_output('Internal server error');
        }

        $this->genMetada($entity);
    }
{'fraction_non_alphanumeric': 0.16158636512618813, 'fraction_numerical': 0.00961433409810991, 'mean_word_length': 2.171864171864172, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 134, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 18}, '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': '4645403', 'n_tokens_mistral': 2746, 'n_tokens_neox': 2596, 'n_words': 448}