PageRenderTime 75ms CodeModel.GetById 38ms RepoModel.GetById 1ms app.codeStats 0ms

/start.php

https://github.com/uCore/uCore
PHP | 161 lines | 119 code | 12 blank | 30 comment | 14 complexity | 05d3d8b682f4231847c8f2a2eb18a8cf MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, Apache-2.0, GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Initialise global timer
  4. */
  5. define('UCORE_START_TIME', microtime(true));
  6. /**
  7. * When running as Command Line Interface, for example crons, initialise some required server variables
  8. * CLI execution is useful for bypassing the http server.
  9. * Usage: php <path_to_core_index> <request_uri>
  10. * Example: php uCore/index.php /
  11. */
  12. if(PHP_SAPI == "cli")
  13. {
  14. $_SERVER['HTTP_HOST'] = 'cli';
  15. if(isset($argv[1]))
  16. {
  17. $_SERVER['REQUEST_URI'] = $argv[1];
  18. }
  19. else
  20. {
  21. $_SERVER['REQUEST_URI'] = $argv[0];
  22. }
  23. $_SERVER['SCRIPT_NAME'] = '/' . basename(dirname(__FILE__)) . '/index.php';
  24. $_SERVER['REMOTE_ADDR'] = 'cli';
  25. putenv('HTTP_MOD_REWRITE=On');
  26. $q = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
  27. parse_str($q, $_GET);
  28. }
  29. /**
  30. * Initialise Character set to UTF8
  31. */
  32. define('CHARSET_ENCODING', 'utf-8');
  33. define('SQL_CHARSET_ENCODING', 'utf8');
  34. define('SQL_COLLATION', 'utf8_general_ci');
  35. /**
  36. * start GZIP if enabled on the server to compress output
  37. */
  38. if(!ini_get('output_buffering'))
  39. {
  40. ob_start();
  41. }
  42. $enc = isset($_SERVER['HTTP_ACCEPT_ENCODING']) ? $_SERVER['HTTP_ACCEPT_ENCODING'] : '';
  43. define('GZIP_ENABLED', substr_count($enc, 'gzip') || substr_count($enc, 'deflate'));
  44. if(GZIP_ENABLED)
  45. {
  46. ob_start("ob_gzhandler");
  47. }
  48. else
  49. {
  50. ob_start();
  51. }
  52. function fix_path($path, $slash = '')
  53. {
  54. if(!$slash)
  55. {
  56. $slash = DIRECTORY_SEPARATOR;
  57. }
  58. $path = str_replace(['\\', '/'], $slash, $path);
  59. return str_replace($slash . $slash, $slash, $path);
  60. }
  61. /**
  62. * Define constants for directory structure:
  63. * PATH_ABS_CORE: absolute (server) path to core folder
  64. * PATH_ABS_ROOT: absolute (server) path to document root
  65. * PATH_REL_CORE: path to core folder relative to document root
  66. * PATH_REL_ROOT: path to document relative to document root (including /~UserDir/)
  67. * DEFAULT_FILE: relative path to core index.php
  68. * PATH_ABS_CONFIG: absolute path to configuration file
  69. * PATH_ABS_MODULES: absolute path to uModules folder
  70. * PATH_ABS_TEMPLATES: absolute path to uTemplates folder (DEPRECIATED)
  71. * PATH_ABS_THEMES: absolute path to uThemes folder
  72. * PATH_FULL_ROOT: full URL to root including domain and schema
  73. * PATH_FULL_CORE: full URL to core including domain and schema
  74. */
  75. define('PATH_ABS_CORE', fix_path(dirname(__FILE__) . DIRECTORY_SEPARATOR));
  76. define('PATH_ABS_ROOT', fix_path(realpath(PATH_ABS_CORE . '..') . DIRECTORY_SEPARATOR));
  77. $coreDiff = fix_path(preg_replace('/^' . preg_quote(PATH_ABS_ROOT, '/') . '/', '', PATH_ABS_CORE), '/');
  78. define('PATH_REL_ROOT', '/');
  79. define('PATH_REL_CORE', fix_path(PATH_REL_ROOT . $coreDiff, '/'));
  80. define('DEFAULT_FILE', PATH_REL_CORE . 'index.php');
  81. define('PATH_ABS_CONFIG', fix_path(PATH_ABS_ROOT . 'uConfig.php'));
  82. define('PATH_ABS_MODULES', fix_path(PATH_ABS_ROOT . 'uModules') . '/');
  83. define('PATH_ABS_TEMPLATES', fix_path(PATH_ABS_ROOT . 'uTemplates') . '/');
  84. define('PATH_ABS_THEMES', fix_path(PATH_ABS_ROOT . 'uThemes') . '/');
  85. define(
  86. 'PATH_FULL_ROOT',
  87. ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . PATH_REL_ROOT
  88. );
  89. define(
  90. 'PATH_FULL_CORE',
  91. ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . PATH_REL_CORE
  92. );
  93. define('MAX_ORDER', 99999999);
  94. /**
  95. * Initialise session
  96. */
  97. ini_set('session.cookie_path', PATH_REL_ROOT);
  98. session_cache_limiter(false);
  99. session_name('ucore');
  100. session_start();
  101. $timeout = 3600;
  102. if(isset($_SESSION['SESSION_LIFETIME']))
  103. {
  104. $timeout = $_SESSION['SESSION_LIFETIME'];
  105. }
  106. if(isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > $timeout))
  107. {
  108. session_unset();
  109. session_destroy();
  110. }
  111. if(!isset($_SESSION['CREATED']))
  112. {
  113. $_SESSION['CREATED'] = time();
  114. }
  115. if(time() - $_SESSION['CREATED'] > 1800)
  116. { // session started more than 30 minates ago
  117. session_regenerate_id(true);
  118. $_SESSION['CREATED'] = time();
  119. }
  120. $_SESSION['LAST_ACTIVITY'] = time();
  121. /**
  122. * Initialise error handling
  123. */
  124. include('error.php');
  125. /**
  126. * Glob and load all interfaces and functs scripts
  127. */
  128. foreach(glob(PATH_ABS_CORE . 'interfaces/*.php') as $fn)
  129. {
  130. include($fn);
  131. }
  132. foreach(glob(PATH_ABS_CORE . 'functs/*.php') as $fn)
  133. {
  134. include($fn);
  135. }
  136. /**
  137. * Initialise the core configuration
  138. */
  139. require(PATH_ABS_CORE . 'setup.php');
  140. /**
  141. * Run initialisation code
  142. */
  143. require(PATH_ABS_CORE . 'initialise.php'); // init