<?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;
}




 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 Subscribers set  
	  Subscribers_status=1
	  ,Subscribers_dateVerification=now()
	  where Subscribers_verificationCode='".$_REQUEST['vkey']."' and Subscribers_sessionid='".session_id()."'
	 
	  ");
	  
	  
	  if( $updateSubscriber && mysqli_affected_rows($connect)>0){
		  session_regenerate_id() ;
	  	 go("landingPage.php?error=3&LanCode=".post($_REQUEST['LanCode'])."&AffiCode=".post($_REQUEST['AffiCode'])."");
	  
	  }else{
		   go("landingPage.php?error=4&LanCode=".post($_REQUEST['LanCode'])."&AffiCode=".post($_REQUEST['AffiCode'])."");
		  
		  }
	  
	  
	  
	  }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['LanCode']."'");
$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['AffiCode']."'
 
 
 ");

 





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 Subscribers set
		    Subscribers_phone='".$phone."',
			 Subscribers_serviceID='".$_REQUEST['serviceid']."', 
		   Subscribers_landingid='".$rowLadingPage['landingPage_id']."', 
		    
			  Subscribers_affi='".$rowAffile['affiliates_landing_affID']."', 
			Subscribers_verificationCode='".generateRandom(6)."',
			 Subscribers_status=0
			 ,Subscribers_date=now()
			 ,Subscribers_sessionid='".session_id()."'
			 ,Subscribers_sessionidExpired=date_add(now(), interval 3 minute)
			 ,Subscribers_operatorID='".$Prefix[0]."'
			  ,Subscribers_serviceoperator='".$Prefix[2]."'
			 ");
		   
		   
		    
		   
		 if($insert){
			  $error=0;
			 }else{
				  $error=2;
				 }		 
		 
		 
		 }else{
			 $error=1;
			 }
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 header("location:landingPage.php?error=".$error."&LanCode=".post($_REQUEST['LanCode'])."&AffiCode=".post($_REQUEST['AffiCode'])."");
	 
	 
	 
	 
 }
	 
	 
	 
	 
	 
	 
	 
	 
	 }else{

 
 ?>






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

<link rel="stylesheet" type="text/css" href="responsiveform.css">
<link rel="stylesheet" media="screen and (max-width: 1200px) and (min-width: 601px)" href="responsiveform1.css" />
<link rel="stylesheet" media="screen and (max-width: 600px) and (min-width: 351px)" href="responsiveform2.css" />
<link rel="stylesheet" media="screen and (max-width: 350px)" href="responsiveform3.css" />

 






</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);










?>


<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td align="center">&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"><?php  echo($rowLadingPage['landingPage_introText']); ?></td>
  </tr>
  <tr>
    <td align="center">&nbsp;</td>
  </tr>
  
  <?php  
 $selectSubscriber=mysqli_query($connect,"select * from Subscribers 
 where Subscribers_sessionid='".session_id()."'
 and Subscribers_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="LanCode" value="<?php echo($rowLadingPage['landingPageCode']); ?>">
       <input type="hidden" name="AffiCode" value="<?php echo($affiliateID); ?>">
        <input type="hidden" name="serviceid" value="<?php echo($rowService['Services_id']); ?>">
        
        
       
      <select name="prefix" id="prefix"    class="textarea"  >
      <option value="0"> Operator </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']."'
");

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"   >
      <input type="submit" name="submit" id="submit"   value="<?php echo($rowLadingPage['landingPageSubmitValue']); ?>">
    </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="LanCode" value="<?php echo($_REQUEST['LanCode']); ?>">
       <input type="hidden" name="AffiCode" value="<?php echo($_REQUEST['AffiCode']); ?>">
     
      <input type="text" name="vkey" id="vkey" style="float:left; margin-left:10px;width:40%"  placeholder="كود التفعيل">
      <input type="submit" name="submit2" id="submit2" value="ارسل" class="bttn_img">
    </form></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr> <?php }  ?>
  <tr>
    <td>&nbsp;</td>
  </tr>
   <tr>
    <td align="center"><?php  
	if(isset($_REQUEST['error'])){
	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;
		
		}
	}
	
	 ?></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
</table>
 
</body>
</html>

<?php  }  ?>