<?php
/*
Freeway eCommerce 
http://www.zac-ware.com/freeway 
Copyright (c) 2007 ZacWare 

Released under the GNU General Public License
*/

// Check to ensure this file is included in Freeway!
defined('_FEXEC') or die();


function tep_check_country_differ($payment=false){
	global $order,$IP_COUNTRY,$USER_BARRED;
	
	$USER_BARED=false;

	if ((BLOCK_DIFFER_COUNTRY_IP!='Yes' && BLOCK_EXCLUDED_COUNTRIES!='Yes') || (BLOCK_DIFFER_COUNTRY_IP!='Yes' && !$payment)) return;
	$IP_COUNTRY=tep_get_ip_country();

	if (BLOCK_DIFFER_COUNTRY_IP!='Yes' || $IP_COUNTRY=="") return;
	if ($IP_COUNTRY!=$order->customer["country"]["iso_code_2"] && $IP_COUNTRY!=$order->customer["country"]["iso_code_3"] && strtolower($IP_COUNTRY)!=strtolower($order->customer["country"]["title"])){
		$USER_BARRED=true;
	}
}
function tep_get_ip_country(){
	if (isset($_SESSION["ip_country"])){
		return $_SESSION["ip_country"];
	}
	$ip=$_SERVER["REMOTE_ADDR"];
	$country="";
	if (defined("GEOIP_LICENSE_KEY") && GEOIP_LICENSE_KEY!=''){
		$query = "http://maxmind.com:8010/a?l=" . GEOIP_LICENSE_KEY . "&i=" . $ip;
		$url = parse_url($query);
		$host = $url["host"];
		$path = $url["path"] . "?" . $url["query"];
		$timeout = 1;
		$fp = @fsockopen ($host, 8010, $errno, $errstr, $timeout);
		if ($fp) {
		  @fputs ($fp, "GET $path HTTP/1.0\nHost: " . $host . "\n\n");
		  while (!feof($fp)) {
			$buf .= fgets($fp, 128);
		  }
		  $lines = split("\n", $buf);
		  $country = $lines[count($lines)-1];
		  fclose($fp);
		} 
		if (strlen($country)>3) $country="";
	} else {
		$result=@file_get_contents("http://www.ippages.com/simple/?ip=" . $ip . "&get=country");
		if (strpos($result,"-")!==false){
			$splt=split("-",$result);
			$country=$splt[0];
		}
	}
	$_SESSION["ip_country"]=$country;
	return $country;
	
}
function tep_get_country_title($iso_code){
	$query=tep_db_query("SELECT countries_name from " . TABLE_COUNTRIES . " where countries_iso_code_2='$iso_code' or countries_iso_code_3='$iso_code' or lower(countries_name)='" . strtolower($iso_code) . "'");
	$result=tep_db_fetch_array($query);
	return $result["countries_name"];
}
function tep_output_differ_check(){
	global $IP_COUNTRY,$USER_BARRED;

	if (!$USER_BARRED) return;
	echo '<tr>
			<td  class="messageStackWarning" style="padding:4px">
			<table border="0" cellpadding="2" cellspacing="0" width="100%">
				<tr>
					<td class="smallText"><img src="images/icons/error.gif" alt="Transaction barred"><b>' . TEXT_TRANSACTION_BARRED . '</b>
				</tr>
				<tr>
					<td class="smallText">' . sprintf(TEXT_TRANSACTION_BARRED_REASON,tep_get_country_title($IP_COUNTRY)) . '
					</td>
				</tr>
			</table>
			</td>
		</tr>
		 ';
}
function tep_check_payment_barred($exclude){
	global $IP_COUNTRY;
	if ($exclude=="" || substr($exclude,0,1)=='M') return false;
	if ($IP_COUNTRY=="") return false;
	if (BLOCK_EXCLUDED_COUNTRIES!='Yes') return false;
	
	$query=tep_db_query("SELECT countries_name from " . TABLE_COUNTRIES . " where countries_iso_code_2='$IP_COUNTRY' or countries_iso_code_3='$IP_COUNTRY' or lower(countries_name)='" . strtolower($IP_COUNTRY) . "' and countries_id in(" . $exclude . ")");
	if (tep_db_num_rows($query)>0) return true;
	return false;
}
//Support Packs
/*function tep_show_support_packs($product_id) {
	global $languages_id;
	$pack_query=tep_db_query("select sud.subscription_name,su.support_minutes,psp.no_users from  " . TABLE_PRODUCTS . " p, " .TABLE_SUBSCRIPTIONS_DESCRIPTION  . " sud, " . TABLE_SUBSCRIPTIONS ." su, " . TABLE_PRODUCTS_TO_SUPPORT_PACKS . " psp where sud.subscription_id=su.subscription_id and sud.subscription_id=psp.subscription_id and psp.product_id=p.products_id and sud.language_id='" . $languages_id . "' and p.products_id='" . $product_id . "'");
		if(tep_db_num_rows($pack_query)>0) { 
			$result='<table cellpadding="0" cellspacing="0" border="0" width="100%">';
			$result.='<tr><td class="main"><b>Support Packs: </b></td></tr>';
			while($pack_result=tep_db_fetch_array($pack_query)){
				 $result.='<tr><td class="main">' . $pack_result['support_minutes'] . ' minutes ' . $pack_result['subscription_name'] . ' for ' . $pack_result['no_users'].' users</td></tr>';
			}
			$result.='</table>';
		}
		return $result;
	}
*/	
function tep_send_support_pack_default_email($type,$orders_id){
	global $order;
	$packs_query=tep_db_query("select opsp.subscription_id,spl.license_code,opsp.subscription_name from " . TABLE_ORDERS_PRODUCTS_SUPPORT_PACKS . " opsp, " . TABLE_SUPPORT_PACKS_LICENSE . " spl where spl.support_packs_id=opsp.support_packs_id and opsp.orders_id='" . $orders_id . "'");
	if(tep_db_num_rows($packs_query)<=0) return;
	
	//define('DEBUGGING_MODE_VIEW',true);
	$details=array();
	$details['type']=$type;
	$details['table']=TABLE_EMAIL_MESSAGES;
	tep_get_template($details);      	  // get template using type and table   
	
	$prev_id='';
	while ($packs_result=tep_db_fetch_array($packs_query)){
		$license_code.=sprintf('%05d',$packs_result['license_code']) .',';
		if($prev_id!=$packs_result['subscription_id']) {
			$packs_list.=$packs_result['subscription_name']. ",";
			$prev_id=$packs_result['subscription_id'];
		}	
	}
	if($license_code!='') $license_code=substr($license_code,0,-1);
	if($packs_list!='') $packs_list=substr($packs_list,0,-1);
	
	$merge_details=array(CUST_CF=>$order->customer['firstname'],
						CUST_CL=>$order->customer['lastname'],
						CUST_CM=>$order->customer['company'],
						TEXT_SM=>STORE_NAME,
						TEXT_SN=>STORE_OWNER,
						TEXT_SE=>STORE_OWNER_EMAIL_ADDRESS,
						TEXT_AL=>STORE_OWNER_EMAIL_ADDRESS,
						CUST_CT=>$order->customer['street_address'],
						CUST_CP=>$order->customer['postcode'],
						CUST_CC=>$order->customer['city'],
						CUST_CB=>$order->customer['subrub'],
						CUST_CE=>$order->customer['state'],
						CUST_CU=>$order->customer['country']['title'],
						CUST_CO=>$order->customer['telephone'],
						CUST_CA=>'<a href="mailto:' . $order->customer['email_address'] . '"><u>' . $order->customer['email_address'] .'</u></a>',
						TEXT_LC=>$license_code,
						TEXT_SPL=>$packs_list
					);		
	
	tep_replace_template($details,$merge_details); // replace template content with merge details
	tep_strip_html($details); // strip tags;

    //send mails to given addresses
		$details['to_name']=$order->customer['firstname'] . ' ' . $order->customer['lastname'];
		$details['to_email']=$order->customer['email_address'];
		$details['from_name']=STORE_OWNER;
		$details['from_email']=STORE_OWNER_EMAIL_ADDRESS;
		tep_send_email($details,true); // send email
}	


function tep_send_support_packs_code_activation_email($type,$license_code) {
	//define('DEBUGGING_MODE_VIEW',true);
	
	$license_query=tep_db_query("select spl.customer_domain,customer_name,customer_email,opsp.support_minutes,o.orders_id from " . TABLE_SUPPORT_PACKS_LICENSE . " spl, " . TABLE_ORDERS_PRODUCTS_SUPPORT_PACKS . " opsp, " . TABLE_ORDERS . " o where o.orders_id=opsp.orders_id and opsp.support_packs_id=spl.support_packs_id and spl.license_code='" . $license_code . "'");
	$license_result=tep_db_fetch_array($license_query);
	$order = new order($license_result['orders_id']);
	
	$details=array();
	$details['type']=$type;
	$details['table']=TABLE_EMAIL_MESSAGES;
	tep_get_template($details);      	  // get template using type and table   
	
	$merge_details=array(TEXT_LC=>sprintf('%05d',$license_code),
						TEXT_DN=>$license_result['customer_domain'],
						TEXT_DE=>$license_result['customer_email'],
						TEXT_DU=>$license_result['customer_name'],
						TEXT_TST=>$license_result['support_minutes'],
						TEXT_SM=>STORE_NAME,
						TEXT_SN=>STORE_OWNER,
						TEXT_SE=>STORE_OWNER_EMAIL_ADDRESS,
						TEXT_AL=>STORE_OWNER_EMAIL_ADDRESS,
					);		
					
	
	tep_replace_template($details,$merge_details); // replace template content with merge details
	tep_strip_html($details); // strip tags;

	//mail for domain user
    //send mails to given addresses 
		$details['to_name']=$license_result['customer_name'];
		$details['to_email']=$license_result['customer_email'];
		$details['from_name']=STORE_OWNER;
		$details['from_email']=STORE_OWNER_EMAIL_ADDRESS;
		tep_send_email($details,true); // send email
		
	//mail for customer who purchased the software pack	
		$details['to_name']=$order->customer['firstname'] . ' ' . $order->customer['lastname'];
		$details['to_email']=$order->customer['email_address'];
		$details['from_name']=STORE_OWNER;
		$details['from_email']=STORE_OWNER_EMAIL_ADDRESS;
		tep_send_email($details,true); // send email
		
	//mail for store owner	
		$details['to_name']=STORE_OWNER;
		$details['to_email']=STORE_OWNER_EMAIL_ADDRESS;
		$details['from_name']=STORE_OWNER;
		$details['from_email']=STORE_OWNER_EMAIL_ADDRESS;
		tep_send_email($details,true); // send email
} //Support Packs

 function get_discount_details($orders_id,$product_id){
 global $currencies;
 	$return='';
    $product_query = tep_db_query("select discount_type,discount_text,products_price,final_price,products_tax,products_quantity from " . TABLE_ORDERS_PRODUCTS . " where orders_id='" . $orders_id . "' and products_id='" . $product_id . "'" );
	
	$product_result=tep_db_fetch_array($product_query);
	if($product_result['discount_type']=='C' || $product_result['discount_type']=='S') {
		$product_to_categories_query = tep_db_query("select categories_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id = '" . $product_id . "'");
		$product_to_categories = tep_db_fetch_array($product_to_categories_query);
		$category = $product_to_categories['categories_id'];
		$fp=$product_result["final_price"];
		$tax=$product_result["products_tax"];
		$qty=$product_result["products_quantity"];

		$price=$product_result['products_price'];
		$product_amount=tep_add_tax(($price * $product_result['products_quantity']),$tax);
		$final_amount=tep_add_tax(($fp*$product_result['products_quantity']),$tax);
		$total=($product_amount-$final_amount);
		
			$return .=TEXT_DISCOUNT_APPLIED . '&nbsp;&nbsp;'. $product_result['discount_text']  . '<br>';
			$return .=TEXT_ORIGINAL_AMOUNT . '&nbsp;&nbsp;'. $currencies->format($product_amount) . '<br>';
			$return .=TEXT_DISCOUNT . '&nbsp;&nbsp;'. $currencies->format($total);
				
	}
	return $return;
 	
 }
 
 function tep_get_post_vars($post_value,$name="",$filter_array=array()){
 	global $POST_HIDDEN;
 	if (count($post_value)<=0) return;
	reset($post_value);
	while(list($key,)=each($post_value)){
		if ($name=="" && in_array($key,$filter_array)) continue;
		if (is_array($post_value[$key])){
			tep_get_post_vars($post_value[$key],($name==''?$key:$name . "[" . $key . "]"));
		} else {
			$POST_HIDDEN.='<input type="hidden" name="' . ($name==''?$key:$name . "[" . $key . "]") . '" value="' . tep_output_string($post_value[$key]) . '">' . "\n";
		}
	}
 }
?>
