Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
831 florian 1
<?php
2
 
3
////////////////////////////////////////////////////////////////////////////////
4
//                                                                            //
5
//   Copyright (C) 2006  Phorum Development Team                              //
6
//   http://www.phorum.org                                                    //
7
//                                                                            //
8
//   This program is free software. You can redistribute it and/or modify     //
9
//   it under the terms of either the current Phorum License (viewable at     //
10
//   phorum.org) or the Phorum License that was distributed with this file    //
11
//                                                                            //
12
//   This program is distributed in the hope that it will be useful,          //
13
//   but WITHOUT ANY WARRANTY, without even the implied warranty of           //
14
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     //
15
//                                                                            //
16
//   You should have received a copy of the Phorum License                    //
17
//   along with this program.                                                 //
18
////////////////////////////////////////////////////////////////////////////////
19
 
20
if ( !defined( "PHORUM" ) ) return;
21
 
22
/**
23
 * Formats forum messages.
24
 *
25
 * @param data - An array containing a messages to be formatted.
26
 * @return data - The formatted messages.
27
 */
28
function phorum_format_messages ($data)
29
{
30
    $PHORUM = $GLOBALS["PHORUM"];
31
 
32
    // Prepare the bad-words replacement code.
33
    $bad_word_check= false;
34
    $banlists = phorum_db_get_banlists();
35
    if (isset($banlists[PHORUM_BAD_WORDS]) && is_array($banlists[PHORUM_BAD_WORDS])) {
36
        $replace_vals  = array();
37
        $replace_words = array();
38
        foreach ($banlists[PHORUM_BAD_WORDS] as $item) {
39
            $replace_words[] = "/\b".preg_quote($item['string'])."(ing|ed|s|er|es)*\b/";
40
            $replace_vals[]  = PHORUM_BADWORD_REPLACE;
41
            $bad_word_check  = true;
42
        }
43
    }
44
 
45
    // A special <br> tag to keep track of breaks that are added by phorum.
46
    $phorum_br = '<phorum break>';
47
 
48
    // Apply Phorum's formatting rules to all messages.
49
    foreach( $data as $key => $message )
50
    {
51
        // Work on the message body ========================
52
 
53
        if (isset($message["body"]))
54
        {
55
            $body = $message["body"];
56
 
57
            // Convert legacy <> urls into bare urls.
58
            $body = preg_replace("/<((http|https|ftp):\/\/[a-z0-9;\/\?:@=\&\$\-_\.\+!*'\(\),~%]+?)>/i", "$1", $body);
59
 
60
            // Escape special HTML characters. The function htmlspecialchars()
61
            // does too much, prior to PHP version 4.0.3.
62
            $body = str_replace(array("&","<",">"), array("&amp;","&lt;","&gt;"), $body);
63
 
64
            // Replace newlines with $phorum_br temporarily.
65
            // This way the mods know what Phorum did vs the user.
66
            $body = str_replace("\n", "$phorum_br\n", $body);
67
 
68
            // Run bad word replacement code.
69
            if($bad_word_check) {
70
               $body = preg_replace($replace_words, $replace_vals, $body);
71
            }
72
 
73
            $data[$key]["body"] = $body;
74
        }
75
 
76
        // Work on the other fields ========================
77
 
78
        // Run bad word replacement code on subject and author.
79
        if($bad_word_check) {
80
            if (isset($message["subject"]))
81
                $message["subject"] = preg_replace($replace_words, $replace_vals, $message["subject"]);
82
            if (isset($message["author"]))
83
                $message["author"] = preg_replace($replace_words, $replace_vals, $message["author"]);
84
        }
85
 
86
        // Escape special HTML characters in fields.
87
        if (isset($message["email"]))
88
            $data[$key]["email"] = str_replace(array("<",">"), array("&lt;","&gt;"), $message["email"]);
89
        if (isset($message["subject"]))
90
            $data[$key]["subject"] = str_replace(array("&","<",">"), array("&amp;","&lt;","&gt;"), $message["subject"]);
91
 
92
        // Some special things we have to do for the escaped author name.
93
        // We never should have put HTML in the core. Now we have to
94
        // do this hack to get the escaped author name in the linked_author.
95
        if (isset($message["author"])) {
96
            $data[$key]["author"]  = str_replace(array("<",">"), array("&lt;","&gt;"), $message["author"]);
97
            $safe_author = str_replace(array("&","<",">"), array("&amp;","&lt;","&gt;"), $message["author"]);
98
            if ($safe_author != $data[$key]["author"] && isset($data[$key]["linked_author"])) {
99
                $data[$key]["linked_author"] = str_replace($data[$key]["author"], $safe_author, $data[$key]["linked_author"]);
100
                $data[$key]["author"] = $safe_author;
101
            }
102
        }
103
    }
104
 
105
    // A hook for module writers to apply custom message formatting.
106
    $data = phorum_hook("format", $data);
107
 
108
    // Clean up after the mods are done.
109
    foreach( $data as $key => $message ) {
110
 
111
        // Clean up line breaks inside pre and xmp tags. These tags
112
        // take care of showing newlines as breaks themselves.
113
        if (isset($message["body"])) {
114
            foreach (array("pre","goep","xmp") as $tagname) {
115
                if (preg_match_all( "/(<$tagname.*?>).+?(<\/$tagname>)/si", $message["body"], $matches)) {
116
                    foreach ($matches[0] as $match) {
117
                        $stripped = str_replace ($phorum_br, "", $match);
118
                        $message["body"] = str_replace ($match, $stripped, $message["body"]);
119
                    }
120
                }
121
            }
122
            // Remove line break after quote and code tags. These tags have
123
            // their own line break. Without this, there would be to much
124
            // white lines.
125
            $message["body"] = preg_replace("/\s*(<\/*(xmp|blockquote|pre).*?>)\s*\Q$phorum_br\E/", "$1", $message["body"]);
126
 
127
            // Normalize the Phorum line breaks that are left.
128
            $data[$key]["body"] = str_replace($phorum_br, "<br />", $message["body"]);
129
        }
130
    }
131
 
132
    return $data;
133
}
134
 
135
/**
136
 * Formats an epoch timestamp to a date/time for displaying on screen.
137
 *
138
 * @param picture - The time formatting to use, in strftime() format
139
 * @param ts - The epoch timestamp to format
140
 * @return datetime - The formatted date/time string
141
 */
142
function phorum_date( $picture, $ts )
143
{
144
    $PHORUM = $GLOBALS["PHORUM"];
145
 
146
    // Setting locale.
147
    if (!isset($PHORUM['locale']))
148
        $PHORUM['locale']="EN";
149
    setlocale(LC_TIME, $PHORUM['locale']);
150
 
151
    // Format the date.
152
    if ($PHORUM["user_time_zone"] && isset($PHORUM["user"]["tz_offset"]) && $PHORUM["user"]["tz_offset"]!=-99) {
153
        $ts += $PHORUM["user"]["tz_offset"] * 3600;
154
        return gmstrftime( $picture, $ts );
155
    } else {
156
        $ts += $PHORUM["tz_offset"] * 3600;
157
        return strftime( $picture, $ts );
158
    }
159
}
160
 
161
/**
162
 * Strips HTML <tags> and BBcode [tags] from the body.
163
 *
164
 * @param body - The block of body text to strip
165
 * @return stripped - The stripped body
166
 */
167
function phorum_strip_body( $body )
168
{
169
    // Strip HTML <tags>
170
    $stripped = preg_replace("|</*[a-z][^>]*>|i", "", $body);
171
    // Strip BB Code [tags]
172
    $stripped = preg_replace("|\[/*[a-z][^\]]*\]|i", "", $stripped);
173
 
174
    return $stripped;
175
}
176
 
177
/**
178
 * Formats a file size in bytes to a human readable format. Human
179
 * readable formats are MB (MegaByte), kB (KiloByte) and byte.
180
 *
181
 * @param bytes - The number of bytes
182
 * @param formatted - The formatted size
183
 */
184
function phorum_filesize( $bytes )
185
{
186
    if ($bytes >= 1024*1024) {
187
        return round($bytes/1024/1024, 2) . "MB";
188
    } elseif ($bytes >= 1024) {
189
        return round($bytes/1024, 1) . "kB";
190
    } else {
191
        return $bytes . ($bytes == 1 ? " byte" : " bytes");
192
    }
193
}
194
 
195
?>