post
poster: G-man
description: Some bbcode-to-html madness
language: PHP
[download]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
function wordwrapper($content){
    return wordwrap($content,85,"\n",yes);
}
$GLOBALS['queries'] = 0;
function print_quote($id,$content){
    $row = mysql_fetch_assoc(query("select `username`,`thread-id` from `posts` where `id` = '".$id."'"));
    $return = escape("</pre><div class='quote'><div class='quote_header'><a href='index.php?id=viewthread&t=".$row['thread-id']."#".$id."'>Quote by: ".$row['username']."</a></div><pre>".$content."</pre></div><pre>");
    return $return;
}
function processor($query){
    $query = htmlentities($query, ENT_QUOTES);
    while(preg_match("/\[quote=(\d+)\](.*)\[\/quote\]/i",$query,$parts)){
        $pattern = "/\[quote=(\d+)\](.*)\[\/quote\]/i";
        $replace = print_quote($parts[1],$parts[2]);
        $query = preg_replace($pattern, $replace, $query);
    }
    $query = "<pre>".$query."</pre>";
    while(preg_match("/\[img=(.*)(.gif||.jpg||.png)\]/i",$query,$parts)){
        $query = preg_replace("/\[img=(.*)(.gif||.jpg||.png)\]/i","<img src=$2>",$query);
    }
    $query = str_replace("[b]", "<b>", $query);
    $query = str_replace("[/b]", "</b>", $query);
    $query = str_replace("[i]", "<i>", $query);
    $query = str_replace("[/i]", "</i>", $query);
    $query = str_replace("[u]", "<u>", $query);
    $query = str_replace("[/u]", "</u>", $query);
    $query = str_replace("[/quote]", "</div class=\'quote\'>", $query);
    $query = str_replace("[/u]", "</u>", $query);
    return $query;
}

function deprocessor($query){
    $query = str_replace("<pre>", "", $query);
    $query = str_replace("</pre>", "", $query);
    $pattern = "/\<div class=\'quote\'\>(.*)\<\/div\>/i";
    if(preg_match($pattern,$query,$parts)){
        $query = preg_replace($pattern, "$2", $query);
    }
    while(preg_match("/\[quote=(\d+)\](.*)\[\/quote\]/i",$query,$parts)){
        $pattern = "/\[quote=(\d+)\](.*)\[\/quote\]/i";
        $query = preg_replace($pattern, "", $query);
    }
    $query = str_replace("<b>", "[b]", $query);
    $query = str_replace("</b>", "[/b]", $query);
    $query = str_replace("<i>", "[i]", $query);
    $query = str_replace("</i>", "[/i]", $query);
    $query = str_replace("<u>", "[u]", $query);
    $query = str_replace("</u>", "[/u]", $query);
    $query = str_replace("<br />", "\n", $query);

    return $query;
}
function user_info($un){
    $un = secure($un);
    $user_info = query("select `power`, `date`,`signature`,`id`,`posts`,`lastactivity` from `users` where `username` = '$un'");
    $user_info = mysql_fetch_assoc($user_info);
    $return = "<td class='post_user'><a href='index.php?id=user&id=".$user_info['id']."'>".$un."</a>";
    if(is_logged()){
        $return .= " (<a href='index.php?id=messages&compose=1&to=".$un."'>msg</a>)";
    }
    $return .="<br>
Posts: ".$user_info['posts']."<br>Reg: ".    $user_info['date']."<br>Status: ".$user_info['power']."<br>";
    $useronline_time = 600; // seconds in online time limit
    if(($user_info['lastactivity'] == 0) || ($user_info['lastactivity']+$useronline_time) < time()){
        $return .= "<i><font color=#999999>Offline</font></i>";
    }
    else{
        $return .= "<i><font color='#ffff00'>Online</font></i>";
    }
    $return .= "<br><br><i>".wordwrap($user_info['signature'],20,"\n",y)."</i></td>";
return $return;
}
function print_post($id, $type) {
    global $username;
    if(empty($id) || empty($type)){
        print_error("print_post error: Invalid parameters, id and type needed, in that order.");
        exit;
    }
    echo "<div class='post_container'>";
    if($type == "thread"){
        $result = query("select * from `threads` where `id` = '$id'");
        if(!$row = mysql_fetch_assoc($result)){
            print_error("print_post error: Invalid thread ID.");
            exit;
        }
        $board_name = mysql_fetch_assoc(query("select `name` from `boards` where `id` = '".$row['board-id']."'"));
        echo "
<div class='post_title'><b><a href='index.php?id=viewthread&t=".$id."'>".$row['topic']."</a></b> in <a href='index.php?id=viewboard&b=".$row['board-id']."'>".$board_name['name']."</a> on <i>".$row['date']."</i> at 
<i>".$row['time']."</i> - <a href='index.php?id=editpost&t=".$id."&type=thread'>Edit</a> - <a href='index.php?id=postreply&t=".$id."'>Reply</a>";
if(is_logged() && (is_mod($username, $row['board-id']) || is_admin($username, "quite"))){
        echo ' - <a href="index.php?mod=deletethread&t='. $id .'">Delete</a>';
echo ' - <a href="index.php?mod=edithistory&t='. $id .'">Edit history</a>';
echo ' - <a href="index.php?mod=sticky&t='. $id .'">Sticky</a>/<a href="index.php?mod=sticky&unsticky=1&t='. $id .'">Unsticky</a>';
echo ' - <a href="index.php?mod=move&t='. $id .'">Move</a>';
}
echo "</div><table width=100%><tr>".user_info($row['username'])."<td>".wordwrapper($row['content'])."";
        $archive = query("select * from `thread_edits` where `thread-id` = '".$id."' order by `id` desc");
        if($archive = mysql_fetch_assoc($archive)){
            echo "<br><span class='smallfont'>Editted by ".$archive['username']." on <i>".$archive['date']."</i> at <i>".$archive['time']."</i></span>";
        }
        echo"</td></tr></table></div>";
    }
    elseif($type == "deleted-thread"){
        $result = query("select * from `deleted_threads` where `thread-id` = '$id'");
        if(!$row = mysql_fetch_assoc($result)){
            print_error("print_post error: Invalid deleted-thread ID.");
            exit;
        }
        echo "
        
<div class='post_title'><b><a href='index.php?id=viewthread&t=".$id."'>".$row['topic']."</a></b> posted on <i>".$row['date-created']."</i> at 
<i>".$row['time-created']."</i>, deleted on <i>".$row['date-deleted']."</i> at <i>".$row['time-deleted']."</i>";
echo ' <a href="index.php?admin=deleted_threads_perm&t='. $id .'">Delete</a> - <a href="index.php?admin=deleted_threads_undelete&t='. $id.'">Recover</a>';
echo ' - <a href="index.php?mod=edithistory&t='. $id .'&deleted=1">Edit history</a>';
echo '</div>';
echo "
<table width=100%><tr>".user_info($row['username'])."<td>".wordwrapper($row['content'])."";
        $archive = query("select * from `thread_edits` where `thread-id` = '".$id."' order by `id` desc");
        if($archive = mysql_fetch_assoc($archive)){
            echo "<br><span class='smallfont'>Editted by ".$archive['username']." on <i>".$archive['date']."</i> at <i>".$archive['time']."</i></font>";
        }
        echo "</td></tr></table></div>";
    }
    elseif($type == "post"){
        $result = query("select * from `posts` where `id` = '$id'");
        if(!$row = mysql_fetch_assoc($result)){
            print_error("print_post error: Invalid post ID.");
            exit;
        }
        echo "<div class='post_title' id='$id'><b><a href='index.php?id=viewpost&p=".$id."'>".$row['topic']."</a> <a href='index.php?id=viewthread&t=".$row['thread-id']."#".$id."'>(#".$id.")</a></b>" ."  in <a href='index.php?id=viewthread&t=" . $row['thread-id'] . "'>";
        $thread_info = query("select `topic`,`board-id` from `threads` where `id` = '".$row['thread-id']."'");
        $thread_info = mysql_fetch_assoc($thread_info);
        echo $thread_info['topic']."</a> ";
        echo "posted on <i>".$row['date']."</i> at <i>".$row['time']."</i> - <a href='index.php?id=editpost&p=".$id."&type=post'>Edit</a>";

        if(is_logged() && (is_mod($username, $thread_info['board-id']) || is_admin($username, "quite"))){
            echo ' - <a href="index.php?mod=edithistory&p='. $id .'">Edit history</a>';
        }
        echo " - <a href='index.php?id=postreply&t=".$row['thread-id']."&qt=post&qid=".$id."'>Reply</a></div><table width=100%><tr>".user_info($row['username'])."<td >";
        echo wordwrapper($row['content']);
        $archive = query("select * from `posts_edits` where `post-id` = '".$id."' order by `id` desc");
        if($archive = mysql_fetch_assoc($archive)){
            echo "<br><span class='smallfont'>Editted by ".$archive['username']." on <i>".$archive['date']."</i> at <i>".$archive['time']."</i></span>";
        }
        echo "</td></tr></table></div>";
    }
    elseif($type == "deleted-post"){
        $result = query("select * from `posts` where `id` = '$id'");
        if(!$row = mysql_fetch_assoc($result)){
            print_error("print_post error: Invalid post ID.");
            exit;
        }
        echo "<div class='post_title'><b>".$row['topic']."</b> on <i>".$row['date']."</i> at 
<i>".$row['time']."</i>";
        $thread_info = query("select `topic`,`board-id` from `threads` where `id` = '".$row['thread-id']."'");
        $thread_info = mysql_fetch_assoc($thread_info);
        if(is_logged() && (is_mod($username, $thread_info['board-id']) || is_admin($username, "quite"))){
            echo ' - <a href="index.php?mod=edithistory&p='. $id .'">Edit history</a>';
        }
        echo "</div><table width=100%><tr>".user_info($row['username'])."<td >".wordwrap($row['content'])."";
        $archive = query("select * from `posts_edits` where `post-id` = '".$id."' order by `id` desc");
        if($archive = mysql_fetch_assoc($archive)){
            echo "<br><span class='smallfont'>Editted by ".$archive['username']." on <i>".$archive['date']."</i> at <i>".$archive['time']."</i></span>";
        }
        echo "</td></tr></table></div>";
    }
    elseif($type == "doc-comment") {
        $result = query("select * from `comments` where `id` = '$id'");
        if(!$row = mysql_fetch_assoc($result)){
            print_error("print_post error: Invalid post ID.");
            exit;
        }
        echo "<div class='post_title' id='$id'><b>".$row['topic']."</b>" ." in <a href='index.php?id=viewdoc&d=" . $row['document_id'] . "'>";
        $doc_info = mysql_fetch_assoc(query("select `topic`,`cat-id`,`username` from `documents` where `id` = '".$row['document_id']."'"));
        echo $doc_info['topic']."</a> ";
        echo "posted on <i>".$row['date']."</i> at <i>".$row['time']."</i>";
        if(is_logged() && (is_manager($username, $doc_info['doc-id']) || is_admin($username, "quite"))) {
            echo ' - <a href="index.php?mod=deletecomment&cm='. $id .'">Delete</a>';
        }
        echo " - <a href='index.php?id=addcomment&d=".$row['document_id']."&qid=".$id."'>Reply</a></div><table width=100%><tr>".user_info($row['username'])."<td >";
        echo "".wordwrapper($row['content'])."";
        echo "</td></tr></table></div>";
    }

    elseif($type == "pastebin"){
        $result = query("select * from `pastebin` where `id` = '$id'");
        if(!$row = mysql_fetch_assoc($result)){
            print_error("print_post error: Invalid pastebin ID.");
            exit;
        }
        echo "<div class='post_title'><b><a href='".$row['url']."'>".$row['url']."</a></b> on <i>".$row['date']."</a></i> at <i>".$row['time']."</i>"." Number of lines: ".$row['lines']." - <a href='index.php?id=pastebin&codeid=".$id."'>Repost</a></div>";
        echo "<table width=100%><tr>".user_info($row['username'])."<td width=90%><table><tr><td style='color: green;'><pre>";
        $lines = 1;
        while($lines != ($row['lines']+1)){
            echo $lines . "<br>";
            ++$lines;
        }
        echo "</pre></td>";
        
        echo "<td><pre>".htmlentities(fileread($row['url']), ENT_QUOTES)."</pre></td></tr></table></td><td><p align='right'></p></td></tr></table></div>";
    }
    elseif($type == "document") {
        $result = query("select * from `documents` where `id` = '$id'");
        if(!$row = mysql_fetch_assoc($result)){
            print_error("print_post error: Invalid doc ID.");
            exit;
        }
        echo "<div class='post_title'><b><a href='index.php?id=viewdoc&d=".$id."'>".$row['topic']."</a></b> in ";
        $cat = mysql_fetch_assoc(query("select `name` from `categories` where `id` = '".$row['cat-id']."'"));
        echo "<a href='index.php?id=viewcat&c=".$row['cat-id']."'>".$cat['name']."</a> ";
        echo "<i>".$row['date']."</i> at 
<i>".$row['time']."</i> - <a href='index.php?id=addcomment&d=".$id."'>Reply</a>";
if(is_manager($username,$row['cat-id']) || is_admin($username,"quite")){
    echo " - <a href='index.php?mod=deletedocument&d=".$id."'>Remove</a>";
}
    echo "</div><table width=100%><tr>".user_info($row['username'])."</td><td>".wordwrapper($row['content']). "</td><td><p lign='right'></p></td></tr>
</table>";
if(is_logged()){
    if($row['rating-enabled'] && (!$rating = mysql_fetch_assoc(query("select * from `document_ratings` where `username` = '$username' and `document_id` = '".$row['id']."'")))) {
    echo "<hr><span style='float:left'><form method='post' action='index.php?id=ratedoc&d=$id'>
    Rate: <select name='frating' class='dropdown'>
            <option value='1'>1
            <option value='2'>2
            <option value='3'>3
            <option value='4'>4
            <option value='5'>5
        </select>    <input type='submit' class='submit'>
    </form></span>";
    }
}
echo "</div>";
    }
    elseif($type == "thread_edit"){
            $result = query("select * from `thread_edits` where `id` = '$id'");
            if(!$row = mysql_fetch_assoc($result)){
                print_error("print_post error: Invalid thread ID.");
                exit;
            }
            $create_time = mysql_fetch_assoc(query("select `date`,`time` from `threads` where `id` = '".$row['thread-id']."' order by `id`"));
    echo "
    <div class='post_title'><b><a href='index.php?id=viewthread&t=".$id."'>".$row['topic-before']."</a></b> on <i>".$create_time['date']."</i> at <i>".$create_time['time']."</i></div>

    <table width=100%><tr>".user_info($row['username'])."<td>".wordwrapper($row['content-before'])."</td></tr></table><hr>";
    echo "<div class='post_container'>";
    echo "<div class='post_title'><b><a href='index.php?id=viewthread&t=".$id."'>".$row['topic-after']."</a></b> on <i>".$row['date']."</i> at <i>".$row['time']."</i></div>

    <table width=100%><tr>".user_info($row['username'])."<td>".wordwrapper($row['content-after'])."</td></tr></table></div></div>";
        echo "<div style='float:right'><b>^ Edit by ".$row['username']." on <i>".$row['date']."</i> at <i>".$row['time']."</i></b>";
            
        }
        elseif($type == "post_edit"){
                $result = query("select * from `posts_edits` where `id` = '$id'");
                if(!$row = mysql_fetch_assoc($result)){
                    print_error("print_post error: Invalid post ID.");
                    exit;
                }
                $create_time = mysql_fetch_assoc(query("select `date`,`time` from `posts` where `id` = '".$row['post-id']."'"));
    echo "<div class='post_title'><b><a href='index.php?id=viewpost&p=".$row['post-id']."'>".$row['topic-before']."</a></b> on <i>".$create_time['date']."</i> at <i>".$create_time['time']."</i></div><table width=100%><tr>".user_info($row['username'])."<td>".wordwrapper($row['content-before'])."</td></tr></table><hr>";
    echo "<div class='post_container'>";
    echo "<div class='post_title'><b><a href='index.php?id=viewpost&p=".$row['post-id']."'>".$row['topic-after']."</a></b> on <i>".$row['date']."</i> at <i>".$row['time']."</i></div>

    <table width=100%><tr>".user_info($row['username'])."<td>".wordwrapper($row['content-after'])."</td></tr></table></div></div>";
        echo "<div style='float:right'><b>^ Edit by ".$row['username']." on <i>".$row['date']."</i> at <i>".$row['time']."</i></b>";
            
        }
        elseif($type == "message"){
            $result = query("select * from `messages` where `id` = '$id'");
            if(!$row = mysql_fetch_assoc($result)){
                print_error("print_post error: Invalid message ID.");
                exit;
            }
            if($row['viewed'] != 1){
                query("update `messages` set `viewed` = '1' where `id` = '".$id."'");
            }
            echo "<div class='post_title'><b><a href='index.php?id=messages&m=".$id."'>".$row['subject']."</a></b> on <i>".$row['date']."</i> at 
<i>".$row['time']."</i> - <a href='index.php?id=messages&compose=1&to=".$row['from']."&subject=".$row['subject']."'>Respond</a></div><table width=100%><tr>".user_info($row['from'])."</td><td>".wordwrapper($row['content']). "</td><td></td></tr>
</table></div>";
        }
        else{
            print_error("Print_post() error: Unknown post type " . $type .".");
            exit;
        }
}
?>