<?php 
include("includes/db_helper.php");
include("includes/lb_helper.php"); 
include("language/api_language.php"); 

$file_path = getBaseUrl();

$mysqli->set_charset('utf8mb4');

$remote_addr = $_SERVER['REMOTE_ADDR'];
$user_agent = trim($_SERVER['HTTP_USER_AGENT']);
if (isset($_SERVER['HTTP_ORIGIN'])) {
    header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
}
header('Access-Control-Allow-Credentials: true');
header('Content-Type: application/json');

$jsonData = json_decode(file_get_contents('php://input'), true);

// App details
if(@$jsonData['token'] === APP_TOKEN){
	
    /* Use tbl_settings */
    $sql="SELECT * FROM tbl_settings WHERE id='1'";
    $result = mysqli_query($mysqli,$sql) or die(mysqli_error($mysqli));
    while($data = mysqli_fetch_assoc($result)){
        // App Details
		$row['app_details']['app_name'] = $data['app_name'];
		$row['app_details']['app_logo'] = $file_path."images/".$data['app_logo'];
		$row['app_details']['app_email'] = $data['app_email'];
		$row['app_details']['app_author'] = $data['app_author'];
        $row['app_details']['app_contact'] = $data['app_contact'];
        $row['app_details']['app_author'] = $data['app_author'];
        $row['app_details']['app_contact'] = $data['app_contact'];
        $row['app_details']['app_website'] = $data['app_website'];
        $row['app_details']['app_description'] = $data['app_description'];
        $row['app_details']['app_developed_by'] = $data['app_developed_by'];
		$row['app_details']['bg_login'] = !empty($data['bg_login']) ? $file_path."images/".$data['bg_login'] : "";
		$row['app_details']['bg_main'] = !empty($data['bg_main']) ? $file_path."images/".$data['bg_main'] : "";
		$row['app_details']['bg_setting'] = !empty($data['bg_setting']) ? $file_path."images/".$data['bg_setting'] : "";
		$row['app_details']['bg_search'] = !empty($data['bg_search']) ? $file_path."images/".$data['bg_search'] : "";
		$row['app_details']['bg_color'] = $data['bg_color'];
		$row['app_details']['countdown_status'] = (bool) $data['countdown_status'];
		$row['app_details']['countdown_time'] = $data['countdown_time'];
		$row['app_details']['check_auth'] = (int) $data['check_auth'];

    }
    mysqli_free_result($result);
	
	$demo = "";
	if ($config_ini['demo'] == 1) {
		$demo = " AND created_dns >= NOW() - INTERVAL 7 DAY ";
	}
	
	/* Use tbl_dns */
	$sql="SELECT * FROM tbl_dns WHERE tbl_dns.status='1' $demo ORDER BY tbl_dns.id DESC";
	$result = mysqli_query($mysqli,$sql) or die(mysqli_error($mysqli));
	$rowss = array();
    if(mysqli_num_rows($result) > 0){
        while ($data = mysqli_fetch_assoc($result)){
			$rowss[] = $data['dns_base'];
        }
    }
    foreach ($rowss as $dns) {
        $api_link = $dns . '/player_api.php?username=' . $jsonData['username'] . '&password=' . $jsonData['password'];
        $api_req = call_api_dns($api_link);
        if (is_array($api_req) && $api_req['result'] == 'success') {
            $data = $api_req['data'];
            if (isset($data->user_info->auth) && $data->user_info->auth != 0) {
				$row['app_player'] = $data;
				$row['serverURL'] = $dns;
			    break;
            }
        }
    }
	mysqli_free_result($result);
	
    $jsonObj= array();
	$data_arr= array();
	/* Use tbl_notification */
	$sql="SELECT * FROM tbl_notification WHERE tbl_notification.`id` ORDER BY tbl_notification.`id` DESC";
    $result = mysqli_query($mysqli,$sql) or die(mysqli_error($mysqli));
    if(mysqli_num_rows($result) > 0){
        while ($data = mysqli_fetch_assoc($result)){
            
            $data_arr['id'] = $data['id'];
          	$data_arr['notification_title'] = $data['notification_title'];
          	$data_arr['notification_msg'] = $data['notification_msg']; 
          	$data_arr['notification_description'] = $data['notification_description']; 
            $data_arr['notification_on'] = calculate_time_span($data['notification_on'],true);
			array_push($jsonObj, $data_arr);
        }
    }
	$row['notification_data'] = $jsonObj;
	mysqli_free_result($result);
	
    $set = $row;
	outputJson($set);
	die();
}
else if(@$jsonData['token']=="get_settings") {

    /* Use tbl_settings */
    $sql="SELECT * FROM tbl_settings WHERE id='1'";
    $result = mysqli_query($mysqli,$sql) or die(mysqli_error($mysqli));
    while($data = mysqli_fetch_assoc($result)){
        // App Details
		$row['app_details']['app_name'] = $data['app_name'];
		$row['app_details']['app_logo'] = $file_path."images/".$data['app_logo'];
		$row['app_details']['app_email'] = $data['app_email'];
		$row['app_details']['app_author'] = $data['app_author'];
        $row['app_details']['app_contact'] = $data['app_contact'];
        $row['app_details']['app_author'] = $data['app_author'];
        $row['app_details']['app_contact'] = $data['app_contact'];
        $row['app_details']['app_website'] = $data['app_website'];
        $row['app_details']['app_description'] = $data['app_description'];
        $row['app_details']['app_developed_by'] = $data['app_developed_by'];
		$row['app_details']['bg_login'] = !empty($data['bg_login']) ? $file_path."images/".$data['bg_login'] : "";
		$row['app_details']['bg_main'] = !empty($data['bg_main']) ? $file_path."images/".$data['bg_main'] : "";
		$row['app_details']['bg_setting'] = !empty($data['bg_setting']) ? $file_path."images/".$data['bg_setting'] : "";
		$row['app_details']['bg_search'] = !empty($data['bg_search']) ? $file_path."images/".$data['bg_search'] : "";
		$row['app_details']['bg_color'] = $data['bg_color'];
		$row['app_details']['countdown_status'] = (bool) $data['countdown_status'];
		$row['app_details']['countdown_time'] = $data['countdown_time'];
		$row['app_details']['check_auth'] = $data['check_auth'];
		$row['app_details']['is_theme'] = (string) $data['is_theme'];
		$row['app_details']['is_epg'] = (string) $data['is_epg'];
		$row['app_details']['is_category'] = (string) $data['is_category'];

    }
    mysqli_free_result($result);
	outputJson($row);
	die();
}
else if(@$jsonData['token']=="post_report"){

    $user_name=cleanInput($postData['user_name']);
	$user_pass=cleanInput($postData['user_pass']);
	$report_title=cleanInput($postData['report_title']);
	$report_msg=cleanInput($postData['report_msg']);
    
	$data = array(
        'user_name' => $user_name,
        'user_pass'  =>  $user_pass,
        'report_title'  =>  $report_title,
        'report_msg'  =>  $report_msg,
        'report_on'  =>  strtotime(date('d-m-Y h:i:s A'))
    );
    $qry = Insert('tbl_reports',$data);
    
	$set = array('MSG'=> $app_lang['report_success'],'success'=> '1');
	outputJson($set);
	die();
}
else if(@$jsonData['token']=="get_poster") {

	$jsonObj= array();	

    $sql="SELECT * FROM tbl_poster_gallery WHERE tbl_poster_gallery.status='1' AND tbl_poster_gallery.poster_type ='movie_ui' ORDER BY RAND() DESC";
	$result = mysqli_query($mysqli,$sql) or die(mysqli_error($mysqli));
    if(mysqli_num_rows($result) > 0){
        while($data = mysqli_fetch_assoc($result)){
            $row['poster_image'] = $file_path.'images/'.$data['poster_image'];
            array_push($jsonObj,$row);
        }
    }
	
	outputJson($jsonObj);
	die();
}
else if(@$jsonData['deeplink'] === APP_TOKEN) {

	$user = "deeplink";
	$pass = "deeplink";

    if($jsonData['username'] == $user && $jsonData['password'] == $pass){
       $output = array("status"=>"success","error"=>"Login success");
	}else{
		$output = array("status"=>"failed","error"=>"Invalid Login");
    }
	
	outputJson($output);
	die();
}else{
	$output[] = array("status"=>"failed","error"=>"Invalet Token");
	outputJson($output);
	die();
}

