<?php
if (isset($_FILES["up"]["name"]))
procedureUpload();
//本地调试
if (!defined('SAE_TMP_PATH')) {
define( 'SAE_TMP_PATH', 'tmp' );
}
//主函数,接收post上传过来的文件然后解包
function procedureUpload() {
if (strcasecmp(substr($_FILES["up"]["name"], strlen($_FILES["up"]["name"]) -4), ".tsk")) {
echo "错误:文件不是.tsk文件!";
exit;
}
if ($_FILES["up"]["error"] > 0)
{
echo "上传错误:,错误代码:" . $_FILES["up"]["error"];
exit;
} else {
$result = unpackTSk($_FILES["up"]["tmp_name"], $name);
if (!is_array($result)) { //不是数组,那就是出错了
echo $result;
} else {
$returnZip = SAE_TMP_PATH . '/' . basename($_FILES["up"]["tmp_name"]) .".zip";
packZIP($result, $returnZip);
if (!file_exists($returnZip)) {
echo "错误:文件打包失败";
exit;
}
header("Content-type: application/zip");
header("Accept-Ranges: bytes");
header("Accept-Length: ".filesize($returnZip));
header("Content-Disposition: attachment; filename=". $name . ".zip");
readfile($returnZip);
exit;
}
}
}
//TSK皮肤文件解包方法,关于这个文件的格式我在之前的博文中已经分析过了,这里就不再写关于文件格式的注释了
function unpackTSK($filepath, &$themeName = '') {
if (!file_exists($filepath))
return "错误: 文件不存在或不是tsk文件";
$tkRes = file_get_contents($filepath);
if (strcmp(pack("a4L3","TPAK", 1, 0 ,0), substr($tkRes, 0, 16)))
return "错误:文件头校验错误,文件可能已经损坏";
//主题名
$ptr = 16;
$num = unpack("L2",substr($tkRes, 16, 8));
$themeName = unpack("a" . $num[2], substr($tkRes, 24, $num[2]));
$themeName = $themeName[1];
//print_r($themeName);//utf-8编码
//文件数目
$ptr = 20 + $num[1];
$num = unpack("L",substr($tkRes, $ptr, 4));
$num = $num[1] /8 -1;
//echo $num;
$ptr += 4;
//本地调试 windows 创建文件夹 gbk编码
//正式部署的时候直接注释掉吧
if (getenv('OS') == 'Windows_NT')
$themeName = iconv('UTF-8', 'GBK', $themeName);
$outputDir = SAE_TMP_PATH . '/' . $themeName;
mkdir($outputDir, 0777, true);
for($i = 0; $i < $num; $i++) {
$itemNameLoc = unpack("L",substr($tkRes, $ptr, 4));
$itemNameLen = unpack("L",substr($tkRes, $itemNameLoc[1], 4));
$itemName = unpack("a" . $itemNameLen[1], substr($tkRes, $itemNameLoc[1]+4, $itemNameLen[1]));
$itemName = $itemName[1];
$ptr += 4;
$itemContentLoc = unpack("L",substr($tkRes, $ptr, 4));
$ptr += 8;
$nextItemContentLoc = unpack("L",substr($tkRes, $ptr, 4));
$itemContentLen = $nextItemContentLoc[1] -$itemContentLoc[1];
$itemContent = unpack("a" . $itemContentLen, substr($tkRes, $itemContentLoc[1], $itemContentLen));
file_put_contents($outputDir . $itemName, $itemContent); //sae临时文件夹
//echo $itemName . "\n";
$returnFileNames[] = $outputDir . $itemName;
$ptr -= 4;
}
return $returnFileNames;
//echo strlen($tkRes);
}
//打包zip文件
function packZIP($files, $output) {
include_once('pclzip4sae.lib.php');
//这个pclzip我只是修改了开头关于临时文件夹常量的定义,以适应sae,其他没什么特别的
$archive = new PclZip($output);
$v_list = $archive->create($files,
PCLZIP_OPT_REMOVE_PATH, SAE_TMP_PATH
);
if ($v_list == 0)
die("Error : ".$archive->errorInfo(true));
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>天天动听皮肤解包工具 - 在线版</title>
<style>
body {
background:#f5f5f5;
}
.input-file {
overflow:hidden;
position:relative;
width:80px;
height:30px;
line-height:30px;
background:#EEE;
border:1px #666 solid;
display:inline-block;
text-align:center;
font-size:18px;
}
.input-file:hover {
background:#CCC;
}
.input-file input{
opacity:0;
filter:alpha(opacity=0);
font-size:100px;
position:absolute;
top:0;
right:0;
}
img {border:0}
.content {margin:6em auto; width:500px;border:1px #efefef solid;padding:20px;background:#fafafa;}
</style>
<body>
<div class="content">
<center><h1>天天动听皮肤解包工具 - 云端版</h1></center><br />
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
第一步、选择你想要解包的皮肤文件:<span class="input-file">浏览...
<input type="file" name="up" id="up" /></span><br />第二步、提交上传
<input type="submit" name="submit" id="submit" class="input-file" value="提交" style=" height: 32px; " />
</form>
<br />第三步、云端完成解包工作,并将直接弹出下载。<br /><br /><br />
开发者:<a href="http://weibo.com/horsley" target="_blank">@horsley</a> | <a href="http://ihorsley.com" target="_blank">Horsley实验室</a>
( Powered by
<a href="http://sae.sina.com.cn" target="_blank"><img src="http://static.sae.sina.com.cn/image/poweredby/117X12px.gif" title="Powered by Sina App Engine"></a>)
</div>
</body>
</html>