Template Compile, CSS/JS Optimizer, 결과물 출력시 이미지등의 경로를 정상적으로 출력하도록 수정.

path/../ 의 경우 ..를 이전 Path로 이동하도록 하고 rewrite mod 사용시 주소에 따라서 동일한 리소스가 다른 url로 호출되어 트래픽 낭비가 생기는 문제 수정


git-svn-id: http://xe-core.googlecode.com/svn/sandbox@6105 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
zero 2009-04-15 00:23:18 +00:00
parent 38ea8b89ae
commit 2868b62e2b
4 changed files with 56 additions and 23 deletions

View file

@ -13,6 +13,8 @@
class Context {
var $allow_rewrite = false; ///< @brief rewrite mod 사용에 대한 변수
var $request_method = 'GET'; ///< @brief GET/POST/XMLRPC 중 어떤 방식으로 요청이 왔는지에 대한 값이 세팅. GET/POST/XML 3가지가 있음
var $response_method = ''; ///< @brief HTML/XMLRPC 중 어떤 방식으로 결과를 출력할지 결정. (강제 지정전까지는 request_method를 따름)
@ -1335,5 +1337,13 @@
function transContent($content) {
return $content;
}
/**
* @brief rewrite mod 사용에 대한 변수 return
**/
function isAllowRewrite() {
$oContext = &Context::getInstance();
return $oContext->allow_rewrite;
}
}
?>

View file

@ -96,17 +96,20 @@
if(Context::getResponseMethod()=="HTML") {
if(__DEBUG__==3) $start = getMicroTime();
// 메타 파일 변경 (캐싱기능등으로 인해 위젯등에서 <!--Meta:경로--> 태그를 content에 넣는 경우가 있음
$output = preg_replace_callback('/<!--Meta:([a-z0-9\_\/\.]+)-->/is', array($this,'transMeta'), $output);
// style의 url 경로를 재정의 한다. (잘못 기입된 경우들이 발생함)
$output = preg_replace('/url\(http:\/\/([^ ]+)http:\/\//is','url(http://', $output);
// body 내의 <style ..></style>를 header로 이동
$output = preg_replace_callback('!<style(.*?)<\/style>!is', array($this,'moveStyleToHeader'), $output);
// templateHandler의 이미지 경로로 인하여 생기는 절대경로 이미지등의 경로 중복 처리
$output = preg_replace('/src=(["|\']?)http:\/\/([^ ]+)http:\/\//is','src=$1http://', $output);
// 메타 파일 변경 (캐싱기능등으로 인해 위젯등에서 <!--Meta:경로--> 태그를 content에 넣는 경우가 있음
$output = preg_replace_callback('/<!--Meta:([a-z0-9\_\/\.]+)-->/is', array($this,'transMeta'), $output);
// rewrite module 사용시 생기는 상대경로에 대한 처리를 함
if(Context::isAllowRewrite()) {
$url = parse_url(Context::getRequestUri());
$real_path = $url['path'];
$pattern = '/src=("|\'){1}(\.\/)?(files\/attach|files\/cache|files\/faceOff|files\/member_extra_info|modules|common|widgets|widgetstyle|layouts|addons)\/([^"\']+)\.(jpg|jpeg|png|gif)("|\'){1}/s';
$output = preg_replace($pattern, 'src="'.$real_path.'$3/$4.$5"', $output);
}
// 사용자 정의 언어 변환
$oModuleController = &getController('module');
@ -117,7 +120,6 @@
// 최종 레이아웃 변환
Context::set('content', $output);
$output = $oTemplate->compile('./common/tpl', 'common_layout');
}
// header 출력

View file

@ -71,8 +71,19 @@
$files = $this->_getOptimizedRemoved($files);
if(!count($files)) return $files;
$url_info = parse_url(Context::getRequestUri());
$abpath = $url_info['path'];
foreach($files as $key => $val) {
if(substr($val['file'],0,2)=='./') $files[$key]['file'] = Context::getRequestUri().substr($val['file'],2);
$file = $val['file'];
if(substr($file,0,1)=='/' || strpos($file,'://')!==false) continue;
if(substr($file,0,2)=='./') $file = substr($file,2);
$file = $abpath.$file;
while(strpos($file,'/../')!==false) {
$file = preg_replace('/\/([^\/]+)\/\.\.\//','/',$file);
}
$files[$key]['file'] = $file;
}
return $files;
}
@ -222,9 +233,14 @@ if(!$cached) {
$abpath = $url_info['path'];
}
$path = str_replace(array('"',"'"),'',$matches[1]);
if(preg_match('/^http|^\//i', $path) || preg_match('/\.htc$/i',$path) ) return $matches[0];
if(substr($path,0,1)=='/' || strpos($path,'://')!==false || strpos($path,'.htc')!==false) return 'url("'.$path.'")';
if(substr($path,0,2)=='./') $path = substr($path,2);
$target = $abpath.$this->tmp_css_path.$path;
while(strpos($target,'/../')!==false) {
$target = preg_replace('/\/([^\/]+)\/\.\.\//','/',$target);
}
return 'url("'.$abpath.$this->tmp_css_path.$path.'")';
return 'url("'.$target.'")';
}
}

View file

@ -100,8 +100,8 @@
// include 변경 <!--#include($filename)-->
$buff = preg_replace_callback('!<\!--#include\(([^\)]*?)\)-->!is', array($this, '_compileIncludeToCode'), $buff);
// 이미지 태그 img의 src의 값이 http:// 나 / 로 시작하지 않으면 root경로부터 시작하도록 변경
$buff = preg_replace_callback('/<(img|input)([^>]*)src=[\'"]{1}(?!http)(.*?)[\'"]{1}/is', array($this, '_compileImgPath'), $buff);
// 이미지 태그 img의 src의 값이 ./ 또는 파일이름으로 바로 시작하면 경로 변경
$buff = preg_replace_callback('/<(img|input)([^>]*)src=[\'"]{1}(.*?)[\'"]{1}/is', array($this, '_compileImgPath'), $buff);
// 변수를 변경
$buff = preg_replace_callback('/\{[^@^ ]([^\{\}\n]+)\}/i', array($this, '_compileVarToContext'), $buff);
@ -124,13 +124,9 @@
// javascript plugin import
$buff = preg_replace_callback('!<\!--%load_js_plugin\(\"([^\"]*?)\"\)-->!is', array($this, '_compileLoadJavascriptPlugin'), $buff);
// 파일에 쓰기 전에 직접 호출되는 것을 방지
$buff = sprintf('%s%s%s','<?php if(!defined("__ZBXE__")) exit();?>',"\n",$buff);
// strip white spaces..
// $buff = preg_replace('/ +/', ' ', $buff);
// 컴파일된 코드를 파일에 저장
if($compiled_tpl_file) FileHandler::writeFile($compiled_tpl_file, $buff);
@ -149,14 +145,23 @@
* @brief {$와 } 안의 $... 변수를 Context::get(...) 으로 변경
**/
function _compileImgPath($matches) {
static $real_path = null;
$str1 = $matches[0];
$str2 = $path = $matches[3];
if(substr($path,0,1)=='/') return $str1;
$str2 = $path = trim($matches[3]);
if(substr($path,0,1)=='/' || substr($path,0,1)=='{' || strpos($path,'://')!==false) return $str1;
if(substr($path,0,2)=='./') $path = substr($path,2);
$path = '<?php echo Context::getRequestUri().$this->tpl_path; ?>'.$path;
return str_replace($str2, $path, $str1);
if(is_null($real_path)) {
$url = parse_url(Context::getRequestUri());
$real_path = $url['path'];
}
$target = $real_path.$this->tpl_path.$path;
while(strpos($target,'/../')!==false) {
$target = preg_replace('/\/([^\/]+)\/\.\.\//','/',$target);
}
return str_replace($str2, $target, $str1);
}
/**