[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/webroot/ -> index.php (source)

   1  <?php
   2  
   3  $phabricator_root = dirname(dirname(__FILE__));
   4  require_once $phabricator_root.'/support/PhabricatorStartup.php';
   5  
   6  // If the preamble script exists, load it.
   7  $preamble_path = $phabricator_root.'/support/preamble.php';
   8  if (file_exists($preamble_path)) {
   9    require_once $preamble_path;
  10  }
  11  
  12  PhabricatorStartup::didStartup();
  13  
  14  $show_unexpected_traces = false;
  15  try {
  16    PhabricatorStartup::loadCoreLibraries();
  17  
  18    PhabricatorEnv::initializeWebEnvironment();
  19  
  20    $debug_time_limit = PhabricatorEnv::getEnvConfig('debug.time-limit');
  21    if ($debug_time_limit) {
  22      PhabricatorStartup::setDebugTimeLimit($debug_time_limit);
  23    }
  24  
  25    $show_unexpected_traces = PhabricatorEnv::getEnvConfig(
  26      'phabricator.developer-mode');
  27  
  28    // This is the earliest we can get away with this, we need env config first.
  29    PhabricatorAccessLog::init();
  30    $access_log = PhabricatorAccessLog::getLog();
  31    PhabricatorStartup::setGlobal('log.access', $access_log);
  32    $access_log->setData(
  33      array(
  34        'R' => AphrontRequest::getHTTPHeader('Referer', '-'),
  35        'r' => idx($_SERVER, 'REMOTE_ADDR', '-'),
  36        'M' => idx($_SERVER, 'REQUEST_METHOD', '-'),
  37      ));
  38  
  39    DarkConsoleXHProfPluginAPI::hookProfiler();
  40    DarkConsoleErrorLogPluginAPI::registerErrorHandler();
  41  
  42    $sink = new AphrontPHPHTTPSink();
  43  
  44    $response = PhabricatorSetupCheck::willProcessRequest();
  45    if ($response) {
  46      PhabricatorStartup::endOutputCapture();
  47      $sink->writeResponse($response);
  48      return;
  49    }
  50  
  51    $host = AphrontRequest::getHTTPHeader('Host');
  52    $path = $_REQUEST['__path__'];
  53  
  54    switch ($host) {
  55      default:
  56        $config_key = 'aphront.default-application-configuration-class';
  57        $application = PhabricatorEnv::newObjectFromConfig($config_key);
  58        break;
  59    }
  60  
  61    $application->setHost($host);
  62    $application->setPath($path);
  63    $application->willBuildRequest();
  64    $request = $application->buildRequest();
  65  
  66    // Until an administrator sets "phabricator.base-uri", assume it is the same
  67    // as the request URI. This will work fine in most cases, it just breaks down
  68    // when daemons need to do things.
  69    $request_protocol = ($request->isHTTPS() ? 'https' : 'http');
  70    $request_base_uri = "{$request_protocol}://{$host}/";
  71    PhabricatorEnv::setRequestBaseURI($request_base_uri);
  72  
  73    $write_guard = new AphrontWriteGuard(array($request, 'validateCSRF'));
  74  
  75    $application->setRequest($request);
  76    list($controller, $uri_data) = $application->buildController();
  77    $request->setURIMap($uri_data);
  78    $controller->setRequest($request);
  79  
  80    $access_log->setData(
  81      array(
  82        'U' => (string)$request->getRequestURI()->getPath(),
  83        'C' => get_class($controller),
  84      ));
  85  
  86    // If execution throws an exception and then trying to render that exception
  87    // throws another exception, we want to show the original exception, as it is
  88    // likely the root cause of the rendering exception.
  89    $original_exception = null;
  90    try {
  91      $response = $controller->willBeginExecution();
  92  
  93      if ($request->getUser() && $request->getUser()->getPHID()) {
  94        $access_log->setData(
  95          array(
  96            'u' => $request->getUser()->getUserName(),
  97            'P' => $request->getUser()->getPHID(),
  98          ));
  99      }
 100  
 101      if (!$response) {
 102        $controller->willProcessRequest($uri_data);
 103        $response = $controller->handleRequest($request);
 104      }
 105    } catch (Exception $ex) {
 106      $original_exception = $ex;
 107      $response = $application->handleException($ex);
 108    }
 109  
 110    try {
 111      $response = $controller->didProcessRequest($response);
 112      $response = $application->willSendResponse($response, $controller);
 113      $response->setRequest($request);
 114  
 115      $unexpected_output = PhabricatorStartup::endOutputCapture();
 116      if ($unexpected_output) {
 117        $unexpected_output = "Unexpected output:\n\n{$unexpected_output}";
 118        phlog($unexpected_output);
 119  
 120        if ($response instanceof AphrontWebpageResponse) {
 121          echo phutil_tag(
 122            'div',
 123            array('style' =>
 124              'background: #eeddff;'.
 125              'white-space: pre-wrap;'.
 126              'z-index: 200000;'.
 127              'position: relative;'.
 128              'padding: 8px;'.
 129              'font-family: monospace',
 130            ),
 131            $unexpected_output);
 132        }
 133      }
 134  
 135      $sink->writeResponse($response);
 136    } catch (Exception $ex) {
 137      $write_guard->dispose();
 138      $access_log->write();
 139      if ($original_exception) {
 140        $ex = new PhutilAggregateException(
 141          'Multiple exceptions during processing and rendering.',
 142          array(
 143            $original_exception,
 144            $ex,
 145          ));
 146      }
 147      PhabricatorStartup::didEncounterFatalException(
 148        'Rendering Exception',
 149        $ex,
 150        $show_unexpected_traces);
 151    }
 152  
 153    $write_guard->dispose();
 154  
 155    $access_log->setData(
 156      array(
 157        'c' => $response->getHTTPResponseCode(),
 158        'T' => PhabricatorStartup::getMicrosecondsSinceStart(),
 159      ));
 160  
 161    DarkConsoleXHProfPluginAPI::saveProfilerSample($access_log);
 162  
 163    // Add points to the rate limits for this request.
 164    if (isset($_SERVER['REMOTE_ADDR'])) {
 165      $user_ip = $_SERVER['REMOTE_ADDR'];
 166  
 167      // The base score for a request allows users to make 30 requests per
 168      // minute.
 169      $score = (1000 / 30);
 170  
 171      // If the user was logged in, let them make more requests.
 172      if ($request->getUser() && $request->getUser()->getPHID()) {
 173        $score = $score / 5;
 174      }
 175  
 176      PhabricatorStartup::addRateLimitScore($user_ip, $score);
 177    }
 178  
 179  } catch (Exception $ex) {
 180    PhabricatorStartup::didEncounterFatalException(
 181      'Core Exception',
 182      $ex,
 183      $show_unexpected_traces);
 184  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1