Rev 443 | Rev 451 | 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>'); |
||
37 | write('<link href="' . $this->url . 'styles.css" rel="stylesheet" type="text/css" media="screen">'); |
||
38 | write('</head>'); |
||
39 | write('<body>'); |
||
40 | write('<div class="sidebar">'); |
||
41 | write('<div class="sidebar-header">'); |
||
42 | write('<h1>Tim32</h1>'); |
||
43 | write('</div>'); |
||
44 | write('<div class="sidebar-menu">'); |
||
45 | $this->drawMenuItem('Home', 'index.php'); |
||
46 | $this->drawMenuItem('Blog', 'blog/'); |
||
47 | $this->drawMenuItem('Projects', 'projects/'); |
||
48 | $this->drawMenuItem('Forums', 'forums/'); |
||
49 | $this->drawMenuItem('Wiki', 'wiki/'); |
||
50 | $this->drawMenuItem('Photos', 'photos/'); |
||
51 | write('<br />'); |
||
52 | if ($this->isLoggedIn()) |
||
53 | { |
||
54 | $this->drawMenuItem('Administration', 'admin/'); |
||
55 | $this->drawMenuItem('Logout', 'logout-do.php'); |
||
56 | } |
||
57 | else |
||
58 | { |
||
59 | $this->drawMenuItem('Login', 'login.php'); |
||
60 | $this->drawMenuItem('Register', 'register.php'); |
||
61 | } |
||
62 | write('<br />'); |
||
63 | |||
64 | $this->drawnHeader = true; |
||
65 | } |
||
66 | } |
||
67 | |||
68 | function drawMenuItem($t, $u) |
||
69 | { |
||
70 | write('<p><a href="' . $this->url . $u . '">' . $t . '</a></p>'); |
||
71 | } |
||
72 | |||
73 | function drawMiddle() |
||
74 | { |
||
75 | if (!$this->drawnMiddle) |
||
76 | { |
||
77 | write('<br />'); |
||
78 | write('</div>'); |
||
79 | write('</div>'); |
||
80 | write('<div class="content">'); |
||
81 | write('<h2>' . $this->title . '</h2>'); |
||
82 | |||
83 | $this->drawnMiddle = true; |
||
84 | } |
||
85 | } |
||
86 | |||
87 | function drawFooter() |
||
88 | { |
||
89 | if (!$this->drawnFooter) |
||
90 | { |
||
443 | tom | 91 | write('<br /><p class="copyright">© 2011 Tim32 ·</p>'); |
297 | freddie | 92 | write('</div>'); |
93 | write('</body>'); |
||
94 | write('</html>'); |
||
95 | |||
96 | $this->drawnFooter = true; |
||
97 | } |
||
98 | |||
99 | die(); |
||
100 | } |
||
101 | |||
102 | function drawError($text, $die = true) |
||
103 | { |
||
104 | $this->drawHeader(); |
||
105 | $this->drawMiddle(); |
||
106 | |||
107 | write('<h4 style="color: red;">Error: ' . $text . '</h4>'); |
||
108 | |||
109 | if ($die) |
||
110 | { |
||
111 | $this->drawFooter(); |
||
112 | die(); |
||
113 | } |
||
114 | } |
||
115 | |||
116 | function drawBlogPostTree($id, $first = false) |
||
117 | { |
||
118 | $post = $this->getBlogPost($id); |
||
119 | if ($first) |
||
120 | { |
||
121 | write('<h3><a href="post.php?id=' . $id . '">' . $post->title. '</a> <a href="post.php?id=' . $post->parent->ID . '">^</a></h3>'); |
||
122 | } |
||
123 | else |
||
124 | { |
||
125 | write('<a href="post.php?id=' . $id . '"><h3>' . $post->title. '</h3></a>'); |
||
126 | } |
||
127 | write('<h5 style="color: #666666;">Posted On ' . date('l j F Y', $post->datePosted) . ' by ' . $post->user->name . ' (' . $post->user->username . ')</h5>'); |
||
128 | write('<p>' . $this->replaceBBCode($post->content) . '</p>'); |
||
129 | |||
130 | if ($this->isUserNormal($this->getLoggedInUser())) |
||
131 | { |
||
132 | echo '<p class="bold"><a href="add-post.php?id=' . $id . '">Add Comment</a>'; |
||
133 | if ($this->isUserAdmin($this->getLoggedInUser()) || $this->getLoggedInUser()->ID == $post->author->ID) |
||
134 | { |
||
135 | echo ' · <a href="edit-post.php?id=' . $id . '">Edit Post</a>'; |
||
136 | echo ' · <a href="del-post.php?id=' . $id . '">Delete Post</a>'; |
||
137 | } |
||
138 | write('</p><br />'); |
||
139 | } |
||
140 | |||
141 | $ids = $this->findIDs('BlogPosts', 'WHERE ParentID=' . $id); |
||
142 | for ($i = 0; $i < count($ids); $i++) |
||
143 | { |
||
144 | write('<div class="indent">'); |
||
145 | $this->drawBlogPostTree($ids[$i]); |
||
146 | write('</div>'); |
||
147 | } |
||
148 | } |
||
149 | |||
150 | function drawBlogCategoriesMenu() |
||
151 | { |
||
152 | $cats = array(); |
||
153 | |||
154 | $ids = $this->findIDs('BlogPosts', 'WHERE ParentID = -1'); |
||
155 | for ($i = 0; $i < count($ids); $i++) |
||
156 | { |
||
157 | $cat = $this->getBlogPost($ids[$i])->category; |
||
158 | if (!in_array($cat, $cats)) |
||
159 | { |
||
160 | array_push($cats, $cat); |
||
161 | } |
||
162 | } |
||
163 | |||
164 | write('<h3>Categories</h3>'); |
||
165 | for ($i = 0; $i < count($cats); $i++) |
||
166 | { |
||
167 | $this->drawMenuItem($cats[$i], 'blog/index.php?cat=' . $cats[$i]); |
||
168 | } |
||
169 | } |
||
170 | |||
171 | function replaceBBCode($str) |
||
172 | { |
||
437 | tom | 173 | /*$newstrarray = explode("\n", $str); |
434 | tom | 174 | $newstr = ""; |
175 | foreach ($newstrarray as $line) |
||
176 | { |
||
436 | tom | 177 | if ($line == "\n" || $line == " \n" || $line == "\n " || $line == "\n\r") |
434 | tom | 178 | { |
179 | $line = "</p><p>"; |
||
180 | } |
||
181 | |||
182 | $newstr .= ($line . "\n"); |
||
437 | tom | 183 | }*/ |
184 | |||
185 | $newstr = $str; |
||
434 | tom | 186 | $newstr = str_replace("<", "[", $newstr); |
308 | tom | 187 | $newstr = str_replace(">", "]", $newstr); |
437 | tom | 188 | $newstr = str_replace("\n", "</p><p>", $newstr); |
362 | tom | 189 | $newstr = str_replace("\\'", "'", $newstr); |
190 | $newstr = str_replace("\\\"",'"', $newstr); |
||
297 | freddie | 191 | $newstr = str_replace(' ', ' ', $newstr); |
192 | $newstr = str_replace(' :)', ' <img src="' . $this->url . 'data/smilies/face-smile.png" class="smiley" />', $newstr); |
||
193 | $newstr = str_replace(' :p', ' <img src="' . $this->url . 'data/smilies/face-raspberry.png" class="smiley" />', $newstr); |
||
194 | $newstr = str_replace(' :P', ' <img src="' . $this->url . 'data/smilies/face-raspberry.png" class="smiley" />',$newstr); |
||
195 | $newstr = str_replace(' :|', ' <img src="' . $this->url . 'data/smilies/face-plain.png" class="smiley" />',$newstr); |
||
196 | $newstr = str_replace(' :D', ' <img src="' . $this->url . 'data/smilies/face-laugh.png" class="smiley" />',$newstr); |
||
197 | $newstr = str_replace(' =D', ' <img src="' . $this->url . 'data/smilies/face-laugh.png" class="smiley" />',$newstr); |
||
198 | $newstr = str_replace(' :(', ' <img src="' . $this->url . 'data/smilies/face-sad.png" class="smiley" />',$newstr); |
||
199 | $newstr = str_replace(' :0', ' <img src="' . $this->url . 'data/smilies/face-surprise.png" class="smiley" />',$newstr); |
||
200 | $newstr = str_replace(' :o', ' <img src="' . $this->url . 'data/smilies/face-surprise.png" class="smiley" />',$newstr); |
||
201 | $newstr = str_replace(' :O', ' <img src="' . $this->url . 'data/smilies/face-surprise.png" class="smiley" />',$newstr); |
||
202 | $newstr = str_replace(' :/', ' <img src="' . $this->url . 'data/smilies/face-uncertain.png" class="smiley" />',$newstr); |
||
203 | $newstr = str_replace(' ;)', ' <img src="' . $this->url . 'data/smilies/face-wink.png" class="smiley" />',$newstr); |
||
204 | |||
205 | $bbcode = array( |
||
206 | '/\[b\](.+?)\[\/b\]/is', |
||
207 | '/\[i\](.+?)\[\/i\]/is', |
||
208 | '/\[u\](.+?)\[\/u\]/is', |
||
209 | '/\[url\](.+?)\[\/url\]/is', |
||
360 | tom | 210 | '/\[url=(.+?)\](.+?)\[\/url\]/is', |
297 | freddie | 211 | '/\[code\](.+?)\[\/code\]/is', |
401 | tom | 212 | '/\[img\](.+?)\[\/img\]/is', |
213 | '/\[ul\](.+?)\[\/ul\]/is', |
||
214 | '/\[ol\](.+?)\[\/ol\]/is', |
||
450 | tom | 215 | '/\[li\](.+?)\[\/li\]/is', |
216 | '/\[pre\](.+?)\[\/pre\]/is' |
||
297 | freddie | 217 | ); |
218 | |||
219 | $html = array( |
||
220 | '<b>$1</b>', |
||
221 | '<i>$1</i>', |
||
222 | '<u>$1</u>', |
||
429 | tom | 223 | '<a href="$1">$1</a>', |
224 | '<a href="$1">$2</a>', |
||
408 | tom | 225 | '<div class="code">$1</div>', |
401 | tom | 226 | '<img src="$1" />', |
227 | '<ul>$1</ul>', |
||
228 | '<ol>$1</ol>', |
||
450 | tom | 229 | '<li>$1</li>', |
230 | '<pre>$1</pre>', |
||
297 | freddie | 231 | ); |
232 | |||
233 | $newstr = preg_replace($bbcode, $html, $newstr); |
||
234 | |||
235 | return $newstr; |
||
236 | } |
||
237 | |||
238 | function redirect($u) |
||
239 | { |
||
240 | header('Location: ' . $u); |
||
241 | die(); |
||
242 | } |
||
243 | |||
244 | function isLoggedIn() |
||
245 | { |
||
246 | $cookie = $_COOKIE['Tim32_Login']; |
||
247 | if (!empty($cookie)) |
||
248 | { |
||
249 | $clist = explode('|~|', $cookie); |
||
250 | $user = $this->getUserByUsername($clist[0]); |
||
251 | if ($user) |
||
252 | { |
||
253 | if ($user->password == $clist[1]) |
||
254 | { |
||
255 | return true; |
||
256 | } |
||
257 | } |
||
258 | } |
||
259 | |||
260 | return false; |
||
261 | } |
||
262 | |||
263 | function isUserAdmin() |
||
264 | { |
||
265 | if ($this->isLoggedIn()) |
||
266 | { |
||
267 | if ($this->getLoggedInUser()->accessID <= 0) |
||
268 | { |
||
269 | return true; |
||
270 | } |
||
271 | } |
||
272 | |||
273 | return false; |
||
274 | } |
||
275 | |||
276 | function isUserGM() |
||
277 | { |
||
278 | if ($this->isLoggedIn()) |
||
279 | { |
||
280 | if ($this->getLoggedInUser()->accessID <= 1) |
||
281 | { |
||
282 | return true; |
||
283 | } |
||
284 | } |
||
285 | |||
286 | return false; |
||
287 | } |
||
288 | |||
289 | function isUserNormal() |
||
290 | { |
||
291 | if ($this->isLoggedIn()) |
||
292 | { |
||
293 | if ($this->getLoggedInUser()->accessID <= 2) |
||
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 | { |
||
354 | $result = $this->query('SELECT * FROM Users WHERE ID = ' . $id); |
||
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 | { |
||
396 | $result = $this->query('SELECT * FROM BlogPosts WHERE ID = ' . $id); |
||
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 |
||
411 | $post->title = $row['Title']; |
||
412 | $post->content = $row['Content']; |
||
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 | { |
||
425 | $result = $this->query('SELECT * FROM Projects WHERE ID = ' . $id); |
||
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 | { |
||
448 | $result = $this->query('SELECT * FROM ForumCategories WHERE ID = ' . $id); |
||
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 | { |
||
466 | $result = $this->query('SELECT * FROM ForumPosts WHERE ID = ' . $id); |
||
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 | { |
||
488 | $ids = $this->findIDs('BlogPosts', 'WHERE ParentID=' . $id); |
||
489 | for ($i = 0; $i < count($ids); $i++) |
||
490 | { |
||
491 | $this->delBlogPost($ids[$i]); |
||
492 | } |
||
493 | |||
494 | $this->query('DELETE FROM BlogPosts WHERE ID=' . $id); |
||
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 | ?> |