|
|
@@ -7,18 +7,24 @@ define('TELEMETRY_SETTINGS_FILE', 'telemetry_settings.php');
|
|
|
/**
|
|
|
* @return PDO|false
|
|
|
*/
|
|
|
-function getPdo()
|
|
|
+function getPdo($returnErrorMessage = false)
|
|
|
{
|
|
|
if (
|
|
|
!file_exists(TELEMETRY_SETTINGS_FILE)
|
|
|
|| !is_readable(TELEMETRY_SETTINGS_FILE)
|
|
|
) {
|
|
|
+ if($returnErrorMessage){
|
|
|
+ return 'missing TELEMETRY_SETTINGS_FILE';
|
|
|
+ }
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
require TELEMETRY_SETTINGS_FILE;
|
|
|
|
|
|
if (!isset($db_type)) {
|
|
|
+ if($returnErrorMessage){
|
|
|
+ return "db_type not set in '" . TELEMETRY_SETTINGS_FILE . "'";
|
|
|
+ }
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
@@ -27,6 +33,47 @@ function getPdo()
|
|
|
];
|
|
|
|
|
|
try {
|
|
|
+ if ('mssql' === $db_type) {
|
|
|
+ if (!isset(
|
|
|
+ $MsSql_server,
|
|
|
+ $MsSql_databasename,
|
|
|
+ $MsSql_WindowsAuthentication
|
|
|
+ )) {
|
|
|
+ if($returnErrorMessage){
|
|
|
+ return "Required MSSQL database settings missing in '" . TELEMETRY_SETTINGS_FILE . "'";
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!$MsSql_WindowsAuthentication and
|
|
|
+ !isset(
|
|
|
+ $MsSql_username,
|
|
|
+ $MsSql_password,
|
|
|
+ )
|
|
|
+ ) {
|
|
|
+ if($returnErrorMessage){
|
|
|
+ return "Required MSSQL database settings missing in '" . TELEMETRY_SETTINGS_FILE . "'";
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ $dsn = 'sqlsrv:'
|
|
|
+ .'server='.$MsSql_server
|
|
|
+ .';Database='.$MsSql_databasename;
|
|
|
+
|
|
|
+ if($MsSql_TrustServerCertificate === true){
|
|
|
+ $dsn = $dsn . ';TrustServerCertificate=1';
|
|
|
+ }
|
|
|
+ if($MsSql_TrustServerCertificate === false){
|
|
|
+ $dsn = $dsn . ';TrustServerCertificate=0';
|
|
|
+ }
|
|
|
+
|
|
|
+ if($MsSql_WindowsAuthentication){
|
|
|
+ return new PDO($dsn, "", "", $pdoOptions);
|
|
|
+ } else {
|
|
|
+ return new PDO($dsn, $MySql_username, $MySql_password, $pdoOptions);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
if ('mysql' === $db_type) {
|
|
|
if (!isset(
|
|
|
$MySql_hostname,
|
|
|
@@ -35,7 +82,10 @@ function getPdo()
|
|
|
$MySql_username,
|
|
|
$MySql_password
|
|
|
)) {
|
|
|
- return false;
|
|
|
+ if($returnErrorMessage){
|
|
|
+ return "Required mysql database settings missing in '" . TELEMETRY_SETTINGS_FILE . "'";
|
|
|
+ }
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
$dsn = 'mysql:'
|
|
|
@@ -48,6 +98,9 @@ function getPdo()
|
|
|
|
|
|
if ('sqlite' === $db_type) {
|
|
|
if (!isset($Sqlite_db_file)) {
|
|
|
+ if($returnErrorMessage){
|
|
|
+ return "Required sqlite database settings missing in '" . TELEMETRY_SETTINGS_FILE . "'";
|
|
|
+ }
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
@@ -80,7 +133,10 @@ function getPdo()
|
|
|
$PostgreSql_username,
|
|
|
$PostgreSql_password
|
|
|
)) {
|
|
|
- return false;
|
|
|
+ if($returnErrorMessage){
|
|
|
+ return "Required postgresql database settings missing in '" . TELEMETRY_SETTINGS_FILE . "'";
|
|
|
+ }
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
$dsn = 'pgsql:'
|
|
|
@@ -90,9 +146,15 @@ function getPdo()
|
|
|
return new PDO($dsn, $PostgreSql_username, $PostgreSql_password, $pdoOptions);
|
|
|
}
|
|
|
} catch (Exception $e) {
|
|
|
+ if($returnErrorMessage){
|
|
|
+ return $e->getMessage();
|
|
|
+ }
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+ if($returnErrorMessage){
|
|
|
+ return "db_type '" . $db_type . "' not supported";
|
|
|
+ }
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
@@ -109,12 +171,15 @@ function isObfuscationEnabled()
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @return string|false returns the id of the inserted column or false on error
|
|
|
+ * @return string|false returns the id of the inserted column or false on error if returnErrorMessage is false or a error message if returnErrorMessage is true
|
|
|
*/
|
|
|
-function insertSpeedtestUser($ip, $ispinfo, $extra, $ua, $lang, $dl, $ul, $ping, $jitter, $log)
|
|
|
+function insertSpeedtestUser($ip, $ispinfo, $extra, $ua, $lang, $dl, $ul, $ping, $jitter, $log, $returnExceptionOnError = false)
|
|
|
{
|
|
|
$pdo = getPdo();
|
|
|
if (!($pdo instanceof PDO)) {
|
|
|
+ if($returnExceptionOnError){
|
|
|
+ return new Exception("Failed to get database connection object");
|
|
|
+ }
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
@@ -129,6 +194,9 @@ function insertSpeedtestUser($ip, $ispinfo, $extra, $ua, $lang, $dl, $ul, $ping,
|
|
|
]);
|
|
|
$id = $pdo->lastInsertId();
|
|
|
} catch (Exception $e) {
|
|
|
+ if($returnExceptionOnError){
|
|
|
+ return $e;
|
|
|
+ }
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
@@ -142,16 +210,19 @@ function insertSpeedtestUser($ip, $ispinfo, $extra, $ua, $lang, $dl, $ul, $ping,
|
|
|
/**
|
|
|
* @param int|string $id
|
|
|
*
|
|
|
- * @return array|null|false returns the speedtest data as array, null
|
|
|
+ * @return array|null|false|exception returns the speedtest data as array, null
|
|
|
* if no data is found for the given id or
|
|
|
- * false if there was an error
|
|
|
+ * false or an exception if there was an error (based on returnExceptionOnError)
|
|
|
*
|
|
|
* @throws RuntimeException
|
|
|
*/
|
|
|
-function getSpeedtestUserById($id)
|
|
|
+function getSpeedtestUserById($id,$returnExceptionOnError = false)
|
|
|
{
|
|
|
$pdo = getPdo();
|
|
|
if (!($pdo instanceof PDO)) {
|
|
|
+ if($returnExceptionOnError){
|
|
|
+ return new Exception("Failed to get database connection object");
|
|
|
+ }
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
@@ -170,6 +241,9 @@ function getSpeedtestUserById($id)
|
|
|
$stmt->execute();
|
|
|
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
} catch (Exception $e) {
|
|
|
+ if($returnExceptionOnError){
|
|
|
+ return $e;
|
|
|
+ }
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
@@ -195,14 +269,20 @@ function getLatestSpeedtestUsers()
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+ require TELEMETRY_SETTINGS_FILE;
|
|
|
+
|
|
|
try {
|
|
|
- $stmt = $pdo->query(
|
|
|
- 'SELECT
|
|
|
- id, timestamp, ip, ispinfo, ua, lang, dl, ul, ping, jitter, log, extra
|
|
|
+ $sql = 'SELECT ';
|
|
|
+
|
|
|
+ if('mssql' === $db_type) {$sql .= ' TOP(100) ';}
|
|
|
+
|
|
|
+ $sql .= ' id, timestamp, ip, ispinfo, ua, lang, dl, ul, ping, jitter, log, extra
|
|
|
FROM speedtest_users
|
|
|
- ORDER BY timestamp DESC
|
|
|
- LIMIT 100'
|
|
|
- );
|
|
|
+ ORDER BY timestamp DESC ';
|
|
|
+
|
|
|
+ if('mssql' !== $db_type) {$sql .= ' LIMIT 100 ';}
|
|
|
+
|
|
|
+ $stmt = $pdo->query($sql);
|
|
|
|
|
|
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|