<?php

if (basename($_SERVER['PHP_SELF']) == basename(__FILE__)) {
	die('Error , cannot Access To this File .');
}


if (isset($_GET['download_key']) && isset($_GET['file']) && isset($_GET['expire'])&& isset( $_GET['ex_key'])&& isset($_GET['lockip'])) {
	$download_key = $_GET['download_key'];
	$file = $_GET['file'];
	$expire = $_GET['expire'];
	$ex_key = $_GET['ex_key'];
	$uip=getRealIP();
	$lockip = md5($uip.$config['passphrase']);
	if ($ex_key == md5($expire.$config['passphrase'].base64_decode($file)) && $download_key == md5(base64_decode($file).$config['passphrase']) ){
			if (($expire>=time()) or ($expire==0)){
						if ($config['lock_to_ip']=='on') {
							if (! isset($_GET['lockip']) || $_GET['lockip']<> $lockip){
								//header("HTTP/1.1 401 Invalid IP");
								//die( "You Must Use History Purchase For Download This file");
								error_template(403,$config['domain_for_error_page']);

							}
						}
						$validateurl = parse_url(base64_decode($file), PHP_URL_SCHEME);
						if (empty($validateurl)){
						$strDownload = 'http://' .base64_decode($file);
						$strDownload =parse_url($strDownload, PHP_URL_PATH);
						} else {
						$strDownload = parse_url(base64_decode($file), PHP_URL_PATH);
						}
						$strDownload = substr($strDownload, 1);
						if ($config['hidden_folder']!=='') $strDownload = $config['hidden_folder'].'/'.$strDownload  ;
						$strDownload = realpath('./'.$strDownload);
						if(file_exists($strDownload)){
										$file=$strDownload;
										$contenttype = getcontent($file);
										serve_file_resumable ($file, $contenttype);						
						
						}else{
							//header("HTTP/1.1 404 File not Found.");
							//die( "We couldn't find the file to download.");
							error_template(404,$config['domain_for_error_page']);
						}
		
		}else {
			//header("HTTP/1.1 498 Link Expired.");
			//die("This download has expired.");
			error_template(498,$config['domain_for_error_page']);
		}
	}else {
		//header("HTTP/1.1 499 Token Required.");
		//die("No download key was provided. Please return to the previous page and try again.");
		error_template(499,$config['domain_for_error_page']);
	}

} elseif(!empty($_GET['downloadkey'])) {
    $resDB = mysqli_connect($config['mysqlserver'],$config['dbusername'],$config['dbpassword'],$config['dbname']) or die($er_query);
	//check the DB for the key
	$strSQL = "SELECT * FROM ".$config['tablename']." WHERE (downloadkey = '".wpcar_sanitize_data($_GET['downloadkey'])."') AND (downloadid='".wpcar_sanitize_data($_GET['downloadid'])."') AND (priceid='".wpcar_sanitize_data($_GET['priceid'])."') AND (filenum='".wpcar_sanitize_data($_GET['filenum'])."') LIMIT 1";
	$resCheck = mysqli_query($resDB, $strSQL) or   die($er_query);
	$arrCheck   =  $resCheck->fetch_assoc();

	if($arrCheck['file']){
		//check that the download time hasnt expired
		if(($arrCheck['expires']>=time())OR($arrCheck['expires']==0)){
			if (($arrCheck['dllimit']==0) OR ($arrCheck['downloads']<=$arrCheck['dllimit'])){

				//everything is hunky dory - check the file exists and then let the user download it

					$validateurl = parse_url($arrCheck['linkfile'], PHP_URL_SCHEME);
					if (empty($validateurl)){
					$strDownload = 'http://' .$arrCheck['linkfile'];
					$strDownload =parse_url($strDownload, PHP_URL_PATH);
					} else {
					$strDownload = parse_url($arrCheck['linkfile'], PHP_URL_PATH);
					}
					$strDownload = substr($strDownload, 1);
					if ($config['hidden_folder']!=='') $strDownload = $config['hidden_folder'].'/'.$strDownload  ;
					$strDownload = realpath('./'.$strDownload);
					if(file_exists($strDownload)){

									$uip=getRealIP();

									if ($config['lock_to_ip']=='on' && $arrCheck['lockip']<>md5($uip.$config['passphrase'])) {
										//header("HTTP/1.1 401 Invalid IP");
										//die( "You Must Use History Purchase For Download This file");
										error_template(403,$config['domain_for_error_page']);
									}

									if ($arrCheck['ip']<>$uip){
										//$file=$strDownloadFolder.$arrCheck['file'];
										$file=$strDownload;
										$strSQL = "UPDATE ".$config['tablename']." SET downloads = downloads + 1 , ip='".$uip."' WHERE (downloadkey = '".wpcar_sanitize_data($_GET['downloadkey'])."') AND (downloadid='".wpcar_sanitize_data($_GET['downloadid'])."') AND (priceid='".wpcar_sanitize_data($_GET['priceid'])."') AND (filenum='".wpcar_sanitize_data($_GET['filenum'])."')  LIMIT 1";
										mysqli_query($resDB,$strSQL)  or die($er_query);

										$contenttype = getcontent($file);

										serve_file_resumable ($file, $contenttype );

										} else
										{
										$file=$strDownload;
										$contenttype = getcontent($file);

										serve_file_resumable ($file, $contenttype );
										}

					   }else{
							//header("HTTP/1.1 404 File not Found.");
							//die( "We couldn't find the file to download.");
							error_template(404,$config['domain_for_error_page']);
						}
			}else{
				//this file has already been downloaded and multiple downloads are not allowed
			//	header("HTTP/1.1 423 File Has been Downloaded.");
			//	die("This file has already been downloaded.");
				error_template(423,$config['domain_for_error_page']);
			}
		}else{
			//this download has passed its expiry date
			//header("HTTP/1.1 498 Link Expired.");
			//die("This download has expired.");
			error_template(498,$config['domain_for_error_page']);
		}
	}else{
		//the download key given didnt match anything in the DB
		//header("HTTP/1.1 400 Bad Request.");
		//die("No file was found to download.");
		error_template(400,$config['domain_for_error_page']);
	}

}else{
	//No download key wa provided to this script
	//header("HTTP/1.1 499 Token Required.");
	//die("No download key was provided. Please return to the previous page and try again.");
	error_template(499,$config['domain_for_error_page']);
}