<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// | PHP version 4/5                                                      |
// +----------------------------------------------------------------------+
// | This source file is a part of iScripts Multicart                     |
// +----------------------------------------------------------------------+
// | Authors: Programmer<programmer@armia.com>        		              |
// +----------------------------------------------------------------------+
// | Copyrights Armia Systems, Inc and iScripts.com � 2005                |
// | All rights reserved                                                  |
// +----------------------------------------------------------------------+
// | This script may not be distributed, sold, given away for free to     |
// | third party, or used as a part of any internet services such as      |
// | webdesign etc.                                                       |
// +----------------------------------------------------------------------+
?>
<?php
include "../includes/config.php";
include "../includes/session.php";
include "../includes/functions.php";
include "../includes/dwolla.php";
if(isset( $_SESSION["sess_artistid"]) and $_SESSION["sess_artistid"]!= ""){
	$userid = $_SESSION["sess_artistid"];
}



$sql = "SELECT * FROM ".$tableprefix."settings ";
$resN = mysql_query($sql);
$row = mysql_fetch_array($resN);
$txtPaypalEmail = $row["paypalemail"];
$txtPaypalSandbox = $row["enablepaypalsandbox"];

$gift_coupon_flag = 0;
$paypal_check = 0;
$error_description = '';
// first, check for any errors
if (array_key_exists("error", $_GET)) {
	// find out what happened:
	$error_description = $_GET['error_description']; // "User Cancelled"
	//exit($error_description);
}
$status = $_GET['status'];


 $flag_to_continue=false;

if(($_GET['amount']!='' && $_GET['signature']!='') || $error_description!='' ){


       $checkoutId = $_GET['checkoutId'];
       $amount = $_GET['amount'];
       $amount_compare = $_GET['amount'];
       $signature = $_GET['signature'];
       if($error_description!=''){
            header("Location:failure.php?error=".$error_description);
            exit;
       }
       $signatureValid = verifyGatewaySignature($signature, $checkoutId, $amount);
       if (!$signatureValid) {
            header("Location:failure.php?error=bad signature");
            exit;
       }


}
else
    {

      if(isset($_SESSION['SESS_pay_amount']) && $_SESSION['SESS_pay_amount'] != "")
   {
       if($_SESSION['SESS_pay_amount'] == trim($_GET['amount']))
       {
       $flag_to_continue =true;
       }
   }
}



$amount_compare=trim($_GET['amount']);

$tx_token   = $_GET['transaction'];
if(isset($_SESSION['SESS_pay_amount']) && $_SESSION['SESS_pay_amount'] != "")
{
	if($_SESSION['SESS_pay_amount'] == trim($_GET['amount']))
	{
		$paypal_check = 0;
		
	}
}


if($paypal_check==0)
{
	if(function_exists('session_unregister')){
	session_unregister($_SESSION['SESS_pay_amount']);
        }
        else{
            	unset($_SESSION['SESS_pay_amount']);
        }
	
}


if(isset($_SESSION['SESS_pay_amount']) && $_SESSION['SESS_pay_amount'] != "")
{
	if($_SESSION['SESS_pay_amount'] != $amount_compare)
	{
		$paypal_check = 1;
	}
}

//-----------------------------Inserting artist starts here --------------------------------------------//
if(isset($_SESSION['SESS_insertQry']))
{
	mysql_query($_SESSION['SESS_insertQry'][0]) or die(mysql_error());
	$userid = mysql_insert_id();
	$_SESSION['sess_artistid']=$userid;
	
	$_SESSION["sess_artistname"] = trim($_SESSION['SESS_insertQry'][2]['txtUserName']);
	$_SESSION["sess_artistid"] 	 = $userid;					
	
	// Ending Session for User, Affiliates		
	if(isset($_SESSION["sess_userid"]) and $_SESSION["sess_userid"]!= "")
	{ // Logging Out User  from same window
		if(function_exists('session_unregister')){
		session_unregister($_SESSION["sess_userid"]);
                }
                else{
                    unset($_SESSION["sess_userid"]);
               }
		
	}
	if(isset($_SESSION["sess_affiliateid"]) and $_SESSION["sess_affiliateid"]!= "")
	{ // Logging OutSeller  from same window
		if(function_exists('session_unregister')) {
		session_unregister($_SESSION["sess_artistid"]);
                }
                else{
                    unset($_SESSION["sess_artistid"]);
                }
		
	}
}
//------------------------------Ends here--------------------------------------------------------------//



$select_Plan = "SELECT plan_Period
                                                           FROM ".$tableprefix."sellerPlans
                                                           WHERE plan_Id = ".trim($_SESSION['membership']['plan_Id']);
$result_Plan = mysql_query($select_Plan) or die(mysql_error());
$result_Plan = mysql_fetch_assoc($result_Plan);	
if($result_Plan['plan_Period']=="M")
$noOfDays=30;
if($result_Plan['plan_Period']=="Y")
$noOfDays=365;
	

$_SESSION['SES_MEMPLAN']=$_SESSION['membership']['plan_Id'];

if($paypal_check==0)
{ // check if paypal is success

		$sql = "UPDATE " .$tableprefix. "artists SET sellerPlanID =".$_SESSION['membership']['plan_Id']." WHERE artist_id =".$_SESSION['sess_artistid'];
		mysql_query($sql);
		$orderid = mysql_insert_id();
		
		$fromDate= date("Y-m-d");
		$endDate= date("Y-m-d",strtotime("+".$noOfDays." day"));


		$sql = "INSERT INTO " .$tableprefix. "artists_membershipDetails (artist_id, artist_memStartDate, artist_memEndDate,plan_Id,plan_SubscriptionCharge,transaction_id) VALUES
				(".$_SESSION['sess_artistid'].",'".$fromDate."','".$endDate."',".$_SESSION['membership']['plan_Id'].",".$_SESSION['membership']['plan_SubscriptionCharge'].",'".$tx_token."')";
		mysql_query($sql);
		$orderid = mysql_insert_id();
}		

 $select_seller_name = "SELECT * FROM ".$tableprefix."artists WHERE artist_id = ".$_SESSION['sess_artistid'];
					$result_seller_name = mysql_query($select_seller_name) or die(mysql_error());

if(mysql_num_rows($result_seller_name) > 0)
{

        $seller_name_row = mysql_fetch_assoc($result_seller_name);
        $txtSellerName = $seller_name_row['artist_name'];
        $txtFirstName  = $seller_name_row['first_name'];
        $txtLastName  = $seller_name_row['last_name'];
        $txtUserName  = $seller_name_row['artist_name'];
        $txtEmail     = $seller_name_row['email'];

}


$sellerapproval	=getSettingsValue('approve_sellers');

$userfullname = $txtFirstName;
       if($txtLastName != "")
       {
               $userfullname .= " ". $txtLastName;
       }
       $to = $txtEmail;

/*---------Multicart 2.0 Upgradation------------*/

		$subject = MSG_SELLER_ACC.stripslashes(SITE_NAME);


/*-------Email Template Reading Fuction Call--------*/

                 $txtPassword = trim($_SESSION['SESS_insertQry'][2]['txtPassword']);
		$mailcontent = readEmailTemplate('seller_registration','artists');

		//echo $mailcontent;exit;
		 $mailcontent = str_replace('[first_name]',$txtFirstName,$mailcontent);
		 $mailcontent = str_replace('[last_name]',$txtLastName,$mailcontent);
		 $mailcontent = str_replace('[user_name]',$txtUserName,$mailcontent);
		 $mailcontent = str_replace('[password]',$txtPassword,$mailcontent);



/*---------------Seller Login Url-------------------*/


		if($sellerapproval==3)
		{ // check if seller approval is through Email - Activation
			$userid=base64_encode($_SESSION['sess_artistid']);;
			$mailcontent.="\r\n"."<font color='green'><b>".MSG_CLICK_LINK." :</b></font>\r\n\n";
			$mailcontent.="\r\n"."<a href=".SITE_URL."/sellers_activation.php?aid=$userid&status='Y'>".MSG_CLICK_HERE."</a>\r\n\n";

		} // end email activation check
		if($sellerapproval==1)
		{
		$mailcontent .= "\r\n".MAIL_PLAN_PURCHASE_TEXT_CLICK."&nbsp;<a href=".SITE_URL."/sellers>".MAIL_PLAN_PURCHASE_TEXT_HERE."</a> ".MAIL_PLAN_PURCHASE_TEXT_TO_LOGIN."\r\n\n";
		}
		if($sellerapproval==2)
		{
		$mailcontent .= "\r\n\n\n\n\n"."<font color='green'><b>".MSG_ACC_APPRV_ADMIN."</b></font>\r\n\n";
		}



/*---------------Seller Login Url-------------------*/

/*------------Seller Store Url---------------------*/

// send the friendly url in mail based on config settings

				if($enable_seller_friendly == "Y")
				{

					$friendly_url = SITE_URL."/store/".$txtSellerName;
				}
				else
				{
					$friendly_url = SITE_URL."/showportfolio.php?artistid=".$_SESSION['sess_artistid'];
				}

				$mailcontent .= "\r\n"."<font color='#FF0000'>".MAIL_PLAN_PURCHASE_TEXT_SELLER_STORE_URL."</font>"."\r\n\n";
				$mailcontent .= "<b>".$friendly_url."</b>\n\n";



/*------------Seller Store Url---------------------*/


		$headers  = "MIME-Version: 1.0" . "\r\n";
		$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
		$headers .= "From: ".SITE_EMAIL."<".SITE_EMAIL.">" . "\r\n";
		$headers .= "Reply-To: ".SITE_NAME."<".SITE_EMAIL.">" . "\r\n";
		$headers .= "Return-Path: ".SITE_NAME."<noreply@".SITE_EMAIL.">" . "\r\n";
		//$headers .= "Organization: ".SITE_NAME."\r\n";


		$mailcontent = nl2br($mailcontent);

		//$mail_send_details = "<html><head><link href='".SITE_URL."/style/template1-admin.css' rel='stylesheet' type='text/css'></head><body><center><div align='left' id='layout'><div align='left' class='header'><div align='left' class='logo'><img src=".$logofolder."/".$logoimage." width='125' height='39'/></div></div><div>".$mailcontent."</div></div><div align='left' class='clear_float'><img src=".$imagefolder."/clear.gif width=1 height=1/></div></div></div></center></body></html>";
 $mail_send_details = '<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <title >'.SITE_NAME.'</title>

</head>
<body>

<table width="103%" cellspacing="10" cellpadding="0" bgcolor="#EAEAEA">
   <tr>
     <td align="center" valign="top">

         <table width="600" border="0" cellpadding="0" cellspacing="0" bordercolor="#EAEAEA" bgcolor="#FFFFFF" class="manage_table">
            <tr>
               <td align="right" >
              </td>
            </tr>
            <tr>
               <td valign="middle" align="left"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
                 <tr>
                   <td  bgcolor="#EAEAEA" > <img src='.SITE_URL.'/banners/'.getSettingsValue('site_logo').' width="125" height="60"/></td>
                 </tr>
               </table></td>
            </tr>
            <tr>
              <td align="center" valign="top"><table width="100%"  border="0" cellspacing="0" cellpadding="10">
                <tr>
                  <td align="left"><p>'.$mailcontent.'</p>
                  </td>
                </tr>
              </table></td>
            </tr>
            <tr>
               <td align="center" bgcolor="#EAEAEA" class="footer">
                  <p>&nbsp;</p>
              </td>
            </tr>
       </table>

     </td>
   </tr>
</table>


</body>
</html>';
		$mailsent = mail($to, $subject, $mail_send_details, $headers);

		// **********************************************************************Send Mail To Administrator


		$subject_admin = MSG_NEW_SELLER_REG.SITE_NAME;

/*-------Email Template Reading Fuction Call--------*/

		$mailcontent_admin = readEmailTemplate('seller_registration_admin','artists');

		$mailcontent_admin = str_replace('[user_name]',$txtUserName,$mailcontent_admin);

		$mailcontent_admin = str_replace('[password]',$txtPassword,$mailcontent_admin);

		$mailcontent_admin = str_replace('[firstname]',$txtFirstName,$mailcontent_admin);

		$mailcontent_admin = str_replace('[lastname]',$txtLastName,$mailcontent_admin);

/*---------------User Login Url-------------------*/

		$mailcontent_admin .= "\r\n";
		$mailcontent_admin .= "\r\n".MAIL_PLAN_PURCHASE_TEXT_CLICK."&nbsp;<a href=".SITE_URL."/admin/login.php>".MAIL_PLAN_PURCHASE_TEXT_HERE."</a> ".MAIL_PLAN_PURCHASE_TEXT_TO_LOGIN_TO_VIEW_DETAILS."\r\n\n";

/*---------------User Login Url-------------------*/

		$from= "From: ".SITE_EMAIL."\n";
		$headers  = "MIME-Version: 1.0" . "\r\n";
		$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
		$headers .= "From: ".SITE_EMAIL."<".SITE_EMAIL.">" . "\r\n";
		$headers .= "Reply-To: ".SITE_NAME."<".SITE_EMAIL.">" . "\r\n";
		$headers .= "Return-Path: ".SITE_NAME."<".SITE_EMAIL.">"."\r\n";
		$headers .= "Organization: ".SITE_NAME."\r\n";

		$mailcontent_admin = nl2br($mailcontent_admin);

		//$mail_send_details_admin = "<html><head><link href='".SITE_URL."/style/template1-admin.css' rel='stylesheet' type='text/css'></head><body><center><div align='left' id='layout'><div align='left' class='header'><div align='left' class='logo'><img src=".$logofolder."/".$logoimage." width='125' height='39'/></div></div><div>".$mailcontent_admin."</div></div><div align='left' class='clear_float'><img src=".$imagefolder."/clear.gif width=1 height=1/></div></div></div></center></body></html>";



$mail_send_details_admin = '<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <title >'.SITE_NAME.'</title>
 </head>
<body>

<table width="103%" cellspacing="10" cellpadding="0" bgcolor="#EAEAEA">
   <tr>
     <td align="center" valign="top">

         <table width="600" border="0" cellpadding="0" cellspacing="0" bordercolor="#EAEAEA" bgcolor="#FFFFFF" class="manage_table">
            <tr>
               <td align="right" >
              </td>
            </tr>
            <tr>
               <td valign="middle" align="left"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
                 <tr>
                   <td  bgcolor="#EAEAEA" > <img src='.SITE_URL.'/banners/'.getSettingsValue('site_logo').' width="125" height="60"/></td>
                 </tr>
               </table></td>
            </tr>
            <tr>
              <td align="center" valign="top"><table width="100%"  border="0" cellspacing="0" cellpadding="10">
                <tr>
                  <td align="left"><p>'.$mailcontent_admin.'</p>
                  </td>
                </tr>
              </table></td>
            </tr>
            <tr>
               <td align="center" bgcolor="#EAEAEA" class="footer">
                  <p>&nbsp;</p>
              </td>
            </tr>
       </table>

     </td>
   </tr>
</table>


</body>
</html>';


		$adminemail	=getSettingsValue('site_email');


		$mailsent = mail($adminemail, $subject_admin, $mail_send_details_admin, $headers);



unset($_SESSION['membership']);
unset($_SESSION['SESS_pay_amount']);
unset($_SESSION['SESS_insertQry']);

$_SESSION['PAY_MESSAGE']=MSG_PROC_SUC;

$sellerapproval	=getSettingsValue('approve_sellers');
	
if($sellerapproval==3)
        { // Aproval After Email Confirmation 
                unset($_SESSION['sess_artistid']);
                header("Location:validate.php?approval=3");
                exit;
        } // end if 
        else if($sellerapproval==2)
        { // Aproval After Email Confirmation 
                unset($_SESSION['sess_artistid']);
                header("Location:validate.php?approval=2");
                exit;
        } // end if 
        else
        {
                header("location:artistmain.php");
                exit;
        }

?>															