Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class KingK_BbCodeManager_BbCode_Formatter_Base extends XFCP_KingK_BbCodeManager_BbCode_Formatter_Base
- {
- /****
- * GLOBAL VARIABLES
- ***/
- protected $_customTags = null;
- protected $_parser;
- protected $_preloadCustomTemplates = array();
- protected $_threadParams = null;
- protected $_postsDatas = null;
- protected $_bbCodesMap = null;
- protected $_bbCodesIncrementation = array();
- protected $_currentPostParams = null;
- /****
- * CREATE TAGS
- ***/
- public function getTags()
- {
- $parentTags = parent::getTags();
- if($this->_customTags === null)
- {
- $this->bakeCustomTags();
- }
- if($this->_customTags !== null)
- {
- return array_merge((array) $parentTags, (array) $this->_customTags);
- }
- return $parentTags;
- }
- public function bakeCustomTags()
- {
- $customTags = XenForo_Model::create('KingK_BbCodeManager_Model_CustomBbCode')->getAllCustomBbCodes('strict');
- if(!is_array($customTags))
- {
- return false;
- }
- $allCustomTags = array();
- foreach($customTags AS $custom)
- {
- if((boolean)$custom['active'])
- {
- if($custom['replacementBegin'])
- {
- if($custom['advancedOptions'])
- {
- $allCustomTags[$custom['tag']]['hasOption'] = true;
- $allCustomTags[$custom['tag']]['numberOfOptions'] = $custom['numberOfOptions'];
- $allCustomTags[$custom['tag']]['replacementBegin'] = $custom['replacementBegin'];
- $allCustomTags[$custom['tag']]['replacementEnd'] = $custom['replacementEnd'];
- $allCustomTags[$custom['tag']]['callback'] = array($this, 'renderAdvancedOptionsTag');
- if($custom['plainCallback'])
- {
- $allCustomTags[$custom['tag']]['parseCallback'] = array($this, 'parseValidatePlainIfNoOption');
- }
- }
- else
- {
- /*
- We're in the XenForo standard replacement tag system which is using the sprintf cmd
- If the user has written a percentage (ie:width, height) and didn't double it, then it will occur an error
- Let's fix it
- */
- $fixSprintfPattern = '#(?<!%)%(?![%s])#';
- $custom['replacementBegin'] = preg_replace($fixSprintfPattern, '%%', $custom['replacementBegin']);
- $custom['replacementEnd'] = preg_replace($fixSprintfPattern, '%%', $custom['replacementEnd']);
- $allCustomTags[$custom['tag']]['hasOption'] = ($custom['requiresOption'] == 0 ? false : true);
- $allCustomTags[$custom['tag']]['replace'] = array($custom['replacementBegin'], $custom['replacementEnd']);
- }
- }
- else if($custom['phpcallback_class'])
- {
- $allCustomTags[$custom['tag']]['phpcallback_class'] = $custom['phpcallback_class'];
- $allCustomTags[$custom['tag']]['phpcallback_method'] = $custom['phpcallback_method'];
- $allCustomTags[$custom['tag']]['callback'] = array($this, 'renderAdvancedTag');
- if($custom['plainCallback'])
- {
- $allCustomTags[$custom['tag']]['parseCallback'] = array($this, 'parseValidatePlainIfNoOption');
- }
- //Search if the callback has some templates to preload (from the method "preloadTemplates")
- if(method_exists($custom['phpcallback_class'], 'preloadTemplates'))
- {
- $templateNames = $custom['phpcallback_class']::preloadTemplates($custom['phpcallback_method']);
- if(!is_array($templateNames))
- {
- $templateNames = array($templateNames);
- }
- foreach($templateNames as $templateName)
- {
- if(!empty($templateName) && array_search($templateName, $this->_preloadCustomTemplates) === false)
- {
- $this->_preloadCustomTemplates[] = $templateName;
- }
- }
- }
- }
- if($custom['trimLeadingLinesAfter'] > 0 && $custom['trimLeadingLinesAfter'] < 3)
- {
- $allCustomTags[$custom['tag']]['trimLeadingLinesAfter'] = $custom['trimLeadingLinesAfter'];
- }
- if($custom['regex'])
- {
- $allCustomTags[$custom['tag']]['optionRegex'] = $custom['regex'];
- }
- if($custom['plainChildren'])
- {
- $allCustomTags[$custom['tag']]['plainChildren'] = true;
- }
- if($custom['stopSmilies'])
- {
- $allCustomTags[$custom['tag']]['stopSmilies'] = true;
- }
- if($custom['stopLineBreakConversion'])
- {
- $allCustomTags[$custom['tag']]['stopLineBreakConversion'] = true;
- }
- }
- }
- $this->_customTags = $allCustomTags;
- }
- /****
- * RENDER TAGS
- ***/
- public function renderAdvancedOptionsTag(array $tag, array $rendererStates)
- {
- $this->_getCurrentPostParams($tag['tag']);
- $tagInfo['numberOfOptions'] = $this->_tags[$tag['tag']]['numberOfOptions'];
- $tagInfo['replacementBegin'] = $this->_tags[$tag['tag']]['replacementBegin'];
- $tagInfo['replacementEnd'] = $this->_tags[$tag['tag']]['replacementEnd'];
- if (!empty($tag['option']) && $this->parseMultipleOptions($tag['option']))
- {
- $options = $this->parseMultipleOptions($tag['option']);
- if((int)count($options) == $tagInfo['numberOfOptions'])
- {
- for($replaceIndex = 0; $replaceIndex <= ($tagInfo['numberOfOptions'] - 1); $replaceIndex++)
- {
- if(!(isset($options[$replaceIndex])))
- {
- //With the fix in the for loop, this protection shouldn't be needed anymore but let's keep it for safety
- continue;
- }
- $replaceNumber = $replaceIndex + 1;
- $tagInfo['replacementBegin'] = str_replace('{' . $replaceNumber . '}', $options[$replaceIndex], $tagInfo['replacementBegin']);
- $tagInfo['replacementEnd'] = str_replace('{' . $replaceNumber . '}', $options[$replaceIndex], $tagInfo['replacementEnd']);
- $content = $this->renderSubTree($tag['children'], $rendererStates);
- }
- return $tagInfo['replacementBegin'] . $content . $tagInfo['replacementEnd'];
- }
- else
- {
- return $tag['original'][0] . $tag['children'][0] . $tag['original'][1];
- }
- }
- }
- public function parseMultipleOptions($tag)
- {
- $options = XenForo_Application::get('options');
- $separator = (empty($options->Bbcm_BbCode_Options_Separator)) ? ', ' : $options->Bbcm_BbCode_Options_Separator;
- $attributes = explode($separator, $tag);
- return $attributes;
- }
- public function renderAdvancedTag(array $tag, array $rendererStates)
- {
- $this->_getCurrentPostParams($tag['tag']);
- $phpcallback_class = $this->_tags[$tag['tag']]['phpcallback_class'];
- $phpcallback_method = $this->_tags[$tag['tag']]['phpcallback_method'];
- XenForo_Application::autoload($phpcallback_class);
- return call_user_func_array(array($phpcallback_class, $phpcallback_method), array($tag, $rendererStates, &$this));
- }
- /****
- * PARSER TOOLS
- ***/
- public function getParser()
- {
- if (!isset($this->_parser))
- {
- $this->_parser = new XenForo_BbCode_parser($this);
- }
- return $this->_parser;
- }
- public function ParseMyBBcodesOptions($string)
- {
- $tester = strlen($string) - strlen(strip_tags($string));
- if (empty($tester) AND preg_match('#[\[{](.+?)(?:=.+?)?[}\]].+?[{\[]/\1([}\]])#i', $string, $captures))
- {
- $builder = $string;
- if(isset($captures[2]) && $captures[2] == '}')
- {
- //This is an old special tag {a}...{/a}, convert it back to a normal tag [a]...[/a]
- $builder = preg_replace('#[\[{]((.+?)(?:=.+?)?)[}\]](.+?)[{\[](/\2)[}\]]#i', '[$1]$3[$4]', $string);
- }
- $parser = $this->getParser();
- $string = $parser->render($builder);
- //Fix for htmlspecialchars (no need to make it twice if it has already been done in options management)
- $string = str_replace(array('&lt;', '&gt;', '&amp;'), array('<', '>', '&'), $string);
- }
- return $string;
- }
- /****
- * TAG ALIGN FIX
- ***/
- public function renderTagAlign(array $tag, array $rendererStates)
- {
- $options = XenForo_Application::get('options');
- if(!$options->Bbcm_PatchAlignBbCode)
- {
- return parent::renderTagAlign($tag, $rendererStates);
- }
- $text = $this->renderSubTree($tag['children'], $rendererStates);
- if (trim($text) === '')
- {
- $text = '<br />';
- }
- switch (strtolower($tag['tag']))
- {
- case 'left':
- case 'center':
- case 'right':
- return '<div align="' . $tag['tag'] . '">' . $text . '</div>';
- default:
- return $text;
- }
- }
- /****
- * PRELOAD & RENDER TEMPLATES TOOL
- ***/
- public function preLoadTemplates(XenForo_View $view)
- {
- //Preload Custom Templates
- if($this->_view && is_array($this->_preloadCustomTemplates))
- {
- foreach($this->_preloadCustomTemplates as $templateName)
- {
- $this->_view->preLoadTemplate($templateName);
- }
- }
- return parent::preLoadTemplates($view);
- }
- public function renderCustomTemplate($templateName, array $params = array())
- {
- if ($this->_view)
- {
- //Create and render template
- $template = $this->_view->createTemplateObject($templateName, $params);
- return $template->render();
- }
- return false;
- }
- /****
- * GET THREAD/POSTS PARAMS TOOLS
- ***/
- public function setView(XenForo_View $view = null)
- {
- parent::setView($view);
- if ($view)
- {
- $params = $view->getParams();
- if(isset($params['posts']))
- {
- $this->_postsDatas = $params['posts'];
- $this->_createBbCodesMap($params['posts']);
- }
- if(isset($params['thread']))
- {
- $this->_threadParams = $params['thread'];
- }
- }
- }
- protected function _createBbCodesMap($posts = NULL)
- {
- if( $posts === NULL || !is_array($posts) )
- {
- return;
- }
- foreach($posts as $post_id => $post)
- {
- $BbCodesTree = $this->getParser()->parse($post['message']);
- $it = new RecursiveIteratorIterator( new RecursiveArrayIterator($BbCodesTree) );
- foreach ($it as $tagKey => $tagName)
- {
- if($tagKey === 'tag')
- {
- $this->_bbCodesMap[$tagName][] = $post_id;
- }
- }
- }
- }
- protected function _getCurrentPostParams($tag)
- {
- $id = $this->_getCurrentTagId($tag);
- if( !isset($this->_bbCodesMap[$tag][$id]) )
- {
- $this->_currentPostParams = NULL;
- return;
- }
- $postId = $this->_bbCodesMap[$tag][$id];
- if ( !isset($this->_postsDatas[$postId]) )
- {
- $this->_currentPostParams = NULL;
- return;
- }
- $this->_currentPostParams = $this->_postsDatas[$postId];
- }
- protected function _getCurrentTagId($tag)
- {
- if( !isset($this->_bbCodesIncrementation[$tag]) )
- {
- $this->_bbCodesIncrementation[$tag] = 0;
- }
- else
- {
- $this->_bbCodesIncrementation[$tag]++;
- }
- return $this->_bbCodesIncrementation[$tag];
- }
- public function getThreadParams()
- {
- return $this->_threadParams;
- }
- public function getThreadParam($param)
- {
- if( isset($this->_threadParams[$param]) )
- {
- return $this->_threadParams[$param];
- }
- return NULL;
- }
- public function getPostParams()
- {
- return $this->_currentPostParams;
- }
- public function getPostParam($param)
- {
- if( isset($this->_currentPostParams[$param]) )
- {
- return $this->_currentPostParams[$param];
- }
- return NULL;
- }
- }
- //Zend_Debug::dump($abc);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement