diff --git a/config.default.php b/config.default.php index 262fcb9..e3a80db 100755 --- a/config.default.php +++ b/config.default.php @@ -48,6 +48,7 @@ $dbhost = ""; // mysql host, eg. localhost $dbuser = ""; // database user $dbpass = ""; // database pass $dbname = ""; // database name +$dbprefix = ""; // optional table names prefix, eg. "ulogger_" // other // require login/password authentication diff --git a/helpers/config.php b/helpers/config.php index e30bb77..fec4073 100644 --- a/helpers/config.php +++ b/helpers/config.php @@ -49,6 +49,7 @@ static $dbuser = ""; // database user static $dbpass = ""; // database pass static $dbname = ""; // database name + static $dbprefix = ""; // optional table names prefix, eg. "ulogger_" // require login/password authentication static $require_authentication = true; @@ -112,6 +113,7 @@ 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; } if (isset($admin_user)) { self::$admin_user = $admin_user; } diff --git a/helpers/db.php b/helpers/db.php index e28b800..b0625fe 100644 --- a/helpers/db.php +++ b/helpers/db.php @@ -30,6 +30,13 @@ */ protected static $instance; + /** + * Table names + * + * @var array Array of names + */ + protected static $tables; + /** * Private constuctor * @@ -57,8 +64,23 @@ if (!self::$instance) { $config = new uConfig(); self::$instance = new self($config::$dbhost, $config::$dbuser, $config::$dbpass, $config::$dbname); + self::$tables = []; + $prefix = preg_replace('/[^a-z0-9_]/i', '', $config::$dbprefix); + self::$tables['positions'] = $prefix . "positions"; + self::$tables['tracks'] = $prefix . "tracks"; + self::$tables['users'] = $prefix . "users"; } return self::$instance; } + + /** + * Get full table name including prefix + * + * @param string $name Name + * @return string Full table name + */ + public function table($name) { + return self::$tables[$name]; + } } ?> \ No newline at end of file diff --git a/helpers/position.php b/helpers/position.php index 36ab153..6170fb3 100644 --- a/helpers/position.php +++ b/helpers/position.php @@ -55,7 +55,7 @@ $query = "SELECT p.id, p.time, 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 positions p + FROM `" . self::$db->table('positions') . "` p LEFT JOIN users u ON (p.user_id = u.id) LEFT JOIN tracks t ON (p.track_id = t.id) WHERE id = ? LIMIT 1"; @@ -84,7 +84,7 @@ public function add($userId, $trackId, $time, $lat, $lon, $altitude, $speed, $bearing, $accuracy, $provider, $comment, $imageId) { $positionId = false; if (!is_null($lat) && !is_null($lon) && !is_null($time) && !empty($userId) && !empty($trackId)) { - $query = "INSERT INTO positions + $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(?), ?, ?, ?, ?, ?, ?, ?, ?, ?)"; @@ -120,7 +120,7 @@ $args[0] .= "i"; $args[2] = &$trackId; } - $query = "DELETE FROM positions $where"; + $query = "DELETE FROM `" . self::$db->table('positions') . "` $where"; $stmt = self::$db->prepare($query); call_user_func_array([ $stmt, 'bind_param' ], $args); $stmt->execute(); @@ -150,7 +150,7 @@ $query = "SELECT p.id, p.time, 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 positions p + FROM `" . self::$db->table('positions') . "` p LEFT JOIN users u ON (p.user_id = u.id) LEFT JOIN tracks t ON (p.track_id = t.id) $where @@ -182,7 +182,7 @@ $query = "SELECT p.id, p.time, 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 positions p + FROM `" . self::$db->table('positions') . "` p LEFT JOIN users u ON (p.user_id = u.id) LEFT JOIN tracks t ON (p.track_id = t.id) $where diff --git a/helpers/track.php b/helpers/track.php index 0425bf9..36491d1 100644 --- a/helpers/track.php +++ b/helpers/track.php @@ -43,7 +43,8 @@ self::$db = uDb::getInstance(); if (!empty($trackId)) { - $stmt = self::$db->prepare("SELECT id, user_id, name, comment FROM tracks WHERE id = ? LIMIT 1"); + $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); @@ -66,7 +67,7 @@ public function add($userId, $name, $comment = NULL) { $trackId = false; if (!empty($userId) && !empty($name)) { - $query = "INSERT INTO tracks (user_id, name, comment) VALUES (?, ?, ?)"; + $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(); @@ -92,7 +93,7 @@ return false; } // delete track metadata - $query = "DELETE FROM tracks WHERE id = ?"; + $query = "DELETE FROM `" . self::$db->table('tracks') . "` WHERE id = ?"; $stmt = self::$db->prepare($query); $stmt->bind_param('i', $this->id); $stmt->execute(); @@ -122,7 +123,7 @@ if (is_null($comment)) { $comment = $this->comment; } if ($comment == "") { $comment = NULL; } if ($this->isValid) { - $query = "UPDATE tracks SET name = ?, comment = ? WHERE id = ?"; + $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(); @@ -145,7 +146,7 @@ public function deleteAll($userId) { $ret = false; if (!empty($userId)) { - $query = "DELETE FROM tracks WHERE user_id = ?"; + $query = "DELETE FROM `" . self::$db->table('tracks') . "` WHERE user_id = ?"; $stmt = self::$db->prepare($query); $stmt->bind_param('i', $userId); $stmt->execute(); @@ -169,7 +170,7 @@ } else { $where = ""; } - $query = "SELECT id, user_id, name, comment FROM tracks $where ORDER BY id DESC"; + $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; diff --git a/helpers/user.php b/helpers/user.php index 646a380..97e2395 100644 --- a/helpers/user.php +++ b/helpers/user.php @@ -44,7 +44,8 @@ public function __construct($login = NULL) { self::$db = uDb::getInstance(); if (!empty($login)) { - $stmt = self::$db->prepare("SELECT id, login, password FROM users WHERE login = ? LIMIT 1"); + $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); @@ -67,7 +68,7 @@ $userid = false; if (!empty($login) && !empty($pass) && $this->validPassStrength($pass)) { $hash = password_hash($pass, PASSWORD_DEFAULT); - $sql = "INSERT INTO users (login, password) VALUES (?, ?)"; + $sql = "INSERT INTO `" . self::$db->table('users') . "` (login, password) VALUES (?, ?)"; $stmt = self::$db->prepare($sql); $stmt->bind_param('ss', $login, $hash); $stmt->execute(); @@ -99,7 +100,7 @@ return false; } // remove user - $sql = "DELETE FROM users WHERE id = ?"; + $sql = "DELETE FROM `" . self::$db->table('users') . "` WHERE id = ?"; $stmt = self::$db->prepare($sql); $stmt->bind_param('i', $this->id); $stmt->execute(); @@ -126,7 +127,7 @@ $ret = false; if ($this->validPassStrength($pass)) { $hash = password_hash($pass, PASSWORD_DEFAULT); - $sql = "UPDATE users SET password = ? WHERE login = ?"; + $sql = "UPDATE `" . self::$db->table('users') . "` SET password = ? WHERE login = ?"; $stmt = self::$db->prepare($sql); $stmt->bind_param('ss', $hash, $this->login); $stmt->execute(); @@ -188,7 +189,7 @@ * @return array|bool Array of uUser users, false on error */ public function getAll() { - $query = "SELECT id, login, password FROM users ORDER BY login"; + $query = "SELECT id, login, password FROM `" . self::$db->table('users') . "` ORDER BY login"; $result = self::$db->query($query); if ($result === false) { return false; diff --git a/lang/en.php b/lang/en.php index 50e8a55..23e09b4 100644 --- a/lang/en.php +++ b/lang/en.php @@ -40,7 +40,7 @@ $langSetup["passfunchack"] = "Please edit 'helpers/user.php' file and uncomment $langSetup["dorestart"] = "Please restart this script when you are done."; $langSetup["createconfig"] = "Please create 'config.php' file in root folder. You may start by copying it from 'config.default.php'. Make sure that you adjust config values to match your needs and your database setup."; $langSetup["nodbsettings"] = "You must provide your database credentials in 'config.php' file (%s)."; // substitutes variable names -$langSetup["scriptdesc"] = "This script will set up tables needed for µlogger. They will be created in your database named %s. Warning, if the tables already exist they will be dropped and recreated, their content will be destroyed."; // substitutes db name +$langSetup["scriptdesc"] = "This script will set up tables needed for µlogger (%s). They will be created in your database named %s. Warning, if the tables already exist they will be dropped and recreated, their content will be destroyed."; // substitutes table names and db name $langSetup["scriptdesc2"] = "When done the script will ask you to provide user name and password for your µlogger user."; $langSetup["startbutton"] = "Press to start"; $langSetup["restartbutton"] = "Restart"; diff --git a/lang/pl.php b/lang/pl.php index a0922f8..ea77f32 100644 --- a/lang/pl.php +++ b/lang/pl.php @@ -37,7 +37,7 @@ $langSetup["passfunchack"] = "Otwórz proszę plik 'helpers/user.php' w edytorze $langSetup["dorestart"] = "Uruchom ten skrypt ponownie, kiedy zakończysz."; $langSetup["createconfig"] = "Utwórz proszę plik 'config.php' w głównym folderze. Możesz skopiować jego początkową zawartość z pliku 'config.default.php'. Pamiętaj, żeby dostosować konfiguracje do swoich potrzeb i ustawień bazy danych."; $langSetup["nodbsettings"] = "Musisz skonfigurować parametry dostępu do bazy danych w pliku 'config.php' (%s)."; -$langSetup["scriptdesc"] = "Ten skrypt utworzy tablice niezbędne do działania aplikacji µlogger. Zostaną one utworzone w bazie danych o nazwie %s. Uwaga, jeśli tablice już istnieją, zostaną usunięte i utworzone ponownie, ich zawartość zostanie skasowana."; +$langSetup["scriptdesc"] = "Ten skrypt utworzy tablice niezbędne do działania aplikacji µlogger (%s). Zostaną one utworzone w bazie danych o nazwie %s. Uwaga, jeśli tablice już istnieją, zostaną usunięte i utworzone ponownie, ich zawartość zostanie skasowana."; $langSetup["scriptdesc2"] = "Następnie skrypt poprosi o utworzenie konta do logowania w aplikacji µlogger."; $langSetup["startbutton"] = "Naciśnij, aby rozpocząć"; $langSetup["restartbutton"] = "Uruchom ponownie"; diff --git a/scripts/migrate_from_phptrackme.php b/scripts/migrate_from_phptrackme.php index e20388f..f23cd2d 100644 --- a/scripts/migrate_from_phptrackme.php +++ b/scripts/migrate_from_phptrackme.php @@ -80,6 +80,10 @@ if ($mysqli->connect_errno) { echo "Can't connect to $dbname database : (" . $mysqli->errno . ") " . $mysqli->error . "\n"; exit(1); } +$prefix = preg_replace('/[^a-z0-9_]/i', '', $dbprefix); +$tPositions = $prefix . "positions"; +$tTracks = $prefix . "tracks"; +$tUsers = $prefix . "users"; // import data if (!$users_result = $pt_mysqli->query("SELECT * FROM users ORDER BY ID")) { @@ -87,7 +91,7 @@ if (!$users_result = $pt_mysqli->query("SELECT * FROM users ORDER BY ID")) { exit(1); } -if (!($user_insert = $mysqli->prepare("INSERT INTO users (login, password) VALUES (?, ?)"))) { +if (!($user_insert = $mysqli->prepare("INSERT INTO `$tUsers` (login, password) VALUES (?, ?)"))) { echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error . "\n"; exit(1); } @@ -142,7 +146,7 @@ function process_user_tracks($user_id) { exit(1); } $tracks_select->store_result(); - if (!($track_insert = $mysqli->prepare("INSERT INTO tracks (user_id, name, comment) VALUES (?, ?, ?)"))) { + if (!($track_insert = $mysqli->prepare("INSERT INTO `$tTracks` (user_id, name, comment) VALUES (?, ?, ?)"))) { echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error . "\n"; exit(1); } @@ -190,7 +194,7 @@ function process_track($user_id, $old_id, $new_id) { exit(1); } $pos_select->store_result(); - if (!($pos_insert = $mysqli->prepare("INSERT INTO positions (time, user_id, track_id, latitude, longitude, altitude, speed, bearing, accuracy, provider, comment, image_id) + if (!($pos_insert = $mysqli->prepare("INSERT INTO `$tPositions` (time, user_id, track_id, latitude, longitude, altitude, speed, bearing, accuracy, provider, comment, image_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"))) { echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error . "\n"; exit(1); diff --git a/scripts/setup.php b/scripts/setup.php index bf1beed..a657c00 100644 --- a/scripts/setup.php +++ b/scripts/setup.php @@ -33,13 +33,18 @@ require_once(ROOT_DIR . "/lang.php"); $command = isset($_REQUEST['command']) ? $_REQUEST['command'] : NULL; +$prefix = preg_replace('/[^a-z0-9_]/i', '', $config::$dbprefix); +$tPositions = $prefix . "positions"; +$tTracks = $prefix . "tracks"; +$tUsers = $prefix . "users"; + $messages = []; switch ($command) { case "setup": $queries = []; // positions - $queries[] = "DROP TABLE IF EXISTS `positions`"; - $queries[] = "CREATE TABLE `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, @@ -59,8 +64,8 @@ switch ($command) { ) ENGINE=InnoDB DEFAULT CHARSET=utf8"; // tracks - $queries[] = "DROP TABLE IF EXISTS `tracks`"; - $queries[] = "CREATE TABLE `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, @@ -70,8 +75,8 @@ switch ($command) { ) ENGINE=InnoDB DEFAULT CHARSET=utf8"; // users - $queries[] = "DROP TABLE IF EXISTS `users`"; - $queries[] = "CREATE TABLE `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 '', @@ -160,7 +165,7 @@ switch ($command) { $messages[] = "
"; break; } - $messages[] = sprintf($langSetup["scriptdesc"], "{$config::$dbname}"); + $messages[] = sprintf($langSetup["scriptdesc"], "'$tPositions', '$tTracks', '$tUsers'", "{$config::$dbname}"); $messages[] = $langSetup["scriptdesc2"]; $messages[] = ""; break;