Rev 155 | Rev 163 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
33 | 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 | { |
||
77 | tom | 54 | $this->drawMenuItem('Administration', 'admin/'); |
33 | freddie | 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 | { |
||
49 | tom | 70 | write('<p><a href="' . $this->url . $u . '">' . $t . '</a></p>'); |
33 | freddie | 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 | |||
52 | tom | 98 | die(); |
33 | freddie | 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 | |||
90 | freddie | 115 | function drawBlogPostTree($id, $first = false) |
33 | freddie | 116 | { |
117 | $post = $this->getBlogPost($id); |
||
90 | freddie | 118 | if ($first) |
95 | freddie | 119 | write('<h3><a href="post.php?id=' . $id . '">' . $post->title. '</a> <a href="post.php?id=' . $post->parent->ID . '">^</a></h3>'); |
90 | freddie | 120 | else |
121 | write('<a href="post.php?id=' . $id . '"><h3>' . $post->title. '</h3></a>'); |
||
33 | freddie | 122 | write('<h5 style="color: #666666;">Posted On ' . date('l j F Y', $post->datePosted) . ' by ' . $post->user->name . ' (' . $post->user->username . ')</h5>'); |
123 | write('<p>' . $post->content . '</p>'); |
||
74 | tom | 124 | write('<br />'); |
73 | tom | 125 | if ($this->isUserNormal($this->getLoggedInUser())) |
72 | tom | 126 | { |
116 | freddie | 127 | echo '<p class="bold"><a href="add-post.php?id=' . $id . '">Add Comment</a>'; |
117 | freddie | 128 | if ($this->isUserAdmin($this->getLoggedInUser()) || $this->getLoggedInUser() == $post->author->ID) |
129 | echo ' · <a href="del-post.php?id=' . $id . '">Delete Post</a>'; |
||
116 | freddie | 130 | write('</p><br />'); |
72 | tom | 131 | } |
33 | freddie | 132 | |
43 | freddie | 133 | $ids = $this->findIDs('BlogPosts', 'WHERE ParentID=' . $id); |
33 | freddie | 134 | for ($i = 0; $i < count($ids); $i++) |
135 | { |
||
52 | tom | 136 | write('<div class="indent">'); |
43 | freddie | 137 | $this->drawBlogPostTree($ids[$i]); |
52 | tom | 138 | write('</div>'); |
33 | freddie | 139 | } |
140 | } |
||
57 | tom | 141 | |
58 | tom | 142 | function drawBlogCategoriesMenu() |
57 | tom | 143 | { |
144 | $cats = array(); |
||
145 | |||
62 | tom | 146 | $ids = $this->findIDs('BlogPosts', 'WHERE ParentID = -1'); |
57 | tom | 147 | for ($i = 0; $i < count($ids); $i++) |
148 | { |
||
149 | $cat = $this->getBlogPost($ids[$i])->category; |
||
150 | if (!in_array($cat, $cats)) |
||
151 | { |
||
152 | array_push($cats, $cat); |
||
153 | } |
||
154 | } |
||
155 | |||
61 | tom | 156 | write('<h3>Categories</h3>'); |
57 | tom | 157 | for ($i = 0; $i < count($cats); $i++) |
158 | { |
||
65 | tom | 159 | $this->drawMenuItem($cats[$i], 'blog/index.php?cat=' . $cats[$i]); |
57 | tom | 160 | } |
161 | } |
||
137 | tom | 162 | |
163 | function replaceBBCode($str) |
||
164 | { |
||
165 | $newstr = str_replace("\n", '</p><p>', $str); |
||
144 | tom | 166 | $newstr = str_replace(' ', ' ', $newstr); |
152 | tom | 167 | $newstr = str_replace(' :)', ' <img src="' . $this->url . 'data/smilies/face-smile.png" style="vertical-align: middle;" />', $newstr); |
168 | $newstr = str_replace(' :p', ' <img src="' . $this->url . 'data/smilies/face-raspberry.png" style="vertical-align: middle;" />', $newstr); |
||
169 | $newstr = str_replace(' :P', ' <img src="' . $this->url . 'data/smilies/face-raspberry.png" style="vertical-align: middle;" />',$newstr); |
||
170 | $newstr = str_replace(' :|', ' <img src="' . $this->url . 'data/smilies/face-plain.png" style="vertical-align: middle;" />',$newstr); |
||
171 | $newstr = str_replace(' :D', ' <img src="' . $this->url . 'data/smilies/face-laugh.png" style="vertical-align: middle;" />',$newstr); |
||
172 | $newstr = str_replace(' =D', ' <img src="' . $this->url . 'data/smilies/face-laugh.png" style="vertical-align: middle;" />',$newstr); |
||
173 | $newstr = str_replace(' :(', ' <img src="' . $this->url . 'data/smilies/face-sad.png" style="vertical-align: middle;" />',$newstr); |
||
174 | $newstr = str_replace(' :0', ' <img src="' . $this->url . 'data/smilies/face-surprise.png" style="vertical-align: middle;" />',$newstr); |
||
175 | $newstr = str_replace(' :o', ' <img src="' . $this->url . 'data/smilies/face-surprise.png" style="vertical-align: middle;" />',$newstr); |
||
176 | $newstr = str_replace(' :O', ' <img src="' . $this->url . 'data/smilies/face-surprise.png" style="vertical-align: middle;" />',$newstr); |
||
177 | $newstr = str_replace(' :/', ' <img src="' . $this->url . 'data/smilies/face-uncertain.png" style="vertical-align: middle;" />',$newstr); |
||
178 | $newstr = str_replace(' ;)', ' <img src="' . $this->url . 'data/smilies/face-wink.png" style="vertical-align: middle;" />',$newstr); |
||
137 | tom | 179 | |
139 | tom | 180 | $bbcode = array( |
143 | tom | 181 | '/\[b\](.+?)\[\/b\]/is', |
141 | tom | 182 | '/\[i\](.+?)\[\/i\]/is', |
183 | '/\[u\](.+?)\[\/u\]/is', |
||
184 | '/\[url\](.+?)\[\/url\]/is', |
||
155 | tom | 185 | '/\[code\](.+?)\[\/code\]/is', |
186 | '/\[img\](.+?)\[\/img\]/is' |
||
139 | tom | 187 | ); |
188 | |||
189 | $html = array( |
||
190 | '<b>$1</b>', |
||
141 | tom | 191 | '<i>$1</i>', |
192 | '<u>$1</u>', |
||
193 | '<a href="$1">$1</a>', |
||
156 | tom | 194 | '<div class="code">$1</div>', |
155 | tom | 195 | '<img src="$1" />' |
139 | tom | 196 | ); |
197 | |||
198 | $newstr = preg_replace($bbcode, $html, $newstr); |
||
199 | |||
137 | tom | 200 | return $newstr; |
201 | } |
||
33 | freddie | 202 | |
64 | tom | 203 | function redirect($u) |
33 | freddie | 204 | { |
64 | tom | 205 | header('Location: ' . $u); |
33 | freddie | 206 | die(); |
207 | } |
||
208 | |||
209 | function isLoggedIn() |
||
210 | { |
||
211 | $cookie = $_COOKIE['Tim32_Login']; |
||
212 | if (!empty($cookie)) |
||
213 | { |
||
214 | $clist = explode('|~|', $cookie); |
||
215 | $user = $this->getUserByUsername($clist[0]); |
||
216 | if ($user) |
||
217 | { |
||
218 | if ($user->password == $clist[1]) |
||
219 | { |
||
220 | return true; |
||
221 | } |
||
222 | } |
||
223 | } |
||
224 | |||
225 | return false; |
||
226 | } |
||
227 | |||
228 | function isUserAdmin() |
||
229 | { |
||
230 | if ($this->isLoggedIn()) |
||
231 | { |
||
232 | if ($this->getLoggedInUser()->accessID <= 0) |
||
233 | { |
||
234 | return true; |
||
235 | } |
||
236 | } |
||
237 | |||
238 | return false; |
||
239 | } |
||
240 | |||
241 | function isUserGM() |
||
242 | { |
||
243 | if ($this->isLoggedIn()) |
||
244 | { |
||
245 | if ($this->getLoggedInUser()->accessID <= 1) |
||
246 | { |
||
247 | return true; |
||
248 | } |
||
249 | } |
||
250 | |||
251 | return false; |
||
252 | } |
||
253 | |||
254 | function isUserNormal() |
||
255 | { |
||
256 | if ($this->isLoggedIn()) |
||
257 | { |
||
258 | if ($this->getLoggedInUser()->accessID <= 2) |
||
259 | { |
||
260 | return true; |
||
261 | } |
||
262 | } |
||
263 | |||
264 | return false; |
||
265 | } |
||
266 | |||
267 | function checkLoggedIn() |
||
268 | { |
||
269 | if (!$this->isLoggedIn()) |
||
270 | { |
||
271 | $this->drawError('You need to be logged in.'); |
||
272 | } |
||
273 | } |
||
274 | |||
275 | function query($query) |
||
276 | { |
||
277 | $result = mysql_query($query); |
||
278 | if (!$result) |
||
279 | { |
||
280 | $this->drawError('MySQL Error: ' . mysql_error()); |
||
281 | } |
||
282 | |||
283 | return $result; |
||
284 | } |
||
285 | |||
286 | function findIDs($table, $query = '') |
||
287 | { |
||
288 | $array = array(); |
||
289 | |||
290 | $result = $this->query('SELECT ID FROM ' . $table . ' ' . $query); |
||
291 | while ($row = mysql_fetch_array($result)) |
||
292 | { |
||
293 | array_push($array, $row['ID']); |
||
294 | } |
||
295 | |||
296 | return $array; |
||
297 | } |
||
298 | |||
299 | function getUserByID($id) |
||
300 | { |
||
301 | $result = $this->query('SELECT * FROM Users WHERE ID = ' . $id); |
||
302 | while ($row = mysql_fetch_array($result)) |
||
303 | { |
||
304 | $user = new User; |
||
305 | $user->ID = $row['ID']; |
||
306 | $user->accessID = $row['AccessID']; |
||
307 | $user->username = $row['Username']; |
||
308 | $user->password = $row['Password']; |
||
309 | $user->emailAddress = $row['EmailAddress']; |
||
310 | $user->name = $row['Name']; |
||
311 | $user->challengeID = $row['ChallengeID']; |
||
312 | |||
313 | return $user; |
||
314 | } |
||
315 | |||
316 | return false; |
||
317 | } |
||
318 | |||
319 | function getUserByUsername($username) |
||
320 | { |
||
321 | $result = $this->query('SELECT * FROM Users WHERE Username = "' . $username . '"'); |
||
322 | while ($row = mysql_fetch_array($result)) |
||
323 | { |
||
324 | return $this->getUserByID($row['ID']); |
||
325 | } |
||
326 | |||
327 | return false; |
||
328 | } |
||
329 | |||
330 | function getLoggedInUser() |
||
331 | { |
||
332 | if ($this->isLoggedIn()) |
||
333 | { |
||
334 | $clist = explode('|~|', $_COOKIE['Tim32_Login']); |
||
335 | return $this->getUserByUsername($clist[0]); |
||
336 | } |
||
337 | |||
338 | return false; |
||
339 | } |
||
340 | |||
341 | function getBlogPost($id) |
||
342 | { |
||
343 | $result = $this->query('SELECT * FROM BlogPosts WHERE ID = ' . $id); |
||
344 | while ($row = mysql_fetch_array($result)) |
||
345 | { |
||
346 | $post = new BlogPost; |
||
347 | $post->ID = $row['ID']; |
||
348 | if ($row['ParentID'] == -1) |
||
349 | { |
||
350 | $post->parent = -1; |
||
351 | } |
||
352 | else |
||
353 | { |
||
354 | $post->parent = $this->getBlogPost($row['ParentID']); |
||
355 | } |
||
104 | tom | 356 | $post->author = $this->getUserByID($row['AuthorID']); |
105 | tom | 357 | $post->user = $this->getUserByID($row['AuthorID']); // For some older pages |
33 | freddie | 358 | $post->title = $row['Title']; |
137 | tom | 359 | $post->content = $this->replaceBBCode($row['Content']); |
33 | freddie | 360 | $post->datePosted = strtotime($row['DatePosted']); |
361 | $post->category = $row['Category']; |
||
362 | $post->spam = $row['Spam']; |
||
363 | |||
364 | return $post; |
||
365 | } |
||
366 | |||
367 | $this->drawError('Cannot find blog post, #' . $id); |
||
368 | } |
||
369 | |||
113 | freddie | 370 | function delBlogPost($id) |
371 | { |
||
372 | $ids = $this->findIDs('BlogPosts', 'WHERE ParentID=' . $id); |
||
373 | for ($i = 0; $i < count($ids); $i++) |
||
374 | $this->delBlogPost($ids[$i]); |
||
375 | |||
376 | $this->query('delete from BlogPosts where ID=' . $id); |
||
377 | } |
||
378 | |||
33 | freddie | 379 | function getGetID() |
380 | { |
||
381 | $id = $_GET['id']; |
||
382 | if (empty($id)) |
||
383 | { |
||
384 | $id = 1; |
||
385 | } |
||
386 | |||
387 | return $id; |
||
388 | } |
||
389 | |||
390 | function getPostID() |
||
391 | { |
||
41 | freddie | 392 | $id = $_POST['id']; |
33 | freddie | 393 | if (empty($id)) |
394 | { |
||
395 | $id = 1; |
||
396 | } |
||
397 | |||
398 | return $id; |
||
399 | } |
||
400 | |||
401 | } |
||
402 | |||
403 | class User |
||
404 | { |
||
405 | public $ID; |
||
406 | public $accessID; |
||
407 | public $username; |
||
408 | public $password; |
||
409 | public $emailAddress; |
||
410 | public $name; |
||
411 | public $challengeID; |
||
412 | } |
||
413 | |||
414 | class BlogPost |
||
415 | { |
||
416 | public $ID; |
||
417 | public $parent; |
||
418 | public $author; |
||
419 | public $title; |
||
420 | public $content; |
||
421 | public $datePosted; |
||
422 | public $category; |
||
423 | public $spam; |
||
424 | } |
||
425 | |||
426 | function write($str) |
||
427 | { |
||
428 | echo $str; |
||
429 | echo "\n"; |
||
430 | } |
||
431 | |||
432 | ?> |