Rev 489 | Rev 491 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
297 | freddie | 1 | <?php |
2 | |||
3 | require '_config.php'; |
||
4 | |||
5 | class Taios_Page |
||
6 | { |
||
7 | function __construct($title, $url = "") |
||
8 | { |
||
9 | $this->title = $title; |
||
10 | $this->url = $url; |
||
11 | |||
12 | $this->drawnHeader = false; |
||
13 | $this->drawnMiddle = false; |
||
14 | $this->drawnFooter = false; |
||
15 | |||
487 | tom | 16 | try { |
17 | $this->db = new PDO("mysql:dbname=Tim32;host=" . MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, |
||
18 | array( PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'" )); |
||
19 | } catch (PDOException $e) { |
||
20 | $this->drawError("Failed to connect to database!"); |
||
21 | } |
||
297 | freddie | 22 | } |
23 | |||
24 | function drawHeader() |
||
25 | { |
||
26 | if (!$this->drawnHeader) |
||
27 | { |
||
28 | write('<!DOCTYPE html>'); |
||
490 | tom | 29 | write('<html lang="en">'); |
297 | freddie | 30 | write('<head>'); |
31 | write('<meta http-equiv="Content-Type" content="text/html;charset=utf-8">'); |
||
32 | write('<title>Tim32 · ' . $this->title . '</title>'); |
||
490 | tom | 33 | write('<link href="' . $this->url . 'styles.css" rel="stylesheet" type="text/css" media="all" />'); |
463 | tom | 34 | write('<link rel="shortcut icon" href="' . $this->url . 'data/favicon.png" />'); |
485 | muzer | 35 | write('<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.0.min.js"></script>'); |
36 | write('<script type="text/javascript" src="' . $this->url . 'tcp.js"></script>'); |
||
297 | freddie | 37 | write('</head>'); |
38 | write('<body>'); |
||
39 | write('<div class="sidebar">'); |
||
40 | write('<div class="sidebar-header">'); |
||
483 | muzer | 41 | write('<a href="' . $this->url . '"><h1>Tim32</h1></a>'); |
297 | freddie | 42 | write('</div>'); |
43 | write('<div class="sidebar-menu">'); |
||
44 | $this->drawMenuItem('Home', 'index.php'); |
||
45 | $this->drawMenuItem('Blog', 'blog/'); |
||
46 | $this->drawMenuItem('Projects', 'projects/'); |
||
47 | $this->drawMenuItem('Forums', 'forums/'); |
||
48 | $this->drawMenuItem('Wiki', 'wiki/'); |
||
49 | $this->drawMenuItem('Photos', 'photos/'); |
||
50 | write('<br />'); |
||
485 | muzer | 51 | |
52 | if ($this->isLoggedIn() && $this->isUserNormal($this->getLoggedInUser())) { |
||
297 | freddie | 53 | $this->drawMenuItem('Administration', 'admin/'); |
54 | $this->drawMenuItem('Logout', 'logout-do.php'); |
||
485 | muzer | 55 | } else if ($this->isLoggedIn()) { |
471 | muzer | 56 | $this->drawMenuItem('Logout', 'logout-do.php'); |
485 | muzer | 57 | |
58 | if ($this->getLoggedInUser()->username != "cake") { |
||
471 | muzer | 59 | $this->drawMenuItem('You are banned', NULL); |
485 | muzer | 60 | } else { |
472 | tom | 61 | $this->drawMenuItem('<span style="color:#032865">#undefined</span>', '/challenge/cakefolder'); |
485 | muzer | 62 | } |
63 | } else { |
||
297 | freddie | 64 | $this->drawMenuItem('Login', 'login.php'); |
65 | $this->drawMenuItem('Register', 'register.php'); |
||
66 | } |
||
485 | muzer | 67 | |
297 | freddie | 68 | write('<br />'); |
69 | $this->drawnHeader = true; |
||
70 | } |
||
71 | } |
||
72 | |||
485 | muzer | 73 | function drawMenuItem($t, $u) { |
74 | if ($u == NULL) { |
||
471 | muzer | 75 | write('<p style="color:red">' . $t . '</p>'); |
485 | muzer | 76 | } else { |
471 | muzer | 77 | write('<p><a href="' . $this->url . $u . '">' . $t . '</a></p>'); |
78 | } |
||
297 | freddie | 79 | } |
80 | |||
81 | function drawMiddle() |
||
82 | { |
||
485 | muzer | 83 | if (!$this->drawnMiddle) { |
297 | freddie | 84 | write('</div>'); |
85 | write('</div>'); |
||
86 | write('<div class="content">'); |
||
483 | muzer | 87 | write('<a href="./"><h2>' . $this->title . '</h2></a>'); |
297 | freddie | 88 | |
89 | $this->drawnMiddle = true; |
||
90 | } |
||
91 | } |
||
92 | |||
93 | function drawFooter() |
||
94 | { |
||
95 | if (!$this->drawnFooter) |
||
96 | { |
||
97 | write('</div>'); |
||
98 | write('</body>'); |
||
99 | write('</html>'); |
||
100 | |||
101 | $this->drawnFooter = true; |
||
102 | } |
||
103 | |||
104 | die(); |
||
105 | } |
||
106 | |||
107 | function drawError($text, $die = true) |
||
108 | { |
||
109 | $this->drawHeader(); |
||
110 | $this->drawMiddle(); |
||
111 | |||
112 | write('<h4 style="color: red;">Error: ' . $text . '</h4>'); |
||
113 | |||
490 | tom | 114 | if ($die) { |
297 | freddie | 115 | $this->drawFooter(); |
116 | die(); |
||
117 | } |
||
118 | } |
||
119 | |||
120 | function drawBlogPostTree($id, $first = false) |
||
121 | { |
||
122 | $post = $this->getBlogPost($id); |
||
123 | if ($first) |
||
124 | { |
||
125 | write('<h3><a href="post.php?id=' . $id . '">' . $post->title. '</a> <a href="post.php?id=' . $post->parent->ID . '">^</a></h3>'); |
||
126 | } |
||
127 | else |
||
128 | { |
||
129 | write('<a href="post.php?id=' . $id . '"><h3>' . $post->title. '</h3></a>'); |
||
130 | } |
||
131 | write('<h5 style="color: #666666;">Posted On ' . date('l j F Y', $post->datePosted) . ' by ' . $post->user->name . ' (' . $post->user->username . ')</h5>'); |
||
132 | write('<p>' . $this->replaceBBCode($post->content) . '</p>'); |
||
133 | |||
134 | if ($this->isUserNormal($this->getLoggedInUser())) |
||
135 | { |
||
136 | echo '<p class="bold"><a href="add-post.php?id=' . $id . '">Add Comment</a>'; |
||
137 | if ($this->isUserAdmin($this->getLoggedInUser()) || $this->getLoggedInUser()->ID == $post->author->ID) |
||
138 | { |
||
139 | echo ' · <a href="edit-post.php?id=' . $id . '">Edit Post</a>'; |
||
140 | echo ' · <a href="del-post.php?id=' . $id . '">Delete Post</a>'; |
||
141 | } |
||
142 | write('</p><br />'); |
||
143 | } |
||
144 | |||
484 | muzer | 145 | $ids = $this->findIDs('BlogPosts', 'WHERE ParentID="' . $id . '"'); |
297 | freddie | 146 | for ($i = 0; $i < count($ids); $i++) |
147 | { |
||
148 | write('<div class="indent">'); |
||
149 | $this->drawBlogPostTree($ids[$i]); |
||
150 | write('</div>'); |
||
151 | } |
||
152 | } |
||
153 | |||
154 | function drawBlogCategoriesMenu() |
||
155 | { |
||
156 | $cats = array(); |
||
157 | |||
158 | $ids = $this->findIDs('BlogPosts', 'WHERE ParentID = -1'); |
||
159 | for ($i = 0; $i < count($ids); $i++) |
||
160 | { |
||
161 | $cat = $this->getBlogPost($ids[$i])->category; |
||
483 | muzer | 162 | if (!in_array($cat, $cats) && ($cat != "Drafts" || $this->isUserGM($this->getLoggedInUser()))) |
297 | freddie | 163 | { |
164 | array_push($cats, $cat); |
||
165 | } |
||
166 | } |
||
167 | |||
168 | write('<h3>Categories</h3>'); |
||
169 | for ($i = 0; $i < count($cats); $i++) |
||
170 | { |
||
171 | $this->drawMenuItem($cats[$i], 'blog/index.php?cat=' . $cats[$i]); |
||
172 | } |
||
173 | } |
||
174 | |||
175 | function replaceBBCode($str) |
||
176 | { |
||
437 | tom | 177 | $newstr = $str; |
484 | muzer | 178 | $newstr = str_replace("<", "<", $newstr); |
179 | $newstr = str_replace(">", ">", $newstr); |
||
481 | muzer | 180 | $newstr = str_replace("\n", "<br />", $newstr); |
362 | tom | 181 | $newstr = str_replace("\\'", "'", $newstr); |
182 | $newstr = str_replace("\\\"",'"', $newstr); |
||
297 | freddie | 183 | $newstr = str_replace(' ', ' ', $newstr); |
184 | |||
185 | $bbcode = array( |
||
485 | muzer | 186 | '/\[b\](.+?)\[\/b\]/is', |
187 | '/\[i\](.+?)\[\/i\]/is', |
||
188 | '/\[u\](.+?)\[\/u\]/is', |
||
189 | '/\[url\](.+?)\[\/url\]/is', |
||
486 | muzer | 190 | '/\[w\](.+?)\[\/w\]/is', |
485 | muzer | 191 | '/\[url=(?:")?(.+?)(?:")?\](.+?)\[\/url\]/is', |
486 | muzer | 192 | '/\[w=(?:")?(.+?)(?:")?\](.+?)\[\/w\]/is', |
485 | muzer | 193 | '/\[code\](.+?)\[\/code\]/is', |
194 | '/\[img\](.+?)\[\/img\]/is', |
||
195 | '/\[ul\](.+?)\[\/ul\]/is', |
||
196 | '/\[ol\](.+?)\[\/ol\]/is', |
||
197 | '/\[li\](.+?)\[\/li\]/is', |
||
198 | '/\[mono\](.+?)\[\/mono\]/is', |
||
199 | '/\[tcp\](.+?)\[\/tcp\]/is' |
||
297 | freddie | 200 | ); |
201 | |||
202 | $html = array( |
||
485 | muzer | 203 | '<b>$1</b>', |
204 | '<i>$1</i>', |
||
205 | '<u>$1</u>', |
||
206 | '<a href="$1">$1</a>', |
||
486 | muzer | 207 | '<a href="/wiki/index.php?page=$1">$1</a>', |
485 | muzer | 208 | '<a href="$1">$2</a>', |
486 | muzer | 209 | '<a href="/wiki/index.php?page=$1">$2</a>', |
485 | muzer | 210 | '</p><div class="code">$1</div><p>', |
211 | '<img src="$1" alt="BBCode-included image" />', |
||
212 | '<ul>$1</ul>', |
||
213 | '<ol>$1</ol>', |
||
214 | '<li>$1</li>', |
||
215 | '<span style="font-family: Droid Sans Mono, monospace, fixed; margin-left: 1em; margin-right: 1em;">$1</span>', |
||
216 | '<span class="tcp" data-status="closed" data-text="$1">$1<img title="Open TCP Editor" class="tcp_button" src="http://tim32.org/~freddie/timlan/goTCP.png" alt="Open TCP Editor" /></span>' |
||
297 | freddie | 217 | ); |
218 | |||
219 | $newstr = preg_replace($bbcode, $html, $newstr); |
||
220 | |||
221 | return $newstr; |
||
222 | } |
||
223 | |||
224 | function redirect($u) |
||
225 | { |
||
226 | header('Location: ' . $u); |
||
227 | die(); |
||
228 | } |
||
229 | |||
230 | function isLoggedIn() |
||
231 | { |
||
232 | $cookie = $_COOKIE['Tim32_Login']; |
||
233 | if (!empty($cookie)) |
||
234 | { |
||
235 | $clist = explode('|~|', $cookie); |
||
236 | $user = $this->getUserByUsername($clist[0]); |
||
237 | if ($user) |
||
238 | { |
||
239 | if ($user->password == $clist[1]) |
||
240 | { |
||
241 | return true; |
||
242 | } |
||
243 | } |
||
244 | } |
||
245 | |||
246 | return false; |
||
247 | } |
||
248 | |||
249 | function isUserAdmin() |
||
250 | { |
||
251 | if ($this->isLoggedIn()) |
||
252 | { |
||
253 | if ($this->getLoggedInUser()->accessID <= 0) |
||
254 | { |
||
255 | return true; |
||
256 | } |
||
257 | } |
||
258 | |||
259 | return false; |
||
260 | } |
||
261 | |||
262 | function isUserGM() |
||
263 | { |
||
264 | if ($this->isLoggedIn()) |
||
265 | { |
||
266 | if ($this->getLoggedInUser()->accessID <= 1) |
||
267 | { |
||
268 | return true; |
||
269 | } |
||
270 | } |
||
271 | |||
272 | return false; |
||
273 | } |
||
274 | |||
275 | function isUserNormal() |
||
276 | { |
||
277 | if ($this->isLoggedIn()) |
||
278 | { |
||
279 | if ($this->getLoggedInUser()->accessID <= 2) |
||
280 | { |
||
281 | return true; |
||
282 | } |
||
283 | } |
||
284 | |||
285 | return false; |
||
286 | } |
||
287 | |||
471 | muzer | 288 | function isUserBanned() |
289 | { |
||
290 | if ($this->isLoggedIn()) |
||
291 | { |
||
292 | if ($this->getLoggedInUser()->accessID >= 3) |
||
293 | { |
||
294 | return true; |
||
295 | } |
||
296 | } |
||
297 | |||
298 | return false; |
||
299 | } |
||
300 | |||
384 | tom | 301 | function checkChallengeStatus($challengeID, $previous, $next) |
302 | { |
||
303 | $currentChallengeID = $this->getLoggedInUser()->challengeID; |
||
304 | |||
305 | if (!$this->isLoggedIn()) |
||
306 | { |
||
307 | $this->redirect('index.php'); |
||
308 | } |
||
309 | else if ($currentChallengeID > $challengeID) |
||
310 | { |
||
311 | $this->redirect($next . '.php'); |
||
312 | } |
||
313 | else if ($currentChallengeID < $challengeID) |
||
314 | { |
||
315 | $this->redirect($previous . '.php'); |
||
316 | } |
||
317 | } |
||
318 | |||
297 | freddie | 319 | function checkLoggedIn() |
320 | { |
||
321 | if (!$this->isLoggedIn()) |
||
322 | { |
||
323 | $this->drawError('You need to be logged in.'); |
||
324 | } |
||
325 | } |
||
326 | |||
489 | tom | 327 | function query($query, $args = array()) |
297 | freddie | 328 | { |
487 | tom | 329 | $statement = $this->db->prepare($query); |
330 | if (!$statement->execute($args)) { |
||
331 | $this->drawError("Query Failed! MySQL Error: " . $statement->errorInfo()); |
||
332 | } |
||
333 | |||
334 | return $statement->fetchAll(); |
||
297 | freddie | 335 | } |
336 | |||
490 | tom | 337 | function findIDs($table, $query = '', $args = array()) |
297 | freddie | 338 | { |
339 | $array = array(); |
||
340 | |||
490 | tom | 341 | $results = $this->query('SELECT ID FROM ' . $table . ' ' . $query, $args); |
487 | tom | 342 | foreach ($results as $row) { |
297 | freddie | 343 | array_push($array, $row['ID']); |
344 | } |
||
345 | |||
346 | return $array; |
||
347 | } |
||
348 | |||
349 | function getUserByID($id) |
||
350 | { |
||
490 | tom | 351 | foreach ($this->query("SELECT * FROM Users WHERE ID = ?", array($id)) as $row) { |
352 | $user = new User(); |
||
297 | freddie | 353 | $user->ID = $row['ID']; |
354 | $user->accessID = $row['AccessID']; |
||
355 | $user->username = $row['Username']; |
||
356 | $user->password = $row['Password']; |
||
357 | $user->emailAddress = $row['EmailAddress']; |
||
358 | $user->name = $row['Name']; |
||
359 | $user->challengeID = $row['ChallengeID']; |
||
360 | |||
361 | return $user; |
||
362 | } |
||
363 | |||
364 | return false; |
||
365 | } |
||
366 | |||
490 | tom | 367 | function getUserByUsername($username) { |
368 | foreach ($this->query("SELECT * FROM Users WHERE Username = ?", array($username)) as $row) { |
||
297 | freddie | 369 | return $this->getUserByID($row['ID']); |
370 | } |
||
371 | |||
372 | return false; |
||
373 | } |
||
374 | |||
375 | function getLoggedInUser() |
||
376 | { |
||
377 | if ($this->isLoggedIn()) |
||
378 | { |
||
379 | $clist = explode('|~|', $_COOKIE['Tim32_Login']); |
||
380 | return $this->getUserByUsername($clist[0]); |
||
381 | } |
||
382 | |||
383 | return false; |
||
384 | } |
||
385 | |||
386 | function getBlogPost($id) |
||
387 | { |
||
487 | tom | 388 | $results = $this->query('SELECT * FROM BlogPosts WHERE ID = "' . $id . '"'); |
389 | foreach ($results as $row) { |
||
297 | freddie | 390 | $post = new BlogPost; |
391 | $post->ID = $row['ID']; |
||
392 | if ($row['ParentID'] == -1) |
||
393 | { |
||
394 | $post->parent = -1; |
||
395 | } |
||
396 | else |
||
397 | { |
||
398 | $post->parent = $this->getBlogPost($row['ParentID']); |
||
399 | } |
||
400 | $post->author = $this->getUserByID($row['AuthorID']); |
||
401 | $post->user = $this->getUserByID($row['AuthorID']); // For some older pages |
||
485 | muzer | 402 | $post->title = htmlspecialchars($row['Title']); |
403 | $post->content = htmlspecialchars($row['Content']); |
||
297 | freddie | 404 | $post->datePosted = strtotime($row['DatePosted']); |
405 | $post->category = $row['Category']; |
||
406 | $post->spam = $row['Spam']; |
||
407 | |||
408 | return $post; |
||
409 | } |
||
410 | |||
411 | $this->drawError('Cannot find blog post, #' . $id); |
||
412 | } |
||
413 | |||
414 | function getProject($id) |
||
415 | { |
||
487 | tom | 416 | $results = $this->query('SELECT * FROM Projects WHERE ID = "' . $id . '"'); |
417 | foreach ($results as $row) { |
||
297 | freddie | 418 | $project = new Project; |
419 | |||
420 | $project->ID = $row['ID']; |
||
421 | $project->author = $this->getUserByID($row['AuthorID']); |
||
422 | $project->title = $row['Title']; |
||
423 | $project->description = $row['Description']; |
||
424 | $project->logoURL = $row['LogoURL']; |
||
425 | $project->downloadURL = $row['DownloadURL']; |
||
426 | $project->websiteURL = $row['WebsiteURL']; |
||
427 | $project->latestVersion = $row['LatestVersion']; |
||
428 | $project->lastUpdate = strtotime($row['LastUpdate']); |
||
429 | |||
430 | return $project; |
||
431 | } |
||
432 | |||
433 | return false; |
||
434 | } |
||
435 | |||
436 | function getForumCategory($id) |
||
437 | { |
||
487 | tom | 438 | $results = $this->query('SELECT * FROM ForumCategories WHERE ID = "' . $id . '"'); |
439 | foreach ($results as $row) { |
||
297 | freddie | 440 | $f = new ForumCategory; |
441 | |||
442 | $f->ID = $row['ID']; |
||
443 | $f->parent = $this->getForumCategory($row['ParentID']); |
||
444 | $f->title = $row['Title']; |
||
445 | $f->description = $row['Description']; |
||
446 | |||
447 | return $f; |
||
448 | } |
||
449 | |||
450 | return false; |
||
451 | } |
||
452 | |||
453 | function getForumPost($id) |
||
454 | { |
||
487 | tom | 455 | $results = $this->query('SELECT * FROM ForumPosts WHERE ID = "' . $id . '"'); |
456 | foreach ($results as $row) { |
||
297 | freddie | 457 | $f = new ForumPost; |
458 | |||
459 | $f->ID = $row['ID']; |
||
460 | $f->author = $this->getUserByID($row['AuthorID']); |
||
461 | $f->category = $this->getForumCategory($row['CategoryID']); |
||
462 | $f->parent = $this->getForumPost($row['ParentID']); |
||
463 | $f->title = $row['Title']; |
||
464 | $f->content = $row['Content']; |
||
465 | $f->datePosted = strtotime($row['DatePosted']); |
||
466 | $f->spam = $row['Spam']; |
||
467 | |||
468 | return $f; |
||
469 | } |
||
470 | |||
471 | return false; |
||
472 | } |
||
473 | |||
474 | function delBlogPost($id) |
||
475 | { |
||
484 | muzer | 476 | $ids = $this->findIDs('BlogPosts', 'WHERE ParentID="' . $id . '"'); |
297 | freddie | 477 | for ($i = 0; $i < count($ids); $i++) |
478 | { |
||
479 | $this->delBlogPost($ids[$i]); |
||
480 | } |
||
481 | |||
484 | muzer | 482 | $this->query('DELETE FROM BlogPosts WHERE ID="' . $id . '"'); |
297 | freddie | 483 | } |
484 | |||
485 | function getGetID() |
||
486 | { |
||
487 | $id = $_GET['id']; |
||
488 | if (empty($id)) |
||
489 | { |
||
490 | $id = 1; |
||
491 | } |
||
492 | |||
493 | return $id; |
||
494 | } |
||
495 | |||
496 | function getPostID() |
||
497 | { |
||
498 | $id = $_POST['id']; |
||
499 | if (empty($id)) |
||
500 | { |
||
501 | $id = 1; |
||
502 | } |
||
503 | |||
504 | return $id; |
||
505 | } |
||
506 | |||
507 | } |
||
508 | |||
509 | class User |
||
510 | { |
||
511 | public $ID; |
||
512 | public $accessID; |
||
513 | public $username; |
||
514 | public $password; |
||
515 | public $emailAddress; |
||
516 | public $name; |
||
443 | tom | 517 | |
297 | freddie | 518 | public $challengeID; |
519 | } |
||
520 | |||
521 | class BlogPost |
||
522 | { |
||
523 | public $ID; |
||
524 | public $parent; |
||
525 | public $author; |
||
526 | public $title; |
||
527 | public $content; |
||
528 | public $datePosted; |
||
529 | public $category; |
||
530 | public $spam; |
||
531 | } |
||
532 | |||
533 | class Project |
||
534 | { |
||
535 | public $ID; |
||
536 | public $author; |
||
537 | public $title; |
||
538 | public $description; |
||
426 | tom | 539 | |
434 | tom | 540 | |
297 | freddie | 541 | public $logoURL; |
542 | public $downloadURL; |
||
543 | public $websiteURL; |
||
544 | public $latestVersion; |
||
545 | public $lastUpdate; |
||
546 | } |
||
547 | |||
548 | class ForumCategory |
||
549 | { |
||
550 | public $ID; |
||
551 | public $parent; |
||
552 | public $title; |
||
553 | public $description; |
||
401 | tom | 554 | |
297 | freddie | 555 | } |
556 | |||
557 | class ForumPost |
||
558 | { |
||
559 | public $id; |
||
560 | public $author; |
||
561 | public $category; |
||
562 | public $parent; |
||
563 | public $title; |
||
564 | public $content; |
||
565 | public $datePosted; |
||
566 | public $spam; |
||
567 | } |
||
568 | |||
569 | function write($str) |
||
570 | { |
||
571 | echo $str; |
||
572 | echo "\n"; |
||
573 | } |
||
574 | |||
575 | ?> |