Advertisement
Guest User

Sedo_Formatter_2013-3-3

a guest
Mar 2nd, 2013
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.12 KB | None | 0 0
  1. <?php
  2.  
  3. class KingK_BbCodeManager_BbCode_Formatter_Base extends XFCP_KingK_BbCodeManager_BbCode_Formatter_Base
  4. {
  5.     /****
  6.     *   GLOBAL VARIABLES
  7.     ***/
  8.     protected $_customTags = null;
  9.     protected $_parser;
  10.     protected $_preloadCustomTemplates = array();
  11.  
  12.     protected $_threadParams = null;
  13.     protected $_postsDatas = null;
  14.     protected $_bbCodesMap = null;
  15.     protected $_bbCodesIncrementation = array();
  16.     protected $_currentPostParams = null;
  17.    
  18.     /****
  19.     *   CREATE TAGS
  20.     ***/
  21.     public function getTags()
  22.     {
  23.         $parentTags = parent::getTags();
  24.        
  25.         if($this->_customTags === null)
  26.         {
  27.             $this->bakeCustomTags();
  28.         }
  29.        
  30.         if($this->_customTags !== null)
  31.         {
  32.             return array_merge((array) $parentTags, (array) $this->_customTags);
  33.         }
  34.        
  35.         return $parentTags;
  36.     }
  37.  
  38.     public function bakeCustomTags()
  39.     {
  40.         $customTags = XenForo_Model::create('KingK_BbCodeManager_Model_CustomBbCode')->getAllCustomBbCodes('strict');
  41.          
  42.         if(!is_array($customTags))
  43.         {
  44.             return false;
  45.         }
  46.  
  47.         $allCustomTags = array();
  48.  
  49.         foreach($customTags AS $custom)
  50.         {
  51.             if((boolean)$custom['active'])
  52.             {
  53.                     if($custom['replacementBegin'])
  54.                     {
  55.                         if($custom['advancedOptions'])
  56.                         {
  57.                             $allCustomTags[$custom['tag']]['hasOption'] = true;
  58.                             $allCustomTags[$custom['tag']]['numberOfOptions'] = $custom['numberOfOptions'];
  59.                             $allCustomTags[$custom['tag']]['replacementBegin'] = $custom['replacementBegin'];
  60.                             $allCustomTags[$custom['tag']]['replacementEnd'] = $custom['replacementEnd'];
  61.                             $allCustomTags[$custom['tag']]['callback'] = array($this, 'renderAdvancedOptionsTag');
  62.  
  63.                             if($custom['plainCallback'])
  64.                             {
  65.                                 $allCustomTags[$custom['tag']]['parseCallback'] = array($this, 'parseValidatePlainIfNoOption');
  66.                             }
  67.                         }
  68.                         else
  69.                         {
  70.                             /*
  71.                                 We're in the XenForo standard replacement tag system which is using the sprintf cmd
  72.                                 If the user has written a percentage (ie:width, height) and didn't double it, then it will occur an error
  73.                                 Let's fix it
  74.                             */
  75.                             $fixSprintfPattern = '#(?<!%)%(?![%s])#';
  76.                             $custom['replacementBegin'] = preg_replace($fixSprintfPattern, '%%', $custom['replacementBegin']);
  77.                             $custom['replacementEnd'] = preg_replace($fixSprintfPattern, '%%', $custom['replacementEnd']);
  78.  
  79.                             $allCustomTags[$custom['tag']]['hasOption'] = ($custom['requiresOption'] == 0 ? false : true);
  80.                             $allCustomTags[$custom['tag']]['replace'] = array($custom['replacementBegin'], $custom['replacementEnd']);
  81.                         }
  82.                     }
  83.                     else if($custom['phpcallback_class'])
  84.                     {
  85.                         $allCustomTags[$custom['tag']]['phpcallback_class'] = $custom['phpcallback_class'];
  86.                         $allCustomTags[$custom['tag']]['phpcallback_method'] = $custom['phpcallback_method'];
  87.                         $allCustomTags[$custom['tag']]['callback'] = array($this, 'renderAdvancedTag');
  88.  
  89.                         if($custom['plainCallback'])
  90.                         {
  91.                             $allCustomTags[$custom['tag']]['parseCallback'] = array($this, 'parseValidatePlainIfNoOption');
  92.                         }
  93.                        
  94.                         //Search if the callback has some templates to preload (from the method "preloadTemplates")
  95.                         if(method_exists($custom['phpcallback_class'], 'preloadTemplates'))
  96.                         {
  97.                             $templateNames = $custom['phpcallback_class']::preloadTemplates($custom['phpcallback_method']);
  98.                            
  99.                             if(!is_array($templateNames))
  100.                             {
  101.                                 $templateNames = array($templateNames);
  102.                             }
  103.                            
  104.                             foreach($templateNames as $templateName)
  105.                             {
  106.                                 if(!empty($templateName) && array_search($templateName, $this->_preloadCustomTemplates) === false)
  107.                                 {
  108.                                     $this->_preloadCustomTemplates[] = $templateName;
  109.                                 }
  110.                             }
  111.                         }
  112.                     }
  113.                    
  114.                     if($custom['trimLeadingLinesAfter'] > 0 && $custom['trimLeadingLinesAfter'] < 3)
  115.                     {
  116.                         $allCustomTags[$custom['tag']]['trimLeadingLinesAfter'] = $custom['trimLeadingLinesAfter'];
  117.                     }
  118.                    
  119.                     if($custom['regex'])
  120.                     {
  121.                         $allCustomTags[$custom['tag']]['optionRegex'] = $custom['regex'];
  122.                     }
  123.                    
  124.                     if($custom['plainChildren'])
  125.                     {
  126.                         $allCustomTags[$custom['tag']]['plainChildren'] = true;
  127.                     }
  128.                    
  129.                     if($custom['stopSmilies'])
  130.                     {
  131.                         $allCustomTags[$custom['tag']]['stopSmilies'] = true;
  132.                     }
  133.                    
  134.                     if($custom['stopLineBreakConversion'])
  135.                     {
  136.                         $allCustomTags[$custom['tag']]['stopLineBreakConversion'] = true;
  137.                     }
  138.                 }
  139.         }
  140.  
  141.          $this->_customTags = $allCustomTags;
  142.     }
  143.  
  144.     /****
  145.     *   RENDER TAGS
  146.     ***/
  147.     public function renderAdvancedOptionsTag(array $tag, array $rendererStates)
  148.     {
  149.         $this->_getCurrentPostParams($tag['tag']);
  150.        
  151.         $tagInfo['numberOfOptions'] = $this->_tags[$tag['tag']]['numberOfOptions'];
  152.         $tagInfo['replacementBegin'] = $this->_tags[$tag['tag']]['replacementBegin'];
  153.         $tagInfo['replacementEnd'] = $this->_tags[$tag['tag']]['replacementEnd'];
  154.        
  155.         if (!empty($tag['option']) && $this->parseMultipleOptions($tag['option']))
  156.         {
  157.             $options = $this->parseMultipleOptions($tag['option']);
  158.  
  159.             if((int)count($options) == $tagInfo['numberOfOptions'])
  160.             {
  161.                 for($replaceIndex = 0; $replaceIndex <= ($tagInfo['numberOfOptions'] - 1); $replaceIndex++)
  162.                 {
  163.                     if(!(isset($options[$replaceIndex])))
  164.                     {
  165.                         //With the fix in the for loop, this protection shouldn't be needed anymore but let's keep it for safety
  166.                         continue;
  167.                     }
  168.  
  169.                     $replaceNumber = $replaceIndex + 1;
  170.                     $tagInfo['replacementBegin'] = str_replace('{' . $replaceNumber . '}', $options[$replaceIndex], $tagInfo['replacementBegin']);
  171.                     $tagInfo['replacementEnd'] = str_replace('{' . $replaceNumber . '}', $options[$replaceIndex], $tagInfo['replacementEnd']);
  172.                    
  173.                     $content = $this->renderSubTree($tag['children'], $rendererStates);
  174.                 }
  175.                 return $tagInfo['replacementBegin'] . $content . $tagInfo['replacementEnd'];
  176.             }
  177.             else
  178.             {
  179.                 return $tag['original'][0] . $tag['children'][0] . $tag['original'][1];
  180.             }
  181.         }
  182.     }
  183.  
  184.     public function parseMultipleOptions($tag)
  185.     {
  186.         $options = XenForo_Application::get('options');
  187.         $separator = (empty($options->Bbcm_BbCode_Options_Separator)) ? ', ' : $options->Bbcm_BbCode_Options_Separator;
  188.  
  189.         $attributes = explode($separator, $tag);
  190.         return $attributes;
  191.     }
  192.    
  193.     public function renderAdvancedTag(array $tag, array $rendererStates)
  194.     {
  195.         $this->_getCurrentPostParams($tag['tag']);
  196.  
  197.         $phpcallback_class = $this->_tags[$tag['tag']]['phpcallback_class'];
  198.         $phpcallback_method = $this->_tags[$tag['tag']]['phpcallback_method'];
  199.        
  200.         XenForo_Application::autoload($phpcallback_class);
  201.         return call_user_func_array(array($phpcallback_class, $phpcallback_method), array($tag, $rendererStates, &$this));
  202.     }
  203.  
  204.     /****
  205.     *   PARSER TOOLS
  206.     ***/
  207.     public function getParser()
  208.     {
  209.         if (!isset($this->_parser))
  210.         {
  211.             $this->_parser = new XenForo_BbCode_parser($this);
  212.         }
  213.         return $this->_parser;
  214.     }
  215.    
  216.     public function ParseMyBBcodesOptions($string)
  217.     {
  218.         $tester = strlen($string) - strlen(strip_tags($string));
  219.  
  220.         if (empty($tester) AND preg_match('#[\[{](.+?)(?:=.+?)?[}\]].+?[{\[]/\1([}\]])#i', $string, $captures))
  221.         {
  222.                 $builder = $string;
  223.  
  224.                 if(isset($captures[2]) && $captures[2] == '}')
  225.                 {
  226.                     //This is an old special tag {a}...{/a}, convert it back to a normal tag [a]...[/a]
  227.                     $builder = preg_replace('#[\[{]((.+?)(?:=.+?)?)[}\]](.+?)[{\[](/\2)[}\]]#i', '[$1]$3[$4]', $string);
  228.                 }
  229.  
  230.                 $parser = $this->getParser();
  231.                 $string = $parser->render($builder);
  232.            
  233.                 //Fix for htmlspecialchars (no need to make it twice if it has already been done in options management)
  234.                 $string = str_replace(array('&amp;lt;', '&amp;gt;', '&amp;amp;'), array('&lt;', '&gt;', '&amp;'), $string);
  235.         }
  236.  
  237.         return $string;
  238.     }
  239.  
  240.     /****
  241.     *   TAG ALIGN FIX
  242.     ***/
  243.     public function renderTagAlign(array $tag, array $rendererStates)
  244.     {
  245.         $options = XenForo_Application::get('options');
  246.        
  247.         if(!$options->Bbcm_PatchAlignBbCode)
  248.         {      
  249.             return parent::renderTagAlign($tag, $rendererStates);
  250.         }
  251.        
  252.         $text = $this->renderSubTree($tag['children'], $rendererStates);
  253.         if (trim($text) === '')
  254.         {
  255.             $text = '<br />';
  256.         }
  257.  
  258.         switch (strtolower($tag['tag']))
  259.         {
  260.             case 'left':
  261.             case 'center':
  262.             case 'right':
  263.                 return '<div align="' . $tag['tag'] . '">' . $text . '</div>';
  264.  
  265.             default:
  266.                 return $text;
  267.         }
  268.     }
  269.  
  270.     /****
  271.     *   PRELOAD & RENDER TEMPLATES TOOL
  272.     ***/
  273.     public function preLoadTemplates(XenForo_View $view)
  274.     {
  275.          //Preload Custom Templates
  276.          if($this->_view && is_array($this->_preloadCustomTemplates))
  277.          {
  278.             foreach($this->_preloadCustomTemplates as $templateName)
  279.             {
  280.                 $this->_view->preLoadTemplate($templateName);
  281.             }
  282.         }
  283.  
  284.         return parent::preLoadTemplates($view);
  285.     }
  286.  
  287.     public function renderCustomTemplate($templateName, array $params = array())
  288.     {
  289.         if ($this->_view)
  290.         {
  291.             //Create and render template
  292.             $template = $this->_view->createTemplateObject($templateName, $params);
  293.             return $template->render();
  294.         }
  295.  
  296.         return false;
  297.     }
  298.  
  299.     /****
  300.     *   GET THREAD/POSTS PARAMS TOOLS
  301.     ***/
  302.     public function setView(XenForo_View $view = null)
  303.     {
  304.         parent::setView($view);
  305.  
  306.         if ($view)
  307.         {
  308.             $params = $view->getParams();
  309.            
  310.             if(isset($params['posts']))
  311.             {
  312.                 $this->_postsDatas = $params['posts'];
  313.                 $this->_createBbCodesMap($params['posts']);
  314.             }
  315.  
  316.             if(isset($params['thread']))
  317.             {
  318.                 $this->_threadParams = $params['thread'];
  319.             }          
  320.         }
  321.     }
  322.  
  323.     protected function _createBbCodesMap($posts = NULL)
  324.     {
  325.         if( $posts === NULL || !is_array($posts) )
  326.         {
  327.             return;
  328.         }
  329.    
  330.         foreach($posts as $post_id => $post)
  331.         {
  332.             $BbCodesTree = $this->getParser()->parse($post['message']);
  333.  
  334.             $it = new RecursiveIteratorIterator( new RecursiveArrayIterator($BbCodesTree) );
  335.  
  336.             foreach ($it as $tagKey => $tagName)
  337.             {
  338.                 if($tagKey === 'tag')
  339.                 {
  340.                     $this->_bbCodesMap[$tagName][] = $post_id;
  341.                 }
  342.             }
  343.         }
  344.     }
  345.  
  346.     protected function _getCurrentPostParams($tag)
  347.     {
  348.         $id = $this->_getCurrentTagId($tag);
  349.  
  350.         if( !isset($this->_bbCodesMap[$tag][$id]) )
  351.         {
  352.             $this->_currentPostParams = NULL;
  353.             return;
  354.         }
  355.        
  356.         $postId = $this->_bbCodesMap[$tag][$id];
  357.  
  358.         if ( !isset($this->_postsDatas[$postId]) )
  359.         {
  360.             $this->_currentPostParams = NULL;
  361.             return;
  362.         }
  363.        
  364.         $this->_currentPostParams = $this->_postsDatas[$postId];
  365.     }
  366.  
  367.     protected function _getCurrentTagId($tag)
  368.     {
  369.         if( !isset($this->_bbCodesIncrementation[$tag]) )
  370.         {
  371.             $this->_bbCodesIncrementation[$tag] = 0;   
  372.         }
  373.         else
  374.         {
  375.             $this->_bbCodesIncrementation[$tag]++;
  376.         }
  377.        
  378.         return $this->_bbCodesIncrementation[$tag];
  379.     }
  380.    
  381.     public function getThreadParams()
  382.     {
  383.         return $this->_threadParams;
  384.     }
  385.  
  386.     public function getThreadParam($param)
  387.     {
  388.         if( isset($this->_threadParams[$param]) )
  389.         {
  390.             return $this->_threadParams[$param];
  391.         }
  392.         return NULL;
  393.     }
  394.  
  395.     public function getPostParams()
  396.     {
  397.         return $this->_currentPostParams;
  398.     }
  399.  
  400.     public function getPostParam($param)
  401.     {
  402.         if( isset($this->_currentPostParams[$param]) )
  403.         {
  404.             return $this->_currentPostParams[$param];
  405.         }
  406.        
  407.         return NULL;
  408.     }  
  409. }
  410. //Zend_Debug::dump($abc);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement