From 151c59149f6f729f7b471ceb398239e6e458181c Mon Sep 17 00:00:00 2001 From: Charlie Root Date: Fri, 12 Dec 2014 19:01:23 +0000 Subject: [PATCH] captive portal remove captiveportal_read_db and replaced with new model --- src/etc/inc/captiveportal.inc | 38 +++++++++---------- src/etc/inc/voucher.inc | 16 +++----- .../mvc/app/models/Captiveportal/DB.php | 11 +++++- 3 files changed, 32 insertions(+), 33 deletions(-) diff --git a/src/etc/inc/captiveportal.inc b/src/etc/inc/captiveportal.inc index c919c073e..5ac180da3 100644 --- a/src/etc/inc/captiveportal.inc +++ b/src/etc/inc/captiveportal.inc @@ -236,15 +236,6 @@ function captiveportal_allowedhostname_configure() { return $rules; } -/* read captive portal DB into array */ -function captiveportal_read_db($query = "") { - $cpdb = array(); - - throw Exception("TODO FIX code "); - - return $cpdb; -} - // Unchanged @@ -952,21 +943,26 @@ function captiveportal_radius_stop_all() { $radiusservers = captiveportal_get_radius_servers(); if (!empty($radiusservers)) { - $cpdb = captiveportal_read_db(); - foreach ($cpdb as $cpentry) { - if (empty($cpentry[11])) - $cpentry[11] = 'first'; - if (!empty($radiusservers[$cpentry[11]])) { - RADIUS_ACCOUNTING_STOP($cpentry[1], // ruleno - $cpentry[4], // username - $cpentry[5], // sessionid - $cpentry[0], // start time - $radiusservers[$cpentry[11]], - $cpentry[2], // clientip - $cpentry[3], // clientmac + $cpdb = new Captiveportal\DB($cpzone); + + $clients = $cpdb->listClients(array()); + + foreach ($clients as $cpentry) { + if (empty($cpentry->radiusctx)) + $cpentry->radiusctx = 'first'; + if (!empty($radiusservers[$cpentry->radiusctx])) { + RADIUS_ACCOUNTING_STOP($cpentry->pipeno_in, // ruleno + $cpentry->username, // username + $cpentry->sessionid, // sessionid + $cpentry->allow_time, // start time + $radiusservers[$cpentry->radiusctx], + $cpentry->ip, // clientip + $cpentry->mac, // clientmac 7); // Admin Reboot } } + + unset($cpdb); } } diff --git a/src/etc/inc/voucher.inc b/src/etc/inc/voucher.inc index 3e539fe81..d820dfb2a 100644 --- a/src/etc/inc/voucher.inc +++ b/src/etc/inc/voucher.inc @@ -242,7 +242,6 @@ function voucher_expire($voucher_received) { // split into an array. Useful for multiple vouchers given $a_vouchers_received = preg_split("/[\t\n\r ]+/s", $voucher_received); $active_dirty = false; - $unsetindexes = array(); // go through all received vouchers, check their valid and extract // Roll# and Ticket# using the external readvoucher binary @@ -276,12 +275,13 @@ function voucher_expire($voucher_received) { captiveportal_syslog("{$voucher} ({$roll}/{$nr}) forced to expire"); /* Check if this voucher has any active sessions */ - $cpentry = captiveportal_read_db("WHERE username = '{$voucher}'"); - if (!empty($cpentry)) { - captiveportal_disconnect($cpentry,null,13); - captiveportal_logportalauth($cpentry[4],$cpentry[3],$cpentry[2],"FORCLY TERMINATING VOUCHER {$voucher} SESSION"); - $unsetindexes[] = $cpentry[5]; + $cpdb = new Captiveportal\DB($cpzone); + if ($db->countClients(array("username"=>$voucher)) > 0 ) { + captiveportal_disconnect(array("username"=>$voucher),null,13); + //TODO: fix logging (in disconnect?) captiveportal_logportalauth($cpentry[4],$cpentry[3],$cpentry[2],"FORCLY TERMINATING VOUCHER {$voucher} SESSION"); } + + unset($cpdb); } else captiveportal_syslog("$voucher ($roll/$nr): not found on any registererd Roll"); } else @@ -314,10 +314,6 @@ function voucher_expire($voucher_received) { unlock($voucherlck); - /* Write database */ - if (!empty($unsetindexes)) - captiveportal_remove_entries($unsetindexes); - return true; } diff --git a/src/opnsense/mvc/app/models/Captiveportal/DB.php b/src/opnsense/mvc/app/models/Captiveportal/DB.php index 9893d8b64..4c8d60e44 100644 --- a/src/opnsense/mvc/app/models/Captiveportal/DB.php +++ b/src/opnsense/mvc/app/models/Captiveportal/DB.php @@ -259,10 +259,17 @@ class DB { * * @return mixed number of connected users/clients */ - function countClients(){ + function countClients($qryargs=array(),$operator="and"){ $query = "select count(*) cnt from captiveportal "; + $qry_tag = "where " ; + foreach ( $qryargs as $fieldname => $fieldvalue ){ + if ( array_key_exists($fieldname,$this->captiveportal_types) ){ + $query .= $qry_tag . $fieldname." = "." :".$fieldname." "; + $qry_tag = " ".$operator." "; + } + } - $resultset = $this->handle->query($query, array(), $this->captiveportal_types); + $resultset = $this->handle->query($query, $qryargs, $this->captiveportal_types); $resultset->setFetchMode(\Phalcon\Db::FETCH_OBJ); return $resultset->fetchAll()[0]->cnt;