Projet

Général

Profil

Paste
Statistiques
| Branche: | Révision:

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

Historique | Voir | Annoter | Télécharger (51,5 ko)

1
<?php
2

    
3
/*
4
 * @version $Id: software.function.php 7875 2009-01-23 15:16:47Z moyo $
5
 -------------------------------------------------------------------------
6
 GLPI - Gestionnaire Libre de Parc Informatique
7
 Copyright (C) 2003-2009 by the INDEPNET Development Team.
8

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

12
 LICENSE
13

14
 This file is part of GLPI.
15

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

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

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

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

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

    
41
/**
42
 * Show License add div
43
 *
44
 * @param $ID ID of the software
45
 * @return nothing
46
 */
47
function showLicensesAdd($ID) {
48

    
49
        global $CFG_GLPI, $LANG;
50

    
51
        if (!haveRight("software", "w"))
52
                return false;
53

    
54
        echo "<div class='center'>&nbsp;<table class='tab_cadre_fixe' cellpadding='2'>";
55
        echo "<tr><td align='center' class='tab_bg_2'><strong>";
56
        echo "<a href=\"" . $CFG_GLPI["root_doc"] . "/front/software.licenses.php?form=add&amp;sID=$ID\">";
57
        echo $LANG["software"][12];
58
        echo "</a></strong></td></tr>";
59
        echo "</table></div><br>";
60
}
61

    
62
/**
63
 * Show Licenses of a software
64
 *
65
 * @param $sID ID of the software
66
 * @param $show_computers Show computers using the licenses
67
 * @return nothing
68
 */
69
function showLicenses($sID, $show_computers = 0) {
70

    
71
        global $DB, $CFG_GLPI, $LANG;
72

    
73
        if (!haveRight("software", "r"))
74
                return false;
75
        $canedit = haveRight("software", "w");
76
        $canshowcomputer = haveRight("computer", "r");
77
        $ci = new CommonItem();
78
        $query = "SELECT count(*) AS COUNT  FROM glpi_licenses WHERE (sID = '$sID')";
79
        $query_update = "SELECT count(glpi_licenses.ID) AS COUNT  FROM glpi_licenses, glpi_software WHERE (glpi_software.ID = glpi_licenses.sID AND glpi_software.update_software = '$sID' and glpi_software.is_update='1')";
80

    
81
        $found_soft = false;
82
        if ($result = $DB->query($query)) {
83
                if ($DB->result($result, 0, 0) != 0) {
84
                        $nb_licences = $DB->result($result, 0, "COUNT");
85

    
86
                        $result_update = $DB->query($query_update);
87
                        $nb_updates = $DB->result($result_update, 0, "COUNT");
88
                        $installed = getInstalledLicence($sID);
89
                        $tobuy = getLicenceToBuy($sID);
90

    
91
                        $isfreeorglobal = isFreeSoftware($sID) || isGlobalSoftware($sID);
92
                        // As t'on utilisé trop de licences en prenant en compte les mises a jours (double install original + mise �jour)
93
                        // Rien si free software
94
                        $pb = "";
95
                        if (($nb_licences - $nb_updates - $installed) < 0 && !$isfreeorglobal)
96
                                $pb = "class='tab_bg_1_2'";
97

    
98
                        echo "<form id='lic_form' name='lic_form' method='post' action=\"" . $CFG_GLPI["root_doc"] . "/front/software.licenses.php\">";
99

    
100
                        echo "<br><div class='center'><table cellpadding='2' class='tab_cadre_fixe'>";
101
                        echo "<tr>";
102
                        if ($canedit && !$show_computers) {
103
                                echo "<th>&nbsp;</th>";
104
                        }
105

    
106
                        echo "<th colspan='6' $pb >";
107
                        echo $nb_licences;
108
                        echo "&nbsp;" . $LANG["software"][13] . "&nbsp;-&nbsp;$nb_updates&nbsp;" . $LANG["software"][36] . "&nbsp;-&nbsp;$installed&nbsp;" . $LANG["software"][19] . "&nbsp;-&nbsp;$tobuy&nbsp;" . $LANG["software"][37] . "</th>";
109
                        echo "<th colspan='1'>";
110
                        echo " " . $LANG["software"][19] . " :</th></tr>";
111
                        $i = 0;
112
                        echo "<tr>";
113
                        if ($canedit && !$show_computers) {
114
                                echo "<th>&nbsp;</th>";
115
                        }
116

    
117
                        echo "<th>" . $LANG["software"][5] . "</th><th>" . $LANG["common"][19] . "</th><th>" . $LANG["common"][33] . "</th><th>" . $LANG["software"][32] . "</th><th>" . $LANG["software"][28] . "</th><th>" . $LANG["software"][35] . "</th>";
118
                        echo "<th>";
119

    
120
                        if ($canedit) {
121
                                if ($show_computers) {
122
                                        echo $LANG["buttons"][14] . "&nbsp;";
123
                                        echo "<select name='update_licenses' id='update_licenses_choice'>";
124
                                        echo "<option value=''>-----</option>";
125
                                        echo "<option value='update_expire'>" . $LANG["software"][32] . "</option>";
126
                                        echo "<option value='update_buy'>" . $LANG["software"][35] . "</option>";
127
                                        echo "<option value='move'>" . $LANG["buttons"][20] . "</option>";
128
                                        echo "<option value='delete_license'>" . $LANG["buttons"][6] . "</option>";
129
                                        echo "</select>";
130

    
131
                                        $params = array (
132
                                                'type' => '__VALUE__',
133
                                                'sID' => $sID,
134
                                                
135
                                        );
136
                                        ajaxUpdateItemOnSelectEvent("update_licenses_choice", "update_licenses_view", $CFG_GLPI["root_doc"] . "/ajax/updateLicenses.php", $params, false);
137

    
138
                                        echo "<span id='update_licenses_view'>\n";
139
                                        echo "&nbsp;";
140
                                        echo "</span>\n";
141
                                } else {
142
                                        echo $LANG["buttons"][14] . "&nbsp;";
143
                                        echo "<select name='update_licenses' id='update_licenses_choice'>";
144
                                        echo "<option value=''>-----</option>";
145
                                        echo "<option value='move_to_software'>" . $LANG["buttons"][20] . "</option>";
146
                                        echo "<option value='delete_similar_license'>" . $LANG["buttons"][6] . "</option>";
147
                                        echo "</select>";
148

    
149
                                        $params = array (
150
                                                'type' => '__VALUE__',
151
                                                'sID' => $sID,
152
                                                
153
                                        );
154
                                        ajaxUpdateItemOnSelectEvent("update_licenses_choice", "update_licenses_view", $CFG_GLPI["root_doc"] . "/ajax/updateLicenses.php", $params, false);
155

    
156
                                }
157
                                echo "<span id='update_licenses_view'>\n";
158
                                echo "&nbsp;";
159
                                echo "</span>\n";
160

    
161
                        } else
162
                                echo "&nbsp;";
163

    
164
                        echo "</th></tr>";
165
                } else {
166

    
167
                        echo "<br><div class='center'><table border='0' width='50%' cellpadding='2'>";
168
                        echo "<tr><th>" . $LANG["software"][14] . "</th></tr>";
169
                        echo "</table></div>";
170
                }
171
        }
172

    
173
        $query = "SELECT count(ID) AS COUNT, version as VERSION, serial as SERIAL, expire as EXPIRE, oem as OEM, oem_computer as OEM_COMPUTER, buy as BUY, ID AS ID  FROM glpi_licenses WHERE (sID = '$sID') GROUP BY version, serial, expire, oem, oem_computer, buy ORDER BY version, serial,oem, oem_computer";
174
        //echo $query;
175
        if ($result = $DB->query($query)) {
176
                while ($data = $DB->fetch_array($result)) {
177
                        $version = $data["VERSION"];
178
                        $serial = $data["SERIAL"];
179
                        $expire = $data["EXPIRE"];
180
                        $oem = $data["OEM"];
181
                        $oem_computer = $data["OEM_COMPUTER"];
182
                        $buy = $data["BUY"];
183

    
184
                        $SEARCH_LICENCE = "(glpi_licenses.sID = $sID AND glpi_licenses.serial = '" . $serial . "'  AND glpi_licenses.oem = '$oem' AND glpi_licenses.oem_computer = '$oem_computer'  AND glpi_licenses.buy = '$buy' ";
185
                        if ($expire == "")
186
                                $SEARCH_LICENCE .= " AND glpi_licenses.expire IS NULL";
187
                        else
188
                                $SEARCH_LICENCE .= " AND glpi_licenses.expire = '$expire'";
189

    
190
                        if ($version == "" || is_null($version))
191
                                $SEARCH_LICENCE .= " AND (glpi_licenses.version='' OR glpi_licenses.version IS NULL))";
192
                        else
193
                                $SEARCH_LICENCE .= " AND glpi_licenses.version = '$version')";
194

    
195
                        $today = date("Y-m-d");
196
                        $expirer = 0;
197
                        $expirecss = "";
198
                        if ($expire != NULL && $today > $expire) {
199
                                $expirer = 1;
200
                                $expirecss = "_2";
201
                        }
202
                        // Get installed licences
203

    
204
                        $query_lic = "SELECT glpi_inst_software.ID AS ID, glpi_licenses.ID AS lID, ";
205
                        $query_lic .= " glpi_infocoms.ID as infocoms, glpi_licenses.comments AS COMMENT, ";
206
                        $query_lic .= " glpi_inst_software.cID AS cID, glpi_computers.name AS cname FROM glpi_licenses";
207
                        $query_lic .= " LEFT JOIN glpi_inst_software ";
208
                        $query_lic .= " ON ( glpi_inst_software.license = glpi_licenses.ID )";
209
                        $query_lic .= " LEFT JOIN glpi_computers ON (glpi_inst_software.cID= glpi_computers.ID) ";
210
                        $query_lic .= " LEFT JOIN glpi_infocoms ON (glpi_infocoms.device_type='" . LICENSE_TYPE . "' AND glpi_infocoms.FK_device=glpi_licenses.ID) ";
211
                        $query_lic .= " WHERE $SEARCH_LICENCE ";
212
                        $query_lic .= "AND (glpi_computers.ID IS NULL OR (glpi_computers.deleted='0' AND glpi_computers.is_template='0'))";
213
                        $query_lic .= "ORDER BY cname";
214

    
215
                        $result_lic = $DB->query($query_lic);
216
                        $num_tot = $DB->numrows($result_lic);
217

    
218
                        $num_inst = 0;
219
                        $firstID = 0;
220
                        $freeID = 0;
221

    
222
                        if ($num_tot > 0 ){
223
                                while ($data_lic = $DB->fetch_array($result_lic)) {
224
                                        if ($firstID == 0) {
225
                                                $firstID = $data_lic['lID'];
226
                                        }
227
                                        if ($data_lic['cID'] > 0) {
228
                                                $num_inst++;
229
                                        } else {
230
                                                $freeID = $data_lic['lID'];
231
                                        }
232
                                }
233
        
234
                                $DB->data_seek($result_lic, 0);
235
                        }
236

    
237
                        $restant = $num_tot - $num_inst;
238

    
239
                        echo "<tr class='tab_bg_1' valign='top'>";
240
                        if ($canedit && !$show_computers) {
241
                                $found_soft = true;
242
                                echo "<td><input type='checkbox' name='license_" . $data['ID'] . "'></td>";
243
                        }
244
                        echo "<td class='center'><strong>" . $version . "</strong></td>";
245
                        echo "<td class='center'><strong>" . $serial . "</strong></td>";
246
                        echo "<td class='center'><strong>";
247
                        echo $num_tot;
248
                        echo "</strong></td>";
249

    
250
                        echo "<td align='center' class='tab_bg_1$expirecss'><strong>";
251
                        if ($expire == NULL)
252
                                echo $LANG["software"][26];
253
                        else {
254
                                if ($expirer)
255
                                        echo $LANG["software"][27];
256
                                else
257
                                        echo $LANG["software"][25] . "&nbsp;" . convDate($expire);
258
                        }
259

    
260
                        echo "</strong></td>";
261
                        // OEM
262
                        if ($data["OEM"]) {
263
                                $comp = new Computer();
264
                                $comp->getFromDB($data["OEM_COMPUTER"]);
265
                        }
266
                        echo "<td align='center' class='tab_bg_1" . ($data["OEM"] && !isset ($comp->fields['ID']) ? "_2" : "") . "'>" . ($data["OEM"] ? $LANG["choice"][1] : $LANG["choice"][0]);
267
                        if ($data["OEM"]) {
268
                                echo "<br><strong>";
269
                                if (isset ($comp->fields['ID']))
270
                                        echo "<a href='" . $CFG_GLPI["root_doc"] . "/front/computer.form.php?ID=" . $comp->fields['ID'] . "'>" . $comp->fields['name'] . "</a>";
271
                                else
272
                                        echo "N/A";
273
                                echo "<strong>";
274
                        }
275
                        echo "</td>";
276

    
277
                        if ($serial != "free") {
278
                                // BUY
279
                                echo "<td class='center'>" . ($data["BUY"] ? $LANG["choice"][1] : $LANG["choice"][0]);
280
                                echo "</td>";
281
                        } else
282
                                echo "<td>&nbsp;</td>";
283

    
284
                        echo "<td class='center'>";
285

    
286
                        // Logiciels install� :
287
                        echo "<table width='100%'>";
288

    
289
                        // Restant        
290

    
291
                        echo "<tr><td class='center'>";
292

    
293
                        if (!$show_computers) {
294
                                echo $LANG["software"][19] . ": $num_inst&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
295
                        }
296

    
297
                        //                        $query_new="SELECT glpi_licenses.ID as ID FROM glpi_licenses WHERE $SEARCH_LICENCE";        
298
                        //echo $query_new;        
299
                        //                        if ($result_new = $DB->query($result_lic)) 
300

    
301
                        if ($firstID && $serial != "free" && $serial != "global" && $canedit) {
302
                                echo $LANG["software"][20] . ":";
303
                                echo "<select name='stock_licenses_$firstID'>";
304
                                if (max(0, $restant -100) > 0)
305
                                        echo "<option value='0'>0</option>";
306
                                for ($i = max(0, $restant -100); $i <= $restant +100; $i++)
307
                                        echo "<option value='$i' " . ($i == $restant ? " selected " : "") . ">$i</option>";
308
                                echo "</select>";
309
                                echo "<input type='hidden' name='nb_licenses_$firstID' value='$restant'>";
310
                                echo "<input type='image' name='update_stock_licenses' value='$firstID' src='" . $CFG_GLPI["root_doc"] . "/pics/actualiser.png' class='calendrier'>";
311
                        }
312
                        if (($serial == "free" || $serial == "global")) {
313
                                // Display infocoms
314
                                echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>";
315
                                showDisplayInfocomLink(LICENSE_TYPE, $firstID, 1);
316
                                echo "</strong>";
317
                        }
318

    
319
                        if ($restant > 0 || $serial == "free" || $serial == "global") {
320
                                if ($firstID > 0) {
321
                                        echo "</td><td class='center'>";
322
                                        if ($canedit) {
323
                                                if (($serial == "free" || $serial == "global")) {
324
                                                        echo "<strong><a href=\"" . $CFG_GLPI["root_doc"] . "/front/software.licenses.php?delete=delete&amp;ID=$firstID\">";
325
                                                        echo "<img src=\"" . $CFG_GLPI["root_doc"] . "/pics/delete.png\" alt='" . $LANG["buttons"][6] . "' title='" . $LANG["buttons"][6] . "'>";
326
                                                        echo "</a></strong>";
327
                                                        if ($CFG_GLPI["license_deglobalisation"]) {
328
                                                                echo "&nbsp;&nbsp;<a href=\"javascript:confirmAction('" . addslashes($LANG["common"][40]) . "\\n" . addslashes($LANG["common"][39]) . "','" . $CFG_GLPI["root_doc"] . "/front/software.licenses.php?unglobalize=unglobalize&amp;sID=$sID&amp;ID=$firstID')\" title=\"" . $LANG["common"][39] . "\">" . $LANG["common"][38] . "</a>&nbsp;";
329
                                                                echo "<img src='" . $CFG_GLPI["root_doc"] . "/pics/aide.png' alt=\"" . $LANG["common"][39] . "\" title=\"" . $LANG["common"][39] . "\">";
330
                                                        }
331
                                                }
332
                                                echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong><a href=\"" . $CFG_GLPI["root_doc"] . "/front/software.licenses.php?form=update&amp;lID=$firstID&amp;sID=$sID\">";
333
                                                echo "<img src=\"" . $CFG_GLPI["root_doc"] . "/pics/edit.png\" alt='" . $LANG["buttons"][14] . "' title='" . $LANG["buttons"][14] . "'>";
334
                                                echo "</a></strong>";
335
                                        } else {
336
                                                echo "&nbsp;";
337
                                        }
338
                                }
339
                        }
340

    
341
                        // Add select all checkbox
342
                        if ($show_computers && $canedit) {
343
                                if ($num_inst > 0) {
344
                                        if ($serial != "free" && $serial != "global") {
345
                                                echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $LANG["common"][66] . ":";
346
                                                $rand = mt_rand();
347

    
348
                                                $found_soft = true;
349
                                                echo "<input type='checkbox' onclick='toggle$rand();'>";
350
                                                echo "<script type='text/javascript' >\n";
351
                                                echo "function toggle$rand(){\n";
352
                                                while ($data_inst = $DB->fetch_array($result_lic)) {
353
                                                        if ($data_inst['cID'] > 0) {
354
                                                                echo " var lic=window.document.getElementById('license_" . $data_inst["lID"] . "');";
355
                                                                echo " if (lic.checked) \n";
356
                                                                echo "      lic.checked = false;\n";
357
                                                                echo " else lic.checked = true;\n";
358
                                                        }
359
                                                }
360
                                                echo "}</script>\n";
361
                                                $DB->data_seek($result_lic, 0);
362
                                        } else {
363
                                                echo "<input type='checkbox' name='license_" . $data['ID'] . "'>";
364
                                        }
365
                                }
366
                        }
367

    
368
                        echo "</td></tr>";
369

    
370
                        // Logiciels install�
371
                        if ($show_computers) {
372
                                while ($data_inst = $DB->fetch_array($result_lic)) {
373
                                        if ($data_inst['cID'] > 0) {
374
                                                echo "<tr class='tab_bg_1" . (($data["OEM"] && $data["OEM_COMPUTER"] != $data_inst["cID"]) ? "_2" : "") . "'><td class='center'>";
375

    
376
                                                if ($serial != "free" && $serial != "global" && $canedit) {
377
                                                        $found_soft = true;
378
                                                        echo "<input type='checkbox' name='license_" . $data_inst["lID"] . "' id='license_" . $data_inst["lID"] . "'>";
379
                                                }
380
                                                $ci->getFromDB(COMPUTER_TYPE, $data_inst["cID"]);
381

    
382
                                                echo "&nbsp;<strong>";
383
                                                echo $ci->getLink($canshowcomputer);
384
                                                echo "</strong></td><td class='center'>";
385

    
386
                                                // Comment
387
                                                if (!empty ($data_inst["COMMENT"])) {
388
                                                        echo "<img onmouseout=\"cleanhide('comment_" . $data_inst["ID"] . "')\" onmouseover=\"cleandisplay('comment_" . $data_inst["ID"] . "')\" src=\"" . $CFG_GLPI["root_doc"] . "/pics/aide.png\" alt=''>";
389
                                                        echo "<div class='over_link' id='comment_" . $data_inst["ID"] . "'>" . nl2br($data_inst["COMMENT"]) . "</div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
390
                                                }
391
                                                // delete
392
                                                if ($canedit) {
393
                                                        echo "<a href=\"" . $CFG_GLPI["root_doc"] . "/front/software.licenses.php?uninstall=uninstall&amp;ID=" . $data_inst["ID"] . "&amp;cID=" . $data_inst["cID"] . "\">";
394
                                                        echo "<img src=\"" . $CFG_GLPI["root_doc"] . "/pics/remove.png\" alt='" . $LANG["buttons"][5] . "' title='" . $LANG["buttons"][5] . "'>";
395
                                                        echo "</a>";
396
                                                }
397

    
398
                                                if ($serial != "free" && $serial != "global") {
399
                                                        echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
400
                                                        if ($canedit) {
401
                                                                echo "<strong><a href=\"" . $CFG_GLPI["root_doc"] . "/front/software.licenses.php?form=update&amp;lID=" . $data_inst["lID"] . "&amp;sID=$sID\">";
402
                                                                echo "<img src=\"" . $CFG_GLPI["root_doc"] . "/pics/edit.png\" alt='" . $LANG["buttons"][14] . "' title='" . $LANG["buttons"][14] . "'>";
403
                                                                echo "</a></strong>";
404
                                                        }
405
                                                        // Display infocoms
406
                                                        echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>";
407
                                                        showDisplayInfocomLink(LICENSE_TYPE, $data_inst["lID"], 1);
408
                                                        echo "</strong>";
409
                                                }
410

    
411
                                                echo "</td></tr>";
412
                                        }
413
                                }
414
                        }
415
                        echo "</table></td>";
416

    
417
                        echo "</tr>";
418

    
419
                }
420
        }
421
        echo "</table></div>\n\n";
422
        if ($found_soft) {
423
                echo "<div>";
424
                echo "<table width='950px'>";
425
                echo "<tr><td><img src=\"" . $CFG_GLPI["root_doc"] . "/pics/arrow-left.png\" alt=''></td><td><a onclick= \"if ( markAllRows('lic_form') ) return false;\" href='" . $_SERVER['PHP_SELF'] . "?ID=$sID&amp;select=all'>" . $LANG["buttons"][18] . "</a></td>";
426

    
427
                echo "<td>/</td><td ><a onclick=\"if ( unMarkAllRows('lic_form') ) return false;\" href='" . $_SERVER['PHP_SELF'] . "?ID=$sID&amp;select=none'>" . $LANG["buttons"][19] . "</a>";
428
                echo "</td><td class='left' width='80%'>&nbsp;";
429
                echo "</td></table></div>";
430
        }
431

    
432
        echo "</form>";
433
}
434

    
435
/**
436
 * Show Licenses form
437
 *
438
 * @param $target  target of the form
439
 * @param $action Which action to do : add or update 
440
 * @param $sID  ID of the software
441
 * @param $lID ID of the license of update
442
 * @return nothing
443
 */
444
function showLicenseForm($target, $action, $sID, $lID = "") {
445

    
446
        global $CFG_GLPI, $LANG;
447

    
448
        if (!haveRight("software", "w"))
449
                return false;
450

    
451
        $show_infocom = false;
452

    
453
        switch ($action) {
454
                case "add" :
455
                        $title = $LANG["software"][15] . " ($sID):";
456
                        $button = $LANG["buttons"][8];
457
                        $ic = new Infocom();
458

    
459
                        if ($ic->getFromDBforDevice(SOFTWARE_TYPE, $sID))
460
                                $show_infocom = true;
461

    
462
                        break;
463
                case "update" :
464
                        $title = $LANG["software"][34] . " ($lID):";
465
                        $button = $LANG["buttons"][14];
466
                        break;
467
        }
468

    
469
        // Get previous values or defaults values
470
        $values = array ();
471
        // defaults values :
472
        $values['version'] = '';
473
        $values['serial'] = '';
474
        $values['expire'] = "0000-00-00";
475
        $values['oem'] = 0;
476
        $values["oem_computer"] = '';
477
        $values["comments"] = '';
478
        $values['buy'] = 1;
479

    
480
        if (isset ($_POST) && !empty ($_POST)) { // Get from post form
481
                foreach ($values as $key => $val) {
482
                        if (isset ($_POST[$key])) {
483
                                $values[$key] = $_POST[$key];
484
                        }
485
                }
486

    
487
        } else
488
                if (!empty ($lID)) { // Get from DB
489
                        $lic = new License();
490
                        $lic->getFromDB($lID);
491
                        $values = $lic->fields;
492
                }
493

    
494
        $soft=new Software();
495
        $soft->getFromDB($sID);
496

    
497
        if (empty ($values['expire']))
498
                $values['expire'] = "0000-00-00";
499

    
500
        echo "<div class='center'><strong>";
501
        echo "<a href=\"" . $CFG_GLPI["root_doc"] . "/front/software.form.php?ID=$sID\">";
502
        echo $LANG["buttons"][13] . "</strong>";
503
        echo "</a><br>";
504

    
505
        echo "<form name='form' method='post' action=\"$target\">";
506

    
507
        echo "<table class='tab_cadre'><tr><th colspan='3'>$title</th></tr>";
508

    
509
        echo "<tr class='tab_bg_1'><td>" . $LANG["software"][5] . "</td>";
510
        echo "<td>";
511
        autocompletionTextField("version", "glpi_licenses", "version", $values["version"], 20);
512
        echo "</td></tr>";
513

    
514
        echo "<tr class='tab_bg_1'><td>" . $LANG["software"][16] . "</td>";
515
        echo "<td>";
516

    
517
        $readonly = "";
518

    
519
        if ($action == "add") {
520
                switch ($CFG_GLPI["licenses_management_restrict"]) {
521
                        case 2 :
522
                                $readonly = "";
523
                                break;
524
                        case 1 :
525
                                $values["serial"] = "global";
526
                                $readonly = "readonly";
527
                                break;
528
                        case 0 :
529
                                $values["serial"] = "free";
530
                                $readonly = "readonly";
531
                                break;
532
                }
533
        }
534
        elseif ($values["serial"] == "free" || $values["serial"] == "global") $readonly = "readonly";
535

    
536
        autocompletionTextField("serial", "glpi_licenses", "serial", $values["serial"], 20, $readonly);
537
        echo "</td></tr>";
538

    
539
        if ($action != "update") {
540
                echo "<tr class='tab_bg_1'><td>";
541
                echo $LANG["tracking"][29] . ":</td><td><select name=number>";
542
                echo "<option value='1' selected>1</option>";
543
                for ($i = 2; $i <= 1000; $i++)
544
                        echo "<option value='$i'>$i</option>";
545
                echo "</select></td></tr>";
546
        }
547

    
548
        if ($show_infocom) {
549
                echo "<tr class='tab_bg_1'><td>" . $LANG["financial"][3] . ":</td><td>";
550
                showDisplayInfocomLink(SOFTWARE_TYPE, $sID);
551
                echo "</td></tr>";
552
        }
553

    
554
        echo "<tr class='tab_bg_1'><td>" . $LANG["search"][9] . ":</td><td>";
555
        showCalendarForm("form", "expire", $values['expire']);
556
        echo "</td></tr>";
557

    
558
        // OEM
559
        echo "<tr class='tab_bg_1'><td>" . $LANG["software"][28] . "</td><td>";
560
        dropdownYesNo("oem", $values['oem']);
561
        dropdownValue("glpi_computers", "oem_computer", $values["oem_computer"],1,$soft->fields['FK_entities']);
562
        echo "</td></tr>";
563
        // BUY
564
        echo "<tr class='tab_bg_1'><td>" . $LANG["software"][35] . "</td><td>";
565
        dropdownYesNo("buy", $values['buy']);
566
        echo "</td></tr>";
567

    
568
        echo "<tr class='tab_bg_1'><td>" . $LANG["common"][25] . "</td><td>";
569
        echo "<textarea name='comments' rows='6' cols='40'>" . $values['comments'] . "</textarea>";
570
        echo "</td></tr>";
571

    
572
        echo "<tr class='tab_bg_2'>";
573
        echo "<td align='center' colspan='3'>";
574
        echo "<input type='hidden' name='sID' value=" . $sID . ">";
575
        if ($action == "update")
576
                echo "<input type='hidden' name='ID' value=" . $lID . ">";
577
        echo "<input type='hidden' name='form' value=" . $action . ">";
578
        echo "<input type='submit' name='$action' value=\"" . $button . "\" class='submit'>";
579
        echo "</td>";
580

    
581
        echo "</table></form></div>";
582
}
583

    
584
/**
585
 * Update number of license : create or delete license if needed
586
 *
587
 * @param $likeID ID of a license used to define the reference
588
 * @param $number actual number of license
589
 * @param $new_number  new license number
590
 * @return nothing
591
 */
592
function updateNumberLicenses($likeID, $number, $new_number) {
593
        global $DB;
594

    
595
        $lic = new License();
596
        // Delete unused licenses
597
        if ($number > $new_number) {
598
                if ($lic->getFromDB($likeID)) {
599
                
600
                        $SEARCH_LICENCE = "(glpi_licenses.sID = " . $lic->fields["sID"] . " AND glpi_licenses.serial = '" . $lic->fields["serial"] . "'  AND glpi_licenses.oem = '" . $lic->fields["oem"] . "' AND glpi_licenses.oem_computer = '" . $lic->fields["oem_computer"] . "'  AND glpi_licenses.buy = '" . $lic->fields["buy"] . "' ";
601
                        if ($lic->fields["expire"] == "")
602
                                $SEARCH_LICENCE .= " AND glpi_licenses.expire IS NULL)";
603
                        else
604
                                $SEARCH_LICENCE .= " AND glpi_licenses.expire = '" . $lic->fields["expire"] . "')";
605

    
606
                        for ($i = 0; $i < $number - $new_number; $i++) {
607
                                $query_first = "SELECT glpi_licenses.ID as ID, glpi_inst_software.license as iID FROM glpi_licenses LEFT JOIN glpi_inst_software ON glpi_inst_software.license = glpi_licenses.ID WHERE $SEARCH_LICENCE";
608

    
609
                                if ($result_first = $DB->query($query_first)) {
610
                                        if ($lic->fields["serial"] == "free" || $lic->fields["serial"] == "global")
611
                                                $ID = $DB->result($result_first, 0, "ID");
612
                                        else {
613
                                                $fin = 0;
614
                                                while (!$fin && $temp = $DB->fetch_array($result_first))
615
                                                        if ($temp["iID"] == NULL) {
616
                                                                $fin = 1;
617
                                                                $ID = $temp["ID"];
618
                                                        }
619
                                        }
620
                                        if (!empty ($ID)) {
621
                                                $lic->delete(array (
622
                                                        "ID" => $ID
623
                                                ));
624
                                        }
625
                                }
626

    
627
                        }
628
                }
629
                // Create new licenses
630
        } else if ($number < $new_number) {
631
                        $lic->getFromDB($likeID);
632
                        unset ($lic->fields["ID"]);
633

    
634
                        if (is_null($lic->fields["expire"]))
635
                                unset ($lic->fields["expire"]);
636

    
637
                        for ($i = 0; $i < $new_number - $number; $i++) {
638
                                unset ($lic->fields["ID"]);
639
                                $lic->addToDB();
640
                        }
641

    
642
                }
643

    
644
}
645

    
646
/**
647
 * Install a software on a computer
648
 *
649
 * @param $cID ID of the computer where to install a software
650
 * @param $lID ID of the license to install
651
 * @param $sID ID of the software of the license (used for history)
652
 * @param $dohistory Do history ?
653
 * @return nothing
654
 */
655
function installSoftware($cID, $lID, $sID = '', $dohistory = 1) {
656

    
657
        global $DB,$LANG;
658

    
659
        if (!empty ($lID) && $lID > 0) {
660

    
661
                $query_exists = "SELECT ID FROM glpi_inst_software WHERE cID='" . $cID . "' AND license='" . $lID."'";
662
                $result = $DB->query($query_exists);
663
                if ($DB->numrows($result) > 0)
664
                        return $DB->result($result, 0, "ID");
665
                else {
666
                        $query = "INSERT INTO glpi_inst_software VALUES (NULL,'$cID','$lID')";
667
                        if ($result = $DB->query($query)) {
668
                                $newID = $DB->insert_id();
669
                                if ($dohistory) {
670
                                        $lic = new License();
671
                                        $lic->getFromDB($lID);
672
                                        $soft = new Software();
673
                                        if ($soft->getFromDB($lic->fields["sID"])) {
674
                                                $changes[0] = '0';
675
                                                $changes[1] = "";
676
                                                $changes[2] = addslashes($soft->fields["name"] . " (v. " . $lic->fields["version"] . ")");
677
                                                // history log
678
                                                historyLog($cID, COMPUTER_TYPE, $changes, 0, HISTORY_INSTALL_SOFTWARE);
679
                                                $comp = new Computer();
680
                                                $comp->getFromDB($cID);
681
                                                $changes[2] = addslashes($comp->fields["name"] . " (v. " . $lic->fields["version"] . ")");
682
                                                historyLog($lic->fields["sID"], SOFTWARE_TYPE, $changes, 0, HISTORY_INSTALL_SOFTWARE);
683
                                        }
684
                                }
685
                                return $newID;
686
                        } else {
687
                                return false;
688
                        }
689
                }
690
        } else
691
                if ($lID < 0 && !empty ($sID)) { // Auto Add a license
692
                        $lic = new License();
693
                        $newinput = array ();
694
                        $newinput['buy'] = 0;
695
                        $newinput['sID'] = $sID;
696
                        $newinput['serial'] = $LANG["software"][6];
697
                        $lID = $lic->add($newinput);
698

    
699
                        $query = "INSERT INTO glpi_inst_software VALUES (NULL,'$cID','$lID')";
700
                        if ($result = $DB->query($query)) {
701
                                $newID = $DB->insert_id();
702
                                if ($dohistory) {
703
                                        $soft = new Software();
704
                                        if ($soft->getFromDB($sID)) {
705
                                                $changes[0] = '0';
706
                                                $changes[1] = "";
707
                                                $changes[2] = addslashes($soft->fields["name"]);
708
                                                // history log
709
                                                historyLog($cID, COMPUTER_TYPE, $changes, 0, HISTORY_INSTALL_SOFTWARE);
710
                                                $comp = new Computer();
711
                                                $comp->getFromDB($cID);
712
                                                $changes[2] = addslashes($comp->fields["name"]);
713
                                                historyLog($sID, SOFTWARE_TYPE, $changes, 0, HISTORY_INSTALL_SOFTWARE);
714
                                        }
715
                                }
716

    
717
                                return $newID;
718
                        } else {
719
                                return false;
720
                        }
721

    
722
                }
723
}
724

    
725
/**
726
 * Uninstall a software on a computer
727
 *
728
 * @param $ID ID of the install software link (license/computer)
729
 * @param $dohistory Do history ?
730
 * @return nothing
731
 */
732
function uninstallSoftware($ID, $dohistory = 1) {
733

    
734
        global $DB;
735

    
736
        // license data for history
737
        if ($dohistory) {
738
                $query2 = "SELECT * FROM glpi_inst_software WHERE (ID = '$ID')";
739
                $result2 = $DB->query($query2);
740
                $data = $DB->fetch_array($result2);
741
                $lic = new License();
742
                $lic->getFromDB($data["license"]);
743
        }
744

    
745
        $query = "DELETE FROM glpi_inst_software WHERE (ID = '$ID')";
746

    
747
        if ($result = $DB->query($query)) {
748
                if ($dohistory) {
749
                        $soft = new Software();
750
                        if ($soft->getFromDB($lic->fields["sID"])) {
751
                                $changes[0] = '0';
752
                                $changes[1] = addslashes($soft->fields["name"] . " (v. " . $lic->fields["version"] . ")");
753
                                $changes[2] = "";
754
                                // history log
755
                                historyLog($data["cID"], COMPUTER_TYPE, $changes, 0, HISTORY_UNINSTALL_SOFTWARE);
756
                                $comp = new Computer();
757
                                $comp->getFromDB($data["cID"]);
758
                                $changes[1] = addslashes($comp->fields["name"] . " (v. " . $lic->fields["version"] . ")");
759
                                historyLog($lic->fields["sID"], SOFTWARE_TYPE, $changes, 0, HISTORY_UNINSTALL_SOFTWARE);
760

    
761
                        }
762
                }
763

    
764
                return true;
765
        } else {
766
                return false;
767
        }
768
}
769

    
770
/**
771
 * SHow softwrae installed on a computer
772
 *
773
 * @param $instID ID of the computer
774
 * @param $withtemplate template case of the view process
775
 * @return nothing
776
 */
777
function showSoftwareInstalled($instID, $withtemplate = '') {
778

    
779
        global $DB, $CFG_GLPI, $LANG;
780
        if (!haveRight("software", "r"))
781
                return false;
782
        $comp = new Computer();
783
        $comp->getFromDB($instID);
784
        $FK_entities = $comp->fields["FK_entities"];
785

    
786
        $query_cat = "SELECT 1 as TYPE, glpi_dropdown_software_category.name as category, glpi_software.category as category_id, glpi_software.name as softname, glpi_inst_software.license as license, glpi_inst_software.ID as ID,glpi_licenses.expire,glpi_software.deleted, glpi_licenses.sID, GROUP_CONCAT( DISTINCT CONCAT(CONCAT_WS(' - ',glpi_licenses.version, glpi_licenses.serial),'$$',glpi_inst_software.ID) SEPARATOR '$$$$') AS version, glpi_licenses.serial, glpi_licenses.version AS orig_version, glpi_licenses.oem, glpi_licenses.oem_computer, glpi_licenses.buy        
787
                FROM glpi_inst_software 
788
                LEFT JOIN glpi_licenses ON ( glpi_inst_software.license = glpi_licenses.ID )
789
                LEFT JOIN glpi_software ON (glpi_licenses.sID = glpi_software.ID) 
790
                LEFT JOIN glpi_dropdown_software_category ON (glpi_dropdown_software_category.ID = glpi_software.category)";
791

    
792
        $query_cat .= " WHERE glpi_inst_software.cID = '$instID' AND glpi_software.category > 0 
793
                                GROUP BY glpi_licenses.sID";
794

    
795
        $query_nocat = "SELECT 2 as TYPE, glpi_dropdown_software_category.name as category, glpi_software.category as category_id, glpi_software.name as softname, glpi_inst_software.license as license, glpi_inst_software.ID as ID,glpi_licenses.expire,glpi_software.deleted, glpi_licenses.sID, GROUP_CONCAT( DISTINCT CONCAT(CONCAT_WS(' - ',glpi_licenses.version, glpi_licenses.serial),'$$',glpi_inst_software.ID) SEPARATOR '$$$$') AS version, glpi_licenses.serial, glpi_licenses.version AS orig_version, glpi_licenses.oem, glpi_licenses.oem_computer, glpi_licenses.buy  
796
                FROM glpi_inst_software 
797
                LEFT JOIN glpi_licenses ON ( glpi_inst_software.license = glpi_licenses.ID ) 
798
                LEFT JOIN glpi_software ON (glpi_licenses.sID = glpi_software.ID)  
799
                LEFT JOIN glpi_dropdown_software_category ON (glpi_dropdown_software_category.ID = glpi_software.category)";
800
        $query_nocat .= " WHERE glpi_inst_software.cID = '$instID' AND (glpi_software.category <= 0 OR glpi_software.category IS NULL ) 
801
                        GROUP BY glpi_licenses.sID";
802
        $query = "( $query_cat ) UNION ($query_nocat) ORDER BY TYPE, category, softname, version";
803

    
804
        $DB->query("SET SESSION group_concat_max_len = 9999999;");
805

    
806
        $result = $DB->query($query);
807
        $i = 0;
808

    
809
        echo "<br><br><div class='center'><table class='tab_cadre_fixe'>";
810

    
811
        if ((empty ($withtemplate) || $withtemplate != 2) && haveRight("software", "w")) {
812
                echo "<tr class='tab_bg_1'><td align='center' colspan='5'>";
813
                echo "<form method='post' action=\"" . $CFG_GLPI["root_doc"] . "/front/software.licenses.php\">";
814

    
815
                echo "<div class='software-instal'>";
816
                echo "<input type='hidden' name='cID' value='$instID'>";
817
                dropdownSoftwareToInstall("licenseID", $withtemplate, $FK_entities);
818
                echo "<input type='submit' name='install' value=\"" . $LANG["buttons"][4] . "\" class='submit'>";
819
                echo "</div>";
820
                echo "</form>";
821
                echo "</td></tr>";
822
        }
823

    
824
        echo "<tr><th colspan='6'>" . $LANG["software"][17] . ":</th></tr>";
825

    
826
        $cat = -1;
827

    
828
        if ($DB->numrows($result)) {
829
                while ($data = $DB->fetch_array($result)) {
830
                        if ($data["category_id"] != $cat) {
831
                                $cat = displayCategoryHeader($data, $cat);
832
                        }
833

    
834
                        displaySoftsByCategory($data, $instID, $withtemplate);
835
                }
836

    
837
                echo "</table></div></td></tr>";
838

    
839
                $q = "SELECT count(*) FROM glpi_software WHERE deleted='0' AND is_template='0'";
840
                $result = $DB->query($q);
841
                $nb = $DB->result($result, 0, 0);
842

    
843
        }
844

    
845
        echo "</table></div>";
846

    
847
}
848

    
849
/**
850
 * Display category header for showSoftwareInstalled function
851
 *
852
 * @param $data data used to display 
853
 * @param $cat current category ID
854
 * @return new category ID
855
 */
856
function displayCategoryHeader($data, $cat) {
857
        global $LANG, $CFG_GLPI;
858
        $expirecss = '';
859

    
860
        // Close old one
861
        if ($cat != -1) {
862
                echo "</table></div></td></tr>";
863
        }
864

    
865
        $display = "none";
866

    
867
        $cat = $data["category_id"];
868
        $catname = $data["category"];
869
        if (!$cat) {
870
                $catname = $LANG["softwarecategories"][3];
871
                $display = $CFG_GLPI["expand_soft_not_categorized"];
872
        } else
873
                $display = $CFG_GLPI["expand_soft_categorized"];
874

    
875
        echo "        <tr class='tab_bg_2$expirecss'>";
876
        echo "          <td align='center' colspan='5'>";
877
        echo "                        <a  href=\"javascript:showHideDiv('softcat$cat','imgcat$cat','" . GLPI_ROOT . "/pics/folder.png','" . GLPI_ROOT . "/pics/folder-open.png');\">";
878
        echo "                                <img alt='' name='imgcat$cat' src=\"" . GLPI_ROOT . "/pics/folder" . (!$display ? '' : "-open") . ".png\">&nbsp;<strong>" . $catname . "</strong>";
879
        echo "                        </a>";
880
        echo "                </td>";
881
        echo "        </tr>";
882
        echo "<tr class='tab_bg_2$expirecss'>";
883
        echo "                <td colspan='5'>
884
                                     <div align='center' id='softcat$cat' " . (!$display ? "style=\"display:none;\"" : '') . ">";
885
        echo "                        <table class='tab_cadre_fixe'>";
886
        echo "                                <tr>";
887
        echo "                                        <th>" . $LANG["common"][16] . "</th><th>" . $LANG["software"][11] . "</th><th>" . $LANG["software"][32] . "</th><th>" . $LANG["software"][28] . "</th><th>" . $LANG["software"][35] . "</th>";
888
        echo "                                </tr>";
889
        return $cat;
890
}
891

    
892
/**
893
 * Display a software for a category
894
 *
895
 * @param $data data used to display 
896
 * @param $instID ID of the computer
897
 * @param $withtemplate template case of the view process
898
 * @return nothing
899
 */
900
function displaySoftsByCategory($data, $instID, $withtemplate) {
901
        global $LANG, $CFG_GLPI;
902
        $expirecss = '';
903
        $expirer = 0;
904

    
905
        $lID = $data["license"];
906
        $ID = $data["ID"];
907
        $multiple = false;
908

    
909
        $today = date("Y-m-d");
910

    
911
        if ($data['expire'] != NULL && $today > $data['expire']) {
912
                $expirer = 1;
913
                $expirecss = "_2";
914
        }
915

    
916
        if ($data['deleted']) {
917
                $expirer = 1;
918
                $expirecss = "_2";
919
        }
920

    
921
        echo "<tr class='tab_bg_1$expirecss'>";
922
        echo "<td class='center'><strong><a href=\"" . $CFG_GLPI["root_doc"] . "/front/software.form.php?ID=" . $data['sID'] . "\">";
923
        echo $data["softname"] . ($CFG_GLPI["view_ID"] ? " (" . $data['ID'] . ")" : "") . "</a>";
924
        echo "</strong>";
925
        echo "</td>";
926
        echo "<td>";
927

    
928
        if (!strpos($data["version"], "$$$$")) {
929
                echo $data["serial"] . " - " . $data["orig_version"];
930
                if (empty ($withtemplate) || $withtemplate != 2) {
931
                        echo " - <a href=\"" . $CFG_GLPI["root_doc"] . "/front/software.licenses.php?uninstall=uninstall&amp;ID=$ID&amp;cID=$instID\">";
932
                        echo "<strong>" . $LANG["buttons"][5] . "</strong></a>";
933
                }
934
        } else {
935
                $multiple = true;
936
                $split = explode("$$$$", $data["version"]);
937
                $count_display = 0;
938
                $out = "";
939
                for ($k = 0; $k < count($split); $k++) {
940
                        if ($count_display)
941
                                echo "<br>";
942
                        $count_display++;
943
                        $split2 = explode("$$", $split[$k]);
944
                        echo $split2[0];
945

    
946
                        if (isset ($split2[1]) && is_numeric($split2[1]) && (empty ($withtemplate) || $withtemplate != 2)) {
947
                                echo " - <a href=\"" . $CFG_GLPI["root_doc"] . "/front/software.licenses.php?uninstall=uninstall&amp;ID=" . $split2[1] . "&amp;cID=$instID\">";
948
                                echo "<strong>" . $LANG["buttons"][5] . "</strong></a>";
949
                        }
950
                }
951
        }
952
        echo "</td>";
953
        echo "<td class='center'><strong>";
954
        if ($data['expire'] == NULL)
955
                echo $LANG["software"][26];
956
        else {
957
                if ($expirer)
958
                        echo $LANG["software"][27];
959
                else
960
                        echo $LANG["software"][25] . "&nbsp;" . $data['expire'];
961
        }
962

    
963
        echo "</strong></td>";
964
        if ($data['serial'] != "free" && $data['serial'] != "global") {
965
                // OEM
966
                if ($data["oem"]) {
967
                        $comp = new Computer();
968
                        $comp->getFromDB($data["oem_computer"]);
969
                }
970
                echo "<td align='center' class='tab_bg_1" . ( ( $expirer || ($data["oem"] && (!isset($comp->fields['ID']) || $comp->fields['ID'] != $instID) ) ) ? "_2" : "") . "'>" . ($data["oem"] ? $LANG["choice"][1] : $LANG["choice"][0]);
971
                if ($data["oem"]) {
972
                        echo "<br><strong>";
973
                        if (isset ($comp->fields['ID']))
974
                                echo "<a href='" . $CFG_GLPI["root_doc"] . "/front/computer.form.php?ID=" . $comp->fields['ID'] . "'>" . $comp->fields['name'] . "</a>";
975
                        else
976
                                echo "N/A";
977
                        echo "<strong>";
978
                }
979
                echo "</td>";
980

    
981
                // BUY
982
                echo "<td class='center'>" . ($data["buy"] ? $LANG["choice"][1] : $LANG["choice"][0]);
983
                echo "</td>";
984
        } else {
985
                echo "<td>&nbsp;</td><td>&nbsp;</td>";
986
        }
987
        echo "</tr>";
988
}
989

    
990
/**
991
 * Unglobalize a license
992
 *
993
 * @param $ID ID of the license
994
 * @return nothing
995
 */
996
function unglobalizeLicense($ID) {
997
        global $DB;
998
        $license = new License();
999
        $license->getFromDB($ID);
1000
        // Check if it is a real global license
1001
        if ($license->fields["serial"] == "free" || $license->fields["serial"] == "global") {
1002

    
1003
                $query = "SELECT * FROM glpi_inst_software WHERE license = '$ID'";
1004
                $result = $DB->query($query);
1005

    
1006
                if (($nb = $DB->numrows($result)) > 0) {
1007
                        // Update item to unit management :
1008
                        $input = $license->fields;
1009
                        $input["serial"] = "_" . $license->fields["serial"] . "_";
1010

    
1011
                        // skip first
1012
                        $data = $DB->fetch_array($result);
1013
                        if ($license->fields["oem"]) {
1014
                                $input["oem_computer"] = $data["cID"];
1015
                        }
1016
                        $license->update($input);
1017

    
1018
                        $input = $license->fields;
1019
                        $input["_duplicate_license"] = $ID;
1020
                        unset ($input["ID"]);
1021

    
1022
                        // Get ID of the inst_software
1023
                        while ($data = $DB->fetch_array($result)) {
1024
                                if ($license->fields["oem"]) {
1025
                                        $input["oem_computer"] = $data["cID"];
1026
                                } else {
1027
                                        $input["oem"] = 0;
1028
                                }
1029

    
1030
                                // Add new Item
1031
                                unset ($license->fields);
1032
                                if ($newID = $license->add($input)) {
1033
                                        // Update inst_software
1034
                                        $query2 = "UPDATE glpi_inst_software SET license='$newID' WHERE ID='" . $data["ID"] . "'";
1035
                                        $DB->query($query2);
1036
                                }
1037
                        }
1038
                }
1039
        }
1040

    
1041
}
1042

    
1043
/**
1044
 * Count Installations of a software and create string to display
1045
 *
1046
 * @param $sID ID of the software
1047
 * @param $nohtml do not use HTML to highlight ?
1048
 * @return string contains counts
1049
 */
1050
function countInstallations($sID, $nohtml = 0) {
1051

    
1052
        global $DB, $CFG_GLPI, $LANG;
1053

    
1054
        // Get total
1055
        $total = getLicenceNumber($sID);
1056
        $out = "";
1057
        if ($total != 0) {
1058

    
1059
                if (isFreeSoftware($sID)) {
1060
                        // Get installed
1061
                        $installed = getInstalledLicence($sID);
1062
                        if (!$nohtml)
1063
                                $out .= "<i>" . $LANG["software"][39] . "</i>&nbsp;&nbsp;" . $LANG["software"][19] . ": <strong>$installed</strong>";
1064
                        else
1065
                                $out .= $LANG["software"][39] . "  " . $LANG["software"][19] . ": $installed";
1066
                } else
1067
                        if (isGlobalSoftware($sID)) {
1068
                                $installed = getInstalledLicence($sID);
1069
                                if (!$nohtml)
1070
                                        $out .= "<i>" . $LANG["software"][38] . "</i>&nbsp;&nbsp;" . $LANG["software"][19] . ": <strong>$installed</strong>";
1071
                                else
1072
                                        $out .= $LANG["software"][38] . "  " . $LANG["software"][19] . ": $installed";
1073

    
1074
                        } else {
1075

    
1076
                                // Get installed
1077
                                $i = 0;
1078
                                $installed = getInstalledLicence($sID);
1079

    
1080
                                // Get remaining
1081
                                $remaining = max(0, $total - $installed);
1082

    
1083
                                // Output
1084
                                if (!$nohtml) {
1085
                                        $out .= "<table cellpadding='2' cellspacing='0'><tr>";
1086
                                        $out .= "<td width='35%'>" . $LANG["software"][19] . ": <strong>$installed</strong></td>";
1087
                                } else
1088
                                        $out .= "  " . $LANG["software"][19] . ": $installed";
1089

    
1090
                                $color = "red";
1091

    
1092
                                if ($remaining == 0) {
1093
                                        $color = "green";
1094
                                } else {
1095
                                        $color = "blue";
1096
                                }
1097

    
1098
                                if (!$nohtml) {
1099
                                        $remaining = "<span class='$color'>$remaining";
1100
                                        $remaining .= "</span>";
1101
                                        $out .= "<td width='20%'>" . $LANG["software"][20] . ": <strong>$remaining</strong></td>";
1102
                                        $out .= "<td width='20%'>" . $LANG["common"][33] . ": <strong>" . $total . "</strong></td>";
1103
                                } else {
1104
                                        $out .= "  " . $LANG["software"][20] . ": $remaining";
1105
                                        $out .= "  " . $LANG["common"][33] . ": " . $total;
1106
                                }
1107

    
1108
                                $tobuy = getLicenceToBuy($sID);
1109
                                if ($tobuy > 0) {
1110
                                        if (!$nohtml)
1111
                                                $out .= "<td width='25%'>" . $LANG["software"][37] . ": <strong><span class='red'>" . $tobuy . "</span></strong></td>";
1112
                                        else
1113
                                                $out .= "  " . $LANG["software"][37] . ": " . $tobuy;
1114
                                } else {
1115
                                        if (!$nohtml)
1116
                                                $out .= "<td width='20%'>&nbsp;</td>";
1117
                                }
1118
                                if (!$nohtml)
1119
                                        $out .= "</tr></table>";
1120
                        }
1121
        } else {
1122
                if (!$nohtml)
1123
                        $out .= "<div class='center'><i>" . $LANG["software"][40] . "</i></div>";
1124
                else
1125
                        $out .= $LANG["software"][40];
1126
        }
1127
        return $out;
1128
}
1129

    
1130
/**
1131
 * Move licenses to another software
1132
 *
1133
 * @param $lID ID of the license used to define which licenses need to be moved (all similar ones)
1134
 * @param $sID ID of the destination software
1135
 * @return nothing
1136
 */
1137
function moveSimilarLicensesToSoftware($lID, $sID) {
1138
        global $DB;
1139
        $lic = new License();
1140
        if ($lic->getFromDB($lID)) {
1141
                $query = "UPDATE glpi_licenses SET sID='$sID' WHERE version='" . addslashes($lic->fields['version']) . "'
1142
                                        AND serial='" . addslashes($lic->fields['serial']) . "'
1143
                                        AND oem='" . addslashes($lic->fields['oem']) . "'
1144
                                        AND oem_computer='" . addslashes($lic->fields['oem_computer']) . "'
1145
                                        AND buy='" . addslashes($lic->fields['buy']) . "'
1146
                                        AND sID='" . addslashes($lic->fields['sID']) . "' ";
1147
                if ($lic->fields['expire'] == "")
1148
                        $query .= " AND expire IS NULL";
1149
                else
1150
                        $query .= " AND .expire = '" . addslashes($lic->fields['expire']) . "'";
1151
                $DB->query($query);
1152
        }
1153
}
1154

    
1155
/**
1156
 * Delete similar licenses
1157
 *
1158
 * @param $lID ID of the license used to define which licenses need to be deleted (all similar ones)
1159
 * @return nothing
1160
 */
1161
function deleteSimilarLicenses($lID) {
1162
        global $DB;
1163
        $lic = new License();
1164
        if ($lic->getFromDB($lID)) {
1165
                $query = "SELECT ID FROM glpi_licenses WHERE version='" . addslashes($lic->fields['version']) . "' 
1166
                                                AND serial='" . addslashes($lic->fields['serial']) . "' 
1167
                                                AND oem='" . addslashes($lic->fields['oem']) . "' 
1168
                                                AND oem_computer='" . addslashes($lic->fields['oem_computer']) . "' 
1169
                                                AND buy='" . addslashes($lic->fields['buy']) . "' 
1170
                                                AND sID='" . addslashes($lic->fields['sID']) . "' ";
1171

    
1172
                if ($lic->fields['expire'] == "") {
1173
                        $query .= " AND expire IS NULL";
1174
                } else {
1175
                        $query .= " AND .expire = '" . addslashes($lic->fields['expire']) . "'";
1176
                }
1177

    
1178
                if ($result = $DB->query($query)) {
1179
                        while ($data = $DB->fetch_array($result)) {
1180
                                $lic->delete(array (
1181
                                        'ID' => $data['ID']
1182
                                ));
1183
                        }
1184
                }
1185
        }
1186
}
1187

    
1188
/**
1189
 * Move licenses to another license
1190
 *
1191
 * @param $lID destination license ID
1192
 * @param $tomove array of ID of the licenses to move
1193
 * @return nothing
1194
 */
1195
function moveLicensesToLicense($tomove = array (), $lID) {
1196
        global $DB;
1197

    
1198
        $lic = new License();
1199
        $lic2 = new License();
1200

    
1201
        if (count($tomove) && $lic->getFromDB($lID)) {
1202

    
1203
                if ($lic->fields['serial'] == 'free' || $lic->fields['serial'] == 'global') {
1204
                        // Destination is global : Only move inst_software and delete old license if unused 
1205
                        foreach ($tomove as $moveID) {
1206
                                if ($moveID != $lID && $lic2->getFromDB($moveID)) {
1207
                                        $query = "UPDATE glpi_inst_software SET license='$lID' WHERE license='$moveID'";
1208
                                        $DB->query($query);
1209

    
1210
                                        if (getInstallionsForLicense($moveID) == 0) {
1211
                                                $lic2->delete(array (
1212
                                                        'ID' => $moveID
1213
                                                ));
1214
                                        }
1215
                                }
1216
                        }
1217
                } else {
1218
                        // Individual one : if original is global create a copy else copy license
1219
                        foreach ($tomove as $moveID) {
1220
                                if ($moveID != $lID && $lic2->getFromDB($moveID)) {
1221
                                        if ($lic2->fields['serial'] == 'free' || $lic2->fields['serial'] == 'global') {
1222
                                                // Create a copy of the original one foreach installation and move inst_software 
1223
                                                $query = "SELECT * FROM glpi_inst_software WHERE license='$moveID'";
1224

    
1225
                                                $input = $lic->fields;
1226
                                                unset ($input['ID']);
1227

    
1228
                                                if ($result = $DB->query($query)) {
1229
                                                        while ($data_inst = $DB->fetch_array($result)) {
1230
                                                                unset ($lic2->fields);
1231
                                                                $newID = $lic2->add($input);
1232
                                                                $query = "UPDATE glpi_inst_software SET license='$newID' WHERE ID='" . $data_inst['ID'] . "'";
1233
                                                                $DB->query($query);
1234
                                                        }
1235
                                                }
1236
                                        } else {
1237
                                                // Only update license to be the same as the destination one 
1238
                                                $input = $lic->fields;
1239
                                                $input["ID"] = $moveID;
1240
                                                unset ($lic2->fields);
1241
                                                $lic2->update($input);
1242
                                        }
1243
                                        if (getInstallionsForLicense($moveID) == 0) {
1244
                                                $lic2->delete(array (
1245
                                                        'ID' => $moveID
1246
                                                ));
1247
                                        }
1248
                                }
1249
                        }
1250
                }
1251
        }
1252
}
1253

    
1254
/**
1255
 * Get number of installed licenses of a software
1256
 *
1257
 * @param $sID software ID
1258
 * @return number of installed licenses
1259
 */
1260
function getInstalledLicence($sID) {
1261
        global $DB;
1262
        $query = "SELECT count(*) FROM glpi_licenses INNER JOIN glpi_inst_software ON (glpi_licenses.sID = '$sID' AND glpi_licenses.ID = glpi_inst_software.license ) 
1263
                        INNER JOIN glpi_computers ON ( glpi_inst_software.cID=glpi_computers.ID AND glpi_computers.deleted='0' AND glpi_computers.is_template='0' )";
1264

    
1265
        $result = $DB->query($query);
1266

    
1267
        if ($DB->numrows($result) != 0) {
1268
                return $DB->result($result, 0, 0);
1269
        } else
1270
                return 0;
1271

    
1272
}
1273
/**
1274
 * Get number of licenses to buy of a software
1275
 *
1276
 * @param $sID software ID
1277
 * @return number of licenses to buy
1278
 */
1279
function getLicenceToBuy($sID) {
1280
        global $DB;
1281
        $query = "SELECT ID FROM glpi_licenses WHERE (sID = '$sID' AND buy ='0'  AND serial <> 'free' AND serial <> 'global')";
1282
        $result = $DB->query($query);
1283
        return $DB->numrows($result);
1284
}
1285

    
1286
/**
1287
 * Get number of licensesof a software
1288
 *
1289
 * @param $sID software ID
1290
 * @return number of licenses
1291
 */
1292
function getLicenceNumber($sID) {
1293
        global $DB;
1294
        $query = "SELECT ID,serial FROM glpi_licenses WHERE (sID = '$sID')";
1295
        $result = $DB->query($query);
1296
        return $DB->numrows($result);
1297
}
1298
/**
1299
 * A software have a global license ?
1300
 *
1301
 * @param $sID software ID
1302
 * @return boolean : is the software have a global license
1303
 */
1304
function isGlobalSoftware($sID) {
1305
        global $DB;
1306
        $query = "SELECT ID,serial FROM glpi_licenses WHERE (sID = '$sID' and serial='global')";
1307
        $result = $DB->query($query);
1308

    
1309
        return ($DB->numrows($result) > 0);
1310
}
1311

    
1312
/**
1313
 * A software have a free license ?
1314
 *
1315
 * @param $sID software ID
1316
 * @return boolean : is the software have a free license
1317
 */
1318
function isFreeSoftware($sID) {
1319
        global $DB;
1320
        $query = "SELECT ID,serial FROM glpi_licenses WHERE (sID = '$sID'  and serial='free')";
1321
        $result = $DB->query($query);
1322
        return ($DB->numrows($result) > 0);
1323
}
1324
/**
1325
 * Get number of installation of a license
1326
 *
1327
 * @param $ID license ID
1328
 * @return number of installation 
1329
 */
1330
function getInstallionsForLicense($ID) {
1331
        global $DB;
1332
        $query = "SELECT count(*) FROM glpi_inst_software INNER JOIN glpi_computers ON ( glpi_inst_software.cID=glpi_computers.ID ) WHERE glpi_inst_software.license ='$ID' AND glpi_computers.deleted='0' AND glpi_computers.is_template='0' ";
1333

    
1334
        $result = $DB->query($query);
1335

    
1336
        if ($DB->numrows($result) != 0) {
1337
                return $DB->result($result, 0, 0);
1338
        } else
1339
                return 0;
1340

    
1341
}
1342

    
1343
/*
1344
 * Indicates if software is in trash or not
1345
 * @param ID the software's ID
1346
 * @return true if it's in trash, false if not
1347
 
1348
/* // NOT_USED
1349
function isSoftwareDeleted($ID) {
1350
        global $DB;
1351
        $query = "SELECT deleted FROM glpi_software WHERE ID =" . $ID;
1352
        $result = $DB->query($query);
1353
        if ($DB->numrows($result) > 0)
1354
                return $DB->result($result, 0, "deleted");
1355
        else
1356
                return false;
1357
}
1358
*/
1359
/**
1360
 * Create a new software
1361
 * @param name the software's name
1362
 * @param manufacturer the software's manufacturer
1363
 * @param entity the entity in which the software must be added
1364
 * @param comments
1365
 * @param process_type process which calls the function (IMPORT_TYPE_OCS, IMPORT_TYPE_DICTIONNARY, etc...) 
1366
 * @return the software's ID
1367
 */
1368
function addSoftware($name, $manufacturer, $entity, $comments = '', $process_type = -1) {
1369
        global $LANG, $DB;
1370
        $software = new Software;
1371

    
1372
        $manufacturer_id = 0;
1373
        if ($manufacturer != '') {
1374
                $manufacturer_id = externalImportDropdown("glpi_dropdown_manufacturer", $manufacturer);
1375
        }                                                                        
1376

    
1377
        $sql = "SELECT ID FROM glpi_software WHERE FK_entities='$entity' AND FK_glpi_enterprise='$manufacturer_id' AND name='".$name."'";
1378
        $res_soft = $DB->query($sql);
1379
        if ($soft = $DB->fetch_array($res_soft)) {
1380
                $id = $soft["ID"];
1381
        } else {
1382
                $input["name"] = $name;
1383
                $input["FK_glpi_enterprise"] = $manufacturer_id;
1384
                $input["FK_entities"] = $entity;
1385
                $input["comments"] = $LANG["rulesengine"][88];
1386

    
1387
                //Process software's category rules
1388
                $softcatrule = new SoftwareCategoriesRuleCollection;
1389
                $result = $softcatrule->processAllRules(null, null, $input);
1390
                if (!empty ($result) && isset ($result["category"])) {
1391
                        $input["category"] = $result["category"];
1392
                } else {
1393
                        $input["category"] = 0;
1394
                }
1395

    
1396
                switch ($process_type) {
1397
                        case IMPORT_TYPE_OCS :
1398
                                $input["_from_ocs"] = 1;
1399
                                break;
1400
                        default :
1401
                                break;
1402
                }
1403

    
1404
                $id = $software->add($input);
1405
        }
1406
        return $id;
1407
}
1408

    
1409
/**
1410
 * Put software in trash because it's been removed by GLPI software dictionnary
1411
 * @param $ID  the ID of the software to put in trash
1412
 * @param $comments the comments to add to the already existing software's comments
1413
 * @param $process_type process which calls the function (IMPORT_TYPE_OCS, IMPORT_TYPE_DICTIONNARY, etc...)
1414
 */
1415
function putSoftwareInTrash($ID, $comments = '', $process_type = -1) {
1416
        global $LANG,$CFG_GLPI;
1417
        $software = new Software;
1418
        //Get the software's fields
1419
        $software->getFromDB($ID);
1420

    
1421
        $input["ID"] = $ID;
1422
        $input["deleted"] = 1;
1423
        
1424
        $config = new Config;
1425
        $config->getFromDB($CFG_GLPI["ID"]);
1426
        
1427
        //change category of the software on deletion (if defined in glpi_config)
1428
        if (isset($config->fields["category_on_software_delete"]) && $config->fields["category_on_software_delete"] != 0)
1429
                $input["category"] = $config->fields["category_on_software_delete"];
1430
                
1431
        //Add dictionnary comment to the current comments
1432
        $input["comments"] = ($software->fields["comments"] != '' ? "\n" : '') . $comments;
1433

    
1434
        //Update the software
1435
        switch ($process_type) {
1436
                case IMPORT_TYPE_OCS :
1437
                        $input["_from_ocs"] = 1;
1438
                        break;
1439
                default :
1440
                        break;
1441
        }
1442

    
1443
        $software->update($input);
1444
}
1445

    
1446
/**
1447
 * Restore a software from trash
1448
 * @param $ID  the ID of the software to put in trash
1449
 * @param $process_type process which calls the function (IMPORT_TYPE_OCS, IMPORT_TYPE_DICTIONNARY, etc...) 
1450
 */
1451
function removeSoftwareFromTrash($ID, $process_type = -1) {
1452
        $s = new Software;
1453

    
1454
        $s->getFromDB($ID);
1455
        $softcatrule = new SoftwareCategoriesRuleCollection;
1456
        $result = $softcatrule->processAllRules(null, null, $s->fields);
1457

    
1458
        if (!empty ($result) && isset ($result["category"]))
1459
                $input["category"] = $result["category"];
1460
        else
1461
                $input["category"] = 0;
1462

    
1463
        //Update the software
1464
        switch ($process_type) {
1465
                case IMPORT_TYPE_OCS :
1466
                        $input["_from_ocs"] = 1;
1467
                        break;
1468
                default :
1469
                        break;
1470
        }
1471
        $s->restore(array (
1472
                "ID" => $ID
1473
        ));
1474
}
1475
/**
1476
 * Add a Software. If already exist in trash restore it
1477
 * @param name the software's name
1478
 * @param manufacturer the software's manufacturer
1479
 * @param entity the entity in which the software must be added
1480
 * @param comments comments
1481
 * @param process_type process which calls the function (IMPORT_TYPE_OCS, IMPORT_TYPE_DICTIONNARY, etc...) 
1482
 */
1483
function addSoftwareOrRestoreFromTrash($name,$manufacturer,$entity,$comments='',$process_type=-1) {
1484
        global $DB;
1485
        //Look for the software by his name in GLPI for a specific entity
1486
        $query_search = "SELECT glpi_software.ID as ID, glpi_software.deleted as deleted  
1487
                        FROM glpi_software 
1488
                        WHERE name = '" . $name . "' AND is_template='0' AND FK_entities='" . $entity."'";
1489
        $result_search = $DB->query($query_search);
1490
        if ($DB->numrows($result_search) > 0) {
1491
                //Software already exists for this entity, get his ID
1492
                $data = $DB->fetch_array($result_search);
1493
                $ID = $data["ID"];
1494

    
1495
                // restore software
1496
                if ($data['deleted'])
1497
                        removeSoftwareFromTrash($ID);
1498
        } else {
1499
                $ID = 0;
1500
        }
1501

    
1502
        if (!$ID) 
1503
                $ID = addSoftware($name, $manufacturer, $entity, $comments, $process_type);
1504
        return $ID;        
1505
}
1506

    
1507

    
1508
/**
1509
 * Cron action on softwares : alert on expired licences
1510
 * @param $display display informations instead or log in file ?
1511
 * @return 0 : nothing to do 1 : done with success
1512
 **/
1513
function cron_software($display=false){
1514
        global $DB,$CFG_GLPI,$LANG;
1515

    
1516
        if (!$CFG_GLPI["mailing"]){
1517
                return false;
1518
        }
1519

    
1520
        loadLanguage($CFG_GLPI["default_language"]);
1521

    
1522
        $message=array();
1523
        $items_notice=array();
1524
        $items_end=array();
1525

    
1526
        // Check notice
1527
        $query="SELECT glpi_licenses.*, glpi_software.FK_entities, glpi_software.name as softname
1528
                FROM glpi_licenses 
1529
                LEFT JOIN glpi_alerts ON (glpi_licenses.ID = glpi_alerts.FK_device 
1530
                                        AND glpi_alerts.device_type='".LICENSE_TYPE."' 
1531
                                        AND glpi_alerts.type='".ALERT_END."') 
1532
                LEFT JOIN glpi_software ON (glpi_licenses.sID = glpi_software.ID)
1533
                WHERE glpi_alerts.date IS NULL
1534
                        AND glpi_licenses.expire <> '0000-00-00' 
1535
                        AND glpi_licenses.expire IS NOT NULL 
1536
                        AND glpi_licenses.expire < CURDATE()
1537
                ";
1538

    
1539
        $result=$DB->query($query);
1540
        if ($DB->numrows($result)>0){
1541
                while ($data=$DB->fetch_array($result)){
1542
                        if (!isset($message[$data["FK_entities"]])){
1543
                                $message[$data["FK_entities"]]="";
1544
                        }
1545
                        if (!isset($items_notice[$data["FK_entities"]])){
1546
                                $items[$data["FK_entities"]]=array();
1547
                        }
1548

    
1549
                        $name = $data['softname'].' '.$data['version'].' - '.$data['serial'];
1550

    
1551
                        // define message alert
1552
                        if (strstr($message[$data["FK_entities"]],$name)===false){
1553
                                $message[$data["FK_entities"]].=$LANG["mailing"][51]." ".$name.": ".convDate($data["expire"])."<br>\n";
1554
                        }
1555
                        $items[$data["FK_entities"]][]=$data["ID"];
1556
                }
1557

    
1558

    
1559
        }
1560

    
1561
        if (count($message)>0){
1562
                foreach ($message as $entity => $msg){
1563
                        $mail=new MailingAlert("alertlicense",$msg,$entity);
1564
                        if ($mail->send()){
1565
                                if ($display){
1566
                                        addMessageAfterRedirect(getDropdownName("glpi_entities",$entity).":  $msg");
1567
                                }
1568
                                logInFile("cron",getDropdownName("glpi_entities",$entity).":  $msg\n");
1569
                
1570
                                // Mark alert as done
1571
                                $alert=new Alert();
1572
                                $input["device_type"]=LICENSE_TYPE;
1573

    
1574
                                $input["type"]=ALERT_END;
1575
                                if (isset($items[$entity])){
1576
                                        foreach ($items[$entity] as $ID){
1577
                                                $input["FK_device"]=$ID;
1578
                                                $alert->add($input);
1579
                                                unset($alert->fields['ID']);
1580
                                        }
1581
                                }
1582
                        } else {
1583
                                if ($display){
1584
                                        addMessageAfterRedirect(getDropdownName("glpi_entities",$entity).":  Send licenses alert failed");
1585
                                }
1586
                                logInFile("cron",getDropdownName("glpi_entities",$entity).":  Send licenses alert failed\n");
1587
                        }
1588
                }
1589
                return 1;
1590
        }
1591

    
1592
        return 0;
1593

    
1594

    
1595
}
1596

    
1597

    
1598
?>
Redmine Appliance - Powered by TurnKey Linux