Example #1
0
     */
    public static function conv_from($str, $fsencoding)
    {
        if ($fsencoding != '' && $fsencoding != self::$encoding) {
            $str = iconv($fsencoding, self::$encoding, $str);
        }
        return $str;
    }
    /**
     * 当文件上传/保存过程中发生错误时设置错误信息
     *
     * @param string $str 错误信息
     * @return string 空字符串
     */
    public static function error($str)
    {
        self::$last_error = $str;
        return '';
    }
    /**
     * 获取错误信息
     *
     * @return string
     */
    public static function last_error()
    {
        return self::$last_error;
    }
}
File::$encoding = Config::get('main', 'encoding', 'encoding');
Example #2
0
 /**
  * 编译指定的模板
  *
  * @param string $src 源文件
  * @param string $dest 目标文件
  * @param string $theme 模板的主题
  */
 protected static function _compile($src, $dest, $theme)
 {
     if (!file_exists($src)) {
         if (DEBUG) {
             throw new Minifw\Exception('模板不存在:' . $src);
         } else {
             return false;
         }
     }
     //global $config;
     $srctime = filemtime($src);
     $desttime = 0;
     if (file_exists($dest)) {
         $desttime = filemtime($dest);
     }
     if (self::$always_compile == 1 || $desttime == 0 || $desttime <= $srctime) {
         $str = file_get_contents($src);
         /* 处理模板中的处理逻辑语句——开始 */
         $str = preg_replace('/\\<{inc (\\S*?)\\s*}\\>/', '<?php ' . __NAMESPACE__ . '\\Tpl::_inc("/$1",[],"' . $theme . '"); ?>', $str);
         $str = preg_replace('/\\<{inc (\\S*?) (\\S*?)\\s*}\\>/', '<?php ' . __NAMESPACE__ . '\\Tpl::_inc("/$1",$2,"' . $theme . '"); ?>', $str);
         $str = preg_replace('/\\<{inc (\\S*?) (\\S*?) (\\S*?)\\s*}\\>/', '<?php ' . __NAMESPACE__ . '\\Tpl::_inc("/$1",$2,"$3"); ?>', $str);
         $str = preg_replace('/\\<{=(.*?)}\\>/', '<?= ($1); ?>', $str);
         $str = preg_replace('/\\<{if (.*?)}\\>/', '<?php if($1){ ?>', $str);
         $str = preg_replace('/\\<{elseif (.*?)}\\>/', '<?php }elseif($1){ ?>', $str);
         $str = preg_replace('/\\<{else}\\>/', '<?php }else{ ?>', $str);
         $str = preg_replace('/\\<{\\/if}\\>/', '<?php } ?>', $str);
         $str = preg_replace('/\\<{for (\\S*?) (\\S*?) (\\S*?)\\s*?}\\>/', '<?php for($1=$2; $1 <= $3; $1++){ ?>', $str);
         $str = preg_replace('/\\<{\\/for}\\>/', '<?php } ?>', $str);
         $str = preg_replace('/\\<{foreach (\\S*?) (\\S*?)}\\>/', '<?php foreach($1 as $2){ ?>', $str);
         $str = preg_replace('/\\<{foreach (\\S*?) (\\S*?) (\\S*?)\\s*?}\\>/', '<?php foreach($1 as $2 => $3){ ?>', $str);
         $str = preg_replace('/\\<{\\/foreach}\\>/', '<?php } ?>', $str);
         $str = preg_replace('/\\<{(\\S.*?)}\\>/', '<?php $1; ?>', $str);
         $str = preg_replace('/\\<{\\*((.|\\r|\\n)*?)\\*}\\>/', '', $str);
         /* 处理模板中的处理逻辑语句——完成 */
         //处理相对路径:"/xxxx/yyyy"
         $str = preg_replace('/\\<link (.*?)href="\\/([^"]*)"(.*?) \\/\\>/i', '<link $1 href="' . self::$res_path . '/' . $theme . '/$2" $3 />', $str);
         $str = preg_replace('/\\<script (.*?)src="\\/([^"]*)"(.*?)\\>/i', '<script $1 src="' . self::$res_path . '/' . $theme . '/$2" $3>', $str);
         $str = preg_replace('/\\<img (.*?)src="\\/([^"]*)"(.*?) \\/\\>/i', '<img $1 src="' . self::$res_path . '/' . $theme . '/$2" $3 />', $str);
         //处理绝对路径:"|xxx/yyy"
         $str = preg_replace('/\\<link (.*?)href="\\|([^"]*)"(.*?) \\/\\>/i', '<link $1 href="/www/$2" $3 />', $str);
         $str = preg_replace('/\\<script (.*?)src="\\|([^"]*)"(.*?)\\>/i', '<script $1 src="/www/$2" $3>', $str);
         $str = preg_replace('/\\<img (.*?)src="\\|([^"]*)"(.*?) \\/\\>/i', '<img $1 src="/www/$2" $3 />', $str);
         /* 处理绝对路径——完成 */
         //处理原始路径:"\xxx/yyy"
         $str = preg_replace('/\\<link (.*?)href="\\\\([^"]*)"(.*?) \\/\\>/i', '<link $1 href="/$2" $3 />', $str);
         $str = preg_replace('/\\<script (.*?)src="\\\\([^"]*)"(.*?)\\>/i', '<script $1 src="/$2" $3>', $str);
         $str = preg_replace('/\\<img (.*?)src="\\\\([^"]*)"(.*?) \\/\\>/i', '<img $1 src="/$2" $3 />', $str);
         /* 处理原始路径——完成 */
         /* 删除模板中多余的空行和空格——开始 */
         $str = preg_replace('/^\\s*(.*?)\\s*$/im', '$1', $str);
         $str = preg_replace('/\\r|\\n/', '', $str);
         $str = preg_replace('/\\>\\s*\\</', '>$1<', $str);
         $str = preg_replace('/\\s*\\?\\>\\s*\\<\\?php\\s*/', '', $str);
         $str = preg_replace('/\\>\\s*(.*?)\\s*\\</', '>$1<', $str);
         $str = preg_replace('/\\s{2,}/i', ' ', $str);
         $str = preg_replace('/\\?\\>$/i', '', $str);
         /* 删除模板中多余的空行和空格——完成 */
         Minifw\File::mkdir(dirname($dest));
         if (!file_put_contents($dest, $str)) {
             return fasle;
         }
     }
     return true;
 }