<commit_before><?php

include_once APPROOT . 'inc/lib_entity_forms.inc';
include_once 'lib_analysis.inc';

/**
 * @author ks
 *
 */
class analysisModule extends shnModule {

    function section_mod_menu() {
        $data['breadcrumbs'] = shnBreadcrumbs::getBreadcrumbs();
        if ($_GET['act'] == 'search_query')
            $data['active'] = 'query';
        else if ($_GET['act'] == 'adv_search')
            $data['active'] = 'adv_search';
        else if ($_GET['act'] == 'adv_report')
            $data['active'] = 'adv_search';
        return $data;
    }

    function __construct() {
        $this->main_entity = (isset($_POST['main_entity'])) ? $_POST['main_entity'] : $_GET['main_entity'];

        $this->search_entity = (isset($_GET['shuffle_results']) && $_GET['shuffle_results'] != 'all') ? $_GET['shuffle_results'] : $this->main_entity;
    }

    public function _act_search() {
        $this->search_entities = analysis_get_search_entities();
    }

    /**
     *
     * creates shuffle actions relavent to the entity type
     *
     * @access public
     *
     */
    public function shuffle_actions($main_entity) {
        $options = analysis_get_relationship_array();

        foreach ($options as $entity => $option) {
            if ($main_entity == $entity) {
                foreach ($options[$entity] as $key => $value) {
                    $this->shuffle_options[$key] = $value['label'];
                }
            }
        }
    }

    public function address_search_form($fields) {
        $address_index = null;
        foreach ($fields as $key => $field) {
            if ($field['map']['field'] == 'person_addresses') {
                $address_index = $key;
                break;
            }
        }
        $this->address_index = $address_index;

        if ($fields[$address_index] != null) {
            $this->address_form = address_form('search');
            formArrayRefine($this->address_form);
            $this->address_form['person_addresses'] = array('type' => 'hidden', 'extra_opts' => array('value' => 'enabled')); //added to be used when creating sql to inform that there is address in search	
            $this->address_fields = shn_form_get_html_fields($this->address_form);
        }
    }

    public function entity_search_form($entity_type) {
        include_once APPROOT . 'inc/lib_form_util.inc';

        if ($entity_type != null) {
            switch ($entity_type) {
                case 'event':
                    $this->search_header = _t("EVENT_SEARCH_FORM");
                    $this->search_form = event_form('search');
                    formArrayRefine($this->search_form);
                    $this->fields = shn_form_get_html_fields($this->search_form);
                    break;
                case 'person':
                    $this->search_header = _t("PERSON_SEARCH_FORM");
                    $this->search_form = person_form('search');
                    $this->address_search_form($this->search_form);
                    formArrayRefine($this->search_form);
                    $this->fields = shn_form_get_html_fields($this->search_form);

                    break;
                case 'victim':
                    $this->search_header = _t("VICTIM_SEARCH_FORM");
                    $this->search_form = victim_form('search');
                    $this->address_search_form($this->search_form);
                    formArrayRefine($this->search_form);
                    $this->fields = shn_form_get_html_fields($this->search_form);

                    break;
                case 'perpetrator':
                    $this->search_header = _t("PERPETRATOR_SEARCH_FORM");
                    $this->search_form = perpetrator_form('search');
                    $this->address_search_form($this->search_form);
                    formArrayRefine($this->search_form);
                    $this->fields = shn_form_get_html_fields($this->search_form);
                    break;
                case 'source':
                    $this->search_header = _t("SOURCE_SEARCH_FORM");
                    $this->search_form = source_form('search');
                    $this->address_search_form($this->search_form);
                    formArrayRefine($this->search_form);
                    $this->fields = shn_form_get_html_fields($this->search_form);
                    break;
                case 'intervening_party':
                    $this->search_header = _t("INTERVENING_PARTY_SEARCH_FORM");
                    $this->search_form = intervening_party_form('search');
                    $this->address_search_form($this->search_form);
                    formArrayRefine($this->search_form);
                    $this->fields = shn_form_get_html_fields($this->search_form);
                    break;
                case 'information':
                    $this->search_header = _t("INFORMATION_SEARCH_FORM");
                    $this->search_form = information_form('search');
                    formArrayRefine($this->search_form);
                    $this->fields = shn_form_get_html_fields($this->search_form);
                    break;
                case 'intervention':
                    $this->search_header = _t("INTERVENTION_SEARCH_FORM");
                    $this->search_form = intervention_form('search');
                    formArrayRefine($this->search_form);
                    $this->fields = shn_form_get_html_fields($this->search_form);
                    break;
                case 'act':
                    $this->search_header = _t("ACT_SEARCH_FORM");
                    $this->search_form = act_form('search');
                    formArrayRefine($this->search_form);
                    $this->fields = shn_form_get_html_fields($this->search_form);
                    break;
                case 'involvement':
                    $this->search_header = _t("INVOLVEMENT_SEARCH_FORM");
                    $this->search_form = involvement_form('search');
                    formArrayRefine($this->search_form);
                    $this->fields = shn_form_get_html_fields($this->search_form);
                    break;
                case 'supporting_docs_meta':
                    $this->search_header = _t("DOCUMENT_SEARCH_FORM");
                    $this->search_form = document_form('search');
                    formArrayRefine($this->search_form);
                    $this->fields = shn_form_get_html_fields($this->search_form);
                    break;
            }
        }
    }

    /* {{{ Query Functions */

    public function act_save_query() {
        include_once APPROOT . 'inc/lib_uuid.inc';
        if ($_GET['actions'] == 'save_org_sql')
            unset($_GET['shuffle_results']);
        if (isset($_GET['query_save'])) {
            $saveQuery = new SaveQuery();
            $saveQuery->save_query_record_number = shn_create_uuid('query');
            $saveQuery->name = $_GET['query_name'];
            $saveQuery->description = $_GET['query_desc'];
            $saveQuery->created_date = date("Y-m-d");
            $saveQuery->created_by = $_SESSION['username'];
            $query = (isset($_GET['query'])) ? $_GET['query'] : analysis_get_query();<commit_msg>Added input sanitisation to analysis->save query to prevent presistent XSS vulnerability<commit_after><?php

include_once APPROOT . 'inc/lib_entity_forms.inc';
include_once 'lib_analysis.inc';

/**
 * @author ks
 *
 */
class analysisModule extends shnModule {

    function section_mod_menu() {
        $data['breadcrumbs'] = shnBreadcrumbs::getBreadcrumbs();
        if ($_GET['act'] == 'search_query')
            $data['active'] = 'query';
        else if ($_GET['act'] == 'adv_search')
            $data['active'] = 'adv_search';
        else if ($_GET['act'] == 'adv_report')
            $data['active'] = 'adv_search';
        return $data;
    }

    function __construct() {
        $this->main_entity = (isset($_POST['main_entity'])) ? $_POST['main_entity'] : $_GET['main_entity'];

        $this->search_entity = (isset($_GET['shuffle_results']) && $_GET['shuffle_results'] != 'all') ? $_GET['shuffle_results'] : $this->main_entity;
    }

    public function _act_search() {
        $this->search_entities = analysis_get_search_entities();
    }

    /**
     *
     * creates shuffle actions relavent to the entity type
     *
     * @access public
     *
     */
    public function shuffle_actions($main_entity) {
        $options = analysis_get_relationship_array();

        foreach ($options as $entity => $option) {
            if ($main_entity == $entity) {
                foreach ($options[$entity] as $key => $value) {
                    $this->shuffle_options[$key] = $value['label'];
                }
            }
        }
    }

    public function address_search_form($fields) {
        $address_index = null;
        foreach ($fields as $key => $field) {
            if ($field['map']['field'] == 'person_addresses') {
                $address_index = $key;
                break;
            }
        }
        $this->address_index = $address_index;

        if ($fields[$address_index] != null) {
            $this->address_form = address_form('search');
            formArrayRefine($this->address_form);
            $this->address_form['person_addresses'] = array('type' => 'hidden', 'extra_opts' => array('value' => 'enabled')); //added to be used when creating sql to inform that there is address in search	
            $this->address_fields = shn_form_get_html_fields($this->address_form);
        }
    }

    public function entity_search_form($entity_type) {
        include_once APPROOT . 'inc/lib_form_util.inc';

        if ($entity_type != null) {
            switch ($entity_type) {
                case 'event':
                    $this->search_header = _t("EVENT_SEARCH_FORM");
                    $this->search_form = event_form('search');
                    formArrayRefine($this->search_form);
                    $this->fields = shn_form_get_html_fields($this->search_form);
                    break;
                case 'person':
                    $this->search_header = _t("PERSON_SEARCH_FORM");
                    $this->search_form = person_form('search');
                    $this->address_search_form($this->search_form);
                    formArrayRefine($this->search_form);
                    $this->fields = shn_form_get_html_fields($this->search_form);

                    break;
                case 'victim':
                    $this->search_header = _t("VICTIM_SEARCH_FORM");
                    $this->search_form = victim_form('search');
                    $this->address_search_form($this->search_form);
                    formArrayRefine($this->search_form);
                    $this->fields = shn_form_get_html_fields($this->search_form);

                    break;
                case 'perpetrator':
                    $this->search_header = _t("PERPETRATOR_SEARCH_FORM");
                    $this->search_form = perpetrator_form('search');
                    $this->address_search_form($this->search_form);
                    formArrayRefine($this->search_form);
                    $this->fields = shn_form_get_html_fields($this->search_form);
                    break;
                case 'source':
                    $this->search_header = _t("SOURCE_SEARCH_FORM");
                    $this->search_form = source_form('search');
                    $this->address_search_form($this->search_form);
                    formArrayRefine($this->search_form);
                    $this->fields = shn_form_get_html_fields($this->search_form);
                    break;
                case 'intervening_party':
                    $this->search_header = _t("INTERVENING_PARTY_SEARCH_FORM");
                    $this->search_form = intervening_party_form('search');
                    $this->address_search_form($this->search_form);
                    formArrayRefine($this->search_form);
                    $this->fields = shn_form_get_html_fields($this->search_form);
                    break;
                case 'information':
                    $this->search_header = _t("INFORMATION_SEARCH_FORM");
                    $this->search_form = information_form('search');
                    formArrayRefine($this->search_form);
                    $this->fields = shn_form_get_html_fields($this->search_form);
                    break;
                case 'intervention':
                    $this->search_header = _t("INTERVENTION_SEARCH_FORM");
                    $this->search_form = intervention_form('search');
                    formArrayRefine($this->search_form);
                    $this->fields = shn_form_get_html_fields($this->search_form);
                    break;
                case 'act':
                    $this->search_header = _t("ACT_SEARCH_FORM");
                    $this->search_form = act_form('search');
                    formArrayRefine($this->search_form);
                    $this->fields = shn_form_get_html_fields($this->search_form);
                    break;
                case 'involvement':
                    $this->search_header = _t("INVOLVEMENT_SEARCH_FORM");
                    $this->search_form = involvement_form('search');
                    formArrayRefine($this->search_form);
                    $this->fields = shn_form_get_html_fields($this->search_form);
                    break;
                case 'supporting_docs_meta':
                    $this->search_header = _t("DOCUMENT_SEARCH_FORM");
                    $this->search_form = document_form('search');
                    formArrayRefine($this->search_form);
                    $this->fields = shn_form_get_html_fields($this->search_form);
                    break;
            }
        }
    }

    /* {{{ Query Functions */

    public function act_save_query() {
        include_once APPROOT . 'inc/lib_uuid.inc';
        if ($_GET['actions'] == 'save_org_sql')
            unset($_GET['shuffle_results']);
        if (isset($_GET['query_save'])) {
            $saveQuery = new SaveQuery();
            $saveQuery->save_query_record_number = shn_create_uuid('query');
            $saveQuery->name = Reform::HtmlEncode($_GET['query_name']);
            $saveQuery->description = Reform::HtmlEncode($_GET['query_desc']);
            $saveQuery->created_date = date("Y-m-d");
            $saveQuery->created_by = $_SESSION['username'];
            $query = (isset($_GET['query'])) ? $_GET['query'] : analysis_get_query();
{'fraction_non_alphanumeric': 0.13202766554817502, 'fraction_numerical': 0.0, 'mean_word_length': 1.8941736028537455, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 178, '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': '2217085', 'n_tokens_mistral': 4096, 'n_tokens_neox': 3899, 'n_words': 581}