Show sourcecode
The following files exists in this folder. Click to view.
webbserverprogrammering/submissions/projekt-matkort-handler/
.github/
add_logs.php
admin/
api/
card_balance.php
classes/
config/
food_logs.php
forgot_password.php
includes/
index.php
insert_restaurants.php
install.php
login.php
logout.php
public/
register.php
reset_password.php
verify.php
install.php
169 lines UTF-8 Windows (CRLF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
<?php
// AUTO-CREATE TABLES (om de inte finns)
// Funktion för att skapa tabell om den inte finns
require_once './config/database.php';
function createTableIfNotExists($pdo, $tableName, $createSQL, $debug = false) {
try {
// Kolla om tabellen finns
$pdo->query("DESCRIBE `$tableName`");
if ($debug) {
echo "Table '$tableName' exists.<br />";
}
} catch (PDOException $e) {
// Tabellen finns inte - skapa den
try {
$pdo->exec($createSQL);
if ($debug) {
echo "Created table '$tableName'.<br />";
}
} catch (PDOException $ex) {
error_log("Error creating table '$tableName': " . $ex->getMessage());
if ($debug) {
echo "Error creating table '$tableName': " . $ex->getMessage() . "<br />";
}
}
}
}
// TABELL 1: students
createTableIfNotExists($pdo, 'students', "
CREATE TABLE students (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
is_admin TINYINT(1) DEFAULT 0,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
last_login DATETIME NULL,
is_verified TINYINT(1) DEFAULT 0,
verification_token VARCHAR(64) NULL,
token_expires_at DATETIME NULL,
reset_token VARCHAR(64) NULL,
reset_token_expires_at DATETIME NULL,
INDEX idx_email (email),
INDEX idx_admin (is_admin)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
", $DEBUG);
// TABELL 2: food_logs
createTableIfNotExists($pdo, 'food_logs', "
CREATE TABLE food_logs (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
user_id INT UNSIGNED NOT NULL,
restaurant_id INT UNSIGNED NOT NULL,
money_spent DECIMAL(10,2) NOT NULL,
healthy_rating INT NOT NULL,
happy_rating INT NOT NULL,
comment TEXT NULL,
log_date DATETIME NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
INDEX idx_user_id (user_id),
INDEX idx_restaurant_id (restaurant_id),
FOREIGN KEY (user_id) REFERENCES students(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
", true);
// TABELL 3: restaurants
createTableIfNotExists($pdo, 'restaurants', "
CREATE TABLE restaurants (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
restaurant_name VARCHAR(255) NOT NULL,
location VARCHAR(255) NOT NULL,
price VARCHAR(50) DEFAULT '90',
lat DECIMAL(10,6) NOT NULL,
lng DECIMAL(10,6) NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
", $DEBUG);
// TABELL 4: card_balance
createTableIfNotExists($pdo, 'card_balance', "
CREATE TABLE card_balance (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
user_id INT UNSIGNED NOT NULL,
amount DECIMAL(10,2) NOT NULL,
last_reset_date DATE,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
INDEX idx_user_id (user_id),
FOREIGN KEY (user_id) REFERENCES students(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
", $DEBUG);
// TABELL 5: favourite_restaurants
createTableIfNotExists($pdo, 'favourite_restaurants', "
CREATE TABLE favourite_restaurants (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
user_id INT UNSIGNED NOT NULL,
restaurant_id INT UNSIGNED NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
INDEX idx_user_id (user_id),
FOREIGN KEY (user_id) REFERENCES students(id) ON DELETE CASCADE,
FOREIGN KEY (restaurant_id) REFERENCES restaurants(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
", $DEBUG);
// TABELL 6: reset_schedule
createTableIfNotExists($pdo, 'reset_schedule', "
CREATE TABLE reset_schedule (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
reset_date DATE NOT NULL,
reset_amount DECIMAL(10,2) NOT NULL DEFAULT 2800,
is_processed BOOLEAN DEFAULT FALSE,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
INDEX idx_reset_date (reset_date)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
", $DEBUG);
// SKAPA FÖRSTA ADMIN (om students-tabellen är tom)
try {
$stmt = $pdo->query("SELECT COUNT(*) as count FROM students WHERE is_admin = 1");
$result = $stmt->fetch();
if ($result['count'] == 0) {
// Ingen admin finns - skapa en
$default_password = password_hash('Tobias123', PASSWORD_DEFAULT);
$stmt = $pdo->prepare("
INSERT INTO students (name, email, password, is_admin, created_at, is_verified)
VALUES ('Admin', 'firuz.nabiev123@gmail.com', :password, 1, NOW(), 1)
");
$stmt->execute([':password' => $default_password]);
if ($DEBUG) {
echo "Created default admin user.<br />";
echo " Email: firuz.nabiev123@gmail.com<br />";
echo " Password: Tobias123<br />";
}
}
} catch (PDOException $e) {
error_log("Error checking/creating admin: " . $e->getMessage());
if ($DEBUG) {
echo "Error checking/creating admin: " . $e->getMessage() . "<br />";
}
}
if ($DEBUG) {
echo '<hr>';
echo '<p><strong>Database setup complete!</strong></p>';
echo '<p>Remove or set $DEBUG = false before deploying to production.</p>';
}