示例#1
0
function bbcode2email($text, $wrap_length = 72)
{
    static $base_url;
    if (!isset($base_url)) {
        $base_url = get_base_url();
    }
    $text = pun_trim($text, "\t\n ");
    $shortcut_urls = array('topic' => '/viewtopic.php?id=$1', 'post' => '/viewtopic.php?pid=$1#p$1', 'forum' => '/viewforum.php?id=$1', 'user' => '/profile.php?id=$1');
    // Split code blocks and text so BBcode in codeblocks won't be touched
    list($code, $text) = extract_blocks($text, '[code]', '[/code]');
    // Strip all bbcodes, except the quote, url, img, email, code and list items bbcodes
    $text = preg_replace(array('%\\[/?(?!(?:quote|url|topic|post|user|forum|img|email|code|list|\\*))[a-z]+(?:=[^\\]]+)?\\]%i', '%\\n\\[/?list(?:=[^\\]]+)?\\]%i'), '', $text);
    // Match the deepest nested bbcode
    // An adapted example from Mastering Regular Expressions
    $match_quote_regex = '%
		\\[(quote|\\*|url|img|email|topic|post|user|forum)(?:=([^\\]]+))?\\]
		(
			(?>[^\\[]*)
			(?>
				(?!\\[/?\\1(?:=[^\\]]+)?\\])
				\\[
				[^\\[]*
			)*
		)
		\\[/\\1\\]
	%ix';
    $url_index = 1;
    $url_stack = array();
    while (preg_match($match_quote_regex, $text, $matches)) {
        // Quotes
        if ($matches[1] == 'quote') {
            // Put '>' or '> ' at the start of a line
            $replacement = preg_replace(array('%^(?=\\>)%m', '%^(?!\\>)%m'), array('>', '> '), $matches[2] . " said:\n" . $matches[3]);
        } elseif ($matches[1] == '*') {
            $replacement = ' * ' . $matches[3];
        } elseif (in_array($matches[1], array('url', 'email'))) {
            if (!empty($matches[2])) {
                $replacement = '[' . $matches[3] . '][' . $url_index . ']';
                $url_stack[$url_index] = $matches[2];
                $url_index++;
            } else {
                $replacement = '[' . $matches[3] . ']';
            }
        } elseif ($matches[1] == 'img') {
            if (!empty($matches[2])) {
                $replacement = '[' . $matches[2] . '][' . $url_index . ']';
            } else {
                $replacement = '[' . basename($matches[3]) . '][' . $url_index . ']';
            }
            $url_stack[$url_index] = $matches[3];
            $url_index++;
        } elseif (in_array($matches[1], array('topic', 'post', 'forum', 'user'))) {
            $url = isset($shortcut_urls[$matches[1]]) ? $base_url . $shortcut_urls[$matches[1]] : '';
            if (!empty($matches[2])) {
                $replacement = '[' . $matches[3] . '][' . $url_index . ']';
                $url_stack[$url_index] = str_replace('$1', $matches[2], $url);
                $url_index++;
            } else {
                $replacement = '[' . str_replace('$1', $matches[3], $url) . ']';
            }
        }
        // Update the main text if there is a replacment
        if (!is_null($replacement)) {
            $text = str_replace($matches[0], $replacement, $text);
            $replacement = null;
        }
    }
    // Put code blocks and text together
    if (isset($code)) {
        $parts = explode("", $text);
        $text = '';
        foreach ($parts as $i => $part) {
            $text .= $part;
            if (isset($code[$i])) {
                $text .= trim($code[$i], "\n\r");
            }
        }
    }
    // Put URLs at the bottom
    if ($url_stack) {
        $text .= "\n\n";
        foreach ($url_stack as $i => $url) {
            $text .= "\n" . ' [' . $i . ']: ' . $url;
        }
    }
    // Wrap lines if $wrap_length is higher than -1
    if ($wrap_length > -1) {
        // Split all lines and wrap them individually
        $parts = explode("\n", $text);
        foreach ($parts as $k => $part) {
            preg_match('%^(>+ )?(.*)%', $part, $matches);
            $parts[$k] = wordwrap($matches[1] . $matches[2], $wrap_length - strlen($matches[1]), "\n" . $matches[1]);
        }
        return implode("\n", $parts);
    } else {
        return $text;
    }
}
示例#2
0
function parse_message($text)
{
    global $luna_config, $luna_user;
    if ($luna_config['o_censoring'] == '1') {
        $text = censor_words($text);
    }
    // Convert applicable characters to HTML entities
    $text = luna_htmlspecialchars($text);
    // If the message contains a code tag we have to split it up (text within [code][/code] shouldn't be touched)
    if (strpos($text, '[code]') !== false && strpos($text, '[/code]') !== false) {
        list($inside, $text) = extract_blocks($text, '[code]', '[/code]');
    }
    if (strpos($text, '[') !== false && strpos($text, ']') !== false) {
        $text = do_bbcode($text);
    }
    $text = do_smilies($text);
    // Deal with newlines, tabs and multiple spaces
    $pattern = array("\n", "\t", '  ', '  ');
    $replace = array('<br />', '&#160; &#160; ', '&#160; ', ' &#160;');
    $text = str_replace($pattern, $replace, $text);
    // If we split up the message before we have to concatenate it together again (code tags)
    if (isset($inside)) {
        $parts = explode("", $text);
        $text = '';
        foreach ($parts as $i => $part) {
            $text .= $part;
            if (isset($inside[$i])) {
                $num_lines = substr_count($inside[$i], "\n");
                $code_line = explode("\n", $inside[$i]);
                $first_line = trim($code_line[1]);
                if (strpos($first_line, '[[') !== false && strpos($first_line, ']]') !== false) {
                    // fetching the language name
                    $language = strtolower(trim(str_replace(array('[[', ']]'), '', $first_line)));
                    if ($language == 'html' || $language == 'xhtml' || $language == 'xml') {
                        // Markup case
                        $h_class = ' class="language-markup"';
                    } elseif ($language == 'php' || $language == 'c++' || $language == 'perl') {
                        // C-like languages case
                        $h_class = ' class="language-clike"';
                    } elseif ($language == 'javascript') {
                        // JavaScript case
                        $h_class = ' class="language-javascript"';
                    } elseif ($language == 'php') {
                        // C-like languages case
                        $h_class = ' class="language-php"';
                    } else {
                        // Other cases
                        $h_class = '';
                    }
                    // Deleting the line giving the code name
                    $inside[$i] = str_replace($first_line, '', $inside[$i]);
                    // Generating the the HTML code block
                    $text .= '</p><div class="codebox"><pre' . ($num_lines > 28 ? ' class="vscroll"' : '') . '><code' . $h_class . '>' . luna_trim($inside[$i], "\n\r") . '</code></pre></div><p>';
                } else {
                    $text .= '</p><div class="codebox"><pre' . ($num_lines > 28 ? ' class="vscroll"' : '') . '><code>' . luna_trim($inside[$i], "\n\r") . '</code></pre></div><p>';
                }
            }
        }
    }
    return clean_paragraphs($text);
}
示例#3
0
function parse_message($text, $hide_smilies)
{
    global $pun_config, $lang_common, $pun_user;
    if ($pun_config['o_censoring'] == '1') {
        $text = censor_words($text);
    }
    // Convert applicable characters to HTML entities
    $text = pun_htmlspecialchars($text);
    // If the message contains a code tag we have to split it up (text within [code][/code] shouldn't be touched)
    if (strpos($text, '[code]') !== false && strpos($text, '[/code]') !== false) {
        list($inside, $text) = extract_blocks($text, '[code]', '[/code]');
    }
    if ($pun_config['p_message_bbcode'] == '1' && strpos($text, '[') !== false && strpos($text, ']') !== false) {
        $text = do_bbcode($text);
    }
    if ($pun_config['o_smilies'] == '1' && $pun_user['show_smilies'] == '1' && $hide_smilies == '0') {
        $text = do_smilies($text);
    }
    // Deal with newlines, tabs and multiple spaces
    $pattern = array("\n", "\t", '  ', '  ');
    $replace = array('<br />', '&#160; &#160; ', '&#160; ', ' &#160;');
    $text = str_replace($pattern, $replace, $text);
    // If we split up the message before we have to concatenate it together again (code tags)
    if (isset($inside)) {
        $parts = explode("", $text);
        $text = '';
        foreach ($parts as $i => $part) {
            $text .= $part;
            if (isset($inside[$i])) {
                $num_lines = substr_count($inside[$i], "\n");
                $text .= '</p><div class="codebox"><pre' . ($num_lines > 28 ? ' class="vscroll"' : '') . '><code>' . pun_trim($inside[$i], "\n\r") . '</code></pre></div><p>';
            }
        }
    }
    return clean_paragraphs($text);
}
示例#4
0
function set_file($name, $filename)
{
    extract_blocks($name, load_file($filename));
}
示例#5
0
    public function bbcode2email($text, $wrap_length = 72)
    {
        $text = panther_trim($text, "\t\n ");
        $shortcut_urls = array('topic' => $this->url['topic'], 'post' => $this->url['post'], 'forum' => $this->url['forum'], 'user' => $this->url['profile']);
        // Split code blocks and text so BBcode in codeblocks won't be touched
        list($code, $text) = extract_blocks($text, '[code]', '[/code]');
        // Strip all bbcodes, except the quote, url, img, email, code and list items bbcodes
        $text = preg_replace(array('%\\[/?(?!(?:quote|url|topic|post|user|forum|img|email|code|list|\\*))[a-z]+(?:=[^\\]]+)?\\]%i', '%\\n\\[/?list(?:=[^\\]]+)?\\]%i'), '', $text);
        // Match the deepest nested bbcode
        // An adapted example from Mastering Regular Expressions
        $match_quote_regex = '%
			\\[(quote|\\*|url|img|email|topic|post|user|forum)(?:=([^\\]]+))?\\]
			(
				(?>[^\\[]*)
				(?>
					(?!\\[/?\\1(?:=[^\\]]+)?\\])
					\\[
					[^\\[]*
				)*
			)
			\\[/\\1\\]
		%ix';
        $url_index = 1;
        $url_stack = array();
        while (preg_match($match_quote_regex, $text, $matches)) {
            // Quotes
            if ($matches[1] == 'quote') {
                // Put '>' or '> ' at the start of a line
                $replacement = preg_replace(array('%^(?=\\>)%m', '%^(?!\\>)%m'), array('>', '> '), $matches[2] . ' ' . $this->lang['wrote'] . '\\n' . $matches[3]);
            } elseif ($matches[1] == '*') {
                $replacement = ' * ' . $matches[3];
            } elseif (in_array($matches[1], array('url', 'email'))) {
                if (!empty($matches[2])) {
                    $replacement = '[' . $matches[3] . '][' . $url_index . ']';
                    $url_stack[$url_index] = $matches[2];
                    $url_index++;
                } else {
                    $replacement = '[' . $matches[3] . ']';
                }
            } elseif ($matches[1] == 'img') {
                if (!empty($matches[2])) {
                    $replacement = '[' . $matches[2] . '][' . $url_index . ']';
                } else {
                    $replacement = '[' . basename($matches[3]) . '][' . $url_index . ']';
                }
                $url_stack[$url_index] = $matches[3];
                $url_index++;
            } elseif (in_array($matches[1], array('topic', 'post', 'forum', 'user'))) {
                $arg = '';
                if ($matches[1] == 'topic') {
                    $data = array(':id' => $matches[3]);
                    $ps = $this->db->select('topics', 'subject', $data, 'id=:id');
                    $arg = url_friendly($ps->fetchColumn());
                } else {
                    if ($matches[1] == 'forum') {
                        if (isset($this->forums[$matches[3]])) {
                            $arg = url_friendly($this->forums[$matches[3]]['forum_name']);
                        }
                    } else {
                        if ($matches[1] == 'user') {
                            $data = array(':username' => $matches[3]);
                            $ps = $this->db->select('users', 'id', $data, 'username=:username');
                            $id = $ps->fetchColumn();
                            $arg = url_friendly($matches[3]);
                            $matches[3] = $id;
                        }
                    }
                }
                if (!empty($matches[2])) {
                    $replacement = '[' . $matches[3] . '][' . $url_index . ']';
                    $url_stack[$url_index] = panther_link($shortcut_urls[$matches[1]], array($matches[2], $arg));
                    $url_index++;
                } else {
                    $replacement = '[' . panther_link($shortcut_urls[$matches[1]], array($matches[3], $arg)) . ']';
                }
            }
            // Update the main text if there is a replacement
            if (!is_null($replacement)) {
                $text = str_replace($matches[0], $replacement, $text);
                $replacement = null;
            }
        }
        // Put code blocks and text together
        if (isset($code)) {
            $parts = explode("", $text);
            $text = '';
            foreach ($parts as $i => $part) {
                $text .= $part;
                if (isset($code[$i])) {
                    $text .= trim($code[$i], "\n\r");
                }
            }
        }
        // Put URLs at the bottom
        if ($url_stack) {
            $text .= "\n\n";
            foreach ($url_stack as $i => $url) {
                $text .= "\n" . ' [' . $i . ']: ' . $url;
            }
        }
        // Wrap lines if $wrap_length is higher than -1
        if ($wrap_length > -1) {
            // Split all lines and wrap them individually
            $parts = explode("\n", $text);
            foreach ($parts as $k => $part) {
                preg_match('%^(>+ )?(.*)%', $part, $matches);
                $parts[$k] = wordwrap($matches[1] . $matches[2], $wrap_length - strlen($matches[1]), "\n" . $matches[1]);
            }
            return implode("\n", $parts);
        } else {
            return $text;
        }
    }
function parse_message($text, $hide_smilies)
{
    global $pun_config, $lang_common, $pun_user;
    if ($pun_config['o_censoring'] == '1') {
        $text = censor_words($text);
    }
    // If the message contains a code tag we have to split it up (text within [code][/code] shouldn't be touched)
    if (strpos($text, '[code]') !== false && strpos($text, '[/code]') !== false) {
        list($inside, $text) = extract_blocks($text, '[code]', '[/code]');
    }
    $Parsedown = new Parsedown();
    $text = $Parsedown->setMarkupEscaped(true)->setUrlsLinked(false)->text($text);
    if ($pun_config['o_make_links'] == '1') {
        $text = do_clickable($text);
    }
    // If the message contains code in Markdown
    if (strpos($text, '[code]') !== false && strpos($text, '[/code]') !== false) {
        // If we have code from BBCode, put them back into the text
        if (isset($inside)) {
            $parts = explode("", $text);
            $text = '';
            foreach ($parts as $i => $part) {
                $text .= $part . (isset($inside[$i]) ? '[code]' . $inside[$i] . '[/code]' : '');
            }
        }
        // And take them out again.
        list($inside, $text) = extract_blocks($text, '[code]', '[/code]');
    }
    if ($pun_config['p_message_bbcode'] == '1' && strpos($text, '[') !== false && strpos($text, ']') !== false) {
        $text = do_bbcode($text);
    }
    if ($pun_config['o_smilies'] == '1' && $pun_user['show_smilies'] == '1' && $hide_smilies == '0') {
        $text = do_smilies($text);
    }
    // Deal with newlines, tabs and multiple spaces
    $pattern = array("\n", "\t", '  ', '  ');
    $replace = array('<br />', '&#160; &#160; ', '&#160; ', ' &#160;');
    $text = str_replace($pattern, $replace, $text);
    // If we split up the message before we have to concatenate it together again (code tags)
    if (isset($inside)) {
        $parts = explode("", $text);
        $text = '';
        foreach ($parts as $i => $part) {
            $text .= $part;
            if (isset($inside[$i])) {
                $num_lines = substr_count($inside[$i], "\n");
                $text .= '</p><div class="codebox"><pre' . ($num_lines > 28 ? ' class="vscroll"' : '') . '><code>' . pun_trim(pun_htmlspecialchars($inside[$i]), "\n\r") . '</code></pre></div><p>';
            }
        }
    }
    return clean_paragraphs($text);
}