<?php
include("includes/connection.php");
include("includes/function.php");
include("language/app_language.php");
include("smtp_email.php");

define("API_LANG_ORDER_BY", $settings_details['api_lan_order_by']);
define("API_GENR_ORDER_BY", $settings_details['api_gen_order_by']);

define("API_PAGE_LIMIT", $settings_details['api_page_limit']);

error_reporting(0);

date_default_timezone_set("Asia/Kolkata");

$file_path = getBaseUrl();

function generateRandomPassword($length = 10)
{
	$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	$charactersLength = strlen($characters);
	$randomString = '';
	for ($i = 0; $i < $length; $i++) {
		$randomString .= $characters[rand(0, $charactersLength - 1)];
	}
	return $randomString;
}

function get_user_status($user_id)
{
	global $mysqli;

	$user_qry = "SELECT * FROM tbl_users where id='" . $user_id . "'";
	$user_result = mysqli_query($mysqli, $user_qry);
	$user_row = mysqli_fetch_assoc($user_result);

	if (mysqli_num_rows($user_result) > 0) {
		if ($user_row['status'] == 0) {
			return 'false';
		} else if ($user_row['status'] == 1) {
			return 'true';
		}
	} else {
		return 'false';
	}
}


function quality_info_data($post_id, $param, $type = 'movie')
{

	global $mysqli;

	$sql = "SELECT * FROM tbl_quality WHERE `post_id` = '$post_id' AND `type` = '$type'";

	$res = mysqli_query($mysqli, $sql);

	if (mysqli_num_rows($res) > 0) {
		$row = mysqli_fetch_assoc($res);
		return $row[$param];
	} else {
		return '';
	}
	mysqli_free_result($res);
	exit;
}

function get_user_info($user_id)
{
	global $mysqli;

	$user_qry = "SELECT * FROM tbl_users where id='" . $user_id . "'";
	$user_result = mysqli_query($mysqli, $user_qry);
	$user_row = mysqli_fetch_assoc($user_result);

	return $user_row['name'];
}



$get_method = checkSignSalt($_POST['data']);


 if ($_GET['method_name'] == "cat_list") {
	$category_list = array();
	

	$query_rec = "SELECT COUNT(*) as num FROM tbl_category WHERE status=1";    	

    $total_pages = mysqli_fetch_array(mysqli_query($mysqli, $query_rec));
	$jsonObj = array();
	$cid = API_CAT_ORDER_BY;

	$page_limit = API_PAGE_LIMIT;

	$limit = ($get_method['page'] - 1) * $page_limit;
	
	$query = "SELECT cid,category_name,category_image FROM tbl_category WHERE `status`='1' ORDER BY tbl_category.$cid LIMIT $page_limit";	
	$sql = mysqli_query($mysqli, $query) or die(mysqli_error($mysqli));

	while ($data = mysqli_fetch_assoc($sql)) {
		$row['total_records'] = $total_pages['num'];
		$row['cid'] = $data['cid'];
		$row['category_name'] = $data['category_name'];
		$row['category_image'] = $file_path . 'images/' . $data['category_image'];
		$row['category_image_thumb'] = $file_path . 'images/thumbs/' . $data['category_image'];


		array_push($jsonObj, $row);
	}

   $set['DRAW'] = $jsonObj;

	header('Content-Type: application/json; charset=utf-8');
	echo $val = str_replace('\\/', '/', json_encode($set, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
	die();
} 
else if ($_GET['method_name'] == "all_iteams") {
    $category_list = array();
    $query_rec = "SELECT COUNT(*) as num FROM tbl_iteams WHERE status=1";
    // echo $query_rec;

    $total_pages = mysqli_fetch_array(mysqli_query($mysqli, $query_rec));

    $page_limit = 10;

    $limit = ($get_method['page'] - 1) * $page_limit;

    $jsonObj = array();

    // $cat_order = API_CAT_ORDER_BY;

    $query = "SELECT * FROM tbl_iteams WHERE status='1' LIMIT  $page_limit";
    $sql = mysqli_query($mysqli, $query);

    while ($data = mysqli_fetch_assoc($sql)) {
        $row['total_records'] = $total_pages['num'];

        $row['itemid'] = $data['itemid'];
        $row['cat_id'] = $data['cat_id'];
        $row['iteam_name'] = html_entity_decode($data['iteam_name'], ENT_QUOTES, "UTF-8");

        $category_image = $data['category_image'];
        if (empty($category_image)) {
            $row['iteam_image'] = $file_path . 'images/add-image.png';
            $row['iteam_image_thumb'] = $file_path . 'images/thumbs/add-image.png';
        } else {
            $row['iteam_image'] = $file_path . 'images/' . $data['iteam_image'];
            $row['iteam_image_thumb'] = $file_path . 'images/thumbs/' . $data['iteam_image'];
        }

        $row['iteam_image'] = $file_path . 'images/' . $data['iteam_image'];
        $row['iteam_image_thumb'] = $file_path . 'images/thumbs/' . $data['iteam_image'];

        array_push($jsonObj, $row);
    }

    $set['DRAW'] = $jsonObj;

    header('Content-Type: application/json; charset=utf-8');
    echo $val = str_replace('\\/', '/', json_encode($set, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
    die();
} else if ($get_method['method_name'] == "cat_iteams") {
    $post_order_by = API_CAT_POST_ORDER_BY;

    $cat_id = $get_method['cat_id'];

    $query_rec = "SELECT COUNT(*) as num FROM tbl_iteams
		LEFT JOIN tbl_category ON tbl_iteams.`cat_id`= tbl_category.`cid` 
		WHERE tbl_iteams.`cat_id`='$cat_id' AND tbl_category.`status`='1' AND tbl_iteams.`status`='1'";
    $total_pages = mysqli_fetch_array(mysqli_query($mysqli, $query_rec));
    $page_limit = 10;

    $limit = ($get_method['page'] - 1) * $page_limit;

    $jsonObj = array();

    $query2 = "SELECT * FROM tbl_iteams WHERE tbl_iteams.`status`='1'";

    $result2 = mysqli_query($mysqli, $query2);

    while ($data = mysqli_fetch_assoc($result2)) {
        $songs_list = explode(",",  $data['cat_id']);
        foreach ($songs_list as $song_id) {
            if ($cat_id == $song_id) {

                $row['aid'] = $data['itemid'];
                $row['cat_id'] = $data['cat_id'];
                $row['iteam_name'] = $data['iteam_name'];

                $image = $data['image'];
                if (empty($image)) {
                    $row['image'] = $file_path . 'images/add-image.png';
                    $row['image_thumb'] = $file_path . 'images/thumbs/add-image.png';
                } else {
                    $row['image'] = $file_path . 'images/' . $data['image'];
                    $row['image_thumb'] = $file_path . 'images/thumbs/' . $data['image'];
                }

                $row['status'] = $data['status'];
                $row['type'] = $data['iteam_subscription_type'];

                array_push($jsonObj, $row);
            }
        }
    }

    $set['DRAW'] = $jsonObj;

    header('Content-Type: application/json; charset=utf-8');
    echo $val = str_replace('\\/', '/', json_encode($set, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
    die();
} 
// else if ($_GET['method_name'] == "app_details") {
//     $jsonObj = array();

//     $query = "SELECT * FROM tbl_settings WHERE id='1'";
//     $sql = mysqli_query($mysqli, $query);

//     while ($data = mysqli_fetch_assoc($sql)) {

//         $row['app_version'] = $data['app_version'];

//         $row['app_privacy_policy'] = htmlspecialchars_decode($data['app_privacy_policy']);
//         $row['terms_condition'] = htmlspecialchars_decode($data['terms_condition']);

//         $row['publisher_id'] = $data['publisher_id'];
//         if ($data['show_interstitial'] == 0) {
//             $value = $data['show_interstitial'] = false;
//         } else {
//             $value = $data['show_interstitial'] = true;
//         }
//         $row['show_interstitial'] = $value;

//         if ($data['show_banner'] == 0) {
//             $value2 = $data['show_banner'] = false;
//         } else {
//             $value2 = $data['show_banner'] = true;
//         }
//         $row['show_banner'] = $value2;

//         if ($data['show_rewarded'] == 0) {
//             $value3 = $data['show_rewarded'] = false;
//         } else {
//             $value3 = $data['show_rewarded'] = true;
//         }
//         $row['show_rewarded'] = $value3;
//         $row['interstitial_android'] = $data['interstitial_android'];
//         $row['interstitial_ios'] = $data['interstitial_ios'];

//         $row['banner_android'] = $data['banner_android'];
//         $row['banner_ios'] = $data['banner_ios'];
//         $row['rewarded_android'] = $data['rewarded_android'];
//         $row['rewarded_ios'] = $data['rewarded_ios'];

//         if ($data['showAds'] == 0) {
//             $value4= $data['showAds'] = false;
//         } else {
//             $value4 = $data['showAds'] = true;
//         }

//         $row['showAds'] = $value4;

//         array_push($jsonObj, $row);
//     }



//     $set['DRAW'] = $jsonObj;

//     header('Content-Type: application/json; charset=utf-8');
//     echo $val = str_replace('\\/', '/', json_encode($set, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
//     die();
// } 
else if ($_GET['method_name'] == "app_details") {
    $jsonObj = array();

    $query = "SELECT * FROM tbl_settings WHERE id='1'";
    $sql = mysqli_query($mysqli, $query);

    while ($data = mysqli_fetch_assoc($sql)) {

        $row['app_version'] = $data['app_version'];

        
        $row['app_privacy_policy'] = htmlspecialchars_decode($data['app_privacy_policy']);
        $row['terms_condition'] = htmlspecialchars_decode($data['app_terms_and_conditions']);
        

        $row['google_app_id'] = $data['publisher_id'];
        $row['facebook_app_id'] = $data['f_publisher_id'];
        $row['applovin_app_id'] = $data['al_publisher_id'];
        
        if ($data['banner_ad_id_status'] == 0) {
            $value11= $data['banner_ad_id_status'] = false;
        } else {
            $value11 = $data['banner_ad_id_status'] = true;
        }
        
        if ($data['interstital_ad_id_status'] == 0) {
            $value22= $data['interstital_ad_id_status'] = false;
        } else {
            $value22 = $data['interstital_ad_id_status'] = true;
        }
        
        if ($data['app_open_ad_id_status'] == 0) {
            $value33= $data['app_open_ad_id_status'] = false;
        } else {
            $value33 = $data['app_open_ad_id_status'] = true;
        }
        
        if ($data['ios_banner_ad_id_status'] == 0) {
            $value44= $data['ios_banner_ad_id_status'] = false;
        } else {
            $value44 = $data['ios_banner_ad_id_status'] = true;
        }
        
        if ($data['ios_interstital_ad_id_status'] == 0) {
            $value55= $data['ios_interstital_ad_id_status'] = false;
        } else {
            $value55 = $data['ios_interstital_ad_id_status'] = true;
        }
        
        if ($data['ios_app_open_ad_id_status'] == 0) {
            $value66= $data['ios_app_open_ad_id_status'] = false;
        } else {
            $value66 = $data['ios_app_open_ad_id_status'] = true;
        }
        
        if ($data['rewarded_ios_status'] == 0) {
            $value77= $data['rewarded_ios_status'] = false;
        } else {
            $value77 = $data['rewarded_ios_status'] = true;
        }
        
        if ($data['rewarded_android_status'] == 0) {
            $value88= $data['rewarded_android_status'] = false;
        } else {
            $value88 = $data['rewarded_android_status'] = true;
        }

        $row['banner_android_status'] = $value11;
        $row['interstital_android_status'] = $value22;        
        $row['native_ad_status'] = $value33;        
        $row['banner_ios_status'] = $value44;
        $row['interstital_ios_status'] = $value55;        
        $row['ios_native_id_status'] = $value66;        
        $row['rewarded_ios_status'] = $value77;
        $row['rewarded_android_status'] = $value88;
        
        

        $row['google_banner_android_id'] = $data['banner_ad_id'];
        $row['google_interstital_android_id'] = $data['interstital_ad_id'];
        $row['google_rewarded_android_id'] = $data['rewarded_android'];

        $row['google_banner_ios_id'] = $data['banner_ad_id_ios']; 
        $row['google_interstital_ios_id'] = $data['interstital_ad_id_ios'];               
        $row['google_rewarded_ios_id'] = $data['rewarded_ios'];




        $row['facebook_banner_android_id'] = $data['f_banner_ad_id'];
        $row['facebook_interstital_android_id'] = $data['f_interstital_ad_id'];
        $row['facebook_rewarded_android_id'] = $data['f_rewarded_android'];

        $row['facebook_banner_ios_id'] = $data['f_banner_ad_id_ios'];    
        $row['facebook_interstital_ios_id'] = $data['f_interstital_ad_id_ios'];             
        $row['facebook_rewarded_ios_id'] = $data['f_rewarded_ios'];

        $row['applovin_banner_android_id'] = $data['al_banner_ad_id'];
        $row['applovin_interstital_android_id'] = $data['al_interstital_ad_id'];
        $row['applovin_rewarded_android_id'] = $data['al_rewarded_android'];
        $row['applovin_native_android_id'] = $data['al_app_open_ad_id'];

        $row['applovin_banner_ios_id'] = $data['al_banner_ad_id_ios'];    
        $row['applovin_interstital_ios_id'] = $data['al_interstital_ad_id_ios'];             
        $row['applovin_rewarded_ios_id'] = $data['al_rewarded_ios'];
        $row['applovin_native_id_ios'] = $data['ios_app_open_ad_id'];




        if ($data['showAds'] == 0) {
            $value4= $data['showAds'] = false;
        } else {
            $value4 = $data['showAds'] = true;
        }

        $row['showAds'] = $value4;
        $row['limit'] = (int) $data['d_limit'];
        array_push($jsonObj, $row);
    }



    $set['DRAW'] = $jsonObj;

    header('Content-Type: application/json; charset=utf-8');
    echo $val = str_replace('\\/', '/', json_encode($set, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
    die();
} 
elseif ($_GET['method_name'] == "get_subscription") {
	$category_list = array();
	

	$query_rec = "SELECT COUNT(*) as num FROM tbl_subscription WHERE status=1";    	

    $total_pages = mysqli_fetch_array(mysqli_query($mysqli, $query_rec));
	$jsonObj = array();
	$cid = API_CAT_ORDER_BY;

	$page_limit = API_PAGE_LIMIT;

	$limit = ($get_method['page'] - 1) * $page_limit;
	
	$query = "SELECT id,plan_name,plan_duration,plan_type,plan_price,plan_description,playstore_id,appstore_id,status,productCategory FROM tbl_subscription WHERE `status`='1'";	
	$sql = mysqli_query($mysqli, $query) or die(mysqli_error($mysqli));

	while ($data = mysqli_fetch_assoc($sql)) {
		$row['total_records'] = $total_pages['num'];
		$row['id'] = $data['id'];
		$row['plan_name'] = $data['plan_name'];
		$row['plan_duration'] = $data['plan_duration'];
		$row['plan_type'] = $data['plan_type'];
		$row['plan_price'] = $data['plan_price'];
		$row['plan_description'] = strip_tags(htmlspecialchars_decode($data['plan_description']));
		$row['Android'] = $data['playstore_id'];
		$row['IOS'] = $data['appstore_id'];
		$row['Type'] = $data['productCategory'];
		$row['status'] = $data['status'];
	


		array_push($jsonObj, $row);
	}

   $set['DRAW'] = $jsonObj;

	header('Content-Type: application/json; charset=utf-8');
	echo $val = str_replace('\\/', '/', json_encode($set, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
	die();
} 
else{
	$get_method = checkSignSalt($_POST['data']);
	$set['LIVETV'][] = array('msg' => 'Access denied OR Data not found', 'success' => '1');
	header('Content-Type: application/json; charset=utf-8');
	echo $val = str_replace('\\/', '/', json_encode($set, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
	die();
}
