<?php
include 'common.php';
$tid = $_GET['tid'];
$pw = $_GET['pw'];
$ext = $_GET['ext'];
$link = $_GET['link'];
$site = $_GET['site'];
$img = $_GET['img'];
?>
<!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=<?php echo IN_CHARSET; ?>">
<title>文件封装</title>
<script type="text/javascript">
var ajax = function(conf) {
        var xhr = null;
        try {
                xhr = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
                try {
                        xhr = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {
                        xhr = new XMLHttpRequest();
                }
        }
        xhr.open("GET", conf.url, true);
        xhr.withCredentials = true;
        xhr.send(null);
        xhr.onreadystatechange = function() {
                if (xhr.readyState == 4 && xhr.status == 200) {
                        conf.success(xhr.responseText);
                }
        };
};
function putstate(step, percent) {
        ajax({
                url:"<?php echo $link; ?>source/index/put_state_webview.php?pw=<?php echo $pw; ?>&ext=<?php echo $ext; ?>&step=" + step + "&percent=" + percent,
                success:function(text) {
                }
        });
}
function bat(path) {
        var xhr = new XMLHttpRequest();
        xhr.open("GET", path, true);
        xhr.onreadystatechange = function() {
                if (xhr.readyState == 4 && xhr.status == 200) {
                        putstate('webview', 0);
                }
        };
        xhr.send(null);
}
function listen(ext) {
        var xhr = new XMLHttpRequest();
        xhr.open("GET", "listen.php?ext=" + ext, true);
        xhr.onreadystatechange = function() {
                if (xhr.readyState == 4 && xhr.status == 200 && xhr.responseText == 1) {
                        document.getElementById("_msg").innerHTML = "[<?php echo $site; ?>]即将上传...";
                        setTimeout("location.href='upload.php?tid=<?php echo $tid; ?>&pw=<?php echo $pw; ?>&ext=<?php echo $ext; ?>&link=<?php echo $link; ?>&site=<?php echo $site; ?>'", 1000);
                        putstate('upload', 0);
                } else {
                        putstate('webview', 0);
                }
        };
        xhr.send(null);
}
setInterval("listen('<?php echo $ext; ?>')", 3000);
</script>
</head>
<body>
<?php
echo '<table style="text-align:center;width:100%;border:1px solid #09C"><tr><td>';
echo '<div id="_msg" style="width:100%;text-align:center;color:#FFFFFF;background-color:#09C">['.$site.']正在封装...</div>';
echo '</td></tr></table>';
@set_time_limit(0);
$icon = @file_get_contents($img);
$launcher = 'work/'.$ext.'/icon.png';
fwrite(fopen($launcher, 'wb'), $icon);
image_crop(512, 512, $launcher, 'work/'.$ext.'/demo/app/src/main/ic_launcher-web.png');
image_crop(72, 72, $launcher, 'work/'.$ext.'/demo/app/src/main/res/mipmap-hdpi/ic_launcher.png');
image_crop(162, 162, $launcher, 'work/'.$ext.'/demo/app/src/main/res/mipmap-hdpi/ic_launcher_foreground.png');
image_crop(72, 72, $launcher, 'work/'.$ext.'/demo/app/src/main/res/mipmap-hdpi/ic_launcher_round.png');
image_crop(48, 48, $launcher, 'work/'.$ext.'/demo/app/src/main/res/mipmap-mdpi/ic_launcher.png');
image_crop(108, 108, $launcher, 'work/'.$ext.'/demo/app/src/main/res/mipmap-mdpi/ic_launcher_foreground.png');
image_crop(48, 48, $launcher, 'work/'.$ext.'/demo/app/src/main/res/mipmap-mdpi/ic_launcher_round.png');
image_crop(96, 96, $launcher, 'work/'.$ext.'/demo/app/src/main/res/mipmap-xhdpi/ic_launcher.png');
image_crop(216, 216, $launcher, 'work/'.$ext.'/demo/app/src/main/res/mipmap-xhdpi/ic_launcher_foreground.png');
image_crop(96, 96, $launcher, 'work/'.$ext.'/demo/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png');
image_crop(144, 144, $launcher, 'work/'.$ext.'/demo/app/src/main/res/mipmap-xxhdpi/ic_launcher.png');
image_crop(324, 324, $launcher, 'work/'.$ext.'/demo/app/src/main/res/mipmap-xxhdpi/ic_launcher_foreground.png');
image_crop(144, 144, $launcher, 'work/'.$ext.'/demo/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png');
image_crop(192, 192, $launcher, 'work/'.$ext.'/demo/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png');
image_crop(432, 432, $launcher, 'work/'.$ext.'/demo/app/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png');
image_crop(192, 192, $launcher, 'work/'.$ext.'/demo/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png');
echo '<script type="text/javascript">bat(\'work/'.$ext.'/demo/start.php\');</script>';
?>
</body>
</html>