<?php
include '../../system/db.class.php';
$ac = isset($_GET['ac']) ? $_GET['ac'] : NULL;
if($ac == 'android'){
	include '../../system/user.php';
	include_once '../zip/zip.php';
	header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
	header("Cache-Control: no-cache, must-revalidate");
	header("Pragma: no-cache");
	header("Content-type: text/html;charset=".IN_CHARSET);
	$GLOBALS['userlogined'] or exit('return_0');
	$GLOBALS['erduo_in_points'] < IN_WEBVIEWPOINTS and exit('return_1');
	$GLOBALS['db']->query("update ".tname('user')." set in_points=in_points-".IN_WEBVIEWPOINTS." where in_userid=".$GLOBALS['erduo_in_userid']);
	$ssl = is_ssl() ? 'https://' : 'http://';
	$title = unescape(SafeRequest("title","get"));
	$url = SafeRequest("url","get");
	$aicon = str_replace($ssl.$_SERVER['HTTP_HOST'].IN_PATH.'data', '../../../data', SafeRequest("aicon","get"));
	$dir = '../../../data/tmp';
	$time = $GLOBALS['erduo_in_userid'].'-'.time();
                creatdir($dir.'/'.$time);
                $unzip = new PclZip('../../../static/pack/webview/apk.zip');
                $unzip->extract(PCLZIP_OPT_PATH, $dir.'/'.$time, PCLZIP_OPT_REPLACE_NEWER);
	$var = ['folder' => $dir.'/', 'dir' => $time];
	$package = file_get_contents($var['folder'].$var['dir'].'/AndroidManifest.xml');
	$package = str_replace('package="com.appid', 'package="com.t'.time(), $package);
	fwrite(fopen($var['folder'].$var['dir'].'/AndroidManifest.xml', 'w+'), $package);
	$link = file_get_contents($var['folder'].$var['dir'].'/smali/com/appid/MainActivity.smali');
	$link = str_replace('[url]', $url, $link);
	fwrite(fopen($var['folder'].$var['dir'].'/smali/com/appid/MainActivity.smali', 'w+'), $link);
	$name = file_get_contents($var['folder'].$var['dir'].'/res/values/strings.xml');
	$name = str_replace('[name]', convert_charset($title), $name);
	fwrite(fopen($var['folder'].$var['dir'].'/res/values/strings.xml', 'w+'), $name);
	image_crop(72, 72, $aicon, $var['folder'].$var['dir'].'/res/mipmap-hdpi/ic_launcher.png');
	image_crop(162, 162, $aicon, $var['folder'].$var['dir'].'/res/mipmap-hdpi/ic_launcher_foreground.png');
	image_crop(72, 72, $aicon, $var['folder'].$var['dir'].'/res/mipmap-hdpi/ic_launcher_round.png');
	image_crop(48, 48, $aicon, $var['folder'].$var['dir'].'/res/mipmap-mdpi/ic_launcher.png');
	image_crop(108, 108, $aicon, $var['folder'].$var['dir'].'/res/mipmap-mdpi/ic_launcher_foreground.png');
	image_crop(48, 48, $aicon, $var['folder'].$var['dir'].'/res/mipmap-mdpi/ic_launcher_round.png');
	image_crop(96, 96, $aicon, $var['folder'].$var['dir'].'/res/mipmap-xhdpi/ic_launcher.png');
	image_crop(216, 216, $aicon, $var['folder'].$var['dir'].'/res/mipmap-xhdpi/ic_launcher_foreground.png');
	image_crop(96, 96, $aicon, $var['folder'].$var['dir'].'/res/mipmap-xhdpi/ic_launcher_round.png');
	image_crop(144, 144, $aicon, $var['folder'].$var['dir'].'/res/mipmap-xxhdpi/ic_launcher.png');
	image_crop(324, 324, $aicon, $var['folder'].$var['dir'].'/res/mipmap-xxhdpi/ic_launcher_foreground.png');
	image_crop(144, 144, $aicon, $var['folder'].$var['dir'].'/res/mipmap-xxhdpi/ic_launcher_round.png');
	image_crop(192, 192, $aicon, $var['folder'].$var['dir'].'/res/mipmap-xxxhdpi/ic_launcher.png');
	image_crop(432, 432, $aicon, $var['folder'].$var['dir'].'/res/mipmap-xxxhdpi/ic_launcher_foreground.png');
	image_crop(192, 192, $aicon, $var['folder'].$var['dir'].'/res/mipmap-xxxhdpi/ic_launcher_round.png');
	exec(IN_ROOT.'apktool/bin/java -jar '.IN_ROOT.'apktool/apktool.jar b '.$var['folder'].$var['dir'].' -o '.$var['folder'].$var['dir'].'/unsigned.apk');
	exec(IN_ROOT.'apktool/zipalign -v 4 '.$var['folder'].$var['dir'].'/unsigned.apk '.$var['folder'].$var['dir'].'/aligned.apk');
	exec(IN_ROOT.'apktool/apksigner sign --ks '.IN_ROOT.'apktool/release.jks --ks-pass pass:release --out '.$var['folder'].$var['dir'].'.apk '.$var['folder'].$var['dir'].'/aligned.apk');
	rename($aicon, '../../../data/tmp/'.$time.'.png');
	echo $time;
}else{
	if(!empty($_FILES)){
		$filepart = pathinfo($_FILES['android']['name']);
		$fileext = strtolower($filepart['extension']);
		if(in_array($fileext, array('jpg', 'jpeg', 'gif', 'png'))){
			$ext = $_POST['ext'];
			preg_match('/^(\d+\-\d+)$/', $ext) or exit('-1');
			$dir = '../../../data/tmp';
			creatdir($dir);
			@move_uploaded_file($_FILES['android']['tmp_name'], $dir.'/'.$ext.'.png');
			echo '1';
		} else{
			echo 'return_0';
		}
	}
}
?>