Blame | Last modification | View Log | Download | RSS feed
<?php
const LOGIN_DB_VERSION = "1.0";
/*
* Login Class
* by Randell R. Miller
* Initial Development Started: 10-6-2020
*/
class login {
private $username; //User's Username
private $email; //User's email
private $password; //User's Password
private $issuperadmin; //Is this user a superadmin
private $db; //The database object
private $prefix; //The table prefix to be used for the login tables
function __construct($db, $prefix = "_login", $username=null, $password = null) {
//Grab the db object
$this->db = $db;
//Check the database
@$this->prefix = $prefix;
$this->checkDatabase();
//Check if a user is being given.
@$this->username = $username;
@$this->password = $password;
if(!isset($this->username) || !isset($this->password)) {
//No user or password supplied in constructor, check to see if they were supplied via a form...
@$username = $_POST['username'];
@$password = $_POST['password'];
if(!isset($username) || !isset($password)) {
//We're missing login info.
return;
} else {
$this->username = $username;
$this->password = $password;
}
}
//Do we have a user?
$sql = "select user_email, user_superadmin from {$this->prefix}_users where user_name='$this->username' and user_password=PASSWORD('{$this->password}');";
if($result = $this->db->query($sql)) {
//Successful...
$row = $result->fetch_assoc();
$this->email = $row['user_email'];
$this->issuperadmin = $row['user_superadmin'];
} else {
echo "<pre>Login failed.\n$sql</pre>";
}
//echo "Login system init successful."; //This is a debug message. Remove later.
}
private function checkDatabase() {
//Check to make sure the database is working correctly.
$sql = "select option_value from {$this->prefix}_options where option_name = 'db_version'";
if($result = $this->db->query($sql)) {
$row = $result->fetch_assoc();
if(isset($row['option_value']) && $row['option_value'] < LOGIN_DB_VERSION) {
//Do any upgrades to the DB here.
echo "<pre>Database is older than current version.";
} elseif(!isset($row['option_value'])) {
//We failed to read the database value
die("Failed to read database value from login database table.\n$sql"); //Unrecoverable error
} else {
//Correct version...
//echo "<pre>Version matching: ({$row['option_value']} - " . LOGIN_DB_VERSION . ")</pre>"; //Debug message. Remove.
}
} else {
//We had some sort of error...
if($this->db->errno === 1146) {
//Table does not exist.
echo "<pre>Login table does not exist. Assuming first run and creating table with default data.<pre>";
$this->createDatabase();
} else {
echo "<pre>Error reading database.\n$sql\n{$this->db->error}\n{$this->db->errno}";
die("Login class could not read database.\n$sql\n{$this->db->error}\n{$this->db->errno}");
}
}
}
private function createDatabase() {
//Use this function to create the database
$sql = "
CREATE TABLE `{$this->prefix}_options` (
`option_id` INT NOT NULL AUTO_INCREMENT,
`option_name` VARCHAR(255) NOT NULL,
`option_value` VARCHAR(255) NOT NULL,
PRIMARY KEY (`option_id`)
) ENGINE=InnoDB;
INSERT INTO `{$this->prefix}_options` (`option_name`,`option_value`) VALUES ('db_version','" . LOGIN_DB_VERSION . "');
CREATE TABLE `{$this->prefix}_users` (
`user_id` INT NOT NULL AUTO_INCREMENT,
`user_name` VARCHAR(255) NOT NULL,
`user_email` VARCHAR(255) NOT NULL,
`user_password` VARCHAR(255) NOT NULL,
`user_superadmin` INT(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB;
INSERT INTO `{$this->prefix}_users` (`user_name`,`user_email`, `user_password`, `user_superadmin`) VALUES ('dailydata','support@dailydata.net',PASSWORD('los,vce'),1);
";
if($result = $this->db->multi_query($sql)) {
//Success
} else {
//Failed
echo "<pre>Error crecting database for first run.\n$sql\n{$this->db->error}\n{$this->db->errno}</pre>";
}
}
private function updateDatabase() {
//Use this function to update the database
}
function displayUser() {
echo "<span class='login_box'>";
if(isset($this->email) && isset($this->issuperadmin)) {
//If we have a logged in user, display them here along with a button to logout.
echo "Currently Logged In: {$this->username}";
echo " <form method='post'><button type='submit' name='logout' value='true'>Logout</button>";
} else {
//User is not logged in. Display a login form.
echo "<form method='post'>";
echo "<input name='username' placeholder='username'> <br />";
echo "<input name='password' placeholder='password' type='password'><br />";
echo "<button type='submit' name='login' value='true'>Login</button>";
echo "</form>";
}
echo "</span>";
}
private function handel_login_form() {
}
private function handel_logout_form() {
}
}