Example #1
0
 /**
  * Register new task for plugin (called by plugin during install)
  *
  * @param $itemtype        itemtype of the plugin object
  * @param $name            of the task
  * @param $frequency       of execution
  * @param $options   array of optional options
  *       (state, mode, allowmode, hourmin, hourmax, logs_lifetime, param, comment)
  *
  * @return bool for success
  **/
 public static function Register($itemtype, $name, $frequency, $options = array())
 {
     // Check that hook exists
     if (!isPluginItemType($itemtype)) {
         return false;
     }
     $temp = new self();
     // Avoid duplicate entry
     if ($temp->getFromDBbyName($itemtype, $name)) {
         return false;
     }
     $input = array('itemtype' => $itemtype, 'name' => $name, 'frequency' => $frequency);
     foreach (array('allowmode', 'comment', 'hourmax', 'hourmin', 'logs_lifetime', 'mode', 'param', 'state') as $key) {
         if (isset($options[$key])) {
             $input[$key] = $options[$key];
         }
     }
     return $temp->add($input);
 }
Example #2
0
 /**
  * Register new task for plugin (called by plugin during install)
  *
  * @param $itemtype        itemtype of the plugin object
  * @param $name            of the task
  * @param $frequency       of execution
  * @param $options   array of optional options
  *       (state, mode, allowmode, hourmin, hourmax, logs_lifetime, param, comment)
  *
  * @return bool for success
  **/
 public static function Register($itemtype, $name, $frequency, $options = array())
 {
     // Check that hook exists
     if (!isPluginItemType($itemtype)) {
         return false;
     }
     $temp = new self();
     // Avoid duplicate entry
     if ($temp->getFromDBbyName($itemtype, $name)) {
         return false;
     }
     $input = array('itemtype' => $itemtype, 'name' => $name, 'allowmode' => self::MODE_INTERNAL | self::MODE_EXTERNAL, 'frequency' => $frequency);
     foreach (array('allowmode', 'comment', 'hourmax', 'hourmin', 'logs_lifetime', 'mode', 'param', 'state') as $key) {
         if (isset($options[$key])) {
             $input[$key] = $options[$key];
         }
     }
     if (defined('GLPI_SYSTEM_CRON') && $input['allowmode'] & self::MODE_EXTERNAL && !isset($input['mode'])) {
         // Downstream packages may provide a good system cron
         $input['mode'] = self::MODE_EXTERNAL;
     }
     return $temp->add($input);
 }
 /**
  * @param $params
  */
 static function showSnmpDeviceToAdd($params)
 {
     global $DB, $CFG_GLPI;
     // Default values of parameters
     $p['link'] = array();
     $p['field'] = array();
     $p['contains'] = array();
     $p['searchtype'] = array();
     $p['sort'] = '1';
     $p['order'] = 'ASC';
     $p['start'] = 0;
     $p['export_all'] = 0;
     $p['link2'] = '';
     $p['contains2'] = '';
     $p['field2'] = '';
     $p['itemtype2'] = '';
     $p['searchtype2'] = '';
     $p['itemtype'] = '';
     $p['ip'] = '';
     $p['tolinked'] = 0;
     $p['check'] = 'all';
     $p['plugin_ocsinventoryng_ocsservers_id'] = 0;
     foreach ($params as $key => $val) {
         $p[$key] = $val;
     }
     $tolinked = $p['tolinked'];
     $start = $p['start'];
     $plugin_ocsinventoryng_ocsservers_id = $p['plugin_ocsinventoryng_ocsservers_id'];
     $title = __('Import new SNMP devices', 'ocsinventoryng');
     if ($tolinked) {
         $title = __('Import new SNMP devices into glpi', 'ocsinventoryng');
     }
     $target = $CFG_GLPI['root_doc'] . '/plugins/ocsinventoryng/front/ocsngsnmp.import.php';
     if ($tolinked) {
         $target = $CFG_GLPI['root_doc'] . '/plugins/ocsinventoryng/front/ocsngsnmp.link.php';
     }
     if (!$start) {
         $start = 0;
     }
     // Get all links between glpi and OCS
     $query_glpi = "SELECT ocs_id\n                     FROM `glpi_plugin_ocsinventoryng_snmpocslinks`\n                     WHERE `plugin_ocsinventoryng_ocsservers_id` = '" . $plugin_ocsinventoryng_ocsservers_id . "'";
     $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["ocs_id"];
         }
     }
     $snmpOptions = array('ORDER' => 'LASTDATE', 'FILTER' => array('EXCLUDE_IDS' => $already_linked), 'DISPLAY' => array('CHECKSUM' => PluginOcsinventoryngOcsClient::CHECKSUM_BIOS), 'ORDER' => 'NAME');
     //if ($cfg_ocs["tag_limit"] and $tag_limit = explode("$", trim($cfg_ocs["tag_limit"]))) {
     //   $snmpOptions['FILTER']['TAGS'] = $tag_limit;
     //}
     //if ($cfg_ocs["tag_exclude"] and $tag_exclude = explode("$", trim($cfg_ocs["tag_exclude"]))) {
     //   $snmpOptions['FILTER']['EXCLUDE_TAGS'] = $tag_exclude;
     //}
     $ocsClient = PluginOcsinventoryngOcsServer::getDBocs($plugin_ocsinventoryng_ocsservers_id);
     $ocsResult = $ocsClient->getSnmp($snmpOptions);
     if (isset($ocsResult['SNMP'])) {
         if (count($ocsResult['SNMP'])) {
             // Get all hardware from OCS DB
             $hardware = array();
             $snmp = array_slice($ocsResult['SNMP'], $start, $_SESSION['glpilist_limit']);
             foreach ($snmp as $data) {
                 $data = Toolbox::clean_cross_side_scripting_deep(Toolbox::addslashes_deep($data));
                 $id = $data['META']['ID'];
                 $hardware[$id]["id"] = $data['META']["ID"];
                 $hardware[$id]["date"] = $data['META']["LASTDATE"];
                 $hardware[$id]["name"] = $data['META']["NAME"];
                 $hardware[$id]["ipaddr"] = $data['META']["IPADDR"];
                 $hardware[$id]["snmpdeviceid"] = $data['META']["SNMPDEVICEID"];
                 $hardware[$id]["description"] = $data['META']["DESCRIPTION"];
                 $hardware[$id]["type"] = $data['META']["TYPE"];
                 $hardware[$id]["contact"] = $data['META']["CONTACT"];
                 $hardware[$id]["location"] = $data['META']["LOCATION"];
             }
             foreach ($hardware as $id => $field) {
                 if ($field["type"] == "Network") {
                     $field["type"] = "NetworkEquipment";
                 }
                 if (!empty($p['itemtype']) && $field['type'] != $p['itemtype']) {
                     unset($hardware[$id]);
                 }
                 if (!empty($p['ip']) && !preg_match("/" . $p['ip'] . "/", $field['ipaddr'])) {
                     unset($hardware[$id]);
                 }
             }
             $output_type = Search::HTML_OUTPUT;
             if (isset($_GET["display_type"])) {
                 $output_type = $_GET["display_type"];
             }
             $parameters = "itemtype=" . $p['itemtype'] . "&ip=" . $p['ip'];
             // Define begin and end var for loop
             // Search case
             $begin_display = $start;
             $end_display = $start + $_SESSION["glpilist_limit"];
             $numrows = $ocsResult['TOTAL_COUNT'];
             // Export All case
             if (isset($_GET['export_all'])) {
                 $begin_display = 0;
                 $end_display = $numrows;
             }
             $nbcols = 10;
             if ($output_type == Search::HTML_OUTPUT && $tolinked && count($hardware)) {
                 echo "<div class='center b'>" . __('Caution! The imported data (see your configuration) will overwrite the existing one', 'ocsinventoryng') . "</div>";
             }
             if ($numrows) {
                 $parameters = "";
                 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'
                             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'>";
                      echo "</td></tr>";
                      echo "</table>";
                      Html::closeForm();
                      echo "</div>";
                   }*/
                 if ($output_type == Search::HTML_OUTPUT) {
                     echo "<form method='post' name='ocsng_form' id='ocsng_form' action='{$target}'>";
                 }
                 if ($output_type == Search::HTML_OUTPUT && !$tolinked) {
                     echo "<div class='center'>";
                     PluginOcsinventoryngOcsServer::checkBox($target);
                     echo "</div>";
                 }
                 if ($output_type == Search::HTML_OUTPUT) {
                     echo "<table class='tab_cadrehov'>";
                     echo "<tr class='tab_bg_1'><td colspan='10' class='center'>";
                     if (!$tolinked) {
                         echo "<input class='submit' type='submit' name='import_ok' value=\"" . _sx('button', 'Import', 'ocsinventoryng') . "\">";
                     } else {
                         echo "<input class='submit' type='submit' name='import_ok' value=\"" . _sx('button', 'Link', 'ocsinventoryng') . "\">";
                     }
                     echo "</td></tr>\n";
                 }
                 echo Search::showHeader($output_type, $end_display - $begin_display + 1, $nbcols);
                 echo Search::showNewLine($output_type);
                 $header_num = 1;
                 echo Search::showHeaderItem($output_type, __('Name'), $header_num);
                 //, $linkto, $p['sort']==$val, $p['order']
                 echo Search::showHeaderItem($output_type, __('Description'), $header_num);
                 echo Search::showHeaderItem($output_type, __('IP address'), $header_num);
                 echo Search::showHeaderItem($output_type, __('Date'), $header_num);
                 echo Search::showHeaderItem($output_type, __('Contact SNMP', 'ocsinventoryng'), $header_num);
                 echo Search::showHeaderItem($output_type, __('Location SNMP', 'ocsinventoryng'), $header_num);
                 echo Search::showHeaderItem($output_type, __('Type SNMP', 'ocsinventoryng'), $header_num);
                 if (!$tolinked) {
                     echo Search::showHeaderItem($output_type, __('Item type to create', 'ocsinventoryng'), $header_num, "", 0, "", 'width=15%');
                     echo Search::showHeaderItem($output_type, "", $header_num);
                 } else {
                     echo Search::showHeaderItem($output_type, __('Item to link', 'ocsinventoryng'), $header_num, "", 0, "", 'width=15%');
                 }
                 // End Line for column headers
                 echo Search::showEndLine($output_type);
                 $row_num = 1;
                 foreach ($hardware as $ID => $tab) {
                     $row_num++;
                     $item_num = 1;
                     if ($tab["type"] == "Network") {
                         $tab["type"] = "NetworkEquipment";
                     }
                     echo Search::showNewLine($output_type, $row_num % 2);
                     echo Search::showItem($output_type, $tab["name"], $item_num, $row_num);
                     echo Search::showItem($output_type, $tab["description"], $item_num, $row_num, 'width=15%');
                     echo Search::showItem($output_type, $tab["ipaddr"], $item_num, $row_num, 'width=5%');
                     echo Search::showItem($output_type, Html::convDateTime($tab["date"]), $item_num, $row_num, 'width=15%');
                     echo Search::showItem($output_type, $tab["contact"], $item_num, $row_num, 'width=5%');
                     echo Search::showItem($output_type, $tab["location"], $item_num, $row_num, 'width=15%');
                     echo Search::showItem($output_type, $tab["type"], $item_num, $row_num);
                     if (!$tolinked) {
                         echo "<td width='15%'>";
                         $value = false;
                         if (getItemForItemtype($tab["type"])) {
                             $value = $tab["type"];
                         }
                         $type = "toimport_itemtype[" . $tab["id"] . "]";
                         Dropdown::showItemTypes($type, self::$snmptypes, array('value' => $value));
                         echo "</td>\n";
                     }
                     /* if ($p['change_import_mode'] && !$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='10'>";
                     if (!$tolinked) {
                         echo "<input type='checkbox' name='toimport[" . $tab["id"] . "]' " . ($p['check'] == "all" ? "checked" : "") . ">";
                     } else {
                         /* $tab['entities_id'] = $p['glpiactiveentities'];
                                                $rulelink         = new RuleImportComputerCollection();
                                                $rulelink_results = array();
                                                $params           = array('entities_id' => $p['glpiactiveentities'],
                                                'plugin_ocsinventoryng_ocsservers_id'
                                                => $plugin_ocsinventoryng_ocsservers_id);
                                                $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'] != PluginOcsinventoryngOcsServer::LINK_RESULT_NO_IMPORT){
                         
                                                if (!empty($rulelink_results['found_computers'])){
                                                $options['value']  = $rulelink_results['found_computers'][0];
                                                $options['entity'] = $p['glpiactiveentities'];
                                                } */
                         /* } else{
                            echo "<img src='".$CFG_GLPI['root_doc']. "/pics/redbutton.png'>";
                            } */
                         $value = false;
                         if (getItemForItemtype($tab["type"])) {
                             $type = $tab["type"];
                             $options['name'] = "tolink_items[" . $tab["id"] . "]";
                             $self = new self();
                             if ($item = $self->getFromDBbyName($tab["type"], $tab["name"])) {
                                 $options['value'] = isset($item->fields['id']) ? $item->fields['id'] : false;
                             }
                             $type::dropdown($options);
                             echo "<input type='hidden' name='tolink_itemtype[" . $tab["id"] . "]' value='" . $tab["type"] . "'>";
                         } else {
                             $mtrand = mt_rand();
                             $mynamei = "itemtype";
                             $myname = "tolink_items[" . $tab["id"] . "]";
                             $rand = Dropdown::showItemTypes($mynamei, self::$snmptypes, array('rand' => $mtrand));
                             $p = array('itemtype' => '__VALUE__', 'id' => $tab["id"], 'rand' => $rand, 'myname' => $myname);
                             //print_r($p);
                             Ajax::updateItemOnSelectEvent("dropdown_{$mynamei}{$rand}", "results_{$mynamei}{$rand}", $CFG_GLPI["root_doc"] . "/plugins/ocsinventoryng/ajax/dropdownitems.php", $p);
                             echo "<span id='results_{$mynamei}{$rand}'>\n";
                             echo "</span>\n";
                         }
                     }
                     echo "</td></tr>\n";
                 }
                 echo "<tr class='tab_bg_1'><td colspan='10' class='center'>";
                 if (!$tolinked) {
                     echo "<input class='submit' type='submit' name='import_ok' value=\"" . _sx('button', 'Import', 'ocsinventoryng') . "\">";
                 } else {
                     echo "<input class='submit' type='submit' name='import_ok' value=\"" . _sx('button', 'Link', 'ocsinventoryng') . "\">";
                 }
                 echo "<input type=hidden name='plugin_ocsinventoryng_ocsservers_id' " . "value='" . $plugin_ocsinventoryng_ocsservers_id . "'>";
                 echo "</td></tr>";
                 echo "</table>\n";
                 Html::closeForm();
                 if (!$tolinked) {
                     echo "<div class='center'>";
                     PluginOcsinventoryngOcsServer::checkBox($target);
                     echo "</div>";
                 }
                 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 SNMP device to be imported', 'ocsinventoryng') . "</td></tr>\n";
                 echo "</table>";
             }
             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 SNMP device to be imported', 'ocsinventoryng') . "</td></tr>\n";
             echo "</table></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 SNMP device to be imported', 'ocsinventoryng') . "</td></tr>\n";
         echo "</table></div>";
     }
 }