// can't be bothered to come up with a proper comment
This commit is contained in:
parent
fbd25a443a
commit
c4cd40eb11
3 changed files with 68 additions and 17 deletions
|
@ -71,6 +71,7 @@ require_once 'src/Users/auth.php';
|
|||
require_once 'src/Users/avatar.php';
|
||||
require_once 'src/Users/background.php';
|
||||
require_once 'src/Users/login_attempt.php';
|
||||
require_once 'src/Users/object.php';
|
||||
require_once 'src/Users/profile.php';
|
||||
require_once 'src/Users/recovery.php';
|
||||
require_once 'src/Users/relations.php';
|
||||
|
|
|
@ -8,7 +8,6 @@ class DatabaseStatement {
|
|||
public $pdo;
|
||||
public $stmt;
|
||||
private $isQuery;
|
||||
private $hasExecuted = false;
|
||||
|
||||
public function __construct(PDOStatement $stmt, PDO $pdo, bool $isQuery) {
|
||||
$this->stmt = $stmt;
|
||||
|
@ -22,26 +21,13 @@ class DatabaseStatement {
|
|||
}
|
||||
|
||||
public function execute(array $params = []): bool {
|
||||
if($this->hasExecuted)
|
||||
return true;
|
||||
$this->hasExecuted = true;
|
||||
|
||||
return count($params) ? $this->stmt->execute($params) : $this->stmt->execute();
|
||||
}
|
||||
|
||||
public function executeGetId(array $params = []): int {
|
||||
if($this->hasExecuted)
|
||||
return true;
|
||||
$this->hasExecuted = true;
|
||||
|
||||
return $this->execute($params) ? $this->pdo->lastInsertId() : 0;
|
||||
}
|
||||
|
||||
public function reset(): DatabaseStatement {
|
||||
$this->hasExecuted = false;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function fetch($default = []) {
|
||||
$out = $this->isQuery || $this->execute() ? $this->stmt->fetch(PDO::FETCH_ASSOC) : false;
|
||||
return $out ? $out : $default;
|
||||
|
@ -61,7 +47,7 @@ class DatabaseStatement {
|
|||
$out = false;
|
||||
|
||||
if($this->isQuery || $this->execute()) {
|
||||
$out = $args === null ? $this->fetchObject($className) : $this->fetchObject($className, $args);
|
||||
$out = $args === null ? $this->stmt->fetchObject($className) : $this->stmt->fetchObject($className, $args);
|
||||
}
|
||||
|
||||
return $out !== false ? $out : $default;
|
||||
|
@ -70,8 +56,10 @@ class DatabaseStatement {
|
|||
public function fetchObjects(string $className = 'stdClass', ?array $args = null): array {
|
||||
$objects = [];
|
||||
|
||||
while(($object = $this->fetchObject($className, $args, false)) !== false) {
|
||||
$objects[] = $object;
|
||||
if($this->isQuery || $this->execute()) {
|
||||
while(($object = $this->stmt->fetchObject($className, $args)) !== false) {
|
||||
$objects[] = $object;
|
||||
}
|
||||
}
|
||||
|
||||
return $objects;
|
||||
|
|
62
src/Users/object.php
Normal file
62
src/Users/object.php
Normal file
|
@ -0,0 +1,62 @@
|
|||
<?php
|
||||
namespace Misuzu\Users;
|
||||
|
||||
use Misuzu\DB;
|
||||
|
||||
class User {
|
||||
private const USER_SELECT = '
|
||||
SELECT `user_id`, `username`, `password`, `email`, `user_super`, `user_title`,
|
||||
`user_country`, `user_colour`, `display_role`, `user_totp_key`,
|
||||
`user_about_content`, `user_about_parser`,
|
||||
`user_signature_content`, `user_signature_parser`,
|
||||
`user_birthdate`, `user_background_settings`,
|
||||
INET6_NTOA(`register_ip`) AS `register_ip`,
|
||||
INET6_NTOA(`last_ip`) AS `last_ip`,
|
||||
UNIX_TIMESTAMP(`user_created`) AS `user_created`,
|
||||
UNIX_TIMESTAMP(`user_active`) AS `user_active`,
|
||||
UNIX_TIMESTAMP(`user_deleted`) AS `user_deleted`,
|
||||
`user_website`, `user_twitter`, `user_github`, `user_skype`,
|
||||
`user_discord`, `user_youtube`, `user_steam`, `user_ninswitch`,
|
||||
`user_twitchtv`, `user_osu`, `user_lastfm`
|
||||
FROM `msz_users`
|
||||
';
|
||||
|
||||
public function __construct() {
|
||||
//
|
||||
}
|
||||
|
||||
public static function create(
|
||||
string $username,
|
||||
string $password,
|
||||
string $email,
|
||||
string $ipAddress
|
||||
): ?User {
|
||||
$createUser = DB::prepare('
|
||||
INSERT INTO `msz_users`
|
||||
(
|
||||
`username`, `password`, `email`, `register_ip`,
|
||||
`last_ip`, `user_country`, `display_role`
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
:username, :password, LOWER(:email), INET6_ATON(:register_ip),
|
||||
INET6_ATON(:last_ip), :user_country, 1
|
||||
)
|
||||
') ->bind('username', $username)->bind('email', $email)
|
||||
->bind('register_ip', $ipAddress)->bind('last_ip', $ipAddress)
|
||||
->bind('password', user_password_hash($password))
|
||||
->bind('user_country', ip_country_code($ipAddress))
|
||||
->executeGetId();
|
||||
|
||||
if($createUser < 1)
|
||||
return null;
|
||||
|
||||
return static::get($createUser);
|
||||
}
|
||||
|
||||
public static function get(int $userId): ?User {
|
||||
return DB::prepare(self::USER_SELECT . 'WHERE `user_id` = :user_id')
|
||||
->bind('user_id', $userId)
|
||||
->fetchObject(User::class);
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue