Rev 12 | Rev 27 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | tom | 1 | <?php |
| 2 | |||
| 4 | tom | 3 | require '_config.php'; |
| 3 | tom | 4 | |
| 1 | tom | 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 | |||
| 5 | tom | 16 | $this->db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD); |
| 1 | tom | 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 | { |
||
| 9 | tom | 54 | $this->drawMenuItem('Manage Account', 'admin/account.php?id=' . $this->getLoggedInUser()->ID); |
| 1 | tom | 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 | { |
||
| 70 | write('<p><a href="' . $this->url . $u . '"</a>' . $t . '</a></p>'); |
||
| 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 | |||
| 2 | tom | 98 | // die(); |
| 1 | tom | 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 redirect($url) |
||
| 116 | { |
||
| 117 | header('Location: ' . $url); |
||
| 118 | die(); |
||
| 119 | } |
||
| 120 | |||
| 121 | function isLoggedIn() |
||
| 122 | { |
||
| 123 | $cookie = $_COOKIE['Tim32_Login']; |
||
| 124 | if (!empty($cookie)) |
||
| 125 | { |
||
| 126 | $clist = explode('|~|', $cookie); |
||
| 127 | $user = $this->getUserByUsername($clist[0]); |
||
| 128 | if ($user) |
||
| 129 | { |
||
| 130 | if ($user->password == $clist[1]) |
||
| 131 | { |
||
| 132 | return true; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | return false; |
||
| 138 | } |
||
| 139 | |||
| 140 | function isUserAdmin() |
||
| 141 | { |
||
| 142 | if ($this->isLoggedIn()) |
||
| 143 | { |
||
| 144 | if ($this->getLoggedInUser()->accessID <= 0) |
||
| 145 | { |
||
| 146 | return true; |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | return false; |
||
| 151 | } |
||
| 152 | |||
| 153 | function isUserGM() |
||
| 154 | { |
||
| 155 | if ($this->isLoggedIn()) |
||
| 156 | { |
||
| 157 | if ($this->getLoggedInUser()->accessID <= 1) |
||
| 158 | { |
||
| 159 | return true; |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | return false; |
||
| 164 | } |
||
| 165 | |||
| 166 | function isUserNormal() |
||
| 167 | { |
||
| 168 | if ($this->isLoggedIn()) |
||
| 169 | { |
||
| 170 | if ($this->getLoggedInUser()->accessID <= 2) |
||
| 171 | { |
||
| 172 | return true; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | return false; |
||
| 177 | } |
||
| 178 | |||
| 6 | tom | 179 | function checkLoggedIn() |
| 180 | { |
||
| 181 | if (!$this->isLoggedIn()) |
||
| 182 | { |
||
| 183 | $this->drawError('You need to be logged in.'); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 1 | tom | 187 | function query($query) |
| 188 | { |
||
| 189 | $result = mysql_query($query); |
||
| 190 | if (!$result) |
||
| 191 | { |
||
| 192 | $this->drawError('MySQL Error: ' . mysql_error()); |
||
| 193 | } |
||
| 194 | |||
| 195 | return $result; |
||
| 196 | } |
||
| 197 | |||
| 198 | function findIDs($table, $query = '') |
||
| 199 | { |
||
| 200 | $array = array(); |
||
| 201 | |||
| 202 | $result = $this->query('SELECT ID FROM ' . $table . ' ' . $query); |
||
| 203 | while ($row = mysql_fetch_array($result)) |
||
| 204 | { |
||
| 205 | array_push($array, $row['ID']); |
||
| 206 | } |
||
| 207 | |||
| 208 | return $array; |
||
| 209 | } |
||
| 210 | |||
| 211 | function getUserByID($id) |
||
| 212 | { |
||
| 213 | $result = $this->query('SELECT * FROM Users WHERE ID = ' . $id); |
||
| 214 | while ($row = mysql_fetch_array($result)) |
||
| 215 | { |
||
| 216 | $user = new User; |
||
| 217 | $user->ID = $row['ID']; |
||
| 218 | $user->accessID = $row['AccessID']; |
||
| 219 | $user->username = $row['Username']; |
||
| 220 | $user->password = $row['Password']; |
||
| 221 | $user->emailAddress = $row['EmailAddress']; |
||
| 222 | $user->name = $row['Name']; |
||
| 223 | $user->challengeID = $row['ChallengeID']; |
||
| 224 | |||
| 225 | return $user; |
||
| 226 | } |
||
| 227 | |||
| 228 | return false; |
||
| 229 | } |
||
| 230 | |||
| 231 | function getUserByUsername($username) |
||
| 232 | { |
||
| 233 | $result = $this->query('SELECT * FROM Users WHERE Username = "' . $username . '"'); |
||
| 234 | while ($row = mysql_fetch_array($result)) |
||
| 235 | { |
||
| 236 | return $this->getUserByID($row['ID']); |
||
| 237 | } |
||
| 238 | |||
| 239 | return false; |
||
| 240 | } |
||
| 241 | |||
| 242 | function getLoggedInUser() |
||
| 243 | { |
||
| 244 | if ($this->isLoggedIn()) |
||
| 245 | { |
||
| 246 | $clist = explode('|~|', $_COOKIE['Tim32_Login']); |
||
| 247 | return $this->getUserByUsername($clist[0]); |
||
| 248 | } |
||
| 249 | |||
| 250 | return false; |
||
| 251 | } |
||
| 252 | |||
| 253 | function getBlogPost($id) |
||
| 254 | { |
||
| 255 | $result = $this->query('SELECT * FROM BlogPosts WHERE ID = ' . $id); |
||
| 256 | while ($row = mysql_fetch_array($result)) |
||
| 257 | { |
||
| 258 | $post = new BlogPost; |
||
| 259 | $post->ID = $row['ID']; |
||
| 20 | tom | 260 | if ($row['ParentID'] == -1) |
| 261 | { |
||
| 262 | $post->parent = -1; |
||
| 263 | } |
||
| 264 | else |
||
| 265 | { |
||
| 266 | $post->parent = $this->getBlogPost($row['ParentID']); |
||
| 267 | } |
||
| 1 | tom | 268 | $post->user = $this->getUserByID($row['AuthorID']); |
| 269 | $post->title = $row['Title']; |
||
| 270 | $post->content = $row['Content']; |
||
| 271 | $post->datePosted = strtotime($row['DatePosted']); |
||
| 272 | $post->category = $row['Category']; |
||
| 20 | tom | 273 | $post->spam = $row['Spam']; |
| 1 | tom | 274 | |
| 275 | return $post; |
||
| 276 | } |
||
| 277 | |||
| 278 | $this->drawError('Cannot find blog post, #' . $id); |
||
| 279 | } |
||
| 12 | tom | 280 | |
| 281 | function getGetID() |
||
| 282 | { |
||
| 283 | $id = $_GET['id']; |
||
| 284 | if (empty($id)) |
||
| 285 | { |
||
| 286 | $id = 1; |
||
| 287 | } |
||
| 288 | |||
| 289 | return $id; |
||
| 290 | } |
||
| 291 | |||
| 292 | function getPostID() |
||
| 293 | { |
||
| 294 | $id = $_POSt['id']; |
||
| 295 | if (empty($id)) |
||
| 296 | { |
||
| 297 | $id = 1; |
||
| 298 | } |
||
| 299 | |||
| 300 | return $id; |
||
| 301 | } |
||
| 1 | tom | 302 | } |
| 303 | |||
| 304 | class User |
||
| 305 | { |
||
| 306 | public $ID; |
||
| 307 | public $accessID; |
||
| 308 | public $username; |
||
| 309 | public $password; |
||
| 310 | public $emailAddress; |
||
| 311 | public $name; |
||
| 312 | public $challengeID; |
||
| 313 | } |
||
| 314 | |||
| 315 | class BlogPost |
||
| 316 | { |
||
| 317 | public $ID; |
||
| 20 | tom | 318 | public $parent; |
| 1 | tom | 319 | public $author; |
| 320 | public $title; |
||
| 321 | public $content; |
||
| 322 | public $datePosted; |
||
| 323 | public $category; |
||
| 20 | tom | 324 | public $spam; |
| 1 | tom | 325 | } |
| 326 | |||
| 327 | function write($str) |
||
| 328 | { |
||
| 329 | echo $str; |
||
| 330 | echo "\n"; |
||
| 331 | } |
||
| 332 | |||
| 333 | ?> |