/**
  * Manage login redirection
  *
  * @param $where string: where to redirect ?
  **/
 static function manageRedirect($where)
 {
     global $CFG_GLPI, $PLUGIN_HOOKS;
     if (!empty($where)) {
         if (isset($_SESSION["glpiactiveprofile"]["interface"]) && !empty($_SESSION["glpiactiveprofile"]["interface"])) {
             $decoded_where = rawurldecode($where);
             // redirect to URL : URL must be rawurlencoded
             if ($link = preg_match('/https?:\\/\\/.+/', $decoded_where)) {
                 Html::redirect($decoded_where);
             }
             // Redirect based on GLPI_ROOT : URL must be rawurlencoded
             if ($decoded_where[0] == '/') {
                 //                echo $decoded_where;exit();
                 Html::redirect($CFG_GLPI["root_doc"] . $decoded_where);
             }
             $data = explode("_", $where);
             $forcetab = '';
             // forcetab for simple items
             if (isset($data[1])) {
                 $forcetab = 'forcetab=' . $data[1];
             }
             if (isset($data[2])) {
                 $forcetab = 'forcetab=' . $data[2];
             }
             switch ($_SESSION["glpiactiveprofile"]["interface"]) {
                 case "helpdesk":
                     switch (strtolower($data[0])) {
                         // Use for compatibility with old name
                         case "tracking":
                         case "ticket":
                             $data[0] = 'Ticket';
                             // redirect to item
                             if (isset($data[1]) && is_numeric($data[1]) && $data[1] > 0) {
                                 // Check entity
                                 if (($item = getItemForItemtype($data[0])) && $item->isEntityAssign()) {
                                     if ($item->getFromDB($data[1])) {
                                         if (!Session::haveAccessToEntity($item->getEntityID())) {
                                             Session::changeActiveEntities($item->getEntityID(), 1);
                                         }
                                     }
                                 }
                                 Html::redirect($CFG_GLPI["root_doc"] . "/front/ticket.form.php?id=" . $data[1] . "&{$forcetab}");
                                 // redirect to list
                             } else {
                                 if (!empty($data[0])) {
                                     if ($item = getItemForItemtype($data[0])) {
                                         Html::redirect($item->getSearchURL() . "?{$forcetab}");
                                     }
                                 }
                             }
                             Html::redirect($CFG_GLPI["root_doc"] . "/front/helpdesk.public.php");
                             break;
                         case "preference":
                             Html::redirect($CFG_GLPI["root_doc"] . "/front/preference.php?{$forcetab}");
                             break;
                         default:
                             Html::redirect($CFG_GLPI["root_doc"] . "/front/helpdesk.public.php");
                             break;
                     }
                     break;
                 case "central":
                     switch (strtolower($data[0])) {
                         case "preference":
                             Html::redirect($CFG_GLPI["root_doc"] . "/front/preference.php?{$forcetab}");
                             break;
                             // Use for compatibility with old name
                             // no break
                         // Use for compatibility with old name
                         // no break
                         case "tracking":
                             $data[0] = "Ticket";
                         default:
                             // redirect to item
                             if (!empty($data[0]) && isset($data[1]) && is_numeric($data[1]) && $data[1] > 0) {
                                 // Check entity
                                 if ($item = getItemForItemtype($data[0])) {
                                     if ($item->isEntityAssign()) {
                                         if ($item->getFromDB($data[1])) {
                                             if (!Session::haveAccessToEntity($item->getEntityID())) {
                                                 Session::changeActiveEntities($item->getEntityID(), 1);
                                             }
                                         }
                                     }
                                     Html::redirect($item->getFormURL() . "?id=" . $data[1] . "&{$forcetab}");
                                 }
                                 // redirect to list
                             } else {
                                 if (!empty($data[0])) {
                                     if ($item = getItemForItemtype($data[0])) {
                                         Html::redirect($item->getSearchURL() . "?{$forcetab}");
                                     }
                                 }
                             }
                             Html::redirect($CFG_GLPI["root_doc"] . "/front/central.php");
                             break;
                     }
                     break;
             }
         }
     }
 }
Example #2
0
 /**
  * Change active entity to the entities_id one.
  *
  * @param $params   array with theses options :
  *   - 'entities_id': (default 'all') ID of the new active entity ("all" = load all possible entities). Optionnal
  *   - 'is_recursive': (default false) Also display sub entities of the active entity.  Optionnal
  *
  * @return bool
  **/
 protected function changeActiveEntities($params = array())
 {
     $this->initEndpoint();
     if (!isset($params['entities_id'])) {
         $params['entities_id'] = 'all';
     }
     if (!isset($params['is_recursive'])) {
         $params['is_recursive'] = false;
     }
     return Session::changeActiveEntities(intval($params['entities_id']), $params['is_recursive']);
 }
        Session::changeProfile($_POST['newprofile']);
        if ($_SESSION["glpiactiveprofile"]["interface"] == "helpdesk") {
            if ($_SESSION['glpiactiveprofile']['create_ticket_on_login']) {
                Html::redirect($CFG_GLPI['root_doc'] . "/front/helpdesk.public.php?create_ticket=1");
            } else {
                Html::redirect($CFG_GLPI['root_doc'] . "/front/helpdesk.public.php");
            }
        }
        Html::redirect($_SERVER['PHP_SELF']);
    }
    Html::redirect(preg_replace("/entities_id.*/", "", $_SERVER['HTTP_REFERER']));
}
// Manage entity change
if (isset($_GET["active_entity"])) {
    if (!isset($_GET["is_recursive"])) {
        $_GET["is_recursive"] = 0;
    }
    if (Session::changeActiveEntities($_GET["active_entity"], $_GET["is_recursive"])) {
        if ($_GET["active_entity"] == $_SESSION["glpiactive_entity"] && isset($_SERVER['HTTP_REFERER'])) {
            Html::redirect(preg_replace("/entities_id.*/", "", $_SERVER['HTTP_REFERER']));
        }
    }
}
Html::header(Central::getTypeName(1), $_SERVER['PHP_SELF']);
// Redirect management
if (isset($_GET["redirect"])) {
    Toolbox::manageRedirect($_GET["redirect"]);
}
$central = new Central();
$central->display();
Html::footer();
 /**
  * Change the current entity(ies) of a authenticated user
  *
  * @param $params array of options
  *  - entity : ID of the new entity or "all"
  *  - recursive : 1 to see children
  * @return like plugin_webservices_method_listEntities
  */
 static function methodSetMyEntity($params, $protocol)
 {
     if (isset($params['help'])) {
         return array('entity' => 'integer,mandatory', 'recursive' => 'bool,optional', 'help' => 'bool,optional');
     }
     if (!Session::getLoginUserID()) {
         return self::Error($protocol, WEBSERVICES_ERROR_NOTAUTHENTICATED);
     }
     if (!isset($params['entity'])) {
         return self::Error($protocol, WEBSERVICES_ERROR_MISSINGPARAMETER, '', 'entity');
     }
     if (Session::changeActiveEntities($params['entity'], isset($params['recursive']) && $params['recursive'])) {
         return self::methodListEntities($_SESSION['glpiactiveentities'], $params);
     }
     return self::Error($protocol, WEBSERVICES_ERROR_BADPARAMETER, '', "entity=" . $params['entity']);
 }
 static function isThisItemCheckRule($parm)
 {
     global $DB;
     $itemtype = get_class($parm);
     $items_id = $parm->fields['id'];
     $a_find = array();
     $pmComponentscatalog_rule = new PluginMonitoringComponentscatalog_rule();
     $pmComponentscatalog = new PluginMonitoringComponentscatalog();
     $pmSearch = new PluginMonitoringSearch();
     $query = "SELECT * FROM `" . $pmComponentscatalog_rule->getTable() . "`\n         WHERE `itemtype`='" . $itemtype . "'";
     $result = $DB->query($query);
     $get_tmp = array();
     if (isset($_GET)) {
         $get_tmp = $_GET;
     }
     while ($data = $DB->fetch_array($result)) {
         if (isset($_SESSION["glpisearchcount"][$data['itemtype']])) {
             unset($_SESSION["glpisearchcount"][$data['itemtype']]);
         }
         if (isset($_SESSION["glpisearchcount2"][$data['itemtype']])) {
             unset($_SESSION["glpisearchcount2"][$data['itemtype']]);
         }
         $_GET = importArrayFromDB($data['condition']);
         $_GET["glpisearchcount"] = count($_GET['field']);
         if (isset($_GET['field2'])) {
             $_GET["glpisearchcount2"] = count($_GET['field2']);
         }
         if (!isset($_SESSION['glpiactiveentities_string'])) {
             $_SESSION['glpiactiveentities_string'] = $parm->fields['entities_id'];
         }
         // Load right entity
         $pmComponentscatalog->getFromDB($data['plugin_monitoring_componentscalalog_id']);
         $default_entity = 0;
         if (isset($_SESSION['glpiactive_entity'])) {
             $default_entity = $_SESSION['glpiactive_entity'];
         }
         $entities_isrecursive = 0;
         if (isset($_SESSION['glpiactiveentities']) and count($_SESSION['glpiactiveentities']) > 1) {
             $entities_isrecursive = 1;
         }
         if (!isset($_SESSION['glpiactiveprofile']['entities'])) {
             $_SESSION['glpiactiveprofile']['entities'] = array($pmComponentscatalog->fields['entities_id'] => array('id' => $pmComponentscatalog->fields['entities_id'], 'name' => '', 'is_recursive' => $pmComponentscatalog->fields['is_recursive']));
         }
         Session::changeActiveEntities($pmComponentscatalog->fields['entities_id'], $pmComponentscatalog->fields['is_recursive']);
         Search::manageGetValues($data['itemtype']);
         $resultr = $pmSearch->constructSQL($itemtype, $_GET, $items_id);
         if ($DB->numrows($resultr) > 0) {
             $a_find[$data['plugin_monitoring_componentscalalog_id']] = 1;
         } else {
             if (!isset($a_find[$data['plugin_monitoring_componentscalalog_id']])) {
                 $a_find[$data['plugin_monitoring_componentscalalog_id']] = 0;
             }
         }
         // Reload current entity
         Session::changeActiveEntities($default_entity, $entities_isrecursive);
     }
     if (count($get_tmp) > 0) {
         $_GET = $get_tmp;
     }
     $pmComponentscatalog_Host = new PluginMonitoringComponentscatalog_Host();
     foreach ($a_find as $componentscalalog_id => $is_present) {
         if ($is_present == '0') {
             // * Remove from dynamic if present
             $query = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n               WHERE `plugin_monitoring_componentscalalog_id`='" . $componentscalalog_id . "'\n                  AND `itemtype`='" . $itemtype . "'\n                  AND `items_id`='" . $items_id . "'\n                  AND`is_static`='0'";
             $result = $DB->query($query);
             while ($data = $DB->fetch_array($result)) {
                 $pmComponentscatalog_Host->delete(array('id' => $data['id']));
             }
         } else {
             //  add if not present
             $query = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n               WHERE `plugin_monitoring_componentscalalog_id`='" . $componentscalalog_id . "'\n                  AND `itemtype`='" . $itemtype . "'\n                  AND `items_id`='" . $items_id . "'\n                     LIMIT 1";
             $result = $DB->query($query);
             if ($DB->numrows($result) == '0') {
                 $input = array();
                 $input['plugin_monitoring_componentscalalog_id'] = $componentscalalog_id;
                 $input['is_static'] = '0';
                 $input['items_id'] = $items_id;
                 $input['itemtype'] = $itemtype;
                 $componentscatalogs_hosts_id = $pmComponentscatalog_Host->add($input);
                 $pmComponentscatalog_Host->linkComponentsToItem($componentscalalog_id, $componentscatalogs_hosts_id);
             } else {
                 $data2 = $DB->fetch_assoc($result);
                 // modify entity of services (if entity of device is changed)
                 $item = new $itemtype();
                 $item->getFromDB($items_id);
                 $queryu = "UPDATE `glpi_plugin_monitoring_services`\n                     SET `entities_id`='" . $item->fields['entities_id'] . "'\n                        WHERE `plugin_monitoring_componentscatalogs_hosts_id`='" . $data2['id'] . "'";
                 $DB->query($queryu);
             }
         }
     }
     if ($itemtype == 'NetworkEquipment') {
         //Get networkports
         $pmComponentscatalog_rule->isThisItemCheckRuleNetworkport($parm);
     }
 }
Example #6
0
 /**
  * Entity right check
  */
 public function testEntity()
 {
     global $DB;
     $DB->connect();
     $ent0 = $this->sharedFixture['entity'][0];
     $ent1 = $this->sharedFixture['entity'][1];
     $ent2 = $this->sharedFixture['entity'][2];
     $ent3 = $this->sharedFixture['entity'][3];
     $ent4 = $this->sharedFixture['entity'][4];
     $entity = new Entity();
     $this->assertTrue(Session::changeActiveEntities("all"));
     $this->assertTrue($entity->can(0, READ), "Fail: can't read root entity");
     $this->assertTrue($entity->can($ent0, READ), "Fail: can't read entity 0");
     $this->assertTrue($entity->can($ent1, READ), "Fail: can't read entity 1");
     $this->assertTrue($entity->can($ent2, READ), "Fail: can't read entity 2");
     $this->assertTrue($entity->can($ent3, READ), "Fail: can't read entity 2.1");
     $this->assertTrue($entity->can($ent4, READ), "Fail: can't read entity 2.2");
     $this->assertTrue($entity->canEdit(0), "Fail: can't write root entity");
     $this->assertTrue($entity->canEdit($ent0), "Fail: can't write entity 0");
     $this->assertTrue($entity->canEdit($ent1), "Fail: can't write entity 1");
     $this->assertTrue($entity->canEdit($ent2), "Fail: can't write entity 2");
     $this->assertTrue($entity->canEdit($ent3), "Fail: can't write entity 2.1");
     $this->assertTrue($entity->canEdit($ent4), "Fail: can't write entity 2.2");
     $input = array('entities_id' => $ent1);
     $this->assertTrue($entity->can(-1, CREATE, $input), "Fail: can create entity in root");
     $input = array('entities_id' => $ent2);
     $this->assertTrue($entity->can(-1, CREATE, $input), "Fail: can't create entity in 2");
     $input = array('entities_id' => $ent3);
     $this->assertTrue($entity->can(-1, CREATE, $input), "Fail: can't create entity in 2.1");
     $input = array('entities_id' => 99999);
     $this->assertFalse($entity->can(-1, CREATE, $input), "Fail: can create entity in not existing entity");
     $input = array('entities_id' => -1);
     $this->assertFalse($entity->can(-1, CREATE, $input), "Fail: can create entity in not existing entity");
     $this->assertTrue(Session::changeActiveEntities($ent2, true));
     $this->assertTrue($entity->can(0, READ), "Fail: can't read root entity");
     $this->assertTrue($entity->can($ent0, READ), "Fail: can't read entity 0");
     $this->assertFalse($entity->can($ent1, READ), "Fail: can read entity 1");
     $this->assertTrue($entity->can($ent2, READ), "Fail: can't read entity 2");
     $this->assertTrue($entity->can($ent3, READ), "Fail: can't read entity 2.1");
     $this->assertTrue($entity->can($ent4, READ), "Fail: can't read entity 2.2");
     $this->assertFalse($entity->can(99999, READ), "Fail: can read not existing entity");
     $this->assertFalse($entity->canEdit(0), "Fail: can write root entity");
     $this->assertFalse($entity->canEdit($ent0), "Fail: can write entity 0");
     $this->assertFalse($entity->canEdit($ent1), "Fail: can write entity 1");
     $this->assertTrue($entity->canEdit($ent2), "Fail: can't write entity 2");
     $this->assertTrue($entity->canEdit($ent3), "Fail: can't write entity 2.1");
     $this->assertTrue($entity->canEdit($ent4), "Fail: can't write entity 2.2");
     $this->assertFalse($entity->canEdit(99999), "Fail: can write not existing entity");
     $input = array('entities_id' => $ent1);
     $this->assertFalse($entity->can(-1, CREATE, $input), "Fail: can create entity in root");
     $input = array('entities_id' => $ent2);
     $this->assertTrue($entity->can(-1, CREATE, $input), "Fail: can't create entity in 2");
     $input = array('entities_id' => $ent3);
     $this->assertTrue($entity->can(-1, CREATE, $input), "Fail: can't create entity in 2.1");
     $input = array('entities_id' => 99999);
     $this->assertFalse($entity->can(-1, CREATE, $input), "Fail: can create entity in not existing entity");
     $input = array('entities_id' => -1);
     $this->assertFalse($entity->can(-1, CREATE, $input), "Fail: can create entity in not existing entity");
     $this->assertTrue(Session::changeActiveEntities($ent2, false));
     $input = array('entities_id' => $ent1);
     $this->assertFalse($entity->can(-1, CREATE, $input), "Fail: can create entity in root");
     $input = array('entities_id' => $ent2);
     // next should be false (or not).... but check is done on glpiactiveprofile
     // will require to save current state in session - this is probably acceptable
     // this allow creation when no child defined yet (no way to select tree in this case)
     $this->assertTrue($entity->can(-1, CREATE, $input), "Fail: can't create entity in 2");
     $input = array('entities_id' => $ent3);
     $this->assertFalse($entity->can(-1, CREATE, $input), "Fail: can create entity in 2.1");
 }
 static function getItemsDynamicly($parm)
 {
     global $DB;
     $pmDisplayview_rule = new PluginMonitoringDisplayview_rule();
     $pmDisplayview_item = new PluginMonitoringDisplayview_item();
     $pmDisplayview = new PluginMonitoringDisplayview();
     $pmSearch = new PluginMonitoringSearch();
     $pmService = new PluginMonitoringService();
     $devices_present = array();
     if ($pmDisplayview_rule->getFromDB($parm->fields['id'])) {
         if ($pmDisplayview->getFromDB($pmDisplayview_rule->fields['plugin_monitoring_displayviews_id'])) {
             // Load right entity
             $default_entity = 0;
             if (isset($_SESSION['glpiactive_entity'])) {
                 $default_entity = $_SESSION['glpiactive_entity'];
             }
             $entities_isrecursive = 0;
             if (isset($_SESSION['glpiactiveentities']) and count($_SESSION['glpiactiveentities']) > 1) {
                 $entities_isrecursive = 1;
             }
             Session::changeActiveEntities($pmDisplayview->fields['entities_id'], $pmDisplayview->fields['is_recursive']);
             $get_tmp = '';
             $itemtype = $pmDisplayview_rule->fields['itemtype'];
             if (isset($_GET)) {
                 $get_tmp = $_GET;
             }
             if (isset($_SESSION["glpisearchcount"][$pmDisplayview_rule->fields['itemtype']])) {
                 unset($_SESSION["glpisearchcount"][$pmDisplayview_rule->fields['itemtype']]);
             }
             if (isset($_SESSION["glpisearchcount2"][$pmDisplayview_rule->fields['itemtype']])) {
                 unset($_SESSION["glpisearchcount2"][$pmDisplayview_rule->fields['itemtype']]);
             }
             $_GET = importArrayFromDB($pmDisplayview_rule->fields['condition']);
             $_GET["glpisearchcount"] = count($_GET['field']);
             if (isset($_GET['field2'])) {
                 $_GET["glpisearchcount2"] = count($_GET['field2']);
             }
             $params = Search::manageParams($pmDisplayview_rule->fields['itemtype'], $_GET);
             //            Search::manageGetValues($pmDisplayview_rule->fields['itemtype']);
             $queryd = "SELECT * FROM `glpi_plugin_monitoring_displayviews_items`\n               WHERE `plugin_monitoring_displayviews_id`='" . $pmDisplayview_rule->fields["plugin_monitoring_displayviews_id"] . "'\n                  AND `itemtype`='" . $pmDisplayview_rule->fields['type'] . "'\n                  AND `extra_infos`='" . $pmDisplayview_rule->fields['itemtype'] . "'";
             $result = $DB->query($queryd);
             while ($data = $DB->fetch_array($result)) {
                 $devices_present[$data['items_id']] = $data['id'];
             }
             $glpilist_limit = $_SESSION['glpilist_limit'];
             $_SESSION['glpilist_limit'] = 500000;
             $result = $pmSearch->constructSQL($itemtype, $_GET);
             $_SESSION['glpilist_limit'] = $glpilist_limit;
             while ($data = $DB->fetch_array($result)) {
                 if (!isset($devices_present[$data['id']])) {
                     // Verify this device has one or more resources
                     $query_h = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts`" . " LEFT JOIN `glpi_plugin_monitoring_services`" . "    ON `plugin_monitoring_componentscatalogs_hosts_id`=" . " `glpi_plugin_monitoring_componentscatalogs_hosts`.`id`" . " WHERE `items_id`='" . $data['id'] . "'" . "    AND `itemtype`='" . $pmDisplayview_rule->fields['itemtype'] . "'" . "    AND `glpi_plugin_monitoring_services`.`id` IS NOT NULL";
                     $result_h = $DB->query($query_h);
                     if ($DB->numrows($result_h) > 0) {
                         $input = array();
                         $input['plugin_monitoring_displayviews_id'] = $pmDisplayview_rule->fields["plugin_monitoring_displayviews_id"];
                         $input['x'] = '1';
                         $input['y'] = '1';
                         $input['items_id'] = $data['id'];
                         $input['itemtype'] = $pmDisplayview_rule->fields['type'];
                         $input['extra_infos'] = $pmDisplayview_rule->fields['itemtype'];
                         $pmDisplayview_item->add($input);
                     }
                 } else {
                     // Verify this device has one or more resources
                     $query_h = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts`" . " LEFT JOIN `glpi_plugin_monitoring_services`" . "    ON `plugin_monitoring_componentscatalogs_hosts_id`=" . " `glpi_plugin_monitoring_componentscatalogs_hosts`.`id`" . " WHERE `items_id`='" . $data['id'] . "'" . "    AND `itemtype`='" . $pmDisplayview_rule->fields['itemtype'] . "'" . "    AND `glpi_plugin_monitoring_services`.`id` IS NOT NULL";
                     $result_h = $DB->query($query_h);
                     if ($DB->numrows($result_h) > 0) {
                         unset($devices_present[$data['id']]);
                     }
                 }
             }
             // Reload current entity
             Session::changeActiveEntities($default_entity, $entities_isrecursive);
         } else {
             $pmDisplayview->delete(array('id' => $pmDisplayview_rule->fields['plugin_monitoring_displayviews_id']));
         }
     }
     foreach ($devices_present as $id) {
         $pmDisplayview_item->delete(array('id' => $id));
     }
     return true;
 }
 static function isThisItemCheckRule($parm)
 {
     global $DB;
     $itemtype = get_class($parm);
     $items_id = $parm->fields['id'];
     $session_glpisearch = array();
     if (isset($_SESSION['glpisearch'])) {
         $session_glpisearch = $_SESSION['glpisearch'];
     }
     $session_glpisearchcount = array();
     if (isset($_SESSION['glpisearchcount'])) {
         $session_glpisearchcount = $_SESSION['glpisearchcount'];
     }
     $session_glpisearchcount2 = array();
     if (isset($_SESSION['glpisearchcount2'])) {
         $session_glpisearchcount2 = $_SESSION['glpisearchcount2'];
     }
     $a_find = array();
     $pmComponentscatalog_rule = new PluginMonitoringComponentscatalog_rule();
     $pmComponentscatalog = new PluginMonitoringComponentscatalog();
     $query = "SELECT * FROM `" . $pmComponentscatalog_rule->getTable() . "`\n         WHERE `itemtype`='" . $itemtype . "'";
     $result = $DB->query($query);
     $get_tmp = array();
     if (isset($_GET)) {
         $get_tmp = $_GET;
     }
     while ($data = $DB->fetch_array($result)) {
         if (!isset($_SESSION['glpiactiveentities_string'])) {
             $_SESSION['glpiactiveentities_string'] = $parm->fields['entities_id'];
         }
         // Load right entity
         $pmComponentscatalog->getFromDB($data['plugin_monitoring_componentscalalog_id']);
         $default_entity = 0;
         if (isset($_SESSION['glpiactive_entity'])) {
             $default_entity = $_SESSION['glpiactive_entity'];
         }
         $entities_isrecursive = 0;
         if (isset($_SESSION['glpiactiveentities']) and count($_SESSION['glpiactiveentities']) > 1) {
             $entities_isrecursive = 1;
         }
         if (!isset($_SESSION['glpiactiveprofile']['entities'])) {
             $_SESSION['glpiactiveprofile']['entities'] = array($pmComponentscatalog->fields['entities_id'] => array('id' => $pmComponentscatalog->fields['entities_id'], 'name' => '', 'is_recursive' => $pmComponentscatalog->fields['is_recursive']));
         }
         Session::changeActiveEntities($pmComponentscatalog->fields['entities_id'], $pmComponentscatalog->fields['is_recursive']);
         $itemtype = $data['itemtype'];
         $condition = importArrayFromDB($data['condition']);
         $params = Search::manageParams($itemtype, $condition, FALSE);
         $datar = Search::prepareDatasForSearch($itemtype, $params);
         Search::constructSQL($datar);
         $DBread = DBConnection::getReadConnection();
         $DBread->query("SET SESSION group_concat_max_len = 16384;");
         $resultr = $DBread->query($datar['sql']['search']);
         /// Check group concat limit : if warning : increase limit
         if ($result2 = $DBread->query('SHOW WARNINGS')) {
             if ($DBread->numrows($result2) > 0) {
                 $res = $DBread->fetch_assoc($result2);
                 if ($res['Code'] == 1260) {
                     $DBread->query("SET SESSION group_concat_max_len = 4194304;");
                     $resultr = $DBread->query($datar['sql']['search']);
                 }
             }
         }
         $find = 0;
         while ($datar = $DB->fetch_array($resultr)) {
             if ($datar['id'] == $items_id) {
                 $find = 1;
                 break;
             }
         }
         if ($find == 1) {
             $a_find[$data['plugin_monitoring_componentscalalog_id']] = 1;
         } else {
             if (!isset($a_find[$data['plugin_monitoring_componentscalalog_id']])) {
                 $a_find[$data['plugin_monitoring_componentscalalog_id']] = 0;
             }
         }
         // Reload current entity
         Session::changeActiveEntities($default_entity, $entities_isrecursive);
     }
     if (count($get_tmp) > 0) {
         $_GET = $get_tmp;
     }
     $pmComponentscatalog_Host = new PluginMonitoringComponentscatalog_Host();
     foreach ($a_find as $componentscalalog_id => $is_present) {
         if ($is_present == '0') {
             // * Remove from dynamic if present
             $query = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n               WHERE `plugin_monitoring_componentscalalog_id`='" . $componentscalalog_id . "'\n                  AND `itemtype`='" . $itemtype . "'\n                  AND `items_id`='" . $items_id . "'\n                  AND`is_static`='0'";
             $result = $DB->query($query);
             while ($data = $DB->fetch_array($result)) {
                 $pmComponentscatalog_Host->delete(array('id' => $data['id']));
             }
         } else {
             //  add if not present
             $query = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n               WHERE `plugin_monitoring_componentscalalog_id`='" . $componentscalalog_id . "'\n                  AND `itemtype`='" . $itemtype . "'\n                  AND `items_id`='" . $items_id . "'\n                     LIMIT 1";
             $result = $DB->query($query);
             if ($DB->numrows($result) == '0') {
                 $input = array();
                 $input['plugin_monitoring_componentscalalog_id'] = $componentscalalog_id;
                 $input['is_static'] = '0';
                 $input['items_id'] = $items_id;
                 $input['itemtype'] = $itemtype;
                 $pmComponentscatalog_Host->add($input);
             } else {
                 $data2 = $DB->fetch_assoc($result);
                 // modify entity of services (if entity of device is changed)
                 $item = new $itemtype();
                 $item->getFromDB($items_id);
                 $queryu = "UPDATE `glpi_plugin_monitoring_services`\n                     SET `entities_id`='" . $item->fields['entities_id'] . "'\n                        WHERE `plugin_monitoring_componentscatalogs_hosts_id`='" . $data2['id'] . "'";
                 $DB->query($queryu);
             }
         }
     }
     if ($itemtype == 'NetworkEquipment') {
         //Get networkports
         $pmComponentscatalog_rule->isThisItemCheckRuleNetworkport($parm);
     }
     $_SESSION['glpisearch'] = $session_glpisearch;
     $_SESSION['glpisearchcount'] = $session_glpisearchcount;
     $_SESSION['glpisearchcount2'] = $session_glpisearchcount2;
 }
Example #9
0
 /**
  * change current entity
  */
 protected function setEntity($entityname, $subtree)
 {
     $this->assertTrue(Session::changeActiveEntities(getItemByTypeName('Entity', $entityname, true), $subtree));
 }
 /**
  * Manage communication with old protocol (XML over POST)
  *
  **/
 function handleOCSCommunication($rawdata, $xml = '', $output = 'ext')
 {
     // ***** For debug only ***** //
     //$rawdata = gzcompress('');
     // ********** End ********** //
     $config = new PluginFusioninventoryConfig();
     $user = new User();
     //      ob_start();
     if (!isset($_SESSION['glpiID'])) {
         $users_id = $config->getValue('users_id');
         $_SESSION['glpiID'] = $users_id;
         $user->getFromDB($users_id);
         Session::changeActiveEntities();
         $_SESSION["glpiname"] = $user->getField('name');
         $_SESSION['glpiactiveprofile'] = array();
         $_SESSION['glpiactiveprofile']['interface'] = 'central';
         $_SESSION['glpiactiveprofile']['internet'] = 'w';
         $_SESSION['glpiactiveprofile']['computer'] = 'w';
         $_SESSION['glpiactiveprofile']['monitor'] = 'w';
         $_SESSION['glpiactiveprofile']['printer'] = 'w';
         $_SESSION['glpiactiveprofile']['peripheral'] = 'w';
         $_SESSION['glpiactiveprofile']['networking'] = 'w';
         $_SESSION["glpi_plugin_fusioninventory_profile"]['unmanaged'] = 'w';
     }
     $communication = new PluginFusioninventoryCommunication();
     $pfToolbox = new PluginFusioninventoryToolbox();
     // identify message compression algorithm
     $agent = new PluginFusioninventoryAgent();
     $agent->disableDebug();
     $compressmode = '';
     if (!empty($xml)) {
         $compressmode = 'none';
     } else {
         if ($_SERVER['CONTENT_TYPE'] == "application/x-compress-zlib") {
             $xml = gzuncompress($rawdata);
             $compressmode = "zlib";
         } else {
             if ($_SERVER['CONTENT_TYPE'] == "application/x-compress-gzip") {
                 $xml = $pfToolbox->gzdecode($rawdata);
                 $compressmode = "gzip";
             } else {
                 if ($_SERVER['CONTENT_TYPE'] == "application/xml") {
                     $xml = $rawdata;
                     $compressmode = 'none';
                 } else {
                     # try each algorithm successively
                     if ($xml = gzuncompress($rawdata)) {
                         $compressmode = "zlib";
                     } else {
                         if ($xml = $pfToolbox->gzdecode($rawdata)) {
                             $compressmode = "gzip";
                         } else {
                             if ($xml = gzinflate(substr($rawdata, 2))) {
                                 // accept deflate for OCS agent 2.0 compatibility,
                                 // but use zlib for answer
                                 if (strstr($xml, "<QUERY>PROLOG</QUERY>") and !strstr($xml, "<TOKEN>")) {
                                     $compressmode = "zlib";
                                 } else {
                                     $compressmode = "deflate";
                                 }
                             } else {
                                 $xml = $rawdata;
                                 $compressmode = 'none';
                             }
                         }
                     }
                 }
             }
         }
     }
     $agent->restoreDebug();
     // check if we are in ssl only mode
     $ssl = $config->getValue('ssl_only');
     if ($ssl == "1" and (!isset($_SERVER["HTTPS"]) or $_SERVER["HTTPS"] != "on")) {
         if ($output == 'glpi') {
             Session::addMessageAfterRedirect('SSL REQUIRED BY SERVER', false, ERROR);
         } else {
             $communication->setMessage("<?xml version='1.0' encoding='UTF-8'?>\n<REPLY>\n   <ERROR>SSL REQUIRED BY SERVER</ERROR>\n</REPLY>");
             $communication->sendMessage($compressmode);
         }
         return;
     }
     PluginFusioninventoryConfig::logIfExtradebug('pluginFusioninventory-dial' . uniqid(), $xml);
     // Check XML integrity
     $pxml = '';
     if ($pxml = @simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)) {
     } else {
         if ($pxml = @simplexml_load_string(utf8_encode($xml), 'SimpleXMLElement', LIBXML_NOCDATA)) {
             $xml = utf8_encode($xml);
         } else {
             $xml = preg_replace('/<FOLDER>.*?<\\/SOURCE>/', '', $xml);
             $pxml = @simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
             if (!$pxml) {
                 if ($output == 'glpi') {
                     Session::addMessageAfterRedirect('XML not well formed!', false, ERROR);
                 } else {
                     $communication->setMessage("<?xml version='1.0' encoding='UTF-8'?>\n<REPLY>\n   <ERROR>XML not well formed!</ERROR>\n</REPLY>");
                     $communication->sendMessage($compressmode);
                 }
                 return;
             }
         }
     }
     $_SESSION['plugin_fusioninventory_compressmode'] = $compressmode;
     // Convert XML into PHP array
     $arrayinventory = PluginFusioninventoryFormatconvert::XMLtoArray($pxml);
     unset($pxml);
     $deviceid = '';
     if (isset($arrayinventory['DEVICEID'])) {
         $deviceid = $arrayinventory['DEVICEID'];
     }
     $agent = new PluginFusioninventoryAgent();
     $agents_id = $agent->importToken($arrayinventory);
     $_SESSION['plugin_fusioninventory_agents_id'] = $agents_id;
     if (!$communication->import($arrayinventory)) {
         if ($deviceid != '') {
             $communication->setMessage("<?xml version='1.0' encoding='UTF-8'?>\n<REPLY>\n</REPLY>");
             $a_agent = $agent->InfosByKey($deviceid);
             // Get taskjob in waiting
             $communication->getTaskAgent($a_agent['id']);
             // ******** Send XML
             $communication->addInventory($a_agent['id']);
             $communication->addProlog();
             $communication->sendMessage($compressmode);
         }
     } else {
         if ($output == 'glpi') {
             Session::addMessageAfterRedirect('XML has been imported succesfully!');
         } else {
             $communication->setMessage("<?xml version='1.0' encoding='UTF-8'?>\n<REPLY>\n</REPLY>");
             $communication->sendMessage($compressmode);
         }
     }
 }