<?php


include ('dbConnect.php');
ob_start( );
$ui= $_POST['ui'] ? $_POST['ui'] : $_GET['ui'];

if($ui=="get_users"){
 
	$s = $_POST['st'];
	$aid = $_POST['aid'];
    //
	if ($aid < 0){
		$usql = "SELECT * FROM `Users` WHERE `subscribed` = '$s' ORDER BY `join_date` DESC LIMIT 15000 ";
	}else{
		$usql = "SELECT * FROM `Users` WHERE `subscribed` = '$s' AND `affiliateID` = '$aid' ORDER BY `join_date` DESC LIMIT 5000";
	}
    $result = mysqli_query($hookup, $usql);
    $cnt = 0;
    $xml='<?xml version="1.0" encoding="utf-8"?><data>';

    //
  
        while ($row = mysqli_fetch_assoc($result)) {
            
                $cnt++;

                $xml.= "<item><e>".$row['email']."</e>";
				/*
                $xml.="<first_name>".$row['first_name']."</first_name>";

                $xml.="<middle_name>".$row['middle_name']."</middle_name>";

                $xml.="<last_name>".$row['last_name']."</last_name>";
				*/
                $xml.="<jd>".split(" ",$row['join_date'])[0]."</jd>";
				
				
                $xml.="<cfirst_name>".$row['cfirst_name']."</cfirst_name>";
				/*
                $xml.="<cmiddle_name>".$row['cmiddle_name']."</cmiddle_name>";

                $xml.="<clast_name>".$row['clast_name']."</clast_name>";
				*/
               
                $xml.="<g>".$row['gender']."</g>";
                $xml.="<aid>".$row['affiliateID']."</aid>";

               // $xml.="<uid>".$row['uid']."</uid>";

                $xml.="<s>".$row['subscribed']."</s>";
                $xml.="<mo>".$row['mail_opt']."</mo>";

                $xml.="<sd>".split(" ",$row['sub_date'])[0]."</sd>";
				
               // $xml.="<init>".$row['init']."</init>";

              //  $xml.="<public_id>".$row['public_id']."</public_id>";

                $xml.="</item>";            

        }	
       
		$xml.= "<total>".$cnt."</total></data>";
       
    
			
    echo $xml;
	
}else if($ui=="quick_load"){
    //
	$usql = "SELECT * FROM `Users` ORDER BY `join_date` DESC LIMIT 10000";
	
    $result = mysqli_query($hookup, $usql);
    $cnt = 0;
    $xml='<?xml version="1.0" encoding="utf-8"?><data>';

    //
  
        while ($row = mysqli_fetch_assoc($result)) {
            
                $cnt++;

                $xml.= "<item><e>".$row['email']."</e>";
				/*
                $xml.="<first_name>".$row['first_name']."</first_name>";

                $xml.="<middle_name>".$row['middle_name']."</middle_name>";

                $xml.="<last_name>".$row['last_name']."</last_name>";
				*/
                $xml.="<jd>".split(" ",$row['join_date'])[0]."</jd>";
				
				
                $xml.="<cfirst_name>".$row['cfirst_name']."</cfirst_name>";
				/*
                $xml.="<cmiddle_name>".$row['cmiddle_name']."</cmiddle_name>";

                $xml.="<clast_name>".$row['clast_name']."</clast_name>";
				*/
               
                $xml.="<g>".$row['gender']."</g>";
                $xml.="<aid>".$row['affiliateID']."</aid>";

               // $xml.="<uid>".$row['uid']."</uid>";

                $xml.="<s>".$row['subscribed']."</s>";
                $xml.="<mo>".$row['mail_opt']."</mo>";

                $xml.="<sd>".split(" ",$row['sub_date'])[0]."</sd>";
				
               // $xml.="<init>".$row['init']."</init>";

              //  $xml.="<public_id>".$row['public_id']."</public_id>";

                $xml.="</item>";            

        }	
       
		$xml.= "<total>".$cnt."</total></data>";
       
    
			
    echo $xml;
	
}else if($ui=="quick_sales"){
 
    //
	$usql = "SELECT * FROM `Users` WHERE  `subscribed` > 0 ORDER BY `sub_date` DESC LIMIT 1000 ";
	
    $result = mysqli_query($hookup, $usql);
    $cnt = 0;
    $xml='<?xml version="1.0" encoding="utf-8"?><data>';

    //
  
        while ($row = mysqli_fetch_assoc($result)) {
            
                $cnt++;

                $xml.= "<item><e>".$row['email']."</e>";
				/*
                $xml.="<first_name>".$row['first_name']."</first_name>";

                $xml.="<middle_name>".$row['middle_name']."</middle_name>";

                $xml.="<last_name>".$row['last_name']."</last_name>";
				*/
                $xml.="<jd>".split(" ",$row['join_date'])[0]."</jd>";
				
				
                $xml.="<cfirst_name>".$row['cfirst_name']."</cfirst_name>";
				/*
                $xml.="<cmiddle_name>".$row['cmiddle_name']."</cmiddle_name>";

                $xml.="<clast_name>".$row['clast_name']."</clast_name>";
				*/
               
                $xml.="<g>".$row['gender']."</g>";
                $xml.="<aid>".$row['affiliateID']."</aid>";

               // $xml.="<uid>".$row['uid']."</uid>";

                $xml.="<s>".$row['subscribed']."</s>";
                $xml.="<mo>".$row['mail_opt']."</mo>";

                $xml.="<sd>".split(" ",$row['sub_date'])[0]."</sd>";
				
               // $xml.="<init>".$row['init']."</init>";

              //  $xml.="<public_id>".$row['public_id']."</public_id>";

                $xml.="</item>";            

        }	
       
		$xml.= "<total>".$cnt."</total></data>";
       
    
			
    echo $xml;
	
}else if($ui=="search_users"){
	
	$email = $_POST['email'] ? $_POST['email'] : $_GET['email'];

	$bsql = "SELECT * FROM `Users` WHERE `email` = '$email'";
    $sresult = mysqli_query($hookup, $bsql);
    $cnt = 0;
    $xml="<xml><data>";
	
	 while ($row = mysqli_fetch_assoc($sresult)) {
            
                $cnt++;
                $xml.="<item><id>".$row['ID']."</id>";

                $xml.= "<email>".$row['email']."</email>";
				
				$xml.="<uid>".$row['uid']."</uid>";
				
                $xml.="<first_name>".$row['first_name']."</first_name>";

                $xml.="<middle_name>".$row['middle_name']."</middle_name>";

                $xml.="<last_name>".$row['last_name']."</last_name>";
				
				$xml.="<cfirst_name>".$row['cfirst_name']."</cfirst_name>";

                $xml.="<cmiddle_name>".$row['cmiddle_name']."</cmiddle_name>";

                $xml.="<clast_name>".$row['clast_name']."</clast_name>";
				
                $xml.="<subscribed>".$row['subscribed']."</subscribed>";

                $xml.="<subsdate>".$row['sub_date']."</subsdate>";

                $xml.="<user_name>".$row['user_name']."</user_name>";
				
				$xml.="<dob>".$row['dob']."</dob>";
				
                $xml.="</item>";            

        }	
       
    	$xml.= "</data></xml>";
			
    echo $xml;

}else if($ui=="edituser"){

	$uid =  $_POST['uid'];
	$subscribed = $_POST['subscribed'];
	$sub_date = $_POST['sub_date'];
	$first = $_POST['firstname'];
	$middle = $_POST['middlename'];
	$last = $_POST['lastname'];
	$cfirst = $_POST['cfirstname'];
	$cmiddle = $_POST['cmiddlename'];
	$clast = $_POST['clastname'];
	$uname = $_POST['user_name'];
	$dob = $_POST['dob'];
	

	$sql = "UPDATE `Users` SET `subscribed` = '$subscribed', `sub_date` = '$sub_date', `first_name` = '$first',  `middle_name` = '$middle',  `last_name` = '$last', `cfirst_name` = '$cfirst',  `cmiddle_name` = '$cmiddle',  `clast_name` = '$clast', `dob` = '$dob',  `user_name` = '$uname'  WHERE `ID` = '$uid'";

	$result=mysqli_query($hookup, $sql);

	if($result){
		

		$return = "success";

		echo $return; 

	}else{

		$return = "error=" . $type. " / ". mysqli_error($hookup);

		echo $return; 

	 }
}else if($ui=="user_optout"){

	$email=$_POST['email'];

	$sql = "UPDATE `Users` SET `mail_opt` = '0' WHERE `email` IN ($email)";

	$result=mysqli_query($hookup, $sql);

	if($result){
		

		$return = "success: ".$email;

		echo $return; 

	}else{

		$return = "error=" . $type. " / ". mysqli_error($hookup);

		echo $return; 

	 }
}else if($ui=="get_vendors"){


	$sql = "SELECT * FROM `Vendors`";
	$result=mysqli_query($hookup, $sql);

	 $xml="<xml><data>";
	
	 while ($row = mysqli_fetch_assoc($result)) {
            
                $xml.="<item><id>".$row['ID']."</id>";

                $xml.= "<vname>".$row['vname']."</vname>";
							
                $xml.="</item>";            

        }	
       
    	$xml.= "</data></xml>";
	
    echo $xml;
}
?>