<?php
include "includes/config.php";
include "includes/session.php";
include_once("includes/functions.php");

if($_GET["p_flag"]=="Y")
$pwd = $_GET['password'];
else
$pwd = md5($_GET['password']);


$numRows  = 1;

if($_SESSION["sess_userid"]=='')
    {

	$check_user_valid = "SELECT usr.user_id 
						 FROM ".$tableprefix."users usr
						 WHERE usr.user_name = '".$_GET['user_name']."'
						 AND usr.password = '".$pwd."'";
			
	$result_user_valid = mysql_query($check_user_valid) or die(mysql_error());			

        $numRows = mysql_num_rows($result_user_valid);

        $user_row = mysql_fetch_assoc($result_user_valid);

        $user_Id = $user_row['user_id'];

	
    }
    else
        {
                $user_Id = $_SESSION["sess_userid"];
    }

    if($numRows ==0)
        {
            echo 1;
        }

	else
	{
		/*$check_user_authorize = "SELECT user_id FROM ".$tableprefix."users
								 WHERE user_name = '".$_GET['download_user_name']."'
								 AND '".$_GET['download_user_name']."' = '".$_GET['user_name']."'";
		$result_user_authorize = mysql_query($check_user_authorize) or die(mysql_error());*/
		
		
		$check_user_authorize = "SELECT usr.user_name FROM ".$tableprefix."users usr,".$tableprefix."orders ord,
										".$tableprefix."digital_product dp
								 WHERE usr.user_id = ord.user_id 
								 AND dp.ndigital_orderid = ord.order_id 
								 AND dp.vdigital_product_code = ".GetSQLValueString($_GET['download_product_code'],"text");
								  
		//echo $check_user_authorize; 						  
		  
		$result_user_authorize = mysql_query($check_user_authorize) or die(mysql_error());
				
		if(mysql_num_rows($result_user_authorize) > 0)
		{
		
			$user_authorize_row = mysql_fetch_assoc($result_user_authorize);
									
			if($user_authorize_row['user_name'] != $_GET['user_name'])
			{
				echo 2;
				
			}	
			else
			{
					
				$select_product_download_details = "SELECT ndigital_productid,ndownload_try,ndownload_count,ndownload_expiry 
													FROM ".$tableprefix."digital_product 
													WHERE vdigital_product_code = ".GetSQLValueString($_GET['download_product_code'],"text");
				//echo $select_product_download_details; 
											
				$result_product_download_details = mysql_query($select_product_download_details) or die(mysql_error());		
				
				if(mysql_num_rows($result_product_download_details) == 0)
				{
					echo 2;				
				}
				else
				{
					$select_current_datetime = "SELECT NOW() as download_expiry_date";
					$result_current_datetime = mysql_query($select_current_datetime) or die(mysql_error());
					$datetime_row = mysql_fetch_assoc($result_current_datetime);
									
					
					$product_download_details_row = mysql_fetch_assoc($result_product_download_details);
					
					$download_count = $product_download_details_row["ndownload_count"];
					$new_count = $download_count + 1;
					
					$update_download_flag1 = "UPDATE ".$tableprefix."digital_product SET ndownload_count = '".$new_count."'
												 WHERE vdigital_product_code = ".GetSQLValueString($_GET['download_product_code'],"text");
					mysql_query($update_download_flag1) or die(mysql_error());
					
					if($product_download_details_row['ndownload_count'] >= $product_download_details_row['ndownload_try'])
					{
					
					
						$update_download_flag = "UPDATE ".$tableprefix."digital_product SET ndownload_status = 1						                                                                    
												 WHERE vdigital_product_code = ".GetSQLValueString($_GET['download_product_code'],"text");
						mysql_query($update_download_flag) or die(mysql_error());			
						
						echo 3;
			 
					
					}
					if($datetime_row['download_expiry_date'] >= $product_download_details_row['ndownload_expiry'])
					{
					
											
						$update_download_flag = "UPDATE ".$tableprefix."digital_product SET ndownload_status = 1						                                                                   
												 WHERE vdigital_product_code = ".GetSQLValueString($_GET['download_product_code'],"text");
						mysql_query($update_download_flag) or die(mysql_error());
						
						echo 4;
					
					}
					
				}	
			
			}	

			
			
			$_SESSION['SESS_download'] = $user_Id;
		}
		else
		{
		
			echo 5;
		
		}
	}	

?>