Created
August 21, 2009 11:28
-
-
Save laszlokorte/171883 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; | |
} | |
public function __toString() | |
{ | |
return $this->name; | |
} | |
} | |
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) | |
{ | |
if( ! isset(self::$patterns[$pattern->getName()])) | |
{ | |
self::$patterns = array_merge(self::$patterns, array($pattern->getName() => $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) | |
{ | |
uasort(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 getPatterns() | |
{ | |
self::sortPatterns(); | |
return self::$patterns; | |
} | |
} | |
final class ClansphereAbcode | |
{ | |
private static $cache = array(); | |
public static function load() | |
{ | |
Abcode::register(new AbcodePattern('bold', "/\[b\](.*?)\[\/b\]/si","<strong>$1</strong>"), TRUE); | |
Abcode::register(new AbcodePattern('italic', "/\[i\](.*?)\[\/i\]/si","<em>$1</em>"), TRUE); | |
Abcode::register(new AbcodePattern('underlined', "/\[u\](.*?)\[\/u\]/si","<span class=\"underlined\">$1</span>"), TRUE); | |
Abcode::register(new AbcodePattern('striked', "/\[s\](.*?)\[\/s\]/si","<del>$1</del>"), TRUE); | |
Abcode::register(new AbcodePattern('image', "/\[img\](.*?)\[\/img\](.{0})/si","<img src=\"$1\" alt=\"\" />"), TRUE); | |
Abcode::register(new AbcodePattern('urlimage', "/\[url\=(.*?)\]\[img width\=(.*?) height\=(.*?)\](.*?)\[\/img\]\[\/url\]/si","ClansphereAbcode::url_image()"), TRUE); | |
Abcode::register(new AbcodePattern('sizedimage', "/\[img width\=([\d]*?) height\=([\d]*?)\](.*?)\[\/img\]/si","ClansphereAbcode::image()"), TRUE); | |
Abcode::register(new AbcodePattern('mail', "/\[mail\](.+)\[\/mail]/si","ClansphereAbcode::mail()"), TRUE); | |
Abcode::register(new AbcodePattern('automail', "/([^\s]{3,})@([^\s]*?)\.([^\s]{2,7})(?![^<]+>|[^&]*;)/si","ClansphereAbcode::mail()"), TRUE); | |
Abcode::register(new AbcodePattern('color', "/\[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>"), TRUE); | |
Abcode::register(new AbcodePattern('size', "/\[size\=([1-3]?\d)\](.+)\[\/size\]/si","<span style=\"font-size: $1px\">$2</span>"), TRUE); | |
Abcode::register(new AbcodePattern('align', "/\[(?P<align>left|center|right|justify)\](.+)\[\/(?P=align)]/si","<p style=\"text-align: $1;\">$2</p>"), TRUE); | |
Abcode::register(new AbcodePattern('typedlist', "/\[list\=([a-z_-]+?)\](.+?)\[\/list\]/si","ClansphereAbcode::unordered_list()"), TRUE); | |
Abcode::register(new AbcodePattern('list', "/\[list\](.+?)\[\/list\]/si","ClansphereAbcode::unordered_list()"), TRUE); | |
Abcode::register(new AbcodePattern('namedurl', "/\[url\=(.+)\](.+)\[\/url]/si","ClansphereAbcode::url()"), TRUE); | |
Abcode::register(new AbcodePattern('url', "/\[url\](.+)\[\/url]/si","ClansphereAbcode::url()"), TRUE); | |
Abcode::register(new AbcodePattern('autourl', "/(www\.|http:\/\/|ftp:\/\/)([^\s,]+)\.([^\s]+)(?![^<]+>|[^&]*;)/si", 'ClansphereAbcode::url_auto()'), TRUE); | |
Abcode::register(new AbcodePattern('flag', "/\[flag\=([A-Za-z]{2})\]/si","ClansphereAbcode::flag()"), TRUE); | |
Abcode::register(new AbcodePattern('indention', "/\[indent\=([\d]+)\](.+)\[\/indent\]/siU","<div style=\"padding-left: $1em\">$2</div>", 0, TRUE), TRUE); | |
Abcode::register(new AbcodePattern('thread', "/\[threadid\=([\d]+)\](.+)\[\/threadid\]/si","ClansphereAbcode::thread_link()"), TRUE); | |
Abcode::register(new AbcodePattern('headline', "/\[h\=([1-7])\](.+)\[\/h\]/si","<h$1>$2</h$1>"), TRUE); | |
Abcode::register(new AbcodePattern('ruler', "/\[hr\]/","<hr>"), TRUE); | |
Abcode::register(new AbcodePattern('quote', "/\[quote\](.*)\[\/quote\]/siU","<div style=\"padding: 5px;\">$1</div>", 0, TRUE), TRUE); | |
Abcode::register(new AbcodePattern('clip', "/\[clip=(.+)\](.*)\[\/clip\]/siU","<div class=\"abcode_clip\"><h4 class=\"clip_title\">$1</h4><div class=\"clip_body\">$2</div></div>", 0, TRUE), TRUE); | |
Abcode::register(new AbcodePattern('typedcode', "/\[code=(.+)\](.*)\[\/code\]/si","ClansphereAbcode::extract_code()",-4), TRUE); | |
Abcode::register(new AbcodePattern('code', "/\[code\](.*)\[\/code\]/si","ClansphereAbcode::extract_code()", -4), TRUE); | |
Abcode::register(new AbcodePattern('_insertcode', "/\[code=(\d+)\/\]/si","ClansphereAbcode::insert_cached()",4), TRUE); | |
Abcode::register(new AbcodePattern('html', "/\[html\](.*)\[\/html\]/si","ClansphereAbcode::extract_html()", -4), TRUE); | |
Abcode::register(new AbcodePattern('_inserthtml', "/\[html=(\d+)\/\]/si","ClansphereAbcode::insert_cached()",4), TRUE); | |
Abcode::register(new AbcodePattern('filter', "/\[filter\](.*)\[\/filter\]/si","ClansphereAbcode::extract_filter()", -5), TRUE); | |
Abcode::register(new AbcodePattern('_insertfilter', "/\[filter=(\d+)\/\]/si","ClansphereAbcode::insert_cached()",5), TRUE); | |
Abcode::register(new AbcodePattern('phpcode', "/\[phpcode\](.*)\[\/phpcode\]/si","ClansphereAbcode::extract_phpcode()", -4), TRUE); | |
Abcode::register(new AbcodePattern('_insertphpcode', "/\[phpcode=(\d+)\/\]/si","ClansphereAbcode::insert_cached()", 4), TRUE); | |
Abcode::register(new AbcodePattern('linebreaks', "/[\n]/i", "<br/>\n", 10), TRUE); | |
} | |
public static function thread_link($matches) | |
{ | |
return "<a href=\"?mod=board&action=thread&id={$matches[1]}\">{$matches[2]}</a>"; | |
} | |
//TODO: Rework: url(), url_auto(), url_image(), image(), mail() | |
public static function url($matches) { | |
$java = substr($matches[1],0,10); | |
if($java != 'javascript') { | |
if(empty($matches[2])) { | |
$matches[2] = $matches[1]; | |
} | |
$matches[1] = strpos($matches[1],'www.') === 0 ? 'http://' . $matches[1] : $matches[1]; | |
return cs_html_link($matches[1],$matches[2],1); | |
} else { | |
return cs_abcode_i(array(0,'Javascript Links are not allowed')); | |
} | |
} | |
public static function url_auto($matches) { | |
if (strpos($matches[0],'</a>') !== FALSE || strpos($matches[0],'[/threadid]') !== FALSE) | |
return $matches[0]; | |
$after = ''; | |
if (substr($matches[0],-1) == ',') { $matches[0] = substr($matches[0],0,-1); $after = ','; } | |
$url = substr($matches[0],0,4) == 'www.' ? 'http://' . $matches[0] : $matches[0]; | |
return '<a href="' . $url . '">' . $matches[0] . '</a>' . $after; | |
} | |
public static function url_image($matches) { | |
} | |
public static function image($matches) { | |
if ($matches[0]{4} == ']') { | |
return '<img src="' . $matches[1] . '" alt="" />'; | |
} else { | |
return '<a href="' . $matches[3] . '"<img src="' . $matches[1] . '" style="width: ' . $matches[1] . 'px; height: ' . $matches[1] . 'px" alt="" /></a>'; | |
} | |
} | |
public static function mail($matches) { | |
$mail = $matches[1]; | |
if (strpos($matches[0],'</a>') !== FALSE) | |
return $matches[0]; | |
if ($matches[0]{0} != '[') | |
$mail = $matches[0]; | |
return '<a href="mailto:' . $mail . '">' . $mail . '</a>'; | |
} | |
public static function flag($matches) | |
{ | |
$short = $matches[1]; | |
$country = $short . ': not included'; | |
return '<img src="symbols/countries/' . $short . '.png" alt="' . $country . '" />'; | |
} | |
public static function unordered_list($matches) | |
{ | |
$list_types = array('none', 'circle', 'square', 'disc', 'decimal', 'lower-roman', 'upper-roman', 'decimal-leading-zero', 'lower-greek', 'lower-latin', 'upper-latin', 'armenian', 'georgian'); | |
$type = (isset($matches[2]) && in_array($matches[1], $list_types)) ? $matches[1] : 'disc'; | |
if(preg_match_all("/\[\*\]([\w\d\s]+)/si",end($matches),$items)>0) | |
{ | |
$items = end($items); | |
$list = '<ul style="list-style: ' . $type . ';">'; | |
foreach($items AS $item) | |
{ | |
$list .= '<li>' . $item . '</li>'; | |
} | |
$content; | |
$list .= '</ul>'; | |
return $list; | |
} | |
return $matches[0]; | |
} | |
public static function extract_code($matches) | |
{ | |
$lang = isset($matches[2]) ? $matches[1] : 'text'; | |
$code = isset($matches[2]) ? $matches[2] : $matches[1]; | |
$code = preg_replace("/<br(.*?)>/si",'',$code); | |
$line_count = substr_count ( $code, "\n" ) + 1; | |
$line_numbers = ''; | |
for($i=1;$i<=$line_count;$i++) | |
{ | |
$line_numbers .= ($i.'<br/>'); | |
} | |
$code = '<table class="code"><tr><td class="lines"><pre>' . $line_numbers . '</pre></td><td class=\"code\"><pre><code class="' . $lang . '">' . $code . '</code></pre></td></tr></table>'; | |
$index = self::set_cache($code); | |
return '[code=' . $index .'/]'; | |
} | |
public static function extract_html($matches) | |
{ | |
$html = Secure::rebuild_html($matches[1]); | |
$index = self::set_cache($html); | |
return '[html=' . $index .'/]'; | |
} | |
public static function extract_filter($matches) | |
{ | |
$index = self::set_cache($matches[1]); | |
return '[filter=' . $index .'/]'; | |
} | |
public static function extract_phpcode($matches) | |
{ | |
ob_start(); | |
eval($matches[1]); | |
$eval = ob_get_contents(); | |
ob_end_clean(); | |
$eval; | |
$index = self::set_cache($eval); | |
return '[phpcode=' . $index .'/]'; | |
} | |
private static function set_cache($content) { | |
array_push(self::$cache, $content); | |
return key(self::$cache); | |
} | |
private static function get_cache($index) { | |
if(isset(self::$cache[$index])) | |
{ | |
$result = self::$cache[$index]; | |
unset(self::$cache[$index]); | |
reset(self::$cache); | |
return $result; | |
} | |
return FALSE; | |
} | |
public static function insert_cached($matches) | |
{ | |
$cached = self::get_cache($matches[1]); | |
if($cached !== FALSE) | |
{ | |
return $cached; | |
} | |
return $matches[0]; | |
} | |
} | |
ClansphereAbcode::load(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment