242 lines
6.9 KiB
PHP
Raw Normal View History

<?php
/* μlogger
*
* Copyright(C) 2017 Bartek Fabiszewski (www.fabiszewski.net)
*
* This is free software; you can redistribute it and/or modify it under
2017-04-07 00:05:28 +02:00
* the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
2017-04-07 00:05:28 +02:00
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
2017-04-12 20:16:39 +02:00
require_once(ROOT_DIR . "/helpers/db.php");
require_once(ROOT_DIR . "/helpers/track.php");
require_once(ROOT_DIR . "/helpers/position.php");
2017-04-09 23:35:55 +02:00
/**
* User handling routines
*/
class uUser {
public $id;
public $login;
public $hash;
public $isAdmin = false;
public $isValid = false;
2017-04-09 23:35:55 +02:00
/**
* Constructor
*
* @param string $login Login
*/
public function __construct($login = NULL) {
if (!empty($login)) {
2019-01-23 12:23:25 +01:00
try {
2020-02-17 18:51:27 +01:00
$query = "SELECT id, login, password, admin FROM " . self::db()->table('users') . " WHERE login = ? LIMIT 1";
2019-01-23 12:23:25 +01:00
$stmt = self::db()->prepare($query);
$stmt->execute([ $login ]);
2019-01-24 19:07:41 +01:00
$stmt->bindColumn('id', $this->id, PDO::PARAM_INT);
2019-01-23 12:23:25 +01:00
$stmt->bindColumn('login', $this->login);
$stmt->bindColumn('password', $this->hash);
2020-02-17 18:51:27 +01:00
$stmt->bindColumn('admin', $this->isAdmin, PDO::PARAM_BOOL);
2019-01-24 19:07:41 +01:00
if ($stmt->fetch(PDO::FETCH_BOUND)) {
$this->isValid = true;
}
2019-01-23 12:23:25 +01:00
} catch (PDOException $e) {
// TODO: handle exception
2019-01-24 19:07:41 +01:00
syslog(LOG_ERR, $e->getMessage());
}
}
}
/**
* Get db instance
*
* @return uDb instance
*/
private static function db() {
2019-05-15 11:48:24 +02:00
return uDb::getInstance();
}
2020-02-17 18:51:27 +01:00
/**
* Add new user
*
* @param string $login Login
* @param string $pass Password
* @param bool $isAdmin Is admin
* @return int|bool New user id, false on error
*/
public static function add($login, $pass, $isAdmin = false) {
$userid = false;
2020-02-20 17:08:47 +01:00
if (!empty($login) && !empty($pass)) {
$hash = password_hash($pass, PASSWORD_DEFAULT);
2019-01-23 12:23:25 +01:00
$table = self::db()->table('users');
try {
2020-02-17 18:51:27 +01:00
$query = "INSERT INTO $table (login, password, admin) VALUES (?, ?, ?)";
2019-01-23 12:23:25 +01:00
$stmt = self::db()->prepare($query);
2020-02-17 18:51:27 +01:00
$stmt->execute([ $login, $hash, (int) $isAdmin ]);
$userid = (int) self::db()->lastInsertId("${table}_id_seq");
2019-01-23 12:23:25 +01:00
} catch (PDOException $e) {
// TODO: handle exception
2019-01-24 19:07:41 +01:00
syslog(LOG_ERR, $e->getMessage());
}
}
return $userid;
}
2017-04-12 20:16:39 +02:00
/**
* Delete user
* This will also delete all user's positions and tracks
*
* @return bool True if success, false otherwise
*/
public function delete() {
$ret = false;
if ($this->isValid) {
// remove tracks and positions
if (uTrack::deleteAll($this->id) === false) {
2017-04-12 20:16:39 +02:00
return false;
}
// remove user
2019-01-23 12:23:25 +01:00
try {
$query = "DELETE FROM " . self::db()->table('users') . " WHERE id = ?";
$stmt = self::db()->prepare($query);
$stmt->execute([ $this->id ]);
2017-04-12 20:16:39 +02:00
$ret = true;
$this->id = NULL;
$this->login = NULL;
$this->hash = NULL;
$this->isValid = false;
$this->isAdmin = false;
2019-01-23 12:23:25 +01:00
} catch (PDOException $e) {
// TODO: handle exception
2019-01-24 19:07:41 +01:00
syslog(LOG_ERR, $e->getMessage());
2017-04-12 20:16:39 +02:00
}
}
return $ret;
}
/**
* Set user admin status
*
* @param bool $isAdmin True if is admin
* @return bool True on success, false otherwise
*/
public function setAdmin($isAdmin) {
$ret = false;
try {
$query = "UPDATE " . self::db()->table('users') . " SET admin = ? WHERE login = ?";
$stmt = self::db()->prepare($query);
2020-02-19 18:42:44 +01:00
$stmt->execute([ (int) $isAdmin, $this->login ]);
$ret = true;
$this->isAdmin = $isAdmin;
} catch (PDOException $e) {
// TODO: handle exception
syslog(LOG_ERR, $e->getMessage());
}
return $ret;
}
2017-04-09 23:35:55 +02:00
/**
* Set user password
*
* @param string $pass Password
2017-04-09 23:35:55 +02:00
* @return bool True on success, false otherwise
*/
public function setPass($pass) {
2017-04-06 23:23:25 +02:00
$ret = false;
2020-02-20 17:08:47 +01:00
if (!empty($this->login) && !empty($pass)) {
$hash = password_hash($pass, PASSWORD_DEFAULT);
2019-01-23 12:23:25 +01:00
try {
$query = "UPDATE " . self::db()->table('users') . " SET password = ? WHERE login = ?";
$stmt = self::db()->prepare($query);
$stmt->execute([ $hash, $this->login ]);
$ret = true;
$this->hash = $hash;
2019-01-23 12:23:25 +01:00
} catch (PDOException $e) {
// TODO: handle exception
2019-01-24 19:07:41 +01:00
syslog(LOG_ERR, $e->getMessage());
}
2017-04-06 23:23:25 +02:00
}
return $ret;
}
2017-04-09 23:35:55 +02:00
/**
* Check if given password matches user's one
*
* @param String $password Password
* @return bool True if matches, false otherwise
*/
public function validPassword($password) {
return password_verify($password, $this->hash);
}
2017-04-09 23:35:55 +02:00
/**
* Store uUser object in session
*/
public function storeInSession() {
$_SESSION['user'] = $this;
}
2017-04-09 23:35:55 +02:00
/**
* Fill uUser object properties from session data
2017-08-25 13:59:19 +02:00
* @return uUser
2017-04-09 23:35:55 +02:00
*/
2020-02-20 17:08:47 +01:00
public static function getFromSession() {
$user = new uUser();
if (isset($_SESSION['user'])) {
$sessionUser = $_SESSION['user'];
2020-02-20 17:08:47 +01:00
$user->id = $sessionUser->id;
$user->login = $sessionUser->login;
$user->hash = $sessionUser->hash;
$user->isAdmin = $sessionUser->isAdmin;
$user->isValid = $sessionUser->isValid;
}
2020-02-20 17:08:47 +01:00
return $user;
}
2017-04-09 23:35:55 +02:00
/**
* Get all users
*
2019-11-16 17:43:23 +01:00
* @return uUser[]|bool Array of uUser users, false on error
2017-04-09 23:35:55 +02:00
*/
public static function getAll() {
2019-01-23 12:23:25 +01:00
try {
2020-02-17 18:51:27 +01:00
$query = "SELECT id, login, password, admin FROM " . self::db()->table('users') . " ORDER BY login";
2019-01-23 12:23:25 +01:00
$result = self::db()->query($query);
$userArr = [];
while ($row = $result->fetch()) {
$userArr[] = self::rowToObject($row);
}
} catch (PDOException $e) {
// TODO: handle exception
2019-01-24 19:07:41 +01:00
syslog(LOG_ERR, $e->getMessage());
2019-01-23 12:23:25 +01:00
$userArr = false;
}
return $userArr;
}
2017-04-09 23:35:55 +02:00
/**
* Convert database row to uUser
*
* @param array $row Row
* @return uUser User
*/
private static function rowToObject($row) {
$user = new uUser();
$user->id = $row['id'];
$user->login = $row['login'];
$user->hash = $row['password'];
2020-02-17 18:51:27 +01:00
$user->isAdmin = (bool) $row['admin'];
$user->isValid = true;
return $user;
}
2017-04-09 23:35:55 +02:00
}
2017-04-16 22:45:56 +02:00
?>