blob: b4dd6ad528308b3dcab036bfe9cc61d8f859fe4b (
plain)
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
|
<?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) Notify about email
*
* TODO: Set the source of the post.
*/
$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 'finish':
if (finish_images())
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
// TODO: Display ToS Here
form_start('post');
echo "<p><label><input type=\"checkbox\" name=\"tos\" value=\"1\" />"
." I agree to the terms of service.</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',
'description' => 'Description',
'email' => 'Email Address',
'email2' => 'Confirm Email Address',
);
$error = '';
$values = array();
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']);
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('finish');
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']) and is_array($_FILES['images'])) {
foreach ($_FILES['images'] as $file) {
$post->addImage($file['tmp_name']);
}
}
return true;
}
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="") {
global $values;
if ($error != '') {
echo "<div class=\"errorbox\">$error</div>";
}
echo "<p><label>Title: <input type=\"text\" name=\"title\" value=\"${_POST[title]}\" /></label></p>";
echo "<p><label for=\"desc\">Description:</label></p>";
echo "<p><textarea name=\"description\" id=\"desc\" rows=\"10\""
. " cols=\"80\">${_POST[description]}</textarea></p>";
echo "<p><label>Email Address: <input type=\"text\" name=\"email\" value=\"${_POST[email]}\" />"
. "</label>";
echo " <label>Confirm Email: <input type=\"text\" name=\"email2\" value=\"${_POST[email2]}\" />"
. "</label></p>"
. "<p>Your email address will only be visible to our moderators.</p>";
}
?>
|