Created
November 14, 2009 18:08
-
-
Save laszlokorte/234668 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 | |
{ | |
/* | |
* | |
* Alle HTML-Befehlszeichen aus einem String in HTML-Entities umwandeln | |
* | |
*/ | |
public static function html($string) | |
{ | |
global $cs_main; | |
return htmlentities($string, ENT_QUOTES, $cs_main['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) | |
{ | |
global $cs_main; | |
return html_entity_decode($string, ENT_QUOTES, $cs_main['charset']); | |
} | |
} | |
/* | |
* | |
* Pattern Klasse | |
* | |
*/ | |
final class BbcodePattern | |
{ | |
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, $regex = TRUE, $recursive = FALSE) | |
{ | |
$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 Bbcode | |
{ | |
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, $securehtml = false) | |
{ | |
self::sortPatterns(); | |
if (isset($securehtml)) $string = Secure::html($string); | |
if (empty($this->active_patterns)) return $string; | |
foreach(self::$patterns AS $pattern) | |
{ | |
if (array_key_exists($pattern->getname(), $this->active_patterns) && $this->active_patterns[$pattern->getname()] === TRUE) | |
{ | |
$string = $pattern->apply($string); | |
} | |
} | |
return $string; | |
} | |
// For quick & easy use | |
public static function apply ($string, $group = 'base', $securehtml = true) { | |
$bbcode = new self($group); | |
return $bbcode->convert ($string, $securehtml); | |
} | |
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(BbcodePattern $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('Bbcode','sortPatternsCallback')); | |
self::$sorted = TRUE; | |
} | |
} | |
private static function sortPatternsCallback($patternA, $patternB) | |
{ | |
return $patternA->getOrder() - $patternB->getOrder(); | |
} | |
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