laravel.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php namespace Laravel;
  2. /**
  3. * Bootstrap the core framework components like the IoC container and
  4. * the configuration class, and the class auto-loader. Once this file
  5. * has run, the framework is essentially ready for use.
  6. */
  7. require 'core.php';
  8. /**
  9. * Register the PHP exception handler. The framework throws exceptions
  10. * on every error that cannot be handled. All of those exceptions will
  11. * be sent through this closure for processing.
  12. */
  13. set_exception_handler(function($e)
  14. {
  15. Error::exception($e);
  16. });
  17. /**
  18. * Register the PHP error handler. All PHP errors will fall into this
  19. * handler which will convert the error into an ErrorException object
  20. * and pass the exception into the exception handler.
  21. */
  22. set_error_handler(function($code, $error, $file, $line)
  23. {
  24. Error::native($code, $error, $file, $line);
  25. });
  26. /**
  27. * Register the shutdown handler. This function will be called at the
  28. * end of the PHP script or on a fatal PHP error. If a PHP error has
  29. * occured, we will convert it to an ErrorException and pass it
  30. * to the common exception handler for the framework.
  31. */
  32. register_shutdown_function(function()
  33. {
  34. Error::shutdown();
  35. });
  36. /**
  37. * Setting the PHP error reporting level to -1 essentially forces
  38. * PHP to report every error, and it is guranteed to show every
  39. * error on future versions of PHP.
  40. *
  41. * If error detail is turned off, we will turn off all PHP error
  42. * reporting and display since the framework will be displaying
  43. * a generic message and we do not want any sensitive details
  44. * about the exception leaking into the views.
  45. */
  46. error_reporting(-1);
  47. ini_set('display_errors', Config::get('error.display'));
  48. /**
  49. * Even though "Magic Quotes" are deprecated in PHP 5.3, they may
  50. * still be enabled on the server. To account for this, we will
  51. * strip slashes on all input arrays if magic quotes are turned
  52. * on for the server environment.
  53. */
  54. if (magic_quotes())
  55. {
  56. $magics = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
  57. foreach ($magics as &$magic)
  58. {
  59. $magic = array_strip_slashes($magic);
  60. }
  61. }
  62. /**
  63. * Load the session using the session manager. The payload will
  64. * be registered in the IoC container as an instance so it can
  65. * be easily access throughout the framework.
  66. */
  67. if (Config::get('session.driver') !== '')
  68. {
  69. Session::start(Config::get('session.driver'));
  70. Session::load(Cookie::get(Config::get('session.cookie')));
  71. IoC::instance('laravel.session', Session::$instance);
  72. }
  73. /**
  74. * Gather the input to the application based on the global input
  75. * variables for the current request. The input will be gathered
  76. * based on the current request method and will be set on the
  77. * Input manager class' static $input property.
  78. */
  79. $input = array();
  80. switch (Request::method())
  81. {
  82. case 'GET':
  83. $input = $_GET;
  84. break;
  85. case 'POST':
  86. $input = $_POST;
  87. break;
  88. case 'PUT':
  89. case 'DELETE':
  90. if (Request::spoofed())
  91. {
  92. $input = $_POST;
  93. }
  94. else
  95. {
  96. parse_str(file_get_contents('php://input'), $input);
  97. if (magic_quotes()) $input = array_strip_slashes($input);
  98. }
  99. }
  100. /**
  101. * The spoofed request method is removed from the input so it is not
  102. * unexpectedly included in Input::all() or Input::get(). Leaving it
  103. * in the input array could cause unexpected results if an Eloquent
  104. * model is filled with the input.
  105. */
  106. unset($input[Request::spoofer]);
  107. Input::$input = $input;
  108. /**
  109. * Load the "application" bundle. Though the application folder is
  110. * not typically considered a bundle, it is started like one and
  111. * essentially serves as the "default" bundle.
  112. */
  113. Bundle::start(DEFAULT_BUNDLE);
  114. /**
  115. * Start all of the bundles that are specified in the configuration
  116. * array of auto-loaded bundles. This lets the developer have an
  117. * easy way to load bundles for every request.
  118. */
  119. foreach (Bundle::all() as $bundle => $config)
  120. {
  121. if ($config['auto']) Bundle::start($bundle);
  122. }
  123. /**
  124. * If the requset URI has too many segments, we will bomb out of
  125. * the request. This is too avoid potential DDoS attacks against
  126. * the framework by overloading the controller lookup method
  127. * with thousands of segments.
  128. */
  129. $uri = URI::current();
  130. if (count(URI::$segments) > 15)
  131. {
  132. throw new \Exception("Invalid request. Too many URI segments.");
  133. }
  134. /**
  135. * Route the request to the proper route in the application. If a
  136. * route is found, the route will be called via the request class
  137. * static property. If no route is found, the 404 response will
  138. * be returned to the browser.
  139. */
  140. Request::$route = Routing\Router::route(Request::method(), $uri);
  141. if (is_null(Request::$route))
  142. {
  143. Request::$route = new Routing\Route('GET /404', array(function()
  144. {
  145. return Response::error('404');
  146. }));
  147. $response = Response::error('404');
  148. }
  149. $response = Request::$route->call();
  150. /**
  151. * Close the session and write the active payload to persistent
  152. * storage. The session cookie will also be written and if the
  153. * driver is a sweeper, session garbage collection might be
  154. * performed depending on the "sweepage" probability.
  155. */
  156. if (Config::get('session.driver') !== '')
  157. {
  158. Session::save();
  159. }
  160. /**
  161. * Send all of the cookies to the browser. The cookies are
  162. * stored in a "jar" until the end of a request, primarily
  163. * to make testing the cookie functionality of the site
  164. * much easier since the jar can be inspected.
  165. */
  166. Cookie::send();
  167. /**
  168. * Send the final response to the browser and fire the
  169. * final event indicating that the processing for the
  170. * current request is completed.
  171. */
  172. $response->send();
  173. Event::fire('laravel: done');