Subversion Repositories taios

Rev

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