<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
global $LANG;
global $configverssion_id;
global $configapp_version;
global $server;
global $hooks;
global $URL;

function v3updates20200401_GETLISTIP($userID) {
	global $server;	
	if(userData($userID,'role_id') < 3) $userID = 0;
	$query="SELECT * FROM whitlisted_api_ips WHERE customer_id = '$userID'"; 
	$result = mysqli_query($server,$query) or die(mysqli_error($server));  
	$records = mysqli_num_rows($result);
	if($records > 0) {
		$roww = mysqli_fetch_assoc($result); 
		return $roww['ip'];
	} else {
		return '';
	}
}
function v3updates20200401_countDeRoute($user=0) {
  	global $server;	
	$query="SELECT count(id) as value FROM routes WHERE country_code = '0' AND operator_code = 0 AND customer_id = 0"; 
	$result = mysqli_query($server,$query) or die(mysqli_error($server));  
	$row = mysqli_fetch_assoc($result); 
	return $row['value'];	
}

//save IP API
$hooks->add_action('CustomJavaScripts','v3updates20200401_jqueries');
function v3updates20200401_jqueries(){
	global $userID; global $LANG; global $server;global $URL;
	if( ($URL == 'aapis' || $URL== 'apis') && getUser() > 0) { ?>
	<script>
	$(document).ready(function(){
		$('#ips-button').click(function(event) { 
			event.preventDefault();	$('#ips-button').hide(); $('#ips-spin').show();
			var formData = {
				'customer_id' : '<?=getUser()?>',
				'ip_addresses' : $('#ip_addresses').val(),
				'v3updates20200401_updateIP' : '<?=$userID?>'
			};
			$.ajax({
				type        : 'POST', 
				url         : 'API',
				data        : formData, 
			}).done(function(data) {
				var status = data;
				if(status.replace(/\s+/g, '') == 'success') {
					swal({   
						title: "<?=$LANG['Done']?>!",   
						text: "<?=$LANG['Your IP address list has been updated']?>",   
						type: "success",   
						confirmButtonColor: "#086",   
						confirmButtonText: "All Good!", 
						closeOnConfirm: false 
					}, function(){  
						window.location = '<?=$URL?>'; 
					});
					$('#ips-button').show();	$('#ips-spin').hide();
				} else {
					swal("Oops!", status, "warning");
					$('#ips-button').show();	$('#ips-spin').hide();	
				}
			}).fail(function (jqXHR,status,err) {
				swal("Oops!", '<?=$LANG['Unable to establish connection to server']?>! '+err, "warning");
				$('#ips-button').show();	$('#ips-spin').hide();
			});
		});	
	});	   
	</script>
<?php }
}

$hooks->add_action('OnAjaxRequests','v3updates20200401_APIRE');
function v3updates20200401_APIRE() {
	global $server;	
	if(isset($_POST['v3updates20200401_updateIP'])) {
		foreach ($_POST as $key => $value ){
			${$key} = $value = mysqli_real_escape_string($server,$value);
		} 
		if(getUser() < 1) {
			die("User authentication failed! Please login and try again");	
		}
		$customer_id = 0;
		if(userRole()>2) {
			$customer_id = getUser();
		}
		$ip_addresses = preg_replace('/\s+/', '', $ip_addresses);
		$query="SELECT * FROM whitlisted_api_ips WHERE customer_id = '$customer_id'"; 
		$result = mysqli_query($server,$query) or die(mysqli_error($server));  
		$records = mysqli_num_rows($result);
		if($records > 0) {
			mysqli_query($server,"UPDATE `whitlisted_api_ips` SET `ip` = '$ip_addresses' WHERE `customer_id` = '$customer_id'");
		} else {
			mysqli_query($server,"INSERT INTO whitlisted_api_ips (`customer_id`, `ip`) VALUES ('$customer_id', '$ip_addresses');") or die(mysqli_error($server));
		}
		logEvent($customer_id,'Updated API IP access restriction settings');		
		die("success");
	}
}
?>