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