<?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 lang="en">
<head>
   
    <meta charset="utf-8">
    <title>Welcome to Hadara SMS Push Platform </title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="Charisma, a fully featured, responsive, HTML5, Bootstrap admin template.">
    

    <!-- The styles -->
    <link id="bs-css" href="Admins/css/bootstrap-cerulean.min.css" rel="stylesheet">

    <link href="Admins/css/charisma-app.css" rel="stylesheet">
    <link href='Admins/bower_components/fullcalendar/dist/fullcalendar.css' rel='stylesheet'>
    <link href='Admins/bower_components/fullcalendar/dist/fullcalendar.print.css' rel='stylesheet' media='print'>
    <link href='Admins/bower_components/chosen/chosen.min.css' rel='stylesheet'>
    <link href='Admins/bower_components/colorbox/example3/colorbox.css' rel='stylesheet'>
    <link href='Admins/bower_components/responsive-tables/responsive-tables.css' rel='stylesheet'>
    <link href='Admins/bower_components/bootstrap-tour/build/css/bootstrap-tour.min.css' rel='stylesheet'>
    <link href='Admins/css/jquery.noty.css' rel='stylesheet'>
    <link href='Admins/css/noty_theme_default.css' rel='stylesheet'>
    <link href='Admins/css/elfinder.min.css' rel='stylesheet'>
    <link href='Admins/css/elfinder.theme.css' rel='stylesheet'>
    <link href='Admins/css/jquery.iphone.toggle.css' rel='stylesheet'>
    <link href='Admins/css/uploadify.css' rel='stylesheet'>
    <link href='Admins/css/animate.min.css' rel='stylesheet'>

    <!-- jQuery -->
    <script src="Admins/bower_components/jquery/jquery.min.js"></script>

    <!-- The HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->

    <!-- The fav icon -->
    <link rel="shortcut icon" href="img/favicon.ico">

</head>

<body>

<?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");
?>


 	





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

 <div class="ch-container">
 <div class="row">
        <div class=" col-md-12 center ">
    &nbsp;</div></div>
  <div class="row">
        <div class=" col-md-12 center "><?php 	echo($rowLadingPage['landingPage_Title']); ?></div></div>
  <div class="row">
        <div class=" col-md-12 center ">
    &nbsp;</div></div>
  <div class="row">
        <div class=" col-md-12 center "><img src="landingPageFiles/<?php  echo($rowLadingPage['landingPage_image']); ?>"   class="form-control" alt=""/></div></div>
  <div class="row">
        <div class=" col-md-12 center ">&nbsp;</div></div>
  <div class="row">
        <div class=" col-md-12 center "><?php  echo($rowLadingPage['landingPage_introText']); ?></div></div>
  <div class="row">
        <div class=" col-md-12 center ">&nbsp;</div></div>
  
  <?php  
 $selectSubscriber=mysqli_query($connect,"select * from SubscribersLogs
 where SubscribersLogs_sessionid='".session_id()."'
 and SubscribersLogs_sessionidExpired > now()
 ");
 
 if(mysqli_num_rows($selectSubscriber)==0){ 
  ?>
  <div class="row">
        <div class=" col-md-12 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']); ?>">
        
       <div class="input-group input-group-lg">
       
      <select name="prefix" id="prefix"    class="form-control" >
      <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="form-control">
      <input type="submit" name="submit" id="submit"   value="<?php echo($rowLadingPage['landingPageSubmitValue']); ?>" class="form-control">
      </div>
    </form>
    
     </div>
    
    </div></div>
 
    &nbsp;</div></div>
 
  <?php }else{ ?>   
  <div class="row">
        <div class=" col-md-12 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></div></div>
 
    &nbsp;</div></div> <?php }  ?>
  
  <?php if(isset($_REQUEST['error'])){ ?>
  
    &nbsp;</div></div>
   <div class="row">
        <div class=" col-md-12 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;
		}
	
	
	 ?></div></div> 
 
    &nbsp;</div></div><?php }  ?>
  <div class="row">
        <div class=" col-md-12 center "> <?php 	echo($rowLadingPage['SubmitMessage_pricemessage']); ?></div></div>
  <div class="row">
        <div class=" col-md-12 center "> <?php 	echo($rowLadingPage['SubmitMessage_unsubmessage']); ?></div></div>

 
    &nbsp;</div></div>
  <div class="row">
        <div class=" col-md-12 center ">
    <a href="#"    onclick="toggle_visibility('popup');">سياسه الاستخدام</a></div></div>
 
<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; display:none">
 <div id="close" onClick="closeDiv('popup')"  > X</div>
 
 
 
  <?php 	echo($rowLadingPage['landingPage_disclamer']); ?></div>
 



<!-- external javascript -->

<script src="Admins/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>

<!-- library for cookie management -->
<script src="Admins/js/jquery.cookie.js"></script>
<!-- calender plugin -->
<script src='Admins/bower_components/moment/min/moment.min.js'></script>
<script src='Admins/bower_components/fullcalendar/dist/fullcalendar.min.js'></script>
<!-- data table plugin -->
<script src='Admins/js/jquery.dataTables.min.js'></script>

<!-- select or dropdown enhancer -->
<script src="Admins/bower_components/chosen/chosen.jquery.min.js"></script>
<!-- plugin for gallery image view -->
<script src="Admins/bower_components/colorbox/jquery.colorbox-min.js"></script>
<!-- notification plugin -->
<script src="Admins/js/jquery.noty.js"></script>
<!-- library for making tables responsive -->
<script src="Admins/bower_components/responsive-tables/responsive-tables.js"></script>
<!-- tour plugin -->
<script src="Admins/bower_components/bootstrap-tour/build/js/bootstrap-tour.min.js"></script>
<!-- star rating plugin -->
<script src="Admins/js/jquery.raty.min.js"></script>
<!-- for iOS style toggle switch -->
<script src="Admins/js/jquery.iphone.toggle.js"></script>
<!-- autogrowing textarea plugin -->
<script src="Admins/js/jquery.autogrow-textarea.js"></script>
<!-- multiple file upload plugin -->
<script src="Admins/js/jquery.uploadify-3.1.min.js"></script>
<!-- history.js for cross-browser state change on ajax -->
<script src="Admins/js/jquery.history.js"></script>
<!-- application script for Charisma demo -->
<script src="Admins/js/charisma.js"></script>
</div>

</body>
</html>


<?php  }  ?>