Subversion Repositories taios

Rev

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