Location: PHPKode > scripts > Noostr > _engine/includes/config.php
<?php defined('SYSPATH') OR die('No direct access to this file is allowed.');
/**
 * Loader for the Noostr application.
 * 
 * Everything gets setup from here, including classes, function libraries and
 * constants.
 *
 * @package Noostr
 */

/**
 * Include necessary libraries.
 */
include_once(SYSPATH.'/'.HANDLERS.'/error.php');
include_once(SYSPATH.'/'.INCLUDES.'/general.php');
include_once(SYSPATH.'/'.INCLUDES.'/stories.php');
include_once(SYSPATH.'/'.INCLUDES.'/template.php');
include_once(SYSPATH.'/'.INCLUDES.'/users.php');
include_once(SYSPATH.'/'.INCLUDES.'/version.php');
include_once(SYSPATH.'/'.CLASSES.'/ACL.php');
include_once(SYSPATH.'/'.CLASSES.'/Database.php');
include_once(SYSPATH.'/'.CLASSES.'/Noostr.php');
include_once(SYSPATH.'/'.CLASSES.'/Page.php');
include_once(SYSPATH.'/'.CLASSES.'/Site.php');
include_once(SYSPATH.'/'.CLASSES.'/Template.php');
include_once(SYSPATH.'/'.CLASSES.'/User.php');

/**
 * Setup the error and exception handlers.
 */
set_error_handler('error_handler');
set_exception_handler('exception_handler');

/**
 * Setup the default timezone.
 */
date_default_timezone_set('UTC');

/**
 * Initialize our two truly global variables.
 */
$logwritten = false;
$ns = new Noostr();

/**
 * Start the database
 */
$db = new Database($dbcon['server'], $dbcon['user'], $dbcon['pass'], $dbcon['database']);

/**
 * Set the initial url, path, protocol and port values for the site.
 */
define('URL', strtolower($_SERVER['SERVER_NAME']));
if (isset($_SERVER['REQUEST_URI'])) {
	if (strpos($_SERVER['REQUEST_URI'], '?') !== false) {
		define('PATH', substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], '?')));
	} else {
		define('PATH', $_SERVER['REQUEST_URI']);
	}
} else {
	define('PATH', '');
}
if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') {
	define('HTTP', 'https://');
} else {
	define('HTTP', 'http://');
}
if ((HTTP == 'http://' && $_SERVER["SERVER_PORT"] != 80) && (HTTP == 'https://' && $_SERVER["SERVER_PORT"] != 443)) {
	define('PORT', ':'.$_SERVER["SERVER_PORT"]);
} else {
	define('PORT', '');
}

/**
 * Create the Site object and populate it with data.
 */
$site = new Site($db->query('select * from '.PREFIX.'settings_site'));
$site->setquerystring($_SERVER['QUERY_STRING']);

/**
 * The "httpport" and "httpsport" values in the database should be blank in the
 * case of ports 80 and 443 or the HTML5 validator will fail.
 */
$port = '';
if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') {
	$port = $site->httpsport;
} else {
	$port = $site->httpport;
}

/**
 * Redirect if the generated URL is not the same as the URL constant. This only
 * occurs if "www" is missing from the domain and is required.
 */
if ($site->getdomain() != URL) {
	locate(HTTP.URL.$port.PATH);
}

/**
 * More constants that needed dynamic values from the "site" table.
 */
define('TEMPLATEFOLDER', SYSPATH.'/'.STYLE.'/'.$site->style);
define('TEMPLATEPATH', '/'.STYLE.'/'.$site->style);
define('TEMPLATEDEFAULTFOLDER', SYSPATH.'/'.STYLE.'/default');
define('TEMPLATEDEFAULTPATH', '/'.STYLE.'/default');

/**
 * Send headers to prevent caching, if requested by "site" table.
 */
if ($site->forcenobrowsercache) {
	header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
	header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
}

/**
 * Compare current port against expected port and redirect if necessary.
 */
if ($port != PORT && PORT != '') {
	locate(HTTP.URL.$port.PATH);
}

/**
 * Check for HTTPS requirements and redirect if necessary.
 */
if ($site->httpsrequired && (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) != 'on')) {
	locate('https://'.URL.$site->httpsport.PATH);
}
if (!$site->https && (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on')) {
	// "httpsrequired" overrides "https" preference, so don't redirect
	// if "httpsrequired" = true
	if (!$site->httpsrequired) {
		locate('http://'.URL.$site->httpport.PATH);
	}
}

/**
 * User loading time!
 */
$user = new User();
$content = '';
if (form_required(array('check1', 'check2', 'loginname', 'password'))) {
	// User is about to attempt a login from the login form
	if ($user->loadfromlogin(clean(form('loginname')), clean(form('password')), clean(form('check1')), clean(form("check2")))) {
		$user->loggedin = true;
		// Dump the anonymous session cookie if the login is good
		bake('anon', '', 0);
	} else {
		// TODO: user failed the login!
		form_preserve(array('loginname' => form('loginname'), 'signupname' => ''));
		$content = "We couldn't find your account! <a href=\"/login/\">Please try again.</a>";
		die($content);
	}
} elseif (cookie_required(array('check1', 'check2'))) {
	// User might already be logged in; let's check it out
	if ($user->loadfromhash($_COOKIE["check1"], $_COOKIE["check2"])) {
		$user->loggedin = true;
	} else {
		// TODO: cookies are missing or expired!
		$content = "Your login has expired. <a href=\"/login/\">Please login again.</a>";
		bake('check1', '', 0);
		bake('check2', '', 0);
		die($content);
	}
}

/**
 * If the user isn't logged in, we'll create an anonymous session cookie.
 */
if (!$user->loggedin) {
	if (cookie_required('anon')) {
		$user->loginhash = cookie('anon');
	} else {
		$user->loginhash = createuid(32);
		bake('anon', $user->loginhash, time() + $site->timeout_login);
	}
}

/**
 * Prepare the template system!
 */
$template = new Template();

/**
 * Time to load the page content!
 */
$page = new Page($template->get('page'));

/**
 * Load the ACL library!
 */
$acl = new ACL();

/**
 * If the user is supposed to be logged in for this page and isn't and the
 * current page isn't the login page, we need to redirect.
 */
if ($site->admin) {
	$page->roleid = $acl->admin;
}
if (($site->forcelogin || $site->admin || !$acl->canSee($page->roleid, $user->roleid)) && !$user->loggedin && strtolower(PATH) != '/login' && strtolower(PATH) != '/'.ADMIN.'/login') {
	$template->httpstatus = 403;
	if ($site->admin) {
		locate(HTTP.URL.PORT.'/login?loginreturn=admin/');
	} else {
		locate(HTTP.URL.PORT.'/login?loginreturn='.$template->get('page'));
	}
}

/**
 * If the user is logged in and is trying to see the admin section and isn't
 * allowed, make it fail.
 */
if ($site->admin && !$acl->canSee($page->roleid, $user->roleid)) {
	set_error('a01');
	locate(HTTP.URL.PORT.'/');
}

if ($content != '') {
	$page->set('url', $template->get('page'), 'description', $content);
}

/**
 * If we're visiting an admin section page, load the admin functions.
 */
if ($site->admin) {
	include_once(SYSPATH.'/'.INCLUDES.'/admin.php');
}

/**
 * Set a few final variables for the Site class, now that all the redirect
 * conditions are passed.
 */
$site->stylepath = HTTP.URL.PORT.TEMPLATEPATH;
$site->root = HTTP.URL.PORT;

include_once(SYSPATH.'/'.HANDLERS.'/forms.php');
include_once(SYSPATH.'/'.HANDLERS.'/moderation.php');
include_once(SYSPATH.'/'.HANDLERS.'/votes.php');
include_once(SYSPATH.'/'.HANDLERS.'/rss.php');

$error = errors(get_error());
$formfields = form_preserved();

//phpinfo();
Return current item: Noostr