Subversion Repositories taios

Rev

Rev 297 | Rev 310 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
297 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
            {
54
                $this->drawMenuItem('Administration', 'admin/');
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 . '">' . $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
 
98
        die();
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, $first = false)
116
    {
117
        $post = $this->getBlogPost($id);
118
        if ($first)
119
        {
120
            write('<h3><a href="post.php?id=' . $id . '">' . $post->title. '</a> <a href="post.php?id=' . $post->parent->ID . '">^</a></h3>');
121
        }
122
        else
123
        {
124
            write('<a href="post.php?id=' . $id . '"><h3>' . $post->title. '</h3></a>');
125
        }
126
        write('<h5 style="color: #666666;">Posted On ' . date('l j F Y', $post->datePosted) . ' by ' . $post->user->name . ' (' . $post->user->username . ')</h5>');
127
        write('<p>' . $this->replaceBBCode($post->content) . '</p>');
128
 
129
        if ($this->isUserNormal($this->getLoggedInUser()))
130
        {
131
            echo '<p class="bold"><a href="add-post.php?id=' . $id . '">Add Comment</a>';
132
            if ($this->isUserAdmin($this->getLoggedInUser()) || $this->getLoggedInUser()->ID == $post->author->ID)
133
            {
134
                echo ' &nbsp; &middot &nbsp; <a href="edit-post.php?id=' . $id . '">Edit Post</a>';
135
                echo ' &nbsp; &middot &nbsp; <a href="del-post.php?id=' . $id . '">Delete Post</a>';
136
            }
137
            write('</p><br />');
138
        }
139
 
140
        $ids = $this->findIDs('BlogPosts', 'WHERE ParentID=' . $id);
141
        for ($i = 0; $i < count($ids); $i++)
142
        {
143
            write('<div class="indent">');
144
            $this->drawBlogPostTree($ids[$i]);
145
            write('</div>');
146
        }
147
    }
148
 
149
    function drawBlogCategoriesMenu()
150
    {
151
        $cats = array();
152
 
153
        $ids = $this->findIDs('BlogPosts', 'WHERE ParentID = -1');
154
        for ($i = 0; $i < count($ids); $i++)
155
        {
156
            $cat = $this->getBlogPost($ids[$i])->category;
157
            if (!in_array($cat, $cats))
158
            {
159
                array_push($cats, $cat);
160
            }
161
        }
162
 
163
        write('<h3>Categories</h3>');
164
        for ($i = 0; $i < count($cats); $i++)
165
        {
166
            $this->drawMenuItem($cats[$i], 'blog/index.php?cat=' . $cats[$i]);
167
        }
168
    }
169
 
170
    function replaceBBCode($str)
171
    {
308 tom 172
        $newstr = str_replace("<", "[", $str);
173
        $newstr = str_replace(">", "]", $newstr);
174
        $newstr = str_replace("\n", '</p><p>', $newstr);
297 freddie 175
        $newstr = str_replace('  ', '&nbsp;&nbsp;', $newstr);
176
        $newstr = str_replace(' :)', ' <img src="' . $this->url . 'data/smilies/face-smile.png" class="smiley" />', $newstr);
177
        $newstr = str_replace(' :p', ' <img src="' . $this->url . 'data/smilies/face-raspberry.png" class="smiley" />', $newstr);
178
        $newstr = str_replace(' :P', ' <img src="' . $this->url . 'data/smilies/face-raspberry.png" class="smiley" />',$newstr);
179
        $newstr = str_replace(' :|', ' <img src="' . $this->url . 'data/smilies/face-plain.png" class="smiley" />',$newstr);
180
        $newstr = str_replace(' :D', ' <img src="' . $this->url . 'data/smilies/face-laugh.png" class="smiley" />',$newstr);
181
        $newstr = str_replace(' =D', ' <img src="' . $this->url . 'data/smilies/face-laugh.png" class="smiley" />',$newstr);
182
        $newstr = str_replace(' :(', ' <img src="' . $this->url . 'data/smilies/face-sad.png" class="smiley" />',$newstr);
183
        $newstr = str_replace(' :0', ' <img src="' . $this->url . 'data/smilies/face-surprise.png" class="smiley" />',$newstr);
184
        $newstr = str_replace(' :o', ' <img src="' . $this->url . 'data/smilies/face-surprise.png" class="smiley" />',$newstr);
185
        $newstr = str_replace(' :O', ' <img src="' . $this->url . 'data/smilies/face-surprise.png" class="smiley" />',$newstr);
186
        $newstr = str_replace(' :/', ' <img src="' . $this->url . 'data/smilies/face-uncertain.png" class="smiley" />',$newstr);
187
        $newstr = str_replace(' ;)', ' <img src="' . $this->url . 'data/smilies/face-wink.png" class="smiley" />',$newstr);
188
 
189
        $bbcode = array(
190
        '/\[b\](.+?)\[\/b\]/is',
191
        '/\[i\](.+?)\[\/i\]/is',
192
        '/\[u\](.+?)\[\/u\]/is',
193
        '/\[url\](.+?)\[\/url\]/is',
194
        '/\[code\](.+?)\[\/code\]/is',
195
        '/\[img\](.+?)\[\/img\]/is'
196
        );
197
 
198
        $html = array(
199
        '<b>$1</b>',
200
        '<i>$1</i>',
201
        '<u>$1</u>',
202
        '<a href="$1">$1</a>',
203
        '<div class="code">$1</div>',
204
        '<img src="$1" />'
205
        );
206
 
207
        $newstr = preg_replace($bbcode, $html, $newstr);
208
 
209
        return $newstr;
210
    }
211
 
212
    function redirect($u)
213
    {
214
        header('Location: ' . $u);
215
        die();
216
    }
217
 
218
    function isLoggedIn()
219
    {
220
        $cookie = $_COOKIE['Tim32_Login'];
221
        if (!empty($cookie))
222
        {
223
            $clist = explode('|~|', $cookie);
224
            $user = $this->getUserByUsername($clist[0]);
225
            if ($user)
226
            {
227
                if ($user->password == $clist[1])
228
                {
229
                    return true;
230
                }
231
            }
232
        }
233
 
234
        return false;
235
    }
236
 
237
    function isUserAdmin()
238
    {
239
        if ($this->isLoggedIn())
240
        {
241
            if ($this->getLoggedInUser()->accessID <= 0)
242
            {
243
                return true;
244
            }
245
        }
246
 
247
        return false;
248
    }
249
 
250
    function isUserGM()
251
    {
252
        if ($this->isLoggedIn())
253
        {
254
            if ($this->getLoggedInUser()->accessID <= 1)
255
            {
256
                return true;
257
            }
258
        }
259
 
260
        return false;
261
    }
262
 
263
    function isUserNormal()
264
    {
265
        if ($this->isLoggedIn())
266
        {
267
            if ($this->getLoggedInUser()->accessID <= 2)
268
            {
269
                return true;
270
            }
271
        }
272
 
273
        return false;
274
    }
275
 
276
    function checkLoggedIn()
277
    {
278
        if (!$this->isLoggedIn())
279
        {
280
            $this->drawError('You need to be logged in.');
281
        }
282
    }
283
 
284
    function query($query)
285
    {
286
        $result = mysql_query($query);
287
        if (!$result)
288
        {
289
            $this->drawError('Query Failed: ' . $query . "\n" . 'MySQL Error: ' . mysql_error());
290
        }
291
 
292
        return $result;
293
    }
294
 
295
    function findIDs($table, $query = '')
296
    {
297
        $array = array();
298
 
299
        $result = $this->query('SELECT ID FROM ' . $table . ' ' . $query);
300
        while ($row = mysql_fetch_array($result))
301
        {
302
            array_push($array, $row['ID']);
303
        }
304
 
305
        return $array;
306
    }
307
 
308
    function getUserByID($id)
309
    {
310
        $result = $this->query('SELECT * FROM Users WHERE ID = ' . $id);
311
        while ($row = mysql_fetch_array($result))
312
        {
313
            $user = new User;
314
            $user->ID = $row['ID'];
315
            $user->accessID = $row['AccessID'];
316
            $user->username = $row['Username'];
317
            $user->password = $row['Password'];
318
            $user->emailAddress = $row['EmailAddress'];
319
            $user->name = $row['Name'];
320
            $user->challengeID = $row['ChallengeID'];
321
 
322
            return $user;
323
        }
324
 
325
        return false;
326
    }
327
 
328
    function getUserByUsername($username)
329
    {
330
        $result = $this->query('SELECT * FROM Users WHERE Username = "' . $username . '"');
331
        while ($row = mysql_fetch_array($result))
332
        {
333
            return $this->getUserByID($row['ID']);
334
        }
335
 
336
        return false;
337
    }
338
 
339
    function getLoggedInUser()
340
    {
341
        if ($this->isLoggedIn())
342
        {
343
            $clist = explode('|~|', $_COOKIE['Tim32_Login']);
344
            return $this->getUserByUsername($clist[0]);
345
        }
346
 
347
        return false;
348
    }
349
 
350
    function getBlogPost($id)
351
    {
352
        $result = $this->query('SELECT * FROM BlogPosts WHERE ID = ' . $id);
353
        while ($row = mysql_fetch_array($result))
354
        {
355
            $post = new BlogPost;
356
            $post->ID = $row['ID'];
357
            if ($row['ParentID'] == -1)
358
            {
359
                $post->parent = -1;
360
            }
361
            else
362
            {
363
                $post->parent = $this->getBlogPost($row['ParentID']);
364
            }
365
            $post->author = $this->getUserByID($row['AuthorID']);
366
            $post->user = $this->getUserByID($row['AuthorID']); // For some older pages
367
            $post->title = $row['Title'];
368
            $post->content = $row['Content'];
369
            $post->datePosted = strtotime($row['DatePosted']);
370
            $post->category = $row['Category'];
371
            $post->spam = $row['Spam'];
372
 
373
            return $post;
374
        }
375
 
376
        $this->drawError('Cannot find blog post, #' . $id);
377
    }
378
 
379
    function getProject($id)
380
    {
381
        $result = $this->query('SELECT * FROM Projects WHERE ID = ' . $id);
382
        while ($row = mysql_fetch_array($result))
383
        {
384
            $project = new Project;
385
 
386
            $project->ID = $row['ID'];  
387
            $project->author = $this->getUserByID($row['AuthorID']);
388
            $project->title = $row['Title'];
389
            $project->description = $row['Description'];
390
            $project->logoURL = $row['LogoURL'];
391
            $project->downloadURL = $row['DownloadURL'];
392
            $project->websiteURL = $row['WebsiteURL'];
393
            $project->latestVersion = $row['LatestVersion'];
394
            $project->lastUpdate = strtotime($row['LastUpdate']);          
395
 
396
            return $project;
397
        }
398
 
399
        return false;
400
    }
401
 
402
    function getForumCategory($id)
403
    {
404
        $result = $this->query('SELECT * FROM ForumCategories WHERE ID = ' . $id);
405
        while ($row = mysql_fetch_array($result))
406
        {
407
            $f = new ForumCategory;
408
 
409
            $f->ID = $row['ID'];
410
            $f->parent = $this->getForumCategory($row['ParentID']);
411
            $f->title = $row['Title'];
412
            $f->description = $row['Description'];
413
 
414
            return $f;
415
        }
416
 
417
        return false;
418
    }
419
 
420
    function getForumPost($id)
421
    {
422
        $result = $this->query('SELECT * FROM ForumPosts WHERE ID = ' . $id);
423
        while ($row = mysql_fetch_array($result))
424
        {
425
            $f = new ForumPost;
426
 
427
            $f->ID = $row['ID'];
428
            $f->author = $this->getUserByID($row['AuthorID']);
429
            $f->category = $this->getForumCategory($row['CategoryID']);
430
            $f->parent = $this->getForumPost($row['ParentID']);
431
            $f->title = $row['Title'];
432
            $f->content = $row['Content'];
433
            $f->datePosted = strtotime($row['DatePosted']);
434
            $f->spam = $row['Spam'];
435
 
436
            return $f;
437
        }
438
 
439
        return false;
440
    }
441
 
442
    function delBlogPost($id)
443
    {
444
        $ids = $this->findIDs('BlogPosts', 'WHERE ParentID=' . $id);
445
        for ($i = 0; $i < count($ids); $i++)
446
        {
447
            $this->delBlogPost($ids[$i]);
448
        }
449
 
450
        $this->query('DELETE FROM BlogPosts WHERE ID=' . $id);
451
    }
452
 
453
    function getGetID()
454
    {
455
        $id = $_GET['id'];
456
        if (empty($id))
457
        {
458
            $id = 1;
459
        }
460
 
461
        return $id;
462
    }
463
 
464
    function getPostID()
465
    {
466
        $id = $_POST['id'];
467
        if (empty($id))
468
        {
469
            $id = 1;
470
        }
471
 
472
        return $id;
473
    }
474
 
475
}
476
 
477
class User
478
{
479
    public $ID;
480
    public $accessID;
481
    public $username;
482
    public $password;
483
    public $emailAddress;
484
    public $name;
485
    public $challengeID;
486
}
487
 
488
class BlogPost
489
{
490
    public $ID;
491
    public $parent;
492
    public $author;
493
    public $title;
494
    public $content;
495
    public $datePosted;
496
    public $category;
497
    public $spam;
498
}
499
 
500
class Project
501
{
502
    public $ID;
503
    public $author;
504
    public $title;
505
    public $description;
506
    public $logoURL;
507
    public $downloadURL;
508
    public $websiteURL;
509
    public $latestVersion;
510
    public $lastUpdate;
511
}
512
 
513
class ForumCategory
514
{
515
    public $ID;
516
    public $parent;
517
    public $title;
518
    public $description;
519
}
520
 
521
class ForumPost
522
{
523
    public $id;
524
    public $author;
525
    public $category;
526
    public $parent;
527
    public $title;
528
    public $content;
529
    public $datePosted;
530
    public $spam;
531
}
532
 
533
function write($str)
534
{
535
    echo $str;
536
    echo "\n";
537
}
538
 
539
?>