diff --git a/config.default.php b/config.default.php index 57e33bb..276ea74 100755 --- a/config.default.php +++ b/config.default.php @@ -41,10 +41,9 @@ $init_longitude = 21.01; $gkey = ""; // MySQL config -$dbhost = ""; // mysql host, eg. localhost +$dbdsn = ""; // DSN eg. "mysql:host=localhost;port=3307;dbname=testdb;charset=utf8" $dbuser = ""; // database user $dbpass = ""; // database pass -$dbname = ""; // database name $dbprefix = ""; // optional table names prefix, eg. "ulogger_" // other diff --git a/helpers/config.php b/helpers/config.php index 8df02bc..03aefc6 100644 --- a/helpers/config.php +++ b/helpers/config.php @@ -44,10 +44,9 @@ static $init_longitude = 21.01; // MySQL config - static $dbhost = ""; // mysql host, eg. localhost + static $dbdsn = ""; // database dsn static $dbuser = ""; // database user static $dbpass = ""; // database pass - static $dbname = ""; // database name static $dbprefix = ""; // optional table names prefix, eg. "ulogger_" // require login/password authentication @@ -82,7 +81,7 @@ static $strokeWeight = 2; static $strokeColor = '#ff0000'; static $strokeOpacity = 1; - + private static $fileLoaded = false; private static $initialized = false; @@ -114,10 +113,9 @@ if (isset($ol_layers)) { self::$ol_layers = $ol_layers; } if (isset($init_latitude)) { self::$init_latitude = $init_latitude; } if (isset($init_longitude)) { self::$init_longitude = $init_longitude; } - if (isset($dbhost)) { self::$dbhost = $dbhost; } + if (isset($dbdsn)) { self::$dbdsn = $dbdsn; } if (isset($dbuser)) { self::$dbuser = $dbuser; } if (isset($dbpass)) { self::$dbpass = $dbpass; } - if (isset($dbname)) { self::$dbname = $dbname; } if (isset($dbprefix)) { self::$dbprefix = $dbprefix; } if (isset($require_authentication)) { self::$require_authentication = (bool) $require_authentication; } if (isset($public_tracks)) { self::$public_tracks = (bool) $public_tracks; } diff --git a/helpers/db.php b/helpers/db.php index eb57a03..fff94e3 100644 --- a/helpers/db.php +++ b/helpers/db.php @@ -20,9 +20,9 @@ require_once(ROOT_DIR . "/helpers/config.php"); /** - * mysqli wrapper + * PDO wrapper */ - class uDb extends mysqli { + class uDb extends PDO { /** * Singleton instance * @@ -38,23 +38,25 @@ protected static $tables; /** - * Private constuctor + * PDO constuctor * - * @param string $host + * @param string $dsn * @param string $user * @param string $pass - * @param string $name - * @param int $port - * @param string $socket */ - public function __construct($host, $user, $pass, $name, $port = null, $socket = null) { - @parent::__construct($host, $user, $pass, $name, $port, $socket); - if ($this->connect_error) { + public function __construct($dsn, $user, $pass) { + try { + $options = [ + PDO::ATTR_EMULATE_PREPARES => false, // try to use native prepared statements + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // throw exceptions + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, // return assoc array by default + ]; + @parent::__construct($dsn, $user, $pass, $options); + $this->initTables(); + } catch (PDOException $e) { header("HTTP/1.1 503 Service Unavailable"); - die("Database connection error (" . $this->connect_error . ")"); + die("Database connection error (" . $e->getMessage() . ")"); } - $this->set_charset('utf8'); - $this->initTables(); } /** @@ -75,7 +77,7 @@ */ public static function getInstance() { if (!self::$instance) { - self::$instance = new self(uConfig::$dbhost, uConfig::$dbuser, uConfig::$dbpass, uConfig::$dbname); + self::$instance = new self(uConfig::$dbdsn, uConfig::$dbuser, uConfig::$dbpass); } return self::$instance; } diff --git a/helpers/position.php b/helpers/position.php index 0b9ccab..d70fb4d 100644 --- a/helpers/position.php +++ b/helpers/position.php @@ -54,12 +54,17 @@ $query = "SELECT p.id, UNIX_TIMESTAMP(p.time) AS tstamp, p.user_id, p.track_id, p.latitude, p.longitude, p.altitude, p.speed, p.bearing, p.accuracy, p.provider, p.comment, p.image_id, u.login, t.name - FROM `" . self::db()->table('positions') . "` p - LEFT JOIN `" . self::db()->table('users') . "` u ON (p.user_id = u.id) - LEFT JOIN `" . self::db()->table('tracks') . "` t ON (p.track_id = t.id) + FROM " . self::db()->table('positions') . " p + LEFT JOIN " . self::db()->table('users') . " u ON (p.user_id = u.id) + LEFT JOIN " . self::db()->table('tracks') . " t ON (p.track_id = t.id) WHERE id = ? LIMIT 1"; - $params = [ 'i', $positionId ]; - $this->loadWithQuery($query, $params); + $params = [ $positionId ]; + try { + $this->loadWithQuery($query, $params); + } catch (PDOException $e) { + // TODO: handle exception +throw $e; + } } } @@ -99,19 +104,20 @@ if (is_numeric($lat) && is_numeric($lon) && is_numeric($timestamp) && is_numeric($userId) && is_numeric($trackId)) { $track = new uTrack($trackId); if ($track->isValid && $track->userId == $userId) { - $query = "INSERT INTO `" . self::db()->table('positions') . "` - (user_id, track_id, - time, latitude, longitude, altitude, speed, bearing, accuracy, provider, comment, image_id) - VALUES (?, ?, FROM_UNIXTIME(?), ?, ?, ?, ?, ?, ?, ?, ?, ?)"; - $stmt = self::db()->prepare($query); - $stmt->bind_param('iisddddddssi', - $userId, $trackId, - $timestamp, $lat, $lon, $altitude, $speed, $bearing, $accuracy, $provider, $comment, $imageId); - $stmt->execute(); - if (!self::db()->error && !$stmt->errno) { - $positionId = self::db()->insert_id; + try { + $table = self::db()->table('positions'); + $query = "INSERT INTO $table + (user_id, track_id, + time, latitude, longitude, altitude, speed, bearing, accuracy, provider, comment, image_id) + VALUES (?, ?, FROM_UNIXTIME(?), ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + $stmt = self::db()->prepare($query); + $params = [ $userId, $trackId, + $timestamp, $lat, $lon, $altitude, $speed, $bearing, $accuracy, $provider, $comment, $imageId ]; + $stmt->execute($params); + $positionId = self::db()->lastInsertId("${table}_id_seq"); + } catch (PDOException $e) { + // TODO: handle error } - $stmt->close(); } } return $positionId; @@ -129,21 +135,20 @@ if (!empty($userId)) { $args = []; $where = "WHERE user_id = ?"; - $args[0] = "i"; - $args[1] = &$userId; + $args[] = $userId; if (!empty($trackId)) { $where .= " AND track_id = ?"; - $args[0] .= "i"; - $args[2] = &$trackId; + $args[] = $trackId; } - $query = "DELETE FROM `" . self::db()->table('positions') . "` $where"; - $stmt = self::db()->prepare($query); - call_user_func_array([ $stmt, 'bind_param' ], $args); - $stmt->execute(); - if (!self::db()->error && !$stmt->errno) { + try { + $query = "DELETE FROM " . self::db()->table('positions') . " $where"; + $stmt = self::db()->prepare($query); + $stmt->execute($args); $ret = true; + } catch (PDOException $e) { + // TODO: handle exception +throw $e; } - $stmt->close(); } return $ret; } @@ -158,7 +163,7 @@ public static function getLast($userId = NULL) { if (!empty($userId)) { $where = "WHERE p.user_id = ?"; - $params = [ 'i', $userId ]; + $params = [ $userId ]; } else { $where = ""; $params = NULL; @@ -166,13 +171,18 @@ $query = "SELECT p.id, UNIX_TIMESTAMP(p.time) AS tstamp, p.user_id, p.track_id, p.latitude, p.longitude, p.altitude, p.speed, p.bearing, p.accuracy, p.provider, p.comment, p.image_id, u.login, t.name - FROM `" . self::db()->table('positions') . "` p - LEFT JOIN `" . self::db()->table('users') . "` u ON (p.user_id = u.id) - LEFT JOIN `" . self::db()->table('tracks') . "` t ON (p.track_id = t.id) + FROM " . self::db()->table('positions') . " p + LEFT JOIN " . self::db()->table('users') . " u ON (p.user_id = u.id) + LEFT JOIN " . self::db()->table('tracks') . " t ON (p.track_id = t.id) $where ORDER BY p.time DESC, p.id DESC LIMIT 1"; $position = new uPosition(); - $position->loadWithQuery($query, $params); + try { + $position->loadWithQuery($query, $params); + } catch (PDOException $e) { + // TODO: handle exception +throw $e; + } return $position; } @@ -186,10 +196,10 @@ public static function getAll($userId = NULL, $trackId = NULL) { $rules = []; if (!empty($userId)) { - $rules[] = "p.user_id = '" . self::db()->real_escape_string($userId) ."'"; + $rules[] = "p.user_id = " . self::db()->quote($userId); } if (!empty($trackId)) { - $rules[] = "p.track_id = '" . self::db()->real_escape_string($trackId) ."'"; + $rules[] = "p.track_id = " . self::db()->quote($trackId); } if (!empty($rules)) { $where = "WHERE " . implode(" AND ", $rules); @@ -199,20 +209,21 @@ $query = "SELECT p.id, UNIX_TIMESTAMP(p.time) AS tstamp, p.user_id, p.track_id, p.latitude, p.longitude, p.altitude, p.speed, p.bearing, p.accuracy, p.provider, p.comment, p.image_id, u.login, t.name - FROM `" . self::db()->table('positions') . "` p - LEFT JOIN `" . self::db()->table('users') . "` u ON (p.user_id = u.id) - LEFT JOIN `" . self::db()->table('tracks') . "` t ON (p.track_id = t.id) + FROM " . self::db()->table('positions') . " p + LEFT JOIN " . self::db()->table('users') . " u ON (p.user_id = u.id) + LEFT JOIN " . self::db()->table('tracks') . " t ON (p.track_id = t.id) $where ORDER BY p.time, p.id"; - $result = self::db()->query($query); - if ($result === false) { - return false; + try { + $positionsArr = []; + $result = self::db()->query($query); + while ($row = $result->fetch()) { + $positionsArr[] = self::rowToObject($row); + } + } catch (PDOException $e) { + // TODO: handle exception +throw $e; } - $positionsArr = []; - while ($row = $result->fetch_assoc()) { - $positionsArr[] = self::rowToObject($row); - } - $result->close(); return $positionsArr; } @@ -274,27 +285,32 @@ * Fill class properties with database query result * * @param string $query Query - * @param array|null $bindParams Optional array of bind parameters (types, params) + * @param array|null $params Optional array of bind parameters + * @throws PDOException */ - private function loadWithQuery($query, $bindParams = NULL) { + private function loadWithQuery($query, $params = NULL) { $stmt = self::db()->prepare($query); - if (is_array($bindParams)) { - $params = []; - foreach ($bindParams as &$value) { - $params[] =& $value; - } - call_user_func_array([ $stmt, 'bind_param' ], $params); - } - if ($stmt->execute()) { - $stmt->bind_result($this->id, $this->timestamp, $this->userId, $this->trackId, - $this->latitude, $this->longitude, $this->altitude, $this->speed, - $this->bearing, $this->accuracy, $this->provider, - $this->comment, $this->imageId, $this->userLogin, $this->trackName); - if ($stmt->fetch()) { - $this->isValid = true; - } - } - $stmt->close(); + $stmt->execute($params); + + $stmt->bindColumn('id', $this->id); + $stmt->bindColumn('tstamp', $this->timestamp); + $stmt->bindColumn('user_id', $this->userId); + $stmt->bindColumn('track_id', $this->trackId); + $stmt->bindColumn('latitude', $this->latitude); + $stmt->bindColumn('longitude', $this->longitude); + $stmt->bindColumn('altitude', $this->altitude); + $stmt->bindColumn('speed', $this->speed); + $stmt->bindColumn('bearing', $this->bearing); + $stmt->bindColumn('accuracy', $this->accuracy); + $stmt->bindColumn('provider', $this->provider); + $stmt->bindColumn('comment', $this->comment); + $stmt->bindColumn('image_id', $this->imageId); + $stmt->bindColumn('login', $this->userLogin); + $stmt->bindColumn('name', $this->trackName); + + $stmt->fetch(PDO::FETCH_BOUND); + $this->isValid = true; + $stmt = null; } } diff --git a/helpers/track.php b/helpers/track.php index 3cc4fdf..e0064c1 100644 --- a/helpers/track.php +++ b/helpers/track.php @@ -41,15 +41,20 @@ public function __construct($trackId = NULL) { if (!empty($trackId)) { - $query = "SELECT id, user_id, name, comment FROM `" . self::db()->table('tracks') . "` WHERE id = ? LIMIT 1"; - $stmt = self::db()->prepare($query); - $stmt->bind_param('i', $trackId); - $stmt->execute(); - $stmt->bind_result($this->id, $this->userId, $this->name, $this->comment); - if ($stmt->fetch()) { + try { + $query = "SELECT id, user_id, name, comment FROM " . self::db()->table('tracks') . " WHERE id = ? LIMIT 1"; + $stmt = self::db()->prepare($query); + $stmt->execute([$trackId]); + $stmt->bindColumn('id', $this->id); + $stmt->bindColumn('user_id', $this->userId); + $stmt->bindColumn('name', $this->name); + $stmt->bindColumn('comment', $this->comment); + $stmt->fetch(); $this->isValid = true; + } catch (PDOException $e) { + // TODO: handle exception +throw $e; } - $stmt->close(); } } @@ -77,14 +82,17 @@ public static function add($userId, $name, $comment = NULL) { $trackId = false; if (!empty($userId) && !empty($name)) { - $query = "INSERT INTO `" . self::db()->table('tracks') . "` (user_id, name, comment) VALUES (?, ?, ?)"; - $stmt = self::db()->prepare($query); - $stmt->bind_param('iss', $userId, $name, $comment); - $stmt->execute(); - if (!self::db()->error && !$stmt->errno) { - $trackId = self::db()->insert_id; + try { + $table = self::db()->table('tracks'); + $query = "INSERT INTO $table (user_id, name, comment) VALUES (?, ?, ?)"; + $stmt = self::db()->prepare($query); + $params = [ $userId, $name, $comment ]; + $stmt->execute($params); + $trackId = self::db()->lastInsertId("${table}_id_seq"); + } catch (PDOException $e) { + // TODO: handle exception +throw $e; } - $stmt->close(); } return $trackId; } @@ -125,19 +133,20 @@ return false; } // delete track metadata - $query = "DELETE FROM `" . self::db()->table('tracks') . "` WHERE id = ?"; - $stmt = self::db()->prepare($query); - $stmt->bind_param('i', $this->id); - $stmt->execute(); - if (!self::db()->error && !$stmt->errno) { + try { + $query = "DELETE FROM " . self::db()->table('tracks') . " WHERE id = ?"; + $stmt = self::db()->prepare($query); + $stmt->execute([ $this->id ]); $ret = true; $this->id = NULL; $this->userId = NULL; $this->name = NULL; $this->comment = NULL; $this->isValid = false; + } catch (PDOException $e) { + // TODO: handle exception +throw $e; } - $stmt->close(); } return $ret; } @@ -155,16 +164,18 @@ if (is_null($comment)) { $comment = $this->comment; } if ($comment == "") { $comment = NULL; } if ($this->isValid) { - $query = "UPDATE `" . self::db()->table('tracks') . "` SET name = ?, comment = ? WHERE id = ?"; - $stmt = self::db()->prepare($query); - $stmt->bind_param('ssi', $name, $comment, $this->id); - $stmt->execute(); - if (!self::db()->error && !$stmt->errno) { + try { + $query = "UPDATE " . self::db()->table('tracks') . " SET name = ?, comment = ? WHERE id = ?"; + $stmt = self::db()->prepare($query); + $params = [ $name, $comment, $this->id ]; + $stmt->execute($params); $ret = true; $this->name = $name; $this->comment = $comment; + } catch (PDOException $e) { + // TODO: handle exception +throw $e; } - $stmt->close(); } return $ret; } @@ -181,14 +192,15 @@ // remove all positions if (uPosition::deleteAll($userId) === true) { // remove all tracks - $query = "DELETE FROM `" . self::db()->table('tracks') . "` WHERE user_id = ?"; - $stmt = self::db()->prepare($query); - $stmt->bind_param('i', $userId); - $stmt->execute(); - if (!self::db()->error && !$stmt->errno) { + try { + $query = "DELETE FROM " . self::db()->table('tracks') . " WHERE user_id = ?"; + $stmt = self::db()->prepare($query); + $stmt->execute([ $userId ]); $ret = true; + } catch (PDOException $e) { + // TODO: handle exception +throw $e; } - $stmt->close(); } } @@ -203,20 +215,22 @@ */ public static function getAll($userId = NULL) { if (!empty($userId)) { - $where = "WHERE user_id='" . self::db()->real_escape_string($userId) ."'"; + $where = "WHERE user_id=" . self::db()->quote($userId); } else { $where = ""; } - $query = "SELECT id, user_id, name, comment FROM `" . self::db()->table('tracks') . "` $where ORDER BY id DESC"; - $result = self::db()->query($query); - if ($result === false) { - return false; + $query = "SELECT id, user_id, name, comment FROM " . self::db()->table('tracks') . " $where ORDER BY id DESC"; + try { + $result = self::db()->query($query); + $trackArr = []; + while ($row = $result->fetch()) { + $trackArr[] = self::rowToObject($row); + } + } catch (PDOException $e) { + // TODO: handle exception +throw $e; + $trackArr = false; } - $trackArr = []; - while ($row = $result->fetch_assoc()) { - $trackArr[] = self::rowToObject($row); - } - $result->close(); return $trackArr; } diff --git a/helpers/user.php b/helpers/user.php index e8cf1bc..bfce602 100644 --- a/helpers/user.php +++ b/helpers/user.php @@ -43,15 +43,19 @@ */ public function __construct($login = NULL) { if (!empty($login)) { - $sql = "SELECT id, login, password FROM `" . self::db()->table('users') . "` WHERE login = ? LIMIT 1"; - $stmt = self::db()->prepare($sql); - $stmt->bind_param('s', $login); - $stmt->execute(); - $stmt->bind_result($this->id, $this->login, $this->hash); - if ($stmt->fetch()) { + try { + $query = "SELECT id, login, password FROM " . self::db()->table('users') . " WHERE login = ? LIMIT 1"; + $stmt = self::db()->prepare($query); + $stmt->execute([ $login ]); + $stmt->bindColumn('id', $this->id); + $stmt->bindColumn('login', $this->login); + $stmt->bindColumn('password', $this->hash); + $stmt->fetch(); $this->isValid = true; + } catch (PDOException $e) { + // TODO: handle exception +throw $e; } - $stmt->close(); $this->isAdmin = self::isAdmin($this->login); } } @@ -79,14 +83,16 @@ $userid = false; if (!empty($login) && !empty($pass) && self::validPassStrength($pass)) { $hash = password_hash($pass, PASSWORD_DEFAULT); - $sql = "INSERT INTO `" . self::db()->table('users') . "` (login, password) VALUES (?, ?)"; - $stmt = self::db()->prepare($sql); - $stmt->bind_param('ss', $login, $hash); - $stmt->execute(); - if (!self::db()->error && !$stmt->errno) { - $userid = self::db()->insert_id; + $table = self::db()->table('users'); + try { + $query = "INSERT INTO $table (login, password) VALUES (?, ?)"; + $stmt = self::db()->prepare($query); + $stmt->execute([ $login, $hash ]); + $userid = self::db()->lastInsertId("${table}_id_seq"); + } catch (PDOException $e) { + // TODO: handle exception +throw $e; } - $stmt->close(); } return $userid; } @@ -105,19 +111,20 @@ return false; } // remove user - $sql = "DELETE FROM `" . self::db()->table('users') . "` WHERE id = ?"; - $stmt = self::db()->prepare($sql); - $stmt->bind_param('i', $this->id); - $stmt->execute(); - if (!self::db()->error && !$stmt->errno) { + try { + $query = "DELETE FROM " . self::db()->table('users') . " WHERE id = ?"; + $stmt = self::db()->prepare($query); + $stmt->execute([ $this->id ]); $ret = true; $this->id = NULL; $this->login = NULL; $this->hash = NULL; $this->isValid = false; $this->isAdmin = false; + } catch (PDOException $e) { + // TODO: handle exception +throw $e; } - $stmt->close(); } return $ret; } @@ -132,14 +139,15 @@ $ret = false; if (!empty($this->login) && !empty($pass) && self::validPassStrength($pass)) { $hash = password_hash($pass, PASSWORD_DEFAULT); - $sql = "UPDATE `" . self::db()->table('users') . "` SET password = ? WHERE login = ?"; - $stmt = self::db()->prepare($sql); - $stmt->bind_param('ss', $hash, $this->login); - $stmt->execute(); - if (!self::db()->error && !$stmt->errno) { + try { + $query = "UPDATE " . self::db()->table('users') . " SET password = ? WHERE login = ?"; + $stmt = self::db()->prepare($query); + $stmt->execute([ $hash, $this->login ]); $ret = true; + } catch (PDOException $e) { + // TODO: handle exception +throw $e; } - $stmt->close(); } return $ret; } @@ -193,16 +201,18 @@ * @return array|bool Array of uUser users, false on error */ public static function getAll() { - $query = "SELECT id, login, password FROM `" . self::db()->table('users') . "` ORDER BY login"; - $result = self::db()->query($query); - if ($result === false) { - return false; + try { + $query = "SELECT id, login, password FROM " . self::db()->table('users') . " ORDER BY login"; + $result = self::db()->query($query); + $userArr = []; + while ($row = $result->fetch()) { + $userArr[] = self::rowToObject($row); + } + } catch (PDOException $e) { + // TODO: handle exception +throw $e; + $userArr = false; } - $userArr = []; - while ($row = $result->fetch_assoc()) { - $userArr[] = self::rowToObject($row); - } - $result->close(); return $userArr; } diff --git a/scripts/setup.php b/scripts/setup.php index bcd1cb6..708d2c0 100644 --- a/scripts/setup.php +++ b/scripts/setup.php @@ -18,7 +18,7 @@ */ // This script is disabled by default. Change below to true before running. -$enabled = false; +$enabled = true; /* -------------------------------------------- */ @@ -43,70 +43,27 @@ $tUsers = $prefix . "users"; $messages = []; switch ($command) { case "setup": - $queries = []; - // positions - $queries[] = "DROP TABLE IF EXISTS `$tPositions`"; - $queries[] = "CREATE TABLE `$tPositions` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, - `user_id` int(11) NOT NULL, - `track_id` int(11) NOT NULL, - `latitude` double NOT NULL, - `longitude` double NOT NULL, - `altitude` double DEFAULT NULL, - `speed` double DEFAULT NULL, - `bearing` double DEFAULT NULL, - `accuracy` int(11) DEFAULT NULL, - `provider` varchar(100) DEFAULT NULL, - `comment` varchar(255) DEFAULT NULL, - `image_id` int(11) DEFAULT NULL, - PRIMARY KEY (`id`), - KEY `index_trip_id` (`track_id`), - KEY `index_user_id` (`user_id`) - ) ENGINE=InnoDB DEFAULT CHARSET=utf8"; - - // tracks - $queries[] = "DROP TABLE IF EXISTS `$tTracks`"; - $queries[] = "CREATE TABLE `$tTracks` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `user_id` int(11) NOT NULL, - `name` varchar(255) DEFAULT NULL, - `comment` varchar(1024) DEFAULT NULL, - PRIMARY KEY (`id`), - KEY `user_id` (`user_id`) - ) ENGINE=InnoDB DEFAULT CHARSET=utf8"; - - // users - $queries[] = "DROP TABLE IF EXISTS `$tUsers`"; - $queries[] = "CREATE TABLE `$tUsers` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `login` varchar(15) CHARACTER SET latin1 NOT NULL, - `password` varchar(255) CHARACTER SET latin1 NOT NULL DEFAULT '', - PRIMARY KEY (`id`), - UNIQUE KEY `login` (`login`) - ) ENGINE=InnoDB DEFAULT CHARSET=utf8"; $error = false; - mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); try { - $mysqli = new mysqli(uConfig::$dbhost, uConfig::$dbuser, uConfig::$dbpass, uConfig::$dbname); - } catch (mysqli_sql_exception $e ) { + $db = new PDO(uConfig::$dbdsn, uConfig::$dbuser, uConfig::$dbpass); + } catch (PDOException $e ) { $messages[] = "{$langSetup["dbconnectfailed"]}"; $messages[] = sprintf($langSetup["serversaid"], "" . $e->getMessage() . ""); $messages[] = $langSetup["checkdbsettings"]; break; } try { - $mysqli->set_charset('utf8'); + $queries = getQueries($db); foreach ($queries as $query) { - $mysqli->query($query); + $db->query($query); } - } catch (mysqli_sql_exception $e) { + } catch (PDOException $e) { $messages[] = "{$langSetup["dbqueryfailed"]}"; $messages[] = sprintf($langSetup["serversaid"], "" . $e->getMessage() . ""); $error = true; } - $mysqli->close(); + $db = null; if (!$error) { $messages[] = "{$langSetup["dbtablessuccess"]}"; $messages[] = $langSetup["setupuser"]; @@ -169,6 +126,146 @@ switch ($command) { break; } +function getQueries($db) { + $driver = $db->getAttribute(PDO::ATTR_DRIVER_NAME); + $queries = []; + switch($driver) { + case "mysql": + // positions + $queries[] = "DROP TABLE IF EXISTS `$tPositions`"; + $queries[] = "CREATE TABLE `$tPositions` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + `user_id` int(11) NOT NULL, + `track_id` int(11) NOT NULL, + `latitude` double NOT NULL, + `longitude` double NOT NULL, + `altitude` double DEFAULT NULL, + `speed` double DEFAULT NULL, + `bearing` double DEFAULT NULL, + `accuracy` int(11) DEFAULT NULL, + `provider` varchar(100) DEFAULT NULL, + `comment` varchar(255) DEFAULT NULL, + `image_id` int(11) DEFAULT NULL, + PRIMARY KEY (`id`), + KEY `index_trip_id` (`track_id`), + KEY `index_user_id` (`user_id`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8"; + + // tracks + $queries[] = "DROP TABLE IF EXISTS `$tTracks`"; + $queries[] = "CREATE TABLE `$tTracks` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `user_id` int(11) NOT NULL, + `name` varchar(255) DEFAULT NULL, + `comment` varchar(1024) DEFAULT NULL, + PRIMARY KEY (`id`), + KEY `user_id` (`user_id`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8"; + + // users + $queries[] = "DROP TABLE IF EXISTS `$tUsers`"; + $queries[] = "CREATE TABLE `$tUsers` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `login` varchar(15) CHARACTER SET latin1 NOT NULL, + `password` varchar(255) CHARACTER SET latin1 NOT NULL DEFAULT '', + PRIMARY KEY (`id`), + UNIQUE KEY `login` (`login`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8"; + break; + + case "pgsql": + // positions + $queries[] = "DROP TABLE IF EXISTS $tPositions"; + $queries[] = "CREATE TABLE $tPositions ( + id SERIAL PRIMARY KEY, + time TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP, + user_id INT NOT NULL, + track_id INT NOT NULL, + latitude DOUBLE PRECISION NOT NULL, + longitude DOUBLE PRECISION NOT NULL, + altitude DOUBLE PRECISION DEFAULT NULL, + speed DOUBLE PRECISION DEFAULT NULL, + bearing DOUBLE PRECISION DEFAULT NULL, + accuracy INT DEFAULT NULL, + provider VARCHAR(100) DEFAULT NULL, + comment VARCHAR(255) DEFAULT NULL, + image_id INT DEFAULT NULL + )"; + $queries[] = "CREATE INDEX index_trip_id ON $tPositions (track_id)"; + $queries[] = "CREATE INDEX index_user_id ON $tPositions (user_id)"; + + // tracks + $queries[] = "DROP TABLE IF EXISTS $tTracks"; + $queries[] = "CREATE TABLE $tTracks ( + id SERIAL PRIMARY KEY, + user_id INT NOT NULL, + name VARCHAR(255) DEFAULT NULL, + comment VARCHAR(1024) DEFAULT NULL + )"; + $queries[] = "CREATE INDEX user_id ON $tTracks (user_id)"; + + // users + $queries[] = "DROP TABLE IF EXISTS $tUsers"; + $queries[] = "CREATE TABLE $tUsers ( + id SERIAL PRIMARY KEY, + login varchar(15) NOT NULL UNIQUE, + password varchar(255) NOT NULL DEFAULT '' + )"; + break; + + case "sqlite": + + // positions + $queries[] = "DROP TABLE IF EXISTS `$tPositions`"; + $queries[] = "CREATE TABLE `$tPositions` ( + `id` INTEGER NOT NULL , + `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` TEXT DEFAULT NULL, + `comment` TEXT DEFAULT NULL, + `image_id` INTEGER DEFAULT NULL, + PRIMARY KEY (`id`) + )"; + $queries[] = "CREATE INDEX `positions_index_trip_id` ON `$tPositions` (`track_id`)"; + $queries[] = "CREATE INDEX `positions_index_user_id` ON `$tPositions` (`user_id`)"; + + // tracks + $queries[] = "DROP TABLE IF EXISTS `$tTracks`"; + $queries[] = "CREATE TABLE `$tTracks` ( + `id` INTEGER NOT NULL, + `user_id` INTEGER NOT NULL, + `name` TEXT DEFAULT NULL, + `comment` TEXT DEFAULT NULL, + PRIMARY KEY (`id`) + )"; + $queries[] = "CREATE INDEX `tracks_user_id` ON `$tTracks` (`user_id`)"; + + // users + $queries[] = "DROP TABLE IF EXISTS `$tUsers`"; + $queries[] = "CREATE TABLE `$tUsers` ( + `id` INTEGER NOT NULL , + `login` TEXT NOT NULL, + `password` TEXT NOT NULL DEFAULT '', + PRIMARY KEY (`id`) + )"; + $queries[] = "CREATE UNIQUE INDEX `users_login` ON `$tUsers` (`login`)"; + break; + + default: + throw InvalidArgumentException("Driver not supported"); + } + + +} + ?>