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