|
RewriteRule ^((.*/js/)?.*\.js)$ combinejs.php?path=$1&pre=$2&type=js [NC,L,QSA]
RewriteCond %{REQUEST_URI} !\.js$
RewriteCond %{REQUEST_URI} !combinejs\.php$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ http://js.wcdn.cn/$1 [NC,L,QSA,R]
<?php
require_once 'combine/JSCombineProcessor.php';
//t4/home/js/comp/content/publisherTop.js
$path = @$_GET['path'];
//t4/home/js/
$pre = @$_GET['pre'];
//D:\sina\t4\home\js\comp\content\publisherTop.js
$real_path = realpath($path);
if($real_path == false) {
$path = str_replace("/js/" , "/js/conf/" , $path);
$real_path = realpath($path);
if($real_path == false) {
$uri = $_SERVER['REQUEST_URI'];
JSCombineProcessor::getOnlineSource($uri);
return;
}
}
header ( 'Content-Type:application/x-javascript;Charset=UTF-8' );
//D:/sina/t4/home/js/comp/content/publisherTop.js
$real_path = str_replace('\\' , '/' , $real_path);
//D:/sina
$basepath = str_replace($path, "", $real_path);
//处理打包逻辑
$processor = new JSCombineProcessor($basepath , $path , $pre);
//获取文件内容
$fileContent = $processor -> getContent();
echo $fileContent;
?>
<?php
class JSCombineProcessor {
public $basePath;
public $relativePath;
public $prePath;
public $resultArr;
public $map;
public static function getOnlineSource($path) {
$api = "http://js.wcdn.cn".$path;
header('Location:'.$api);
/*
$api = "http://js.t.sinajs.cn".$path;
$host_ip = '211.101.171.217'; //ip地址
$dns_info = dns_get_record('js.t.sinajs.cn' , DNS_A);
if(count($dns_info) >= 1) {
$host_ip = $dns_info[0]['ip'];
}
$url_info = parse_url($api);
$api = str_replace($url_info['host'], $host_ip, $api); // 将域名替换成ip地址,指定到此ip地址下运行
$ch = curl_init($api);
curl_setopt($ch, CURLOPT_URL, $api);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curl_host = array("Host:{$url_info['host']}"); //绑定域名到此ip地址上
curl_setopt($ch, CURLOPT_HTTPHEADER, $curl_host); // 传送头信息
$result = curl_exec($ch);
$content_type = '';
// Check if any error occured
if(!curl_errno($ch))
{
$info = curl_getinfo($ch);
$content_type = $info['content_type'];
header('Content-Type:'.$content_type);
}
echo $result;
// Close handle
curl_close($ch);
*/
}
public function __construct($basePath , $relativePath , $prePath) {
$this->basePath = $basePath;
$this->relativePath = $relativePath;
$this->prePath = $basePath.$prePath;
$this->resultArr = array();
$this->map = array();
}
public function checkFileBom($path) {
$chars = array(239,187,191);
$lines = file($path);
$bom = false;
if(count($lines) >= 1) {
$line = $lines[0] ;
if(strlen($line) >= 3) {
$bom = true;
for($i = 0 ; $i < 3 ; $i++) {
if(ord($line[$i]) != $chars[$i]) {
$bom = false;
break;
}
}
}
}
return $bom;
}
public function getContent() {
$entry = $this->basePath.$this->relativePath;
$this->processOneFile($entry);
return implode('',$this->resultArr);
}
public function processOneFile($filePath , $mapkey=false) {
$real_path = realpath($filePath);
if($real_path == false && $mapkey) {
$this->map[$mapkey] = 1;
return;
}
$isutf8bom = $this->checkFileBom($filePath);
if($isutf8bom) {
echo "alert('文件:".$filePath." 有utf8 bom 头,请修改')";
exit;
}
$list = file($filePath);
foreach($list as $line) {
preg_match ( '/^\s*\$import\s*\(\s*([\'"])\s*(.+)\s*\1\s*\).*$/i', $line, $aMatch );
if($aMatch) {
$namespace = $aMatch[2];
$replace = str_replace(".", "/", $namespace);
$entry = $this->prePath.$replace.".js";
if(!array_key_exists($namespace, $this->map)) {
$this->map[$namespace] = 1;
$this->processOneFile($entry , $namespace);
}
} else {
$this->resultArr[] = $line;
}
}
}
}
?> |
|
|