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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
<?php
/* Foursquare Community Site
*
* Copyright (C) 2011 Foursquare Church.
*
* Developers: Jesse Morgan <jmorgan@foursquarestaff.com>
*
*/
require_once "src/base.inc.php";
require_once "src/header.inc.php";
const MAX_IMAGE_UPLOADS = 4;
echo "<h2>New Posting</h2>";
/*
* Stages:
* 1) Pick a category
* 2) ToS
* 3) Title, Desc, Location, email
* 4) Images
* 5) Source
* 6) Notify about email
*
*/
$stage = 'category';
if (isset($_GET['stage'])) {
$stage = trim($_GET['stage']);
}
if (!isset($_SESSION['newpost'])) {
$stage = 'category';
}
switch ($stage) {
case 'category':
handle_category();
break;
case 'tos':
if (finish_category())
handle_tos();
break;
case 'post':
if (finish_tos())
handle_post();
break;
case 'images':
if (finish_post())
handle_images();
break;
case 'source':
if (finish_images())
handle_source();
break;
case 'finish':
if (finish_source())
handle_finish();
break;
default:
// Category
handle_category();
}
/* Stage Handlers */
function handle_category() {
$_SESSION['newpost'] = new Post();
// Display instructions
echo "<p>Start by choosing a category from the list below</p>";
// List Categories
foreach (Category::getCategories() as $short => $name) {
echo "<p><a href=\"". $GLOBALS['CONFIG']['urlroot']
. "/new-post.php?stage=tos&category=$short\">$name</a></p>";
}
}
function finish_category() {
$post = $_SESSION['newpost'];
if (isset($_GET['category'])) {
$category = Category::getByShortname(addslashes($_GET['category']));
if ($category) {
$post->setCategory($category->getId());
return true;
}
}
handle_category();
return false;
}
function handle_tos() {
// Display ToS
form_start('post');
echo "<p><label><input type=\"checkbox\" name=\"tos\" value=\"1\" />"
." I agree to the <a href=\"". buildUrl('page/tou.html')
."\">terms of service</a>.</label></p>";
form_end();
}
function finish_tos() {
if (isset($_POST['tos']) and $_POST['tos'] == 1) {
return true;
} else {
header('Location: ' . $GLOBALS['CONFIG']['urlroot']);
exit;
}
}
function handle_post($error='') {
// Display Form
form_start('images');
render_form($error);
form_end();
}
function finish_post() {
$post = $_SESSION['newpost'];
$required = array(
'title' => 'Title',
'location' => 'Location',
'description' => 'Description',
'email' => 'Email Address',
'email2' => 'Confirm Email Address',
);
$error = '';
$values = array('title' => '', 'description' => '',
'email' => '', 'email2' => '');
foreach ($required as $field => $desc) {
if (!isset($_POST[$field]) or trim($_POST[$field]) == '') {
$error .= "<p>$desc is a required field.</p>";
} else {
$values[$field] = trim($_POST[$field]);
}
}
if ($values['email'] != $values['email2']) {
$error .= "<p>Email addresses must match.</p>";
}
if ($error == '') {
$post->setEmail($values['email']);
$post->setName($values['title']);
$post->setDescription($values['description']);
$post->setLocation($values['location']);
if ($post->save()) {
return true;
} else {
$error .= 'An internal error has occured.';
}
}
handle_post($error);
return false;
}
function handle_images() {
$post = $_SESSION['newpost'];
// Display image form
echo "<p>You may upload up to four images with your post.</p>";
form_start('source');
for ($i = 1; $i <= MAX_IMAGE_UPLOADS; $i++) {
echo "<p><label>Image $i: "
. "<input type=\"file\" name=\"images[]\" /></label></p>";
}
form_end();
}
function finish_images() {
$post = $_SESSION['newpost'];
if (isset($_FILES['images'])) {
foreach ($_FILES["images"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$post->addImage($_FILES['images']['tmp_name'][$key]);
}
}
}
return true;
}
function handle_source($error='') {
form_start('finish');
if ($error != '') {
echo "<div class=\"errorbox\">$error</div>";
}
echo "<p>Where did you hear about this site?</p>";
echo "<select name=\"source\">";
foreach(Source::getSources() as $source) {
echo "<option value=\"". $source->getId() ."\">"
. $source->getName() ."</option>";
}
echo "<option value=\"0\">Other</option>";
echo "</select>";
echo "<p>If you selecte other, please explain:";
echo " <input type=\"text\" name=\"explain\" /></p>";
form_end();
}
function finish_source() {
$error = '<p>This question is required.</p>';
$post = $_SESSION['newpost'];
if (isset($_POST['source']) and is_numeric($_POST['source'])) {
if ($_POST['source'] == 0) {
if (isset($_POST['explain']) and trim($_POST['explain']) != '') {
$post->otherSource($_POST['explain']);
$post->save();
return true;
} else {
$error = '<p>Explaination is a required field.</p>';
}
} else {
$source = Source::getById($_POST['source']);
if ($source) {
$post->setSource($_POST['source']);
$post->save();
return true;
}
}
}
handle_source($error);
return false;
}
function handle_finish() {
$post = $_SESSION['newpost'];
// Send validation email.
$post->sendValidation();
// Display confirmation message
echo "<p>Your posting is almost complete. You must verify your email address by visiting the link we have emailed you, then your posting will be reviewed by our moderation team.</p>";
}
function form_start($stage) {
echo "<form action=\"". $GLOBALS['CONFIG']['urlroot'] ."/new-post.php?stage=$stage\""
." method=\"post\" enctype=\"multipart/form-data\">";
}
function form_end() {
echo "<p><input type=\"submit\" value=\"Next >\" /></p></form>";
}
require_once "src/footer.inc.php";
function render_form($error="") {
$title = isset($_POST['title']) ? $_POST['title'] : '';
$location = isset($_POST['location']) ? $_POST['location'] : '';
$description = isset($_POST['description']) ? $_POST['description'] : '';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$email2 = isset($_POST['email2']) ? $_POST['email2'] : '';
if ($error != '') {
echo "<div class=\"errorbox\">$error</div>";
}
echo "<p><label>Title: <input type=\"text\" name=\"title\" value=\"$title\" /></label></p>";
echo "<p><label>Location: <input type=\"text\" name=\"location\" value=\"$location\" /></label></p>";
echo "<p><label for=\"desc\">Description:</label></p>";
echo "<p><textarea name=\"description\" id=\"desc\" rows=\"10\""
. " cols=\"80\">$description</textarea></p>";
echo "<p><label>Email Address: <input type=\"text\" name=\"email\" value=\"$email\" />"
. "</label>";
echo " <label>Confirm Email: <input type=\"text\" name=\"email2\" value=\"$email2\" />"
. "</label></p>"
. "<p>Your email address will only be visible to our moderators.</p>";
}
?>
|