Newer
Older
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# Return user logs summary
function getWiSPUserLogsSummary($params) {
$res = DBSelect("
SELECT
user_attributes.Name,
user_attributes.Value
FROM
user_attributes
WHERE
user_attributes.UserID = ?",
array($params[0]['ID'])
);
# Return if error
if (!is_object($res)) {
return $res;
}
# Array of results
$resultArray = array();
# Fetch uptime and traffic limits, if not found, this is prepaid account.. use -1 as we need int
$trafficCap = -1;
$uptimeCap = -1;
while ($row = $res->fetchObject()) {
if ($row->name == 'SMRadius-Capping-Traffic-Limit') {
$trafficCap = (int)$row->value;
}
if ($row->name == 'SMRadius-Capping-Uptime-Limit') {
$uptimeCap = (int)$row->value;
}
}
# Add cap type / amount to result
$resultArray['trafficCap'] = $trafficCap;
$resultArray['uptimeCap'] = $uptimeCap;
# Dates we want to use to search search
$dateFrom = new DateTime($params[0]['From']);
$dateTo = new DateTime($params[0]['To']);
# Fetch user uptime and traffic summary
$res = DBSelect("
SELECT
topups_summary.Balance,
topups.Type,
topups.Value
FROM
topups_summary,
topups
WHERE
topups_summary.TopupID = topups.ID
AND topups.UserID = ?
AND topups_summary.PeriodKey = ?
AND topups.Depleted = 0
ORDER BY
topups.Timestamp",
array($params[0]['ID'],$dateFrom->format('Y-m'))
);
# Return if error
if (!is_object($res)) {
return $res;
}
# Store summary topups
$topups = array();
$i = 0;
while ($row = $res->fetchObject()) {
$topups[$i] = array();
$topups[$i]['Type'] = $row->type;
$topups[$i]['Limit'] = $row->balance;
$topups[$i]['OriginalLimit'] = $row->value;
$i++;
}
# Fetch user uptime and traffic topups
$res = DBSelect("
SELECT
Value, Type
FROM
topups
WHERE
topups.UserID = ?
AND topups.ValidFrom = ?
AND topups.ValidTo >= ?
AND topups.Depleted = 0
ORDER BY
topups.Timestamp",
array($params[0]['ID'],$dateFrom->format('Y-m-d'),$dateTo->format('Y-m-d'))
);
# Return if error
if (!is_object($res)) {
return $res;
}
# Store normal topups
while ($row = $res->fetchObject()) {
$topups[$i] = array();
$topups[$i]['Type'] = $row->type;
$topups[$i]['Limit'] = $row->value;
$i++;
}
$res = DBSelect("
SELECT
accounting.AcctSessionTime,
accounting.AcctInputOctets,
accounting.AcctInputGigawords,
accounting.AcctOutputOctets,
accounting.AcctOutputGigawords
FROM
accounting, users
WHERE
users.ID = ?
AND EventTimestamp >= ?
AND accounting.Username = users.Username",
array($params[0]['ID'],$dateFrom->format('Y-m-d'))
);
if (!is_object($res)) {
return $res;
}
# Set total traffic and uptime used
$totalTraffic = 0;
$totalUptime = 0;
while ($row = $res->fetchObject()) {
# Traffic in
$inputDataItem = 0;
if (isset($row->acctinputoctets) && $row->acctinputoctets > 0) {
$inputDataItem += ($row->acctinputoctets / 1024) / 1024;
}
if (isset($row->acctinputgigawords) && $row->acctinputgigawords > 0) {
$inputDataItem += ($row->acctinputgigawords * 4096);
}
$totalTraffic += $inputDataItem;
# Traffic out
$outputDataItem = 0;
if (isset($row->acctoutputoctets) && $row->acctoutputoctets > 0) {
$outputDataItem += ($row->acctoutputoctets / 1024) / 1024;
}
if (isset($row->acctoutputgigawords) && $row->acctoutputgigawords > 0) {
$outputDataItem += ($row->acctoutputgigawords * 4096);
}
$totalTraffic += $outputDataItem;
# Uptime
$sessionTimeItem = 0;
if (isset($row->acctsessiontime) && $row->acctsessiontime > 0) {
$sessionTimeItem += $row->acctsessiontime;
}
$totalUptime += $sessionTimeItem;
# Round up
$totalUptime = ceil($totalUptime / 60);
}
# Set excess traffic usage
$excessTraffic = 0;
if (is_numeric($trafficCap) && $trafficCap > 0) {
$excessTraffic += $totalTraffic - $trafficCap;
} else {
$excessTraffic += $totalTraffic;
}
# Set excess uptime usage
$excessUptime = 0;
if (is_numeric($uptimeCap) && $uptimeCap > 0) {
$excessUptime += $totalUptime - $uptimeCap;
} else {
$excessUptime += $totalUptime;
}
$currentTrafficTopup = array();
$topupTrafficRemaining = 0;
# Loop through traffic topups and check for current topup, total topups not being used
if (is_string($trafficCap) || $trafficCap != 0) {
$i = 0;
# User is using traffic from topups
if ($excessTraffic > 0) {
foreach ($topups as $topupItem) {
if ($topupItem['Type'] == 1) {
if ($excessTraffic <= 0) {
$topupTrafficRemaining += $topupItem['Limit'];
next($topupItem);
} elseif ($excessTraffic >= $topupItem['Limit']) {
$excessTraffic -= $topupItem['Limit'];
} else {
if (isset($topupItem['OriginalLimit'])) {
$currentTrafficTopup['Cap'] = $topupItem['OriginalLimit'];
} else {
$currentTrafficTopup['Cap'] = $topupItem['Limit'];
}
$currentTrafficTopup['Used'] = $excessTraffic;
$excessTraffic -= $topupItem['Limit'];
}
}
}
# User has not used traffic topups yet
} else {
foreach ($topups as $topupItem) {
if ($topupItem['Type'] == 1) {
if ($i == 0) {
if (isset($topupItem['OriginalLimit'])) {
$currentTrafficTopup['Cap'] = $topupItem['OriginalLimit'];
} else {
$currentTrafficTopup['Cap'] = $topupItem['Limit'];
}
$i = 1;
$currentTrafficTopup['Used'] = 0;
} else {
$topupTrafficRemaining += $topupItem['Limit'];
}
}
}
}
}
$currentUptimeTopup = array();
$topupUptimeRemaining = 0;
# Loop through uptime topups and check for current topup, total topups not being used
if (is_string($uptimeCap) || $uptimeCap != 0) {
$i = 0;
# User is using uptime from topups
if ($excessUptime > 0) {
foreach ($topups as $topupItem) {
if ($topupItem['Type'] == 2) {
if ($excessUptime <= 0) {
$topupUptimeRemaining += $topupItem['Limit'];
next($topupItem);
} elseif ($excessUptime >= $topupItem['Limit']) {
$excessUptime -= $topupItem['Limit'];
} else {
if (isset($topupItem['OriginalLimit'])) {
$currentUptimeTopup['Cap'] = $topupItem['OriginalLimit'];
} else {
$currentUptimeTopup['Cap'] = $topupItem['Limit'];
}
$currentUptimeTopup['Used'] = $excessUptime;
$excessUptime -= $topupItem['Limit'];
}
}
}
# User has not used uptime topups yet
} else {
foreach ($topups as $topupItem) {
if ($topupItem['Type'] == 2) {
if ($i == 0) {
if (isset($topupItem['OriginalLimit'])) {
$currentUptimeTopup['Cap'] = $topupItem['OriginalLimit'];
} else {
$currentUptimeTopup['Cap'] = $topupItem['Limit'];
}
$i = 1;
$currentUptimeTopup['Used'] = 0;
} else {
$topupUptimeRemaining += $topupItem['Limit'];
}
}
}
}
}
# Traffic..
$resultArray['trafficCurrentTopupUsed'] = -1;
$resultArray['trafficCurrentTopupCap'] = -1;
if (count($currentTrafficTopup) > 0) {
$resultArray['trafficCurrentTopupUsed'] = $currentTrafficTopup['Used'];
$resultArray['trafficCurrentTopupCap'] = (int)$currentTrafficTopup['Cap'];
}
$resultArray['trafficTopupRemaining'] = $topupTrafficRemaining;
# Uptime..
$resultArray['uptimeCurrentTopupUsed'] = -1;
$resultArray['uptimeCurrentTopupCap'] = -1;
if (count($currentUptimeTopup) > 0) {
$resultArray['uptimeCurrentTopupUsed'] = $currentUptimeTopup['Used'];
$resultArray['uptimeCurrentTopupCap'] = (int)$currentUptimeTopup['Cap'];
}
$resultArray['uptimeTopupRemaining'] = $topupUptimeRemaining;
# Return results
return array($resultArray, 1);
}
function getWiSPUserLogs($params) {
# Filters and sorts are the same here
$filtersorts = array(
'ID' => 'accounting.ID',
'EventTimestamp' => 'accounting.EventTimestamp',
'AcctStatusType' => 'accounting.AcctStatusType',
'ServiceType' => 'accounting.ServiceType',
'FramedProtocol' => 'accounting.FramedProtocol',
'NASPortType' => 'accounting.NASPortType',
'NASPortID' => 'accounting.NASPortID',
'CallingStationID' => 'accounting.CallingStationID',
'CalledStationID' => 'accounting.CalledStationID',
'AcctSessionID' => 'accounting.AcctSessionID',
'FramedIPAddress' => 'accounting.FramedIPAddress',
);
accounting.EventTimestamp,
accounting.AcctStatusType,
accounting.ServiceType,
accounting.FramedProtocol,
accounting.NASPortType,
accounting.NASPortID,
accounting.CallingStationID,
accounting.CalledStationID,
accounting.AcctSessionID,
accounting.FramedIPAddress,
accounting.AcctInputOctets,
accounting.AcctInputGigawords,
accounting.AcctOutputOctets,
accounting.AcctOutputGigawords,
accounting.AcctTerminateCause,
accounting.AcctSessionTime
users.Username = accounting.Username
AND
users.ID = ".DBQuote($params[0])."
",$params[1],$filtersorts,$filtersorts);
$sth = $res[0]; $numResults = $res[1];
# If STH is blank, return the error back to whoever requested the data
if (!isset($sth)) {
return $res;
}
$resultArray = array();
while ($row = $sth->fetchObject()) {
# Input
$acctInputMbyte = 0;
if (isset($row->acctinputoctets) && $row->acctinputoctets > 0) {
$acctInputMbyte += ($row->acctinputoctets / 1024) / 1024;
}
if (isset($row->acctinputgigawords) && $row->acctinputgigawords > 0) {
$acctInputMbyte += ($row->acctinputgigawords * 4096);
}
# Output
$acctOutputMbyte = 0;
if (isset($row->acctoutputoctets) && $row->acctoutputoctets > 0) {
$acctOutputMbyte += ($row->acctoutputoctets / 1024) / 1024;
}
if (isset($row->acctoutputgigawords) && $row->acctoutputgigawords > 0) {
$acctOutputMbyte += ($row->acctoutputgigawords * 4096);
}
# Uptime
$acctSessionTime = 0;
if (isset($row->acctsessiontime) && $row->acctsessiontime > 0) {
$acctSessionTime += ($row->acctsessiontime / 60);
}
ceil($acctSessionTime);
$date = new DateTime($row->eventtimestamp);
$value = $date->format("Y-m-d H:i:s");
$item['EventTimestamp'] = $value;
$item['AcctStatusType'] = $row->acctstatustype;
$item['ServiceType'] = $row->servicetype;
$item['FramedProtocol'] = $row->framedprotocol;
$item['NASPortType'] = $row->nasporttype;
$item['NASPortID'] = $row->nasportid;
$item['CallingStationID'] = $row->callingstationid;
$item['CalledStationID'] = $row->calledstationid;
$item['AcctSessionID'] = $row->acctsessionid;
$item['FramedIPAddress'] = $row->framedipaddress;
$item['AcctInputMbyte'] = $acctInputMbyte;
$item['AcctOutputMbyte'] = $acctOutputMbyte;
$item['ConnectTermReason'] = strRadiusTermCode($row->servicetype);
return array($resultArray,$numResults);
}
# vim: ts=4