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