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