<?php

if (!isset($_SERVER['HTTPS']) || empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] !== 'on') {
	echo '';
	exit();
}

// apt/cydia's useragent is "Telesphoreo APT-HTTP"
if (0 == preg_match('/Telesphoreo/',$_SERVER['HTTP_USER_AGENT'])) {
	//$f = fopen('t', 'w');
	//fwrite($f, $_SERVER['HTTP_USER_AGENT']);
	//fclose($f);
	echo '';
	exit();
}

// apache_request_headers()
// X-Machine: iPhone3,1
// X-Unique-ID: cb1eb2b9f0731bd1a49f161fd3130892597c19e7
// X-Firmware: 6.1.2
//
// $_SERVER['HTTP_X_MACHINE']: iPhone3,1
// $_SERVER['HTTP_X_UNIQUE_ID']: cb1eb2b9f0731bd1a49f161fd3130892597c19e7
// $_SERVER['HTTP_X_FIRMWARE']: 6.1.2

error_reporting(0); # 0 for production

//require_once('iskrip.php');

$host = 'localhost';
$user = 'devbug';
$password = '#include';
$db = 'cydia';

$bid = $_GET['bid'];
$version = $_GET['version'];
$udid = $_SERVER['HTTP_X_UNIQUE_ID'];


$is_valid_user = false;
// >> it's me
// iPhone 4
if ($udid == "cb1eb2b9f0731bd1a49f161fd3130892597c19e7") {
	$is_valid_user = true;
}
// iPhone 5
else if ($udid == "b57f6da5c2b1ab5ce75a675e1cc2d21f12d7700e") {
	$is_valid_user = true;
}
// iPad 2
else if ($udid == "97b1261cdab4925c3707661137acf8e19355a253") {
	$is_valid_user = true;
}
// iPhone 3Gs
else if ($udid == "43fb3f43acb82522737479d90d6edb48012a765e") {
	$is_valid_user = true;
}
// iPhone 5s
else if ($udid == "0e865793b03b158e6275b904f76acdb23785b8a0") {
	$is_valid_user = true;
}
// << it's me

if ($is_valid_user === false) {
	header($_SERVER['SERVER_PROTOCOL'] . ' 401 Unauthorized. error: can not activate user.', true, 500);
	die('<b>error: can not activate user</b>');
}

$file = "./_chiyo_packs_/".$bid."_".$version."_iphoneos-arm.deb";

	//First, see if the file exists
	if (!is_file($file)) { die("<b>404 File not found!</b>"); }
	
$d = mysql_connect($host, $user, $password);
if (!$d) {
	die('Could not connect: '.mysql_error());
}

if (!mysql_select_db($db)) {
	mysql_close($d);
	die('Could not select database: '.mysql_error());
}

$q = sprintf('INSERT INTO `down_counter` (`bid`,`version`) VALUES("%s","%s") ON DUPLICATE KEY UPDATE `timestamp`=CURRENT_TIMESTAMP(),`count`=`count`+1', mysql_real_escape_string($bid), mysql_real_escape_string($version));

if (!mysql_query($q)) {
	$q = '';
	//header($header.': Could not update table: '.mysql_error());
	//mysql_close($d);
	//exit();
}

mysql_close($d);
	
	
	
	//Gather relevent info about file
	$len = filesize($file);
	$filename = basename($file);
	$file_extension = strtolower(substr(strrchr($filename,"."),1));
	
	//This will set the Content-Type to the appropriate setting for the file
	switch( $file_extension ) {
		case "pdf": $ctype="application/pdf"; break;
		case "exe": $ctype="application/octet-stream"; break;
		case "zip": $ctype="application/zip"; break;
		case "doc": $ctype="application/msword"; break;
		case "xls": $ctype="application/vnd.ms-excel"; break;
		case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
		case "gif": $ctype="image/gif"; break;
		case "png": $ctype="image/png"; break;
		case "jpeg":
		case "jpg": $ctype="image/jpg"; break;
		case "mp3": $ctype="audio/mpeg"; break;
		case "wav": $ctype="audio/x-wav"; break;
		case "mpeg":
		case "mpg":
		case "mpe": $ctype="video/mpeg"; break;
		case "mov": $ctype="video/quicktime"; break;
		case "avi": $ctype="video/x-msvideo"; break;
		
		//The following are for extensions that shouldn't be downloaded (sensitive stuff, like php files)
		case "php":
		case "htm":
		case "html":
		case "txt": die("<b>Cannot be used for ". $file_extension ." files!</b>"); break;
		
		default: $ctype="application/force-download";
	}
	
	//Begin writing headers
	header("Pragma: public");
	header("Expires: 0");
	header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
	header("Cache-Control: public"); 
	header("Content-Description: File Transfer");
	
	//Use the switch-generated Content-Type
	header("Content-Type: $ctype");
	
	//Force the download
	$header="Content-Disposition: attachment; filename=".$filename.";";
	header($header );
	header("Content-Transfer-Encoding: binary");
	header("Content-Length: ".$len);
	@readfile($file);
	exit;
?>
