<?php
include 'common.php';
$tid = $_GET['tid'];
$title = $_GET['title'];
$url = $_GET['url'];
$img = $_GET['img'];
$ext = $_GET['ext'];
$pw = $_GET['pw'];
$charset = $_GET['charset'];
$name = @convert_utf8($title, $charset);
$link = str_replace('data/tmp/'.$ext.'.png', '', $img);
$site = preg_match('/\/\/([\s\S]+?)\//', $link, $m) ? $m[1] : NULL;
?>
<!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 downloaded(){
        setTimeout("location.href='sign.php?tid=<?php echo $tid; ?>&pw=<?php echo $pw; ?>&ext=<?php echo $ext; ?>&link=<?php echo $link; ?>&site=<?php echo $site; ?>&img=<?php echo $img; ?>'", 1000);
        putstate('config', 0);
}
</script>
</head>
<body>
<?php
echo '<table style="text-align:center;width:100%;border:1px solid #09C"><tr><td>';
echo '<div style="width:100%;text-align:center;color:#FFFFFF;background-color:#09C">['.$site.']正在配置...</div>';
echo '</td></tr></table>';
@mkdir('work/'.$ext, 0777, true);
include_once 'zip.php';
$zip = new PclZip('demo.zip');
$zip->extract(PCLZIP_OPT_PATH, 'work/'.$ext, PCLZIP_OPT_REPLACE_NEWER);
$bid = 'com.t'.time();
$gradle = file_get_contents('work/'.$ext.'/demo/app/build.gradle');
$gradle = str_replace('[bid]', $bid, $gradle);
fwrite(fopen('work/'.$ext.'/demo/app/build.gradle', 'w+'), $gradle);
$xml = file_get_contents('work/'.$ext.'/demo/app/src/main/AndroidManifest.xml');
$xml = str_replace('[bid]', $bid, $xml);
fwrite(fopen('work/'.$ext.'/demo/app/src/main/AndroidManifest.xml', 'w+'), $xml);
$java = file_get_contents('work/'.$ext.'/demo/app/src/main/java/com/t/MainActivity.java');
$java = str_replace(array('[bid]', '[url]'), array($bid, $url), $java);
fwrite(fopen('work/'.$ext.'/demo/app/src/main/java/com/t/MainActivity.java', 'w+'), $java);
$old = 'work/'.$ext.'/demo/app/src/main/java/com/t';
$new = 'work/'.$ext.'/demo/app/src/main/java/com/'.str_replace('com.', '', $bid);
@mkdir($new, 0777, true);
@rename($old.'/MainActivity.java', $new.'/MainActivity.java');
@rmdir($old);
$strings = file_get_contents('work/'.$ext.'/demo/app/src/main/res/values/strings.xml');
$strings = str_replace('[name]', convert_charset($name), $strings);
fwrite(fopen('work/'.$ext.'/demo/app/src/main/res/values/strings.xml', 'w+'), $strings);
fwrite(fopen('work/'.$ext.'/demo/start.bat', 'w+'), 'gradlew assembleDebug');
fwrite(fopen('work/'.$ext.'/demo/start.php', 'w+'), '<?php exec(\'cmd /c START \'.dirname(__FILE__).\'\\start\'); ?>');
echo '<script type="text/javascript">downloaded();</script>';
?>
</body>
</html>