~budgester/irm/trunk

« back to all changes in this revision

Viewing changes to include/ports.functions.php

  • Committer: Martin N Stevens
  • Date: 2008-03-18 19:34:25 UTC
  • Revision ID: budgester@budgester.com-20080318193425-snjufybd20f0pxge
Refactored all individual functions in ports.functions.php into a class and renamed ports.functions.php to ports.class.php

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
################################################################################
3
 
#    IRM - The Information Resource Manager
4
 
#    Copyright (C) 2003 Yann Ramin
5
 
#
6
 
#    This program is free software; you can redistribute it and/or modify
7
 
#    it under the terms of the GNU General Public License as published by
8
 
#    the Free Software Foundation; either version 2 of the License, or
9
 
#    (at your option) any later version.
10
 
#
11
 
#    This program is distributed in the hope that it will be useful,
12
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
#    GNU General Public License (in file COPYING) for more details.
15
 
#
16
 
#    You should have received a copy of the GNU General Public License
17
 
#    along with this program; if not, write to the Free Software
18
 
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
 
#
20
 
################################################################################
21
 
class ports extends IRMMain
22
 
{
23
 
        function ports()
24
 
        {
25
 
                commonHeader(_("Networking Connector Wizard"));
26
 
                if($_REQUEST['connect']){
27
 
                        $this->updateWires();
28
 
                } else {
29
 
                        $this->pID=$_REQUEST['pID'];
30
 
                        $this->device=$_REQUEST['device'];
31
 
                        $this->device_name=$_REQUEST['device_name'];
32
 
                        $this->selectDeviceType();
33
 
                        if($this->device != NULL){
34
 
                                $this->selectDevice();
35
 
                        }
36
 
                        if($this->device_name != NULL){
37
 
                                $this->portList();
38
 
                        }
39
 
                }
40
 
                commonFooter();
41
 
        }
42
 
        
43
 
        function portList()
44
 
        {
45
 
                $query = "SELECT * FROM networking_ports WHERE (device_on = $this->device_name) AND (device_type =\"$this->device\") ORDER BY logical_number";
46
 
                $DB = Config::Database();
47
 
                $data = $DB->getAll($query);
48
 
                PRINT "<table class=networking>";
49
 
                PRINT "<tr>";
50
 
                PRINT "<th>" .("ID") . "</th>";
51
 
                PRINT "<th>" .("Port Name") . "</th>";
52
 
                PRINT "<th>" .("Type") . "</th>";
53
 
                PRINT "<th>" .("IP Address") . "</th>";
54
 
                PRINT "<th>" .("MAC Address") . "</th>";
55
 
                PRINT "<th>" .("Port Name") . "</th>";
56
 
                PRINT "</tr>";
57
 
 
58
 
                foreach($data as $port){
59
 
                        PRINT "<form type=post method=$_SELF>";
60
 
                        PRINT "<tr>";
61
 
 
62
 
                        PRINT "<td class=networkingdetail>";
63
 
                        PRINT "<input type=hidden name=pID value=" . $port['ID']. ">";
64
 
                        PRINT "<input type=hidden name=pID1 value=$this->pID>";
65
 
                        PRINT "<input type=hidden name=connect value=true>";
66
 
                        PRINT "<input type=submit value=" . _("Connect") . ">";
67
 
                        PRINT "</td>";
68
 
 
69
 
                        PRINT "<td class=networkingdetail>" . $port['logical_number'] . "</td>";
70
 
                        PRINT "<td class=networkingdetail>" . $port['name'] . "</td>";
71
 
                        PRINT "<td class=networkingdetail>" . $port['iface'] . "</td>";
72
 
                        PRINT "<td class=networkingdetail>" . $port['ifaddr'] . "</td>";
73
 
                        PRINT "<td class=networkingdetail>" . $port['ifmac'] . "</td>";
74
 
 
75
 
                        PRINT "</form>\n";
76
 
                }
77
 
                PRINT "</table>";
78
 
        }
79
 
        
80
 
        function updateWires()
81
 
        {
82
 
                $vals = array(
83
 
                        'end1' => $_REQUEST[pID],
84
 
                        'end2' => $_REQUEST[pID1]
85
 
                        );
86
 
                $DB = Config::Database();
87
 
                $DB->InsertQuery('networking_wire', $vals);
88
 
                PRINT _("You have now connected two devices");
89
 
        }
90
 
 
91
 
}
92
 
 
93
 
 
94
 
function portsTableHead()
95
 
{
96
 
        PRINT '<table class="networking">';
97
 
        PRINT '<tr class="networkingheader">';
98
 
        PRINT "<th>"._("Port #")."</th>";
99
 
        PRINT "<th>"._("Name")."</th>";
100
 
        PRINT "<th>"._("Interface")."</th>";
101
 
        PRINT "<th>"._("IP Address")."</th>";
102
 
        PRINT "<th>"._("MAC/Network Address")."</th>";
103
 
 
104
 
        if(Config::Get('mrtg')== "1"){
105
 
                PRINT "<th>"._("MRTG Graph")."</th>";
106
 
        }
107
 
        PRINT "<th>"._("Connected to...")."</th>";
108
 
        PRINT "</tr>\n";
109
 
}
110
 
 
111
 
 
112
 
function getPortsOnDevice($device, $device_type)
113
 
{
114
 
        $DB = Config::Database();
115
 
        $qdevice = $DB->getTextValue($device);
116
 
        $qdevice_type = $DB->getTextValue($device_type);
117
 
        $query = "SELECT * FROM networking_ports WHERE (device_on = $qdevice AND device_type = $qdevice_type) ORDER BY logical_number";
118
 
 
119
 
        $data = $DB->getAll($query);
120
 
        return $data;
121
 
}
122
 
 
123
 
function getWires($ID)
124
 
{
125
 
        $DB = Config::Database();
126
 
        $qID = $DB->getTextValue($ID);
127
 
        $wquery = "SELECT * FROM networking_wire WHERE (end1 = $qID OR end2 = $qID)";
128
 
        $wire = $DB->getRow($wquery);
129
 
        return $wire;
130
 
}
131
 
 
132
 
function showPortRow($data, $ip)
133
 
{
134
 
        $DB = Config::Database();
135
 
        $userbase = Config::AbsLoc('users');
136
 
 
137
 
        foreach ($data as $result)
138
 
        {
139
 
                $found = 0;
140
 
                $pType=0;
141
 
 
142
 
                $ID = $result["ID"];
143
 
                $name = $result["name"];
144
 
                $logical_number = $result["logical_number"];
145
 
                $iface = $result["iface"];
146
 
                $ifaddr = $result["ifaddr"];
147
 
                $ifmac = $result["ifmac"];
148
 
 
149
 
                $wire = getWires($ID);
150
 
 
151
 
                if (count($wire) > 0) 
152
 
                {
153
 
                        $wID = $wire["ID"];
154
 
                        $wend1 = $wire["end1"];
155
 
                        $wend2 = $wire["end2"];
156
 
                        if ($wend1 == $ID) 
157
 
                        {
158
 
                                $qend = $DB->getTextValue($wend2);
159
 
                        } else {
160
 
                                $qend = $DB->getTextValue($wend1);
161
 
                        }
162
 
                        
163
 
                        $pquery = "SELECT * FROM networking_ports WHERE (ID = $qend)";
164
 
                        $farend = $DB->getRow($pquery);
165
 
                                
166
 
                        $pID = $farend["ID"];
167
 
                        $pNum = $farend["logical_number"];
168
 
                        $pOn = $farend["device_on"];
169
 
                        $pName = $farend["name"];
170
 
                        $pType = $farend["device_type"];
171
 
                        $qOn = $DB->getTextValue($pOn);
172
 
 
173
 
                        switch ($pType)
174
 
                        {
175
 
                        case "1": 
176
 
                                $portOnDeviceType = "computers"; 
177
 
                                break;
178
 
                        case "2":
179
 
                                $portOnDeviceType = "networking"; 
180
 
                                break;
181
 
                        default:
182
 
                                $portOnDeviceType = $pType;
183
 
                        }
184
 
        
185
 
                        $nquery = "SELECT ID,name FROM $portOnDeviceType WHERE (ID = $qOn) order by name";
186
 
                        
187
 
                        $nresult = $DB->getRow($nquery);
188
 
                        $nname = $nresult["name"];
189
 
                        $nID = $nresult["ID"];
190
 
                        $found = 1;
191
 
                }
192
 
                else
193
 
                {
194
 
                        $found = 0;
195
 
                        $pType = 0;
196
 
                }
197
 
        
198
 
                PRINT '<tr class="networkingdetail">';
199
 
                PRINT "<td><a href=\"$userbase/networking-port.php?ID=$ID\">$logical_number</a></td>\n";
200
 
                PRINT "<td>$name</td>\n";
201
 
                PRINT "<td>$iface</td>\n";
202
 
                PRINT "<td>$ifaddr</td>\n";
203
 
                PRINT "<td>$ifmac</td>\n";
204
 
 
205
 
                //TODO MRTG Graph of port
206
 
                
207
 
                if(Config::Get('mrtg')== "1"){
208
 
                        $url = Config::Get('mrtglocation');
209
 
                        
210
 
                        if($ifaddr && $ifaddr != "127.0.0.1"){
211
 
                                PRINT "<td><img src=" . $url . $ifaddr . "_" . $logical_number . "-day.png></td>\n";
212
 
                        }elseif ($ip != null && $ifaddr != "127.0.0.1"){
213
 
                                PRINT "<td><img src=http://192.168.1.11/~budgester/mrtg/" . $ip . "_" . $logical_number . "-day.png></td>\n";
214
 
                        } else {
215
 
                                PRINT "<td></td>\n";
216
 
                        }
217
 
                }
218
 
                PRINT "<td>\n";
219
 
 
220
 
                if ($found != 1) 
221
 
                {
222
 
                        PRINT _("Nothing Connected.")." <a href=\"$userbase/ports.php?pID=$ID\">"._("Connect")."</a>\n";
223
 
                } else {
224
 
                        switch ($pType)
225
 
                        {
226
 
                        case "1": 
227
 
                                PRINTF(_("Connected to port %s on computer %s"),
228
 
                                        "<a href=\"$userbase/networking-port.php?ID=$pID\">$pNum ($pName)</a>\n",
229
 
                                        "<a href=\"$userbase/computers-index.php?action=info&ID=$nID\">$nname ($nID)</a>\n"
230
 
                                        );
231
 
                                PRINT " | <a href=\"$userbase/networking-port-discon.php?ID=$ID\">"._("Disconnect")."</a>.\n";
232
 
                                break;
233
 
                        case "2":       
234
 
                                PRINTF(_("Connected to port %s on network device %s"),
235
 
                                        "<a href=\"$userbase/networking-port.php?ID=$pID\">$pNum ($pName)</a>\n",
236
 
                                        "<a href=\"$userbase/networking-index.php?devicetype=networking&action=info&ID=$nID\">$nname ($nID)</a>\n"
237
 
                                        );
238
 
                                PRINT " | <a href=\"$userbase/networking-port-discon.php?ID=$ID\">"._("Disconnect")."</a>.\n";
239
 
                                break;
240
 
                        default:
241
 
                                PRINTF(_("Connected to port %s on %s device %s"),
242
 
                                        "<a href=\"$userbase/networking-port.php?ID=$pID\">$pNum $pName</a>\n",
243
 
                                        $pType,
244
 
                                        "<a href=\"$userbase/device-info.php?ID=$nID&devicetype=$pType\">$nname ($nID)</a>\n"
245
 
                                        );
246
 
                                PRINT " | <a href=\"$userbase/networking-port-discon.php?ID=$ID\">"._("Disconnect")."</a>.\n";
247
 
                        }
248
 
                }
249
 
                PRINT "</td>\n";
250
 
                PRINT "</tr>\n";
251
 
        }
252
 
}
253
 
 
254
 
function showPortsOnDevice($device, $device_type)
255
 
{
256
 
        # 1 is computer, 2 networking device
257
 
 
258
 
        $userbase = Config::AbsLoc('users');
259
 
        $data = getPortsOnDevice($device, $device_type);
260
 
 
261
 
        portsTableHead();
262
 
 
263
 
        if(Config::Get('snmp')== "1"){
264
 
                $deviceConnection = new DeviceConnection;
265
 
                $ip = $deviceConnection->ID2IP($device, $device_type);
266
 
        }
267
 
 
268
 
        if (count($data) < 1){
269
 
                PRINT "<TR><td colspan=6>"._("Looks like a lonely device to me.  No ports found.")."</TD></TR>\n";
270
 
        } else {
271
 
                showPortRow($data, $ip);
272
 
        }
273
 
 
274
 
        PRINT '<tr class="networkingupdate">';
275
 
        PRINT "<td colspan=6 align=right><A HREF=\"$userbase/networking-port-add-form.php?device=$device&device_type=$device_type\">"._("Add Port")."</a></td>\n";
276
 
        PRINT "</tr>\n";
277
 
        PRINT "</table>\n";
278
 
}
279
 
?>