96 lines
2.5 KiB
PHP
96 lines
2.5 KiB
PHP
<?php
|
|
namespace App\Models;
|
|
|
|
use App\Config;
|
|
|
|
class ClientSession
|
|
{
|
|
protected static $conn = null;
|
|
|
|
protected static function connectDB()
|
|
{
|
|
try {
|
|
if (!self::$conn) {
|
|
$dsn = 'mysql:host='.Config::DB_HOST.';dbname='.Config::DB_DB;
|
|
self::$conn = new \PDO($dsn, Config::DB_UNAME, Config::DB_PWD);
|
|
|
|
self::$conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
|
}
|
|
return self::$conn;
|
|
} catch (PDOException $e) {
|
|
throw new \Exception($e->getMessage, 444);
|
|
}
|
|
}
|
|
|
|
public static function fetch($args)
|
|
{
|
|
$sql = "SELECT * FROM `client_session` WHERE ";
|
|
|
|
$x = 0;
|
|
foreach ($args as $key => $value) {
|
|
$sql .= "$key=?";
|
|
if ($x < count($args)-1) {
|
|
$sql .= " AND ";
|
|
}
|
|
$x++;
|
|
}
|
|
|
|
try {
|
|
$db = static::connectDB();
|
|
$query = $db->prepare($sql);
|
|
$x = 1;
|
|
foreach ($args as $value) {
|
|
$query->bindValue($x, $value);
|
|
$x++;
|
|
}
|
|
|
|
$query->execute();
|
|
$result = $query->fetch(\PDO::FETCH_ASSOC);
|
|
} catch (PDOException $e) {
|
|
$result = $e->getMessage();
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
public static function entry($args)
|
|
{
|
|
$sql = "INSERT INTO `client_session` (`ip_address`, `uid`) VALUES (?, ?)";
|
|
|
|
try {
|
|
$db = static::connectDB();
|
|
$query = $db->prepare($sql);
|
|
$query->bindValue(1, $args['ip_address']);
|
|
$query->bindValue(2, $args['uid']);
|
|
$query->execute();
|
|
|
|
$last_entry = "SELECT LAST_INSERT_ID()";
|
|
$last_entry = $db->prepare($last_entry);
|
|
$last_entry->execute();
|
|
|
|
$result = $last_entry->fetch(\PDO::FETCH_ASSOC);
|
|
return true;
|
|
} catch (PDOException $e) {
|
|
$result = $e->getMessage();
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
public static function remove($ip, $id)
|
|
{
|
|
$sql = "DELETE FROM `client_session` WHERE `ip_address` = ? AND `uid` = ?";
|
|
|
|
try {
|
|
$db = static::connectDB();
|
|
$query = $db->prepare($sql);
|
|
$query->bindValue(1, $ip);
|
|
$query->bindValue(2, $id);
|
|
$query->execute();
|
|
|
|
$result = self::fetch(['uid' => $id]);
|
|
} catch (PDOException $e) {
|
|
$result = $e->getMessage();
|
|
}
|
|
return $result;
|
|
}
|
|
}
|