/**
  * Inventory process
  * @param simpleXML $simpleXMLObj
  */
 public function startAction($simpleXMLObj)
 {
     $log = new Logger();
     $log->notifyDebugMessage("-- PING ACTION START --");
     $log->notifyDebugMessage("The ping is " . $simpleXMLObj->ID);
     $log->notifyDebugMessage("-- PING ACTION END --");
     $xmlResponse = $this->_getActionXMLResponse();
     echo $xmlResponse;
 }
 /**
  * get device from pciid
  * @access public
  * @param string $pciid
  */
 public static function filter($section)
 {
     $log = new Logger();
     switch ($section->getName()) {
         case 'CONTROLLERS':
             if (!file_exists(LIBSERVERFUSIONINVENTORY_STORAGELOCATION . "/DataFilter/pciids")) {
                 $log->notifyDebugMessage("You have to create tree folders for PCI, no filter for CONTROLLERS");
                 $nofilter = 'CONTROLLERS';
                 return $nofilter;
             }
             if (isset($section->PCIID) and $section->PCIID != '') {
                 $manufacturer = self::_getDataFromPCIID($section->PCIID);
                 $section->MANUFACTURER = $manufacturer;
             }
             break;
         case 'NETWORKS':
             if (!file_exists(LIBSERVERFUSIONINVENTORY_STORAGELOCATION . "/DataFilter/oui")) {
                 $log->notifyDebugMessage("You have to create tree folders for OUI, no filter for NETWORKS");
                 $nofilter = 'NETWORKS';
                 return $nofilter;
             }
             if (isset($section->MACADDR) and $section->MACADDR != '') {
                 //Mac address is locally or universal ?
                 $msByte = substr($section->MACADDR, 0, 2);
                 $msBin = decbin(hexdec($msByte));
                 if (substr($msBin, -2, 1) != 1) {
                     //second bit isn't 1, the mac address isn't locally
                     $manufacturer = self::_getDataFromMACADDR($section->MACADDR);
                     $section->addChild('MANUFACTURER', $manufacturer);
                 }
             }
             break;
         case 'USBDEVICES':
             if (!file_exists(LIBSERVERFUSIONINVENTORY_STORAGELOCATION . "/DataFilter/usbids")) {
                 $log->notifyDebugMessage("You have to create tree folders for USB, no filter for USB DEVICES");
                 $nofilter = 'USBDEVICES';
                 return $nofilter;
             }
             if (isset($section->VENDORID) and $section->VENDORID != '' and isset($section->PRODUCTID)) {
                 $dataArray = self::_getDataFromUSBID($section->VENDORID, $section->PRODUCTID);
                 $section->addChild('MANUFACTURER', $dataArray[0]);
                 $section->addChild('PRODUCTNAME', $dataArray[1]);
             }
             break;
         default:
             break;
     }
     return false;
 }
 /**
  * Inventory process
  * @param simpleXML $simpleXMLObj
  */
 public function startAction($simpleXMLObj)
 {
     $log = new Logger();
     $libData = StorageInventoryFactory::createStorage($this->_applicationName, $this->_config, $simpleXMLObj);
     $log->notifyDebugMessage("INVENTORY ACTION START");
     if ($internalId = $libData->isMachineExist()) {
         $log->notifyDebugMessage("Machine {$internalId} already exists");
         //Sections update
         $xmlSections = $this->_getXMLSections($simpleXMLObj);
         $libData->updateLibMachine($xmlSections, $internalId);
         $log->notifyDebugMessage("Machine {$internalId}: All sections updated");
     } else {
         $log->notifyDebugMessage("Machine doesn't exist");
         //We launch CreateMachine() hook and provide an InternalId
         $xmlSections = $this->_getXMLSections($simpleXMLObj);
         $internalId = uniqid();
         try {
             $classhook = LIBSERVERFUSIONINVENTORY_HOOKS_CLASSNAME;
             $externalId = call_user_func(array($classhook, "createMachine"));
             $libData->addLibMachine($internalId, $externalId);
             $libData->addLibCriteriasMachine($internalId);
             $log->notifyDebugMessage("Machine {$internalId} created");
             $libData->updateLibMachine($xmlSections, $internalId);
             $log->notifyDebugMessage("Machine {$internalId}: All sections created");
         } catch (MyException $e) {
             echo 'created machine stage: error';
         }
     }
     $log->notifyDebugMessage("INVENTORY ACTION END");
     $xmlResponse = $this->_getActionXMLResponse();
     echo $xmlResponse;
 }
 public function start()
 {
     $log = new Logger();
     $log->notifyDebugMessage("----- FUSION SERVER START -----");
     $simpleXMLObjArray = $this->_importXMLObjArray();
     foreach ($simpleXMLObjArray as $simpleXMLObj) {
         if ($simpleXMLObj->QUERY == "PROLOG") {
             $xmlResponse = $this->_getXMLResponse($this->_actionsConfigs);
             echo $xmlResponse;
         } else {
             foreach ($this->_actionsConfigs as $actionName => $config) {
                 if ($simpleXMLObj->QUERY == strtoupper($actionName)) {
                     $action = ActionFactory::createAction($actionName);
                     $action->checkConfig($this->_applicationName, $config);
                     $action->startAction($simpleXMLObj);
                 }
             }
         }
     }
     $log->notifyDebugMessage("----- FUSION SERVER END -----");
 }
    /**
     * Determine if there are sections changements and update
     * @param array $xmlSections
     * @param array $infoSections
     * @param int $internalId
     */
    public function updateLibMachine($xmlSections, $internalId)
    {
        $log = new Logger();
        // Retrieve all sections stored in info file
        $infoSections = $this->_getInfoSections($internalId);
        // Retrieve all sections from xml file
        $serializedSectionsFromXML = array();
        foreach ($xmlSections as $xmlSection) {
            array_push($serializedSectionsFromXML, $xmlSection["sectionDatawName"]);
        }
        //Retrieve changes, sections to Add and sections to Remove
        $sectionsToAdd = array_diff($serializedSectionsFromXML, $infoSections["sections"]);
        $sectionsToRemove = array_diff($infoSections["sections"], $serializedSectionsFromXML);
        $classhook = LIBSERVERFUSIONINVENTORY_HOOKS_CLASSNAME;
        //updated section: process
        if ($sectionsToRemove && $sectionsToAdd) {
            $sectionsToAddTmp = array();
            $datasToUpdate = array();
            $existUpdate = 0;
            foreach ($sectionsToRemove as $sectionId => $serializedSectionToRemove) {
                $sectionName = substr($infoSections["sections"][$sectionId], strpos($infoSections["sections"][$sectionId], '}') + 1);
                if (in_array($sectionName, $this->_configs["sections"])) {
                    foreach ($sectionsToAdd as $arrayId => $serializedSectionToAdd) {
                        //check if we have the same section Name for an sectionToRemove and an sectionToAdd
                        if ($xmlSections[$arrayId]['sectionName'] == $sectionName) {
                            //Finally, we have to determine if it's an update or not
                            $boolUpdate = false;
                            $arrSectionToAdd = unserialize($serializedSectionToAdd);
                            $arrSectionToRemove = unserialize($serializedSectionToRemove);
                            //TODO: Traiter les notices sur les indices de tableau qui n'existent pas.
                            switch ($sectionName) {
                                case "DRIVES":
                                    if ($arrSectionToAdd["SERIAL"] == $arrSectionToRemove["SERIAL"] or $arrSectionToAdd["NAME"] == $arrSectionToRemove["NAME"] or $arrSectionToAdd["VOLUMN"] == $arrSectionToRemove["VOLUMN"]) {
                                        $boolUpdate = true;
                                    }
                                    break;
                                case "SOFTWARES":
                                    if ($arrSectionToAdd["GUID"] == $arrSectionToRemove["GUID"] or $arrSectionToAdd["NAME"] == $arrSectionToRemove["NAME"]) {
                                        $boolUpdate = true;
                                    }
                                    break;
                                case "CONTROLLERS":
                                    if ($arrSectionToAdd["PCIID"] == $arrSectionToRemove["PCIID"] and $arrSectionToAdd["PCISLOT"] == $arrSectionToRemove["PCISLOT"]) {
                                        $boolUpdate = true;
                                    }
                                    break;
                                case "ENVS":
                                    if ($arrSectionToAdd["NAME"] == $arrSectionToRemove["NAME"]) {
                                        $boolUpdate = true;
                                    }
                                    break;
                                case "INPUTS":
                                    if ($arrSectionToAdd["CAPTION"] == $arrSectionToRemove["CAPTION"]) {
                                        $boolUpdate = true;
                                    }
                                    break;
                                case "MEMORIES":
                                    if ($arrSectionToAdd["SERIALNUMBER"] == $arrSectionToRemove["SERIALNUMBER"]) {
                                        $boolUpdate = true;
                                    }
                                    break;
                                case "MONITORS":
                                    if ($arrSectionToAdd["DESCRIPTION"] == $arrSectionToRemove["DESCRIPTION"]) {
                                        $boolUpdate = true;
                                    }
                                    break;
                                case "NETWORKS":
                                    if ($arrSectionToAdd["MACADDR"] == $arrSectionToRemove["MACADDR"]) {
                                        $boolUpdate = true;
                                    }
                                    break;
                                case "PORTS":
                                    if ($arrSectionToAdd["CAPTION"] == $arrSectionToRemove["CAPTION"]) {
                                        $boolUpdate = true;
                                    }
                                    break;
                                case "PRINTERS":
                                    if ($arrSectionToAdd["DESCRIPTION"] == $arrSectionToRemove["DESCRIPTION"] or $arrSectionToAdd["PORT"] == $arrSectionToRemove["PORT"]) {
                                        $boolUpdate = true;
                                    }
                                    break;
                                case "PROCESSES":
                                    if ($arrSectionToAdd["STARTED"] == $arrSectionToRemove["STARTED"] and $arrSectionToAdd["PID"] == $arrSectionToRemove["PID"]) {
                                        $boolUpdate = true;
                                    }
                                    break;
                                case "SOUNDS":
                                    if ($arrSectionToAdd["NAME"] == $arrSectionToRemove["NAME"]) {
                                        $boolUpdate = true;
                                    }
                                    break;
                                case "STORAGES":
                                    if ($arrSectionToAdd["MODEL"] == $arrSectionToRemove["MODEL"] or $arrSectionToAdd["SERIALNUMBER"] == $arrSectionToRemove["SERIALNUMBER"]) {
                                        $boolUpdate = true;
                                    }
                                    break;
                                case "USERS":
                                    if ($arrSectionToAdd["LOGIN"] == $arrSectionToRemove["LOGIN"]) {
                                        $boolUpdate = true;
                                    }
                                    break;
                                case "VIDEOS":
                                    if ($arrSectionToAdd["NAME"] == $arrSectionToRemove["NAME"]) {
                                        $boolUpdate = true;
                                    }
                                    break;
                                case "USBDEVICES":
                                    if ($arrSectionToAdd["SERIAL"] == $arrSectionToRemove["SERIAL"] or $arrSectionToAdd["CLASS"] == $arrSectionToRemove["CLASS"] and $arrSectionToAdd["PRODUCTID"] == $arrSectionToRemove["PRODUCTID"] and $arrSectionToAdd["SUBCLASS"] == $arrSectionToRemove["SUBCLASS"] and $arrSectionToAdd["VENDORID"] == $arrSectionToRemove["VENDORID"]) {
                                        $boolUpdate = true;
                                    }
                                    break;
                                default:
                                    break;
                            }
                            if ($boolUpdate) {
                                //Then we update this section
                                $infoSections["sections"][$sectionId] = $serializedSectionToAdd;
                                //Delete this section from sectionToRemove and sectionToAdd
                                unset($sectionsToRemove[$sectionId]);
                                unset($sectionsToAdd[$arrayId]);
                                array_push($datasToUpdate, array("sectionId" => $sectionId, "dataSection" => $xmlSections[$arrayId]['sectionData']));
                                $existUpdate++;
                            } else {
                                //push element into an temporary array, to allow update transposition
                                $sectionsToAddTmp[$arrayId] = $sectionsToAdd[$arrayId];
                                unset($sectionsToAdd[$arrayId]);
                            }
                            break;
                        }
                    }
                }
            }
            if ($existUpdate) {
                call_user_func(array($classhook, "updateSections"), $datasToUpdate, $infoSections["externalId"]);
                $log->notifyDebugMessage($existUpdate . " section(s) modified");
            }
            if (!empty($sectionsToAddTmp)) {
                //Retrieve removed data in sectionsToAdd
                foreach ($sectionsToAddTmp as $k => $v) {
                    $sectionsToAdd[$k] = $v;
                }
                ksort($sectionsToAdd);
            }
        }
        if ($sectionsToRemove) {
            $sectionsIdToRemove = array();
            foreach ($sectionsToRemove as $sectionId => $serializedSection) {
                unset($infoSections["sections"][$sectionId]);
                array_push($sectionsIdToRemove, $sectionId);
            }
            call_user_func(array($classhook, "removeSections"), $sectionsIdToRemove, $infoSections["externalId"]);
            $log->notifyDebugMessage(count($sectionsToRemove) . " section(s) removed");
        }
        if ($sectionsToAdd) {
            $datasToAdd = array();
            //format data to send to hook createSection
            foreach ($sectionsToAdd as $arrayId => $serializedSection) {
                array_push($datasToAdd, array("sectionName" => $xmlSections[$arrayId]['sectionName'], "dataSection" => $xmlSections[$arrayId]['sectionData']));
            }
            $sectionsId = call_user_func(array($classhook, "addSections"), $datasToAdd, $infoSections["externalId"]);
            $log->notifyDebugMessage(count($sectionsToAdd) . " section(s) added");
            $infoSectionsId = array();
            //Retrieve section id from infofile
            foreach ($infoSections["sections"] as $sId => $serializedSection) {
                array_push($infoSectionsId, $sId);
            }
            $allSectionsId = array_merge($infoSectionsId, $sectionsId);
            $infoSections["sections"] = array_merge($infoSections["sections"], $sectionsToAdd);
            if (count($allSectionsId) != count($infoSections["sections"])) {
                $log->notifyDebugMessage("Number of lines of array return by hooks sections (add and remove) are not same with number of sections");
            }
            $infoSections["sections"] = array_combine($allSectionsId, $infoSections["sections"]);
        }
        /* Complete info file */
        $serializedSections = "";
        foreach ($infoSections["sections"] as $key => $serializedSection) {
            $serializedSections .= "\t" . $key . "<<=>>" . $serializedSection . "\n";
        }
        $externalId = $infoSections["externalId"];
        $data = <<<INFOCONTENT
{$externalId}
{$serializedSections}
INFOCONTENT;
        $infoPath = $this->_getInfoPathDSN($internalId);
        file_put_contents($infoPath . "/infos.file", $data);
    }