Subversion Repositories taios

Rev

Rev 490 | Rev 495 | 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>');
490 tom 29
            write('<html lang="en">');
297 freddie 30
            write('<head>');
31
            write('<meta http-equiv="Content-Type" content="text/html;charset=utf-8">');
32
            write('<title>Tim32 &middot; ' . $this->title . '</title>');
490 tom 33
            write('<link href="' . $this->url . 'styles.css" rel="stylesheet" type="text/css" media="all" />');
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
 
490 tom 114
        if ($die) {
297 freddie 115
            $this->drawFooter();
116
            die();
117
        }
118
    }
119
 
120
    function drawBlogPostTree($id, $first = false)
121
    {
122
        $post = $this->getBlogPost($id);
123
        if ($first)
124
        {
125
            write('<h3><a href="post.php?id=' . $id . '">' . $post->title. '</a> <a href="post.php?id=' . $post->parent->ID . '">^</a></h3>');
126
        }
127
        else
128
        {
129
            write('<a href="post.php?id=' . $id . '"><h3>' . $post->title. '</h3></a>');
130
        }
131
        write('<h5 style="color: #666666;">Posted On ' . date('l j F Y', $post->datePosted) . ' by ' . $post->user->name . ' (' . $post->user->username . ')</h5>');
132
        write('<p>' . $this->replaceBBCode($post->content) . '</p>');
133
 
134
        if ($this->isUserNormal($this->getLoggedInUser()))
135
        {
136
            echo '<p class="bold"><a href="add-post.php?id=' . $id . '">Add Comment</a>';
137
            if ($this->isUserAdmin($this->getLoggedInUser()) || $this->getLoggedInUser()->ID == $post->author->ID)
138
            {
139
                echo ' &nbsp; &middot &nbsp; <a href="edit-post.php?id=' . $id . '">Edit Post</a>';
140
                echo ' &nbsp; &middot &nbsp; <a href="del-post.php?id=' . $id . '">Delete Post</a>';
141
            }
142
            write('</p><br />');
143
        }
144
 
484 muzer 145
        $ids = $this->findIDs('BlogPosts', 'WHERE ParentID="' . $id . '"');
297 freddie 146
        for ($i = 0; $i < count($ids); $i++)
147
        {
148
            write('<div class="indent">');
149
            $this->drawBlogPostTree($ids[$i]);
150
            write('</div>');
151
        }
152
    }
153
 
154
    function drawBlogCategoriesMenu()
155
    {
156
        $cats = array();
157
 
158
        $ids = $this->findIDs('BlogPosts', 'WHERE ParentID = -1');
159
        for ($i = 0; $i < count($ids); $i++)
160
        {
161
            $cat = $this->getBlogPost($ids[$i])->category;
483 muzer 162
            if (!in_array($cat, $cats) && ($cat != "Drafts" || $this->isUserGM($this->getLoggedInUser())))
297 freddie 163
            {
164
                array_push($cats, $cat);
165
            }
166
        }
167
 
168
        write('<h3>Categories</h3>');
169
        for ($i = 0; $i < count($cats); $i++)
170
        {
171
            $this->drawMenuItem($cats[$i], 'blog/index.php?cat=' . $cats[$i]);
172
        }
173
    }
174
 
175
    function replaceBBCode($str)
176
    {
437 tom 177
        $newstr = $str;    
484 muzer 178
        $newstr = str_replace("<", "&lt;", $newstr);
179
        $newstr = str_replace(">", "&gt;", $newstr);
481 muzer 180
        $newstr = str_replace("\n", "<br />", $newstr);
362 tom 181
        $newstr = str_replace("\\'", "'", $newstr);
182
        $newstr = str_replace("\\\"",'"', $newstr);
297 freddie 183
        $newstr = str_replace('  ', '&nbsp;&nbsp;', $newstr);
184
 
185
        $bbcode = array(
485 muzer 186
            '/\[b\](.+?)\[\/b\]/is',
187
            '/\[i\](.+?)\[\/i\]/is',
188
            '/\[u\](.+?)\[\/u\]/is',
189
            '/\[url\](.+?)\[\/url\]/is',
486 muzer 190
            '/\[w\](.+?)\[\/w\]/is',
485 muzer 191
            '/\[url=(?:&quot;)?(.+?)(?:&quot;)?\](.+?)\[\/url\]/is',
486 muzer 192
            '/\[w=(?:&quot;)?(.+?)(?:&quot;)?\](.+?)\[\/w\]/is',
485 muzer 193
            '/\[code\](.+?)\[\/code\]/is',
194
            '/\[img\](.+?)\[\/img\]/is',
195
            '/\[ul\](.+?)\[\/ul\]/is',
196
            '/\[ol\](.+?)\[\/ol\]/is',
197
            '/\[li\](.+?)\[\/li\]/is',
198
            '/\[mono\](.+?)\[\/mono\]/is',
199
            '/\[tcp\](.+?)\[\/tcp\]/is'
297 freddie 200
        );
201
 
202
        $html = array(
485 muzer 203
            '<b>$1</b>',
204
            '<i>$1</i>',
205
            '<u>$1</u>',
206
            '<a href="$1">$1</a>',
486 muzer 207
            '<a href="/wiki/index.php?page=$1">$1</a>',
485 muzer 208
            '<a href="$1">$2</a>',
486 muzer 209
            '<a href="/wiki/index.php?page=$1">$2</a>',
485 muzer 210
            '</p><div class="code">$1</div><p>',
211
            '<img src="$1" alt="BBCode-included image" />',
212
            '<ul>$1</ul>',
213
            '<ol>$1</ol>',
214
            '<li>$1</li>',
215
            '<span style="font-family: Droid Sans Mono, monospace, fixed; margin-left: 1em; margin-right: 1em;">$1</span>',
216
            '<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 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
 
471 muzer 288
    function isUserBanned()
289
    {
290
        if ($this->isLoggedIn())
291
        {
292
            if ($this->getLoggedInUser()->accessID >= 3)
293
            {
294
                return true;
295
            }
296
        }
297
 
298
        return false;
299
    }
300
 
384 tom 301
    function checkChallengeStatus($challengeID, $previous, $next)
302
    {
303
        $currentChallengeID = $this->getLoggedInUser()->challengeID;
304
 
305
        if (!$this->isLoggedIn())
306
        {
307
            $this->redirect('index.php');
308
        }
309
        else if ($currentChallengeID > $challengeID)
310
        {
311
            $this->redirect($next . '.php');
312
        }
313
        else if ($currentChallengeID < $challengeID)
314
        {
315
            $this->redirect($previous . '.php');
316
        }
317
    }
318
 
297 freddie 319
    function checkLoggedIn()
320
    {
321
        if (!$this->isLoggedIn())
322
        {
323
            $this->drawError('You need to be logged in.');
324
        }
325
    }
326
 
489 tom 327
    function query($query, $args = array())
297 freddie 328
    {
487 tom 329
                $statement = $this->db->prepare($query);
330
                if (!$statement->execute($args)) {
331
                    $this->drawError("Query Failed! MySQL Error: " . $statement->errorInfo());
332
                }
333
 
334
                return $statement->fetchAll();
297 freddie 335
    }
336
 
490 tom 337
    function findIDs($table, $query = '', $args = array())
297 freddie 338
    {
339
        $array = array();
340
 
490 tom 341
        $results = $this->query('SELECT ID FROM ' . $table . ' ' . $query, $args);
487 tom 342
        foreach ($results as $row) {
297 freddie 343
            array_push($array, $row['ID']);
344
        }
345
 
346
        return $array;
347
    }
348
 
349
    function getUserByID($id)
350
    {
490 tom 351
        foreach ($this->query("SELECT * FROM Users WHERE ID = ?", array($id)) as $row) {
352
            $user = new User();
297 freddie 353
            $user->ID = $row['ID'];
354
            $user->accessID = $row['AccessID'];
355
            $user->username = $row['Username'];
356
            $user->password = $row['Password'];
357
            $user->emailAddress = $row['EmailAddress'];
358
            $user->name = $row['Name'];
359
            $user->challengeID = $row['ChallengeID'];
360
 
361
            return $user;
362
        }
363
 
364
        return false;
365
    }
366
 
490 tom 367
    function getUserByUsername($username) {
368
        foreach ($this->query("SELECT * FROM Users WHERE Username = ?", array($username)) as $row) {
297 freddie 369
            return $this->getUserByID($row['ID']);
370
        }
371
 
372
        return false;
373
    }
374
 
491 tom 375
    function getLoggedInUser() {
376
        if ($this->isLoggedIn()) {
297 freddie 377
            $clist = explode('|~|', $_COOKIE['Tim32_Login']);
378
            return $this->getUserByUsername($clist[0]);
379
        }
380
 
381
        return false;
382
    }
383
 
491 tom 384
    function getBlogPost($id) {
385
        foreach ($this->query("SELECT * FROM BlogPosts WHERE ID = ?", array($id)) as $row) {
297 freddie 386
            $post = new BlogPost;
387
            $post->ID = $row['ID'];
491 tom 388
 
389
            if ($row['ParentID'] == -1) {
297 freddie 390
                $post->parent = -1;
491 tom 391
            } else {
297 freddie 392
                $post->parent = $this->getBlogPost($row['ParentID']);
393
            }
491 tom 394
 
297 freddie 395
            $post->author = $this->getUserByID($row['AuthorID']);
396
            $post->user = $this->getUserByID($row['AuthorID']); // For some older pages
485 muzer 397
            $post->title = htmlspecialchars($row['Title']);
398
            $post->content = htmlspecialchars($row['Content']);
297 freddie 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
 
491 tom 409
    function getProject($id) {
410
        foreach ($this->query("SELECT * FROM Projects WHERE ID = ?", array($id)) as $row) {
297 freddie 411
            $project = new Project;
412
 
413
            $project->ID = $row['ID'];  
414
            $project->author = $this->getUserByID($row['AuthorID']);
415
            $project->title = $row['Title'];
416
            $project->description = $row['Description'];
417
            $project->logoURL = $row['LogoURL'];
418
            $project->downloadURL = $row['DownloadURL'];
419
            $project->websiteURL = $row['WebsiteURL'];
420
            $project->latestVersion = $row['LatestVersion'];
421
            $project->lastUpdate = strtotime($row['LastUpdate']);          
422
 
423
            return $project;
424
        }
425
 
426
        return false;
427
    }
428
 
491 tom 429
    function getForumCategory($id) {
430
        foreach ($this->query("SELECT * FROM ForumCategories WHERE ID = ?", array($id)) as $row) {
297 freddie 431
            $f = new ForumCategory;
432
 
433
            $f->ID = $row['ID'];
434
            $f->parent = $this->getForumCategory($row['ParentID']);
435
            $f->title = $row['Title'];
436
            $f->description = $row['Description'];
437
 
438
            return $f;
439
        }
440
 
441
        return false;
442
    }
443
 
491 tom 444
    function getForumPost($id) {
445
        foreach ($this->query("SELECT * FROM ForumPosts WHERE ID = ?", array($id)) as $row) {
297 freddie 446
            $f = new ForumPost;
447
 
448
            $f->ID = $row['ID'];
449
            $f->author = $this->getUserByID($row['AuthorID']);
450
            $f->category = $this->getForumCategory($row['CategoryID']);
451
            $f->parent = $this->getForumPost($row['ParentID']);
452
            $f->title = $row['Title'];
453
            $f->content = $row['Content'];
454
            $f->datePosted = strtotime($row['DatePosted']);
455
            $f->spam = $row['Spam'];
456
 
457
            return $f;
458
        }
459
 
460
        return false;
461
    }
462
 
491 tom 463
    function delBlogPost($id) {
464
        foreach ($this->findIDs("BlogPosts", "WHERE ParentID = ?", array($id)) as $i) {
465
            $this->delBlogPost($i);
297 freddie 466
        }
491 tom 467
 
468
        $this->query("DELETE FROM BlogPosts WHERE ID = ?", array($id));
297 freddie 469
    }
470
 
491 tom 471
    function getGetID() {
297 freddie 472
        $id = $_GET['id'];
491 tom 473
        if (empty($id)) {
297 freddie 474
            $id = 1;
475
        }
476
 
477
        return $id;
478
    }
479
 
491 tom 480
    function getPostID() {
297 freddie 481
        $id = $_POST['id'];
491 tom 482
        if (empty($id)) {
297 freddie 483
            $id = 1;
484
        }
485
 
486
        return $id;
487
    }
488
 
489
}
490
 
491
class User
492
{
493
    public $ID;
494
    public $accessID;
495
    public $username;
496
    public $password;
497
    public $emailAddress;
498
    public $name;
443 tom 499
 
297 freddie 500
    public $challengeID;
501
}
502
 
503
class BlogPost
504
{
505
    public $ID;
506
    public $parent;
507
    public $author;
508
    public $title;
509
    public $content;
510
    public $datePosted;
511
    public $category;
512
    public $spam;
513
}
514
 
515
class Project
516
{
517
    public $ID;
518
    public $author;
519
    public $title;
520
    public $description;
426 tom 521
 
434 tom 522
 
297 freddie 523
    public $logoURL;
524
    public $downloadURL;
525
    public $websiteURL;
526
    public $latestVersion;
527
    public $lastUpdate;
528
}
529
 
530
class ForumCategory
531
{
532
    public $ID;
533
    public $parent;
534
    public $title;
535
    public $description;
401 tom 536
 
297 freddie 537
}
538
 
539
class ForumPost
540
{
541
    public $id;
542
    public $author;
543
    public $category;
544
    public $parent;
545
    public $title;
546
    public $content;
547
    public $datePosted;
548
    public $spam;
549
}
550
 
551
function write($str)
552
{
553
    echo $str;
554
    echo "\n";
555
}
556
 
557
?>