Exemplo n.º 1
0
 PhabricatorEnv::setEnvConfig($conf);
 // This needs to be done before we create the log, because
 // PhabricatorAccessLog::getLog() calls date()
 $tz = PhabricatorEnv::getEnvConfig('phabricator.timezone');
 if ($tz) {
     date_default_timezone_set($tz);
 }
 // Append any paths to $PATH if we need to.
 $paths = PhabricatorEnv::getEnvConfig('environment.append-paths');
 if (!empty($paths)) {
     $current_env_path = getenv('PATH');
     $new_env_paths = implode(':', $paths);
     putenv('PATH=' . $current_env_path . ':' . $new_env_paths);
 }
 // This is the earliest we can get away with this, we need env config first.
 PhabricatorAccessLog::init();
 $access_log = PhabricatorAccessLog::getLog();
 if ($access_log) {
     $access_log->setData(array('R' => idx($_SERVER, 'HTTP_REFERER', '-'), 'r' => idx($_SERVER, 'REMOTE_ADDR', '-'), 'M' => idx($_SERVER, 'REQUEST_METHOD', '-')));
 }
 DarkConsoleXHProfPluginAPI::hookProfiler();
 PhutilErrorHandler::initialize();
 PhutilErrorHandler::setErrorListener(array('DarkConsoleErrorLogPluginAPI', 'handleErrors'));
 foreach (PhabricatorEnv::getEnvConfig('load-libraries') as $library) {
     phutil_load_library($library);
 }
 if (PhabricatorEnv::getEnvConfig('phabricator.setup')) {
     try {
         PhabricatorSetup::runSetup();
     } catch (Exception $ex) {
         echo "EXCEPTION!\n";
 /**
  * @phutil-external-symbol class PhabricatorStartup
  */
 public static function runHTTPRequest(AphrontHTTPSink $sink)
 {
     $multimeter = MultimeterControl::newInstance();
     $multimeter->setEventContext('<http-init>');
     $multimeter->setEventViewer('<none>');
     // Build a no-op write guard for the setup phase. We'll replace this with a
     // real write guard later on, but we need to survive setup and build a
     // request object first.
     $write_guard = new AphrontWriteGuard('id');
     PhabricatorEnv::initializeWebEnvironment();
     $multimeter->setSampleRate(PhabricatorEnv::getEnvConfig('debug.sample-rate'));
     $debug_time_limit = PhabricatorEnv::getEnvConfig('debug.time-limit');
     if ($debug_time_limit) {
         PhabricatorStartup::setDebugTimeLimit($debug_time_limit);
     }
     // This is the earliest we can get away with this, we need env config first.
     PhabricatorAccessLog::init();
     $access_log = PhabricatorAccessLog::getLog();
     PhabricatorStartup::setAccessLog($access_log);
     $access_log->setData(array('R' => AphrontRequest::getHTTPHeader('Referer', '-'), 'r' => idx($_SERVER, 'REMOTE_ADDR', '-'), 'M' => idx($_SERVER, 'REQUEST_METHOD', '-')));
     DarkConsoleXHProfPluginAPI::hookProfiler();
     DarkConsoleErrorLogPluginAPI::registerErrorHandler();
     $response = PhabricatorSetupCheck::willProcessRequest();
     if ($response) {
         PhabricatorStartup::endOutputCapture();
         $sink->writeResponse($response);
         return;
     }
     $host = AphrontRequest::getHTTPHeader('Host');
     $path = $_REQUEST['__path__'];
     switch ($host) {
         default:
             $config_key = 'aphront.default-application-configuration-class';
             $application = PhabricatorEnv::newObjectFromConfig($config_key);
             break;
     }
     $application->setHost($host);
     $application->setPath($path);
     $application->willBuildRequest();
     $request = $application->buildRequest();
     // Now that we have a request, convert the write guard into one which
     // actually checks CSRF tokens.
     $write_guard->dispose();
     $write_guard = new AphrontWriteGuard(array($request, 'validateCSRF'));
     // Build the server URI implied by the request headers. If an administrator
     // has not configured "phabricator.base-uri" yet, we'll use this to generate
     // links.
     $request_protocol = $request->isHTTPS() ? 'https' : 'http';
     $request_base_uri = "{$request_protocol}://{$host}/";
     PhabricatorEnv::setRequestBaseURI($request_base_uri);
     $access_log->setData(array('U' => (string) $request->getRequestURI()->getPath()));
     $processing_exception = null;
     try {
         $response = $application->processRequest($request, $access_log, $sink, $multimeter);
         $response_code = $response->getHTTPResponseCode();
     } catch (Exception $ex) {
         $processing_exception = $ex;
         $response_code = 500;
     }
     $write_guard->dispose();
     $access_log->setData(array('c' => $response_code, 'T' => PhabricatorStartup::getMicrosecondsSinceStart()));
     $multimeter->newEvent(MultimeterEvent::TYPE_REQUEST_TIME, $multimeter->getEventContext(), PhabricatorStartup::getMicrosecondsSinceStart());
     $access_log->write();
     $multimeter->saveEvents();
     DarkConsoleXHProfPluginAPI::saveProfilerSample($access_log);
     // Add points to the rate limits for this request.
     if (isset($_SERVER['REMOTE_ADDR'])) {
         $user_ip = $_SERVER['REMOTE_ADDR'];
         // The base score for a request allows users to make 30 requests per
         // minute.
         $score = 1000 / 30;
         // If the user was logged in, let them make more requests.
         if ($request->getUser() && $request->getUser()->getPHID()) {
             $score = $score / 5;
         }
         PhabricatorStartup::addRateLimitScore($user_ip, $score);
     }
     if ($processing_exception) {
         throw $processing_exception;
     }
 }