Scripts

Tekst filter

Een filter voor het veilig plaatsen van ingevoerde content. Voorbeelden: - Filteren van 1 string De tags en zijn dus toegestaan. - Filteren van bijvoorbeeld een post array

Escape.php
<?php

/**
 * Gestione_Escape voor het filteren van de opgegeven tekst. Het voordeel boven de htmlspecialchars, htmlenities
 * van PHP is dat je bepaalde tags kan toevoegen die worden genegeerd bij het filter van de tekst.
 *
 * @author 		Niels Kieviet <[email protected]>
 * @version 	$Revision v1.00$
 * @copyright	Copyright (c) 2011, Niels Kieviet
 */
class Gestione_Escape {
	
	/**
	 * Toegestane tags
	 * 
	 * @access	Protected
	 * @var		Array
	 */
	protected $allowedTags = array();
	
	/**
	 * Methode voor het parsen van de opgegeven tekst
	 * 
	 * @param	String	$text
	 * @param	Array	$flags (Opt)
	 * @access	Public
	 * @return	String
	 */
	public static function parse($text, $tags = null) {
		if (is_array($tags) && count($tags) > 0) {
			self::setAllowedTags($tags);
			$tags = implode('', $this->allowedTags);
		}
		
		// Speciale characters omzetten
		$search = array('/&lsquo;/u', '/&rsquo;/u', '/&ldquo;/u', '/&rdquo;/u', '/&mdash;/u');
       	$replace = array('\'', '\'', '"', '"', '-');
       	$text = preg_replace($search, $replace, $text);
       	
       	// html entities omzetten naar UTF-8
		$text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
		
		// Alle /* .. */ style commentaren omzetten naar normale HTML commentaar
		if (mb_stripos($text, '/*') !== false) {
			$text = mb_eregi_replace('#/\*.*?\*/#s', '', $text, 'm');
		}
		
		// Witruimte voor wiskundige berekeningen
		$text = preg_replace(array('/<([0-9]+)/'), array('< $1'), $text);
		$text = strip_tags($text, $tags);
		
		// Meerdere spaties achterelkaar terug zetten naar één
		$text = preg_replace(array('/^\s\s+/', '/\s\s+$/', '/\s\s+/u'), array('', '', ' '), $text);

		// Verwijder inline css en css tags
		$search = array('#<(strong|b)[^>]*>(.*?)</(strong|b)>#isu', '#<(em|i)[^>]*>(.*?)</(em|i)>#isu', '#<u[^>]*>(.*?)</u>#isu');
        $replace = array('<b>$2</b>', '<i>$2</i>', '<u>$1</u>');
        $text = preg_replace($search, $replace, $text); 
        
        // Sommige MS Style definities geven rare tekens mee waarneer een tekst wordt gekopieerd
        $numMatches = preg_match_all('/\<!--/u', $text, $matches);
        if ($numMatches) {
        	$text = preg_replace('/\<!--(.)*--\>/isu', '', $text);
        }
        
        // Retourneer de gefilterde tekst
        return $text; 
	}
	
	/**
	 * Methode voor het parsen van een array
	 * 
	 * @param	Array	$text
	 * @access	Public
	 * @return	Array
	 */
	public static function parseFromArray(array $text = array(), $tags = null) {
		if (is_array($tags) && count($tags) > 0) {
			$this->setAllowedTags($tags);
			$tags = implode('', $this->allowedTags);
		}
		$rText = array();
     	if (is_array($text) && count($text) > 0) {
        	foreach ($text as $columnName => $value) {
            	$pText = self::parse($value, $tags);
            	$rText[$columnName] = $pText;
        	}
        }
		return $rText;
	}
	
	/**
	 * Methode voor het zetten van toegestane tags
	 * 
	 * @param 	Array $tags
	 * @access	Public
	 * @return 	Void
	 */
	public function setAllowedTags($tags) {
		if (is_array($tags) && count($tags) > 0) {
			foreach ($tags as $tag) {
				if (!in_array($tag, $this->allowedTags)) {
					$this->allowedTags[] = $tag;
				}
			}	
		}
	}
	
	/**
	 * Methode voor het verwijderen van alle tags die zijn toegestaan
	 * 
	 * @access	Public
	 * @return	Void
	 */
	public function clearAllAllowedTags() {
		$this->allowedTags = array();
	}
	
	/**
	 * Methode voor het verwijderen van toegestane tags
	 * 
	 * @param	Array	$tag
	 * @access	Public
	 * @return	Void
	 */
	public function clearAllowedTags($tags) {
		if (is_array($tags) && count($tags) > 0) {
			foreach ($tags as $tag) {
				if (in_array($tag, $this->allowedTags)) {
					unset($this->allowedTags[$tag]);
				}
			}	
		}
	}
	
	/**
	 * Methode voor het verwijderen van een toegestane tag
	 * 
	 * @param	String	$tag
	 * @access	Public
	 * @return	Void
	 */
	public function clearAllowedTag($tag) {
		if (is_string($tag) && !is_null($tag)) {
			unset($this->allowedTags[$tag]);
		}
	}
} 

Reacties

0
Nog geen reacties.