特殊模板变量解析
格式 以 $Think. 打头的变量属于特殊模板变量
@access public
@param string $varStr  变量字符串
@return string
424660
public function parseThinkVar($varStr){
        $vars = explode('.',$varStr);
        $vars[1] = strtoupper(trim($vars[1]));
        $parseStr = '';
        if(count($vars)>=3){
            $vars[2] = trim($vars[2]);
            switch($vars[1]){
                case 'SERVER':
                    $parseStr = '$_SERVER[\''.strtoupper($vars[2]).'\']';break;
                case 'GET':
                    $parseStr = '$_GET[\''.$vars[2].'\']';break;
                case 'POST':
                    $parseStr = '$_POST[\''.$vars[2].'\']';break;
                case 'COOKIE':
                    if(isset($vars[3])) {
                        $parseStr = '$_COOKIE[\''.$vars[2].'\'][\''.$vars[3].'\']';
                    }else{
                        $parseStr = 'cookie(\''.$vars[2].'\')';
                    }
                    break;
                case 'SESSION':
                    if(isset($vars[3])) {
                        $parseStr = '$_SESSION[\''.$vars[2].'\'][\''.$vars[3].'\']';
                    }else{
                        $parseStr = 'session(\''.$vars[2].'\')';
                    }
                    break;
                case 'ENV':
                    $parseStr = '$_ENV[\''.strtoupper($vars[2]).'\']';break;
                case 'REQUEST':
                    $parseStr = '$_REQUEST[\''.$vars[2].'\']';break;
                case 'CONST':
                    $parseStr = strtoupper($vars[2]);break;
                case 'LANG':
                    $parseStr = 'L("'.$vars[2].'")';break;
                case 'CONFIG':
                    if(isset($vars[3])) {
                        $vars[2] .= '.'.$vars[3];
                    }
                    $parseStr = 'C("'.$vars[2].'")';break;
                default:break;
            }
        }else if(count($vars)==2){
            switch($vars[1]){
                case 'NOW':
                    $parseStr = "date('Y-m-d g:i a',time())";
                    break;
                case 'VERSION':
                    $parseStr = 'THINK_VERSION';
                    break;
                case 'TEMPLATE':
                    $parseStr = "'".$this->templateFile."'";//'C("TEMPLATE_NAME")';
                    break;
                case 'LDELIM':
                    $parseStr = 'C("TMPL_L_DELIM")';
                    break;
                case 'RDELIM':
                    $parseStr = 'C("TMPL_R_DELIM")';
                    break;
                default:
                    if(defined($vars[1]))
                        $parseStr = $vars[1];
            }
        }
        return $parseStr;
    }
859462
public static function randStr($type, $length = 6)
    {
        // TODO 生成随机长度的字符串
        if ($type == self::ALPHABET) {
            return self::randAlphabet($length);
        } elseif ($type == self::ALPHABET_AND_NUMBERS) {
            return self::randAlphabetAndNumbers($length);
        } elseif ($type == self::NUMBERS) {
            return self::randNumbers($length);
        }

        return "unknown type";
    }
205783
function _strlen($str) {
        static $mbstring;

        // Cache expensive function_exists() calls.
        if (!isset($mbstring)) {
            $mbstring = function_exists('mb_strwidth');
        }

        if ($mbstring) {
            return mb_strwidth($str, $this->_charset);
        }

        return strlen($str);
    }
465264
public static function substr($str, $start, $length, $suffix = false) {
        if (mb_strlen($str, 'utf-8') <= $length) return $str;
        $slice = mb_substr($str, $start, $length, 'utf-8');
		if ($suffix) return $slice."…";
		return $slice;
	}
878548
protected static function interpolateVars($str, array $vars)
	{
		return preg_replace_callback(
			'(\\{@(\\w+)\\})',
			function ($m) use ($vars)
			{
				return (isset($vars[$m[1]])) ? $vars[$m[1]] : '';
			},
			$str
		);
	}
828492
public static function getString(&$str)
    {
        $p = mb_orig_strpos($str, "\x00");
        if ($p === false) {
            return '';
        }
        $r = mb_orig_substr($str, 0, $p);
        $str = mb_orig_substr($str, $p + 1);
        return $r;
    }
190082
public static function substr($str, $start = 0, $len = 50)
    {
        return mb_strlen($str) > $len ? mb_substr($str, $start, $len, 'UTF-8') . "…" : $str;
    }
1285208
static public function createNoncestr($length = 32, $str = "")
    {
        $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }
1127131
public function getString($varname)
    {
        $value = $this->get($varname);
        return $value === null ? null : trim($value);
    }
82468
public function widont($str) {
		$str = rtrim($str);
		$space = strrpos($str, ' ');

		if ($space !== false) {
			$str = substr($str, 0, $space) . '&nbsp;' . substr($str, $space + 1);
		}

		return $str;
	}
1041684
public function translate($string, Array $vars = array())
    {
        // Use custom translator
        if ($this->translator) {
            return $this->translator($this->getString($string), $vars);
        }

        return $this->compileString($this->getString($string), $vars);
    }
715941
public static function randStr($len = 6)
    {
        $chars = str_repeat('0123456789', $len);
        $chars = str_shuffle($chars);
        $str   = substr($chars, 0, $len);
        return $str;
    }
218637
public function getSubstr($str, $charset, $start, $length = null)
    {
        $mb = $this->getMbCharset($charset);
        if ($mb !== false) {
            return mb_substr($str, $start, $length, $mb);
        }
        $ic = $this->getIconvAlias($charset);
        if ($length === null) {
            $length = iconv_strlen($str, $ic);
        }
        return iconv_substr($str, $start, $length, $ic);
    }
801850
public function getDisplayHelpTextLstr(): ?Lstr {
		$helpText = $this->displayConfig->getHelpText();
		if ($helpText === null) {
			return null;
		}
		
		return Rocket::createLstr($helpText, $this->getEiMask()->getModuleNamespace())->t($n2nLocale);
	}
635070
public static function decode(String $str) : String
    {
        return preg_replace
        (
            array_keys(self::$scriptTagCharsDecode), 
            array_values(self::$scriptTagCharsDecode), 
            $str
        );
    }
1277719
public static List<String> findVars(final String str) {
        final List<String> vars = new ArrayList<String>();
        final char[] strArray = str.toCharArray();
        int i = 0;
        while (i < strArray.length - 1) {
            if (strArray[i] == VAR_START && strArray[i + 1] == VAR_LEFT_BOUND) {
                i = i + 2;
                final int begin = i;
                while (strArray[i] != VAR_RIGHT_BOUND) {
                    ++i;
                }
                final String var = str.substring(begin, i++);
                if (!vars.contains(var)) {
                    vars.add(var);
                }
            } else {
                ++i;
            }
        }
        return vars;
    }
1367014
private function parse_fun_var(& $str)
    {
        // 变量： 点号变量转为数组式变量  $a.b.c   =>  $a[b][c] 
        $str = preg_replace('/\$(\w+)\.(\w+)\.(\w+)\.(\w+)/is','$\\1[\'\\2\'][\'\\3\'][\'\\4\']',$str);
        $str = preg_replace('/\$(\w+)\.(\w+)\.(\w+)/is','$\\1[\'\\2\'][\'\\3\']',$str);
        $str = preg_replace('/\$(\w+)\.(\w+)/is','$\\1[\'\\2\']',$str);
        
        // 方法: {function()}}
        $str = preg_replace("/\{(\!?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:\\-\>$]*\(([^{}]*)\))\s*\}/", "<?php print_r(\\1); ?>", $str );
        
        // 方法: {function a=a1 b=b1}
        $str = \preg_replace_callback("/\{(\w+)\s+([^}]+)\}/i", array($this,'func_arr_params_callback'), $str);
        
        // 常量: {XXXX}
        $str = preg_replace ( "/\{([A-Z_\x7f-\xff][A-Z0-9_\x7f-\xff]*)\}/s", "<?php print_r(\\1); ?>", $str );
        
        // 变量: {$varname[]}, {$varname}
        //$str = preg_replace("/\{(\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff\[\]\'\"]*)\}/", "<?php print_r(isset(\\1) ? \\1 : '');\?\>", $str );
        
        // 其他: {$var...} to directly output, {$var||'abd'} or {$var}
        $str = preg_replace("/\{(\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*(?:[^{}]*))\|\|\s*([^}]+)\s*\}/s", "<?php print_r(isset(\\1) && \\1 ? \\1 : \\2); ?>", $str );
        $str = preg_replace("/\{(\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*(?:[^{}]*))\}/s", "<?php print_r(isset(\\1) ? \\1 : ''); ?>", $str );

        // 转义变量，放在后边
        /*
         * $str = preg_replace("/\{(\\$[a-zA-Z0-9_\[\]\'\"\$\x7f-\xff]+)\}/es", "\$this->addquote('<?php echo \\1; ?>')", $str);
         */
        $str = preg_replace_callback ("/\{(\\$[a-zA-Z0-9_\[\]\'\"\$\x7f-\xff]+)\}/s", array($this,'addquote_callback'), $str);
        
        return $str;
    }