ryxeo-glpi-git / htdocs / inc / connection.function.php @ 1c14bcc4
Historique | Voir | Annoter | Télécharger (13,5 ko)
1 | 1c14bcc4 | Eric Seigne | <?php
|
---|---|---|---|
2 | /*
|
||
3 | * @version $Id: connection.function.php 7763 2009-01-06 18:44:50Z moyo $
|
||
4 | -------------------------------------------------------------------------
|
||
5 | GLPI - Gestionnaire Libre de Parc Informatique
|
||
6 | Copyright (C) 2003-2009 by the INDEPNET Development Team.
|
||
7 | |||
8 | http://indepnet.net/ http://glpi-project.org
|
||
9 | -------------------------------------------------------------------------
|
||
10 | |||
11 | LICENSE
|
||
12 | |||
13 | This file is part of GLPI.
|
||
14 | |||
15 | GLPI is free software; you can redistribute it and/or modify
|
||
16 | it under the terms of the GNU General Public License as published by
|
||
17 | the Free Software Foundation; either version 2 of the License, or
|
||
18 | (at your option) any later version.
|
||
19 | |||
20 | GLPI is distributed in the hope that it will be useful,
|
||
21 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
23 | GNU General Public License for more details.
|
||
24 | |||
25 | You should have received a copy of the GNU General Public License
|
||
26 | along with GLPI; if not, write to the Free Software
|
||
27 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||
28 | --------------------------------------------------------------------------
|
||
29 | */
|
||
30 | |||
31 | // ----------------------------------------------------------------------
|
||
32 | // Original Author of file:
|
||
33 | // Purpose of file:
|
||
34 | // ----------------------------------------------------------------------
|
||
35 | |||
36 | if (!defined('GLPI_ROOT')){ |
||
37 | die("Sorry. You can't access directly to this file"); |
||
38 | } |
||
39 | |||
40 | /**
|
||
41 | * Prints a direct connection to a computer
|
||
42 | *
|
||
43 | * @param $target the page where we'll print out this.
|
||
44 | * @param $ID the connection ID
|
||
45 | * @param $type the connection type
|
||
46 | * @return nothing (print out a table)
|
||
47 | *
|
||
48 | */
|
||
49 | function showConnect($target,$ID,$type) { |
||
50 | // Prints a direct connection to a computer
|
||
51 | |||
52 | global $LANG, $CFG_GLPI; |
||
53 | |||
54 | $connect = new Connection; |
||
55 | |||
56 | // Is global connection ?
|
||
57 | $global=0; |
||
58 | $ci=new CommonItem(); |
||
59 | if (haveTypeRight($type,"r")){ |
||
60 | $canedit=haveTypeRight($type,"w"); |
||
61 | |||
62 | $ci->getFromDB($type,$ID); |
||
63 | $global=$ci->getField('is_global'); |
||
64 | |||
65 | $computers = $connect->getComputersContact($type,$ID); |
||
66 | |||
67 | if (!$computers) $nb=0; |
||
68 | else $nb=count($computers); |
||
69 | |||
70 | echo "<br><div class='center'><table width='50%' class='tab_cadre'><tr><th colspan='2'>"; |
||
71 | echo $LANG["connect"][0].": ".$nb; |
||
72 | echo "</th></tr>"; |
||
73 | |||
74 | if ($computers&&count($computers)>0) { |
||
75 | foreach ($computers as $key => $computer){ |
||
76 | echo "<tr><td class='tab_bg_1".($computer['deleted']?"_2":"")."'><strong>".$LANG["help"][25].": "; |
||
77 | echo "<a href=\"".$CFG_GLPI["root_doc"]."/front/computer.form.php?ID=".$computer['ID']."\">"; |
||
78 | echo $computer['name']; |
||
79 | if ($CFG_GLPI["view_ID"]||empty($computer['name'])) echo " (".$computer['ID'].")"; |
||
80 | echo "</a>"; |
||
81 | echo "</strong></td>"; |
||
82 | echo "<td class='tab_bg_2".($computer['deleted']?"_2":"")."' align='center'><strong>"; |
||
83 | if ($canedit){ |
||
84 | echo "<a href=\"$target?disconnect=1&cID=".$computer['ID']."&ID=".$key."\">".$LANG["buttons"][10]."</a>"; |
||
85 | } else {
|
||
86 | echo " "; |
||
87 | } |
||
88 | echo "</strong>"; |
||
89 | } |
||
90 | } else {
|
||
91 | echo "<tr><td class='tab_bg_1'><strong>".$LANG["help"][25].": </strong>"; |
||
92 | echo "<i>".$LANG["connect"][1]."</i>"; |
||
93 | echo "</td>"; |
||
94 | echo "<td class='tab_bg_2' align='center'>"; |
||
95 | if ($canedit){ |
||
96 | echo "<form method='post' action=\"$target\">"; |
||
97 | echo "<input type='hidden' name='connect' value='connect'>"; |
||
98 | echo "<input type='hidden' name='sID' value='$ID'>"; |
||
99 | echo "<input type='hidden' name='device_type' value='$type'>"; |
||
100 | dropdownConnect(COMPUTER_TYPE,$type,"item",$ci->getField('FK_entities')); |
||
101 | echo "<input type='submit' value=\"".$LANG["buttons"][9]."\" class='submit'>"; |
||
102 | echo "</form>"; |
||
103 | } else echo " "; |
||
104 | |||
105 | } |
||
106 | |||
107 | if ($global&&$computers&&count($computers)>0){ |
||
108 | echo "</td>"; |
||
109 | echo "</tr>"; |
||
110 | echo "<tr><td class='tab_bg_1'> "; |
||
111 | echo "</td>"; |
||
112 | echo "<td class='tab_bg_2' align='center'>"; |
||
113 | echo "<form method='post' action=\"$target\">"; |
||
114 | echo "<input type='hidden' name='connect' value='connect'>"; |
||
115 | echo "<input type='hidden' name='sID' value='$ID'>"; |
||
116 | echo "<input type='hidden' name='device_type' value='$type'>"; |
||
117 | dropdownConnect(COMPUTER_TYPE,$type,"item",$ci->getField('FK_entities')); |
||
118 | echo "<input type='submit' value=\"".$LANG["buttons"][9]."\" class='submit'>"; |
||
119 | |||
120 | echo "</form>"; |
||
121 | |||
122 | } |
||
123 | |||
124 | echo "</td>"; |
||
125 | echo "</tr>"; |
||
126 | echo "</table></div><br>"; |
||
127 | } |
||
128 | } |
||
129 | |||
130 | |||
131 | |||
132 | /**
|
||
133 | * Disconnects a direct connection
|
||
134 | *
|
||
135 | *
|
||
136 | * @param $ID the connection ID to disconnect.
|
||
137 | * @param $dohistory make history
|
||
138 | * @param $doautoactions make auto actions on disconnect
|
||
139 | * @param $ocs_server_id ocs server id of the computer if know
|
||
140 | * @return nothing
|
||
141 | */
|
||
142 | function Disconnect($ID,$dohistory=1,$doautoactions=true,$ocs_server_id=0) { |
||
143 | global $DB,$LINK_ID_TABLE,$LANG,$CFG_GLPI; |
||
144 | |||
145 | |||
146 | //Get info about the periph
|
||
147 | $query = "SELECT end1,end2, type FROM glpi_connect_wire WHERE ID='$ID'"; |
||
148 | $res = $DB->query($query); |
||
149 | |||
150 | if($DB->numrows($res)>0){ |
||
151 | |||
152 | // Init
|
||
153 | if ($dohistory || $doautoactions){ |
||
154 | $data = $DB->fetch_array($res); |
||
155 | |||
156 | $decoConf = ""; |
||
157 | $type_elem= $data["type"]; |
||
158 | $id_elem= $data["end1"]; |
||
159 | $id_parent= $data["end2"]; |
||
160 | $table = $LINK_ID_TABLE[$type_elem]; |
||
161 | |||
162 | |||
163 | //Get the computer name
|
||
164 | $computer = new Computer; |
||
165 | $computer->getFromDB($id_parent); |
||
166 | //Get device fields
|
||
167 | $device=new CommonItem(); |
||
168 | $device->getFromDB($type_elem,$id_elem); |
||
169 | } |
||
170 | |||
171 | if ($dohistory){ |
||
172 | |||
173 | //History log
|
||
174 | //Log deconnection in the computer's history
|
||
175 | $changes[0]='0'; |
||
176 | if ($device->getField("serial")){ |
||
177 | $changes[1]=addslashes($device->getField("name")." -- ".$device->getField("serial")); |
||
178 | } else {
|
||
179 | $changes[1]=addslashes($device->getField("name")); |
||
180 | } |
||
181 | |||
182 | $changes[2]=""; |
||
183 | |||
184 | historyLog ($id_parent,COMPUTER_TYPE,$changes,$type_elem,HISTORY_DISCONNECT_DEVICE); |
||
185 | |||
186 | //Log deconnection in the device's history
|
||
187 | $changes[1]=addslashes($computer->fields["name"]); |
||
188 | historyLog ($id_elem,$type_elem,$changes,COMPUTER_TYPE,HISTORY_DISCONNECT_DEVICE); |
||
189 | } |
||
190 | if ($doautoactions){ |
||
191 | if (!$device->getField('is_global')){ |
||
192 | |||
193 | $updates=array(); |
||
194 | if ($CFG_GLPI["autoclean_link_location"] && $device->getField('location')){ |
||
195 | $updates[]="location"; |
||
196 | $device->obj->fields['location']=0; |
||
197 | } |
||
198 | if ($CFG_GLPI["autoclean_link_user"] && $device->getField('FK_users')) { |
||
199 | $updates[]="FK_users"; |
||
200 | $device->obj->fields['FK_users']=0; |
||
201 | } |
||
202 | if ($CFG_GLPI["autoclean_link_group"] && $device->getField('FK_groups')){ |
||
203 | $updates[]="FK_groups"; |
||
204 | $device->obj->fields['FK_groups']=0; |
||
205 | } |
||
206 | if ($CFG_GLPI["autoclean_link_contact"] && $device->getField('contact')){ |
||
207 | $updates[]="contact"; |
||
208 | $device->obj->fields['contact']=""; |
||
209 | } |
||
210 | if ($CFG_GLPI["autoclean_link_contact"] && $device->getField('contact_num')){ |
||
211 | $updates[]="contact_num"; |
||
212 | $device->obj->fields['contact_num']=""; |
||
213 | } |
||
214 | if ($CFG_GLPI["autoclean_link_state"]<0 && $device->getField('state')) { |
||
215 | $updates[]="state"; |
||
216 | $device->obj->fields['state']=0; |
||
217 | } |
||
218 | if ($CFG_GLPI["autoclean_link_state"]>0 && $device->getField('state')!=$CFG_GLPI["autoclean_link_state"]) { |
||
219 | $updates[]="state"; |
||
220 | $device->obj->fields['state']=$CFG_GLPI["autoclean_link_state"]; |
||
221 | } |
||
222 | if (count($updates)) { |
||
223 | $device->obj->updateInDB($updates); |
||
224 | } |
||
225 | } |
||
226 | |||
227 | if ($ocs_server_id==0){ |
||
228 | $ocs_server_id = getOCSServerByMachineID($data["end2"]); |
||
229 | } |
||
230 | if ($ocs_server_id>0){ |
||
231 | |||
232 | //Get OCS configuration
|
||
233 | $ocs_config = getOcsConf($ocs_server_id); |
||
234 | |||
235 | //Get the management mode for this device
|
||
236 | $mode = getMaterialManagementMode($ocs_config,$type_elem); |
||
237 | $decoConf= $ocs_config["deconnection_behavior"]; |
||
238 | |||
239 | //Change status if :
|
||
240 | // 1 : the management mode IS NOT global
|
||
241 | // 2 : a deconnection's status have been defined
|
||
242 | // 3 : unique with serial
|
||
243 | if($mode >= 2 && strlen($decoConf)>0){ |
||
244 | //Delete periph from glpi
|
||
245 | if($decoConf == "delete") |
||
246 | $device->obj->delete($id_elem); |
||
247 | |||
248 | //Put periph in trash
|
||
249 | else if($decoConf == "trash") |
||
250 | { |
||
251 | $tmp["ID"]=$id_elem; |
||
252 | $tmp["deleted"]=1; |
||
253 | $device->obj->update($tmp,$dohistory); |
||
254 | } |
||
255 | } |
||
256 | } // $ocs_server_id>0
|
||
257 | } |
||
258 | |||
259 | // Disconnects a direct connection
|
||
260 | $connect = new Connection; |
||
261 | $connect->deletefromDB($ID); |
||
262 | |||
263 | } |
||
264 | |||
265 | } |
||
266 | |||
267 | |||
268 | /**
|
||
269 | * Makes a direct connection
|
||
270 | *
|
||
271 | * @param $sID connection source ID.
|
||
272 | * @param $cID computer ID (where the sID would be connected).
|
||
273 | * @param $type connection type.
|
||
274 | * @param $dohistory store change in history ?
|
||
275 | */
|
||
276 | function Connect($sID,$cID,$type,$dohistory=1) { |
||
277 | global $LANG,$CFG_GLPI,$DB; |
||
278 | // Makes a direct connection
|
||
279 | |||
280 | // Mise a jour lieu du periph si nécessaire
|
||
281 | $dev=new CommonItem(); |
||
282 | $dev->getFromDB($type,$sID); |
||
283 | |||
284 | // Handle case where already used, should never happen (except from OCS sync)
|
||
285 | if (!$dev->getField('is_global') ){ |
||
286 | $query = "SELECT ID,end2 FROM glpi_connect_wire WHERE glpi_connect_wire.end1 = '$sID' AND glpi_connect_wire.type = '$type'"; |
||
287 | $result = $DB->query($query); |
||
288 | while ($data=$DB->fetch_assoc($result)){ |
||
289 | Disconnect($data["ID"],$dohistory); |
||
290 | |||
291 | // As we come from OCS, do not lock the device
|
||
292 | switch ($type) { |
||
293 | case MONITOR_TYPE: |
||
294 | deleteInOcsArray($data["end2"],$data["ID"],"import_monitor"); |
||
295 | break;
|
||
296 | case DEVICE_TYPE: |
||
297 | deleteInOcsArray($data["end2"],$data["ID"],"import_device"); |
||
298 | break;
|
||
299 | case PERIPHERAL_TYPE: |
||
300 | deleteInOcsArray($data["end2"],$data["ID"],"import_peripheral"); |
||
301 | break;
|
||
302 | case PRINTER_TYPE: |
||
303 | deleteInOcsArray($data["end2"],$data["ID"],"import_printers"); |
||
304 | break;
|
||
305 | } |
||
306 | } |
||
307 | } |
||
308 | |||
309 | // Create the New connexion
|
||
310 | $connect = new Connection; |
||
311 | $connect->end1=$sID; |
||
312 | $connect->end2=$cID; |
||
313 | $connect->type=$type; |
||
314 | $newID=$connect->addtoDB(); |
||
315 | |||
316 | if ($dohistory){ |
||
317 | $changes[0]='0'; |
||
318 | $changes[1]=""; |
||
319 | if ($dev->getField("serial")){ |
||
320 | $changes[2]=addslashes($dev->getField("name")." -- ".$dev->getField("serial")); |
||
321 | } else {
|
||
322 | $changes[2]=addslashes($dev->getField("name")); |
||
323 | } |
||
324 | |||
325 | //Log connection in the device's history
|
||
326 | historyLog ($cID,COMPUTER_TYPE,$changes,$type,HISTORY_CONNECT_DEVICE); |
||
327 | } |
||
328 | |||
329 | if (!$dev->getField('is_global')){ |
||
330 | $comp=new Computer(); |
||
331 | $comp->getFromDB($cID); |
||
332 | |||
333 | if ($dohistory){ |
||
334 | $changes[2]=addslashes($comp->fields["name"]); |
||
335 | historyLog ($sID,$type,$changes,COMPUTER_TYPE,HISTORY_CONNECT_DEVICE); |
||
336 | } |
||
337 | |||
338 | if ($CFG_GLPI["autoupdate_link_location"]&&$comp->fields['location']!=$dev->getField('location')){ |
||
339 | $updates[0]="location"; |
||
340 | $dev->obj->fields['location']=addslashes($comp->fields['location']); |
||
341 | $dev->obj->updateInDB($updates); |
||
342 | addMessageAfterRedirect($LANG["computers"][48],true); |
||
343 | } |
||
344 | if (($CFG_GLPI["autoupdate_link_user"]&&$comp->fields['FK_users']!=$dev->getField('FK_users')) |
||
345 | ||($CFG_GLPI["autoupdate_link_group"]&&$comp->fields['FK_groups']!=$dev->getField('FK_groups'))){ |
||
346 | if ($CFG_GLPI["autoupdate_link_user"]){ |
||
347 | $updates[]="FK_users"; |
||
348 | $dev->obj->fields['FK_users']=$comp->fields['FK_users']; |
||
349 | } |
||
350 | if ($CFG_GLPI["autoupdate_link_group"]){ |
||
351 | $updates[]="FK_groups"; |
||
352 | $dev->obj->fields['FK_groups']=$comp->fields['FK_groups']; |
||
353 | } |
||
354 | $dev->obj->updateInDB($updates); |
||
355 | addMessageAfterRedirect($LANG["computers"][50],true); |
||
356 | } |
||
357 | |||
358 | if ($CFG_GLPI["autoupdate_link_contact"] |
||
359 | &&($comp->fields['contact']!=$dev->getField('contact')||$comp->fields['contact_num']!=$dev->getField('contact_num'))){ |
||
360 | $updates[0]="contact"; |
||
361 | $updates[1]="contact_num"; |
||
362 | $dev->obj->fields['contact']=addslashes($comp->fields['contact']); |
||
363 | $dev->obj->fields['contact_num']=addslashes($comp->fields['contact_num']); |
||
364 | $dev->obj->updateInDB($updates); |
||
365 | addMessageAfterRedirect($LANG["computers"][49],true); |
||
366 | } |
||
367 | if ($CFG_GLPI["autoupdate_link_state"]<0 && $comp->fields['state']!=$dev->getField('state')) { |
||
368 | $updates[0]="state"; |
||
369 | $dev->obj->fields['state']=$comp->fields['state']; |
||
370 | $dev->obj->updateInDB($updates); |
||
371 | addMessageAfterRedirect($LANG["computers"][56],true); |
||
372 | } |
||
373 | if ($CFG_GLPI["autoupdate_link_state"]>0 && $dev->getField('state')!=$CFG_GLPI["autoupdate_link_state"]) { |
||
374 | $updates[0]="state"; |
||
375 | $dev->obj->fields['state']=$CFG_GLPI["autoupdate_link_state"]; |
||
376 | $dev->obj->updateInDB($updates); |
||
377 | } |
||
378 | } |
||
379 | return $newID; |
||
380 | } |
||
381 | |||
382 | /**
|
||
383 | * Get the connection count of an item
|
||
384 | *
|
||
385 | * @param $type item type
|
||
386 | * @param $ID item ID
|
||
387 | * @return integer connection count
|
||
388 | */
|
||
389 | function getNumberConnections($type,$ID){ |
||
390 | global $DB; |
||
391 | $query = "SELECT count(*) FROM glpi_connect_wire INNER JOIN glpi_computers ON ( glpi_connect_wire.end2=glpi_computers.ID ) WHERE glpi_connect_wire.end1 = '$ID' AND glpi_connect_wire.type = '$type' AND glpi_computers.deleted='0' AND glpi_computers.is_template='0'"; |
||
392 | |||
393 | $result = $DB->query($query); |
||
394 | |||
395 | if ($DB->numrows($result)!=0){ |
||
396 | return $DB->result($result,0,0); |
||
397 | } else return 0; |
||
398 | |||
399 | } |
||
400 | |||
401 | /**
|
||
402 | * Unglobalize an item : duplicate item and connections
|
||
403 | *
|
||
404 | * @param $device_type item type
|
||
405 | * @param $ID item ID
|
||
406 | */
|
||
407 | function unglobalizeDevice($device_type,$ID){ |
||
408 | global $DB; |
||
409 | $ci=new CommonItem(); |
||
410 | // Update item to unit management :
|
||
411 | $ci->getFromDB($device_type,$ID); |
||
412 | if ($ci->getField('is_global')){ |
||
413 | $input=array("ID"=>$ID,"is_global"=>"0"); |
||
414 | $ci->obj->update($input); |
||
415 | |||
416 | // Get connect_wire for this connection
|
||
417 | $query = "SELECT glpi_connect_wire.ID AS connectID FROM glpi_connect_wire WHERE glpi_connect_wire.end1 = '$ID' AND glpi_connect_wire.type = '$device_type'"; |
||
418 | $result=$DB->query($query); |
||
419 | if (($nb=$DB->numrows($result))>1){ |
||
420 | for ($i=1;$i<$nb;$i++){ |
||
421 | // Get ID of the computer
|
||
422 | if ($data=$DB->fetch_array($result)){ |
||
423 | // Add new Item
|
||
424 | unset($ci->obj->fields['ID']); |
||
425 | if ($newID=$ci->obj->add(array("ID"=>$ID))){ |
||
426 | // Update Connection
|
||
427 | $query2="UPDATE glpi_connect_wire SET end1='$newID' WHERE ID='".$data["connectID"]."'"; |
||
428 | $DB->query($query2); |
||
429 | } |
||
430 | |||
431 | } |
||
432 | } |
||
433 | |||
434 | } |
||
435 | } |
||
436 | } |
||
437 | |||
438 | ?> |