<?php
//This code is copyright (c) ViciSoft SL, all rights reserved.
//The contents of this file are protected under law as the intellectual property 
//of ViciSoft SL. Any use, reproduction, disclosure or copying of any kind 
//without the express and written permission of ViciSoft SL is forbidden.
//Author: Vince Reid, vincereid@gmail.com
global $ordAuthNumber,$storeurl,$digidownloaddays,$digidownloadsecret,$digidownloadpath,$digidownloadpattern,$imgdownloadproducts,$digidownloadmethod,$warnexpireddownload,$imgcontinueshopping,$imgprintversion;
if(@$GLOBALS['iswordpress'])
	require ABSPATH . (@$GLOBALS['isvsadmindir']?'':'/vsadmin') . '/inc/incemail.php';
else
	include "./vsadmin/inc/incemail.php";
$success=FALSE;
$warnexpireddownload=TRUE;
$GLOBALS['noshowdigiordertext']=TRUE;
$orderText="";
if(@$digidownloadsecret=="") $digidownloadsecret="this is some secret text";
$theordid = trim(str_replace("'","", @$_GET["ordid"]));
if(getpost('ordid')!='') $theordid = trim(str_replace("'","", @$_POST["ordid"]));
$thepass = trim(str_replace("'","", @$_GET["pass"]));
if(getpost('pass')!='') $thepass = trim(str_replace("'","", @$_POST["pass"]));
$isposted=(@$_GET["posted"]=="1" || ($theordid!='' && $thepass!=''));
if($isposted){
	if($theordid!='' && $thepass!='' && is_numeric($theordid)){
		$sSQL = "SELECT ordID,ordAuthNumber,ordSessionID FROM orders WHERE ordID='" . escape_string($theordid) . "'";
		$result=ect_query($sSQL) or ect_error();
		if($rs=ect_fetch_assoc($result)){
			$ordID = $rs["ordID"];
			$ordAuthNumber = $rs["ordAuthNumber"];
			$ordSessionID = $rs["ordSessionID"];
			$fingerprint = vrhmac($digidownloadsecret, $ordID . $ordAuthNumber . $ordSessionID);
			$fingerprint = substr($fingerprint, 0, 14);
			if($fingerprint==$thepass){
				$GLOBALS['xxRecEml']="";
				$success=TRUE;
			}else
				$errmsg=$GLOBALS['xxNoLog'];
		}else{
			$success=FALSE;
			$errmsg=$GLOBALS['xxNoLog'];
		}
		if($success){
			// Check there is something to download
			$sSQL="SELECT cartProdID FROM products INNER JOIN cart ON products.pID=cart.cartProdID INNER JOIN orders ON cart.cartOrderID=orders.ordID WHERE ordStatus>=3 AND cartCompleted=1 AND cartOrderID='" . escape_string($theordid) . "' AND ordAuthNumber='" . escape_string($ordAuthNumber) . "' AND pDownload<>''";
			$result=ect_query($sSQL) or ect_error();
			if(ect_num_rows($result)==0){
				$success=FALSE;
				$errmsg="Your order id and password are correct, however there are no downloadable products associated with this order.";
			}
		}
	}else{
		$success=FALSE;
		$errmsg=$GLOBALS['xxPlEnt'];
	}
}
?>
&nbsp;<br />
<?php	if(!($isposted && $success)){ ?>
	<form method="post" action="latedownload<?php print $GLOBALS['extension']?>">
		<div class="receiptdownload">
			<input type="hidden" name="posted" value="1" />
            <div class="receiptsectionhead latedownloadhead"><?php print $GLOBALS['xxPlEnt']?></div>
<?php		if($isposted && ! $success){ ?>
			<div class="ectwarning"><?php print $errmsg?></div>
<?php		} ?>
            <div class="downloadlist latedownloadlist">
				<div class="downloadlistrow latedownloadlistrow">
					<div><?php print $GLOBALS['xxOrdId']?>:</div>
					<div><input type="text" name="ordid" class="ecttextinput" size="30" value="<?php print $theordid?>" class="ecttextinput"></div>
				</div>
				<div class="downloadlistrow latedownloadlistrow">
					<div><?php print $GLOBALS['xxPwd']?>:</div>
					<div><input type="password" name="pass" class="ecttextinput" size="30" value="<?php print $thepass?>" class="ecttextinput" autocomplete="off"></div>
				</div>
				<div class="downloadlistrow latedownloadlistrow">
					<div>&nbsp;</div>
					<div><input type="submit" value="<?php print $GLOBALS['xxSubmt']?>" class="ectbutton latedownloadsubmit"></div>
				</div>
            </div>
		</div>
	</form>
<?php	} ?>
<br />&nbsp;
<?php
$dltext = "";
if(trim(@$digidownloadpath)!=''){
	if(substr($digidownloadpath,-1)!='/'&&substr($digidownloadpath,-1)!='\\') $digidownloadpath.=DIRECTORY_SEPARATOR;
}else
	$digidownloadpath='';
if(@$digidownloaddays=="") $digidownloaddays=3;
	if($success){
		if(@$digidownloadsecret=="") $digidownloadsecret="this is some secwet text";
		$sSQL="SELECT cartID,cartProdID,cartProdName,cartProdPrice,cartSessionID,pDownload FROM products INNER JOIN cart ON products.pID=cart.cartProdID INNER JOIN orders ON cart.cartOrderID=orders.ordID WHERE ordStatus>=3 AND cartCompleted=1 AND cartOrderID='" . $ordID . "' AND ordAuthNumber='" . escape_string($ordAuthNumber) . "' AND pDownload<>'' AND ordDate > '" . date("Y-m-d", (time()-(60*60*24*$digidownloaddays))) . "'";
		$result=ect_query($sSQL) or ect_error();
		if(ect_num_rows($result) > 0){
			while($rs=ect_fetch_assoc($result)){
				$theid = $rs['cartProdID'];
				$hasdownload = TRUE;
				$noregexpcol=FALSE;
				if(($thedownload=trim($rs['pDownload']))!=''){
					if(strtolower($thedownload)=='a'){ // auto download
						$thedownload = str_replace("%pid%", $theid, $digidownloadpattern);
					}
					$thedownloadarray=explode(',',$thedownload);
					$sSQL="SELECT optRegExp FROM cartoptions INNER JOIN options ON cartoptions.coOptID=options.optID WHERE coCartID=" . $rs["cartID"] . " ORDER BY coCartID";
					$result2=ect_query($sSQL) or $noregexpcol=TRUE;
					if($noregexpcol != TRUE){
						while($rs2=ect_fetch_assoc($result2)){
							$theexp = trim($rs2["optRegExp"]);
							if(substr($theexp, 0, 1)=="!"){
								$theexp = substr($theexp, 1);
								for($dlindex=0; $dlindex<count($thedownloadarray);$dlindex++){
									if(substr($theexp, 0, 1)=="!"){
										$hasdownload = FALSE;
										$thedownloadarray[$dlindex]='';
									}else{
										$theexp = str_replace('%s', $thedownloadarray[$dlindex], $theexp);
										if(strpos($theexp, " ") !== FALSE){ // Search and replace
											$exparr = explode(' ', $theexp, 2);
											$thedownloadarray[$dlindex] = str_replace($exparr[0], $exparr[1], $thedownloadarray[$dlindex]);
										}else
											$thedownloadarray[$dlindex] = $theexp;
									}
								}
							}
						}
					}
					ect_free_result($result2);
					for($dlindex=0; $dlindex<count($thedownloadarray);$dlindex++){
						if($thedownloadarray[$dlindex]!=''){
							$fingerprint = vrhmac($digidownloadsecret, $rs['cartID'] . $theid . $rs['cartSessionID'] . (@$digidownloadpath!=''?$thedownloadarray[$dlindex]:''));
							$dltext.='<div class="downloadlistrow"><div>' . $theid . '</div><div>' . $rs["cartProdName"] . '</div><div>';
							$ispdf=FALSE;
							if(@$digidownloadmethod!='filesystem') $ispdf=strtolower(substr($thedownloadarray[$dlindex],-4))=='.pdf';
							if(@$imgdownloadproducts!=''){
								if(@$digidownloadmethod=='filesystem'){
									$dltext .= '<input '.($imgdownloadproducts=='button'?'type="button"':'type="image" src="'.$imgdownloadproducts.'"').' value="' . htmlspecials($GLOBALS['xxDlPro'] . (@$digidownloadpath!=''?' - ' . $thedownloadarray[$dlindex]:'')) . '" class="'.($imgdownloadproducts=='button'?'ectbutton ':'').'downloadproducts" onclick="document.location=\'' . $GLOBALS['storeurlssl'] . 'vsadmin/dodownload.php?ref=' . $theid . '&id=' . $rs['cartID'] . '&sd=' . $rs["cartSessionID"] . '&rd=' . $fingerprint . '\'" />';
								}else{
									$dltext .= '<input '.($imgdownloadproducts=='button'?'type="button"':'type="image" src="'.$imgdownloadproducts.'"').' value="' . htmlspecials($GLOBALS['xxDlPro'] . (@$digidownloadpath!=''?' - ' . $thedownloadarray[$dlindex]:'')) . '" class="'.($imgdownloadproducts=='button'?'ectbutton ':'').'downloadproducts" onclick="' . ($ispdf?'window.open(\'' . jsescapel($digidownloadpath . $thedownloadarray[$dlindex]) . '\',\'_blank\')':'document.location=\'' . jsescapel($digidownloadpath . $thedownloadarray[$dlindex]) . '\'') . '" />';
								}
							}else{
								if(@$digidownloadmethod=='filesystem'){
									$dltext .= '<a class="ectlink" style="color:#FF0000;font-size:18px;font-weight:bold" href="' . $GLOBALS['storeurlssl'] . 'vsadmin/dodownload.php?ref=' . $theid . '&id=' . $rs['cartID'] . '&sd=' . $rs["cartSessionID"] . '&rd=' . $fingerprint . '">' . htmlspecials($GLOBALS['xxDlPro'] . (@$digidownloadpath!=''?' - ' . $thedownloadarray[$dlindex]:'')) . '</a>';
								}else{
									$dltext .= '<a class="ectlink" style="color:#FF0000;font-size:18px;font-weight:bold" href="' . str_replace('"', "&quot;", $digidownloadpath . $thedownloadarray[$dlindex]) . '"' . ($ispdf?' target="_blank"':'') . '>' . htmlspecials($GLOBALS['xxDlPro'] . (@$digidownloadpath!=''?' - ' . $thedownloadarray[$dlindex]:'')) . '</a>';
								}
							}
							$dltext.="</div></div>\r\n";
						}
					}
				}
			}
		}
		ect_free_result($result);
		if($dltext!=''){
			$dltext='<div class="receiptdownload">' .
					'  <div class="receiptsectionhead receiptdownloadhead">' . $GLOBALS['xxDlPros'] . '</div>' .
					'  <div class="downloadlist">' .
					'  <div class="downloadheadrow"><div>' . $GLOBALS['xxPrId'] . '</div><div>' . $GLOBALS['xxPrNm'] . '</div><div>' . $GLOBALS['xxDownl'] . '</div></div>' . $dltext . '  </div>' .
					"</div>\r\n";
		}elseif(@$warnexpireddownload){
			$dltext='<div class="receiptdownload receiptnodownload">' .
					'  <div class="receiptsectionhead receiptdownloadhead">' . $GLOBALS['xxDlPros'] . '</div>' .
					'  <div class="receiptnodownloadtext">There are no downloadable products associated with this order or the download time has expired.</div>' .
					"</div>\r\n";
		}
		if(@$GLOBALS['noshowdigiordertext']==TRUE){
			print $dltext;
		}else{
			$GLOBALS['recpt'] = str_replace('<!--%digidownloaditems%-->',$dltext,$GLOBALS['recpt']);
?>
			<div class="orderreceipt">
<?php		if(@$GLOBALS['xxThkYou']!='') print '<div class="recptthanks">' . $GLOBALS['xxThkYou'] . '</div>' ?>
				<div id="printcontent" class="recptbody"><?php print str_replace('%nl%','<br />',str_replace("%messagebody%", $GLOBALS['recpt'], $GLOBALS['receiptheader']))?></div>
<?php		if(@$GLOBALS['noreceiptbuttons']!=TRUE){ ?>
				<div class="receiptbuttons">
<?php			if(trim($GLOBALS['xxRecEml'])!='') print '<div class="receiptrecemail">' . $GLOBALS['xxRecEml'] . '</div>';
				print '<div class="receiptcontinueshopping">' . imageorbutton(@$imgcontinueshopping,'&nbsp;'.$GLOBALS['xxCntShp'].'&nbsp;','continueshopping',(@$thankspagecontinue!=''?$thankspagecontinue:$storeurl),@$thankspagecontinue=='javascript:history.go(-1)'?TRUE:FALSE) . '</div>';
				print '<div class="receiptprintversion">' . imageorbutton(@$imgprintversion,'&nbsp;'.$GLOBALS['xxPrint'].'&nbsp;','printversion','doprintcontent()', TRUE) . '</div>';
?>				</div>
<?php		} ?>
			</div>
<?php	}
	} ?>