Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
cse4707-secure-messaging/app/database/init.php
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
53 lines (45 sloc)
1.68 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
defined('APP_DIR') or define('APP_DIR', 'app/'); | |
include_once('app/config/config.php'); | |
include_once('app/database/Database.php'); | |
include_once('app/model/User.php'); | |
$dbh = new PDO('mysql:host=' . Config::db_host, Config::db_user, Config::db_pass); | |
/* Create database if it doesn't exist */ | |
$dbh->exec('CREATE DATABASE IF NOT EXISTS ' . Config::db_name); | |
/* Connect to database */ | |
$dbh = Database::connect(); | |
/* Create users table if it doesn't exist */ | |
$stmt = 'CREATE TABLE IF NOT EXISTS users ( | |
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, | |
username VARCHAR(32) NOT NULL, | |
password VARCHAR(64) NOT NULL, | |
access VARCHAR(8) NOT NULL, | |
login_attempts INT(6) UNSIGNED NOT NULL | |
)'; | |
$dbh->exec($stmt); | |
/* Create messages table if it doesn't exitst */ | |
$stmt = 'CREATE TABLE IF NOT EXISTS messages ( | |
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, | |
message TEXT NOT NULL, | |
sender_id INT UNSIGNED NOT NULL, | |
receiver_id INT UNSIGNED NOT NULL, | |
enc_keys VARCHAR(64) NOT NULL, | |
keys_iv VARCHAR(24) NOT NULL, | |
message_iv VARCHAR(24) NOT NULL, | |
is_read BOOLEAN NOT NULL DEFAULT 0, | |
FOREIGN KEY (sender_id) REFERENCES users(id), | |
FOREIGN KEY (receiver_id) REFERENCES users(id) | |
)'; | |
$dbh->exec($stmt); | |
/* Create user and admin if they don't exist */ | |
$user = new User; | |
$user->username = 'user'; | |
$user->password = password_hash('userpass', PASSWORD_DEFAULT); | |
$user->access = 'user'; | |
$user->save(); | |
$user = new User; | |
$user->username = 'admin'; | |
$user->password = password_hash('adminpass', PASSWORD_DEFAULT); | |
$user->access = 'admin'; | |
$user->save(); | |
?> |