Commit e1443b42 authored by Ad Schellevis's avatar Ad Schellevis Committed by Franco Fichtner

(captive portal) fix report PHP Warning: SQLite3::query(): Unable to prepare...

(captive portal) fix report PHP Warning:  SQLite3::query(): Unable to prepare statement: 5, database is locked in /usr/local/opnsense/scripts/OPNsense/CaptivePortal/process_accounting_messages.php on line 53

(cherry picked from commit 94e28e10)
parent 3867f651
...@@ -35,6 +35,7 @@ use OPNsense\Auth\AuthenticationFactory; ...@@ -35,6 +35,7 @@ use OPNsense\Auth\AuthenticationFactory;
// open database // open database
$database_filename = '/var/captiveportal/captiveportal.sqlite'; $database_filename = '/var/captiveportal/captiveportal.sqlite';
$db = new SQLite3($database_filename); $db = new SQLite3($database_filename);
$db->busyTimeout(2000);
// query all sessions with client restrictions // query all sessions with client restrictions
$result = $db->query(' $result = $db->query('
...@@ -53,43 +54,46 @@ $result = $db->query(' ...@@ -53,43 +54,46 @@ $result = $db->query('
'); ');
// process all sessions // process all sessions
while($row = $result->fetchArray(SQLITE3_ASSOC) ){ if ($result !== false) {
$authFactory = new OPNsense\Auth\AuthenticationFactory(); while($row = $result->fetchArray(SQLITE3_ASSOC) ){
$authenticator = $authFactory->get($row['authenticated_via']); $authFactory = new OPNsense\Auth\AuthenticationFactory();
if ($authenticator != null) { $authenticator = $authFactory->get($row['authenticated_via']);
if ($row['state'] == null) { if ($authenticator != null) {
// new accounting state, send start event (if applicable) if ($row['state'] == null) {
$stmt = $db->prepare('insert into accounting_state(zoneid, sessionid, state) // new accounting state, send start event (if applicable)
values (:zoneid, :sessionid, \'RUNNING\')'); $stmt = $db->prepare('insert into accounting_state(zoneid, sessionid, state)
$stmt->bindParam(':zoneid', $row['zoneid']); values (:zoneid, :sessionid, \'RUNNING\')');
$stmt->bindParam(':sessionid', $row['sessionid']); $stmt->bindParam(':zoneid', $row['zoneid']);
$stmt->execute(); $stmt->bindParam(':sessionid', $row['sessionid']);
if (method_exists($authenticator,'startAccounting')) { $stmt->execute();
// send start accounting event if (method_exists($authenticator,'startAccounting')) {
$authenticator->startAccounting($row['username'], $row['sessionid']); // send start accounting event
} $authenticator->startAccounting($row['username'], $row['sessionid']);
} elseif ($row['deleted'] == 1 && $row['state'] != 'STOPPED') { }
// stop accounting, send stop event (if applicable) } elseif ($row['deleted'] == 1 && $row['state'] != 'STOPPED') {
$stmt = $db->prepare('update accounting_state // stop accounting, send stop event (if applicable)
set state = \'STOPPED\' $stmt = $db->prepare('update accounting_state
where zoneid = :zoneid set state = \'STOPPED\'
and sessionid = :sessionid'); where zoneid = :zoneid
$stmt->bindParam(':zoneid', $row['zoneid']); and sessionid = :sessionid');
$stmt->bindParam(':sessionid', $row['sessionid']); $stmt->bindParam(':zoneid', $row['zoneid']);
$stmt->execute(); $stmt->bindParam(':sessionid', $row['sessionid']);
if (method_exists($authenticator,'startAccounting')) { $stmt->execute();
if (method_exists($authenticator,'startAccounting')) {
$time_spend = time() - $row['created']; $time_spend = time() - $row['created'];
$authenticator->stopAccounting($row['username'], $row['sessionid'], $time_spend); $authenticator->stopAccounting($row['username'], $row['sessionid'], $time_spend);
} }
} elseif ($row['state'] != 'STOPPED') { } elseif ($row['state'] != 'STOPPED') {
// send interim updates (if applicable) // send interim updates (if applicable)
if (method_exists($authenticator,'updateAccounting')) { if (method_exists($authenticator,'updateAccounting')) {
// send interim update event // send interim update event
$time_spend = time() - $row['created']; $time_spend = time() - $row['created'];
$authenticator->updateAccounting($row['username'], $row['sessionid'], $time_spend); $authenticator->updateAccounting($row['username'], $row['sessionid'], $time_spend);
}
} }
} }
} }
} }
$db->close(); $db->close();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment