Projet

Général

Profil

« Précédent | Suivant » 

Révision 5cf91315

Ajouté par Renato Botelho il y a environ 10 ans

Migrate captive portal code to SQLite3 php module

Voir les différences:

etc/inc/captiveportal.inc
1287 1287
function captiveportal_opendb() {
1288 1288
	global $g, $cpzone;
1289 1289

  
1290
	if (file_exists("{$g['vardb_path']}/captiveportal{$cpzone}.db"))
1291
		$DB = @sqlite_open("{$g['vardb_path']}/captiveportal{$cpzone}.db");
1292
	else {
1293
		$errormsg = "";
1294
		$DB = @sqlite_open("{$g['vardb_path']}/captiveportal{$cpzone}.db");
1295
		if (@sqlite_exec($DB, "CREATE TABLE captiveportal (allow_time INTEGER, pipeno INTEGER, ip TEXT, mac TEXT, username TEXT, sessionid TEXT, bpassword TEXT, session_timeout INTEGER, idle_timeout INTEGER, session_terminate_time INTEGER, interim_interval INTEGER, radiusctx TEXT) ", $errormsg)) {
1296
			@sqlite_exec($DB, "CREATE UNIQUE INDEX idx_active ON captiveportal (sessionid, username)");
1297
			@sqlite_exec($DB, "CREATE INDEX user ON captiveportal (username)");
1298
			@sqlite_exec($DB, "CREATE INDEX ip ON captiveportal (ip)");
1299
			@sqlite_exec($DB, "CREATE INDEX starttime ON captiveportal (allow_time)");
1300
			@sqlite_exec($DB, "CREATE INDEX serviceid ON captiveportal (serviceid)");
1301
		} else
1302
			captiveportal_syslog("Error during table {$cpzone} creation. Error message: {$errormsg}");
1303
	}
1290
	$DB = new SQLite3("{$g['vardb_path']}/captiveportal{$cpzone}.db");
1291
	if (! $DB->exec("CREATE TABLE IF NOT EXISTS captiveportal (" .
1292
				"allow_time INTEGER, pipeno INTEGER, ip TEXT, mac TEXT, username TEXT, " .
1293
				"sessionid TEXT, bpassword TEXT, session_timeout INTEGER, idle_timeout INTEGER, " .
1294
				"session_terminate_time INTEGER, interim_interval INTEGER, radiusctx TEXT); " .
1295
			"CREATE UNIQUE INDEX IF NOT EXISTS idx_active ON captiveportal (sessionid, username); " .
1296
			"CREATE INDEX IF NOT EXISTS user ON captiveportal (username); " .
1297
			"CREATE INDEX IF NOT EXISTS ip ON captiveportal (ip); " .
1298
			"CREATE INDEX IF NOT EXISTS starttime ON captiveportal (allow_time)"))
1299
		captiveportal_syslog("Error during table {$cpzone} creation. Error message: {$DB->lastErrorMsg()}");
1304 1300

  
1305 1301
	return $DB;
1306 1302
}
1307 1303

  
1308 1304
/* read captive portal DB into array */
1309 1305
function captiveportal_read_db($query = "") {
1306
	$cpdb = array();
1310 1307

  
1311 1308
	$DB = captiveportal_opendb();
1312 1309
	if ($DB) {
1313
		sqlite_exec($DB, "BEGIN");
1314
		if (!empty($query))
1315
			$cpdb = @sqlite_array_query($DB, "SELECT * FROM captiveportal {$query}", SQLITE_NUM);
1316
		else {
1317
			$response = @sqlite_unbuffered_query($DB, "SELECT * FROM captiveportal", SQLITE_NUM);
1318
			$cpdb = @sqlite_fetch_all($response, SQLITE_NUM);
1319
		}
1320
		sqlite_exec($DB, "END");
1321
		@sqlite_close($DB);
1310
		$response = $DB->query("SELECT * FROM captiveportal {$query}");
1311
		while ($row = $response->fetchArray())
1312
			$cpdb[] = $row;
1313
		$DB->close();
1322 1314
	}
1323
	if (!$cpdb)
1324
		$cpdb = array();
1325 1315

  
1326 1316
	return $cpdb;
1327 1317
}
......
1352 1342

  
1353 1343
	$DB = captiveportal_opendb();
1354 1344
	if ($DB) {
1355
		$error_msg = "";
1356
		sqlite_exec($DB, "BEGIN TRANSACTION");
1357
		$result = @sqlite_exec($DB, $query, $error_msg);
1345
		$DB->exec("BEGIN TRANSACTION");
1346
		$result = $DB->exec($query);
1358 1347
		if (!$result)
1359
			captiveportal_syslog("Trying to modify DB returned error: {$error_msg}");
1348
			captiveportal_syslog("Trying to modify DB returned error: {$DB->lastErrorMsg()}");
1360 1349
		else
1361
			sqlite_exec($DB, "END TRANSACTION");
1362
		@sqlite_close($DB);
1350
			$DB->exec("END TRANSACTION");
1351
		$DB->close();
1363 1352
		return $result;
1364 1353
	} else
1365 1354
		return true;
......
1982 1971
			$interim_interval = (!empty($attributes['interim_interval'])) ? $attributes['interim_interval'] : 'NULL';
1983 1972

  
1984 1973
			/* escape username */
1985
			$safe_username = sqlite_escape_string($username);
1974
			$safe_username = SQLite3::escapeString($username);
1986 1975

  
1987 1976
			/* encode password in Base64 just in case it contains commas */
1988 1977
			$bpassword = base64_encode($password);

Formats disponibles : Unified diff