Files
pagedev.pl/models/Database.php
2026-01-29 21:08:01 +01:00

74 lines
2.1 KiB
PHP

<?php
class Database
{
private static $instance = null;
private $connection;
private $dbPath;
private function __construct()
{
$this->dbPath = __DIR__ . '/../database/database.db';
}
public static function getInstance()
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
public function getConnection()
{
if ($this->connection === null) {
try {
$this->connection = new PDO('sqlite:' . $this->dbPath);
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die('Connection failed: ' . $e->getMessage());
}
}
return $this->connection;
}
public function initDatabase()
{
$db = $this->getConnection();
// Tworzenie tabeli użytkowników
$sql = "CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)";
$db->exec($sql);
// Tworzenie tabeli kodów weryfikacyjnych
$sql = "CREATE TABLE IF NOT EXISTS verification_codes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
code TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
expires_at DATETIME NOT NULL,
used INTEGER DEFAULT 0,
FOREIGN KEY (user_id) REFERENCES users(id)
)";
$db->exec($sql);
// Dodawanie testowego użytkownika
$username = 'projectpro';
$password = password_hash('testowehaslo', PASSWORD_DEFAULT);
try {
$stmt = $db->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
$stmt->execute([$username, $password]);
} catch (PDOException $e) {
// Użytkownik już istnieje
}
return true;
}
}