<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>
#
# *****************************************************************************
*
* PHP version 5.3.3
* 
* @category  Model
* @package   Licenses
* @author    <NAME> <<EMAIL>>
* @copyright 2014 Opmantek
* @license   http://www.gnu.org/licenses/agpl-3.0.html aGPL v3
* @version   GIT: Open-AudIT_3.3.2
* @link      http://www.open-audit.org
*/

/**
* Base Model Licenses
*
* @access   public
* @category Model
* @package  Licenses
* @author   <NAME> <<EMAIL>>
* @license  http://www.gnu.org/licenses/agpl-3.0.html aGPL v3
* @link     http://www.open-audit.org
 */
class M_licenses extends MY_Model
{
    /**
    * Constructor
    *
    * @access public
    */
    public function __construct()
    {
        parent::__construct();
        $this->log = new stdClass();
        $this->log->status = 'reading data';
        $this->log->type = 'system';
    }

    /**
     * Create an individual item in the database
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public function create($data = null)
    {
        if ($id = $this->insert_collection('licenses', $data)) {
            return intval($id);
        } else {
            return false;
        }
    }

    /**
     * [read description]
     * @param  integer $id [description]
     * @return [type]      [description]
     */
    public function read($id = 0)
    {
        $id = intval($id);
        $sql = 'SELECT * FROM licenses WHERE id = ?';
        $data = array($id);
        $result = $this->run_sql($sql, $data);
        $CI = & get_instance();
        if ($result[0]->org_descendants === 'n') {
            $sql = "SELECT count(software.name) AS `count` FROM system LEFT JOIN software ON (system.id = software.system_id AND software.current = 'y') WHERE system.org_id = ? AND software.name LIKE ?";
            $data = array(intval($result[0]->org_id), (string)$result[0]->match_string);
            $data_result = $this->run_sql($sql, $data);
            if ( ! empty($data_result[0]->count)) {
                $result[0]->used_count = $data_result[0]->count;
            }
            unset($sql, $data, $data_result);
        } else {
            $children = $CI->m_orgs->get_children($result[0]->org_id);
            $children[] = $result[0]->org_id;
            $children = implode(',', $children);
            $sql = "SELECT count(software.name) AS `count` FROM system LEFT JOIN software ON (system.id = software.system_id AND software.current = 'y') WHERE system.org_id IN ({$children}) AND software.name LIKE ?";
            $data = array((string)$result[0]->match_string);
            $data_result = $this->run_sql($sql, $data);
            if ( ! empty($data_result[0]->count)) {
                $result[0]->used_count = $data_result[0]->count;
            }
            unset($sql, $data, $data_result);
        }
        $result = $this->format_data($result, 'licenses');
        return($result);
    }

    /**
     * Delete an individual item from the database, by ID
     *
     * @param  int $id The ID of the requested item
     * @return bool True = success, False = fail
     */
    public function delete($id = 0)
    {
        $sql = 'DELETE FROM `licenses` WHERE `id` = ?';
        $data = array($id);
        $test = $this->run_sql($sql, $data);
        if ( ! empty($test)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Read the collection from the database
     *
     * @param  int $user_id  The ID of the requesting user, no $response->meta->filter used and no $response->data populated
     * @param  int $response A flag to tell us if we need to use $response->meta->filter and populate $response->data
     * @return bool True = success, False = fail
     */
    public function collection($user_id = null, $response = null)
    {
        $CI = & get_instance();
        if ( ! empty($user_id)) {
            $org_list = array_unique(array_merge($CI->user->orgs, $CI->m_orgs->get_user_descendants($user_id)));
            $sql = 'SELECT * FROM licenses WHERE org_id IN (' . implode(',', $org_list) . ')';
            $result = $this->run_sql($sql, array());
            if (is_array($result) && ! empty($result)) {
                for ($i=0; $i < count($result); $i++) { 
                    $result[$i]->used_count = $this->count($result[$i]->id);
                }
            }
            $result = $this->format_data($result, 'licenses');
            return $result;
        }
        if ( ! empty($response)) {
            $total = $this->collection($CI->user->id);
            $CI->response->meta->total = count($total);
            $sql = "SELECT {$CI->response->meta->internal->properties}, orgs.id AS `orgs.id`, orgs.name AS `orgs.name` FROM licenses LEFT JOIN orgs ON (licenses.org_id = orgs.id) " . 
                    $CI->response->meta->internal->filter . ' ' . 
                    $CI->response->meta->internal->groupby . ' ' . 
                    $CI->response->meta->internal->sort . ' ' . 
                    $CI->response->meta->internal->limit;
            $result = $this->run_sql($sql, array());
            $CI->response->data = $this->format_data($result, 'licenses');
            if (is_array($result) && ! empty($result)) {
                for ($i=0; $i < count($result); $i++) { 
                    $result[$i]->used_count = $this->count($result[$i]->id);
                }
            }
            $CI->response->meta->filtered = count($CI->response->data);
        }
    }

    /**
     * [execute description]
     * @param  integer $id [description]
     * @return [type]      [description]
     */
    public function execute($id = 0)
    {
        $id = intval($id);
        $sql = "SELECT * FROM licenses WHERE id = {$id}";
        $result = $this->run_sql($sql, array());
        if (empty($result)) {
            // TODO log an error, no matching license
            return false;
        } else {
            $license = $result[0];
        }
        $sql = "SELECT system.id AS `system.id`, system.name AS `system.name`, software.name AS `software.name`, software.version AS `software.version` FROM system LEFT JOIN software ON (system.id = software.system_id AND software.current = 'y') WHERE software.name LIKE '" . $license->match_string . "'";
        $result = $this->run_sql($sql, array());
        $result = $this->format_data($result, 'licenses');
        return ($result);
    }

    /**
     * [count description]
     * @param  integer $id [description]
     * @return [type]      [description]
     */
    public function count($id = 0)
    {
        $CI = & get_instance();
        $id = intval($id);
        $sql = "SELECT * FROM licenses WHERE id = {$id}";
        $result = $this->run_sql($sql, array());
        if (empty($result[0])) {
            // TODO log an error, no matching license
            return 0;
        } else {
            $license = $result[0];
        }
        if ( ! empty($license)) {<commit_msg>Fix - Correctly pass parameters to SQL query to avoid SQL injection.<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>
#
# *****************************************************************************
*
* PHP version 5.3.3
* 
* @category  Model
* @package   Licenses
* @author    <NAME> <<EMAIL>>
* @copyright 2014 Opmantek
* @license   http://www.gnu.org/licenses/agpl-3.0.html aGPL v3
* @version   GIT: Open-AudIT_3.3.2
* @link      http://www.open-audit.org
*/

/**
* Base Model Licenses
*
* @access   public
* @category Model
* @package  Licenses
* @author   <NAME> <<EMAIL>>
* @license  http://www.gnu.org/licenses/agpl-3.0.html aGPL v3
* @link     http://www.open-audit.org
 */
class M_licenses extends MY_Model
{
    /**
    * Constructor
    *
    * @access public
    */
    public function __construct()
    {
        parent::__construct();
        $this->log = new stdClass();
        $this->log->status = 'reading data';
        $this->log->type = 'system';
    }

    /**
     * Create an individual item in the database
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public function create($data = null)
    {
        if ($id = $this->insert_collection('licenses', $data)) {
            return intval($id);
        } else {
            return false;
        }
    }

    /**
     * [read description]
     * @param  integer $id [description]
     * @return [type]      [description]
     */
    public function read($id = 0)
    {
        $id = intval($id);
        $sql = 'SELECT * FROM licenses WHERE id = ?';
        $data = array($id);
        $result = $this->run_sql($sql, $data);
        $CI = & get_instance();
        if ($result[0]->org_descendants === 'n') {
            $sql = "SELECT count(software.name) AS `count` FROM system LEFT JOIN software ON (system.id = software.system_id AND software.current = 'y') WHERE system.org_id = ? AND software.name LIKE ?";
            $data = array(intval($result[0]->org_id), (string)$result[0]->match_string);
            $data_result = $this->run_sql($sql, $data);
            if ( ! empty($data_result[0]->count)) {
                $result[0]->used_count = $data_result[0]->count;
            }
            unset($sql, $data, $data_result);
        } else {
            $children = $CI->m_orgs->get_children($result[0]->org_id);
            $children[] = $result[0]->org_id;
            $children = implode(',', $children);
            $sql = "SELECT count(software.name) AS `count` FROM system LEFT JOIN software ON (system.id = software.system_id AND software.current = 'y') WHERE system.org_id IN ({$children}) AND software.name LIKE ?";
            $data = array((string)$result[0]->match_string);
            $data_result = $this->run_sql($sql, $data);
            if ( ! empty($data_result[0]->count)) {
                $result[0]->used_count = $data_result[0]->count;
            }
            unset($sql, $data, $data_result);
        }
        $result = $this->format_data($result, 'licenses');
        return($result);
    }

    /**
     * Delete an individual item from the database, by ID
     *
     * @param  int $id The ID of the requested item
     * @return bool True = success, False = fail
     */
    public function delete($id = 0)
    {
        $sql = 'DELETE FROM `licenses` WHERE `id` = ?';
        $data = array($id);
        $test = $this->run_sql($sql, $data);
        if ( ! empty($test)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Read the collection from the database
     *
     * @param  int $user_id  The ID of the requesting user, no $response->meta->filter used and no $response->data populated
     * @param  int $response A flag to tell us if we need to use $response->meta->filter and populate $response->data
     * @return bool True = success, False = fail
     */
    public function collection($user_id = null, $response = null)
    {
        $CI = & get_instance();
        if ( ! empty($user_id)) {
            $org_list = array_unique(array_merge($CI->user->orgs, $CI->m_orgs->get_user_descendants($user_id)));
            $sql = 'SELECT * FROM licenses WHERE org_id IN (' . implode(',', $org_list) . ')';
            $result = $this->run_sql($sql, array());
            if (is_array($result) && ! empty($result)) {
                for ($i=0; $i < count($result); $i++) { 
                    $result[$i]->used_count = $this->count($result[$i]->id);
                }
            }
            $result = $this->format_data($result, 'licenses');
            return $result;
        }
        if ( ! empty($response)) {
            $total = $this->collection($CI->user->id);
            $CI->response->meta->total = count($total);
            $sql = "SELECT {$CI->response->meta->internal->properties}, orgs.id AS `orgs.id`, orgs.name AS `orgs.name` FROM licenses LEFT JOIN orgs ON (licenses.org_id = orgs.id) " . 
                    $CI->response->meta->internal->filter . ' ' . 
                    $CI->response->meta->internal->groupby . ' ' . 
                    $CI->response->meta->internal->sort . ' ' . 
                    $CI->response->meta->internal->limit;
            $result = $this->run_sql($sql, array());
            $CI->response->data = $this->format_data($result, 'licenses');
            if (is_array($result) && ! empty($result)) {
                for ($i=0; $i < count($result); $i++) { 
                    $result[$i]->used_count = $this->count($result[$i]->id);
                }
            }
            $CI->response->meta->filtered = count($CI->response->data);
        }
    }

    /**
     * [execute description]
     * @param  integer $id [description]
     * @return [type]      [description]
     */
    public function execute($id = 0)
    {
        $id = intval($id);
        $sql = "SELECT * FROM licenses WHERE id = {$id}";
        $result = $this->run_sql($sql, array());
        if (empty($result)) {
            // TODO log an error, no matching license
            return false;
        } else {
            $license = $result[0];
        }
        $sql = "SELECT system.id AS `system.id`, system.name AS `system.name`, software.name AS `software.name`, software.version AS `software.version` FROM system LEFT JOIN software ON (system.id = software.system_id AND software.current = 'y') WHERE software.name LIKE ?";
        $result = $this->run_sql($sql, array($license->match_string));
        $result = $this->format_data($result, 'licenses');
        return ($result);
    }

    /**
     * [count description]
     * @param  integer $id [description]
     * @return [type]      [description]
     */
    public function count($id = 0)
    {
        $CI = & get_instance();
        $id = intval($id);
        $sql = "SELECT * FROM licenses WHERE id = {$id}";
        $result = $this->run_sql($sql, array());
        if (empty($result[0])) {
            // TODO log an error, no matching license
            return 0;
        } else {
            $license = $result[0];
        }
        if ( ! empty($license)) {
{'fraction_non_alphanumeric': 0.16154614712126888, 'fraction_numerical': 0.005869863869114525, 'mean_word_length': 2.1864305610823718, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 187, 'https://': 0, 'lorem ipsum': 0, 'www.': 14, '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': '7651130', 'n_tokens_mistral': 4868, 'n_tokens_neox': 4733, 'n_words': 1283}