PageRenderTime 53ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/index.php

http://github.com/ryancramerdesign/ProcessWire
PHP | 250 lines | 115 code | 30 blank | 105 comment | 42 complexity | 756b8a5685ce2b6b6e92062dcf040973 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * ProcessWire Bootstrap
  4. *
  5. * This file may be used to bootstrap either the http web accessible
  6. * version, or the command line client version of ProcessWire.
  7. *
  8. * Note: if you happen to change any directory references in here, please
  9. * do so after you have installed the site, as the installer is not informed
  10. * of any changes made in this file.
  11. *
  12. * ProcessWire 2.x
  13. * Copyright (C) 2015 by Ryan Cramer
  14. * Licensed under Mozilla Public License v2.0 http://mozilla.org/MPL/2.0/
  15. *
  16. * https://processwire.com
  17. *
  18. * @version 2.7
  19. *
  20. */
  21. define("PROCESSWIRE", 270); // index version
  22. /**
  23. * Build the ProcessWire configuration
  24. *
  25. * @return Config
  26. *
  27. */
  28. function ProcessWireBootConfig() {
  29. /*
  30. * Define installation paths and urls
  31. *
  32. */
  33. $rootPath = dirname(__FILE__);
  34. if(DIRECTORY_SEPARATOR != '/') $rootPath = str_replace(DIRECTORY_SEPARATOR, '/', $rootPath);
  35. if(isset($_SERVER['HTTP_HOST'])) {
  36. $httpHost = strtolower($_SERVER['HTTP_HOST']);
  37. // when serving pages from a web server
  38. $rootURL = rtrim(dirname($_SERVER['SCRIPT_NAME']), "/\\") . '/';
  39. // check if we're being included from another script and adjust the rootPath accordingly
  40. $sf = empty($_SERVER['SCRIPT_FILENAME']) ? '' : dirname(realpath($_SERVER['SCRIPT_FILENAME']));
  41. $f = dirname(realpath(__FILE__));
  42. if($sf && $sf != $f && strpos($sf, $f) === 0) {
  43. $x = rtrim(substr($sf, strlen($f)), '/');
  44. $rootURL = substr($rootURL, 0, strlen($rootURL) - strlen($x));
  45. }
  46. unset($sf, $f, $x);
  47. } else {
  48. // when included from another app or command line script
  49. $httpHost = '';
  50. $rootURL = '/';
  51. }
  52. /*
  53. * Allow for an optional /index.config.php file that can point to a different site configuration per domain/host.
  54. *
  55. */
  56. $siteDir = 'site';
  57. $indexConfigFile = $rootPath . "/index.config.php";
  58. if(is_file($indexConfigFile)) {
  59. // optional config file is present in root
  60. @include($indexConfigFile);
  61. $hostConfig = function_exists("ProcessWireHostSiteConfig") ? ProcessWireHostSiteConfig() : array();
  62. if($httpHost && isset($hostConfig[$httpHost])) $siteDir = $hostConfig[$httpHost];
  63. else if(isset($hostConfig['*'])) $siteDir = $hostConfig['*']; // default override
  64. }
  65. // other default directories
  66. $wireDir = 'wire';
  67. $coreDir = "$wireDir/core";
  68. $assetsDir = "$siteDir/assets";
  69. $adminTplDir = 'templates-admin';
  70. /*
  71. * Setup ProcessWire class autoloads
  72. *
  73. */
  74. require("$rootPath/$coreDir/ProcessWire.php");
  75. /*
  76. * Setup configuration data and default paths/urls
  77. *
  78. */
  79. $config = new Config();
  80. $config->urls = new Paths($rootURL);
  81. $config->urls->wire = "$wireDir/";
  82. $config->urls->site = "$siteDir/";
  83. $config->urls->modules = "$wireDir/modules/";
  84. $config->urls->siteModules = "$siteDir/modules/";
  85. $config->urls->core = "$coreDir/";
  86. $config->urls->assets = "$assetsDir/";
  87. $config->urls->cache = "$assetsDir/cache/";
  88. $config->urls->logs = "$assetsDir/logs/";
  89. $config->urls->files = "$assetsDir/files/";
  90. $config->urls->tmp = "$assetsDir/tmp/";
  91. $config->urls->templates = "$siteDir/templates/";
  92. $config->urls->adminTemplates = is_dir("$siteDir/$adminTplDir") ? "$siteDir/$adminTplDir/" : "$wireDir/$adminTplDir/";
  93. $config->paths = clone $config->urls;
  94. $config->paths->root = $rootPath . '/';
  95. $config->paths->sessions = $config->paths->assets . "sessions/";
  96. /*
  97. * Styles and scripts are CSS and JS files, as used by the admin application.
  98. * But reserved here if needed by other apps and templates.
  99. *
  100. */
  101. $config->styles = new FilenameArray();
  102. $config->scripts = new FilenameArray();
  103. /*
  104. * Include system and user-specified configuration options
  105. *
  106. */
  107. include("$rootPath/$wireDir/config.php");
  108. $configFile = "$rootPath/$siteDir/config.php";
  109. $configFileDev = "$rootPath/$siteDir/config-dev.php";
  110. @include(is_file($configFileDev) ? $configFileDev : $configFile);
  111. /*
  112. * $config->debugIf: optional setting to determine if debug mode should be on or off
  113. *
  114. */
  115. if($config->debugIf && is_string($config->debugIf)) {
  116. $debugIf = trim($config->debugIf);
  117. if(strpos($debugIf, '/') === 0) $debugIf = (bool) @preg_match($debugIf, $_SERVER['REMOTE_ADDR']); // regex IPs
  118. else if(is_callable($debugIf)) $debugIf = $debugIf(); // callable function to determine debug mode for us
  119. else $debugIf = $debugIf === $_SERVER['REMOTE_ADDR']; // exact IP match
  120. $config->debug = $debugIf;
  121. }
  122. /*
  123. * If debug mode is on then echo all errors, if not then disable all error reporting
  124. *
  125. */
  126. if($config->debug) {
  127. error_reporting(E_ALL | E_STRICT);
  128. ini_set('display_errors', 1);
  129. } else {
  130. error_reporting(0);
  131. ini_set('display_errors', 0);
  132. }
  133. /*
  134. * If PW2 is not installed, go to the installer
  135. *
  136. */
  137. if(!$config->dbName) {
  138. if(is_file("./install.php") && strtolower($_SERVER['REQUEST_URI']) == strtolower($rootURL)) {
  139. require("./install.php");
  140. exit(0);
  141. } else {
  142. header("HTTP/1.1 404 Page Not Found");
  143. echo "404 page not found (no site configuration or install.php available)";
  144. exit(0);
  145. }
  146. }
  147. /*
  148. * Prepare any PHP ini_set options
  149. *
  150. */
  151. session_name($config->sessionName);
  152. ini_set('session.use_cookies', true);
  153. ini_set('session.use_only_cookies', 1);
  154. ini_set('session.cookie_httponly', 1);
  155. ini_set('session.gc_maxlifetime', $config->sessionExpireSeconds);
  156. if(ini_get('session.save_handler') == 'files') {
  157. if(ini_get('session.gc_probability') == 0) {
  158. // Some debian distros replace PHP's gc without fully implementing it,
  159. // which results in broken garbage collection if the save_path is set.
  160. // As a result, we avoid setting the save_path when this is detected.
  161. } else {
  162. ini_set("session.save_path", rtrim($config->paths->sessions, '/'));
  163. }
  164. }
  165. return $config;
  166. }
  167. /**
  168. * Shutdown function for cleanup of externally bootstrapped requests
  169. *
  170. */
  171. function ProcessWireExternalShutdown() {
  172. if(error_get_last()) return;
  173. $process = wire('process');
  174. if($process == 'ProcessPageView') $process->finished();
  175. }
  176. /**
  177. * Determine if the request is one we should handle (internal) or one that another
  178. * script coming after this (external) will handle.
  179. *
  180. */
  181. $internal = isset($_SERVER['HTTP_HOST']) && realpath(__FILE__) == realpath($_SERVER['SCRIPT_FILENAME']);
  182. if(!$internal) register_shutdown_function('ProcessWireExternalShutdown');
  183. /*
  184. * If you include ProcessWire's index.php from another script, or from a
  185. * command-line script, the $wire variable or wire() function is your
  186. * connection to the API of this ProcessWire instance.
  187. *
  188. */
  189. $process = null;
  190. $wire = null;
  191. /**
  192. * Build the ProcessWire configuration
  193. *
  194. */
  195. $config = ProcessWireBootConfig();
  196. /*
  197. * Load and execute ProcessWire
  198. *
  199. */
  200. try {
  201. /*
  202. * Bootstrap ProcessWire's core and make the API available with $wire or wire()
  203. *
  204. */
  205. $wire = new ProcessWire($config);
  206. $process = $wire->modules->get('ProcessPageView');
  207. $wire->wire('process', $process);
  208. echo $process->execute($internal);
  209. $internal ? $process->finished() : extract($wire->wire('all')->getArray());
  210. } catch(Exception $e) {
  211. /*
  212. * Formulate error message and send to the error handler
  213. *
  214. */
  215. if($process) $process->failed($e);
  216. $wire ? $wire->trackException($e) : $config->trackException($e);
  217. $errorMessage = "Exception: " . $e->getMessage() . " (in " . $e->getFile() . " line " . $e->getLine() . ")";
  218. if($config->debug || ($wire && $wire->user && $wire->user->isSuperuser())) $errorMessage .= "\n\n" . $e->getTraceAsString();
  219. trigger_error($errorMessage, E_USER_ERROR);
  220. }