forked from ryanshoover/Gift-Registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.php
222 lines (198 loc) · 6.44 KB
/
event.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
require_once(dirname(__FILE__) . "/includes/funcLib.php");
require_once(dirname(__FILE__) . "/includes/MySmarty.class.php");
$smarty = new MySmarty();
$opt = $smarty->opt();
session_start();
if (!isset($_SESSION["userid"])) {
header("Location: " . getFullPath("login.php"));
exit;
}
else {
$userid = $_SESSION["userid"];
}
if (!empty($_GET["message"])) {
$message = $_GET["message"];
}
if (isset($_GET["eventid"])) {
$eventid = $_GET["eventid"];
}
// for security, let's make sure that if an eventid was passed in, it belongs
// to $userid (or is a system event and the user is an admin).
// all operations on this page should only be performed by the event's owner.
if (isset($eventid)) {
try {
$query = "SELECT * FROM {$opt["table_prefix"]}events WHERE eventid = ? AND ";
if ($_SESSION["admin"] == 1)
$query .= "(userid = ? OR userid IS NULL)";
else
$query .= "userid = ?";
$stmt = $smarty->dbh()->prepare($query);
$stmt->bindParam(1, $eventid, PDO::PARAM_INT);
$stmt->bindParam(2, $userid, PDO::PARAM_INT);
$stmt->execute();
if (!$stmt->fetch())
die("Nice try! (That's not your event.)");
}
catch (PDOException $e) {
die("sql exception: " . $e->getMessage());
}
}
$action = isset($_GET["action"]) ? $_GET["action"] : "";
if ($action == "insert" || $action == "update") {
/* validate the data. */
$description = trim($_GET["description"]);
try {
$eventdate = new DateTime($_GET["eventdate"]);
}
catch (Exception $e) {
$eventdate = FALSE;
}
$recurring = (strtoupper($_GET["recurring"]) == "ON" ? 1 : 0);
$systemevent = (strtoupper($_GET["systemevent"]) == "ON" ? 1 : 0);
$haserror = false;
if ($description == "") {
$haserror = true;
$description_error = "A description is required.";
}
if ($eventdate == FALSE) {
$haserror = true;
$eventdate_error = "Date is out of range for this server.";
}
}
if ($action == "delete") {
try {
$stmt = $smarty->dbh()->prepare("DELETE FROM {$opt["table_prefix"]}events WHERE eventid = ?");
$stmt->bindParam(1, $eventid, PDO::PARAM_INT);
$stmt->execute();
header("Location: " . getFullPath("event.php?message=Event+deleted."));
exit;
}
catch (PDOException $e) {
die("sql exception: " . $e->getMessage());
}
}
else if ($action == "edit") {
try {
$stmt = $smarty->dbh()->prepare("SELECT description, eventdate, recurring, userid FROM {$opt["table_prefix"]}events WHERE eventid = ?");
$stmt->bindParam(1, $eventid, PDO::PARAM_INT);
$stmt->execute();
// we know this will work, see above.
$row = $stmt->fetch();
$description = $row["description"];
$eventdate = new DateTime($row["eventdate"]);
$recurring = $row["recurring"];
$systemevent = ($row["userid"] == "");
}
catch (PDOException $e) {
die("sql exception: " . $e->getMessage());
}
}
else if ($action == "") {
$description = "";
$eventdate = new DateTime();
$recurring = 1;
$systemevent = 0;
}
else if ($action == "insert") {
if (!$haserror) {
try {
$stmt = $smarty->dbh()->prepare("INSERT INTO {$opt["table_prefix"]}events(userid,description,eventdate,recurring) VALUES(?, ?, ?, ?)");
$stmt->bindValue(1, $systemevent ? NULL : $userid, PDO::PARAM_BOOL);
$stmt->bindParam(2, $description, PDO::PARAM_STR);
$stmt->bindValue(3, $eventdate->format("Y-m-d"), PDO::PARAM_STR);
$stmt->bindParam(4, $recurring, PDO::PARAM_BOOL);
$stmt->execute();
header("Location: " . getFullPath("event.php?message=Event+added."));
exit;
}
catch (PDOException $e) {
die("sql exception: " . $e->getMessage());
}
}
}
else if ($action == "update") {
if (!$haserror) {
try {
$stmt = $smarty->dbh()->prepare("UPDATE {$opt["table_prefix"]}events SET " .
"userid = ?, " .
"description = ?, " .
"eventdate = ?, " .
"recurring = ? " .
"WHERE eventid = ?");
$stmt->bindValue(1, $systemevent ? NULL : $userid, PDO::PARAM_BOOL);
$stmt->bindParam(2, $description, PDO::PARAM_STR);
$stmt->bindValue(3, $eventdate->format("Y-m-d"), PDO::PARAM_STR);
$stmt->bindParam(4, $recurring, PDO::PARAM_BOOL);
$stmt->bindParam(5, $eventid, PDO::PARAM_INT);
$stmt->execute();
header("Location: " . getFullPath("event.php?message=Event+updated."));
exit;
}
catch (PDOException $e) {
die("sql exception: " . $e->getMessage());
}
}
}
else {
die("Unknown verb.");
}
try {
$query = "SELECT eventid, userid, description, eventdate, recurring " .
"FROM {$opt["table_prefix"]}events " .
"WHERE userid = ?";
if ($_SESSION["admin"] == 1)
$query .= " OR userid IS NULL"; // add in system events
$query .= " ORDER BY userid, eventdate";
$stmt = $smarty->dbh()->prepare($query);
$stmt->bindParam(1, $userid, PDO::PARAM_INT);
$stmt->execute();
$events = array();
while ($row = $stmt->fetch()) {
try {
$eventDateTime = new DateTime($row['eventdate']);
}
catch (Exception $e) {
die("There was an error with an event with datetime " . $row['eventdate']);
}
$row['eventdate'] = $eventDateTime->format($opt["date_format"]);
$events[] = $row;
}
if (isset($message)) {
$smarty->assign('message', $message);
}
$smarty->assign('action', $action);
$smarty->assign('haserror', $haserror);
$smarty->assign('events', $events);
$smarty->assign('eventdate', $eventdate->format($opt["date_format"]));
if (isset($eventdate_error)) {
$smarty->assign('eventdate_error', $eventdate_error);
}
$smarty->assign('description', $description);
if (isset($description_error)) {
$smarty->assign('description_error', $description_error);
}
$smarty->assign('recurring', $recurring);
$smarty->assign('systemevent', $systemevent);
if (isset($eventid)) {
$smarty->assign('eventid', $eventid);
}
$smarty->assign('userid', $userid);
$smarty->assign('protocol', effectiveProtocol());
$smarty->display('event.tpl');
}
catch (PDOException $e) {
die("sql exception: " . $e->getMessage());
}
?>