-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnews.php
424 lines (213 loc) · 8.33 KB
/
news.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<?php
require "include/bittorrent.php";
loggedinorreturn();
if (get_user_class() < UC_ADMINISTRATOR)
stderr("Error", "Permission denied.");
$action = $_GET["action"];
$cat = "<option value=mystatus>---None selected---</option>n";
$cat_r = mysql_query("SELECT * from newscats ORDER BY name") or die;
while ($cat_a = mysql_fetch_array($cat_r))
$cat .= "<option value=$cat_a[img]" . ($arr["img"] == $cat_a['img'] ? " selected" : "") . ">$cat_a[name]</option>\n";
// Delete News Item //////////////////////////////////////////////////////
if ($action == 'delete')
{
$newsid = $_GET["newsid"];
if (!is_valid_id($newsid))
stderr("Error","Invalid news item ID - Code 1.");
$returnto = $_GET["returnto"];
$sure = $_GET["sure"];
if (!$sure)
stderr("Delete news item","Do you really want to delete a news item? Click " .
"<a href=?action=delete&newsid=$newsid&returnto=$returnto&sure=1>here</a> if you are sure.",0);
mysql_query("DELETE FROM news WHERE id=$newsid") or sqlerr(__FILE__, __LINE__);
if ($returnto != "")
header("Location: $returnto");
else
$warning = "News item was deleted successfully.";
}
// Add News Item /////////////////////////////////////////////////////////
if ($action == 'add')
{
$body = $_POST["body"];
if (!$body)
stderr("Error","The news item cannot be empty!");
$title = $_POST['title'];
if (!$title)
stderr("Error","The news title cannot be empty!");
$added = $_POST["added"];
if (!$added)
$added = sqlesc(get_date_time());
$cat = $_POST["cat"];
if (!$cat)
stderr("Error","The news category cannot be empty!");
mysql_query("INSERT INTO news (userid, added, body, title, cat) VALUES (".
$CURUSER['id'] . ", $added, " . sqlesc($body) . ", " . sqlesc($title) . ", " . sqlesc($cat) . ")") or sqlerr(__FILE__, __LINE__);
if (mysql_affected_rows() == 1)
$warning = "News item was added successfully.";
else
stderr("Error","Something weird just happened.");
}
// Edit News Item ////////////////////////////////////////////////////////
if ($action == 'edit')
{
$newsid = $_GET["newsid"];
if (!is_valid_id($newsid))
stderr("Error","Invalid news item ID - Code 2.");
$res = mysql_query("SELECT * FROM news WHERE id=$newsid") or sqlerr(__FILE__, __LINE__);
if (mysql_num_rows($res) != 1)
stderr("Error", "No news item with ID $newsid.");
$arr = mysql_fetch_array($res);
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$body = $_POST['body'];
if ($body == "")
stderr("Error", "Body cannot be empty!");
$title = $_POST['title'];
if ($title == "")
stderr("Error", "Title cannot be empty!");
$cat = $_POST['cat'];
if ($cat == "")
stderr("Error", "Category cannot be empty!");
$body = sqlesc($body);
$editedat = sqlesc(get_date_time());
mysql_query("UPDATE news SET body=$body, title='$title', cat='$cat' WHERE id=$newsid") or sqlerr(__FILE__, __LINE__);
$returnto = $_POST['returnto'];
if ($returnto != "")
header("Location: $returnto");
else
$warning = "News item was edited successfully.";
}
else
{
$returnto = $_GET['returnto'];
stdhead();
$cat1 = mysql_fetch_assoc(mysql_query("SELECT cat FROM news WHERE id = '$newsid'"));
$cat1 = $cat1['cat'];
print("<h1>Edit News Item</h1>");
print("<form method=post action=?action=edit&newsid=$newsid>");
print("<table border=1 cellspacing=0 cellpadding=5>");
print("<input type=hidden name=returnto value=$returnto>");
print("<tr><td style='padding: 10px'>Title: <input type=text name=title value=" . htmlspecialchars($arr["title"]) . ">");
print("<br><img src=pic/spacer.gif width=100 height=1><br>");
$cat_r = mysql_query("SELECT * from newscats ORDER BY id") or die;
while ($cat_a = mysql_fetch_array($cat_r)):
$catsel .= "<option value='$cat_a[id]' ".($cat1 == $cat_a['id'] ? "SELECTED" : "").">$cat_a[name]</option>\n";
endwhile;
print("Cat: <select name=cat>$catsel</select><br>");
print("<br><img src=pic/spacer.gif width=100 height=1><br>");
print("<textarea name=body cols=145 rows=5 style='border: 0px'>" . htmlspecialchars($arr["body"]) . "</textarea>");
print("<p align=center><input type=submit value='Okay' class=btn></p>");
print("</td></tr></table>");
print("</form>");
stdfoot();
die;
}
}
// Other Actions and followup ////////////////////////////////////////////
stdhead("Site news");
?>
<div id="editcat" style="display:none;">
<?php
begin_frame("Edit Categories");
$r1 = mysql_query("SELECT * FROM newscats") or die(mysql_error());
while($r = mysql_fetch_assoc($r1)):
?>
<?="<a href=javascript:; onclick=\"$('#editcat$r[id]').show('slow');\">$r[name]</a>"?>
<div id="editcat<?=$r['id']?>" style="display:none;">
<form action="admin/editcat.php" method="post">
<input type='hidden' name='id' value='<?=$r['id'];?>' >
Name: <input type="text" name="name" id=name value="<?=$r['name'];?>"> <br>
Image(the image has to be placed in <?=$pic_base_url?>news directory: <input type="text" name="image" id=image value="<?=$r['img'];?>"> <br>
<input type="submit" value="Edit Category" /> [<a href=javascript:; onclick="$('#editcat<?=$r['id']?>').hide('slow');">Cancel edit</a>] [<a href=admin/deletencat.php?id=<?=$r['id']?>>Delete Category</a>]
</form>
</div><br>
<?php
endwhile;
end_frame();
?>
</div>
<?php
print("<h1>Submit News Item</h1>");
if ($warning)
print("<p><font size=-3>($warning)</font></p>");
?>
<div id="addcat" style="display:none;">
<?php
javascript('jquery.select');
begin_frame('Add News Category',1,'10','100%');
?>
<form id="AddCat" action="admin/addncat.php" method="post">
Name: <input type="text" name="name" id=name /> <br>
Image(the image has to be placed in <?=$pic_base_url?>news directory: <input type="text" name="image" id=image /> <br>
<input type="submit" value="Submit Category" />
</form>
<script type="text/javascript">
$(document).ready(function() {
$('#AddCat').ajaxForm(function() {
$('#addcat').hide('slow');
var name = jQuery.fieldValue($("#name")[0]);
var image = jQuery.fieldValue($("#image")[0]);
var myOptions = {
image : name
}
$("#cats").addOption(myOptions, true);
});
});
</script>
<a href=javascript:; onclick= "$('#addcat').hide('fast');">Cancel</a>
<?php
end_frame();
?>
</div><br><br>
<?php
print("<form method=post action=?action=add>");
print("<table border=1 cellspacing=0 cellpadding=5>");
print("<tr><td style='padding: 10px'>Title: <input type=text name=title><br>");
print("<br><img src=pic/spacer.gif width=100 height=1><br>");
$cat_r = mysql_query("SELECT * from newscats ORDER BY id") or die;
while ($cat_a = mysql_fetch_array($cat_r)):
$catsel .= "<option value='$cat_a[id]'>$cat_a[name]</option>\n";
endwhile;
print("Cat: <select name=cat id=cats>$catsel</select> [<a href=javascript:; onclick= \"$('#addcat').show('fast');\">Add Category</a>] [<a href=javascript:; onclick= \"$('#editcat').show('fast');\">Edit Categories</a>]<br>");
print("<br><img src=pic/spacer.gif width=100 height=1><br>");
print("<textarea name=body cols=141 rows=5 style='border: 0px'></textarea>");
print("<br><br><div align=center><input type=submit value='Okay' class=btn></div></td></tr>");
print("</table></form><br><br>");
$res = mysql_query("SELECT * FROM news ORDER BY added DESC") or sqlerr(__FILE__, __LINE__);
if (mysql_num_rows($res) > 0)
{
begin_main_frame();
begin_frame();
while ($arr = mysql_fetch_array($res))
{
$newsid = $arr["id"];
$body = $arr["body"];
$title = $arr["title"];
$cat = $arr["cat"];
$userid = $arr["userid"];
$added = $arr["added"] . " GMT (" . (get_elapsed_time(sql_timestamp_to_unix_timestamp($arr["added"]))) . " ago)";
$res2 = mysql_query("SELECT username, donor FROM users WHERE id = $userid") or sqlerr(__FILE__, __LINE__);
$arr2 = mysql_fetch_array($res2);
$postername = $arr2["username"];
if ($postername == "")
$by = "unknown[$userid]";
else
$by = "<a href=userdetails.php?id=$userid><b>$postername</b></a>" .
($arr2["donor"] == "yes" ? "<img src=pic/star.png alt='Donor'>" : "");
print("<p class=sub><table border=0 cellspacing=0 cellpadding=0><tr><td class=embedded>");
print("$added --- by $by");
print(" - [<a href=?action=edit&newsid=$newsid><b>Edit</b></a>]");
print(" - [<a href=?action=delete&newsid=$newsid><b>Delete</b></a>]");
print("</td></tr></table></p>");
begin_table(true);
print("<tr valign=top><td class=comment><b>$title</b><br>$body</td></tr>");
end_table();
}
end_frame();
end_main_frame();
}
else
stdmsg("Sorry", "No news available!");
stdfoot();
die;
?>