<?php



// include all the necessary files
include_once filter_input(INPUT_SERVER, "DOCUMENT_ROOT") . "/includes.php";

// get the websites id we are working with
// get the full page url
$websitesid = get_websitesid();
$fullpage_url = filter_input(INPUT_GET, "fullpage");

// check if we have a page redirect
$pr = new cpages_redirect();
$redirectedPage = $pr->CheckPageRedirect($websitesid);

// load the main website data
// which will be used every where else in the loading process
$w = new cwebsites();
$w->LoadByID($websitesid);

// check if this site should be down due to non payment
if ($w->disable_nonpayment) {
    $sp = new csystem_pages();
    $sp->LoadBy_page_variable_load("non_payment");
    ?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <title><?php print $sp->title; ?></title>
        <meta charset="UTF-8">        
        <meta name="description" content="<?php print $sp->meta_description; ?>">
        <meta name="author" content="Boot Once">
    </head>
    <body>
        <?php print $sp->content; ?>
    </body>
</html>
    <?php
    
    exit();
    return;
}


// load up the static page
// get the pages_published
// ouput that page to the browser
$pp = new cpages_published();
$pp->LoadByURL($fullpage_url, $websitesid);

// get the pages information also
// load up other properties if necessary ex. login protected
$p = new cpages();
$p->LoadByID($pp->pagesid);

if ($pp->pagesid != 0 && $pp->websitesid != 0) {
    // check for ssl forced if so reload to the ssl domain
    if ($pp->ssl_forced) {        
        if (functions::is_ssl() == false) {
            $ssl_domain = filter_input(INPUT_SERVER, 'HTTP_HOST', FILTER_SANITIZE_STRING);
            if (isset($w->ssl_domain)) {
                $ssl_domain = $w->ssl_domain;
            }
            $redirect = 'https://' . $ssl_domain . filter_input(INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_STRING);
            header('HTTP/1.1 301 Moved Permanently');
            header('Location: ' . $redirect);
            exit();
        }
    }
    
    // check if the page is login protected
    if ($p->login_protected == 1 && !isset($_SESSION[$fullpage_url])) {
        // display a login page and/or sign up page
        header('Location: /login_protected/login_protected.php?fullpage=' . functions::get_input("fullpage"));
    }
}

// check to see if the url is a special url like sitemap etc.
if (strpos($fullpage_url, "sitemap.xml") !== FALSE) {
    include_once "sitemap.php";
    print_sitemap($websitesid);
    exit();
    return;
}

// check if robots.txt
if (strpos($fullpage_url, "robots.txt") !== FALSE) {
    include_once "robots.php";
    print_robots($websitesid);
    exit();
    return;
}

// ouput the html for the page
print $pp->html;    

