<?php
/*
 * @version $Id: HEADER 15930 2011-10-30 15:47:55Z tsmr $
 -------------------------------------------------------------------------
 Eventlog plugin for GLPI
 Copyright (C) 2003-2011 by the Eventlog Development Team.

 https://forge.indepnet.net/projects/eventlog
 -------------------------------------------------------------------------

 LICENSE
		
 This file is part of Eventlog.

 Eventlog 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, or
 (at your option) any later version.

 Eventlog 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 Eventlog. If not, see <http://www.gnu.org/licenses/>.
 --------------------------------------------------------------------------
 */

define('GLPI_ROOT', '../../..');
include (GLPI_ROOT."/inc/includes.php");
header("Content-Type: text/html; charset=UTF-8");
Html::header_nocache();

$eventlog = new PluginEventlogEventlog();
$eventlog->checkGlobal("r");

Html::header($LANG['plugin_eventlog']['title'][0],$_SERVER['PHP_SELF'],"plugins","eventlog");

$cat=new PluginEventlogTicketCategory();
$know=new PluginEventlogKnowbaseItemCategory();

if (isset($_POST["action"])&&isset($_POST["id"])&&isset($_POST["item"])&&count($_POST["item"])) {

	switch($_POST["action"]) {
		case "delete":

			foreach ($_POST["item"] as $key => $val) {
				if ($val==1) {
					$eventlog->deleteEvent($key,$_POST["eventlogservers_id"]);
			}
		}
		break;
		case "import":
			$date = date("Y-m-d H:i:s");
         foreach ($_POST["item"] as $key => $val) {
            if ($val==1) {
               $eventlog->getfromDBEventlog($key,$_POST["eventlogservers_id"]);

               $input["content"]= Toolbox::encodeInUtf8(addslashes($eventlog->fields["strings"]));
               $input["name"]= $LANG['plugin_eventlog']['title'][0].": ".
               $LANG['plugin_eventlog'][4]." ".$eventlog->fields["log"]." ".
               $LANG['plugin_eventlog'][17]." ".$eventlog->fields["eventID"].
               " ".resume_text($input["content"],20);

               if ($eventlog->GetIDByName($eventlog->fields["computer"])) {
                  $input["itemtype"]= "Computer";
                  $input["items_id"]= $eventlog->GetIDByName($eventlog->fields["computer"]);
               } else {
                  $input["itemtype"]= "0";
               }
               $input["requesttypes_id"]= "6";
               $input["urgency"]= "3";
               $input["_users_id_assign"]=0;
               $input['_groups_id_assign']=0;

               $input["entities_id"]= $_SESSION["glpiactive_entity"];

               if ($cat->getfromDB(1) && $CFG_GLPI["is_ticket_category_mandatory"]==1) {
                  $input["ticketcategories_id"]= $cat->fields["ticketcategories_id"];
               } else {
                  $input["ticketcategories_id"]=0;
               }
               
               $rules = new RuleTicketCollection();
               $input = $rules->processAllRules($input, $input);
               $Ticket = new Ticket();
               $Ticket->add($input);

            }
         }
         break;
		case "kb":
         foreach ($_POST["item"] as $key => $val) {
            if ($val==1) {
;
               $eventlog->getfromDBEventlog($key,$_POST["eventlogservers_id"]);
               $input["computer"]=addslashes($eventlog->fields["computer"]);
               $input["domain"]=addslashes($eventlog->fields["domain"]);
               $input["log"]= addslashes($eventlog->fields["log"]);
               $input["source"]= addslashes($eventlog->fields["source"]);
               $input["eventID"]= addslashes($eventlog->fields["eventID"]);
               $query="INSERT INTO `acks` ( `computer` ,`domain` , `log` , `source` , `eventID` , `timePeriod` )
   VALUES ('".$input["computer"]."','".$input["domain"]."', '".$input["log"]."', '".$input["source"]."', '".$input["eventID"]."', NULL);";
               $result = $DBeventLog->query($query);
               //kb
               $input["is_faq"]=0;
               if ($know->getfromDB(1)) {
                  $input["knowbaseitemcategories_id"]= $know->fields["knowbaseitemcategories_id"];
               }
               $input["question"]= Toolbox::encodeInUtf8(addslashes($eventlog->fields["strings"]));
               $input["entities_id"]= $_SESSION["glpiactive_entity"];
               $kb=new KnowbaseItem();
               $kb->add($input);

            }
         }
         break;
		case "ack":
         foreach ($_POST["item"] as $key => $val) {
            if ($val==1) {
               $eventlog->getfromDBEventlog($key,$_POST["eventlogservers_id"]);
               $input["computer"]=addslashes($eventlog->fields["computer"]);
               $input["domain"]=addslashes($eventlog->fields["domain"]);
               $input["log"]= addslashes($eventlog->fields["log"]);
               $input["source"]= addslashes($eventlog->fields["source"]);
               $input["eventID"]= addslashes($eventlog->fields["eventID"]);
               $query="INSERT INTO `acks` ( `computer` ,`domain` , `log` , `source` , `eventID` , `timePeriod` )
   VALUES ('".$input["computer"]."','".$input["domain"]."', '".$input["log"]."', '".$input["source"]."', '".$input["eventID"]."', NULL);";
               $result = $DBeventLog->query($query);
            }
         }
         break;
		case "desack":
         foreach ($_POST["item"] as $key => $val) {
            if ($val==1) {
               $eventlog->getfromDBEventlog($key,$_POST["eventlogservers_id"]);
               $input["computer"]=addslashes($eventlog->fields["computer"]);
               $input["log"]= addslashes($eventlog->fields["log"]);
               $input["source"]= addslashes($eventlog->fields["source"]);
               $input["eventID"]= addslashes($eventlog->fields["eventID"]);
               $query="DELETE
                     FROM `acks`
                     WHERE `computer` = '".$input["computer"]."'
                     AND `log` = '".$input["log"]."'
                     AND `source` ='".$input["source"]."'
                     AND `eventID` ='".$input["eventID"]."';";
               $result = $DBeventLog->query($query);
            }
         }
         break;
	}

	Session::addMessageAfterRedirect($LANG['common'][23]);
	Html::back();

} else {

	echo "<div align='center'><img src=\"".$CFG_GLPI["root_doc"]."/pics/warning.png\" alt=\"warning\"><br><br>";
	echo "<b>".$LANG['common'][24]."</b></div>";

}

Html::footer();

?>