Subversion Repositories taios

Rev

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