Subversion Repositories taios

Rev

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