-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
296 lines (227 loc) · 9.26 KB
/
index.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
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
<?php
error_reporting(E_ALL);
ini_set('display_errors','On');
if (!file_exists('config.php')) {
header("Location: install.php");
exit;
}
include_once "utils.php";
include_once "dudleyinclude.php";
session_start();
is_autologin();
$commenttext = "";
//$userid = logged_in_userid(1);
$mode = '';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
/* Adding or editing a comment */
$errorstr = NULL;
$commenttext = trim($_POST['commenttext']);
$sanitized = sanitize_comment($commenttext);
if (strlen($sanitized) < 1) $errorstr .= "<p>Not adding an empty comment.";
$stripid = $_POST['stripid'];
if (!isset($stripid) || (!preg_match('/^[0-9]+$/', $stripid))) $errorstr .= "<p>Strip ID is weird.";
$commentid = (isset($_POST['commentid']) ? $_POST['commentid'] : NULL);
if (isset($commentid) && (!preg_match('/^[0-9]+$/', $commentid))) $errorstr .= "<p>Comment ID is weird.";
$sql = "select approved from strip where stripid='".$stripid."' and approved=true";
$res = db_exec($sql);
$numrows = db_numrows($res);
if ($numrows != 1) $errorstr .= "<p>That strip is not available.";
if (!isset($_SESSION['userid'])) {
$tmpstr = nhquestion_validate($_POST);
if ($tmpstr) $errorstr .= $tmpstr;
if (isset($commentid)) $errorstr .= "<p>Anon cannot edit comments.";
}
if (!isset($errorstr)) {
if (isset($commentid)) {
if (!is_admin()) $extrasql = " and userid=".$_SESSION['userid'];
else $extrasql = "";
$sql = "update comment set commenttext='".db_escape_string($commenttext)."',edittime='NOW' where stripid=".$stripid." and commentid=".$commentid.$extrasql;
} else
if (isset($_SESSION['userid'])) {
$sql = "insert into comment (stripid,commenttime,userid,commenttext) values ('".$stripid."','NOW','".$_SESSION['userid']."','".db_escape_string($commenttext)."')";
} else {
$sql = "insert into comment (stripid,commenttime,userid,commenttext,anon) values ('".$stripid."','NOW',NULL,'".db_escape_string($commenttext)."','true')";
}
$res = db_exec($sql);
$commenttext = "";
} else $commenttext = stripslashes($_POST['commenttext']);
$show_strip_id = $stripid;
}
$do_login = NULL;
$anythat = "any";
$qact = $_SERVER['QUERY_STRING'];
if (strpos($qact, '&')) $qact = substr($qact, 0, strpos($qact, '&'));
if (strlen($qact) > 1 && preg_match('/=$/', $qact)) $qact = substr($qact, 0, -1);
if (preg_match('/login/i', $qact)) {
header('Location: '.$dudley_root_url.'login.php');
exit;
} else if (preg_match('/^[0-9]+$/', $qact)) {
$show_strip_id = $qact;
} else if (preg_match('/^today$/', $qact)) {
$find_strip_tstamp = time();
} else if (isset($_GET['editcomment'])) {
$commentid = trim($_GET['editcomment']);
if (preg_match('/^[0-9]+$/', $commentid)) {
if (!isset($_SESSION['userid'])) $errorstr .= "<p>Strange username / password combo or you are not logged in.";
else {
$sql = "select stripid,userid,commenttext from comment where commentid=".$commentid;
$res = db_exec($sql);
$numrows = db_numrows($res);
if ($numrows == 1) {
$dat = db_get_rowdata($res, 0);
if (($dat['userid'] == $_SESSION['userid']) || is_admin()) {
$commenttext = $dat['commenttext'];
$commenttext = preg_replace('/\\\\\'/', '\'', $commenttext);
$commenttext = preg_replace('/\\\\"/', '"', $commenttext);
$show_strip_id = $dat['stripid'];
$mode = 'editcomment';
} else $errorstr .= "<p>Not your comment. Not yours.";
} else $errorstr .= "<p>No such comment.";
}
}
} else if (isset($_GET['delcomment'])) {
$commentid = trim($_GET['delcomment']);
if (preg_match('/^[0-9]+$/', $commentid)) {
if (!isset($_SESSION['userid'])) $errorstr .= "<p>Strange username / password combo or you are not logged in.";
else if (!is_admin()) $errorstr .= "<p>Only admin can delete comments.";
else {
$sql = "select stripid from comment where commentid=".$commentid;
$res = db_exec($sql);
$numrows = db_numrows($res);
if ($numrows == 1) {
$dat = db_get_rowdata($res, 0);
$show_strip_id = $dat['stripid'];
$sql = "delete from comment where commentid=".$commentid;
$res = db_exec($sql);
$numrows = db_numrows($res);
} else $errorstr .= "<p>No such strip to delete?";
}
}
}
if (isset($_GET['f'])) {
if ($_GET['f'] == 'first') {
$tmp = db_get_strip_first();
if (isset($tmp['stripid'])) {
$show_strip_id = $tmp['stripid'];
}
} else if (preg_match('/^([0-9][0-9][0-9][0-9])[-.]?([0-9]?[0-9])[-.]?([0-9]?[0-9])$/', $_GET['f'], $match)) {
$find_strip_tstamp = mktime(0,0,0, $match[2], $match[3], $match[1]);
$anythat = 'that';
if ($find_strip_tstamp > time()) {
$find_strip_tstamp = time();
$errorstr .= "<p>You expect to see in to the future?";
}
}
}
if (!isset($find_strip_tstamp)) $find_strip_tstamp = time();
if (isset($show_strip_id)) {
$strip_data = db_get_strip_by_id($show_strip_id);
$anythat = "that";
} else {
$strip_data = db_get_strip($find_strip_tstamp);
if (!isset($strip_data['stripid'])) $strip_data = db_get_strip_prev($find_strip_tstamp);
if (isset($strip_data['stripid'])) $anythat = "that";
}
$title = strip_tags($dudley_comic_title);
if (isset($strip_data['epoch'])) $title .= " -- ".date("l, j F, Y", $strip_data['epoch']);
dud_html_top(array('css'=>$dudley_css_files,
'rss'=>'dudley.rss',
'title'=>$title));
print '<center>'."\n";
print '<h1>'.$dudley_comic_title.'</h1>'."\n";
print '<div class="dudley_strip">'."\n";
print '<div class="controls">';
$dudmenu = get_dudmenu();
print '<p>'.$dudmenu;
$pagecontrols = '';
if (!isset($strip_data['stripid'])) {
print '</div>';
if (isset($errorstr)) {
print '<div class="errorstr">'.$errorstr.'</div>'."\n";
}
print '<p><b>Weird, I can\'t find '.$anythat.' Dudley strip'.($anythat == 'any' ? 's' : '').' in the database.</b>'."\n";
print '<p>Perhaps you should <a href="diydudley.php">create and submit some</a>?'."\n";
} else {
if (!isset($str_data['stripdata'])) $strip_data = db_get_strip_by_id($strip_data['stripid']);
$lines = preg_split("/\n/", $strip_data['stripdata']);
$pagecontrols = get_pagecontrols($strip_data['epoch']);
print '</div>';
if (isset($errorstr)) {
print '<div class="errorstr">'.$errorstr.'</div>'."\n";
}
$strip = parse_comic_strip($lines);
$strip['epoch'] = $strip_data['epoch'];
print render_comic_strip($strip);
$news = get_news($strip_data['stripid']);
if (isset($news) && strlen($news['newstext']) > 0)
print '<div class="newstext"><h3>News</h3>'.$news['newstext'].'</div>'."\n";
if (isset($strip_data['viewed'])) {
$sql = "update strip set viewed=".($strip_data['viewed'] + 1)." where stripid=".$strip_data['stripid'];
} else {
$sql = "update strip set viewed=1 where stripid=".$strip_data['stripid'];
$strip_data['viewed'] = 1;
}
$res = db_exec($sql);
print '<div class="infotools">';
print '<ul>';
print '<li class="num_viewed">This strip has been viewed '.
$strip_data['viewed'].' time'.($strip_data['viewed'] > 1 ? 's': '').'</li>';
if (isset($_SESSION['userid'])) {
print '<li class="printlink"><a href="view.php?'.$strip_data['stripid'].'&print=1">Print</a></li>';
}
print '</ul>';
print '</div>';
print '<hr>';
print $pagecontrols;
print '<div class="columnwrapper">';
print '<div class="maincolumn">';
print ratings_bars($strip_data['stripid']);
print '</div>';
print '</div>';
print '<div class="rightcolumn">';
print get_monthtable($strip_data['epoch']);
print '</div>';
if (!isset($_SESSION['userid'])) {
print '<P>Please <a href="login.php">log in</a> or <a href="register.php">register</a> to rate this strip.';
}
show_comments($strip_data['stripid']);
print '<div id="comment">';
if ($mode == 'editcomment') print '<h3>Edit your comment</h3>';
else print '<h3>Comment the strip</h3>';
print '<small>See <a href="about.php">the about-page for allowed formatting</a>.</small>';
parse_str($_SERVER['QUERY_STRING'], $querystr);
unset ($querystr['editcomment']);
unset ($querystr['delcomment']);
$querystr = remove_null_keys($querystr);
print '<form method="POST" action="' . phpself_querystr($querystr) . ($mode == 'editcomment' ? '#comment_'.$commentid : '') .'">';
if (isset($_SESSION['userid'])) {
print 'Logged in as <b><em>'.$_SESSION['username'].'</em></b><br>';
} else {
print '<b>Not logged in.</b><br>';
}
print '<input type="hidden" name="stripid" value="'.$strip_data['stripid'].'">';
print '<textarea name="commenttext" onfocus="document.onkeydown=null;" onblur="document.onkeydown=handler;" cols="80" rows="10">'.htmlentities($commenttext).'</textarea>';
print '<br>';
if (!isset($_SESSION['userid'])) {
print nhquestion_inputbar();
print '<br>';
}
if ($mode == 'editcomment') {
print '<input type="hidden" name="commentid" value="'.$commentid.'">';
print '<input type="Submit" value="Update the comment">';
} else {
print '<input type="Submit" value="Submit the comment">';
}
print '</form>';
print '</div>';
}
print '</div>';
print '</center>';
print '<div id="bottomlinks">';
print $dudmenu;
print $pagecontrols;
print '</div>';
if (isset($_SESSION['s_keybnav']) && $_SESSION['s_keybnav'] == 'true') {
print get_keybhandler($strip_data['epoch']);
}
dud_html_bottom();