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