示例#1
0
/**
 * @see inc/RuleCollection::prepareInputDataForProcess()
 * @since 0.84
 * @param $params           input data
 * @return an array of criteria value to add for processing
 **/
function plugin_ocsinventoryng_ruleCollectionPrepareInputDataForProcess($params)
{
    switch ($params['rule_itemtype']) {
        case 'RuleImportEntity':
        case 'RuleImportComputer':
            if ($params['rule_itemtype'] == 'RuleImportEntity') {
                $ocsservers_id = $params['values']['input']['ocsservers_id'];
            } else {
                $ocsservers_id = $params['values']['params']['plugin_ocsinventoryng_ocsservers_id'];
            }
            $rule_parameters = array('ocsservers_id' => $ocsservers_id, 'OCS_SERVER' => $ocsservers_id);
            if (isset($params['values']['params']['ocsid'])) {
                $ocsid = $params['values']['params']['ocsid'];
            } else {
                if ($params['values']['input']['id']) {
                    $ocsid = $params['values']['input']['id'];
                }
            }
            $ocsClient = PluginOcsinventoryngOcsServer::getDBocs($ocsservers_id);
            $tables = array_keys(plugin_ocsinventoryng_getTablesForQuery($params['rule_itemtype']));
            $fields = plugin_ocsinventoryng_getFieldsForQuery($params['rule_itemtype']);
            $ocsComputer = $ocsClient->getOcsComputer($ocsid, $tables);
            if (!is_null($ocsComputer)) {
                if (isset($ocsComputer['NETWORKS'])) {
                    $networks = $ocsComputer['NETWORKS'];
                    $ipblacklist = Blacklist::getIPs();
                    $macblacklist = Blacklist::getMACs();
                    foreach ($networks as $data) {
                        if (isset($data['IPSUBNET'])) {
                            $rule_parameters['IPSUBNET'][] = $data['IPSUBNET'];
                        }
                        if (isset($data['MACADDR']) && !in_array($data['MACADDR'], $macblacklist)) {
                            $rule_parameters['MACADDRESS'][] = $data['MACADDR'];
                        }
                        if (isset($data['IPADDRESS']) && !in_array($data['IPADDRESS'], $ipblacklist)) {
                            $rule_parameters['IPADDRESS'][] = $data['IPADDRESS'];
                        }
                    }
                }
                $ocs_data = array();
                foreach ($fields as $field) {
                    // TODO cleaner way of getting fields
                    $field = explode('.', $field);
                    if (count($field) < 2) {
                        continue;
                    }
                    $table = strtoupper($field[0]);
                    $fieldSql = explode(' ', $field[1]);
                    $ocsField = $fieldSql[0];
                    $glpiField = $fieldSql[count($fieldSql) - 1];
                    $section = array();
                    if (isset($ocsComputer[$table])) {
                        $section = $ocsComputer[$table];
                    }
                    if (array_key_exists($ocsField, $section)) {
                        // Not multi
                        $ocs_data[$glpiField][] = $section[$ocsField];
                    } else {
                        foreach ($section as $sectionLine) {
                            $ocs_data[$glpiField][] = $sectionLine[$ocsField];
                        }
                    }
                }
                //This case should never happend but...
                //Sometimes OCS can't find network ports but fill the right ip in hardware table...
                //So let's use the ip to proceed rules (if IP is a criteria of course)
                if (in_array("IPADDRESS", $fields) && !isset($ocs_data['IPADDRESS'])) {
                    $ocs_data['IPADDRESS'] = PluginOcsinventoryngOcsServer::getGeneralIpAddress($ocsservers_id, $ocsid);
                }
                return array_merge($rule_parameters, $ocs_data);
            }
    }
    return array();
}
示例#2
0
/**
 * @see inc/RuleCollection::prepareInputDataForProcess()
 * @since 0.84
 * @param $params           input data
 * @return an array of criteria value to add for processing
 **/
function plugin_ocsinventoryng_ruleCollectionPrepareInputDataForProcess($params)
{
    global $PluginOcsinventoryngDBocs;
    switch ($params['rule_itemtype']) {
        case 'RuleImportEntity':
        case 'RuleImportComputer':
            if ($params['rule_itemtype'] == 'RuleImportEntity') {
                $ocsservers_id = $params['values']['input']['ocsservers_id'];
            } else {
                $ocsservers_id = $params['values']['params']['plugin_ocsinventoryng_ocsservers_id'];
            }
            $tables = plugin_ocsinventoryng_getTablesForQuery();
            $fields = plugin_ocsinventoryng_getFieldsForQuery();
            $rule_parameters = array('ocsservers_id' => $ocsservers_id);
            $select_sql = "";
            if (isset($params['values']['params']['ocsid'])) {
                $ocsid = $params['values']['params']['ocsid'];
            } else {
                if ($params['values']['input']['id']) {
                    $ocsid = $params['values']['input']['id'];
                }
            }
            //Get information about network ports
            $query = "SELECT *\n                   FROM `networks`\n                   WHERE `HARDWARE_ID` = '{$ocsid}'";
            $ipblacklist = Blacklist::getIPs();
            $macblacklist = Blacklist::getMACs();
            foreach ($PluginOcsinventoryngDBocs->request($query) as $data) {
                if (isset($data['IPSUBNET'])) {
                    $rule_parameters['IPSUBNET'][] = $data['IPSUBNET'];
                }
                if (isset($data['MACADDR']) && !in_array($data['MACADDR'], $macblacklist)) {
                    $rule_parameters['MACADDRESS'][] = $data['MACADDR'];
                }
                if (isset($data['IPADDRESS']) && !in_array($data['IPADDRESS'], $ipblacklist)) {
                    $rule_parameters['IPADDRESS'][] = $data['IPADDRESS'];
                }
            }
            //Build the select request
            foreach ($fields as $field) {
                switch (Toolbox::strtoupper($field)) {
                    //OCS server ID is provided by extra_params -> get the configuration associated with the ocs server
                    case "OCS_SERVER":
                        $rule_parameters["OCS_SERVER"] = $ocsservers_id;
                        break;
                        //TAG and DOMAIN should come from the OCS DB
                    //TAG and DOMAIN should come from the OCS DB
                    default:
                        $select_sql .= ($select_sql != "" ? " , " : "") . $field;
                }
            }
            //Build the FROM part of the request
            //Remove all the non duplicated table names
            $from_sql = "FROM `hardware` ";
            foreach ($tables as $table => $linkfield) {
                if ($table != 'hardware' && !empty($linkfield)) {
                    $from_sql .= " LEFT JOIN `{$table}` ON (`{$table}`.`{$linkfield}` = `hardware`.`ID`)";
                }
            }
            if ($select_sql != "") {
                //Build the all request
                $sql = "SELECT {$select_sql}\n                    {$from_sql}\n                    WHERE `hardware`.`ID` = '{$ocsid}'";
                PluginOcsinventoryngOcsServer::checkOCSconnection($ocsservers_id);
                $result = $PluginOcsinventoryngDBocs->query($sql);
                $ocs_data = array();
                $fields = plugin_ocsinventoryng_getFieldsForQuery(1);
                //May have more than one line : for example in case of multiple network cards
                if ($PluginOcsinventoryngDBocs->numrows($result) > 0) {
                    while ($datas = $PluginOcsinventoryngDBocs->fetch_assoc($result)) {
                        foreach ($fields as $field) {
                            if ($field != "OCS_SERVER" && isset($datas[$field])) {
                                $ocs_data[$field][] = $datas[$field];
                            }
                        }
                    }
                }
                //This cas should never happend but...
                //Sometimes OCS can't find network ports but fill the right ip in hardware table...
                //So let's use the ip to proceed rules (if IP is a criteria of course)
                if (in_array("IPADDRESS", $fields) && !isset($ocs_data['IPADDRESS'])) {
                    $ocs_data['IPADDRESS'] = PluginOcsinventoryngOcsServer::getGeneralIpAddress($ocsservers_id, $ocsid);
                }
                return array_merge($rule_parameters, $ocs_data);
            }
            return $rule_parameters;
    }
    return array();
}
 /**
  * Display a list of computers to add or to link
  *
  * @param $serverId
  * @param display $advanced
  * @param indicates $check
  * @param display $start
  * @param a|int $entity
  * @param bool|false $tolinked
  * @internal param the $plugin_ocsinventoryng_ocsservers_id ID of the ocs server
  * @internal param display $advanced detail about the computer import or not (target entity, matched rules, etc.)
  * @internal param indicates $check if checkboxes are checked or not
  * @internal param display $start a list of computers starting at rowX
  * @internal param a $entity list of entities in which computers can be added or linked
  * @internal param false $tolinked for an import, true for a link
  *
  * @return bool
  */
 static function showComputersToAdd($serverId, $advanced, $check, $start, $entity = 0, $tolinked = false)
 {
     global $DB, $CFG_GLPI;
     if (!Session::haveRight("plugin_ocsinventoryng", UPDATE)) {
         return false;
     }
     $title = __('Import new computers', 'ocsinventoryng');
     if ($tolinked) {
         $title = __('Link new OCSNG computers to existing GLPI computers', 'ocsinventoryng');
     }
     $target = $CFG_GLPI['root_doc'] . '/plugins/ocsinventoryng/front/ocsng.import.php';
     if ($tolinked) {
         $target = $CFG_GLPI['root_doc'] . '/plugins/ocsinventoryng/front/ocsng.link.php';
     }
     // Get all links between glpi and OCS
     $query_glpi = "SELECT ocsid\n                     FROM `glpi_plugin_ocsinventoryng_ocslinks`\n                     WHERE `plugin_ocsinventoryng_ocsservers_id` = '{$serverId}'";
     $result_glpi = $DB->query($query_glpi);
     $already_linked = array();
     if ($DB->numrows($result_glpi) > 0) {
         while ($data = $DB->fetch_array($result_glpi)) {
             $already_linked[] = $data["ocsid"];
         }
     }
     $cfg_ocs = self::getConfig($serverId);
     $computerOptions = array('ORDER' => 'LASTDATE', 'COMPLETE' => '0', 'DISPLAY' => array('CHECKSUM' => PluginOcsinventoryngOcsClient::CHECKSUM_BIOS | PluginOcsinventoryngOcsClient::CHECKSUM_NETWORK_ADAPTERS), 'ORDER' => 'NAME');
     if ($cfg_ocs["tag_limit"] and $tag_limit = explode("\$", trim($cfg_ocs["tag_limit"]))) {
         $computerOptions['FILTER']['TAGS'] = $tag_limit;
     }
     if ($cfg_ocs["tag_exclude"] and $tag_exclude = explode("\$", trim($cfg_ocs["tag_exclude"]))) {
         $computerOptions['FILTER']['EXCLUDE_TAGS'] = $tag_exclude;
     }
     $ocsClient = self::getDBocs($serverId);
     $ocsResult = $ocsClient->getComputers($computerOptions);
     $computerlink = array();
     if (isset($ocsResult['COMPUTERS'])) {
         if (count($ocsResult['COMPUTERS'])) {
             foreach ($ocsResult['COMPUTERS'] as $comp) {
                 if (!in_array($comp['META']['ID'], $already_linked)) {
                     $computerlink[] = $comp;
                 }
             }
             $computers = array_slice($computerlink, $start, $_SESSION['glpilist_limit']);
         }
     }
     $hardware = array();
     if (isset($computers)) {
         if (count($computers)) {
             // Get all hardware from OCS DB
             foreach ($computers as $data) {
                 $data = Toolbox::clean_cross_side_scripting_deep(Toolbox::addslashes_deep($data));
                 $id = $data['META']['ID'];
                 $hardware[$id]["date"] = $data['META']["LASTDATE"];
                 $hardware[$id]["name"] = $data['META']["NAME"];
                 $hardware[$id]["TAG"] = $data['META']["TAG"];
                 $hardware[$id]["id"] = $data['META']["ID"];
                 $hardware[$id]["UUID"] = $data['META']["UUID"];
                 $contact = $data['META']["USERID"];
                 if (!empty($contact)) {
                     $query = "SELECT `id`\n                            FROM `glpi_users`\n                            WHERE `name` = '" . $contact . "';";
                     $result = $DB->query($query);
                     $hardware[$id]["locations_id"] = 0;
                     if ($DB->numrows($result) == 1) {
                         $user_id = $DB->result($result, 0, 0);
                         $user = new User();
                         $user->getFromDB($user_id);
                         $hardware[$id]["locations_id"] = $user->fields["locations_id"];
                     }
                 }
                 if (isset($data['BIOS']) && count($data['BIOS'])) {
                     $hardware[$id]["serial"] = $data['BIOS']["SSN"];
                     $hardware[$id]["model"] = $data['BIOS']["SMODEL"];
                     $hardware[$id]["manufacturer"] = $data['BIOS']["SMANUFACTURER"];
                 } else {
                     $hardware[$id]["serial"] = '';
                     $hardware[$id]["model"] = '';
                     $hardware[$id]["manufacturer"] = '';
                 }
                 if (isset($data['NETWORKS']) && count($data['NETWORKS'])) {
                     $hardware[$id]["NETWORKS"] = $data["NETWORKS"];
                 }
             }
             if ($tolinked && count($hardware)) {
                 echo "<div class='center b'>" . __('Caution! The imported data (see your configuration) will overwrite the existing one', 'ocsinventoryng') . "</div>";
             }
             echo "<div class='center'>";
             if ($numrows = count($computerlink)) {
                 $parameters = "check={$check}";
                 Html::printPager($start, $numrows, $target, $parameters);
                 //Show preview form only in import even in multi-entity mode because computer import
                 //can be refused by a rule
                 if (!$tolinked) {
                     echo "<div class='firstbloc'>";
                     echo "<form method='post' name='ocsng_import_mode' id='ocsng_import_mode'\n                         action='{$target}'>\n";
                     echo "<table class='tab_cadre_fixe'>";
                     echo "<tr><th>" . __('Manual import mode', 'ocsinventoryng') . "</th></tr>\n";
                     echo "<tr class='tab_bg_1'><td class='center'>";
                     if ($advanced) {
                         Html::showSimpleForm($target, 'change_import_mode', __('Disable preview', 'ocsinventoryng'), array('id' => 'false'));
                     } else {
                         Html::showSimpleForm($target, 'change_import_mode', __('Enable preview', 'ocsinventoryng'), array('id' => 'true'));
                     }
                     echo "</td></tr>";
                     echo "<tr class='tab_bg_1'><td class='center b'>" . __('Check first that duplicates have been correctly managed in OCSNG', 'ocsinventoryng') . "</td>";
                     echo "</tr></table>";
                     Html::closeForm();
                     echo "</div>";
                 }
                 echo "<form method='post' name='ocsng_form' id='ocsng_form' action='{$target}'>";
                 if (!$tolinked) {
                     self::checkBox($target);
                 }
                 echo "<table class='tab_cadre_fixe'>";
                 echo "<tr class='tab_bg_1'><td colspan='" . ($advanced || $tolinked ? 10 : 7) . "' class='center'>";
                 echo "<input class='submit' type='submit' name='import_ok' value=\"" . _sx('button', 'Import', 'ocsinventoryng') . "\">";
                 echo "</td></tr>\n";
                 echo "<tr><th>" . __('Name') . "</th>\n";
                 echo "<th>" . __('Manufacturer') . "</th>\n";
                 echo "<th>" . __('Model') . "</th>\n";
                 echo "<th>" . _n('Information', 'Informations', 2) . "</th>\n";
                 echo "<th>" . __('Date') . "</th>\n";
                 echo "<th>" . __('OCSNG TAG', 'ocsinventoryng') . "</th>\n";
                 if ($advanced && !$tolinked) {
                     echo "<th>" . __('Match the rule ?', 'ocsinventoryng') . "</th>\n";
                     echo "<th>" . __('Destination entity') . "</th>\n";
                     echo "<th>" . __('Target location', 'ocsinventoryng') . "</th>\n";
                 }
                 echo "<th width='20%'>&nbsp;</th></tr>\n";
                 $rule = new RuleImportEntityCollection();
                 foreach ($hardware as $ID => $tab) {
                     $comp = new Computer();
                     $comp->fields["id"] = $tab["id"];
                     $data = array();
                     if ($advanced && !$tolinked) {
                         $location = isset($tab["locations_id"]) ? $tab["locations_id"] : 0;
                         $data = $rule->processAllRules(array('ocsservers_id' => $serverId, '_source' => 'ocsinventoryng', 'locations_id' => $location), array('locations_id' => $location), array('ocsid' => $tab["id"]));
                     }
                     echo "<tr class='tab_bg_2'><td>" . $tab["name"] . "</td>\n";
                     echo "<td>" . $tab["manufacturer"] . "</td>";
                     echo "<td>" . $tab["model"] . "</td>";
                     echo "<td>";
                     $ssnblacklist = Blacklist::getSerialNumbers();
                     $ok = 1;
                     if (!in_array($tab['serial'], $ssnblacklist)) {
                         printf(__('%1$s : %2$s'), __('Serial number'), $tab["serial"]);
                     } else {
                         echo "<span class='red'>";
                         printf(__('%1$s : %2$s'), __('Blacklisted serial number', 'ocsinventoryng'), $tab["serial"]);
                         echo "</span>";
                         $ok = 0;
                     }
                     $uuidblacklist = Blacklist::getUUIDs();
                     if (!in_array($tab['UUID'], $uuidblacklist)) {
                         echo "<br>";
                         printf(__('%1$s : %2$s'), __('UUID'), $tab["UUID"]);
                     } else {
                         echo "<br>";
                         echo "<span class='red'>";
                         printf(__('%1$s : %2$s'), __('Blacklisted UUID', 'ocsinventoryng'), $tab["UUID"]);
                         echo "</span>";
                         $ok = 0;
                     }
                     if (isset($tab['NETWORKS'])) {
                         $networks = $tab['NETWORKS'];
                         $ipblacklist = Blacklist::getIPs();
                         $macblacklist = Blacklist::getMACs();
                         foreach ($networks as $opt) {
                             if (isset($opt['MACADDR'])) {
                                 if (!in_array($opt['MACADDR'], $macblacklist)) {
                                     echo "<br>";
                                     printf(__('%1$s : %2$s'), __('MAC'), $opt['MACADDR']);
                                 } else {
                                     echo "<br>";
                                     echo "<span class='red'>";
                                     printf(__('%1$s : %2$s'), __('Blacklisted MAC', 'ocsinventoryng'), $opt['MACADDR']);
                                     echo "</span>";
                                     //$ok = 0;
                                 }
                                 if (!in_array($opt['IPADDRESS'], $ipblacklist)) {
                                     echo " - ";
                                     printf(__('%1$s : %2$s'), __('IP'), $opt['IPADDRESS']);
                                 } else {
                                     echo " - ";
                                     echo "<span class='red'>";
                                     printf(__('%1$s : %2$s'), __('Blacklisted IP', 'ocsinventoryng'), $opt['IPADDRESS']);
                                     echo "</span>";
                                     //$ok = 0;
                                 }
                             }
                         }
                     }
                     echo "</td>";
                     echo "<td>" . Html::convDateTime($tab["date"]) . "</td>\n";
                     echo "<td>" . $tab["TAG"] . "</td>\n";
                     if ($advanced && !$tolinked) {
                         if (!isset($data['entities_id']) || $data['entities_id'] == -1) {
                             echo "<td class='center'><img src=\"" . $CFG_GLPI['root_doc'] . "/pics/redbutton.png\"></td>\n";
                             $data['entities_id'] = -1;
                         } else {
                             echo "<td class='center'>";
                             $tmprule = new RuleImportEntity();
                             if ($tmprule->can($data['_ruleid'], READ)) {
                                 echo "<a href='" . $tmprule->getLinkURL() . "'>" . $tmprule->getName() . "</a>";
                             } else {
                                 echo $tmprule->getName();
                             }
                             echo "</td>\n";
                         }
                         echo "<td width='30%'>";
                         $ent = "toimport_entities[" . $tab["id"] . "]";
                         Entity::dropdown(array('name' => $ent, 'value' => $data['entities_id'], 'comments' => 0));
                         echo "</td>\n";
                         echo "<td width='30%'>";
                         if (!isset($data['locations_id'])) {
                             $data['locations_id'] = 0;
                         }
                         $loc = "toimport_locations[" . $tab["id"] . "]";
                         Location::dropdown(array('name' => $loc, 'value' => $data['locations_id'], 'comments' => 0));
                         echo "</td>\n";
                     }
                     echo "<td>";
                     if (!$tolinked) {
                         echo "<input type='checkbox' name='toimport[" . $tab["id"] . "]' " . ($check == "all" ? "checked" : "") . ">";
                     } else {
                         $tab['entities_id'] = $entity;
                         $rulelink = new RuleImportComputerCollection();
                         $rulelink_results = array();
                         $params = array('entities_id' => $entity, 'plugin_ocsinventoryng_ocsservers_id' => $serverId);
                         $rulelink_results = $rulelink->processAllRules(Toolbox::stripslashes_deep($tab), array(), $params);
                         //Look for the computer using automatic link criterias as defined in OCSNG configuration
                         $options = array('name' => "tolink[" . $tab["id"] . "]");
                         $show_dropdown = true;
                         //If the computer is not explicitly refused by a rule
                         if (!isset($rulelink_results['action']) || $rulelink_results['action'] != self::LINK_RESULT_NO_IMPORT && $ok) {
                             if (!empty($rulelink_results['found_computers'])) {
                                 $options['value'] = $rulelink_results['found_computers'][0];
                                 $options['entity'] = $entity;
                             }
                             $options['width'] = "100%";
                             Computer::dropdown($options);
                         } else {
                             echo "<img src='" . $CFG_GLPI['root_doc'] . "/pics/redbutton.png'>";
                         }
                     }
                     echo "</td></tr>\n";
                 }
                 echo "<tr class='tab_bg_1'><td colspan='" . ($advanced || $tolinked ? 10 : 7) . "' class='center'>";
                 echo "<input class='submit' type='submit' name='import_ok' value=\"" . _sx('button', 'Import', 'ocsinventoryng') . "\">\n";
                 echo "<input type=hidden name='plugin_ocsinventoryng_ocsservers_id' " . "value='{$serverId}'>";
                 echo "</td></tr>";
                 echo "</table>\n";
                 Html::closeForm();
                 if (!$tolinked) {
                     self::checkBox($target);
                 }
                 Html::printPager($start, $numrows, $target, $parameters);
             } else {
                 echo "<table class='tab_cadre_fixe'>";
                 echo "<tr><th>" . $title . "</th></tr>\n";
                 echo "<tr class='tab_bg_1'>";
                 echo "<td class='center b'>" . __('No new computer to be imported', 'ocsinventoryng') . "</td></tr>\n";
                 echo "</table>";
                 echo "<br><div class='center'>";
                 echo "<a href='" . $CFG_GLPI["root_doc"] . "/plugins/ocsinventoryng/front/ocsng.php'>";
                 _e('Back');
                 echo "</a>";
                 echo "</div>";
             }
             echo "</div>";
         } else {
             echo "<div class='center'>";
             echo "<table class='tab_cadre_fixe'>";
             echo "<tr><th>" . $title . "</th></tr>\n";
             echo "<tr class='tab_bg_1'>";
             echo "<td class='center b'>" . __('No new computer to be imported', 'ocsinventoryng') . "</td></tr>\n";
             echo "</table></div>";
             echo "<br><div class='center'>";
             echo "<a href='" . $CFG_GLPI["root_doc"] . "/plugins/ocsinventoryng/front/ocsng.php'>";
             _e('Back');
             echo "</a>";
             echo "</div>";
         }
     } else {
         echo "<div class='center'>";
         echo "<table class='tab_cadre_fixe'>";
         echo "<tr><th>" . $title . "</th></tr>\n";
         echo "<tr class='tab_bg_1'>";
         echo "<td class='center b'>" . __('No new computer to be imported', 'ocsinventoryng') . "</td></tr>\n";
         echo "</table></div>";
         echo "<br><div class='center'>";
         echo "<a href='" . $CFG_GLPI["root_doc"] . "/plugins/ocsinventoryng/front/ocsng.php'>";
         _e('Back');
         echo "</a>";
         echo "</div>";
     }
 }