<commit_before><?php
/*
 * Copyright 2005-2010 MERETHIS
 * 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 MERETHIS
 * 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 MERETHIS choice, provided that
 * MERETHIS 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>
 *
 * SVN : $URL$
 * SVN : $Id$
 *
 */
	if (!isset ($oreon))
		exit ();

	function testContactExistence ($name = NULL)	{
		global $pearDB, $form;
		$id = NULL;
		if (isset($form))
			$id = $form->getSubmitValue('contact_id');
		$DBRESULT =& $pearDB->query("SELECT contact_name, contact_id FROM contact WHERE contact_name = '".htmlentities($name, ENT_QUOTES)."'");
		$contact =& $DBRESULT->fetchRow();
		#Modif case
		if ($DBRESULT->numRows() >= 1 && $contact["contact_id"] == $id)
			return true;
		#Duplicate entry
		else if ($DBRESULT->numRows() >= 1 && $contact["contact_id"] != $id)
			return false;
		else
			return true;
	}

	function testAliasExistence ($alias = NULL)	{
		global $pearDB, $form;
		$id = NULL;
		if (isset($form))
			$id = $form->getSubmitValue('contact_id');
		$DBRESULT =& $pearDB->query("SELECT contact_alias, contact_id FROM contact WHERE contact_alias = '".htmlentities($alias, ENT_QUOTES)."'");
		$contact =& $DBRESULT->fetchRow();
		#Modif case
		if ($DBRESULT->numRows() >= 1 && $contact["contact_id"] == $id)
			return true;
		#Duplicate entry
		else if ($DBRESULT->numRows() >= 1 && $contact["contact_id"] != $id)
			return false;
		else
			return true;
	}

	/**
	 *
	 * Check if a least one contact is enable
	 */
	function keepOneContactAtLeast() {
		global $pearDB, $form;

		$contact_id = $form->getSubmitValue('contact_id');
		(isset($form)) ? $cct_oreon = $form->getSubmitValue('contact_oreon') : $cct_oreon["contact_oreon"] = 0;
		$cct_oreon = $cct_oreon["contact_oreon"];
		(isset($form)) ? $cct_activate = $form->getSubmitValue('contact_activate') : $cct_activate["contact_activate"] = 0;
		$cct_activate = $cct_activate["contact_activate"];

		/*
		 * Get activated contacts
		 */
		$DBRESULT =& $pearDB->query("SELECT COUNT(*) AS nbr_valid FROM contact WHERE contact_activate = '1' AND contact_oreon = '1' AND contact_id <> '$contact_id'");
		$contacts = $DBRESULT->fetchRow();

		if ($contacts["nbr_valid"] == 0) {
			if ($cct_oreon == 0 || $cct_activate == 0) {
				return false;
			}
		}
		return true;
	}

	function enableContactInDB ($contact_id = null, $contact_arr = array())	{
		global $pearDB, $oreon;

		if (!$contact_id && !count($contact_arr))
			return;
		if ($contact_id)
			$contact_arr = array($contact_id=>"1");
		foreach($contact_arr as $key=>$value)	{
			$DBRESULT =& $pearDB->query("UPDATE contact SET contact_activate = '1' WHERE contact_id = '".$key."'");
			$DBRESULT2 =& $pearDB->query("SELECT contact_name FROM `contact` WHERE `contact_id` = '".$key."' LIMIT 1");
			$row = $DBRESULT2->fetchRow();
			$oreon->CentreonLogAction->insertLog("contact", $key, $row['contact_name'], "enable");
		}
	}

	function disableContactInDB ($contact_id = null, $contact_arr = array())	{
		if (!$contact_id && !count($contact_arr)) return;
		global $pearDB, $oreon;
		if ($contact_id)
			$contact_arr = array($contact_id=>"1");
		foreach($contact_arr as $key=>$value)	{
			if (keepOneContactAtLeast())	{
				$DBRESULT =& $pearDB->query("UPDATE contact SET contact_activate = '0' WHERE contact_id = '".$key."'");
				$DBRESULT2 =& $pearDB->query("SELECT contact_name FROM `contact` WHERE `contact_id` = '".$key."' LIMIT 1");
				$row = $DBRESULT2->fetchRow();
				$oreon->CentreonLogAction->insertLog("contact", $key, $row['contact_name'], "disable");
			}
		}
	}

	function deleteContactInDB ($contacts = array())	{
		global $pearDB, $oreon;
		foreach($contacts as $key=>$value)	{
			$DBRESULT2 =& $pearDB->query("SELECT contact_name FROM `contact` WHERE `contact_id` = '".$key."' LIMIT 1");
			$row = $DBRESULT2->fetchRow();

			$DBRESULT =& $pearDB->query("DELETE FROM contact WHERE contact_id = '".$key."'");
			$oreon->CentreonLogAction->insertLog("contact", $key, $row['contact_name'], "d");
		}
	}

	function multipleContactInDB ($contacts = array(), $nbrDup = array())	{
		global $pearDB, $oreon;
		foreach ($contacts as $key=>$value)	{
			$DBRESULT =& $pearDB->query("SELECT * FROM contact WHERE contact_id = '".$key."' LIMIT 1");
			$row = $DBRESULT->fetchRow();
			$row["contact_id"] = '';
			for ($i = 1; $i <= $nbrDup[$key]; $i++)	{
				$val = null;
				foreach ($row as $key2=>$value2)	{
					$key2 == "contact_name" ? ($contact_name = $value2 = $value2."_".$i) : null;
					$key2 == "contact_alias" ? ($contact_alias = $value2 = $value2."_".$i) : null;
					$val ? $val .= ($value2!=NULL?(", '".$value2."'"):", NULL") : $val .= ($value2 != NULL?("'".$value2."'"):"NULL");
					if ($key2 != "contact_id")
						$fields[$key2] = $value2;
					$fields["contact_name"] = $contact_name;
					$fields["contact_alias"] = $contact_alias;
				}
				if (testContactExistence($contact_name) && testAliasExistence($contact_alias))	{
					$val ? $rq = "INSERT INTO contact VALUES (".$val.")" : $rq = null;
					$DBRESULT =& $pearDB->query($rq);
					$DBRESULT =& $pearDB->query("SELECT MAX(contact_id) FROM contact");
					$maxId =& $DBRESULT->fetchRow();
					if (isset($maxId["MAX(contact_id)"]))	{
						$DBRESULT =& $pearDB->query("SELECT DISTINCT command_command_id FROM contact_hostcommands_relation WHERE contact_contact_id = '".$key."'");<commit_msg>fixe problem with check of activated number of contact<commit_after><?php
/*
 * Copyright 2005-2010 MERETHIS
 * 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 MERETHIS
 * 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 MERETHIS choice, provided that
 * MERETHIS 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>
 *
 * SVN : $URL$
 * SVN : $Id$
 *
 */
	if (!isset ($oreon))
		exit ();

	function testContactExistence ($name = NULL)	{
		global $pearDB, $form;
		$id = NULL;
		if (isset($form))
			$id = $form->getSubmitValue('contact_id');
		$DBRESULT =& $pearDB->query("SELECT contact_name, contact_id FROM contact WHERE contact_name = '".htmlentities($name, ENT_QUOTES)."'");
		$contact =& $DBRESULT->fetchRow();
		#Modif case
		if ($DBRESULT->numRows() >= 1 && $contact["contact_id"] == $id)
			return true;
		#Duplicate entry
		else if ($DBRESULT->numRows() >= 1 && $contact["contact_id"] != $id)
			return false;
		else
			return true;
	}

	function testAliasExistence ($alias = NULL)	{
		global $pearDB, $form;
		$id = NULL;
		if (isset($form))
			$id = $form->getSubmitValue('contact_id');
		$DBRESULT =& $pearDB->query("SELECT contact_alias, contact_id FROM contact WHERE contact_alias = '".htmlentities($alias, ENT_QUOTES)."'");
		$contact =& $DBRESULT->fetchRow();
		#Modif case
		if ($DBRESULT->numRows() >= 1 && $contact["contact_id"] == $id)
			return true;
		#Duplicate entry
		else if ($DBRESULT->numRows() >= 1 && $contact["contact_id"] != $id)
			return false;
		else
			return true;
	}

	/**
	 *
	 * Check if a least one contact is enable
	 */
	function keepOneContactAtLeast($ct_id = null) {
		global $pearDB, $form;

		if (isset($contact_id)) {
			$contact_id = $ct_id;
		} else if (isset($_GET["contact_id"])) {
			$contact_id = $_GET["contact_id"];
		} else {
			$contact_id = $form->getSubmitValue('contact_id');
		}

		(isset($form)) ? $cct_oreon = $form->getSubmitValue('contact_oreon') : $cct_oreon["contact_oreon"] = 0;
		$cct_oreon = $cct_oreon["contact_oreon"];
		(isset($form)) ? $cct_activate = $form->getSubmitValue('contact_activate') : $cct_activate["contact_activate"] = 0;
		$cct_activate = $cct_activate["contact_activate"];

		/*
		 * Get activated contacts
		 */
		$DBRESULT =& $pearDB->query("SELECT COUNT(*) AS nbr_valid FROM contact WHERE contact_activate = '1' AND contact_oreon = '1' AND contact_id <> '$contact_id'");
		$contacts = $DBRESULT->fetchRow();

		if ($contacts["nbr_valid"] == 0) {
			if ($cct_oreon == 0 || $cct_activate == 0) {
				return false;
			}
		}
		return true;
	}

	function enableContactInDB ($contact_id = null, $contact_arr = array())	{
		global $pearDB, $oreon;

		if (!$contact_id && !count($contact_arr)) {
			return;
		}
		if ($contact_id) {
			$contact_arr = array($contact_id=>"1");
		}

		foreach ($contact_arr as $key => $value)	{
			$DBRESULT =& $pearDB->query("UPDATE contact SET contact_activate = '1' WHERE contact_id = '".$key."'");
			$DBRESULT2 =& $pearDB->query("SELECT contact_name FROM `contact` WHERE `contact_id` = '".$key."' LIMIT 1");
			$row = $DBRESULT2->fetchRow();
			$oreon->CentreonLogAction->insertLog("contact", $key, $row['contact_name'], "enable");
		}
	}

	function disableContactInDB ($contact_id = null, $contact_arr = array())	{
		global $pearDB, $oreon;

		if (!$contact_id && !count($contact_arr)) {
			return;
		}
		if ($contact_id) {
			$contact_arr = array($contact_id=>"1");
		}

		foreach ($contact_arr as $key => $value) {
			if (keepOneContactAtLeast($key)) {
				$DBRESULT =& $pearDB->query("UPDATE contact SET contact_activate = '0' WHERE contact_id = '".$key."'");
				$DBRESULT2 =& $pearDB->query("SELECT contact_name FROM `contact` WHERE `contact_id` = '".$key."' LIMIT 1");
				$row = $DBRESULT2->fetchRow();
				$oreon->CentreonLogAction->insertLog("contact", $key, $row['contact_name'], "disable");
			}
		}
	}

	function deleteContactInDB ($contacts = array())	{
		global $pearDB, $oreon;

		foreach($contacts as $key => $value) {
			$DBRESULT2 =& $pearDB->query("SELECT contact_name FROM `contact` WHERE `contact_id` = '".$key."' LIMIT 1");
			$row = $DBRESULT2->fetchRow();

			$DBRESULT =& $pearDB->query("DELETE FROM contact WHERE contact_id = '".$key."'");
			$oreon->CentreonLogAction->insertLog("contact", $key, $row['contact_name'], "d");
		}
	}

	function multipleContactInDB ($contacts = array(), $nbrDup = array())	{
		global $pearDB, $oreon;

		foreach ($contacts as $key => $value)	{
			$DBRESULT =& $pearDB->query("SELECT * FROM contact WHERE contact_id = '".$key."' LIMIT 1");
			$row = $DBRESULT->fetchRow();
			$row["contact_id"] = '';
			for ($i = 1; $i <= $nbrDup[$key]; $i++)	{
				$val = null;
				foreach ($row as $key2=>$value2)	{
					$key2 == "contact_name" ? ($contact_name = $value2 = $value2."_".$i) : null;
					$key2 == "contact_alias" ? ($contact_alias = $value2 = $value2."_".$i) : null;
					$val ? $val .= ($value2!=NULL?(", '".$value2."'"):", NULL") : $val .= ($value2 != NULL?("'".$value2."'"):"NULL");
					if ($key2 != "contact_id")
						$fields[$key2] = $value2;
					$fields["contact_name"] = $contact_name;
					$fields["contact_alias"] = $contact_alias;
				}
				if (testContactExistence($contact_name) && testAliasExistence($contact_alias))	{
					$val ? $rq = "INSERT INTO contact VALUES (".$val.")" : $rq = null;
					$DBRESULT =& $pearDB->query($rq);
					$DBRESULT =& $pearDB->query("SELECT MAX(contact_id) FROM contact");
					$maxId =& $DBRESULT->fetchRow();
					if (isset($maxId["MAX(contact_id)"]))	{
						$DBRESULT =& $pearDB->query("SELECT DISTINCT command_command_id FROM contact_hostcommands_relation WHERE contact_contact_id = '".$key."'");
{'fraction_non_alphanumeric': 0.16165602353447658, 'fraction_numerical': 0.0074621511085599486, 'mean_word_length': 4.2576386269332325, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 107, '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': '1792841', 'n_tokens_mistral': 5063, 'n_tokens_neox': 4591, 'n_words': 1322}