Subversion Repositories taios

Rev

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