Rev 2 | Rev 4 | 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 | |||
3 | tom | 3 | require '_config.php' |
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 | |||
3 | tom | 16 | $this->db = mysql_connect($mysql_host, $mysql_username, $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 | |||
179 | function query($query) |
||
180 | { |
||
181 | $result = mysql_query($query); |
||
182 | if (!$result) |
||
183 | { |
||
184 | $this->drawError('MySQL Error: ' . mysql_error()); |
||
185 | } |
||
186 | |||
187 | return $result; |
||
188 | } |
||
189 | |||
190 | function findIDs($table, $query = '') |
||
191 | { |
||
192 | $array = array(); |
||
193 | |||
194 | $result = $this->query('SELECT ID FROM ' . $table . ' ' . $query); |
||
195 | while ($row = mysql_fetch_array($result)) |
||
196 | { |
||
197 | array_push($array, $row['ID']); |
||
198 | } |
||
199 | |||
200 | return $array; |
||
201 | } |
||
202 | |||
203 | function getUserByID($id) |
||
204 | { |
||
205 | $result = $this->query('SELECT * FROM Users WHERE ID = ' . $id); |
||
206 | while ($row = mysql_fetch_array($result)) |
||
207 | { |
||
208 | $user = new User; |
||
209 | $user->ID = $row['ID']; |
||
210 | $user->accessID = $row['AccessID']; |
||
211 | $user->username = $row['Username']; |
||
212 | $user->password = $row['Password']; |
||
213 | $user->emailAddress = $row['EmailAddress']; |
||
214 | $user->name = $row['Name']; |
||
215 | $user->challengeID = $row['ChallengeID']; |
||
216 | |||
217 | return $user; |
||
218 | } |
||
219 | |||
220 | return false; |
||
221 | } |
||
222 | |||
223 | function getUserByUsername($username) |
||
224 | { |
||
225 | $result = $this->query('SELECT * FROM Users WHERE Username = "' . $username . '"'); |
||
226 | while ($row = mysql_fetch_array($result)) |
||
227 | { |
||
228 | return $this->getUserByID($row['ID']); |
||
229 | } |
||
230 | |||
231 | return false; |
||
232 | } |
||
233 | |||
234 | function getLoggedInUser() |
||
235 | { |
||
236 | if ($this->isLoggedIn()) |
||
237 | { |
||
238 | $clist = explode('|~|', $_COOKIE['Tim32_Login']); |
||
239 | return $this->getUserByUsername($clist[0]); |
||
240 | } |
||
241 | |||
242 | return false; |
||
243 | } |
||
244 | |||
245 | function getBlogPost($id) |
||
246 | { |
||
247 | $result = $this->query('SELECT * FROM BlogPosts WHERE ID = ' . $id); |
||
248 | while ($row = mysql_fetch_array($result)) |
||
249 | { |
||
250 | $post = new BlogPost; |
||
251 | $post->ID = $row['ID']; |
||
252 | $post->user = $this->getUserByID($row['AuthorID']); |
||
253 | $post->title = $row['Title']; |
||
254 | $post->content = $row['Content']; |
||
255 | $post->datePosted = strtotime($row['DatePosted']); |
||
256 | $post->category = $row['Category']; |
||
257 | |||
258 | return $post; |
||
259 | } |
||
260 | |||
261 | $this->drawError('Cannot find blog post, #' . $id); |
||
262 | } |
||
263 | } |
||
264 | |||
265 | class User |
||
266 | { |
||
267 | public $ID; |
||
268 | public $accessID; |
||
269 | public $username; |
||
270 | public $password; |
||
271 | public $emailAddress; |
||
272 | public $name; |
||
273 | public $challengeID; |
||
274 | } |
||
275 | |||
276 | class BlogPost |
||
277 | { |
||
278 | public $ID; |
||
279 | public $author; |
||
280 | public $title; |
||
281 | public $content; |
||
282 | public $datePosted; |
||
283 | public $category; |
||
284 | } |
||
285 | |||
286 | function write($str) |
||
287 | { |
||
288 | echo $str; |
||
289 | echo "\n"; |
||
290 | } |
||
291 | |||
292 | ?> |