All pastes #714018 Raw Edit

Vermischtes

public php v1 · immutable
#714018 ·published 2007-09-25 21:33 UTC
rendered paste body
<?php    class mc_XBBcodeParser    {        /**         * array für neue tags die eigend definiert wurden         *         * @example $this->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] = '<a href="http://google.de/search?q=$1&hl=de" target="blank">Google: $1</a>';                    $this->new_tags['wiki'][0] = '\[wiki\](.*)\[\/wiki\]';                $this->new_tags['wiki'][1] = '<a href="http://de.wikipedia.org/wiki/$1" target="blank">Wiki: $1</a>';        }                /**         * 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 "<img src=\"$imgsrc\" style=\"width: {$sizeW}px; height: {$sizeH}px;\" alt=\"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 "<img src=\"$imgsrc\" style=\"width: {$sizeW}px; height: {$sizeH}px;\" alt=\"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(                '\'<span style="font-weight: bold;">$1</span>\'',                '\'<span style="font-style: italic;">$1</span>\'',                '\'<span style="text-decoration: underline;">$1</span>\'',                '\'<span style="text-decoration: line-through;">$1</span>\'',                '\'<span style="color: $1;">$2</span>\'',                '\'<span style="font-size: $1px;">$2</span>\''            );                        $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("[*]", "<li>", $result[2]);                        return "<ul style=\"list-style-type: {$type};\">$li</ul>";                    '                ),                $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 "<a href=\"{$result[1]}\" target=\"blank\">{$result[2]}</a>";                        }                    '                ),                $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[] = '<a href="\1" target="blank">\\1</a>';                        $s[] = '"( |^)(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)"i';            $r[] = '\\1<a href="http://\2" target="blank">\\2</a>';                        $s[] = '"([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})"i';            $r[] = '<a href="mailto:\1">\\1</a>';                        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, '<img src="'.$image.'" alt="smilie" border="0" />');            }            else {                foreach($smilie AS $sm => $image) $this->smilies[] = array($sm, '<img src="'.$image.'" alt="smilie" border="0" />');            }        }                /**         * 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 "<br/>\r\n<b>PHP-Code:</b>\r\n<br/><div style=\"padding: 4px; border: 1px solid #000000;\">$str</div>";                        }                        '                ),                $string            );                        //code-box            $string = preg_replace('#\[code\](.*)\[/code\]#Uise', "'<br/>\r\n<b>Code:</b>\r\n<br/><div style=\"padding: 4px; border: 1px solid #000000;\"><code>'.((".(($phtml === true) ? "true" : "false").") ? htmlentities('\\1') : '\\1').'</code></div>'", $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 "<br/>\r\n<b>Code:</b>\r\n<br/><div style=\"padding: 4px; border: 1px solid #000000;\"><code>$str</code></div>";                            }                            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 "<i>geshi nicht gefunden!</i>";                                }                                elseif('.(($this->geshi_include_path != null) ? "true" : "false").') {                                    if(file_exists("'.$this->geshi_include_path.'")) require_once "'.$this->geshi_include_path.'";                                    else return "<i>geshi nicht gefunden!</i>";                                }                                else return "<i>geshi nicht gefunden!</i>";                                                            $type = htmlentities(strtolower($res[1]));                                $str = htmlspecialchars_decode($res[2]);                                $geshi = new GeSHi($str, $type);                                return "<br/>\r\n<b>Code type: $type</b>\r\n<br/><div style=\"padding: 4px; border: 1px solid #000000;\">".$geshi->parse_code()."</div>";                            }                        }                        else return null;                    '                ),                $string            );                            $this->geshi_temp_include_path = null;            return $string;                        }    };?>