<?php 
/* DB connection */
require_once('mfit-admin/config.php');
countVisitor();

//**************************************//
//@ Route
//**************************************//
$tt_page_name = null;
if ($section_0 == '' || $section_0 == 'home') {
  // home page
  $tt_page_name = 'homepage';
  require_once('includes/home.php');
  exit();
}

// Single Page
elseif ($section_0 == getSingleValue('tt_page', 'page_url',  $section_0, 'page_url') && getSingleValue('tt_page', 'page_url',  $section_0, 'page_status') == 'published') {

  $the_page = showSingleItem("tt_page", "WHERE  page_url ='$section_0' AND page_status = 'published' ");
  require_once('includes/single-page.php');
  exit();

}

// Single Converter
elseif ( $section_0 == 'bangla-converter') {

  require_once('includes/single-converter.php');
  exit();
}

// Reporters
elseif ( $section_0 == 'reporters') {

  require_once('includes/single-reporters.php');
  exit();
}


// Register Page
elseif ($section_0 == 'register' && $advancenews['ttWriterAccountSystem'] == 'on') {


  if ($writer_login == false) {

    // User Not login, Show Register Page
    require_once('includes/register.php');
    exit();

  }else {
    // If User Not Login First need to Login
    header('Location:'.$site_domain.'account/');
    exit();
  }
}


elseif ($section_0 == 'photo-stories') {

  if ($section_1 !='') {
    $tt_single_story = showAllttdata("mahamudfutureit_photostories", "WHERE story_status = 'Published' and  story_url = '$section_1' LIMIT 1");
    if (!empty($tt_single_story)) {

      $the_story = ttSingleData($tt_single_story);
      if (isPublished($the_story['story_date'])) {

        $storyData =  json_decode( $the_story['story_items'] ,true);

        $post_form_data = array(
          "story_view"      => $the_story['story_view'] + 1,
        );
         // Count View
         UpdateData('mahamudfutureit_photostories', 'id', $the_story['id'], $post_form_data);


        // Single Project page
        require_once('includes/photo-stories.php');
        exit();
      }else{
        // 404 Page
        require_once 'includes/404.php';
        exit();
      }

    }else {
      // 404 Page
      require_once 'includes/404.php';
      exit();
    }
  }else {
    // Story Archive
    require_once('includes/single-story.php');
    exit();
  }
}

// Login Page
elseif ($section_0 == 'login' && $advancenews['ttWriterAccountSystem'] == 'on') {

  if ($writer_login == false) {

    // User Not login, Show Login Page
    require_once('includes/login.php');
    exit();
  }else {
    // If User Not Login First need to Login
    header('Location:'.$site_domain.'account/');
    exit();
  }

}

elseif ($section_0 == 'account' && $advancenews['ttWriterAccountSystem'] == 'on') {

  // Logout Access
  if ($section_1 == 'logout') {
    // Logout Account
    setcookie('lsp_cpa', '', time() - 3600, '/');
    header("Location:".$site_domain);
    exit();
  }

  // If User Login Try Account Dashboard
  if ($writer_login == true) {

    $post_author_id = $the_writer['UserID']; // Author ID

    // Check Verification Status
    if ($the_writer['FirstVerificationStatus'] == 'Unverified') {

      // Account Not Verified, Lets Verify
      require_once('includes/account-otp_verify.php');
      exit();
    }else {
      // Account page
      if ($section_1 == '') {

        // User login, Show Account Page
        require_once('includes/account.php');
        exit();
      }
      elseif ($section_1 == 'personal-info') {

        // Edit Personal Information
        require_once('includes/account-personal_information.php');
        exit();
      }
      elseif ($section_1 == 'security') {

        // Edit Personal Information
        require_once('includes/account-change_password.php');
        exit();
      }
      elseif ($section_1 == 'address') {

        // Edit Personal Information
        require_once('includes/account-address.php');
        exit();
      }
      elseif ($section_1 == 'article' && $the_writer['PostAbility'] == 'yes') {


        if ($section_2 == 'new') {
          // New Post
          require_once('includes/account-post-new.php');
          exit();

        }elseif ($section_2 == 'published') {

          // Published Post
          $tt_author_article_post = showAllttdata("mahamudfutureit_article", "WHERE PostAuthor = '$post_author_id' AND PostStatus = 'published' ORDER BY  PostDate  DESC ");
          $tauthor_page_title = 'প্রকাশিত লেখা সমূহ';
          $tauthor_page_icon = 'hand-with-pen.png';
          require_once('includes/account-post-all.php');
          exit();

        }elseif ($section_2 == 'pending') {

          // Pending Post
          $tt_author_article_post = showAllttdata("mahamudfutureit_article", "WHERE PostAuthor = '$post_author_id' AND PostStatus = 'pending' ORDER BY  PostDate  DESC ");
          $tauthor_page_title = 'অপেক্ষামান লেখা সমূহ';
          $tauthor_page_icon = 'review-post.png';
          require_once('includes/account-post-all.php');
          exit();

        }elseif ($section_2 == 'rejected') {

          // Rejected Post
          $tt_author_article_post = showAllttdata("mahamudfutureit_article", "WHERE PostAuthor = '$post_author_id' AND PostStatus = 'rejected' ORDER BY  PostDate  DESC ");
          $tauthor_page_title = 'বাতিলকৃত লেখা সমূহ';
          $tauthor_page_icon = 'trash.png';
          require_once('includes/account-post-all.php');
          exit();

        }elseif ($section_2 == 'edit' && $section_3 != '' &&  getSingleValue('mahamudfutureit_article', 'PostId',  $section_3, 'PostAuthor') == $post_author_id) {

          // Edit Post
          $edit_post = isAuthArticles($section_3 , 'id');
          require_once('includes/account-post-edit.php');
          exit();

        }else {
          // 404 Page
          require_once 'includes/404.php';
          exit();
        }

      }
      else {
        // 404 Page
        require_once 'includes/404.php';
        exit();
      }
    }

  }else {
    // If User Not Login First need to Login
    header('Location:'.$site_domain.'login');
    exit();
  }
}




// Ajax Request
elseif ($section_0 == 'ajax_master') {
  // Ajax Req
  require_once 'ajax_master_oparetion.php';
  exit();
}

// Single Post By pl_one
elseif ($section_0 == $advancenews["Post_Parmalink_text"] && $section_1 !='' && $section_1 == isArticles($section_1 , 'id', true) && $section_2 == isArticles($section_2 , 'url', true)) {
  // Single Post
  $the_post = isArticles($section_1 , 'id');
  require_once 'includes/single-post.php';
  exit();
}


// Single Post By pl_two
elseif ($section_0 == $advancenews["Post_Parmalink_text"] && $section_1 !='' && $section_1 == isArticles($section_1 , 'id', true) && $section_2 == '') {
  // Single Post
  $the_post = isArticles($section_1 , 'id');
  require_once 'includes/single-post.php';
  exit();
}

// Single Post By pl_three
elseif ($section_0 == $advancenews["Post_Parmalink_text"] && $section_1 !='' && $section_1 == isArticles($section_1 , 'url', true) && $section_2 == '') {
  // Single Post
  $the_post = isArticles($section_1 , 'url');
  require_once 'includes/single-post.php';
  exit();
}

// Single Post By pl_four
elseif (getSingleValue('mahamudfutureit_category', 'CategoryURL', $section_0, 'CategoryID') != '' && $section_1 !='' && $section_1 == isArticles($section_1 , 'url', true)) {
  $the_article = isArticles($section_1 , 'url');
  $this_category_id = getSingleValue('mahamudfutureit_category', 'CategoryURL', $section_0, 'CategoryID');
  $the_article_category_id = $the_article['PostCategory'];

  if ($this_category_id == $the_article_category_id) {
    // Single Post
    $the_post = isArticles($section_1 , 'url');
    require_once 'includes/single-post.php';
    exit();
  }else {
    // 404 Page
    require_once 'includes/404.php';
    exit();
  }
}

// Video
elseif ($section_0 == 'video' && $section_1 !='' && $section_1 == isVideo($section_1 , true) && $section_2 == '') {
  // Single Video
  $the_video = isVideo($section_1);
  require_once 'includes/single-video.php';
  exit();
}elseif ($section_0 == 'video' && $section_1 =='') {
  require_once 'includes/single-video-archive.php';
  exit();
}

// Photo Gallery
elseif ($section_0 =='photos') {
  if ($section_1 !='') {
    if (isGallery($section_1, 'url', true)) {
      $the_gallery = isGallery($section_1, 'url', false);
      require_once 'includes/single-gallery.php';
      exit();
    }else{
      // 404 Page
      require_once 'includes/404.php';
      exit();
    }
  }else {
    // Gallery Page
    require_once 'includes/single-gallery-archive.php';
    exit();
  }
}

// Poll
elseif ($section_0 =='opinion-poll') {

    if (isset($section_1) && isPoll($section_1, true)) {
      $my_poll_status = true;
      $my_poll = isPoll($section_1);
      $mypoll_title = $my_poll['PollTitle'];
      $mypoll_image = getPollImage($my_poll['PollID']);
      $mypoll_pagelink = 'opinion-poll/'.$section_1.'/';

    }else {
      $my_poll_status = false;
      $mypoll_title = 'অনলাইন জরিপ';
      $mypoll_image = $advancenews['Poll_DF_Img'];
      $mypoll_pagelink = 'opinion-poll/';
    }

    require_once 'includes/single-poll.php';
    exit();

}

// Post Archive
elseif ($section_0 =='archive') {

  if ($section_1 != '') {
      $d = DateTime::createFromFormat('Y-m-d', $section_1);
      // Check if parsing succeeded and the date matches exactly
      if ($d && $d->format('Y-m-d') === $section_1) {
          $the_post_date = $section_1;
      } else {
          // Invalid date format or invalid date
          $the_post_date = date('Y-m-d');
      }
  } else {
      $the_post_date = date('Y-m-d');
  }

  // Archive Page
  require_once 'includes/single-archive.php';
  exit();
}

// Author Page
elseif ($section_0 == $advancenews["ttWriterAccountProfileParmalink"] && $advancenews["ttWriterAccountProfile"] == 'on' && $section_1 !='' && isUser($section_1) != false) {
  $the_user = isUser($section_1);
  require_once 'includes/single-user.php';
  exit();
}

// Photocard
elseif (($c_user !='' || $advancenews['PhotoCardPublic'] == 'on') && $section_0 == 'mahamudfutureit-photocard' && $section_1 != '' && $section_1 == isArticles($section_1 , 'id', true) ) {
  // Photocard
  $the_post = isArticles($section_1 , 'id');
  require_once 'includes/post-photocard.php';
  exit();
}

// Print View
elseif ($advancenews['NewsPrintView'] == 'on' && $section_0 == 'print-post' && $section_1 != '' && $section_1 == isArticles($section_1 , 'id', true) ) {
  // Photocard
  $the_post = isArticles($section_1 , 'id');
  require_once 'includes/post-printview.php';
  exit();
}


// Category Page
elseif (isCategory($section_0, 'url', true )) {
  // Single Category
  $the_category = isCategory($section_0 , 'url');
  require_once 'includes/single-category.php';
  exit();
}

// Topic Page
elseif ( $section_0=='topic' &&  $section_1 !='') {
  $this_topic = urldecode($section_1);
  require_once 'includes/single-topic.php';
  exit();
}

// Search Page
elseif ( $section_0=='search') {
  $search = isset($_GET['q']) ? trim($_GET['q']) : '';
  require_once 'includes/single-search.php';
  exit();
}

// Search Page
elseif ( $section_0=='country') {

  // Upazila Finder
  if (isset($section_1) && isset($section_2) && isset($section_3)) {
    $upazila_id = getSingleValue('mahamudfutureit_loc_upazilas', 'slag', $section_3 , 'id');
    $district_id = getSingleValue('mahamudfutureit_loc_districts', 'slag', $section_2 , 'id');
    $division_id = getSingleValue('mahamudfutureit_loc_divisions', 'slag', $section_1, 'id');
    $the_upazila = showSingleItem("mahamudfutureit_loc_upazilas", "WHERE district_id = '$district_id' AND division_id = '$division_id' AND slag = '$section_3' LIMIT 1");

    if (!empty($the_upazila)) {
      // Global Data
      $tt_lpage_title = $the_upazila['bn_name']. " উপজেলার সর্বশেষ খবর";
      $tt_lpage_url = $site_domain."country/".$section_1."/".$section_2."/".$section_3."/";
      $tt_lpage_type = "upazila";
      $tt_lpage_kid = $upazila_id;

      require_once 'includes/single-location.php';
      exit();
    }else {
      // 404 Page
      require_once 'includes/404.php';
      exit();
    }
  }

  // Zila Finder
  elseif(isset($section_1) && isset($section_2) && !isset($section_3)) {
    $district_id = getSingleValue('mahamudfutureit_loc_districts', 'slag', $section_2 , 'id');
    $division_id = getSingleValue('mahamudfutureit_loc_divisions', 'slag', $section_1, 'id');
    $the_district = showSingleItem("mahamudfutureit_loc_districts", "WHERE division_id = '$division_id' AND slag = '$section_2' LIMIT 1");

    if (!empty($the_district)) {
      // Global Data
      $tt_lpage_title = $the_district['bn_name']. " জেলার সর্বশেষ খবর";
      $tt_lpage_url = $site_domain."country/".$section_1."/".$section_2."/";
      $tt_lpage_subitems = showAllttdata("mahamudfutureit_loc_upazilas", "WHERE district_id = '$district_id'");
      $tt_lpage_type = "district";
      $tt_lpage_kid = $district_id;

      require_once 'includes/single-location.php';
      exit();
    }else {
      // 404 Page
      require_once 'includes/404.php';
      exit();
    }

  }

  // Division Finder
  elseif(isset($section_1) && !isset($section_2) && !isset($section_3)) {
    $division_id = getSingleValue('mahamudfutureit_loc_divisions', 'slag', $section_1, 'id');
    $the_division = showSingleItem("mahamudfutureit_loc_divisions", "WHERE slag = '$section_1' LIMIT 1");

    if (!empty($the_division)) {
      // Global Data
      $tt_lpage_title = $the_division['bn_name']. " বিভাগের সর্বশেষ খবর";
      $tt_lpage_url = $site_domain."country/".$section_1."/";
      $tt_lpage_subitems = showAllttdata("mahamudfutureit_loc_districts", "WHERE division_id = '$division_id'");
      $tt_lpage_type = "division";
      $tt_lpage_kid = $division_id;

      require_once 'includes/single-location.php';
      exit();
    }else {
      // 404 Page
      require_once 'includes/404.php';
      exit();
    }
  }
  // 404 Page
  require_once 'includes/404.php';
  exit();
}

// Sitemap
elseif ($section_0=='sitemaps') {
  // Sitemap
  require_once 'sitemap-advancenews.php';
  exit();

}

// Rss Feed
elseif ($section_0=='rss') {
  if (isset($section_1) && $section_1 =='rss.xml') {
    // RSS Feed
    require_once 'rss-advancenews.php';
    exit();
  }else {
    // 404 Page
    require_once 'includes/404.php';
    exit();
  }
}
// Live TV Page
elseif ($section_0 == 'livetv') {
  require_once('includes/single-livetv.php');
  exit();
}



// Rss Feed
elseif ($section_0=='api') {
  if (isset($section_1) && $section_1 =='allpost') {

    $get_post_data = getArticles(null, 50);

    // Set header to tell browser it's JSON
    header('Content-Type: application/json');

    // Output JSON-encoded data
    echo json_encode($get_post_data, true);
    exit();

  }elseif($section_1 =='single-post'){

      // Single Post
      $the_post = isArticles($section_2 , 'id');
    // Set header to tell browser it's JSON
    header('Content-Type: application/json');

    // Output JSON-encoded data
    echo json_encode($the_post, true);
      exit();

  }
  else {
    // 404 Page
    require_once 'includes/404.php';
    exit();
  }
}

// 404 Page
else {
  // 404 Page
  require_once 'includes/404.php';
  exit();
}

?>
