Created
September 22, 2009 17:07
-
-
Save laszlokorte/191230 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 | |
/* | |
* | |
* Secure Class | |
* | |
*/ | |
class Secure | |
{ | |
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*?]{50})(?![^<]+>|[^&]*;)=","\\0 ", $string); | |
} | |
/* | |
* | |
* HTML-Entities wieder echtes HTML machen | |
* | |
*/ | |
public static function rebuild_html($string) | |
{ | |
return html_entity_decode($string, ENT_QUOTES, self::$charset); | |
} | |
} | |
/** | |
* Abcode Klasse | |
* | |
* Der Kern des Abcodes hat Suchmuster und den dazugehörigen Aktion als eine Instanz gespeichert | |
* | |
* const VERSION - Die Version der Klasse | |
* const SIMPLE_STRING_TYPE - Eine Konstante für die Zuordnung des Typs der Aktion einer Instanz: Das Muster ist kein Regulärer Ausdruck und die Aktion ist ein String | |
* const PREG_STRING_TYPE - Eine Konstante für die Zuordnung des Typs der Aktion einer Instanz: Das Muster ist ein Regulärer Ausdruck und die Aktion ist ein String | |
* const PREG_CALLBACK_TYPE - Eine Konstante für die Zuordnung des Typs der Aktion einer Instanz: Das Muster ist ein Regulärer Ausdruck und die Aktion ist der Name einer Funktion oder Eine Anonyme Funktion | |
* | |
* static array patterns - Hier sind alle Instanzen der Klasse mit numerischem Schlüssel gespeichert | |
* static int patterns_count - Die Anzahl der Instanzen der Klasse | |
* | |
* private string pattern - Das Suchmuster | |
* private string action - Die Aktion die auf das Suchmuster angewendet werden soll (einfacher String, Anonyme Funktion oder Name einer Funktion) | |
* private int type - Der Typ der Aktion, wird automatisch anhand der gegebenen Aktion ermittelt | |
* private boolean recursive - Gibt an, ob das Suchmuster rekursiv angewendet werden soll | |
* private int level - das Zugriffslevel, das benötigt wird, um ein Suchmuster anwenden zu dürfen | |
* | |
*/ | |
class Abcode | |
{ | |
const VERSION = 0.9; | |
const SIMPLE_STRING_TYPE = 0; | |
const PREG_STRING_TYPE = 1; | |
const PREG_CALLBACK_TYPE = 2; | |
private static $patterns = array(); | |
private static $patterns_count=0; | |
private $pattern; | |
private $action; | |
private $type; | |
private $recursive; | |
private $level; | |
/* | |
* Ein neues Suchmuster registrieren | |
* | |
* string $pattern - das Suchmuster (regulärer Ausdruck) | |
* string $action - die Aktion, die bei einem Treffer durchgeführt werden soll als String oder Function-Object. | |
* ->Als Standard ist $action ein der mit preg_replace() auf das Suchmuster angewendet | |
* ->Wenn $action ein string ist, der mit () endet, wird $action als Name einer Funktion erkennt und mit preg_replace_callback() auf das Suchmuster angewendet | |
* ->Wenn $action ein Object ist, wird es als Anonyme Methode erkannt und mit preg_replace_callback() auf das Suchmuster angewendet | |
* | |
* boolean $recursive - ob das Suchmuster rekursiv angewendet werden soll [standard: FALSE] | |
* -> Für die Recursivität muss das Suchmuster $pattern dafür ausgelegt sein was die "Gier" der Untermuster angeht | |
* int $level - das Zugriffslevel, ab dem das Suchmuster benutzt werden darf [standard: 0] | |
* boolean $start_pos - Wenn Auf TRUE gesetzt, wird die Instanz an den Anfang der Liste angefügt, also das Suchmuster später zuerst ausgeführt [standard: FALSE] | |
* boolean $regex - bestimmt ob das Muster als Regulärer Ausdruck gewertet werden soll, oder als einfacher String | |
* | |
*/ | |
public function __construct($pattern, $action, $recursive = FALSE, $level = 0, $start_pos = FALSE, $regex = TRUE) | |
{ | |
$this->pattern = $pattern; | |
$this->action = $action; | |
$this->recursive = $recursive; | |
$this->level = $level; | |
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; | |
} | |
if($start_pos===TRUE) | |
{ | |
array_unshift(self::$patterns, $this); | |
} | |
else | |
{ | |
array_push(self::$patterns, $this); | |
} | |
self::$patterns_count += 1; | |
} | |
/* | |
* Ersetzen | |
* | |
* Wendet alle Suchmuster auf einen $string an, dessen level <= $level ist und gibt den den veränderten $string zurück | |
* | |
*/ | |
public static function replace($string, $level = 0) | |
{ | |
for($i=0; $i<self::$patterns_count; $i++) | |
{ | |
if(self::$patterns[$i]->level <= $level) | |
{ | |
$string = self::$patterns[$i]->apply($string); | |
} | |
} | |
return $string; | |
} | |
/* | |
* Suchmuster Entfernen | |
* | |
* Entfernt die Syntax aller Suchmuster aus einem $string und gibt ihn zurück. | |
* Wenn $level nicht FALSE ist (sondern ein integer) werden nur die Suchmuster benutzt, | |
* dessen level <= $level ist. | |
* | |
*/ | |
public static function clear($string, $level = FALSE) | |
{ | |
$string = nl2br($string); | |
for($i=0; $i<self::$patterns_count; $i++) | |
{ | |
if($level===FALSE || self::$patterns[$i]->level <= $level) | |
{ | |
$string = self::$patterns[$i]->filter($string); | |
} | |
} | |
return $string; | |
} | |
/* | |
* | |
* Ein einzelnes Suchmuster anwenden | |
* | |
* Wendet das Suchmuster einer einzelnen Instanz auf $string an und gibt $string zurück. | |
* | |
*/ | |
private 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; | |
} | |
/* | |
* | |
* Ein Suchmuster einer Instanz aus einem String filtern | |
* | |
* Wendet das Suchmuster einer einzelnen Instanz auf $string an und ersetzt das Ergebnis (statt die Aktion anzuwenden) mit dem letzten Untermuster des Musters | |
* | |
*/ | |
private 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; | |
} | |
/* | |
* | |
* Eine Liste mit allen Instanzen ausgeben | |
* | |
*/ | |
public static function debug() { | |
echo "<pre>"; | |
print_r(self::$patterns); | |
echo "</pre>"; | |
} | |
} | |
/* | |
* | |
* Clansphere AbcodeKlasse | |
* | |
* Die Klasse dient als namespace für alle Callback-Methoden, die von den Clansphere-Standardsuchmustern benutzt werden | |
* | |
* const URL_Pattern - Ein Teil eines Regulären Ausdrucks, der an mehreren Stellen benutzt werden kann, um eine URL zu erkennen, ist aber noch nicht in benutzung | |
* | |
* static array $cache - Wird benutzt, um Treffer zwischen zu speichern und später wieder einzufügen, z.B. bei [html], [code], [phpcode] und [filter] | |
* | |
*/ | |
class ClansphereAbcode | |
{ | |
# not in use yet | |
const URL_PATTERN = "((http|https|ftp):\/\/([a-z0-9]+:[a-z0-9]+@)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?)?"; | |
private static $cache = array(); | |
/* | |
* | |
* Clansphere ABcode laden | |
* | |
* Beim Aufruf dieser methode werden alle benötigten Suchmuster bei der Abcode Klasse registriert | |
* | |
*/ | |
public static function load() | |
{ | |
new Abcode("/\[b\](.*?)\[\/b\]/si","<strong>$1</strong>"); | |
new Abcode("/\[i\](.*?)\[\/i\]/si","<em>$1</em>"); | |
new Abcode("/\[u\](.*?)\[\/u\]/si","<span class=\"underlined\">$1</span>"); | |
new Abcode("/\[s\](.*?)\[\/s\]/si","<del>$1</del>"); | |
new Abcode("/\[img\](.*?)\[\/img\](.{0})/si","<img src=\"$1\" alt=\"\" />"); | |
new Abcode("/\[url\=(.*?)\]\[img width\=(.*?) height\=(.*?)\](.*?)\[\/img\]\[\/url\]/si","ClansphereAbcode::url_image()"); | |
new Abcode("/\[img width\=([\d]*?) height\=([\d]*?)\](.*?)\[\/img\]/si","ClansphereAbcode::image()"); | |
new Abcode("/\[mail\](.+)\[\/mail]/si","ClansphereAbcode::mail()"); | |
new Abcode("/([^\s]{3,})@([^\s]*?)\.([^\s]{2,7})(?![^<]+>|[^&]*;)/si","ClansphereAbcode::mail()"); | |
new Abcode("/\[size\=([1-3]?\d)\]\[color\=(#[0-9a-fA-F]{3}|#[0-9a-fA-F]{6}|red|green|blue|yellow|black|white|grey|purple)\](.+)\[\/color\]\[\/size\]/si","<span style=\"font-size: $1px; color: $2\">$3</span>"); | |
new Abcode("/\[color\=(#[0-9a-fA-F]{3}|#[0-9a-fA-F]{6}|red|green|blue|yellow|black|white|grey|purple)\]\[size\=([1-3]?\d)\](.+)\[\/size\]\[\/color\]/si","<span style=\"font-size: $1px; color: $2\">$3</span>"); | |
new Abcode("/\[color\=(#[0-9a-fA-F]{3}|#[0-9a-fA-F]{6}|red|green|blue|yellow|black|white|grey|purple)\](.+)\[\/color\]/si","<span style=\"color: $1\">$2</span>"); | |
new Abcode("/\[size\=([1-3]?\d)\](.+)\[\/size\]/si","<span style=\"font-size: $1px\">$2</span>"); | |
new Abcode("/\[(?P<align>left|center|right|justify)\](.+)\[\/(?P=align)]/si","<p style=\"text-align: $1;\">$2</p>"); | |
new Abcode("/\[list\=([a-z_-]+?)\](.+?)\[\/list\]/si","ClansphereAbcode::unordered_list()"); | |
new Abcode("/\[list\](.+?)\[\/list\]/si","ClansphereAbcode::unordered_list()"); | |
new Abcode("/\[url\=(.+)\](.+)\[\/url]/si","ClansphereAbcode::url()"); | |
new Abcode("/\[url\](.+)\[\/url]/si","ClansphereAbcode::url()"); | |
new Abcode("/(www\.|http:\/\/|ftp:\/\/)([^\s,]+)\.([^\s]+)(?![^<]+>|[^&]*;)/si", 'ClansphereAbcode::url_auto()'); | |
new Abcode("/\[flag\=([A-Za-z]{2})\]/si","ClansphereAbcode::flag()"); | |
new Abcode("/\[indent\=([\d]+)\](.+)\[\/indent\]/siU","<div style=\"padding-left: $1em\">$2</div |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment