Beste leden van criminalspoint, op dit moment ben ik druk bezig aan één nieuwe website. Maar daarvoor heb ik een stack based ubb script voor nodig. Nu ik er eindelijk een heb gevonden die werkt, zit ik met één groter probleem.
Wanneer je 2 enters typt zet hij er maar één neer, terwijl er 2 nodig zijn. Wanneer ik er dan echter nl2br overheen gooi, wordt het script zo ongeveer per enter 2 enters neergezet. Ook niet echt de bedoeling.
Nu hoop ik dat iemand me hiermee kan helpen. Voor live voorbeeld PM me.
PHP
<?php
/*
* Auteur: Grayen Myrith
*/
class ubb
{
private $aPieces = array();
private $aTree = array();
private $bParse = true;
private $bParagraphise = true;
private $aAlias = array
(
'*' => 'li',
'list' => 'ul',
'ulist' => 'ul',
'olist' => 'ol',
'url' => 'a',
'quote' => 'blockquote',
'google' => 'search'
);
/*
allowed: alle tags die in de gedefineerde tag mogen staan.
child: de verplichte childs die een tag moet hebben.
parent: de verplichte parent die en tag moet hebben.
inline: is het inline element? of niet?
solid: is het een block element waarbij eerst alle tags moeten worden gesloten? Voorbeelden hiervan: table, ul, ol dat moet je uiteraard niet aan tags als td gaan geven, want dan krijg je bijvoorbeeld het volgende: <table><tr></tr></table><td>tekst</td> (en dat willen we natuurljik niet :P)
single: de tag heeft geen bij behorende sluitende tag.
master: door deze item toe te voegen negeert de tag alle regels (Pas op! bij deze tags kun je niet meer gebruik maken van $this->parse('tag') functionaliteit)
*/
public $aFilters = array
(
'basic' => array
(
'b' => array
(
'allowed' => 'all',
'solid' => false,
'inline' => true
),
'i' => array
(
'allowed' => 'all',
'solid' => false,
'inline' => true
),
'u' => array
(
'allowed' => 'all',
'solid' => false,
'inline' => true
),
's' => array
(
'allowed' => 'all',
'solid' => false,
'inline' => true
),
'sub' => array
(
'allowed' => 'all',
'solid' => false,
'inline' => true
),
'sup' => array
(
'allowed' => 'all',
'solid' => false,
'inline' => true
)
),
'extended' => array
(
'code' => array
(
'allowed' => 'all',
'solid' => true,
'inline' => false
),
'h2' => array
(
'allowed' => 'all',
'solid' => true,
'inline' => false
),
'h3' => array
(
'allowed' => 'all',
'solid' => true,
'inline' => false
),
'h4' => array
(
'allowed' => 'all',
'solid' => true,
'inline' => false
),
'h5' => array
(
'allowed' => 'all',
'solid' => true,
'inline' => false
),
'h6' => array
(
'allowed' => 'all',
'solid' => true,
'inline' => false
),
'blockquote' => array
(
'allowed' => 'all',
'solid' => true,
'inline' => false,
'master' => true
),
'hr' => array
(
'allowed' => 'all',
'solid' => true,
'inline' => false,
'single' => true
)
),
'links' => array
(
'a' => array
(
'allowed' => 'all',
'solid' => false,
'inline' => true
),
'search' => array
(
'allowed' => 'all',
'solid' => false,
'inline' => true
)
),
'images' => array
(
'img' => array
(
'allowed' => 'none',
'solid' => true,
'inline' => false,
'single' => true
)
),
'tables' => array
(
'table' => array
(
'allowed' => 'all',
'child' => array('tr'),
'solid' => true,
'inline' => false
),
'tr' => array
(
'allowed' => 'all',
'parent' => array('table'),
'child' => array('td', 'th'),
'solid' => false,
'inline' => false
),
'th' => array
(
'allowed' => 'all',
'parent' => array('tr'),
'solid' => false,
'inline' => false
),
'td' => array
(
'allowed' => 'all',
'parent' => array('tr'),
'solid' => false,
'inline' => false
)
),
'lists' => array
(
'ol' => array
(
'allowed' => 'all',
'child' => array('li'),
'solid' => true,
'inline' => false
),
'ul' => array
(
'allowed' => 'all',
'child' => array('li'),
'solid' => true,
'inline' => false
),
'li' => array
(
'allowed' => 'all',
'parent' => array('ul', 'ol'),
'solid' => false,
'inline' => false
)
)
);
private $aFilter = array('basic', 'extended', 'links', 'images', 'lists', 'tables');
private $aTags = array();
public $sText = '';
public $aText = array();
public function __construct($sText)
{
$this->sText = trim($sText);
foreach($this->aFilters as $sName => $aTags)
{
if(in_array($sName, $this->aFilter) === true)
{
$this->aTags = array_merge($this->aTags, $aTags);
}
}
}
private function cleanWhiteSpace()
{
$this->sText = preg_replace(array("/\\n{3,}/", "/\\n *\\n/"), "\\n\\n", str_replace(array("\\r\\n", "\\n\\r"), "\\n", $this->sText));
}
private function realEmpty($sString)
{
return !(empty($sString) === false && substr_count($sString, "\\n") != strlen($sString));
}
private function getPieces()
{
# De tekst die in delen moet worden gehakt.
$sText = $this->sText;
# De counter voor de key waarden voor elk stukje.
$i = 0;
# De tekst dat zich nog voor de tag bevindt.
$sOther = '';
while(strlen($sText) > 0)
{
$iOpen = strpos($sText, '[');
$iClose = strpos($sText, ']');
# Er is geen tag gevonden, het is dus tekst.
if($iClose === false || $iOpen === false)
{
$this->aPieces['t'.$i] = $sText;
$i++;
return true;
}
else
{
# Is het echt wel een tag, zoals [tag] en niet zomaar twee haakjes als ][.
while($iOpen > $iClose)
{
$iClose = strpos($sText, ']', $iClose + 1);
# Het was geen echte tag, dus het is tekst.
if($iClose === false)
{
$this->aPieces['t'.$i] = $sText;
$i++;
return true;
}
}
# Zit er niet nog een [ voor de tag.
$iTemp = strpos($sText, '[', $iOpen + 1);
while($iOpen < $iTemp && $iTemp < $iClose)
{
$iOpen = $iTemp;
$iTemp = strpos($sText, '[', $iOpen + 1);
}
# Er is een tag gevonden.
if($iOpen < $iClose)
{
$sTag = substr($sText, $iOpen, $iClose - $iOpen + 1);
# Heeft de tag wel een waarde en is het niet gewoon [].
$sTagValue = substr($sTag, 1, -1);
if(empty($sTagValue) === true)
{
$iOpen = $iClose + 1;
}
# De tekst voor de tag ophalen.
$sTemp = substr($sText, 0, $iOpen);
if(empty($sTemp) === false)
{
$sOther .= $sTemp;
}
# Is de tag niet leeg?
if(empty($sTagValue) === false)
{
# Stond er nog tekst voor de tag?
if(empty($sOther) === false)
{
$this->aPieces['t'.$i] = $sOther;
$i++;
$sOther = '';
}
# Stop de tag in de array.
$this->aPieces[($sTag{1} == '/' ? 'c' : 'o').$i] = $sTag;
$i++;
}
# Hakt het al geparsde deel van de tekst af.
$sText = substr($sText, $iClose + 1);
}
}
}
}
private function createTree()
{
foreach($this->aPieces as $sKey => $sPiece)
{
switch($sKey{0})
{
case 't':
$this->append($this->createText($sPiece));
break;
case 'o':
$this->append($this->createOpenTag($sPiece));
break;
case 'c':
$this->append($this->createCloseTag($sPiece));
break;
}
}
}
private function append($aData)
{
$this->aTree[] = $aData;
}
private function createOpenTag($sTag)
{
# Zet alle gegevens over de tag in een array.
$aNode = array();
$aNode['type'] = 'open';
$aNode['text'] = $sTag;
# De tag zonder haakjes.
$sTag = substr($sTag, 1, -1);
if(strpos($sTag, ' ') == true || strpos($sTag, '=') == true)
{
$aNode['tag'] = strtok($sTag, " =");
}
else
{
$aNode['tag'] = $sTag;
}
$aNode['attributes'] = array();
$iSpace = strpos($aNode['text'], ' ');
$iIsSign = strpos($aNode['text'], '=');
if($iSpace !== false || $iIsSign !== false)
{
while(preg_match('#([a-zA-Z0-9]+?)=\\'(.*?)\\'(\\s|$)#si', $sTag, $aMatches) == true || preg_match('#([a-zA-Z0-9]+?)=(.*?)(\\s|$)#si', $sTag, $aMatches) == true)
{
$sValue = trim($aMatches[2]);
if(empty($sValue) === false)
{
$aNode['attributes'][isset($this->aAlias[trim($aMatches[1])]) === true ? $this->aAlias[trim($aMatches[1])] : trim($aMatches[1])] = str_replace(array('"', "'"), '', $sValue);
}
$sTag = str_replace($aMatches[0], '', $sTag);
}
}
# Vervangt de waarde naar de alias.
if(isset($this->aAlias[$aNode['tag']]) === true)
{
$aNode['tag'] = $this->aAlias[$aNode['tag']];
if($iSpace === false && $iIsSign === false)
{
$aNode['text'] = '['.$aNode['tag'].']';
}
else
{
if($iSpace > $iIsSign || $iIsSign !== false && $iSpace === false)
{
$aNode['text'] = '['.$aNode['tag'].substr($aNode['text'], $iIsSign).']';
}
else if($iIsSign > $iSpace || $iSpace === false && $iIsSign !== false)
{
$aNode['text'] = '['.$aNode['tag'].substr($aNode['text'], $iSpace).']';
}
}
}
return $aNode;
}
private function createCloseTag($sTag)
{
$aNode = array();
$aNode['type'] = 'close';
$aNode['text'] = $sTag;
$aNode['tag'] = substr($sTag, 2, -1);
# Kijken of er een alias voor bestaat.
$aNode['tag'] = isset($this->aAlias[$aNode['tag']]) === true ? $this->aAlias[$aNode['tag']] : $aNode['tag'];
return $aNode;
}
private function createText($sText)
{
$aNode = array();
$aNode['type'] = 'text';
$aNode['text'] = $sText;
return $aNode;
}
private function validateTree()
{
# Is er een child toegevoegd (en is dus nog niet gesloten)?
$sOpened = '';
# Welk block element geopend is als laatst geopend?
$bSolidOpened = false;
$aNewTree = array();
$aOpenTags = array();
$aNode = array_shift($this->aTree);
while(empty($aNode) === false)
{
$aPrevNode = end($aNewTree);
switch($aNode['type'])
{
case 'text':
# Heeft de parent tag nog een bepaalde child nodig?
if($this->realEmpty($aNode['text']) === false && empty($aOpenTags) === false && ($vChild = $this->childNeeded(end($aOpenTags), 'text')) !== false && is_bool($vChild) === false)
{
array_unshift($this->aTree, $this->createCloseTag('[/'.$vChild['tag'].']'));
array_unshift($this->aTree, $aNode);
array_unshift($this->aTree, $vChild);
}
else
{
# Als het vorige fragment ook tekst is, voeg deze dan samen.
if($aPrevNode['type'] === 'text')
{
$aNode['text'] = $aPrevNode['text'].$aNode['text'];
array_pop($aNewTree);
}
# Als er een paragraaf in de tekst zit, sluit dan eerst alle open tags.
if(empty($aOpenTags) === false && isset($this->aTags[end($aOpenTags)]) === true && strpos($aNode['text'], "\\n\\n") !== false)
{
# Verwijder spaties links en recht van de tekst indien de tekst in een element staat.
$aNode['text'] = trim($aNode['text']);
if($this->aTags[end($aOpenTags)]['inline'] === true)
{
$aParagraphs = explode("\\n\\n", $aNode['text']);
$aNode['text'] = array_shift($aParagraphs);
$aNewTree[] = $aNode;
while(end($aOpenTags) && (isset($this->aTags[end($aOpenTags)]['single']) === false || $this->aTags[end($aOpenTags)]['single'] !== true))
{
$aNewTree[] = $this->createCloseTag('[/'.end($aOpenTags).']');
array_pop($aOpenTags);
}
$aNode['text'] = "\\n\\n".implode("\\n\\n", $aParagraphs);
$aNewTree[] = $aNode;
}
else
{
$aNode['text'] = $aNode['text'];
$aNewTree[] = $aNode;
}
}
else
{
$aNewTree[] = $aNode;
}
}
break;
case 'open':
$bSkip = false;
if(isset($this->aTags[$aNode['tag']]) === false)
{
$aNode['type'] = 'text';
if($aNode['type'] == 'text')
{
$aNode['text'] = $aPrevNode['text'].$aNode['text'];
array_pop($aNewTree);
}
$aNewTree[] = $aNode;
}
else
{
# Is de tag wel toegestaan binnen zijn parent?
if(empty($aOpenTags) === true || $this->isAllowed(end($aOpenTags), $aNode['tag']) === true)
{
# Voorkomt dat een inline element om een block element komt te staan.
if(isset($this->aTags[end($aOpenTags)]) === true && $this->aTags[end($aOpenTags)]['inline'] === true && $this->aTags[$aNode['tag']]['inline'] === false)
{
array_pop($aNewTree);
array_pop($aOpenTags);
continue;
}
# Zorgt ervoor dat alle tags worden gesloten bij een nieuw block element.
if(isset($this->aTags[$aNode['tag']]['solid']) === true && $this->aTags[$aNode['tag']]['solid'] === true)
{
$bSolidOpened = true;
while(end($aOpenTags) && (isset($this->aTags[end($aOpenTags)]['single']) === false || $this->aTags[end($aOpenTags)]['single'] !== true) && isset($this->aTags[end($aOpenTags)]['master']) === false)
{
$aNewTree[] = $this->createCloseTag('[/'.end($aOpenTags).']');
array_pop($aOpenTags);
}
}
$vParent = $this->parentNeeded(end($aOpenTags), $aNode['tag']);
$vChild = $this->childNeeded(end($aOpenTags), $aNode['tag']);
# Omdat we het gebruik van parent willen verkomen moeten we kijken of de vorige node niet al de juiste parent heeft aangemaakt.
if(is_bool($vParent) === false)
{
if($aNode['tag'] == end($aOpenTags))
{
$aNewTree[] = $this->createCloseTag('[/'.$aNode['tag'].']');
array_pop($aOpenTags);
}
else
{
array_unshift($this->aTree, $aNode);
array_unshift($this->aTree, $vParent);
$bSkip = true;
}
}
# Is een child noodzakelijk?
if(is_bool($vChild) === false)
{
array_unshift($this->aTree, $aNode);
array_unshift($this->aTree, $vChild);
$bSkip = true;
}
if($bSkip === false)
{
# Voeg de tag toe aan de nieuwe tree.
$aNewTree[] = $aNode;
$aOpenTags[] = $aNode['tag'];
}
}
}
break;
case 'close':
# Als er niks tussen de tags staat, haal ze dan weg.
if($aPrevNode['type'] == 'open' && $aPrevNode['tag'] == $aNode['tag'])
{
array_pop($aNewTree);
array_pop($aOpenTags);
}
else
{
# Is de sluitende tag wel gelijk aan de geopende?
if(empty($aOpenTags) === false && ($aNode['tag'] == end($aOpenTags)))
{
if(in_array($aNode['tag'], $aOpenTags) === true)
{
$aNewTree[] = $aNode;
array_pop($aOpenTags);
}
}
else if(isset($this->aTags[$aNode['tag']]) === false)
{
$aNode['type'] = 'text';
if($aNode['type'] == 'text')
{
$aNode['text'] = $aPrevNode['text'].$aNode['text'];
array_pop($aNewTree);
}
$aNewTree[] = $aNode;
}
}
break;
}
$aNode = array_shift($this->aTree);
}
# Sluit alle nog open tags.
while(end($aOpenTags) && (isset($this->aTags[end($aOpenTags)]['single']) === false || $this->aTags[end($aOpenTags)]['single'] !== true))
{
$aNewTree[] = $this->createCloseTag('[/'.end($aOpenTags).']');
array_pop($aOpenTags);
}
# Update de oude tree.
$this->aTree = $aNewTree;
}
private function childNeeded($sParent, $sChild)
{
# Er zijn geen verplichte childs.
if(isset($this->aTags[$sParent]['child']) === false)
{
return false;
}
# De nieuwe child is al in orde.
if(in_array($sChild, $this->aTags[$sParent]['child']) === true)
{
return false;
}
# Een child is nodig, dus moet er een worden toegevoegd.
return $this->createOpenTag('['.$this->aTags[$sParent]['child'][0].']');
}
private function parentNeeded($sParent, $sChild)
{
# Er zijn geen verplichte parents.
if(isset($this->aTags[$sChild]['parent']) === false)
{
return false;
}
# De parent is al in orde.
if(in_array($sParent, $this->aTags[$sChild]['parent']) === true)
{
return false;
}
# Een andere parent is nodig, dus moet er een worden toegevoegd.
return $this->createOpenTag('['.$this->aTags[$sChild]['parent'][0].']');
}
private function isAllowed($sParent, $sChild)
{
# Alle tags zijn toegestaan.
if($this->aTags[$sParent]['allowed'] == 'all')
{
return true;
}
# Geen enkele tag is binnen deze tag toegestaan.
if($this->aTags[$sParent]['allowed'] == 'none')
{
return false;
}
# Leest de argumenten uit.
$aArguments = explode('^', $this->aTags[$sChild]['allowed']);
$aTags = explode(',', $aArguments[1]);
# Kijkt of de child aan de eisen voldoet.
if($aArguments[0] == 'none' && in_array($sChild, $aTags) === true)
{
return true;
}
if($aArguments[0] == 'all' && in_array($sChild, $aTags) === true)
{
return false;
}
return false;
}
# Zodat de parser weet of hij zich in het eerste level bevindt of niet.
private $iLevel = 0;
# Zorgt ervoor dat de parse functie ook werkt binnen ubb methodes die je zelf aan heb gemaakt.
private $bChangeSolid = false;
private function parse($sStopTag = '')
{
# Als er niks is om te parsen.
if(empty($this->aTree) === true)
{
return '';
}
# De tag van het block type dat open is.
$aOpen = array();
# De hoeveelheid tags er van het type block en hetzelfde zijn die open zijn.
$iOpen = 0;
# Wat het vorige type was.
$bSolid = false;
# Het type van het huidige element.
$bNodeInline = false;
# De counter voor de keys van de text array.
$i = 0;
$iLevel = $this->iLevel;
$this->iLevel++;
$sText = '';
while($aNode = array_shift($this->aTree))
{
if($this->bParagraphise === true && $iLevel == 0)
{
# Is het een tag?
if(isset($aNode['tag']) === true && isset($this->aTags[$aNode['tag']]) === true)
{
# Is geen tag open van het type block?
if(empty($aOpen) === true)
{
$bNodeInline = $this->aTags[$aNode['tag']]['solid'];
}
}
else if($aNode['type'] == 'text' && empty($aOpen) === true && empty($aNode['text']) === false)
{
$bNodeInline = false;
}
# Stopt de tekst als een item in de tekst array.
if($bSolid !== $bNodeInline && $this->realEmpty($sText) === false)
{
$this->aText[($bSolid === false ? 'i' : 'b').$i] = trim($sText);
$sText = '';
$i++;
}
# Update het vorige type naar het huidige.
$bSolid = $bNodeInline;
}
if(isset($aNode['tag']) === true && isset($this->aTags[$aNode['tag']]) === true)
{
if($aNode['tag'] == $sStopTag)
{
$this->bChangeSolid = true;
return $sText;
}
else
{
if(method_exists($this, $aNode['tag']) === true)
{
if($this->bParse === true)
{
$sText .= $this->$aNode['tag']($aNode);
}
else
{
$sText .= $aNode['text'];
}
}
}
}
else
{
$sText .= $aNode['text'];
}
# Zet de tekst in array formaat als dit aan staat.
if($this->bParagraphise === true && $iLevel == 0)
{
# Is het een tag?
if(isset($aNode['tag']) === true && isset($this->aTags[$aNode['tag']]) === true)
{
if(isset($this->aTags[$aNode['tag']]['master']) === true)
{
$this->aText['b'.$i] = trim($sText);
$sText = '';
$i++;
$aOpen = array();
$iOpen = 0;
}
else
{
# Er is een tag geopend van het type block.
if($aNode['type'] == 'open' && $this->aTags[$aNode['tag']]['solid'] === true)
{
if(empty($aOpen) === true)
{
$aOpen = $aNode;
}
# Is het dezelfde tag als de geen die al open is?
if($aNode['tag'] == $aOpen['tag'])
{
$iOpen++;
}
}
# Er is een tag gesloten van het type block.
if(($aNode['type'] == 'close' && $this->aTags[$aNode['tag']]['solid'] === true && empty($aOpen) === false && $aNode['tag'] == $aOpen['tag']) || $this->bChangeSolid === true)
{
$this->bChangeSolid = false;
$iOpen--;
if($iOpen == 0 && $this->realEmpty($sText) === false)
{
$this->aText[($bSolid === false ? 'i' : 'b').$i] = trim($sText);
$sText = '';
$i++;
$aOpen = array();
}
}
}
}
}
}
# Als paragrafen aanmaken aanstaat, maak dan paragrafen om de teksten.
if($this->bParagraphise === true && $iLevel == 0)
{
# Het overgebleven geparsde tekst toevoegen aan de tekst array.
if($this->realEmpty($sText) === false)
{
$this->aText[($bSolid === false ? 'i' : 'b').$i] = $sText;
}
# Zorg ervoor dat er paragrafen komen.
$this->sText = '';
foreach($this->aText as $sKey => $sText)
{
if($sKey{0} == 'i')
{
$this->sText .= $this->paragraphise($sText);
}
else
{
$this->sText .= $sText;
}
}
}
else
{
$this->sText = $sText;
}
}
private function paragraphise($sText)
{
$aParagraphs = explode("\\n\\n", $sText);
$sText = '';
foreach($aParagraphs as $sParagraph)
{
$sText .= '<p>'.str_replace("\\n", '<br />', trim($sParagraph)).'</p>';
}
return $sText;
}
private function basic($aNode, $sTag)
{
if($aNode['type'] == 'open')
{
return '<'.$sTag.'>';
}
else
{
return '</'.$sTag.'>';
}
}
private function b($aNode)
{
return $this->basic($aNode, 'strong');
}
private function i($aNode)
{
return $this->basic($aNode, 'em');
}
private function u($aNode)
{
return $this->basic($aNode, 'ins');
}
private function s($aNode)
{
return $this->basic($aNode, 'del');
}
private function sub($aNode)
{
return $this->basic($aNode, 'sub');
}
private function sup($aNode)
{
return $this->basic($aNode, 'sup');
}
private function a($aNode)
{
if($aNode['type'] == 'open')
{
if(isset($aNode['attributes'][$aNode['tag']]) === true)
{
$sHref = $aNode['attributes'][$aNode['tag']];
if(strpos($sHref, 'http://') === false)
{
$sHref = 'http://'.$sHref;
}
}
else
{
$sHref = '#';
}
return '<a href="'.$sHref.'">';
}
else
{
return '</a>';
}
}
private function search($aNode)
{
$sSearch = $this->parse('search');
return 'Google zoekopdracht: <a href="http://www.google.nl/search?q='.$sSearch.'">'.$sSearch.'</a>';
}
private function h2($aNode)
{
return $this->basic($aNode, 'h2');
}
private function h3($aNode)
{
return $this->basic($aNode, 'h3');
}
private function h4($aNode)
{
return $this->basic($aNode, 'h4');
}
private function h5($aNode)
{
return $this->basic($aNode, 'h5');
}
private function h6($aNode)
{
return $this->basic($aNode, 'h6');
}
private function ol($aNode)
{
return $this->basic($aNode, 'ol');
}
private function ul($aNode)
{
return $this->basic($aNode, 'ul');
}
private function li($aNode)
{
return $this->basic($aNode, 'li');
}
private function table($aNode)
{
return $this->basic($aNode, 'table');
}
private function tr($aNode)
{
return $this->basic($aNode, 'tr');
}
private function th($aNode)
{
return $this->basic($aNode, 'th');
}
private function td($aNode)
{
return $this->basic($aNode, 'td');
}
private function img($aNode)
{
$this->bParse = false;
$sIMG = $this->parse('img');
$this->bParse = true;
return '<img src="'.stripslashes($sIMG).'" alt="Kon niet weergegeven worden" />';
}
private function hr($aNode)
{
if($aNode['type'] == 'open')
{
return '<hr />';
}
}
private function blockquote($aNode)
{
return $this->basic($aNode, 'blockquote');
}
private function code($aNode)
{
$this->bParse = false;
$sCode = $this->parse('code');
$this->bParse = true;
$sCode = str_replace("<br>", "", $sCode);
$sCode = str_replace("<br />", "", $sCode);
$sCode = str_replace("&gt;", ">", $sCode);
$sCode = str_replace("&lt;", "<", $sCode);
$sCode = str_replace("&quot;", "\\"", $sCode);
$sCode = str_replace("&amp;", "&", $sCode);
$sCode = str_replace('$', '\\$', $sCode);
$sCode = str_replace('\\n', '\\\\\\\\n', $sCode);
$sCode = str_replace('\\r', '\\\\\\\\r', $sCode);
$sCode = str_replace('\\t', '\\\\\\\\t', $sCode);
$sCode = str_replace(':)', ':)', $sCode);
$lines = explode(PHP_EOL, trim($sCode));
$i=1;
$code = '<div class="Code">
<div class="Code_Titel">PHP code</div>
<div class="Left">';
foreach ($lines as $line)
{
$code .= $i.'<br />';
$i++;
}
$code .= '</div>
<div class="Right">
'.highlight_string(stripslashes(trim($sCode)), true).'
</div>
</div>';
return $code;
}
static public function execute($sText)
{
$oParser = new ubb($sText);
$oParser->cleanWhiteSpace();
$oParser->getPieces();
$oParser->createTree();
$oParser->validateTree();
$oParser->parse();
echo $oParser->sText;
return noXSS($oParser->sText);
}
}
function noXSS($sString)
{
return stripslashes(preg_replace('#(href|src)=(\\'|\\")(.+?)(\\'|\\")( |>)#ise', "'\\\\1=\\\\2'.removeJavascript('\\\\3').'\\\\4\\\\5'", $sString));
}
function removeJavascript($sString)
{
$sReturn = '#';
$sCheck = str_replace(array("\\n", ' '), '', stripslashes(strip_tags(html_entity_decode(trim(strtolower($sString))))));
$bEvil = false;
$aEvil = array('javascript:', 'onfocus=', 'onblur=', 'onclick=', 'ondblclick=', 'onmousedown=', 'onmouseup=', 'onmouseover=', 'onmousemove=', 'onmouseout=', 'onkeypress=', 'onkeydown=', 'onkeyup=');
foreach($aEvil as $sEvil)
{
if(strpos($sCheck, $sEvil) !== false)
{
$bEvil = true;
break;
}
}
if($bEvil === false)
{
$sReturn = $sString;
}
return $sReturn;
}
?>
Toon Meer