File: D:/HostingSpaces/SBogers64/klimroosbudel.nl/wwwroot/kms/lib/bootstrap.class.php
<?php
/**
* bootstrap.class.php
* Created by Komma Mediadesign.
* Author: mike
* Date: 3/19/13
*/
class Bootstrap extends Controller
{
public function __construct()
{
// Start the session
Session::init();
// Convert URL_PAGE & URL_SUB to a controller and a method within a controller
$LanguageHandler = new LanguageHandler();
$LanguageHandler->set(URL_LANG);
// Define controller & method
defined('URL_PAGE') ? $controller = $LanguageHandler->convert(URL_PAGE) : $controller = 'Login';
defined('URL_SUB') ? $method = $LanguageHandler->convert(URL_SUB) : $method = '';
$controller = str_replace('-', '_', $controller);
//Check whether a user is logged in
$Login = new Login();
$Login->loadModel('login');
if ($Login->Model->isLoggedIn()) {
// If URL_PAGE is not defined, call the homepage controller.
if (! defined('URL_PAGE')) {
require_once DOCUMENT_ROOT.'app/controllers/c_home.class.php';
$Controller = new Home();
$Controller->init();
$Controller->index();
return false;
}
} else {
$limitAllowed = ['login', 'forgot_pass', 'pass_activation', 'pass_updater'];
if (! in_array($controller, $limitAllowed)) {
$controller = 'Login';
$method = 'index';
}
}
// Load the page
$this->loadPage($controller, $method);
return false;
}
private function loadPage($name, $method)
{
$file = DOCUMENT_ROOT.'app/controllers/c_'.strtolower($name).'.class.php';
//Check if the controller file exists. If so, require the file and create a new Controller object
$basicPage = $this->isBasicPage($name);
if ($basicPage) {
require_once DOCUMENT_ROOT.'app/controllers/c_basic_page.class.php';
} elseif (is_file($file)) {
if (! class_exists($name)) {
require_once $file;
}
} else {
header('HTTP/1.0 404 Not Found');
header('location:'.SITE_ROOT.'404/');
exit;
}
// Catch 404
if ($name == '404') {
$name = 'Page_Not_Found';
}
// PAGE -> Create a controller object and load the model
if ($basicPage) {
$Controller = new Basic_Page($name);
$Controller->loadModel('basic_page');
} else {
$Controller = new $name;
$Controller->loadModel($name);
}
$Controller->init();
// SUB -> If SUB exists as a method, call that method. If no sub,
if (! empty($method)) {
if (method_exists($Controller, $method)) {
$Controller->{$method}();
return true;
}
} else {
if (method_exists($Controller, 'index')) {
$Controller->index();
return true;
}
}
$Controller->Functions->notFound();
return false;
}
/*
* This functions checks whether the constructor should load a specific controller for a page,
* or whether he should load the Basic_Page controller.
*/
private function isBasicPage($name)
{
$Dbh = new DatabaseHandler();
$Dbh->setTableName('kms_basic_pages');
$Dbh->addRule('name', $name);
$result = $Dbh->select();
return ! empty($result);
}
}