laravel.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 set on a static property of the Session class for easy
  65. * access throughout the framework and application.
  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. }
  72. /**
  73. * Gather the input to the application based on the global input
  74. * variables for the current request. The input will be gathered
  75. * based on the current request method and will be set on the
  76. * Input manager class' static $input property.
  77. */
  78. $input = array();
  79. switch (Request::method())
  80. {
  81. case 'GET':
  82. $input = $_GET;
  83. break;
  84. case 'POST':
  85. $input = $_POST;
  86. break;
  87. case 'PUT':
  88. case 'DELETE':
  89. if (Request::spoofed())
  90. {
  91. $input = $_POST;
  92. }
  93. else
  94. {
  95. parse_str(file_get_contents('php://input'), $input);
  96. if (magic_quotes()) $input = array_strip_slashes($input);
  97. }
  98. }
  99. /**
  100. * The spoofed request method is removed from the input so it is not
  101. * unexpectedly included in Input::all() or Input::get(). Leaving it
  102. * in the input array could cause unexpected results if an Eloquent
  103. * model is filled with the input.
  104. */
  105. unset($input[Request::spoofer]);
  106. Input::$input = $input;
  107. /**
  108. * Load the "application" bundle. Though the application folder is
  109. * not typically considered a bundle, it is started like one and
  110. * essentially serves as the "default" bundle.
  111. */
  112. Bundle::start(DEFAULT_BUNDLE);
  113. /**
  114. * Auto-start any bundles configured to start on every request.
  115. * This is especially useful for debug bundles or bundles that
  116. * are used throughout the application.
  117. */
  118. foreach (Bundle::$bundles as $bundle => $config)
  119. {
  120. if ($config['auto']) Bundle::start($bundle);
  121. }
  122. /**
  123. * Register the "catch-all" route that handles 404 responses for
  124. * routes that can not be matched to any other route within the
  125. * application. We'll just raise the 404 event.
  126. */
  127. Routing\Router::register('*', '(:all)', function()
  128. {
  129. return Event::first('404');
  130. });
  131. /**
  132. * If the requset URI has too many segments, we will bomb out of
  133. * the request. This is too avoid potential DDoS attacks against
  134. * the framework by overloading the controller lookup method
  135. * with thousands of segments.
  136. */
  137. $uri = URI::current();
  138. if (count(URI::$segments) > 15)
  139. {
  140. throw new \Exception("Invalid request. Too many URI segments.");
  141. }
  142. /**
  143. * Route the request to the proper route in the application. If a
  144. * route is found, the route will be called via the request class
  145. * static property. If no route is found, the 404 response will
  146. * be returned to the browser.
  147. */
  148. Request::$route = Routing\Router::route(Request::method(), $uri);
  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');