<commit_before><?php
/*
 * Copyright 2005-2015 Centreon
 * Centreon is developped by : <NAME> and <NAME> under
 * GPL Licence 2.0.
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation ; either version 2 of the License.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, see <http://www.gnu.org/licenses>.
 *
 * Linking this program statically or dynamically with other modules is making a
 * combined work based on this program. Thus, the terms and conditions of the GNU
 * General Public License cover the whole combination.
 *
 * As a special exception, the copyright holders of this program give Centreon
 * permission to link this program with independent modules to produce an executable,
 * regardless of the license terms of these independent modules, and to copy and
 * distribute the resulting executable under terms of Centreon choice, provided that
 * Centreon also meet, for each linked independent module, the terms  and conditions
 * of the license of that module. An independent module is a module which is not
 * derived from this program. If you modify this program, you may extend this
 * exception to your version of the program, but you are not obliged to do so. If you
 * do not wish to do so, delete this exception statement from your version.
 *
 * For more information : <EMAIL>
 *
 */

if (!isset($centreon)) {
    exit();
}

function testHostDependencyExistence($name = null)
{
    global $pearDB;
    global $form;

    CentreonDependency::purgeObsoleteDependencies($pearDB);

    $id = null;
    if (isset($form)) {
        $id = $form->getSubmitValue('dep_id');
    }
    $query = "SELECT dep_name, dep_id FROM dependency WHERE dep_name = '" . CentreonDB::escape($name) . "'";
    $dbResult = $pearDB->query($query);
    $dep = $dbResult->fetch();

    if ($dbResult->rowCount() >= 1 && $dep["dep_id"] == $id) {
        return true;
    } elseif ($dbResult->rowCount() >= 1 && $dep["dep_id"] != $id) {
        return false;
    } else {
        return true;
    }
}

function testHostDependencyCycle($childs = null)
{
    global $pearDB;
    global $form;
    $parents = array();
    $childs = array();
    if (isset($form)) {
        $parents = $form->getSubmitValue('dep_hostParents');
        $childs = $form->getSubmitValue('dep_hostChilds');
        $childs = array_flip($childs);
    }
    foreach ($parents as $parent) {
        if (array_key_exists($parent, $childs)) {
            return false;
        }
    }
    return true;
}

function deleteHostDependencyInDB($dependencies = array())
{
    global $pearDB, $centreon;

    foreach ($dependencies as $key => $value) {
        $dbResult2 = $pearDB->query("SELECT dep_name FROM `dependency` WHERE `dep_id` = '" . $key . "' LIMIT 1");
        $row = $dbResult2->fetch();
        $dbResult = $pearDB->query("DELETE FROM dependency WHERE dep_id = '" . $key . "'");
        $centreon->CentreonLogAction->insertLog("host dependency", $key, $row['dep_name'], "d");
    }
}

function multipleHostDependencyInDB($dependencies = array(), $nbrDup = array())
{
    foreach ($dependencies as $key => $value) {
        global $pearDB, $centreon;
        $dbResult = $pearDB->query("SELECT * FROM dependency WHERE dep_id = '" . $key . "' LIMIT 1");
        $row = $dbResult->fetch();
        $row["dep_id"] = null;
        for ($i = 1; $i <= $nbrDup[$key]; $i++) {
            $val = null;
            foreach ($row as $key2 => $value2) {
                $key2 == "dep_name" ? ($dep_name = $value2 = $value2 . "_" . $i) : null;
                $val
                    ? $val .= ($value2 != null ? (", '" . $value2 . "'") : ", NULL")
                    : $val .= ($value2 != null ? ("'" . $value2 . "'") : "NULL");
                if ($key2 != "dep_id") {
                    $fields[$key2] = $value2;
                }
                if (isset($dep_name)) {
                    $fields["dep_name"] = $dep_name;
                }
            }
            if (isset($dep_name) && testHostDependencyExistence($dep_name)) {
                $val ? $rq = "INSERT INTO dependency VALUES (" . $val . ")" : $rq = null;
                $pearDB->query($rq);
                $dbResult = $pearDB->query("SELECT MAX(dep_id) FROM dependency");
                $maxId = $dbResult->fetch();
                if (isset($maxId["MAX(dep_id)"])) {
                    $query = "SELECT service_service_id, host_host_id FROM dependency_serviceChild_relation " .
                        "WHERE dependency_dep_id = " . $key;
                    $dbResult = $pearDB->query($query);
                    $fields["dep_serviceChilds"] = "";
                    while ($service = $dbResult->fetch()) {
                        $query = "INSERT INTO dependency_serviceChild_relation VALUES ('" .
                            $maxId["MAX(dep_id)"] . "', '" . $service["service_service_id"] . "', '" .
                            $service["host_host_id"] . "')";
                        $pearDB->query($query);
                        $fields["dep_serviceChilds"] .= $service["host_host_id"] .
                            '-' . $service["service_service_id"] . ",";
                    }

                    $query = "SELECT DISTINCT host_host_id FROM dependency_hostParent_relation " .
                        "WHERE dependency_dep_id = '" . $key . "'";
                    $dbResult = $pearDB->query($query);
                    $fields["dep_hostParents"] = "";
                    while ($host = $dbResult->fetch()) {
                        $query = "INSERT INTO dependency_hostParent_relation " .
                            "VALUES ('" . $maxId["MAX(dep_id)"] . "', '" . $host["host_host_id"] . "')";
                        $pearDB->query($query);
                        $fields["dep_hostParents"] .= $host["host_host_id"] . ",";
                    }
                    $fields["dep_hostParents"] = trim($fields["dep_hostParents"], ",");
                    $dbResult->closeCursor();
                    $query = "SELECT DISTINCT host_host_id FROM dependency_hostChild_relation " .
                        "WHERE dependency_dep_id = '" . $key . "'";
                    $dbResult = $pearDB->query($query);
                    $fields["dep_hostChilds"] = "";
                    while ($host = $dbResult->fetch()) {
                        $query = "INSERT INTO dependency_hostChild_relation " .
                            "VALUES ('" . $maxId["MAX(dep_id)"] . "', '" . $host["host_host_id"] . "')";
                        $pearDB->query($query);
                        $fields["dep_hostChilds"] .= $host["host_host_id"] . ",";
                    }
                    $fields["dep_hostChilds"] = trim($fields["dep_hostChilds"], ",");
                    $dbResult->closeCursor();
                    $centreon->CentreonLogAction->insertLog(
                        "host dependency",
                        $maxId["MAX(dep_id)"],
                        $dep_name,
                        "a",
                        $fields
                    );
                }<commit_msg>[SNYK] Sanitize and bind ACL host dependency queries (#11389)<commit_after><?php
/*
 * Copyright 2005-2015 Centreon
 * Centreon is developped by : <NAME> and <NAME> under
 * GPL Licence 2.0.
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation ; either version 2 of the License.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, see <http://www.gnu.org/licenses>.
 *
 * Linking this program statically or dynamically with other modules is making a
 * combined work based on this program. Thus, the terms and conditions of the GNU
 * General Public License cover the whole combination.
 *
 * As a special exception, the copyright holders of this program give Centreon
 * permission to link this program with independent modules to produce an executable,
 * regardless of the license terms of these independent modules, and to copy and
 * distribute the resulting executable under terms of Centreon choice, provided that
 * Centreon also meet, for each linked independent module, the terms  and conditions
 * of the license of that module. An independent module is a module which is not
 * derived from this program. If you modify this program, you may extend this
 * exception to your version of the program, but you are not obliged to do so. If you
 * do not wish to do so, delete this exception statement from your version.
 *
 * For more information : <EMAIL>
 *
 */

if (!isset($centreon)) {
    exit();
}

function testHostDependencyExistence($name = null)
{
    global $pearDB;
    global $form;

    CentreonDependency::purgeObsoleteDependencies($pearDB);

    $id = null;
    if (isset($form)) {
        $id = $form->getSubmitValue('dep_id');
    }
    $query = "SELECT dep_name, dep_id FROM dependency WHERE dep_name = '" . CentreonDB::escape($name) . "'";
    $dbResult = $pearDB->query($query);
    $dep = $dbResult->fetch();

    if ($dbResult->rowCount() >= 1 && $dep["dep_id"] == $id) {
        return true;
    } elseif ($dbResult->rowCount() >= 1 && $dep["dep_id"] != $id) {
        return false;
    } else {
        return true;
    }
}

function testHostDependencyCycle($childs = null)
{
    global $pearDB;
    global $form;
    $parents = array();
    $childs = array();
    if (isset($form)) {
        $parents = $form->getSubmitValue('dep_hostParents');
        $childs = $form->getSubmitValue('dep_hostChilds');
        $childs = array_flip($childs);
    }
    foreach ($parents as $parent) {
        if (array_key_exists($parent, $childs)) {
            return false;
        }
    }
    return true;
}

function deleteHostDependencyInDB($dependencies = array())
{
    global $pearDB, $centreon;

    foreach ($dependencies as $key => $value) {
        $dbResult2 = $pearDB->query("SELECT dep_name FROM `dependency` WHERE `dep_id` = '" . $key . "' LIMIT 1");
        $row = $dbResult2->fetch();
        $dbResult = $pearDB->query("DELETE FROM dependency WHERE dep_id = '" . $key . "'");
        $centreon->CentreonLogAction->insertLog("host dependency", $key, $row['dep_name'], "d");
    }
}

function multipleHostDependencyInDB($dependencies = array(), $nbrDup = array())
{
    foreach ($dependencies as $key => $value) {
        global $pearDB, $centreon;
        $dbResult = $pearDB->query("SELECT * FROM dependency WHERE dep_id = '" . $key . "' LIMIT 1");
        $row = $dbResult->fetch();
        $row["dep_id"] = null;
        for ($i = 1; $i <= $nbrDup[$key]; $i++) {
            $val = null;
            foreach ($row as $key2 => $value2) {
                $key2 == "dep_name" ? ($dep_name = $value2 = $value2 . "_" . $i) : null;
                $val
                    ? $val .= ($value2 != null ? (", '" . $value2 . "'") : ", NULL")
                    : $val .= ($value2 != null ? ("'" . $value2 . "'") : "NULL");
                if ($key2 != "dep_id") {
                    $fields[$key2] = $value2;
                }
                if (isset($dep_name)) {
                    $fields["dep_name"] = $dep_name;
                }
            }
            if (isset($dep_name) && testHostDependencyExistence($dep_name)) {
                $val ? $rq = "INSERT INTO dependency VALUES (" . $val . ")" : $rq = null;
                $pearDB->query($rq);
                $dbResult = $pearDB->query("SELECT MAX(dep_id) FROM dependency");
                $maxId = $dbResult->fetch();
                if (isset($maxId["MAX(dep_id)"])) {
                    $query = "SELECT service_service_id, host_host_id FROM dependency_serviceChild_relation " .
                        "WHERE dependency_dep_id = " . $key;
                    $dbResult = $pearDB->query($query);
                    $fields["dep_serviceChilds"] = "";
                    $statement = $pearDB->prepare("INSERT INTO dependency_serviceChild_relation " .
                        " VALUES (:max_dep_id, :service_id, :host_host_id)");
                    while ($service = $dbResult->fetch()) {
                        $statement->bindValue(':max_dep_id', (int)$maxId["MAX(dep_id)"], \PDO::PARAM_INT);
                        $statement->bindValue(':service_id', (int)$service["service_service_id"], \PDO::PARAM_INT);
                        $statement->bindValue(':host_host_id', (int)$service["host_host_id"], \PDO::PARAM_INT);
                        $statement->execute();

                        $fields["dep_serviceChilds"] .= $service["host_host_id"] .
                            '-' . $service["service_service_id"] . ",";
                    }

                    $query = "SELECT DISTINCT host_host_id FROM dependency_hostParent_relation " .
                        "WHERE dependency_dep_id = '" . $key . "'";
                    $dbResult = $pearDB->query($query);
                    $fields["dep_hostParents"] = "";
                    $statement = $pearDB->prepare("INSERT INTO dependency_hostParent_relation " .
                        "VALUES (:max_dep_id, :host_host_id)");
                    while ($host = $dbResult->fetch()) {
                        $statement->bindValue(':max_dep_id', (int)$maxId["MAX(dep_id)"], \PDO::PARAM_INT);
                        $statement->bindValue(':host_host_id', (int)$host["host_host_id"], \PDO::PARAM_INT);
                        $statement->execute();
                        $fields["dep_hostParents"] .= $host["host_host_id"] . ",";
                    }
                    $fields["dep_hostParents"] = trim($fields["dep_hostParents"], ",");
                    $dbResult->closeCursor();
                    $query = "SELECT DISTINCT host_host_id FROM dependency_hostChild_relation " .
                        "WHERE dependency_dep_id = '" . $key . "'";
                    $dbResult = $pearDB->query($query);
                    $fields["dep_hostChilds"] = "";
                    $statement = $pearDB->prepare("INSERT INTO dependency_hostChild_relation " .
                        "VALUES (:max_dep_id, :host_host_id)");
                    while ($host = $dbResult->fetch()) {
                        $statement->bindValue(':max_dep_id', (int)$maxId["MAX(dep_id)"], \PDO::PARAM_INT);
                        $statement->bindValue(':host_host_id', (int)$host["host_host_id"], \PDO::PARAM_INT);
                        $statement->execute();
                        $fields["dep_hostChilds"] .= $host["host_host_id"] . ",";
                    }
                    $fields["dep_hostChilds"] = trim($fields["dep_hostChilds"], ",");
                    $dbResult->closeCursor();
                    $centreon->CentreonLogAction->insertLog(
                        "host dependency",
                        $maxId["MAX(dep_id)"],
                        $dep_name,
                        "a",
                        $fields
                    );
                }
{'fraction_non_alphanumeric': 0.13850291349170776, 'fraction_numerical': 0.004162131011077672, 'mean_word_length': 2.0373395565927654, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7662489', 'n_tokens_mistral': 4458, 'n_tokens_neox': 4241, 'n_words': 1133}