function executeActions($output, $params)
 {
     PluginFusioninventoryToolbox::logIfExtradebug("pluginFusioninventory-entityrules", "execute action\n");
     if (count($this->actions)) {
         foreach ($this->actions as $action) {
             switch ($action->fields["action_type"]) {
                 case "assign":
                     PluginFusioninventoryToolbox::logIfExtradebug("pluginFusioninventory-entityrules", "value " . $action->fields["value"] . "\n");
                     $output[$action->fields["field"]] = $action->fields["value"];
                     break;
                 case "regex_result":
                     //Assign entity using the regex's result
                     if ($action->fields["field"] == "_affect_entity_by_tag") {
                         PluginFusioninventoryToolbox::logIfExtradebug("pluginFusioninventory-entityrules", "value " . $action->fields["value"] . "\n");
                         //Get the TAG from the regex's results
                         $res = RuleAction::getRegexResultById($action->fields["value"], $this->regex_results[0]);
                         if (!is_null($res)) {
                             //Get the entity associated with the TAG
                             $target_entity = Entity::getEntityIDByTag($res);
                             if ($target_entity != '') {
                                 $output["entities_id"] = $target_entity;
                             } else {
                                 $output['pass_rule'] = True;
                             }
                         }
                     }
                     break;
             }
         }
     }
     return $output;
 }
 function executeActions($output, $params)
 {
     PluginFusioninventoryToolbox::logIfExtradebug("pluginFusioninventory-locationrules", "execute action\n");
     if (count($this->actions)) {
         foreach ($this->actions as $action) {
             switch ($action->fields["action_type"]) {
                 case "assign":
                     PluginFusioninventoryToolbox::logIfExtradebug("pluginFusioninventory-locationrules", "value " . $action->fields["value"] . "\n");
                     $output[$action->fields["field"]] = $action->fields["value"];
                     break;
                 case "regex_result":
                     $res = '';
                     if (isset($this->regex_results[0])) {
                         $res .= RuleAction::getRegexResultById($action->fields["value"], $this->regex_results[0]);
                     } else {
                         $res .= $action->fields["value"];
                     }
                     if ($res != '') {
                         $entities_id = 0;
                         if (isset($_SESSION["plugin_fusioninventory_entity"]) && $_SESSION["plugin_fusioninventory_entity"] > 0) {
                             $entities_id = $_SESSION["plugin_fusioninventory_entity"];
                         }
                         $res = Dropdown::importExternal(getItemTypeForTable(getTableNameForForeignKeyField($action->fields['field'])), $res, $entities_id);
                     }
                     $output[$action->fields["field"]] = $res;
                     break;
             }
         }
     }
     return $output;
 }
 /**
  * Get URL for module (for REST)
  *
  * @param $module value name of module
  *
  * @return nothing
  *
  **/
 static function getUrlForModule($modulename, $entities_id = -1)
 {
     // Get current entity URL if it exists ...
     $pfEntity = new PluginFusioninventoryEntity();
     $baseUrl = $pfEntity->getValue('agent_base_url', $entities_id);
     if (!empty($baseUrl)) {
         PluginFusioninventoryToolbox::logIfExtradebug("pluginFusioninventory-agent-url", "Entity " . $entities_id . ", agent base URL: " . $baseUrl);
         if ($baseUrl != 'N/A') {
             return $baseUrl . '/plugins/fusioninventory/b/' . strtolower($modulename) . '/';
         }
     }
     // ... else use global plugin configuration parameter.
     $config = new PluginFusioninventoryConfig();
     if (strlen($pfEntity->getValue('agent_base_url', $entities_id)) < 10) {
         PluginFusioninventoryCommunicationRest::sendError();
         exit;
         // die ("agent_base_url is unset!\n");
     }
     PluginFusioninventoryToolbox::logIfExtradebug("pluginFusioninventory-agent-url", "Global configuration URL: " . $pfEntity->getValue('agent_base_url', $entities_id));
     # Construct the path to the JSON back from the agent_base_url.
     # agent_base_url is the initial URL used by the agent
     return $pfEntity->getValue('agent_base_url', $entities_id) . '/plugins/fusioninventory/b/' . strtolower($modulename) . '/';
 }
 /**
  * Execute the actions as defined in the rule
  *
  * @param $output the fields to manipulate
  * @param $params parameters
  *
  * @return the $output array modified
  **/
 function executeActions($output, $params)
 {
     if (isset($params['class'])) {
         $class = $params['class'];
     } else {
         if (isset($_SESSION['plugin_fusioninventory_classrulepassed'])) {
             $classname = $_SESSION['plugin_fusioninventory_classrulepassed'];
             $class = new $classname();
         }
     }
     $pfRulematchedlog = new PluginFusioninventoryRulematchedlog();
     $inputrulelog = array();
     $inputrulelog['date'] = date('Y-m-d H:i:s');
     $inputrulelog['rules_id'] = $this->fields['id'];
     if (!isset($params['return'])) {
         if (isset($_SESSION['plugin_fusioninventory_classrulepassed'])) {
             $inputrulelog['method'] = $class->getMethod();
         }
         if (isset($_SESSION['plugin_fusioninventory_agents_id'])) {
             $inputrulelog['plugin_fusioninventory_agents_id'] = $_SESSION['plugin_fusioninventory_agents_id'];
         }
     }
     PluginFusioninventoryToolbox::logIfExtradebug("pluginFusioninventory-rules", "execute action\n");
     if (count($this->actions)) {
         foreach ($this->actions as $action) {
             if ($action->fields['field'] == '_fusion') {
                 PluginFusioninventoryToolbox::logIfExtradebug("pluginFusioninventory-rules", "value" . $action->fields["value"] . "\n");
                 if ($action->fields["value"] == self::RULE_ACTION_LINK) {
                     if (isset($this->criterias_results['found_equipment'])) {
                         foreach ($this->criterias_results['found_equipment'] as $itemtype => $datas) {
                             $items_id = current($datas);
                             $output['found_equipment'] = array($items_id, $itemtype);
                             if (isset($_SESSION['plugin_fusioninventory_classrulepassed'])) {
                                 if (!isset($params['return'])) {
                                     $inputrulelog['items_id'] = $items_id;
                                     $inputrulelog['itemtype'] = $itemtype;
                                     $pfRulematchedlog->add($inputrulelog);
                                     $pfRulematchedlog->cleanOlddata($items_id, $itemtype);
                                     $class->rulepassed($items_id, $itemtype);
                                 }
                                 return $output;
                             } else {
                                 $_SESSION['plugin_fusioninventory_rules_id'] = $this->fields['id'];
                                 $output['action'] = self::LINK_RESULT_LINK;
                                 return $output;
                             }
                         }
                     } else {
                         // Import into new equipment
                         $itemtype_found = 0;
                         if (count($this->criterias)) {
                             foreach ($this->criterias as $criteria) {
                                 if ($criteria->fields['criteria'] == 'itemtype') {
                                     $itemtype = $criteria->fields['pattern'];
                                     if (isset($_SESSION['plugin_fusioninventory_classrulepassed'])) {
                                         if (!isset($params['return'])) {
                                             $_SESSION['plugin_fusioninventory_rules_id'] = $this->fields['id'];
                                             $class->rulepassed("0", $itemtype);
                                         }
                                         $output['found_equipment'] = array(0, $itemtype);
                                         return $output;
                                     } else {
                                         $_SESSION['plugin_fusioninventory_rules_id'] = $this->fields['id'];
                                         $output['action'] = self::LINK_RESULT_CREATE;
                                         return $output;
                                     }
                                     $itemtype_found = 1;
                                 }
                             }
                         }
                         if ($itemtype_found == "0") {
                             if (isset($_SESSION['plugin_fusioninventory_classrulepassed'])) {
                                 if (!isset($params['return'])) {
                                     $_SESSION['plugin_fusioninventory_rules_id'] = $this->fields['id'];
                                     $class->rulepassed("0", "PluginFusioninventoryUnmanaged");
                                 }
                                 $output['found_equipment'] = array(0, "PluginFusioninventoryUnmanaged");
                                 return $output;
                             } else {
                                 $_SESSION['plugin_fusioninventory_rules_id'] = $this->fields['id'];
                                 $output['action'] = self::LINK_RESULT_CREATE;
                                 return $output;
                             }
                         }
                     }
                 } else {
                     if ($action->fields["value"] == self::RULE_ACTION_DENIED) {
                         $_SESSION['plugin_fusioninventory_rules_id'] = $this->fields['id'];
                         $output['action'] = self::LINK_RESULT_DENIED;
                         return $output;
                     }
                 }
             } else {
                 if ($action->fields['field'] == '_ignore_import') {
                     PluginFusioninventoryToolbox::logIfExtradebug("pluginFusioninventory-rules", "value" . $action->fields["value"] . "\n");
                     $_SESSION['plugin_fusioninventory_rules_id'] = $this->fields['id'];
                     $output['action'] = self::LINK_RESULT_DENIED;
                     return $output;
                 } else {
                     // no import
                     $itemtype_found = 0;
                     if (count($this->criterias)) {
                         foreach ($this->criterias as $criteria) {
                             if ($criteria->fields['criteria'] == 'itemtype') {
                                 $itemtype = $criteria->fields['pattern'];
                                 if (isset($_SESSION['plugin_fusioninventory_classrulepassed'])) {
                                     if (!isset($params['return'])) {
                                         $_SESSION['plugin_fusioninventory_rules_id'] = $this->fields['id'];
                                         $class->rulepassed("0", $itemtype);
                                     }
                                     $output['found_equipment'] = array(0, $itemtype);
                                     return $output;
                                 } else {
                                     $_SESSION['plugin_fusioninventory_rules_id'] = $this->fields['id'];
                                     $output['action'] = self::LINK_RESULT_CREATE;
                                     return $output;
                                 }
                                 $itemtype_found = 1;
                             }
                         }
                     }
                     if ($itemtype_found == "0") {
                         if (isset($_SESSION['plugin_fusioninventory_classrulepassed'])) {
                             if (!isset($params['return'])) {
                                 $_SESSION['plugin_fusioninventory_rules_id'] = $this->fields['id'];
                                 $class->rulepassed("0", "PluginFusioninventoryUnmanaged");
                             }
                             $output['found_equipment'] = array(0, 'PluginFusioninventoryUnmanaged');
                             return $output;
                         } else {
                             $_SESSION['plugin_fusioninventory_rules_id'] = $this->fields['id'];
                             $output['action'] = self::LINK_RESULT_CREATE;
                             return $output;
                         }
                     }
                 }
             }
         }
     }
     return $output;
 }
 /**
  * If rule have found computer or rule give to create computer
  *
  * @param $items_id integer id of the computer found (or 0 if must be created)
  * @param $itemtype value Computer type here
  *
  * @return nothing
  *
  **/
 function rulepassed($items_id, $itemtype)
 {
     global $DB, $PLUGIN_FUSIONINVENTORY_XML, $PF_ESXINVENTORY, $CFG_GLPI;
     PluginFusioninventoryToolbox::logIfExtradebug("pluginFusioninventory-rules", "Rule passed : " . $items_id . ", " . $itemtype . "\n");
     $pfFormatconvert = new PluginFusioninventoryFormatconvert();
     $a_computerinventory = $pfFormatconvert->replaceids($this->arrayinventory);
     $entities_id = $_SESSION["plugin_fusioninventory_entity"];
     if ($itemtype == 'Computer') {
         $pfInventoryComputerLib = new PluginFusioninventoryInventoryComputerLib();
         $pfAgent = new PluginFusioninventoryAgent();
         $computer = new Computer();
         if ($items_id == '0') {
             if ($entities_id == -1) {
                 $entities_id = 0;
                 $_SESSION["plugin_fusioninventory_entity"] = 0;
             }
             $_SESSION['glpiactiveentities'] = array($entities_id);
             $_SESSION['glpiactiveentities_string'] = $entities_id;
             $_SESSION['glpiactive_entity'] = $entities_id;
         } else {
             $computer->getFromDB($items_id);
             $a_computerinventory['Computer']['states_id'] = $computer->fields['states_id'];
             $input = array();
             PluginFusioninventoryInventoryComputerInventory::addDefaultStateIfNeeded($input);
             if (isset($input['states_id'])) {
                 $a_computerinventory['Computer']['states_id'] = $input['states_id'];
             }
             if ($entities_id == -1) {
                 $entities_id = $computer->fields['entities_id'];
                 $_SESSION["plugin_fusioninventory_entity"] = $computer->fields['entities_id'];
             }
             $_SESSION['glpiactiveentities'] = array($entities_id);
             $_SESSION['glpiactiveentities_string'] = $entities_id;
             $_SESSION['glpiactive_entity'] = $entities_id;
             if ($computer->fields['entities_id'] != $entities_id) {
                 $pfEntity = new PluginFusioninventoryEntity();
                 $pfInventoryComputerComputer = new PluginFusioninventoryInventoryComputerComputer();
                 $moveentity = FALSE;
                 if ($pfEntity->getValue('transfers_id_auto', $computer->fields['entities_id']) > 0) {
                     if (!$pfInventoryComputerComputer->getLock($items_id)) {
                         $moveentity = TRUE;
                     }
                 }
                 if ($moveentity) {
                     $pfEntity = new PluginFusioninventoryEntity();
                     $transfer = new Transfer();
                     $transfer->getFromDB($pfEntity->getValue('transfers_id_auto', $entities_id));
                     $item_to_transfer = array("Computer" => array($items_id => $items_id));
                     $transfer->moveItems($item_to_transfer, $entities_id, $transfer->fields);
                 } else {
                     $_SESSION["plugin_fusioninventory_entity"] = $computer->fields['entities_id'];
                     $_SESSION['glpiactiveentities'] = array($computer->fields['entities_id']);
                     $_SESSION['glpiactiveentities_string'] = $computer->fields['entities_id'];
                     $_SESSION['glpiactive_entity'] = $computer->fields['entities_id'];
                     $entities_id = $computer->fields['entities_id'];
                 }
             }
         }
         $a_computerinventory = $pfFormatconvert->extraCollectInfo($a_computerinventory, $items_id);
         $a_computerinventory = $pfFormatconvert->computerSoftwareTransformation($a_computerinventory, $entities_id);
         $no_history = FALSE;
         // * New
         $setdynamic = 1;
         if ($items_id == '0') {
             $input = array();
             $input['entities_id'] = $entities_id;
             PluginFusioninventoryInventoryComputerInventory::addDefaultStateIfNeeded($input);
             if (isset($input['states_id'])) {
                 $a_computerinventory['Computer']['states_id'] = $input['states_id'];
             } else {
                 $a_computerinventory['Computer']['states_id'] = 0;
             }
             $items_id = $computer->add($input);
             $no_history = TRUE;
             $setdynamic = 0;
         }
         if (isset($_SESSION['plugin_fusioninventory_locations_id'])) {
             $a_computerinventory['Computer']['locations_id'] = $_SESSION['plugin_fusioninventory_locations_id'];
             unset($_SESSION['plugin_fusioninventory_locations_id']);
         }
         $serialized = gzcompress(serialize($a_computerinventory));
         $a_computerinventory['fusioninventorycomputer']['serialized_inventory'] = Toolbox::addslashes_deep($serialized);
         if (!$PF_ESXINVENTORY) {
             $pfAgent->setAgentWithComputerid($items_id, $this->device_id, $entities_id);
         }
         $pfConfig = new PluginFusioninventoryConfig();
         $query = "INSERT INTO `glpi_plugin_fusioninventory_dblockinventories`\n            SET `value`='" . $items_id . "'";
         $CFG_GLPI["use_log_in_files"] = FALSE;
         if (!$DB->query($query)) {
             $communication = new PluginFusioninventoryCommunication();
             $communication->setMessage("<?xml version='1.0' encoding='UTF-8'?>\n         <REPLY>\n         <ERROR>ERROR: SAME COMPUTER IS CURRENTLY UPDATED</ERROR>\n         </REPLY>");
             $communication->sendMessage($_SESSION['plugin_fusioninventory_compressmode']);
             exit;
         }
         $CFG_GLPI["use_log_in_files"] = TRUE;
         // * For benchs
         //$start = microtime(TRUE);
         PluginFusioninventoryInventoryComputerStat::increment();
         $pfInventoryComputerLib->updateComputer($a_computerinventory, $items_id, $no_history, $setdynamic);
         $query = "DELETE FROM `glpi_plugin_fusioninventory_dblockinventories`\n               WHERE `value`='" . $items_id . "'";
         $DB->query($query);
         $plugin = new Plugin();
         if ($plugin->isActivated('monitoring')) {
             Plugin::doOneHook("monitoring", "ReplayRulesForItem", array('Computer', $items_id));
         }
         // * For benchs
         //Toolbox::logInFile("exetime", (microtime(TRUE) - $start)." (".$items_id.")\n".
         //  memory_get_usage()."\n".
         //  memory_get_usage(TRUE)."\n".
         //  memory_get_peak_usage()."\n".
         //  memory_get_peak_usage()."\n");
         if (isset($_SESSION['plugin_fusioninventory_rules_id'])) {
             $pfRulematchedlog = new PluginFusioninventoryRulematchedlog();
             $inputrulelog = array();
             $inputrulelog['date'] = date('Y-m-d H:i:s');
             $inputrulelog['rules_id'] = $_SESSION['plugin_fusioninventory_rules_id'];
             if (isset($_SESSION['plugin_fusioninventory_agents_id'])) {
                 $inputrulelog['plugin_fusioninventory_agents_id'] = $_SESSION['plugin_fusioninventory_agents_id'];
             }
             $inputrulelog['items_id'] = $items_id;
             $inputrulelog['itemtype'] = $itemtype;
             $inputrulelog['method'] = 'inventory';
             $pfRulematchedlog->add($inputrulelog, array(), FALSE);
             $pfRulematchedlog->cleanOlddata($items_id, $itemtype);
             unset($_SESSION['plugin_fusioninventory_rules_id']);
         }
         // Write XML file
         if (!empty($PLUGIN_FUSIONINVENTORY_XML)) {
             PluginFusioninventoryToolbox::writeXML($items_id, $PLUGIN_FUSIONINVENTORY_XML->asXML(), 'computer');
         }
     } else {
         if ($itemtype == 'PluginFusioninventoryUnmanaged') {
             $a_computerinventory = $pfFormatconvert->computerSoftwareTransformation($a_computerinventory, $entities_id);
             $class = new $itemtype();
             if ($items_id == "0") {
                 if ($entities_id == -1) {
                     $entities_id = 0;
                     $_SESSION["plugin_fusioninventory_entity"] = 0;
                 }
                 $input = array();
                 $input['date_mod'] = date("Y-m-d H:i:s");
                 $items_id = $class->add($input);
                 if (isset($_SESSION['plugin_fusioninventory_rules_id'])) {
                     $pfRulematchedlog = new PluginFusioninventoryRulematchedlog();
                     $inputrulelog = array();
                     $inputrulelog['date'] = date('Y-m-d H:i:s');
                     $inputrulelog['rules_id'] = $_SESSION['plugin_fusioninventory_rules_id'];
                     if (isset($_SESSION['plugin_fusioninventory_agents_id'])) {
                         $inputrulelog['plugin_fusioninventory_agents_id'] = $_SESSION['plugin_fusioninventory_agents_id'];
                     }
                     $inputrulelog['items_id'] = $items_id;
                     $inputrulelog['itemtype'] = $itemtype;
                     $inputrulelog['method'] = 'inventory';
                     $pfRulematchedlog->add($inputrulelog);
                     $pfRulematchedlog->cleanOlddata($items_id, $itemtype);
                     unset($_SESSION['plugin_fusioninventory_rules_id']);
                 }
             }
             $class->getFromDB($items_id);
             $_SESSION["plugin_fusioninventory_entity"] = $class->fields['entities_id'];
             $input = array();
             $input['id'] = $class->fields['id'];
             // Write XML file
             if (!empty($PLUGIN_FUSIONINVENTORY_XML)) {
                 PluginFusioninventoryToolbox::writeXML($items_id, $PLUGIN_FUSIONINVENTORY_XML->asXML(), 'PluginFusioninventoryUnmanaged');
             }
             if (isset($a_computerinventory['Computer']['name'])) {
                 $input['name'] = $a_computerinventory['Computer']['name'];
             }
             $input['item_type'] = "Computer";
             if (isset($a_computerinventory['Computer']['domains_id'])) {
                 $input['domain'] = $a_computerinventory['Computer']['domains_id'];
             }
             if (isset($a_computerinventory['Computer']['serial'])) {
                 $input['serial'] = $a_computerinventory['Computer']['serial'];
             }
             $class->update($input);
         }
     }
 }
 function prepareInputDataForProcess($input, $params)
 {
     PluginFusioninventoryToolbox::logIfExtradebug("pluginFusioninventory-locationrules", "execute action\n" . print_r($input, TRUE) . "\n" . print_r($params, TRUE));
     return $input;
 }
 /**
  * Import data
  *
  * @param $arrayinventory array to import
  *
  * @return TRUE (import ok) / FALSE (import ko)
  **/
 function import($arrayinventory)
 {
     $pfAgent = new PluginFusioninventoryAgent();
     PluginFusioninventoryToolbox::logIfExtradebug('pluginFusioninventory-communication', 'Function import().');
     $this->message = $arrayinventory;
     $errors = '';
     $xmltag = $this->message['QUERY'];
     if ($xmltag == "NETDISCOVERY") {
         $xmltag = "NETWORKDISCOVERY";
     }
     if ($xmltag == "SNMPQUERY" or $xmltag == "SNMPINVENTORY") {
         $xmltag = "NETWORKINVENTORY";
     }
     if (!isset($_SESSION['plugin_fusioninventory_agents_id'])) {
         $agent = $pfAgent->InfosByKey($this->message['DEVICEID']);
     } else {
         $agent = array('id' => $_SESSION['plugin_fusioninventory_agents_id']);
     }
     if ($xmltag == "PROLOG") {
         return FALSE;
     }
     if (isset($this->message['CONTENT']['MODULEVERSION'])) {
         $pfAgent->setAgentVersions($agent['id'], $xmltag, $this->message['CONTENT']['MODULEVERSION']);
     } else {
         if (isset($this->message['CONTENT']['VERSIONCLIENT'])) {
             $version = str_replace("FusionInventory-Agent_", "", $this->message['CONTENT']['VERSIONCLIENT']);
             $pfAgent->setAgentVersions($agent['id'], $xmltag, $version);
         }
     }
     if (isset($this->message->CONTENT->MODULEVERSION)) {
         $pfAgent->setAgentVersions($agent['id'], $xmltag, (string) $this->message->CONTENT->MODULEVERSION);
     } else {
         if (isset($this->message->CONTENT->VERSIONCLIENT)) {
             $version = str_replace("FusionInventory-Agent_", "", (string) $this->message->CONTENT->VERSIONCLIENT);
             $pfAgent->setAgentVersions($agent['id'], $xmltag, $version);
         }
     }
     if (isset($_SESSION['glpi_plugin_fusioninventory']['xmltags']["{$xmltag}"])) {
         $moduleClass = $_SESSION['glpi_plugin_fusioninventory']['xmltags']["{$xmltag}"];
         $moduleCommunication = new $moduleClass();
         $errors .= $moduleCommunication->import($this->message['DEVICEID'], $this->message['CONTENT'], $arrayinventory);
     } else {
         $errors .= __('Unattended element in', 'fusioninventory') . ' QUERY : *' . $xmltag . "*\n";
     }
     $result = TRUE;
     // TODO manage this error ( = delete it)
     if ($errors != '') {
         echo $errors;
         if (isset($_SESSION['glpi_plugin_fusioninventory_processnumber'])) {
             $result = TRUE;
         } else {
             // It's PROLOG
             $result = FALSE;
         }
     }
     return $result;
 }