This repository has been archived by the owner on May 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathcron.php
160 lines (138 loc) · 5.46 KB
/
cron.php
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
<?php
//Check if script is already running
$tmpfilename = "/tmp/freeradius_cron.pid";
if (!($tmpfile = @fopen($tmpfilename,"w")))
{
return 0;
}
if (!@flock( $tmpfile, LOCK_EX | LOCK_NB, &$wouldblock) || $wouldblock)
{
@fclose($tmpfile);
return 0;
}
//end
//Include some files
require(dirname(__FILE__).'/config.php');
//end
//Set some varibles
if($general_timezone != '') { date_default_timezone_set($general_timezone); }
$radius_users = array();
//end
//Curl is important
if (!function_exists('curl_init')){
echo "cURL is not installed\n";
die('cURL is not installed');
}
//
//Make sure the WHMCS API details are present & tested
if( ( $whmcs_api_url == "" ) || ( $whmcs_api_username == "" ) || ( $whmcs_api_password == "" ) ) {
die( "WHMCS API details missing\n" );
}
//end
//Connect to the radius database
$mysql = mysql_connect($freeradius_db_host, $freeradius_db_username, $freeradius_db_password);
$conn = mysql_select_db($freeradius_db_database);
if (!$conn) {
die( "Could not connect to WHMCS database: " . mysql_error() . "\n" );
}
//end
//Get a list of users in the radcheck table
$radcheck_q = mysql_query("SELECT DISTINCT username FROM radcheck");
while($radcheck = mysql_fetch_array($radcheck_q)) {
array_push($radius_users,$radcheck['username']);
}
//end
//Start the overusage checking
foreach($radius_users as $user) {
$postfields = array();
$postfields["username"] = $whmcs_api_username;
$postfields["password"] = md5($whmcs_api_password);
$postfields["api_key"] = $whmcs_api_key;
$postfields["action"] = "freeradiusapi";
$postfields["service_username"] = $user;
$postfields["responsetype"] = "json";
$query_string = "";
foreach ($postfields AS $k=>$v) $query_string .= "$k=".urlencode($v)."&";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $whmcs_api_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$jsondata = curl_exec($ch);
if( curl_error($ch) ) {
echo "WHMCS API Connection Error: " . curl_errno($ch) . " - " . curl_error($ch) . "\n";
curl_close($ch);
continue;
}
curl_close($ch);
$api_data = json_decode($jsondata, true);
if( $api_data["result"] == 'error' ) {
echo $user . ": " . $api_data["message"] . "\n";
continue;
}
$startdate = $api_data["startdate"];
$enddate = $api_data["enddate"];
$usagelimit = $api_data["usagelimit"];
if( $usagelimit == 0 ) {
echo $user . ": No usage limit set\n";
continue;
}
$query = "SELECT SUM(radacct.AcctOutputOctets) + SUM(radacct.AcctInputOctets) AS total FROM radacct WHERE radacct.Username='$user' AND radacct.AcctStartTime>='".$startdate."'";
if ($enddate) $query .= " AND radacct.AcctStartTime<='".$startdate."'";
$result = mysql_query($query);
$data = mysql_fetch_array($result);
$usagetotal = $data[0];
if( ( $usagetotal == "" ) || ( $usagetotal == NULL ) ) { $usagetotal = 0; }
if($usagetotal >= $usagelimit) {
$result = mysql_query( "SELECT value FROM radcheck where username = '$user' and attribute = 'Expiration'" );
$row = mysql_fetch_array($result);
if( !$row ) {
$result = mysql_query( "INSERT into radcheck (username, attribute, op, value) values ('".$user."','Expiration',':=','".date("Y-d-m G:i:s")."')" );
echo $user . ": Account has reached its usage limit.\n";
disconnect($user);
}
}
else {
$result = mysql_query( "SELECT value FROM radcheck where username = '$user' and attribute = 'Expiration'" );
$row = mysql_fetch_array($result);
if( $row ) {
$result = mysql_query("DELETE from radcheck where username = '".$user."' and attribute = 'Expiration'");
echo $user . ": Account is within its usage limit. Removing suspension\n";
}
}
}
//end
//Functions
function disconnect($username) {
require( dirname(__FILE__) . '/config.php' );
$result = mysql_query("SELECT radacctid FROM radacct where username = '".$username."' and acctstoptime is null");
while( $row = mysql_fetch_array($result) ) {
$result = mysql_query("SELECT * FROM radacct where radacctid = '".$row['radacctid']."'");
$radacct = mysql_fetch_array($result);
$result = mysql_query("SELECT * FROM nas where nasname = '".$radacct['nasipaddress']."'");
$nas = mysql_fetch_array($result);
if( $nas ) {
$command = "echo \"User-Name='".$username."',Framed-IP-Address='".$radacct['framedipaddress']."',Acct-Session-Id='".$radacct['acctsessionid']."',NAS-IP-Address='".$radacct['nasipaddress']."'\"| ".$radclient_path." -r 3 -x ".$nas['nasname'].":".$nas['ports']." disconnect ".$nas['secret']."";
exec($command,$output,$rv);
$podreply = end($output);
if( strpos( $podreply, "Disconnect-ACK" ) === true ) {
$message = $username.": Successfully disconnected session";
}
else if( strpos($podreply, "Disconnect-NAK" ) === true ) {
$message = "Failed to disconnected session. Device rejected the request.";
}
else {
$message = $username.": Failed to disconnected session. Device time out.";
}
echo $message . "\n";
}
}
}
//end
//Close radius mysql connection
mysql_close($mysql);
//end
?>