1<?php
2
3/*
4   ------------------------------------------------------------------------
5   FusionInventory
6   Copyright (C) 2010-2016 by the FusionInventory Development Team.
7
8   http://www.fusioninventory.org/   http://forge.fusioninventory.org/
9   ------------------------------------------------------------------------
10
11   LICENSE
12
13   This file is part of FusionInventory project.
14
15   FusionInventory is free software: you can redistribute it and/or modify
16   it under the terms of the GNU Affero General Public License as published by
17   the Free Software Foundation, either version 3 of the License, or
18   (at your option) any later version.
19
20   FusionInventory is distributed in the hope that it will be useful,
21   but WITHOUT ANY WARRANTY; without even the implied warranty of
22   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23   GNU Affero General Public License for more details.
24
25   You should have received a copy of the GNU Affero General Public License
26   along with FusionInventory. If not, see <http://www.gnu.org/licenses/>.
27
28   ------------------------------------------------------------------------
29
30   @package   FusionInventory
31   @author    David Durieux
32   @co-author
33   @copyright Copyright (c) 2010-2016 FusionInventory team
34   @license   AGPL License 3.0 or (at your option) any later version
35              http://www.gnu.org/licenses/agpl-3.0-standalone.html
36   @link      http://www.fusioninventory.org/
37   @link      http://forge.fusioninventory.org/projects/fusioninventory-for-glpi/
38   @since     2012
39
40   ------------------------------------------------------------------------
41 */
42
43if (!defined('GLPI_ROOT')) {
44   die("Sorry. You can't access directly to this file");
45}
46
47class PluginFusioninventoryFormatconvert {
48   var $foreignkey_itemtype = array();
49   var $manufacturer_cache = array();
50
51
52   static function XMLtoArray($xml) {
53      global $PLUGIN_FUSIONINVENTORY_XML;
54
55      $PLUGIN_FUSIONINVENTORY_XML = $xml;
56      $datainventory = json_decode(json_encode((array)$xml), TRUE);
57      if (isset($datainventory['CONTENT']['ENVS'])) {
58         unset($datainventory['CONTENT']['ENVS']);
59      }
60      if (isset($datainventory['CONTENT']['PROCESSES'])) {
61         unset($datainventory['CONTENT']['PROCESSES']);
62      }
63      if (isset($datainventory['CONTENT']['PORTS'])) {
64         unset($datainventory['CONTENT']['PORTS']);
65      }
66      $datainventory = PluginFusioninventoryFormatconvert::cleanArray($datainventory);
67      // Hack for some sections
68         $a_fields = array('SOUNDS', 'VIDEOS', 'CONTROLLERS', 'CPUS', 'DRIVES',
69                           'MEMORIES', 'NETWORKS', 'SOFTWARE', 'USERS',
70                           'VIRTUALMACHINES', 'ANTIVIRUS', 'MONITORS',
71                           'PRINTERS', 'USBDEVICES', 'PHYSICAL_VOLUMES',
72                           'VOLUME_GROUPS', 'LOGICAL_VOLUMES', 'BATTERIES',
73                           'LICENSEINFOS', 'STORAGES', 'INPUTS');
74         foreach ($a_fields as $field) {
75            if (isset($datainventory['CONTENT'][$field])
76                    AND !is_array($datainventory['CONTENT'][$field])) {
77               $datainventory['CONTENT'][$field] = array($datainventory['CONTENT'][$field]);
78            } else if (isset($datainventory['CONTENT'][$field])
79                    AND !is_int(key($datainventory['CONTENT'][$field]))) {
80               $datainventory['CONTENT'][$field] = array($datainventory['CONTENT'][$field]);
81            }
82         }
83      if (isset($datainventory['CONTENT'])
84              && isset($datainventory['CONTENT']['BIOS'])
85              && !is_array($datainventory['CONTENT']['BIOS'])) {
86         unset($datainventory['CONTENT']['BIOS']);
87      }
88      if (isset($datainventory['CONTENT']['VIRTUALMACHINES'])){
89         foreach ($datainventory['CONTENT']['VIRTUALMACHINES'] as $key=>$data){
90            if (isset($data['NETWORKS'])
91                    && !is_int(key($data['NETWORKS']))) {
92               $datainventory['CONTENT']['VIRTUALMACHINES'][$key]['NETWORKS'] =
93                  array($datainventory['CONTENT']['VIRTUALMACHINES'][$key]['NETWORKS']);
94
95            }
96         }
97      }
98
99      // Hack for Network discovery and inventory
100      if (isset($datainventory['CONTENT']['DEVICE'])
101              AND !is_array($datainventory['CONTENT']['DEVICE'])) {
102         $datainventory['CONTENT']['DEVICE'] = array($datainventory['CONTENT']['DEVICE']);
103      } else if (isset($datainventory['CONTENT']['DEVICE'])
104              AND !is_int(key($datainventory['CONTENT']['DEVICE']))) {
105         $datainventory['CONTENT']['DEVICE'] = array($datainventory['CONTENT']['DEVICE']);
106      }
107      if (isset($datainventory['CONTENT']['DEVICE'])) {
108         foreach ($datainventory['CONTENT']['DEVICE'] as $num=>$data) {
109            if (isset($data['INFO']['IPS']['IP'])
110                    AND !is_array($data['INFO']['IPS']['IP'])) {
111               $datainventory['CONTENT']['DEVICE'][$num]['INFO']['IPS']['IP'] =
112                     array($datainventory['CONTENT']['DEVICE'][$num]['INFO']['IPS']['IP']);
113            } else if (isset($data['INFO']['IPS']['IP'])
114                    AND !is_int(key($data['INFO']['IPS']['IP']))) {
115               $datainventory['CONTENT']['DEVICE'][$num]['INFO']['IPS']['IP'] =
116                     array($datainventory['CONTENT']['DEVICE'][$num]['INFO']['IPS']['IP']);
117            }
118
119            if (isset($data['PORTS']['PORT'])
120                    AND !is_array($data['PORTS']['PORT'])) {
121               $datainventory['CONTENT']['DEVICE'][$num]['PORTS']['PORT'] =
122                     array($datainventory['CONTENT']['DEVICE'][$num]['PORTS']['PORT']);
123            } else if (isset($data['PORTS']['PORT'])
124                    AND !is_int(key($data['PORTS']['PORT']))) {
125               $datainventory['CONTENT']['DEVICE'][$num]['PORTS']['PORT'] =
126                     array($datainventory['CONTENT']['DEVICE'][$num]['PORTS']['PORT']);
127            }
128            if (isset($datainventory['CONTENT']['DEVICE'][$num]['PORTS'])) {
129               foreach ($datainventory['CONTENT']['DEVICE'][$num]['PORTS']['PORT']
130                                    as $numport=>$a_port) {
131                  if (isset($a_port['CONNECTIONS'])
132                          && isset($a_port['CONNECTIONS']['CONNECTION'])
133                          && isset($a_port['CONNECTIONS']['CONNECTION']['MAC'])
134                          && !is_array($a_port['CONNECTIONS']['CONNECTION']['MAC'])) {
135                     $datainventory['CONTENT']['DEVICE'][$num]['PORTS']['PORT'][$numport]['CONNECTIONS']['CONNECTION']['MAC'] =
136                           array($a_port['CONNECTIONS']['CONNECTION']['MAC']);
137                  }
138                  if (isset($a_port['VLANS'])
139                          && isset($a_port['VLANS']['VLAN'])
140                          && !is_int(key($a_port['VLANS']['VLAN']))) {
141                     $datainventory['CONTENT']['DEVICE'][$num]['PORTS']['PORT'][$numport]['VLANS']['VLAN'] =
142                           array($a_port['VLANS']['VLAN']);
143                  }
144               }
145            }
146         }
147      }
148      return $datainventory;
149   }
150
151
152
153   static function JSONtoArray($json) {
154      $datainventory = json_decode($json, TRUE);
155      $datainventory = PluginFusioninventoryFormatconvert::cleanArray($datainventory);
156      return $datainventory;
157   }
158
159
160
161   static function cleanArray($data) {
162      foreach ($data as $key=>$value) {
163         //if (is_array($value)) {
164           if ((array)$value === $value) {
165            if (count($value) == 0) {
166               $value = '';
167            } else {
168               $value = PluginFusioninventoryFormatconvert::cleanArray($value);
169            }
170         } else {
171            if (strpos($value, "\'")) {
172               $value = str_replace("\'", "'", $value);
173            }
174
175            if (preg_match("/[^a-zA-Z0-9 \-_\(\)]+/", $value)) {
176               $value = Toolbox::addslashes_deep($value);
177            }
178            $value = Toolbox::clean_cross_side_scripting_deep($value);
179         }
180         $data[$key] = $value;
181      }
182      return array_change_key_case($data, CASE_UPPER);
183   }
184
185
186
187   /*
188    * Modify Computer inventory
189    */
190   static function computerInventoryTransformation($array) {
191      global $DB, $PF_ESXINVENTORY, $CFG_GLPI;
192
193      // Initialize
194      $a_inventory = array(
195         'Computer'                => array(),
196         'fusioninventorycomputer' => array(),
197         'processor'               => array(),
198         'memory'                  => array(),
199         'harddrive'               => array(),
200         'drive'                   => array(),
201         'graphiccard'             => array(),
202         'networkcard'             => array(),
203         'soundcard'               => array(),
204         'controller'              => array(),
205         'SOFTWARES'               => array(),
206         'virtualmachine'          => array(),
207         'computerdisk'            => array(),
208         'networkport'             => array(),
209         'antivirus'               => array(),
210         'licenseinfo'             => array(),
211         'batteries'               => array(),
212         'monitor'                 => array(),
213         'printer'                 => array(),
214         'peripheral'              => array(),
215         'storage'                 => array()
216      );
217      $thisc = new self();
218      $pfConfig = new PluginFusioninventoryConfig();
219
220      $ignorecontrollers = array();
221
222      if (isset($array['ACCOUNTINFO'])) {
223         $a_inventory['ACCOUNTINFO'] = $array['ACCOUNTINFO'];
224      }
225
226      // * HARDWARE
227      $array_tmp = $thisc->addValues($array['HARDWARE'],
228                                     array(
229                                        'NAME'           => 'name',
230                                        'OSNAME'         => 'operatingsystems_id',
231                                        'OSVERSION'      => 'operatingsystemversions_id',
232                                        'WINPRODID'      => 'os_licenseid',
233                                        'WINPRODKEY'     => 'os_license_number',
234                                        'WORKGROUP'      => 'domains_id',
235                                        'UUID'           => 'uuid',
236                                        'LASTLOGGEDUSER' => 'users_id',
237                                        'operatingsystemservicepacks_id' =>
238                                                      'operatingsystemservicepacks_id',
239                                        'manufacturers_id' => 'manufacturers_id',
240                                        'computermodels_id' => 'computermodels_id',
241                                        'serial' => 'serial',
242                                        'computertypes_id' => 'computertypes_id'));
243      if ($array_tmp['operatingsystemservicepacks_id'] == ''
244              && isset($array['HARDWARE']['OSCOMMENTS'])
245              && $array['HARDWARE']['OSCOMMENTS'] != '') {
246         $array_tmp['operatingsystemservicepacks_id'] = $array['HARDWARE']['OSCOMMENTS'];
247      }
248      if (isset($array_tmp['users_id'])) {
249         if ($array_tmp['users_id'] == '') {
250            unset($array_tmp['users_id']);
251         } else {
252            $array_tmp['contact'] = $array_tmp['users_id'];
253            $tmp_users_id = $array_tmp['users_id'];
254            $split_user = explode("@", $tmp_users_id);
255            $query = "SELECT `id`
256                      FROM `glpi_users`
257                      WHERE `name` = '" . $split_user[0] . "'
258                      LIMIT 1";
259            $result = $DB->query($query);
260            if ($DB->numrows($result) == 1) {
261               $array_tmp['users_id'] = $DB->result($result, 0, 0);
262            } else {
263               $array_tmp['users_id'] = 0;
264            }
265         }
266      }
267      $array_tmp['is_dynamic'] = 1;
268
269      $a_inventory['Computer'] = $array_tmp;
270
271      $array_tmp = $thisc->addValues($array['HARDWARE'],
272                                     array(
273                                        'OSINSTALLDATE'  => 'operatingsystem_installationdate',
274                                        'WINOWNER'       => 'winowner',
275                                        'WINCOMPANY'     => 'wincompany'));
276      $array_tmp['last_fusioninventory_update'] = date('Y-m-d H:i:s');
277
278      // * Determine "Public contact address"
279      if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { // Try "X-Forwarded-For" HTTP header
280         // Parse "X-Forwarded-For" header (can contain multiple IP addresses, client should be first)
281         $forwarded_for_ip_tmp = explode(', ', $_SERVER['HTTP_X_FORWARDED_FOR']);
282         $forwarded_for_ip_tmp = new IPAddress($forwarded_for_ip_tmp[0]);
283         if ($forwarded_for_ip_tmp->is_valid()) {
284            $array_tmp['remote_addr'] = $forwarded_for_ip_tmp->getTextual();
285         }
286      } elseif (isset($_SERVER['HTTP_X_REAL_IP'])) { // Then try "X-Real-IP" HTTP header
287         $real_ip_tmp = new IPAddress($_SERVER['HTTP_X_REAL_IP']);
288         if ($real_ip_tmp->is_valid()) {
289            $array_tmp['remote_addr'] = $real_ip_tmp->getTextual();
290         }
291      } elseif (isset($_SERVER['REMOTE_ADDR'])) { // Fall back on the currently connected IP
292         $array_tmp['remote_addr'] = $_SERVER['REMOTE_ADDR'];
293      }
294
295      $a_inventory['fusioninventorycomputer'] = $array_tmp;
296      if (isset($array['OPERATINGSYSTEM']['INSTALL_DATE'])
297              && !empty($array['OPERATINGSYSTEM']['INSTALL_DATE'])) {
298         $a_inventory['fusioninventorycomputer']['operatingsystem_installationdate'] =
299                     $array['OPERATINGSYSTEM']['INSTALL_DATE'];
300      }
301
302      if (isset($array['HARDWARE']['DESCRIPTION'])) {
303         $a_inventory['fusioninventorycomputer']['oscomment'] = $array['HARDWARE']['DESCRIPTION'];
304      }
305
306      if (empty($a_inventory['fusioninventorycomputer']['operatingsystem_installationdate'])) {
307         $a_inventory['fusioninventorycomputer']['operatingsystem_installationdate'] = "NULL";
308      }
309
310      // * BIOS
311      if (isset($array['BIOS'])) {
312         if (isset($array['BIOS']['ASSETTAG'])) {
313            $a_inventory['fusioninventorycomputer']['bios_assettag']= $array['BIOS']['ASSETTAG'];
314         }
315         if ((isset($array['BIOS']['SMANUFACTURER']))
316               AND (!empty($array['BIOS']['SMANUFACTURER']))) {
317            $a_inventory['Computer']['manufacturers_id'] = $array['BIOS']['SMANUFACTURER'];
318         } else if ((isset($array['BIOS']['MMANUFACTURER']))
319                      AND (!empty($array['BIOS']['MMANUFACTURER']))) {
320            $a_inventory['Computer']['manufacturers_id'] = $array['BIOS']['MMANUFACTURER'];
321         } else if ((isset($array['BIOS']['BMANUFACTURER']))
322                      AND (!empty($array['BIOS']['BMANUFACTURER']))) {
323            $a_inventory['Computer']['manufacturers_id'] = $array['BIOS']['BMANUFACTURER'];
324         } else {
325            if ((isset($array['BIOS']['MMANUFACTURER']))
326                         AND (!empty($array['BIOS']['MMANUFACTURER']))) {
327               $a_inventory['Computer']['manufacturers_id'] = $array['BIOS']['MMANUFACTURER'];
328            } else {
329               if ((isset($array['BIOS']['BMANUFACTURER']))
330                            AND (!empty($array['BIOS']['BMANUFACTURER']))) {
331                  $a_inventory['Computer']['manufacturers_id'] = $array['BIOS']['BMANUFACTURER'];
332               }
333            }
334         }
335         if ((isset($array['BIOS']['MMANUFACTURER']))
336                      AND (!empty($array['BIOS']['MMANUFACTURER']))) {
337            $a_inventory['Computer']['mmanufacturer'] = $array['BIOS']['MMANUFACTURER'];
338         }
339         if ((isset($array['BIOS']['BMANUFACTURER']))
340                      AND (!empty($array['BIOS']['BMANUFACTURER']))) {
341            $a_inventory['Computer']['bmanufacturer'] = $array['BIOS']['BMANUFACTURER'];
342         }
343
344         if (isset($array['BIOS']['SMODEL']) AND $array['BIOS']['SMODEL'] != '') {
345            $a_inventory['Computer']['computermodels_id'] = $array['BIOS']['SMODEL'];
346         } else if (isset($array['BIOS']['MMODEL']) AND $array['BIOS']['MMODEL'] != '') {
347            $a_inventory['Computer']['computermodels_id'] = $array['BIOS']['MMODEL'];
348         }
349         if (isset($array['BIOS']['MMODEL']) AND $array['BIOS']['MMODEL'] != '') {
350            $a_inventory['Computer']['mmodel'] = $array['BIOS']['MMODEL'];
351         }
352
353         if (isset($array['BIOS']['SSN'])) {
354            $a_inventory['Computer']['serial'] = trim($array['BIOS']['SSN']);
355            // HP patch for serial begin with 'S'
356            if ((isset($a_inventory['Computer']['manufacturers_id']))
357                  AND (strstr($a_inventory['Computer']['manufacturers_id'], "ewlett"))
358                    && preg_match("/^[sS]/", $a_inventory['Computer']['serial'])) {
359               $a_inventory['Computer']['serial'] = trim(
360                                                preg_replace("/^[sS]/",
361                                                             "",
362                                                             $a_inventory['Computer']['serial']));
363            }
364         }
365         if (isset($array['BIOS']['MSN'])){
366            $a_inventory['Computer']['mserial'] = trim($array['BIOS']['MSN']);
367         }
368      }
369
370      // * Type of computer
371
372      //First the HARDWARE/VMSYSTEM is not Physical : then it's a virtual machine
373      if (isset($array['HARDWARE']['VMSYSTEM'])
374            && $array['HARDWARE']['VMSYSTEM'] != ''
375               && $array['HARDWARE']['VMSYSTEM'] != 'Physical') {
376         $a_inventory['Computer']['computertypes_id'] = $array['HARDWARE']['VMSYSTEM'];
377         // HACK FOR BSDJail, remove serial and UUID (because it's of host, not contener)
378         if ($array['HARDWARE']['VMSYSTEM'] == 'BSDJail') {
379            if (isset($a_inventory['Computer']['serial'])) {
380               $a_inventory['Computer']['serial'] = '';
381            }
382
383            $a_inventory['Computer']['uuid'] .= "-".$a_inventory['Computer']['name'];
384         }
385      } else {
386         //It's not a virtual machine, then check :
387         //1 - HARDWARE/CHASSIS_TYPE
388         //2 - BIOS/TYPE
389         //3 - BIOS/MMODEL
390         //4 - HARDWARE/VMSYSTEM (should not go there)
391         if (isset($array['HARDWARE']['CHASSIS_TYPE'])
392               && !empty($array['HARDWARE']['CHASSIS_TYPE'])) {
393            $a_inventory['Computer']['computertypes_id'] = $array['HARDWARE']['CHASSIS_TYPE'];
394         } else  if (isset($array['BIOS']['TYPE'])
395               && !empty($array['BIOS']['TYPE'])) {
396            $a_inventory['Computer']['computertypes_id'] = $array['BIOS']['TYPE'];
397         } else if (isset($array['BIOS']['MMODEL'])
398               && !empty($array['BIOS']['MMODEL'])) {
399            $a_inventory['Computer']['computertypes_id'] = $array['BIOS']['MMODEL'];
400         } else if (isset($array['HARDWARE']['VMSYSTEM'])
401               && !empty($array['HARDWARE']['VMSYSTEM'])) {
402            $a_inventory['Computer']['computertypes_id'] = $array['HARDWARE']['VMSYSTEM'];
403         }
404      }
405
406//      if (isset($array['BIOS']['SKUNUMBER'])) {
407//         $a_inventory['BIOS']['PARTNUMBER'] = $array['BIOS']['SKUNUMBER'];
408//      }
409
410      if (isset($array['BIOS']['BDATE'])) {
411         $a_split = explode("/", $array['BIOS']['BDATE']);
412         // 2011-06-29 13:19:48
413         if (isset($a_split[0])
414                 AND isset($a_split[1])
415                 AND isset($a_split[2])) {
416            $a_inventory['fusioninventorycomputer']['bios_date'] = $a_split[2]."-".$a_split[0]."-".$a_split[1];
417         }
418      }
419      if (isset($array['BIOS']['BVERSION'])) {
420         $a_inventory['fusioninventorycomputer']['bios_version'] = $array['BIOS']['BVERSION'];
421      }
422      if (isset($array['BIOS']['BMANUFACTURER'])) {
423         $a_inventory['fusioninventorycomputer']['bios_manufacturers_id'] = $array['BIOS']['BMANUFACTURER'];
424      }
425
426      $CFG_GLPI['plugin_fusioninventory_computermanufacturer'][$a_inventory['Computer']['manufacturers_id']] = $a_inventory['Computer']['manufacturers_id'];
427
428
429      // * OPERATINGSYSTEM
430      if (isset($array['OPERATINGSYSTEM'])) {
431         $array_tmp = $thisc->addValues(
432                 $array['OPERATINGSYSTEM'],
433                 array(
434                    'FULL_NAME'      => 'operatingsystems_id',
435                    'VERSION'        => 'operatingsystemversions_id',
436                    'SERVICE_PACK'   => 'operatingsystemservicepacks_id',
437                    'ARCH'           => 'plugin_fusioninventory_computerarchs_id'));
438
439         if (!isset($array['OPERATINGSYSTEM']['VERSION'])
440                 && isset($array['OPERATINGSYSTEM']['KERNEL_VERSION'])) {
441            $array_tmp['operatingsystemversions_id'] = $array['OPERATINGSYSTEM']['KERNEL_VERSION'];
442         }
443         foreach ($array_tmp as $key=>$value) {
444            if (isset($a_inventory['Computer'][$key])
445                    && $a_inventory['Computer'][$key] != '') {
446               $a_inventory['Computer'][$key] = $value;
447            }
448         }
449         if (isset($array_tmp['plugin_fusioninventory_computerarchs_id'])
450                 && $array_tmp['plugin_fusioninventory_computerarchs_id'] != '') {
451
452            $rulecollection = new PluginFusioninventoryRuleDictionnaryComputerArchCollection();
453            $res_rule = $rulecollection->processAllRules(array("name"=>$array_tmp['plugin_fusioninventory_computerarchs_id']));
454            if (isset($res_rule['name'])) {
455            $a_inventory['fusioninventorycomputer']['plugin_fusioninventory_computerarchs_id'] =
456                 $res_rule['name'];
457            } else {
458               $a_inventory['fusioninventorycomputer']['plugin_fusioninventory_computerarchs_id'] =
459                  $array_tmp['plugin_fusioninventory_computerarchs_id'];
460            }
461         }
462      }
463
464      // otherserial (on tag) if defined in config
465      if ($pfConfig->getValue('otherserial') == 1) {
466         if (isset($array['ACCOUNTINFO'])) {
467            //In very rare case, ACCOUNTINFO section is present twice in the XML file...
468            if (isset($array['ACCOUNTINFO'][0])) {
469               $tmpacc = $array['ACCOUNTINFO'][0];
470               $array['ACCOUNTINFO'] = $tmpacc;
471            }
472            if (isset($array['ACCOUNTINFO']['KEYNAME'])
473                    && $array['ACCOUNTINFO']['KEYNAME'] == 'TAG') {
474               if (isset($array['ACCOUNTINFO']['KEYVALUE'])
475                       && $array['ACCOUNTINFO']['KEYVALUE'] != '') {
476                  $a_inventory['Computer']['otherserial'] = $array['ACCOUNTINFO']['KEYVALUE'];
477               }
478            }
479         }
480      }
481
482      // Hack for problems of ESX inventory with same deviceid than real computer inventory
483      if (isset($a_inventory['Computer']['operatingsystems_id'])
484              && strstr($a_inventory['Computer']['operatingsystems_id'], 'VMware ESX')) {
485         $PF_ESXINVENTORY = TRUE;
486      }
487
488      // * BATTERIES
489//      $a_inventory['batteries'] = array();
490//      if (isset($array['BATTERIES'])) {
491//         foreach ($array['BATTERIES'] as $a_batteries) {
492//            $a_inventory['soundcard'][] = $thisc->addValues($a_batteries,
493//               array(
494//                  'NAME'          => 'name',
495//                  'MANUFACTURER'  => 'manufacturers_id',
496//                  'SERIAL'     => 'serial',
497//                  'DATE'       => 'date',
498//                  'CAPACITY'   => 'capacity',
499//                  'CHEMISTRY'  => 'plugin_fusioninventory_inventorycomputerchemistries_id',
500//                  'VOLTAGE'    => 'voltage'));
501//         }
502//      }
503
504
505
506      // * SOUNDS
507      $a_inventory['soundcard'] = array();
508      if ($pfConfig->getValue('component_soundcard') == 1) {
509         if (isset($array['SOUNDS'])) {
510            foreach ($array['SOUNDS'] as $a_sounds) {
511               $a_inventory['soundcard'][] = $thisc->addValues($a_sounds,
512                                                           array(
513                                                              'NAME'          => 'designation',
514                                                              'MANUFACTURER'  => 'manufacturers_id',
515                                                              'DESCRIPTION'   => 'comment'));
516
517               $ignorecontrollers[$a_sounds['NAME']] = 1;
518            }
519         }
520      }
521
522      // * VIDEOS
523      $a_inventory['graphiccard'] = array();
524      if ($pfConfig->getValue('component_graphiccard') == 1) {
525         if (isset($array['VIDEOS'])) {
526            foreach ($array['VIDEOS'] as $a_videos) {
527               if (is_array($a_videos)
528                       && isset($a_videos['NAME'])) {
529                  $array_tmp = $thisc->addValues($a_videos, array(
530                                                              'NAME'   => 'designation',
531                                                              'MEMORY' => 'memory'));
532                  $array_tmp['designation'] = trim($array_tmp['designation']);
533                  $a_inventory['graphiccard'][] = $array_tmp;
534                  if (isset($a_videos['NAME'])) {
535                     $ignorecontrollers[$a_videos['NAME']] = 1;
536                  }
537                  if (isset($a_videos['CHIPSET'])) {
538                     $ignorecontrollers[$a_videos['CHIPSET']] = 1;
539                  }
540               }
541            }
542         }
543      }
544
545      // * NETWORK CARD
546      $a_inventory['networkcard'] = array();
547      if ($pfConfig->getValue('component_networkcard') == 1) {
548         if (isset($array['NETWORKS'])) {
549            foreach ($array['NETWORKS'] as $a_netcards) {
550               if (is_array($a_netcards)
551                       && isset($a_netcards['DESCRIPTION'])) {
552
553                  // Search in controller if find NAME = CONTROLLER TYPE
554                  $a_found = array();
555                  if (isset($array['CONTROLLERS'])) {
556                     foreach ($array['CONTROLLERS'] as $a_controllers) {
557                        if (count($a_found) == 0) {
558                           if (isset($a_controllers['TYPE'])
559                              && ($a_netcards['DESCRIPTION'] == $a_controllers['TYPE']
560                                   || strtolower($a_netcards['DESCRIPTION']." controller") ==
561                                          strtolower($a_controllers['TYPE']))
562                                 && !isset($ignorecontrollers[$a_controllers['NAME']])) {
563                              $a_found = $a_controllers;
564                              if (isset($a_netcards['MACADDR'])) {
565                                 $a_found['MACADDR'] = $a_netcards['MACADDR'];
566                              }
567                           }
568                        }
569                     }
570                  }
571                  if (count($a_found) > 0) {
572                     $array_tmp = $thisc->addValues($a_found,
573                                                    array(
574                                                       'NAME'          => 'designation',
575                                                       'MANUFACTURER'  => 'manufacturers_id',
576                                                       'MACADDR'       => 'mac'));
577                     if (isset($a_found['PCIID'])) {
578                        $a_PCIData =
579                              PluginFusioninventoryInventoryExternalDB::getDataFromPCIID(
580                                $a_found['PCIID']
581                              );
582                        if (isset($a_PCIData['manufacturer'])) {
583                           $array_tmp['manufacturers_id'] = $a_PCIData['manufacturer'];
584                        }
585                        if (isset($a_PCIData['name'])) {
586                           $array_tmp['designation'] = $a_PCIData['name'];
587                        }
588                        $array_tmp['designation'] = Toolbox::addslashes_deep($array_tmp['designation']);
589                     }
590                     $array_tmp['mac'] = strtolower($array_tmp['mac']);
591                     $a_inventory['networkcard'][] = $array_tmp;
592
593                     if (isset($a_found['NAME'])) {
594                        $ignorecontrollers[$a_found['NAME']] = 1;
595                     }
596                  }
597               }
598            }
599         }
600      }
601
602      // * NETWORKS
603      $a_inventory['networkport'] = array();
604      if ($pfConfig->getValue('component_networkcard') == 1) {
605         if (isset($array['NETWORKS'])) {
606            $a_networknames = array();
607            foreach ($array['NETWORKS'] as $a_networks) {
608               $virtual_import = 1;
609               if ($pfConfig->getValue("component_networkcardvirtual") == 0) {
610                  if (isset($a_networks['VIRTUALDEV'])
611                          && $a_networks['VIRTUALDEV'] == 1) {
612
613                     $virtual_import = 0;
614                  }
615               }
616               if ($virtual_import == 1) {
617                  $array_tmp = $thisc->addValues($a_networks,
618                                                 array(
619                                                    'DESCRIPTION' => 'name',
620                                                    'MACADDR'     => 'mac',
621                                                    'TYPE'        => 'instantiation_type',
622                                                    'IPADDRESS'   => 'ip',
623                                                    'VIRTUALDEV'  => 'virtualdev',
624                                                    'IPSUBNET'    => 'subnet',
625                                                    'SSID'        => 'ssid',
626                                                    'IPGATEWAY'   => 'gateway',
627                                                    'IPMASK'      => 'netmask',
628                                                    'IPDHCP'      => 'dhcpserver',
629                                                    'SPEED'       => 'speed'));
630
631                  if ((isset($array_tmp['name'])
632                          && $array_tmp['name'] != '')
633                       || (isset($array_tmp['mac'])
634                          && $array_tmp['mac'] != '')) {
635
636                     if (!isset($array_tmp['virtualdev'])
637                             || $array_tmp['virtualdev'] != 1) {
638                        $array_tmp['virtualdev'] = 0;
639                     }
640                     $array_tmp['logical_number'] = 1;
641                     if ($array_tmp['virtualdev'] == 1) {
642                        $array_tmp['logical_number'] = 0;
643                     }
644
645                     $array_tmp['mac'] = strtolower($array_tmp['mac']);
646                     if (isset($a_networknames[$array_tmp['name'].'-'.$array_tmp['mac']])) {
647                        if (isset($array_tmp['ip'])
648                                && $array_tmp['ip'] != '') {
649                           if (!in_array($array_tmp['ip'], $a_networknames[$array_tmp['name'].'-'.$array_tmp['mac']]['ipaddress'])) {
650                              $a_networknames[$array_tmp['name'].'-'.$array_tmp['mac']]['ipaddress'][]
651                                      = $array_tmp['ip'];
652                           }
653                        }
654                        if (isset($a_networks['IPADDRESS6'])
655                                && $a_networks['IPADDRESS6'] != '') {
656                           if (!in_array($a_networks['IPADDRESS6'], $a_networknames[$array_tmp['name'].'-'.$array_tmp['mac']]['ipaddress'])) {
657                              $a_networknames[$array_tmp['name'].'-'.$array_tmp['mac']]['ipaddress'][]
658                                      = $a_networks['IPADDRESS6'];
659                           }
660                        }
661                     } else {
662                        if (isset($array_tmp['ip'])
663                                && $array_tmp['ip'] != '') {
664                           $array_tmp['ipaddress'] = array($array_tmp['ip']);
665                           unset($array_tmp['ip']);
666                        } else {
667                           $array_tmp['ipaddress'] = array();
668                        }
669                        if (isset($a_networks['IPADDRESS6'])
670                              && $a_networks['IPADDRESS6'] != '') {
671                           $array_tmp['ipaddress'][] = $a_networks['IPADDRESS6'];
672                        }
673
674                        if (isset($array_tmp["instantiation_type"])
675                                AND $array_tmp["instantiation_type"] == 'Ethernet') {
676                           $array_tmp["instantiation_type"] = 'NetworkPortEthernet';
677                        } else if (isset($array_tmp["instantiation_type"])
678                                AND ($array_tmp["instantiation_type"] == 'wifi'
679                                     OR $array_tmp["instantiation_type"] == 'IEEE')) {
680                           $array_tmp["instantiation_type"] = 'NetworkPortWifi';
681                        } else if ($array_tmp['mac'] != '') {
682                           $array_tmp["instantiation_type"] = 'NetworkPortEthernet';
683                        } else {
684                           $array_tmp["instantiation_type"] = 'NetworkPortLocal';
685                        }
686                        if (isset($array_tmp['ip'])) {
687                           unset($array_tmp['ip']);
688                        }
689                        if (isset($array_tmp['speed'])
690                                && is_numeric($array_tmp['speed'])) {
691                           // Old agent version have speed in b/s instead Mb/s
692                           if ($array_tmp['speed'] > 100000) {
693                              $array_tmp['speed'] = $array_tmp['speed'] / 1000000;
694                           }
695                        } else {
696                           $array_tmp['speed'] = 0;
697                        }
698
699                        $a_networknames[$array_tmp['name'].'-'.$array_tmp['mac']] = $array_tmp;
700                     }
701                  }
702               }
703            }
704            $a_inventory['networkport'] = $a_networknames;
705         }
706      }
707
708
709      // * CONTROLLERS
710      $a_inventory['controller'] = array();
711      if ($pfConfig->getValue('component_control') == 1) {
712         if (isset($array['CONTROLLERS'])) {
713            foreach ($array['CONTROLLERS'] as $a_controllers) {
714               if ((isset($a_controllers["NAME"]))
715                       AND (!isset($ignorecontrollers[$a_controllers["NAME"]]))) {
716                  $array_tmp = $thisc->addValues($a_controllers,
717                                                 array(
718                                                    'NAME'          => 'designation',
719                                                    'MANUFACTURER'  => 'manufacturers_id',
720                                                    'type'          => 'interfacetypes_id'));
721                  if (isset($a_controllers['PCIID'])) {
722                     $a_PCIData =
723                           PluginFusioninventoryInventoryExternalDB::getDataFromPCIID(
724                             $a_controllers['PCIID']
725                           );
726                     if (isset($a_PCIData['manufacturer'])) {
727                        $array_tmp['manufacturers_id'] = $a_PCIData['manufacturer'];
728                     }
729                     if (isset($a_PCIData['name'])) {
730                        $array_tmp['designation'] = $a_PCIData['name'];
731                     }
732                     $array_tmp['designation'] = Toolbox::addslashes_deep($array_tmp['designation']);
733                  }
734                  $a_inventory['controller'][] = $array_tmp;
735               }
736            }
737         }
738      }
739
740      // * CPUS
741      $a_inventory['processor'] = array();
742      if ($pfConfig->getValue('component_processor') == 1) {
743         if (isset($array['CPUS'])) {
744            foreach ($array['CPUS'] as $a_cpus) {
745               if (is_array($a_cpus)
746                       && (isset($a_cpus['NAME'])
747                        || isset($a_cpus['TYPE']))) {
748                  $array_tmp = $thisc->addValues($a_cpus,
749                                                 array(
750                                                    'SPEED'        => 'frequency',
751                                                    'MANUFACTURER' => 'manufacturers_id',
752                                                    'SERIAL'       => 'serial',
753                                                    'NAME'         => 'designation',
754                                                    'CORE'         => 'nbcores',
755                                                    'THREAD'       => 'nbthreads'));
756                  if ($array_tmp['designation'] == ''
757                          && isset($a_cpus['TYPE'])) {
758                     $array_tmp['designation'] = $a_cpus['TYPE'];
759                  }
760                  $array_tmp['frequence'] = $array_tmp['frequency'];
761                  $array_tmp['frequency_default'] = $array_tmp['frequency'];
762                  $a_inventory['processor'][] = $array_tmp;
763               }
764            }
765         }
766      }
767
768      // * DRIVES
769      $a_inventory['computerdisk'] = array();
770      if (isset($array['DRIVES'])) {
771         foreach ($array['DRIVES'] as $a_drives) {
772            if ($pfConfig->getValue("component_drive") == '0'
773                OR ($pfConfig->getValue("component_networkdrive") == '0'
774                    AND ((isset($a_drives['TYPE'])
775                       AND $a_drives['TYPE'] == 'Network Drive')
776                        OR isset($a_drives['FILESYSTEM'])
777                       AND $a_drives['FILESYSTEM'] == 'nfs'))
778                OR ((isset($a_drives['TYPE'])) AND
779                    (($a_drives['TYPE'] == "Removable Disk")
780                   OR ($a_drives['TYPE'] == "Compact Disc")))) {
781
782            } else {
783               if ($pfConfig->getValue('import_volume') == 1) {
784                  $array_tmp = $thisc->addValues($a_drives,
785                                                 array(
786                                                    'VOLUMN'      => 'device',
787                                                    'FILESYSTEM'  => 'filesystems_id',
788                                                    'TOTAL'       => 'totalsize',
789                                                    'FREE'        => 'freesize'));
790                  if ((isset($a_drives['LABEL'])) AND (!empty($a_drives['LABEL']))) {
791                     $array_tmp['name'] = $a_drives['LABEL'];
792                  } else if (((!isset($a_drives['VOLUMN']))
793                          OR (empty($a_drives['VOLUMN'])))
794                          AND (isset($a_drives['LETTER']))) {
795                     $array_tmp['name'] = $a_drives['LETTER'];
796                  } else if (isset($a_drives['TYPE'])) {
797                     $array_tmp['name'] = $a_drives['TYPE'];
798                  } else if (isset($a_drives['VOLUMN'])) {
799                     $array_tmp['name'] = $a_drives['VOLUMN'];
800                  }
801                  if (isset($a_drives['MOUNTPOINT'])) {
802                     $array_tmp['mountpoint'] = $a_drives['MOUNTPOINT'];
803                  } else if (isset($a_drives['LETTER'])) {
804                     $array_tmp['mountpoint'] = $a_drives['LETTER'];
805                  } else if (isset($a_drives['TYPE'])) {
806                     $array_tmp['mountpoint'] = $a_drives['TYPE'];
807                  }
808                  $a_inventory['computerdisk'][] = $array_tmp;
809               }
810            }
811         }
812      }
813
814      // * MEMORIES
815      $a_inventory['memory'] = array();
816      if ($pfConfig->getValue('component_memory') == 1) {
817         if (isset($array['MEMORIES'])) {
818            foreach ($array['MEMORIES'] as $a_memories) {
819               if ((!isset($a_memories["CAPACITY"]))
820                    OR ((isset($a_memories["CAPACITY"]))
821                            AND (!preg_match("/^[0-9]+$/i", $a_memories["CAPACITY"])))) {
822                  // Nothing
823               } else {
824                  $array_tmp = $thisc->addValues($a_memories,
825                                                 array(
826                                                    'CAPACITY'     => 'size',
827                                                    'SPEED'        => 'frequence',
828                                                    'TYPE'         => 'devicememorytypes_id',
829                                                    'SERIALNUMBER' => 'serial',
830                                                    'NUMSLOTS'     => 'busID'));
831                  if ($array_tmp['size'] > 0) {
832                     $array_tmp['designation'] = "";
833                     if (isset($a_memories["TYPE"])
834                             && $a_memories["TYPE"]!="Empty Slot"
835                             && $a_memories["TYPE"] != "Unknown") {
836                        $array_tmp["designation"] = $a_memories["TYPE"];
837                     }
838                     if (isset($a_memories["DESCRIPTION"])) {
839                        if (!empty($array_tmp["designation"])) {
840                           $array_tmp["designation"].=" - ";
841                        }
842                        $array_tmp["designation"] .= $a_memories["DESCRIPTION"];
843                     }
844                     $a_inventory['memory'][] = $array_tmp;
845                  }
846               }
847            }
848         }
849         else if (isset($array['HARDWARE']['MEMORY'])) {
850            $array_tmp = $thisc->addValues($array['HARDWARE'],
851                                           array(
852                                              'MEMORY' => 'size'));
853            $array_tmp['designation'] = 'Dummy Memory Module';
854            $a_inventory['memory'][] = $array_tmp;
855         }
856      }
857
858      // * MONITORS
859      $a_inventory['monitor'] = array();
860      if (isset($array['MONITORS'])) {
861         $a_serialMonitor = array();
862         foreach ($array['MONITORS'] as $a_monitors) {
863            $array_tmp = $thisc->addValues($a_monitors,
864                                           array(
865                                              'CAPTION'      => 'name',
866                                              'MANUFACTURER' => 'manufacturers_id',
867                                              'SERIAL'       => 'serial',
868                                              'DESCRIPTION'  => 'comment'));
869            $array_tmp['is_dynamic'] = 1;
870            if (!isset($array_tmp['name'])) {
871               $array_tmp['name'] = '';
872            }
873            if ($array_tmp['name'] == ''
874                    && isset($array_tmp['comment'])) {
875               $array_tmp['name'] = $array_tmp['comment'];
876            }
877            if (isset($array_tmp['comment'])) {
878               unset($array_tmp['comment']);
879            }
880            if (!isset($array_tmp['serial'])) {
881               $array_tmp['serial'] = '';
882            }
883            if (!isset($array_tmp['manufacturers_id'])) {
884               $array_tmp['manufacturers_id'] = '';
885            }
886            if (!isset($a_serialMonitor[$array_tmp['serial']])) {
887               $a_inventory['monitor'][] = $array_tmp;
888               $a_serialMonitor[$array_tmp['serial']] = 1;
889            }
890         }
891      }
892
893
894      // * PRINTERS
895      $a_inventory['printer'] = array();
896      if (isset($array['PRINTERS'])) {
897         $rulecollection = new RuleDictionnaryPrinterCollection();
898         foreach ($array['PRINTERS'] as $a_printers) {
899            $array_tmp = $thisc->addValues($a_printers,
900                                           array(
901                                              'NAME'         => 'name',
902                                              'PORT'         => 'port',
903                                              'SERIAL'       => 'serial'));
904            $array_tmp['is_dynamic'] = 1;
905            if (strstr($array_tmp['port'], "USB")) {
906               $array_tmp['have_usb'] = 1;
907            } else {
908               $array_tmp['have_usb'] = 0;
909            }
910            unset($array_tmp['port']);
911            $res_rule = $rulecollection->processAllRules(array("name"=>$array_tmp['name']));
912            if (isset($res_rule['_ignore_ocs_import'])
913                    && $res_rule['_ignore_ocs_import'] == "1") {
914               // Ignrore import printer
915            } else if (isset($res_rule['_ignore_import'])
916                    && $res_rule['_ignore_import'] == "1") {
917               // Ignrore import printer
918            } else {
919               if (isset($res_rule['name'])) {
920                  $array_tmp['name'] = $res_rule['name'];
921               }
922               if (isset($res_rule['manufacturer'])) {
923                  $array_tmp['manufacturers_id'] = $res_rule['manufacturer'];
924               }
925               $a_inventory['printer'][] = $array_tmp;
926            }
927         }
928      }
929
930
931
932      // * PERIPHERAL
933      $a_inventory['peripheral'] = array();
934      $a_peripheral_name = array();
935      $per = 0;
936      if (isset($array['USBDEVICES'])) {
937         foreach ($array['USBDEVICES'] as $a_peripherals) {
938            $array_tmp = $thisc->addValues($a_peripherals,
939                                           array(
940                                              'NAME'         => 'name',
941                                              'MANUFACTURER' => 'manufacturers_id',
942                                              'SERIAL'       => 'serial',
943                                              'PRODUCTNAME'  => 'productname'));
944
945            $array_tmp['is_dynamic'] = 1;
946            if(isset($a_peripherals['VENDORID'])
947                     AND $a_peripherals['VENDORID'] != ''
948                     AND isset($a_peripherals['PRODUCTID'])) {
949
950               $dataArray = PluginFusioninventoryInventoryExternalDB::getDataFromUSBID(
951                          $a_peripherals['VENDORID'],
952                          $a_peripherals['PRODUCTID']
953                       );
954               $dataArray[0] = preg_replace('/&(?!\w+;)/', '&amp;', $dataArray[0]);
955               if (!empty($dataArray[0])
956                       AND empty($array_tmp['manufacturers_id'])) {
957                  $array_tmp['manufacturers_id'] = $dataArray[0];
958               }
959               $dataArray[1] = preg_replace('/&(?!\w+;)/', '&amp;', $dataArray[1]);
960               if (!empty($dataArray[1])
961                       AND empty($a_peripherals['productname'])) {
962                  $a_peripherals['productname'] = $dataArray[1];
963               }
964            }
965
966            if ($array_tmp['productname'] != '') {
967               $array_tmp['name'] = $array_tmp['productname'];
968            }
969            unset($array_tmp['productname']);
970
971            $a_inventory['peripheral'][] = $array_tmp;
972            $a_peripheral_name[$array_tmp['name']] = $per;
973            $per++;
974         }
975      }
976      if (isset($array['INPUTS'])) {
977         $a_pointingtypes = array(
978             3 => 'Mouse',
979             4 => 'Trackball',
980             5 => 'Track Point',
981             6 => 'Glide Point',
982             7 => 'Touch Pad',
983             8 => 'Touch Screen',
984             9 => 'Mouse - Optical Sensor'
985         );
986         foreach ($array['INPUTS'] as $a_peripherals) {
987            $array_tmp = $thisc->addValues($a_peripherals,
988                                           array(
989                                              'NAME'         => 'name',
990                                              'MANUFACTURER' => 'manufacturers_id'));
991            $array_tmp['serial'] = '';
992            $array_tmp['peripheraltypes_id'] = '';
993            if (isset($a_peripherals['POINTINGTYPE'])
994                    && isset($a_pointingtypes[$a_peripherals['POINTINGTYPE']])) {
995
996               $array_tmp['peripheraltypes_id'] = $a_pointingtypes[$a_peripherals['POINTINGTYPE']];
997            }
998            if (isset($a_peripherals['LAYOUT'])) {
999               $array_tmp['peripheraltypes_id'] = 'keyboard';
1000            }
1001
1002            if (isset($a_peripheral_name[$array_tmp['name']])) {
1003               $a_inventory['peripheral'][$a_peripheral_name[$array_tmp['name']]]['peripheraltypes_id'] = $array_tmp['peripheraltypes_id'];
1004            } else {
1005               $a_inventory['peripheral'][] = $array_tmp;
1006            }
1007         }
1008      }
1009
1010      // * SLOTS
1011
1012      // * SOFTWARES
1013      $a_inventory['SOFTWARES'] = array();
1014      if ($pfConfig->getValue('import_software') == 1) {
1015         if (isset($array['SOFTWARES'])) {
1016            $a_inventory['SOFTWARES'] = $array['SOFTWARES'];
1017         }
1018      }
1019
1020      // * STORAGES/COMPUTERDISK
1021      $a_inventory['harddrive'] = array();
1022      if (isset($array['STORAGES'])) {
1023         foreach ($array['STORAGES']  as $a_storage) {
1024            $type_tmp = PluginFusioninventoryFormatconvert::getTypeDrive($a_storage);
1025            if ($type_tmp == "Drive") {
1026               // it's cd-rom / dvd
1027//               if ($pfConfig->getValue(,
1028//                    "component_drive") =! 0) {
1029               if ($pfConfig->getValue('component_drive') == 1) {
1030                  $array_tmp = $thisc->addValues($a_storage,
1031                                                 array(
1032                                                    'SERIALNUMBER' => 'serial',
1033                                                    'NAME'         => 'designation',
1034                                                    'TYPE'         => 'interfacetypes_id',
1035                                                    'MANUFACTURER' => 'manufacturers_id',
1036                                                     ));
1037                  if ($array_tmp['designation'] == '') {
1038                     if (isset($a_storage['DESCRIPTION'])) {
1039                        $array_tmp['designation'] = $a_storage['DESCRIPTION'];
1040                     }
1041                  }
1042                  $a_inventory['drive'][] = $array_tmp;
1043                }
1044            } else {
1045               // it's harddisk
1046//               if ($pfConfig->getValue(,
1047//                    "component_harddrive") != 0) {
1048               if (is_array($a_storage)) {
1049                  if ($pfConfig->getValue('component_harddrive') == 1) {
1050                     $array_tmp = $thisc->addValues($a_storage,
1051                                                    array(
1052                                                        'DISKSIZE'      => 'capacity',
1053                                                        'INTERFACE'     => 'interfacetypes_id',
1054                                                        'MANUFACTURER'  => 'manufacturers_id',
1055                                                        'MODEL'         => 'designation',
1056                                                        'SERIALNUMBER'  => 'serial'));
1057                     if ($array_tmp['designation'] == '') {
1058                        if (isset($a_storage['NAME'])) {
1059                           $array_tmp['designation'] = $a_storage['NAME'];
1060                        } else if (isset($a_storage['DESIGNATION'])) {
1061                           $array_tmp['designation'] = $a_storage['DESIGNATION'];
1062                        }
1063                     }
1064                     $a_inventory['harddrive'][] = $array_tmp;
1065                  }
1066               }
1067            }
1068         }
1069      }
1070
1071
1072
1073
1074      // * USERS
1075      $cnt = 0;
1076      if (isset($array['USERS'])) {
1077         if (count($array['USERS']) > 0) {
1078            $user_temp = '';
1079            if (isset($a_inventory['Computer']['contact'])) {
1080               $user_temp = $a_inventory['Computer']['contact'];
1081            }
1082            $a_inventory['Computer']['contact'] = '';
1083         }
1084         foreach ($array['USERS'] as $a_users) {
1085            $array_tmp = $thisc->addValues($a_users,
1086                                           array(
1087                                              'LOGIN'  => 'login',
1088                                              'DOMAIN' => 'domain'));
1089            $user = '';
1090            if (isset($array_tmp['login'])) {
1091               $user = $array_tmp['login'];
1092               if (isset($array_tmp['domain'])
1093                       && !empty($array_tmp['domain'])) {
1094                  $user .= "@".$array_tmp['domain'];
1095               }
1096            }
1097            if ($cnt == 0) {
1098               if (isset($array_tmp['login'])) {
1099                  $query = "SELECT `id`
1100                            FROM `glpi_users`
1101                            WHERE `name` = '" . $array_tmp['login'] . "'
1102                            LIMIT 1";
1103                  $result = $DB->query($query);
1104                  if ($DB->numrows($result) == 1) {
1105                     $a_inventory['Computer']['users_id'] = $DB->result($result, 0, 0);
1106                  }
1107               }
1108            }
1109
1110            if ($user != '') {
1111               if (isset($a_inventory['Computer']['contact'])) {
1112                  if ($a_inventory['Computer']['contact'] == '') {
1113                     $a_inventory['Computer']['contact'] = $user;
1114                  } else {
1115                     $a_inventory['Computer']['contact'] .= "/".$user;
1116                  }
1117               } else {
1118                  $a_inventory['Computer']['contact'] = $user;
1119               }
1120            }
1121            $cnt++;
1122         }
1123         if (empty($a_inventory['Computer']['contact'])) {
1124            $a_inventory['Computer']['contact'] = $user_temp;
1125         }
1126      }
1127
1128      // * VIRTUALMACHINES
1129      $a_inventory['virtualmachine'] = array();
1130      if ($pfConfig->getValue('import_vm') == 1) {
1131         if (isset($array['VIRTUALMACHINES'])) {
1132            foreach ($array['VIRTUALMACHINES'] as $a_virtualmachines) {
1133               $array_tmp = $thisc->addValues($a_virtualmachines,
1134                                              array(
1135                                                 'NAME'        => 'name',
1136                                                 'VCPU'        => 'vcpu',
1137                                                 'MEMORY'      => 'ram',
1138                                                 'VMTYPE'      => 'virtualmachinetypes_id',
1139                                                 'SUBSYSTEM'   => 'virtualmachinesystems_id',
1140                                                 'STATUS'      => 'virtualmachinestates_id',
1141                                                 'UUID'        => 'uuid'));
1142               $array_tmp['is_dynamic'] = 1;
1143               // Hack for BSD jails
1144               if ($array_tmp['virtualmachinetypes_id'] == 'jail') {
1145                  $array_tmp['uuid'] = $a_inventory['Computer']['uuid']."-".$array_tmp['name'];
1146               }
1147
1148               $a_inventory['virtualmachine'][] = $array_tmp;
1149            }
1150         }
1151      }
1152      if ($pfConfig->getValue('create_vm') == 1) {
1153         if (isset($array['VIRTUALMACHINES'])) {
1154            foreach ($array['VIRTUALMACHINES'] as $a_virtualmachines) {
1155               if (strstr($a_virtualmachines['MEMORY'], 'MB')) {
1156                  $a_virtualmachines['MEMORY'] = str_replace('MB', '', $a_virtualmachines['MEMORY']);
1157               } else if (strstr($a_virtualmachines['MEMORY'], 'KB')) {
1158                  $a_virtualmachines['MEMORY'] = str_replace('KB', '', $a_virtualmachines['MEMORY']);
1159                  $a_virtualmachines['MEMORY'] = $a_virtualmachines['MEMORY'] / 1000;
1160               } else if (strstr($a_virtualmachines['MEMORY'], 'GB')) {
1161                  $a_virtualmachines['MEMORY'] = str_replace('GB', '', $a_virtualmachines['MEMORY']);
1162                  $a_virtualmachines['MEMORY'] = $a_virtualmachines['MEMORY'] * 1000;
1163               } else if (strstr($a_virtualmachines['MEMORY'], 'B')) {
1164                  $a_virtualmachines['MEMORY'] = str_replace('B', '', $a_virtualmachines['MEMORY']);
1165                  $a_virtualmachines['MEMORY'] = $a_virtualmachines['MEMORY'] / 1000000;
1166               }
1167               $array_tmp = $thisc->addValues($a_virtualmachines,
1168                                              array(
1169                                                 'NAME'            => 'name',
1170                                                 'VCPU'            => 'vcpu',
1171                                                 'MEMORY'          => 'ram',
1172                                                 'VMTYPE'          => 'computertypes_id',
1173                                                 'UUID'            => 'uuid',
1174                                                 'OPERATINGSYSTEM' => 'operatingsystems_id',
1175                                                 'CUSTOMFIELDS'    => 'comment'));
1176               $array_tmp['is_dynamic'] = 1;
1177               if (isset($array_tmp['comment'])
1178                       && is_array($array_tmp['comment'])) {
1179                  $a_com_temp = $array_tmp['comment'];
1180                  $array_tmp['comment'] = '';
1181                  foreach ($a_com_temp as $data) {
1182                     $array_tmp['comment'] .= $data['NAME'].' : '.$data['VALUE'].'\n';
1183                  }
1184               }
1185               $array_tmp['networkport'] = array();
1186               if (isset($a_virtualmachines['NETWORKS'])
1187                       && is_array($a_virtualmachines['NETWORKS'])) {
1188                  foreach ($a_virtualmachines['NETWORKS'] as $data) {
1189
1190                     $array_tmp_np = $thisc->addValues($data,
1191                                  array(
1192                                     'DESCRIPTION' => 'name',
1193                                     'MACADDR'     => 'mac',
1194                                     'IPADDRESS'   => 'ip'));
1195                     $array_tmp_np['instantiation_type'] = 'NetworkPortEthernet';
1196                     $array_tmp_np['mac'] = strtolower($array_tmp_np['mac']);
1197                     if (isset($array_tmp['networkport'][$array_tmp_np['name'].'-'.$array_tmp_np['mac']])) {
1198                        if (isset($array_tmp_np['ip'])) {
1199                           $array_tmp['networkport'][$array_tmp_np['name'].'-'.$array_tmp_np['mac']]['ipaddress'][]
1200                                   = $array_tmp_np['ip'];
1201                        }
1202                     } else {
1203                        if (isset($array_tmp_np['ip'])
1204                                && $array_tmp_np['ip'] != '') {
1205                           $array_tmp_np['ipaddress'] = array($array_tmp_np['ip']);
1206                           unset($array_tmp_np['ip']);
1207                        } else {
1208                           $array_tmp_np['ipaddress'] = array();
1209                        }
1210                        $array_tmp['networkport'][$array_tmp_np['name'].'-'.$array_tmp_np['mac']] = $array_tmp_np;
1211                     }
1212                  }
1213               }
1214               $a_inventory['virtualmachine_creation'][] = $array_tmp;
1215            }
1216         }
1217      }
1218
1219      // * ANTIVIRUS
1220      $a_inventory['antivirus'] = array();
1221      if (isset($array['ANTIVIRUS'])) {
1222         foreach ($array['ANTIVIRUS'] as $a_antiviruses) {
1223            $array_tmp = $thisc->addValues($a_antiviruses,
1224                                           array(
1225                                              'NAME'     => 'name',
1226                                              'COMPANY'  => 'manufacturers_id',
1227                                              'VERSION'  => 'version',
1228                                              'ENABLED'  => 'is_active',
1229                                              'UPTODATE' => 'uptodate'));
1230            $a_inventory['antivirus'][] = $array_tmp;
1231         }
1232      }
1233
1234      // * STORAGE/VOLUMES
1235      $a_inventory['storage'] = array();
1236/* begin code, may works at 90%
1237      if (isset($array['PHYSICAL_VOLUMES'])) {
1238         foreach ($array['PHYSICAL_VOLUMES'] as $a_physicalvolumes) {
1239            $array_tmp = $thisc->addValues($a_physicalvolumes,
1240                                           array(
1241                                              'DEVICE'   => 'name',
1242                                              'PV_UUID'  => 'uuid',
1243                                              'VG_UUID'  => 'uuid_link',
1244                                              'SIZE'     => 'totalsize',
1245                                              'FREE'     => 'freesize'));
1246            $array_tmp['plugin_fusioninventory_inventorycomputerstoragetypes_id'] =
1247                  'partition';
1248            $a_inventory['storage'][] = $array_tmp;
1249         }
1250      }
1251      if (isset($array['STORAGES'])) {
1252         foreach ($array['STORAGES']  as $a_storage) {
1253            $type_tmp = PluginFusioninventoryFormatconvert::getTypeDrive($a_storage);
1254            if ($type_tmp != "Drive") {
1255               if (isset($a_storage['NAME'])
1256                       AND $a_storage['NAME'] != '') {
1257                  $detectsize = 0;
1258                  $array_tmp = array();
1259
1260                  foreach ($a_inventory['storage'] as $a_physicalvol) {
1261                     if (preg_match("/^\/dev\/".$a_storage['NAME']."/", $a_physicalvol['name'])) {
1262                        $array_tmp['name'] = $a_storage['NAME'];
1263                        if (isset($a_storage['SERIALNUMBER'])) {
1264                           $array_tmp['uuid'] = $a_storage['SERIALNUMBER'];
1265                        } else {
1266                           $array_tmp['uuid'] = $a_storage['NAME'];
1267                        }
1268                        $array_tmp['plugin_fusioninventory_inventorycomputerstoragetypes_id'] =
1269                           'hard disk';
1270                        if (!isset($array_tmp['uuid_link'])) {
1271                           $array_tmp['uuid_link'] = array();
1272                        }
1273                        $array_tmp['uuid_link'][] = $a_physicalvol['uuid'];
1274                        $detectsize += $a_physicalvol['totalsize'];
1275                     }
1276                  }
1277                  if (isset($a_storage['DISKSIZE'])
1278                          && $a_storage['DISKSIZE'] != '') {
1279                     $array_tmp['totalsize'] = $a_storage['DISKSIZE'];
1280                     $array_tmp['size_dynamic'] = 0;
1281                  } else {
1282                     $array_tmp['totalsize'] = $detectsize;
1283                     $array_tmp['size_dynamic'] = 1;
1284                  }
1285                  $a_inventory['storage'][] = $array_tmp;
1286               }
1287            }
1288         }
1289      }
1290
1291      if (isset($array['VOLUME_GROUPS'])) {
1292         foreach ($array['VOLUME_GROUPS'] as $a_volumegroups) {
1293            $array_tmp = $thisc->addValues($a_volumegroups,
1294                                           array(
1295                                              'VG_NAME'  => 'name',
1296                                              'VG_UUID'  => 'uuid',
1297                                              'SIZE'     => 'totalsize',
1298                                              'FREE'     => 'freesize'));
1299            $array_tmp['plugin_fusioninventory_inventorycomputerstoragetypes_id'] =
1300                  'volume groups';
1301            $a_inventory['storage'][] = $array_tmp;
1302         }
1303      }
1304      if (isset($array['LOGICAL_VOLUMES'])) {
1305         foreach ($array['LOGICAL_VOLUMES'] as $a_logicalvolumes) {
1306            $array_tmp = $thisc->addValues($a_logicalvolumes,
1307                                           array(
1308                                              'LV_NAME'  => 'name',
1309                                              'LV_UUID'  => 'uuid',
1310                                              'VG_UUID'  => 'uuid_link',
1311                                              'SIZE'     => 'totalsize'));
1312            $array_tmp['plugin_fusioninventory_inventorycomputerstoragetypes_id'] =
1313                  'logical volumes';
1314            $a_inventory['storage'][] = $array_tmp;
1315         }
1316      }
1317
1318      if (isset($array['DRIVES'])) {
1319         foreach ($array['DRIVES'] as $a_drives) {
1320            if ((((isset($a_drives['TYPE'])
1321                       AND $a_drives['TYPE'] == 'Network Drive')
1322                        OR isset($a_drives['FILESYSTEM'])
1323                       AND $a_drives['FILESYSTEM'] == 'nfs'))
1324                OR ((isset($a_drives['TYPE'])) AND
1325                    (($a_drives['TYPE'] == "Removable Disk")
1326                   OR ($a_drives['TYPE'] == "Compact Disc")))) {
1327
1328            } else if (isset($a_drives['VOLUMN'])
1329                    && strstr($a_drives['VOLUMN'], "/dev/mapper")){
1330               // LVM
1331               $a_split = explode("-", $a_drives['VOLUMN']);
1332               $volumn = end($a_split);
1333               $detectsize = 0;
1334               $array_tmp = array();
1335               foreach ($a_inventory['storage'] as $num=>$a_physicalvol) {
1336                  if ($a_physicalvol['plugin_fusioninventory_inventorycomputerstoragetypes_id']
1337                          == 'logical volumes') {
1338                     if ($volumn == $a_physicalvol['name']) {
1339                        $array_tmp['name'] = $a_drives['TYPE'];
1340                        if (isset($a_drives['SERIAL'])) {
1341                           $array_tmp['uuid'] = $a_drives['SERIAL'];
1342                        } else {
1343                           $array_tmp['uuid'] = $a_drives['TYPE'];
1344                        }
1345                        $array_tmp['plugin_fusioninventory_inventorycomputerstoragetypes_id'] =
1346                           'mount';
1347                        if (!isset($array_tmp['uuid_link'])) {
1348                           $array_tmp['uuid_link'] = array();
1349                        }
1350                        $array_tmp['uuid_link'][] = $a_physicalvol['uuid'];
1351                        $detectsize += $a_physicalvol['totalsize'];
1352                     }
1353                  }
1354               }
1355               if (isset($array_tmp['name'])) {
1356                  $array_tmp['totalsize'] = $a_drives['TOTAL'];
1357                  $a_inventory['storage'][] = $array_tmp;
1358               }
1359
1360            } else if (isset($a_drives['VOLUMN'])
1361                    && strstr($a_drives['VOLUMN'], "/dev/")){
1362               $detectsize = 0;
1363               $array_tmp = array();
1364               foreach ($a_inventory['storage'] as $num=>$a_physicalvol) {
1365                  $volumn = $a_drives['VOLUMN'];
1366                  $volumn = substr_replace($volumn , "", -1);
1367                  $volumn = str_replace("/dev/", "", $volumn);
1368                  if ($volumn == $a_physicalvol['name']) {
1369                     $array_tmp['name'] = $a_drives['VOLUMN'];
1370                     if (isset($a_drives['SERIAL'])) {
1371                        $array_tmp['uuid'] = $a_drives['SERIAL'];
1372                     } else {
1373                        $array_tmp['uuid'] = $a_drives['TYPE'];
1374                     }
1375                     $array_tmp['plugin_fusioninventory_inventorycomputerstoragetypes_id'] =
1376                        'partition';
1377                     if (!isset($array_tmp['uuid_link'])) {
1378                        $array_tmp['uuid_link'] = array();
1379                     }
1380                     $array_tmp['uuid_link'][] = $a_physicalvol['uuid'];
1381                     $detectsize += $a_physicalvol['totalsize'];
1382                     if ($a_physicalvol['size_dynamic'] == 1) {
1383                        $a_inventory['storage'][$num]['totalsize'] += $a_drives['TOTAL'];
1384                     }
1385                  }
1386               }
1387               if (isset($array_tmp['name'])) {
1388                  $array_tmp['totalsize'] = $a_drives['TOTAL'];
1389                  $a_inventory['storage'][] = $array_tmp;
1390
1391                  $array_tmp['plugin_fusioninventory_inventorycomputerstoragetypes_id'] =
1392                           'mount';
1393                  $array_tmp['name'] = $a_drives['TYPE'];
1394                  $array_tmp['uuid_link'] = array();
1395                  $array_tmp['uuid_link'][] = $array_tmp['uuid'];
1396                  $array_tmp['uuid'] = $array_tmp['uuid']."-mount";
1397                  $a_inventory['storage'][] = $array_tmp;
1398               }
1399            }
1400         }
1401      }
1402*/
1403
1404      // * LICENSEINFOS
1405      $a_inventory['licenseinfo'] = array();
1406      if (isset($array['LICENSEINFOS'])) {
1407         foreach ($array['LICENSEINFOS'] as $a_licenseinfo) {
1408            $array_tmp = $thisc->addValues($a_licenseinfo,
1409                                           array(
1410                                              'NAME'     => 'name',
1411                                              'FULLNAME' => 'fullname',
1412                                              'KEY'      => 'serial'));
1413            $a_inventory['licenseinfo'][] = $array_tmp;
1414         }
1415      }
1416
1417      return $a_inventory;
1418   }
1419
1420
1421
1422   function computerSoftwareTransformation($a_inventory, $entities_id) {
1423
1424   /*
1425    * Sometimes we can have 2 same software, but one without manufacturer and
1426    * one with. So in this case, delete the software without manufacturer
1427    */
1428
1429      $softwareWithManufacturer = array();
1430      $softwareWithoutManufacturer = array();
1431
1432      $entities_id_software = Entity::getUsedConfig('entities_id_software',
1433                                                    $entities_id);
1434      $is_software_recursive = 0;
1435      $nb_RuleDictionnarySoftware = countElementsInTable("glpi_rules",
1436                                                         "`sub_type`='RuleDictionnarySoftware'
1437                                                            AND `is_active`='1'");
1438
1439      //Configuration says that software can be created in the computer's entity
1440      if ($entities_id_software < 0) {
1441         $entities_id_software = $entities_id;
1442      } else {
1443         //Software will be created in an entity which is not the computer's entity.
1444         //It should be set as recursive
1445         $is_software_recursive = 1;
1446      }
1447      $a_inventory['software'] = array();
1448
1449      $rulecollection = new RuleDictionnarySoftwareCollection();
1450
1451      foreach ($a_inventory['SOFTWARES'] as $a_softwares) {
1452         if (isset($a_softwares['PUBLISHER'])
1453                 && gettype($a_softwares['PUBLISHER']) == 'array')  {
1454            $a_softwares['PUBLISHER'] = current($a_softwares['PUBLISHER']);
1455         }
1456
1457         $array_tmp = $this->addValues($a_softwares,
1458                                        array(
1459                                           'PUBLISHER'   => 'manufacturers_id',
1460                                           'NAME'        => 'name',
1461                                           'VERSION'     => 'version'));
1462         if (!isset($array_tmp['name'])
1463                 || $array_tmp['name'] == '') {
1464            if (isset($a_softwares['GUID'])
1465                    && $a_softwares['GUID'] != '') {
1466               $array_tmp['name'] = $a_softwares['GUID'];
1467            }
1468         }
1469
1470         if (!(!isset($array_tmp['name'])
1471                 || $array_tmp['name'] == '')) {
1472            if (count($array_tmp) > 0) {
1473               $res_rule = array();
1474               if ($nb_RuleDictionnarySoftware > 0) {
1475                  $res_rule = $rulecollection->processAllRules(
1476                                               array(
1477                                                   "name"         => $array_tmp['name'],
1478                                                   "manufacturer" => $array_tmp['manufacturers_id'],
1479                                                   "old_version"  => $array_tmp['version'],
1480                                                   "entities_id"  => $entities_id_software
1481                                                )
1482                                             );
1483               }
1484
1485               if (isset($res_rule['_ignore_import'])
1486                       && $res_rule['_ignore_import'] == 1) {
1487
1488               } else {
1489                  if (isset($res_rule["name"])) {
1490                     $array_tmp['name'] = $res_rule["name"];
1491                  }
1492                  if (isset($res_rule["version"])) {
1493                     $array_tmp['version'] = $res_rule["version"];
1494                  }
1495                  if (isset($res_rule["manufacturer"])) {
1496                     $array_tmp['manufacturers_id'] = Dropdown::import("Manufacturer",
1497                                                   array('name' => $res_rule["manufacturer"]));
1498                  } else if (isset($array_tmp['manufacturers_id'])
1499                          && $array_tmp['manufacturers_id'] != ''
1500                          && $array_tmp['manufacturers_id'] != '0') {
1501
1502                     if (!isset($this->manufacturer_cache[$array_tmp['manufacturers_id']])) {
1503                        $new_value = Dropdown::importExternal('Manufacturer',
1504                                                              $array_tmp['manufacturers_id']);
1505                        $this->manufacturer_cache[$array_tmp['manufacturers_id']] = $new_value;
1506                     }
1507                     $array_tmp['manufacturers_id'] =
1508                                 $this->manufacturer_cache[$array_tmp['manufacturers_id']];
1509                  } else {
1510                     $array_tmp['manufacturers_id'] = 0;
1511                  }
1512                  if (isset($res_rule['new_entities_id'])) {
1513                     $array_tmp['entities_id'] = $res_rule['new_entities_id'];
1514                     $is_software_recursive = 1;
1515                  }
1516                  if (!isset($array_tmp['entities_id'])
1517                          || $array_tmp['entities_id'] == '') {
1518                     $array_tmp['entities_id'] = $entities_id_software;
1519                  }
1520                  if (!isset($array_tmp['version'])) {
1521                     $array_tmp['version'] = "";
1522                  }
1523                  $array_tmp['is_template_computer'] = 0;
1524                  $array_tmp['is_deleted_computer'] = 0;
1525                  $array_tmp['is_recursive']= $is_software_recursive;
1526                  $comp_key = strtolower($array_tmp['name']).
1527                               "$$$$".strtolower($array_tmp['version']).
1528                               "$$$$".$array_tmp['manufacturers_id'].
1529                               "$$$$".$array_tmp['entities_id'];
1530
1531                  $comp_key_simple = strtolower($array_tmp['name']).
1532                               "$$$$".strtolower($array_tmp['version']).
1533                               "$$$$".$array_tmp['entities_id'];
1534
1535                  if ($array_tmp['manufacturers_id'] == 0) {
1536                     $softwareWithoutManufacturer[$comp_key_simple] = $array_tmp;
1537                  } else {
1538                     if (!isset($a_inventory['software'][$comp_key])) {
1539                        $softwareWithManufacturer[$comp_key_simple] = 1;
1540                        $a_inventory['software'][$comp_key] = $array_tmp;
1541                     }
1542                  }
1543               }
1544            }
1545         }
1546      }
1547      foreach ($softwareWithoutManufacturer as $key=>$array_tmp) {
1548         if (!isset($softwareWithManufacturer[$key])) {
1549            $comp_key = strtolower($array_tmp['name']).
1550                         "$$$$".strtolower($array_tmp['version']).
1551                         "$$$$".$array_tmp['manufacturers_id'].
1552                         "$$$$".$array_tmp['entities_id'];
1553            if (!isset($a_inventory['software'][$comp_key])) {
1554               $a_inventory['software'][$comp_key] = $array_tmp;
1555            }
1556         }
1557      }
1558      unset($a_inventory['SOFTWARES']);
1559      return $a_inventory;
1560   }
1561
1562
1563
1564   function extraCollectInfo($a_inventory, $computers_id) {
1565      global $DB;
1566
1567      $pfCollectRuleCollection = new PluginFusioninventoryCollectRuleCollection();
1568
1569      // Get data from rules / collect registry, wmi, find files
1570      $data_collect = array();
1571
1572      $data_registries = getAllDatasFromTable('glpi_plugin_fusioninventory_collects_registries_contents',
1573                                         "`computers_id`='".$computers_id."'");
1574
1575      foreach ($data_registries as $data) {
1576         $res_rule = $pfCollectRuleCollection->processAllRules(
1577                       array(
1578                           "regkey"   => $data['key'],
1579                           "regvalue" => $data['value'],
1580                        )
1581                     );
1582         if (!isset($res_rule['_no_rule_matches'])) {
1583            $data_collect[] = $res_rule;
1584         }
1585      }
1586
1587      $data_wmis = getAllDatasFromTable('glpi_plugin_fusioninventory_collects_wmis_contents',
1588                                         "`computers_id`='".$computers_id."'");
1589
1590      foreach ($data_wmis as $data) {
1591         $res_rule = $pfCollectRuleCollection->processAllRules(
1592                       array(
1593                           "wmiproperty"  => $data['property'],
1594                           "wmivalue"     => $data['value'],
1595                        )
1596                     );
1597         if (!isset($res_rule['_no_rule_matches'])) {
1598            $data_collect[] = $res_rule;
1599         }
1600      }
1601
1602      $data_files = getAllDatasFromTable('glpi_plugin_fusioninventory_collects_files_contents',
1603                                         "`computers_id`='".$computers_id."'");
1604
1605      foreach ($data_files as $data) {
1606         $a_split = explode("/", $data['pathfile']);
1607         $filename = array_pop($a_split);
1608         $path = implode("/", $a_split);
1609
1610         $res_rule = $pfCollectRuleCollection->processAllRules(
1611                       array(
1612                           "filename"  => $filename,
1613                           "filepath"  => $path,
1614                           "size"      => $data['size']
1615                        )
1616                     );
1617         if (!isset($res_rule['_no_rule_matches'])) {
1618            $data_collect[] = $res_rule;
1619         }
1620      }
1621
1622      // * Update $a_inventory with $data_collect;
1623      foreach ($data_collect as $data) {
1624         // Update computer model
1625         if (isset($data['computermodels_id'])) {
1626            $a_inventory['Computer']['computermodels_id'] = $data['computermodels_id'];
1627         }
1628         // Update computer type
1629         if (isset($data['computertypes_id'])) {
1630            $a_inventory['Computer']['computertypes_id'] = $data['computertypes_id'];
1631         }
1632         // Update computer OS
1633         if (isset($data['operatingsystems_id'])) {
1634            $a_inventory['Computer']['operatingsystems_id'] = $data['operatingsystems_id'];
1635         }
1636         // Update computer OS version
1637         if (isset($data['operatingsystemversions_id'])) {
1638            $a_inventory['Computer']['operatingsystemversions_id'] = $data['operatingsystemversions_id'];
1639         }
1640         // Update computer user
1641         if (isset($data['user'])) {
1642            $query = "SELECT `id`
1643                      FROM `glpi_users`
1644                      WHERE `name` = '" . $data['user'] . "'
1645                      LIMIT 1";
1646            $result = $DB->query($query);
1647            if ($DB->numrows($result) == 1) {
1648               $a_inventory['Computer']['users_id'] = $DB->result($result, 0, 0);
1649            }
1650         }
1651         // Update computer location
1652         if (isset($data['locations_id'])) {
1653            $a_inventory['Computer']['locations_id'] = $data['locations_id'];
1654         }
1655         // Update computer status
1656         if (isset($data['states_id'])) {
1657            $a_inventory['Computer']['states_id'] = $data['states_id'];
1658         }
1659         // Add software
1660         if (isset($data['software'])
1661                 && isset($data['softwareversion'])) {
1662            $a_inventory['SOFTWARES'][] = array(
1663                'NAME'     => $data['software'],
1664                'VERSION'  => $data['softwareversion']
1665            );
1666         }
1667         // Update computer inventory number
1668         if (isset($data['otherserial'])) {
1669            $a_inventory['Computer']['otherserial'] = $data['otherserial'];
1670         }
1671      }
1672      return $a_inventory;
1673   }
1674
1675
1676
1677   static function addValues($array, $a_key) {
1678      $a_return = array();
1679      //if (!is_array($array)) {
1680      if ((array)$array !== $array) {
1681         return $a_return;
1682      }
1683      foreach ($array as $key=>$value) {
1684         if (isset($a_key[$key])) {
1685            $a_return[$a_key[$key]] = $value;
1686         }
1687      }
1688
1689      $a_int_values = array('capacity', 'freesize', 'totalsize', 'memory', 'memory_size',
1690         'pages_total', 'pages_n_b', 'pages_color', 'pages_recto_verso', 'scanned',
1691         'pages_total_print', 'pages_n_b_print', 'pages_color_print', 'pages_total_copy',
1692         'pages_n_b_copy', 'pages_color_copy', 'pages_total_fax',
1693         'cpu', 'trunk', 'is_active', 'uptodate', 'nbthreads', 'vcpu', 'ram',
1694         'ifinerrors', 'ifinoctets', 'ifouterrors', 'ifoutoctets', 'ifmtu', 'speed',
1695         'nbcores', 'nbthreads', 'frequency');
1696
1697      foreach ($a_key as $key=>$value) {
1698         if (!isset($a_return[$value])
1699                 || $a_return[$value] == '') {
1700
1701            if (in_array($value, $a_int_values)) {
1702               $a_return[$value] = 0;
1703            } else {
1704               $a_return[$value] = '';
1705            }
1706         }
1707      }
1708      return $a_return;
1709   }
1710
1711
1712
1713   function replaceids($array, $itemtype, $items_id, $level=0) {
1714      global $CFG_GLPI;
1715
1716      $a_lockable = PluginFusioninventoryLock::getLockFields(getTableForItemType($itemtype),
1717                                                             $items_id);
1718
1719      foreach ($array as $key=>$value) {
1720         if (!is_int($key)
1721                 && ($key == "software"
1722                     || $key == 'ipaddress'
1723                     || $key == 'internalport')) {
1724            // do nothing
1725         } else {
1726            //if (is_array($value)) {
1727            if ((array)$value === $value) {
1728               $new_itemtype = $itemtype;
1729               if ($level == 0) {
1730                  $new_itemtype = $key;
1731               }
1732               $array[$key] = $this->replaceids($value, $new_itemtype, $items_id, $level + 1);
1733            } else {
1734               if (!PluginFusioninventoryLock::isFieldLocked($a_lockable, $key)) {
1735                  if (!is_numeric($key)
1736                          && ($key == "manufacturers_id"
1737                              || $key == 'bios_manufacturers_id')) {
1738                     $manufacturer = new Manufacturer();
1739                     $array[$key]  = $manufacturer->processName($value);
1740                     if ($key == 'bios_manufacturers_id') {
1741                        $this->foreignkey_itemtype[$key] =
1742                                 getItemTypeForTable(getTableNameForForeignKeyField('manufacturers_id'));
1743                     } else {
1744                        if (isset($CFG_GLPI['plugin_fusioninventory_computermanufacturer'][$value])) {
1745                           $CFG_GLPI['plugin_fusioninventory_computermanufacturer'][$value] = $array[$key];
1746                        }
1747                     }
1748                  }
1749                  if (!is_numeric($key)) {
1750                     if ($key == "locations_id") {
1751                        $array[$key] = Dropdown::importExternal('Location',
1752                                                                $value,
1753                                                                $_SESSION["plugin_fusioninventory_entity"]);
1754                     } else if (isset($this->foreignkey_itemtype[$key])) {
1755                        $array[$key] = Dropdown::importExternal($this->foreignkey_itemtype[$key],
1756                                                                $value,
1757                                                                $_SESSION["plugin_fusioninventory_entity"]);
1758                     } else if (isForeignKeyField($key)
1759                             && $key != "users_id") {
1760                        $this->foreignkey_itemtype[$key] =
1761                                    getItemTypeForTable(getTableNameForForeignKeyField($key));
1762                        if ($key == 'computermodels_id') {
1763                           if (isset($CFG_GLPI['plugin_fusioninventory_computermanufacturer'])) {
1764                              $manufacturer = current($CFG_GLPI['plugin_fusioninventory_computermanufacturer']);
1765                              $array[$key] = Dropdown::importExternal($this->foreignkey_itemtype[$key],
1766                                                                      $value,
1767                                                                      $_SESSION["plugin_fusioninventory_entity"],
1768                                                                      array('manufacturer' => $manufacturer));
1769                           } else {
1770                              $array[$key] = 0;
1771                           }
1772                        } else {
1773                           $table = getTableForItemType($this->foreignkey_itemtype[$key]);
1774                           $array[$key] = Dropdown::importExternal($this->foreignkey_itemtype[$key],
1775                                                                   $value,
1776                                                                   $_SESSION["plugin_fusioninventory_entity"]);
1777                        }
1778                     }
1779                  }
1780               } else {
1781                  unset($array[$key]);
1782               }
1783            }
1784         }
1785      }
1786      return $array;
1787   }
1788
1789
1790   /*
1791    * Modify switch inventory
1792    */
1793   static function networkequipmentInventoryTransformation($array) {
1794
1795      $a_inventory = array();
1796      $thisc = new self();
1797
1798      // * INFO
1799      $array_tmp = $thisc->addValues($array['INFO'],
1800                                     array(
1801                                        'NAME'         => 'name',
1802                                        'SERIAL'       => 'serial',
1803                                        'ID'           => 'id',
1804                                        'LOCATION'     => 'locations_id',
1805                                        'MODEL'        => 'networkequipmentmodels_id',
1806                                        'MANUFACTURER' => 'manufacturers_id',
1807                                        'FIRMWARE'     => 'networkequipmentfirmwares_id',
1808                                        'RAM'          => 'ram',
1809                                        'MEMORY'       => 'memory',
1810                                        'MAC'          => 'mac'));
1811
1812      if (strstr($array_tmp['networkequipmentfirmwares_id'], "CW_VERSION")
1813              OR strstr($array_tmp['networkequipmentfirmwares_id'], "CW_INTERIM_VERSION")) {
1814         $explode = explode("$", $array_tmp['networkequipmentfirmwares_id']);
1815         if (isset($explode[1])) {
1816            $array_tmp['networkequipmentfirmwares_id'] = $explode[1];
1817         }
1818      }
1819      $array_tmp['is_dynamic'] = 1;
1820      $a_inventory['NetworkEquipment'] = $array_tmp;
1821      $a_inventory['itemtype'] = 'NetworkEquipment';
1822
1823
1824      $array_tmp = $thisc->addValues($array['INFO'],
1825                                     array(
1826                                        'COMMENTS' => 'sysdescr',
1827                                        'UPTIME'   => 'uptime',
1828                                        'CPU'      => 'cpu',
1829                                        'MEMORY'   => 'memory'));
1830      if (!isset($array_tmp['cpu'])
1831              || $array_tmp['cpu'] == '') {
1832         $array_tmp['cpu'] = 0;
1833      }
1834
1835      $array_tmp['last_fusioninventory_update'] = date('Y-m-d H:i:s');
1836      $a_inventory['PluginFusioninventoryNetworkEquipment'] = $array_tmp;
1837
1838      // * Internal ports
1839      $a_inventory['internalport'] = array();
1840      if (isset($array['INFO']['IPS'])) {
1841         foreach ($array['INFO']['IPS']['IP'] as $IP) {
1842            $a_inventory['internalport'][] = $IP;
1843         }
1844      }
1845      $a_inventory['internalport'] = array_unique($a_inventory['internalport']);
1846
1847      // * PORTS
1848      $a_inventory['networkport'] = array();
1849      if (isset($array['PORTS'])) {
1850         foreach ($array['PORTS']['PORT'] as $a_port) {
1851            $array_tmp = $thisc->addValues($a_port,
1852                                           array(
1853                                              'IFNAME'            => 'name',
1854                                              'IFNUMBER'          => 'logical_number',
1855                                              'MAC'               => 'mac',
1856                                              'IFSPEED'           => 'speed',
1857                                              'IFDESCR'           => 'ifdescr',
1858                                              'IFALIAS'           => 'ifalias',
1859                                              'IFINERRORS'        => 'ifinerrors',
1860                                              'IFINOCTETS'        => 'ifinoctets',
1861                                              'IFINTERNALSTATUS'  => 'ifinternalstatus',
1862                                              'IFLASTCHANGE'      => 'iflastchange',
1863                                              'IFMTU'             => 'ifmtu',
1864                                              'IFOUTERRORS'       => 'ifouterrors',
1865                                              'IFOUTOCTETS'       => 'ifoutoctets',
1866                                              'IFSTATUS'          => 'ifstatus',
1867                                              'IFTYPE'            => 'iftype',
1868                                              'TRUNK'             => 'trunk',
1869                                              'IFPORTDUPLEX'      => 'portduplex'));
1870            $array_tmp['ifspeed'] = $array_tmp['speed'];
1871            if ($array_tmp['ifdescr'] == '') {
1872               $array_tmp['ifdescr'] = $array_tmp['name'];
1873            }
1874            $array_tmp['ifspeed'] = $array_tmp['speed'];
1875
1876            if (!isset($a_port['IFNUMBER'])) {
1877               continue;
1878            }
1879            $a_inventory['networkport'][$a_port['IFNUMBER']] = $array_tmp;
1880
1881            if (isset($a_port['CONNECTIONS'])) {
1882               if (isset($a_port['CONNECTIONS']['CDP'])
1883                       && $a_port['CONNECTIONS']['CDP'] == 1) {
1884
1885                  $array_tmp = $thisc->addValues($a_port['CONNECTIONS']['CONNECTION'],
1886                                                 array(
1887                                                    'IFDESCR'  => 'ifdescr',
1888                                                    'IFNUMBER' => 'logical_number',
1889                                                    'SYSDESCR' => 'sysdescr',
1890                                                    'MODEL'    => 'model',
1891                                                    'IP'       => 'ip',
1892                                                    'SYSMAC'   => 'mac',
1893                                                    'SYSNAME'  => 'name'));
1894                  $a_inventory['connection-lldp'][$a_port['IFNUMBER']] = $array_tmp;
1895               } else {
1896                  // MAC
1897                  if (isset($a_port['CONNECTIONS']['CONNECTION'])) {
1898                     if (!is_array($a_port['CONNECTIONS']['CONNECTION'])) {
1899                        $a_port['CONNECTIONS']['CONNECTION'] =
1900                                       array($a_port['CONNECTIONS']['CONNECTION']);
1901                     } else if (!is_int(key($a_port['CONNECTIONS']['CONNECTION']))) {
1902                        $a_port['CONNECTIONS']['CONNECTION'] =
1903                                       array($a_port['CONNECTIONS']['CONNECTION']);
1904                     }
1905                     foreach ($a_port['CONNECTIONS']['CONNECTION'] as $dataconn) {
1906                        foreach ($dataconn as $keymac=>$mac) {
1907                           if ($keymac == 'MAC') {
1908                              $a_inventory['connection-mac'][$a_port['IFNUMBER']] = $mac;
1909                           }
1910                        }
1911                     }
1912                     $a_inventory['connection-mac'][$a_port['IFNUMBER']] =
1913                                    array_unique($a_inventory['connection-mac'][$a_port['IFNUMBER']]);
1914                  }
1915               }
1916            }
1917
1918            // VLAN
1919            if (isset($a_port['VLANS'])) {
1920               if (!is_int(key($a_port['VLANS']['VLAN']))) {
1921                  $a_port['VLANS']['VLAN'] = array($a_port['VLANS']['VLAN']);
1922               }
1923
1924               foreach ($a_port['VLANS']['VLAN'] as $a_vlan) {
1925                  $array_tmp = $thisc->addValues($a_vlan,
1926                                                 array(
1927                                                    'NAME'   => 'name',
1928                                                    'NUMBER' => 'tag'));
1929                  if (isset($array_tmp['tag'])) {
1930                     $a_inventory['vlans'][$a_port['IFNUMBER']][$array_tmp['tag']] = $array_tmp;
1931                  }
1932               }
1933            }
1934            // AGGREGATE PORT
1935            if (isset($a_port['AGGREGATE'])) {
1936               if (!is_int(key($a_port['AGGREGATE']['PORT']))) {
1937                  $a_port['AGGREGATE']['PORT'] = array($a_port['AGGREGATE']['PORT']);
1938               }
1939               $a_inventory['aggregate'][$a_port['IFNUMBER']] = $a_port['AGGREGATE']['PORT'];
1940            }
1941         }
1942      }
1943      return $a_inventory;
1944   }
1945
1946
1947
1948   /*
1949    * Modify switch inventory
1950    */
1951   static function printerInventoryTransformation($array) {
1952
1953      $a_inventory = array();
1954      $thisc = new self();
1955
1956      // * INFO
1957      $array_tmp = $thisc->addValues($array['INFO'],
1958                                     array(
1959                                        'NAME'         => 'name',
1960                                        'SERIAL'       => 'serial',
1961                                        'ID'           => 'id',
1962                                        'MANUFACTURER' => 'manufacturers_id',
1963                                        'LOCATION'     => 'locations_id',
1964                                        'MODEL'        => 'printermodels_id',
1965                                        'MEMORY'       => 'memory_size'));
1966      $array_tmp['is_dynamic'] = 1;
1967      $array_tmp['have_ethernet'] = 1;
1968
1969      $a_inventory['Printer'] = $array_tmp;
1970      $a_inventory['itemtype'] = 'Printer';
1971
1972      $array_tmp = $thisc->addValues($array['INFO'],
1973                                     array(
1974                                        'COMMENTS' => 'sysdescr'));
1975      $array_tmp['last_fusioninventory_update'] = date('Y-m-d H:i:s');
1976      $a_inventory['PluginFusioninventoryPrinter'] = $array_tmp;
1977
1978      // * PORTS
1979      $a_inventory['networkport'] = array();
1980      if (isset($array['PORTS'])) {
1981         foreach ($array['PORTS']['PORT'] as $a_port) {
1982            if (!isset($a_port['IFNUMBER'])) {
1983               $array_tmp = $thisc->addValues($a_port,
1984                                              array(
1985                                                 'IFNAME'   => 'name',
1986                                                 'IFNUMBER' => 'logical_number',
1987                                                 'MAC'      => 'mac',
1988                                                 'IP'       => 'ip',
1989                                                 'IFTYPE'   => 'iftype'));
1990
1991               $a_inventory['networkport'][$a_port['IFNUMBER']] = $array_tmp;
1992            }
1993         }
1994      }
1995
1996      // CARTRIDGES
1997      $a_inventory['cartridge'] = array();
1998      if (isset($array['CARTRIDGES'])) {
1999         $pfMapping = new PluginFusioninventoryMapping();
2000
2001         foreach ($array['CARTRIDGES'] as $name=>$value) {
2002            $plugin_fusioninventory_mappings = $pfMapping->get("Printer", strtolower($name));
2003            if ($plugin_fusioninventory_mappings) {
2004               if (strstr($value, 'pages')) { // 30pages
2005                  $value = str_replace('pages', '', $value);
2006                  $value = 0 - $value;
2007               } else if ($value == '') { // no info
2008                  // nothing to do
2009               } else if (is_numeric($value)) { // percentage
2010                  // nothing to do
2011               } else if ($value == 'OK') { // state type 'OK'
2012                  $value = 100000;
2013               } else {
2014                  // special cases
2015                  $value = '';
2016               }
2017               if ($value != '') {
2018                  $a_inventory['cartridge'][$plugin_fusioninventory_mappings['id']] = $value;
2019               }
2020            }
2021         }
2022      }
2023
2024      // * PAGESCOUNTER
2025      $a_inventory['pagecounters'] = array();
2026      if (isset($array['PAGECOUNTERS'])) {
2027         $array_tmp = $thisc->addValues($array['PAGECOUNTERS'],
2028                                        array(
2029                                           'TOTAL'       => 'pages_total',
2030                                           'BLACK'       => 'pages_n_b',
2031                                           'COLOR'       => 'pages_color',
2032                                           'RECTOVERSO'  => 'pages_recto_verso',
2033                                           'SCANNED'     => 'scanned',
2034                                           'PRINTTOTAL'  => 'pages_total_print',
2035                                           'PRINTBLACK'  => 'pages_n_b_print',
2036                                           'PRINTCOLOR'  => 'pages_color_print',
2037                                           'COPYTOTAL'   => 'pages_total_copy',
2038                                           'COPYBLACK'   => 'pages_n_b_copy',
2039                                           'COPYCOLOR'   => 'pages_color_copy',
2040                                           'FAXTOTAL'    => 'pages_total_fax'
2041                                         ));
2042         $a_inventory['pagecounters'] = $array_tmp;
2043      }
2044
2045      return $a_inventory;
2046   }
2047
2048
2049
2050   /**
2051   * Get type of the drive
2052   *
2053   * @param $data array of the storage
2054   *
2055   * @return "Drive" or "HardDrive"
2056   *
2057   **/
2058   static function getTypeDrive($data) {
2059      if (((isset($data['TYPE'])) AND
2060              ((preg_match("/rom/i", $data["TYPE"])) OR (preg_match("/dvd/i", $data["TYPE"]))
2061               OR (preg_match("/blue.{0, 1}ray/i", $data["TYPE"]))))
2062            OR
2063         ((isset($data['MODEL'])) AND
2064              ((preg_match("/rom/i", $data["MODEL"])) OR (preg_match("/dvd/i", $data["MODEL"]))
2065               OR (preg_match("/blue.{0, 1}ray/i", $data["MODEL"]))))
2066            OR
2067         ((isset($data['NAME'])) AND
2068              ((preg_match("/rom/i", $data["NAME"])) OR (preg_match("/dvd/i", $data["NAME"]))
2069               OR (preg_match("/blue.{0, 1}ray/i", $data["NAME"]))))) {
2070
2071         return "Drive";
2072      } else {
2073         return "HardDrive";
2074      }
2075   }
2076}
2077
2078?>
2079