<?php
class UGMarkup
{
    public static $BASEPATH = '';
    public static $JSPATH = '';
    public static $CSSPATH = '';
    public static $FULLPATH = '';
    public static $IMAGEPATH = '';
    public static $URLPATH = '';
    public static $vbulletin = NULL;
    public static $user = NULL;
    public static $displaygroupid = NULL;
    public static $text = '';
    public static $fields = array();

    public static function init($vbulletin)
    {
        if(!$vbulletin) return;
        self::$vbulletin =& $vbulletin;
        self::$BASEPATH = self::$vbulletin->options['bburl'] . '/ugmarkup';
        self::$JSPATH = self::$BASEPATH . '/clientscript';
        self::$CSSPATH = self::$BASEPATH . '/css';
        self::$IMAGEPATH = trim(self::setting('icon_path'), '/');
        self::$FULLPATH = DIR . '/' . self::$IMAGEPATH;
        self::$URLPATH = self::$vbulletin->options['bburl'] . '/' . self::$IMAGEPATH;
    }

    public static function process(&$user, $displaygroupid)
    {
        if(!self::$vbulletin OR !$user) return;
        self::$user =& $user;
        self::$text = $user['username'];
        self::$displaygroupid = $displaygroupid;
        self::fetchAll();
        if($compiled = self::compile()){
            $user['musername'] = $compiled;
        }
    }

    public static function processText($text, $usergroupid)
    {
        if(!$text OR !$usergroupid) return $text;
        self::$text = $text;
        self::$displaygroupid = $usergroupid;
        self::fetchAll();
        if($compiled = self::compile()){
            return $compiled;
        }
        return $text;
    }

    protected static function compile()
    {
        if(self::$fields['image'] OR self::$fields['rainbow'] OR
            self::$fields['custom']
        ){
            $image   =& self::$fields['image'];
            $rainbow =& self::$fields['rainbow'];
            $custom  =& self::$fields['custom'];
            if($rainbow) $custom = preg_replace('/color:[^;]+?;/i', '', $custom);
            $left = $right = '';
            self::fetchField('imagedir') ? $right = $image : $left = $image;
            return
                $left . '<span style="'.$custom.'">' .
                ($rainbow ? $rainbow : self::$text) . '</span>' . $right;
        }
        return false;
    }

    protected static function fetchAll()
    {
        self::fetchColor();
        self::fetchImage();
        self::fetchRainbow();
        self::fetchCustom();
    }

    protected static function fetchColor()
    {
        $color = self::fetchField('color');
        self::$fields['color'] = $color ? "color:$color;" : '';
    }

    protected static function fetchImage()
    {
        $image = '';
        if($image = self::fetchField('image')){
            $max = 'max-width:'.self::setting('icon_maxwidth').';max-height:'.self::setting('icon_maxheight').';';
            $image =
            '<img src="'.self::$URLPATH."/$image".'" alt="" border="0" style="'.(self::fetchField('imagedir') ? self::setting('icon_css_right') : self::setting('icon_css')). $max.'"/>';
        }
        self::$fields['image'] = $image;
    }

    protected static function fetchRainbow()
    {
        $ret = '';
        $text = self::$text;
        if($rainbow = self::fetchField('rainbow')){
            $colors = array(
                'ff00ff',
                'ff00cc',
                'ff0099',
                'ff0066',
                'ff0033',
                'ff0000',
                'ff3300',
                'ff6600',
                'ff9900',
                'ffcc00',
                'ffff00',
                'ccff00',
                '99ff00',
                '66ff00',
                '33ff00',
                '00ff00',
                '00ff33',
                '00ff66',
                '00ff99',
                '00ffcc',
                '00ffff',
                '00ccff',
                '0099ff',
                '0066ff',
                '0033ff',
                '0000ff',
                '3300ff',
                '6600ff',
                '9900ff',
                'cc00ff'
            );
            if(self::setting('randomize_rainbow')) shuffle($colors);
            $i = 0;
            $textlength = strlen($text);

            while($i <= $textlength){
                foreach($colors as $value){
                    if ($text[$i] != ''){
                        $ret .= '<span style="color:#'.$value.';">'.$text[$i]."</span>";
                    }
                $i++;
                }
            }
        }
        self::$fields['rainbow'] = $ret;
    }

    protected static function fetchCustom()
    {
        $custom = array();
        if($custom = self::fetchField('custom') OR self::fetchField('bold') OR self::$fields['color']){
            $custom = @explode(PHP_EOL, $custom);
            array_walk($custom, array('UGMarkup', 'cleanCSS'));
            if(self::fetchField('bold') AND !in_array('font-weight:bold', $custom)){
                $custom[] = 'font-weight:bold';
            }
            if(self::$fields['color']) $custom[] = self::$fields['color'];
        }
        self::$fields['custom'] = @implode(';', $custom);
    }

    protected static function cleanCSS(&$value)
    {
        $value = str_replace(array(';', ''), '', $value);
    }

    protected static function fetchField($fieldname)
    {
        if($field = self::$vbulletin->usergroupcache[self::$displaygroupid]["ugmarkup_$fieldname"]){
            return $field;
        }
        return false;
    }

    public static function setting($name)
    {
        return self::$vbulletin->options["ugmarkup_$name"];
    }
}