ryxeo-glpi-git / inc / networking.function.php @ b67d8923
Historique | Voir | Annoter | Télécharger (22,9 ko)
1 |
<?php
|
---|---|
2 |
/*
|
3 |
* @version $Id: networking.function.php 7875 2009-01-23 15:16:47Z 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 |
///// Manage Netdevices /////
|
41 |
|
42 |
|
43 |
///// Manage Ports on Devices /////
|
44 |
|
45 |
function showPorts ($device,$device_type,$withtemplate='') { |
46 |
|
47 |
global $DB,$CFG_GLPI, $LANG,$LINK_ID_TABLE; |
48 |
|
49 |
if (!haveRight("networking","r")) return false; |
50 |
$canedit=haveRight("networking","w"); |
51 |
|
52 |
$device_real_table_name = $LINK_ID_TABLE[$device_type]; |
53 |
|
54 |
$query = "SELECT ID FROM glpi_networking_ports WHERE (on_device = '$device' AND device_type = '$device_type') ORDER BY name, logical_number"; |
55 |
if ($result = $DB->query($query)) { |
56 |
if ($DB->numrows($result)!=0) { |
57 |
$colspan=8; |
58 |
if ($withtemplate!=2){ |
59 |
echo "<form id='networking_ports' name='networking_ports' method='post' action=\"".$CFG_GLPI["root_doc"]."/front/networking.port.php\">"; |
60 |
if ($canedit) |
61 |
$colspan++;
|
62 |
} |
63 |
|
64 |
echo "<div class='center'><table class='tab_cadrehov'>"; |
65 |
echo "<tr>"; |
66 |
echo "<th colspan='$colspan'>"; |
67 |
echo $DB->numrows($result)." "; |
68 |
if ($DB->numrows($result)<2) { |
69 |
echo $LANG["networking"][37]; |
70 |
} else {
|
71 |
echo $LANG["networking"][13]; |
72 |
} |
73 |
echo ":</th>"; |
74 |
|
75 |
echo "</tr>"; |
76 |
echo "<tr>"; |
77 |
if ($withtemplate!=2&&$canedit){ |
78 |
echo "<th> </th>"; |
79 |
} |
80 |
echo "<th>#</th><th>".$LANG["common"][16]."</th><th>".$LANG["networking"][51]."</th>"; |
81 |
echo "<th>".$LANG["networking"][14]."<br>".$LANG["networking"][15]."</th>"; |
82 |
echo "<th>".$LANG["networking"][60]." / ".$LANG["networking"][61]; |
83 |
|
84 |
echo "<br>".$LANG["networking"][59]."</th>"; |
85 |
|
86 |
echo "<th>".$LANG["networking"][56]."</th>"; |
87 |
echo "<th>".$LANG["common"][65]."</th>"; |
88 |
|
89 |
echo"<th>".$LANG["networking"][17].":</th></tr>\n"; |
90 |
$i=0; |
91 |
while ($devid=$DB->fetch_row($result)) { |
92 |
$netport = new Netport; |
93 |
$netport->getFromDB(current($devid)); |
94 |
echo "<tr class='tab_bg_1'>"; |
95 |
if ($withtemplate!=2&&$canedit){ |
96 |
echo "<td align='center' width='20'><input type='checkbox' name='del_port[".$netport->fields["ID"]."]' value='1'></td>"; |
97 |
} |
98 |
echo "<td class='center'><strong>"; |
99 |
if ($withtemplate!=2) echo "<a href=\"".$CFG_GLPI["root_doc"]."/front/networking.port.php?ID=".$netport->fields["ID"]."\">"; |
100 |
echo $netport->fields["logical_number"]; |
101 |
if ($withtemplate!=2) echo "</a>"; |
102 |
echo "</strong></td>"; |
103 |
echo "<td>".$netport->fields["name"]."</td>"; |
104 |
echo "<td>".getDropdownName("glpi_dropdown_netpoint",$netport->fields["netpoint"])."</td>"; |
105 |
echo "<td>".$netport->fields["ifaddr"]."<br>"; |
106 |
echo $netport->fields["ifmac"]."</td>"; |
107 |
echo "<td>".$netport->fields["netmask"]." / "; |
108 |
echo $netport->fields["subnet"]."<br>"; |
109 |
echo $netport->fields["gateway"]."</td>"; |
110 |
// VLANs
|
111 |
echo "<td>"; |
112 |
showPortVLAN($netport->fields["ID"],$withtemplate); |
113 |
echo "</td>"; |
114 |
echo "<td>".getDropdownName("glpi_dropdown_iface",$netport->fields["iface"])."</td>"; |
115 |
echo "<td width='300'>"; |
116 |
showConnection($netport->fields["ID"],$withtemplate,$device_type); |
117 |
echo "</td>"; |
118 |
echo "</tr>"; |
119 |
} |
120 |
echo "</table>"; |
121 |
echo "</div>\n\n"; |
122 |
|
123 |
if ($canedit&&$withtemplate!=2){ |
124 |
echo "<div class='center'>"; |
125 |
echo "<table width='80%'>"; |
126 |
echo "<tr><td><img src=\"".$CFG_GLPI["root_doc"]."/pics/arrow-left.png\" alt=''></td><td class='center'><a onclick= \"if ( markAllRows('networking_ports') ) return false;\" href='".$_SERVER['PHP_SELF']."?ID=$device&select=all'>".$LANG["buttons"][18]."</a></td>"; |
127 |
|
128 |
echo "<td>/</td><td class='center'><a onclick= \"if ( unMarkAllRows('networking_ports') ) return false;\" href='".$_SERVER['PHP_SELF']."?ID=$device&select=none'>".$LANG["buttons"][19]."</a>"; |
129 |
echo "</td>"; |
130 |
echo "<td width='80%' align='left'>"; |
131 |
dropdownMassiveActionPorts($device_type);
|
132 |
echo "</td>"; |
133 |
echo "</table>"; |
134 |
|
135 |
echo "</div>"; |
136 |
} else {
|
137 |
echo "<br>"; |
138 |
} |
139 |
if ($withtemplate!=2){ |
140 |
echo "</form>"; |
141 |
} |
142 |
} |
143 |
} |
144 |
} |
145 |
|
146 |
function showPortVLAN($ID,$withtemplate,$referer=''){ |
147 |
global $DB,$CFG_GLPI,$LANG; |
148 |
|
149 |
$canedit=haveRight("networking","w"); |
150 |
|
151 |
|
152 |
|
153 |
$query="SELECT * from glpi_networking_vlan WHERE FK_port='$ID'"; |
154 |
$result=$DB->query($query); |
155 |
if ($DB->numrows($result)>0){ |
156 |
echo "<table cellpadding='0' cellspacing='0'>"; |
157 |
while ($line=$DB->fetch_array($result)){ |
158 |
echo "<tr><td>".getDropdownName("glpi_dropdown_vlan",$line["FK_vlan"]); |
159 |
echo "</td><td>"; |
160 |
if ($canedit){ |
161 |
echo "<a href='".$CFG_GLPI["root_doc"]."/front/networking.port.php?unassign_vlan=unassigned&ID=".$line["ID"]."&referer=$referer'>"; |
162 |
echo "<img src=\"".$CFG_GLPI["root_doc"]."/pics/delete2.png\" alt='".$LANG["buttons"][6]."' title='".$LANG["buttons"][6]."'></a>"; |
163 |
} else echo " "; |
164 |
echo "</td></tr>"; |
165 |
} |
166 |
echo "</table>"; |
167 |
} else echo " "; |
168 |
|
169 |
|
170 |
} |
171 |
|
172 |
function assignVlan($port,$vlan){ |
173 |
global $DB; |
174 |
$query="INSERT INTO glpi_networking_vlan (FK_port,FK_vlan) VALUES ('$port','$vlan')"; |
175 |
$DB->query($query); |
176 |
|
177 |
$np=new NetPort(); |
178 |
if ($np->getContact($port)){ |
179 |
$query="INSERT INTO glpi_networking_vlan (FK_port,FK_vlan) VALUES ('".$np->contact_id."','$vlan')"; |
180 |
$DB->query($query); |
181 |
} |
182 |
|
183 |
} |
184 |
|
185 |
function unassignVlanbyID($ID){ |
186 |
global $DB; |
187 |
|
188 |
$query="SELECT * FROM glpi_networking_vlan WHERE ID='$ID'"; |
189 |
if ($result=$DB->query($query)){ |
190 |
$data=$DB->fetch_array($result); |
191 |
|
192 |
// Delete VLAN
|
193 |
$query="DELETE FROM glpi_networking_vlan WHERE ID='$ID'"; |
194 |
$DB->query($query); |
195 |
|
196 |
// Delete Contact VLAN if set
|
197 |
$np=new NetPort(); |
198 |
if ($np->getContact($data['FK_port'])){ |
199 |
$query="DELETE FROM glpi_networking_vlan WHERE FK_port='".$np->contact_id."' AND FK_vlan='".$data['FK_vlan']."'"; |
200 |
$DB->query($query); |
201 |
} |
202 |
} |
203 |
} |
204 |
|
205 |
function unassignVlan($portID,$vlanID){ |
206 |
global $DB; |
207 |
$query="DELETE FROM glpi_networking_vlan WHERE FK_port='$portID' AND FK_vlan='$vlanID'"; |
208 |
$DB->query($query); |
209 |
|
210 |
// Delete Contact VLAN if set
|
211 |
$np=new NetPort(); |
212 |
if ($np->getContact($portID)){ |
213 |
$query="DELETE FROM glpi_networking_vlan WHERE FK_port='".$np->contact_id."' AND FK_vlan='$vlanID'"; |
214 |
$DB->query($query); |
215 |
} |
216 |
|
217 |
} |
218 |
|
219 |
function showNetportForm($target,$ID,$ondevice,$devtype,$several) { |
220 |
|
221 |
global $CFG_GLPI, $LANG, $REFERER; |
222 |
|
223 |
if (!haveRight("networking","r")) return false; |
224 |
|
225 |
$netport = new Netport; |
226 |
if($ID) |
227 |
{ |
228 |
$netport->getFromDB($ID); |
229 |
$netport->getDeviceData($netport->fields["on_device"],$netport->fields["device_type"]); |
230 |
} |
231 |
else
|
232 |
{ |
233 |
$netport->getDeviceData($ondevice,$devtype); |
234 |
$netport->getEmpty();
|
235 |
} |
236 |
|
237 |
// Ajout des infos d��remplies
|
238 |
if (isset($_POST)&&!empty($_POST)){ |
239 |
foreach ($netport->fields as $key => $val) |
240 |
if ($key!='ID'&&isset($_POST[$key])) |
241 |
$netport->fields[$key]=$_POST[$key]; |
242 |
} |
243 |
|
244 |
displayTitle("","","",array($REFERER=>$LANG["buttons"][13])); |
245 |
|
246 |
echo "<br><div>"; |
247 |
|
248 |
echo "<form method='post' action=\"$target\">"; |
249 |
|
250 |
echo "<input type='hidden' name='referer' value='".rawurlencode($REFERER)."'>"; |
251 |
echo "<table class='tab_cadre_fixe'><tr>"; |
252 |
|
253 |
echo "<th colspan='4'>".$LANG["networking"][20].":</th>"; |
254 |
echo "</tr>"; |
255 |
|
256 |
if ($several!="yes"){ |
257 |
echo "<tr class='tab_bg_1'><td>".$LANG["networking"][21].":</td>"; |
258 |
echo "<td>"; |
259 |
autocompletionTextField("logical_number","glpi_networking_ports","logical_number",$netport->fields["logical_number"],5); |
260 |
echo "</td></tr>"; |
261 |
} |
262 |
else {
|
263 |
echo "<tr class='tab_bg_1'><td>".$LANG["networking"][21].":</td>"; |
264 |
echo "<input type='hidden' name='several' value='yes'>"; |
265 |
echo "<input type='hidden' name='logical_number' value=''>"; |
266 |
echo "<td>"; |
267 |
echo $LANG["networking"][47].": "; |
268 |
dropdownInteger('from_logical_number',0,0,100); |
269 |
echo $LANG["networking"][48].": "; |
270 |
dropdownInteger('to_logical_number',0,0,100); |
271 |
echo "</td></tr>"; |
272 |
} |
273 |
|
274 |
echo "<tr class='tab_bg_1'><td>".$LANG["common"][16].":</td>"; |
275 |
echo "<td>"; |
276 |
autocompletionTextField("name","glpi_networking_ports","name",$netport->fields["name"],80); |
277 |
echo "</td></tr>"; |
278 |
|
279 |
echo "<tr class='tab_bg_1'><td>".$LANG["common"][65].":</td><td>"; |
280 |
dropdownValue("glpi_dropdown_iface","iface", $netport->fields["iface"]); |
281 |
echo "</td></tr>"; |
282 |
|
283 |
echo "<tr class='tab_bg_1'><td>".$LANG["networking"][14].":</td><td>"; |
284 |
autocompletionTextField("ifaddr","glpi_networking_ports","ifaddr",$netport->fields["ifaddr"],20); |
285 |
echo "</td></tr>\n"; |
286 |
|
287 |
// Show device MAC adresses
|
288 |
if ((!empty($netport->device_type)&&$netport->device_type==COMPUTER_TYPE)||($several!="yes"&&$devtype==COMPUTER_TYPE)){ |
289 |
$comp=new Computer(); |
290 |
|
291 |
if (!empty($netport->device_type)) |
292 |
$comp->getFromDBwithDevices($netport->device_ID); |
293 |
else
|
294 |
$comp->getFromDBwithDevices($ondevice); |
295 |
|
296 |
$macs=array(); |
297 |
$i=0; |
298 |
// Get MAC adresses :
|
299 |
if (count($comp->devices)>0) |
300 |
foreach ($comp->devices as $key => $val) |
301 |
if ($val['devType']==NETWORK_DEVICE&&!empty($val['specificity'])){ |
302 |
$macs[$i]=$val['specificity']; |
303 |
$i++;
|
304 |
} |
305 |
if (count($macs)>0){ |
306 |
echo "<tr class='tab_bg_1'><td>".$LANG["networking"][15].":</td><td>"; |
307 |
echo "<select name='pre_mac'>"; |
308 |
echo "<option value=''>------</option>"; |
309 |
foreach ($macs as $key => $val){ |
310 |
echo "<option value='".$val."' >$val</option>"; |
311 |
} |
312 |
echo "</select>"; |
313 |
|
314 |
echo "</td></tr>\n"; |
315 |
|
316 |
echo "<tr class='tab_bg_2'><td> </td>"; |
317 |
echo "<td>".$LANG["networking"][57]; |
318 |
echo "</td></tr>\n"; |
319 |
|
320 |
} |
321 |
} |
322 |
|
323 |
echo "<tr class='tab_bg_1'><td>".$LANG["networking"][15].":</td><td>"; |
324 |
autocompletionTextField("ifmac","glpi_networking_ports","ifmac",$netport->fields["ifmac"],25); |
325 |
echo "</td></tr>\n"; |
326 |
|
327 |
echo "<tr class='tab_bg_1'><td>".$LANG["networking"][60].":</td><td>"; |
328 |
autocompletionTextField("netmask","glpi_networking_ports","netmask",$netport->fields["netmask"],25); |
329 |
echo "</td></tr>\n"; |
330 |
|
331 |
echo "<tr class='tab_bg_1'><td>".$LANG["networking"][59].":</td><td>"; |
332 |
autocompletionTextField("gateway","glpi_networking_ports","gateway",$netport->fields["gateway"],25); |
333 |
echo "</td></tr>\n"; |
334 |
|
335 |
echo "<tr class='tab_bg_1'><td>".$LANG["networking"][61].":</td><td>"; |
336 |
autocompletionTextField("subnet","glpi_networking_ports","subnet",$netport->fields["subnet"],25); |
337 |
echo "</td></tr>\n"; |
338 |
|
339 |
if ($several!="yes"){ |
340 |
echo "<tr class='tab_bg_1'><td>".$LANG["networking"][51].":</td>"; |
341 |
|
342 |
echo "<td >"; |
343 |
dropdownNetpoint("netpoint", $netport->fields["netpoint"],$netport->location,1,$netport->FK_entities, |
344 |
($ID?$netport->fields["device_type"]: $devtype)); |
345 |
echo "</td></tr>"; |
346 |
} |
347 |
if ($ID) { |
348 |
echo "<tr class='tab_bg_2'>"; |
349 |
echo "<td class='center'>"; |
350 |
echo "<input type='hidden' name='ID' value=".$netport->fields["ID"].">"; |
351 |
echo "<input type='submit' name='delete' value=\"".$LANG["buttons"][6]."\" class='submit' " . |
352 |
"OnClick='return window.confirm(\"".$LANG["common"][50]."\");'>"; |
353 |
echo "</td>"; |
354 |
|
355 |
echo "<td class='center'>"; |
356 |
echo "<input type='submit' name='update' value=\"".$LANG["buttons"][7]."\" class='submit'>"; |
357 |
echo "</td></tr>"; |
358 |
} else
|
359 |
{ |
360 |
|
361 |
echo "<tr class='tab_bg_2'>"; |
362 |
echo "<td align='center' colspan='2'>"; |
363 |
echo "<input type='hidden' name='on_device' value='$ondevice'>"; |
364 |
echo "<input type='hidden' name='device_type' value='$devtype'>"; |
365 |
echo "<input type='submit' name='add' value=\"".$LANG["buttons"][8]."\" class='submit'>"; |
366 |
echo "</td></tr>"; |
367 |
} |
368 |
|
369 |
echo "</table></form></div>"; |
370 |
// SHOW VLAN
|
371 |
if ($ID){ |
372 |
echo "<div class='center'>"; |
373 |
echo "<form method='post' action=\"$target\">"; |
374 |
echo "<input type='hidden' name='referer' value='$REFERER'>"; |
375 |
echo "<input type='hidden' name='ID' value='$ID'>"; |
376 |
|
377 |
echo "<table class='tab_cadre'>"; |
378 |
echo "<tr><th>".$LANG["setup"][90]."</th></tr>"; |
379 |
echo "<tr class='tab_bg_2'><td>"; |
380 |
showPortVLAN($netport->fields["ID"],0,$REFERER); |
381 |
echo "</td></tr>"; |
382 |
|
383 |
echo "<tr class='tab_bg_2'><td>"; |
384 |
echo $LANG["networking"][55].": "; |
385 |
dropdown("glpi_dropdown_vlan","vlan"); |
386 |
echo " <input type='submit' name='assign_vlan' value='".$LANG["buttons"][3]."' class='submit'>"; |
387 |
echo "</td></tr>"; |
388 |
|
389 |
echo "</table>"; |
390 |
|
391 |
echo "</form>"; |
392 |
|
393 |
|
394 |
|
395 |
|
396 |
echo "</div>"; |
397 |
|
398 |
|
399 |
} |
400 |
} |
401 |
|
402 |
|
403 |
function showPortsAdd($ID,$devtype) { |
404 |
|
405 |
global $DB,$CFG_GLPI, $LANG,$LINK_ID_TABLE; |
406 |
|
407 |
if (!haveTypeRight($devtype,"w")) return false; |
408 |
|
409 |
$device_real_table_name = $LINK_ID_TABLE[$devtype]; |
410 |
|
411 |
|
412 |
echo "<div class='center'><table class='tab_cadre_fixe' cellpadding='2'>"; |
413 |
echo "<tr>"; |
414 |
echo "<td align='center' class='tab_bg_2' >"; |
415 |
echo "<a href=\"".$CFG_GLPI["root_doc"]."/front/networking.port.php?on_device=$ID&device_type=$devtype\"><strong>"; |
416 |
echo $LANG["networking"][19]; |
417 |
echo "</strong></a></td>"; |
418 |
echo "<td align='center' class='tab_bg_2' width='50%'>"; |
419 |
echo "<a href=\"".$CFG_GLPI["root_doc"]."/front/networking.port.php?on_device=$ID&device_type=$devtype&several=yes\"><strong>"; |
420 |
echo $LANG["networking"][46]; |
421 |
echo "</strong></a></td>"; |
422 |
|
423 |
echo "</tr>"; |
424 |
echo "</table></div><br>"; |
425 |
} |
426 |
|
427 |
function showConnection($ID,$withtemplate='',$type=COMPUTER_TYPE) { |
428 |
|
429 |
global $CFG_GLPI, $LANG,$INFOFORM_PAGES; |
430 |
|
431 |
if (!haveTypeRight($type,"r")) return false; |
432 |
$canedit=haveRight("networking","w"); |
433 |
|
434 |
$contact = new Netport; |
435 |
$netport = new Netport; |
436 |
|
437 |
if ($contact->getContact($ID)) { |
438 |
$netport->getFromDB($contact->contact_id); |
439 |
$netport->getDeviceData($netport->fields["on_device"],$netport->fields["device_type"]); |
440 |
echo "\n\n<table border='0' cellspacing='0' width='100%'><tr ".($netport->deleted?"class='tab_bg_2_2'":"").">"; |
441 |
echo "<td><strong>"; |
442 |
echo "<a href=\"".$CFG_GLPI["root_doc"]."/front/networking.port.php?ID=".$netport->fields["ID"]."\">"; |
443 |
if (rtrim($netport->fields["name"])!="") |
444 |
echo $netport->fields["name"]; |
445 |
else echo $LANG["common"][0]; |
446 |
echo "</a></strong>"; |
447 |
echo " ".$LANG["networking"][25]." <strong>"; |
448 |
|
449 |
echo "<a href=\"".$CFG_GLPI["root_doc"]."/".$INFOFORM_PAGES[$netport->fields["device_type"]]."?ID=".$netport->device_ID."\">"; |
450 |
|
451 |
echo $netport->device_name; |
452 |
if ($CFG_GLPI["view_ID"]) echo " (".$netport->device_ID.")"; |
453 |
echo "</a>"; |
454 |
echo "</strong></td>"; |
455 |
if ($canedit){ |
456 |
echo "<td class='right'><strong>"; |
457 |
if ($withtemplate!=2) |
458 |
echo "<a href=\"".$CFG_GLPI["root_doc"]."/front/networking.port.php?disconnect=disconnect&ID=$ID\">".$LANG["buttons"][10]."</a>"; |
459 |
else " "; |
460 |
echo "</strong></td>"; |
461 |
} |
462 |
echo "</tr></table>"; |
463 |
|
464 |
} else {
|
465 |
echo "<table border='0' cellspacing='0' width='100%'><tr>"; |
466 |
if ($canedit){ |
467 |
echo "<td class='left'>"; |
468 |
if ($withtemplate!=2&&$withtemplate!=1){ |
469 |
$netport->getFromDB($ID); |
470 |
|
471 |
if ($netport->getDeviceData($netport->fields["on_device"],$netport->fields["device_type"])){ |
472 |
dropdownConnectPort($ID,$type,"dport",$netport->FK_entities); |
473 |
} |
474 |
} |
475 |
else echo " "; |
476 |
echo "</td>"; |
477 |
} |
478 |
echo "<td><div id='not_connected_display$ID'>".$LANG["connect"][1]."</div></td>"; |
479 |
|
480 |
echo "</tr></table>"; |
481 |
} |
482 |
} |
483 |
|
484 |
|
485 |
/**
|
486 |
* Wire the Ports
|
487 |
*
|
488 |
*@param $sport : source port ID
|
489 |
*@param $dport : destination port ID
|
490 |
*@param $dohistory : add event in the history
|
491 |
*@param $addmsg : display HTML message on success
|
492 |
*
|
493 |
*@return true on success
|
494 |
**/
|
495 |
function makeConnector($sport, $dport, $dohistory=true, $addmsg=false) { |
496 |
|
497 |
global $DB,$CFG_GLPI, $LANG; |
498 |
|
499 |
// Get netpoint for $sport and $dport
|
500 |
$ps=new Netport; |
501 |
if (!$ps->getFromDB($sport)){ |
502 |
return false; |
503 |
} |
504 |
$pd=new Netport; |
505 |
if (!$pd->getFromDB($dport)){ |
506 |
return false; |
507 |
} |
508 |
|
509 |
$items_to_check=array('ifmac'=>$LANG["networking"][15],'ifaddr'=>$LANG["networking"][14],'netpoint'=>$LANG["networking"][51],'subnet'=>$LANG["networking"][61],'netmask'=>$LANG["networking"][60],'gateway'=>$LANG["networking"][59]); |
510 |
|
511 |
$update_items=array(); |
512 |
$conflict_items=array(); |
513 |
|
514 |
foreach ($items_to_check as $item=>$name){ |
515 |
$source=""; |
516 |
$destination=""; |
517 |
switch ($item){ |
518 |
case 'netpoint': |
519 |
if (isset($ps->fields["netpoint"])&&$ps->fields["netpoint"]!=0){ |
520 |
$source=$ps->fields["netpoint"]; |
521 |
} |
522 |
if (isset($pd->fields["netpoint"])&&$pd->fields["netpoint"]!=0){ |
523 |
$destination=$pd->fields["netpoint"]; |
524 |
} |
525 |
break;
|
526 |
default:
|
527 |
if (isset($ps->fields[$item])){ |
528 |
$source=$ps->fields[$item]; |
529 |
} |
530 |
if (isset($pd->fields[$item])){ |
531 |
$destination=$pd->fields[$item]; |
532 |
} |
533 |
break;
|
534 |
} |
535 |
// Update Item
|
536 |
$updates[0]=$item; |
537 |
if (empty($source)&&!empty($destination)){ |
538 |
$ps->fields[$item]=$destination; |
539 |
$ps->updateInDB($updates); |
540 |
$update_items[]=$item; |
541 |
} |
542 |
else if (!empty($source)&&empty($destination)){ |
543 |
$pd->fields[$item]=$source; |
544 |
$pd->updateInDB($updates); |
545 |
$update_items[]=$item; |
546 |
} |
547 |
else if ($source!=$destination){ |
548 |
$conflict_items[]=$item; |
549 |
} |
550 |
} |
551 |
if (count($update_items)){ |
552 |
$message=$LANG["connect"][15].": "; |
553 |
$first=true; |
554 |
foreach ($update_items as $item){ |
555 |
if ($first) $first=false; |
556 |
else $message.=" - "; |
557 |
$message.=$items_to_check[$item]; |
558 |
} |
559 |
addMessageAfterRedirect($message);
|
560 |
} |
561 |
if (count($conflict_items)){ |
562 |
$message=$LANG["connect"][16].": "; |
563 |
$first=true; |
564 |
foreach ($conflict_items as $item){ |
565 |
if ($first) $first=false; |
566 |
else $message.=" - "; |
567 |
$message.=$items_to_check[$item]; |
568 |
} |
569 |
addMessageAfterRedirect($message);
|
570 |
} |
571 |
|
572 |
// Manage VLAN : use networkings one as defaults
|
573 |
$npnet=-1; |
574 |
$npdev=-1; |
575 |
if ($ps->fields["device_type"]!=NETWORKING_TYPE && $pd->fields["device_type"]==NETWORKING_TYPE){ |
576 |
$npnet=$dport; |
577 |
$npdev=$sport; |
578 |
} |
579 |
if ($pd->fields["device_type"]!=NETWORKING_TYPE && $ps->fields["device_type"]==NETWORKING_TYPE){ |
580 |
$npnet=$sport; |
581 |
$npdev=$dport; |
582 |
} |
583 |
if ($npnet>0&&$npdev>0){ |
584 |
// Get networking VLAN
|
585 |
// Unset MAC and IP from networking device
|
586 |
$query = "SELECT * FROM glpi_networking_vlan WHERE FK_port='$npnet'"; |
587 |
if ($result=$DB->query($query)){ |
588 |
if ($DB->numrows($result)>0){ |
589 |
// Found VLAN : clean vlan device and add found ones
|
590 |
$query="DELETE FROM glpi_networking_vlan WHERE FK_port='$npdev' "; |
591 |
$DB->query($query); |
592 |
while ($data=$DB->fetch_array($result)){ |
593 |
$query="INSERT INTO glpi_networking_vlan (FK_port,FK_vlan) VALUES ('$npdev','".$data['FK_vlan']."')"; |
594 |
$DB->query($query); |
595 |
} |
596 |
} |
597 |
} |
598 |
} |
599 |
// end manage VLAN
|
600 |
|
601 |
$query = "INSERT INTO glpi_networking_wire VALUES (NULL,'$sport','$dport')"; |
602 |
if ($result = $DB->query($query)) { |
603 |
$source=new CommonItem; |
604 |
$source->getFromDB($ps->fields['device_type'],$ps->fields['on_device']); |
605 |
$dest=new CommonItem; |
606 |
$dest->getFromDB($pd->fields['device_type'],$pd->fields['on_device']); |
607 |
|
608 |
if ($dohistory) { |
609 |
$changes[0]=0; |
610 |
$changes[1]=""; |
611 |
$changes[2]=$dest->getName(); |
612 |
if ($ps->fields["device_type"]==NETWORKING_TYPE) { |
613 |
$changes[2] = "#" . $ps->fields["name"] . " > " . $changes[2]; |
614 |
} |
615 |
if ($pd->fields["device_type"]==NETWORKING_TYPE) { |
616 |
$changes[2] = $changes[2] . " > #" . $pd->fields["name"]; |
617 |
} |
618 |
historyLog ($ps->fields["on_device"],$ps->fields["device_type"],$changes,$pd->fields["device_type"],HISTORY_CONNECT_DEVICE); |
619 |
|
620 |
$changes[2]=$source->getName(); |
621 |
if ($pd->fields["device_type"]==NETWORKING_TYPE) { |
622 |
$changes[2] = "#" . $pd->fields["name"] . " > " . $changes[2]; |
623 |
} |
624 |
if ($ps->fields["device_type"]==NETWORKING_TYPE) { |
625 |
$changes[2] = $changes[2] . " > #" . $ps->fields["name"]; |
626 |
} |
627 |
historyLog ($pd->fields["on_device"],$pd->fields["device_type"],$changes,$ps->fields["device_type"],HISTORY_CONNECT_DEVICE); |
628 |
} |
629 |
|
630 |
if ($addmsg) { |
631 |
echo "<br><div class='center'><strong>".$LANG["networking"][44]." ".$source->getName()." - ".$ps->fields['logical_number']." (".$ps->fields['ifaddr']." - ".$ps->fields['ifmac'].") ".$LANG["networking"][45]." ".$dest->getName()." - ".$pd->fields['logical_number']." (".$pd->fields['ifaddr']." - ".$pd->fields['ifmac'].") </strong></div>"; |
632 |
} |
633 |
return true; |
634 |
} else {
|
635 |
return false; |
636 |
} |
637 |
|
638 |
} |
639 |
|
640 |
/**
|
641 |
* Unwire the Ports
|
642 |
*
|
643 |
*@param $ID : ID a network port
|
644 |
*@param $dohistory : add event in the history
|
645 |
*
|
646 |
*@return true on success
|
647 |
**/
|
648 |
function removeConnector($ID, $dohistory=true) { |
649 |
|
650 |
global $DB,$CFG_GLPI; |
651 |
|
652 |
// Update to blank networking item
|
653 |
$nw=new Netwire; |
654 |
if ($ID2=$nw->getOppositeContact($ID)){ |
655 |
$query = "DELETE FROM glpi_networking_wire WHERE (end1 = '$ID' OR end2 = '$ID')"; |
656 |
if ($result=$DB->query($query)) { |
657 |
|
658 |
// clean datas of linked ports if network one
|
659 |
$np1=new Netport; |
660 |
$np2=new Netport; |
661 |
if ($np1->getFromDB($ID) && $np2->getFromDB($ID2)){ |
662 |
$npnet=-1; |
663 |
$npdev=-1; |
664 |
if ($np1->fields["device_type"]!=NETWORKING_TYPE&&$np2->fields["device_type"]==NETWORKING_TYPE){ |
665 |
$npnet=$ID2; |
666 |
$npdev=$ID; |
667 |
} |
668 |
if ($np2->fields["device_type"]!=NETWORKING_TYPE&&$np1->fields["device_type"]==NETWORKING_TYPE){ |
669 |
$npnet=$ID; |
670 |
$npdev=$ID2; |
671 |
} |
672 |
if ($npnet!=-1&&$npdev!=-1){ |
673 |
// Unset MAC and IP from networking device
|
674 |
if ($np1->fields["ifmac"] == $np2->fields["ifmac"]){ |
675 |
$query = "UPDATE glpi_networking_ports SET ifmac='' WHERE ID='$npnet'"; |
676 |
$DB->query($query); |
677 |
} |
678 |
if ($np1->fields["ifaddr"] == $np2->fields["ifaddr"]){ |
679 |
$query = "UPDATE glpi_networking_ports SET ifaddr='',netmask='', subnet='',gateway='' WHERE ID='$npnet'"; |
680 |
$DB->query($query); |
681 |
} |
682 |
|
683 |
// Unset netpoint from common device
|
684 |
$query = "UPDATE glpi_networking_ports SET netpoint=0 WHERE ID='$npdev'"; |
685 |
$DB->query($query); |
686 |
} |
687 |
|
688 |
if ($dohistory) { |
689 |
$device=new CommonItem(); |
690 |
|
691 |
$device->getFromDB($np2->fields["device_type"],$np2->fields["on_device"]); |
692 |
$changes[0]=0; |
693 |
$changes[1]=$device->getName(); |
694 |
$changes[2]=""; |
695 |
if ($np1->fields["device_type"]==NETWORKING_TYPE) { |
696 |
$changes[1] = "#" . $np1->fields["name"] . " > " . $changes[1]; |
697 |
} |
698 |
if ($np2->fields["device_type"]==NETWORKING_TYPE) { |
699 |
$changes[1] = $changes[1] . " > #" . $np2->fields["name"]; |
700 |
} |
701 |
historyLog ($np1->fields["on_device"],$np1->fields["device_type"],$changes,$np2->fields["device_type"],HISTORY_DISCONNECT_DEVICE); |
702 |
|
703 |
$device->getFromDB($np1->fields["device_type"],$np1->fields["on_device"]); |
704 |
$changes[1]=$device->getName(); |
705 |
if ($np2->fields["device_type"]==NETWORKING_TYPE) { |
706 |
$changes[1] = "#" . $np2->fields["name"] . " > " . $changes[1]; |
707 |
} |
708 |
if ($np1->fields["device_type"]==NETWORKING_TYPE) { |
709 |
$changes[1] = $changes[1] . " > #" . $np1->fields["name"]; |
710 |
} |
711 |
historyLog ($np2->fields["on_device"],$np2->fields["device_type"],$changes,$np1->fields["device_type"],HISTORY_DISCONNECT_DEVICE); |
712 |
} |
713 |
} |
714 |
|
715 |
return true; |
716 |
} else {
|
717 |
return false; |
718 |
} |
719 |
} else return false; |
720 |
} |
721 |
|
722 |
|
723 |
?>
|