<?php
if ($access != 1) {
    exit("Direct Access not allowed");
}
require_once("db.php");
if (isset($_GET["page"])) {
    $page = $_GET["page"];
}

//Content
if (file_exists(PAGES . $page . ".php")) {
    $content = PAGES . $page . ".php";
} else {
    $content = HEAD . "default.php";
}

//Sidebar
if (file_exists(SIDEBAR . $page . ".php")) {
    $sidebar = SIDEBAR . $page . ".php";
} else {
    $sidebar = SIDEBAR . "default.php";
}

//Widget
function widget($name)
{
    if (file_exists(WIDGETS . $name . ".php")) {
        include WIDGETS . $name . ".php";
    } else {
        echo "WIGET NOT FOUND";
    }
}

//Plugins 
function plugins($name)
{
    if (file_exists(PLUGINS . $name . DS . "index.php")) {
        include PLUGINS . $name . DS . "index.php";
    } else {
        echo "PLUGIN NOT FOUND";
    }
}

//Querys    
if (file_exists(QUERYS . $page . ".php")) {
    $querys = QUERYS . $page . ".php";
} else {
    $querys = QUERYS . "default.php";
}

//Currency    
if (file_exists(WIDGETS . "currencychange.php")) {
    $currency = WIDGETS . "currencychange.php";
}

//Header    
if (file_exists(HEAD . $page . ".php")) {
    $head = HEAD . $page . ".php";
} else {
    $head = HEAD . "default.php";
}

//Footer
if (file_exists(FOOTER . $page . ".php")) {
    $footer = FOOTER . $page . ".php";
} else {
    $footer = FOOTER . "default.php";
}


//Layout Pages

//Pages Data
$pageslist = mysqli_query($conn, "SELECT page_link FROM page_settings ");
$pagestotal = mysqli_num_rows($pageslist);

if (isset($_GET["page"])) {
    $page = $_GET["page"];

$type = "notfound";
    
for($i = 0; $i < $pagestotal; $i++) {
  $row = mysqli_fetch_array($pageslist);
  
$casename = str_replace('.php', '', $row[0]);  
   switch ($page) {
        case $casename:
            $type = $row[0];
            break;
    }
}

if($type == 'notfound') {
  header("Location: ".SITE."");
} else {
    $theme = ABSPATH . "template" . DS . THEME_NAME . DS . $type;
}
    include $theme;
} else {
    echo "PAGE NOT FOUND";
    exit();
}
?>