Rev 259 | Rev 308 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
169 | tom | 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 | { |
||
91 | write('</div>'); |
||
92 | write('</body>'); |
||
93 | write('</html>'); |
||
94 | |||
95 | $this->drawnFooter = true; |
||
96 | } |
||
97 | |||
98 | die(); |
||
99 | } |
||
100 | |||
101 | function drawError($text, $die = true) |
||
102 | { |
||
103 | $this->drawHeader(); |
||
104 | $this->drawMiddle(); |
||
105 | |||
106 | write('<h4 style="color: red;">Error: ' . $text . '</h4>'); |
||
107 | |||
108 | if ($die) |
||
109 | { |
||
110 | $this->drawFooter(); |
||
111 | die(); |
||
112 | } |
||
113 | } |
||
114 | |||
115 | function drawBlogPostTree($id, $first = false) |
||
116 | { |
||
117 | $post = $this->getBlogPost($id); |
||
118 | if ($first) |
||
175 | tom | 119 | { |
169 | tom | 120 | write('<h3><a href="post.php?id=' . $id . '">' . $post->title. '</a> <a href="post.php?id=' . $post->parent->ID . '">^</a></h3>'); |
175 | tom | 121 | } |
169 | tom | 122 | else |
175 | tom | 123 | { |
169 | tom | 124 | write('<a href="post.php?id=' . $id . '"><h3>' . $post->title. '</h3></a>'); |
175 | tom | 125 | } |
169 | tom | 126 | write('<h5 style="color: #666666;">Posted On ' . date('l j F Y', $post->datePosted) . ' by ' . $post->user->name . ' (' . $post->user->username . ')</h5>'); |
175 | tom | 127 | write('<p>' . $this->replaceBBCode($post->content) . '</p>'); |
275 | tom | 128 | |
169 | tom | 129 | if ($this->isUserNormal($this->getLoggedInUser())) |
130 | { |
||
131 | echo '<p class="bold"><a href="add-post.php?id=' . $id . '">Add Comment</a>'; |
||
179 | tom | 132 | if ($this->isUserAdmin($this->getLoggedInUser()) || $this->getLoggedInUser()->ID == $post->author->ID) |
174 | tom | 133 | { |
134 | echo ' · <a href="edit-post.php?id=' . $id . '">Edit Post</a>'; |
||
177 | tom | 135 | echo ' · <a href="del-post.php?id=' . $id . '">Delete Post</a>'; |
174 | tom | 136 | } |
169 | tom | 137 | write('</p><br />'); |
138 | } |
||
139 | |||
140 | $ids = $this->findIDs('BlogPosts', 'WHERE ParentID=' . $id); |
||
141 | for ($i = 0; $i < count($ids); $i++) |
||
142 | { |
||
143 | write('<div class="indent">'); |
||
144 | $this->drawBlogPostTree($ids[$i]); |
||
145 | write('</div>'); |
||
146 | } |
||
147 | } |
||
148 | |||
149 | function drawBlogCategoriesMenu() |
||
150 | { |
||
151 | $cats = array(); |
||
152 | |||
153 | $ids = $this->findIDs('BlogPosts', 'WHERE ParentID = -1'); |
||
154 | for ($i = 0; $i < count($ids); $i++) |
||
155 | { |
||
156 | $cat = $this->getBlogPost($ids[$i])->category; |
||
157 | if (!in_array($cat, $cats)) |
||
158 | { |
||
159 | array_push($cats, $cat); |
||
160 | } |
||
161 | } |
||
162 | |||
163 | write('<h3>Categories</h3>'); |
||
164 | for ($i = 0; $i < count($cats); $i++) |
||
165 | { |
||
166 | $this->drawMenuItem($cats[$i], 'blog/index.php?cat=' . $cats[$i]); |
||
167 | } |
||
168 | } |
||
169 | |||
170 | function replaceBBCode($str) |
||
171 | { |
||
172 | $newstr = str_replace("\n", '</p><p>', $str); |
||
173 | $newstr = str_replace(' ', ' ', $newstr); |
||
174 | $newstr = str_replace(' :)', ' <img src="' . $this->url . 'data/smilies/face-smile.png" class="smiley" />', $newstr); |
||
175 | $newstr = str_replace(' :p', ' <img src="' . $this->url . 'data/smilies/face-raspberry.png" class="smiley" />', $newstr); |
||
176 | $newstr = str_replace(' :P', ' <img src="' . $this->url . 'data/smilies/face-raspberry.png" class="smiley" />',$newstr); |
||
177 | $newstr = str_replace(' :|', ' <img src="' . $this->url . 'data/smilies/face-plain.png" class="smiley" />',$newstr); |
||
178 | $newstr = str_replace(' :D', ' <img src="' . $this->url . 'data/smilies/face-laugh.png" class="smiley" />',$newstr); |
||
179 | $newstr = str_replace(' =D', ' <img src="' . $this->url . 'data/smilies/face-laugh.png" class="smiley" />',$newstr); |
||
180 | $newstr = str_replace(' :(', ' <img src="' . $this->url . 'data/smilies/face-sad.png" class="smiley" />',$newstr); |
||
181 | $newstr = str_replace(' :0', ' <img src="' . $this->url . 'data/smilies/face-surprise.png" class="smiley" />',$newstr); |
||
182 | $newstr = str_replace(' :o', ' <img src="' . $this->url . 'data/smilies/face-surprise.png" class="smiley" />',$newstr); |
||
183 | $newstr = str_replace(' :O', ' <img src="' . $this->url . 'data/smilies/face-surprise.png" class="smiley" />',$newstr); |
||
184 | $newstr = str_replace(' :/', ' <img src="' . $this->url . 'data/smilies/face-uncertain.png" class="smiley" />',$newstr); |
||
185 | $newstr = str_replace(' ;)', ' <img src="' . $this->url . 'data/smilies/face-wink.png" class="smiley" />',$newstr); |
||
186 | |||
187 | $bbcode = array( |
||
188 | '/\[b\](.+?)\[\/b\]/is', |
||
189 | '/\[i\](.+?)\[\/i\]/is', |
||
190 | '/\[u\](.+?)\[\/u\]/is', |
||
191 | '/\[url\](.+?)\[\/url\]/is', |
||
192 | '/\[code\](.+?)\[\/code\]/is', |
||
193 | '/\[img\](.+?)\[\/img\]/is' |
||
194 | ); |
||
195 | |||
196 | $html = array( |
||
197 | '<b>$1</b>', |
||
198 | '<i>$1</i>', |
||
199 | '<u>$1</u>', |
||
200 | '<a href="$1">$1</a>', |
||
201 | '<div class="code">$1</div>', |
||
202 | '<img src="$1" />' |
||
203 | ); |
||
204 | |||
205 | $newstr = preg_replace($bbcode, $html, $newstr); |
||
206 | |||
207 | return $newstr; |
||
208 | } |
||
209 | |||
210 | function redirect($u) |
||
211 | { |
||
212 | header('Location: ' . $u); |
||
213 | die(); |
||
214 | } |
||
215 | |||
216 | function isLoggedIn() |
||
217 | { |
||
218 | $cookie = $_COOKIE['Tim32_Login']; |
||
219 | if (!empty($cookie)) |
||
220 | { |
||
221 | $clist = explode('|~|', $cookie); |
||
222 | $user = $this->getUserByUsername($clist[0]); |
||
223 | if ($user) |
||
224 | { |
||
225 | if ($user->password == $clist[1]) |
||
226 | { |
||
227 | return true; |
||
228 | } |
||
229 | } |
||
230 | } |
||
231 | |||
232 | return false; |
||
233 | } |
||
234 | |||
235 | function isUserAdmin() |
||
236 | { |
||
237 | if ($this->isLoggedIn()) |
||
238 | { |
||
239 | if ($this->getLoggedInUser()->accessID <= 0) |
||
240 | { |
||
241 | return true; |
||
242 | } |
||
243 | } |
||
244 | |||
245 | return false; |
||
246 | } |
||
247 | |||
248 | function isUserGM() |
||
249 | { |
||
250 | if ($this->isLoggedIn()) |
||
251 | { |
||
252 | if ($this->getLoggedInUser()->accessID <= 1) |
||
253 | { |
||
254 | return true; |
||
255 | } |
||
256 | } |
||
257 | |||
258 | return false; |
||
259 | } |
||
260 | |||
261 | function isUserNormal() |
||
262 | { |
||
263 | if ($this->isLoggedIn()) |
||
264 | { |
||
265 | if ($this->getLoggedInUser()->accessID <= 2) |
||
266 | { |
||
267 | return true; |
||
268 | } |
||
269 | } |
||
270 | |||
271 | return false; |
||
272 | } |
||
273 | |||
274 | function checkLoggedIn() |
||
275 | { |
||
276 | if (!$this->isLoggedIn()) |
||
277 | { |
||
278 | $this->drawError('You need to be logged in.'); |
||
279 | } |
||
280 | } |
||
281 | |||
282 | function query($query) |
||
283 | { |
||
284 | $result = mysql_query($query); |
||
285 | if (!$result) |
||
286 | { |
||
250 | tom | 287 | $this->drawError('Query Failed: ' . $query . "\n" . 'MySQL Error: ' . mysql_error()); |
169 | tom | 288 | } |
289 | |||
290 | return $result; |
||
291 | } |
||
292 | |||
293 | function findIDs($table, $query = '') |
||
294 | { |
||
295 | $array = array(); |
||
296 | |||
297 | $result = $this->query('SELECT ID FROM ' . $table . ' ' . $query); |
||
298 | while ($row = mysql_fetch_array($result)) |
||
299 | { |
||
300 | array_push($array, $row['ID']); |
||
301 | } |
||
302 | |||
303 | return $array; |
||
304 | } |
||
305 | |||
306 | function getUserByID($id) |
||
307 | { |
||
308 | $result = $this->query('SELECT * FROM Users WHERE ID = ' . $id); |
||
309 | while ($row = mysql_fetch_array($result)) |
||
310 | { |
||
311 | $user = new User; |
||
312 | $user->ID = $row['ID']; |
||
313 | $user->accessID = $row['AccessID']; |
||
314 | $user->username = $row['Username']; |
||
315 | $user->password = $row['Password']; |
||
316 | $user->emailAddress = $row['EmailAddress']; |
||
317 | $user->name = $row['Name']; |
||
318 | $user->challengeID = $row['ChallengeID']; |
||
319 | |||
320 | return $user; |
||
321 | } |
||
322 | |||
323 | return false; |
||
324 | } |
||
325 | |||
326 | function getUserByUsername($username) |
||
327 | { |
||
328 | $result = $this->query('SELECT * FROM Users WHERE Username = "' . $username . '"'); |
||
329 | while ($row = mysql_fetch_array($result)) |
||
330 | { |
||
331 | return $this->getUserByID($row['ID']); |
||
332 | } |
||
333 | |||
334 | return false; |
||
335 | } |
||
336 | |||
337 | function getLoggedInUser() |
||
338 | { |
||
339 | if ($this->isLoggedIn()) |
||
340 | { |
||
341 | $clist = explode('|~|', $_COOKIE['Tim32_Login']); |
||
342 | return $this->getUserByUsername($clist[0]); |
||
343 | } |
||
344 | |||
345 | return false; |
||
346 | } |
||
347 | |||
348 | function getBlogPost($id) |
||
349 | { |
||
350 | $result = $this->query('SELECT * FROM BlogPosts WHERE ID = ' . $id); |
||
351 | while ($row = mysql_fetch_array($result)) |
||
352 | { |
||
353 | $post = new BlogPost; |
||
354 | $post->ID = $row['ID']; |
||
355 | if ($row['ParentID'] == -1) |
||
356 | { |
||
357 | $post->parent = -1; |
||
358 | } |
||
359 | else |
||
360 | { |
||
361 | $post->parent = $this->getBlogPost($row['ParentID']); |
||
362 | } |
||
363 | $post->author = $this->getUserByID($row['AuthorID']); |
||
364 | $post->user = $this->getUserByID($row['AuthorID']); // For some older pages |
||
365 | $post->title = $row['Title']; |
||
175 | tom | 366 | $post->content = $row['Content']; |
169 | tom | 367 | $post->datePosted = strtotime($row['DatePosted']); |
368 | $post->category = $row['Category']; |
||
369 | $post->spam = $row['Spam']; |
||
370 | |||
371 | return $post; |
||
372 | } |
||
373 | |||
374 | $this->drawError('Cannot find blog post, #' . $id); |
||
375 | } |
||
376 | |||
191 | tom | 377 | function getProject($id) |
378 | { |
||
379 | $result = $this->query('SELECT * FROM Projects WHERE ID = ' . $id); |
||
380 | while ($row = mysql_fetch_array($result)) |
||
381 | { |
||
382 | $project = new Project; |
||
383 | |||
384 | $project->ID = $row['ID']; |
||
385 | $project->author = $this->getUserByID($row['AuthorID']); |
||
386 | $project->title = $row['Title']; |
||
387 | $project->description = $row['Description']; |
||
388 | $project->logoURL = $row['LogoURL']; |
||
389 | $project->downloadURL = $row['DownloadURL']; |
||
200 | tom | 390 | $project->websiteURL = $row['WebsiteURL']; |
191 | tom | 391 | $project->latestVersion = $row['LatestVersion']; |
392 | $project->lastUpdate = strtotime($row['LastUpdate']); |
||
393 | |||
394 | return $project; |
||
395 | } |
||
396 | |||
397 | return false; |
||
398 | } |
||
399 | |||
246 | tom | 400 | function getForumCategory($id) |
401 | { |
||
248 | tom | 402 | $result = $this->query('SELECT * FROM ForumCategories WHERE ID = ' . $id); |
246 | tom | 403 | while ($row = mysql_fetch_array($result)) |
404 | { |
||
405 | $f = new ForumCategory; |
||
406 | |||
407 | $f->ID = $row['ID']; |
||
259 | tom | 408 | $f->parent = $this->getForumCategory($row['ParentID']); |
246 | tom | 409 | $f->title = $row['Title']; |
410 | $f->description = $row['Description']; |
||
411 | |||
412 | return $f; |
||
413 | } |
||
414 | |||
415 | return false; |
||
416 | } |
||
417 | |||
252 | tom | 418 | function getForumPost($id) |
419 | { |
||
420 | $result = $this->query('SELECT * FROM ForumPosts WHERE ID = ' . $id); |
||
421 | while ($row = mysql_fetch_array($result)) |
||
422 | { |
||
423 | $f = new ForumPost; |
||
424 | |||
425 | $f->ID = $row['ID']; |
||
426 | $f->author = $this->getUserByID($row['AuthorID']); |
||
427 | $f->category = $this->getForumCategory($row['CategoryID']); |
||
428 | $f->parent = $this->getForumPost($row['ParentID']); |
||
429 | $f->title = $row['Title']; |
||
430 | $f->content = $row['Content']; |
||
431 | $f->datePosted = strtotime($row['DatePosted']); |
||
432 | $f->spam = $row['Spam']; |
||
433 | |||
434 | return $f; |
||
435 | } |
||
436 | |||
437 | return false; |
||
438 | } |
||
439 | |||
169 | tom | 440 | function delBlogPost($id) |
441 | { |
||
442 | $ids = $this->findIDs('BlogPosts', 'WHERE ParentID=' . $id); |
||
443 | for ($i = 0; $i < count($ids); $i++) |
||
191 | tom | 444 | { |
169 | tom | 445 | $this->delBlogPost($ids[$i]); |
191 | tom | 446 | } |
169 | tom | 447 | |
191 | tom | 448 | $this->query('DELETE FROM BlogPosts WHERE ID=' . $id); |
169 | tom | 449 | } |
450 | |||
451 | function getGetID() |
||
452 | { |
||
453 | $id = $_GET['id']; |
||
454 | if (empty($id)) |
||
455 | { |
||
456 | $id = 1; |
||
457 | } |
||
458 | |||
459 | return $id; |
||
460 | } |
||
461 | |||
462 | function getPostID() |
||
463 | { |
||
464 | $id = $_POST['id']; |
||
465 | if (empty($id)) |
||
466 | { |
||
467 | $id = 1; |
||
468 | } |
||
469 | |||
470 | return $id; |
||
471 | } |
||
472 | |||
473 | } |
||
474 | |||
475 | class User |
||
476 | { |
||
477 | public $ID; |
||
478 | public $accessID; |
||
479 | public $username; |
||
480 | public $password; |
||
481 | public $emailAddress; |
||
482 | public $name; |
||
483 | public $challengeID; |
||
484 | } |
||
485 | |||
486 | class BlogPost |
||
487 | { |
||
488 | public $ID; |
||
489 | public $parent; |
||
490 | public $author; |
||
491 | public $title; |
||
492 | public $content; |
||
493 | public $datePosted; |
||
494 | public $category; |
||
495 | public $spam; |
||
496 | } |
||
497 | |||
191 | tom | 498 | class Project |
499 | { |
||
500 | public $ID; |
||
501 | public $author; |
||
502 | public $title; |
||
503 | public $description; |
||
504 | public $logoURL; |
||
505 | public $downloadURL; |
||
506 | public $websiteURL; |
||
507 | public $latestVersion; |
||
508 | public $lastUpdate; |
||
509 | } |
||
510 | |||
246 | tom | 511 | class ForumCategory |
512 | { |
||
513 | public $ID; |
||
259 | tom | 514 | public $parent; |
246 | tom | 515 | public $title; |
516 | public $description; |
||
517 | } |
||
518 | |||
252 | tom | 519 | class ForumPost |
520 | { |
||
521 | public $id; |
||
522 | public $author; |
||
523 | public $category; |
||
524 | public $parent; |
||
525 | public $title; |
||
253 | tom | 526 | public $content; |
252 | tom | 527 | public $datePosted; |
528 | public $spam; |
||
529 | } |
||
530 | |||
169 | tom | 531 | function write($str) |
532 | { |
||
533 | echo $str; |
||
534 | echo "\n"; |
||
535 | } |
||
536 | |||
537 | ?> |