diff --git a/cli-script.php b/cli-script.php index 07bcad2d0..70cbdbf02 100755 --- a/cli-script.php +++ b/cli-script.php @@ -1,11 +1,6 @@ #!/usr/bin/php5 diff --git a/core/Core.php b/core/Core.php index 2d5c73ad3..8df6f116b 100755 --- a/core/Core.php +++ b/core/Core.php @@ -1,11 +1,91 @@ 'HTTP/1.1', + 'HTTP_ACCEPT' => 'text/plain;q=0.5', + 'HTTP_ACCEPT_LANGUAGE' => '*;q=0.5', + 'HTTP_ACCEPT_ENCODING' => '', + 'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1;q=0.5', + 'SERVER_SIGNATURE' => 'Command-line PHP/' . phpversion(), + 'SERVER_SOFTWARE' => 'PHP/' . phpversion(), + 'SERVER_ADDR' => '127.0.0.1', + 'REMOTE_ADDR' => '127.0.0.1', + 'REQUEST_METHOD' => 'GET', + ); + + $_SERVER = array_merge($serverDefaults, $_SERVER); + +/** + * If we have an HTTP_HOST value, then we're being called from the webserver and there are some things that + * need checking + */ +} else { + /** + * Fix magic quotes setting + */ + if (get_magic_quotes_gpc()) { + if($_REQUEST) stripslashes_recursively($_REQUEST); + if($_GET) stripslashes_recursively($_GET); + if($_POST) stripslashes_recursively($_POST); + } +} + /** * Define system paths */ @@ -38,6 +118,69 @@ define('PR_HIGH',100); define('PR_MEDIUM',50); define('PR_LOW',10); +/** + * Ensure we have enough memory + */ +$memString = ini_get("memory_limit"); +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); +} + +// Check we have at least 32M +if ($memory < (32 * 1024 * 1024)) { + // Increase memory limit + ini_set('memory_limit', '32M'); +} + +/////////////////////////////////////////////////////////////////////////////// +// INCLUDES + +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"); + +/////////////////////////////////////////////////////////////////////////////// +// MANIFEST + +/** + * Build the manifest + */ +if(ManifestBuilder::staleManifest()){ + ManifestBuilder::compileManifest(); +} + +require_once(MANIFEST_FILE); + +/** + * ?debugmanifest=1 hook + */ +if(isset($_GET['debugmanifest'])) Debug::show(file_get_contents(MANIFEST_FILE)); + + +/////////////////////////////////////////////////////////////////////////////// +// POST-MANIFEST COMMANDS + +/** + * Load error handlers + */ +Debug::loadErrorHandlers(); + +/////////////////////////////////////////////////////////////////////////////// +// HELPER FUNCTIONS + /** * Returns the temporary folder that sapphire/silverstripe should use for its cache files * This is loaded into the TEMP_FOLDER define on start up diff --git a/core/control/Director.php b/core/control/Director.php index 4b7dece0e..0f17e8018 100644 --- a/core/control/Director.php +++ b/core/control/Director.php @@ -276,7 +276,8 @@ class Director { if(substr($url,0,4) != "http") { if($url[0] != "/") $url = Director::baseURL() . $url; - $url = self::protocolAndHost() . $url; + // Sometimes baseURL() can return a full URL instead of just a path + if(substr($url,0,4) != "http") $url = self::protocolAndHost() . $url; } return $url; @@ -289,7 +290,7 @@ class Director { */ static function protocolAndHost() { if(self::$alternateBaseURL) { - if(preg_match('/^(http[^:]*:\/\/[^\/]+)\//', self::$alternateBaseURL, $matches)) { + if(preg_match('/^(http[^:]*:\/\/[^\/]+)(\/|$)/', self::$alternateBaseURL, $matches)) { return $matches[1]; } } diff --git a/main.php b/main.php index d866320ad..adb7f78a4 100644 --- a/main.php +++ b/main.php @@ -35,16 +35,6 @@ * @see Director::direct() */ -/** - * Include _ss_environment.php file - */ -$envFiles = array('../_ss_environment.php', '../../_ss_environment.php', '../../../_ss_environment.php'); -foreach ($envFiles as $envFile) { - if (@file_exists($envFile)) { - include($envFile); - break; - } -} /** * Include Sapphire's core code @@ -57,44 +47,6 @@ if (function_exists('mb_http_output')) { mb_internal_encoding('UTF-8'); } -if (get_magic_quotes_gpc()) { - if($_REQUEST) stripslashes_recursively($_REQUEST); - if($_GET) stripslashes_recursively($_GET); - if($_POST) stripslashes_recursively($_POST); -} -if (isset($_REQUEST['trace'])) { - apd_set_pprof_trace(); -} - -// Ensure we have enough memory -$memString = ini_get("memory_limit"); -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); -} - -// Check we have at least 32M -if ($memory < (32 * 1024 * 1024)) { - // Increase memory limit - ini_set('memory_limit', '32M'); -} - - -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 // This is necessary to force developers to acknowledge and fix // notice level errors (you can override this directive in your _config.php) @@ -104,6 +56,7 @@ if (Director::isDev()) { Session::start(); +// Apache rewrite rules use this if (isset($_GET['url'])) { $url = $_GET['url']; @@ -114,23 +67,16 @@ if (isset($_GET['url'])) { if ($_GET) $_REQUEST = array_merge((array)$_REQUEST, (array)$_GET); } -require_once("core/ManifestBuilder.php"); -if (ManifestBuilder::staleManifest()) { - ManifestBuilder::compileManifest(); +// Fix glitches in URL generation +if (substr($url, 0, strlen(BASE_URL)) == BASE_URL) $url = substr($url, strlen(BASE_URL)); + + +if (isset($_GET['debug_profile'])) { + Profiler::init(); + Profiler::mark('all_execution'); + Profiler::mark('main.php init'); } -require_once(MANIFEST_FILE); - -if (isset($_GET['debugmanifest'])) Debug::show(file_get_contents(MANIFEST_FILE)); - -if (isset($_GET['debug_profile'])) Profiler::init(); -if (isset($_GET['debug_profile'])) Profiler::mark('all_execution'); - -if (isset($_GET['debug_profile'])) Profiler::mark('main.php init'); - -// Load error handlers -Debug::loadErrorHandlers(); - // Connect to database require_once("core/model/DB.php"); @@ -138,12 +84,9 @@ if (isset($_GET['debug_profile'])) Profiler::mark('DB::connect'); DB::connect($databaseConfig); if (isset($_GET['debug_profile'])) Profiler::unmark('DB::connect'); - -// Get the request URL -if (substr($url, 0, strlen(BASE_URL)) == BASE_URL) $url = substr($url, strlen(BASE_URL)); +if (isset($_GET['debug_profile'])) Profiler::unmark('main.php init'); // Direct away - this is the "main" function, that hands control to the appropriate controller -if (isset($_GET['debug_profile'])) Profiler::unmark('main.php init'); Director::direct($url); if (isset($_GET['debug_profile'])) { diff --git a/tests/control/DirectorTest.php b/tests/control/DirectorTest.php index 7202726df..daaea2fa0 100644 --- a/tests/control/DirectorTest.php +++ b/tests/control/DirectorTest.php @@ -26,25 +26,25 @@ class DirectorTest extends SapphireTest { unlink($tempFilePath); } - /* + public function testAlternativeBaseURL() { - // relative base URLs - Director::setBaseURL('/relativebase'); - $this->assertEquals(Director::baseURL(), '/relativebase'); - $this->assertEquals(Director::absoluteBaseURL(), BASE_URL . '/relativebase'); - $this->assertEquals(Director::absoluteURL('subfolder'), $origBaseURL . '/relativebase/subfolder'); - - // absolute base URLs - Director::setBaseURL('http://www.example.org'); - $this->assertEquals(Director::baseURL(), 'http://www.example.org'); - $this->assertEquals(Director::absoluteBaseURL(), 'http://www.example.org'); - $this->assertEquals(Director::absoluteURL('subfolder'), 'http://www.example.org/subfolder'); - + // relative base URLs - you should end them in a / + Director::setBaseURL('/relativebase/'); + $this->assertEquals('/relativebase/', Director::baseURL()); + $this->assertEquals(Director::protocolAndHost() . '/relativebase/', Director::absoluteBaseURL()); + $this->assertEquals(Director::protocolAndHost() . '/relativebase/subfolder/test', Director::absoluteURL('subfolder/test')); + + // absolute base URLs - you should end them in a / + Director::setBaseURL('http://www.example.org/'); + $this->assertEquals('http://www.example.org/', Director::baseURL()); + $this->assertEquals('http://www.example.org/', Director::absoluteBaseURL()); + $this->assertEquals('http://www.example.org/subfolder/test', Director::absoluteURL('subfolder/test')); + + // Setting it to false restores functionality Director::setBaseURL(false); - $this->assertEquals(Director::baseURL(), BASE_URL); - $this->assertEquals(Director::absoluteBaseURL(BASE_URL), BASE_URL); - $this->assertEquals(Director::absoluteURL('subfolder'), BASE_URL . '/subfolder'); + $this->assertEquals(BASE_URL.'/', Director::baseURL()); + $this->assertEquals(Director::protocolAndHost().BASE_URL.'/', Director::absoluteBaseURL(BASE_URL)); + $this->assertEquals(Director::protocolAndHost().BASE_URL . '/subfolder/test', Director::absoluteURL('subfolder/test')); } - */ } ?> \ No newline at end of file