Rev 147 | Rev 149 | 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); |
148 | tom | 167 | $newstr = str_replace(':)', '<img src="' . $this->url . 'data/smilies/face-smile.png" style="vertical-align: middle;" />', $newstr); |
137 | tom | 168 | |
139 | tom | 169 | $bbcode = array( |
143 | tom | 170 | '/\[b\](.+?)\[\/b\]/is', |
141 | tom | 171 | '/\[i\](.+?)\[\/i\]/is', |
172 | '/\[u\](.+?)\[\/u\]/is', |
||
173 | '/\[url\](.+?)\[\/url\]/is', |
||
142 | tom | 174 | '/\[code\](.+?)\[\/code\]/is' |
139 | tom | 175 | ); |
176 | |||
177 | $html = array( |
||
178 | '<b>$1</b>', |
||
141 | tom | 179 | '<i>$1</i>', |
180 | '<u>$1</u>', |
||
181 | '<a href="$1">$1</a>', |
||
142 | tom | 182 | '<div class="code">$1</div>' |
139 | tom | 183 | ); |
184 | |||
185 | $newstr = preg_replace($bbcode, $html, $newstr); |
||
186 | |||
137 | tom | 187 | return $newstr; |
188 | } |
||
33 | freddie | 189 | |
64 | tom | 190 | function redirect($u) |
33 | freddie | 191 | { |
64 | tom | 192 | header('Location: ' . $u); |
33 | freddie | 193 | die(); |
194 | } |
||
195 | |||
196 | function isLoggedIn() |
||
197 | { |
||
198 | $cookie = $_COOKIE['Tim32_Login']; |
||
199 | if (!empty($cookie)) |
||
200 | { |
||
201 | $clist = explode('|~|', $cookie); |
||
202 | $user = $this->getUserByUsername($clist[0]); |
||
203 | if ($user) |
||
204 | { |
||
205 | if ($user->password == $clist[1]) |
||
206 | { |
||
207 | return true; |
||
208 | } |
||
209 | } |
||
210 | } |
||
211 | |||
212 | return false; |
||
213 | } |
||
214 | |||
215 | function isUserAdmin() |
||
216 | { |
||
217 | if ($this->isLoggedIn()) |
||
218 | { |
||
219 | if ($this->getLoggedInUser()->accessID <= 0) |
||
220 | { |
||
221 | return true; |
||
222 | } |
||
223 | } |
||
224 | |||
225 | return false; |
||
226 | } |
||
227 | |||
228 | function isUserGM() |
||
229 | { |
||
230 | if ($this->isLoggedIn()) |
||
231 | { |
||
232 | if ($this->getLoggedInUser()->accessID <= 1) |
||
233 | { |
||
234 | return true; |
||
235 | } |
||
236 | } |
||
237 | |||
238 | return false; |
||
239 | } |
||
240 | |||
241 | function isUserNormal() |
||
242 | { |
||
243 | if ($this->isLoggedIn()) |
||
244 | { |
||
245 | if ($this->getLoggedInUser()->accessID <= 2) |
||
246 | { |
||
247 | return true; |
||
248 | } |
||
249 | } |
||
250 | |||
251 | return false; |
||
252 | } |
||
253 | |||
254 | function checkLoggedIn() |
||
255 | { |
||
256 | if (!$this->isLoggedIn()) |
||
257 | { |
||
258 | $this->drawError('You need to be logged in.'); |
||
259 | } |
||
260 | } |
||
261 | |||
262 | function query($query) |
||
263 | { |
||
264 | $result = mysql_query($query); |
||
265 | if (!$result) |
||
266 | { |
||
267 | $this->drawError('MySQL Error: ' . mysql_error()); |
||
268 | } |
||
269 | |||
270 | return $result; |
||
271 | } |
||
272 | |||
273 | function findIDs($table, $query = '') |
||
274 | { |
||
275 | $array = array(); |
||
276 | |||
277 | $result = $this->query('SELECT ID FROM ' . $table . ' ' . $query); |
||
278 | while ($row = mysql_fetch_array($result)) |
||
279 | { |
||
280 | array_push($array, $row['ID']); |
||
281 | } |
||
282 | |||
283 | return $array; |
||
284 | } |
||
285 | |||
286 | function getUserByID($id) |
||
287 | { |
||
288 | $result = $this->query('SELECT * FROM Users WHERE ID = ' . $id); |
||
289 | while ($row = mysql_fetch_array($result)) |
||
290 | { |
||
291 | $user = new User; |
||
292 | $user->ID = $row['ID']; |
||
293 | $user->accessID = $row['AccessID']; |
||
294 | $user->username = $row['Username']; |
||
295 | $user->password = $row['Password']; |
||
296 | $user->emailAddress = $row['EmailAddress']; |
||
297 | $user->name = $row['Name']; |
||
298 | $user->challengeID = $row['ChallengeID']; |
||
299 | |||
300 | return $user; |
||
301 | } |
||
302 | |||
303 | return false; |
||
304 | } |
||
305 | |||
306 | function getUserByUsername($username) |
||
307 | { |
||
308 | $result = $this->query('SELECT * FROM Users WHERE Username = "' . $username . '"'); |
||
309 | while ($row = mysql_fetch_array($result)) |
||
310 | { |
||
311 | return $this->getUserByID($row['ID']); |
||
312 | } |
||
313 | |||
314 | return false; |
||
315 | } |
||
316 | |||
317 | function getLoggedInUser() |
||
318 | { |
||
319 | if ($this->isLoggedIn()) |
||
320 | { |
||
321 | $clist = explode('|~|', $_COOKIE['Tim32_Login']); |
||
322 | return $this->getUserByUsername($clist[0]); |
||
323 | } |
||
324 | |||
325 | return false; |
||
326 | } |
||
327 | |||
328 | function getBlogPost($id) |
||
329 | { |
||
330 | $result = $this->query('SELECT * FROM BlogPosts WHERE ID = ' . $id); |
||
331 | while ($row = mysql_fetch_array($result)) |
||
332 | { |
||
333 | $post = new BlogPost; |
||
334 | $post->ID = $row['ID']; |
||
335 | if ($row['ParentID'] == -1) |
||
336 | { |
||
337 | $post->parent = -1; |
||
338 | } |
||
339 | else |
||
340 | { |
||
341 | $post->parent = $this->getBlogPost($row['ParentID']); |
||
342 | } |
||
104 | tom | 343 | $post->author = $this->getUserByID($row['AuthorID']); |
105 | tom | 344 | $post->user = $this->getUserByID($row['AuthorID']); // For some older pages |
33 | freddie | 345 | $post->title = $row['Title']; |
137 | tom | 346 | $post->content = $this->replaceBBCode($row['Content']); |
33 | freddie | 347 | $post->datePosted = strtotime($row['DatePosted']); |
348 | $post->category = $row['Category']; |
||
349 | $post->spam = $row['Spam']; |
||
350 | |||
351 | return $post; |
||
352 | } |
||
353 | |||
354 | $this->drawError('Cannot find blog post, #' . $id); |
||
355 | } |
||
356 | |||
113 | freddie | 357 | function delBlogPost($id) |
358 | { |
||
359 | $ids = $this->findIDs('BlogPosts', 'WHERE ParentID=' . $id); |
||
360 | for ($i = 0; $i < count($ids); $i++) |
||
361 | $this->delBlogPost($ids[$i]); |
||
362 | |||
363 | $this->query('delete from BlogPosts where ID=' . $id); |
||
364 | } |
||
365 | |||
33 | freddie | 366 | function getGetID() |
367 | { |
||
368 | $id = $_GET['id']; |
||
369 | if (empty($id)) |
||
370 | { |
||
371 | $id = 1; |
||
372 | } |
||
373 | |||
374 | return $id; |
||
375 | } |
||
376 | |||
377 | function getPostID() |
||
378 | { |
||
41 | freddie | 379 | $id = $_POST['id']; |
33 | freddie | 380 | if (empty($id)) |
381 | { |
||
382 | $id = 1; |
||
383 | } |
||
384 | |||
385 | return $id; |
||
386 | } |
||
387 | |||
388 | } |
||
389 | |||
390 | class User |
||
391 | { |
||
392 | public $ID; |
||
393 | public $accessID; |
||
394 | public $username; |
||
395 | public $password; |
||
396 | public $emailAddress; |
||
397 | public $name; |
||
398 | public $challengeID; |
||
399 | } |
||
400 | |||
401 | class BlogPost |
||
402 | { |
||
403 | public $ID; |
||
404 | public $parent; |
||
405 | public $author; |
||
406 | public $title; |
||
407 | public $content; |
||
408 | public $datePosted; |
||
409 | public $category; |
||
410 | public $spam; |
||
411 | } |
||
412 | |||
413 | function write($str) |
||
414 | { |
||
415 | echo $str; |
||
416 | echo "\n"; |
||
417 | } |
||
418 | |||
419 | ?> |