1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-05 14:17:49 +02:00

toHTML modifiers extracted to new method.

This commit is contained in:
Cameron
2021-01-15 10:59:52 -08:00
parent 13517e83a8
commit 497e7279b6
3 changed files with 154 additions and 98 deletions

View File

@@ -332,8 +332,7 @@ class e_parse
*/
public function setMultibyte($bool)
{
// var_dump(e_LAN);
// var_dump(e_LANGUAGE);
if($bool === false)
{
$this->multibyte = false;
@@ -1072,7 +1071,6 @@ class e_parse
* @param string $ending It will be used as Ending and appended to the trimmed string.
* @param boolean $exact If false, $text will not be cut mid-word
* @return string Trimmed string.
* @todo find a modern replacement
*/
public function html_truncate($text, $length = 100, $ending = '...', $exact = true)
{
@@ -1355,42 +1353,7 @@ class e_parse
global $fromadmin;
// Set default modifiers to start
$opts = $this->e_optDefault;
// Now process any modifiers that are specified
if($modifiers)
{
$aMods = explode(',', $modifiers);
// If there's a supermodifier, it must be first, and in uppercase
$psm = trim($aMods[0]);
if(isset($this->e_SuperMods[$psm]))
{
// Supermodifier found - override default values where necessary
$opts = array_merge($opts, $this->e_SuperMods[$psm]);
$opts['context'] = $psm;
unset($aMods[0]);
}
// Now find any regular modifiers; use them to modify the context
// (there should only be one or two out of the list of possibles)
foreach($aMods as $mod)
{
// Slight concession to varying coding styles - stripping spaces is a waste of CPU cycles!
$mod = trim($mod);
if(isset($this->e_Modifiers[$mod]))
{
// This is probably quicker than array_merge
// - especially as usually only one or two loops
foreach($this->e_Modifiers[$mod] as $k => $v)
{
// Update our context-specific options
$opts[$k] = $v;
}
}
}
}
$opts = $this->getModifiers($modifiers);
// Turn off a few things if not enabled in options
if(empty($pref['smiley_activate']))
@@ -1434,12 +1397,12 @@ class e_parse
{
$text = strip_tags($text);
}
/*
if(MAGIC_QUOTES_GPC === true) // precaution for badly saved data.
{
$text = stripslashes($text);
}
*/
// Make sure we have a valid count for word wrapping
if(!$wrap && !empty($pref['main_wordwrap']))
@@ -1484,6 +1447,7 @@ class e_parse
else
{
// Set the options for this pass
$opts = $saveOpts;
// Have to have a good test in case a 'non-key' bbcode starts the block
@@ -1501,34 +1465,21 @@ class e_parse
// $matches[5] - closing tag
// In case we decide to load a file
$bbPath = e_CORE . 'bbcodes/';
$bbFile = strtolower(str_replace('_', '', $matches[2]));
$bbcode = '';
$className = '';
// $bbPath = e_CORE . 'bbcodes/';
// $bbFile = strtolower(str_replace('_', '', $matches[2]));
// $bbcode = '';
// $className = '';
$full_text = '';
$code_text = $matches[4];
$parm = $matches[3] ? substr($matches[3], 1) : '';
// $parm = $matches[3] ? substr($matches[3], 1) : '';
$last_bbcode = $matches[2];
switch($matches[2])
{
case 'php' :
// Probably run the output through the normal processing functions - but put here so the PHP code can disable if desired
$proc_funcs = true;
// This is just the contents of the php.bb file pulled in - its short, so will be quicker
// $search = array("&quot;", "&#039;", "&#036;", '<br />', E_NL, "-&gt;", "&lt;br /&gt;");
// $replace = array('"', "'", "$", "\n", "\n", "->", "<br />");
// Shouldn't have any parameter on this bbcode
// Not sure whether checks are necessary now we've reorganised
// if (!$matches[3]) $bbcode = str_replace($search, $replace, $matches[4]);
// Because we're bypassing most of the initial parser processing, we should be able to just reverse the effects of toDB() and execute the code
// [SecretR] - avoid php code injections, missing php.bb will completely disable user posted php blocks
$bbcode = file_get_contents($bbPath . $bbFile . '.bb');
if(!$matches[3])
{
$code_text = html_entity_decode($matches[4], ENT_QUOTES, 'UTF-8');
}
$proc_funcs = false;
$code_text = '';
break;
case 'html' : // This overrides and deprecates html.bb
@@ -1560,40 +1511,13 @@ class e_parse
case 'hide' :
$proc_funcs = true;
default : // Most bbcodes will just execute their normal file
// @todo should we cache these bbcodes? require_once should make class-related codes quite efficient
if(file_exists($bbPath . 'bb_' . $bbFile . '.php'))
{ // Its a bbcode class file
require_once($bbPath . 'bb_' . $bbFile . '.php');
$className = 'bb_' . $last_bbcode;
$this->bbList[$last_bbcode] = new $className();
}
elseif(file_exists($bbPath . $bbFile . '.bb'))
{
$bbcode = file_get_contents($bbPath . $bbFile . '.bb');
}
} // end - switch ($matches[2])
if($className)
{
/** @var e_bb_base $tempCode */
$tempCode = new $className();
$full_text = $tempCode->bbPreDisplay($matches[4], $parm);
}
elseif($bbcode)
{ // Execute the file
$full_text = eval($bbcode); // Require output of bbcode to be returned
// added to remove possibility of nested bbcode exploits ...
// (same as in bbcode_handler - is it right that it just operates on $bbcode_return and not on $bbcode_output? - QUERY XXX-02
}
if(strpos($full_text, '[') !== false)
{
$exp_search = array('eval', 'expression');
$exp_replace = array('ev<b></b>al', 'expres<b></b>sion');
$bbcode_return = str_replace($exp_search, $exp_replace, $full_text);
case 'scode':
case 'code' :
$parseBB = false;
$full_text = $this->parseBBcodes('['.$last_bbcode.']'.$code_text.'[/'.$last_bbcode.']', $postID);
break;
}
}
}
@@ -5384,6 +5308,53 @@ class e_parse
return false;
}
/**
* @param $modifiers
* @return array
*/
private function getModifiers($modifiers)
{
$opts = $this->e_optDefault;
if(empty($modifiers))
{
return $opts;
}
// Now process any modifiers that are specified
$aMods = explode(',', $modifiers);
// If there's a supermodifier, it must be first, and in uppercase
$psm = trim($aMods[0]);
if(isset($this->e_SuperMods[$psm]))
{
// Supermodifier found - override default values where necessary
$opts = array_merge($opts, $this->e_SuperMods[$psm]);
$opts['context'] = $psm;
unset($aMods[0]);
}
// Now find any regular modifiers; use them to modify the context
// (there should only be one or two out of the list of possibles)
foreach($aMods as $mod)
{
// Slight concession to varying coding styles - stripping spaces is a waste of CPU cycles!
$mod = trim($mod);
if(isset($this->e_Modifiers[$mod]))
{
// This is probably quicker than array_merge
// - especially as usually only one or two loops
foreach($this->e_Modifiers[$mod] as $k => $v)
{
// Update our context-specific options
$opts[$k] = $v;
}
}
}
return $opts;
}
}

View File

@@ -89,8 +89,8 @@
'block' =>
array (
),
'code' =>
array (
'code' => array (
),
'glyph' =>
array (
@@ -217,6 +217,7 @@
{
$input = '['.$bbcode.']http://mysite.com[/'.$bbcode.']';
$result = $this->bb->parseBBCodes($input, true); // parsing to check for PHP errors.
// $this->assertNotEmpty($result, $input." was empty.");
continue;
}

View File

@@ -135,6 +135,43 @@ while(&#036;row = &#036;sql-&gt;fetch())
}
*/
function testToHTMLWithBBcode()
{
$tests = array(
0 => array(
'text' => '[code]$something = "something";[/code]',
'expected' => "<pre class='prettyprint linenums code_highlight code-box bbcode-code' style='unicode-bidi: embed; direction: ltr'>\$something = &quot;something&quot;;</pre>",
),
1 => array(
'text' => '[b]Title[/b][code]$something = "something"; [b]Not parsed[/b][/code]',
'expected' => "<strong class='bbcode bold bbcode-b'>Title</strong><pre class='prettyprint linenums code_highlight code-box bbcode-code' style='unicode-bidi: embed; direction: ltr'>\$something = &quot;something&quot;; &#091;b]Not parsed&#091;/b]</pre>",
),
2 => array(
'text' => '[php]<?php $something = "something";[/php]', // legacy usage, now deprecated.
'expected' => "",
),
);
foreach($tests as $var)
{
$result = $this->tp->toHTML($var['text'], true);
if(!isset($var['expected']))
{
echo $result."\n\n";
continue;
}
$this->assertEquals($var['expected'], $result);
}
}
public function testParseTemplateWithEnabledCoreShortcodes()
{
@@ -295,7 +332,7 @@ while(&#036;row = &#036;sql-&gt;fetch())
//$this->assertSame();
}*/
public function testMultibyte()
public function testMultibyteOn()
{
// enable multibyte mode.
@@ -331,6 +368,46 @@ while(&#036;row = &#036;sql-&gt;fetch())
$result = $this->tp->ustrrpos($input, 'с');
$this->assertEquals(3, $result);
$this->tp->setMultibyte(false); // disable after test.
}
public function testMultibyteOff()
{
// enable multibyte mode.
$this->tp->setMultibyte(false);
$input = "an example of text";
// strtoupper
$result = $this->tp->ustrtoupper($input);
$this->assertEquals('AN EXAMPLE OF TEXT', $result);
// strlen
$result = $this->tp->ustrlen($input);
$this->assertEquals(18, $result);
// strtolower
$result = $this->tp->ustrtolower('AN EXAMPLE OF TEXT');
$this->assertEquals($input, $result);
// strpos
$result = $this->tp->ustrpos($input, 't');
$this->assertEquals(14, $result);
// substr
$result = $this->tp->usubstr($input, 0, 5);
$this->assertEquals('an ex', $result);
// stristr
$result = $this->tp->ustristr($input, 'of', true);
$this->assertEquals('an example ', $result);
// strrpos (last occurance of a string)
$result = $this->tp->ustrrpos($input, 'e');
$this->assertEquals(15, $result);
}
/*
@@ -733,6 +810,12 @@ while(&#036;row = &#036;sql-&gt;fetch())
'options' => array('w'=>300, 'h'=>200, 'type'=>'webp'),
'expected' =>'/thumb.php?src=e_PLUGIN%2Fgallery%2Fimages%2Fbutterfly.jpg&amp;w=300&amp;h=200&amp;type=webp'
),
3 => array(
'path' => '{e_PLUGIN}gallery/images/butterfly.jpg',
'options' => array('w'=>300, 'h'=>200, 'scale'=>'2x'),
'expected' =>'/thumb.php?src=e_PLUGIN%2Fgallery%2Fimages%2Fbutterfly.jpg&amp;w=600&amp;h=400'
),
);
foreach($urls as $val)
@@ -1833,6 +1916,7 @@ while(&#036;row = &#036;sql-&gt;fetch())
array("before http://something.com after", 'before <a class="e-url" href="http://something.com" >http://something.com</a> after'),
array("before https://someplace.com after", 'before <a class="e-url" href="https://someplace.com" >https://someplace.com</a> after'),
array("before (www.something.com) after", 'before (<a class="e-url" href="http://www.something.com" >www.something.com</a>) after'),
array('', ''),
);
foreach($tests as $row)