Podgląd pliku: / bbcode / bbcode_debug_100RC.phps


<?php
/************************************************** 
 *           Klasa do parsowania BBcode           * 
 ************************************************** 
 * Wersja: 1.0 RC  (debug)                        * 
 * Autor: Jacek Kowalski (http://jacekk.info)     * 
 *                                                * 
 * Utwór rozprowadzany na licencji                * 
 * http://creativecommons.org/licenses/by-nc/2.5/ * 
 **************************************************/

// Kodowanie znakow: ISO-8859-2

define('DEBUG_BBCODE'TRUE);

class 
bbcode {
    static 
$simpletags = array(
        
'b' => array('<b>''</b>'),
        
'i' => array('<i>''</i>'),
        
'u' => array('<u>''</u>'),
    );
    static 
$reqarg = array(
        
// [tag=%]
        // 'tag' => array('<znacznik abc="$">', '</znacznik>', 'typ danych do $ = test(%, TYP);');
        
'size' => array('<span style="font-size:$%;">''</span>''size'),
        
'color' => array('<span style="color:$;">''</span>''color'),
        
'bg' => array('<span style="background-color:$;">''</span>''color'),
    );
    
    
// Funkcja przechwytująca błędy w BBCode
    
static function error($tekst) {
        echo 
$tekst."\n";
    }
    
    static function 
test($tekst$typ) {
        switch(
$typ) {
            case 
'color':
                
$allowed = array('aqua' => '#0FF',    'green' => '#080',    'navy' => '#008',    'silver' => '#C0C0C0',    'black' => '#000',    'grey' => '#888',    'gray' => '#888',    'olive' => '#880',    'teal' => '#088',    'blue' => '#00F',    'lime' => '#0F0',    'purple' => '#808',    'white' => '#FFF',    'fuchsia' => '#F0F',    'maroon' => '#800',    'red' => '#F00',    'yellow' => '#FF0',    'orange' => '#F80',    'pink' => '#F6B',    'violet' => '#93C',    'brown' => '#A22',    'gold' => '#FD0',    'cyan' => '#0FF',    'magenta' => '#F0F');
                if(isset(
$allowed[$tekst])) {
                    return 
$allowed[$tekst];
                }
                elseif(
substr($tekst01)=='#') {
                    
$tekst strtoupper(substr($tekst1));
                    if(
strlen($tekst) != AND strlen($tekst) != 6) {
                        
self::error('Niepoprawny kolor');
                        return 
FALSE;
                    }
                    else
                    {
                        if(
trim($tekst'0123456789ABCDEF')=='') {
                            return 
'#'.$tekst;
                        }
                        else
                        {
                            
self::error(E_WARNING'Niepoprawny kolor');
                            return 
FALSE;
                        }
                    }
                }
                else
                {
                    
self::error(E_WARNING'Niepoprawny kolor');
                    return 
FALSE;
                }
            break;
            case 
'size':
                if(!
ctype_digit($tekst)) {
                    
self::error(E_WARNING'Podana wielkość czcionki jest nieprawidłowa (dozwolone tylko cyfry)');
                    return 
FALSE;
                }
                elseif(
$tekst<20) {
                    
self::error(E_WARNING'Zbyt mała wielkość czcionki (miniumum 20%)');
                    return 
FALSE;
                }
                elseif(
$tekst>200) {
                    
self::error(E_WARNING'Zbyt duża wielkość czcionki (maksimum 200%)');
                    return 
FALSE;
                }
                
                return (int)
$tekst;
            break;
        }
    }
    
    static function 
parse($tekst) {
        
$last 0;
        
$opened = array();
        
$autoclosed = array();
        if(
defined(DEBUG_BBCODE)) {
            echo 
'<pre>';
        }
        while(
true) {
            
$start = @strpos($tekst'['$last);
            if(
$start===FALSE) { break; }
            
$end = @strpos($tekst']'$start);
            if(
$end===FALSE) { break; }
            
$check = @strpos($tekst'['$start+1);
            if(
$check<$end AND $check!==FALSE) {
                
$last $check;
                continue;
            }
            
            
$pre substr($tekst0$start);
            
$post substr($tekst$end+1);
            
            
$tekst substr($tekst$start+1$end-$start-1);
            
$tag explode('='strtolower($tekst));
            
            if(
defined(DEBUG_BBCODE)) {
                echo 
$tekst.' ... ';
            }
            switch(
count($tag)) {
                case 
1:
                    if(
substr($tag[0], 01)=='/') {
                        
$tag[0] = substr($tag[0], 1);
                        if((
$otag array_search($tag[0], $autoclosed)) !== FALSE) {
                            if(
defined(DEBUG_BBCODE)) {
                                echo 
'have been autoclosed';
                            }
                            
// Tag został już zamknięty automatycznie
                            
unset($autoclosed[$otag]);
                            break;
                        }
                        elseif(!
in_array($tag[0], $opened)) {
                            if(
defined(DEBUG_BBCODE)) {
                                echo 
'not opened - skip';
                            }
                            
// Tag nie był otwarty - pomiń
                            
$pre .= '['.$tekst.']';
                            break;
                        }
                        else
                        {
                            
// Zamykanie tagu i autozamykanie tagów (zła kolejność)
                            
if(defined(DEBUG_BBCODE)) {
                                echo 
'closing'."\n";
                            }
                            while (
true) {
                                
$otag array_pop($opened);
                                if(
$otag===NULL) {
                                    if(
defined(DEBUG_BBCODE)) {
                                        echo 
'  no more tags to close - skip';
                                    }
                                    
$pre .= '['.$tekst.']';
                                    break;
                                }
                                
                                if(
self::$simpletags[$otag]) {
                                    
$pre .= self::$simpletags[$otag][1];
                                }
                                else
                                {
                                    
$pre .= self::$reqarg[$otag][1];
                                }
                                
                                if(
$otag != $tag[0]) {
                                    
// Tag autozamknięty
                                    
if(defined(DEBUG_BBCODE)) {
                                        echo 
'  '.$otag.' ... autoclosing'."\n";
                                    }
                                    
array_push($autoclosed$otag);
                                }
                                else
                                {
                                    
// Tag właściwy zamknięty
                                    
if(defined(DEBUG_BBCODE)) {
                                        echo 
'  closed';
                                    }
                                    break;
                                }
                            }
                        }
                    }
                    elseif(
self::$simpletags[$tag[0]]) {
                        
// Otwieranie tagu
                        
if(defined(DEBUG_BBCODE)) {
                            echo 
'opening';
                        }
                        
$pre .= self::$simpletags[$tag[0]][0];
                        
array_push($opened$tag[0]);
                    }
                    elseif(
self::$reqarg[$tag[0]])
                    {
                        
// Tag wymaga argumentu - pomiń
                        
if(defined(DEBUG_BBCODE)) {
                            echo 
'req arg - skip';
                        }
                        
$pre .= '['.$tekst.']';
                    }
                    else
                    {
                        
// Tag nieznany - pomiń
                        
if(defined(DEBUG_BBCODE)) {
                            echo 
'unknown - skip';
                        }
                        
$pre .= '['.$tekst.']';
                    }
                break;
                case 
2:
                    if(
self::$simpletags[$tag[0]]) {
                        
// Dany tag nie ma argumentów - pomiń
                        
if(defined(DEBUG_BBCODE)) {
                            echo 
'req no arg - skip';
                        }
                        
$pre .= '['.$tekst.']';
                    }
                    elseif(
self::$reqarg[$tag[0]])
                    {
                        
// Otwieranie tagu
                        
$value self::test($tag[1], self::$reqarg[$tag[0]][2]);
                        if(
$value) {
                            if(
defined(DEBUG_BBCODE)) {
                                echo 
'opening';
                            }
                            
$pre .= str_replace('$'$valueself::$reqarg[$tag[0]][0]);
                            
array_push($opened$tag[0]);
                        }
                        else
                        {
                            if(
defined(DEBUG_BBCODE)) {
                                echo 
'invalid';
                            }
                        }
                    }
                break;
            }
            
            if(
defined(DEBUG_BBCODE)) {
                echo 
"\n";
            }
            
            
$tekst $pre.$post;
            
$last strlen($pre);
        }
        
        if(
defined(DEBUG_BBCODE)) {
            echo 
' autoclosing'."\n";
        }
        
        while (
true) {
            
$otag array_pop($opened);
            if(
$otag===NULL) {
                break;
            }
            
            if(
defined(DEBUG_BBCODE)) {
                echo 
'  '.$otag."\n";
            }
            
            if(
self::$simpletags[$otag]) {
                
$tekst .= self::$simpletags[$otag][1];
            }
            else
            {
                
$tekst .= self::$reqarg[$otag][1];
            }
        }
        
        if(
defined(DEBUG_BBCODE)) {
            echo 
'</pre>';
        }
        
        return 
$tekst;
    }
}
?>

Browse Code © 2010 by Jacek Kowalski