Created
October 11, 2009 20:10
-
-
Save laszlokorte/207850 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
final class Secure | |
{ | |
const LINEBREAK_WIDTH = 50; | |
private static $charset = 'utf-8'; | |
/* | |
* | |
* Alle HTML-Befehlszeichen aus einem String in HTML-Entities umwandeln | |
* | |
*/ | |
public static function html($string) | |
{ | |
return htmlentities($string, ENT_QUOTES, self::$charset); | |
} | |
/* | |
* | |
* Nach allen 50 Zeichen ohne Zeilenumbruch einen solchen einfügen | |
* | |
*/ | |
public static function linebreak($string) | |
{ | |
return $replace = preg_replace("/([^\s*?]{" . self::LINEBREAK_WIDTH . "})(?![^<]+>|[^&]*;)/","\\0 ", $string); | |
} | |
/* | |
* | |
* HTML-Entities wieder echtes HTML machen | |
* | |
*/ | |
public static function rebuild_html($string) | |
{ | |
return html_entity_decode($string, ENT_QUOTES, self::$charset); | |
} | |
} | |
/* | |
* | |
* Pattern Klasse | |
* | |
*/ | |
final class AbcodePattern | |
{ | |
const SIMPLE_STRING_TYPE = 0; | |
const PREG_STRING_TYPE = 1; | |
const PREG_CALLBACK_TYPE = 2; | |
private $name; | |
private $pattern; | |
private $action; | |
private $order; | |
private $type; | |
private $recursive; | |
public function __construct($name, $pattern, $action, $order = 0, $recursive = FALSE, $regex = TRUE) | |
{ | |
$this->name = $name; | |
$this->pattern = $pattern; | |
$this->action = $action; | |
$this->order = $order; | |
$this->recursive = $recursive; | |
if(is_object($action)) | |
{ | |
$this->type = self::PREG_CALLBACK_TYPE; | |
} | |
elseif(is_string($action)) | |
{ | |
if(substr ($action, -2) === '()') | |
{ | |
$this->type = self::PREG_CALLBACK_TYPE; | |
$this->action = substr ($action, 0, -2); | |
} | |
else | |
{ | |
if($regex===FALSE) | |
{ | |
$this->type = self::SIMPLE_STRING_TYPE; | |
} | |
else | |
{ | |
$this->type = self::PREG_STRING_TYPE; | |
} | |
} | |
} | |
if($regex!==TRUE) | |
{ | |
$this->type *= -1; | |
} | |
} | |
public function apply($string) | |
{ | |
do | |
{ | |
switch($this->type) | |
{ | |
case self::PREG_STRING_TYPE: | |
$string = preg_replace($this->pattern, $this->action, $string); | |
break; | |
case self::SIMPLE_STRING_TYPE: | |
$string = str_replace($this->pattern, $this->action, $string); | |
break; | |
case self::PREG_CALLBACK_TYPE: | |
$string = preg_replace_callback($this->pattern, $this->action, $string); | |
} | |
} | |
while($this->recursive && preg_match($this->pattern, $string)); | |
return $string; | |
} | |
public function filter($string) | |
{ | |
do | |
{ | |
switch($this->type) | |
{ | |
case self::PREG_CALLBACK_TYPE: | |
case self::PREG_STRING_TYPE: | |
$string = preg_replace_callback($this->pattern, "end", $string); | |
break; | |
} | |
} | |
while($this->recursive && preg_match($this->pattern, $string)); | |
return $string; | |
} | |
public function getName() | |
{ | |
return $this->name; | |
} | |
public function getOrder() | |
{ | |
return $this->order; | |
} | |
} | |
final class Abcode | |
{ | |
const VERSION = 0.9; | |
private static $groups = array('base' => array()); | |
private static $patterns = array(); | |
private static $sorted = FALSE; | |
private $active_patterns; | |
public function __construct($group = 'base') | |
{ | |
if($group !== FALSE) | |
{ | |
$this->active_patterns = self::$groups[$group]; | |
} | |
} | |
public function convert($string) | |
{ | |
self::sortPatterns(); | |
foreach(self::$patterns AS $pattern) | |
{ | |
if($this->active_patterns[$pattern->getname()] === TRUE) | |
{ | |
$string = $pattern->apply($string); | |
} | |
} | |
return $string; | |
} | |
public function cleanup($string) | |
{ | |
self::sortPatterns(); | |
foreach(self::$patterns AS $pattern) | |
{ | |
if($this->active_patterns[$pattern->getname()] === TRUE) | |
{ | |
$string = $pattern->filter($string); | |
} | |
} | |
return $string; | |
} | |
public function activatePattern($name) | |
{ | |
$this->active_patterns[$name] = TRUE; | |
} | |
public function deactivatePattern($name) | |
{ | |
$this->active_patterns[$name] = FALSE; | |
} | |
public function clearPatterns() | |
{ | |
foreach($this->active_patterns AS $pattern) | |
{ | |
$pattern = FALSE; | |
} | |
} | |
public function getActivePatterns() | |
{ | |
return $this->active_patterns; | |
} | |
public static function register(AbcodePattern $pattern, $groups = FALSE) | |
{ | |
array_push(self::$patterns, $pattern); | |
if(is_array($groups)) | |
{ | |
foreach($groups AS $name) | |
{ | |
self::$groups[$name][$pattern->getName()] = TRUE; | |
} | |
} | |
elseif($groups === TRUE) | |
{ | |
foreach(self::$groups AS $group => $val) | |
{ | |
self::$groups[$group][$pattern->getName()] = TRUE; | |
} | |
} | |
self::$sorted = FALSE; | |
} | |
public static function sortPatterns() | |
{ | |
if(self::$sorted!==TRUE) | |
{ | |
usort(self::$patterns, array('Abcode','sortPatternsCallback')); | |
self::$sorted = TRUE; | |
} | |
} | |
private static function sortPatternsCallback($patternA, $patternB) | |
{ | |
if ($patternA->getOrder() == $patternB->getOrder()) | |
{ | |
return 0; | |
} | |
return ($patternA->getOrder() > $patternB->getOrder()) ? 1 : -1; | |
} | |
public static function debug() | |
{ | |
return '<pre>' . print_r(self::$patterns, TRUE) . '</pre>'; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment