File: function_computers.php

package info (click to toggle)
ocsinventory-server 2.5%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,088 kB
  • sloc: php: 27,462; perl: 8,241; sh: 1,680; sql: 1,355; xml: 1,041; makefile: 34
file content (319 lines) | stat: -rwxr-xr-x 13,115 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<?php
/*
 * Copyright 2005-2016 OCSInventory-NG/OCSInventory-ocsreports contributors.
 * See the Contributors file for more details about them.
 *
 * This file is part of OCSInventory-NG/OCSInventory-ocsreports.
 *
 * OCSInventory-NG/OCSInventory-ocsreports is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 2 of the License,
 * or (at your option) any later version.
 *
 * OCSInventory-NG/OCSInventory-ocsreports is distributed in the hope that it
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with OCSInventory-NG/OCSInventory-ocsreports. if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 */

/**
 * Hardware locking function. Prevents the hardware to be altered by either the server or another administrator using the GUI
 * @param id Hardware identifier to be locked
 */
function lock($id) {
    $reqClean = "DELETE FROM locks WHERE unix_timestamp(since)<(unix_timestamp(NOW())-3600)";
    mysql2_query_secure($reqClean, $_SESSION['OCS']["writeServer"]);

    $reqLock = "INSERT INTO locks(hardware_id) VALUES ('%s')";
    $argLock = $id;
    if (mysql2_query_secure($reqLock, $_SESSION['OCS']["writeServer"], $argLock)) {
        return( mysqli_affected_rows($_SESSION['OCS']["writeServer"]) == 1 );
    } else {
        return false;
    }
}

/**
 * Hardware unlocking function
 * @param id Hardware identifier to be unlocked
 */
function unlock($id) {
    $reqLock = "DELETE FROM locks WHERE hardware_id='%s'";
    $argLock = $id;
    mysql2_query_secure($reqLock, $_SESSION['OCS']["writeServer"], $argLock);
    return( mysqli_affected_rows($_SESSION['OCS']["writeServer"]) == 1 );
}

/**
 * Show an error message if the locking failed
 */
function errlock() {
    global $l;
    msg_error($l->g(376));
}

function computer_list_by_tag($tag = "", $format = 'LIST') {
    $arg_sql = array();
    if ($tag == "") {
        $sql_mycomputers['SQL'] = "select hardware_id from accountinfo a where " . $_SESSION['OCS']["mesmachines"];
    } elseif (is_array($tag)) {
        $sql_mycomputers = "select hardware_id from accountinfo a where a.tag in ";
        $sql_mycomputers = mysql2_prepare($sql_mycomputers, $arg_sql, $tag);
    } else {
        $sql_mycomputers = "select hardware_id from accountinfo a where a.tag in ";
        $sql_mycomputers = mysql2_prepare($sql_mycomputers, $arg_sql, $tag);
    }
    $res_mycomputers = mysql2_query_secure($sql_mycomputers['SQL'], $_SESSION['OCS']["readServer"], $sql_mycomputers['ARG']);
    $mycomputers = "(";
    while ($item_mycomputers = mysqli_fetch_object($res_mycomputers)) {
        $mycomputers .= $item_mycomputers->hardware_id . ",";
        $array_mycomputers[] = $item_mycomputers->hardware_id;
    }
    $mycomputers = substr($mycomputers, 0, -1) . ")";
    if ($mycomputers == "()" || !isset($array_mycomputers)) {
        $mycomputers = "ERROR";
    }
    if ($format == 'LIST') {
        return $mycomputers;
    } else {
        return $array_mycomputers;
    }
}

/**
 * Deleting function
 * @param id Hardware identifier to be deleted
 * @param checkLock Tells wether or not the locking system must be used (default true)
 * @param traceDel Tells wether or not the deleted entities must be inserted in deleted_equiv for tracking purpose (default true)
 */
function deleteDid($id, $checkLock = true, $traceDel = true, $silent = false
) {
    global $l;
    if ($_SESSION['OCS']['profile']->getConfigValue('DELETE_COMPUTERS') == "NO") {
        msg_error($l->g(1273));
        return false;
    }
    //If lock is not user OR it is used and available
    if (!$checkLock || lock($id)) {
        $sql = "SELECT deviceid,name,IPADDR,OSNAME FROM hardware WHERE id='%s'";
        $resId = mysql2_query_secure($sql, $_SESSION['OCS']["readServer"], $id);
        $valId = mysqli_fetch_array($resId);
        $idHard = $id;
        $did = $valId["deviceid"];
        if ($did) {
            //Deleting a network device
            if (strpos($did, "NETWORK_DEVICE-") === false) {
                $sql = "SELECT macaddr FROM networks WHERE hardware_id='%s'";
                $resNetm = mysql2_query_secure($sql, $_SESSION['OCS']["readServer"], $idHard);
                while ($valNetm = mysqli_fetch_array($resNetm)) {
                    $sql = "DELETE FROM netmap WHERE mac='%s'";
                    mysql2_query_secure($sql, $_SESSION['OCS']["readServer"], $valNetm["macaddr"]);
                }
            }
            //deleting a regular computer
            if ($did != "_SYSTEMGROUP_" && $did != '_DOWNLOADGROUP_') {
                $tables = $_SESSION['OCS']['SQL_TABLE_HARDWARE_ID'];
            } elseif ($did == "_SYSTEMGROUP_" || $did == '_DOWNLOADGROUP_') {//Deleting a group
                $tables = Array("devices");
                //del messages on this group
                $sql_group_msg = "DELETE FROM config WHERE name like '%s' and ivalue='%s'";
                mysql2_query_secure($sql_group_msg, $_SESSION['OCS']["writeServer"], array('GUI_REPORT_MSG%', $idHard));
                $sql_group = "DELETE FROM groups WHERE hardware_id='%s'";
                mysql2_query_secure($sql_group, $_SESSION['OCS']["writeServer"], $idHard);
                $sql_group_cache = "DELETE FROM groups_cache WHERE group_id='%s'";
                $resDelete = mysql2_query_secure($sql_group_cache, $_SESSION['OCS']["writeServer"], $idHard);
                $affectedComputers = mysqli_affected_rows($_SESSION['OCS']["writeServer"]);
                //Deleting redistribution group
                $sql_group_cache = "DELETE FROM download_servers WHERE group_id='%s'";
                $resDelete = mysql2_query_secure($sql_group_cache, $_SESSION['OCS']["writeServer"], $idHard);
                $affectedComputers = mysqli_affected_rows($_SESSION['OCS']["writeServer"]);
            }

            if (!$silent) {
                msg_success($valId["name"] . " " . $l->g(220));
            }

            if (isset($tables) && is_array($tables)) {
                foreach ($tables as $table) {
                    $sql = "DELETE FROM %s WHERE hardware_id='%s'";
                    $arg = array($table, $idHard);
                    mysql2_query_secure($sql, $_SESSION['OCS']["writeServer"], $arg);
                }
            }
            $sql = "delete from download_enable where SERVER_ID='%s'";
            mysql2_query_secure($sql, $_SESSION['OCS']["writeServer"], $idHard);
            $sql = "DELETE FROM hardware WHERE id='%s'";
            mysql2_query_secure($sql, $_SESSION['OCS']["writeServer"], $idHard);
            //Deleted computers tracking
            if ($traceDel && mysqli_num_rows(mysql2_query_secure("SELECT IVALUE FROM config WHERE IVALUE>0 AND NAME='TRACE_DELETED'", $_SESSION['OCS']["readServer"]))) {
                $sql = "insert into deleted_equiv(DELETED,EQUIVALENT) values('%s',%s)";
                $arg = array($idHard, 'NULL');
                mysql2_query_secure($sql, $_SESSION['OCS']["writeServer"], $arg);
            }
        }
        //Using lock ? Unlock
        if ($checkLock) {
            unlock($id);
        }
        return $valId["name"];
    } else {
        errlock();
    }
}

function fusionne($afus) {
    global $l;
    $i = 0;
    $maxStamp = 0;
    $minStamp = mktime(0, 0, 0, date("m"), date("d") + 1, date("Y")); //demain
    foreach ($afus as $a) {
        $d = $a["lastcome"];
        $param = array();
        $param[] = (int) $d[11] . $d[12];
        $param[] = (int) $d[14] . $d[15];
        $param[] = (int) $d[17] . $d[18];
        $param[] = (int) $d[5] . $d[6];
        $param[] = (int) $d[8] . $d[9];
        $param[] = (int) $d[0] . $d[1] . $d[2] . $d[3];
        $a["stamp"] = mktime($param[0], $param[1], $param[2], $param[3], $param[4], $param[5]);
        if ($maxStamp < $a["stamp"]) {
            $maxStamp = $a["stamp"];
            $maxInd = $i;
        }
        if ($minStamp > $a["stamp"]) {
            $minStamp = $a["stamp"];
            $minInd = $i;
        }
        $i++;
    }
    if ($afus[$minInd]["deviceid"] != "") {
        $okLock = true;
        foreach ($afus as $a) {
            if (!$okLock = ($okLock && lock($a["id"]))) {
                break;
            } else {
                $locked[] = $a["id"];
            }
        }

        if ($okLock) {
            //TRACE_DELETED
            if (mysqli_num_rows(mysql2_query_secure("SELECT * FROM config WHERE IVALUE>0 AND NAME='TRACE_DELETED'", $_SESSION['OCS']["readServer"]))) {
                foreach ($afus as $a) {
                    if ($afus[$maxInd]["deviceid"] == $a["deviceid"]) {
                        continue;
                    }
                    $sql = "insert into deleted_equiv(DELETED,EQUIVALENT) values('%s','%s')";
                    $arg = array($a["id"], $afus[$maxInd]["id"]);
                    mysql2_query_secure($sql, $_SESSION['OCS']["writeServer"], $arg);
                }
            }

            //KEEP OLD QUALITY,FIDELITY AND CHECKSUM
            $sql = "SELECT CHECKSUM,QUALITY,FIDELITY FROM hardware WHERE ID='%s'";
            $persistent_req = mysql2_query_secure($sql, $_SESSION['OCS']["readServer"], $afus[$minInd]["id"]);

            $reqDelAccount = "DELETE FROM accountinfo WHERE hardware_id='%s'";
            mysql2_query_secure($reqDelAccount, $_SESSION['OCS']["writeServer"], $afus[$maxInd]["id"]);
            msg_success($l->g(190) . " " . $afus[$maxInd]["deviceid"] . " " . $l->g(191));

            $keep = array("accountinfo", "devices", "groups_cache");
            foreach ($keep as $tableToBeKept) {
                $reqRecupAccount = "UPDATE %s SET hardware_id='%s' WHERE hardware_id='%s'";
                $argRecupAccount = array($tableToBeKept, $afus[$maxInd]["id"], $afus[$minInd]["id"]);
                mysql2_query_secure($reqRecupAccount, $_SESSION['OCS']["writeServer"], $argRecupAccount);
            }
            msg_success($l->g(190) . " " . $afus[$minInd]["deviceid"] . " " . $l->g(206) . " " . $afus[$maxInd]["deviceid"]);
            $i = 0;
            foreach ($afus as $a) {
                if ($i != $maxInd) {
                    deleteDid($a["id"], false, false, false);
                    $lesDel .= $a["deviceid"] . "/";
                }
                $i++;
            }

            //RESTORE PERSISTENT VALUES
            $persistent_values = mysqli_fetch_row($persistent_req);
            $sql = "UPDATE hardware SET QUALITY=%s,FIDELITY=%s,CHECKSUM=CHECKSUM|%s WHERE id='%s'";
            $arg = array($persistent_values[1], $persistent_values[2], $persistent_values[0], $afus[$maxInd]["id"]);
            mysql2_query_secure($sql, $_SESSION['OCS']["writeServer"], $arg);
        } else {
            errlock();
        }

        foreach ($locked as $a) {
            unlock($a);
        }
    }
    $lesDel .= " => " . $afus[$maxInd]["deviceid"];
    AddLog("FUSION", $lesDel);
}

function insert_manual_computer($values, $nb = 1) {
    global $i;
    if ($nb == 1) {
        $name = $values['COMPUTER_NAME_GENERIC'];
    } else {
        $name = $values['COMPUTER_NAME_GENERIC'] . $i;
    }

    $sql = "insert into hardware (deviceid,name) values ('%s','%s')";
    $arg = array('MANUEL', $name . '_M');
    mysql2_query_secure($sql, $_SESSION['OCS']["writeServer"], $arg);
    $id_computer = mysqli_insert_id($_SESSION['OCS']["writeServer"]);

    $sql = "insert into bios (hardware_id,ssn) values ('%s','%s')";
    $arg = array($id_computer, $values['SERIAL_GENERIC'] . '_M');
    mysql2_query_secure($sql, $_SESSION['OCS']["writeServer"], $arg);

    $sql = "insert into networks (hardware_id,macaddr) values ('%s','%s')";
    $arg = array($id_computer, $values['ADDR_MAC_GENERIC'] . '_M');
    mysql2_query_secure($sql, $_SESSION['OCS']["writeServer"], $arg);

    return $id_computer;
}

/*
 * function to verify if user can access
 * on computer
 */

function is_mine_computer($id) {
    if (isset($_SESSION['OCS']['TAGS']) && is_array($_SESSION['OCS']['TAGS'])) {
        $sql = "select hardware_id from accountinfo where hardware_id = %s and tag in ";
        $arg = array($id);
        $sql = mysql2_prepare($sql, $arg, $_SESSION['OCS']['TAGS']);
        $result = mysql2_query_secure($sql['SQL'], $_SESSION['OCS']["readServer"], $sql['ARG']);
        $item = mysqli_fetch_object($result);
        if (!isset($item->hardware_id)) {
            return false;
        }
    }
    return true;
}

function RandomMAC() {
    $word = "A,B,C,D,E,F,0,1,2,3,4,5,6,7,8,9";
    $mac = '';
    $array = explode(",", $word);
    while ($j < 8) {
        $i = 0;
        while ($i < 2) {
            shuffle($array);
            $mac .= $array[0];
            $i++;
        }
        $mac .= ":";
        $j++;
    }

    return substr($mac, 0, -1);
}

?>