-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.php
More file actions
177 lines (156 loc) · 6.74 KB
/
Copy pathcontroller.php
File metadata and controls
177 lines (156 loc) · 6.74 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
session_start();
include_once('model.php');
//if no command, default to opening mainpage if already signed in, else go to startpage
if (empty($_POST['command']) && empty($_GET['command'])){
if (isset($_SESSION['signedin']))
include('mainpage.php');
else{
include('startpage.php');
}
exit();
}
//to allow commands received via get or post
if (isset($_POST['command']))
$command = $_POST['command'];
if (isset($_GET['command']))
$command = $_GET['command'];
//commands for when user is not signed in
if (!isset($_SESSION['signedin'])){
switch($command){
// open sign in page
case 'signinpage':
include('signin.php');
exit();
// open register page
case 'registerpage':
include('register.php');
exit();
// ajax to check name availability on register page
case 'namecheck':
$username = $_GET['username'];
if (checkDuplicateUser($username))
echo("Unavailable");
else
echo("Available");
exit();
// create new user
case 'register':
$username = $_POST['username'];
$password = $_POST['password'];
createUser($username, $password);
include('signin.php');
exit();
// sign in check
case 'signin':
$username = $_POST['username'];
$password = $_POST['password'];
if (checkValidity($username, $password)){
$_SESSION['signedin'] = 'YES';
$_SESSION['username'] = $username;
include('mainpage.php');
}
else{
include('signin.php');
echo '<script type = "text/javascript">'
,'$("#signform p").html(" incorrect username/password");'
,'$("#signform p").css("color", "red")'
,'</script>';
}
exit();
}
}
// these commands are only accessible if user is signed in
else{
switch($command){
// open user's auctions
case 'myauctions':
include('userauctions.php');
exit();
// command for creating new auction
case 'newauction':
$image_location = "";
// Check if image is uploaded by user
if (file_exists($_FILES['fileselect']['tmp_name']) && is_uploaded_file($_FILES['fileselect']['tmp_name'])){
$target_dir = "uploaded_images/"; // directory to hold uploaded images
$target_file = $target_dir . basename($_FILES['fileselect']['name']);
$id = 0;
// Check if there are duplicate names. Will append incrementing integer values to file name until
// duplicate name is no longer found
while (file_exists($target_file)){
$temp = explode(".", $_FILES["fileselect"]["name"]);
$target_file = $target_dir . $temp[0] . ++$id . "." . $temp[1];
}
move_uploaded_file($_FILES['fileselect']['tmp_name'], $target_file);
$image_location = $target_file;
}
$user = $_SESSION['username'];
$title = $_POST['title'];
$price = $_POST['reserveprice'];
$category = $_POST['category'];
$description = $_POST['description'];
$datetime = date("Y-m-d H:i:s", time());
//calcuate auction time by adding the specifed hours to the current time
$hours = $_POST['auctionhours'];
$time = new DateTime($datetime);
$time->modify("+$hours hours");
$auctiontime = $time->format("Y-m-d H:i:s");
newAuction($title, $price, $datetime, $auctiontime, $user, $image_location, $description, $category);
header("Location: controller.php?command=myauctions");
exit();
//command to get user's auctions
case 'myauctionsdata':
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$user = $_SESSION['username'];
echo(getUserAuctions($user));
exit();
//command to get auctions where user placed a bid
case 'myauctionsbid':
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$user = $_SESSION['username'];
echo(getUserBidAuctions($user));
closeConn();
exit();
//command to delete auction
case 'deleteauction':
deleteAuction($_GET['id']);
header("Location: controller.php?command=myauctions");
exit();
//command to displayed individual auctions
case 'auctionpage':
$auction = getAuction($_GET['id']);
$id = $auction['id'];
$title = $auction['title'];
$image = $auction['image'];
$description = $auction['description'];
$timeleft = $auction['auctiontime'];
$price = $auction['price'];
include('auctionpage.php');
exit();
//command to place bid on auction
case 'auctionbid':
$aid = $_GET['aid'];
$user = $_SESSION['username'];
$amount = $_GET['bid'];
insertBid($aid, $user, $amount);
header("Location: controller.php?command=auctionpage&id=" . $aid);
exit();
//get bids to be displayed in the my auctions page
case 'getbids':
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$aid = $_GET['aid'];
echo(getBids($aid));
exit();
//home page to display all auctions
case 'allauctionsdata':
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
error_log($_GET['category']);
echo(getAllAuctions($_GET['category']));
exit();
}
}
?>