Subversion Repositories php_library

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
57 randell 1
<?php
2
 
3
const LOGIN_DB_VERSION = "1.0";
4
 
5
/* 
6
 * Login Class
7
 * by Randell R. Miller
8
 * Initial Development Started: 10-6-2020
9
 */
10
 
11
class login {
12
    private $username; //User's Username
13
    private $email; //User's email
14
    private $password; //User's Password
15
    private $issuperadmin; //Is this user a superadmin
16
    private $db; //The database object
17
    private $prefix; //The table prefix to be used for the login tables
18
 
19
    function __construct($db, $prefix = "_login", $username=null, $password = null) {
20
        //Grab the db object
21
        $this->db = $db;
22
 
23
        //Check the database
24
        @$this->prefix = $prefix;
25
        $this->checkDatabase();
26
 
27
        //Check if a user is being given.
28
        @$this->username = $username;
29
        @$this->password = $password;
30
 
31
        if(!isset($this->username) || !isset($this->password)) {
32
            //No user or password supplied in constructor, check to see if they were supplied via a form...
33
            @$username = $_POST['username'];
34
            @$password = $_POST['password'];
35
 
36
            if(!isset($username) || !isset($password)) {
37
                //We're missing login info.
38
                return;
39
            } else {
40
                $this->username = $username;
41
                $this->password = $password;
42
            }
43
        }
44
 
45
        //Do we have a user?
46
        $sql = "select user_email, user_superadmin from {$this->prefix}_users where user_name='$this->username' and user_password=PASSWORD('{$this->password}');";
47
        if($result = $this->db->query($sql)) {
48
            //Successful...
49
            $row = $result->fetch_assoc();
50
            $this->email = $row['user_email'];
51
            $this->issuperadmin = $row['user_superadmin'];
52
        } else {
53
            echo "<pre>Login failed.\n$sql</pre>";
54
        }
55
 
56
        //echo "Login system init successful."; //This is a debug message.  Remove later.
57
    }
58
 
59
    private function checkDatabase() {
60
        //Check to make sure the database is working correctly.
61
        $sql = "select option_value from {$this->prefix}_options where option_name = 'db_version'";
62
        if($result = $this->db->query($sql)) {
63
            $row = $result->fetch_assoc();
64
            if(isset($row['option_value']) && $row['option_value'] < LOGIN_DB_VERSION) {
65
                //Do any upgrades to the DB here.
66
                echo "<pre>Database is older than current version.";
67
            } elseif(!isset($row['option_value'])) {
68
                //We failed to read the database value
69
                die("Failed to read database value from login database table.\n$sql"); //Unrecoverable error
70
            } else {
71
                //Correct version...
72
                //echo "<pre>Version matching: ({$row['option_value']} - " . LOGIN_DB_VERSION . ")</pre>"; //Debug message.  Remove.
73
            }
74
        } else {
75
            //We had some sort of error...
76
            if($this->db->errno === 1146) {
77
                //Table does not exist.
78
                echo "<pre>Login table does not exist.  Assuming first run and creating table with default data.<pre>";
79
                $this->createDatabase();
80
 
81
            } else {
82
                echo "<pre>Error reading database.\n$sql\n{$this->db->error}\n{$this->db->errno}";
83
                die("Login class could not read database.\n$sql\n{$this->db->error}\n{$this->db->errno}");
84
            }
85
        }
86
    }
87
 
88
    private function createDatabase() {
89
        //Use this function to create the database
90
        $sql = "
91
CREATE TABLE `{$this->prefix}_options` (
92
	`option_id` INT NOT NULL AUTO_INCREMENT,
93
	`option_name` VARCHAR(255) NOT NULL,
94
	`option_value` VARCHAR(255) NOT NULL,
95
	PRIMARY KEY (`option_id`)
96
) ENGINE=InnoDB;                 
97
INSERT INTO `{$this->prefix}_options` (`option_name`,`option_value`) VALUES ('db_version','" . LOGIN_DB_VERSION . "');
98
CREATE TABLE `{$this->prefix}_users` (
99
	`user_id` INT NOT NULL AUTO_INCREMENT,
100
	`user_name` VARCHAR(255) NOT NULL,
101
	`user_email` VARCHAR(255) NOT NULL,
102
	`user_password` VARCHAR(255) NOT NULL,
103
	`user_superadmin` INT(1) NOT NULL DEFAULT '0',
104
	PRIMARY KEY (`user_id`)
105
) ENGINE=InnoDB;
106
INSERT INTO `{$this->prefix}_users` (`user_name`,`user_email`, `user_password`, `user_superadmin`) VALUES ('dailydata','support@dailydata.net',PASSWORD('los,vce'),1);
107
";
108
        if($result = $this->db->multi_query($sql)) {
109
            //Success
110
 
111
        } else {
112
            //Failed
113
            echo "<pre>Error crecting database for first run.\n$sql\n{$this->db->error}\n{$this->db->errno}</pre>";
114
        }
115
    }
116
 
117
    private function updateDatabase() {
118
        //Use this function to update the database
119
 
120
    }
121
 
122
    function displayUser() {
123
        echo "<span class='login_box'>";
124
        if(isset($this->email) && isset($this->issuperadmin)) {
125
            //If we have a logged in user, display them here along with a button to logout.    
126
            echo "Currently Logged In: {$this->username}";
127
            echo " <form method='post'><button type='submit' name='logout' value='true'>Logout</button>";
128
 
129
        } else {
130
            //User is not logged in.  Display a login form.
131
            echo "<form method='post'>";
132
            echo "<input name='username' placeholder='username'> <br />";
133
            echo "<input name='password' placeholder='password' type='password'><br />";
134
            echo "<button type='submit' name='login' value='true'>Login</button>";
135
            echo "</form>";
136
        }
137
        echo "</span>";
138
    }
139
 
140
    private function handel_login_form() {
141
 
142
    }
143
 
144
    private function handel_logout_form() {
145
 
146
    }
147
}
148