Rev 491 | Rev 500 | 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', |
||
495 | muzer | 189 | '/\[s\](.+?)\[\/s\]/is', |
485 | muzer | 190 | '/\[url\](.+?)\[\/url\]/is', |
486 | muzer | 191 | '/\[w\](.+?)\[\/w\]/is', |
485 | muzer | 192 | '/\[url=(?:")?(.+?)(?:")?\](.+?)\[\/url\]/is', |
486 | muzer | 193 | '/\[w=(?:")?(.+?)(?:")?\](.+?)\[\/w\]/is', |
485 | muzer | 194 | '/\[code\](.+?)\[\/code\]/is', |
195 | '/\[img\](.+?)\[\/img\]/is', |
||
196 | '/\[ul\](.+?)\[\/ul\]/is', |
||
197 | '/\[ol\](.+?)\[\/ol\]/is', |
||
198 | '/\[li\](.+?)\[\/li\]/is', |
||
199 | '/\[mono\](.+?)\[\/mono\]/is', |
||
200 | '/\[tcp\](.+?)\[\/tcp\]/is' |
||
297 | freddie | 201 | ); |
202 | |||
203 | $html = array( |
||
485 | muzer | 204 | '<b>$1</b>', |
205 | '<i>$1</i>', |
||
206 | '<u>$1</u>', |
||
495 | muzer | 207 | '<del>$1</del>', |
485 | muzer | 208 | '<a href="$1">$1</a>', |
486 | muzer | 209 | '<a href="/wiki/index.php?page=$1">$1</a>', |
485 | muzer | 210 | '<a href="$1">$2</a>', |
486 | muzer | 211 | '<a href="/wiki/index.php?page=$1">$2</a>', |
485 | muzer | 212 | '</p><div class="code">$1</div><p>', |
213 | '<img src="$1" alt="BBCode-included image" />', |
||
214 | '<ul>$1</ul>', |
||
215 | '<ol>$1</ol>', |
||
216 | '<li>$1</li>', |
||
217 | '<span style="font-family: Droid Sans Mono, monospace, fixed; margin-left: 1em; margin-right: 1em;">$1</span>', |
||
218 | '<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 | 219 | ); |
220 | |||
221 | $newstr = preg_replace($bbcode, $html, $newstr); |
||
222 | |||
223 | return $newstr; |
||
224 | } |
||
225 | |||
226 | function redirect($u) |
||
227 | { |
||
228 | header('Location: ' . $u); |
||
229 | die(); |
||
230 | } |
||
231 | |||
232 | function isLoggedIn() |
||
233 | { |
||
234 | $cookie = $_COOKIE['Tim32_Login']; |
||
235 | if (!empty($cookie)) |
||
236 | { |
||
237 | $clist = explode('|~|', $cookie); |
||
238 | $user = $this->getUserByUsername($clist[0]); |
||
239 | if ($user) |
||
240 | { |
||
241 | if ($user->password == $clist[1]) |
||
242 | { |
||
243 | return true; |
||
244 | } |
||
245 | } |
||
246 | } |
||
247 | |||
248 | return false; |
||
249 | } |
||
250 | |||
251 | function isUserAdmin() |
||
252 | { |
||
253 | if ($this->isLoggedIn()) |
||
254 | { |
||
255 | if ($this->getLoggedInUser()->accessID <= 0) |
||
256 | { |
||
257 | return true; |
||
258 | } |
||
259 | } |
||
260 | |||
261 | return false; |
||
262 | } |
||
263 | |||
264 | function isUserGM() |
||
265 | { |
||
266 | if ($this->isLoggedIn()) |
||
267 | { |
||
268 | if ($this->getLoggedInUser()->accessID <= 1) |
||
269 | { |
||
270 | return true; |
||
271 | } |
||
272 | } |
||
273 | |||
274 | return false; |
||
275 | } |
||
276 | |||
277 | function isUserNormal() |
||
278 | { |
||
279 | if ($this->isLoggedIn()) |
||
280 | { |
||
281 | if ($this->getLoggedInUser()->accessID <= 2) |
||
282 | { |
||
283 | return true; |
||
284 | } |
||
285 | } |
||
286 | |||
287 | return false; |
||
288 | } |
||
289 | |||
471 | muzer | 290 | function isUserBanned() |
291 | { |
||
292 | if ($this->isLoggedIn()) |
||
293 | { |
||
294 | if ($this->getLoggedInUser()->accessID >= 3) |
||
295 | { |
||
296 | return true; |
||
297 | } |
||
298 | } |
||
299 | |||
300 | return false; |
||
301 | } |
||
302 | |||
384 | tom | 303 | function checkChallengeStatus($challengeID, $previous, $next) |
304 | { |
||
305 | $currentChallengeID = $this->getLoggedInUser()->challengeID; |
||
306 | |||
307 | if (!$this->isLoggedIn()) |
||
308 | { |
||
309 | $this->redirect('index.php'); |
||
310 | } |
||
311 | else if ($currentChallengeID > $challengeID) |
||
312 | { |
||
313 | $this->redirect($next . '.php'); |
||
314 | } |
||
315 | else if ($currentChallengeID < $challengeID) |
||
316 | { |
||
317 | $this->redirect($previous . '.php'); |
||
318 | } |
||
319 | } |
||
320 | |||
297 | freddie | 321 | function checkLoggedIn() |
322 | { |
||
323 | if (!$this->isLoggedIn()) |
||
324 | { |
||
325 | $this->drawError('You need to be logged in.'); |
||
326 | } |
||
327 | } |
||
328 | |||
489 | tom | 329 | function query($query, $args = array()) |
297 | freddie | 330 | { |
487 | tom | 331 | $statement = $this->db->prepare($query); |
332 | if (!$statement->execute($args)) { |
||
333 | $this->drawError("Query Failed! MySQL Error: " . $statement->errorInfo()); |
||
334 | } |
||
335 | |||
336 | return $statement->fetchAll(); |
||
297 | freddie | 337 | } |
338 | |||
490 | tom | 339 | function findIDs($table, $query = '', $args = array()) |
297 | freddie | 340 | { |
341 | $array = array(); |
||
342 | |||
490 | tom | 343 | $results = $this->query('SELECT ID FROM ' . $table . ' ' . $query, $args); |
487 | tom | 344 | foreach ($results as $row) { |
297 | freddie | 345 | array_push($array, $row['ID']); |
346 | } |
||
347 | |||
348 | return $array; |
||
349 | } |
||
350 | |||
351 | function getUserByID($id) |
||
352 | { |
||
490 | tom | 353 | foreach ($this->query("SELECT * FROM Users WHERE ID = ?", array($id)) as $row) { |
354 | $user = new User(); |
||
297 | freddie | 355 | $user->ID = $row['ID']; |
356 | $user->accessID = $row['AccessID']; |
||
357 | $user->username = $row['Username']; |
||
358 | $user->password = $row['Password']; |
||
359 | $user->emailAddress = $row['EmailAddress']; |
||
360 | $user->name = $row['Name']; |
||
361 | $user->challengeID = $row['ChallengeID']; |
||
362 | |||
363 | return $user; |
||
364 | } |
||
365 | |||
366 | return false; |
||
367 | } |
||
368 | |||
490 | tom | 369 | function getUserByUsername($username) { |
370 | foreach ($this->query("SELECT * FROM Users WHERE Username = ?", array($username)) as $row) { |
||
297 | freddie | 371 | return $this->getUserByID($row['ID']); |
372 | } |
||
373 | |||
374 | return false; |
||
375 | } |
||
376 | |||
491 | tom | 377 | function getLoggedInUser() { |
378 | if ($this->isLoggedIn()) { |
||
297 | freddie | 379 | $clist = explode('|~|', $_COOKIE['Tim32_Login']); |
380 | return $this->getUserByUsername($clist[0]); |
||
381 | } |
||
382 | |||
383 | return false; |
||
384 | } |
||
385 | |||
491 | tom | 386 | function getBlogPost($id) { |
387 | foreach ($this->query("SELECT * FROM BlogPosts WHERE ID = ?", array($id)) as $row) { |
||
297 | freddie | 388 | $post = new BlogPost; |
389 | $post->ID = $row['ID']; |
||
491 | tom | 390 | |
391 | if ($row['ParentID'] == -1) { |
||
297 | freddie | 392 | $post->parent = -1; |
491 | tom | 393 | } else { |
297 | freddie | 394 | $post->parent = $this->getBlogPost($row['ParentID']); |
395 | } |
||
491 | tom | 396 | |
297 | freddie | 397 | $post->author = $this->getUserByID($row['AuthorID']); |
398 | $post->user = $this->getUserByID($row['AuthorID']); // For some older pages |
||
485 | muzer | 399 | $post->title = htmlspecialchars($row['Title']); |
400 | $post->content = htmlspecialchars($row['Content']); |
||
297 | freddie | 401 | $post->datePosted = strtotime($row['DatePosted']); |
402 | $post->category = $row['Category']; |
||
403 | $post->spam = $row['Spam']; |
||
404 | |||
405 | return $post; |
||
406 | } |
||
407 | |||
408 | $this->drawError('Cannot find blog post, #' . $id); |
||
409 | } |
||
410 | |||
491 | tom | 411 | function getProject($id) { |
412 | foreach ($this->query("SELECT * FROM Projects WHERE ID = ?", array($id)) as $row) { |
||
297 | freddie | 413 | $project = new Project; |
414 | |||
415 | $project->ID = $row['ID']; |
||
416 | $project->author = $this->getUserByID($row['AuthorID']); |
||
417 | $project->title = $row['Title']; |
||
418 | $project->description = $row['Description']; |
||
419 | $project->logoURL = $row['LogoURL']; |
||
420 | $project->downloadURL = $row['DownloadURL']; |
||
421 | $project->websiteURL = $row['WebsiteURL']; |
||
422 | $project->latestVersion = $row['LatestVersion']; |
||
423 | $project->lastUpdate = strtotime($row['LastUpdate']); |
||
424 | |||
425 | return $project; |
||
426 | } |
||
427 | |||
428 | return false; |
||
429 | } |
||
430 | |||
491 | tom | 431 | function getForumCategory($id) { |
432 | foreach ($this->query("SELECT * FROM ForumCategories WHERE ID = ?", array($id)) as $row) { |
||
297 | freddie | 433 | $f = new ForumCategory; |
434 | |||
435 | $f->ID = $row['ID']; |
||
436 | $f->parent = $this->getForumCategory($row['ParentID']); |
||
437 | $f->title = $row['Title']; |
||
438 | $f->description = $row['Description']; |
||
439 | |||
440 | return $f; |
||
441 | } |
||
442 | |||
443 | return false; |
||
444 | } |
||
445 | |||
491 | tom | 446 | function getForumPost($id) { |
447 | foreach ($this->query("SELECT * FROM ForumPosts WHERE ID = ?", array($id)) as $row) { |
||
297 | freddie | 448 | $f = new ForumPost; |
449 | |||
450 | $f->ID = $row['ID']; |
||
451 | $f->author = $this->getUserByID($row['AuthorID']); |
||
452 | $f->category = $this->getForumCategory($row['CategoryID']); |
||
453 | $f->parent = $this->getForumPost($row['ParentID']); |
||
454 | $f->title = $row['Title']; |
||
455 | $f->content = $row['Content']; |
||
456 | $f->datePosted = strtotime($row['DatePosted']); |
||
457 | $f->spam = $row['Spam']; |
||
458 | |||
459 | return $f; |
||
460 | } |
||
461 | |||
462 | return false; |
||
463 | } |
||
464 | |||
491 | tom | 465 | function delBlogPost($id) { |
466 | foreach ($this->findIDs("BlogPosts", "WHERE ParentID = ?", array($id)) as $i) { |
||
467 | $this->delBlogPost($i); |
||
297 | freddie | 468 | } |
491 | tom | 469 | |
470 | $this->query("DELETE FROM BlogPosts WHERE ID = ?", array($id)); |
||
297 | freddie | 471 | } |
472 | |||
491 | tom | 473 | function getGetID() { |
297 | freddie | 474 | $id = $_GET['id']; |
491 | tom | 475 | if (empty($id)) { |
297 | freddie | 476 | $id = 1; |
477 | } |
||
478 | |||
479 | return $id; |
||
480 | } |
||
481 | |||
491 | tom | 482 | function getPostID() { |
297 | freddie | 483 | $id = $_POST['id']; |
491 | tom | 484 | if (empty($id)) { |
297 | freddie | 485 | $id = 1; |
486 | } |
||
487 | |||
488 | return $id; |
||
489 | } |
||
490 | |||
491 | } |
||
492 | |||
493 | class User |
||
494 | { |
||
495 | public $ID; |
||
496 | public $accessID; |
||
497 | public $username; |
||
498 | public $password; |
||
499 | public $emailAddress; |
||
500 | public $name; |
||
443 | tom | 501 | |
297 | freddie | 502 | public $challengeID; |
503 | } |
||
504 | |||
505 | class BlogPost |
||
506 | { |
||
507 | public $ID; |
||
508 | public $parent; |
||
509 | public $author; |
||
510 | public $title; |
||
511 | public $content; |
||
512 | public $datePosted; |
||
513 | public $category; |
||
514 | public $spam; |
||
515 | } |
||
516 | |||
517 | class Project |
||
518 | { |
||
519 | public $ID; |
||
520 | public $author; |
||
521 | public $title; |
||
522 | public $description; |
||
426 | tom | 523 | |
434 | tom | 524 | |
297 | freddie | 525 | public $logoURL; |
526 | public $downloadURL; |
||
527 | public $websiteURL; |
||
528 | public $latestVersion; |
||
529 | public $lastUpdate; |
||
530 | } |
||
531 | |||
532 | class ForumCategory |
||
533 | { |
||
534 | public $ID; |
||
535 | public $parent; |
||
536 | public $title; |
||
537 | public $description; |
||
401 | tom | 538 | |
297 | freddie | 539 | } |
540 | |||
541 | class ForumPost |
||
542 | { |
||
543 | public $id; |
||
544 | public $author; |
||
545 | public $category; |
||
546 | public $parent; |
||
547 | public $title; |
||
548 | public $content; |
||
549 | public $datePosted; |
||
550 | public $spam; |
||
551 | } |
||
552 | |||
553 | function write($str) |
||
554 | { |
||
555 | echo $str; |
||
556 | echo "\n"; |
||
557 | } |
||
558 | |||
559 | ?> |