Ne znam jel' nekome potrebalo, jel' neko sam dodao sitnice koje su falile, kako god, evo završene varijante koja:
- neće da propusti dalje ukoliko ima teksta a taj tekst nije u sklopu validnih tagova koji smeju da se nađu unutar head taga ili je tekst mimo html komentara.
- neće da propusti dalje ukoliko nisu u pitanju samo dozvoljeni head tagovi te ako ima php ili xml tagova (u svim varijantama)
- neće da propusti dalje ukoliko su tagovi UPPERCASE (Mala slova i html5 bre. Ko još piše html tagove velikim slovima .. ? Mis'im ima.. ali.. whatever. To je mikrosekunda posla da se doda, al' ja to neću da radim)
ono što je izmenjeno/dodato:
<?php
// Deo pre prve petlje
$details=preg_replace('/\s+/', ' ', $textarea);
$details=str_replace('><','> <', $details);
$details=str_replace('> </','></', $details);
$details=str_replace('> <','>:*:<', $details);
$details=(explode(':*:', $details));
// I mala izmena u prvoj petlji
foreach($details as $i=>$line)
{
$checkLine=trim($details[$i]);
if((strpos($checkLine, $allowedTags[0]) === false
&& substr_count($checkLine, '>') > 2)
|| substr($checkLine,0,1) !=='<'
|| substr($checkLine,-1) !== '>')
{
$HAAAALT = true;
break;
}
else
{
$checkLine=str_replace('>',' ', $details[$i]);
$checkLine=explode(' ', $checkLine);
$checkLine=trim($checkLine[0]);
$cl[]=$checkLine;
}
}
Sve ostalo je isto.
Evo i komplet metode u svom završnom izdanju:
(pozivanje i provera se vrši na isti način kao što je prehodno opisano)
<?php
// Sanitize allowed head tags for meta area before writing
protected function NotAllowedTagsForHead($textarea)
{
// HTML tags
$cl=array();
$counter = array();
$allowedTags = array (
'<!--',
'<title',
'<style',
'<base',
'<link',
'<meta',
'<script',
'<noscript',
);
$details=preg_replace('/\s+/', ' ', $textarea);
$details=str_replace('><','> <', $details);
$details=str_replace('> </','></', $details);
$details=str_replace('> <','>:*:<', $details);
$details=(explode(':*:', $details));
if(count($details)>=1)
{
foreach($details as $i=>$line)
{
$checkLine=trim($details[$i]);
if((strpos($checkLine, $allowedTags[0]) === false
&& substr_count($checkLine, '>') > 2)
|| substr($checkLine,0,1) !=='<'
|| substr($checkLine,-1) !== '>')
{
$HAAAALT = true;
break;
}
else
{
$checkLine=str_replace('>',' ', $details[$i]);
$checkLine=explode(' ', $checkLine);
$checkLine=trim($checkLine[0]);
$cl[]=$checkLine;
}
}
unset($i, $line, $details, $checkLine);
foreach($cl as $suspect):
if(in_array($suspect, $allowedTags)):
else: $HAAAALT = true; break; endif;
endforeach;
unset($cl, $suspect);
}
else unset($cl, $details);
if(!isset($HAAAALT))
{
foreach($allowedTags as $value):
if(strpos($textarea, $value) !== false):
$counter[]=true; else: endif;
unset($value);
endforeach;
unset($allowedTags);
$HAAAALT=(
(count($counter)<1 || empty($counter)) ?
true:false
);
}
// SS/xml tags piece start
foreach( array (
'fullphp' => '<?php',
'shortphp' => '<?',
'aspphp' => '<%',
'shortecho' => '<?=',
'endasp' => '%>',
'endtag' => '?>'
) as $var=>$value):
$$var=strpos($textarea, $value);
unset($value, $var);
endforeach;
$serverSideSTOP=(
in_array(true,
array($fullphp, $shortphp, $aspphp, $shortecho, $endasp, $endtag)) ?
true:false
);
// SS/xml tags piece end
return ( (trim($textarea) !== '' && $HAAAALT || $serverSideSTOP ) ? true:false );
}
[Ovu poruku je menjao plus_minus dana 19.04.2015. u 22:55 GMT+1]