Convert methods to static where possible

This commit is contained in:
Bartek Fabiszewski 2017-08-17 15:38:58 +02:00
parent 838dbe4dc4
commit 65a5c95f67
11 changed files with 134 additions and 116 deletions

View File

@ -45,8 +45,7 @@ switch ($action) {
$login = isset($_REQUEST['login']) ? $_REQUEST['login'] : NULL; $login = isset($_REQUEST['login']) ? $_REQUEST['login'] : NULL;
$pass = isset($_REQUEST['password']) ? $_REQUEST['password'] : NULL; $pass = isset($_REQUEST['password']) ? $_REQUEST['password'] : NULL;
if (!empty($login) && !empty($pass)) { if (!empty($login) && !empty($pass)) {
$newUser = new uUser(); $newId = uUser::add($login, $pass);
$newId = $newUser->add($login, $pass);
if ($newId !== false) { if ($newId !== false) {
// return user id // return user id
$response['userid'] = $newId; $response['userid'] = $newId;
@ -66,8 +65,7 @@ switch ($action) {
break; break;
} }
require_once(ROOT_DIR . "/helpers/track.php"); require_once(ROOT_DIR . "/helpers/track.php");
$track = new uTrack(); $trackId = uTrack::add($user->id, $trackName);
$trackId = $track->add($user->id, $trackName);
if ($trackId === false) { if ($trackId === false) {
setError($response, "Server error"); setError($response, "Server error");
break; break;
@ -96,8 +94,7 @@ switch ($action) {
} }
require_once(ROOT_DIR . "/helpers/position.php"); require_once(ROOT_DIR . "/helpers/position.php");
$position = new uPosition(); $positionId = uPosition::add($user->id, $trackId,
$positionId = $position->add($user->id, $trackId,
$timestamp, $lat, $lon, $altitude, $speed, $bearing, $accuracy, $provider, $comment, $imageId); $timestamp, $lat, $lon, $altitude, $speed, $bearing, $accuracy, $provider, $comment, $imageId);
if ($positionId === false) { if ($positionId === false) {

View File

@ -50,21 +50,31 @@
*/ */
public function __construct($positionId = NULL) { public function __construct($positionId = NULL) {
self::$db = uDb::getInstance();
if (!empty($positionId)) { if (!empty($positionId)) {
$query = "SELECT p.id, UNIX_TIMESTAMP(p.time) AS tstamp, p.user_id, p.track_id, $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.latitude, p.longitude, p.altitude, p.speed, p.bearing, p.accuracy, p.provider,
p.comment, p.image_id, u.login, t.name p.comment, p.image_id, u.login, t.name
FROM `" . self::$db->table('positions') . "` p FROM `" . self::db()->table('positions') . "` p
LEFT JOIN `" . self::$db->table('users') . "` u ON (p.user_id = u.id) 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) LEFT JOIN `" . self::db()->table('tracks') . "` t ON (p.track_id = t.id)
WHERE id = ? LIMIT 1"; WHERE id = ? LIMIT 1";
$params = [ 'i', $positionId ]; $params = [ 'i', $positionId ];
$this->loadWithQuery($query, $params); $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 * Add position
* *
@ -82,22 +92,22 @@
* @param int $imageId * @param int $imageId
* @return int|bool New position id in database, false on error * @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; $positionId = false;
if (!is_null($lat) && !is_null($lon) && !is_null($timestamp) && !empty($userId) && !empty($trackId)) { if (!is_null($lat) && !is_null($lon) && !is_null($timestamp) && !empty($userId) && !empty($trackId)) {
$track = new uTrack($trackId); $track = new uTrack($trackId);
if ($track->isValid && $track->userId == $userId) { if ($track->isValid && $track->userId == $userId) {
$query = "INSERT INTO `" . self::$db->table('positions') . "` $query = "INSERT INTO `" . self::db()->table('positions') . "`
(user_id, track_id, (user_id, track_id,
time, latitude, longitude, altitude, speed, bearing, accuracy, provider, comment, image_id) time, latitude, longitude, altitude, speed, bearing, accuracy, provider, comment, image_id)
VALUES (?, ?, FROM_UNIXTIME(?), ?, ?, ?, ?, ?, ?, ?, ?, ?)"; VALUES (?, ?, FROM_UNIXTIME(?), ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = self::$db->prepare($query); $stmt = self::db()->prepare($query);
$stmt->bind_param('iisddddddssi', $stmt->bind_param('iisddddddssi',
$userId, $trackId, $userId, $trackId,
$timestamp, $lat, $lon, $altitude, $speed, $bearing, $accuracy, $provider, $comment, $imageId); $timestamp, $lat, $lon, $altitude, $speed, $bearing, $accuracy, $provider, $comment, $imageId);
$stmt->execute(); $stmt->execute();
if (!self::$db->error && !$stmt->errno) { if (!self::db()->error && !$stmt->errno) {
$positionId = self::$db->insert_id; $positionId = self::db()->insert_id;
} }
$stmt->close(); $stmt->close();
} }
@ -112,7 +122,7 @@
* @param int $trackId Optional track id * @param int $trackId Optional track id
* @return bool True if success, false otherwise * @return bool True if success, false otherwise
*/ */
public function deleteAll($userId, $trackId = NULL) { public static function deleteAll($userId, $trackId = NULL) {
$ret = false; $ret = false;
if (!empty($userId)) { if (!empty($userId)) {
$args = []; $args = [];
@ -124,11 +134,11 @@
$args[0] .= "i"; $args[0] .= "i";
$args[2] = &$trackId; $args[2] = &$trackId;
} }
$query = "DELETE FROM `" . self::$db->table('positions') . "` $where"; $query = "DELETE FROM `" . self::db()->table('positions') . "` $where";
$stmt = self::$db->prepare($query); $stmt = self::db()->prepare($query);
call_user_func_array([ $stmt, 'bind_param' ], $args); call_user_func_array([ $stmt, 'bind_param' ], $args);
$stmt->execute(); $stmt->execute();
if (!self::$db->error && !$stmt->errno) { if (!self::db()->error && !$stmt->errno) {
$ret = true; $ret = true;
} }
$stmt->close(); $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) * (for given user if specified)
* *
* @param int $userId Optional user id * @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)) { if (!empty($userId)) {
$where = "WHERE p.user_id = ?"; $where = "WHERE p.user_id = ?";
$params = [ 'i', $userId ]; $params = [ 'i', $userId ];
@ -154,13 +164,14 @@
$query = "SELECT p.id, UNIX_TIMESTAMP(p.time) AS tstamp, p.user_id, p.track_id, $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.latitude, p.longitude, p.altitude, p.speed, p.bearing, p.accuracy, p.provider,
p.comment, p.image_id, u.login, t.name p.comment, p.image_id, u.login, t.name
FROM `" . self::$db->table('positions') . "` p FROM `" . self::db()->table('positions') . "` p
LEFT JOIN `" . self::$db->table('users') . "` u ON (p.user_id = u.id) 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) LEFT JOIN `" . self::db()->table('tracks') . "` t ON (p.track_id = t.id)
$where $where
ORDER BY p.time DESC, p.id DESC LIMIT 1"; ORDER BY p.time DESC, p.id DESC LIMIT 1";
$this->loadWithQuery($query, $params); $position = new uPosition();
return $this; $position->loadWithQuery($query, $params);
return $position;
} }
/** /**
@ -170,13 +181,13 @@
* @param int $trackId Optional limit to given track id * @param int $trackId Optional limit to given track id
* @return array|bool Array of uPosition positions, false on error * @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 = []; $rules = [];
if (!empty($userId)) { 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)) { 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)) { if (!empty($rules)) {
$where = "WHERE " . implode(" AND ", $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, $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.latitude, p.longitude, p.altitude, p.speed, p.bearing, p.accuracy, p.provider,
p.comment, p.image_id, u.login, t.name p.comment, p.image_id, u.login, t.name
FROM `" . self::$db->table('positions') . "` p FROM `" . self::db()->table('positions') . "` p
LEFT JOIN `" . self::$db->table('users') . "` u ON (p.user_id = u.id) 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) LEFT JOIN `" . self::db()->table('tracks') . "` t ON (p.track_id = t.id)
$where $where
ORDER BY p.time, p.id"; ORDER BY p.time, p.id";
$result = self::$db->query($query); $result = self::db()->query($query);
if ($result === false) { if ($result === false) {
return false; return false;
} }
$positionsArr = []; $positionsArr = [];
while ($row = $result->fetch_assoc()) { while ($row = $result->fetch_assoc()) {
$positionsArr[] = $this->rowToObject($row); $positionsArr[] = self::rowToObject($row);
} }
$result->close(); $result->close();
return $positionsArr; return $positionsArr;
@ -236,7 +247,7 @@
* @param array $row Row * @param array $row Row
* @return uPosition Position * @return uPosition Position
*/ */
private function rowToObject($row) { private static function rowToObject($row) {
$position = new uPosition(); $position = new uPosition();
$position->id = $row['id']; $position->id = $row['id'];
$position->timestamp = $row['tstamp']; $position->timestamp = $row['tstamp'];
@ -264,7 +275,7 @@
* @param array|null $bindParams Optional array of bind parameters (types, params) * @param array|null $bindParams Optional array of bind parameters (types, params)
*/ */
private function loadWithQuery($query, $bindParams = NULL) { private function loadWithQuery($query, $bindParams = NULL) {
$stmt = self::$db->prepare($query); $stmt = self::db()->prepare($query);
if (is_array($bindParams)) { if (is_array($bindParams)) {
$params = []; $params = [];
foreach ($bindParams as &$value) { foreach ($bindParams as &$value) {

View File

@ -31,7 +31,7 @@
public $isValid = false; public $isValid = false;
private static $db; private static $db = null;
/** /**
* Constructor * Constructor
@ -40,11 +40,9 @@
*/ */
public function __construct($trackId = NULL) { public function __construct($trackId = NULL) {
self::$db = uDb::getInstance();
if (!empty($trackId)) { if (!empty($trackId)) {
$query = "SELECT id, user_id, name, comment FROM `" . self::$db->table('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 = self::db()->prepare($query);
$stmt->bind_param('i', $trackId); $stmt->bind_param('i', $trackId);
$stmt->execute(); $stmt->execute();
$stmt->bind_result($this->id, $this->userId, $this->name, $this->comment); $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 * Add new track
* *
@ -64,15 +74,15 @@
* @param string $comment Optional comment * @param string $comment Optional comment
* @return int|bool New track id, false on error * @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; $trackId = false;
if (!empty($userId) && !empty($name)) { if (!empty($userId) && !empty($name)) {
$query = "INSERT INTO `" . self::$db->table('tracks') . "` (user_id, name, comment) VALUES (?, ?, ?)"; $query = "INSERT INTO `" . self::db()->table('tracks') . "` (user_id, name, comment) VALUES (?, ?, ?)";
$stmt = self::$db->prepare($query); $stmt = self::db()->prepare($query);
$stmt->bind_param('iss', $userId, $name, $comment); $stmt->bind_param('iss', $userId, $name, $comment);
$stmt->execute(); $stmt->execute();
if (!self::$db->error && !$stmt->errno) { if (!self::db()->error && !$stmt->errno) {
$trackId = self::$db->insert_id; $trackId = self::db()->insert_id;
} }
$stmt->close(); $stmt->close();
} }
@ -88,16 +98,15 @@
$ret = false; $ret = false;
if ($this->isValid) { if ($this->isValid) {
// delete positions // delete positions
$position = new uPosition(); if (uPosition::deleteAll($this->userId, $this->id) === false) {
if ($position->deleteAll($this->userId, $this->id) === false) {
return false; return false;
} }
// delete track metadata // delete track metadata
$query = "DELETE FROM `" . self::$db->table('tracks') . "` WHERE id = ?"; $query = "DELETE FROM `" . self::db()->table('tracks') . "` WHERE id = ?";
$stmt = self::$db->prepare($query); $stmt = self::db()->prepare($query);
$stmt->bind_param('i', $this->id); $stmt->bind_param('i', $this->id);
$stmt->execute(); $stmt->execute();
if (!self::$db->error && !$stmt->errno) { if (!self::db()->error && !$stmt->errno) {
$ret = true; $ret = true;
$this->id = NULL; $this->id = NULL;
$this->userId = NULL; $this->userId = NULL;
@ -123,11 +132,11 @@
if (is_null($comment)) { $comment = $this->comment; } if (is_null($comment)) { $comment = $this->comment; }
if ($comment == "") { $comment = NULL; } if ($comment == "") { $comment = NULL; }
if ($this->isValid) { if ($this->isValid) {
$query = "UPDATE `" . self::$db->table('tracks') . "` SET name = ?, comment = ? WHERE id = ?"; $query = "UPDATE `" . self::db()->table('tracks') . "` SET name = ?, comment = ? WHERE id = ?";
$stmt = self::$db->prepare($query); $stmt = self::db()->prepare($query);
$stmt->bind_param('ssi', $name, $comment, $this->id); $stmt->bind_param('ssi', $name, $comment, $this->id);
$stmt->execute(); $stmt->execute();
if (!self::$db->error && !$stmt->errno) { if (!self::db()->error && !$stmt->errno) {
$ret = true; $ret = true;
$this->name = $name; $this->name = $name;
$this->comment = $comment; $this->comment = $comment;
@ -143,14 +152,14 @@
* @param string $userId User id * @param string $userId User id
* @return bool True if success, false otherwise * @return bool True if success, false otherwise
*/ */
public function deleteAll($userId) { public static function deleteAll($userId) {
$ret = false; $ret = false;
if (!empty($userId)) { if (!empty($userId)) {
$query = "DELETE FROM `" . self::$db->table('tracks') . "` WHERE user_id = ?"; $query = "DELETE FROM `" . self::db()->table('tracks') . "` WHERE user_id = ?";
$stmt = self::$db->prepare($query); $stmt = self::db()->prepare($query);
$stmt->bind_param('i', $userId); $stmt->bind_param('i', $userId);
$stmt->execute(); $stmt->execute();
if (!self::$db->error && !$stmt->errno) { if (!self::db()->error && !$stmt->errno) {
$ret = true; $ret = true;
} }
$stmt->close(); $stmt->close();
@ -164,20 +173,20 @@
* @param int $userId Optional limit to user id * @param int $userId Optional limit to user id
* @return array|bool Array of uTrack tracks, false on error * @return array|bool Array of uTrack tracks, false on error
*/ */
public function getAll($userId = NULL) { public static function getAll($userId = NULL) {
if (!empty($userId)) { if (!empty($userId)) {
$where = "WHERE user_id='" . self::$db->real_escape_string($userId) ."'"; $where = "WHERE user_id='" . self::db()->real_escape_string($userId) ."'";
} else { } else {
$where = ""; $where = "";
} }
$query = "SELECT id, user_id, name, comment FROM `" . self::$db->table('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); $result = self::db()->query($query);
if ($result === false) { if ($result === false) {
return false; return false;
} }
$trackArr = []; $trackArr = [];
while ($row = $result->fetch_assoc()) { while ($row = $result->fetch_assoc()) {
$trackArr[] = $this->rowToObject($row); $trackArr[] = self::rowToObject($row);
} }
$result->close(); $result->close();
return $trackArr; return $trackArr;
@ -189,7 +198,7 @@
* @param array $row Row * @param array $row Row
* @return uTrack Track * @return uTrack Track
*/ */
private function rowToObject($row) { private static function rowToObject($row) {
$track = new uTrack(); $track = new uTrack();
$track->id = $row['id']; $track->id = $row['id'];
$track->userId = $row['user_id']; $track->userId = $row['user_id'];

View File

@ -34,7 +34,7 @@
public $isAdmin = false; public $isAdmin = false;
public $isValid = false; public $isValid = false;
private static $db; private static $db = null;
/** /**
* Constructor * Constructor
@ -42,10 +42,9 @@
* @param string $login Login * @param string $login Login
*/ */
public function __construct($login = NULL) { public function __construct($login = NULL) {
self::$db = uDb::getInstance();
if (!empty($login)) { if (!empty($login)) {
$sql = "SELECT id, login, password FROM `" . self::$db->table('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 = self::db()->prepare($sql);
$stmt->bind_param('s', $login); $stmt->bind_param('s', $login);
$stmt->execute(); $stmt->execute();
$stmt->bind_result($this->id, $this->login, $this->hash); $stmt->bind_result($this->id, $this->login, $this->hash);
@ -53,10 +52,22 @@
$this->isValid = true; $this->isValid = true;
} }
$stmt->close(); $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 * Add new user
* *
@ -64,16 +75,16 @@
* @param string $pass Password * @param string $pass Password
* @return int|bool New user id, false on error * @return int|bool New user id, false on error
*/ */
public function add($login, $pass) { public static function add($login, $pass) {
$userid = false; $userid = false;
if (!empty($login) && !empty($pass) && $this->validPassStrength($pass)) { if (!empty($login) && !empty($pass) && self::validPassStrength($pass)) {
$hash = password_hash($pass, PASSWORD_DEFAULT); $hash = password_hash($pass, PASSWORD_DEFAULT);
$sql = "INSERT INTO `" . self::$db->table('users') . "` (login, password) VALUES (?, ?)"; $sql = "INSERT INTO `" . self::db()->table('users') . "` (login, password) VALUES (?, ?)";
$stmt = self::$db->prepare($sql); $stmt = self::db()->prepare($sql);
$stmt->bind_param('ss', $login, $hash); $stmt->bind_param('ss', $login, $hash);
$stmt->execute(); $stmt->execute();
if (!self::$db->error && !$stmt->errno) { if (!self::db()->error && !$stmt->errno) {
$userid = self::$db->insert_id; $userid = self::db()->insert_id;
} }
$stmt->close(); $stmt->close();
} }
@ -90,21 +101,19 @@
$ret = false; $ret = false;
if ($this->isValid) { if ($this->isValid) {
// remove positions // remove positions
$position = new uPosition(); if (uPosition::deleteAll($this->id) === false) {
if ($position->deleteAll($this->id) === false) {
return false; return false;
} }
// remove tracks // remove tracks
$track = new uTrack(); if (uTrack::deleteAll($this->id) === false) {
if ($track->deleteAll($this->id) === false) {
return false; return false;
} }
// remove user // remove user
$sql = "DELETE FROM `" . self::$db->table('users') . "` WHERE id = ?"; $sql = "DELETE FROM `" . self::db()->table('users') . "` WHERE id = ?";
$stmt = self::$db->prepare($sql); $stmt = self::db()->prepare($sql);
$stmt->bind_param('i', $this->id); $stmt->bind_param('i', $this->id);
$stmt->execute(); $stmt->execute();
if (!self::$db->error && !$stmt->errno) { if (!self::db()->error && !$stmt->errno) {
$ret = true; $ret = true;
$this->id = NULL; $this->id = NULL;
$this->login = NULL; $this->login = NULL;
@ -125,13 +134,13 @@
*/ */
public function setPass($pass) { public function setPass($pass) {
$ret = false; $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); $hash = password_hash($pass, PASSWORD_DEFAULT);
$sql = "UPDATE `" . self::$db->table('users') . "` SET password = ? WHERE login = ?"; $sql = "UPDATE `" . self::db()->table('users') . "` SET password = ? WHERE login = ?";
$stmt = self::$db->prepare($sql); $stmt = self::db()->prepare($sql);
$stmt->bind_param('ss', $hash, $this->login); $stmt->bind_param('ss', $hash, $this->login);
$stmt->execute(); $stmt->execute();
if (!self::$db->error && !$stmt->errno) { if (!self::db()->error && !$stmt->errno) {
$ret = true; $ret = true;
} }
$stmt->close(); $stmt->close();
@ -155,7 +164,7 @@
* @param String $password Password * @param String $password Password
* @return bool True if matches, false otherwise * @return bool True if matches, false otherwise
*/ */
private function validPassStrength($password) { private static function validPassStrength($password) {
return preg_match(uConfig::passRegex(), $password); return preg_match(uConfig::passRegex(), $password);
} }
@ -187,15 +196,15 @@
* *
* @return array|bool Array of uUser users, false on error * @return array|bool Array of uUser users, false on error
*/ */
public function getAll() { public static function getAll() {
$query = "SELECT id, login, password FROM `" . self::$db->table('users') . "` ORDER BY login"; $query = "SELECT id, login, password FROM `" . self::db()->table('users') . "` ORDER BY login";
$result = self::$db->query($query); $result = self::db()->query($query);
if ($result === false) { if ($result === false) {
return false; return false;
} }
$userArr = []; $userArr = [];
while ($row = $result->fetch_assoc()) { while ($row = $result->fetch_assoc()) {
$userArr[] = $this->rowToObject($row); $userArr[] = self::rowToObject($row);
} }
$result->close(); $result->close();
return $userArr; return $userArr;
@ -207,12 +216,12 @@
* @param array $row Row * @param array $row Row
* @return uUser User * @return uUser User
*/ */
private function rowToObject($row) { private static function rowToObject($row) {
$user = new uUser(); $user = new uUser();
$user->id = $row['id']; $user->id = $row['id'];
$user->login = $row['login']; $user->login = $row['login'];
$user->hash = $row['password']; $user->hash = $row['password'];
$user->isAdmin = $this->isAdmin($row['login']); $user->isAdmin = self::isAdmin($row['login']);
$user->isValid = true; $user->isValid = true;
return $user; return $user;
} }
@ -223,7 +232,7 @@
* @param string $login Login * @param string $login Login
* @return bool True if admin, false otherwise * @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); return (!empty(uConfig::$admin_user) && uConfig::$admin_user == $login);
} }
} }

View File

@ -27,21 +27,19 @@
if ($user->isAdmin || uConfig::$public_tracks) { if ($user->isAdmin || uConfig::$public_tracks) {
// public access or admin user // public access or admin user
// get last position user // get last position user
$lastPosition = new uPosition(); $lastPosition = uPosition::getLast();
$lastPosition->getLast();
if ($lastPosition->isValid) { if ($lastPosition->isValid) {
// display track of last position user // display track of last position user
$displayUserId = $lastPosition->userId; $displayUserId = $lastPosition->userId;
} }
// populate users array (for <select>) // populate users array (for <select>)
$usersArr = $user->getAll(); $usersArr = uUser::getAll();
} else if ($user->isValid) { } else if ($user->isValid) {
// display track of authenticated user // display track of authenticated user
$displayUserId = $user->id; $displayUserId = $user->id;
} }
$track = new uTrack(); $tracksArr = uTrack::getAll($displayUserId);
$tracksArr = $track->getAll($displayUserId);
if (!empty($tracksArr)) { if (!empty($tracksArr)) {
// get id of the latest track // get id of the latest track
$displayTrackId = $tracksArr[0]->id; $displayTrackId = $tracksArr[0]->id;

View File

@ -124,8 +124,7 @@ switch ($command) {
$login = isset($_REQUEST['login']) ? $_REQUEST['login'] : NULL; $login = isset($_REQUEST['login']) ? $_REQUEST['login'] : NULL;
$pass = isset($_REQUEST['pass']) ? $_REQUEST['pass'] : NULL; $pass = isset($_REQUEST['pass']) ? $_REQUEST['pass'] : NULL;
$user = new uUser(); if (uUser::add($login, $pass) !== false) {
if ($user->add($login, $pass) !== false) {
$messages[] = "<span class=\"ok\">{$langSetup["congratulations"]}</span>"; $messages[] = "<span class=\"ok\">{$langSetup["congratulations"]}</span>";
$messages[] = $langSetup["setupcomplete"]; $messages[] = $langSetup["setupcomplete"];
$messages[] = "<span class=\"warn\">{$langSetup["disablewarn"]}</span><br>"; $messages[] = "<span class=\"warn\">{$langSetup["disablewarn"]}</span><br>";

View File

@ -79,9 +79,7 @@ if (uConfig::$units == "imperial") {
} }
if ($trackId && $userId) { if ($trackId && $userId) {
$position = new uPosition(); $positionsArr = uPosition::getAll($userId, $trackId);
$positionsArr = [];
$positionsArr = $position->getAll($userId, $trackId);
if (empty($positionsArr)) { if (empty($positionsArr)) {
exit(); exit();
} }

View File

@ -29,13 +29,13 @@ if ($userId) {
$positionsArr = []; $positionsArr = [];
if (uConfig::$public_tracks || $user->isAdmin || $user->id === $userId) { if (uConfig::$public_tracks || $user->isAdmin || $user->id === $userId) {
$position = new uPosition();
if ($trackId) { if ($trackId) {
// get all track data // get all track data
$positionsArr = $position->getAll($userId, $trackId); $positionsArr = uPosition::getAll($userId, $trackId);
} else { } else {
// get data only for latest point // get data only for latest point
if ($position->getLast($userId)->isValid) { $position = uPosition::getLast($userId);
if ($position->isValid) {
$positionsArr[] = $position; $positionsArr[] = $position;
} }
} }

View File

@ -27,8 +27,7 @@ if ($userId) {
$tracksArr = []; $tracksArr = [];
if (uConfig::$public_tracks || $user->isAdmin || $user->id === $userId) { if (uConfig::$public_tracks || $user->isAdmin || $user->id === $userId) {
$track = new uTrack(); $tracksArr = uTrack::getAll($userId);
$tracksArr = $track->getAll($userId);
} }
header("Content-type: text/xml"); header("Content-type: text/xml");

View File

@ -35,7 +35,7 @@
if ($aUser->isValid) { if ($aUser->isValid) {
uUtils::exitWithError($lang["userexists"]); uUtils::exitWithError($lang["userexists"]);
} }
if (empty($pass) || $aUser->add($login, $pass) === false) { if (empty($pass) || uUser::add($login, $pass) === false) {
uUtils::exitWithError($lang["servererror"]); uUtils::exitWithError($lang["servererror"]);
} }
break; break;

View File

@ -88,14 +88,12 @@ $trackCnt = 0;
foreach ($gpx->trk as $trk) { foreach ($gpx->trk as $trk) {
$trackName = empty($trk->name) ? $gpxName : $trk->name->__toString(); $trackName = empty($trk->name) ? $gpxName : $trk->name->__toString();
$metaName = empty($gpx->metadata->name) ? NULL : $gpx->metadata->name->__toString(); $metaName = empty($gpx->metadata->name) ? NULL : $gpx->metadata->name->__toString();
$track = new uTrack(); $trackId = uTrack::add($user->id, $trackName, $metaName);
$trackId = $track->add($user->id, $trackName, $metaName);
if ($trackId === false) { if ($trackId === false) {
uUtils::exitWithError($lang["servererror"]); uUtils::exitWithError($lang["servererror"]);
break; break;
} }
$position = new uPosition();
foreach($trk->trkseg as $segment) { foreach($trk->trkseg as $segment) {
foreach($segment->trkpt as $point) { foreach($segment->trkpt as $point) {
$time = isset($point->time) ? strtotime($point->time) : NULL; $time = isset($point->time) ? strtotime($point->time) : NULL;
@ -112,7 +110,7 @@ foreach ($gpx->trk as $trk) {
if (count($ext->accuracy)) { $accuracy = (int) $ext->accuracy; } if (count($ext->accuracy)) { $accuracy = (int) $ext->accuracy; }
if (count($ext->provider)) { $provider = (string) $ext->provider; } if (count($ext->provider)) { $provider = (string) $ext->provider; }
} }
$ret = $position->add($user->id, $trackId, $ret = uPosition::add($user->id, $trackId,
$time, (double) $point["lat"], (double) $point["lon"], $altitude, $time, (double) $point["lat"], (double) $point["lon"], $altitude,
$speed, $bearing, $accuracy, $provider, NULL, NULL); $speed, $bearing, $accuracy, $provider, NULL, NULL);
if ($ret === false) { if ($ret === false) {