Source of: ubbc.php (Download Source)
Last Modified: Fri, 16 Feb 2007 13:38:18 UTC

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
<?php

"
 WARNING: THIS CODE CONTAINS SECURITY LEAKS (XSS)
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 Don't use it without modification! 
"
/*****************************************************************************/
/* UBB Code                                                                  */
/* Module for phpClanpage                                                    */
/* Copyright (c) 2003 The phpClanpage Team                                   */
/*                                                                           */
/* Homepage: http://phpclanpage.blar.de                                      */
/*    Board: http://boards.blar.de/main                                      */
/*                                                                           */
/* This program is free software; you can redistribute it and/or modify it   */
/* under the terms of the GNU General Public License as published by the     */
/* Free Software Foundation; either version 2 of the License, or (at your    */
/* option) any later version.                                                */
/*                                                                           */
/* This program is distributed in the hope that it will be useful, but       */
/* WITHOUT ANY WARRANTY; without even the implied warranty of                */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General */
/* Public License for more details.                                          */
/*****************************************************************************/

/* Usage:
 * $text = ubbc($text);
 * $array = ubbc($array);
 */

define('UBBC_MARK', md5(uniqid(microtime())));
function ubbc($text) {

    if (is_array($text)) {
        foreach ($text as $key=>$val) {
            $text[$key] = ubbc($val);
        }
        return $text;
    }

    // [code][/code]-Abschnitte speichern

    $code_sections = array();
    while (preg_match('=\[code\].*?\[/code\]=s', $text)) {
        $text = preg_replace('=\[code\](.*?)\[/code\]=se', 'ubbc_save_code_sections("code", \'$1\', $code_sections)', $text);
    }
    // [fixed][/fixed]-Abschnitte speichern (haben dieselbe Wirkung wie [code][/code]-Abschnitte)

    while (preg_match('=\[fixed\].*?\[/fixed\]=s', $text)) {
        $text = preg_replace('=\[fixed\](.*?)\[/fixed\]=se', 'ubbc_save_code_sections("code", \'$1\', $code_sections)', $text);
    }
    // < ?php ? > code speichern

    while (preg_match('=<\?php.*?\?>=s', $text)) {
        $text = preg_replace('=<\?php(.*?)\?>=se', 'ubbc_save_code_sections("php", \'$1\', $code_sections)', $text);
    }

    // HTML-Code ersetzen

    $text = htmlentities($text);

    // $text = strtr($text,$smilies);


    // fett, kursiv, unter- und durchgestrichen

    $text = preg_replace("=\[b\](.*?)\[/b\]=is", '<b>\1</b>', $text);
    $text = preg_replace("=\[i\](.*?)\[/i\]=is", '<i>\1</i>', $text);
    $text = preg_replace("=\[u\](.*?)\[/u\]=is", '<u>\1</u>', $text);
    $text = preg_replace("=\[s\](.*?)\[/s\]=is", '<s>\1</s>', $text);

    $text = preg_replace("=\[sup\](.*?)\[/sup\]=is", '<sup>\1</sup>', $text);
    $text = preg_replace("=\[sub\](.*?)\[/sub\]=is", '<sub>\1</sub>', $text);

    $text = preg_replace("=\[left\](.*?)\[/left\]=is", '<left>\1</left>', $text);
    $text = preg_replace("=\[center\](.*?)\[/center\]=is", '<center>\1</center>', $text);
    $text = preg_replace("=\[right\](.*?)\[/right\]=is", '<right>\1</right>', $text);


    $text = preg_replace("=\[tt\](.*?)\[/tt\]=is", '<tt>\1</tt>', $text);

    // URLs

    $text = preg_replace('=\[url\]\s*(.*?)\s*\[/url\]=ie', '"<a href=\"$1\" target=\"" . gettarget("$1") . "\">$1</a>"', $text);
    // URIs mit Linktext

    $text = preg_replace('=\[url\s*\=\s*(.*?)\s*\]\s*(.*?)\s*\[/url\]=ie', '"<a href=\"$1\" target=\"" . gettarget("$1") . "\">$2</a>"', $text);

    // URLs

    $text = preg_replace('=\[ftp\]\s*(.*?)\s*\[/ftp\]=ie', '"<a href=\"ftp://$1\" target=\"" . gettarget("$1") . "\">$1</a>"', $text);

    // Emails

    $text = preg_replace('=\[email\]\s*(.*?)\s*\[/email\]=i', '<a href="mailto:$1">$1</a>', $text);
    // Emails mit Linktext

    $text = preg_replace('=\[email\s*\=\s*(.*?)\s*\]\s*(.*?)\s*\[/email\]=i', '<a href="mailto:$1">$2</a>', $text);

    // Bilder

    $text = preg_replace('=\[img\]\s*(.*?)\s*\[/img\]=i', '<img src="$1" />', $text);

    // Farbangaben

    $text = preg_replace('=\[color\s*\=\s*(#[0-9a-f]{6}|[a-z]+)\s*\]\s*(.*?)\s*\[/color\]=si', '<span style="color:$1">$2</span>', $text);

    // Textgröße (bug? ist text von 0-9 nicht etwas verdammt klein)

    $text = preg_replace('=\[size\s*\=\s*([0-9]+)\s*(pt)?\s*\]\s*(.*?)\s*\[/size\]=si', '<span style="font-size:$1$2">$3</span>', $text);

    // Schriftart

    $text = preg_replace('=\[font\s*\=\s*([a-z ]+)\s*\]\s*(.*?)\s*\[/font\]=si', '<span style="font-family:$1">$2</span>', $text);

    // URIs mit http, ftp usw. am Anfang

    $text = preg_replace('=(\s|^)((ftp|gopher|https?|irc|mailto|news):(//)?((.*:)?.*@)?[a-z0-9\-._~/%&\?\=#;]+)(\s|$)=ie', '"$1<a href=\"$2\" target=\"" . gettarget("$2") . "\">$2</a>$7"', $text);
    // URIs, die mit www. beginnen

    $text = preg_replace('=(\s|^)(((.*:)?.*@)?www\.[a-z0-9\-._~/%&\?\=#;]+)(\s|$)=ie', '"$1<a href=\"http://$2\" target=\"" . gettarget("http://$2") . "\">$2</a>$5"', $text);

    // Zeilenumbrüche

    $text = nl2br($text);

    // Marquee

    $text = preg_replace("=\[move\](.*?)\[/move\]=is", '<marquee>\1</marquee>', $text);

    // Sonstige UBBC loeschen

    //$text = preg_replace("=\[(.*?)\]=is", '', $text);

    $text = preg_replace("=\[(.*?)\](.*?)\[\/(.*?)\]=is", '\2', $text);

    // [code][/code]-Abschnitte laden

    $text = preg_replace('=' . UBBC_MARK . '-php-([0-9]+)=e', 'get_highlight_string("<?php\n" . trim($code_sections[$1]) . "\n?>")', $text);
    // < ?php ? >-Codes darstellen

    $text = preg_replace('=' . UBBC_MARK . '-code-([0-9]+)=e' , '"<code>" . nl2br(htmlentities(trim($code_sections[$1]))) . "</code>"', $text);

    return $text;
}

function ubbc_save_code_sections($tag, $string, &$code_sections)
{
    static $n = 0;
    // String zum Array hinzufügen

    $code_sections[] = stripslashes($string);
    // Markierung zurückgeben

    return UBBC_MARK . "-$tag-" . ($n++);
}

function get_highlight_string($string)
{
    ob_start();
    highlight_string($string);
    $contents = ob_get_contents();
    ob_end_clean();
    return $contents;
}

// target frame für eine URI (URI der Clanseite: aktuelles Fenster, andere URI: neues Fenster)

function gettarget($uri)
{
    return (!strstr($uri, ':') or stristr($uri, $_SERVER['SERVER_NAME'])) ? '_self' : '_blank';
}

?>