<?php
include "config.php";
header('Content-Type: application/json');
$valid_passwords = array("LifeLCT" => "LifLinCnslt");
$valid_users = array_keys($valid_passwords);

$user = $_SERVER['PHP_AUTH_USER'];
$pass = $_SERVER['PHP_AUTH_PW'];

$validated = (in_array($user, $valid_users)) && ($pass == $valid_passwords[$user]);

if (!$validated) {
    header('WWW-Authenticate: Basic realm="My Realm"');
    header('HTTP/1.0 401 Unauthorized');
    $responsefull = array('error_message' => 'Unathorized: please provide a valid password!', 'data' => []);
} else {


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

    $news_events = array();
    $doctor_timing = array();

        $query_menu = "SELECT ps.id as post_id, ps.post_title as post_title, tm.name as name, tm.term_id as term_id, (SELECT guid FROM wp_posts WHERE id = mt.meta_value) AS image
        FROM
        wp_posts ps, wp_postmeta mt, wp_term_relationships tr, wp_terms tm
        WHERE
        ps.id = tr.object_id
        AND tm.term_id = tr.term_taxonomy_id
        AND tm.term_id = 2
        AND ps.post_status = 'publish'
        AND ps.post_type = 'post'
        AND mt.post_id = ps.id
        AND mt.meta_key = '_thumbnail_id'
        ORDER BY ps.post_date DESC";
	    $res_menu = mysqli_query($con, $query_menu);
		while ($row_menu = mysqli_fetch_array($res_menu)) {
            //$post_content = str_replace("\n"," ",$row_menu['post_content']);
            // $post_content2 = str_replace("<!-- wp:html -->"," ",$post_content);
            // $post_content3 = str_replace(" <!-- /wp:html -->"," ",$post_content2);

            $news_date = "SELECT DATE_FORMAT(`meta_value`,'%d-%m-%Y') as news_date FROM `wp_postmeta` WHERE `meta_key`='news_date' and `post_id`='$row_menu[post_id]'";
            $res_news_date = mysqli_query($con, $news_date);
            $rownews_date = mysqli_fetch_array($res_news_date);
            $newsdate = $rownews_date['news_date'];

            $short_desc = "SELECT `meta_value` FROM `wp_postmeta` WHERE `meta_key`='short_description' and `post_id`='$row_menu[post_id]'";
            $res_short_desc = mysqli_query($con, $short_desc);
            $rowshort_desc = mysqli_fetch_array($res_short_desc);
            $short_description = $rowshort_desc['meta_value'];

            $news_events[] = array(
            'id' => $row_menu['post_id'],
            'category' => $row_menu['name'],
            'cat_id' => $row_menu['term_id'],
            'title' => html_entity_decode(htmlentities($row_menu['post_title'])),
            'news_date' => $newsdate,
            'news_img' => $row_menu['image'],
            'short_desc' => html_entity_decode(htmlentities($short_description))
            );
		}



		$data = array("news_events" => $news_events );

$response = array("status" => "true","message" => "Success", "data" => $data);


}
if(isset($response))
{
	echo json_encode($response, JSON_UNESCAPED_SLASHES);
}
?>