Rev 5 | Rev 9 | 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 | { |
||
54 | $this->drawMenuItem('Manage Account', 'admim/?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 | { |
||
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']; |
||
260 | $post->user = $this->getUserByID($row['AuthorID']); |
||
261 | $post->title = $row['Title']; |
||
262 | $post->content = $row['Content']; |
||
263 | $post->datePosted = strtotime($row['DatePosted']); |
||
264 | $post->category = $row['Category']; |
||
265 | |||
266 | return $post; |
||
267 | } |
||
268 | |||
269 | $this->drawError('Cannot find blog post, #' . $id); |
||
270 | } |
||
271 | } |
||
272 | |||
273 | class User |
||
274 | { |
||
275 | public $ID; |
||
276 | public $accessID; |
||
277 | public $username; |
||
278 | public $password; |
||
279 | public $emailAddress; |
||
280 | public $name; |
||
281 | public $challengeID; |
||
282 | } |
||
283 | |||
284 | class BlogPost |
||
285 | { |
||
286 | public $ID; |
||
287 | public $author; |
||
288 | public $title; |
||
289 | public $content; |
||
290 | public $datePosted; |
||
291 | public $category; |
||
292 | } |
||
293 | |||
294 | function write($str) |
||
295 | { |
||
296 | echo $str; |
||
297 | echo "\n"; |
||
298 | } |
||
299 | |||
300 | ?> |