2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2008-01-09 05:18:36 +01:00
|
|
|
* Main file that handles every page request.
|
2008-02-25 03:10:37 +01:00
|
|
|
*
|
|
|
|
* The main.php does a number of set-up activities for the request.
|
|
|
|
*
|
2008-08-11 01:29:30 +02:00
|
|
|
* - Includes the first one of the following files that it finds: (root)/_ss_environment.php,
|
|
|
|
* (root)/../_ss_environment.php, or (root)/../../_ss_environment.php
|
2008-02-25 03:10:37 +01:00
|
|
|
* - Gets an up-to-date manifest from {@link ManifestBuilder}
|
|
|
|
* - Sets up error handlers with {@link Debug::loadErrorHandlers()}
|
2008-08-11 01:29:30 +02:00
|
|
|
* - Calls {@link DB::connect()}, passing it the global variable $databaseConfig that should
|
|
|
|
& be defined in an _config.php
|
2008-02-25 03:10:37 +01:00
|
|
|
* - Sets up the default director rules using {@link Director::addRules()}
|
|
|
|
*
|
2008-08-11 01:29:30 +02:00
|
|
|
* After that, it calls {@link Director::direct()}, which is responsible for doing most of the
|
|
|
|
* real work.
|
2008-02-25 03:10:37 +01:00
|
|
|
*
|
2008-08-11 01:29:30 +02:00
|
|
|
* Finally, main.php will use {@link Profiler} to show a profile if the querystring variable
|
|
|
|
* "debug_profile" is set.
|
2008-02-25 03:10:37 +01:00
|
|
|
*
|
|
|
|
* CONFIGURING THE WEBSERVER
|
|
|
|
*
|
2008-08-11 01:29:30 +02:00
|
|
|
* To use Sapphire, every request that doesn't point directly to a file should be rewritten to
|
|
|
|
* sapphire/main.php?url=(url). For example, http://www.example.com/about-us/rss would be rewritten
|
|
|
|
* to http://www.example.com/sapphire/main.php?url=about-us/rss
|
2008-02-25 03:10:37 +01:00
|
|
|
*
|
2008-08-11 01:29:30 +02:00
|
|
|
* It's important that requests that point directly to a file aren't rewritten; otherwise, visitors
|
|
|
|
* won't be able to download any CSS, JS, image files, or other downloads.
|
2008-02-25 03:10:37 +01:00
|
|
|
*
|
|
|
|
* On Apache, RewriteEngine can be used to do this.
|
|
|
|
*
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage core
|
|
|
|
* @see Director::direct()
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* Include _ss_environment.php file
|
|
|
|
*/
|
|
|
|
$envFiles = array('../_ss_environment.php', '../../_ss_environment.php', '../../../_ss_environment.php');
|
2008-08-11 01:29:30 +02:00
|
|
|
foreach ($envFiles as $envFile) {
|
|
|
|
if (@file_exists($envFile)) {
|
2008-02-25 03:10:37 +01:00
|
|
|
include($envFile);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Include Sapphire's core code
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
require_once("core/Core.php");
|
2008-02-25 03:10:37 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
header("Content-type: text/html; charset=\"utf-8\"");
|
2008-08-11 01:29:30 +02:00
|
|
|
if (function_exists('mb_http_output')) {
|
2007-07-19 12:40:28 +02:00
|
|
|
mb_http_output('UTF-8');
|
|
|
|
mb_internal_encoding('UTF-8');
|
|
|
|
}
|
|
|
|
|
2008-08-11 01:29:30 +02:00
|
|
|
if (get_magic_quotes_gpc()) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if($_REQUEST) stripslashes_recursively($_REQUEST);
|
|
|
|
if($_GET) stripslashes_recursively($_GET);
|
|
|
|
if($_POST) stripslashes_recursively($_POST);
|
|
|
|
}
|
2008-08-11 01:29:30 +02:00
|
|
|
if (isset($_REQUEST['trace'])) {
|
2007-07-19 12:40:28 +02:00
|
|
|
apd_set_pprof_trace();
|
|
|
|
}
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
// Ensure we have enough memory
|
|
|
|
$memString = ini_get("memory_limit");
|
2008-08-11 01:29:30 +02:00
|
|
|
switch(strtolower(substr($memString, -1))) {
|
|
|
|
case "k":
|
|
|
|
$memory = round(substr($memString, 0, -1)*1024);
|
|
|
|
break;
|
|
|
|
case "m":
|
|
|
|
$memory = round(substr($memString, 0, -1)*1024*1024);
|
|
|
|
break;
|
|
|
|
case "g":
|
|
|
|
$memory = round(substr($memString, 0, -1)*1024*1024*1024);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$memory = round($memString);
|
2008-02-25 03:10:37 +01:00
|
|
|
}
|
2008-08-11 01:29:30 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
// Check we have at least 32M
|
2008-08-11 01:29:30 +02:00
|
|
|
if ($memory < (32 * 1024 * 1024)) {
|
2008-02-25 03:10:37 +01:00
|
|
|
// Increase memory limit
|
|
|
|
ini_set('memory_limit', '32M');
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
require_once("core/ManifestBuilder.php");
|
|
|
|
require_once("core/ClassInfo.php");
|
|
|
|
require_once('core/Object.php');
|
|
|
|
require_once('core/control/Director.php');
|
|
|
|
require_once('filesystem/Filesystem.php');
|
|
|
|
require_once("core/Session.php");
|
|
|
|
|
|
|
|
// If this is a dev site, enable php error reporting
|
2008-08-11 01:29:30 +02:00
|
|
|
if (Director::isDev()) {
|
2007-07-19 12:40:28 +02:00
|
|
|
error_reporting(E_ALL);
|
|
|
|
}
|
|
|
|
|
|
|
|
Session::start();
|
|
|
|
|
2008-08-11 01:29:30 +02:00
|
|
|
if (isset($_GET['url'])) {
|
2007-10-02 06:59:06 +02:00
|
|
|
$url = $_GET['url'];
|
|
|
|
|
|
|
|
// Lighttpd uses this
|
|
|
|
} else {
|
|
|
|
list($url, $query) = explode('?', $_SERVER['REQUEST_URI'], 2);
|
|
|
|
parse_str($query, $_GET);
|
2008-08-11 01:29:30 +02:00
|
|
|
if ($_GET) $_REQUEST = array_merge((array)$_REQUEST, (array)$_GET);
|
2007-10-02 06:59:06 +02:00
|
|
|
}
|
|
|
|
|
2008-08-11 01:29:30 +02:00
|
|
|
if (ManifestBuilder::staleManifest()) {
|
2007-07-19 12:40:28 +02:00
|
|
|
ManifestBuilder::compileManifest();
|
|
|
|
}
|
|
|
|
|
|
|
|
require_once(MANIFEST_FILE);
|
|
|
|
|
2008-08-11 01:29:30 +02:00
|
|
|
if (isset($_GET['debugmanifest'])) Debug::show(file_get_contents(MANIFEST_FILE));
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2008-08-11 01:29:30 +02:00
|
|
|
if (isset($_GET['debug_profile'])) Profiler::init();
|
|
|
|
if (isset($_GET['debug_profile'])) Profiler::mark('all_execution');
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2008-08-11 01:29:30 +02:00
|
|
|
if (isset($_GET['debug_profile'])) Profiler::mark('main.php init');
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
// Load error handlers
|
|
|
|
Debug::loadErrorHandlers();
|
|
|
|
|
|
|
|
// Connect to database
|
|
|
|
require_once("core/model/DB.php");
|
|
|
|
|
2008-08-11 01:29:30 +02:00
|
|
|
if (isset($_GET['debug_profile'])) Profiler::mark('DB::connect');
|
2007-07-19 12:40:28 +02:00
|
|
|
DB::connect($databaseConfig);
|
2008-08-11 01:29:30 +02:00
|
|
|
if (isset($_GET['debug_profile'])) Profiler::unmark('DB::connect');
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
// Get the request URL
|
|
|
|
$baseURL = dirname(dirname($_SERVER['SCRIPT_NAME']));
|
|
|
|
|
2007-10-02 06:59:06 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2008-08-11 01:29:30 +02:00
|
|
|
if (substr($url, 0, strlen($baseURL)) == $baseURL) $url = substr($url, strlen($baseURL));
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
// Direct away - this is the "main" function, that hands control to the appropriate controller
|
2008-08-11 01:29:30 +02:00
|
|
|
if (isset($_GET['debug_profile'])) Profiler::unmark('main.php init');
|
2007-07-19 12:40:28 +02:00
|
|
|
Director::direct($url);
|
|
|
|
|
2008-08-11 01:29:30 +02:00
|
|
|
if (isset($_GET['debug_profile'])) {
|
2007-07-19 12:40:28 +02:00
|
|
|
Profiler::unmark('all_execution');
|
|
|
|
Profiler::show(isset($_GET['profile_trace']));
|
|
|
|
}
|