new_tags['bold'][0] regex * @example $this->new_tags['bold'][1] replace * * @var array/public */ var $new_tags = array(); /** * zum verkleinern von bildern innerhalb von [img]-tags (width) * * @var int/public */ var $imageMaxWidth = 700; /** * zum verkleinern von bildern innerhalb von [img]-tags (height) * * @var int/public */ var $imageMaxHeight = 500; /** * array für smilies * * @example $this->smilies[':)'] image * * @var array/public */ var $smilies = array(); /** * legt fest ob innherlab des strings nach geshi-tags gesucht werden soll * * @var bool/public */ var $geshi_callback = true; /** * legt jeweils einmalig den include-path für die geshi-klasse fest * * @var string/public */ var $geshi_temp_include_path = null; /** * legt den permanenten include-path von geshi fest * * @var string/public */ var $geshi_include_path = null; /** * legt als kontruk vordefinierte tags an * * @return null */ public function mc_XBBcodeParser() { $this->new_tags['google'][0] = '\[google\](.*)\[\/google\]'; $this->new_tags['google'][1] = 'Google: $1'; $this->new_tags['wiki'][0] = '\[wiki\](.*)\[\/wiki\]'; $this->new_tags['wiki'][1] = 'Wiki: $1'; } /** * nimmt eigene bb-codes auf * * @param string $tagName (regex) * @param string $replace * @param mixed $attributes * @return null */ public function assign($tagName, $replace, $attributes = '') { if(empty($attributes)) { $this->new_tags[$tagName][0] = "\[$tagName\](.*)\[\/$tagName\]"; $this->new_tags[$tagName][1] = $replace; } else { if(is_array($attributes)) { $this->new_tags[$tagName][0] = "\[$tagName "; foreach($attributes AS $attrName) { $this->new_tags[$tagName][0] .= $attrName."=\'(.*)\' "; } $this->new_tags[$tagName][0] = substr_replace( $this->new_tags[$tagName][0], '', -1 )."\](.*)\[\/$tagName\]"; $this->new_tags[$tagName][1] = $replace; } else { $this->new_tags[$tagName][0] = "\[$tagName $attributes=\'(.*)\'\](.*)\[\/$tagName\]"; $this->new_tags[$tagName][1] = $replace; } } } /** * sucht und ersetzt [img]-tags * * @param string $string * @return string (parsed) */ public function parse_images($string) { //die html-syntax [img='bild.png' /] $pl1 = "\[img=\'(.*)\' \/\]"; //standard-syntax [img='bild.png'][/img] $pl2 = "\[img=\'(.*)\'\]\[\/img\]"; //syntax1 $string = preg_replace_callback( '!'.$pl1.'!isU', create_function( '$result', ' $imgsrc = $result[1]; if(($size = getimagesize($imgsrc)) == false) return null; //kein bild! else { $sw = '.$this->imageMaxWidth.' / $size[0]; $sh = '.$this->imageMaxHeight.' / $size[1]; if($size[0] > '.$this->imageMaxHeight.' || $size[1] > '.$this->imageMaxWidth.') { if($sw < $sh) { $sizeW = '.$this->imageMaxWidth.'; $sizeH = round($size[1] * $sw); } else { $sizeW = round($size[0] * $sh); $sizeH = '.$this->imageMaxHeight.'; } } else { $sizeW = $size[0]; $sizeH = $size[1]; } return "\"Image\""; } ' ), $string ); //syntax2 $string = preg_replace_callback( '!'.$pl2.'!isU', create_function( '$result', ' $imgsrc = $result[1]; if(($size = getimagesize($imgsrc)) == false) return null; //kein bild! else { $sw = '.$this->imageMaxWidth.' / $size[0]; $sh = '.$this->imageMaxHeight.' / $size[1]; if($size[0] > '.$this->imageMaxHeight.' || $size[1] > '.$this->imageMaxWidth.') { if($sw < $sh) { $sizeW = '.$this->imageMaxWidth.'; $sizeH = round($size[1] * $sw); } else { $sizeW = round($size[0] * $sh); $sizeH = '.$this->imageMaxHeight.'; } } else { $sizeW = $size[0]; $sizeH = $size[1]; } return "\"Image\""; } ' ), $string ); return $string; } /** * sucht und ersetzt eigene bb-codes * * @param string $string * @return string (parsed) */ public function parse_own($string) { $plattern = array(); $replaces = array(); foreach($this->new_tags AS $pl) { $plattern[] = '!'.$pl[0].'!sieU'; $replaces[] = "'{$pl[1]}'"; } return preg_replace($plattern, $replaces, $string); } /** * sucht und ersetzt einfache html-tags * * @param string $string * @return string (parsed) */ public function parse_tags($string) { $s = array( '!\[b\](.*)\[\/b\]!sieU', '!\[i\](.*)\[\/i\]!sieU', '!\[u\](.*)\[\/u\]!sieU', '!\[s\](.*)\[\/s\]!sieU', '!\[color=\'(.*)\'\](.*)\[\/color\]!sieU', '!\[size=\'(.*)\'\](.*)\[\/size\]!sieU' ); $r = array( '\'$1\'', '\'$1\'', '\'$1\'', '\'$1\'', '\'$2\'', '\'$2\'' ); $string = preg_replace($s, $r, $string); $string = preg_replace_callback( '!\[list(.*)\](.*)\[\/list\]!isU', create_function( '$result', ' $r = str_replace(array("\'", "=", "\""), \'\', $result[1]); switch($r) { case "1": $type = "decimal"; break; case "*": $type = "square"; break; case "disc": $type = "disc"; break; case "none": $type = "none"; break; case "latin": $type = "upper-latin"; break; case "roman": $type = "upper-roman"; break; default: $type = "disc"; } $li = str_replace("[*]", "
  • ", $result[2]); return ""; ' ), $string ); return $string; } /** * sucht und ersetzt [url]-tags * * @param string $string * @param bool $all (goto: regex für html-links ohne tag) * @return string (parsed) */ public function parse_url($string, $all = false) { $string = preg_replace_callback( '!\[url=\'(.*)\'\](.*)\[\/url\]!isU', create_function( '$result', ' if($result[2] == \'\') $result[2] = $result[1]; if($result[1] == \'\') return \'\'; else { $http = $result[1]{0}.$result[1]{1}.$result[1]{2}.$result[1]{3}.$result[1]{4}.$result[1]{5}.$result[1]{6}; if($http != \'http://\') $result[1] = "http://{$result[1]}"; return "{$result[2]}"; } ' ), $string ); if($all === false) return $string; else return $this->_parse_url($string); } /** * sucht und ersetzt http://||www||ftp||mail strings und macht daraus klickbare links * * @param string $string * @return string (parsed) */ private function _parse_url($string) { //links ohne bbcode-tag "www.link.de" [...] $s[] = '"(( |^)((ftp|http|https){1}://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)"i'; $r[] = '\\1'; $s[] = '"( |^)(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)"i'; $r[] = '\\1\\2'; $s[] = '"([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})"i'; $r[] = '\\1'; return preg_replace($s, $r, $string); } /** * entfernt bbcode-tags * * @param string $string * @param bool $inner (entfernen von inhalten) * @return string (parsed) */ public function remove_tags($string, $inner = false) { if($inner === false) return preg_replace('!\[\/?[^\]]+\]!isU', '', $string); else return preg_replace('!\[(.*)\](.*)\[\/(.*)]!e', '', $string); } /** * nimmt einen oder mehrere smilies auf * * @param mixed $smilie * @param string $image */ public function assign_smilie($smilie, $image = '') { if(!is_array($smilie)) { $this->smilies[] = array($smilie, 'smilie'); } else { foreach($smilie AS $sm => $image) $this->smilies[] = array($sm, 'smilie'); } } /** * sucht und ersetzt aufgenommene smilies * * @param string $string * @return string (parsed) */ public function parse_smilies($string) { foreach($this->smilies AS $index) { $string = str_replace($index[0], $index[1], $string); } return $string; } /** * führt alle funktionen dieser klasse aus * * @param string $string * @param bool $all * @param bool $removeOther * @param bool $removeAllOther * @return string (parsed) */ public function parse($string, $all = true, $removeOther = false, $removeAllOther = false) { $string = htmlentities($string); $string = $this->parse_images($string); $string = $this->parse_tags($string); $string = $this->parse_url($string, $all); $string = $this->parse_smilies($string); $string = $this->parse_own($string); $string = $this->parse_code($string, true); if($this->geshi_callback === true) $string = $this->geshi($string); if($removeOther === true) $string = $this->remove_tags($string, (($removeAllOther === true) ? true : false)); return nl2br($string); } /** * sucht und ersetzt [code] und [.php]-tags * * @param string $string * @param bool $phtml * @return string (parsed) */ public function parse_code($string, $phtml = false) { //php-highlight $string = preg_replace_callback( '!\[php\](.*)\[\/php\]!isU', create_function( '$result', ' if($result[1] == \'\') return null; else { $str = $result[1]; if('.(($phtml === true) ? "true" : "false").') $str = htmlspecialchars_decode($str); $str = highlight_string($str, true); return "
    \r\nPHP-Code:\r\n
    $str
    "; } ' ), $string ); //code-box $string = preg_replace('#\[code\](.*)\[/code\]#Uise', "'
    \r\nCode:\r\n
    '.((".(($phtml === true) ? "true" : "false").") ? htmlentities('\\1') : '\\1').'
    '", $string); return $string; } /** * sucht und ersetzt [code]-tags mit script-type angaben * benötigt die geshi-syntax highlight klasse * http://qbnz.com/highlighter/ * * @param string $string * @param string $path * @return string (parsed) */ public function geshi($string, $path = null) { if($path != null) $this->geshi_temp_include_path = $path; $string = preg_replace_callback( '!\[code=\'(.*)\'\](.*)\[\/code\]!isU', create_function( '$res', ' if($res[2] != "") { if($res[1] == "") { $str = ((ereg("<", $res[2])) ? htmlentities($res[2]) : $res[2]); return "
    \r\nCode:\r\n
    $str
    "; } else { if('.(($this->geshi_temp_include_path != null) ? "true" : "false").') { if(file_exists("'.$this->geshi_temp_include_path.'")) require_once "'.$this->geshi_temp_include_path.'"; else return "geshi nicht gefunden!"; } elseif('.(($this->geshi_include_path != null) ? "true" : "false").') { if(file_exists("'.$this->geshi_include_path.'")) require_once "'.$this->geshi_include_path.'"; else return "geshi nicht gefunden!"; } else return "geshi nicht gefunden!"; $type = htmlentities(strtolower($res[1])); $str = htmlspecialchars_decode($res[2]); $geshi = new GeSHi($str, $type); return "
    \r\nCode type: $type\r\n
    ".$geshi->parse_code()."
    "; } } else return null; ' ), $string ); $this->geshi_temp_include_path = null; return $string; } }; ?>