Projet

Général

Profil

Paste
Statistiques
| Branche: | Révision:

ryxeo-glpi-git / inc / ocsng.function.php @ b67d8923

Historique | Voir | Annoter | Télécharger (114 ko)

1
<?php
2

    
3

    
4
/*
5
 * @version $Id: ocsng.function.php 7881 2009-01-23 17:56:15Z moyo $
6
 -------------------------------------------------------------------------
7
 GLPI - Gestionnaire Libre de Parc Informatique
8
 Copyright (C) 2003-2009 by the INDEPNET Development Team.
9

10
 http://indepnet.net/   http://glpi-project.org
11
 -------------------------------------------------------------------------
12

13
 LICENSE
14

15
 This file is part of GLPI.
16

17
 GLPI is free software; you can redistribute it and/or modify
18
 it under the terms of the GNU General Public License as published by
19
 the Free Software Foundation; either version 2 of the License, or
20
 (at your option) any later version.
21

22
 GLPI is distributed in the hope that it will be useful,
23
 but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
 GNU General Public License for more details.
26

27
 You should have received a copy of the GNU General Public License
28
 along with GLPI; if not, write to the Free Software
29
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
30
 --------------------------------------------------------------------------
31
 */
32

    
33
// ----------------------------------------------------------------------
34
// Original Author of file:
35
// Purpose of file:
36
// ----------------------------------------------------------------------
37

    
38
if (!defined('GLPI_ROOT')) {
39
        die("Sorry. You can't access directly to this file");
40
}
41

    
42
function ocsShowNewComputer($ocs_server_id, $advanced, $check, $start, $tolinked = false) {
43
        global $DB, $DBocs, $LANG, $CFG_GLPI;
44

    
45
        if (!haveRight("ocsng", "w"))
46
                return false;
47

    
48
        $cfg_ocs = getOcsConf($ocs_server_id);
49

    
50
        $WHERE = "";
51
        if (!empty ($cfg_ocs["tag_limit"])) {
52
                $splitter = explode("$", $cfg_ocs["tag_limit"]);
53
                if (count($splitter)) {
54
                        $WHERE = "WHERE TAG='" . $splitter[0] . "' ";
55
                        for ($i = 1; $i < count($splitter); $i++){
56
                                $WHERE .= " OR TAG='" .$splitter[$i] . "' ";
57
                        }
58
                }
59
        }
60

    
61
        $query_ocs = "SELECT hardware.*, accountinfo.TAG AS TAG
62
                                FROM hardware
63
                                INNER JOIN accountinfo ON (hardware.ID = accountinfo.HARDWARE_ID)
64
                                $WHERE
65
                                ORDER BY hardware.NAME";
66
        $result_ocs = $DBocs->query($query_ocs);
67

    
68
        // Existing OCS - GLPI link
69
        $query_glpi = "SELECT * 
70
                        FROM glpi_ocs_link WHERE ocs_server_id='" . $ocs_server_id."'";
71
        $result_glpi = $DB->query($query_glpi);
72

    
73
        if ($tolinked){
74
                // Computers existing in GLPI
75
                $query_glpi_comp = "SELECT ID,name 
76
                                                                FROM glpi_computers 
77
                                                                WHERE is_template='0'";
78
                $result_glpi_comp = $DB->query($query_glpi_comp);
79
        }
80

    
81
        if ($DBocs->numrows($result_ocs) > 0) {
82

    
83
                // Get all hardware from OCS DB
84
                $hardware = array ();
85
                while ($data = $DBocs->fetch_array($result_ocs)) {
86
                        $data = clean_cross_side_scripting_deep(addslashes_deep($data));
87
                        $hardware[$data["ID"]]["date"] = $data["LASTDATE"];
88
                        $hardware[$data["ID"]]["name"] = $data["NAME"];
89
                        $hardware[$data["ID"]]["TAG"] = $data["TAG"];
90
                        $hardware[$data["ID"]]["ID"] = $data["ID"];
91
                }
92

    
93
                // Get all links between glpi and OCS
94
                $already_linked = array ();
95
                if ($DB->numrows($result_glpi) > 0) {
96
                        while ($data = $DBocs->fetch_array($result_glpi)) {
97
                                $already_linked[$data["ocs_id"]] = $data["last_update"];
98
                        }
99
                }
100

    
101
                // Get all existing computers name in GLPI
102
                if ($tolinked){
103
                        $computer_names = array ();
104
                        if ($DB->numrows($result_glpi_comp) > 0) {
105
                                while ($data = $DBocs->fetch_array($result_glpi_comp)) {
106
                                        $computer_names[strtolower($data["name"])] = $data["ID"];
107
                                }
108
                        }
109
                }
110

    
111
                // Clean $hardware from already linked element
112
                if (count($already_linked) > 0) {
113
                        foreach ($already_linked as $ID => $date) {
114
                                if (isset ($hardware[$ID]) && isset ($already_linked[$ID]))
115
                                        unset ($hardware[$ID]);
116
                        }
117
                }
118

    
119
                if ($tolinked && count($hardware)) {
120
                        echo "<div class='center'><strong>" . $LANG["ocsng"][22] . "</strong></div>";
121
                }
122
                
123
                echo "<div class='center'>";
124
                if (($numrows = count($hardware)) > 0) {
125

    
126
                        $parameters = "check=$check";
127
                        printPager($start, $numrows, $_SERVER['PHP_SELF'], $parameters);
128

    
129
                        // delete end 
130
                        array_splice($hardware, $start + $_SESSION["glpilist_limit"]);
131
                        // delete begin
132
                        if ($start > 0)
133
                                array_splice($hardware, 0, $start);
134

    
135
                        if (!$tolinked) {
136
                                echo "<form method='post' name='ocsng_import_mode' id='ocsng_import_mode' action='" . $_SERVER['PHP_SELF'] . "'>";
137

    
138
                                echo "<table class='tab_cadre'>";
139
                                echo "<tr><th>" . $LANG["ocsng"][41] . "</th></tr>";
140
                                echo "<tr class='tab_bg_1'>";
141
                                echo "<td class='center'>";
142

    
143
                                if ($advanced)
144
                                        $status = "false";
145
                                else
146
                                        $status = "true";
147

    
148
                                echo "<a href='" . $_SERVER['PHP_SELF'] . "?change_import_mode=" . $status . "'>";
149
                                if ($advanced)
150
                                        echo $LANG["ocsng"][38];
151
                                else
152
                                        echo $LANG["ocsng"][37];
153
                                echo "</a></td>";
154
                                echo "</tr></table></form><br>";
155
                        }
156

    
157
                        echo "<strong>" . $LANG["ocsconfig"][18] . "</strong><br>";
158
                        echo "<form method='post' name='ocsng_form' id='ocsng_form' action='" . $_SERVER['PHP_SELF'] . "'>";
159
                        if (!$tolinked){
160
                                echo "<a href='" . $_SERVER['PHP_SELF'] . "?check=all&amp;start=$start' onclick= \"if ( markAllRows('ocsng_form') ) return false;\">" . $LANG["buttons"][18] . "</a>&nbsp;/&nbsp;<a href='" . $_SERVER['PHP_SELF'] . "?check=none&amp;start=$start' onclick= \"if ( unMarkAllRows('ocsng_form') ) return false;\">" . $LANG["buttons"][19] . "</a>";
161
                        }
162

    
163
                        echo "<table class='tab_cadre'>";
164

    
165
                        echo "<tr><th>" . $LANG["ocsng"][5] . "</th><th>" . $LANG["common"][27] . "</th><th>TAG</th>";
166
                        if ($advanced && !$tolinked) {
167
                                echo "<th>" . $LANG["ocsng"][40] . "</th>";
168
                                echo "<th>" . $LANG["ocsng"][36] . "</th>";
169
                        }
170
                        echo "<th>&nbsp;</th></tr>";
171

    
172
                        echo "<tr class='tab_bg_1'><td colspan='" . ($advanced ? 6 : 4) . "' align='center'>";
173
                        echo "<input class='submit' type='submit' name='import_ok' value='" . $LANG["buttons"][37] . "'>";
174
                        echo "</td></tr>";
175

    
176
                        $rule = new OcsRuleCollection($ocs_server_id);
177

    
178
                        foreach ($hardware as $ID => $tab) {
179
                                $comp = new Computer;
180
                                $comp->fields["ID"] = $tab["ID"];
181

    
182
                                $data = array ();
183
                                if ($advanced && !$tolinked)
184
                                        $data = $rule->processAllRules(array (), array (), $tab["ID"]);
185

    
186
                                echo "<tr class='tab_bg_2'><td>" . $tab["name"] . "</td><td>" . convDateTime($tab["date"]) . "</td><td>" . $tab["TAG"] . "</td>";
187

    
188
                                if ($advanced && !$tolinked) {
189
                                        if (!isset ($data['FK_entities'])) {
190
                                                echo "<td class='center'><img src=\"" . GLPI_ROOT . "/pics/redbutton.png\"></td>";
191
                                                $data['FK_entities'] = -1;
192
                                        } else
193
                                                echo "<td class='center'><img src=\"" . GLPI_ROOT . "/pics/greenbutton.png\"></td>";
194

    
195
                                        echo "<td>";
196
                                        dropdownValue("glpi_entities", "toimport_entities[" . $tab["ID"] . "]=" . $data['FK_entities'], $data['FK_entities'], 0);
197
                                        echo "</td>";
198
                                }
199

    
200
                                echo "<td>";
201
                                if (!$tolinked){
202
                                        echo "<input type='checkbox' name='toimport[" . $tab["ID"] . "]' " . ($check == "all" ? "checked" : "") . ">";
203
                                } else {
204
                                        if (isset ($computer_names[strtolower($tab["name"])])){
205
                                                dropdownValue("glpi_computers", "tolink[" .
206
                                                $tab["ID"] . "]", $computer_names[strtolower($tab["name"])]);
207
                                        } else {
208
                                                dropdown("glpi_computers", "tolink[" .
209
                                                $tab["ID"] . "]");
210
                                        }
211
                                }
212
                                echo "</td>";
213

    
214
                                echo "</tr>";
215
                        }
216
                        echo "<tr class='tab_bg_1'><td colspan='" . ($advanced ? 6 : 4) . "' align='center'>";
217
                        echo "<input class='submit' type='submit' name='import_ok' value='" . $LANG["buttons"][37] . "'>";
218
                        echo "<input type=hidden name='ocs_server_id' value='" . $ocs_server_id . "'>";
219
                        echo "</td></tr>";
220
                        echo "</table>";
221
                        echo "</form>";
222

    
223
                        printPager($start, $numrows, $_SERVER['PHP_SELF'], $parameters);
224

    
225
                } else
226
                        echo "<strong>" . $LANG["ocsng"][9] . "</strong>";
227

    
228
                echo "</div>";
229

    
230
        } else
231
                echo "<div class='center'><strong>" . $LANG["ocsng"][9] . "</strong></div>";
232
}
233

    
234
/**
235
 * Make the item link between glpi and ocs.
236
 *
237
 * This make the database link between ocs and glpi databases
238
 *
239
 *@param $ocs_id integer : ocs item unique id.
240
 *@param $glpi_computer_id integer : glpi computer id
241
 *@param $ocs_server_id integer : ocs server id
242
 *
243
 *@return integer : link id.
244
 *
245
 **/
246
function ocsLink($ocs_id, $ocs_server_id, $glpi_computer_id) {
247
        global $DB, $DBocs;
248

    
249
        checkOCSconnection($ocs_server_id);
250

    
251
        // Need to get device id due to ocs bug on duplicates
252
        $query_ocs = "SELECT * 
253
                                FROM hardware
254
                                WHERE ID = '$ocs_id';";
255
        $result_ocs = $DBocs->query($query_ocs);
256
        $data = $DBocs->fetch_array($result_ocs);
257

    
258
        $query = "INSERT INTO glpi_ocs_link 
259
                                (glpi_id,ocs_id,ocs_deviceid,last_update,ocs_server_id) 
260
                                VALUES ('" . $glpi_computer_id . "','" . $ocs_id . "','" . $data["DEVICEID"] . "','" . $_SESSION["glpi_currenttime"] . "','" . $ocs_server_id . "')";
261
        $result = $DB->query($query);
262

    
263
        if ($result) {
264
                return ($DB->insert_id());
265
        } else {
266
                // TODO : Check if this code part is ok ? why to send a link if insert do not works ? May have problem for example on ocsLinkComputer 
267
                $query = "SELECT ID
268
                                        FROM glpi_ocs_link
269
                                        WHERE ocs_id = '$ocs_id' AND ocs_server_id='" . $ocs_server_id . "';";
270
                $result = $DB->query($query);
271
                $data = $DB->fetch_array($result);
272
                if ($data['ID']) {
273
                        return $data['ID'];
274
                } else {
275
                        return false;
276
                }
277
        }
278
}
279

    
280
function ocsCheckConfig($what=1) {
281
        global $DBocs;
282

    
283
        # Check OCS version
284
        if ($what & 1) {
285
                $result = $DBocs->query("SELECT TVALUE FROM config WHERE NAME='GUI_VERSION'");
286
                if ($DBocs->numrows($result) != 1 || $DBocs->result($result, 0, 0) < 4020) {
287
                        return false;
288
                }
289
        }
290
        # Check TRACE_DELETED in CONFIG
291
        if ($what & 2) {
292
                $result = $DBocs->query("SELECT IVALUE FROM config WHERE NAME='TRACE_DELETED'");
293
                if ($DBocs->numrows($result) != 1 || $DBocs->result($result, 0, 0) != 1) {
294
                        $query = "UPDATE config SET IVALUE='1' WHERE NAME='TRACE_DELETED'";
295

    
296
                        if (!$DBocs->query($query)) return false;                        
297
                }
298
        }
299
        # Check write access on hardware.CHECKSUM
300
        if ($what & 4) {
301
                if (!$DBocs->query("UPDATE hardware SET CHECKSUM = CHECKSUM LIMIT 1")) {
302
                        return false;                        
303
                }
304
        }
305
        # Check delete access on deleted_equiv
306
        if ($what & 8) {
307
                if (!$DBocs->query("DELETE FROM deleted_equiv LIMIT 0")) {
308
                        return false;                        
309
                }
310
        }
311

    
312
        return true;
313
}
314

    
315
function ocsManageDeleted($ocs_server_id) {
316
        global $DB, $DBocs;
317

    
318
        if (!(checkOCSconnection($ocs_server_id) && ocsCheckConfig(1))) {
319
                return false;
320
        }
321

    
322
        $query = "SELECT * FROM deleted_equiv ORDER BY DATE";
323
        $result = $DBocs->query($query);
324
        if ($DBocs->numrows($result)) {
325
                $deleted = array ();
326
                while ($data = $DBocs->fetch_array($result)) {
327
                        $deleted[$data["DELETED"]] = $data["EQUIVALENT"];
328
                }
329

    
330
                if (count($deleted)){
331
                        foreach ($deleted as $del => $equiv) {
332
                                if (!empty ($equiv)&&!is_null($equiv)) { // New name
333

    
334

    
335
                                        // Get hardware due to bug of duplicates management of OCS
336
                                        if (ereg("-", $equiv)) {
337
                                                $query_ocs = "SELECT * 
338
                                                                FROM hardware 
339
                                                                WHERE DEVICEID='$equiv'";
340
                                                $result_ocs = $DBocs->query($query_ocs);
341
                                                
342
                                                if ($data = $DBocs->fetch_array($result_ocs)) {
343

    
344
                                                        $query = "UPDATE glpi_ocs_link 
345
                                                                        SET ocs_id='" . $data["ID"] . "', ocs_deviceid='" . $data["DEVICEID"] . "' 
346
                                                                        WHERE ocs_deviceid='$del' AND ocs_server_id='$ocs_server_id'";
347
                                                        $DB->query($query);
348

    
349
                                                        //Update hardware checksum due to a bug in OCS 
350
                                                         //(when changing netbios name, software checksum is set instead of hardware checksum...)
351
                                                        $querychecksum = "UPDATE hardware 
352
                                                                        SET CHECKSUM = (CHECKSUM | ".pow(2, HARDWARE_FL).") 
353
                                                                        WHERE ID='".$data["ID"]."'";
354
                                                        $DBocs->query($querychecksum);
355

    
356
                                                }
357

    
358
                                        } else {
359
                                                $query_ocs = "SELECT * 
360
                                                                FROM hardware 
361
                                                                WHERE ID='$equiv'";
362

    
363
                                                $result_ocs = $DBocs->query($query_ocs);
364
                                                if ($data = $DBocs->fetch_array($result_ocs)){
365
                                                        
366
                                                        $query = "UPDATE glpi_ocs_link 
367
                                                                SET ocs_id='" . $data["ID"] . "', ocs_deviceid='" . $data["DEVICEID"] . "' 
368
                                                                WHERE ocs_id='$del' AND ocs_server_id='$ocs_server_id'";
369
                                                        $DB->query($query);
370

    
371
                                                        //Update hardware checksum due to a bug in OCS 
372
                                                         //(when changing netbios name, software checksum is set instead of hardware checksum...)
373
                                                        $querychecksum = "UPDATE hardware 
374
                                                                        SET CHECKSUM = (CHECKSUM | ".pow(2, HARDWARE_FL).") 
375
                                                                        WHERE ID='".$data["ID"]."'";
376
                                                        $DBocs->query($querychecksum);
377
                                                }
378

    
379
                                        }
380

    
381
                                        if ($data) {
382
                                                $sql_id = "SELECT glpi_id FROM glpi_ocs_link WHERE ocs_id='".$data["ID"]."' AND ocs_server_id='$ocs_server_id'";
383
                                                if ($res_id = $DB->query($sql_id)){
384
                                                        if ($DB->numrows($res_id)>0){
385
                                                                //Add history to indicates that the ocs_id changed
386
                                                                $changes[0]='0';
387
                                                                //Old ocs_id
388
                                                                $changes[1]=$del;
389
                                                                //New ocs_id
390
                                                                $changes[2]=$data["ID"];
391
                                                                historyLog ($DB->result($res_id,0,"glpi_id"),COMPUTER_TYPE,$changes,0,HISTORY_OCS_IDCHANGED);
392
                                                        }
393
                                                }
394
                                        }                                        
395
                                } else { // Deleted
396
                                        if (ereg("-", $del))
397
                                                $query = "SELECT * 
398
                                                                                                                FROM glpi_ocs_link 
399
                                                                                                                WHERE ocs_deviceid='$del' AND ocs_server_id='$ocs_server_id'";
400
                                        else
401
                                                $query = "SELECT * 
402
                                                                                                                FROM glpi_ocs_link 
403
                                                                                                                WHERE ocs_id='$del' AND ocs_server_id='$ocs_server_id'";
404
                                        if ($result = $DB->query($query)){
405
                                                if ($DB->numrows($result)>0) {
406
                                                        $data = $DB->fetch_array($result);
407
                                                        $comp = new Computer();
408
                                                        $comp->delete(array (
409
                                                                "ID" => $data["glpi_id"],
410
                                                                "_from_ocs" => 1
411
                                                        ), 0);
412
        
413
                                                        //Add history to indicates that the machine was deleted from OCS
414
                                                        $changes[0]='0';
415
                                                        $changes[1]=$data["ocs_id"];
416
                                                        $changes[2]="";
417
                                                        historyLog ($data["glpi_id"],COMPUTER_TYPE,$changes,0,HISTORY_OCS_DELETE);
418
        
419
                                                        $query = "DELETE FROM glpi_ocs_link WHERE ID ='" . $data["ID"] . "'";
420
                                                        $DB->query($query);
421
                                                }
422
                                        }
423
                                }
424
                                // Delete item in DB
425
                                $equiv_clean="EQUIVALENT = '$equiv'";
426
                                if (empty($equiv)){
427
                                        $equiv_clean=" ( EQUIVALENT = '$equiv' OR EQUIVALENT IS NULL ) ";
428
                                }
429
                                $query="DELETE FROM deleted_equiv WHERE DELETED = '$del' AND $equiv_clean";
430
                                $DBocs->query($query);
431
                        }
432
                }
433
        }
434
}
435

    
436
function ocsImportComputer($ocs_id, $ocs_server_id, $lock = 0, $defaultentity = -1,$canlink=0) {
437
        global $DBocs, $DB;
438

    
439
        checkOCSconnection($ocs_server_id);
440

    
441
                $comp = new Computer;
442

    
443
                // Set OCS checksum to max value
444
                $query = "UPDATE hardware SET CHECKSUM='" . MAX_OCS_CHECKSUM . "' WHERE ID='$ocs_id'";
445
                $DBocs->query($query);
446

    
447
                //No entity predefined, check rules
448
                if ($defaultentity == -1) {
449
                        //Try to affect computer to an entity
450
                        $rule = new OcsRuleCollection($ocs_server_id);
451
                        $data = array ();
452
                        $data = $rule->processAllRules(array (), array (), $ocs_id);
453
                } else
454
                        //An entity has already been defined via the web interface
455
                        $data['FK_entities'] = $defaultentity;
456

    
457
                //Try to match all the rules, return the first good one, or null if not rules matched
458
                if (isset ($data['FK_entities']) && $data['FK_entities'] >= 0) {
459

    
460
                        if ($lock) {
461
                                while (!$fp = setEntityLock($data['FK_entities'])) {
462
                                        sleep(1);
463
                                }
464
                        }
465

    
466
                        //Check if machine could be linked with another one already in DB
467
                        if ($canlink){
468
                                $found_computers = getMachinesAlreadyInGLPI($ocs_id,$ocs_server_id,$data['FK_entities']);
469
                                // machines founded -> try to link
470
                                if (is_array($found_computers) && count($found_computers)>0){
471
                                        foreach ($found_computers as $glpi_id){
472
                                                if (ocsLinkComputer($ocs_id,$ocs_server_id,$glpi_id,$canlink)){ 
473
                                                        return 3;
474
                                                }
475
                                        }
476
                                }
477
                                // Else simple Import
478
                        }
479
                        
480
                        //New machine to import
481
                        $query = "SELECT * FROM hardware WHERE ID='$ocs_id'";
482
                        $result = $DBocs->query($query);
483
                        if ($result && $DBocs->numrows($result) == 1) {
484
        
485
                                $line = $DBocs->fetch_array($result);
486
                                $line = clean_cross_side_scripting_deep(addslashes_deep($line));
487
        
488
                                $cfg_ocs = getOcsConf($ocs_server_id);
489
                                $input = array ();
490
                                $input["FK_entities"] = $data['FK_entities'];
491
                                $input["name"] = $line["NAME"];
492
                                $input["ocs_import"] = 1;
493
                                if ($cfg_ocs["default_state"]>0){
494
                                        $input["state"] = $cfg_ocs["default_state"];
495
                                }
496
                                $input["_from_ocs"] = 1;
497
                                $glpi_id = $comp->add($input);
498
        
499
                                $ocs_id = $line['ID'];
500
        
501
                                $changes[0]='0';
502
                                $changes[1]="";
503
                                $changes[2]=$ocs_id;
504
                                historyLog ($glpi_id,COMPUTER_TYPE,$changes,0,HISTORY_OCS_IMPORT);
505
                                
506
                                if ($idlink = ocsLink($line['ID'], $ocs_server_id, $glpi_id)) {
507
                                        ocsUpdateComputer($idlink, $ocs_server_id, 0);
508
                                }
509
        
510
                        }
511
        
512
                        if ($lock) {
513
                                removeEntityLock($data['FK_entities'], $fp);
514
                        }
515
                        //Return code to indicates that the machine was imported
516
                        return 1;        
517
                }
518
                else
519
                        //Return code to indicates that the machine was not imported because it doesn't matched rules
520
                        return 2;
521
}
522

    
523
function ocsLinkComputer($ocs_id, $ocs_server_id, $glpi_id,$link_auto=0) {
524
        global $DB, $DBocs, $LANG;
525
        checkOCSconnection($ocs_server_id);
526

    
527
        $query = "SELECT *  
528
                                FROM glpi_ocs_link
529
                                WHERE glpi_id='$glpi_id'";
530

    
531
        $result = $DB->query($query);
532
        $ocs_exists = true;
533
        $numrows = $DB->numrows($result);
534
        // Already link - check if the OCS computer already exists
535
        if ($numrows > 0) {
536
                $data = $DB->fetch_assoc($result);
537
                $query = "SELECT * 
538
                                                        FROM hardware 
539
                                                        WHERE ID='" . $data["ocs_id"] . "'";
540
                $result_ocs = $DBocs->query($query);
541
                // Not found
542
                if ($DBocs->numrows($result_ocs) == 0) {
543
                        $ocs_exists = false;
544
                        $query = "DELETE FROM glpi_ocs_link 
545
                                                                                WHERE ID='" . $data["ID"] . "'";
546
                        $DB->query($query);
547
                }
548
        }
549
        
550
        // TODO : if OCS ID change : ocs_link exists but not hardware in OCS so update only ocs_link and do not reset items before updateComputer
551

    
552
        // No ocs_link or ocs computer does not exists so can link 
553
        if (!$ocs_exists || $numrows == 0) {
554

    
555
                $ocsConfig = getOcsConf($ocs_server_id);
556
                
557
                // Set OCS checksum to max value
558
                $query = "UPDATE hardware 
559
                                                        SET CHECKSUM='" . MAX_OCS_CHECKSUM . "' 
560
                                                        WHERE ID='$ocs_id'";
561
                $DBocs->query($query);
562

    
563
                if ($idlink = ocsLink($ocs_id, $ocs_server_id, $glpi_id)) {
564
                
565
                        $comp = new Computer;
566
                        $comp->getFromDB($glpi_id);
567
                        $input["ID"] = $glpi_id;
568
                        $input["ocs_import"] = 1;
569
                        $input["_from_ocs"] = 1;
570
                        
571
                        // Not already import from OCS / mark default state 
572
                        if ($link_auto || (!$comp->fields['ocs_import'] && $ocsConfig["default_state"]>0)){
573
                                $input["state"] = $ocsConfig["default_state"];
574
                        }
575
        
576
                        $comp->update($input);
577
                        
578
                        // Auto restore if deleted 
579
                        if ($comp->fields['deleted']){ 
580
                                $comp->restore(array('ID'=>$glpi_id));
581
                        }
582
                        
583
                        // Reset using GLPI Config
584
                        $cfg_ocs = getOcsConf($ocs_server_id);
585
                        $query = "SELECT * 
586
                                                FROM hardware 
587
                                                WHERE ID='$ocs_id'";
588
                        $result = $DBocs->query($query);
589
                        $line = $DBocs->fetch_array($result);
590

    
591
                        if ($cfg_ocs["import_general_os"])
592
                                ocsResetDropdown($glpi_id, "os", "glpi_dropdown_os");
593
                        if ($cfg_ocs["import_device_processor"])
594
                                ocsResetDevices($glpi_id, PROCESSOR_DEVICE);
595
                        if ($cfg_ocs["import_device_iface"])
596
                                ocsResetDevices($glpi_id, NETWORK_DEVICE);
597
                        if ($cfg_ocs["import_device_memory"])
598
                                ocsResetDevices($glpi_id, RAM_DEVICE);
599
                        if ($cfg_ocs["import_device_hdd"])
600
                                ocsResetDevices($glpi_id, HDD_DEVICE);
601
                        if ($cfg_ocs["import_device_sound"])
602
                                ocsResetDevices($glpi_id, SND_DEVICE);
603
                        if ($cfg_ocs["import_device_gfxcard"])
604
                                ocsResetDevices($glpi_id, GFX_DEVICE);
605
                        if ($cfg_ocs["import_device_drives"])
606
                                ocsResetDevices($glpi_id, DRIVE_DEVICE);
607
                        if ($cfg_ocs["import_device_modems"] || $cfg_ocs["import_device_ports"])
608
                                ocsResetDevices($glpi_id, PCI_DEVICE);
609
                        if ($cfg_ocs["import_software"])
610
                                ocsResetLicenses($glpi_id);
611
                        if ($cfg_ocs["import_periph"])
612
                                ocsResetPeriphs($glpi_id);
613
                        if ($cfg_ocs["import_monitor"] == 1) // Only reset monitor as global in unit management try to link monitor with existing
614
                                ocsResetMonitors($glpi_id);
615
                        if ($cfg_ocs["import_printer"])
616
                                ocsResetPrinters($glpi_id);
617
                        if ($cfg_ocs["import_registry"])
618
                                ocsResetRegistry($glpi_id);
619

    
620
                        $changes[0]='0';
621
                        $changes[1]="";
622
                        $changes[2]=$ocs_id;
623
                        historyLog ($glpi_id,COMPUTER_TYPE,$changes,0,HISTORY_OCS_LINK);
624
                        
625
                        ocsUpdateComputer($idlink, $ocs_server_id, 0);
626
                        return true;
627
                }
628
        } else {
629
                addMessageAfterRedirect($ocs_id . " - " . $LANG["ocsng"][23]);
630
        }
631

    
632
        return false; 
633
}
634

    
635

    
636
function ocsProcessComputer($ocs_id, $ocs_server_id, $lock = 0, $defaultentity = -1,$canlink=0) {
637
        global $DBocs, $DB;
638

    
639
        checkOCSconnection($ocs_server_id);
640

    
641
        $comp = new Computer;
642
        
643
        //Check it machine is already present AND was imported by OCS
644
        $query = "SELECT ID,glpi_id,ocs_id FROM glpi_ocs_link WHERE ocs_id = '$ocs_id' AND ocs_server_id='" . $ocs_server_id . "';";
645
        $result_glpi_ocs_link = $DB->query($query);
646
        if ($DB->numrows($result_glpi_ocs_link)) {
647
                $datas = $DB->fetch_array($result_glpi_ocs_link);
648
                ocsUpdateComputer($datas["ID"], $ocs_server_id, 1, 0);
649

    
650
                //Return code to indicates that the machine was synchronized
651
                return 0;
652
        } else
653
                return ocsImportComputer($ocs_id, $ocs_server_id, $lock, $defaultentity,$canlink);
654
}
655

    
656
/** Return array of GLPI computers matching the OCS one using the OCS config 
657
* @param $ocs_id integer : ocs ID of the computer
658
* @param $ocs_server_id integer : ocs server ID
659
* @param $entity integer : entity ID
660
* @return array containing the glpi computer ID 
661
*/
662
function getMachinesAlreadyInGLPI($ocs_id,$ocs_server_id,$entity){
663
        global $DB,$DBocs;
664
        $conf = getOcsConf($ocs_server_id);
665
        
666
        $found_computers=array(); 
667

    
668
        $sql_fields = "hardware.ID";
669
        $sql_from ="hardware";
670
        $first = true;
671
        $ocsParams = array();
672
        
673
        if ($conf["glpi_link_enabled"])        {
674
                $ok=false;
675
                //Build the request against OCS database to get the machine's informations
676
                if ( $conf["link_ip"] || $conf["link_mac_address"]){
677
                        $sql_from.=" LEFT JOIN networks ON (hardware.ID=networks.HARDWARE_ID) ";
678
                }        
679

    
680
                if ($conf["link_ip"]){
681
                        $sql_fields.=", networks.IPADDRESS";
682
                        $ocsParams["IPADDRESS"] = array();
683
                        $ok=true;
684
                }
685
                if ($conf["link_mac_address"]){
686
                        $sql_fields.=", networks.MACADDR";
687
                        $ocsParams["MACADDR"] = array();
688
                        $ok=true;
689
                }
690
                if ($conf["link_serial"]){
691
                        $sql_from.=" LEFT JOIN bios ON (bios.HARDWARE_ID=hardware.ID) ";
692

    
693
                        $sql_fields.=", bios.SSN";
694
                        $ocsParams["SSN"] = array();
695
                        $ok=true;
696
                }
697
                if ($conf["link_name"] > 0){
698
                        $sql_fields.=", hardware.NAME";
699
                        $ocsParams["NAME"] = array();
700
                        $ok=true;
701
                }
702
                
703
                // No criteria to link
704
                if (!$ok){
705
                        return -1;
706
                }
707

    
708
                //Execute request
709
                $sql = "SELECT ".$sql_fields." FROM $sql_from WHERE hardware.ID='$ocs_id';";
710
                $result = $DBocs->query($sql);
711

    
712
                //Get the list of parameters
713
                
714
                while ($dataOcs = $DB->fetch_array($result)){
715
                        if ($conf["link_ip"])
716
                                if (!empty($dataOcs["IPADDRESS"]) && !in_array($dataOcs["IPADDRESS"],$ocsParams["IPADDRESS"]))
717
                                        $ocsParams["IPADDRESS"][]= $dataOcs["IPADDRESS"];
718
                        if ($conf["link_mac_address"])
719
                                if (!empty($dataOcs["MACADDR"]) && !in_array($dataOcs["MACADDR"],$ocsParams["MACADDR"]))
720
                                        $ocsParams["MACADDR"][]= $dataOcs["MACADDR"];
721
                        if ($conf["link_name"] > 0)
722
                                if (!empty($dataOcs["NAME"]) && !in_array($dataOcs["NAME"],$ocsParams["NAME"]))
723
                                        $ocsParams["NAME"][]= $dataOcs["NAME"];
724
                        if ($conf["link_serial"])
725
                                if (!empty($dataOcs["SSN"]) && !in_array($dataOcs["SSN"],$ocsParams["SSN"]))
726
                                        $ocsParams["SSN"][]= $dataOcs["SSN"];
727
                }
728

    
729
                //Build the request to check if the machine exists in GLPI
730
                $sql_where = " FK_entities=$entity AND is_template=0 ";
731
                $sql_from = "glpi_computers";
732
                if ( $conf["link_ip"] || $conf["link_mac_address"]){
733
                        $sql_from.=" LEFT JOIN glpi_networking_ports ON (glpi_computers.ID=glpi_networking_ports.on_device AND glpi_networking_ports.device_type=".COMPUTER_TYPE.") ";
734
                }
735
                if ($conf["link_ip"]){
736
                        if (empty($ocsParams["IPADDRESS"]))
737
                                return -1;
738
                        else
739
                        {
740
                                $sql_where.=" AND glpi_networking_ports.ifaddr IN ";
741
                                for ($i=0; $i < count($ocsParams["IPADDRESS"]);$i++)
742
                                        $sql_where .= ($i>0 ? ',"' : '("').$ocsParams["IPADDRESS"][$i].'"';
743
                                $sql_where.=")";
744
                        }
745
                }
746
                if ($conf["link_mac_address"]){
747
                        if (empty($ocsParams["MACADDR"]))
748
                                return -1;
749
                        else
750
                        {
751
                                $sql_where.=" AND glpi_networking_ports.ifmac IN ";
752
                                for ($i=0; $i < count($ocsParams["MACADDR"]);$i++)
753
                                        $sql_where .= ($i>0 ? ',"' : '("').$ocsParams["MACADDR"][$i].'"';
754
                                $sql_where.=")";
755
                        }
756
                }
757
                if ($conf["link_name"] > 0){
758
                        //Search only computers with blank name
759
                        if ($conf["link_name"] == 2)
760
                                $sql_where .= " AND (glpi_computers.name='' OR glpi_computers.name IS NULL) ";
761
                        else
762
                        {
763
                                if (empty($ocsParams["NAME"]))
764
                                        return -1;
765
                                else
766
                                        $sql_where .= " AND glpi_computers.name=\"".$ocsParams["NAME"][0]."\"";
767
                        }
768
                }
769
                if ($conf["link_serial"]){
770
                        if (empty($ocsParams["SSN"]))
771
                                return -1;
772
                        else
773
                                $sql_where .= " AND glpi_computers.serial=\"".$ocsParams["SSN"][0]."\"";
774
                }
775
                if ($conf["link_if_status"] > 0)
776
                        $sql_where .= " AND glpi_computers.state='".$conf["link_if_status"]."'";
777

    
778
                $sql_glpi = "SELECT glpi_computers.ID FROM $sql_from WHERE  $sql_where ;";
779
                $result_glpi = $DB->query($sql_glpi);
780
                
781
                // If only one result
782
                        if ($DB->numrows($result_glpi) > 0){
783
                                while ($data = $DB->fetch_array($result_glpi)) {
784
                                        $found_computers[]=$data['ID'];
785
                                }
786
                        }
787
        }
788

    
789
        return $found_computers; 
790
                
791
}
792

    
793
function ocsUpdateComputer($ID, $ocs_server_id, $dohistory, $force = 0) {
794
        global $DB, $DBocs, $CFG_GLPI;
795

    
796
        checkOCSconnection($ocs_server_id);
797

    
798
        $cfg_ocs = getOcsConf($ocs_server_id);
799

    
800
        $query = "SELECT * 
801
                                FROM glpi_ocs_link 
802
                                WHERE ID='$ID' and ocs_server_id='" . $ocs_server_id."'";
803
        $result = $DB->query($query);
804
        if ($DB->numrows($result) == 1) {
805

    
806
                $line = $DB->fetch_assoc($result);
807

    
808
                $comp = new Computer;
809
                $comp->getFromDB($line["glpi_id"]);
810

    
811
                // Get OCS ID 
812
                $query_ocs = "SELECT * 
813
                                                        FROM hardware 
814
                                                        WHERE ID='" . $line['ocs_id'] . "'";
815
                $result_ocs = $DBocs->query($query_ocs);
816
                // Need do history to be 2 not to lock fields
817
                if ($dohistory) {
818
                        $dohistory = 2;
819
                }
820
                if ($DBocs->numrows($result_ocs) == 1) {
821
                        $data_ocs = addslashes_deep($DBocs->fetch_array($result_ocs));
822

    
823
                        // update last_update and and last_ocs_update
824
                        $query = "UPDATE glpi_ocs_link 
825
                                        SET last_update='" . $_SESSION["glpi_currenttime"] . "', last_ocs_update='" . $data_ocs["LASTDATE"] . "' 
826
                                        , ocs_agent_version='".$data_ocs["USERAGENT"]." ' WHERE ID='$ID'";
827
                        $DB->query($query);
828

    
829
                        if ($force) {
830

    
831
                                $ocs_checksum = MAX_OCS_CHECKSUM;
832
                                $query_ocs = "UPDATE hardware 
833
                                                                                                        SET CHECKSUM= (" . MAX_OCS_CHECKSUM . ") 
834
                                                                                                        WHERE ID='" . $line['ocs_id'] . "'";
835
                                $DBocs->query($query_ocs);
836
                        } else {
837
                                $ocs_checksum = $data_ocs["CHECKSUM"];
838
                        }
839

    
840
                        $mixed_checksum = intval($ocs_checksum) & intval($cfg_ocs["checksum"]);
841

    
842
                        /*                        echo "OCS CS=".decbin($ocs_checksum)." - $ocs_checksum<br>";
843
                                                  echo "GLPI CS=".decbin($cfg_ocs["checksum"])." - ".$cfg_ocs["checksum"]."<br>";
844
                                                  echo "MIXED CS=".decbin($mixed_checksum)." - $mixed_checksum <br>";
845
                         */
846
                        
847
                        //By default log history
848
                        $loghistory["history"] = 1;
849
                        
850
                        // Is an update to do ?
851
                        if ($mixed_checksum) {
852
                                // Get updates on computers :
853
                                $computer_updates = importArrayFromDB($line["computer_update"]);
854

    
855
                                // Update Administrative informations
856
                                ocsUpdateAdministrativeInfo($line['glpi_id'], $line['ocs_id'], $ocs_server_id, $cfg_ocs, $computer_updates, $comp->fields['FK_entities'], $dohistory);
857

    
858
                                if ($mixed_checksum & pow(2, HARDWARE_FL))
859
                                        $loghistory = ocsUpdateHardware($line['glpi_id'], $line['ocs_id'], $ocs_server_id, $cfg_ocs, $computer_updates, $dohistory);
860
                                if ($mixed_checksum & pow(2, BIOS_FL))
861
                                        ocsUpdateBios($line['glpi_id'], $line['ocs_id'], $ocs_server_id, $cfg_ocs, $computer_updates, $dohistory);
862
                                // Get import devices
863
                                $import_device = importArrayFromDB($line["import_device"]);
864
                                if ($mixed_checksum & pow(2, MEMORIES_FL))
865
                                        ocsUpdateDevices(RAM_DEVICE, $line['glpi_id'], $line['ocs_id'], $ocs_server_id, $cfg_ocs, $import_device, '', $dohistory);
866
                                if ($mixed_checksum & pow(2, STORAGES_FL)) {
867
                                        ocsUpdateDevices(HDD_DEVICE, $line['glpi_id'], $line['ocs_id'], $ocs_server_id, $cfg_ocs, $import_device, '', $dohistory);
868
                                        ocsUpdateDevices(DRIVE_DEVICE, $line['glpi_id'], $line['ocs_id'], $ocs_server_id, $cfg_ocs, $import_device, '', $dohistory);
869
                                }
870

    
871
                                if ($mixed_checksum & pow(2, HARDWARE_FL))
872
                                        ocsUpdateDevices(PROCESSOR_DEVICE, $line['glpi_id'], $line['ocs_id'], $ocs_server_id, $cfg_ocs, $import_device, '', $dohistory);
873
                                if ($mixed_checksum & pow(2, VIDEOS_FL))
874
                                        ocsUpdateDevices(GFX_DEVICE, $line['glpi_id'], $line['ocs_id'], $ocs_server_id, $cfg_ocs, $import_device, '', $dohistory);
875
                                if ($mixed_checksum & pow(2, SOUNDS_FL))
876
                                        ocsUpdateDevices(SND_DEVICE, $line['glpi_id'], $line['ocs_id'], $ocs_server_id, $cfg_ocs, $import_device, '', $dohistory);
877

    
878
                                if ($mixed_checksum & pow(2, NETWORKS_FL)) {
879
                                        $import_ip = importArrayFromDB($line["import_ip"]);
880
                                        ocsUpdateDevices(NETWORK_DEVICE, $line['glpi_id'], $line['ocs_id'], $ocs_server_id, $cfg_ocs, $import_device, $import_ip, $dohistory);
881
                                }
882
                                if ($mixed_checksum & pow(2, MODEMS_FL) || $mixed_checksum & pow(2, PORTS_FL))
883
                                        ocsUpdateDevices(PCI_DEVICE, $line['glpi_id'], $line['ocs_id'], $ocs_server_id, $cfg_ocs, $import_device, '', $dohistory);
884

    
885
                                if ($mixed_checksum & pow(2, MONITORS_FL)) {
886
                                        // Get import monitors
887
                                        $import_monitor = importArrayFromDB($line["import_monitor"]);
888
                                        ocsUpdatePeripherals(MONITOR_TYPE, $comp->fields["FK_entities"], $line['glpi_id'], $line['ocs_id'], $ocs_server_id, $cfg_ocs, $import_monitor, $dohistory);
889
                                }
890

    
891
                                if ($mixed_checksum & pow(2, PRINTERS_FL)) {
892
                                        // Get import printers
893
                                        $import_printer = importArrayFromDB($line["import_printers"]);
894
                                        ocsUpdatePeripherals(PRINTER_TYPE, $comp->fields["FK_entities"], $line['glpi_id'], $line['ocs_id'], $ocs_server_id, $cfg_ocs, $import_printer, $dohistory);
895
                                }
896

    
897
                                if ($mixed_checksum & pow(2, INPUTS_FL)) {
898
                                        // Get import monitors
899
                                        $import_peripheral = importArrayFromDB($line["import_peripheral"]);
900
                                        ocsUpdatePeripherals(PERIPHERAL_TYPE, $comp->fields["FK_entities"], $line['glpi_id'], $line['ocs_id'], $ocs_server_id, $cfg_ocs, $import_peripheral, $dohistory);
901
                                }
902
                                if ($mixed_checksum & pow(2, SOFTWARES_FL)) {
903
                                        // Get import monitors
904
                                        $import_software = importArrayFromDB($line["import_software"]);
905
                                        ocsUpdateSoftware($line['glpi_id'], $comp->fields["FK_entities"], $line['ocs_id'], $ocs_server_id, $cfg_ocs, $import_software, (!$loghistory["history"]?0:$dohistory));
906
                                }
907
                                if ($mixed_checksum & pow(2, REGISTRY_FL)) {
908
                                        //import registry entries not needed
909
                                        ocsUpdateRegistry($line['glpi_id'], $line['ocs_id'], $ocs_server_id, $cfg_ocs);
910
                                }
911

    
912
                                // Update OCS Cheksum 
913
                                $query_ocs = "UPDATE hardware 
914
                                                                                                        SET CHECKSUM= (CHECKSUM - $mixed_checksum) 
915
                                                                                                        WHERE ID='" . $line['ocs_id'] . "'";
916
                                $DBocs->query($query_ocs);
917
                                $comp = new Computer();
918
                                $comp->cleanCache($line['glpi_id']);
919
                        }
920

    
921
                }
922
        }
923
}
924

    
925
/**
926
 * Get OCSNG mode configuration
927
 *
928
 * Get all config of the OCSNG mode
929
 *
930
 * @param $id int : ID of the OCS config (default value 1)
931
 *@return Value of $confVar fields or false if unfound.
932
 *
933
 **/
934
function getOcsConf($id) {
935

    
936
        global $DB, $CACHE_CFG;
937

    
938
        //if ($data = $CACHE_CFG->get("CFG_OCSGLPI_$id", "GLPI_CFG")) {
939
        //        return $data;
940
        //} else {
941
        $query = "SELECT * 
942
                                                FROM glpi_ocs_config 
943
                                                WHERE ID='$id'";
944
        $result = $DB->query($query);
945
        if ($result)
946
                $data = $DB->fetch_assoc($result);
947
        else
948
                $data = 0;
949
        //        $CACHE_CFG->save($data, "CFG_OCSGLPI_$id", "GLPI_CFG");
950
        return $data;
951
        //}
952
}
953

    
954
/**
955
 * Get numbero of OCSNG mode configurations
956
 *
957
 * Get number of OCS configurations
958
 *
959
 **/
960
/* // NOT_USED
961
function getNumberOfOcsConfigs() {
962
        global $DB, $CACHE_CFG;
963

964
        return countElementsInTable("glpi_ocs_config");
965
}
966
*/
967

    
968
/**
969
 * Update the computer hardware configuration
970
 *
971
 * Update the computer hardware configuration
972
 *
973
 *@param $ocs_id integer : glpi computer id
974
 *@param $glpi_id integer : ocs computer id.
975
 *@param $ocs_server_id integer : ocs server id
976
 *@param $cfg_ocs array : ocs config
977
 *@param $computer_updates array : already updated fields of the computer
978
 *@param $dohistory log updates on history ? 
979
 *
980
 *@return nothing.
981
 *
982
 **/
983
function ocsUpdateHardware($glpi_id, $ocs_id, $ocs_server_id, $cfg_ocs, $computer_updates, $dohistory = 2) {
984
        global $LANG, $DB, $DBocs;
985

    
986
        checkOCSconnection($ocs_server_id);
987

    
988
        $query = "SELECT * 
989
                                FROM hardware 
990
                                WHERE ID='" . $ocs_id . "'";
991
        $result = $DBocs->query($query);
992
        
993
        $logHistory = 1;
994
        
995
        if ($DBocs->numrows($result) == 1) {
996

    
997
                $line = $DBocs->fetch_assoc($result);
998
                $line = clean_cross_side_scripting_deep(addslashes_deep($line));
999
                $compudate = array ();
1000

    
1001
                if ($cfg_ocs["import_os_serial"] && !in_array("os_license_number", $computer_updates)) {
1002
                        if (!empty ($line["WINPRODKEY"]))
1003
                                $compupdate["os_license_number"] = $line["WINPRODKEY"];
1004
                        if (!empty ($line["WINPRODID"]))
1005
                                $compupdate["os_license_id"] = $line["WINPRODID"];
1006
                }
1007

    
1008
                $sql_computer = "SELECT glpi_dropdown_os.name as os_name, glpi_dropdown_os_sp.name as os_sp" .
1009
                        " FROM glpi_computers, glpi_ocs_link, glpi_dropdown_os, glpi_dropdown_os_sp" .
1010
                        " WHERE glpi_ocs_link.glpi_id = glpi_computers.ID AND glpi_dropdown_os.ID = glpi_computers.os AND glpi_dropdown_os_sp.ID = glpi_computers.os_sp" .
1011
                        " AND glpi_ocs_link.ocs_id='".$ocs_id."' AND glpi_ocs_link.ocs_server_id='".$ocs_server_id."'";
1012
                $res_computer = $DB->query($sql_computer);
1013
                if ($DB->numrows($res_computer) ==  1)
1014
                {
1015
                        $data_computer = $DB->fetch_array($res_computer);
1016
                        $computerOS = $data_computer["os_name"];
1017
                        $computerOSSP = $data_computer["os_sp"];
1018

    
1019
                        //Do not log software history in case of OS or Service Pack change
1020
                        if (!$dohistory || $computerOS != $line["OSNAME"] || $computerOSSP != $line["OSCOMMENTS"])
1021
                                $logHistory = 0;
1022
                }
1023
                        
1024
                if ($cfg_ocs["import_general_os"]) {
1025
                        if (!in_array("os", $computer_updates)) {
1026
                                $compupdate["os"] = externalImportDropdown('glpi_dropdown_os', $line["OSNAME"]);
1027
                        }
1028
                        if (!in_array("os_version", $computer_updates)) {
1029
                                $compupdate["os_version"] = externalImportDropdown('glpi_dropdown_os_version', $line["OSVERSION"]);
1030
                        }
1031
                        if (!ereg("CEST", $line["OSCOMMENTS"]) && !in_array("os_sp", $computer_updates)) // Not linux comment
1032
                                $compupdate["os_sp"] = externalImportDropdown('glpi_dropdown_os_sp', $line["OSCOMMENTS"]);
1033
                }
1034

    
1035
                if ($cfg_ocs["import_general_domain"] && !in_array("domain", $computer_updates)) {
1036
                        $compupdate["domain"] = externalImportDropdown('glpi_dropdown_domain', $line["WORKGROUP"]);
1037
                }
1038

    
1039
                if ($cfg_ocs["import_general_contact"] && !in_array("contact", $computer_updates)) {
1040
                        $compupdate["contact"] = $line["USERID"];
1041
                        $query = "SELECT ID 
1042
                                                                                FROM glpi_users
1043
                                                                                WHERE name='" . $line["USERID"] . "';";
1044
                        $result = $DB->query($query);
1045
                        if ($DB->numrows($result) == 1 && !in_array("FK_users", $computer_updates)) {
1046
                                $compupdate["FK_users"] = $DB->result($result, 0, 0);
1047
                        }
1048
                }
1049

    
1050
                if ($cfg_ocs["import_general_name"] && !in_array("name", $computer_updates)) {
1051
                        $compupdate["name"] = $line["NAME"];
1052
                }
1053

    
1054
                if ($cfg_ocs["import_general_comments"] && !in_array("comments", $computer_updates)) {
1055
                        $compupdate["comments"] = "";
1056
                        ;
1057
                        if (!empty ($line["DESCRIPTION"]) && $line["DESCRIPTION"] != "N/A")
1058
                                $compupdate["comments"] .= $line["DESCRIPTION"] . "\r\n";
1059
                        $compupdate["comments"] .= "Swap: " . $line["SWAP"];
1060
                }
1061
                if (count($compupdate)) {
1062
                        $compupdate["ID"] = $glpi_id;
1063
                        $comp = new Computer();
1064
                        $compupdate["_from_ocs"] = 1;
1065
                        $comp->update($compupdate, $dohistory);
1066
                }
1067

    
1068
        }
1069
        return array("history"=>$logHistory);
1070
}
1071

    
1072
/**
1073
 * Update the computer bios configuration
1074
 *
1075
 * Update the computer bios configuration
1076
 *
1077
 *@param $ocs_id integer : glpi computer id
1078
 *@param $glpi_id integer : ocs computer id.
1079
 *@param $ocs_server_id integer : ocs server id
1080
 *@param $cfg_ocs array : ocs config
1081
 *@param $computer_updates array : already updated fields of the computer
1082
 *@param $dohistory boolean : log changes ?
1083
 *
1084
 *@return nothing.
1085
 *
1086
 **/
1087
function ocsUpdateBios($glpi_id, $ocs_id, $ocs_server_id, $cfg_ocs, $computer_updates, $dohistory = 2) {
1088
        global $DBocs;
1089

    
1090
        checkOCSconnection($ocs_server_id);
1091

    
1092
        $query = "SELECT * 
1093
                                FROM bios 
1094
                                WHERE HARDWARE_ID='" . $ocs_id . "'";
1095
        $result = $DBocs->query($query);
1096
        $compupdate = array ();
1097
        if ($DBocs->numrows($result) == 1) {
1098
                $line = $DBocs->fetch_assoc($result);
1099
                $line = clean_cross_side_scripting_deep(addslashes_deep($line));
1100
                $compudate = array ();
1101

    
1102
                if ($cfg_ocs["import_general_serial"] && !in_array("serial", $computer_updates)) {
1103
                        $compupdate["serial"] = $line["SSN"];
1104
                }
1105

    
1106
                if ($cfg_ocs["import_general_model"] && !in_array("model", $computer_updates)) {
1107
                        $compupdate["model"] = externalImportDropdown('glpi_dropdown_model', $line["SMODEL"],-1,(isset($line["SMANUFACTURER"])?array("manufacturer"=>$line["SMANUFACTURER"]):array()));
1108
                }
1109

    
1110
                if ($cfg_ocs["import_general_enterprise"] && !in_array("FK_glpi_enterprise", $computer_updates)) {
1111
                        $compupdate["FK_glpi_enterprise"] = externalImportDropdown("glpi_dropdown_manufacturer", $line["SMANUFACTURER"]);
1112
                }
1113

    
1114
                if ($cfg_ocs["import_general_type"] && !empty ($line["TYPE"]) && !in_array("type", $computer_updates)) {
1115
                        $compupdate["type"] = externalImportDropdown('glpi_type_computers', $line["TYPE"]);
1116
                }
1117

    
1118
                if (count($compupdate)) {
1119
                        $compupdate["ID"] = $glpi_id;
1120
                        $comp = new Computer();
1121
                        $compupdate["_from_ocs"] = 1;
1122
                        $comp->update($compupdate, $dohistory);
1123
                }
1124

    
1125
        }
1126
}
1127

    
1128

    
1129

    
1130
/**
1131
 * Import a group from OCS table.
1132
 *
1133
 *@param $value string : Value of the new dropdown.
1134
 *@param $FK_entities int : entity in case of specific dropdown
1135
 *
1136
 *@return integer : dropdown id.
1137
 *
1138
 **/
1139

    
1140
function ocsImportGroup($value, $FK_entities) {
1141
        global $DB, $CFG_GLPI;
1142

    
1143
        if (empty ($value))
1144
                return 0;
1145

    
1146
        $query2 = "SELECT ID
1147
                                FROM glpi_groups
1148
                                WHERE name='" . $value . "' AND FK_entities='$FK_entities'";
1149
        $result2 = $DB->query($query2);
1150
        if ($DB->numrows($result2) == 0) {
1151
                $group = new Group;
1152
                $input["name"] = $value;
1153
                $input["FK_entities"] = $FK_entities;
1154
                $input["_from_ocs"] = 1;
1155
                return $group->add($input);
1156
        } else {
1157
                $line2 = $DB->fetch_array($result2);
1158
                return $line2["ID"];
1159
        }
1160

    
1161
}
1162

    
1163
function ocsCleanLinks($ocs_server_id) {
1164
        global $DB, $DBocs;
1165

    
1166
        checkOCSconnection($ocs_server_id);
1167
        ocsManageDeleted($ocs_server_id);
1168

    
1169
        // Delete unexisting GLPI computers
1170
        $query = "SELECT glpi_ocs_link.ID AS ID 
1171
                                FROM glpi_ocs_link 
1172
                                LEFT JOIN glpi_computers ON glpi_computers.ID=glpi_ocs_link.glpi_id 
1173
                                WHERE glpi_computers.ID IS NULL AND ocs_server_id='$ocs_server_id'";
1174

    
1175
        $result = $DB->query($query);
1176
        if ($DB->numrows($result) > 0) {
1177
                while ($data = $DB->fetch_array($result)) {
1178
                        $query2 = "DELETE FROM glpi_ocs_link 
1179
                                                                WHERE ID='" . $data['ID'] . "'";
1180
                        $DB->query($query2);
1181
                }
1182
        }
1183

    
1184
        // Delete unexisting OCS hardware
1185
        $query_ocs = "SELECT * 
1186
                                FROM hardware";
1187
        $result_ocs = $DBocs->query($query_ocs);
1188

    
1189
        $hardware = array ();
1190
        if ($DBocs->numrows($result_ocs) > 0) {
1191
                while ($data = $DBocs->fetch_array($result_ocs)) {
1192
                        $data = clean_cross_side_scripting_deep(addslashes_deep($data));
1193
                        $hardware[$data["ID"]] = $data["DEVICEID"];
1194
                }
1195
        }
1196
        $query = "SELECT *
1197
                                FROM glpi_ocs_link
1198
                                WHERE ocs_server_id='$ocs_server_id'";
1199
        $result = $DB->query($query);
1200

    
1201
        if ($DB->numrows($result) > 0) {
1202
                while ($data = $DB->fetch_array($result)) {
1203
                        $data = clean_cross_side_scripting_deep(addslashes_deep($data));
1204
                        if (!isset ($hardware[$data["ocs_id"]])) {
1205
                                $query_del = "DELETE FROM glpi_ocs_link 
1206
                                                WHERE ID='" . $data["ID"] . "'";
1207
                                $DB->query($query_del);
1208
                                $comp = new Computer();
1209
                                $comp->delete(array (
1210
                                        "ID" => $data["glpi_id"],
1211
                                        "_from_ocs" => 1
1212
                                ), 0);
1213

    
1214
                        }
1215
                }
1216
        }
1217

    
1218
}
1219

    
1220
function cron_ocsng() {
1221

    
1222
        global $DB, $CFG_GLPI;
1223

    
1224
        //Get a randon server id
1225
        $ocs_server_id = getRandomOCSServerID();
1226
        if ($ocs_server_id > 0) {
1227
                //Initialize the server connection
1228
                $DBocs = getDBocs($ocs_server_id);
1229

    
1230
                $cfg_ocs = getOcsConf($ocs_server_id);
1231
                        logInFile("cron", "Check updates from server " . $cfg_ocs['name'] . "\n");
1232

    
1233
                if (!$cfg_ocs["cron_sync_number"]){
1234
                        return 0;
1235
                }
1236
                ocsManageDeleted($ocs_server_id);
1237

    
1238

    
1239
                $query = "SELECT MAX(last_ocs_update) FROM glpi_ocs_link WHERE ocs_server_id='$ocs_server_id'";
1240
                $max_date="0000-00-00 00:00:00";
1241
                if ($result=$DB->query($query)){
1242
                        if ($DB->numrows($result)>0){
1243
                                $max_date=$DB->result($result,0,0);
1244
                        }
1245
                }
1246

    
1247
                $query_ocs = "SELECT * FROM hardware INNER JOIN accountinfo ON (hardware.ID = accountinfo.HARDWARE_ID)
1248
                        WHERE ((hardware.CHECKSUM & " . $cfg_ocs["checksum"] . ") > 0 OR hardware.LASTDATE > '$max_date') ";
1249
                        
1250
                // workaround to avoid duplicate when synchro occurs during an inventory 
1251
                // "after" insert in ocsweb.hardware  and "before" insert in ocsweb.deleted_equiv 
1252
                $query_ocs .= " AND TIMESTAMP(LASTDATE) < (NOW()-180) ";
1253
                
1254
                
1255
                if (!empty ($cfg_ocs["tag_limit"])) {
1256
                        $splitter = explode("$", $cfg_ocs["tag_limit"]);
1257
                        if (count($splitter)) {
1258
                                $query_ocs .= " AND (accountinfo.TAG='" . $splitter[0] . "' ";
1259
                                for ($i = 1; $i < count($splitter); $i++){ 
1260
                                        $query_ocs .= " OR accountinfo.TAG='" .$splitter[$i] . "' ";
1261
                                }
1262
                                $query_ocs .=")"; 
1263
                        }
1264
                }
1265
                
1266
                $query_ocs.=" ORDER BY hardware.LASTDATE ASC LIMIT ".$cfg_ocs["cron_sync_number"]; 
1267
                
1268
                $result_ocs = $DBocs->query($query_ocs);
1269
                if ($DBocs->numrows($result_ocs) > 0) {
1270
                        while ($data = $DBocs->fetch_array($result_ocs)) {
1271
                                ocsProcessComputer($data["ID"],$ocs_server_id,0,-1,1);
1272
                                logInFile("cron", "Update computer " . $data["ID"] . "\n");
1273
                        }
1274
                } else {
1275
                        return 0;
1276
                }
1277
        }
1278
        return 1;
1279
}
1280

    
1281

    
1282

    
1283
function ocsShowUpdateComputer($ocs_server_id, $check, $start) {
1284
        global $DB, $DBocs, $LANG, $CFG_GLPI;
1285

    
1286
        checkOCSconnection($ocs_server_id);
1287

    
1288
        if (!haveRight("ocsng", "w"))
1289
                return false;
1290

    
1291
        $cfg_ocs = getOcsConf($ocs_server_id);
1292
        $query_ocs = "SELECT * 
1293
                                FROM hardware 
1294
                                WHERE (CHECKSUM & " . $cfg_ocs["checksum"] . ") > 0 
1295
                                ORDER BY LASTDATE";
1296
        $result_ocs = $DBocs->query($query_ocs);
1297

    
1298
        $query_glpi = "SELECT glpi_ocs_link.last_update as last_update,  glpi_ocs_link.glpi_id as glpi_id, 
1299
                                glpi_ocs_link.ocs_id as ocs_id, glpi_computers.name as name, 
1300
                                glpi_ocs_link.auto_update as auto_update, glpi_ocs_link.ID as ID 
1301
                                        FROM glpi_ocs_link  
1302
                                        LEFT JOIN glpi_computers ON (glpi_computers.ID = glpi_ocs_link.glpi_id) 
1303
                                        WHERE glpi_ocs_link.ocs_server_id='" . $ocs_server_id . "' ORDER BY glpi_ocs_link.auto_update DESC, glpi_ocs_link.last_update, glpi_computers.name";
1304

    
1305
        $result_glpi = $DB->query($query_glpi);
1306
        if ($DBocs->numrows($result_ocs) > 0) {
1307
                // Get all hardware from OCS DB
1308
                $hardware = array ();
1309
                while ($data = $DBocs->fetch_array($result_ocs)) {
1310
                        $hardware[$data["ID"]]["date"] = $data["LASTDATE"];
1311
                        $hardware[$data["ID"]]["name"] = addslashes($data["NAME"]);
1312
                }
1313

    
1314
                // Get all links between glpi and OCS
1315
                $already_linked = array ();
1316
                if ($DB->numrows($result_glpi) > 0) {
1317
                        while ($data = $DB->fetch_assoc($result_glpi)) {
1318
                                $data = clean_cross_side_scripting_deep(addslashes_deep($data));
1319
                                if (isset ($hardware[$data["ocs_id"]])) {
1320
                                        $already_linked[$data["ocs_id"]]["date"] = $data["last_update"];
1321
                                        $already_linked[$data["ocs_id"]]["name"] = $data["name"];
1322
                                        $already_linked[$data["ocs_id"]]["ID"] = $data["ID"];
1323
                                        $already_linked[$data["ocs_id"]]["glpi_id"] = $data["glpi_id"];
1324
                                        $already_linked[$data["ocs_id"]]["ocs_id"] = $data["ocs_id"];
1325
                                        $already_linked[$data["ocs_id"]]["auto_update"] = $data["auto_update"];
1326
                                }
1327
                        }
1328
                }
1329

    
1330
                echo "<div class='center'>";
1331
                echo "<h2>" . $LANG["ocsng"][10] . "</h2>";
1332

    
1333
                if (($numrows = count($already_linked)) > 0) {
1334

    
1335
                        $parameters = "check=$check";
1336
                        printPager($start, $numrows, $_SERVER['PHP_SELF'], $parameters);
1337

    
1338
                        // delete end 
1339
                        array_splice($already_linked, $start + $_SESSION["glpilist_limit"]);
1340
                        // delete begin
1341
                        if ($start > 0)
1342
                                array_splice($already_linked, 0, $start);
1343

    
1344
                        echo "<form method='post' id='ocsng_form' name='ocsng_form' action='" . $_SERVER['PHP_SELF'] . "'>";
1345

    
1346
                        echo "<a href='" . $_SERVER['PHP_SELF'] . "?check=all' onclick= \"if ( markAllRows('ocsng_form') ) return false;\">" . $LANG["buttons"][18] . "</a>&nbsp;/&nbsp;<a href='" . $_SERVER['PHP_SELF'] . "?check=none' onclick= \"if ( unMarkAllRows('ocsng_form') ) return false;\">" . $LANG["buttons"][19] . "</a>";
1347
                        echo "<table class='tab_cadre'>";
1348
                        echo "<tr><th>" . $LANG["ocsng"][11] . "</th><th>" . $LANG["ocsng"][13] . "</th><th>" . $LANG["ocsng"][14] . "</th><th>" . $LANG["ocsng"][6] . "</th><th>&nbsp;</th></tr>";
1349

    
1350
                        echo "<tr class='tab_bg_1'><td colspan='5' align='center'>";
1351
                        echo "<input class='submit' type='submit' name='update_ok' value='" . $LANG["ldap"][15] . "'>";
1352
                        echo "</td></tr>";
1353

    
1354
                        foreach ($already_linked as $ID => $tab) {
1355

    
1356
                                echo "<tr align='center' class='tab_bg_2'>";
1357
                                echo "<td><a href='" . $CFG_GLPI["root_doc"] . "/front/computer.form.php?ID=" . $tab["glpi_id"] . "'>" . $tab["name"] . "</a></td>";
1358
                                echo "<td>" . convDateTime($tab["date"]) . "</td><td>" . convDateTime($hardware[$tab["ocs_id"]]["date"]) . "</td>";
1359
                                echo "<td>" . $LANG["choice"][$tab["auto_update"]] . "</td>";
1360
                                echo "<td><input type='checkbox' name='toupdate[" . $tab["ID"] . "]' " . ($check == "all" ? "checked" : "") . ">";
1361
                                echo "</td></tr>";
1362
                        }
1363
                        echo "<tr class='tab_bg_1'><td colspan='5' align='center'>";
1364
                        echo "<input class='submit' type='submit' name='update_ok' value='" . $LANG["ldap"][15] . "'>";
1365
                        echo "<input type=hidden name='ocs_server_id' value='" . $ocs_server_id . "'>";
1366
                        echo "</td></tr>";
1367
                        echo "</table>";
1368
                        echo "</form>";
1369
                        printPager($start, $numrows, $_SERVER['PHP_SELF'], $parameters);
1370

    
1371
                } else
1372
                        echo "<br><strong>" . $LANG["ocsng"][11] . "</strong>";
1373

    
1374
                echo "</div>";
1375

    
1376
        } else
1377
                echo "<div class='center'><strong>" . $LANG["ocsng"][12] . "</strong></div>";
1378
}
1379

    
1380
function mergeOcsArray($glpi_id, $tomerge, $field) {
1381
        global $DB;
1382
        $query = "SELECT $field 
1383
                                FROM glpi_ocs_link 
1384
                                WHERE glpi_id='$glpi_id'";
1385
        if ($result = $DB->query($query)) {
1386
                if ($DB->numrows($result)){
1387
                        $tab = importArrayFromDB($DB->result($result, 0, 0));
1388
                        $newtab = array_merge($tomerge, $tab);
1389
                        $newtab = array_unique($newtab);
1390
                        $query = "UPDATE glpi_ocs_link 
1391
                                                                        SET `$field`='" . exportArrayToDB($newtab) . "' 
1392
                                                                        WHERE glpi_id='$glpi_id'";
1393
                        $DB->query($query);
1394
                }
1395
        }
1396

    
1397
}
1398

    
1399
function deleteInOcsArray($glpi_id, $todel, $field,$is_value_to_del=false) {
1400
        global $DB;
1401
        $query = "SELECT `$field` FROM glpi_ocs_link WHERE glpi_id='$glpi_id'";
1402
        if ($result = $DB->query($query)) {
1403
                if ($DB->numrows($result)){
1404
                        $tab = importArrayFromDB($DB->result($result, 0, 0));
1405
                        if ($is_value_to_del){
1406
                                $todel=array_search($todel,$tab);
1407
                        }
1408
                        if (isset($tab[$todel])){
1409
                                unset ($tab[$todel]);
1410
                                $query = "UPDATE glpi_ocs_link 
1411
                                                                        SET `$field`='" . exportArrayToDB($tab) . "' 
1412
                                                                        WHERE glpi_id='$glpi_id'";
1413
                                $DB->query($query);
1414
                        }
1415
                }
1416
        }
1417
}
1418

    
1419
function replaceOcsArray($glpi_id, $newArray, $field) {
1420
        global $DB;
1421
        
1422
        $newArray = exportArrayToDB($newArray);
1423
        $query = "SELECT `$field` FROM glpi_ocs_link WHERE glpi_id='".$glpi_id."'";
1424
        if ($result = $DB->query($query)) {
1425
                if ($DB->numrows($result)){
1426
                        $query = "UPDATE glpi_ocs_link 
1427
                                                                SET `$field`='" . $newArray . "' 
1428
                                                                WHERE glpi_id='".$glpi_id."'";
1429
                        $DB->query($query);
1430
                }
1431
        }
1432
}
1433

    
1434
function addToOcsArray($glpi_id, $toadd, $field) {
1435
        global $DB;
1436
        $query = "SELECT `$field` 
1437
                                FROM glpi_ocs_link 
1438
                                WHERE glpi_id='$glpi_id'";
1439
        if ($result = $DB->query($query)) {
1440
                if ($DB->numrows($result)){
1441
                        $tab = importArrayFromDB($DB->result($result, 0, 0));
1442
                        foreach ($toadd as $key => $val) {
1443
                                $tab[$key] = $val;
1444
                        }
1445
                        $query = "UPDATE glpi_ocs_link 
1446
                                                                SET `$field`='" . exportArrayToDB($tab) . "' 
1447
                                                                WHERE glpi_id='$glpi_id'";
1448
                        $DB->query($query);
1449
                }
1450
        }
1451

    
1452

    
1453
}
1454

    
1455
function getOcsLockableFields(){
1456
        global $LANG;
1457
        
1458
        return array (
1459
                        "name"=>$LANG["common"][16],
1460
                        "type"=>$LANG["common"][17],
1461
                        "FK_glpi_enterprise"=>$LANG["common"][5],
1462
                        "model"=>$LANG["common"][22],
1463
                        "serial"=>$LANG["common"][19],
1464
                        "otherserial"=>$LANG["common"][20],
1465
                        "comments"=>$LANG["common"][25],
1466
                        "contact"=>$LANG["common"][18],
1467
                        "contact_num"=>$LANG["common"][21],
1468
                        "domain"=>$LANG["setup"][89],
1469
                        "network"=>$LANG["setup"][88],
1470
                        "os"=>$LANG["computers"][9],
1471
                        "os_sp"=>$LANG["computers"][53],
1472
                        "os_version"=>$LANG["computers"][52],
1473
                        "os_license_number"=>$LANG["computers"][10],
1474
                        "os_license_id"=>$LANG["computers"][11],
1475
                        "FK_users"=>$LANG["common"][34],
1476
                        "location"=>$LANG["common"][15],
1477
                        "FK_groups"=>$LANG["common"][35],
1478
                );
1479
}
1480

    
1481
function ocsUnlockItems($glpi_id,$field){
1482
        global $DB;
1483
        
1484
        if (!in_array($field,array("import_monitor","import_printers","import_peripheral","import_ip","import_software"))){
1485
                return false;
1486
        }
1487
        $query = "SELECT `$field` 
1488
                        FROM glpi_ocs_link 
1489
                        WHERE glpi_id='$glpi_id'";
1490
        if ($result = $DB->query($query)) {
1491
                if ($DB->numrows($result)){
1492
                        $tab = importArrayFromDB($DB->result($result, 0, 0));
1493
                        $update_done=false;
1494
                        
1495
                        foreach ($tab as $key => $val) {
1496
                                if ($val != "_version_070_") {
1497
                                        switch ($field){
1498
                                                case "import_monitor":
1499
                                                case "import_printers":
1500
                                                case "import_peripheral":
1501
                                                        $querySearchLocked = "SELECT end1 FROM glpi_connect_wire WHERE ID='$key'";
1502
                                                        break;
1503
                                                case "import_software":
1504
                                                        $querySearchLocked = "SELECT ID FROM glpi_inst_software WHERE ID='$key'";
1505
                                                        break;
1506
                                                case "import_ip":
1507
                                                        $querySearchLocked = "SELECT * FROM glpi_networking_ports WHERE on_device='$glpi_id' AND device_type='".COMPUTER_TYPE."' AND ifaddr='$val'";
1508
                                                        break;
1509
                                                default :
1510
                                                        return;
1511
                                        }
1512
                                        $resultSearch = $DB->query($querySearchLocked);
1513
                                        if ($DB->numrows($resultSearch) == 0) {
1514
                                                unset($tab[$key]);
1515
                                                $update_done=true;
1516
                                        }
1517
                                }
1518
                        }                
1519
                        
1520
                        if ($update_done){
1521
                                $query = "UPDATE glpi_ocs_link 
1522
                                                SET `$field`='" . exportArrayToDB($tab) . "' 
1523
                                                WHERE glpi_id='$glpi_id'";
1524
                                $DB->query($query);
1525
                        }
1526
                }
1527
        }
1528
        
1529
}
1530

    
1531
function ocsEditLock($target, $ID) {
1532
        global $DB, $LANG, $SEARCH_OPTION;
1533

    
1534
        if (!haveRight("computer","w")) {
1535
                return false;
1536
        }
1537
        $query = "SELECT * 
1538
                                FROM glpi_ocs_link 
1539
                                WHERE glpi_id='$ID'";
1540

    
1541
        $result = $DB->query($query);
1542
        if ($DB->numrows($result) == 1) {
1543
                $data = $DB->fetch_assoc($result);
1544
                if (haveRight("sync_ocsng","w")){
1545
                        echo "<div class='center'>";
1546
                        echo "<form method='post' action=\"$target\">";
1547
                        echo "<input type='hidden' name='ID' value='$ID'>";
1548
                        echo "<table class='tab_cadre'><tr class='tab_bg_2'><td>";
1549
                        echo "<input type='hidden' name='resynch_id' value='" . $data["ID"] . "'>";
1550
                        echo "<input class=submit type='submit' name='force_ocs_resynch' value='" . $LANG["ocsng"][24] . "'>";
1551
                        echo "</td><tr></table>";
1552
                        echo "</form>";
1553
                        echo "</div>";
1554
                }
1555

    
1556
                echo "<div width='50%'>";
1557
                // Print lock fields for OCSNG
1558

    
1559
                $lockable_fields = getOcsLockableFields();
1560
                $locked = importArrayFromDB($data["computer_update"]);
1561
                if (count($locked)>0){
1562
                        foreach ($locked as $key => $val){
1563
                                if (!isset($lockable_fields[$val])){
1564
                                        unset($locked[$key]);
1565
                                }
1566
                        }
1567
                }
1568
                
1569

    
1570
                if (count($locked)) {
1571
                        echo "<form method='post' action=\"$target\">";
1572
                        echo "<input type='hidden' name='ID' value='$ID'>";
1573
                        echo "<table class='tab_cadre'>";
1574
                        echo "<tr><th colspan='2'>" . $LANG["ocsng"][16] . "</th></tr>";
1575
                        foreach ($locked as $key => $val) {
1576
                                echo "<tr class='tab_bg_1'><td>" . $lockable_fields[$val] . "</td><td><input type='checkbox' name='lockfield[" . $key . "]'></td></tr>";
1577
                        }
1578
                        echo "<tr class='tab_bg_2'><td align='center' colspan='2'><input class='submit' type='submit' name='unlock_field' value='" . $LANG["buttons"][38] . "'></td></tr>";
1579
                        echo "</table>";
1580
                        echo "</form>";
1581
                } else {
1582
                        echo "<strong>" . $LANG["ocsng"][15] . "</strong>";
1583
                }
1584
                echo "</div>";
1585

    
1586
                //Search locked monitors
1587
                $header = false;
1588
                echo "<br>";
1589
                echo "<div width='50%'>";
1590
                $locked_monitor = importArrayFromDB($data["import_monitor"]);
1591
                foreach ($locked_monitor as $key => $val) {
1592
                        if ($val != "_version_070_") {
1593
                                $querySearchLockedMonitor = "SELECT end1 FROM glpi_connect_wire WHERE ID='$key'";
1594
                                $resultSearch = $DB->query($querySearchLockedMonitor);
1595
                                if ($DB->numrows($resultSearch) == 0) {
1596
                                        //$header = true;
1597
                                        if (!$header) {
1598
                                                $header = true;
1599
                                                echo "<form method='post' action=\"$target\">";
1600
                                                echo "<input type='hidden' name='ID' value='$ID'>";
1601
                                                echo "<table class='tab_cadre'>";
1602
                                                echo "<tr><th colspan='2'>" . $LANG["ocsng"][30] . "</th></tr>";
1603
                                        }
1604
                                        echo "<tr class='tab_bg_1'><td>" . $val . "</td><td><input type='checkbox' name='lockmonitor[" . $key . "]'></td></tr>";
1605
                                }
1606
                        }
1607
                }
1608
                if ($header) {
1609
                        echo "<tr class='tab_bg_2'><td align='center' colspan='2'><input class='submit' type='submit' name='unlock_monitor' value='" . $LANG["buttons"][38] . "'></td></tr>";
1610
                        echo "</table>";
1611
                        echo "</form>";
1612
                } else
1613
                        echo "<strong>" . $LANG["ocsng"][31] . "</strong>";
1614
                echo "</div>";
1615

    
1616
                //Search locked printers
1617
                $header = false;
1618
                echo "<br>";
1619
                echo "<div class='center'>";
1620
                $locked_printer = importArrayFromDB($data["import_printers"]);
1621
                foreach ($locked_printer as $key => $val) {
1622
                        $querySearchLockedPrinter = "SELECT end1 FROM glpi_connect_wire WHERE ID='$key'";
1623
                        $resultSearchPrinter = $DB->query($querySearchLockedPrinter);
1624
                        if ($DB->numrows($resultSearchPrinter) == 0) {
1625
                                //$header = true;
1626
                                if (!($header)) {
1627
                                        $header = true;
1628
                                        echo "<form method='post' action=\"$target\">";
1629
                                        echo "<input type='hidden' name='ID' value='$ID'>";
1630
                                        echo "<table class='tab_cadre'>";
1631
                                        echo "<tr><th colspan='2'>" . $LANG["ocsng"][34] . "</th></tr>";
1632
                                }
1633
                                echo "<tr class='tab_bg_1'><td>" . $val . "</td><td><input type='checkbox' name='lockprinter[" . $key . "]'></td></tr>";
1634
                        }
1635
                }
1636
                if ($header) {
1637
                        echo "<tr class='tab_bg_2'><td align='center' colspan='2'><input class='submit' type='submit' name='unlock_printer' value='" . $LANG["buttons"][38] . "'></td></tr>";
1638
                        echo "</table>";
1639
                        echo "</form>";
1640
                } else
1641
                        echo "<strong>" . $LANG["ocsng"][35] . "</strong>";
1642
                echo "</div>";
1643

    
1644
                // Search locked peripherals
1645
                $header = false;
1646
                echo "<br>";
1647
                echo "<div class='center'>";
1648
                $locked_printer = importArrayFromDB($data["import_peripheral"]);
1649
                foreach ($locked_printer as $key => $val) {
1650
                        $querySearchLockedPeriph = "SELECT end1 FROM glpi_connect_wire WHERE ID='$key'";
1651
                        $resultSearchPrinter = $DB->query($querySearchLockedPeriph);
1652
                        if ($DB->numrows($resultSearchPrinter) == 0) {
1653
                                //$header = true;
1654
                                if (!($header)) {
1655
                                        $header = true;
1656
                                        echo "<form method='post' action=\"$target\">";
1657
                                        echo "<input type='hidden' name='ID' value='$ID'>";
1658
                                        echo "<table class='tab_cadre'>";
1659
                                        echo "<tr><th colspan='2'>" . $LANG["ocsng"][32] . "</th></tr>";
1660
                                }
1661
                                echo "<tr class='tab_bg_1'><td>" . $val . "</td><td><input type='checkbox' name='lockperiph[" . $key . "]'></td></tr>";
1662
                        }
1663
                }
1664
                if ($header) {
1665
                        echo "<tr class='tab_bg_2'><td align='center' colspan='2'><input class='submit' type='submit' name='unlock_periph' value='" . $LANG["buttons"][38] . "'></td></tr>";
1666
                        echo "</table>";
1667
                        echo "</form>";
1668
                } else
1669
                        echo "<strong>" . $LANG["ocsng"][33] . "</strong>";
1670
                echo "</div>";
1671
                
1672
                // Search locked IP
1673
                $header = false;
1674
                echo "<br>";
1675
                echo "<div class='center'>";
1676
                $locked_ip = importArrayFromDB($data["import_ip"]);
1677
                foreach ($locked_ip as $key => $val) {
1678
                        $querySearchLockedIP = "SELECT * FROM glpi_networking_ports WHERE on_device='$ID' AND device_type='".COMPUTER_TYPE."' AND ifaddr='$val'";
1679
                        $resultSearchIP = $DB->query($querySearchLockedIP);
1680
                        if ($DB->numrows($resultSearchIP) == 0) {
1681
                
1682
                                if (!($header)) {
1683
                                        $header = true;
1684
                                        echo "<form method='post' action=\"$target\">";
1685
                                        echo "<input type='hidden' name='ID' value='$ID'>";
1686
                                        echo "<table class='tab_cadre'>";
1687
                                        echo "<tr><th colspan='2'>" . $LANG["ocsng"][50] . "</th></tr>";
1688
                                }
1689
                                echo "<tr class='tab_bg_1'><td>" . $val . "</td><td><input type='checkbox' name='lockip[" . $key . "]'></td></tr>";
1690
                        }
1691
                }
1692
                if ($header) {
1693
                        echo "<tr class='tab_bg_2'><td align='center' colspan='2'><input class='submit' type='submit' name='unlock_ip' value='" . $LANG["buttons"][38] . "'></td></tr>";
1694
                        echo "</table>";
1695
                        echo "</form>";
1696
                } else
1697
                        echo "<strong>" . $LANG["ocsng"][51] . "</strong>";
1698
                echo "</div>";
1699

    
1700

    
1701
                // Search locked softwares
1702
                $header = false;
1703
                echo "<br>";
1704
                echo "<div class='center'>";
1705
                $locked_software = importArrayFromDB($data["import_software"]);
1706
                foreach ($locked_software as $key => $val) {
1707
                        if ($val != "_version_070_") {
1708
                                $querySearchLockedSoft = "SELECT ID FROM glpi_inst_software WHERE ID='$key'";
1709
                                $resultSearchSoft = $DB->query($querySearchLockedSoft);
1710
                                if ($DB->numrows($resultSearchSoft) == 0) {
1711
                                        //$header = true;
1712
                                        if (!($header)) {
1713
                                                $header = true;
1714
                                                echo "<form method='post' action=\"$target\">";
1715
                                                echo "<input type='hidden' name='ID' value='$ID'>";
1716
                                                echo "<table class='tab_cadre'>";
1717
                                                echo "<tr><th colspan='2'>" . $LANG["ocsng"][52] . "</th></tr>";
1718
                                        }
1719
                                        
1720
                                        echo "<tr class='tab_bg_1'><td>" . ereg_replace('\$\$\$\$\$',' v. ',$val) . "</td><td><input type='checkbox' name='locksoft[" . $key . "]'></td></tr>";
1721
                                }
1722
                        }
1723
                }
1724
                if ($header) {
1725
                        echo "<tr class='tab_bg_2'><td align='center' colspan='2'><input class='submit' type='submit' name='unlock_soft' value='" . $LANG["buttons"][38] . "'></td></tr>";
1726
                        echo "</table>";
1727
                        echo "</form>";
1728
                } else
1729
                        echo "<strong>" . $LANG["ocsng"][53] . "</strong>";
1730
                echo "</div>";                        
1731

    
1732
        }
1733

    
1734
}
1735

    
1736
/**
1737
 * Import the devices for a computer
1738
 *
1739
 * 
1740
 *
1741
 *@param $device_type integer : device type
1742
 *@param $glpi_id integer : glpi computer id.
1743
 *@param $ocs_id integer : ocs computer id (ID).
1744
 *@param $ocs_server_id integer : ocs server id
1745
 *@param $cfg_ocs array : ocs config
1746
 *@param $dohistory boolean : log changes ?
1747
 *@param $import_device array : already imported devices
1748
 *@param $import_ip array : already imported ip
1749
 *
1750
 *@return Nothing (void).
1751
 *
1752
 **/
1753
function ocsUpdateDevices($device_type, $glpi_id, $ocs_id, $ocs_server_id, $cfg_ocs, $import_device, $import_ip, $dohistory) {
1754
        global $DB, $DBocs;
1755

    
1756
        checkOCSconnection($ocs_server_id);
1757

    
1758
        $do_clean = false;
1759
        switch ($device_type) {
1760
                case RAM_DEVICE :
1761
                        //Memoire
1762
                        if ($cfg_ocs["import_device_memory"]) {
1763
                                $do_clean = true;
1764

    
1765
                                $query2 = "SELECT * 
1766
                                                                                                        FROM memories 
1767
                                                                                                        WHERE HARDWARE_ID = '" . $ocs_id . "' 
1768
                                                                                                        ORDER BY ID";
1769
                                $result2 = $DBocs->query($query2);
1770
                                if ($DBocs->numrows($result2) > 0) {
1771
                                        while ($line2 = $DBocs->fetch_array($result2)) {
1772
                                                $line2 = clean_cross_side_scripting_deep(addslashes_deep($line2));
1773
                                                if (!empty ($line2["CAPACITY"]) && $line2["CAPACITY"] != "No") {
1774
                                                        if ($line2["DESCRIPTION"])
1775
                                                                $ram["designation"] = $line2["DESCRIPTION"];
1776
                                                        else
1777
                                                                $ram["designation"] = "Unknown";
1778
                                                        $ram["specif_default"] = $line2["CAPACITY"];
1779
                                                        if (!in_array(RAM_DEVICE . '$$$$$' . $ram["designation"], $import_device)) {
1780
                                                                $ram["frequence"] = $line2["SPEED"];
1781
                                                                $ram["type"] = externalImportDropdown("glpi_dropdown_ram_type", $line2["TYPE"]);
1782
                                                                $ram_id = ocsAddDevice(RAM_DEVICE, $ram);
1783
                                                                if ($ram_id) {
1784
                                                                        $devID = compdevice_add($glpi_id, RAM_DEVICE, $ram_id, $line2["CAPACITY"], $dohistory);
1785
                                                                        addToOcsArray($glpi_id, array (
1786
                                                                                $devID => RAM_DEVICE . '$$$$$' . $ram["designation"]
1787
                                                                        ), "import_device");
1788
                                                                }
1789
                                                        } else {
1790
                                                                $id = array_search(RAM_DEVICE . '$$$$$' . $ram["designation"], $import_device);
1791
                                                                update_device_specif($line2["CAPACITY"], $id, 1,true);
1792
                                                                unset ($import_device[$id]);
1793
                                                        }
1794
                                                }
1795
                                        }
1796
                                }
1797
                        }
1798
                        break;
1799
                case HDD_DEVICE :
1800
                        //Disque Dur
1801
                        if ($cfg_ocs["import_device_hdd"]) {
1802
                                $do_clean = true;
1803

    
1804
                                $query2 = "SELECT * 
1805
                                                                                                        FROM storages 
1806
                                                                                                        WHERE HARDWARE_ID = '" . $ocs_id . "' 
1807
                                                                                                        ORDER BY ID";
1808
                                $result2 = $DBocs->query($query2);
1809

    
1810
                                if ($DBocs->numrows($result2) > 0) {
1811
                                        while ($line2 = $DBocs->fetch_array($result2)) {
1812
                                                $line2 = clean_cross_side_scripting_deep(addslashes_deep($line2));
1813
                                                if (!empty ($line2["DISKSIZE"]) && eregi("disk", $line2["TYPE"])) {
1814
                                                        if ($line2["NAME"])
1815
                                                                $dd["designation"] = $line2["NAME"];
1816
                                                        else
1817
                                                                if ($line2["MODEL"])
1818
                                                                        $dd["designation"] = $line2["MODEL"];
1819
                                                                else
1820
                                                                        $dd["designation"] = "Unknown";
1821

    
1822
                                                        if (!in_array(HDD_DEVICE . '$$$$$' . $dd["designation"], $import_device)) {
1823
                                                                $dd["specif_default"] = $line2["DISKSIZE"];
1824
                                                                $dd_id = ocsAddDevice(HDD_DEVICE, $dd);
1825
                                                                if ($dd_id) {
1826
                                                                        $devID = compdevice_add($glpi_id, HDD_DEVICE, $dd_id, $line2["DISKSIZE"], $dohistory);
1827
                                                                        addToOcsArray($glpi_id, array (
1828
                                                                                $devID => HDD_DEVICE . '$$$$$' . $dd["designation"]
1829
                                                                        ), "import_device");
1830
                                                                }
1831
                                                        } else {
1832
                                                                $id = array_search(HDD_DEVICE . '$$$$$' . $dd["designation"], $import_device);
1833
                                                                update_device_specif($line2["DISKSIZE"], $id, 1,true);
1834
                                                                unset ($import_device[$id]);
1835
                                                        }
1836

    
1837
                                                }
1838
                                        }
1839
                                }
1840
                        }
1841

    
1842
                        break;
1843
                case DRIVE_DEVICE :
1844
                        //lecteurs
1845
                        if ($cfg_ocs["import_device_drives"]) {
1846
                                $do_clean = true;
1847

    
1848
                                $query2 = "SELECT * 
1849
                                                                                                        FROM storages 
1850
                                                                                                        WHERE HARDWARE_ID = '" . $ocs_id . "' ORDER BY ID";
1851
                                $result2 = $DBocs->query($query2);
1852
                                if ($DBocs->numrows($result2) > 0) {
1853
                                        while ($line2 = $DBocs->fetch_array($result2)) {
1854
                                                $line2 = clean_cross_side_scripting_deep(addslashes_deep($line2));
1855
                                                if (empty ($line2["DISKSIZE"]) || !eregi("disk", $line2["TYPE"])) {
1856
                                                        if ($line2["NAME"])
1857
                                                                $stor["designation"] = $line2["NAME"];
1858
                                                        else
1859
                                                                if ($line2["MODEL"])
1860
                                                                        $stor["designation"] = $line2["MODEL"];
1861
                                                                else
1862
                                                                        $stor["designation"] = "Unknown";
1863
                                                        if (!in_array(DRIVE_DEVICE . '$$$$$' . $stor["designation"], $import_device)) {
1864
                                                                $stor["specif_default"] = $line2["DISKSIZE"];
1865
                                                                $stor_id = ocsAddDevice(DRIVE_DEVICE, $stor);
1866
                                                                if ($stor_id) {
1867
                                                                        $devID = compdevice_add($glpi_id, DRIVE_DEVICE, $stor_id, "", $dohistory);
1868
                                                                        addToOcsArray($glpi_id, array (
1869
                                                                                $devID => DRIVE_DEVICE . '$$$$$' . $stor["designation"]
1870
                                                                        ), "import_device");
1871
                                                                }
1872
                                                        } else {
1873
                                                                $id = array_search(DRIVE_DEVICE . '$$$$$' . $stor["designation"], $import_device);
1874
                                                                unset ($import_device[$id]);
1875
                                                        }
1876

    
1877
                                                }
1878
                                        }
1879
                                }
1880
                        }
1881
                        break;
1882
                case PCI_DEVICE :
1883
                        //Modems
1884
                        if ($cfg_ocs["import_device_modems"]) {
1885
                                $do_clean = true;
1886

    
1887
                                $query2 = "SELECT * 
1888
                                                                                                        FROM modems 
1889
                                                                                                        WHERE HARDWARE_ID = '" . $ocs_id . "' 
1890
                                                                                                        ORDER BY ID";
1891
                                $result2 = $DBocs->query($query2);
1892
                                if ($DBocs->numrows($result2) > 0) {
1893
                                        while ($line2 = $DBocs->fetch_array($result2)) {
1894
                                                $line2 = clean_cross_side_scripting_deep(addslashes_deep($line2));
1895
                                                $mdm["designation"] = $line2["NAME"];
1896
                                                if (!in_array(PCI_DEVICE . '$$$$$' . $mdm["designation"], $import_device)) {
1897
                                                        if (!empty ($line2["DESCRIPTION"]))
1898
                                                                $mdm["comment"] = $line2["TYPE"] . "\r\n" . $line2["DESCRIPTION"];
1899
                                                        $mdm_id = ocsAddDevice(PCI_DEVICE, $mdm);
1900
                                                        if ($mdm_id) {
1901
                                                                $devID = compdevice_add($glpi_id, PCI_DEVICE, $mdm_id, "", $dohistory);
1902
                                                                addToOcsArray($glpi_id, array (
1903
                                                                        $devID => PCI_DEVICE . '$$$$$' . $mdm["designation"]
1904
                                                                ), "import_device");
1905
                                                        }
1906
                                                } else {
1907
                                                        $id = array_search(PCI_DEVICE . '$$$$$' . $mdm["designation"], $import_device);
1908
                                                        unset ($import_device[$id]);
1909
                                                }
1910

    
1911
                                        }
1912
                                }
1913
                        }
1914
                        //Ports
1915
                        if ($cfg_ocs["import_device_ports"]) {
1916

    
1917
                                $query2 = "SELECT * 
1918
                                                                                                        FROM ports 
1919
                                                                                                        WHERE HARDWARE_ID = '" . $ocs_id . "' 
1920
                                                                                                        ORDER BY ID";
1921
                                $result2 = $DBocs->query($query2);
1922
                                if ($DBocs->numrows($result2) > 0) {
1923
                                        while ($line2 = $DBocs->fetch_array($result2)) {
1924
                                                $line2 = clean_cross_side_scripting_deep(addslashes_deep($line2));
1925
                                                $port["designation"] = "";
1926
                                                if ($line2["TYPE"] != "Other")
1927
                                                        $port["designation"] .= $line2["TYPE"];
1928
                                                if ($line2["NAME"] != "Not Specified")
1929
                                                        $port["designation"] .= " " . $line2["NAME"];
1930
                                                else
1931
                                                        if ($line2["CAPTION"] != "None")
1932
                                                                $port["designation"] .= " " . $line2["CAPTION"];
1933
                                                if (!empty ($port["designation"]))
1934
                                                        if (!in_array(PCI_DEVICE . '$$$$$' . $port["designation"], $import_device)) {
1935
                                                                if (!empty ($line2["DESCRIPTION"]) && $line2["DESCRIPTION"] != "None")
1936
                                                                        $port["comment"] = $line2["DESCRIPTION"];
1937
                                                                $port_id = ocsAddDevice(PCI_DEVICE, $port);
1938
                                                                if ($port_id) {
1939
                                                                        $devID = compdevice_add($glpi_id, PCI_DEVICE, $port_id, "", $dohistory);
1940
                                                                        addToOcsArray($glpi_id, array (
1941
                                                                                $devID => PCI_DEVICE . '$$$$$' . $port["designation"]
1942
                                                                        ), "import_device");
1943
                                                                }
1944
                                                        } else {
1945
                                                                $id = array_search(PCI_DEVICE . '$$$$$' . $port["designation"], $import_device);
1946
                                                                unset ($import_device[$id]);
1947
                                                        }
1948
                                        }
1949
                                }
1950
                        }
1951
                        break;
1952
                case PROCESSOR_DEVICE :
1953
                        //Processeurs : 
1954
                        if ($cfg_ocs["import_device_processor"]) {
1955
                                $do_clean = true;
1956

    
1957
                                $query = "SELECT * 
1958
                                                                                                        FROM hardware 
1959
                                                                                                        WHERE ID='$ocs_id' ORDER BY ID";
1960
                                $result = $DBocs->query($query);
1961
                                if ($DBocs->numrows($result) == 1) {
1962
                                        $line = $DBocs->fetch_array($result);
1963
                                        $line = clean_cross_side_scripting_deep(addslashes_deep($line));
1964
                                        for ($i = 0; $i < $line["PROCESSORN"]; $i++) {
1965
                                                $processor = array ();
1966
                                                $processor["designation"] = $line["PROCESSORT"];
1967
                                                $processor["specif_default"] = $line["PROCESSORS"];
1968
                                                if (!in_array(PROCESSOR_DEVICE . '$$$$$' . $processor["designation"], $import_device)) {
1969
                                                        $proc_id = ocsAddDevice(PROCESSOR_DEVICE, $processor);
1970
                                                        if ($proc_id) {
1971
                                                                $devID = compdevice_add($glpi_id, PROCESSOR_DEVICE, $proc_id, $line["PROCESSORS"], $dohistory);
1972
                                                                addToOcsArray($glpi_id, array (
1973
                                                                        $devID => PROCESSOR_DEVICE . '$$$$$' . $processor["designation"]
1974
                                                                ), "import_device");
1975
                                                        }
1976
                                                } else {
1977
                                                        $id = array_search(PROCESSOR_DEVICE . '$$$$$' . $processor["designation"], $import_device);
1978
                                                        update_device_specif($line["PROCESSORS"], $id, 1,true);
1979
                                                        unset ($import_device[$id]);
1980
                                                }
1981
                                        }
1982
                                }
1983
                        }
1984
                        break;
1985
                case NETWORK_DEVICE :
1986

    
1987
                        //Carte reseau
1988
                        if ($cfg_ocs["import_device_iface"] || $cfg_ocs["import_ip"]) {
1989

    
1990
                                $query2 = "SELECT * 
1991
                                                                                                        FROM networks 
1992
                                                                                                        WHERE HARDWARE_ID = '" . $ocs_id . "' 
1993
                                                                                                        ORDER BY ID";
1994

    
1995
                                $result2 = $DBocs->query($query2);
1996
                                $i = 0;
1997
                                $manually_link = false;
1998
                                
1999
                                //Count old ip in GLPI
2000
                                $count_ip = count($import_ip);
2001
                                // Add network device
2002
                                if ($DBocs->numrows($result2) > 0) {
2003
                                        while ($line2 = $DBocs->fetch_array($result2)) {
2004
                                                $line2 = clean_cross_side_scripting_deep(addslashes_deep($line2));
2005
                                                if ($cfg_ocs["import_device_iface"]) {
2006
                                                        $do_clean = true;
2007
                                                        $network["designation"] = $line2["DESCRIPTION"];
2008
                                                        if (!in_array(NETWORK_DEVICE . '$$$$$' . $network["designation"], $import_device)) {
2009
                                                                
2010
                                                                if (!empty ($line2["SPEED"]))
2011
                                                                        $network["bandwidth"] = $line2["SPEED"];
2012
                                                                $net_id = ocsAddDevice(NETWORK_DEVICE, $network);
2013
                                                                if ($net_id) {
2014
                                                                        $devID = compdevice_add($glpi_id, NETWORK_DEVICE, $net_id, $line2["MACADDR"], $dohistory);
2015
                                                                        addToOcsArray($glpi_id, array (
2016
                                                                                $devID => NETWORK_DEVICE . '$$$$$' . $network["designation"]
2017
                                                                        ), "import_device");
2018
                                                                }
2019
                                                        } else {
2020
                                                                $id = array_search(NETWORK_DEVICE . '$$$$$' . $network["designation"], $import_device);
2021
                                                                update_device_specif($line2["MACADDR"], $id, 1,true);
2022
                                                                unset ($import_device[$id]);
2023
                                                        }
2024
                                                }
2025

    
2026
                                                if (!empty ($line2["IPADDRESS"]) && $cfg_ocs["import_ip"]) {
2027
                                                        $do_clean = true;
2028
                                                        $ocs_ips = split(",", $line2["IPADDRESS"]);
2029
                                                        $ocs_ips = array_unique($ocs_ips);
2030
                                                        sort($ocs_ips);
2031

    
2032
                                                        //if never imported in 0.70, insert id in the array
2033
                                                        if ($count_ip == 0) {
2034
                                                                //get old IP in DB                                                        
2035
                                                                $querySelectIDandIP = "SELECT ID,ifaddr FROM glpi_networking_ports
2036
                                                                                        WHERE device_type='" . COMPUTER_TYPE . "' 
2037
                                                                                        AND on_device='$glpi_id' 
2038
                                                                                        AND ifmac='" . $line2["MACADDR"] . "'" . "
2039
                                                                                        AND name='" . $line2["DESCRIPTION"] . "'";
2040
                                                                $result = $DB->query($querySelectIDandIP);
2041
                                                                if ($DB->numrows($result) > 0) {
2042
                                                                        while ($data = $DB->fetch_array($result)) {
2043
                                                                                //Upate import_ip column and import_ip array                                                                                
2044
                                                                                addToOcsArray($glpi_id, array (
2045
                                                                                        $data["ID"] => $data["ifaddr"]
2046
                                                                                ), "import_ip");
2047
                                                                                $import_ip[$data["ID"]] = $data["ifaddr"];
2048
                                                                        }
2049
                                                                }
2050
                                                        }
2051
                                                        $netport=array();
2052
                                                        $netport["ifmac"] = $line2["MACADDR"];
2053
                                                        $netport["iface"] = externalImportDropdown("glpi_dropdown_iface", $line2["TYPE"]);
2054
                                                        $netport["name"] = $line2["DESCRIPTION"];
2055
                                                        $netport["on_device"] = $glpi_id;
2056
                                                        $netport["device_type"] = COMPUTER_TYPE;
2057
                                                        $netport["netmask"] = $line2["IPMASK"];
2058
                                                        $netport["gateway"] = $line2["IPGATEWAY"];
2059
                                                        $netport["subnet"] = $line2["IPSUBNET"];
2060

    
2061
                                                        $np = new Netport();
2062

    
2063
                                                        for ($j = 0; $j < count($ocs_ips); $j++) {
2064
                                                                $id_ip = array_search($ocs_ips[$j], $import_ip);
2065
                                                                //Update already in DB
2066
                                                                if ($id_ip>0) {
2067
                                                                        $netport["ifaddr"] = $ocs_ips[$j];
2068
                                                                        $netport["logical_number"] = $j;
2069
                                                                        $netport["ID"] = $id_ip;
2070
                                                                        $netport["_from_ocs"] = 1;
2071
                                                                        $np->update($netport);
2072
                                                                        unset ($import_ip[$id_ip]);
2073
                                                                        $count_ip++;
2074
                                                                }
2075
                                                                //If new IP found
2076
                                                                else {
2077
                                                                        unset ($np->fields["netpoint"]);
2078
                                                                        unset ($netport["ID"]);
2079
                                                                        unset ($np->fields["ID"]);
2080
                                                                        $netport["ifaddr"] = $ocs_ips[$j];
2081
                                                                        $netport["logical_number"] = $j;
2082
                                                                        $netport["_from_ocs"] = 1;
2083
                                                                        $newID = $np->add($netport);
2084
                                                                        //ADD to array
2085
                                                                        addToOcsArray($glpi_id, array (
2086
                                                                                $newID => $ocs_ips[$j]
2087
                                                                        ), "import_ip");
2088
                                                                        $count_ip++;
2089
                                                                }
2090
                                                        }
2091
                                                }
2092
                                        }
2093
                                }
2094
                        }
2095
                        break;
2096
                case GFX_DEVICE :
2097
                        //carte graphique
2098
                        if ($cfg_ocs["import_device_gfxcard"]) {
2099
                                $do_clean = true;
2100

    
2101
                                $query2 = "SELECT DISTINCT(NAME) as NAME, MEMORY 
2102
                                                                                                        FROM videos 
2103
                                                                                                        WHERE HARDWARE_ID = '" . $ocs_id . "'and NAME != '' 
2104
                                                                                                        ORDER BY ID";
2105
                                $result2 = $DBocs->query($query2);
2106
                                if ($DBocs->numrows($result2) > 0) {
2107
                                        while ($line2 = $DBocs->fetch_array($result2)) {
2108
                                                $line2 = clean_cross_side_scripting_deep(addslashes_deep($line2));
2109
                                                $video["designation"] = $line2["NAME"];
2110
                                                if (!in_array(GFX_DEVICE . '$$$$$' . $video["designation"], $import_device)) {
2111
                                                        $video["specif_default"] = "";
2112
                                                        $video["interface"] = "";
2113
                                                        if (!empty ($line2["MEMORY"]))
2114
                                                                $video["specif_default"] = $line2["MEMORY"];
2115
                                                        $video_id = ocsAddDevice(GFX_DEVICE, $video);
2116
                                                        if ($video_id) {
2117
                                                                $devID = compdevice_add($glpi_id, GFX_DEVICE, $video_id, $video["specif_default"], $dohistory);
2118
                                                                addToOcsArray($glpi_id, array (
2119
                                                                        $devID => GFX_DEVICE . '$$$$$' . $video["designation"]
2120
                                                                ), "import_device");
2121
                                                        }
2122
                                                } else {
2123
                                                        $id = array_search(GFX_DEVICE . '$$$$$' . $video["designation"], $import_device);
2124
                                                        update_device_specif($line2["MEMORY"], $id, 1,true);
2125
                                                        unset ($import_device[$id]);
2126
                                                }
2127
                                        }
2128
                                }
2129
                        }
2130
                        break;
2131
                case SND_DEVICE :
2132
                        //carte son
2133
                        if ($cfg_ocs["import_device_sound"]) {
2134
                                $do_clean = true;
2135

    
2136
                                $query2 = "SELECT DISTINCT(NAME) as NAME, DESCRIPTION 
2137
                                                                                                        FROM sounds 
2138
                                                                                                        WHERE HARDWARE_ID = '" . $ocs_id . "' 
2139
                                                                                                        AND NAME != '' ORDER BY ID";
2140
                                $result2 = $DBocs->query($query2);
2141
                                if ($DBocs->numrows($result2) > 0) {
2142
                                        while ($line2 = $DBocs->fetch_array($result2)) {
2143
                                                $line2 = clean_cross_side_scripting_deep(addslashes_deep($line2));
2144
                                                $snd["designation"] = $line2["NAME"];
2145
                                                if (!in_array(SND_DEVICE . '$$$$$' . $snd["designation"], $import_device)) {
2146
                                                        if (!empty ($line2["DESCRIPTION"]))
2147
                                                                $snd["comment"] = $line2["DESCRIPTION"];
2148
                                                        $snd_id = ocsAddDevice(SND_DEVICE, $snd);
2149
                                                        if ($snd_id) {
2150
                                                                $devID = compdevice_add($glpi_id, SND_DEVICE, $snd_id, "", $dohistory);
2151
                                                                addToOcsArray($glpi_id, array (
2152
                                                                        $devID => SND_DEVICE . '$$$$$' . $snd["designation"]
2153
                                                                ), "import_device");
2154
                                                        }
2155
                                                } else {
2156
                                                        $id = array_search(SND_DEVICE . '$$$$$' . $snd["designation"], $import_device);
2157
                                                        unset ($import_device[$id]);
2158
                                                }
2159
                                        }
2160
                                }
2161
                        }
2162
                        break;
2163
        }
2164

    
2165
        // Delete Unexisting Items not found in OCS
2166
        if ($do_clean && count($import_device)) {
2167
                foreach ($import_device as $key => $val) {
2168
                        if (!(strpos($val, $device_type . '$$') === false)) {
2169
                                unlink_device_computer($key, $dohistory);
2170
                                deleteInOcsArray($glpi_id, $key, "import_device");
2171
                        }
2172
                }
2173

    
2174
        }
2175
        if ($do_clean && count($import_ip) && $device_type == NETWORK_DEVICE) {
2176
                foreach ($import_ip as $key => $val) {
2177
                        // Disconnect wire
2178
                        removeConnector($key);
2179

    
2180
                        $query2 = "DELETE FROM glpi_networking_ports WHERE ID = '$key'";
2181
                        $DB->query($query2);
2182
                        deleteInOcsArray($glpi_id, $key, "import_ip");
2183
                }
2184
        }
2185
        //Alimentation
2186
        //Carte mere
2187
}
2188

    
2189
/**
2190
 * Add a new device.
2191
 *
2192
 * Add a new device if doesn't exist.
2193
 *
2194
 *@param $device_type integer : device type identifier.
2195
 *@param $dev_array array : device fields.
2196
 *
2197
 *@return integer : device id.
2198
 *
2199
 **/
2200
function ocsAddDevice($device_type, $dev_array) {
2201

    
2202
        global $DB;
2203
        $table = getDeviceTable($device_type);
2204

    
2205
        $query = "SELECT *
2206
                                FROM " . $table . "
2207
                                WHERE designation='" . $dev_array["designation"] . "'";
2208

    
2209
        switch ($table)
2210
        {
2211
                //For network interfaces, check designation AND speed
2212
                case "glpi_device_iface":
2213
                        if (isset($dev_array["SPEED"]))
2214
                                $query.=" AND bandwidth='".$dev_array["SPEED"]."'";
2215
                break;
2216
                default:
2217
                break;
2218
        }
2219

    
2220
        $result = $DB->query($query);
2221
        
2222
        if ($DB->numrows($result) == 0) {
2223
                $dev = new Device($device_type);
2224
                $input = array ();
2225
                foreach ($dev_array as $key => $val) {
2226
                        $input[$key] = $val;
2227
                }
2228
                $input["_from_ocs"] = 1;
2229
                return $dev->add($input);
2230
        } else {
2231
                $line = $DB->fetch_array($result);
2232
                return $line["ID"];
2233
        }
2234

    
2235
}
2236

    
2237
/**
2238
 * Import the devices for a computer
2239
 *
2240
 * 
2241
 *
2242
 *@param $device_type integer : device type 
2243
 *@param $glpi_id integer : glpi computer id.
2244
 *@param $ocs_id integer : ocs computer id (ID).
2245
 *@param $ocs_server_id integer : ocs server id
2246
 *@param $cfg_ocs array : ocs config
2247
 *@param $entity integer : entity of the computer
2248
 *@param $dohistory boolean : log changes ?
2249
 *@param $import_periph array : already imported periph
2250
 *
2251
 *@return Nothing (void).
2252
 *
2253
 **/
2254
function ocsUpdatePeripherals($device_type, $entity, $glpi_id, $ocs_id, $ocs_server_id, $cfg_ocs, $import_periph, $dohistory) {
2255
        global $DB, $DBocs, $LINK_ID_TABLE;
2256

    
2257
        checkOCSconnection($ocs_server_id);
2258

    
2259
        $do_clean = false;
2260
        $connID = 0;
2261
        //Tag for data since 0.70 for the import_monitor array.
2262
        $tagVersionInArray = "_version_070_";
2263

    
2264
        $count_monitor = count($import_periph);
2265
        switch ($device_type) {
2266
                case MONITOR_TYPE :
2267
                        if ($cfg_ocs["import_monitor"]) {
2268

    
2269
                                //Update data in import_monitor array for 0.70
2270
                                if (!in_array($tagVersionInArray, $import_periph)) {
2271
                                        foreach ($import_periph as $key => $val) {
2272
                                                $monitor_tag = $val;
2273
                                                //delete old value                                                                        
2274
                                                deleteInOcsArray($glpi_id, $key, "import_monitor");
2275
                                                //search serial when it exists        
2276
                                                $monitor_serial = "";
2277
                                                $query_monitor_id = "SELECT end1 FROM glpi_connect_wire WHERE ID='$key'";
2278
                                                $result_monitor_id = $DB->query($query_monitor_id);
2279
                                                if ($DB->numrows($result_monitor_id) == 1) {
2280
                                                        //get monitor Id
2281
                                                        $id_monitor = $DB->result($result_monitor_id, 0, "end1");
2282
                                                        $query_monitor_serial = "SELECT serial FROM glpi_monitors WHERE ID = '$id_monitor'";
2283
                                                        $result_monitor_serial = $DB->query($query_monitor_serial);
2284
                                                        //get serial
2285
                                                        if ($DB->numrows($result_monitor_serial) == 1)
2286
                                                                $monitor_serial = $DB->result($result_monitor_serial, 0, "serial");
2287
                                                }
2288
                                                //concat name + serial
2289
                                                $monitor_tag .= $monitor_serial;
2290
                                                //add new value (serial + name when its possible)                                                        
2291
                                                addToOcsArray($glpi_id, array (
2292
                                                        $key => $monitor_tag
2293
                                                ), "import_monitor");
2294

    
2295
                                                //Update the array with the new value of the monitor
2296
                                                $import_periph[$key] = $monitor_tag;
2297
                                        }
2298
                                        //add the tag for the array version's
2299
                                        addToOcsArray($glpi_id, array (
2300
                                                0 => $tagVersionInArray
2301
                                        ), "import_monitor");
2302

    
2303
                                }
2304

    
2305
                                $do_clean = true;
2306
                                $m = new Monitor;
2307

    
2308
                                $query = "SELECT DISTINCT CAPTION, MANUFACTURER, DESCRIPTION, SERIAL, TYPE 
2309
                                                                                                        FROM monitors 
2310
                                                                                                        WHERE HARDWARE_ID = '" . $ocs_id . "'";
2311
                                $result = $DBocs->query($query);
2312
                                $lines=array();
2313
                                $checkserial=true;
2314
                                
2315
                                // First pass - check if all serial present
2316
                                if ($DBocs->numrows($result) > 0) {
2317
                                        while ($line = $DBocs->fetch_array($result)) {
2318
                                                if (empty($line["SERIAL"])) {
2319
                                                        $checkserial=false;
2320
                                                } 
2321
                                                $lines[]=clean_cross_side_scripting_deep(addslashes_deep($line));
2322
                                        }
2323
                                }
2324
                                /* Second pass - import    
2325
                                        1:Global, 
2326
                                        2:Unique, 
2327
                                        3:Unique on serial : Don't synchronize if serial missing
2328
                                */
2329
                                if (count($lines)>0 && ($cfg_ocs["import_monitor"]<=2 || $checkserial)) foreach ($lines as $line) {
2330
                                        $mon = array ();
2331
                                        $mon["name"] = $line["CAPTION"];
2332

    
2333
                                        if (empty ($line["CAPTION"]) && !empty ($line["MANUFACTURER"])) {
2334
                                                $mon["name"] = $line["MANUFACTURER"];
2335
                                        }
2336
                                        if (empty ($line["CAPTION"]) && !empty ($line["TYPE"])) {
2337
                                                if (!empty ($line["MANUFACTURER"])) {
2338
                                                        $mon["name"] .= " ";
2339
                                                }
2340
                                                $mon["name"] .= $line["TYPE"];
2341
                                        }
2342

    
2343
                                        $mon["serial"] = $line["SERIAL"];
2344
                                        $checkMonitor = "";
2345
                                        if (!empty ($mon["serial"])) {
2346
                                                $checkMonitor = $mon["name"];
2347
                                                $checkMonitor .= $mon["serial"];
2348
                                        } else {
2349
                                                $checkMonitor = $mon["name"];
2350
                                        }
2351

    
2352
                                        if (!empty ($mon["name"])) {
2353
                                                $id = array_search($checkMonitor, $import_periph);
2354
                                                if ($id === false) {
2355
                                                        // Clean monitor object
2356
                                                        $m->reset();
2357

    
2358
                                                        $mon["FK_glpi_enterprise"] = externalImportDropdown("glpi_dropdown_manufacturer", $line["MANUFACTURER"]);
2359
                                                        
2360
                                                        if ($cfg_ocs["import_monitor_comments"])
2361
                                                                $mon["comments"] = $line["DESCRIPTION"];
2362
                                                        $id_monitor = 0;
2363

    
2364
                                                        if ($cfg_ocs["import_monitor"] == 1) {
2365
                                                                //Config says : manage monitors as global
2366
                                                                //check if monitors already exists in GLPI
2367
                                                                $mon["is_global"] = 1;
2368
                                                                $query = "SELECT ID FROM glpi_monitors WHERE name = '" . $mon["name"] . "'
2369
                                                                                                                                                        AND is_global = '1' AND FK_entities=" . $entity;
2370
                                                                $result_search = $DB->query($query);
2371
                                                                if ($DB->numrows($result_search) > 0) {
2372
                                                                        //Periph is already in GLPI
2373
                                                                        //Do not import anything just get periph ID for link
2374
                                                                        $id_monitor = $DB->result($result_search, 0, "ID");
2375
                                                                } else {
2376
                                                                        $input = $mon;
2377
                                                                        if ($cfg_ocs["default_state"]>0){
2378
                                                                                $input["state"] = $cfg_ocs["default_state"];
2379
                                                                        }
2380
                                                                        $input["FK_entities"] = $entity;
2381
                                                                        $input["_from_ocs"] = 1;
2382
                                                                        $id_monitor = $m->add($input);
2383
                                                                }
2384
                                                        } else if ($cfg_ocs["import_monitor"] >= 2) {
2385
                                                                //COnfig says : manage monitors as single units
2386
                                                                //Import all monitors as non global.
2387
                                                                $mon["is_global"] = 0;
2388

    
2389
                                                                // Try to find a monitor with the same serial.
2390
                                                                if (!empty ($mon["serial"])) {
2391
                                                                        $query = "SELECT ID FROM glpi_monitors WHERE serial LIKE '%" . $mon["serial"] . "%' AND is_global=0 AND FK_entities='" . $entity."'";
2392
                                                                        $result_search = $DB->query($query);
2393
                                                                        if ($DB->numrows($result_search) == 1) {
2394
                                                                                //Monitor founded                                                                                                
2395
                                                                                $id_monitor = $DB->result($result_search, 0, "ID");
2396
                                                                        }
2397
                                                                }
2398
                                                                //Search by serial failed, search by name
2399
                                                                if ($cfg_ocs["import_monitor"]==2 && !$id_monitor) {
2400
                                                                        //Try to find a monitor with no serial, the same name and not already connected.
2401
                                                                        if (!empty ($mon["name"])) {
2402
                                                                                $query = "SELECT glpi_monitors.ID FROM glpi_monitors " .
2403
                                                                                                "LEFT JOIN glpi_connect_wire ON (glpi_connect_wire.type=".MONITOR_TYPE." AND glpi_connect_wire.end1=glpi_monitors.ID) " .
2404
                                                                                                "WHERE serial='' AND name = '" . $mon["name"] . "' AND is_global=0 AND FK_entities='$entity' AND glpi_connect_wire.end2 IS NULL";
2405
                                                                                $result_search = $DB->query($query);
2406
                                                                                if ($DB->numrows($result_search) == 1) {
2407
                                                                                        $id_monitor = $DB->result($result_search, 0, "ID");
2408
                                                                                }
2409
                                                                        }
2410
                                                                }
2411
                                                                if (!$id_monitor) {
2412
                                                                        $input = $mon;
2413
                                                                        if ($cfg_ocs["default_state"]>0){
2414
                                                                                $input["state"] = $cfg_ocs["default_state"];
2415
                                                                        }
2416
                                                                        $input["FK_entities"] = $entity;
2417
                                                                        $input["_from_ocs"] = 1;
2418
                                                                        $id_monitor = $m->add($input);
2419
                                                                }
2420
                                                        } // ($cfg_ocs["import_monitor"] >= 2)
2421
                                                        
2422
                                                        if ($id_monitor) {
2423
                                                                //Import unique : Disconnect monitor on other computer done in Connect function
2424
                                                                $connID = Connect($id_monitor, $glpi_id, MONITOR_TYPE, $dohistory);
2425

    
2426
                                                                if (!in_array($tagVersionInArray, $import_periph)) {
2427
                                                                        addToOcsArray($glpi_id, array (0 => $tagVersionInArray), "import_monitor");
2428
                                                                }
2429
                                                                addToOcsArray($glpi_id, array ($connID => $checkMonitor), "import_monitor");
2430
                                                                $count_monitor++;
2431

    
2432
                                                                 //Update column "deleted" set value to 0 and set status to default
2433
                                                                $input = array ();
2434
                                                                
2435
                                                                $old = new Monitor;
2436
                                                                if ($old->getFromDB($id_monitor)) {
2437
                                                                        if ($old->fields["deleted"]) {
2438
                                                                                $input["deleted"] = 0;
2439
                                                                        }                
2440
                                                                        if ($cfg_ocs["default_state"]>0 && $old->fields["state"]!=$cfg_ocs["default_state"]) {
2441
                                                                                $input["state"] = $cfg_ocs["default_state"];
2442
                                                                        }
2443
                                                                        if (empty($old->fields["name"]) && !empty($mon["name"])) {
2444
                                                                                $input["name"] = $mon["name"];
2445
                                                                        }
2446
                                                                        if (empty($old->fields["serial"]) && !empty($mon["serial"])) {
2447
                                                                                $input["serial"] = $mon["serial"];
2448
                                                                        }
2449
                                                                        if (count($input)) {
2450
                                                                                $input["ID"] = $id_monitor;
2451
                                                                                $input["_from_ocs"] = 1;
2452
                                                                                $m->update($input);
2453
                                                                        }                                                                        
2454
                                                                }
2455
                                                        } 
2456
                                                } else { // found in array 
2457
                                                        unset ($import_periph[$id]);
2458
                                                }
2459
                                        } // empty name
2460
                                } // while fetch
2461
                                if (in_array($tagVersionInArray, $import_periph)) {
2462
                                        //unset the version Tag
2463
                                        unset ($import_periph[0]);
2464
                                }
2465
                        }
2466
                        break;
2467
                case PRINTER_TYPE :
2468
                        if ($cfg_ocs["import_printer"]) {
2469
                                $do_clean = true;
2470

    
2471
                                $query = "SELECT * 
2472
                                                                                                        FROM printers 
2473
                                                                                                        WHERE HARDWARE_ID = '" . $ocs_id . "'";
2474
                                $result = $DBocs->query($query);
2475
                                $p = new Printer;
2476

    
2477
                                if ($DBocs->numrows($result) > 0)
2478
                                        while ($line = $DBocs->fetch_array($result)) {
2479
                                                $line = clean_cross_side_scripting_deep(addslashes_deep($line));
2480
                                                // TO TEST : PARSE NAME to have real name.
2481
                                                $print["name"] = $line["NAME"];
2482
                                                if (empty ($print["name"]))
2483
                                                        $print["name"] = $line["DRIVER"];
2484

    
2485
                                                if (!empty ($print["name"]))
2486
                                                        if (!in_array($print["name"], $import_periph)) {
2487
                                                                // Clean printer object
2488
                                                                $p->reset();
2489

    
2490
                                                                //$print["comments"] = $line["PORT"]."\r\n".$line["NAME"];
2491
                                                                $print["comments"] = $line["PORT"] . "\r\n" . $line["DRIVER"];
2492
                                                                $id_printer = 0;
2493

    
2494
                                                                if ($cfg_ocs["import_printer"] == 1) {
2495
                                                                        //Config says : manage printers as global
2496
                                                                        //check if printers already exists in GLPI
2497
                                                                        $print["is_global"] = 1;
2498
                                                                        $query = "SELECT ID 
2499
                                                                                                                                                                                                                                FROM glpi_printers 
2500
                                                                                                                                                                                                                                WHERE name = '" . $print["name"] . "' 
2501
                                                                                                                                                                                                                                AND is_global = '1' AND FK_entities='" . $entity."'";
2502
                                                                        $result_search = $DB->query($query);
2503
                                                                        if ($DB->numrows($result_search) > 0) {
2504
                                                                                //Periph is already in GLPI
2505
                                                                                //Do not import anything just get periph ID for link
2506
                                                                                $id_printer = $DB->result($result_search, 0, "ID");
2507
                                                                        } else {
2508
                                                                                $input = $print;
2509
                                                                                if ($cfg_ocs["default_state"]>0){
2510
                                                                                        $input["state"] = $cfg_ocs["default_state"];
2511
                                                                                }
2512
                                                                                $input["FK_entities"] = $entity;
2513
                                                                                $input["_from_ocs"] = 1;
2514
                                                                                $id_printer = $p->add($input);
2515
                                                                        }
2516
                                                                } else
2517
                                                                        if ($cfg_ocs["import_printer"] == 2) {
2518
                                                                                //COnfig says : manage printers as single units
2519
                                                                                //Import all printers as non global.
2520
                                                                                $input = $print;
2521
                                                                                $input["is_global"] = 0;
2522
                                                                                if ($cfg_ocs["default_state"]>0){
2523
                                                                                        $input["state"] = $cfg_ocs["default_state"];
2524
                                                                                }
2525
                                                                                $input["FK_entities"] = $entity;
2526
                                                                                $input["_from_ocs"] = 1;
2527
                                                                                $id_printer = $p->add($input);
2528
                                                                        }
2529
                                                                if ($id_printer) {
2530
                                                                        $connID = Connect($id_printer, $glpi_id, PRINTER_TYPE, $dohistory);
2531
                                                                        addToOcsArray($glpi_id, array (
2532
                                                                                $connID => $print["name"]
2533
                                                                        ), "import_printers");
2534
                                                                        //Update column "deleted" set value to 0 and set status to default
2535
                                                                        $input = array ();
2536
                                                                        $input["ID"] = $id_printer;
2537
                                                                        $input["deleted"] = 0;
2538
                                                                        if ($cfg_ocs["default_state"]>0){
2539
                                                                                $input["state"] = $cfg_ocs["default_state"];
2540
                                                                        }
2541
                                                                        $input["_from_ocs"] = 1;
2542
                                                                        $p->update($input);
2543
                                                                }
2544
                                                        } else {
2545
                                                                $id = array_search($print["name"], $import_periph);
2546
                                                                unset ($import_periph[$id]);
2547
                                                        }
2548
                                        }
2549
                        }
2550
                        break;
2551
                case PERIPHERAL_TYPE :
2552
                        if ($cfg_ocs["import_periph"]) {
2553
                                $do_clean = true;
2554
                                $p = new Peripheral;
2555

    
2556
                                $query = "SELECT DISTINCT CAPTION, MANUFACTURER, INTERFACE, TYPE 
2557
                                                                                                        FROM inputs 
2558
                                                                                                        WHERE HARDWARE_ID = '" . $ocs_id . "' 
2559
                                                                                                        AND CAPTION <> ''";
2560
                                $result = $DBocs->query($query);
2561
                                if ($DBocs->numrows($result) > 0)
2562
                                        while ($line = $DBocs->fetch_array($result)) {
2563
                                                $line = clean_cross_side_scripting_deep(addslashes_deep($line));
2564

    
2565
                                                $periph["name"] = $line["CAPTION"];
2566
                                                if (!in_array($periph["name"], $import_periph)) {
2567
                                                        // Clean peripheral object
2568
                                                        $p->reset();
2569

    
2570
                                                        if ($line["MANUFACTURER"] != "NULL")
2571
                                                                $periph["brand"] = $line["MANUFACTURER"];
2572
                                                        if ($line["INTERFACE"] != "NULL")
2573
                                                                $periph["comments"] = $line["INTERFACE"];
2574
                                                        $periph["type"] = externalImportDropdown("glpi_type_peripherals", $line["TYPE"]);
2575

    
2576
                                                        $id_periph = 0;
2577

    
2578
                                                        if ($cfg_ocs["import_periph"] == 1) {
2579
                                                                //Config says : manage peripherals as global
2580
                                                                //check if peripherals already exists in GLPI
2581
                                                                $periph["is_global"] = 1;
2582
                                                                $query = "SELECT ID 
2583
                                                                                                                                                                                                        FROM glpi_peripherals 
2584
                                                                                                                                                                                                        WHERE name = '" . $periph["name"] . "' 
2585
                                                                                                                                                                                                        AND is_global = '1' AND FK_entities='" . $entity."'";
2586
                                                                $result_search = $DB->query($query);
2587
                                                                if ($DB->numrows($result_search) > 0) {
2588
                                                                        //Periph is already in GLPI
2589
                                                                        //Do not import anything just get periph ID for link
2590
                                                                        $id_periph = $DB->result($result_search, 0, "ID");
2591
                                                                } else {
2592
                                                                        $input = $periph;
2593
                                                                        if ($cfg_ocs["default_state"]>0){
2594
                                                                                $input["state"] = $cfg_ocs["default_state"];
2595
                                                                        }
2596
                                                                        $input["FK_entities"] = $entity;
2597
                                                                        $input["_from_ocs"] = 1;
2598
                                                                        $id_periph = $p->add($input);
2599
                                                                }
2600
                                                        } else
2601
                                                                if ($cfg_ocs["import_periph"] == 2) {
2602
                                                                        //COnfig says : manage peripherals as single units
2603
                                                                        //Import all peripherals as non global.
2604
                                                                        $input = $periph;
2605
                                                                        $input["is_global"] = 0;
2606
                                                                        if ($cfg_ocs["default_state"]>0){
2607
                                                                                $input["state"] = $cfg_ocs["default_state"];
2608
                                                                        }
2609
                                                                        $input["FK_entities"] = $entity;
2610
                                                                        $input["_from_ocs"] = 1;
2611
                                                                        $id_periph = $p->add($input);
2612
                                                                }
2613
                                                        if ($id_periph) {
2614
                                                                $connID = Connect($id_periph, $glpi_id, PERIPHERAL_TYPE, $dohistory);
2615
                                                                addToOcsArray($glpi_id, array (
2616
                                                                        $connID => $periph["name"]
2617
                                                                ), "import_peripheral");
2618
                                                                //Update column "deleted" set value to 0 and set status to default
2619
                                                                $input = array ();
2620
                                                                $input["ID"] = $id_periph;
2621
                                                                $input["deleted"] = 0;
2622
                                                                if ($cfg_ocs["default_state"]>0){
2623
                                                                        $input["state"] = $cfg_ocs["default_state"];
2624
                                                                }
2625
                                                                $input["_from_ocs"] = 1;
2626
                                                                $p->update($input);
2627
                                                        }
2628
                                                } else {
2629
                                                        $id = array_search($periph["name"], $import_periph);
2630
                                                        unset ($import_periph[$id]);
2631
                                                }
2632
                                        }
2633
                        }
2634
                        break;
2635
        }
2636

    
2637
        // Disconnect Unexisting Items not found in OCS
2638
        if ($do_clean && count($import_periph)) {
2639
                foreach ($import_periph as $key => $val) {
2640

    
2641
                        Disconnect($key, $dohistory, $ocs_server_id);
2642

    
2643
                        switch ($device_type) {
2644
                                case MONITOR_TYPE :
2645
                                        // Only if sync done
2646
                                        if ($cfg_ocs["import_monitor"]<=2 || $checkserial) {
2647
                                                deleteInOcsArray($glpi_id, $key, "import_monitor");
2648
                                        }
2649
                                        break;
2650
                                case PRINTER_TYPE :
2651
                                        deleteInOcsArray($glpi_id, $key, "import_printers");
2652
                                        break;
2653
                                case PERIPHERAL_TYPE :
2654
                                        deleteInOcsArray($glpi_id, $key, "import_peripheral");
2655
                                        break;
2656
                        }
2657
                }
2658
        }
2659

    
2660
}
2661
/**
2662
 * Update the administrative informations
2663
 *
2664
 * This function erase old data and import the new ones about administrative informations
2665
 *
2666
 *
2667
 *@param $glpi_id integer : glpi computer id.
2668
 *@param $ocs_id integer : ocs computer id (ID).
2669
 *@param $ocs_server_id integer : ocs server id
2670
 *@param $computer_updates array : already updated fields of the computer
2671
 *@param $entity integer : entity of the computer
2672
 *@param $dohistory boolean : log changes ?
2673
 *@param $cfg_ocs array : configuration ocs of the server
2674
 
2675
 *@return Nothing (void).
2676
 *
2677
 **/
2678
function ocsUpdateAdministrativeInfo($glpi_id, $ocs_id, $ocs_server_id, $cfg_ocs, $computer_updates, $entity, $dohistory) {
2679
        global $DB, $DBocs;
2680
        checkOCSconnection($ocs_server_id);
2681

    
2682
        //check link between ocs and glpi column
2683
        $queryListUpdate = "SELECT * from glpi_ocs_admin_link where ocs_server_id='$ocs_server_id' ";
2684
        $result = $DB->query($queryListUpdate);
2685
        if ($DB->numrows($result) > 0) {
2686
                $queryOCS = "SELECT * from accountinfo where HARDWARE_ID='$ocs_id'";
2687
                $resultOCS = $DBocs->query($queryOCS);
2688
                if ($DBocs->numrows($resultOCS) > 0) {
2689
                        $data_ocs = $DBocs->fetch_array($resultOCS);
2690
                        $comp = new Computer();
2691

    
2692
                        //update data 
2693
                        while ($links_glpi_ocs = $DB->fetch_array($result)) {
2694
                                //get info from ocs
2695
                                $ocs_column = $links_glpi_ocs['ocs_column'];
2696
                                $glpi_column = $links_glpi_ocs['glpi_column'];
2697
                                if (isset ($data_ocs[$ocs_column]) && !in_array($glpi_column, $computer_updates)) {
2698
                                        $var = $data_ocs[$ocs_column];
2699
                                        switch ($glpi_column) {
2700
                                                case "FK_groups" :
2701
                                                        $var = ocsImportGroup($var, $entity);
2702
                                                        break;
2703
                                                case "location" :
2704
                                                        $var = externalImportDropdown("glpi_dropdown_locations", $var, $entity);
2705
                                                        break;
2706
                                                case "network" :
2707
                                                        $var = externalImportDropdown("glpi_dropdown_network", $var);
2708
                                                        break;
2709
                                        }
2710
                                        $input = array ();
2711
                                        $input[$glpi_column] = $var;
2712
                                        $input["ID"] = $glpi_id;
2713
                                        $input["_from_ocs"] = 1;
2714
                                        $comp->update($input, $dohistory);
2715
                                }
2716
                        }
2717
                }
2718
        }
2719
        //if column in OCS has been deleted, we delete the rules in GLPI
2720
        /*                        else{
2721
                                        $queryDelete ="DELETE from glpi_ocs_admin_link where ocs_server_id='$ocs_server_id' and ocs_column='$ocs_column'";
2722
                                        $DB->query($queryDelete);  
2723
                                }*/
2724

    
2725
}
2726

    
2727
/**
2728
 * Update config of the registry
2729
 *
2730
 * This function erase old data and import the new ones about registry (Microsoft OS after Windows 95)
2731
 *
2732
 *
2733
 *@param $glpi_id integer : glpi computer id.
2734
 *@param $ocs_id integer : ocs computer id (ID).
2735
 *@param $ocs_server_id integer : ocs server id
2736
 *@param $cfg_ocs array : ocs config
2737
 *@return Nothing (void).
2738
 *
2739
 **/
2740
function ocsUpdateRegistry($glpi_id, $ocs_id, $ocs_server_id, $cfg_ocs) {
2741
        global $DB, $DBocs;
2742

    
2743
        checkOCSconnection($ocs_server_id);
2744

    
2745
        if ($cfg_ocs["import_registry"]) {
2746
                //before update, delete all entries about $glpi_id
2747
                $query_delete = "DELETE from glpi_registry WHERE computer_id='" . $glpi_id . "'";
2748
                $DB->query($query_delete);
2749

    
2750
                //Get data from OCS database
2751
                $query = "SELECT registry.NAME as NAME, registry.REGVALUE as regvalue, registry.HARDWARE_ID as computer_id, regconfig.REGTREE as regtree, regconfig.REGKEY as regkey
2752
                                                        FROM registry LEFT JOIN regconfig ON (registry.NAME = regconfig.NAME)
2753
                                                           WHERE HARDWARE_ID = '" . $ocs_id . "'";
2754
                $result = $DBocs->query($query);
2755
                if ($DBocs->numrows($result) > 0) {
2756
                        $reg = new Registry();
2757

    
2758
                        //update data        
2759
                        while ($data = $DBocs->fetch_array($result)) {
2760
                                $data = clean_cross_side_scripting_deep(addslashes_deep($data));
2761
                                $input = array ();
2762
                                $input["computer_id"] = $glpi_id;
2763
                                $input["registry_hive"] = $data["regtree"];
2764
                                $input["registry_value"] = $data["regvalue"];
2765
                                $input["registry_path"] = $data["regkey"];
2766
                                $input["registry_ocs_name"] = $data["NAME"];
2767
                                $input["_from_ocs"] = 1;
2768
                                $isNewReg = $reg->add($input);
2769
                                unset($reg->fields);
2770
                        }
2771
                }
2772
        }
2773
        return;
2774
}
2775
/**
2776
 * Update config of a new software
2777
 *
2778
 * This function create a new software in GLPI with some general datas.
2779
 *
2780
 *
2781
 *@param $glpi_id integer : glpi computer id.
2782
 *@param $ocs_id integer : ocs computer id (ID).
2783
 *@param $ocs_server_id integer : ocs server id
2784
 *@param $cfg_ocs array : ocs config
2785
 *@param $entity integer : entity of the computer
2786
 *@param $dohistory boolean : log changes ?
2787
 *@param $import_software array : already imported softwares
2788
 *
2789
 *@return Nothing (void).
2790
 *
2791
 **/
2792
function ocsUpdateSoftware($glpi_id, $entity, $ocs_id, $ocs_server_id, $cfg_ocs, $import_software, $dohistory) {
2793
        global $DB, $DBocs, $LANG;
2794

    
2795
        checkOCSconnection($ocs_server_id);
2796

    
2797
        //Tag for data since 0.70 for the import_software array.
2798
        $tagVersionInArray = "_version_070_";
2799

    
2800
        if ($cfg_ocs["import_software"]) {
2801

    
2802
                //------------------------------------------------------------------------------------------------------------------//
2803
                //---- Import_software array is not in the new form ( ID => name+version) -----//
2804
                //----------------------------------------------------------------------------------------------------------------//
2805
                if (!in_array($tagVersionInArray, $import_software)) {
2806
                        
2807
                        //Add the tag of the version at the beginning of the array
2808
                        $softs_array[0] = $tagVersionInArray;
2809
                        
2810
                        //For each element of the table, add instID=>name.version
2811
                        foreach ($import_software as $key => $value)
2812
                        {
2813
                                $query_softs = "SELECT glpi_licenses.version as VERSION 
2814
                                FROM glpi_inst_software, glpi_licenses 
2815
                                WHERE glpi_inst_software.license=glpi_licenses.ID AND glpi_inst_software.cID='".$glpi_id."' AND glpi_inst_software.ID='".$key."'";
2816
                                $result_softs = $DB->query($query_softs);
2817
                                $softs = $DB->fetch_array($result_softs);
2818
                                $softs_array[$key] =  $value . '$$$$$'. $softs["VERSION"];
2819
                        }
2820
                        
2821
                        //Replace in GLPI database the import_software by the new one
2822
                        replaceOcsArray($glpi_id, $softs_array, "import_software");
2823

    
2824
                        //Get import_software from the GLPI db
2825
                        //TODO: don't get import_software in DB, but use the newly contructed one
2826
                        $query = "SELECT import_software 
2827
                                                FROM glpi_ocs_link 
2828
                                                WHERE glpi_id='".$glpi_id."'";
2829
                        $result = $DB->query($query);
2830

    
2831
                        //Reload import_software from DB
2832
                        if ($DB->numrows($result))
2833
                        {
2834
                                $tmp = $DB->fetch_array($result);
2835
                                $import_software = importArrayFromDB($tmp["import_software"]);
2836
                        }
2837
                                        
2838
                }
2839
                
2840
                $import_software_licensetype = $cfg_ocs["import_software_licensetype"];
2841
                $import_software_buy = $cfg_ocs["import_software_buy"];
2842

    
2843
                //---- Get all the softwares for this machine from OCS -----//
2844
                if ($cfg_ocs["use_soft_dict"]){
2845
                        $query2 = "SELECT softwares.NAME AS INITNAME, dico_soft.FORMATTED AS NAME, 
2846
                                                        softwares.VERSION AS VERSION, softwares.PUBLISHER AS PUBLISHER, softwares.COMMENTS AS COMMENTS 
2847
                                                        FROM softwares 
2848
                                                        INNER JOIN dico_soft ON (softwares.NAME = dico_soft.EXTRACTED) 
2849
                                                        WHERE softwares.HARDWARE_ID='$ocs_id'";
2850
                } else {
2851
                        $query2 = "SELECT softwares.NAME AS INITNAME, softwares.NAME AS NAME, 
2852
                                                                softwares.VERSION AS VERSION, softwares.PUBLISHER AS PUBLISHER, softwares.COMMENTS AS COMMENTS
2853
                                                                FROM softwares 
2854
                                                                WHERE softwares.HARDWARE_ID='$ocs_id'";
2855
                }
2856

    
2857
                $result2 = $DBocs->query($query2);
2858
                $to_add_to_ocs_array = array ();
2859
                $soft = new Software;
2860
                
2861
                if ($DBocs->numrows($result2) > 0)
2862
                        while ($data2 = $DBocs->fetch_array($result2)) {
2863
                                $data2 = clean_cross_side_scripting_deep(addslashes_deep($data2));
2864
                                $initname = $data2["INITNAME"];
2865
                                $name = $data2["NAME"];
2866
                                $version = $data2["VERSION"];
2867
                                $manufacturer = processManufacturerName($data2["PUBLISHER"]);
2868
                                
2869
                                $use_glpi_dictionnary = false;
2870
                                if (!$cfg_ocs["use_soft_dict"])
2871
                                {
2872
                                        //Software dictionnary
2873
                                        $rulecollection = new DictionnarySoftwareCollection;
2874
                                        $res_rule = $rulecollection->processAllRules(array("name"=>$name,"manufacturer"=>$manufacturer,"old_version"=>$version), array (), array());
2875
                                        
2876
                                        if (isset($res_rule["name"]))
2877
                                                $modified_name = $res_rule["name"];
2878
                                        else
2879
                                                $modified_name = $name;
2880
                                                
2881
                                        if (isset($res_rule["version"]) && $res_rule["version"]!= '')
2882
                                                $modified_version = $res_rule["version"];
2883
                                        else
2884
                                                $modified_version = $version;
2885
                                }
2886
                                else
2887
                                {
2888
                                        $modified_name = $name;
2889
                                        $modified_version = $version;
2890
                                }
2891
                                
2892
                                //Ignore this software
2893
                                if (!isset($res_rule["_ignore_ocs_import"]) || !$res_rule["_ignore_ocs_import"])
2894
                                {        
2895
        
2896
                                        // Clean software object
2897
                                        $soft->reset();
2898
        
2899
                                        //If name+version not in present for this computer in glpi, add it 
2900
                                        if (!in_array($initname . '$$$$$'. $version, $import_software)) 
2901
                                        {
2902
                                                        //------------------------------------------------------------------------------------------------------------------//
2903
                                                        //---- The software doesn't exists in this version for this computer -----//
2904
                                                        //----------------------------------------------------------------------------------------------------------------//
2905
        
2906
                                                        /*
2907
                                                        //Look for the software by his name in GLPI for a specific entity
2908
                                                        $query_search = "SELECT glpi_software.ID as ID, glpi_software.deleted as deleted  
2909
                                                                                                        FROM glpi_software 
2910
                                                                                                        WHERE name = '" . $modified_name . "' AND is_template='0' AND FK_entities=" . $entity;
2911
                                                        $result_search = $DB->query($query_search);
2912
                                                        if ($DB->numrows($result_search) > 0) {
2913
                                                                //Software already exists for this entity, get his ID
2914
                                                                $data = $DB->fetch_array($result_search);
2915
                                                                $isNewSoft = $data["ID"];
2916
                                                                
2917
                                                                // restore software
2918
                                                                if ($data['deleted']){
2919
                                                                        
2920
                                                                        $s = new Software;
2921
                                                                        $s->restore($data);
2922
                                                                }
2923
                                                                
2924
                                                                
2925
                                                        } else {
2926
                                                                $isNewSoft = 0;
2927
                                                        }
2928
                                                        
2929
                                                        if (!$isNewSoft) {
2930
                                                                
2931
                                                                $input = array ();
2932
                                                                $input["name"] = $modified_name;
2933
                                                                
2934
                                                                if ($cfg_ocs["import_software_comments"])
2935
                                                                        $input["comments"] = $data2["COMMENTS"];
2936
                                                                        
2937
                                                                $input["FK_entities"] = $entity;
2938
                
2939
                                                                if (!empty ($data2["PUBLISHER"])) {
2940
                                                                        $input["FK_glpi_enterprise"] = externalImportDropdown("glpi_dropdown_manufacturer", $manufacturer);
2941
                                                                }
2942
                                                                $input["_from_ocs"] = 1;
2943
                                                                $isNewSoft = $soft->add($input);
2944
                                                        
2945
                                                        }
2946
                                                        */
2947
                                                        
2948
                                                        $isNewSoft= addSoftwareOrRestoreFromTrash($modified_name,$manufacturer,$entity,'',IMPORT_TYPE_OCS);
2949
                                                        //Import license for this software
2950
                                                        $licenseID = ocsImportLicense($isNewSoft, $modified_version,$import_software_licensetype,$import_software_buy);
2951
                
2952
                                                        //Install license for this machine
2953
                                                        $instID = installSoftware($glpi_id, $licenseID, '', $dohistory);
2954
                                                        
2955
                                                        //Add the software to the table of softwares for this computer to add in database
2956
                                                        $to_add_to_ocs_array[$instID] = $initname . '$$$$$'. $version;
2957
        
2958
                                        } else {
2959
                                                $instID = -1;
2960
        
2961
                                                //------------------------------------------------------------------------------------------------------------------//
2962
                                                //--------------------- The software exists in this version for this computer --------------//
2963
                                                //----------------------------------------------------------------------------------------------------------------//
2964
        
2965
                                                //Get the name of the software in GLPI to know if the software's name have already been changed by the OCS dictionnary
2966
                                                $instID = array_search($initname . '$$$$$'. $version, $import_software);
2967
                                                $query_soft = "SELECT glpi_software.ID, glpi_software.name FROM glpi_software, glpi_inst_software, glpi_licenses".
2968
                                                " WHERE glpi_inst_software.ID='".$instID."' AND glpi_inst_software.license=glpi_licenses.ID AND glpi_licenses.sID=glpi_software.ID";
2969

    
2970
                                                $result_soft = $DB->query($query_soft);
2971
                                                $tmpsoft = $DB->fetch_array($result_soft);
2972
                                                $softName = $tmpsoft["name"];
2973
                                                $softID = $tmpsoft["ID"];
2974
                                                $s = new Software;
2975
                                                $input["ID"]=$softID;
2976
                                                $input["_from_ocs"] = 1;
2977
                
2978
                                                //First, get the name of the software into GLPI db IF dictionnary is used
2979
                                                if ($cfg_ocs["use_soft_dict"])
2980
                                                {
2981
                                                                //First use of the OCS dictionnary OR name changed in the dictionnary
2982
                                                                if ($softName != $name)
2983
                                                                {
2984
                                                                        $input["name"]=$name;
2985
                                                                        $s->update($input);
2986
                                                                }
2987
                                                }
2988
                                                // OCS Dictionnary not use anymore : revert to original name
2989
                                                else if ($softName != $modified_name)
2990
                                                {        
2991
                                                        $input["name"] = $modified_name;
2992
                                                        $s->update($input);
2993
                                                }
2994
                                                
2995
                                                unset ($import_software[$instID]);
2996
                                        }
2997
                                }
2998
                        }
2999
                
3000
                //Remove the tag from the import_software array
3001
                unset ($import_software[0]);
3002

    
3003
                //Add all the new softwares
3004
                if (count($to_add_to_ocs_array)) {
3005
                        addToOcsArray($glpi_id, $to_add_to_ocs_array, "import_software");
3006
                }
3007

    
3008
                // Remove softwares not present in OCS
3009
                if (count($import_software)) {
3010

    
3011
                        foreach ($import_software as $key => $val) {
3012

    
3013
                                $query = "SELECT * 
3014
                                                                        FROM glpi_inst_software 
3015
                                                                        WHERE ID = '" . $key . "'";
3016
                                $result = $DB->query($query);
3017
                                if ($DB->numrows($result) > 0) {
3018
                                        if ($data = $DB->fetch_assoc($result)) {
3019
                                                uninstallSoftware($key, $dohistory);
3020

    
3021
                                                $query2 = "SELECT COUNT(*) 
3022
                                                                                                        FROM glpi_inst_software 
3023
                                                                                                        WHERE license = '" . $data['license'] . "'";
3024
                                                $result2 = $DB->query($query2);
3025
                                                if ($DB->result($result2, 0, 0) == 0) {
3026
                                                        $lic = new License;
3027
                                                        $lic->getFromDB($data['license']);
3028
                                                        $query3 = "SELECT COUNT(*) 
3029
                                                                                                                                FROM glpi_licenses 
3030
                                                                                                                                WHERE sID='" . $lic->fields['sID'] . "'";
3031
                                                        $result3 = $DB->query($query3);
3032
                                                        if ($DB->result($result3, 0, 0) == 1) 
3033
                                                                putSoftwareInTrash($lic->fields['sID'],$LANG["ocsng"][54],0);
3034
                                                        
3035
                                                        $lic->delete(array (
3036
                                                                "ID" => $data['license'],
3037
                                                                "_from_ocs" => 1
3038
                                                        ));
3039
                                                }
3040
                                        }
3041
                                }
3042

    
3043
                                deleteInOcsArray($glpi_id, $key, "import_software");
3044
                        }
3045
                }
3046
        }
3047
}
3048

    
3049
/**
3050
 * Import config of a new license
3051
 *
3052
 * This function create a new license in GLPI with some general datas.
3053
 *
3054
 *@param $software : id of a software.
3055
 *@param $version : version of the software
3056
 *@param $serial : default serial (used to identify global and freelicenses).
3057
 *@param $buy : is the license buyed ?
3058
 *
3059
 *@return integer : inserted license id.
3060
 *
3061
 **/
3062
function ocsImportLicense($software, $version, $serial = "global", $buy = "0") {
3063
        global $DB, $LANGOcs;
3064

    
3065
        $query = "SELECT ID 
3066
                                FROM glpi_licenses 
3067
                                WHERE sID = '" . $software . "' 
3068
                                AND version='" . $version . "'
3069
                                AND serial='" . $serial . "' 
3070
                                AND buy='" . $buy . "'"; #TODO serial => type
3071

    
3072
        $result = $DB->query($query);
3073
        if ($DB->numrows($result) > 0) {
3074
                $data = $DB->fetch_array($result);
3075
                $isNewLicc = $data["ID"];
3076
        } else {
3077
                $isNewLicc = 0;
3078
        }
3079
        if (!$isNewLicc) {
3080
                $licc = new License;
3081
                $input["sID"] = $software;
3082
                $input["serial"] = $serial;
3083
                $input["buy"] = $buy;
3084
                $input["version"] = $version;
3085
                $input["oem"] = 0;
3086
                $input["_from_ocs"] = 1;
3087
                $isNewLicc = $licc->add($input);
3088
        }
3089
        return ($isNewLicc);
3090
}
3091

    
3092
/**
3093
 * Delete old licenses
3094
 *
3095
 * Delete all old licenses of a computer.
3096
 *
3097
 *@param $glpi_computer_id integer : glpi computer id.
3098
 *
3099
 *@return nothing.
3100
 *
3101
 **/
3102
function ocsResetLicenses($glpi_computer_id) {
3103
        global $DB;
3104

    
3105
        $query = "SELECT * 
3106
                                FROM glpi_inst_software 
3107
                                WHERE cid = '" . $glpi_computer_id . "'";
3108
        $result = $DB->query($query);
3109
        if ($DB->numrows($result) > 0) {
3110
                while ($data = $DB->fetch_assoc($result)) {
3111
                        $query2 = "SELECT COUNT(*) 
3112
                                                                                FROM glpi_inst_software 
3113
                                                                                WHERE license = '" . $data['license'] . "'";
3114
                        $result2 = $DB->query($query2);
3115
                        if ($DB->result($result2, 0, 0) == 1) {
3116
                                $lic = new License;
3117
                                $lic->getFromDB($data['license']);
3118
                                $query3 = "SELECT COUNT(*) 
3119
                                                                                                        FROM glpi_licenses 
3120
                                                                                                        WHERE sID='" . $lic->fields['sID'] . "'";
3121
                                $result3 = $DB->query($query3);
3122
                                if ($DB->result($result3, 0, 0) == 1) {
3123
                                        $soft = new Software();
3124
                                        $soft->delete(array (
3125
                                                'ID' => $lic->fields['sID'],
3126
                                                "_from_ocs" => 1
3127
                                        ), 1);
3128
                                }
3129
                                $lic->delete(array (
3130
                                        "ID" => $data['license'],
3131
                                        "_from_ocs" => 1
3132
                                ));
3133

    
3134
                        }
3135
                }
3136

    
3137
                $query = "DELETE FROM glpi_inst_software 
3138
                                WHERE cid = '" . $glpi_computer_id . "'";
3139
                $DB->query($query);
3140
        }
3141

    
3142
}
3143

    
3144
/**
3145
 * Delete old devices settings
3146
 *
3147
 * Delete Old device settings.
3148
 *
3149
 *@param $device_type integer : device type identifier.
3150
 *@param $glpi_computer_id integer : glpi computer id.
3151
 *
3152
 *@return nothing.
3153
 *
3154
 **/
3155
function ocsResetDevices($glpi_computer_id, $device_type) {
3156
        global $DB;
3157
        $query = "DELETE FROM glpi_computer_device 
3158
                                WHERE device_type = '" . $device_type . "' 
3159
                                AND FK_computers = '" . $glpi_computer_id . "'";
3160
        $DB->query($query);
3161
}
3162

    
3163
/**
3164
 * Delete old periphs
3165
 *
3166
 * Delete all old periphs for a computer.
3167
 *
3168
 *@param $glpi_computer_id integer : glpi computer id.
3169
 *
3170
 *@return nothing.
3171
 *
3172
 **/
3173
function ocsResetPeriphs($glpi_computer_id) {
3174

    
3175
        global $DB;
3176

    
3177
        $query = "SELECT * 
3178
                                FROM glpi_connect_wire 
3179
                                WHERE end2 = '" . $glpi_computer_id . "' 
3180
                                AND type = '" . PERIPHERAL_TYPE . "'";
3181
        $result = $DB->query($query);
3182
        $per = new Peripheral();
3183
        if ($DB->numrows($result) > 0) {
3184
                while ($data = $DB->fetch_assoc($result)) {
3185

    
3186
                        Disconnect($data['ID'],1,false);
3187

    
3188
                        $query2 = "SELECT COUNT(*) 
3189
                                        FROM glpi_connect_wire 
3190
                                        WHERE end1 = '" . $data['end1'] . "' 
3191
                                        AND type = '" . PERIPHERAL_TYPE . "'";
3192
                        $result2 = $DB->query($query2);
3193
                        if ($DB->result($result2, 0, 0) == 1) {
3194
                                $per->delete(array (
3195
                                        'ID' => $data['end1'],
3196
                                        "_from_ocs" => 1
3197
                                ), 1);
3198
                        }
3199
                        
3200
                }
3201
        }
3202

    
3203
}
3204
/**
3205
 * Delete old monitors
3206
 *
3207
 * Delete all old licenses of a computer.
3208
 *
3209
 *@param $glpi_computer_id integer : glpi computer id.
3210
 *
3211
 *@return nothing.
3212
 *
3213
 **/
3214
function ocsResetMonitors($glpi_computer_id) {
3215

    
3216
        global $DB;
3217
        $query = "SELECT * 
3218
                                FROM glpi_connect_wire 
3219
                                WHERE end2 = '" . $glpi_computer_id . "' 
3220
                                AND type = '" . MONITOR_TYPE . "'";
3221

    
3222
        $result = $DB->query($query);
3223
        $mon = new Monitor();
3224
        if ($DB->numrows($result) > 0) {
3225
                while ($data = $DB->fetch_assoc($result)) {
3226

    
3227
                        Disconnect($data['ID'],1,false);
3228

    
3229
                        $query2 = "SELECT COUNT(*) 
3230
                                        FROM glpi_connect_wire 
3231
                                        WHERE end1 = '" . $data['end1'] . "' 
3232
                                        AND type = '" . MONITOR_TYPE . "'";
3233
                        $result2 = $DB->query($query2);
3234
                        if ($DB->result($result2, 0, 0) == 1) {
3235
                                $mon->delete(array (
3236
                                        'ID' => $data['end1'],
3237
                                        "_from_ocs" => 1
3238
                                ), 1);
3239
                        }
3240
                }
3241
        }
3242
}
3243
/**
3244
 * Delete old printers
3245
 *
3246
 * Delete all old printers of a computer.
3247
 *
3248
 *@param $glpi_computer_id integer : glpi computer id.
3249
 *
3250
 *@return nothing.
3251
 *
3252
 **/
3253
function ocsResetPrinters($glpi_computer_id) {
3254

    
3255
        global $DB;
3256

    
3257
        $query = "SELECT * 
3258
                                FROM glpi_connect_wire 
3259
                                WHERE end2 = '" . $glpi_computer_id . "' 
3260
                                AND type = '" . PRINTER_TYPE . "'";
3261
        $result = $DB->query($query);
3262
        if ($DB->numrows($result) > 0) {
3263
                while ($data = $DB->fetch_assoc($result)) {
3264

    
3265
                        Disconnect($data['ID'],1,false);
3266

    
3267
                        $query2 = "SELECT COUNT(*) 
3268
                                        FROM glpi_connect_wire 
3269
                                        WHERE end1 = '" . $data['end1'] . "' 
3270
                                        AND type = '" . PRINTER_TYPE . "'";
3271
                        $result2 = $DB->query($query2);
3272
                        $printer = new Printer();
3273
                        if ($DB->result($result2, 0, 0) == 1) {
3274
                                $printer->delete(array (
3275
                                        'ID' => $data['end1'],
3276
                                        "_from_ocs" => 1
3277
                                ), 1);
3278
                        }
3279
                }
3280

    
3281
        }
3282
}
3283
/**
3284
 * Delete old registry entries
3285

3286
 *
3287
 *@param $glpi_computer_id integer : glpi computer id.
3288
 *
3289
 *@return nothing.
3290
 *
3291
 **/
3292
function ocsResetRegistry($glpi_computer_id) {
3293

    
3294
        global $DB;
3295

    
3296
        $query = "SELECT * 
3297
                                FROM glpi_registry 
3298
                                WHERE computer_id = '" . $glpi_computer_id . "'";
3299
        $result = $DB->query($query);
3300
        if ($DB->numrows($result) > 0) {
3301
                while ($data = $DB->fetch_assoc($result)) {
3302
                        $query2 = "SELECT COUNT(*) 
3303
                                                                                FROM glpi_registry 
3304
                                                                                WHERE computer_id = '" . $data['computer_id'] . "'";
3305
                        $result2 = $DB->query($query2);
3306
                        $registry = new Registry();
3307
                        if ($DB->result($result2, 0, 0) == 1) {
3308
                                $registry->delete(array (
3309
                                        'ID' => $data['computer_id'],
3310
                                        "_from_ocs" => 1
3311
                                ), 1);
3312
                        }
3313
                }
3314
        }
3315
}
3316
/**
3317
 * Delete old dropdown value
3318
 *
3319
 * Delete all old dropdown value of a computer.
3320
 *
3321
 *@param $glpi_computer_id integer : glpi computer id.
3322
 *@param $field string : string of the computer table
3323
 *@param $table string : dropdown table name
3324
 *
3325
 *@return nothing.
3326
 *
3327
 **/
3328
function ocsResetDropdown($glpi_computer_id, $field, $table) {
3329

    
3330
        global $DB;
3331
        $query = "SELECT `$field` AS VAL 
3332
                                FROM glpi_computers 
3333
                                WHERE ID = '" . $glpi_computer_id . "'";
3334
        $result = $DB->query($query);
3335
        if ($DB->numrows($result) == 1) {
3336
                $value = $DB->result($result, 0, "VAL");
3337
                $query = "SELECT COUNT(*) AS CPT 
3338
                                                        FROM glpi_computers 
3339
                                                        WHERE `$field` = '$value'";
3340
                $result = $DB->query($query);
3341
                if ($DB->result($result, 0, "CPT") == 1) {
3342
                        $query2 = "DELETE FROM `$table` 
3343
                                                                                WHERE ID = '$value'";
3344
                        $DB->query($query2);
3345
                }
3346
        }
3347
}
3348

    
3349
/**
3350
 * Choose an ocs server
3351
 *
3352
 * @return nothing. 
3353
 */
3354
function ocsChooseServer($target) {
3355
        global $DB, $LANG;
3356

    
3357
        $query = "SELECT * FROM glpi_ocs_config WHERE is_template='0' ORDER BY name ASC";
3358
        $result = $DB->query($query);
3359
        if ($DB->numrows($result) > 1) {
3360
                echo "<form action=\"$target\" method=\"get\">";
3361
                echo "<div class='center'>";
3362
                echo "<table class='tab_cadre'>";
3363
                echo "<tr class='tab_bg_2'><th colspan='2'>" . $LANG["ocsng"][26] . "</th></tr>";
3364
                echo "<tr class='tab_bg_2'><td class='center'>" . $LANG["common"][16] . "</td><td class='center'>";
3365
                echo "<select name='ocs_server_id'>";
3366
                while ($ocs = $DB->fetch_array($result))
3367
                        echo "<option value=" . $ocs["ID"] . ">" . $ocs["name"] . "</option>";
3368

    
3369
                echo "</select></td></tr>";
3370
                echo "<tr class='tab_bg_2'><td align='center' colspan=2><input class='submit' type='submit' name='ocs_showservers' value='" . $LANG["buttons"][2] . "'></td></tr>";
3371
                echo "</table></div></form>";
3372

    
3373
        }
3374
        elseif ($DB->numrows($result) == 1) {
3375
                $ocs = $DB->fetch_array($result);
3376
                glpi_header($_SERVER['PHP_SELF'] . "?ocs_server_id=" . $ocs["ID"]);
3377
        } else{
3378
                echo "<form action=\"$target\" method=\"get\">";
3379
                echo "<div class='center'>";
3380
                echo "<table class='tab_cadre'>";
3381
                echo "<tr class='tab_bg_2'><th colspan='2'>" . $LANG["ocsng"][26] . "</th></tr>";
3382
                echo "<tr class='tab_bg_2'><td align='center' colspan=2>" . $LANG["ocsng"][27] . "</td></tr>";
3383
                echo "</table></div></form>";
3384
        }
3385
}
3386

    
3387
/**
3388
 * Get a connection to the OCS server
3389
 * @param $ocs_server_id the ocs server id
3390
 * @return the connexion to the ocs database
3391
 */
3392
function getDBocs($ocs_server_id) {
3393
        return new DBocs($ocs_server_id);
3394
}
3395

    
3396
/**
3397
 * Check if OCS connection is always valid
3398
 * If not, then establish a new connection on the good server
3399
 * 
3400
 * @return nothing.
3401
 */
3402
function checkOCSconnection($ocs_server_id) {
3403
        global $DBocs;
3404

    
3405
        //If $DBocs is not initialized, or if the connection should be on a different ocs server
3406
        // --> reinitialize connection to OCS server 
3407
        if (!$DBocs || $ocs_server_id != $DBocs->getServerID()) {
3408
                $DBocs = getDBocs($ocs_server_id);
3409
        }
3410

    
3411
        return $DBocs->connected;
3412
}
3413
/**
3414
 * Get the ocs server id of a machine, by giving the machine id
3415
 * @param $ID the machine ID
3416
 * @return the ocs server id of the machine 
3417
 */
3418
function getOCSServerByMachineID($ID) {
3419
        global $DB;
3420
        $sql = "SELECT ocs_server_id FROM glpi_ocs_link WHERE glpi_ocs_link.glpi_id='" . $ID . "'";
3421
        $result = $DB->query($sql);
3422
        if ($DB->numrows($result) > 0) {
3423
                $datas = $DB->fetch_array($result);
3424
                return $datas["ocs_server_id"];
3425
        }
3426
        return -1;
3427
}
3428

    
3429
/**
3430
 * Get an Ocs Server name, by giving his ID
3431
 * @return the ocs server name
3432
 */
3433
function getOCSServerNameByID($ID) {
3434
        $ocs_server_id = getOCSServerByMachineID($ID);
3435
        $conf = getOcsConf($ocs_server_id);
3436
        return $conf["name"];
3437
}
3438

    
3439
/**
3440
 * Get a random ocs_server_id 
3441
 * @return an ocs server id
3442
 */
3443
function getRandomOCSServerID() {
3444
        global $DB;
3445
        $sql = "SELECT ID FROM glpi_ocs_config ORDER BY RAND() LIMIT 1";
3446
        $result = $DB->query($sql);
3447
        if ($DB->numrows($result) > 0) {
3448
                $datas = $DB->fetch_array($result);
3449
                return $datas["ID"];
3450
        }
3451
        return -1;
3452
}
3453

    
3454
function getColumnListFromAccountInfoTable($ID, $glpi_column) {
3455
        global $DBocs, $DB;
3456
        $listColumn = "";
3457
        if ($ID != -1) {
3458
                checkOCSconnection($ID);
3459
                if (!$DBocs->error) {
3460
                        $result = $DBocs->query("SHOW COLUMNS FROM accountinfo");
3461
                        if ($DBocs->numrows($result) > 0) {
3462
                                while ($data = $DBocs->fetch_array($result)) {
3463
                                        //get the selected value in glpi if specified
3464
                                        $query = "SELECT ocs_column from glpi_ocs_admin_link where ocs_server_id='" . $ID . "' and glpi_column='" . $glpi_column . "'";
3465
                                        $result_DB = $DB->query($query);
3466
                                        $selected = "";
3467
                                        if ($DB->numrows($result_DB) > 0) {
3468
                                                $data_DB = $DB->fetch_array($result_DB);
3469
                                                $selected = $data_DB["ocs_column"];
3470
                                        }
3471
                                        $ocs_column = $data['Field'];
3472
                                        if (!strcmp($ocs_column, $selected))
3473
                                                $listColumn .= "<option value='$ocs_column' selected>" . $ocs_column . "</option>";
3474
                                        else
3475
                                                $listColumn .= "<option value='$ocs_column'>" . $ocs_column . "</option>";
3476
                                }
3477
                        }
3478
                }
3479
        }
3480
        return $listColumn;
3481
}
3482
/*
3483
function getListState($ocs_server_id) {
3484
        global $DB, $LANG;
3485
        $queryStateSelected = "SELECT deconnection_behavior from glpi_ocs_config WHERE ID='$ocs_server_id'";
3486
        $resultSelected = $DB->query($queryStateSelected);
3487
        $selected = 0;
3488
        if ($DB->numrows($resultSelected) > 0) {
3489
                $res = $DB->fetch_array($resultSelected);
3490
                $selected = $res["deconnection_behavior"];
3491
        }
3492

3493
        $values[''] = "-----";
3494
        $values["trash"] = $LANG["ocsconfig"][49];
3495
        $values["delete"] = $LANG["ocsconfig"][50];
3496

3497
        $queryStateList = "SELECT name from glpi_dropdown_state";
3498
        $result = $DB->query($queryStateList);
3499
        if ($DB->numrows($result) > 0) {
3500
                while (($data = $DB->fetch_array($result)))
3501
                        $values[$data["name"]] = $LANG["ocsconfig"][51] .
3502
                        " " . $data["name"];
3503

3504
        }
3505
        dropdownArrayValues("deconnection_behavior", $values, $selected);
3506
}
3507
*/
3508
function setEntityLock($entity) {
3509
        global $CFG_GLPI;
3510
        $fp = fopen(GLPI_LOCK_DIR . "/lock_entity_" . $entity, "w+");
3511

    
3512
        if (flock($fp, LOCK_EX)) {
3513
                return $fp;
3514
        } else {
3515
                fclose($fp);
3516
                return false;
3517
        }
3518
}
3519

    
3520
function removeEntityLock($entity, $fp) {
3521
        flock($fp, LOCK_UN);
3522
        fclose($fp);
3523

    
3524
        //Test if the lock file still exists before removing it
3525
        // (sometimes another thread already already removed the file)
3526
        clearstatcache();
3527
        if (file_exists(GLPI_LOCK_DIR . "/lock_entity_" . $entity)) {
3528
                @unlink(GLPI_LOCK_DIR . "/lock_entity_" . $entity);
3529
        }
3530
}
3531

    
3532
function getMaterialManagementMode($ocs_config, $device_type) {
3533
        global $LANG;
3534
        switch ($device_type) {
3535
                case MONITOR_TYPE :
3536
                        return $ocs_config["import_monitor"];
3537
                        break;
3538
                case PRINTER_TYPE :
3539
                        return $ocs_config["import_printer"];
3540
                        break;
3541
                case PERIPHERAL_TYPE :
3542
                        return $ocs_config["import_periph"];
3543
                        break;
3544
        }
3545
}
3546

    
3547
/**
3548
 * Get IP address from OCS hardware table
3549
 * @param ocs_server_id the ID of the OCS server
3550
 * @param computer_id ID of the computer in OCS hardware table
3551
 * @return the ip address or ''
3552
 */
3553
function getOcsGeneralIpAddress($ocs_server_id,$computer_id)
3554
{
3555
        global $DBocs;
3556
        $res = $DBocs->query("SELECT IPADDR FROM hardware WHERE ID='".$computer_id."'");
3557
        if ($DBocs->numrows($res) == 1)
3558
                return $DBocs->result($res,0,"IPADDR");
3559
        else
3560
                return '';        
3561
}
3562
?>
Redmine Appliance - Powered by TurnKey Linux