Rev 92 | Rev 95 | 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) |
93 | freddie | 119 | write('<h3><a href="post.php?id=' . $id . '">' . $post->title. '</a> <a href="post.php?id=' . $post->parent . '">^</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 | { |
89 | freddie | 127 | write('<p class="bold"><a href="post-add.php?id=' . $id . '">Add Comment</a></p>'); |
72 | tom | 128 | write('<br />'); |
129 | } |
||
33 | freddie | 130 | |
43 | freddie | 131 | $ids = $this->findIDs('BlogPosts', 'WHERE ParentID=' . $id); |
33 | freddie | 132 | for ($i = 0; $i < count($ids); $i++) |
133 | { |
||
52 | tom | 134 | write('<div class="indent">'); |
43 | freddie | 135 | $this->drawBlogPostTree($ids[$i]); |
52 | tom | 136 | write('</div>'); |
33 | freddie | 137 | } |
138 | } |
||
57 | tom | 139 | |
58 | tom | 140 | function drawBlogCategoriesMenu() |
57 | tom | 141 | { |
142 | $cats = array(); |
||
143 | |||
62 | tom | 144 | $ids = $this->findIDs('BlogPosts', 'WHERE ParentID = -1'); |
57 | tom | 145 | for ($i = 0; $i < count($ids); $i++) |
146 | { |
||
147 | $cat = $this->getBlogPost($ids[$i])->category; |
||
148 | if (!in_array($cat, $cats)) |
||
149 | { |
||
150 | array_push($cats, $cat); |
||
151 | } |
||
152 | } |
||
153 | |||
61 | tom | 154 | write('<h3>Categories</h3>'); |
57 | tom | 155 | for ($i = 0; $i < count($cats); $i++) |
156 | { |
||
65 | tom | 157 | $this->drawMenuItem($cats[$i], 'blog/index.php?cat=' . $cats[$i]); |
57 | tom | 158 | } |
159 | } |
||
33 | freddie | 160 | |
64 | tom | 161 | function redirect($u) |
33 | freddie | 162 | { |
64 | tom | 163 | header('Location: ' . $u); |
33 | freddie | 164 | die(); |
165 | } |
||
166 | |||
167 | function isLoggedIn() |
||
168 | { |
||
169 | $cookie = $_COOKIE['Tim32_Login']; |
||
170 | if (!empty($cookie)) |
||
171 | { |
||
172 | $clist = explode('|~|', $cookie); |
||
173 | $user = $this->getUserByUsername($clist[0]); |
||
174 | if ($user) |
||
175 | { |
||
176 | if ($user->password == $clist[1]) |
||
177 | { |
||
178 | return true; |
||
179 | } |
||
180 | } |
||
181 | } |
||
182 | |||
183 | return false; |
||
184 | } |
||
185 | |||
186 | function isUserAdmin() |
||
187 | { |
||
188 | if ($this->isLoggedIn()) |
||
189 | { |
||
190 | if ($this->getLoggedInUser()->accessID <= 0) |
||
191 | { |
||
192 | return true; |
||
193 | } |
||
194 | } |
||
195 | |||
196 | return false; |
||
197 | } |
||
198 | |||
199 | function isUserGM() |
||
200 | { |
||
201 | if ($this->isLoggedIn()) |
||
202 | { |
||
203 | if ($this->getLoggedInUser()->accessID <= 1) |
||
204 | { |
||
205 | return true; |
||
206 | } |
||
207 | } |
||
208 | |||
209 | return false; |
||
210 | } |
||
211 | |||
212 | function isUserNormal() |
||
213 | { |
||
214 | if ($this->isLoggedIn()) |
||
215 | { |
||
216 | if ($this->getLoggedInUser()->accessID <= 2) |
||
217 | { |
||
218 | return true; |
||
219 | } |
||
220 | } |
||
221 | |||
222 | return false; |
||
223 | } |
||
224 | |||
225 | function checkLoggedIn() |
||
226 | { |
||
227 | if (!$this->isLoggedIn()) |
||
228 | { |
||
229 | $this->drawError('You need to be logged in.'); |
||
230 | } |
||
231 | } |
||
232 | |||
233 | function query($query) |
||
234 | { |
||
235 | $result = mysql_query($query); |
||
236 | if (!$result) |
||
237 | { |
||
238 | $this->drawError('MySQL Error: ' . mysql_error()); |
||
239 | } |
||
240 | |||
241 | return $result; |
||
242 | } |
||
243 | |||
244 | function findIDs($table, $query = '') |
||
245 | { |
||
246 | $array = array(); |
||
247 | |||
248 | $result = $this->query('SELECT ID FROM ' . $table . ' ' . $query); |
||
249 | while ($row = mysql_fetch_array($result)) |
||
250 | { |
||
251 | array_push($array, $row['ID']); |
||
252 | } |
||
253 | |||
254 | return $array; |
||
255 | } |
||
256 | |||
257 | function getUserByID($id) |
||
258 | { |
||
259 | $result = $this->query('SELECT * FROM Users WHERE ID = ' . $id); |
||
260 | while ($row = mysql_fetch_array($result)) |
||
261 | { |
||
262 | $user = new User; |
||
263 | $user->ID = $row['ID']; |
||
264 | $user->accessID = $row['AccessID']; |
||
265 | $user->username = $row['Username']; |
||
266 | $user->password = $row['Password']; |
||
267 | $user->emailAddress = $row['EmailAddress']; |
||
268 | $user->name = $row['Name']; |
||
269 | $user->challengeID = $row['ChallengeID']; |
||
270 | |||
271 | return $user; |
||
272 | } |
||
273 | |||
274 | return false; |
||
275 | } |
||
276 | |||
277 | function getUserByUsername($username) |
||
278 | { |
||
279 | $result = $this->query('SELECT * FROM Users WHERE Username = "' . $username . '"'); |
||
280 | while ($row = mysql_fetch_array($result)) |
||
281 | { |
||
282 | return $this->getUserByID($row['ID']); |
||
283 | } |
||
284 | |||
285 | return false; |
||
286 | } |
||
287 | |||
288 | function getLoggedInUser() |
||
289 | { |
||
290 | if ($this->isLoggedIn()) |
||
291 | { |
||
292 | $clist = explode('|~|', $_COOKIE['Tim32_Login']); |
||
293 | return $this->getUserByUsername($clist[0]); |
||
294 | } |
||
295 | |||
296 | return false; |
||
297 | } |
||
298 | |||
299 | function getBlogPost($id) |
||
300 | { |
||
301 | $result = $this->query('SELECT * FROM BlogPosts WHERE ID = ' . $id); |
||
302 | while ($row = mysql_fetch_array($result)) |
||
303 | { |
||
304 | $post = new BlogPost; |
||
305 | $post->ID = $row['ID']; |
||
306 | if ($row['ParentID'] == -1) |
||
307 | { |
||
308 | $post->parent = -1; |
||
309 | } |
||
310 | else |
||
311 | { |
||
312 | $post->parent = $this->getBlogPost($row['ParentID']); |
||
313 | } |
||
314 | $post->user = $this->getUserByID($row['AuthorID']); |
||
315 | $post->title = $row['Title']; |
||
316 | $post->content = $row['Content']; |
||
317 | $post->datePosted = strtotime($row['DatePosted']); |
||
318 | $post->category = $row['Category']; |
||
319 | $post->spam = $row['Spam']; |
||
320 | |||
321 | return $post; |
||
322 | } |
||
323 | |||
324 | $this->drawError('Cannot find blog post, #' . $id); |
||
325 | } |
||
326 | |||
327 | function getGetID() |
||
328 | { |
||
329 | $id = $_GET['id']; |
||
330 | if (empty($id)) |
||
331 | { |
||
332 | $id = 1; |
||
333 | } |
||
334 | |||
335 | return $id; |
||
336 | } |
||
337 | |||
338 | function getPostID() |
||
339 | { |
||
41 | freddie | 340 | $id = $_POST['id']; |
33 | freddie | 341 | if (empty($id)) |
342 | { |
||
343 | $id = 1; |
||
344 | } |
||
345 | |||
346 | return $id; |
||
347 | } |
||
348 | |||
349 | } |
||
350 | |||
351 | class User |
||
352 | { |
||
353 | public $ID; |
||
354 | public $accessID; |
||
355 | public $username; |
||
356 | public $password; |
||
357 | public $emailAddress; |
||
358 | public $name; |
||
359 | public $challengeID; |
||
360 | } |
||
361 | |||
362 | class BlogPost |
||
363 | { |
||
364 | public $ID; |
||
365 | public $parent; |
||
366 | public $author; |
||
367 | public $title; |
||
368 | public $content; |
||
369 | public $datePosted; |
||
370 | public $category; |
||
371 | public $spam; |
||
372 | } |
||
373 | |||
374 | function write($str) |
||
375 | { |
||
376 | echo $str; |
||
377 | echo "\n"; |
||
378 | } |
||
379 | |||
380 | ?> |