~ubuntu-branches/ubuntu/precise/boinc/precise

« back to all changes in this revision

Viewing changes to html/inc/text_transform.inc

Tags: 6.12.8+dfsg-1
* New upstream release.
* Simplified debian/rules

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
    var $bb2html;            // BBCode as HTML? (on)
30
30
    var $images_as_links;    // Images as hyperlinks? (off)
31
31
    var $link_popup;        // Links in new windows? (off)
32
 
    var $closeTags;            // Close extra HTML tags? (on)
33
32
    var $nl2br;                // Convert newlines to <br>'s? (on)
34
33
    var $htmlitems;            // Convert special chars to HTML entities? (on)
35
34
    var $htmlscrub;            // Scrub "bad" HTML tags? (off)
41
40
        $this->bb2html = 1;
42
41
        $this->images_as_links = 0;
43
42
        $this->link_popup = 0;
44
 
        $this->closeTags = 1;
45
43
        $this->nl2br = 1;
46
44
        $this->htmlitems = 1;
47
45
        $this->htmlscrub = 0;
104
102
}
105
103
 
106
104
// Converts bbcode to proper HTML
 
105
// If $export is true, don't use BOINC CSS
107
106
 
108
 
function bb2html($text) {
 
107
function bb2html($text, $export=false) {
109
108
    $urlregex = "(?:\"?)(?:(http\:\/\/)?)([^\[\"<\ ]+)(?:\"?)";
110
109
    $httpsregex = "(?:\"?)https\:\/\/([^\[\"<\ ]+)(?:\"?)";
111
110
    // List of allowable tags
112
111
    $bbtags = array (
 
112
        "@\[pre\](.*?)\[/pre\]@eis",
 
113
        "@\[code\](.*?)\[/code\]@eis",
113
114
        "@\[b\](.*?)\[/b\]@is",
114
115
        "@\[i\](.*?)\[/i\]@is",
115
116
        "@\[u\](.*?)\[/u\]@is",
116
 
        "@\[url=$httpsregex\](.*?)\[/url\]@i",
117
 
        "@\[url\]$httpsregex\[/url\]@i",
118
 
        "@\[link=$urlregex\](.*?)\[/link\]@i",
119
 
        "@\[link\]$urlregex\[/link\]@i",
120
 
        "@\[url=$urlregex\](.*?)\[/url\]@i",
121
 
        "@\[url\]$urlregex\[/url\]@i",
 
117
        "@\[url=$httpsregex\](.*?)\[/url\]@is",
 
118
        "@\[url\]$httpsregex\[/url\]@is",
 
119
        "@\[link=$urlregex\](.*?)\[/link\]@is",
 
120
        "@\[link\]$urlregex\[/link\]@is",
 
121
        "@\[url=$urlregex\](.*?)\[/url\]@is",
 
122
        "@\[url\]$urlregex\[/url\]@is",
122
123
        "@\[quote=(.*?)\](.*?)\[/quote\]@is",
123
124
        "@\[quote\](.*?)\[/quote\]@is",
124
125
        "@\[list\](.*?)\[/list\]@is",
125
126
        "@\[list=1\](.*?)\[/list\]@is",  
126
 
        "@\[pre\](.*?)\[/pre\]@is",
127
127
        "@\[img\]$urlregex\[/img\]@is",
128
128
        "@\[color=(?:\"?)(.{3,8})(?:\"?)\](.*?)\[/color\]@is",
129
129
        "@((?:<ol>|<ul>).*?)\n\*([^\n]+)\n(.*?(</ol>|</ul>))@is",
130
130
        "@\[size=([1-9]|[0-2][0-9])\](.*?)\[/size\]@is",
131
 
        "@\[code\](.*?)\[/code\]@is",
132
131
        "@\[mailto\](.*?)\[/mailto\]@is",
133
132
        "@\[email\](.*?)\[/email\]@is",
134
133
        "@\[trac\](?:\#|ticket:)(\d+)\[/trac\]@is",
140
139
    );
141
140
    
142
141
    // What the above tags are turned in to
143
 
    $htmltags = array (
144
 
        "<b>\\1</b>",
145
 
        "<i>\\1</i>",
146
 
        "<u>\\1</u>",
147
 
        "<a href=\"https://\\1\" rel=\"nofollow\">\\2</a>",
148
 
        "<a href=\"https://\\1\" rel=\"nofollow\">https://\\1</a>",
149
 
        "<a href=\"http://\\2\" rel=\"nofollow\">\\3</a>",
150
 
        "<a href=\"http://\\2\" rel=\"nofollow\">http://\\2</a>",
151
 
        "<a href=\"http://\\2\" rel=\"nofollow\">\\3</a>",
152
 
        "<a href=\"http://\\2\" rel=\"nofollow\">http://\\2</a>",
153
 
        "<div style='font-style: oblique'>\\1 wrote:</div><blockquote class='postbody'>\\2</blockquote>",
154
 
        "<blockquote class='postbody'>\\1</blockquote>",
155
 
        "<ul>\\1</ul><p>",
156
 
        "<ol>\\1</ol><p>",
157
 
        "<pre>\\1</pre>",
158
 
        "<img src=\"http://\\2\">",
159
 
        "<font color=\"\\1\">\\2</font>",
160
 
        "\\1<li>\\2\n\\3",
161
 
        "<span style=\"font-size: \\1px;\">\\2</span>",
162
 
        "<div class=\"code\">\\1</div>",
163
 
        "<a href=\"mailto:\\1\">\\1</a>",
164
 
        "<a href=\"mailto:\\1\">\\1</a>",
165
 
        "<a href=\"http://boinc.berkeley.edu/trac/ticket/\\1\">#\\1</a>",
166
 
        "<a href=\"http://boinc.berkeley.edu/trac/wiki/\\1\">\\1</a>",
167
 
        "<a href=\"http://boinc.berkeley.edu/trac/changeset/\\1\">[\\1]</a>"
168
 
    );
 
142
    if ($export) {
 
143
        $htmltags = array (
 
144
            "'<pre>'.stop_recursion(remove_br('\\1')).'</pre>'",
 
145
            "'<code>'.stop_recursion('\\1').'</code>'",
 
146
            "<b>\\1</b>",
 
147
            "<i>\\1</i>",
 
148
            "<u>\\1</u>",
 
149
            "<a href=\"https://\\1\" rel=\"nofollow\">\\2</a>",
 
150
            "<a href=\"https://\\1\" rel=\"nofollow\">https://\\1</a>",
 
151
            "<a href=\"http://\\2\" rel=\"nofollow\">\\3</a>",
 
152
            "<a href=\"http://\\2\" rel=\"nofollow\">http://\\2</a>",
 
153
            "<a href=\"http://\\2\" rel=\"nofollow\">\\3</a>",
 
154
            "<a href=\"http://\\2\" rel=\"nofollow\">http://\\2</a>",
 
155
            "<i>\\1 wrote:</i><blockquote>\\2</blockquote>",
 
156
            "<blockquote>\\1</blockquote>",
 
157
            "<ul>\\1</ul><p>",
 
158
            "<ol>\\1</ol><p>",
 
159
            "<img hspace=\"8\" src=\"http://\\2\">",
 
160
            "<font color=\"\\1\">\\2</font>",
 
161
            "\\1<li>\\2\n\\3",
 
162
            "<span style=\"font-size: \\1px;\">\\2</span>",
 
163
            "<a href=\"mailto:\\1\">\\1</a>",
 
164
            "<a href=\"mailto:\\1\">\\1</a>",
 
165
            "<a href=\"http://boinc.berkeley.edu/trac/ticket/\\1\">#\\1</a>",
 
166
            "<a href=\"http://boinc.berkeley.edu/trac/wiki/\\1\">\\1</a>",
 
167
            "<a href=\"http://boinc.berkeley.edu/trac/changeset/\\1\">[\\1]</a>"
 
168
        );
 
169
    } else {
 
170
        $htmltags = array (
 
171
            "'<div class=\"pre\">'.stop_recursion(remove_br('\\1')).'</div>'",
 
172
            "'<div class=\"code\">'.stop_recursion('\\1').'</div>'",
 
173
            "<b>\\1</b>",
 
174
            "<i>\\1</i>",
 
175
            "<u>\\1</u>",
 
176
            "<a href=\"https://\\1\" rel=\"nofollow\">\\2</a>",
 
177
            "<a href=\"https://\\1\" rel=\"nofollow\">https://\\1</a>",
 
178
            "<a href=\"http://\\2\" rel=\"nofollow\">\\3</a>",
 
179
            "<a href=\"http://\\2\" rel=\"nofollow\">http://\\2</a>",
 
180
            "<a href=\"http://\\2\" rel=\"nofollow\">\\3</a>",
 
181
            "<a href=\"http://\\2\" rel=\"nofollow\">http://\\2</a>",
 
182
            "<div style='font-style: oblique'>\\1 wrote:</div><blockquote class='postbody'>\\2</blockquote>",
 
183
            "<blockquote class='postbody'>\\1</blockquote>",
 
184
            "<ul>\\1</ul><p>",
 
185
            "<ol>\\1</ol><p>",
 
186
            "<img hspace=\"8\" src=\"http://\\2\">",
 
187
            "<font color=\"\\1\">\\2</font>",
 
188
            "\\1<li>\\2\n\\3",
 
189
            "<span style=\"font-size: \\1px;\">\\2</span>",
 
190
            "<a href=\"mailto:\\1\">\\1</a>",
 
191
            "<a href=\"mailto:\\1\">\\1</a>",
 
192
            "<a href=\"http://boinc.berkeley.edu/trac/ticket/\\1\">#\\1</a>",
 
193
            "<a href=\"http://boinc.berkeley.edu/trac/wiki/\\1\">\\1</a>",
 
194
            "<a href=\"http://boinc.berkeley.edu/trac/changeset/\\1\">[\\1]</a>"
 
195
        );
 
196
    }
169
197
    
170
198
    // Do the actual replacing - iterations for nested items
171
199
    $lasttext = "";
172
200
    $i = 0;
173
 
    // $i<20 to prevent DoS
174
 
    while ($text != $lasttext && $i<20) {
 
201
    // $i<1000 to prevent DoS
 
202
    while ($text != $lasttext && $i<1000) {
175
203
        $lasttext = $text;
176
204
        $text = preg_replace($bbtags,$htmltags,$text);
177
205
        $i = $i + 1;
179
207
    return $text;
180
208
}
181
209
 
 
210
// Removes any <br> tags added by nl2br which are not wanted,
 
211
// for example inside <pre> containers
 
212
// The original \n was retained after the br when it was added
 
213
//
 
214
function remove_br($text){
 
215
    return str_replace("<br />", "", $text);
 
216
}
 
217
 
 
218
 
 
219
// Stops recursion of BBCode by escaping any [ in the given text
 
220
// @param $text The text to stop recursion in
 
221
// @return A text that no longer can cause recursion
 
222
//
 
223
function stop_recursion($text){
 
224
    return str_replace("[", "&#91;", $text);
 
225
}
 
226
 
182
227
// Make links open in new windows.
183
 
 
184
228
function externalize_links($text) {
185
229
    // TODO:  Convert this to PCRE
186
230
    $i=0;$linkpos=true;
199
243
function image_as_link($text){
200
244
    /* This function depends on sanitized HTML */
201
245
    // Build some regex (should be a *lot* faster)
202
 
    $pattern = '@<img src=([^>]+)>@si'; // Gives us the URL in ${1}...
203
 
    $replacement = '<a href=${1}>[Image Link]</a>'; // Turns that URL into a hyperlink
 
246
    $pattern = '@<img([\S\s]+?)src=([^>]+?)>@si';
 
247
    $replacement = '<a href=${2}>[Image link]</a>'; // Turns that URL into a hyperlink
204
248
    $text = preg_replace($pattern, $replacement, $text);
205
249
    return $text;
206
250
}
207
251
 
208
 
// Closes open HTML tags.  Not quite perfect...
209
 
 
210
 
function closeTags($str = null) {
211
 
    // Function from http://episteme.arstechnica.com/eve/ubb.x/a/tpc/f/6330927813/m/139006252731/r/287008552731#287008552731
212
 
    // (thanks Ageless for finding it)
213
 
    // Edited by Rob to better fit in with boinc's needs
214
 
    
215
 
    // List of tags to check $str for
216
 
    // TODO:  Adapt to use the pre-existing array of tags above
217
 
    $tags = array('b', 'i', 'a', 'p', 'font[^>]?', 'strong', 'ul', 'li', 'pre', 'blockquote', 'u');
218
 
    // Note on $tags - no br or img, as they have no closing tags - can we define this above?
219
 
    // Maybe define two arrays, those with closing tags and those without, and combine the
220
 
    // two of them for the standard HTML sanitizing function?
221
 
 
222
 
    // Don't do anything if the string is too short
223
 
    if (strlen($str) < 3) {
224
 
        return $str;
225
 
    } else {
226
 
        // Loop over $str and count the opening and closing for each tag in $tags
227
 
        foreach ($tags as $tag) {
228
 
            $m = array();
229
 
            $o = preg_match_all("/<(".$tag.")>/", $str, $m);
230
 
            $c = substr_count($str, "</{$tag}>");
231
 
 
232
 
            $open[$tag]  = ($o < $c) ? $c - $o : 0;
233
 
            $close[$tag] = ($c < $o) ? $o - $c : 0;
234
 
 
235
 
            // Debuggin'
236
 
            //echo "<pre>Tag: {$tag}\nOpen: {$o}\nClose: {$c}\nOT: {$open[$tag]}\nCT: {$close[$tag]}</pre><hr />";
237
 
        }
238
 
 
239
 
        // Prepend the return string with an opening tag as needed
240
 
        /* $pre = '';  ...uhh... doesn't work right
241
 
 
242
 
        foreach ($open as $tag => $cnt) {
243
 
            $pre .= ($cnt > 0) ? "<{$tag}>" : '';
244
 
        }  */
245
 
 
246
 
        // Append the return string with a closing tag as needed
247
 
        $post = '';
248
 
 
249
 
        foreach ($close as $tag => $cnt) {
250
 
            $post .= ($cnt > 0) ? "</{$tag}>" : '';
251
 
        }
252
 
 
253
 
        return /*$pre.*/$str.$post;
254
 
    }
255
 
}
256
 
 
257
 
 
258
252
// Highlight terms in text (most likely used with searches)
259
253
 
260
254
function highlight_terms($text, $terms) {
271
265
    }
272
266
}
273
267
 
274
 
$cvs_version_tracker[]="\$Id: text_transform.inc 16193 2008-10-12 17:11:19Z jbk $";  //Generated automatically - do not edit
 
268
$cvs_version_tracker[]="\$Id: text_transform.inc 22228 2010-08-14 01:49:08Z davea $";  //Generated automatically - do not edit
275
269
?>