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