-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.php
100 lines (80 loc) · 2.73 KB
/
users.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
<?php
require('objects.php');
require('database.php');
session_start();
if (!isset($_GET['action']) && isset($_GET['id'])){
$user = new User(loadUser($db,$_GET['id']));
//$user->display();
$userHtml = $user->display();
} elseif ($_GET['action'] == 'create'){
//create a new user if url specifies create action
//get new user info from post
//TODO sanitize user information and make sure it is complete
//TODO check that email is valid
$newUser = $_POST;
//add file info for user profile picture
$newUser['userimage'] = $_FILES['userimage']['name'];
createUser($newUser,$_FILES['userimage'],$db);
} elseif ($_GET['action'] == 'edit'){
$user = new User($_SESSION['userData']);
$viewedUser = new User(loadUser($db,$_GET['id']));
echo $user->isAdmin();
echo $user->getID();
if ($_GET['id'] == $user->getID()){
echo 'can edit this entry';
$userHtml = $viewedUser->displayEditable();
} else {
echo 'cannot edit this entry';
$userHtml = $viewedUser->display();
}
} elseif ($_GET['action'] == 'update'){
}
function createUser($user,$userImage,$db){
$query = 'INSERT INTO users VALUES (null, :email , :password , :fName, :lName, :imageName, :admin) ';
try {
$results = $db->prepare($query);
$results->execute(array(':email'=>$user['email'] ,
':password'=>password_hash($user['password'], PASSWORD_DEFAULT),
':fName'=>$user['fname'],
':lName'=>$user['lname'],
':imageName'=>$user['userimage'],
':admin'=>0));
} catch (Exception $e){
echo $e->getMessage();
exit();
}
$filename = "./img/" . $userImage['name'];
move_uploaded_file($userImage['tmp_name'], $filename);
//once account is created, log the user in.
// this should log the user in and make user data accessible to other functions
//login the user by setting session loggedIn variable and userData
session_start();
$user = login($db,$user['email'], $user['password']);
if ($user !== false) {
$_SESSION['userData'] = $user;
$_SESSION['loggedIn'] = true;
} else {
echo 'Login attempt failed';
}
header("Location: index.php");
exit();
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Simple test blog</title>
<meta name="description" content="A very simple blog">
<meta name="author" content="Thomas Machin">
<link rel="stylesheet" href="style.css">
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<?php
echo $userHtml;
?>
</body>
</html>