<commit_before><?php
#  Copyright 2003-2015 Opmantek Limited (www.opmantek.com)
#
#  ALL CODE MODIFICATIONS MUST BE SENT TO <EMAIL>
#
#  This file is part of Open-AudIT.
#
#  Open-AudIT is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Affero General Public License as published
#  by the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  Open-AudIT is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Affero General Public License for more details.
#
#  You should have received a copy of the GNU Affero General Public License
#  along with Open-AudIT (most likely in a file named LICENSE).
#  If not, see <http://www.gnu.org/licenses/>
#
#  For further information on Open-AudIT or for a license other than AGPL please see
#  www.opmantek.com or email <EMAIL>
#
# *****************************************************************************

/**
 * @author <NAME> <<EMAIL>>
 *
 * 
@version 1.14
 *
 * @copyright Copyright (c) 2014, Opmantek
 * @license http://www.gnu.org/licenses/agpl-3.0.html aGPL v3
 */
class M_scripts extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    private function build_properties() {
        $CI = & get_instance();
        $properties = '';
        $temp = explode(',', $CI->response->properties);
        for ($i=0; $i<count($temp); $i++) {
            $temp[$i] = trim($temp[$i]);
        }
        $properties = implode(',', $temp);
        return($properties);
    }

    private function build_filter() {
        $CI = & get_instance();
        $reserved = ' properties limit sub_resource action sort current offset format ';
        $filter = '';
        foreach ($CI->response->filter as $item) {
            if (strpos(' '.$item->name.' ', $reserved) === false) {
                if (!empty($item->name)) {
                    if ($filter != '') {
                        $filter .= ' AND ' . $item->name . ' ' . $item->operator . ' ' . '"' . $item->value . '"';
                    } else {
                        $filter = ' WHERE ' . $item->name . ' ' . $item->operator . ' ' . '"' . $item->value . '"';
                    }
                }
            }
        }
        return($filter);
    }


    public function read()
    {
        $CI = & get_instance();
        $return_data = array();
        $sql = "SELECT * FROM scripts WHERE id = ?";
        $data = array(intval($CI->response->id));
        $result = $this->run_sql($sql, $data);
        $return_data['scripts'] = $result;
        return($return_data);
    }

    public function create()
    {
        $CI = & get_instance();
        # check to see if we already have a script with the same name
        $sql = "SELECT COUNT(id) AS count FROM `scripts` WHERE `name` = ?";
        $data = array($CI->response->post_data['name']);
        $result = $this->run_sql($sql, $data);
        if (intval($result[0]->count) != 0) {
            log_error('ERR-0010', 'm_scripts::create_script');
            return false;
        }
        $sql = "INSERT INTO `scripts` VALUES (NULL, ?, ?, ?, ?, ?, ?, NOW())";
        $data = array(  $CI->response->post_data['name'], 
                        json_encode($CI->response->post_data['options']), 
                        $CI->response->post_data['description'], 
                        $CI->response->post_data['based_on'], 
                        '', 
                        $CI->user->full_name);
        $this->run_sql($sql, $data);
        return $this->db->insert_id();
    }

    public function collection()
    {
        $CI = & get_instance();
        $filter = $this->build_filter();
        $CI->response->internal->filter = $filter;
        $properties = $this->build_properties();
        # get the total number
        $sql = "SELECT count(*) AS count FROM `scripts` " . $filter . " " . $CI->response->internal->groupby;
        $result = $this->run_sql($sql, array());
        $CI->response->total = intval($result[0]->count);

        $sql = "SELECT " . $CI->response->internal->properties . " FROM scripts" . $filter . " " . $CI->response->internal->groupby . " " . $CI->response->internal->sort . " " . $CI->response->internal->limit;
        $result = $this->run_sql($sql, array());
        return $result;
    }

    public function execute($id = 0)
    {
        $CI = & get_instance();
        $script_id = 0;
        if (empty($id)) {
            return;
        }
        $sql = "SELECT * FROM scripts WHERE id = ?";
        $result = $this->run_sql($sql, array(intval($id)));
        $data = $result[0];
        #$filename = dirname(dirname(dirname(dirname(__FILE__)))).'/other/'.$data->based_on;
        $filename = '/usr/local/open-audit/other/' . $data->based_on;
        if (! file_exists($filename)) {
            # TODO - insert an error
            return;
        }
        $file = file_get_contents($filename);
        $options = json_decode($data->options);
        $find = 'Configuration from web UI here';
        foreach ($options as $key => $value) {
            if ($key != 'files') {
                $replace = $find . "\n" . $key . "=\"" . $value . "\"";
                $file = str_replace($find, $replace, $file);
            }
        }

        if (is_array($options->files) and count($options->files) > 0) {
            foreach (array_reverse($options->files) as $key => $value) {
                if ($data->based_on != 'audit_windows.vbs') {
                    $replace = $find . "\nfiles[".intval($key+1)."]=\"" . $value . "\"";
                } else {
                    $replace = $find . "\nfiles(".intval($key+1).")=\"" . $value . "\"";
                }
                $file = str_replace($find, $replace, $file);
            }
            if ($data->based_on == 'audit_windows.vbs') {
                $replace = $find . "\ndim files(".count($options->files).")";
                $file = str_replace($find, $replace, $file);
            }
        }<commit_msg>Some basic code fixes in the scripts model.<commit_after><?php
#  Copyright 2003-2015 Opmantek Limited (www.opmantek.com)
#
#  ALL CODE MODIFICATIONS MUST BE SENT TO <EMAIL>
#
#  This file is part of Open-AudIT.
#
#  Open-AudIT is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Affero General Public License as published
#  by the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  Open-AudIT is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Affero General Public License for more details.
#
#  You should have received a copy of the GNU Affero General Public License
#  along with Open-AudIT (most likely in a file named LICENSE).
#  If not, see <http://www.gnu.org/licenses/>
#
#  For further information on Open-AudIT or for a license other than AGPL please see
#  www.opmantek.com or email <EMAIL>
#
# *****************************************************************************

/**
 * @author <NAME> <<EMAIL>>
 *
 * 
@version 1.14
 *
 * @copyright Copyright (c) 2014, Opmantek
 * @license http://www.gnu.org/licenses/agpl-3.0.html aGPL v3
 */
class M_scripts extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    private function build_properties() {
        $CI = & get_instance();
        $properties = '';
        $temp = explode(',', $CI->response->properties);
        for ($i=0; $i<count($temp); $i++) {
            $temp[$i] = trim($temp[$i]);
        }
        $properties = implode(',', $temp);
        return($properties);
    }

    private function build_filter() {
        $CI = & get_instance();
        $reserved = ' properties limit sub_resource action sort current offset format ';
        $filter = '';
        foreach ($CI->response->filter as $item) {
            if (strpos(' '.$item->name.' ', $reserved) === false) {
                if (!empty($item->name)) {
                    if ($filter != '') {
                        $filter .= ' AND ' . $item->name . ' ' . $item->operator . ' ' . '"' . $item->value . '"';
                    } else {
                        $filter = ' WHERE ' . $item->name . ' ' . $item->operator . ' ' . '"' . $item->value . '"';
                    }
                }
            }
        }
        return($filter);
    }


    public function read()
    {
        $CI = & get_instance();
        $return_data = array();
        $sql = "SELECT * FROM scripts WHERE id = ?";
        $data = array(intval($CI->response->id));
        $result = $this->run_sql($sql, $data);
        $return_data['scripts'] = $result;
        return($return_data);
    }

    public function create()
    {
        $CI = & get_instance();
        # check to see if we already have a script with the same name
        $sql = "SELECT COUNT(id) AS count FROM `scripts` WHERE `name` = ?";
        $data = array($CI->response->post_data['name']);
        $result = $this->run_sql($sql, $data);
        if (intval($result[0]->count) != 0) {
            log_error('ERR-0010', 'm_scripts::create_script');
            return false;
        }
        $sql = "INSERT INTO `scripts` VALUES (NULL, ?, ?, ?, ?, ?, ?, NOW())";
        $data = array(  $CI->response->post_data['name'], 
                        json_encode($CI->response->post_data['options']), 
                        $CI->response->post_data['description'], 
                        $CI->response->post_data['based_on'], 
                        '', 
                        $CI->user->full_name);
        $this->run_sql($sql, $data);
        return $this->db->insert_id();
    }

    public function collection()
    {
        $CI = & get_instance();
        $filter = $this->build_filter();
        $CI->response->internal->filter = $filter;
        $properties = $this->build_properties();
        # get the total number
        $sql = "SELECT count(*) AS count FROM `scripts` " . $filter . " " . $CI->response->internal->groupby;
        $result = $this->run_sql($sql, array());
        $CI->response->total = intval($result[0]->count);

        $sql = "SELECT " . $CI->response->internal->properties . " FROM scripts" . $filter . " " . $CI->response->internal->groupby . " " . $CI->response->internal->sort . " " . $CI->response->internal->limit;
        $result = $this->run_sql($sql, array());
        return $result;
    }

    public function execute($id = 0)
    {
        $CI = & get_instance();
        $script_id = 0;
        if (empty($id)) {
            return;
        }
        $sql = "SELECT * FROM scripts WHERE id = ?";
        $result = $this->run_sql($sql, array(intval($id)));
        $data = $result[0];
        $filename = $CI->config->item('base_path') . '/other/' . $data->based_on;
        if (! file_exists($filename)) {
            # TODO - insert an error
            return;
        }
        $file = file_get_contents($filename);
        $options = json_decode($data->options);

        $find = 'Configuration from web UI here';
        foreach ($options as $key => $value) {
            if ($key != 'files') {
                $replace = $find . "\n" . $key . "=\"" . $value . "\"";
                $file = str_replace($find, $replace, $file);
            }
        }

        if (isset($options->files) and is_array($options->files) and count($options->files) > 0) {
            foreach (array_reverse($options->files) as $key => $value) {
                if ($data->based_on != 'audit_windows.vbs') {
                    $replace = $find . "\nfiles[".intval($key+1)."]=\"" . $value . "\"";
                } else {
                    $replace = $find . "\nfiles(".intval($key+1).")=\"" . $value . "\"";
                }
                $file = str_replace($find, $replace, $file);
            }
            if ($data->based_on == 'audit_windows.vbs') {
                $replace = $find . "\ndim files(".count($options->files).")";
                $file = str_replace($find, $replace, $file);
            }
        }
{'fraction_non_alphanumeric': 0.177888138077017, 'fraction_numerical': 0.005373280143287471, 'mean_word_length': 1.9946367625548513, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 157, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, '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': '2392679', 'n_tokens_mistral': 3686, 'n_tokens_neox': 3546, 'n_words': 896}