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