laravel.php 5.8 KB

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