BUG12245: captive portal - clients are not automatically removed

Message ID 20191120104518.13156-1-alexander.marx@ipfire.org
State Accepted
Commit 650aac182e0d0e7ef035c963780fbadc75aecc88
Headers
Series BUG12245: captive portal - clients are not automatically removed |

Commit Message

Alexander Marx Nov. 20, 2019, 10:45 a.m. UTC
  With this patch the clients are updated and those who are expired get deleted from the hash.
In addition the table of active clients is now sorted.
---
 html/cgi-bin/captive.cgi | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
  

Patch

diff --git a/html/cgi-bin/captive.cgi b/html/cgi-bin/captive.cgi
index b33287dd4..8204eb7b3 100755
--- a/html/cgi-bin/captive.cgi
+++ b/html/cgi-bin/captive.cgi
@@ -514,6 +514,25 @@  END
 	}
 }
 
+sub cleanup_expired_coupons
+{
+	my $acttime=time();
+	&General::readhasharray($clients, \%clientshash) if (-e $clients);
+	foreach my $key (keys %clientshash) {
+
+		#calculate endtime from clientshash
+		my $endtime;
+		if ($clientshash{$key}[3] > '0'){
+			$endtime = $clientshash{$key}[2]+$clientshash{$key}[3];
+			if ($acttime > $endtime) {
+				delete $clientshash{$key};
+			}
+		}
+	}
+	#write back hash
+	&General::writehasharray("$clients", \%clientshash);
+}
+
 sub show_coupons() {
 	&General::readhasharray($coupons, \%couponhash) if (-e $coupons);
 
@@ -601,9 +620,9 @@  sub show_clients() {
 				<th align='center' width='5%'>$Lang::tr{'delete'}</th>
 			</tr>
 END
-
+	&cleanup_expired_coupons();
 	&General::readhasharray($clients, \%clientshash) if (-e $clients);
-	foreach my $key (keys %clientshash) {
+	foreach my $key (sort {$clientshash{$a}[2] <=> $clientshash{$b}[2]} keys %clientshash) {
 		#calculate time from clientshash (starttime)
 		my $starttime = sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1]  }->(localtime($clientshash{$key}[2]));