get_all_set() public method

public get_all_set ( )
Esempio n. 1
0
    /**
     * Init bbcode cache
     *
     * requires: $this->bbcode_bitfield
     * sets: $this->bbcode_cache with bbcode templates needed for bbcode_bitfield
     */
    function bbcode_cache_init()
    {
        global $phpbb_root_path, $template, $user;
        if (empty($this->template_filename)) {
            $this->template_bitfield = new bitfield($user->theme['bbcode_bitfield']);
            $this->template_filename = $phpbb_root_path . 'styles/' . $user->theme['template_path'] . '/template/bbcode.html';
            if (!@file_exists($this->template_filename)) {
                if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id']) {
                    $this->template_filename = $phpbb_root_path . 'styles/' . $user->theme['template_inherit_path'] . '/template/bbcode.html';
                    if (!@file_exists($this->template_filename)) {
                        trigger_error('The file ' . $this->template_filename . ' is missing.', E_USER_ERROR);
                    }
                } else {
                    trigger_error('The file ' . $this->template_filename . ' is missing.', E_USER_ERROR);
                }
            }
        }
        $bbcode_ids = $rowset = $sql = array();
        $bitfield = new bitfield($this->bbcode_bitfield);
        $bbcodes_set = $bitfield->get_all_set();
        foreach ($bbcodes_set as $bbcode_id) {
            if (isset($this->bbcode_cache[$bbcode_id])) {
                // do not try to re-cache it if it's already in
                continue;
            }
            $bbcode_ids[] = $bbcode_id;
            if ($bbcode_id > NUM_CORE_BBCODES) {
                $sql[] = $bbcode_id;
            }
        }
        if (sizeof($sql)) {
            global $db;
            $sql = 'SELECT *
				FROM ' . BBCODES_TABLE . '
				WHERE ' . $db->sql_in_set('bbcode_id', $sql);
            $result = $db->sql_query($sql, 3600);
            while ($row = $db->sql_fetchrow($result)) {
                // To circumvent replacing newlines with <br /> for the generated html,
                // we use carriage returns here. They are later changed back to newlines
                $row['bbcode_tpl'] = str_replace("\n", "\r", $row['bbcode_tpl']);
                $row['second_pass_replace'] = str_replace("\n", "\r", $row['second_pass_replace']);
                $rowset[$row['bbcode_id']] = $row;
            }
            $db->sql_freeresult($result);
        }
        foreach ($bbcode_ids as $bbcode_id) {
            switch ($bbcode_id) {
                case 0:
                    $this->bbcode_cache[$bbcode_id] = array('str' => array('[/quote:$uid]' => $this->bbcode_tpl('quote_close', $bbcode_id)), 'preg' => array('#\\[quote(?:=&quot;(.*?)&quot;)?:$uid\\]((?!\\[quote(?:=&quot;.*?&quot;)?:$uid\\]).)?#ise' => "\$this->bbcode_second_pass_quote('\$1', '\$2')"));
                    break;
                case 1:
                    $this->bbcode_cache[$bbcode_id] = array('str' => array('[b:$uid]' => $this->bbcode_tpl('b_open', $bbcode_id), '[/b:$uid]' => $this->bbcode_tpl('b_close', $bbcode_id)));
                    break;
                case 2:
                    $this->bbcode_cache[$bbcode_id] = array('str' => array('[i:$uid]' => $this->bbcode_tpl('i_open', $bbcode_id), '[/i:$uid]' => $this->bbcode_tpl('i_close', $bbcode_id)));
                    break;
                case 3:
                    $this->bbcode_cache[$bbcode_id] = array('preg' => array('#\\[url:$uid\\]((.*?))\\[/url:$uid\\]#s' => $this->bbcode_tpl('url', $bbcode_id), '#\\[url=([^\\[]+?):$uid\\](.*?)\\[/url:$uid\\]#s' => $this->bbcode_tpl('url', $bbcode_id)));
                    break;
                case 4:
                    if ($user->optionget('viewimg')) {
                        $this->bbcode_cache[$bbcode_id] = array('preg' => array('#\\[img:$uid\\](.*?)\\[/img:$uid\\]#s' => $this->bbcode_tpl('img', $bbcode_id)));
                    } else {
                        $this->bbcode_cache[$bbcode_id] = array('preg' => array('#\\[img:$uid\\](.*?)\\[/img:$uid\\]#s' => str_replace('$2', '[ img ]', $this->bbcode_tpl('url', $bbcode_id, true))));
                    }
                    break;
                case 5:
                    $this->bbcode_cache[$bbcode_id] = array('preg' => array('#\\[size=([\\-\\+]?\\d+):$uid\\](.*?)\\[/size:$uid\\]#s' => $this->bbcode_tpl('size', $bbcode_id)));
                    break;
                case 6:
                    $this->bbcode_cache[$bbcode_id] = array('preg' => array('!\\[color=(#[0-9a-f]{3}|#[0-9a-f]{6}|[a-z\\-]+):$uid\\](.*?)\\[/color:$uid\\]!is' => $this->bbcode_tpl('color', $bbcode_id)));
                    break;
                case 7:
                    $this->bbcode_cache[$bbcode_id] = array('str' => array('[u:$uid]' => $this->bbcode_tpl('u_open', $bbcode_id), '[/u:$uid]' => $this->bbcode_tpl('u_close', $bbcode_id)));
                    break;
                case 8:
                    $this->bbcode_cache[$bbcode_id] = array('preg' => array('#\\[code(?:=([a-z]+))?:$uid\\](.*?)\\[/code:$uid\\]#ise' => "\$this->bbcode_second_pass_code('\$1', '\$2')"));
                    break;
                case 9:
                    $this->bbcode_cache[$bbcode_id] = array('preg' => array('#(\\[\\/?(list|\\*):[mou]?:?$uid\\])[\\n]{1}#' => "\$1", '#(\\[list=([^\\[]+):$uid\\])[\\n]{1}#' => "\$1", '#\\[list=([^\\[]+):$uid\\]#e' => "\$this->bbcode_list('\$1')"), 'str' => array('[list:$uid]' => $this->bbcode_tpl('ulist_open_default', $bbcode_id), '[/list:u:$uid]' => $this->bbcode_tpl('ulist_close', $bbcode_id), '[/list:o:$uid]' => $this->bbcode_tpl('olist_close', $bbcode_id), '[*:$uid]' => $this->bbcode_tpl('listitem', $bbcode_id), '[/*:$uid]' => $this->bbcode_tpl('listitem_close', $bbcode_id), '[/*:m:$uid]' => $this->bbcode_tpl('listitem_close', $bbcode_id)));
                    break;
                case 10:
                    $this->bbcode_cache[$bbcode_id] = array('preg' => array('#\\[email:$uid\\]((.*?))\\[/email:$uid\\]#is' => $this->bbcode_tpl('email', $bbcode_id), '#\\[email=([^\\[]+):$uid\\](.*?)\\[/email:$uid\\]#is' => $this->bbcode_tpl('email', $bbcode_id)));
                    break;
                case 11:
                    if ($user->optionget('viewflash')) {
                        $this->bbcode_cache[$bbcode_id] = array('preg' => array('#\\[flash=([0-9]+),([0-9]+):$uid\\](.*?)\\[/flash:$uid\\]#' => $this->bbcode_tpl('flash', $bbcode_id)));
                    } else {
                        $this->bbcode_cache[$bbcode_id] = array('preg' => array('#\\[flash=([0-9]+),([0-9]+):$uid\\](.*?)\\[/flash:$uid\\]#' => str_replace('$1', '$3', str_replace('$2', '[ flash ]', $this->bbcode_tpl('url', $bbcode_id, true)))));
                    }
                    break;
                case 12:
                    $this->bbcode_cache[$bbcode_id] = array('str' => array('[/attachment:$uid]' => $this->bbcode_tpl('inline_attachment_close', $bbcode_id)), 'preg' => array('#\\[attachment=([0-9]+):$uid\\]#' => $this->bbcode_tpl('inline_attachment_open', $bbcode_id)));
                    break;
                default:
                    if (isset($rowset[$bbcode_id])) {
                        if ($this->template_bitfield->get($bbcode_id)) {
                            // The bbcode requires a custom template to be loaded
                            if (!($bbcode_tpl = $this->bbcode_tpl($rowset[$bbcode_id]['bbcode_tag'], $bbcode_id))) {
                                // For some reason, the required template seems not to be available, use the default template
                                $bbcode_tpl = !empty($rowset[$bbcode_id]['second_pass_replace']) ? $rowset[$bbcode_id]['second_pass_replace'] : $rowset[$bbcode_id]['bbcode_tpl'];
                            } else {
                                // In order to use templates with custom bbcodes we need
                                // to replace all {VARS} to corresponding backreferences
                                // Note that backreferences are numbered from bbcode_match
                                if (preg_match_all('/\\{(URL|LOCAL_URL|EMAIL|TEXT|SIMPLETEXT|INTTEXT|IDENTIFIER|COLOR|NUMBER)[0-9]*\\}/', $rowset[$bbcode_id]['bbcode_match'], $m)) {
                                    foreach ($m[0] as $i => $tok) {
                                        $bbcode_tpl = str_replace($tok, '$' . ($i + 1), $bbcode_tpl);
                                    }
                                }
                            }
                        } else {
                            // Default template
                            $bbcode_tpl = !empty($rowset[$bbcode_id]['second_pass_replace']) ? $rowset[$bbcode_id]['second_pass_replace'] : $rowset[$bbcode_id]['bbcode_tpl'];
                        }
                        // Replace {L_*} lang strings
                        $bbcode_tpl = preg_replace('/{L_([A-Z_]+)}/e', "(!empty(\$user->lang['\$1'])) ? \$user->lang['\$1'] : ucwords(strtolower(str_replace('_', ' ', '\$1')))", $bbcode_tpl);
                        if (!empty($rowset[$bbcode_id]['second_pass_replace'])) {
                            // The custom BBCode requires second-pass pattern replacements
                            $this->bbcode_cache[$bbcode_id] = array('preg' => array($rowset[$bbcode_id]['second_pass_match'] => $bbcode_tpl));
                        } else {
                            $this->bbcode_cache[$bbcode_id] = array('str' => array($rowset[$bbcode_id]['second_pass_match'] => $bbcode_tpl));
                        }
                    } else {
                        $this->bbcode_cache[$bbcode_id] = false;
                    }
                    break;
            }
        }
    }
Esempio n. 2
0
    /**
     * Init bbcode cache
     *
     * requires: $this->bbcode_bitfield
     * sets: $this->bbcode_cache with bbcode templates needed for bbcode_bitfield
     */
    function bbcode_cache_init()
    {
        global $phpbb_root_path, $phpEx, $config, $user, $phpbb_dispatcher, $phpbb_extension_manager, $phpbb_path_helper, $phpbb_container, $phpbb_filesystem;
        if (empty($this->template_filename)) {
            $this->template_bitfield = new bitfield($user->style['bbcode_bitfield']);
            $template = new \phpbb\template\twig\twig($phpbb_container->get('path_helper'), $phpbb_container->get('config'), new \phpbb\template\context(), new \phpbb\template\twig\environment($phpbb_container->get('config'), $phpbb_container->get('filesystem'), $phpbb_container->get('path_helper'), $phpbb_container, $phpbb_container->getParameter('core.root_path') . 'cache/', $phpbb_container->get('ext.manager'), new \phpbb\template\twig\loader($phpbb_filesystem)), $phpbb_container->getParameter('core.root_path') . 'cache/', $phpbb_container->get('user'), $phpbb_container->get('template.twig.extensions.collection'), $phpbb_extension_manager);
            $template->set_style();
            $template->set_filenames(array('bbcode.html' => 'bbcode.html'));
            $this->template_filename = $template->get_source_file_for_handle('bbcode.html');
        }
        $bbcode_ids = $rowset = $sql = array();
        $bitfield = new bitfield($this->bbcode_bitfield);
        $bbcodes_set = $bitfield->get_all_set();
        foreach ($bbcodes_set as $bbcode_id) {
            if (isset($this->bbcode_cache[$bbcode_id])) {
                // do not try to re-cache it if it's already in
                continue;
            }
            $bbcode_ids[] = $bbcode_id;
            if ($bbcode_id > NUM_CORE_BBCODES) {
                $sql[] = $bbcode_id;
            }
        }
        if (sizeof($sql)) {
            global $db;
            $sql = 'SELECT *
				FROM ' . BBCODES_TABLE . '
				WHERE ' . $db->sql_in_set('bbcode_id', $sql);
            $result = $db->sql_query($sql, 3600);
            while ($row = $db->sql_fetchrow($result)) {
                // To circumvent replacing newlines with <br /> for the generated html,
                // we use carriage returns here. They are later changed back to newlines
                $row['bbcode_tpl'] = str_replace("\n", "\r", $row['bbcode_tpl']);
                $row['second_pass_replace'] = str_replace("\n", "\r", $row['second_pass_replace']);
                $rowset[$row['bbcode_id']] = $row;
            }
            $db->sql_freeresult($result);
        }
        // To perform custom second pass in extension, use $this->bbcode_second_pass_by_extension()
        // method which accepts variable number of parameters
        foreach ($bbcode_ids as $bbcode_id) {
            switch ($bbcode_id) {
                case 0:
                    $this->bbcode_cache[$bbcode_id] = array('str' => array('[/quote:$uid]' => $this->bbcode_tpl('quote_close', $bbcode_id)), 'preg' => array('#\\[quote(?:=&quot;(.*?)&quot;)?:$uid\\]((?!\\[quote(?:=&quot;.*?&quot;)?:$uid\\]).)?#ise' => "\$this->bbcode_second_pass_quote('\$1', '\$2')"));
                    break;
                case 1:
                    $this->bbcode_cache[$bbcode_id] = array('str' => array('[b:$uid]' => $this->bbcode_tpl('b_open', $bbcode_id), '[/b:$uid]' => $this->bbcode_tpl('b_close', $bbcode_id)));
                    break;
                case 2:
                    $this->bbcode_cache[$bbcode_id] = array('str' => array('[i:$uid]' => $this->bbcode_tpl('i_open', $bbcode_id), '[/i:$uid]' => $this->bbcode_tpl('i_close', $bbcode_id)));
                    break;
                case 3:
                    $this->bbcode_cache[$bbcode_id] = array('preg' => array('#\\[url:$uid\\]((.*?))\\[/url:$uid\\]#s' => $this->bbcode_tpl('url', $bbcode_id), '#\\[url=([^\\[]+?):$uid\\](.*?)\\[/url:$uid\\]#s' => $this->bbcode_tpl('url', $bbcode_id)));
                    break;
                case 4:
                    if ($user->optionget('viewimg')) {
                        $this->bbcode_cache[$bbcode_id] = array('preg' => array('#\\[img:$uid\\](.*?)\\[/img:$uid\\]#s' => $this->bbcode_tpl('img', $bbcode_id)));
                    } else {
                        $this->bbcode_cache[$bbcode_id] = array('preg' => array('#\\[img:$uid\\](.*?)\\[/img:$uid\\]#s' => str_replace('$2', '[ img ]', $this->bbcode_tpl('url', $bbcode_id, true))));
                    }
                    break;
                case 5:
                    $this->bbcode_cache[$bbcode_id] = array('preg' => array('#\\[size=([\\-\\+]?\\d+):$uid\\](.*?)\\[/size:$uid\\]#s' => $this->bbcode_tpl('size', $bbcode_id)));
                    break;
                case 6:
                    $this->bbcode_cache[$bbcode_id] = array('preg' => array('!\\[color=(#[0-9a-f]{3}|#[0-9a-f]{6}|[a-z\\-]+):$uid\\](.*?)\\[/color:$uid\\]!is' => $this->bbcode_tpl('color', $bbcode_id)));
                    break;
                case 7:
                    $this->bbcode_cache[$bbcode_id] = array('str' => array('[u:$uid]' => $this->bbcode_tpl('u_open', $bbcode_id), '[/u:$uid]' => $this->bbcode_tpl('u_close', $bbcode_id)));
                    break;
                case 8:
                    $this->bbcode_cache[$bbcode_id] = array('preg' => array('#\\[code(?:=([a-z]+))?:$uid\\](.*?)\\[/code:$uid\\]#ise' => "\$this->bbcode_second_pass_code('\$1', '\$2')"));
                    break;
                case 9:
                    $this->bbcode_cache[$bbcode_id] = array('preg' => array('#(\\[\\/?(list|\\*):[mou]?:?$uid\\])[\\n]{1}#' => "\$1", '#(\\[list=([^\\[]+):$uid\\])[\\n]{1}#' => "\$1", '#\\[list=([^\\[]+):$uid\\]#e' => "\$this->bbcode_list('\$1')"), 'str' => array('[list:$uid]' => $this->bbcode_tpl('ulist_open_default', $bbcode_id), '[/list:u:$uid]' => $this->bbcode_tpl('ulist_close', $bbcode_id), '[/list:o:$uid]' => $this->bbcode_tpl('olist_close', $bbcode_id), '[*:$uid]' => $this->bbcode_tpl('listitem', $bbcode_id), '[/*:$uid]' => $this->bbcode_tpl('listitem_close', $bbcode_id), '[/*:m:$uid]' => $this->bbcode_tpl('listitem_close', $bbcode_id)));
                    break;
                case 10:
                    $this->bbcode_cache[$bbcode_id] = array('preg' => array('#\\[email:$uid\\]((.*?))\\[/email:$uid\\]#is' => $this->bbcode_tpl('email', $bbcode_id), '#\\[email=([^\\[]+):$uid\\](.*?)\\[/email:$uid\\]#is' => $this->bbcode_tpl('email', $bbcode_id)));
                    break;
                case 11:
                    if ($user->optionget('viewflash')) {
                        $this->bbcode_cache[$bbcode_id] = array('preg' => array('#\\[flash=([0-9]+),([0-9]+):$uid\\](.*?)\\[/flash:$uid\\]#' => $this->bbcode_tpl('flash', $bbcode_id)));
                    } else {
                        $this->bbcode_cache[$bbcode_id] = array('preg' => array('#\\[flash=([0-9]+),([0-9]+):$uid\\](.*?)\\[/flash:$uid\\]#' => str_replace('$1', '$3', str_replace('$2', '[ flash ]', $this->bbcode_tpl('url', $bbcode_id, true)))));
                    }
                    break;
                case 12:
                    $this->bbcode_cache[$bbcode_id] = array('str' => array('[/attachment:$uid]' => $this->bbcode_tpl('inline_attachment_close', $bbcode_id)), 'preg' => array('#\\[attachment=([0-9]+):$uid\\]#' => $this->bbcode_tpl('inline_attachment_open', $bbcode_id)));
                    break;
                default:
                    if (isset($rowset[$bbcode_id])) {
                        if ($this->template_bitfield->get($bbcode_id)) {
                            // The bbcode requires a custom template to be loaded
                            if (!($bbcode_tpl = $this->bbcode_tpl($rowset[$bbcode_id]['bbcode_tag'], $bbcode_id))) {
                                // For some reason, the required template seems not to be available, use the default template
                                $bbcode_tpl = !empty($rowset[$bbcode_id]['second_pass_replace']) ? $rowset[$bbcode_id]['second_pass_replace'] : $rowset[$bbcode_id]['bbcode_tpl'];
                            } else {
                                // In order to use templates with custom bbcodes we need
                                // to replace all {VARS} to corresponding backreferences
                                // Note that backreferences are numbered from bbcode_match
                                if (preg_match_all('/\\{(URL|LOCAL_URL|EMAIL|TEXT|SIMPLETEXT|INTTEXT|IDENTIFIER|COLOR|NUMBER)[0-9]*\\}/', $rowset[$bbcode_id]['bbcode_match'], $m)) {
                                    foreach ($m[0] as $i => $tok) {
                                        $bbcode_tpl = str_replace($tok, '$' . ($i + 1), $bbcode_tpl);
                                    }
                                }
                            }
                        } else {
                            // Default template
                            $bbcode_tpl = !empty($rowset[$bbcode_id]['second_pass_replace']) ? $rowset[$bbcode_id]['second_pass_replace'] : $rowset[$bbcode_id]['bbcode_tpl'];
                        }
                        // Replace {L_*} lang strings
                        $bbcode_tpl = preg_replace('/{L_([A-Z0-9_]+)}/e', "(!empty(\$user->lang['\$1'])) ? \$user->lang['\$1'] : ucwords(strtolower(str_replace('_', ' ', '\$1')))", $bbcode_tpl);
                        if (!empty($rowset[$bbcode_id]['second_pass_replace'])) {
                            // The custom BBCode requires second-pass pattern replacements
                            $this->bbcode_cache[$bbcode_id] = array('preg' => array($rowset[$bbcode_id]['second_pass_match'] => $bbcode_tpl));
                        } else {
                            $this->bbcode_cache[$bbcode_id] = array('str' => array($rowset[$bbcode_id]['second_pass_match'] => $bbcode_tpl));
                        }
                    } else {
                        $this->bbcode_cache[$bbcode_id] = false;
                    }
                    break;
            }
        }
        $bbcode_cache = $this->bbcode_cache;
        $bbcode_bitfield = $this->bbcode_bitfield;
        $bbcode_uid = $this->bbcode_uid;
        /**
         * Use this event to modify the bbcode_cache
         *
         * @event core.bbcode_cache_init_end
         * @var	array	bbcode_cache		The array of cached search and replace patterns of bbcodes
         * @var	string	bbcode_bitfield		The bbcode bitfield
         * @var	string	bbcode_uid			The bbcode uid
         * @since 3.1.3-RC1
         */
        $vars = array('bbcode_cache', 'bbcode_bitfield', 'bbcode_uid');
        extract($phpbb_dispatcher->trigger_event('core.bbcode_cache_init_end', compact($vars)));
        $this->bbcode_cache = $bbcode_cache;
        $this->bbcode_bitfield = $bbcode_bitfield;
        $this->bbcode_uid = $bbcode_uid;
    }
/**
* BBCode-safe truncating of text
*
* From: http://www.phpbb.com/community/viewtopic.php?f=71&t=670335
* Slightly modified to trim at either the first found end line or space
*
* @param string $text Text containing BBCode tags to be truncated
* @param string $uid BBCode uid
* @param int $max_length Text length limit
* @param string $bitfield BBCode bitfield (optional)
* @param bool $enable_bbcode Whether BBCode is enabled (true by default)
* @return string
*/
function trim_text($text, $uid, $max_length, $bitfield = '', $enable_bbcode = true)
{
    // If there is any custom BBCode that can have space in its argument, turn this on,
    // but else I suggest turning this off as it adds one additional (cache) SQL query
    $check_custom_bbcodes = true;
    if ($enable_bbcode && $check_custom_bbcodes) {
        global $db;
        static $custom_bbcodes = array();
        // Get all custom bbcodes
        if (empty($custom_bbcodes)) {
            $sql = 'SELECT bbcode_id, bbcode_tag
			FROM ' . BBCODES_TABLE;
            $result = $db->sql_query($sql, 108000);
            while ($row = $db->sql_fetchrow($result)) {
                // There can be problems only with tags having an argument
                if (substr($row['bbcode_tag'], -1, 1) == '=') {
                    $custom_bbcodes[$row['bbcode_id']] = array('[' . $row['bbcode_tag'], ':' . $uid . ']');
                }
            }
            $db->sql_freeresult($result);
        }
    }
    // First truncate the text
    if (utf8_strlen($text) > $max_length) {
        $next_space = strpos(substr($text, $max_length), ' ');
        $next_el = strpos(substr($text, $max_length), "\n");
        if ($next_space !== false) {
            if ($next_el !== false) {
                $max_length = $next_space < $next_el ? $next_space + $max_length : $next_el + $max_length;
            } else {
                $max_length = $next_space + $max_length;
            }
        } else {
            if ($next_el !== false) {
                $max_length = $next_el + $max_length;
            } else {
                $max_length = utf8_strlen($text);
            }
        }
        $text = utf8_substr($text, 0, $max_length);
        // Append three dots indicating that this is not the real end of the text
        $text .= '...';
        if (!$enable_bbcode) {
            return $text;
        }
    } else {
        return $text;
    }
    // Some tags may contain spaces inside the tags themselves.
    // If there is any tag that had been started but not ended
    // cut the string off before it begins and add three dots
    // to the end of the text again as this has been just cut off too.
    $unsafe_tags = array(array('<', '>'), array('[quote=&quot;', "&quot;:{$uid}]"));
    // If bitfield is given only check for tags that are surely existing in the text
    if (!empty($bitfield)) {
        // Get all used tags
        $bitfield = new bitfield($bitfield);
        $bbcodes_set = $bitfield->get_all_set();
        // Add custom BBCodes having a parameter and being used
        // to the array of potential tags that can be cut apart.
        foreach ($custom_bbcodes as $bbcode_id => $bbcode_name) {
            if (in_array($bbcode_id, $bbcodes_set)) {
                $unsafe_tags[] = $bbcode_name;
            }
        }
    } else {
        $unsafe_tags = array_merge($unsafe_tags, $custom_bbcodes);
    }
    foreach ($unsafe_tags as $tag) {
        if (($start_pos = strrpos($text, $tag[0])) > strrpos($text, $tag[1])) {
            $text = substr($text, 0, $start_pos) . ' ...';
        }
    }
    // Get all of the BBCodes the text contains.
    // If it does not contain any than just skip this step.
    // Preg expression is borrowed from strip_bbcode()
    if (preg_match_all("#\\[(\\/?)([a-z0-9_\\*\\+\\-]+)(?:=(&quot;.*&quot;|[^\\]]*))?(?::[a-z])?(?:\\:{$uid})\\]#", $text, $matches, PREG_PATTERN_ORDER) != 0) {
        $open_tags = array();
        for ($i = 0, $size = sizeof($matches[0]); $i < $size; ++$i) {
            $bbcode_name =& $matches[2][$i];
            $opening = $matches[1][$i] == '/' ? false : true;
            // If a new BBCode is opened add it to the array of open BBCodes
            if ($opening) {
                $open_tags[] = array('name' => $bbcode_name, 'plus' => $opening && $bbcode_name == 'list' && !empty($matches[3][$i]) ? ':o' : '');
            } else {
                array_pop($open_tags);
            }
        }
        // Sort open BBCode tags so the most recently opened will be the first (because it has to be closed first)
        krsort($open_tags);
        // Close remaining open BBCode tags
        foreach ($open_tags as $tag) {
            $text .= '[/' . $tag['name'] . $tag['plus'] . ':' . $uid . ']';
        }
    }
    return $text;
}
Esempio n. 4
0
    /**
     * Init bbcode cache
     *
     * requires: $this->bbcode_bitfield
     * sets: $this->bbcode_cache with bbcode templates needed for bbcode_bitfield
     */
    function bbcode_cache_init()
    {
        global $_CLASS;
        if (empty($this->template_filename)) {
            $style = 'primary';
            $this->template_bitfield = @$_CLASS['core_user']->img['bbcode_bitfield'];
            $this->template_filename = file_exists(SITE_FILE_ROOT . 'themes/' . $_CLASS['core_display']->theme . '/template/modules/forums/bbcode.html') ? SITE_FILE_ROOT . 'themes/' . $_CLASS['core_display']->theme . '/template/modules/forums/bbcode.html' : SITE_FILE_ROOT . 'includes/templates/modules/forums/bbcode.html';
            if (!@file_exists($this->template_filename)) {
                trigger_error('The file ' . $this->template_filename . ' is missing.', E_USER_ERROR);
            }
        }
        $bbcode_ids = $rowset = $sql = array();
        $bitfield = new bitfield($this->bbcode_bitfield);
        $bbcodes_set = $bitfield->get_all_set();
        foreach ($bbcodes_set as $bbcode_id) {
            if (isset($this->bbcode_cache[$bbcode_id])) {
                // do not try to re-cache it if it's already in
                continue;
            }
            $bbcode_ids[] = $bbcode_id;
            if ($bbcode_id > NUM_CORE_BBCODES) {
                $sql[] = $bbcode_id;
            }
        }
        if (!empty($sql)) {
            $sql = 'SELECT *
				FROM ' . BBCODES_TABLE . '
				WHERE bbcode_id IN (' . implode(', ', $sql) . ')';
            $result = $_CLASS['core_db']->query($sql);
            while ($row = $_CLASS['core_db']->fetch_row_assoc($result)) {
                $rowset[$row['bbcode_id']] = $row;
            }
            $_CLASS['core_db']->free_result($result);
        }
        foreach ($bbcode_ids as $bbcode_id) {
            switch ($bbcode_id) {
                case 0:
                    $this->bbcode_cache[$bbcode_id] = array('str' => array('[/quote:$uid]' => $this->bbcode_tpl('quote_close', $bbcode_id)), 'preg' => array('#\\[quote(?:=&quot;(.*?)&quot;)?:$uid\\](.)#ise' => "\$this->bbcode_second_pass_quote('\$1', '\$2')"));
                    break;
                case 1:
                    $this->bbcode_cache[$bbcode_id] = array('str' => array('[b:$uid]' => $this->bbcode_tpl('b_open', $bbcode_id), '[/b:$uid]' => $this->bbcode_tpl('b_close', $bbcode_id)));
                    break;
                case 2:
                    $this->bbcode_cache[$bbcode_id] = array('str' => array('[i:$uid]' => $this->bbcode_tpl('i_open', $bbcode_id), '[/i:$uid]' => $this->bbcode_tpl('i_close', $bbcode_id)));
                    break;
                case 3:
                    $this->bbcode_cache[$bbcode_id] = array('preg' => array('#\\[url:$uid\\]((.*?))\\[/url:$uid\\]#s' => $this->bbcode_tpl('url', $bbcode_id), '#\\[url=([^\\[]+?):$uid\\](.*?)\\[/url:$uid\\]#s' => $this->bbcode_tpl('url', $bbcode_id)));
                    break;
                case 4:
                    if ($_CLASS['core_user']->user_data_get('viewimg')) {
                        $this->bbcode_cache[$bbcode_id] = array('preg' => array('#\\[img:$uid\\](.*?)\\[/img:$uid\\]#s' => $this->bbcode_tpl('img', $bbcode_id)));
                    } else {
                        $this->bbcode_cache[$bbcode_id] = array('preg' => array('#\\[img:$uid\\](.*?)\\[/img:$uid\\]#s' => str_replace('$2', '[ img ]', $this->bbcode_tpl('url', $bbcode_id))));
                    }
                    break;
                case 5:
                    $this->bbcode_cache[$bbcode_id] = array('preg' => array('#\\[size=([\\-\\+]?[1-2]?[0-9]):$uid\\](.*?)\\[/size:$uid\\]#s' => $this->bbcode_tpl('size', $bbcode_id)));
                    break;
                case 6:
                    $this->bbcode_cache[$bbcode_id] = array('preg' => array('!\\[color=(#[0-9a-fA-F]{6}|[a-z\\-]+):$uid\\](.*?)\\[/color:$uid\\]!s' => $this->bbcode_tpl('color', $bbcode_id)));
                    break;
                case 7:
                    $this->bbcode_cache[$bbcode_id] = array('str' => array('[u:$uid]' => $this->bbcode_tpl('u_open', $bbcode_id), '[/u:$uid]' => $this->bbcode_tpl('u_close', $bbcode_id)));
                    break;
                case 8:
                    $this->bbcode_cache[$bbcode_id] = array('preg' => array('#\\[code(?:=([a-z]+))?:$uid\\](.*?)\\[/code:$uid\\]#ise' => "\$this->bbcode_second_pass_code('\$1', '\$2')"));
                    break;
                case 9:
                    $this->bbcode_cache[$bbcode_id] = array('preg' => array('#(\\[\\/?(list|\\*):[mou]?:?$uid\\])[\\n]{1}#' => "\$1", '#(\\[list=([^\\[]+):$uid\\])[\\n]{1}#' => "\$1", '#\\[list=([^\\[]+):$uid\\]#e' => "\$this->bbcode_list('\$1')"), 'str' => array('[list:$uid]' => $this->bbcode_tpl('ulist_open_default', $bbcode_id), '[/list:u:$uid]' => $this->bbcode_tpl('ulist_close', $bbcode_id), '[/list:o:$uid]' => $this->bbcode_tpl('olist_close', $bbcode_id), '[*:$uid]' => $this->bbcode_tpl('listitem', $bbcode_id), '[/*:$uid]' => $this->bbcode_tpl('listitem_close', $bbcode_id), '[/*:m:$uid]' => $this->bbcode_tpl('listitem_close', $bbcode_id)));
                    break;
                case 10:
                    $this->bbcode_cache[$bbcode_id] = array('preg' => array('#\\[email:$uid\\]((.*?))\\[/email:$uid\\]#is' => $this->bbcode_tpl('email', $bbcode_id), '#\\[email=([^\\[]+):$uid\\](.*?)\\[/email:$uid\\]#is' => $this->bbcode_tpl('email', $bbcode_id)));
                    break;
                case 11:
                    if ($_CLASS['core_user']->user_data_get('viewflash')) {
                        $this->bbcode_cache[$bbcode_id] = array('preg' => array('#\\[flash=([0-9]+),([0-9]+):$uid\\](.*?)\\[/flash:$uid\\]#' => $this->bbcode_tpl('flash', $bbcode_id)));
                    } else {
                        $this->bbcode_cache[$bbcode_id] = array('preg' => array('#\\[flash=([0-9]+),([0-9]+):$uid\\](.*?)\\[/flash:$uid\\]#' => str_replace('$1', '$3', str_replace('$2', '[ flash ]', $this->bbcode_tpl('url', $bbcode_id)))));
                    }
                    break;
                case 12:
                    $this->bbcode_cache[$bbcode_id] = array('str' => array('[/attachment:$uid]' => $this->bbcode_tpl('inline_attachment_close', $bbcode_id)), 'preg' => array('#\\[attachment=([0-9]+):$uid\\]#' => $this->bbcode_tpl('inline_attachment_open', $bbcode_id)));
                    break;
                default:
                    if (!isset($template_bitfield)) {
                        $template_bitfield = new bitfield($this->template_bitfield);
                    }
                    if (isset($rowset[$bbcode_id])) {
                        if ($template_bitfield->get($bbcode_id)) {
                            // The bbcode requires a custom template to be loaded
                            if (!($bbcode_tpl = $this->bbcode_tpl($rowset[$bbcode_id]['bbcode_tag'], $bbcode_id))) {
                                // For some reason, the required template seems not to be available, use the default template
                                $bbcode_tpl = !empty($rowset[$bbcode_id]['second_pass_replace']) ? $rowset[$bbcode_id]['second_pass_replace'] : $rowset[$bbcode_id]['bbcode_tpl'];
                            } else {
                                // In order to use templates with custom bbcodes we need
                                // to replace all {VARS} to corresponding backreferences
                                // Note that backreferences are numbered from bbcode_match
                                if (preg_match_all('/\\{(URL|EMAIL|TEXT|COLOR|NUMBER)[0-9]*\\}/', $rowset[$bbcode_id]['bbcode_match'], $m)) {
                                    foreach ($m[0] as $i => $tok) {
                                        $bbcode_tpl = str_replace($tok, '$' . ($i + 1), $bbcode_tpl);
                                    }
                                }
                            }
                        } else {
                            // Default template
                            $bbcode_tpl = !empty($rowset[$bbcode_id]['second_pass_replace']) ? $rowset[$bbcode_id]['second_pass_replace'] : $rowset[$bbcode_id]['bbcode_tpl'];
                        }
                        // Replace {L_*} lang strings
                        $bbcode_tpl = preg_replace('/{L_([A-Z_]+)}/e', "\$_CLASS['core_user']->get_lang('\$1')", $bbcode_tpl);
                        if (!empty($rowset[$bbcode_id]['second_pass_replace'])) {
                            // The custom BBCode requires second-pass pattern replacements
                            $this->bbcode_cache[$bbcode_id] = array('preg' => array($rowset[$bbcode_id]['second_pass_match'] => $bbcode_tpl));
                        } else {
                            $this->bbcode_cache[$bbcode_id] = array('str' => array($rowset[$bbcode_id]['second_pass_match'] => $bbcode_tpl));
                        }
                    } else {
                        $this->bbcode_cache[$bbcode_id] = false;
                    }
                    break;
            }
        }
    }