diff --git a/client/index.php b/client/index.php index cb0cc79..ca815de 100644 --- a/client/index.php +++ b/client/index.php @@ -45,8 +45,7 @@ switch ($action) { $login = isset($_REQUEST['login']) ? $_REQUEST['login'] : NULL; $pass = isset($_REQUEST['password']) ? $_REQUEST['password'] : NULL; if (!empty($login) && !empty($pass)) { - $newUser = new uUser(); - $newId = $newUser->add($login, $pass); + $newId = uUser::add($login, $pass); if ($newId !== false) { // return user id $response['userid'] = $newId; @@ -66,8 +65,7 @@ switch ($action) { break; } require_once(ROOT_DIR . "/helpers/track.php"); - $track = new uTrack(); - $trackId = $track->add($user->id, $trackName); + $trackId = uTrack::add($user->id, $trackName); if ($trackId === false) { setError($response, "Server error"); break; @@ -96,8 +94,7 @@ switch ($action) { } require_once(ROOT_DIR . "/helpers/position.php"); - $position = new uPosition(); - $positionId = $position->add($user->id, $trackId, + $positionId = uPosition::add($user->id, $trackId, $timestamp, $lat, $lon, $altitude, $speed, $bearing, $accuracy, $provider, $comment, $imageId); if ($positionId === false) { diff --git a/helpers/position.php b/helpers/position.php index 9f4009d..7fc7358 100644 --- a/helpers/position.php +++ b/helpers/position.php @@ -50,21 +50,31 @@ */ public function __construct($positionId = NULL) { - self::$db = uDb::getInstance(); - if (!empty($positionId)) { $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); } } + /** + * Get db instance + * + * @return uDb instance + */ + private static function db() { + if (is_null(self::$db)) { + self::$db = uDb::getInstance(); + } + return self::$db; + } + /** * Add position * @@ -82,22 +92,22 @@ * @param int $imageId * @return int|bool New position id in database, false on error */ - public function add($userId, $trackId, $timestamp, $lat, $lon, $altitude, $speed, $bearing, $accuracy, $provider, $comment, $imageId) { + public static function add($userId, $trackId, $timestamp, $lat, $lon, $altitude, $speed, $bearing, $accuracy, $provider, $comment, $imageId) { $positionId = false; if (!is_null($lat) && !is_null($lon) && !is_null($timestamp) && !empty($userId) && !empty($trackId)) { $track = new uTrack($trackId); if ($track->isValid && $track->userId == $userId) { - $query = "INSERT INTO `" . self::$db->table('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(?), ?, ?, ?, ?, ?, ?, ?, ?, ?)"; - $stmt = self::$db->prepare($query); + $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; + if (!self::db()->error && !$stmt->errno) { + $positionId = self::db()->insert_id; } $stmt->close(); } @@ -112,7 +122,7 @@ * @param int $trackId Optional track id * @return bool True if success, false otherwise */ - public function deleteAll($userId, $trackId = NULL) { + public static function deleteAll($userId, $trackId = NULL) { $ret = false; if (!empty($userId)) { $args = []; @@ -124,11 +134,11 @@ $args[0] .= "i"; $args[2] = &$trackId; } - $query = "DELETE FROM `" . self::$db->table('positions') . "` $where"; - $stmt = self::$db->prepare($query); + $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) { + if (!self::db()->error && !$stmt->errno) { $ret = true; } $stmt->close(); @@ -137,13 +147,13 @@ } /** - * Fill class properties with last position data from database + * Get last position data from database * (for given user if specified) * * @param int $userId Optional user id - * @return uPosition Self + * @return uPosition Position */ - public function getLast($userId = NULL) { + public static function getLast($userId = NULL) { if (!empty($userId)) { $where = "WHERE p.user_id = ?"; $params = [ 'i', $userId ]; @@ -154,13 +164,14 @@ $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"; - $this->loadWithQuery($query, $params); - return $this; + $position = new uPosition(); + $position->loadWithQuery($query, $params); + return $position; } /** @@ -170,13 +181,13 @@ * @param int $trackId Optional limit to given track id * @return array|bool Array of uPosition positions, false on error */ - public function getAll($userId = NULL, $trackId = NULL) { + 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()->real_escape_string($userId) ."'"; } if (!empty($trackId)) { - $rules[] = "p.track_id = '" . self::$db->real_escape_string($trackId) ."'"; + $rules[] = "p.track_id = '" . self::db()->real_escape_string($trackId) ."'"; } if (!empty($rules)) { $where = "WHERE " . implode(" AND ", $rules); @@ -186,18 +197,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, p.id"; - $result = self::$db->query($query); + $result = self::db()->query($query); if ($result === false) { return false; } $positionsArr = []; while ($row = $result->fetch_assoc()) { - $positionsArr[] = $this->rowToObject($row); + $positionsArr[] = self::rowToObject($row); } $result->close(); return $positionsArr; @@ -236,7 +247,7 @@ * @param array $row Row * @return uPosition Position */ - private function rowToObject($row) { + private static function rowToObject($row) { $position = new uPosition(); $position->id = $row['id']; $position->timestamp = $row['tstamp']; @@ -264,7 +275,7 @@ * @param array|null $bindParams Optional array of bind parameters (types, params) */ private function loadWithQuery($query, $bindParams = NULL) { - $stmt = self::$db->prepare($query); + $stmt = self::db()->prepare($query); if (is_array($bindParams)) { $params = []; foreach ($bindParams as &$value) { diff --git a/helpers/track.php b/helpers/track.php index 36491d1..66c8f3a 100644 --- a/helpers/track.php +++ b/helpers/track.php @@ -31,7 +31,7 @@ public $isValid = false; - private static $db; + private static $db = null; /** * Constructor @@ -40,11 +40,9 @@ */ public function __construct($trackId = NULL) { - self::$db = uDb::getInstance(); - if (!empty($trackId)) { - $query = "SELECT id, user_id, name, comment FROM `" . self::$db->table('tracks') . "` WHERE id = ? LIMIT 1"; - $stmt = self::$db->prepare($query); + $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); @@ -56,6 +54,18 @@ } } + /** + * Get db instance + * + * @return uDb instance + */ + private static function db() { + if (is_null(self::$db)) { + self::$db = uDb::getInstance(); + } + return self::$db; + } + /** * Add new track * @@ -64,15 +74,15 @@ * @param string $comment Optional comment * @return int|bool New track id, false on error */ - public function add($userId, $name, $comment = NULL) { + 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); + $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; + if (!self::db()->error && !$stmt->errno) { + $trackId = self::db()->insert_id; } $stmt->close(); } @@ -88,16 +98,15 @@ $ret = false; if ($this->isValid) { // delete positions - $position = new uPosition(); - if ($position->deleteAll($this->userId, $this->id) === false) { + if (uPosition::deleteAll($this->userId, $this->id) === false) { return false; } // delete track metadata - $query = "DELETE FROM `" . self::$db->table('tracks') . "` WHERE id = ?"; - $stmt = self::$db->prepare($query); + $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) { + if (!self::db()->error && !$stmt->errno) { $ret = true; $this->id = NULL; $this->userId = NULL; @@ -123,11 +132,11 @@ 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); + $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) { + if (!self::db()->error && !$stmt->errno) { $ret = true; $this->name = $name; $this->comment = $comment; @@ -143,14 +152,14 @@ * @param string $userId User id * @return bool True if success, false otherwise */ - public function deleteAll($userId) { + public static function deleteAll($userId) { $ret = false; if (!empty($userId)) { - $query = "DELETE FROM `" . self::$db->table('tracks') . "` WHERE user_id = ?"; - $stmt = self::$db->prepare($query); + $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) { + if (!self::db()->error && !$stmt->errno) { $ret = true; } $stmt->close(); @@ -164,20 +173,20 @@ * @param int $userId Optional limit to user id * @return array|bool Array of uTrack tracks, false on error */ - public function getAll($userId = NULL) { + 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()->real_escape_string($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); + $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; } $trackArr = []; while ($row = $result->fetch_assoc()) { - $trackArr[] = $this->rowToObject($row); + $trackArr[] = self::rowToObject($row); } $result->close(); return $trackArr; @@ -189,7 +198,7 @@ * @param array $row Row * @return uTrack Track */ - private function rowToObject($row) { + private static function rowToObject($row) { $track = new uTrack(); $track->id = $row['id']; $track->userId = $row['user_id']; diff --git a/helpers/user.php b/helpers/user.php index 752cdd3..65fa0dd 100644 --- a/helpers/user.php +++ b/helpers/user.php @@ -34,7 +34,7 @@ public $isAdmin = false; public $isValid = false; - private static $db; + private static $db = null; /** * Constructor @@ -42,10 +42,9 @@ * @param string $login Login */ public function __construct($login = NULL) { - self::$db = uDb::getInstance(); if (!empty($login)) { - $sql = "SELECT id, login, password FROM `" . self::$db->table('users') . "` WHERE login = ? LIMIT 1"; - $stmt = self::$db->prepare($sql); + $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); @@ -53,10 +52,22 @@ $this->isValid = true; } $stmt->close(); - $this->isAdmin = $this->isAdmin($this->login); + $this->isAdmin = self::isAdmin($this->login); } } + /** + * Get db instance + * + * @return uDb instance + */ + private static function db() { + if (is_null(self::$db)) { + self::$db = uDb::getInstance(); + } + return self::$db; + } + /** * Add new user * @@ -64,16 +75,16 @@ * @param string $pass Password * @return int|bool New user id, false on error */ - public function add($login, $pass) { + public static function add($login, $pass) { $userid = false; - if (!empty($login) && !empty($pass) && $this->validPassStrength($pass)) { + 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); + $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; + if (!self::db()->error && !$stmt->errno) { + $userid = self::db()->insert_id; } $stmt->close(); } @@ -90,21 +101,19 @@ $ret = false; if ($this->isValid) { // remove positions - $position = new uPosition(); - if ($position->deleteAll($this->id) === false) { + if (uPosition::deleteAll($this->id) === false) { return false; } // remove tracks - $track = new uTrack(); - if ($track->deleteAll($this->id) === false) { + if (uTrack::deleteAll($this->id) === false) { return false; } // remove user - $sql = "DELETE FROM `" . self::$db->table('users') . "` WHERE id = ?"; - $stmt = self::$db->prepare($sql); + $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) { + if (!self::db()->error && !$stmt->errno) { $ret = true; $this->id = NULL; $this->login = NULL; @@ -125,13 +134,13 @@ */ public function setPass($pass) { $ret = false; - if (!empty($this->login) && !empty($pass) && $this->validPassStrength($pass)) { + 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); + $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) { + if (!self::db()->error && !$stmt->errno) { $ret = true; } $stmt->close(); @@ -155,7 +164,7 @@ * @param String $password Password * @return bool True if matches, false otherwise */ - private function validPassStrength($password) { + private static function validPassStrength($password) { return preg_match(uConfig::passRegex(), $password); } @@ -187,15 +196,15 @@ * * @return array|bool Array of uUser users, false on error */ - public function getAll() { - $query = "SELECT id, login, password FROM `" . self::$db->table('users') . "` ORDER BY login"; - $result = self::$db->query($query); + 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; } $userArr = []; while ($row = $result->fetch_assoc()) { - $userArr[] = $this->rowToObject($row); + $userArr[] = self::rowToObject($row); } $result->close(); return $userArr; @@ -207,12 +216,12 @@ * @param array $row Row * @return uUser User */ - private function rowToObject($row) { + private static function rowToObject($row) { $user = new uUser(); $user->id = $row['id']; $user->login = $row['login']; $user->hash = $row['password']; - $user->isAdmin = $this->isAdmin($row['login']); + $user->isAdmin = self::isAdmin($row['login']); $user->isValid = true; return $user; } @@ -223,7 +232,7 @@ * @param string $login Login * @return bool True if admin, false otherwise */ - private function isAdmin($login) { + private static function isAdmin($login) { return (!empty(uConfig::$admin_user) && uConfig::$admin_user == $login); } } diff --git a/index.php b/index.php index 64037b0..fbd70c8 100755 --- a/index.php +++ b/index.php @@ -27,21 +27,19 @@ if ($user->isAdmin || uConfig::$public_tracks) { // public access or admin user // get last position user - $lastPosition = new uPosition(); - $lastPosition->getLast(); + $lastPosition = uPosition::getLast(); if ($lastPosition->isValid) { // display track of last position user $displayUserId = $lastPosition->userId; } // populate users array (for