1 <?php
2
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21
22
23
24
25 if(defined('E_DEPRECATED')) error_reporting((E_ALL ^ E_DEPRECATED) & ~E_STRICT);
26 else error_reporting(E_ALL & ~E_STRICT);
27 28 29 30
31 if (!function_exists('array_fill_keys')) {
32 function array_fill_keys($keys,$value) {
33
34 if(sizeof($keys)==0)
35 return Array();
36 else
37 return array_combine($keys,array_fill(0,count($keys),$value));
38 }
39 }
40
41 42 43
44
45 $rootDir = (isset($_SERVER['SCRIPT_FILENAME'])) ? dirname($_SERVER['SCRIPT_FILENAME']).'/' : dirname(__FILE__).'/';
46 $envDir = dirname($rootDir);
47 for ($i=0; $i<3; $i++) {
48 $envFile = $envDir . '/_ss_environment.php';
49
50 if(@file_exists($envFile)) {
51 define('SS_ENVIRONMENT_FILE', $envFile);
52 include_once($envFile);
53 break;
54 }
55 if (!$envDir || $envDir == '/') break;
56 $envDir = dirname($envDir);
57 }
58
59
60
61
62 63 64 65
66 if(!isset($_SERVER['HTTP_HOST'])) {
67 global $_FILE_TO_URL_MAPPING;
68
69 if(isset($_FILE_TO_URL_MAPPING)) {
70 $fullPath = $testPath = $_SERVER['SCRIPT_FILENAME'];
71 while($testPath && $testPath != "/" && !preg_match('/^[A-Z]:\\\\$/', $testPath)) {
72 if(isset($_FILE_TO_URL_MAPPING[$testPath])) {
73 $url = $_FILE_TO_URL_MAPPING[$testPath]
74 . str_replace(DIRECTORY_SEPARATOR,'/',substr($fullPath,strlen($testPath)));
75
76 $_SERVER['HTTP_HOST'] = parse_url($url, PHP_URL_HOST);
77 $_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF'] = parse_url($url, PHP_URL_PATH);
78 $_SERVER['REQUEST_PORT'] = parse_url($url, PHP_URL_PORT);
79 break;
80 }
81 $testPath = dirname($testPath);
82 }
83 }
84
85
86 $serverDefaults = array(
87 'SERVER_PROTOCOL' => 'HTTP/1.1',
88 'HTTP_ACCEPT' => 'text/plain;q=0.5',
89 'HTTP_ACCEPT_LANGUAGE' => '*;q=0.5',
90 'HTTP_ACCEPT_ENCODING' => '',
91 'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1;q=0.5',
92 'SERVER_SIGNATURE' => 'Command-line PHP/' . phpversion(),
93 'SERVER_SOFTWARE' => 'PHP/' . phpversion(),
94 'SERVER_ADDR' => '127.0.0.1',
95 'REMOTE_ADDR' => '127.0.0.1',
96 'REQUEST_METHOD' => 'GET',
97 'HTTP_USER_AGENT' => 'CLI',
98 );
99
100 $_SERVER = array_merge($serverDefaults, $_SERVER);
101
102 103 104 105
106 } else {
107 108 109
110 if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) {
111 if($_REQUEST) stripslashes_recursively($_REQUEST);
112 if($_GET) stripslashes_recursively($_GET);
113 if($_POST) stripslashes_recursively($_POST);
114 }
115
116 117 118
119 if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
120 $_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST'];
121 }
122 }
123
124 125 126
127 if(!defined('BASE_PATH')) {
128
129
130 define('BASE_PATH', rtrim(dirname($rootDir), DIRECTORY_SEPARATOR));
131 }
132
133 if(!defined('BASE_URL')) {
134
135
136 if(substr($_SERVER['SCRIPT_FILENAME'],0,strlen(BASE_PATH)) == BASE_PATH) {
137 $urlSegmentToRemove = substr($_SERVER['SCRIPT_FILENAME'],strlen(BASE_PATH));
138 if(substr($_SERVER['SCRIPT_NAME'],-strlen($urlSegmentToRemove)) == $urlSegmentToRemove) {
139 $baseURL = substr($_SERVER['SCRIPT_NAME'], 0, -strlen($urlSegmentToRemove));
140 define('BASE_URL', rtrim($baseURL, DIRECTORY_SEPARATOR));
141 }
142 }
143
144
145
146 if(!defined('BASE_URL')) {
147 $dir = (strpos($_SERVER['SCRIPT_NAME'], 'index.php') !== false)
148 ? dirname($_SERVER['SCRIPT_NAME'])
149 : dirname(dirname($_SERVER['SCRIPT_NAME']));
150 define('BASE_URL', rtrim($dir, DIRECTORY_SEPARATOR));
151 }
152 }
153 define('MODULES_DIR', 'modules');
154 define('MODULES_PATH', BASE_PATH . '/' . MODULES_DIR);
155 define('THEMES_DIR', 'themes');
156 define('THEMES_PATH', BASE_PATH . '/' . THEMES_DIR);
157 define('SAPPHIRE_DIR', 'sapphire');
158 define('SAPPHIRE_PATH', BASE_PATH . '/' . SAPPHIRE_DIR);
159 define('CMS_DIR', 'cms');
160 define('CMS_PATH', BASE_PATH . '/' . CMS_DIR);
161 define('THIRDPARTY_DIR', SAPPHIRE_DIR . '/thirdparty');
162 define('THIRDPARTY_PATH', BASE_PATH . '/' . THIRDPARTY_DIR);
163 define('ASSETS_DIR', 'assets');
164 define('ASSETS_PATH', BASE_PATH . '/' . ASSETS_DIR);
165
166 167 168
169 if(!defined('TEMP_FOLDER')) {
170 define('TEMP_FOLDER', getTempFolder());
171 }
172
173 174 175
176 define('PR_HIGH',100);
177 define('PR_MEDIUM',50);
178 define('PR_LOW',10);
179
180 181 182
183
184 increase_memory_limit_to('64M');
185
186
187
188
189 190 191 192
193
194
195
196 set_include_path(str_replace('.' . PATH_SEPARATOR, '.' . PATH_SEPARATOR
197 . BASE_PATH . '/sapphire' . PATH_SEPARATOR
198 . BASE_PATH . '/sapphire/parsers' . PATH_SEPARATOR
199 . BASE_PATH . '/sapphire/thirdparty' . PATH_SEPARATOR
200 , get_include_path()));
201
202 require_once("core/ManifestBuilder.php");
203 require_once("core/ClassInfo.php");
204 require_once('core/Object.php');
205 require_once('core/control/Director.php');
206 require_once('filesystem/Filesystem.php');
207 require_once("core/Session.php");
208
209
210
211
212 213 214
215 ManifestBuilder::include_manifest();
216
217 218 219
220 if(isset($_GET['debugmanifest'])) Debug::show(file_get_contents(MANIFEST_FILE));
221
222
223
224
225 if (Director::isLive()) {
226 if(defined('E_DEPRECATED')) error_reporting(((E_ALL ^ E_NOTICE) ^ E_DEPRECATED) & ~E_STRICT);
227 else error_reporting((E_ALL ^ E_NOTICE) & ~E_STRICT);
228 }
229
230
231
232 233 234
235 Debug::loadErrorHandlers();
236
237
238
239
240 function getSysTempDir() {
241 if(function_exists('sys_get_temp_dir')) {
242 $sysTmp = sys_get_temp_dir();
243 } elseif(isset($_ENV['TMP'])) {
244 $sysTmp = $_ENV['TMP'];
245 } else {
246 $tmpFile = tempnam('adfadsfdas','');
247 unlink($tmpFile);
248 $sysTmp = dirname($tmpFile);
249 }
250 return $sysTmp;
251 }
252
253 254 255 256 257 258 259
260 function getTempFolder($base = null) {
261 $ssTmp = BASE_PATH . "/silverstripe-cache";
262 if(@file_exists($ssTmp)) {
263 return $ssTmp;
264 }
265
266 if(!$base) $base = BASE_PATH;
267
268 if($base) {
269 $cachefolder = "silverstripe-cache" . str_replace(array(' ', "/", ":", "\\"), "-", $base);
270 } else {
271 $cachefolder = "silverstripe-cache";
272 }
273
274 $sysTmp = getSysTempDir();
275
276 $worked = true;
277 $ssTmp = "$sysTmp/$cachefolder";
278 if(!@file_exists($ssTmp)) {
279 @$worked = mkdir($ssTmp);
280 }
281 if(!$worked) {
282 $ssTmp = BASE_PATH . "/silverstripe-cache";
283 $worked = true;
284 if(!@file_exists($ssTmp)) {
285 @$worked = mkdir($ssTmp);
286 }
287 }
288 if(!$worked) {
289 user_error("Permission problem gaining access to a temp folder. " .
290 "Please create a folder named silverstripe-cache in the base folder " .
291 "of the installation and ensure it has the correct permissions", E_USER_ERROR);
292 }
293
294 return $ssTmp;
295 }
296
297 298 299 300 301 302 303
304 function __autoload($className) {
305 global $_CLASS_MANIFEST;
306 $lClassName = strtolower($className);
307 if(isset($_CLASS_MANIFEST[$lClassName])) include_once($_CLASS_MANIFEST[$lClassName]);
308 else if(isset($_CLASS_MANIFEST[$className])) include_once($_CLASS_MANIFEST[$className]);
309 }
310
311 312 313
314 function getClassFile($className) {
315 global $_CLASS_MANIFEST;
316 $lClassName = strtolower($className);
317 if(isset($_CLASS_MANIFEST[$lClassName])) return $_CLASS_MANIFEST[$lClassName];
318 else if(isset($_CLASS_MANIFEST[$className])) return $_CLASS_MANIFEST[$className];
319 }
320
321 322 323 324 325 326 327 328 329 330 331 332
333 function singleton($className) {
334 global $_SINGLETONS;
335 if(!isset($className)) user_error("singleton() Called without a class", E_USER_ERROR);
336 if(!is_string($className)) user_error("singleton() passed bad class_name: " . var_export($className,true), E_USER_ERROR);
337 if(!isset($_SINGLETONS[$className])) {
338 if(!class_exists($className)) user_error("Bad class to singleton() - $className", E_USER_ERROR);
339 $_SINGLETONS[$className] = Object::strong_create($className,null, true);
340 if(!$_SINGLETONS[$className]) user_error("singleton() Unknown class '$className'", E_USER_ERROR);
341 }
342 return $_SINGLETONS[$className];
343 }
344
345 function project() {
346 global $project;
347 return $project;
348 }
349
350 function stripslashes_recursively(&$array) {
351 foreach($array as $k => $v) {
352 if(is_array($v)) stripslashes_recursively($array[$k]);
353 else $array[$k] = stripslashes($v);
354 }
355 }
356
357 358 359
360 function _t($entity, $string = "", $priority = 40, $context = "") {
361 return i18n::_t($entity, $string, $priority, $context);
362 }
363
364 365 366 367
368 function increase_memory_limit_to($memoryLimit = -1) {
369 $curLimit = ini_get('memory_limit');
370
371
372 if($curLimit == -1) return;
373
374
375 if($memoryLimit == -1 || translate_memstring($memoryLimit) > translate_memstring($curLimit)) {
376 ini_set('memory_limit', $memoryLimit);
377 }
378 }
379
380 381 382 383
384 function translate_memstring($memString) {
385 switch(strtolower(substr($memString, -1))) {
386 case "k": return round(substr($memString, 0, -1)*1024);
387 case "m": return round(substr($memString, 0, -1)*1024*1024);
388 case "g": return round(substr($memString, 0, -1)*1024*1024*1024);
389 default: return round($memString);
390 }
391 }
392
393 394 395 396
397 function increase_time_limit_to($timeLimit = null) {
398 if(!ini_get('safe_mode')) {
399 if(!$timeLimit) {
400 set_time_limit(0);
401 } else {
402 $currTimeLimit = ini_get('max_execution_time');
403 if($currTimeLimit && $currTimeLimit < $timeLimit) {
404 set_time_limit($timeLimit);
405 }
406 }
407 }
408 }
409
410 ?>
411
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-