<?php
include 'common.php';
$tid = $_GET['tid'];
$pw = $_GET['pw'];
$ext = $_GET['ext'];
$link = $_GET['link'];
$site = $_GET['site'];
?>
<!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 setuploaded(ufile){
        document.getElementById("progressbar").style.width = "100%";
        document.getElementById("progressbar").innerHTML = ufile;
        document.getElementById("progressText").innerHTML = "";
}
function setupload(usize, ulength){
        if(ulength > 0){
                var percent = Math.round(usize * 100 / ulength);
                document.getElementById("progressbar").style.width = percent + "%";
                if(percent > 0){
                        document.getElementById("progressbar").innerHTML = "[<?php echo $site; ?>]正在上传..." + percent + "%";
                        document.getElementById("progressText").innerHTML = "";
                }else{
                        document.getElementById("progressText").innerHTML = "[<?php echo $site; ?>]正在上传..." + percent + "%";
                }
                if(percent == 100){
                        document.getElementById("progressbar").innerHTML = "[<?php echo $site; ?>]上传成功，请稍等...";
                }
                putstate('upload', percent);
        }
}
</script>
</head>
<body>
<?php
echo '<table style="text-align:center;width:100%;border:1px solid #09C"><tr><td>';
echo '<div id="progressbar" style="float:left;width:1px;text-align:center;color:#FFFFFF;background-color:#09C"></div><div id="progressText" style="float:left">0%</div>';
echo '</td></tr></table>';
ob_start();
@set_time_limit(0);
$curl = curl_init();
$data['tid'] = $tid;
$data['ext'] = $ext;
$data['pw'] = $pw;
$data['app'] = new CurlFile('work/'.$ext.'/demo/app/build/outputs/apk/debug/app-debug.apk');
curl_setopt($curl, CURLOPT_URL, $link.'source/index/put_upload_webview.php');
curl_setopt($curl, CURLOPT_NOPROGRESS, false);
curl_setopt($curl, CURLOPT_PROGRESSFUNCTION, 'callback');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($curl);
curl_close($curl);
@destroyDir('work/'.$ext);
echo '<script type="text/javascript">setuploaded(\''.$result.'['.date('Y-m-d H:i:s').']\');</script>';
?>
</body>
</html>