Add sqlite support

This commit is contained in:
Bartek Fabiszewski 2019-01-28 23:03:03 +01:00
parent 46ee95dbbe
commit c8535cce86
12 changed files with 183 additions and 72 deletions

View File

@ -6,6 +6,10 @@ DB_USER_PASS=$2
mkdir -p /run/nginx mkdir -p /run/nginx
chown nginx:nginx /run/nginx chown nginx:nginx /run/nginx
# Fix permission issues on mounted volume in macOS
sed -i "s/^nobody:.*$/nobody:x:1000:50::nobody:\/:\/sbin\/nologin/" /etc/passwd
sed -i "s/^nobody:.*$/nobody:x:50:/" /etc/group
sed -i "s/^\$dbuser = .*$/\$dbuser = \"ulogger\";/" /var/www/html/config.php sed -i "s/^\$dbuser = .*$/\$dbuser = \"ulogger\";/" /var/www/html/config.php
sed -i "s/^\$dbpass = .*$/\$dbpass = \"${DB_USER_PASS}\";/" /var/www/html/config.php sed -i "s/^\$dbpass = .*$/\$dbpass = \"${DB_USER_PASS}\";/" /var/www/html/config.php
@ -28,7 +32,11 @@ if [ "$ULOGGER_DB_DRIVER" = "pgsql" ]; then
su postgres -c "pg_ctl -w stop" su postgres -c "pg_ctl -w stop"
sed -i "s/^\$dbdsn = .*$/\$dbdsn = \"pgsql:host=localhost;port=5432;dbname=ulogger\";/" /var/www/html/config.php sed -i "s/^\$dbdsn = .*$/\$dbdsn = \"pgsql:host=localhost;port=5432;dbname=ulogger\";/" /var/www/html/config.php
elif [ "$ULOGGER_DB_DRIVER" = "sqlite" ]; then elif [ "$ULOGGER_DB_DRIVER" = "sqlite" ]; then
sqlite3 /data/ulogger.db < /var/www/html/scripts/ulogger.sqlite mkdir -p /data
chown nobody:nobody /data
sqlite3 -init /var/www/html/scripts/ulogger.sqlite /data/ulogger.db
sqlite3 -line /data/ulogger.db "INSERT INTO users (login, password) VALUES ('admin', '\$2y\$10\$7OvZrKgonVZM9lkzrTbiou.CVhO3HjPk5y0W9L68fVwPs/osBRIMq')"
sed -i "s/^\$dbdsn = .*$/\$dbdsn = \"sqlite:\/data\/sqlite\/ulogger.db\";/" /var/www/html/config.php
else else
mkdir -p /run/mysqld mkdir -p /run/mysqld
chown mysql:mysql /run/mysqld chown mysql:mysql /run/mysqld

View File

@ -18,7 +18,7 @@ grep '^\$' /var/www/html/config.php
# start services # start services
if [ "$ULOGGER_DB_DRIVER" = "pgsql" ]; then if [ "$ULOGGER_DB_DRIVER" = "pgsql" ]; then
su postgres -c 'pg_ctl -D /data start' su postgres -c 'pg_ctl -D /data start'
else elif [ "$ULOGGER_DB_DRIVER" = "mysql" ]; then
mysqld_safe --datadir=/data & mysqld_safe --datadir=/data &
fi fi
nginx nginx

View File

@ -1,17 +1,6 @@
<?xml version="1.0"?> <?xml version="1.0" ?>
<mysqldump xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <dataset>
<database name="ulogger"> <users id="1" login="admin" password="$2y$10$7OvZrKgonVZM9lkzrTbiou.CVhO3HjPk5y0W9L68fVwPs/osBRIMq" />
<table_data name="positions"> <tracks />
</table_data> <positions />
<table_data name="tracks"> </dataset>
</table_data>
<table_data name="users">
<!-- admin:admin -->
<row>
<field name="id">1</field>
<field name="login">admin</field>
<field name="password">$2y$10$7OvZrKgonVZM9lkzrTbiou.CVhO3HjPk5y0W9L68fVwPs/osBRIMq</field>
</row>
</table_data>
</database>
</mysqldump>

View File

@ -1,11 +1,6 @@
<?xml version="1.0"?> <?xml version="1.0" ?>
<mysqldump xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <dataset>
<database name="ulogger"> <users />
<table_data name="users"> <tracks />
</table_data> <positions />
<table_data name="tracks"> </dataset>
</table_data>
<table_data name="positions">
</table_data>
</database>
</mysqldump>

View File

@ -37,7 +37,6 @@ abstract class BaseDatabaseTestCase extends PHPUnit_Extensions_Database_TestCase
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
} }
public static function setUpBeforeClass() { public static function setUpBeforeClass() {
@ -81,15 +80,19 @@ abstract class BaseDatabaseTestCase extends PHPUnit_Extensions_Database_TestCase
* @return PHPUnit_Extensions_Database_DataSet_IDataSet * @return PHPUnit_Extensions_Database_DataSet_IDataSet
*/ */
protected function getDataSet() { protected function getDataSet() {
$this->resetSequences(); $this->resetAutoincrement();
return $this->createMySQLXMLDataSet(__DIR__ . '/../fixtures/fixture_empty.xml'); return $this->createFlatXMLDataSet(__DIR__ . '/../fixtures/fixture_empty.xml');
} }
protected function resetSequences($users = 1, $tracks = 1, $positions = 1) { protected function resetAutoincrement($users = 1, $tracks = 1, $positions = 1) {
if (self::$driver == "pgsql") { if (self::$driver == "pgsql") {
self::$pdo->query("ALTER SEQUENCE users_id_seq RESTART WITH $users"); self::$pdo->query("ALTER SEQUENCE users_id_seq RESTART WITH $users");
self::$pdo->query("ALTER SEQUENCE tracks_id_seq RESTART WITH $tracks"); self::$pdo->query("ALTER SEQUENCE tracks_id_seq RESTART WITH $tracks");
self::$pdo->query("ALTER SEQUENCE positions_id_seq RESTART WITH $positions"); self::$pdo->query("ALTER SEQUENCE positions_id_seq RESTART WITH $positions");
} else if (self::$driver == "sqlite") {
self::$pdo->query("DELETE FROM sqlite_sequence WHERE NAME = 'users'");
self::$pdo->query("DELETE FROM sqlite_sequence WHERE NAME = 'tracks'");
self::$pdo->query("DELETE FROM sqlite_sequence WHERE NAME = 'positions'");
} }
} }
@ -224,7 +227,7 @@ abstract class BaseDatabaseTestCase extends PHPUnit_Extensions_Database_TestCase
return "TO_TIMESTAMP($column)"; return "TO_TIMESTAMP($column)";
break; break;
case "sqlite": case "sqlite":
return "DATE($column, 'unixepoch')"; return "DATETIME($column, 'unixepoch')";
break; break;
} }
} }

View File

@ -26,8 +26,8 @@ class UloggerAPITestCase extends BaseDatabaseTestCase {
} }
protected function getDataSet() { protected function getDataSet() {
$this->resetSequences(2); $this->resetAutoincrement(2);
return $this->createMySQLXMLDataSet(__DIR__ . '/../fixtures/fixture_admin.xml'); return $this->createFlatXMLDataSet(__DIR__ . '/../fixtures/fixture_admin.xml');
} }
/** /**

View File

@ -43,7 +43,6 @@ class ClientAPITest extends UloggerAPITestCase {
public function testAddUser() { public function testAddUser() {
$this->assertTrue($this->authenticate(), "Authentication failed"); $this->assertTrue($this->authenticate(), "Authentication failed");
$this->assertEquals(1, $this->getConnection()->getRowCount('users'), "Wrong row count"); $this->assertEquals(1, $this->getConnection()->getRowCount('users'), "Wrong row count");
$options = [ $options = [

View File

@ -37,6 +37,13 @@
*/ */
protected static $tables; protected static $tables;
/**
* Database driver name
*
* @var String Driver
*/
protected static $driver;
/** /**
* PDO constuctor * PDO constuctor
* *
@ -52,6 +59,7 @@
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, // return assoc array by default PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, // return assoc array by default
]; ];
@parent::__construct($dsn, $user, $pass, $options); @parent::__construct($dsn, $user, $pass, $options);
self::$driver = $this->getAttribute(PDO::ATTR_DRIVER_NAME);
$this->setCharset("utf8"); $this->setCharset("utf8");
$this->initTables(); $this->initTables();
} catch (PDOException $e) { } catch (PDOException $e) {
@ -94,8 +102,7 @@
} }
public function unix_timestamp($column) { public function unix_timestamp($column) {
$driver = $this->getAttribute(PDO::ATTR_DRIVER_NAME); switch (self::$driver) {
switch ($driver) {
default: default:
case "mysql": case "mysql":
return "UNIX_TIMESTAMP($column)"; return "UNIX_TIMESTAMP($column)";
@ -110,8 +117,7 @@
} }
public function from_unixtime($column) { public function from_unixtime($column) {
$driver = $this->getAttribute(PDO::ATTR_DRIVER_NAME); switch (self::$driver) {
switch ($driver) {
default: default:
case "mysql": case "mysql":
return "FROM_UNIXTIME($column)"; return "FROM_UNIXTIME($column)";
@ -120,13 +126,15 @@
return "TO_TIMESTAMP($column)"; return "TO_TIMESTAMP($column)";
break; break;
case "sqlite": case "sqlite":
return "DATE($column, 'unixepoch')"; return "DATETIME($column, 'unixepoch')";
break; break;
} }
} }
private function setCharset($charset) { private function setCharset($charset) {
$this->query("SET NAMES '$charset'"); if (self::$driver == "pgsql" || self::$driver == "mysql") {
$this->query("SET NAMES '$charset'");
}
} }
} }
?> ?>

View File

@ -142,11 +142,11 @@
} }
public static function postString($name, $default = NULL) { public static function postString($name, $default = NULL) {
if (is_string(($val = self::requestValue($name, $default, INPUT_POST)))) { return self::requestString($name, $default, INPUT_POST);
return trim($val); }
} else {
return $val; public static function getString($name, $default = NULL) {
} return self::requestString($name, $default, INPUT_GET);
} }
public static function getBool($name, $default = NULL) { public static function getBool($name, $default = NULL) {
@ -154,22 +154,30 @@
} }
public static function postInt($name, $default = NULL) { public static function postInt($name, $default = NULL) {
if (is_float(($val = self::postFloat($name, $default)))) { return self::requestInt($name, $default, INPUT_POST);
return (int) round($val);
} else {
return self::requestValue($name, $default, INPUT_POST, FILTER_VALIDATE_INT);
}
} }
public static function getInt($name, $default = NULL) { public static function getInt($name, $default = NULL) {
if (is_float(($val = self::getFloat($name, $default)))) { return self::requestInt($name, $default, INPUT_GET);
return (int) round($val); }
private static function requestString($name, $default, $type) {
if (is_string(($val = self::requestValue($name, $default, $type)))) {
return trim($val);
} else { } else {
return self::requestValue($name, $default, INPUT_GET, FILTER_VALIDATE_INT); return $val;
} }
} }
public static function requestValue($name, $default = NULL, $type = INPUT_POST, $filters = FILTER_DEFAULT, $flags = NULL) { private static function requestInt($name, $default, $type) {
if (is_float(($val = self::requestValue($name, $default, $type, FILTER_VALIDATE_FLOAT)))) {
return (int) round($val);
} else {
return self::requestValue($name, $default, $type, FILTER_VALIDATE_INT);
}
}
private static function requestValue($name, $default, $type, $filters = FILTER_DEFAULT, $flags = NULL) {
$input = filter_input($type, $name, $filters, $flags); $input = filter_input($type, $name, $filters, $flags);
if ($input !== false && !is_null($input)) { if ($input !== false && !is_null($input)) {
return $input; return $input;

View File

@ -18,7 +18,7 @@
*/ */
// This script is disabled by default. Change below to true before running. // This script is disabled by default. Change below to true before running.
$enabled = true; $enabled = false;
/* -------------------------------------------- */ /* -------------------------------------------- */
@ -31,6 +31,7 @@ if (version_compare(PHP_VERSION, '5.4.0', '<')) {
define("ROOT_DIR", dirname(__DIR__)); define("ROOT_DIR", dirname(__DIR__));
require_once(ROOT_DIR . "/helpers/user.php"); require_once(ROOT_DIR . "/helpers/user.php");
require_once(ROOT_DIR . "/helpers/config.php"); require_once(ROOT_DIR . "/helpers/config.php");
require_once(ROOT_DIR . "/helpers/utils.php");
require_once(ROOT_DIR . "/lang.php"); require_once(ROOT_DIR . "/lang.php");
$command = uUtils::postString('command'); $command = uUtils::postString('command');
@ -39,6 +40,7 @@ $prefix = preg_replace('/[^a-z0-9_]/i', '', uConfig::$dbprefix);
$tPositions = $prefix . "positions"; $tPositions = $prefix . "positions";
$tTracks = $prefix . "tracks"; $tTracks = $prefix . "tracks";
$tUsers = $prefix . "users"; $tUsers = $prefix . "users";
$dbDriver = null;
$messages = []; $messages = [];
switch ($command) { switch ($command) {
@ -46,7 +48,9 @@ switch ($command) {
$error = false; $error = false;
try { try {
$db = new PDO(uConfig::$dbdsn, uConfig::$dbuser, uConfig::$dbpass); $options = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ];
$pdo = new PDO(uConfig::$dbdsn, uConfig::$dbuser, uConfig::$dbpass, $options);
$dbDriver = $pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
} catch (PDOException $e ) { } catch (PDOException $e ) {
$messages[] = "<span class=\"warn\">{$langSetup["dbconnectfailed"]}</span>"; $messages[] = "<span class=\"warn\">{$langSetup["dbconnectfailed"]}</span>";
$messages[] = sprintf($langSetup["serversaid"], "<b>" . $e->getMessage() . "</b>"); $messages[] = sprintf($langSetup["serversaid"], "<b>" . $e->getMessage() . "</b>");
@ -54,16 +58,16 @@ switch ($command) {
break; break;
} }
try { try {
$queries = getQueries($db); $queries = getQueries($pdo);
foreach ($queries as $query) { foreach ($queries as $query) {
$db->query($query); $pdo->query($query);
} }
} catch (PDOException $e) { } catch (PDOException $e) {
$messages[] = "<span class=\"warn\">{$langSetup["dbqueryfailed"]}</span>"; $messages[] = "<span class=\"warn\">{$langSetup["dbqueryfailed"]}</span>";
$messages[] = sprintf($langSetup["serversaid"], "<b>" . $e->getMessage() . "</b>"); $messages[] = sprintf($langSetup["serversaid"], "<b>" . $e->getMessage() . "</b>");
$error = true; $error = true;
} }
$db = null; $pdo = null;
if (!$error) { if (!$error) {
$messages[] = "<span class=\"ok\">{$langSetup["dbtablessuccess"]}</span>"; $messages[] = "<span class=\"ok\">{$langSetup["dbtablessuccess"]}</span>";
$messages[] = $langSetup["setupuser"]; $messages[] = $langSetup["setupuser"];
@ -114,22 +118,28 @@ switch ($command) {
$messages[] = "<form method=\"post\" action=\"setup.php\"><button>{$langSetup["restartbutton"]}</button></form>"; $messages[] = "<form method=\"post\" action=\"setup.php\"><button>{$langSetup["restartbutton"]}</button></form>";
break; break;
} }
if (empty(uConfig::$dbdsn) || empty(uConfig::$dbuser)) { if (empty(uConfig::$dbdsn) || ($dbDriver != "sqlite" && empty(uConfig::$dbuser))) {
$messages[] = sprintf($langSetup["nodbsettings"], "\$dbdsn, \$dbuser, \$dbpass"); if ($dbDriver == "sqlite") {
$required = "\$dbdsn";
} else {
$required = "\$dbdsn, \$dbuser, \$dbpass";
}
$messages[] = sprintf($langSetup["nodbsettings"], $required);
$messages[] = $langSetup["dorestart"]; $messages[] = $langSetup["dorestart"];
$messages[] = "<form method=\"post\" action=\"setup.php\"><button>{$langSetup["restartbutton"]}</button></form>"; $messages[] = "<form method=\"post\" action=\"setup.php\"><button>{$langSetup["restartbutton"]}</button></form>";
break; break;
} }
$messages[] = sprintf($langSetup["scriptdesc"], "'$tPositions', '$tTracks', '$tUsers'", "<b>" . uConfig::$dbname . "</b>"); $messages[] = sprintf($langSetup["scriptdesc"], "'$tPositions', '$tTracks', '$tUsers'", "<b>" . getDbname(uConfig::$dbdsn) . "</b>");
$messages[] = $langSetup["scriptdesc2"]; $messages[] = $langSetup["scriptdesc2"];
$messages[] = "<form method=\"post\" action=\"setup.php\"><input type=\"hidden\" name=\"command\" value=\"setup\"><button>{$langSetup["startbutton"]}</button></form>"; $messages[] = "<form method=\"post\" action=\"setup.php\"><input type=\"hidden\" name=\"command\" value=\"setup\"><button>{$langSetup["startbutton"]}</button></form>";
break; break;
} }
function getQueries($db) { function getQueries($pdo) {
$driver = $db->getAttribute(PDO::ATTR_DRIVER_NAME); global $tPositions, $tUsers, $tTracks, $dbDriver;
$queries = []; $queries = [];
switch($driver) { switch($dbDriver) {
case "mysql": case "mysql":
// users // users
$queries[] = "DROP TABLE IF EXISTS `$tUsers`"; $queries[] = "DROP TABLE IF EXISTS `$tUsers`";
@ -262,6 +272,29 @@ function getQueries($db) {
default: default:
throw InvalidArgumentException("Driver not supported"); throw InvalidArgumentException("Driver not supported");
} }
return $queries;
}
function getDbname($dsn) {
if (strpos($dsn, ':') !== false) {
list($scheme, $dsnWithoutScheme) = explode(':', $dsn, 2);
switch ($scheme) {
case 'sqlite':
case 'sqlite2':
case 'sqlite3':
return $dsnWithoutScheme;
break;
default:
$pattern = '~dbname=([^;]*)(?:;|$)~';
$result = preg_match($pattern, $dsnWithoutScheme, $matches);
if ($result === 1 && !empty($matches[1])) {
return $matches[1];
}
break;
}
}
return "noname";
} }
?> ?>

68
scripts/ulogger.sqlite Normal file
View File

@ -0,0 +1,68 @@
--
-- Database: `ulogger`
--
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` integer PRIMARY KEY AUTOINCREMENT,
`login` varchar(15) NOT NULL UNIQUE,
`password` varchar(255) NOT NULL DEFAULT ''
);
-- --------------------------------------------------------
--
-- Table structure for table `tracks`
--
DROP TABLE IF EXISTS `tracks`;
CREATE TABLE `tracks` (
`id` integer PRIMARY KEY AUTOINCREMENT,
`user_id` integer NOT NULL,
`name` varchar(255) DEFAULT NULL,
`comment` varchar(1024) DEFAULT NULL,
FOREIGN KEY(`user_id`) REFERENCES `users`(`id`)
);
CREATE INDEX `idx_user_id` ON `tracks`(`user_id`);
-- --------------------------------------------------------
--
-- Table structure for table `positions`
--
DROP TABLE IF EXISTS `positions`;
CREATE TABLE `positions` (
`id` integer PRIMARY KEY AUTOINCREMENT,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`user_id` integer NOT NULL,
`track_id` integer NOT NULL,
`latitude` double NOT NULL,
`longitude` double NOT NULL,
`altitude` double DEFAULT NULL,
`speed` double DEFAULT NULL,
`bearing` double DEFAULT NULL,
`accuracy` integer DEFAULT NULL,
`provider` varchar(100) DEFAULT NULL,
`comment` varchar(255) DEFAULT NULL,
`image_id` integer DEFAULT NULL,
FOREIGN KEY(`user_id`) REFERENCES `users`(`id`),
FOREIGN KEY(`track_id`) REFERENCES `tracks`(`id`)
);
CREATE INDEX `idx_ptrack_id` ON `positions`(`track_id`);
CREATE INDEX `idx_puser_id` ON `positions`(`user_id`);
--
-- This will add default user admin with password admin
-- The password should be changed immediatelly after installation
-- Uncomment if needed
--
-- INSERT INTO `users` (`id`, `login`, `password`) VALUES
-- (1, 'admin', '$2y$10$7OvZrKgonVZM9lkzrTbiou.CVhO3HjPk5y0W9L68fVwPs/osBRIMq');

View File

@ -57,9 +57,9 @@ function toHMS($s) {
return (($d > 0) ? "$d d " : "") . sprintf("%02d:%02d:%02d", $h, $m, $s); return (($d > 0) ? "$d d " : "") . sprintf("%02d:%02d:%02d", $h, $m, $s);
} }
$type = uUtils::postString('type', 'kml'); $type = uUtils::getString('type', 'kml');
$userId = uUtils::postInt('userid'); $userId = uUtils::getInt('userid');
$trackId = uUtils::postInt('trackid'); $trackId = uUtils::getInt('trackid');
if (!uConfig::$public_tracks && if (!uConfig::$public_tracks &&
(!$auth->isAuthenticated() || (!$auth->isAdmin() && $auth->user->id !== $userId))) { (!$auth->isAuthenticated() || (!$auth->isAdmin() && $auth->user->id !== $userId))) {