laravel.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. * Determine if we need to set the application key to a random
  50. * string for the developer. This provides the developer with
  51. * a zero configuration install process.
  52. */
  53. if (Config::get('application.key') == '')
  54. {
  55. ob_start() and with(new CLI\Tasks\Key)->generate();
  56. ob_end_clean();
  57. }
  58. /**
  59. * Even though "Magic Quotes" are deprecated in PHP 5.3, they may
  60. * still be enabled on the server. To account for this, we will
  61. * strip slashes on all input arrays if magic quotes are turned
  62. * on for the server environment.
  63. */
  64. if (magic_quotes())
  65. {
  66. $magics = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
  67. foreach ($magics as &$magic)
  68. {
  69. $magic = array_strip_slashes($magic);
  70. }
  71. }
  72. /**
  73. * Load the session using the session manager. The payload will
  74. * be set on a static property of the Session class for easy
  75. * access throughout the framework and application.
  76. */
  77. if (Config::get('session.driver') !== '')
  78. {
  79. Session::start(Config::get('session.driver'));
  80. Session::load(Cookie::get(Config::get('session.cookie')));
  81. }
  82. /**
  83. * Gather the input to the application based on the global input
  84. * variables for the current request. The input will be gathered
  85. * based on the current request method and will be set on the
  86. * Input manager class' static $input property.
  87. */
  88. $input = array();
  89. switch (Request::method())
  90. {
  91. case 'GET':
  92. $input = $_GET;
  93. break;
  94. case 'POST':
  95. $input = $_POST;
  96. break;
  97. case 'PUT':
  98. case 'DELETE':
  99. if (Request::spoofed())
  100. {
  101. $input = $_POST;
  102. }
  103. else
  104. {
  105. parse_str(file_get_contents('php://input'), $input);
  106. if (magic_quotes()) $input = array_strip_slashes($input);
  107. }
  108. }
  109. /**
  110. * The spoofed request method is removed from the input so it is not
  111. * unexpectedly included in Input::all() or Input::get(). Leaving it
  112. * in the input array could cause unexpected results if an Eloquent
  113. * model is filled with the input.
  114. */
  115. unset($input[Request::spoofer]);
  116. Input::$input = $input;
  117. /**
  118. * Load the "application" bundle. Though the application folder is
  119. * not typically considered a bundle, it is started like one and
  120. * essentially serves as the "default" bundle.
  121. */
  122. Bundle::start(DEFAULT_BUNDLE);
  123. /**
  124. * Auto-start any bundles configured to start on every request.
  125. * This is especially useful for debug bundles or bundles that
  126. * are used throughout the application.
  127. */
  128. foreach (Bundle::$bundles as $bundle => $config)
  129. {
  130. if ($config['auto']) Bundle::start($bundle);
  131. }
  132. /**
  133. * Register the "catch-all" route that handles 404 responses for
  134. * routes that can not be matched to any other route within the
  135. * application. We'll just raise the 404 event.
  136. */
  137. Routing\Router::register('*', '(:all)', function()
  138. {
  139. return Event::first('404');
  140. });
  141. /**
  142. * If the requset URI has too many segments, we will bomb out of
  143. * the request. This is too avoid potential DDoS attacks against
  144. * the framework by overloading the controller lookup method
  145. * with thousands of segments.
  146. */
  147. $uri = URI::current();
  148. if (count(URI::$segments) > 15)
  149. {
  150. throw new \Exception("Invalid request. Too many URI segments.");
  151. }
  152. /**
  153. * Route the request to the proper route in the application. If a
  154. * route is found, the route will be called via the request class
  155. * static property. If no route is found, the 404 response will
  156. * be returned to the browser.
  157. */
  158. Request::$route = Routing\Router::route(Request::method(), $uri);
  159. $response = Request::$route->call();
  160. /**
  161. * Close the session and write the active payload to persistent
  162. * storage. The session cookie will also be written and if the
  163. * driver is a sweeper, session garbage collection might be
  164. * performed depending on the "sweepage" probability.
  165. */
  166. if (Config::get('session.driver') !== '')
  167. {
  168. Session::save();
  169. }
  170. /**
  171. * Send all of the cookies to the browser. The cookies are
  172. * stored in a "jar" until the end of a request, primarily
  173. * to make testing the cookie functionality of the site
  174. * much easier since the jar can be inspected.
  175. */
  176. Cookie::send();
  177. /**
  178. * Send the final response to the browser and fire the
  179. * final event indicating that the processing for the
  180. * current request is completed.
  181. */
  182. $response->send();
  183. Event::fire('laravel.done');