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