Иконка ресурса

The Mini Parser (only for developers) 1.3.5

Нет прав для скачивания
Пример использования:
PHP:
<?php
class Sedo_TinyQuattro_BbCode_Formatter_Base extends XFCP_Sedo_TinyQuattro_BbCode_Formatter_Base
{
    /**
     * Custom MCE tags name
     */
    protected $_mceBackgroundColorTagName = 'bcolor';
    protected $_mceTableTagName = 'xtable';
    protected $_mceSubTagName = 'sub';
    protected $_mceSupTagName = 'sup';
    protected $_mceHrTagName = 'hr';
    protected $_mceAnchorTagName = 'anchor';
    protected $_mceFormatTagName = 'format';
    /**
     * Table default skin
     */
    protected $_mceTableDefaultSkin = 'skin1';
    /**
     * Xen Options for MCE Table
     */
    protected $_xenOptionsMceTable;
   
    /**
     * Extend tags
     */
    public function getTags()
    {
        $parentTags = parent::getTags();
       
        if(is_array($parentTags))
        {
            if(Sedo_TinyQuattro_Helper_Quattro::canUseQuattroBbCode('bcolor'))
            {
                $bcTag = Sedo_TinyQuattro_Helper_BbCodes::getQuattroBbCodeTagName('bcolor');
                $this->_mceBackgroundColorTagName = $bcTag;
               
                $parentTags += array(
                    $bcTag => array(
                        'hasOption' => true,
                        'optionRegex' => '/^(rgb\(\s*\d+%?\s*,\s*\d+%?\s*,\s*\d+%?\s*\)|#[a-f0-9]{6}|#[a-f0-9]{3}|[a-z]+)$/i',
                        'replace' => array('<span style="background-color: %s">', '</span>')
                    )
                );           
            }
            if(Sedo_TinyQuattro_Helper_Quattro::canUseQuattroBbCode('justify'))
            {
                $parentTags += array(
                    'justify' => array(
                        'hasOption' => false,
                        'callback' => array($this, 'renderTagAlign'),
                        'trimLeadingLinesAfter' => 1
                    )
                );           
            }
            if(Sedo_TinyQuattro_Helper_Quattro::canUseQuattroBbCode('sub'))
            {
                $subTag = Sedo_TinyQuattro_Helper_BbCodes::getQuattroBbCodeTagName('sub');
                $this->_mceSubTagName = $subTag;
               
                $parentTags += array(
                    $subTag => array(
                        'hasOption' => false,
                        'replace' => array('<sub class="xenmce" style="vertical-align:sub">', '</sub>')
                    )
                );           
            }
            if(Sedo_TinyQuattro_Helper_Quattro::canUseQuattroBbCode('sup'))
            {
                $supTag = Sedo_TinyQuattro_Helper_BbCodes::getQuattroBbCodeTagName('sup');
                $this->_mceSupTagName = $supTag;
               
                $parentTags += array(
                    $supTag => array(
                        'hasOption' => false,
                        'replace' => array('<sup class="xenmce" style="vertical-align:super">', '</sup>')
                    )
                );           
            }
           
            if(Sedo_TinyQuattro_Helper_Quattro::canUseQuattroBbCode('xtable'))
            {
                $this->_preloadMceTemplates[] = 'quattro_bbcode_xtable';
               
                $tableTag = Sedo_TinyQuattro_Helper_BbCodes::getQuattroBbCodeTagName('xtable');
                $this->_mceTableTagName = $tableTag;
                $this->_mceTableDefaultSkin = XenForo_Template_Helper_Core::styleProperty('quattro_table_skin_default');
                $parentTags += array(
                    $tableTag => array(
                        'callback' => array($this, 'renderTagSedoXtable'),
                        'stopLineBreakConversion' => true,
                        'trimLeadingLinesAfter' => 2
                    )
                );
               
                $this->_xenOptionsMceTable = Sedo_TinyQuattro_Helper_BbCodes::getMceTableXenOptions();
            }
            if(Sedo_TinyQuattro_Helper_Quattro::canUseQuattroBbCode('hr'))
            {
                $hrTag = Sedo_TinyQuattro_Helper_BbCodes::getQuattroBbCodeTagName('hr');
                $this->_mceHrTagName = $hrTag;
               
                $parentTags += array(
                    $hrTag => array(
                        'callback' => array($this, 'renderTagSedoHr'),
                        'trimLeadingLinesAfter' => 1
                    )
                );           
            }
            if(Sedo_TinyQuattro_Helper_Quattro::canUseQuattroBbCode('anchor'))
            {
                $anchorTag = Sedo_TinyQuattro_Helper_BbCodes::getQuattroBbCodeTagName('anchor');
                $this->_mceAnchorTagName = $anchorTag;
               
                $parentTags += array(
                    $anchorTag => array(
                        'callback' => array($this, 'renderTagSedoAnchor')
                    )
                );           
            }
            if(Sedo_TinyQuattro_Helper_Quattro::canUseQuattroBbCode('format'))
            {
                $formatTag = Sedo_TinyQuattro_Helper_BbCodes::getQuattroBbCodeTagName('format');
                $this->_mceFormatTagName = $formatTag;
               
                $parentTags += array(
                    $formatTag => array(
                        'callback' => array($this, 'renderTagSedoFormat')
                    )
                );           
            }                                   
        }
       
        return $parentTags;
    }
    /**
     * Extend preloaded templates
     */
    protected $_preloadMceTemplates = array();
    public function preLoadTemplates(XenForo_View $view)
    {
         //Preload Table Template (use for css)
         if($this->_view && !empty($this->_preloadMceTemplates))
        {
            foreach($this->_preloadMceTemplates as $templateName)
            {
                $this->_view->preLoadTemplate($templateName);
            }
        }
        return parent::preLoadTemplates($view);
    }
    /**
     * Cache emulation options
     */
    protected $emulateAllWhiteSpace;
    protected $emulateTabs;
   
    /**
     * Extend XenForo filterString function to add an option to recreate tabs in html
     */
    public function filterString($string, array $rendererStates)
    {
        $string = parent::filterString($string, $rendererStates);
        if(!empty($rendererStates['stopWhiteSpaceEmulation']))
        {
            return $string;
        }
        $insideBbCode = !empty($rendererStates['tagDataStack']);
        if (!isset($this->emulateAllWhiteSpace))
        {
            $xenOptions = XenForo_Application::get('options');
            $emulateAllWhiteSpace = $xenOptions->quattro_emulate_allwhitespace_html;
            $emulateTabs = $xenOptions->quattro_emulate_tabs_html;
           
            $this->emulateAllWhiteSpace = (empty($emulateAllWhiteSpace) || $emulateAllWhiteSpace == 'no' || ($insideBbCode && $emulateAllWhiteSpace == 'limited')) ? false : true;
            $this->emulateTabs = (empty($emulateTabs) || $emulateTabs == 'no' || ($insideBbCode && $emulateTabs == 'limited')) ? false : true;
        }
        if($this->emulateAllWhiteSpace)
        {
            $string = $this->emulateAllWhiteSpace($string);
        }
        if($this->emulateTabs)
        {
            $string = $this->emulateWhiteSpaceTabs($string);
        }
        return $string;       
    }
    public function emulateAllWhiteSpace($string)
    {
        return preg_replace_callback(
            //The below regew will match whitespaces (start from 2 and exclude the last one of a line) + exclude match if a ending html tag is detected
            '#[ ]{2,}+(?<! $)(?![^<]*?>)#',
            array($this, '_emulateAllWhiteSpaceRegexCallback'),
            $string
        );
    }
   
    protected function _emulateAllWhiteSpaceRegexCallback($matches)
    {
        $breaksX = substr_count($matches[0], " ");
        $breakPattern = '&nbsp;'; //other possible UTF8 solutions = http://www.cs.tut.fi/~jkorpela/chars/spaces.html
        $breakOutput = str_repeat($breakPattern, $breaksX);
                   
        return "{$breakOutput}";
    }
    public function emulateWhiteSpaceTabs($string)
    {
        return preg_replace_callback(
            '#[\t]+#',
            array($this, '_emulateWhiteSpaceTabsRegexCallback'),
            $string
        );
    }
   
    protected function _emulateWhiteSpaceTabsRegexCallback($matches)
    {
        $breaksX = substr_count($matches[0], "\t");
        $breakPattern = '    ';
        $breakOutput = str_repeat($breakPattern, $breaksX);
                   
        return "<span style='white-space:pre'>{$breakOutput}</span>";   
    }
   
    /**
     * Extend XenForo Tag align to add jystify option
     */
    public function renderTagAlign(array $tag, array $rendererStates)
    {
        $parentOuput = parent::renderTagAlign($tag, $rendererStates);
        if(strtolower($tag['tag']) == 'justify' && Sedo_TinyQuattro_Helper_Quattro::canUseQuattroBbCode('justify'))
        {
            $text = $this->renderSubTree($tag['children'], $rendererStates);
            $invisibleSpace = $this->_endsInBlockTag($text) ? '' : '&#8203;';
            return $this->_wrapInHtml('<div style="text-align: ' . $tag['tag'] . '">', $invisibleSpace. '</div>', $text);
        }
       
        return $parentOuput;
    }
    /**
     * Mce Horizontal rule tag
     */
    public function renderTagSedoHr(array $tag, array $rendererStates)
    {
        return "<hr />";
    }
    /**
     * Mce Anchor tag
     */
    protected $_quattroRequestPaths;
    protected $_quattroJsonResponse = null;
   
    public function renderTagSedoAnchor(array $tag, array $rendererStates)
    {
        $content = htmlspecialchars(trim($this->renderSubTree($tag['children'], $rendererStates)));
        $anchorPrefix = 'message-anchor';
        /*Anchor point*/
        if(empty($tag['option']))
        {
            $anchor = strtolower($content);
            return "<a id='{$anchorPrefix}-{$anchor}' class='quattro_anchor'></a>";
        }
        /*Anchor link*/
        $anchor = strtolower(htmlspecialchars(trim($tag['option'])));
       
        if($anchor[0] != '#')
        {
            $anchor = "#{$anchorPrefix}-{$anchor}";
        }
        else
        {
            $anchor = "#{$anchorPrefix}-".substr($anchor, 1);
        }
        //Json Response check
        if($this->_quattroJsonResponse === null)
        {
            $fc = XenForo_Application::get('fc');
            $route = $fc->route();
            $this->_quattroJsonResponse = ($route->getResponseType() == 'json');
        }
        $jsonResponse = $this->_quattroJsonResponse;
       
        //Request Paths Management
        if(!$this->_quattroRequestPaths)
        {
            $this->_quattroRequestPaths = XenForo_Application::get('requestPaths');
        }
   
        $requestPaths = $this->_quattroRequestPaths;
        if($jsonResponse)
        {
            //If the response type is json, the request paths will be the one from the json view, so try to get the data from the previous html response
            $sessionCache = XenForo_Application::getSession()->get('sedoQuattro');
           
            if(!empty($sessionCache['noJsonRequestPaths']))
            {
                $requestPaths = $sessionCache['noJsonRequestPaths'];
            }
        }
        //Get url & text
        $url = $requestPaths['fullUri'] . "{$anchor}";
        $text = $content;
        /*Copy of XenForo url tag handler without the proxy feature*/
        $url = $this->_getValidUrl($url);
        if (!$url)
        {
            return $text;
        }
        else
        {
            list($class, $target, $type) = XenForo_Helper_String::getLinkClassTarget($url);
            if ($type == 'internal')
            {
                $noFollow = '';
            }
            else
            {
                $noFollow = (empty($rendererStates['noFollowDefault']) ? '' : ' rel="nofollow"');
            }
            $href = XenForo_Helper_String::censorString($url);
            $class = $class ? " class=\"$class\"" : '';
            $target = $target ? " target=\"$target\"" : '';
            return $this->_wrapInHtml(
                '<a href="' . htmlspecialchars($href) . '"' . $target . $class . $noFollow . '>',
                '</a>',
                $text
            );
        }
    }
    /**
     * Mce Format tag
     */
    public function renderTagSedoFormat(array $tag, array $rendererStates)
    {
        $content = $this->renderSubTree($tag['children'], $rendererStates);
        $option = trim($tag['option']);
        $headings = array('h1', 'h2', 'h3', 'h4', 'h5', 'h6');
        $customSpan = array('cust1', 'cust2', 'cust3');
        $isHeading = in_array($option, $headings);
       
        if(empty($tag['option']))
        {
            return $content;
        }
       
        //HEADING FORMAT
        if($isHeading && XenForo_Template_Helper_Core::styleProperty("quattro_sf_{$option}_text"))
        {
            return "<{$option} class='quattro_sf {$option}'>{$content}</{$option}>";
        }
       
        //CUSTOM FORMAT
        $customKey = array_search($option, $customSpan);
       
        if($customKey === false)
        {
            return $content;
        }
        $customKeyIndex = $customKey+1;
        if(XenForo_Template_Helper_Core::styleProperty("quattro_sf_custom{$customKeyIndex}_text"))
        {
            return "<span class='quattro_sf {$option}'>{$content}</span>";
        }
        return $content;
    }
    //@extend renderTag function to modify trimLeadingLinesAfter value
    public function renderTag(array $element, array $rendererStates, &$trimLeadingLines)
    {
        if(!isset($element['tag']))
        {
            return parent::renderTag($element, $rendererStates, $trimLeadingLines);
        }
       
        $tagName = $element['tag'];
        if(!empty($rendererStates['stopLineBreakConversion']) && !empty($this->forceLineBreakConversionExceptForThoseTags) && !in_array($tagName, ($this->forceLineBreakConversionExceptForThoseTags)))
        {
            $rendererStates['stopLineBreakConversion'] = false;
        }
       
        if($tagName  == $this->_mceFormatTagName)
        {
            $headings = array('h1', 'h2', 'h3', 'h4', 'h5', 'h6');
            $option = trim($element['option']);
           
            if(in_array($option, $headings))
            {
                $this->_tags[$tagName]['trimLeadingLinesAfter'] = 1;           
            }
            else
            {
                $this->_tags[$tagName]['trimLeadingLinesAfter'] = 0;
            }
        }
        return $this->_metaRenderTag(parent::renderTag($element, $rendererStates, $trimLeadingLines),  $element, $rendererStates);
    }
    /**
    *    Management of rendered tags by XenForo formatter
    *    Detect them, replace them with a placeholder, parse the mini parser tags, get back the XenForo formatted tags
    *    Purpose: avoid the nl2br php function breaks the rendered html of xen tags
    **/
    protected $_rTag = 1;
    protected $_tagHolders = array();
    protected $_mceRtagSkipForTags = array('b' => 0, 'i'=> 0, 'u'=> 0, 's'=> 0);
    public $forceLineBreakConversionExceptForThoseTags = array();   
    protected function _metaRenderTag($output,  $element, $rendererStates)
    {
        if(!empty($rendererStates['parsedTagInfoWrapper']) && empty($rendererStates['bbmPreCacheInit']))
        {
            $xenTag = strtolower($element['tag']);
       
            if(!empty($this->_mceRtagSkipForTags[$xenTag]))
            {
                return $output;
            }
            $rTag = 'rTag_'.$this->_rTag;
            $this->_rTag++;
            return "[$rTag]".$output."[/$rTag]";
        }
        else
        {
            return $output;
        }   
    }
    public function xenTagsHolderisation($content)
    {
        return preg_replace_callback('#\[(rTag_(\d{1,3}))](.*?)\[/\1](?:\r\n)?#s', array($this, '_xenTagsHolderisationRegex'), $content);
    }
   
    protected function _xenTagsHolderisationRegex($matches)
    {
        $id = $matches[2];
        $content = $matches[3];
       
        $content = $this->xenTagsHolderisation($content);
        $this->_tagHolders[$id] = $content;
                   
        return "[xentagHolders_$id/]";
    }
   
    public function unXenTagsHolderisation($content, $reset = true)
    {
        foreach($this->_tagHolders as $id => $replacement)
        {
            if(strpos($content, "[xentagHolders_$id/]") !== false)
            {
                unset($this->_tagHolders[$id]);               
                $replacement = $this->unXenTagsHolderisation($replacement, false);
                $content = str_replace("[xentagHolders_$id/]", $replacement, $content);
            }
        }
   
        if($reset)
        {
            $this->resetRtags();
        }
   
        return $content;
    }
   
    public function resetRtags()
    {
        $this->_tagHolders = array();
        $this->_rTag = 1;
        $this->forceLineBreakConversionExceptForThoseTags = array();
    }
    /**
     * Mce Table Master Bb Code Renderer
     */
    public function renderTagSedoXtable(array $tag, array $rendererStates)
    {
        $tagName = $tag['tag'];
        $tagOptions = $tag['option'];
       
        $tableOptionsChecker = new Sedo_TinyQuattro_Helper_TableOptions($tagName, $tagOptions, $this->_xenOptionsMceTable);
        list($attributes, $css, $extraClass) = $tableOptionsChecker->getValidOptions();
        $this->forceLineBreakConversionExceptForThoseTags = array($tagName);
        $rendererStates['parsedTagInfoWrapper'] = true;
        $content = $this->renderSubTree($tag['children'], $rendererStates);
        $content = $this->xenTagsHolderisation($content);
        $slaveTags = array(
            'thead' => array(
                'callback'  => array($this, 'renderTagSedoXtableSlaveTags'),
                'allowedParents' => array($tagName),
                'allowedChildren' => array('tr'),
                'disableTextNodes' => 'inAndAfter'
            ),
            'tbody' => array(
                'callback'  => array($this, 'renderTagSedoXtableSlaveTags'),
                'allowedParents' => array($tagName),
                'allowedChildren' => array('tr'),
                'disableTextNodes' => 'inAndAfter'
            ),
            'tfoot' => array(
                'callback'  => array($this, 'renderTagSedoXtableSlaveTags'),
                'allowedParents' => array($tagName),
                'allowedChildren' => array('tr'),
                'disableTextNodes' => 'inAndAfter'
            ),
            'colgroup' => array(
                'callback'  => array($this, 'renderTagSedoXtableSlaveTags'),
                'allowedParents' => array($tagName),
                'allowedChildren' => array('col'),
                'disableTextNodes' => 'insideContent'
            ),
            'caption' => array(
                'callback'  => array($this, 'renderTagSedoXtableSlaveTags'),
                'allowedParents' => array($tagName),
                'allowedChildren' => 'none'
            ),
            'tr' => array(
                'callback'  => array($this, 'renderTagSedoXtableSlaveTags'),
                'allowedParents' => array($tagName, 'thead', 'tbody', 'tfoot'),
                'allowedChildren' => array('td', 'th'),
                'disableTextNodes' => 'insideContent'
            ),
            'col' => array(
                'callback'  => array($this, 'renderTagSedoXtableSlaveTags'),
                'allowedParents' => array('colgroup'),
                'allowedChildren' => 'none'
            ),
            'td' => array(
                'callback'  => array($this, 'renderTagSedoXtableSlaveTags'),
                'allowedParents' => array('tr'),
                'allowedChildren' => 'none',
                'disableTextNodes' => 'afterClosing'
            ),
            'th' => array(
                'callback'  => array($this, 'renderTagSedoXtableSlaveTags'),
                'allowedParents' => array('tr'),
                'allowedChildren' => 'none',
                'disableTextNodes' => 'afterClosing'
            )
        );
       
        /***
            MiniParser options
             Don't use the XenForo formatter here neither...
        **/
        $miniParserOptions = array(
            'htmlspecialcharsForContent' => false,
            'breakToBr' => true,
            'renderStates' => array(),
            //'externalFormatter' => array($this, 'renderTree')
        );
        $miniParser =  new Sedo_TinyQuattro_Helper_MiniParser($content, $slaveTags, $tag, $miniParserOptions);
        $content = $miniParser->render();
        $content = $this->unXenTagsHolderisation($content);
        if(!preg_match('#skin\d{1,2}#', $extraClass, $match))
        {
            $extraClass .= " $this->_mceTableDefaultSkin";
        }
        $formattedCss = (empty($css)) ? '' : "style='{$css}'";
        $fallback = "<table class='quattro_table {$extraClass}' {$attributes} {$formattedCss}>{$content}</table>";
        if ($this->_view)
        {
            //Create and render template
            $template =
                $this->_view->createTemplateObject(
                    'quattro_bbcode_xtable',
                    array(
                        'tagContent' => $content,
                        'css' => $css,
                        'attributes' => $attributes,
                        'extraClass' => $extraClass
                    )
                );
           
            return $template->render();
        }
        return $fallback;
    }
    /**
     * Mce Table Slave Tags Renderer
     */
    public function renderTagSedoXtableSlaveTags(array $tag, array $rendererStates, $parentClass)
    {
        $tagName = $tag['tag'];
        $tagOptions = $tag['option'];
        $tableOptionsChecker = new Sedo_TinyQuattro_Helper_TableOptions($tagName, $tagOptions, $this->_xenOptionsMceTable);
        list($attributes, $css, $extraClass) = $tableOptionsChecker->getValidOptions();
       
        $formattedClass = (empty($extraClass)) ? '' : "class='{$extraClass}'";
        $formattedCss = (empty($css)) ? '' : "style='{$css}'";
        $openingHtmlTag = "<{$tagName} {$formattedClass} {$attributes} {$formattedCss}>";
        $closingHtmlTag = "</$tagName>";
        /***
            We're using the formatter of the Miniparser - the "wrapInHtml" function is here public
        **/
        $content = $parentClass->renderSubTree($tag['children'], $rendererStates);
        if(empty($content) && $content !== 0)
        {
            //Should not be needed with recent browsers but not sure with old ie
            $content="&nbsp;";           
        }
           
        return $parentClass->wrapInHtml($openingHtmlTag, $closingHtmlTag, $content);
    }
}
//Zend_Debug::dump($parent);

Ещё один пример использования:
PHP:
<?php
class BBM_Protection_Helper_ContentProtection
{
    /**************************************
        This class is used to protect the content of the Bbm BbCodes with view permissions.
        The general idea is to listen the controller and protect these Bb Codes
        > Two XenForo listerners are extended here: the preView & postView Controller
           The Controller preView listerner is used to get some key settings and to modify some Json responses
           The Controller postView listerner is used to modify the other kind of responses (=> it might need to be modified next)
        > Some parts of XenForo can't be protected with only listening the controller:
            # Search
                > Reason:     XenForo_ViewPublic_Search_Results has this function "XenForo_ViewPublic_Helper_Search::renderSearchResults"
                        which modifies the parameters of the Controller Response
                > Solution:    BBM_Protection_Model_Search
               
            # NewsFeed
                > Reason:     XenForo_ViewPublic_NewsFeed_View has this function "XenForo_ViewPublic_Helper_NewsFeed::getTemplates"
                        which modifies the parameters of the Controller Response
                > Solution:    See here: BBM_Protection_Model_NewsFeed
               
            # ThreadWatch
                > Reason:     The mail is sent inside the datawritter (no controller here)
                > Solution:    See here: BBM_Protection_Model_ThreadWatch
        > There are two tools to protect the Bb Content Content:
            These two tools are some stand-alone functions easy to use.
            1) The parsing protection - this is the cleanest way to do this.
                Arguments:
               
                > $string        The string to process
                > $checkVisitorPerms     Optional. Default is true. This will protect the Bb Code Content only if the user doesn't have the permission to see it.
                            In some case the parameter must be set on false. For example with email or alerts => even if the visitor has the permission
                            to see the content it doesn't mean the one who will receive the email or the alert has also this permission
           
            2) The mini parser protection - An alternative to the XenForo parser/formatter
                Reason: with an incomplete closing tag, the XenForo parser will eat all the remaining text... if you apply this to the html output, it's really a problem
                Arguments are the same than the two above
    **************************************/
    /****
    *    Debug tool
    ***/
    protected static $_debug;
    /****
    *    XenForo Listeners Hooks
    ***/
    protected static $_isControllerAdmin = false;
    protected static $_responseType = NULL;
    protected static $_isJson = false; //Double check
    protected static $_controllerName = NULL;
    protected static $_controllerAction = NULL;
    protected static $_viewName = NULL;
    protected static $_processMiniParser = false;   
    protected static $_processParsing = true;
       
    public static function controllerPreDispatch(XenForo_FrontController $fc, XenForo_RouteMatch &$routeMatch)
    {
        /* Listener - Execution order: #1 */
        self::$_responseType = $routeMatch->getResponseType();
    }
    public static function controllerPreView(XenForo_FrontController $fc,
        XenForo_ControllerResponse_Abstract &$controllerResponse,
        XenForo_ViewRenderer_Abstract &$viewRenderer,
        array &$containerParams
    )
    {
        /* Listener - Execution order: #2 */
        self::$_isControllerAdmin = (strstr($controllerResponse->controllerName, 'ControllerAdmin')) ? true : false;
              self::$_controllerName = (isset($controllerResponse->controllerName)) ? $controllerResponse->controllerName : NULL;
              self::$_controllerAction = (isset($controllerResponse->controllerAction)) ? $controllerResponse->controllerAction : NULL;
              self::$_viewName = (isset($controllerResponse->viewName)) ? $controllerResponse->viewName : NULL;
              self::$_isJson = ($viewRenderer instanceof XenForo_ViewRenderer_Json) ? true : false;
   
        if(XenForo_Application::get('options')->get('Bbm_ContentProtection') && self::$_isControllerAdmin === false)
        {
            if(self::$_isJson == true)
            {
                /***
                *  Protect Json Response here. It will not work with the controllerPostView listener (will generate an error)
                **/
               
                if(isset($controllerResponse->params['post']['message']))
                {
                    /***
                    *    Use for:     - Edit inline
                    *            - Thread fast preview (small popup when mouse over title)
                    *            - Thread/post/conversation edit preview
                    **/
                    $controllerResponse->params['post']['message'] = self::parsingProtection($controllerResponse->params['post']['message']);
                }
   
                if(isset($controllerResponse->params['quote']))
                {
                    /***
                    *    Use for:     - Quotes
                    **/
                    $controllerResponse->params['quote'] = self::parsingProtection($controllerResponse->params['quote'], true, 'quotes');
                }
            }
        }
        if(self::$_debug == 'pre' && self::$_isControllerAdmin === false)
        {
            $visitor = XenForo_Visitor::getInstance();
            if($visitor['is_admin'])
            {
                Zend_Debug::dump($controllerResponse);
            }
        }
        /*Extra function to hide tags in Thread fast preview*/
        if(self::$_isJson == true && self::$_viewName == 'XenForo_ViewPublic_Thread_Preview' && XenForo_Application::get('options')->get('Bbm_HideTagsInFastPreview'))
        {
                  if(isset($controllerResponse->params['post']['message']))
                  {
                if(XenForo_Application::get('options')->get('Bbm_HideTagsInFastPreviewInvisible'))
                {
                    $formatter = XenForo_BbCode_Formatter_Base::create('BBM_Protection_BbCode_Formatter_BbCode_Eradicator', false);
                    $formatter->setAllTagsAsProtected();
                    $formatter->invisibleMode();
                }
                else
                {
                    $formatter = XenForo_BbCode_Formatter_Base::create('BBM_Protection_BbCode_Formatter_BbCode_Lupin', false);
                }
               
                $parser = XenForo_BbCode_Parser::create($formatter);
                $extraStates = array(
                    'bbmContentProtection' => true
                );               
               
                $controllerResponse->params['post']['message'] = $parser->render($controllerResponse->params['post']['message'], $extraStates);
                  }       
        }       
    }
    public static function controllerPostView($fc, &$output)
    {
        /* Listener - Execution order: #3 */
        if(XenForo_Application::get('options')->get('Bbm_ContentProtection') && self::$_isControllerAdmin === false && is_string($output))
        {
                  if(self::$_responseType == 'html' && self::$_isJson != true && self::$_processParsing == true)
                  {
                      //Don't use the parser method, if it finds an opening tag it will "eat" all the page. Use instead the regex method
                      //$output = self::parsingProtection($output);
                 
                      $output = self::miniParserProtection($output, true);
                  }
                  elseif(self::$_processMiniParser == true)
                  {
                      $output = self::miniParserProtection($output, true);
                  }
        }
        if(self::$_debug == 'post' && self::$_isControllerAdmin === false)
        {
            $visitor = XenForo_Visitor::getInstance();
            if($visitor['is_admin'])
            {
                Zend_Debug::dump($output);
            }
        }
    }
    /****
    *    Bbm Bb Codes Content Protection tools
    ***/
    public static function parsingProtection($string, $checkVisitorPerms = true, $src = null)
    {
        if(XenForo_Application::get('options')->get('Bbm_ContentProtection'))
        {
            $formatter = XenForo_BbCode_Formatter_Base::create('BBM_Protection_BbCode_Formatter_BbCode_Eradicator', false);
            $formatter->setCheckVisitorPerms($checkVisitorPerms);
            $parser = XenForo_BbCode_Parser::create($formatter);
            $extraStates = array(
                'bbmContentProtection' => true
            );
           
            $string = $parser->render($string, $extraStates);
            if($src == 'quotes')
            {
                $string.= "\r\n";
            }           
        }
       
        return $string;
    }
   
    public static function miniParserProtection($string, $checkVisitorPerms = true)
    {
        $visitor = XenForo_Visitor::getInstance();
        $options = XenForo_Application::get('options');
        if(!$options->Bbm_ContentProtection)
        {
            return $string;
        }
        $bbmCached = XenForo_Application::getSimpleCacheData('bbm_active');
        if(    !is_array($bbmCached)
            || !isset($bbmCached['protected'])
            || !is_array($bbmCached['protected'])
            || empty($bbmCached['protected'])
        )
        {
            return $string;
        }
        if($checkVisitorPerms === true)
        {
            $visitorUserGroupIds = array_merge(array((string)$visitor['user_group_id']), (explode(',', $visitor['secondary_group_ids'])));
        }
       
        $protectedTags = $bbmCached['protected'];
        $replace = new XenForo_Phrase('bbm_viewer_content_protected');
        $openTags = array();
        $xenProtectedTags = array('attach', 'email', 'img', 'media', 'url');
        $xenProtectedTagsNoPermsDisplay = false;//make another phrase inviting the user to log to see the content?
       
        foreach($xenProtectedTags as $tagName)
        {
            $permKey = "bbm_hide_{$tagName}";
            $permsVal = $visitorUserGroupIds;
            if($checkVisitorPerms === true)
            {
                if($visitor->hasPermission('bbm_bbcodes_grp', $permKey))
                {
                    $permsVal = array();
                }
            }
            else
            {
                //ie: for alerts, mails
                if($xenProtectedTagsNoPermsDisplay)
                {
                    $permsVal = array();
                }
            }
               
            $protectedTags[$tagName] = $permsVal;
        }
        foreach($protectedTags AS $tag => $perms)
        {
            if($checkVisitorPerms === true && array_intersect($visitorUserGroupIds, $perms))
            {
                continue;
            }
           
            $openTags[] = $tag;
        }
       
        $tagRules = array();
           
        foreach($openTags as $tag)
        {
            $tagRules[$tag] = array(
                'stringReplace'  => new XenForo_Phrase('bbm_viewer_content_protected')
            );
        }
        $parserOptions = array(
            'parserOpeningCharacter' => '[',
            'parserClosingCharacter' => ']',
            'htmlspecialcharsForContent' => false,
            'htmlspecialcharsForOptions' => false,
            'nl2br' => false,
            'checkClosingTag' => true,
            'preventHtmlBreak' => true
        );
        if(!preg_match('#<body[^>]*?><pre>#', $string))
        {
            $miniParser = new BBM_Protection_Helper_MiniParser($string, $tagRules, array(), $parserOptions);
            $string = $miniParser->render();
        }
        return $string;       
    }
}
//Zend_Debug::dump($string);

Форк на GitHub:
Автор
Hope
Скачивания
6
Просмотры
155
Первый выпуск
Обновление
Оценка
0.00 звёзд 0 оценок

Другие ресурсы пользователя Hope

Поделиться ресурсом

Последние обновления

  1. 1.3.5

    Оптимизация кода
  2. 1.3.3

    Исправление ошибок
Назад
Сверху Снизу