forked from brandonsavage/masterclass-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
User.php
161 lines (136 loc) · 5.87 KB
/
User.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
class User {
public $db;
public function __construct($config) {
$dbconfig = $config['database'];
$dsn = 'mysql:host=' . $dbconfig['host'] . ';dbname=' . $dbconfig['name'];
$this->db = new PDO($dsn, $dbconfig['user'], $dbconfig['pass']);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
public function create() {
$error = null;
// Do the create
if(isset($_POST['create'])) {
if(empty($_POST['username']) || empty($_POST['email']) ||
empty($_POST['password']) || empty($_POST['password_check'])) {
$error = 'You did not fill in all required fields.';
}
if(is_null($error)) {
if(!filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL)) {
$error = 'Your email address is invalid';
}
}
if(is_null($error)) {
if($_POST['password'] != $_POST['password_check']) {
$error = "Your passwords didn't match.";
}
}
if(is_null($error)) {
$check_sql = 'SELECT * FROM user WHERE username = ?';
$check_stmt = $this->db->prepare($check_sql);
$check_stmt->execute(array($_POST['username']));
if($check_stmt->rowCount() > 0) {
$error = 'Your chosen username already exists. Please choose another.';
}
}
if(is_null($error)) {
$params = array(
$_POST['username'],
$_POST['email'],
md5($_POST['username'] . $_POST['password']),
);
$sql = 'INSERT INTO user (username, email, password) VALUES (?, ?, ?)';
$stmt = $this->db->prepare($sql);
$stmt->execute($params);
header("Location: /user/login");
exit;
}
}
// Show the create form
$content = '
<form method="post">
' . $error . '<br />
<label>Username</label> <input type="text" name="username" value="" /><br />
<label>Email</label> <input type="text" name="email" value="" /><br />
<label>Password</label> <input type="password" name="password" value="" /><br />
<label>Password Again</label> <input type="password" name="password_check" value="" /><br />
<input type="submit" name="create" value="Create User" />
</form>
';
require_once 'layout.phtml';
}
public function account() {
$error = null;
if(!isset($_SESSION['AUTHENTICATED'])) {
header("Location: /user/login");
exit;
}
if(isset($_POST['updatepw'])) {
if(!isset($_POST['password']) || !isset($_POST['password_check']) ||
$_POST['password'] != $_POST['password_check']) {
$error = 'The password fields were blank or they did not match. Please try again.';
}
else {
$sql = 'UPDATE user SET password = ? WHERE username = ?';
$stmt = $this->db->prepare($sql);
$stmt->execute(array(
md5($_SESSION['username'] . $_POST['password']), // THIS IS NOT SECURE.
$_SESSION['username'],
));
$error = 'Your password was changed.';
}
}
$dsql = 'SELECT * FROM user WHERE username = ?';
$stmt = $this->db->prepare($dsql);
$stmt->execute(array($_SESSION['username']));
$details = $stmt->fetch(PDO::FETCH_ASSOC);
$content = '
' . $error . '<br />
<label>Username:</label> ' . $details['username'] . '<br />
<label>Email:</label>' . $details['email'] . ' <br />
<form method="post">
' . $error . '<br />
<label>Password</label> <input type="password" name="password" value="" /><br />
<label>Password Again</label> <input type="password" name="password_check" value="" /><br />
<input type="submit" name="updatepw" value="Create User" />
</form>';
require_once 'layout.phtml';
}
public function login() {
$error = null;
// Do the login
if(isset($_POST['login'])) {
$username = $_POST['user'];
$password = $_POST['pass'];
$password = md5($username . $password); // THIS IS NOT SECURE. DO NOT USE IN PRODUCTION.
$sql = 'SELECT * FROM user WHERE username = ? AND password = ? LIMIT 1';
$stmt = $this->db->prepare($sql);
$stmt->execute(array($username, $password));
if($stmt->rowCount() > 0) {
$data = $stmt->fetch(PDO::FETCH_ASSOC);
session_regenerate_id();
$_SESSION['username'] = $data['username'];
$_SESSION['AUTHENTICATED'] = true;
header("Location: /");
exit;
}
else {
$error = 'Your username/password did not match.';
}
}
$content = '
<form method="post">
' . $error . '<br />
<label>Username</label> <input type="text" name="user" value="" />
<label>Password</label> <input type="password" name="pass" value="" />
<input type="submit" name="login" value="Log In" />
</form>
';
require_once('layout.phtml');
}
public function logout() {
// Log out, redirect
session_destroy();
header("Location: /");
}
}