-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathindex.php
More file actions
637 lines (542 loc) · 22 KB
/
index.php
File metadata and controls
637 lines (542 loc) · 22 KB
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
<?php
// Set timezone to UTC
date_default_timezone_set('UTC');
// Deals with the annoying problem of 'get_magic_quotes_gpc' in some shared hosting
// Source: http://stackoverflow.com/questions/517008/how-to-turn-off-magic-quotes-on-shared-hosting
/*
// This appeared to not escape _POST properly (e.g. it also escape \r\n. where /r/n is how linux/UNIX sees it...
// this means magicquote won't touch \r\n as its not "/", but strip slash would. Which is a recipe for trouble)
if (get_magic_quotes_gpc() === 1)
{
$_GET = json_decode(stripslashes(json_encode($_GET, JSON_HEX_APOS)), true);
$_POST = json_decode(stripslashes(json_encode($_POST, JSON_HEX_APOS)), true);
$_COOKIE = json_decode(stripslashes(json_encode($_COOKIE, JSON_HEX_APOS)), true);
$_REQUEST = json_decode(stripslashes(json_encode($_REQUEST, JSON_HEX_APOS)), true);
}
*/
// This one appears to be workable? (Honestly... just disable magic_quotes_gpc )
if ( in_array( strtolower( ini_get( 'magic_quotes_gpc' ) ), array( '1', 'on' ) ) )
{
$_POST = array_map( 'stripslashes', $_POST );
$_GET = array_map( 'stripslashes', $_GET );
$_COOKIE = array_map( 'stripslashes', $_COOKIE );
}
// session system to help store not yet approved 'files'
// or images, while capcha is being processed.
ini_set("session.use_cookies",0);
ini_set("session.use_only_cookies",0);
//ini_set("session.use_trans_sid",1);
session_start();
//Initialize required files
require_once("settings.php");
require_once("LayoutEngine.php");
require_once("Database.php");
require_once("Taskboard.php");
require_once("anonregkit.php");
// For Rendering documents
require_once("./phpmarkdown/markdown.php");
require_once("./htmlpurifier/library/HTMLPurifier.auto.php");
//require("./asciicaptcha/asciicaptcha.php");
//Open up the database connection
Database::openDatabase('rw', $config['database']['dsn'], $config['database']['username'], $config['database']['password']);
//Get the desired page
$uri = isset($_GET['q']) ? $_GET['q'] : '/';
$uri_parts = explode('/', trim($uri, '/'));
//Create our Taskboard object
$board = new Taskboard();
$board->task_lifespan = $config['tasks']['lifespan'];
//Determine our task
switch($uri_parts[0]){
/*
* Test stuff
*/
case 'init':
// Insert test data if requested..
// activate by typing ?q=/init
if (!$__initEnable) {echo 'Permission Denied. init command disabled';exit;}
$board->initDatabase();
if($__debug)$board->createTask('23r34r', 'My first', 'This could be my second.. but blahh', array('first', 'misc'));
if($__debug)$board->createTask('23r34r', 'Poster needed', 'I kinda need a poster making, gotta be x y z', array('graphics', 'first'));
if($__debug)$board->createTask('23r34r', 'Make me music!', 'Please.. I need music', array('music', 'misc'));
if($__debug)$board->createTask('23r34r', 'something', 'something something something something something', array('misc'));
if($__debug)$board->createTask('23r34r', 'website time', 'Website called google.com, itl be a search engine', array('graphics', 'technical'));
if($__debug)echo "Inserted test data\n";
$board->createTask('Anonymous', 'Welcome To TaskBoard', 'This is the first post of TaskBoard, now try out search and submit function!', array('firstpost'));
break;
/*
* Task-related stuff
*/
case 'tasks':
//Check if we want a task
if (isset($uri_parts[1])) {
switch($uri_parts[1]){
/*
* Make a new task
*/
case 'new':
$mode = array('submitForm');
/*
Response to, or clone of a post.
*/
if( isset($_POST['respondtaskid']) ) {
$responding_taskid =$_POST['respondtaskid'];
}else if ( isset($_GET['respondtaskid']) ) {
$responding_taskid = $_GET['respondtaskid'];
}else{
$responding_taskid = "";
}
if( $responding_taskid != "" ){
if(!is_numeric($responding_taskid)){Echo "YOU FAIL";exit;}
//Retrieve the task and get its comments
$responding_to_task = $board->getTaskByID($responding_taskid);
$responding_to_task = $responding_to_task[0];
} else {
$responding_to_task = null;
}
break;
/*
* Submit and process the new task
*/
case 'submitnew':
/*
Grab the latest photos and insert into $imageFileBinary
*/
$imageFileBinary = __getImageFile();
if ($imageFileBinary == NULL) {
if (empty($_SESSION['imageFileBinary'])) {
$_SESSION['imageFileBinary'] = NULL;
}
$imageFileBinary = $_SESSION['imageFileBinary'];
} else {
$_SESSION['imageFileBinary'] = $imageFileBinary;
}
/*
Grab the latest keyfile and insert into $keyFileBinary
*/
$keyFileBinary = __getKeyFile();
if ($keyFileBinary == NULL) {
if (empty($_SESSION['keyFileBinary'])) {
$_SESSION['keyFileBinary'] = NULL;
}
$keyFileBinary = $_SESSION['keyFileBinary'];
} else {
$_SESSION['keyFileBinary'] = $keyFileBinary;
}
//Only pass though message and title if it is set already
if(!isset($_POST['title'], $_POST['message']) || empty($_POST['title']) || empty($_POST['message'])){
echo "<b>Missing title and/or message </b> \n";
$missingfield = true;
} else {
$missingfield = false;
}
// check if message is up to scratch (is not stupid, and does not have spammy words)
if( ! __postGateKeeper($_POST['message']) ){
echo "Your post was rejected by the gatekeeper. Did you make your message too small?
Does it have too many mispelling? Or was it just plain stupid? \n";
exit;
};
// Also it must pass the capcha test
if( isset($_POST['security_code'])) {
$first = false;
} else {
$_POST['security_code'] = "";
$_SESSION['security_code'] = "";
$first = true;
}
if( ($missingfield == false) && ($_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] )) ) {
echo 'Your captcha code was valid.';
unset($_SESSION['security_code']);
} else {
if ($first){
echo 'Please enter the captcha code to confirm your human status';
}else{
echo 'Sorry, you have provided an invalid security code';
}
?>
<br/>
<br/>
Modify Text:
<FORM action='?<?php echo htmlspecialchars(SID); ?>&q=/tasks/submitnew' method='post' >
Title*:<BR> <INPUT type='text' name='title'value='<?php echo $_POST['title'];?>'><BR>
Message*:<br /> <textarea class='' rows=5 name='message'><?php echo $_POST['message'];?></textarea><BR>
Tags:<BR><INPUT type='text' name='tags' value='<?php echo $_POST['tags'];?>'><BR>
<input type="hidden" name="taskID" value="<?php //echo $_POST['taskID']; ?>"><br/>
<INPUT type='hidden' name='keyfile' />
<INPUT type='hidden' name='respondid' value="<?php echo $_POST['respondid'];?>" />
<INPUT type='hidden' name='password' value="<?php echo $_POST['password'];?>" >
<b>CAPTCHA:</b>
<img src="./captcha/CaptchaSecurityImages.php?<?php echo htmlspecialchars(SID); ?>&width=100&height=40&characters=5" /><br />
<label for="security_code">Security Code: </label><input id="security_code" name="security_code" type="text" /><br />
<br />
<input type="submit" value="Submit" />
</form>
<?php
exit;
}
/*
Extract tag to array
*/
//preg_replace('/[^a-zA-Z0-9\s]/', '', $text) - Removes nonalphanumeric char
$s_tag = isset($_POST['tags']) ? preg_replace('/[^a-zA-Z0-9\s]/', '', $_POST['tags']) : "";
// turn it into an array
$s_tag_array_1 = explode(' ', $s_tag);
//also extract any hashtags from the message itself
$hashtagmatch = preg_match_all( '/#(\w+)/', $_POST['title']." ".$_POST['message'] , $pregmatch);
if($hashtagmatch){
$s_tag_array_2 = $pregmatch[1];
}else{
$s_tag_array_2 = array();
}
//merge s_tag_array_1 and s_tag_array_2 to s_tag_array
$s_tag_array = array_merge( $s_tag_array_1 , $s_tag_array_2 );
$s_tag_array = array_unique( $s_tag_array );
//Insert password
if( ( isset($_POST['password']) AND $_POST['password']!='' ) OR $keyFileBinary!=NULL){
$s_pass=__tripCode($_POST['password'].$keyFileBinary);
}else{// If user give blank password, generate a new one for them
//$newpass = md5(mt_rand());
if($__hiddenServer){
$newpass = substr(md5(rand()),0,6);
} else {
$newpass = substr(md5($_SERVER['REMOTE_ADDR']),0,6);
}
$s_pass=__tripCode($newpass);
echo "<div style='z-index:100;background-color:white;color:black;'>Your new password is: '<bold>".$newpass."</bold>' keep it safe! </div>";
echo __prettyTripFormatter($s_pass);
}
$newTaskID = $board->createTask($s_pass, $_POST['title'], $_POST['message'], $s_tag_array, $_POST['respondid'], $imageFileBinary);
echo "Post submitted!<br/>";
echo "Tags:".implode(" ",$s_tag_array)."<br/>";
echo "<a href='?q=/view/".$newTaskID."'>Click to go to your new task</a>";
echo "<meta http-equiv='refresh' content='10; url=?q=/view/".$newTaskID."'> Refreshing in 10 sec<br/>";
exit;
break;
/*
* Submit and process the new Comment
*/
case 'comment':
/*
Grab the latest keyfile and insert into $keyFileBinary
*/
$keyFileBinary = __getKeyFile();
if ($keyFileBinary == NULL) {
if (empty($_SESSION['keyFileBinary'])) {
$_SESSION['keyFileBinary'] = NULL;
}
$keyFileBinary = $_SESSION['keyFileBinary'];
} else {
$_SESSION['keyFileBinary'] = $keyFileBinary;
}
//Only pass though message and title if it is set already
if(!isset( $_POST['comment']) || empty($_POST['comment'])){
echo "Missing comment \n";
echo "<a href='?q=/view/".$uri_parts[2]."'>Click to go back</a>";
exit;
break;
}
// Also it must pass the capcha test
if( isset($_POST['security_code'])) {
$first = false;
} else {
$_POST['security_code'] = "";
$_SESSION['security_code'] = "";
$first = true;
}
if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) {
echo 'Your captcha code was valid.';
unset($_SESSION['security_code']);
} else {
if ($first){
echo 'Please enter the captcha code to confirm your human status';
}else{
echo 'Sorry, you have provided an invalid security code';
}
?>
<br/>
<br/>
Modify Text:
<form name="add_comment" action="?<?php echo htmlspecialchars(SID); ?>&q=/tasks/comment/<?php echo $_POST['taskID']; ?>" method="post" >
<textarea id="comment" name="comment"><?php echo $_POST['comment'];?></textarea>
<input type="hidden" name="taskID" value="<?php echo $_POST['taskID']; ?>"><br/>
<INPUT type='hidden' name='keyfile' />
<INPUT type='hidden' name='password' value="<?php echo $_POST['password'];?>" >
<b>CAPTCHA:</b>
<img src="./captcha/CaptchaSecurityImages.php?<?php echo htmlspecialchars(SID); ?>&width=100&height=40&characters=5" /><br />
<label for="security_code">Security Code: </label><input id="security_code" name="security_code" type="text" /><br />
<br />
<input type="submit" value="Submit" />
</form>
<?php
exit;
}
// check if message is up to scratch (is not stupid, and does not have spammy words)
if( ! __postGateKeeper($_POST['comment']) ){
echo "Your post was rejected by the gatekeeper. Did you make your post too small?
Does it have too many mispelling? Or was it just plain stupid? \n";
exit;
};
//Insert password
if( ( isset($_POST['password']) AND $_POST['password']!='' ) OR $keyFileBinary!=NULL){
$s_pass=__tripCode($_POST['password'].$keyFileBinary);
echo "<meta http-equiv='refresh' content='3; url=?q=/view/".$uri_parts[2]."'> Refreshing in 3 sec";
}else{
// If user give blank password, generate a new one for them
//$newpass = md5(mt_rand());
if($__hiddenServer){
$newpass = substr(md5(rand()),0,6);
} else {
$newpass = substr(md5($_SERVER['REMOTE_ADDR']),0,6);
}
$s_pass=__tripCode($newpass);
echo "<div style='z-index:100;background-color:white;color:black;'>Your new password is: '<bold>".$newpass."</bold>' keep it safe! </div>";
echo __prettyTripFormatter($s_pass);
}
$board->createComment($s_pass, $uri_parts[2], $replyID=NULL, $_POST['comment'], 1);
echo "Post submitted!\n";
echo "<a href='?q=/view/".$uri_parts[2]."'>Click to go back</a>";
echo "<meta http-equiv='refresh' content='5; url=?q=/view/".$uri_parts[2]."'> Refreshing in 5 sec<br/>";
exit;
break;
/*
* Search for a task
*/
case 'search':
// If we're posting a search, redirect to the URL search (helps copy/pasting URLs)
if(isset($_POST['tags'])){
$tags_string = isset($_POST['tags']) ? preg_replace('/[^a-zA-Z0-9\s]/', '', $_POST['tags']) : "";
$tags = explode(' ', $tags_string);
header('Location: ?q=/tasks/search/'.implode(',', $tags));
//echo 'tags'.implode(',', $tags);
exit;
}
if(isset($uri_parts[2])){
$tags = explode(',', $uri_parts[2]);
$mode = array('tasksList');
} else {
$mode = array('tagSearch');
}
if(!empty($tags)){
$tasks = $board->getTasks($tags);
} else {
$tasks = array();
}
break;
/*
* Delete a task
*/
case 'delete':
$pass = $_POST['password'].__getKeyFile();
if ($pass == ""){
$pass = substr(md5($_SERVER['REMOTE_ADDR']),0,6);
}
if(!is_numeric($_POST['taskID'])){Echo "YOU FAIL";exit;}
$s_array[0]=$_POST['taskID'];
$s_array[1]=__tripCode($pass);
/*
Moderator delete
*/
if (array_key_exists($s_array[1],$__superModeratorByTrip)){
$command = 'Delete a post';
$board->delTaskBy($command,$s_array);
break;
}
/*
//normal password delete
*/
var_dump($s_array);
//print_r($s_array);
$command = 'Delete single task with normal password';
$board->delTaskBy($command,$s_array);
break;
}
}
break;
/*
* Get Image from a task and print it out to user.
*/
case 'image':
$taskid =$uri_parts[1];
if(!is_numeric($uri_parts[1])){Echo "YOU FAIL";exit;}
// support thumbnails
if(isset($_GET['mode'])){
if($_GET['mode']== 'thumbnail'){
$tasks = $board->getTaskFileByID($taskid,'thumbnail');
}
}
//Retrieve the image and display it
$tasks = $board->getTaskFileByID($taskid,'image');
break;
/*
* Stuff relating to browsing and searching tasks
basically we view specific task here
*/
case 'view':
$mode = array('tasksView');
$taskid =$uri_parts[1];
if(!is_numeric($uri_parts[1])){Echo "YOU FAIL";exit;}
//Retrieve the task and get its comments
$tasks = $board->getTaskByID($taskid);
$tagsused = $board->tagsByID($taskid);
$comments = $board->getCommentsByTaskId($taskid);
break;
/*
* Stuff relating to displaying the 'task' as an A4 printable page.
basically we view specific task here
*/
case 'printview':
$mode = array('tasksView');
$taskid =$uri_parts[1];
if(!is_numeric($uri_parts[1])){Echo "YOU FAIL";exit;}
//Retrieve the task and get its comments
$tasks = $board->getTaskByID($taskid);
//Load the layout
require("printlayout.php");
exit;
break;
case 'ajaxcomments':
/*Ajax Update Commands go here*/
$mode = array('tasksView');
$taskid = $_POST['taskid'];
//Retrieve latest comment
$comments = $board->getCommentsByTaskId($taskid);
echo __commentDisplay($comments);
exit;
break;
case 'ajaxtasks':
/*Ajax Update Commands go here*/
$mode = array('tasksList');
$tags = explode(',', $_POST['tags']);
//Retrieve latest comment
$tasks = $board->getTasks($tags);
//referral tag for the 'clone' task feature
if (!empty($tags)){
$referraltag = $tags[0];
} else {
$referraltag = "";
}
echo __taskDisplay($tasks,$referraltag);
exit;
break;
case 'embed':
if (isset($_GET['tags'])){
$tags = explode(',', $_GET['tags']);
}else{
$tags = array();
}
if (isset($uri_parts[1])){
$tags = array_merge( explode(',', $uri_parts[1]) , $tags);
}
//Retrieve latest comment
$tasks = $board->getTasks($tags);
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="css/embed.css" type="text/css" />
</head>
<body>
<div id="newTask" class="greybox">
<?php if (!empty($tags)){?>
<a target="_blank" href="?q=/tasks/new&tag=<?php echo $tags[0];?>">Post New</a>
<?php } else {?>
<a target="_blank" href="?q=/tasks/new">Post New</a>
<?php }?>
</div>
<div id="taskDIV" class="tasklist">
<?php
echo __taskDisplay($tasks);
?>
</div>
</body>
<?php
exit;
break;
case 'rss':
if (isset($_GET['tags'])){
$tags = explode(',', $_GET['tags']);
}else{
$tags = array();
}
//XML headers
$rssfeed = '<?xml version="1.0" encoding="ISO-8859-1"?>';
$rssfeed .= "\n";
$rssfeed .= '<rss version="2.0">';
$rssfeed .= "\n";
$rssfeed .= '<channel>';
$rssfeed .= "\n";
$rssfeed .= '<title>TaskBoard</title>';
//$rssfeed .= "\n";
//$rssfeed .= '<link></link>';
$rssfeed .= "\n";
$rssfeed .= '<description>This is the RSS feed for TaskBoard</description>';
$rssfeed .= "\n";
$rssfeed .= '<language>en-us</language>';
//$rssfeed .= "\n";
//$rssfeed .= '<copyright></copyright>';
$rssfeed .= "\n\n\n";
//Retrieve latest comment
$tasks = $board->getTasks($tags);
foreach($tasks as $rowtask) {
// link dir detector
$url = $_SERVER['REQUEST_URI']; //returns the current URL
$parts = explode('/',$url);
$linkdir = $_SERVER['SERVER_NAME'];
for ($i = 0; $i < count($parts) - 2; $i++) {
$linkdir .= $parts[$i] . "/";
}
//RSS entry
$rssfeed .= '<item>';
$rssfeed .= "\n";
$rssfeed .= '<title>(Trip:' . preg_replace('/[^a-zA-Z0-9\s]/', '', $rowtask['tripcode']).") - ".preg_replace('/[^a-zA-Z0-9\s]/', '', $rowtask['title'] ). '</title>';
$rssfeed .= "\n";
$rssfeed .= '<description>'.preg_replace('/[^a-zA-Z0-9\s]/', '',str_replace(array("\r\n", "\r", "\n", "\t"), ' ', htmlentities(stripslashes($rowtask['message']),null, 'utf-8')) ). '</description>';
$rssfeed .= "\n";
if(isset($_SERVER["SERVER_NAME"])){
$rssfeed .= '<link>http://'.$linkdir.'?q=/view/'.$rowtask['task_id'].'</link>';
$rssfeed .= "\n";
}
$rssfeed .= '<guid>' . md5($rowtask['message']) . '</guid>';
$rssfeed .= "\n";
$rssfeed .= '<pubDate>' . date("D, d M Y H:i:s O", $rowtask['created']) . '</pubDate>';
$rssfeed .= "\n";
$rssfeed .= '</item>';
$rssfeed .= "\n\n";
}
$rssfeed .= '</channel>';
$rssfeed .= '</rss>';
echo $rssfeed;
exit;
break;
/*
* The default thing we want to do is get tags.
*/
default:
/*
* Get tags
*/
case 'tags':
// Browsing/searching the tasks
$mode = array('tasksList');
if (isset($uri_parts[1])) {
$tags = explode(',', $uri_parts[1]);
} else if(isset($_POST['tags'])) {
$tags = explode(' ', $_POST['tags']);
} else if(isset($_GET['tags'])) {
$tags = explode(' ', $_POST['tags']);
} else {
$tags = array();
}
//for tagclouds
if(empty($tags)){
$tagClouds = $board->tagsWeight(500);
}
$tagslist = implode(",",$tags);
$tasks = $board->getTasks($tags);
break;
}
//Create the layout
if(!isset($mode)) $mode = array(); //set default mode (should be error page perhaps)
$top_tags = $board->topTags(10);
require("layout.php");
?>