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