Subversion Repositories taios

Rev

Rev 48 | Rev 52 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
33 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('Manage Account', 'admin/account.php?id=' . $this->getLoggedInUser()->ID);
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
    {
49 tom 70
        write('<p><a href="' . $this->url . $u . '">' . $t . '</a></p>');
33 freddie 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
        {
91
            write('</div>');
92
            write('</body>');
93
            write('</html>');
94
 
95
            $this->drawnFooter = true;
96
        }
97
 
98
//        die();
99
    }
100
 
101
    function drawError($text, $die = true)
102
    {
103
        $this->drawHeader();
104
        $this->drawMiddle();
105
 
106
        write('<h4 style="color: red;">Error: ' . $text . '</h4>');
107
 
108
        if ($die)
109
        {
110
            $this->drawFooter();
111
            die();
112
        }
113
    }
114
 
115
    function drawBlogPostTree($id)
116
    {
117
        $post = $this->getBlogPost($id);
118
        write('<h3>' . $post->title. '</h3>');
119
        write('<h5 style="color: #666666;">Posted On ' . date('l j F Y', $post->datePosted) . ' by ' . $post->user->name . ' (' . $post->user->username . ')</h5>');
120
        write('<p>' . $post->content . '</p>');
121
 
43 freddie 122
        $ids = $this->findIDs('BlogPosts', 'WHERE ParentID=' . $id);
33 freddie 123
        for ($i = 0; $i < count($ids); $i++)
124
        {
125
            write('<p class="indent">');
43 freddie 126
            $this->drawBlogPostTree($ids[$i]);
33 freddie 127
            write('</p>');
128
        }
129
 
130
    }
131
 
132
    function redirect($url)
133
    {
134
        header('Location: ' . $url);
135
        die();
136
    }
137
 
138
    function isLoggedIn()
139
    {
140
        $cookie = $_COOKIE['Tim32_Login'];
141
        if (!empty($cookie))
142
        {
143
            $clist = explode('|~|', $cookie);
144
            $user = $this->getUserByUsername($clist[0]);
145
            if ($user)
146
            {
147
                if ($user->password == $clist[1])
148
                {
149
                    return true;
150
                }
151
            }
152
        }
153
 
154
        return false;
155
    }
156
 
157
    function isUserAdmin()
158
    {
159
        if ($this->isLoggedIn())
160
        {
161
            if ($this->getLoggedInUser()->accessID <= 0)
162
            {
163
                return true;
164
            }
165
        }
166
 
167
        return false;
168
    }
169
 
170
    function isUserGM()
171
    {
172
        if ($this->isLoggedIn())
173
        {
174
            if ($this->getLoggedInUser()->accessID <= 1)
175
            {
176
                return true;
177
            }
178
        }
179
 
180
        return false;
181
    }
182
 
183
    function isUserNormal()
184
    {
185
        if ($this->isLoggedIn())
186
        {
187
            if ($this->getLoggedInUser()->accessID <= 2)
188
            {
189
                return true;
190
            }
191
        }
192
 
193
        return false;
194
    }
195
 
196
    function checkLoggedIn()
197
    {
198
        if (!$this->isLoggedIn())
199
        {
200
            $this->drawError('You need to be logged in.');
201
        }
202
    }
203
 
204
    function query($query)
205
    {
206
        $result = mysql_query($query);
207
        if (!$result)
208
        {
209
            $this->drawError('MySQL Error: ' . mysql_error());
210
        }
211
 
212
        return $result;
213
    }
214
 
215
    function findIDs($table, $query = '')
216
    {
217
        $array = array();
218
 
219
        $result = $this->query('SELECT ID FROM ' . $table . ' ' . $query);
220
        while ($row = mysql_fetch_array($result))
221
        {
222
            array_push($array, $row['ID']);
223
        }
224
 
225
        return $array;
226
    }
227
 
228
    function getUserByID($id)
229
    {
230
        $result = $this->query('SELECT * FROM Users WHERE ID = ' . $id);
231
        while ($row = mysql_fetch_array($result))
232
        {
233
            $user = new User;
234
            $user->ID = $row['ID'];
235
            $user->accessID = $row['AccessID'];
236
            $user->username = $row['Username'];
237
            $user->password = $row['Password'];
238
            $user->emailAddress = $row['EmailAddress'];
239
            $user->name = $row['Name'];
240
            $user->challengeID = $row['ChallengeID'];
241
 
242
            return $user;
243
        }
244
 
245
        return false;
246
    }
247
 
248
    function getUserByUsername($username)
249
    {
250
        $result = $this->query('SELECT * FROM Users WHERE Username = "' . $username . '"');
251
        while ($row = mysql_fetch_array($result))
252
        {
253
            return $this->getUserByID($row['ID']);
254
        }
255
 
256
        return false;
257
    }
258
 
259
    function getLoggedInUser()
260
    {
261
        if ($this->isLoggedIn())
262
        {
263
            $clist = explode('|~|', $_COOKIE['Tim32_Login']);
264
            return $this->getUserByUsername($clist[0]);
265
        }
266
 
267
        return false;
268
    }
269
 
270
    function getBlogPost($id)
271
    {
272
        $result = $this->query('SELECT * FROM BlogPosts WHERE ID = ' . $id);
273
        while ($row = mysql_fetch_array($result))
274
        {
275
            $post = new BlogPost;
276
            $post->ID = $row['ID'];
277
            if ($row['ParentID'] == -1)
278
            {
279
                $post->parent = -1;
280
            }
281
            else
282
            {
283
                $post->parent = $this->getBlogPost($row['ParentID']);
284
            }
285
            $post->user = $this->getUserByID($row['AuthorID']);
286
            $post->title = $row['Title'];
287
            $post->content = $row['Content'];
288
            $post->datePosted = strtotime($row['DatePosted']);
289
            $post->category = $row['Category'];
290
            $post->spam = $row['Spam'];
291
 
292
            return $post;
293
        }
294
 
295
        $this->drawError('Cannot find blog post, #' . $id);
296
    }
297
 
298
    function getGetID()
299
    {
300
        $id = $_GET['id'];
301
        if (empty($id))
302
        {
303
            $id = 1;
304
        }
305
 
306
        return $id;
307
    }
308
 
309
    function getPostID()
310
    {
41 freddie 311
        $id = $_POST['id'];
33 freddie 312
        if (empty($id))
313
        {
314
            $id = 1;
315
        }
316
 
317
        return $id;
318
    }
319
 
320
}
321
 
322
class User
323
{
324
    public $ID;
325
    public $accessID;
326
    public $username;
327
    public $password;
328
    public $emailAddress;
329
    public $name;
330
    public $challengeID;
331
}
332
 
333
class BlogPost
334
{
335
    public $ID;
336
    public $parent;
337
    public $author;
338
    public $title;
339
    public $content;
340
    public $datePosted;
341
    public $category;
342
    public $spam;
343
}
344
 
345
function write($str)
346
{
347
    echo $str;
348
    echo "\n";
349
}
350
 
351
?>