Skip to content

Commit 262be61

Browse files
author
epriestley
committedJun 29, 2015
Merge branch 'master' into redesign-2015
2 parents 626c7bc + 6b7183a commit 262be61

14 files changed

+392
-113
lines changed
 

‎src/__phutil_library_map__.php

+16
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@
159159
'AphrontRequestTestCase' => 'aphront/__tests__/AphrontRequestTestCase.php',
160160
'AphrontResponse' => 'aphront/response/AphrontResponse.php',
161161
'AphrontSideNavFilterView' => 'view/layout/AphrontSideNavFilterView.php',
162+
'AphrontSite' => 'aphront/site/AphrontSite.php',
162163
'AphrontStackTraceView' => 'view/widget/AphrontStackTraceView.php',
163164
'AphrontStandaloneHTMLResponse' => 'aphront/response/AphrontStandaloneHTMLResponse.php',
164165
'AphrontTableView' => 'view/control/AphrontTableView.php',
@@ -1660,6 +1661,7 @@
16601661
'PhabricatorConfigSchemaQuery' => 'applications/config/schema/PhabricatorConfigSchemaQuery.php',
16611662
'PhabricatorConfigSchemaSpec' => 'applications/config/schema/PhabricatorConfigSchemaSpec.php',
16621663
'PhabricatorConfigServerSchema' => 'applications/config/schema/PhabricatorConfigServerSchema.php',
1664+
'PhabricatorConfigSiteModuleController' => 'applications/config/controller/PhabricatorConfigSiteModuleController.php',
16631665
'PhabricatorConfigSiteSource' => 'infrastructure/env/PhabricatorConfigSiteSource.php',
16641666
'PhabricatorConfigSource' => 'infrastructure/env/PhabricatorConfigSource.php',
16651667
'PhabricatorConfigStackSource' => 'infrastructure/env/PhabricatorConfigStackSource.php',
@@ -2328,6 +2330,7 @@
23282330
'PhabricatorPhrequentConfigOptions' => 'applications/phrequent/config/PhabricatorPhrequentConfigOptions.php',
23292331
'PhabricatorPhrictionApplication' => 'applications/phriction/application/PhabricatorPhrictionApplication.php',
23302332
'PhabricatorPhrictionConfigOptions' => 'applications/phriction/config/PhabricatorPhrictionConfigOptions.php',
2333+
'PhabricatorPlatformSite' => 'aphront/site/PhabricatorPlatformSite.php',
23312334
'PhabricatorPolicies' => 'applications/policy/constants/PhabricatorPolicies.php',
23322335
'PhabricatorPolicy' => 'applications/policy/storage/PhabricatorPolicy.php',
23332336
'PhabricatorPolicyApplication' => 'applications/policy/application/PhabricatorPolicyApplication.php',
@@ -2525,6 +2528,7 @@
25252528
'PhabricatorRepositoryURITestCase' => 'applications/repository/storage/__tests__/PhabricatorRepositoryURITestCase.php',
25262529
'PhabricatorRepositoryVCSPassword' => 'applications/repository/storage/PhabricatorRepositoryVCSPassword.php',
25272530
'PhabricatorRepositoryVersion' => 'applications/repository/constants/PhabricatorRepositoryVersion.php',
2531+
'PhabricatorResourceSite' => 'aphront/site/PhabricatorResourceSite.php',
25282532
'PhabricatorRobotsController' => 'applications/system/controller/PhabricatorRobotsController.php',
25292533
'PhabricatorS3FileStorageEngine' => 'applications/files/engine/PhabricatorS3FileStorageEngine.php',
25302534
'PhabricatorSMS' => 'infrastructure/sms/storage/PhabricatorSMS.php',
@@ -2614,6 +2618,7 @@
26142618
'PhabricatorSetupIssue' => 'applications/config/issue/PhabricatorSetupIssue.php',
26152619
'PhabricatorSetupIssueUIExample' => 'applications/uiexample/examples/PhabricatorSetupIssueUIExample.php',
26162620
'PhabricatorSetupIssueView' => 'applications/config/view/PhabricatorSetupIssueView.php',
2621+
'PhabricatorSite' => 'aphront/site/PhabricatorSite.php',
26172622
'PhabricatorSlowvoteApplication' => 'applications/slowvote/application/PhabricatorSlowvoteApplication.php',
26182623
'PhabricatorSlowvoteChoice' => 'applications/slowvote/storage/PhabricatorSlowvoteChoice.php',
26192624
'PhabricatorSlowvoteCloseController' => 'applications/slowvote/controller/PhabricatorSlowvoteCloseController.php',
@@ -2892,6 +2897,8 @@
28922897
'PhameBlogListController' => 'applications/phame/controller/blog/PhameBlogListController.php',
28932898
'PhameBlogLiveController' => 'applications/phame/controller/blog/PhameBlogLiveController.php',
28942899
'PhameBlogQuery' => 'applications/phame/query/PhameBlogQuery.php',
2900+
'PhameBlogResourceSite' => 'applications/phame/site/PhameBlogResourceSite.php',
2901+
'PhameBlogSite' => 'applications/phame/site/PhameBlogSite.php',
28952902
'PhameBlogSkin' => 'applications/phame/skins/PhameBlogSkin.php',
28962903
'PhameBlogTransaction' => 'applications/phame/storage/PhameBlogTransaction.php',
28972904
'PhameBlogViewController' => 'applications/phame/controller/blog/PhameBlogViewController.php',
@@ -2920,6 +2927,7 @@
29202927
'PhameQueryPostsConduitAPIMethod' => 'applications/phame/conduit/PhameQueryPostsConduitAPIMethod.php',
29212928
'PhameResourceController' => 'applications/phame/controller/PhameResourceController.php',
29222929
'PhameSchemaSpec' => 'applications/phame/storage/PhameSchemaSpec.php',
2930+
'PhameSite' => 'applications/phame/site/PhameSite.php',
29232931
'PhameSkinSpecification' => 'applications/phame/skins/PhameSkinSpecification.php',
29242932
'PhluxController' => 'applications/phlux/controller/PhluxController.php',
29252933
'PhluxDAO' => 'applications/phlux/storage/PhluxDAO.php',
@@ -3525,6 +3533,7 @@
35253533
'AphrontRequestTestCase' => 'PhabricatorTestCase',
35263534
'AphrontResponse' => 'Phobject',
35273535
'AphrontSideNavFilterView' => 'AphrontView',
3536+
'AphrontSite' => 'Phobject',
35283537
'AphrontStackTraceView' => 'AphrontView',
35293538
'AphrontStandaloneHTMLResponse' => 'AphrontHTMLResponse',
35303539
'AphrontTableView' => 'AphrontView',
@@ -5270,6 +5279,7 @@
52705279
'PhabricatorConfigSchemaQuery' => 'Phobject',
52715280
'PhabricatorConfigSchemaSpec' => 'Phobject',
52725281
'PhabricatorConfigServerSchema' => 'PhabricatorConfigStorageSchema',
5282+
'PhabricatorConfigSiteModuleController' => 'PhabricatorConfigController',
52735283
'PhabricatorConfigSiteSource' => 'PhabricatorConfigProxySource',
52745284
'PhabricatorConfigSource' => 'Phobject',
52755285
'PhabricatorConfigStackSource' => 'PhabricatorConfigSource',
@@ -6028,6 +6038,7 @@
60286038
'PhabricatorPhrequentConfigOptions' => 'PhabricatorApplicationConfigOptions',
60296039
'PhabricatorPhrictionApplication' => 'PhabricatorApplication',
60306040
'PhabricatorPhrictionConfigOptions' => 'PhabricatorApplicationConfigOptions',
6041+
'PhabricatorPlatformSite' => 'PhabricatorSite',
60316042
'PhabricatorPolicies' => 'PhabricatorPolicyConstants',
60326043
'PhabricatorPolicy' => array(
60336044
'PhabricatorPolicyDAO',
@@ -6296,6 +6307,7 @@
62966307
'PhabricatorRepositoryURITestCase' => 'PhabricatorTestCase',
62976308
'PhabricatorRepositoryVCSPassword' => 'PhabricatorRepositoryDAO',
62986309
'PhabricatorRepositoryVersion' => 'Phobject',
6310+
'PhabricatorResourceSite' => 'PhabricatorSite',
62996311
'PhabricatorRobotsController' => 'PhabricatorController',
63006312
'PhabricatorS3FileStorageEngine' => 'PhabricatorFileStorageEngine',
63016313
'PhabricatorSMS' => 'PhabricatorSMSDAO',
@@ -6387,6 +6399,7 @@
63876399
'PhabricatorSetupIssue' => 'Phobject',
63886400
'PhabricatorSetupIssueUIExample' => 'PhabricatorUIExample',
63896401
'PhabricatorSetupIssueView' => 'AphrontView',
6402+
'PhabricatorSite' => 'AphrontSite',
63906403
'PhabricatorSlowvoteApplication' => 'PhabricatorApplication',
63916404
'PhabricatorSlowvoteChoice' => 'PhabricatorSlowvoteDAO',
63926405
'PhabricatorSlowvoteCloseController' => 'PhabricatorSlowvoteController',
@@ -6711,6 +6724,8 @@
67116724
'PhameBlogListController' => 'PhameController',
67126725
'PhameBlogLiveController' => 'PhameController',
67136726
'PhameBlogQuery' => 'PhabricatorCursorPagedPolicyAwareQuery',
6727+
'PhameBlogResourceSite' => 'PhameSite',
6728+
'PhameBlogSite' => 'PhameSite',
67146729
'PhameBlogSkin' => 'PhabricatorController',
67156730
'PhameBlogTransaction' => 'PhabricatorApplicationTransaction',
67166731
'PhameBlogViewController' => 'PhameController',
@@ -6745,6 +6760,7 @@
67456760
'PhameQueryPostsConduitAPIMethod' => 'PhameConduitAPIMethod',
67466761
'PhameResourceController' => 'CelerityResourceController',
67476762
'PhameSchemaSpec' => 'PhabricatorConfigSchemaSpec',
6763+
'PhameSite' => 'PhabricatorSite',
67486764
'PhameSkinSpecification' => 'Phobject',
67496765
'PhluxController' => 'PhabricatorController',
67506766
'PhluxDAO' => 'PhabricatorLiskDAO',

‎src/aphront/configuration/AphrontApplicationConfiguration.php

+31-77
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,9 @@ final public function buildController() {
350350
}
351351
}
352352

353-
if (PhabricatorEnv::getEnvConfig('security.require-https')) {
353+
$site = $this->buildSiteForRequest($request);
354+
355+
if ($site->shouldRequireHTTPS()) {
354356
if (!$request->isHTTPS()) {
355357
$https_uri = $request->getRequestURI();
356358
$https_uri->setDomain($request->getHost());
@@ -362,82 +364,9 @@ final public function buildController() {
362364
}
363365
}
364366

365-
$path = $request->getPath();
366-
$host = $request->getHost();
367-
$base_uri = PhabricatorEnv::getEnvConfig('phabricator.base-uri');
368-
$prod_uri = PhabricatorEnv::getEnvConfig('phabricator.production-uri');
369-
$file_uri = PhabricatorEnv::getEnvConfig(
370-
'security.alternate-file-domain');
371-
$allowed_uris = PhabricatorEnv::getEnvConfig('phabricator.allowed-uris');
372-
373-
$uris = array_merge(
374-
array(
375-
$base_uri,
376-
$prod_uri,
377-
),
378-
$allowed_uris);
379-
380-
$cdn_routes = array(
381-
'/res/',
382-
'/file/data/',
383-
'/file/xform/',
384-
'/phame/r/',
385-
);
386-
387-
$host_match = false;
388-
foreach ($uris as $uri) {
389-
if ($host === id(new PhutilURI($uri))->getDomain()) {
390-
$host_match = true;
391-
break;
392-
}
393-
}
394-
395-
if (!$host_match) {
396-
if ($host === id(new PhutilURI($file_uri))->getDomain()) {
397-
foreach ($cdn_routes as $route) {
398-
if (strncmp($path, $route, strlen($route)) == 0) {
399-
$host_match = true;
400-
break;
401-
}
402-
}
403-
}
404-
}
405-
406-
// NOTE: If the base URI isn't defined yet, don't activate alternate
407-
// domains.
408-
if ($base_uri && !$host_match) {
409-
410-
try {
411-
$blog = id(new PhameBlogQuery())
412-
->setViewer(new PhabricatorUser())
413-
->withDomain($host)
414-
->executeOne();
415-
} catch (PhabricatorPolicyException $ex) {
416-
throw new Exception(
417-
pht(
418-
'This blog is not visible to logged out users, so it can not be '.
419-
'visited from a custom domain.'));
420-
}
421-
422-
if (!$blog) {
423-
if ($prod_uri && $prod_uri != $base_uri) {
424-
$prod_str = pht('%s or %s', $base_uri, $prod_uri);
425-
} else {
426-
$prod_str = $base_uri;
427-
}
428-
throw new Exception(
429-
pht(
430-
'Specified domain %s is not configured for Phabricator '.
431-
'requests. Please use %s to visit this instance.',
432-
$host,
433-
$prod_str));
434-
}
435-
436-
// TODO: Make this more flexible and modular so any application can
437-
// do crazy stuff here if it wants.
438-
439-
$path = '/phame/live/'.$blog->getID().'/'.$path;
440-
}
367+
// TODO: Really, the Site should get more control here and be able to
368+
// do its own routing logic if it wants, but we don't need that for now.
369+
$path = $site->getPathForRouting($request);
441370

442371
list($controller, $uri_data) = $this->buildControllerForPath($path);
443372
if (!$controller) {
@@ -509,4 +438,29 @@ final public function buildControllerForPath($path) {
509438
return array($controller, $uri_data);
510439
}
511440

441+
private function buildSiteForRequest(AphrontRequest $request) {
442+
$sites = PhabricatorSite::getAllSites();
443+
444+
$site = null;
445+
foreach ($sites as $candidate) {
446+
$site = $candidate->newSiteForRequest($request);
447+
if ($site) {
448+
break;
449+
}
450+
}
451+
452+
if (!$site) {
453+
$path = $request->getPath();
454+
$host = $request->getHost();
455+
throw new Exception(
456+
pht(
457+
'This request asked for "%s" on host "%s", but no site is '.
458+
'configured which can serve this request.',
459+
$path,
460+
$host));
461+
}
462+
463+
return $site;
464+
}
465+
512466
}

‎src/aphront/site/AphrontSite.php

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
abstract class AphrontSite extends Phobject {
4+
5+
abstract public function getPriority();
6+
abstract public function getDescription();
7+
8+
abstract public function shouldRequireHTTPS();
9+
abstract public function newSiteForRequest(AphrontRequest $request);
10+
11+
/**
12+
* NOTE: This is temporary glue; eventually, sites will return an entire
13+
* route map.
14+
*/
15+
public function getPathForRouting(AphrontRequest $request) {
16+
return $request->getPath();
17+
}
18+
19+
protected function isHostMatch($host, array $uris) {
20+
foreach ($uris as $uri) {
21+
if (!strlen($uri)) {
22+
continue;
23+
}
24+
25+
$domain = id(new PhutilURI($uri))->getDomain();
26+
27+
if ($domain === $host) {
28+
return true;
29+
}
30+
}
31+
32+
return false;
33+
}
34+
35+
protected function isPathPrefixMatch($path, array $paths) {
36+
foreach ($paths as $candidate) {
37+
if (strncmp($path, $candidate, strlen($candidate)) === 0) {
38+
return true;
39+
}
40+
}
41+
42+
return false;
43+
}
44+
45+
final public static function getAllSites() {
46+
return id(new PhutilClassMapQuery())
47+
->setAncestorClass(__CLASS__)
48+
->setSortMethod('getPriority')
49+
->execute();
50+
}
51+
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
final class PhabricatorPlatformSite extends PhabricatorSite {
4+
5+
public function getDescription() {
6+
return pht('Serves the core platform and applications.');
7+
}
8+
9+
public function getPriority() {
10+
return 1000;
11+
}
12+
13+
public function newSiteForRequest(AphrontRequest $request) {
14+
$uris = array();
15+
$uris[] = PhabricatorEnv::getEnvConfig('phabricator.base-uri');
16+
$uris[] = PhabricatorEnv::getEnvConfig('phabricator.production-uri');
17+
18+
$allowed = PhabricatorEnv::getEnvConfig('phabricator.allowed-uris');
19+
if ($allowed) {
20+
foreach ($allowed as $uri) {
21+
$uris[] = $uri;
22+
}
23+
}
24+
25+
$host = $request->getHost();
26+
if ($this->isHostMatch($host, $uris)) {
27+
return new PhabricatorPlatformSite();
28+
}
29+
30+
return null;
31+
}
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
final class PhabricatorResourceSite extends PhabricatorSite {
4+
5+
public function getDescription() {
6+
return pht('Serves static resources like images, CSS and JS.');
7+
}
8+
9+
public function getPriority() {
10+
return 2000;
11+
}
12+
13+
public function newSiteForRequest(AphrontRequest $request) {
14+
$host = $request->getHost();
15+
16+
$uri = PhabricatorEnv::getEnvConfig('security.alternate-file-domain');
17+
if (!strlen($uri)) {
18+
return null;
19+
}
20+
21+
if ($this->isHostMatch($host, array($uri))) {
22+
return new PhabricatorResourceSite();
23+
}
24+
25+
// These are CDN routes, so we let them through even if the "Host" header
26+
// doesn't match anything we recognize. The
27+
$whitelist = array(
28+
'/res/',
29+
'/file/data/',
30+
'/file/xform/',
31+
);
32+
33+
$path = $request->getPath();
34+
if ($this->isPathPrefixMatch($path, $whitelist)) {
35+
return new PhabricatorResourceSite();
36+
}
37+
38+
return null;
39+
}
40+
41+
}

‎src/aphront/site/PhabricatorSite.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
abstract class PhabricatorSite extends AphrontSite {
4+
5+
public function shouldRequireHTTPS() {
6+
return PhabricatorEnv::getEnvConfig('security.require-https');
7+
}
8+
9+
}

‎src/applications/config/application/PhabricatorConfigApplication.php

+3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ public function getRoutes() {
5656
'(?P<key>[^/]+)/' => 'PhabricatorConfigIssueViewController',
5757
),
5858
'cache/' => 'PhabricatorConfigCacheController',
59+
'module/' => array(
60+
'sites/' => 'PhabricatorConfigSiteModuleController',
61+
),
5962
),
6063
);
6164
}

‎src/applications/config/controller/PhabricatorConfigController.php

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public function buildSideNavView($filter = null, $for_app = false) {
2424
$nav->addFilter('cache/', pht('Cache Status'));
2525
$nav->addLabel(pht('Welcome'));
2626
$nav->addFilter('welcome/', pht('Welcome Screen'));
27+
$nav->addLabel(pht('Modules'));
28+
$nav->addFilter('module/sites/', pht('Sites'));
2729

2830
return $nav;
2931
}

0 commit comments

Comments
 (0)
Failed to load comments.