Please help me on the following: Disallowing HTML tags
Hello world,
As you can see in the first function i use strip_tags to disallow html tags but now when it's used as links etc it will till show the text behind the link like a href="google">123< it shows the 123 i want it so when the function is called it won't post the message/feedback
The Function:
PHP Code:
Function filter($bericht)
{
return strip_tags($bericht);
}
The part wich ads the information to the database:
PHP Code:
if (!empty($bericht)) {
$lul = filter($this->db->quote($bericht));
$sql = 'insert into recensies set
profiel = '.$this->db->quote($id).',
naam = '.$this->db->quote($naam).',
tijd = '.$this->db->quote(time()).',
ip = '.$this->db->quote($_SERVER['REMOTE_ADDR']).',
bericht = '.$lul;
$this->db->execute($sql);
$this->reset_alle_recensies();
}
}
Whole script:
PHP Code:
<?php
Function filter($bericht)
{
return strip_tags($bericht);
}
class mod_recensies_exception extends Exception {
const database_error = 1000;
const toevoegen_geen_profiel = 11;
const toevoegen_geen_cijfer = 14;
const toevoegen_leeg = 12;
const toevoegen_dubbel = 13;
const bewerken_geen_id = 20;
const bewerken_geen_profiel = 21;
const bewerken_geen_cijfer = 23;
const bewerken_leeg = 22;
}
class mod_recensies extends ws_module
{
/**
* @var array $alle_recensies Cache van alle recensies
*/
private $alle_recensies = null;
private $recensies_by_user = array();
/**
* @var array $recensies Array met alle recensies gelinkt aan de verschillende profielen.
*/
private $recensies;
/**
* Instance bewaard een enkele instance van ws_module
*
* Variabele wordt gebruikt voor een singleton pattern. Bewaard een enkele
* instance van ws_module
*
* @var ws_session $instance Singleton instance van ws_module
*/
private static $instance;
/**
* Implementatie van het singleton pattern.
* Stuurt een enkele instantie van de user class terug.
* Voor gebruik bij de ingelogde gebruiker
*
* Deze class kan ook normaal gestart worden zonder singleton
* Eigenschappen. Deze normaal gestarte klassen worden niet
* bij de singleton betrokken.
*
* @return ws_session Enkele instantie van user class.
*/
public static function singleton()
{
if (!isset(self::$instance)) {
$class_name = __CLASS__;
self::$instance = new $class_name();
}
return self::$instance;
}
public function __construct()
{
parent::__construct();
}
/**
* get_recensies()
*
* Stuurt een array van de recensies van het profiel terug
* @see $recensies
*/
public function get_recensies($id)
{
if (!is_array($this->recensies) or !array_key_exists($id,$this->recensies) or is_null($this->recensies[$id])) {
$result = $this->db->execute("select ra.*,r.* from recensies r left join ratings ra on ra.recensie = r.id where r.profiel = ".$this->db->quote($id).' order by r.id desc');
$recensies = array();
if ($result !== false) {
while($row = $result->fetchRow()){
$recensies[$row['id']] = $row;
}
}
$result->free();
$this->recensies[$id] = $recensies;
}
return $this->recensies[$id];
}
/**
* reset_recensies()
*
* Reset de recensies van het profiel zodat deze uit de database gehaald worden bij de volgende vraag.
* @see $recensies,$hoofdrecensies,get_recensies(),get_hoofdrecensies()
*/
public function reset_recensies($id)
{
if ($id) {
$this->recensies[$id] = null;
}
}
/**
* reset_alle_profielen()
*
* Reset de recensies zodat deze uit de database gehaald worden bij de volgende vraag.
* @see $recensies
*/
public function reset_alle_profielen()
{
$this->recensies = null;
}
/**
* get_alle_recensies()
*
* Stuurt een array van alle recensies in de database
*/
public function get_alle_recensies()
{
if (!is_array($this->alle_recensies)) {
$this->alle_recensies = array();
$result = $this->db->execute("select r.* from recensies r left join ratings ra on ra.recensie = r.id order by r.tijd asc");
if ($result !== false) {
while($row = $result->fetchRow()){
$this->alle_recensies[$row['id']] = $row;
}
}
$result->free();
}
return $this->alle_recensies;
}
/**
* reset_alle_recensies()
*
* reset de alle_recensies array zodat deze de volgende keer opnieuw uit de database gehaald worden.
* @uses mod_recensies::reset_alle_profielen()
*/
public function reset_alle_recensies()
{
$this->alle_recensies = null;
$this->reset_alle_profielen();
}
/**
* recensie_toevoegen()
*
* Voeg een tag toe aan de database
* @uses mod_recensies::reset_alle_recensies()
*/
public function recensie_toevoegen($id,$naam,$bericht)
{
$naam = trim(strip_tags($naam));
if (empty($naam)) $naam = 'Anoniem';
if (!is_numeric($id)) {
throw new mod_recensies_exception('Profiel identifictie nummer ongeldig',mod_recensies_exception::toevoegen_geen_profiel);
}
$purifier = ws_htmlpurifier::singleton('recensie');
$bericht = trim($purifier->purify($bericht));
if (empty($bericht)) {
throw new mod_recensies_exception('Er mag geen lege recensie worden toegevoegd',mod_recensies_exception::toevoegen_leeg);
}
/*
$result = $this->db->execute('select count(*) as count from recensies where
profiel = '.$this->db->quote($id).' and
naam = '.$this->db->quote($naam).' and
cijfer = '.$this->db->quote($cijfer).' and
bericht = '.$this->db->quote($bericht));
if ($result !== false) {
$count = $result->fetchRow();
if ($count['count'] > 0) {
throw new mod_recensies_exception( 'Er mag niet twee keer dezelfde recensie worden opgegeven.',mod_recensies_exception::toevoegen_dubbel);
}
} else {
throw new mod_recensies_exception('Kon dubbel toevoegen van recensie niet controleren in de database',mod_recensies_exception::database_error);
}
*/
if (!empty($bericht)) {
$lul = filter($this->db->quote($bericht));
$sql = 'insert into recensies set
profiel = '.$this->db->quote($id).',
naam = '.$this->db->quote($naam).',
tijd = '.$this->db->quote(time()).',
ip = '.$this->db->quote($_SERVER['REMOTE_ADDR']).',
bericht = '.$lul;
$this->db->execute($sql);
$this->reset_alle_recensies();
}
}
/**
* cijfer_toevoegen()
*
* Voeg een tag toe aan de database
* @uses mod_recensies::reset_alle_recensies()
*/
public function cijfer_toevoegen($id,$cijfer,$recensie = null)
{
if (!is_numeric($id)) {
throw new mod_recensies_exception('Profiel identifictie nummer ongeldig',mod_recensies_exception::toevoegen_geen_profiel);
}
if (!is_numeric($cijfer)) {
throw new mod_recensies_exception('Het is verplicht om een cijfer te geven',mod_recensies_exception::toevoegen_geen_cijfer);
}
$sql = 'insert into ratings set
profiel = '.$this->db->quote($id).',
cijfer = '.$this->db->quote($cijfer);
if ($recensie !== null and is_numeric($recensie)) {
$sql .= ', recensie = '.$this->db->quote($recensie);
}
$result = $this->db->execute($sql);
$this->reset_alle_recensies();
}
/**
* recensie_bewerken()
*
* Bewerk een recensie
* @uses mod_recensies::reset_alle_recensies()
*/
public function recensie_bewerken($id,$profiel,$naam,$bericht,$cijfer)
{
$naam = trim(strip_tags($naam));
if (empty($naam)) $naam = 'Anoniem';
if (!is_numeric($id)) {
throw new mod_recensies_exception('Recensie identifictie nummer ongeldig',mod_recensies_exception::bewerken_geen_id);
}
if (!is_numeric($profiel)) {
throw new mod_recensies_exception('Profiel identifictie nummer ongeldig',mod_recensies_exception::bewerken_geen_profiel);
}
if (!is_numeric($cijfer) or $cijfer < 1 or $cijfer > 5) {
throw new mod_recensies_exception('Het is verplicht om een cijfer te geven',mod_recensies_exception::bewerken_geen_cijfer);
}
$purifier = ws_htmlpurifier::singleton('recensie');
$bericht = trim($purifier->purify($bericht));
/*if (empty($bericht)) {
throw new mod_recensies_exception('De recensie mag niet leeg zijn',mod_recensies_exception::bewerken_leeg);
}*/
$sql = 'update recensies set
profiel = '.$this->db->quote($profiel).',
naam = '.$this->db->quote($naam).',
tijd = '.$this->db->quote(time()).',
ip = '.$this->db->quote($_SERVER['REMOTE_ADDR']).',
bericht = '.$this->db->quote($bericht).',
where id = '.$this->db->quote($id);
$result = $this->db->execute($sql);
$this->cijfer_bewerken($profiel,$cijfer,$id);
$this->reset_alle_recensies();
}
/**
* cijfer_bewerken()
*
* Bewerk een cijfer
* @uses mod_recensies::reset_alle_recensies()
*/
public function cijfer_bewerken($profiel,$cijfer,$recensie)
{
if (!is_numeric($recensie)) {
throw new mod_recensies_exception('Recensie identifictie nummer ongeldig',mod_recensies_exception::bewerken_geen_id);
}
if (!is_numeric($profiel)) {
throw new mod_recensies_exception('Profiel identifictie nummer ongeldig',mod_recensies_exception::bewerken_geen_profiel);
}
if (!is_numeric($cijfer) or $cijfer < 1 or $cijfer > 5) {
throw new mod_recensies_exception('Het is verplicht om een cijfer te geven',mod_recensies_exception::bewerken_geen_cijfer);
}
$sql = 'update ratings set
profiel = '.$this->db->quote($profiel).',
cijfer = '.$this->db->quote($cijfer).',
where recensie = '.$this->db->quote($recensie);
$result = $this->db->execute($sql);
$this->reset_alle_recensies();
}
/**
* recensie_wissen()
*
* Wis een recensie
* @uses mod_recensies::reset_alle_recensies()
*/
public function recensie_wissen($recensies)
{
if (!is_array($recensies)) {
$this->db->execute('delete from recensies where id = '.$this->db->quote($recensies));
} else {
$id = array();
$tag = array();
foreach ($recensies as $value) {
if (is_numeric($value)) {
$id[] = 'id = '.$this->db->quote($value);
$tag[] = 'recensie = '.$this->db->quote($value);
}
}
if (count($id) > 0) {
$this->db->execute('delete from recensies where '.implode(' or ',$id));
}
}
$this->cijfer_wissen($recensies);
$this->reset_alle_recensies();
}
/**
* cijfer_wissen()
*
* Wis een cijfer
* @uses mod_recensies::reset_alle_recensies()
*/
public function cijfer_wissen($recensies)
{
if (!is_array($recensies)) {
$this->db->execute('delete from ratings where recensie = '.$this->db->quote($recensies));
} else {
$id = array();
$tag = array();
foreach ($recensies as $value) {
if (is_numeric($value)) {
$tag[] = 'recensie = '.$this->db->quote($value);
}
}
if (count($id) > 0) {
$this->db->execute('delete from ratings where '.implode(' or ',$id));
}
}
$this->reset_alle_recensies();
}
public function __destruct()
{
parent::__destruct();
}
}
?>
Re: Please help me on the following: Disallowing HTML tags
You could add a Regex rule to 'sort' the content.
Posted via Mobile Device
Re: Please help me on the following: Disallowing HTML tags
PHP Code:
<?php
Function filter($bericht)
{
$invert = FALSE; // FALSE= strip all but $tags / TRUE= strip only $tags
$tags = ''; // Tags to strip/ingore (see $invert)
preg_match_all('/<(.+?)[\s]*\/?[\s]*>/si', trim($tags), $tags);
$tags = array_unique($tags[1]);
if(is_array($tags) AND count($tags) > 0) {
if($invert == FALSE) {
return preg_replace('@<(?!(?:'. implode('|', $tags) .')\b)(\w+)\b.*?>.*?</\1>@si', '', $bericht);
}
else {
return preg_replace('@<('. implode('|', $tags) .')\b.*?>.*?</\1>@si', '', $bericht);
}
}
elseif($invert == FALSE) {
return preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $bericht);
}
return $bericht;
}
echo filter('<p>This</p> is a bunch of <a href="http://www.google.com">random</a> words.');
?>
Credit: PHP: strip_tags - Manual
Re: Please help me on the following: Disallowing HTML tags
Very simple.
If you must completely exclude HTML (as all the function above does is escape it, thus securing it when being displayed later), then you could compare the normal version of the variable to the one that has htmlentities used on it already.
If you want to be more selective, then you may need custom functions.
Re: Please help me on the following: Disallowing HTML tags
Re: Please help me on the following: Disallowing HTML tags
Code:
function filter($bericht)
{
$cmpStr = strip_tags($bericht);
if($bericht != $cmpStr)
{
return false;
}
return $bericht;
}
Works ;)
Re: Please help me on the following: Disallowing HTML tags
Quote:
Originally Posted by
Touchwise
Code:
function filter($bericht)
{
$cmpStr = strip_tags($bericht);
if($bericht != $cmpStr)
{
return false;
}
return $bericht;
}
Works ;)
Thats great, well i always use strip_tags ;)