Use server version for version string
[freeradius.git] / dialup_admin / htdocs / clear_opensessions.php3
1 <?php
2 require('../conf/config.php3');
3 require_once('../lib/xlat.php3');
4 if (is_file("../lib/sql/drivers/$config[sql_type]/functions.php3"))
5         include_once("../lib/sql/drivers/$config[sql_type]/functions.php3");
6 else{
7         echo <<<EOM
8 <title>Clear Open User Sessions for $login</title>
9 <meta http-equiv="Content-Type" content="text/html; charset=$config[general_charset]">
10 <link rel="stylesheet" href="style.css">
11 </head>
12 <body>
13 <center>
14 <b>Could not include SQL library functions. Aborting</b>
15 </body>
16 </html>
17 EOM;
18         exit();
19 }
20
21 echo <<<EOM
22 <html>
23 <head>
24 <title>Clear Open User Sessions for $login</title>
25 <meta http-equiv="Content-Type" content="text/html; charset=$config[general_charset]">
26 <link rel="stylesheet" href="style.css">
27 </head>
28 <body>
29 <center>
30 <table border=0 width=550 cellpadding=0 cellspacing=0>
31 <tr valign=top>
32 <td align=center><img src="images/title2.gif"></td>
33 </tr>
34 </table>
35
36 <table border=0 width=400 cellpadding=0 cellspacing=2>
37 EOM;
38
39 include("../html/user_toolbar.html.php3");
40
41 $open_sessions = 0;
42
43 $sql_extra_query = '';
44 if ($config[sql_accounting_extra_query] != ''){
45         $sql_extra_query = xlat($config[sql_accounting_extra_query],$login,$config);
46         $sql_extra_query = da_sql_escape_string($sql_extra_query);
47 }
48
49 print <<<EOM
50 </table>
51
52 <br>
53 <table border=0 width=540 cellpadding=1 cellspacing=1>
54 <tr valign=top>
55 <td width=340></td>
56 <td bgcolor="black" width=200>
57         <table border=0 width=100% cellpadding=2 cellspacing=0>
58         <tr bgcolor="#907030" align=right valign=top><th>
59         <font color="white">Clear open sessions for $login</font>&nbsp;
60         </th></tr>
61         </table>
62 </td></tr>
63 <tr bgcolor="black" valign=top><td colspan=2>
64         <table border=0 width=100% cellpadding=12 cellspacing=0 bgcolor="#ffffd0" valign=top>
65         <tr><td>
66 EOM;
67
68 if ($drop_conns == 1){
69         $method = 'snmp';
70         $nastype = 'cisco';
71         if ($config[general_sessionclear_method] != '')
72                 $method = $config[general_sessionclear_method];
73         if ($config[general_nas_type] != '')
74                 $nastype = $config[general_nas_type];
75         if ($config[general_ld_library_path] != '')
76                 putenv("LD_LIBRARY_PATH=$config[general_ld_library_path]");
77         $nas_by_ip = array();
78         $meth_by_ip = array();
79         $nastype_by_ip = array();
80         foreach ($nas_list as $nas){
81                 if ($nas[ip] != ''){
82                         $ip = $nas[ip];
83                         $nas_by_ip[$ip] = $nas[community];
84                         $meth_by_ip[$ip] = $nas[sessionclear_method];
85                         $nastype_by_ip[$ip] = $nas[nas_type];
86                 }
87         }
88
89         $link = @da_sql_pconnect($config);
90         if ($link){
91                 $search = @da_sql_query($link,$config,
92                 "SELECT nasipaddress,acctsessionid FROM $config[sql_accounting_table]
93                 WHERE username = '$login' AND acctstoptime IS NULL;");
94                 if ($search){
95                         while($row = @da_sql_fetch_array($search,$config)){
96                                 $sessionid = $row[acctsessionid];
97                                 $sessionid = hexdec($sessionid);
98                                 $nas = $row[nasipaddress];
99                                 $port = $row[nasportid];
100                                 $meth = $meth_by_ip[$nas];
101                                 $nastype = ($nastype_by_ip[$nas] != '') ? $nastype_by_ip[$nas] : $nastype;
102                                 $comm = $nas_by_ip[$nas];
103                                 if ($meth == '')
104                                         $meth = $method;
105                                 if ($meth == 'snmp' && $comm != '')
106                                         exec("$config[general_sessionclear_bin] $nas snmp $nastype $login $sessionid $comm");
107                                 if ($meth == 'telnet')
108                                         exec("$config[general_sessionclear_bin] $nas telnet $nastype $login $sessionid $port");
109                         }
110                 }
111                 else
112                         echo "<b>Database query failed: " . da_sql_error($link,$config) . "</b><br>\n";
113         }
114         else
115                 echo "<b>Could not connect to SQL database</b><br>\n";
116 }
117 if ($clear_sessions == 1){
118         $sql_servers = array();
119         if ($config[sql_extra_servers] != '')
120                 $sql_servers = explode(' ',$config[sql_extra_servers]);
121         $quer = '= 0';
122         if ($config[sql_type] == 'pg')
123                 $quer = 'IS NULL';
124         $sql_servers[] = $config[sql_server];
125         foreach ($sql_servers as $server){
126                 $link = @da_sql_host_connect($server,$config);
127                 if ($link){
128                         $res = @da_sql_query($link,$config,
129                         "DELETE FROM $config[sql_accounting_table]
130                         WHERE username='$login' AND acctstoptime $quer $sql_extra_query;");
131                         if ($res)
132                                 echo "<b>Deleted open sessions from accounting table on server $server</b><br>\n";
133                         else
134                                 echo "<b>Error deleting open sessions for user" . da_sql_error($link,$config) . "</b><br>\n";
135                 }
136                 else
137                         echo "<b>Could not connect to SQL database</b><br>\n";
138         }
139         echo <<<EOM
140 </td></tr>
141 </table>
142 </tr>
143 </table>
144 </body>
145 </html>
146 EOM;
147         exit();
148 }
149 else{
150         $link = @da_sql_pconnect($config);
151         if ($link){
152                 $search = @da_sql_query($link,$config,
153                 "SELECT COUNT(*) AS counter FROM $config[sql_accounting_table]
154                 WHERE username = '$login' AND acctstoptime IS NULL $sql_extra_query;");
155                 if ($search){
156                         if ($row = @da_sql_fetch_array($search,$config))
157                                 $open_sessions = $row[counter];
158                 }
159                 else
160                         echo "<b>Database query failed: " . da_sql_error($link,$config) . "</b><br>\n";
161         }
162         else
163                 echo "<b>Could not connect to SQL database</b><br>\n";
164 }
165 ?>
166    <form method=post>
167       <input type=hidden name=login value=<?php print $login ?>>
168       <input type=hidden name=clear_sessions value="0">
169         <table border=1 bordercolordark=#ffffe0 bordercolorlight=#000000 width=100% cellpadding=2 cellspacing=0 bgcolor="#ffffe0" valign=top>
170 <tr>
171 <td align=center>
172 User <?php echo $login; ?> has <i><?php echo $open_sessions; ?></i> open sessions<br><br>
173 Are you sure you want to clear all open user sessions?
174 </td>
175 </tr>
176         </table>
177 <br>
178 <input type=submit class=button value="Yes Clear" OnClick="this.form.clear_sessions.value=1">
179 <br><br>
180 <input type=submit class=button value="Yes Drop Connections" OnClick="this.form.drop_conns.value=1">
181 </form>
182 </td></tr>
183 </table>
184 </tr>
185 </table>
186 </body>
187 </html>