<?php  session_start();
 include("DBConfig/Connection.php"); 
 include("DBConfig/function.php");
function generateRandom($length=6)
{
	$_rand_src = array(
		array(48,57) //digits
		//, array(97,122) //lowercase chars
//		, array(65,90) //uppercase chars
	);
	srand ((double) microtime() * 1000000);
	$random_string = "";
	for($i=0;$i<$length;$i++){
		$i1=rand(0,sizeof($_rand_src)-1);
		$random_string .= chr(rand($_rand_src[$i1][0],$_rand_src[$i1][1]));
	}
	return $random_string;
}
 

 

function savelogs($connect,$affiliates_landing_id,$affiliates_landing_affID,$landingPage_id,$action){
	
	
	
	
	
	$logs=mysqli_query($connect,"insert into affil_landinglogs set affil_landingLogs_date=now(),
affil_landingLogs_affLandID='".$affiliates_landing_id."',
affil_landingLogs_affid='".$affiliates_landing_affID."',
affil_landingLogs_landingID='".$landingPage_id."',
affil_landingLogs_ip='".$_SERVER['REMOTE_ADDR']."'
,affil_landingLogs_sessionid='".session_id()."'
,affil_landingLogs_action='".$action."'
"); 

	



	}


 if($_REQUEST['opt']==1){
	 


	 /*
	 0= sucess
	 1= please fill all feilds 
	 2= cant add 
	 3= thank you 
	  4= virification Wrong
	  */ 
	  
	  if($_REQUEST['optV']==1){ 
	  
	  
	 
	  
	  
	  
	  // verification   
	  
	  $updateSubscriber=mysqli_query($connect,"update SubscribersLogs set  
	  SubscribersLogs_status=1
	  ,SubscribersLogs_dateVerification=now()
	  where SubscribersLogs_verificationCode='".$_REQUEST['vkey']."' and SubscribersLogs_sessionid='".session_id()."'
	 
	  ");
	  
	  if($updateSubscriber){
		  
		  
		  
		  
		  
		  
		  
		  
		  
		  $selectSubscriberLogs=mysqli_query($connect,"select * from SubscribersLogs
		    where SubscribersLogs_verificationCode='".$_REQUEST['vkey']."' and SubscribersLogs_sessionid='".session_id()."'
		   ");
		  
		  while($row=mysqli_fetch_assoc($selectSubscriberLogs)){
			  
			  
			  
			  
			   $chargingAPI=file_get_contents("http://freelance.ps/platform/ChargingAPI.php?mobile=".$row['SubscribersLogs_phone']."&servid=".$row['SubscribersLogs_serviceID']."");
			  
			   $return=json_decode($chargingAPI);
	 
			  
			  if($return->return==0){  // charging is successfully 
			  $insertSubs=mysqli_query($connect,"
			  
			  
			  insert into Subscribers set
		    Subscribers_phone='".$row['SubscribersLogs_phone']."',
			 Subscribers_serviceID='".$row['SubscribersLogs_serviceID']."', 
		   Subscribers_landingid='".$row['SubscribersLogs_landingid']."', 
		    
			  Subscribers_affi='".$row['SubscribersLogs_affi']."', 
			Subscribers_verificationCode='".$row['SubscribersLogs_affi']."',
			 Subscribers_status='".$row['SubscribersLogs_status']."'
			 ,Subscribers_date=now()
			 ,Subscribers_sessionid='".$row['SubscribersLogs_sessionid']."'
			 ,Subscribers_sessionidExpired='".$row['SubscribersLogs_sessionidExpired']."'
			 ,Subscribers_operatorID='".$row['SubscribersLogs_operatorID']."'
			  ,Subscribers_serviceoperator='".$row['SubscribersLogs_serviceoperator']."'
			  ,Subscribers_sublogid='".$row['SubscribersLogs_id']."'
			  ,Subscribers_affTransID='".post($_REQUEST['transactionID'])."'
			  
			  
			  
			  ");
			  
			  
			  
			  
			  //Send To Affiliate Transcation ID
			  
			  
			  $subid=mysqli_insert_id($connect);
			  
			  
			  
			   $affilAPI=file_get_contents("http://freelance.ps/platform/affiliatesAPI.php?tID=".post($_REQUEST['transactionID'])."");
			  
			   $returnaff=json_decode($affilAPI);
	 
			  
 $sel=mysqli_query($connect,"insert into `affAPILogs` set 
`affAPILogs_date`=now(), 
`affAPILogs_transID`='".post($_REQUEST['transactionID'])."',
`affAPILogs_subID`=". $subid.",
`affAPILogs_subLogID`=".$row['SubscribersLogs_id'].",
`affAPILogs_APIReturn`='".$returnaff->return."'
");
			  
			  
			  
			  
			  
			  
			  
			  
			  
			  } else{
				  
				    session_regenerate_id() ;
	  	 go("landingPage.php?error=5&LanCode=".post($_REQUEST['LanCode'])."&AffiCode=".post($_REQUEST['AffiCode'])."&transactionID=".post($_REQUEST['transactionID'])."");
				  exit();
				  }
			  
			  
			  
			  }
		  
		  
		  
		  
		  
		  
		  
		  }
	  
	  
	  
	  
	  if( $updateSubscriber/* && mysqli_affected_rows($connect)>0*/){
		  session_regenerate_id() ;
	  	 go("landingPage.php?error=3&LanCode=".post($_REQUEST['LanCode'])."&AffiCode=".post($_REQUEST['AffiCode'])."&transactionID=".post($_REQUEST['transactionID'])."");
	  
	  }else{
		  go("landingPage.php?error=4&LanCode=".post($_REQUEST['LanCode'])."&AffiCode=".post($_REQUEST['AffiCode'])."&transactionID=".post($_REQUEST['transactionID'])."");
		  
		  }
	  
	  
	  
	  }else{
		  
		  
		  // add new subscriber 
 
	 
	 if($_REQUEST['prefix']!=0 && strlen($_REQUEST['mobile'])> 5){
		 
		 
		 
		 $Prefix=explode("*",$_REQUEST['prefix']);
		 
		 
		 $phone=$Prefix[1].$_REQUEST['mobile'];
		 
		 
		 
		 
		 
		 
	 
$selecLandingPage=mysqli_query($connect,"select * from landingpage
where landingPageCode='".$_REQUEST['landCode']."'");
$rowLadingPage=mysqli_fetch_array($selecLandingPage);


 

 $selectAffileLand=mysqli_query($connect,"select * from affiliates_landing 
 where affiliates_landing_landingId='".$rowLadingPage['landingPage_id']."'
 and affiliates_landing_CodeID='".$_REQUEST['affCode']."'
 
 
 ");

 





if(mysqli_num_rows( $selectAffileLand)==0){
	echo("Wrong Affilites ID");
	exit();
	}

$rowAffile=mysqli_fetch_array($selectAffileLand);
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		/* $insert=mysqli_query($connect," insert into affil_landinglogs 
		 set affil_landingLogs_date=now(),
		  affil_landingLogs_affLandID,
		   affil_landingLogs_affid, 
		   affil_landingLogs_landingID, 
		   affil_landingLogs_ip='".$_SERVER['REMOTE_ADDR']."'");*/
		   
		   $insert=mysqli_query($connect," insert into SubscribersLogs set
		    SubscribersLogs_phone='".$phone."',
			 SubscribersLogs_serviceID='".$_REQUEST['serviceid']."', 
		   SubscribersLogs_landingid='".$rowLadingPage['landingPage_id']."', 
		    
			  SubscribersLogs_affi='".$rowAffile['affiliates_landing_affID']."', 
			SubscribersLogs_verificationCode='".generateRandom(6)."',
			 SubscribersLogs_status=0
			 ,SubscribersLogs_date=now()
			 ,SubscribersLogs_sessionid='".session_id()."'
			 ,SubscribersLogs_sessionidExpired=date_add(now(), interval 3 minute)
			 ,SubscribersLogs_operatorID='".$Prefix[0]."'
			  ,SubscribersLogs_serviceoperator='".$Prefix[2]."'
			  ,SubscribersLogs_affTransID='".post($_REQUEST['transactionID'])."'
			  
			  
			 ");
		   
		   
		    
		   
		 if($insert){
			  $error=0;
			 }else{
				  $error=2;
				 }		 
		 
		 
		 }else{
			 $error=1;
			 }
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 go("landingPage.php?error=".$error."&LanCode=".post($_REQUEST['LanCode'])."&AffiCode=".post($_REQUEST['AffiCode'])."&transactionID=".post($_REQUEST['transactionID'])."");
	 
	 
	 
	 
 }
	 
	 
	 
	 
	 
	 
	 
	 
	 }else{

 
 ?>






<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>





<?php 
$iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");
$palmpre = strpos($_SERVER['HTTP_USER_AGENT'],"webOS");
$berry = strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
$ipod = strpos($_SERVER['HTTP_USER_AGENT'],"iPod");
$ipad = strpos($_SERVER['HTTP_USER_AGENT'],"iPad");
?>


<style>
*{ font-size:22px; font-family:tahoma}



input, select {
	  line-height: 1.42857143;
   
    white-space: nowrap;
    
    -ms-touch-action: manipulation;
    touch-action: manipulation;
    cursor: pointer;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    
    border: 1px solid  #337ab7;
    border-radius: 4px;
	
	
	}
.mobile_img{
	
	
	 height:90% ; border:10px solid #003; max-height:500px}
.input{ width:200px ;   
    display: inline-block;
    padding: 6px 12px;
    margin-bottom: 0;
    
    font-weight: 400;
	
	
	
  
 }
.inputSubmit{ width:150px ;height:45px; cursor:pointer ; color: #fff;
    background-color: #337ab7;
    border-color: #2e6da4;
	 
    display: inline-block;
      
	
	
	
	  }
.inputSelect{ width:200px ;      padding: 6px 12px;
    margin-bottom: 0;
    
    font-weight: 400;
    line-height: 1.42857143; 
   
  }
select > option { font-size:20px}


#popup{ margin-left:7%
;position:absolute; top:0px; display:none; width:80%; min:80%; background:#fff; border-radius:50px; padding:30px; margin-left:6%; margin-top:4%;  
}
#close{ cursor:pointer;position:absolute; top: -24px;
    left: -33px;}


 /*
 
 @media (max-width:400px) {}
    @media (min-width:401px) and (max-width:599px) {}
    @media (min-width:600px) and (max-width:767px) {}
    @media (min-width:768px) and (max-width:950px) {}
    @media (min-width:951px) and (max-width:1050px) {}
    @media (min-width:1051px) {}
	*/
/* Smartphones (portrait and landscape) ----------- */
@media only screen 
and (min-device-width : 320px) 
and (max-device-width : 480px) {
/* Styles */

 
}

/* Smartphones (landscape) ----------- */
@media only screen 
and (min-width : 321px) {
/* Styles */
}

/* Smartphones (portrait) ----------- */
@media only screen 
and (max-width : 320px) {
/* Styles */
}

/* iPads (portrait and landscape) ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) {
/* Styles */
/*.input{  height:60px; 
}
 
.inputSelect{ height:70px;    }*/


}

/* iPads (landscape) ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) 
and (orientation : landscape) {
/* Styles */
}

/* iPads (portrait) ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) 
and (orientation : portrait) {
/* Styles */
}

/* Desktops and laptops ----------- */
@media only screen 
and (min-width : 1224px) {
/* Styles */

}

/* Large screens ----------- */
@media only screen 
and (min-width : 1824px) {
/* Styles */

}

/* iPhone 4 ----------- */
@media
only screen and (-webkit-min-device-pixel-ratio : 1.5),
only screen and (min-device-pixel-ratio : 1.5) {
/* Styles */
*{ font-size:50px; font-family:tahoma}

.mobile_img{     max-width:90%;  border:1px solid #f00; height:100%}
.input{ width:500px ; height:100px;  font-size:50px; margin-bottom:20px
}
.inputSubmit{ width:300px ;height:112px; padding:5px ;  
;  font-size: 40px;
    font-weight: bold;  }
.inputSelect{ width:300px ;height:114px;padding:5px ;font-size:50px ; sel  }
select > option { font-size:15px}
 


#popup > p{ margin-left:6%; font-size:35px
 
}
 



}




 
 

</style>	







</head>


<body>

<?php








$LandingCode=$_REQUEST['LanCode'];
$affiliateID=$_REQUEST['AffiCode'];




// get landing data
$selecLandingPage=mysqli_query($connect,"select * from landingpage
where landingPageCode='".$LandingCode."'");

 

$rowLadingPage=mysqli_fetch_array($selecLandingPage);




 
// get affiliate data
 $selectAffileLand=mysqli_query($connect,"select * from affiliates_landing 
 where affiliates_landing_landingId='".$rowLadingPage['landingPage_id']."'
 and affiliates_landing_CodeID='".$affiliateID."'
  ");

 





if(mysqli_num_rows( $selectAffileLand)==0){
	echo("Wrong Affilites ID");
	exit();
	}

$rowAffile=mysqli_fetch_array($selectAffileLand);





// get service Data
$selService=mysqli_query($connect,"  select * from servicesoperator inner join services
 on Services_id=ServicesOperator_servicesID
 inner join landingserviceoperator 
 on landingServiceOperator_serOpID=ServicesOperator_id
where  landingServiceOperator_landingid=".$rowLadingPage['landingPage_id']."
group by 
 Services_id");
 


$rowService=mysqli_fetch_array($selService);










?>
<style>body{ background-color:<?php 	echo($rowLadingPage['landingPage_BG']); ?>;
	
	}</style>
    
    
    
    
    <?php 
 if(!isset($_REQUEST['error'])){
	savelogs($connect,$rowAffile['affiliates_landing_id'],$rowAffile['affiliates_landing_affID']
	,$rowLadingPage['landingPage_id'],"View");
 }
	?>

<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
 <tr align="center">
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td align="center"><?php 	echo($rowLadingPage['landingPage_Title']); ?></td>
  </tr>
  <tr align="center">
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td align="center"><img src="landingPageFiles/<?php  echo($rowLadingPage['landingPage_image']); ?>"  class="mobile_img" alt=""/></td>
  </tr>
  <tr>
    <td align="center">&nbsp;</td>
  </tr>
  <tr>
    <td align="center" dir="rtl"><?php  echo($rowLadingPage['landingPage_introText']); ?></td>
  </tr>
  <tr>
    <td align="center">&nbsp;</td>
  </tr>
  
  <?php  
 $selectSubscriber=mysqli_query($connect,"select * from SubscribersLogs
 where SubscribersLogs_sessionid='".session_id()."'
 and SubscribersLogs_sessionidExpired > now()
 ");
 
 if(mysqli_num_rows($selectSubscriber)==0){ 
  ?>
  <tr>
    <td align="center"  ><form name="form1" method="post" action="">
      <input type="hidden" name="opt" value="1">
       <input type="hidden" name="landCode" value="<?php echo($rowLadingPage['landingPageCode']); ?>">
       <input type="hidden" name="affCode" value="<?php echo($affiliateID); ?>">
        <input type="hidden" name="transactionID" value="<?php echo(post($_REQUEST['transactionID'])); ?>">
        <input type="hidden" name="serviceid" value="<?php echo($rowService['Services_id']); ?>">
        
        
       
      <select name="prefix" id="prefix"   class="inputSelect" >
      <option value="0"> المقدمة </option>
      <?php   
	  
	  // get operator prefix 
	  
$selectDate=mysqli_query($connect,"
select 
operatorPrefix_pref,operatorPrefix_operatorID ,Operator_name,ServicesOperator_id

 from landingserviceoperator
inner join landingpage on landingPage_id=landingServiceOperator_landingid
inner join servicesoperator on ServicesOperator_id=landingServiceOperator_serOpID
inner join operator on Operator_id=ServicesOperator_operatorID
inner join operatorprefix on operatorPrefix_operatorID=Operator_id
where landingPage_id='".$rowLadingPage['landingPage_id']."'
group by ServicesOperator_id
");



while($rowPrefix=mysqli_fetch_array($selectDate)){
	  
	  ?>
	  <option value="<?php echo($rowPrefix['operatorPrefix_operatorID']); ?>*<?php echo($rowPrefix['operatorPrefix_pref']); ?>*<?php echo($rowPrefix['ServicesOperator_id']); ?>" ><?php /*echo($rowPrefix['Operator_name']); ?> - <?php*/ echo($rowPrefix['operatorPrefix_pref']); ?></option>
	  <?php } ?>
      </select>
      
      <input type="text" name="mobile" id="mobile"       maxlength="7" class="input">
      <input type="submit" name="submit" id="submit"   value="<?php echo($rowLadingPage['landingPageSubmitValue']); ?>" class="inputSubmit">
    </form></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
 
  <?php }else{ ?>   
  <tr>
    <td align="center"><form id="form2" name="form2" method="post">
    <input type="hidden" name="opt" value="1">
    <input type="hidden" name="optV" value="1">
     <input type="hidden" name="transactionID" value="<?php echo($_REQUEST['transactionID']); ?>">
      
      <input type="text" name="vkey" id="vkey" class="input"  placeholder="كود التفعيل">
      <input type="submit" name="submit2" id="submit2" value="ارسل" class="inputSubmit">
    </form></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr> <?php }  ?>
  
  <?php if(isset($_REQUEST['error'])){ ?>
  <tr>
    <td>&nbsp;</td>
  </tr>
   <tr>
    <td align="center"><?php  
	
	switch($_REQUEST['error']){
		
		case 0:
		// sucess
		echo($rowLadingPage['landingPage_SubmitMessage']);
		break;
		
		case 1:
		// fill all feilds
		echo("الرجاء ادخل جميع الحقول");
		break;
		case 2:
		// error 
		echo($rowLadingPage['SubmitMessage_errorVerificationMEssage']);
		break;
		
			case 3:
		// thank you 
		echo($rowLadingPage['SubmitMessage_thankYouMessage']);
		break;
			case 4:
		// verification Wrong 
		echo($rowLadingPage['SubmitMessage_wrongVerification']);
		break;
		case 5:
		// verification Wrong 
		echo("انت مسجل لدبنا من قبل ");
		break;
		}
	
	
	 ?></td>
  </tr> 
  <tr>
    <td>&nbsp;</td>
  </tr><?php }  ?>
  <tr>
    <td align="center"> <?php 	echo($rowLadingPage['SubmitMessage_pricemessage']); ?></td>
  </tr>
  <tr>
    <td align="center"> <?php 	echo($rowLadingPage['SubmitMessage_unsubmessage']); ?></td>
  </tr>

  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr align="center">
    <td><a href="#"    onclick="toggle_visibility('popup');">سياسه الاستخدام</a></td>
  </tr>
</table>
<script>

  function toggle_visibility(id) {
               var e = document.getElementById(id);
               if(e.style.display == 'block')
                  e.style.display = 'none';
               else
                  e.style.display = 'block';

            }
            function closeDiv(id) {
               var e = document.getElementById(id);
              
                  e.style.display = 'none';
             
			}
            
            </script>
 <div id="popup"   style="direction:rtl">
 <div id="close" onClick="closeDiv('popup')"  > X</div>
 
 
 
  <?php 	echo($rowLadingPage['landingPage_disclamer']); ?></div>
</body>
</html>

<?php  }  ?>