forked from corneyflorex/TaskBoard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskboard.php
More file actions
425 lines (362 loc) · 11.4 KB
/
Taskboard.php
File metadata and controls
425 lines (362 loc) · 11.4 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
<?php
/**
* Our main Taskboard class
*/
class Taskboard {
//Constants that do something
const TASK_OPEN = 0;
const TASK_CLOSED = 1;
/**
* Days after the last bump in a task for it to disappear from searches
*
* @var int Value
*/
public $task_lifespan = 30;
/**
* PHP5 Constructor that serves no purpose
*/
public function __construct(){
//asdf
}
/*
* Check who is online in a particular page.
*/
/**
* Creates a new task and adds it to the database.
*
* @param type $tripcode The tripcode (idk what this is)
* @param type $title The title of the task
* @param type $message The message the task contains
* @param type $tags The tags this task contains
* @return type The task id
*/
public function createTask($tripcode, $title, $message, $tags){
//Create the array we will store in the database
$data = array(
'created' => time(),
'bumped' => time(),
'title' => $title,
'message' => $message,
'tripcode' => $tripcode,
'status' => $this::TASK_OPEN
);
//Data types to put into the database
$dataType = array(
'INT',
'INT',
'STR',
'STR',
'STR',
'INT'
);
//Insert the data
$task_id = Database::insert('tasks', $data, $dataType);
if(!$task_id) {echo " error in creating new task <br/>";return false;}
// Create the tags
if( isset($tags) AND !empty($tags)) {
$sql_tags = array();
foreach($tags as $t){
$t = (string)$t;
if(empty($t)) continue;
$sql_tags = array(
'task_id' => $task_id,
'label' => $t,
'created' => time()
);
$sql_tagsType = array(
'INT',
'STR'
);
// TODO: These values should really be in 1 insert query
Database::insert('tags', $sql_tags, $sql_tagsType);
}
}
return $task_id;
}
/**
* Finds a random task.
*
* @param type $limit
*/
public function randomTasks($limit=50) {
//TODO: actually put stuff in here
}
/**
* Deletes a task by either ID or Tripcode
* NOTE: This does not remove any entries that are still in the 'tags' table
*
* @param type $delType
* @param array $input
*/
public function delTaskBy($delType, $input=array()) {
if(!is_array($input)) $input = array(); // Input array is always zero
switch($delType){
case 'Delete a post': // $input <-- post ID
$s_id = $input[0];
$sql[] = "DELETE FROM tasks WHERE id = " . $s_id;
break;
case 'Delete all post by trip': // $input <-- Tripcode name ##DANGER## This will delete everything done by a poster
$s_pass = $input[0];
$sql[] = "DELETE FROM tasks WHERE tripcode = " . $s_pass;
break;
case 'Delete single task with normal password': // $input <-- Task ID, Task Password
$s_ID = $input[0];
$s_pass = $input[1] ;
$sql[] = "DELETE FROM tasks WHERE id = " . $s_ID . " AND tripcode=' " . $s_pass . "'";
break;
default:
echo '\n No action taken as there was an unknown delete option chosen for delTaskBy()\n';
break;
}
try {
foreach($sql as $s) {
Database::query($s);
echo 'Delete command sent';
}
} catch(PDOException $e) {
echo $e;
echo 'Delete Operation failed, did you get your password wrong?';
}
}
/*
* Create a new comment
*
*/
public function createComment($tripcode, $taskID, $replyID=NULL, $message, $vote=1){
//Create the array we will store in the database
$data = array(
'task_id' => $taskID,
'tripcode' => $tripcode,
'reply_comment_id' => $replyID,
'created' => time(),
'message' => $message,
'vote' => $vote
);
//Data types to put into the database
$dataType = array(
'INT',
'STR',
'INT',
'INT',
'STR',
'INT'
);
//Insert the data
$task_id = Database::insert('comments', $data, $dataType);
//Bump the topic
/*Would use this except sqlite doesnt support it... : OUTER JOIN tags ON tasks.id = tags.task_id */
$sql = "UPDATE tasks
SET
bumped = ?
WHERE id == ?
";
try {
$rs = Database::query($sql, array(time(),$taskID) , array("INT","INT") );
} catch (Exception $e){
echo $e;
}
if(!$task_id) {echo " error in creating new comment <br/>";return false;}
return $task_id;
}
/**
* Gets comments by a certain task id.
*
* @param type $id The id of tje task
*/
public function getCommentsByTaskId($id) {
$sql = "SELECT * FROM comments WHERE task_id = " . $id." ORDER BY comments.created DESC";
$result = Database::query($sql);
return $result;
}
/**
* Gets 1 task from a task id.
*
* @param type $id
* @return type
*/
public function getTaskByID($id=''){
$sql = "SELECT DISTINCT tasks.id AS task_id, tasks.tripcode, tasks.created, tasks.bumped, tasks.title, tasks.message FROM tasks WHERE tasks.id = $id LIMIT 1";
try {
$rs = Database::query($sql);
} catch (Eception $e){
return array();
}
// If something failed.. return no tasks
if(!$rs) return array();
return $rs;
}
/**
* Get a list of tasks (optional tag search)
*
* @param array $tags
* @param type $limit
* @return type
*/
public function getTasks($tags=array(), $limit=50){
if(!is_array($tags)) $tags = array();
$sql_tag_labels = array();
foreach($tags as $t){
$tmp = preg_replace("/[^a-zA-Z0-9_\- ]/i", "", $t);
if(!empty($tmp)) $sql_tag_labels[] = $tmp;
}
if(!empty($sql_tag_labels)){
$sql_where_tags = "AND tags.label IN ('".implode("','", $sql_tag_labels)."')";
} else {
$sql_where_tags = '';
}
/*Would use this except sqlite doesnt support it... : OUTER JOIN tags ON tasks.id = tags.task_id */
$sql = "SELECT DISTINCT tasks.id AS task_id, tasks.tripcode, tasks.created, tasks.bumped, tasks.title AS title, tasks.message AS message
FROM tasks
LEFT OUTER JOIN tags ON tasks.id = tags.task_id
WHERE
tasks.status = ?
$sql_where_tags
AND tasks.bumped > ?
ORDER BY tasks.bumped DESC
LIMIT ?";
try {
$rs = Database::query($sql, array($this::TASK_OPEN, time() - strtotime('-'.$this->task_lifespan.' days'), $limit) , array("INT","STR","INT") );
} catch (Exception $e){
return array();
}
// If something failed.. return no tasks
if(!$rs) return array();
// TODO: Get the tags for each task!
return $rs;
}
/**
* Returns an array of the most frequently used tags
*
* @param type $limit
* @return type Array
*/
public function topTags($limit=5){
$sql = "SELECT label, COUNT(*) AS count
FROM tags
GROUP BY label
ORDER BY count DESC
LIMIT ?";
//$rs = Database::query($sql, array($limit));
//$rs = Database::query("SELECT label, COUNT(*) as count FROM tags GROUP BY label ORDER BY count DESC LIMIT ?", array($limit) , array("INT"));
$rs = Database::query($sql, array($limit),array("INT"));
return $rs;
}
/**
* Initializes the database.
*
* @todo Clean up everything
*/
public function initDatabase(){
/*
Note:
One exception to the typelessness of SQLite is a column whose type is INTEGER PRIMARY KEY. (And you must use "INTEGER" not "INT". A column of type INT PRIMARY KEY is typeless just like any other.) INTEGER PRIMARY KEY columns must contain a 32-bit signed integer. Any attempt to insert non-integer data will result in an error.
Hence all primary key field must be of INTEGER not INT
*/
$sql = array();
//MySQL borks without AUTO_INCREMENT, but sql borks with AUTO_INCREMENT. Hence we must provide two differnt table settings to provide cross compatibility.
$dbType = Database::getDataBaseType();
echo $dbType."<br/>";
switch ( $dbType ){
/*SQLite*/
case "sqlite":
$sql[] = <<<SQL
CREATE TABLE IF NOT EXISTS tasks (
id INTEGER NOT NULL,
tripcode VARCHAR(25),
status INTEGER ,
created INTEGER ,
bumped INTEGER ,
title VARCHAR(100),
message VARCHAR(2000),
PRIMARY KEY (id)
);
SQL;
$sql[] = <<<SQL
CREATE TABLE IF NOT EXISTS tags (
task_id INTEGER,
label VARCHAR(50),
created INTEGER
);
SQL;
//Create comments table
$sql[] = "CREATE TABLE IF NOT EXISTS comments (
id INTEGER NOT NULL,
task_id INTEGER NOT NULL,
tripcode VARCHAR(25),
reply_comment_id INTEGER,
created INTEGER,
message TEXT,
vote INTEGER,
PRIMARY KEY (id)
);";
//Uniqueness check (for enforcing originality
$sql[] = "CREATE TABLE IF NOT EXISTS uniqueHash (
id INTEGER NOT NULL,
hash VARCHAR(25),
created INT
);";
//Generalized 'session' holder. E.g. whos online recently.
$sql[] = "CREATE TABLE IF NOT EXISTS miniSessionHash (
id INTEGER NOT NULL,
hash VARCHAR(25),
info VARCHAR(25),
intinfo INTEGER,
created INTEGER
);";
break;
/*SQLite END*/
/*MYSQL VERSION*/
default:
case "mysql":
$sql[] = <<<SQL
CREATE TABLE IF NOT EXISTS tasks (
id INTEGER NOT NULL AUTO_INCREMENT,
tripcode VARCHAR(25),
status INT ,
created INT ,
bumped INT ,
title VARCHAR(100),
message VARCHAR(2000),
PRIMARY KEY (id)
);
SQL;
$sql[] = <<<SQL
CREATE TABLE IF NOT EXISTS tags (
task_id INT,
label VARCHAR(50),
created INT
);
SQL;
//Create comments table
$sql[] = "CREATE TABLE IF NOT EXISTS comments (
id INTEGER NOT NULL AUTO_INCREMENT,
task_id INT NOT NULL,
tripcode VARCHAR(25),
reply_comment_id INT,
created INT,
message TEXT,
vote INT,
PRIMARY KEY (id)
);";
//Uniqueness check (for enforcing originality
$sql[] = "CREATE TABLE IF NOT EXISTS uniqueHash (
id INTEGER NOT NULL AUTO_INCREMENT,
hash VARCHAR(25),
created INT
);";
//Generalized 'session' holder. E.g. whos online recently.
$sql[] = "CREATE TABLE IF NOT EXISTS miniSessionHash (
id INTEGER NOT NULL AUTO_INCREMENT,
hash VARCHAR(25),
info VARCHAR(25),
intinfo INT,
created INT
);";
break;
/* END OF MYSQL VERSION*/
}
foreach($sql as $s) {
Database::query($s);
}
}
}