| @@ -8,168 +8,168 @@ | ||
| 8 | 8 | |
| 9 | 9 | class MultiDomainDomainTest extends SapphireTest | 
| 10 | 10 |  { | 
| 11 | - /** | |
| 12 | - * Set up some test domain data for testing | |
| 13 | - * | |
| 14 | -     * {@inheritDoc} | |
| 15 | - */ | |
| 16 | - public function setUp() | |
| 17 | -    { | |
| 18 | - parent::setUp(); | |
| 19 | - Config::nest(); | |
| 20 | - | |
| 21 | - Config::inst()->remove(MultiDomain::class, 'domains'); | |
| 22 | - Config::inst()->update(MultiDomain::class, 'domains', array( | |
| 23 | - 'primary' => array( | |
| 24 | - 'hostname' => 'example.com' | |
| 25 | - ), | |
| 26 | - 'store' => array( | |
| 27 | - 'hostname' => 'example-store.com', | |
| 28 | - 'resolves_to' => 'shop/store', | |
| 29 | - 'allow' => array( | |
| 30 | - 'admin/*', | |
| 31 | - 'Security/*', | |
| 32 | - 'my-custom-webhook/' | |
| 33 | - ) | |
| 34 | - ), | |
| 35 | - 'configurable' => array( | |
| 36 | - 'hostname' => 'MY_CONSTANT_HOSTNAME' | |
| 37 | - ), | |
| 38 | - 'forceful' => array( | |
| 39 | - 'hostname' => 'forced.com', | |
| 40 | - 'force' => array( | |
| 41 | - 'buy-now/*' | |
| 42 | - ) | |
| 43 | - ) | |
| 44 | - )); | |
| 45 | - } | |
| 46 | - | |
| 47 | - /** | |
| 48 | - * Test that a hostname defined in a constant will override the default configuration, otherwise the default | |
| 49 | - * configuration for the domain is returned | |
| 50 | - */ | |
| 51 | - public function testGetHostname() | |
| 52 | -    { | |
| 53 | -        $configurableDomain = MultiDomain::get_domain('configurable'); | |
| 54 | -        define('MY_CONSTANT_HOSTNAME', 'I am a constant'); | |
| 55 | -        $this->assertSame('I am a constant', $configurableDomain->getHostname()); | |
| 56 | - | |
| 57 | -        $storeDomain = MultiDomain::get_domain('store'); | |
| 58 | -        $this->assertSame('example-store.com', $storeDomain->getHostname()); | |
| 59 | - } | |
| 60 | - | |
| 61 | - /** | |
| 62 | - * Test that the domain's "resolves to" property is returned for the URL if it is defined, otherwise null | |
| 63 | - */ | |
| 64 | - public function testGetUrl() | |
| 65 | -    { | |
| 66 | -        $primaryDomain = MultiDomain::get_domain('primary'); | |
| 67 | - $this->assertNull($primaryDomain->getURL()); | |
| 68 | - | |
| 69 | -        $storeDomain = MultiDomain::get_domain('store'); | |
| 70 | -        $this->assertSame('shop/store', $storeDomain->getURL()); | |
| 71 | - } | |
| 72 | - | |
| 73 | - /** | |
| 74 | - * Test that a domain can be identified as the primary domain or otherwise | |
| 75 | - */ | |
| 76 | - public function testIsPrimary() | |
| 77 | -    { | |
| 78 | - $this->assertTrue(MultiDomain::get_primary_domain()->isPrimary()); | |
| 79 | -        $this->assertFalse(MultiDomain::get_domain('store')->isPrimary()); | |
| 80 | - } | |
| 81 | - | |
| 82 | - /** | |
| 83 | - * When the request URI matches one of the allowed rules for a domain, the isActive method should return false | |
| 84 | - */ | |
| 85 | - public function testIsActiveReturnsFalseWhenRequestUriIsAllowedPath() | |
| 86 | -    { | |
| 87 | -        $domain = MultiDomain::get_domain('store'); | |
| 88 | -        $domain->setRequestUri('/Security/login'); | |
| 89 | - $this->assertFalse($domain->isActive()); | |
| 90 | - } | |
| 91 | - | |
| 92 | - /** | |
| 93 | - * When a subdomain is "allowed" and is requested, subdomains should be allowed through "isActive" as well | |
| 94 | - * as the primary domain | |
| 95 | - */ | |
| 96 | - public function testSubdomainsAllowedInIsActiveWhenConfigured() | |
| 97 | -    { | |
| 98 | - Config::inst()->update(MultiDomain::class, 'allow_subdomains', true); | |
| 99 | - | |
| 100 | -        $domain = MultiDomain::get_domain('store') | |
| 101 | -            ->setRequestUri('/some/page') | |
| 102 | -            ->setHttpHost('api.example-store.com'); | |
| 103 | - | |
| 104 | - $this->assertTrue($domain->isActive()); | |
| 105 | - } | |
| 106 | - | |
| 107 | - /** | |
| 108 | - * The default behaviour would be that if the current host from the request matchese that of the domain model | |
| 109 | - * then isActive should be true | |
| 110 | - */ | |
| 111 | - public function testReturnActiveIfCurrentHostMatchesDomainsHostname() | |
| 112 | -    { | |
| 113 | -        $domain = MultiDomain::get_domain('primary') | |
| 114 | -            ->setRequestUri('/another/page') | |
| 115 | -            ->setHttpHost('example.com'); | |
| 116 | - | |
| 117 | - $this->assertTrue($domain->isActive()); | |
| 118 | - } | |
| 119 | - | |
| 120 | - /** | |
| 121 | - * getNativeUrl should not be used on the primary domain | |
| 122 | - * | |
| 123 | - * @expectedException Exception | |
| 124 | - * @expectedExceptionMessage Cannot convert a native URL on the primary domain | |
| 125 | - */ | |
| 126 | - public function testGetNativeUrlThrowsExceptionOnPrimaryDomain() | |
| 127 | -    { | |
| 128 | -        MultiDomain::get_primary_domain()->getNativeUrl('foo'); | |
| 129 | - } | |
| 130 | - | |
| 131 | - /** | |
| 132 | - * Test that a URL segment can be added to the domain's URL and returned as a "native URL" | |
| 133 | - */ | |
| 134 | - public function testGetNativeUrl() | |
| 135 | -    { | |
| 136 | -        $domain = MultiDomain::get_domain('store'); | |
| 137 | -        $this->assertSame('shop/store/foo/bar', $domain->getNativeUrl('foo/bar')); | |
| 138 | - } | |
| 139 | - | |
| 140 | - /** | |
| 141 | - * "Allowed" and "forced" URLs should just be returned from getNativeUrl as is | |
| 142 | - */ | |
| 143 | - public function testGetNativeUrlReturnsInputWhenUrlIsAllowedOrForced() | |
| 144 | -    { | |
| 145 | -        $domain = MultiDomain::get_domain('store'); | |
| 146 | -        $this->assertSame('my-custom-webhook/', $domain->getNativeUrl('my-custom-webhook/')); | |
| 147 | - | |
| 148 | -        $domain = MultiDomain::get_domain('forceful'); | |
| 149 | -        $this->assertSame('buy-now/whatever', $domain->getNativeUrl('buy-now/whatever')); | |
| 150 | - } | |
| 151 | - | |
| 152 | - /** | |
| 153 | - * The primary domain and "allowed" route matches should be returned as it | |
| 154 | - */ | |
| 155 | - public function testGetVanityUrlReturnsInputWhenUrlIsAllowedOrIsPrimaryDomain() | |
| 156 | -    { | |
| 157 | -        $this->assertSame('/pages/info', MultiDomain::get_primary_domain()->getVanityUrl('/pages/info')); | |
| 158 | -        $this->assertSame('/Security/login', MultiDomain::get_domain('store')->getVanityUrl('/Security/login')); | |
| 159 | - } | |
| 160 | - | |
| 161 | - /** | |
| 162 | - * Non-primary domains and un-allowed route matches should be returned without their URL for vanity | |
| 163 | - */ | |
| 164 | - public function testGetVanityUrl() | |
| 165 | -    { | |
| 166 | -        $this->assertSame('partners/', MultiDomain::get_domain('store')->getVanityUrl('shop/store/partners/')); | |
| 167 | -        $this->assertSame('foo/bar', MultiDomain::get_domain('store')->getVanityUrl('shop/store/foo/bar')); | |
| 168 | - } | |
| 169 | - | |
| 170 | - public function tearDown() | |
| 171 | -    { | |
| 172 | - Config::unnest(); | |
| 173 | - parent::tearDown(); | |
| 174 | - } | |
| 11 | + /** | |
| 12 | + * Set up some test domain data for testing | |
| 13 | + * | |
| 14 | +	 * {@inheritDoc} | |
| 15 | + */ | |
| 16 | + public function setUp() | |
| 17 | +	{ | |
| 18 | + parent::setUp(); | |
| 19 | + Config::nest(); | |
| 20 | + | |
| 21 | + Config::inst()->remove(MultiDomain::class, 'domains'); | |
| 22 | + Config::inst()->update(MultiDomain::class, 'domains', array( | |
| 23 | + 'primary' => array( | |
| 24 | + 'hostname' => 'example.com' | |
| 25 | + ), | |
| 26 | + 'store' => array( | |
| 27 | + 'hostname' => 'example-store.com', | |
| 28 | + 'resolves_to' => 'shop/store', | |
| 29 | + 'allow' => array( | |
| 30 | + 'admin/*', | |
| 31 | + 'Security/*', | |
| 32 | + 'my-custom-webhook/' | |
| 33 | + ) | |
| 34 | + ), | |
| 35 | + 'configurable' => array( | |
| 36 | + 'hostname' => 'MY_CONSTANT_HOSTNAME' | |
| 37 | + ), | |
| 38 | + 'forceful' => array( | |
| 39 | + 'hostname' => 'forced.com', | |
| 40 | + 'force' => array( | |
| 41 | + 'buy-now/*' | |
| 42 | + ) | |
| 43 | + ) | |
| 44 | + )); | |
| 45 | + } | |
| 46 | + | |
| 47 | + /** | |
| 48 | + * Test that a hostname defined in a constant will override the default configuration, otherwise the default | |
| 49 | + * configuration for the domain is returned | |
| 50 | + */ | |
| 51 | + public function testGetHostname() | |
| 52 | +	{ | |
| 53 | +		$configurableDomain = MultiDomain::get_domain('configurable'); | |
| 54 | +		define('MY_CONSTANT_HOSTNAME', 'I am a constant'); | |
| 55 | +		$this->assertSame('I am a constant', $configurableDomain->getHostname()); | |
| 56 | + | |
| 57 | +		$storeDomain = MultiDomain::get_domain('store'); | |
| 58 | +		$this->assertSame('example-store.com', $storeDomain->getHostname()); | |
| 59 | + } | |
| 60 | + | |
| 61 | + /** | |
| 62 | + * Test that the domain's "resolves to" property is returned for the URL if it is defined, otherwise null | |
| 63 | + */ | |
| 64 | + public function testGetUrl() | |
| 65 | +	{ | |
| 66 | +		$primaryDomain = MultiDomain::get_domain('primary'); | |
| 67 | + $this->assertNull($primaryDomain->getURL()); | |
| 68 | + | |
| 69 | +		$storeDomain = MultiDomain::get_domain('store'); | |
| 70 | +		$this->assertSame('shop/store', $storeDomain->getURL()); | |
| 71 | + } | |
| 72 | + | |
| 73 | + /** | |
| 74 | + * Test that a domain can be identified as the primary domain or otherwise | |
| 75 | + */ | |
| 76 | + public function testIsPrimary() | |
| 77 | +	{ | |
| 78 | + $this->assertTrue(MultiDomain::get_primary_domain()->isPrimary()); | |
| 79 | +		$this->assertFalse(MultiDomain::get_domain('store')->isPrimary()); | |
| 80 | + } | |
| 81 | + | |
| 82 | + /** | |
| 83 | + * When the request URI matches one of the allowed rules for a domain, the isActive method should return false | |
| 84 | + */ | |
| 85 | + public function testIsActiveReturnsFalseWhenRequestUriIsAllowedPath() | |
| 86 | +	{ | |
| 87 | +		$domain = MultiDomain::get_domain('store'); | |
| 88 | +		$domain->setRequestUri('/Security/login'); | |
| 89 | + $this->assertFalse($domain->isActive()); | |
| 90 | + } | |
| 91 | + | |
| 92 | + /** | |
| 93 | + * When a subdomain is "allowed" and is requested, subdomains should be allowed through "isActive" as well | |
| 94 | + * as the primary domain | |
| 95 | + */ | |
| 96 | + public function testSubdomainsAllowedInIsActiveWhenConfigured() | |
| 97 | +	{ | |
| 98 | + Config::inst()->update(MultiDomain::class, 'allow_subdomains', true); | |
| 99 | + | |
| 100 | +		$domain = MultiDomain::get_domain('store') | |
| 101 | +			->setRequestUri('/some/page') | |
| 102 | +			->setHttpHost('api.example-store.com'); | |
| 103 | + | |
| 104 | + $this->assertTrue($domain->isActive()); | |
| 105 | + } | |
| 106 | + | |
| 107 | + /** | |
| 108 | + * The default behaviour would be that if the current host from the request matchese that of the domain model | |
| 109 | + * then isActive should be true | |
| 110 | + */ | |
| 111 | + public function testReturnActiveIfCurrentHostMatchesDomainsHostname() | |
| 112 | +	{ | |
| 113 | +		$domain = MultiDomain::get_domain('primary') | |
| 114 | +			->setRequestUri('/another/page') | |
| 115 | +			->setHttpHost('example.com'); | |
| 116 | + | |
| 117 | + $this->assertTrue($domain->isActive()); | |
| 118 | + } | |
| 119 | + | |
| 120 | + /** | |
| 121 | + * getNativeUrl should not be used on the primary domain | |
| 122 | + * | |
| 123 | + * @expectedException Exception | |
| 124 | + * @expectedExceptionMessage Cannot convert a native URL on the primary domain | |
| 125 | + */ | |
| 126 | + public function testGetNativeUrlThrowsExceptionOnPrimaryDomain() | |
| 127 | +	{ | |
| 128 | +		MultiDomain::get_primary_domain()->getNativeUrl('foo'); | |
| 129 | + } | |
| 130 | + | |
| 131 | + /** | |
| 132 | + * Test that a URL segment can be added to the domain's URL and returned as a "native URL" | |
| 133 | + */ | |
| 134 | + public function testGetNativeUrl() | |
| 135 | +	{ | |
| 136 | +		$domain = MultiDomain::get_domain('store'); | |
| 137 | +		$this->assertSame('shop/store/foo/bar', $domain->getNativeUrl('foo/bar')); | |
| 138 | + } | |
| 139 | + | |
| 140 | + /** | |
| 141 | + * "Allowed" and "forced" URLs should just be returned from getNativeUrl as is | |
| 142 | + */ | |
| 143 | + public function testGetNativeUrlReturnsInputWhenUrlIsAllowedOrForced() | |
| 144 | +	{ | |
| 145 | +		$domain = MultiDomain::get_domain('store'); | |
| 146 | +		$this->assertSame('my-custom-webhook/', $domain->getNativeUrl('my-custom-webhook/')); | |
| 147 | + | |
| 148 | +		$domain = MultiDomain::get_domain('forceful'); | |
| 149 | +		$this->assertSame('buy-now/whatever', $domain->getNativeUrl('buy-now/whatever')); | |
| 150 | + } | |
| 151 | + | |
| 152 | + /** | |
| 153 | + * The primary domain and "allowed" route matches should be returned as it | |
| 154 | + */ | |
| 155 | + public function testGetVanityUrlReturnsInputWhenUrlIsAllowedOrIsPrimaryDomain() | |
| 156 | +	{ | |
| 157 | +		$this->assertSame('/pages/info', MultiDomain::get_primary_domain()->getVanityUrl('/pages/info')); | |
| 158 | +		$this->assertSame('/Security/login', MultiDomain::get_domain('store')->getVanityUrl('/Security/login')); | |
| 159 | + } | |
| 160 | + | |
| 161 | + /** | |
| 162 | + * Non-primary domains and un-allowed route matches should be returned without their URL for vanity | |
| 163 | + */ | |
| 164 | + public function testGetVanityUrl() | |
| 165 | +	{ | |
| 166 | +		$this->assertSame('partners/', MultiDomain::get_domain('store')->getVanityUrl('shop/store/partners/')); | |
| 167 | +		$this->assertSame('foo/bar', MultiDomain::get_domain('store')->getVanityUrl('shop/store/foo/bar')); | |
| 168 | + } | |
| 169 | + | |
| 170 | + public function tearDown() | |
| 171 | +	{ | |
| 172 | + Config::unnest(); | |
| 173 | + parent::tearDown(); | |
| 174 | + } | |
| 175 | 175 | } | 
| @@ -9,96 +9,96 @@ | ||
| 9 | 9 | |
| 10 | 10 | class MultiDomainTest extends SapphireTest | 
| 11 | 11 |  { | 
| 12 | - /** | |
| 13 | - * Set up some test domain configuration | |
| 14 | - * | |
| 15 | -     * {@inheritDoc} | |
| 16 | - */ | |
| 17 | - public function setUp() | |
| 18 | -    { | |
| 19 | - parent::setUp(); | |
| 20 | - Config::nest(); | |
| 12 | + /** | |
| 13 | + * Set up some test domain configuration | |
| 14 | + * | |
| 15 | +	 * {@inheritDoc} | |
| 16 | + */ | |
| 17 | + public function setUp() | |
| 18 | +	{ | |
| 19 | + parent::setUp(); | |
| 20 | + Config::nest(); | |
| 21 | 21 | |
| 22 | - Config::inst()->remove(MultiDomain::class, 'domains'); | |
| 23 | - Config::inst()->update(MultiDomain::class, 'domains', array( | |
| 24 | - 'primary' => array( | |
| 25 | - 'hostname' => 'foo.bar', | |
| 26 | - 'resolves_to' => 'bar.baz' | |
| 27 | - ), | |
| 28 | - 'secondary' => array( | |
| 29 | - 'hostname' => 'localhost', | |
| 30 | - 'resolves_to' => 'local.dev' | |
| 31 | - ) | |
| 32 | - )); | |
| 33 | - } | |
| 22 | + Config::inst()->remove(MultiDomain::class, 'domains'); | |
| 23 | + Config::inst()->update(MultiDomain::class, 'domains', array( | |
| 24 | + 'primary' => array( | |
| 25 | + 'hostname' => 'foo.bar', | |
| 26 | + 'resolves_to' => 'bar.baz' | |
| 27 | + ), | |
| 28 | + 'secondary' => array( | |
| 29 | + 'hostname' => 'localhost', | |
| 30 | + 'resolves_to' => 'local.dev' | |
| 31 | + ) | |
| 32 | + )); | |
| 33 | + } | |
| 34 | 34 | |
| 35 | - /** | |
| 36 | - * Test that a MultiDomainDomain can be returned from the configured domains | |
| 37 | - */ | |
| 38 | - public function testGetDomain() | |
| 39 | -    { | |
| 40 | -        $this->assertNull(MultiDomain::get_domain('does-not-exist')); | |
| 41 | -        $this->assertInstanceOf(MultiDomainDomain::class, MultiDomain::get_domain('primary')); | |
| 42 | - } | |
| 35 | + /** | |
| 36 | + * Test that a MultiDomainDomain can be returned from the configured domains | |
| 37 | + */ | |
| 38 | + public function testGetDomain() | |
| 39 | +	{ | |
| 40 | +		$this->assertNull(MultiDomain::get_domain('does-not-exist')); | |
| 41 | +		$this->assertInstanceOf(MultiDomainDomain::class, MultiDomain::get_domain('primary')); | |
| 42 | + } | |
| 43 | 43 | |
| 44 | - /** | |
| 45 | - * Test that all domains can be returned, with or without the primary domain | |
| 46 | - * | |
| 47 | - * @dataProvider getAllDomainsProvider | |
| 48 | - * @param bool $withPrimary | |
| 49 | - */ | |
| 50 | - public function testGetAllDomains($withPrimary) | |
| 51 | -    { | |
| 52 | - $result = MultiDomain::get_all_domains($withPrimary); | |
| 53 | -        $this->assertInternalType('array', $result); | |
| 54 | - $this->assertNotEmpty($result); | |
| 44 | + /** | |
| 45 | + * Test that all domains can be returned, with or without the primary domain | |
| 46 | + * | |
| 47 | + * @dataProvider getAllDomainsProvider | |
| 48 | + * @param bool $withPrimary | |
| 49 | + */ | |
| 50 | + public function testGetAllDomains($withPrimary) | |
| 51 | +	{ | |
| 52 | + $result = MultiDomain::get_all_domains($withPrimary); | |
| 53 | +		$this->assertInternalType('array', $result); | |
| 54 | + $this->assertNotEmpty($result); | |
| 55 | 55 | |
| 56 | - $expectedCount = $withPrimary ? 2 : 1; | |
| 57 | - $this->assertCount($expectedCount, $result); | |
| 58 | - } | |
| 56 | + $expectedCount = $withPrimary ? 2 : 1; | |
| 57 | + $this->assertCount($expectedCount, $result); | |
| 58 | + } | |
| 59 | 59 | |
| 60 | - /** | |
| 61 | - * @return array[] | |
| 62 | - */ | |
| 63 | - public function getAllDomainsProvider() | |
| 64 | -    { | |
| 65 | - return array( | |
| 66 | - array(true), | |
| 67 | - array(false) | |
| 68 | - ); | |
| 69 | - } | |
| 60 | + /** | |
| 61 | + * @return array[] | |
| 62 | + */ | |
| 63 | + public function getAllDomainsProvider() | |
| 64 | +	{ | |
| 65 | + return array( | |
| 66 | + array(true), | |
| 67 | + array(false) | |
| 68 | + ); | |
| 69 | + } | |
| 70 | 70 | |
| 71 | - /** | |
| 72 | - * Test that the primary domain can be returned | |
| 73 | - */ | |
| 74 | - public function testGetPrimaryDomain() | |
| 75 | -    { | |
| 76 | - $result = MultiDomain::get_primary_domain(); | |
| 77 | - $this->assertInstanceOf(MultiDomainDomain::class, $result); | |
| 78 | - $this->assertTrue($result->isPrimary()); | |
| 79 | - } | |
| 71 | + /** | |
| 72 | + * Test that the primary domain can be returned | |
| 73 | + */ | |
| 74 | + public function testGetPrimaryDomain() | |
| 75 | +	{ | |
| 76 | + $result = MultiDomain::get_primary_domain(); | |
| 77 | + $this->assertInstanceOf(MultiDomainDomain::class, $result); | |
| 78 | + $this->assertTrue($result->isPrimary()); | |
| 79 | + } | |
| 80 | 80 | |
| 81 | - /** | |
| 82 | - * Test that the correct domain can be returned by a provided URL | |
| 83 | - */ | |
| 84 | - public function testDomainForUrl() | |
| 85 | -    { | |
| 86 | -        $result = MultiDomain::domain_for_url('foo.bar/my-page'); | |
| 87 | - $this->assertInstanceOf(MultiDomainDomain::class, $result); | |
| 88 | -        $this->assertSame('primary', $result->getKey()); | |
| 89 | - } | |
| 81 | + /** | |
| 82 | + * Test that the correct domain can be returned by a provided URL | |
| 83 | + */ | |
| 84 | + public function testDomainForUrl() | |
| 85 | +	{ | |
| 86 | +		$result = MultiDomain::domain_for_url('foo.bar/my-page'); | |
| 87 | + $this->assertInstanceOf(MultiDomainDomain::class, $result); | |
| 88 | +		$this->assertSame('primary', $result->getKey()); | |
| 89 | + } | |
| 90 | 90 | |
| 91 | - /** | |
| 92 | - * Test that if a URL doesn't match any domain then the primary domain is returned | |
| 93 | - */ | |
| 94 | - public function testDomainForUrlDefaultsToPrimaryDomain() | |
| 95 | -    { | |
| 96 | -        $this->assertTrue(MultiDomain::domain_for_url('does-not-exist.com')->isPrimary()); | |
| 97 | - } | |
| 91 | + /** | |
| 92 | + * Test that if a URL doesn't match any domain then the primary domain is returned | |
| 93 | + */ | |
| 94 | + public function testDomainForUrlDefaultsToPrimaryDomain() | |
| 95 | +	{ | |
| 96 | +		$this->assertTrue(MultiDomain::domain_for_url('does-not-exist.com')->isPrimary()); | |
| 97 | + } | |
| 98 | 98 | |
| 99 | - public function tearDown() | |
| 100 | -    { | |
| 101 | - Config::unnest(); | |
| 102 | - parent::tearDown(); | |
| 103 | - } | |
| 99 | + public function tearDown() | |
| 100 | +	{ | |
| 101 | + Config::unnest(); | |
| 102 | + parent::tearDown(); | |
| 103 | + } | |
| 104 | 104 | } | 
| @@ -20,62 +20,62 @@ | ||
| 20 | 20 | class MultiDomainRequestFilter implements RequestFilter | 
| 21 | 21 |  { | 
| 22 | 22 | |
| 23 | - /** | |
| 24 | - * Gets the active domain, and sets its URL to the native one, with a vanity | |
| 25 | - * URL in the request | |
| 26 | - * | |
| 27 | - * @param HTTPRequest $request | |
| 28 | - * @param Session $session | |
| 29 | - * @param DataModel $model | |
| 30 | - */ | |
| 31 | - public function preRequest(HTTPRequest $request, Session $session, DataModel $model) | |
| 32 | -    { | |
| 23 | + /** | |
| 24 | + * Gets the active domain, and sets its URL to the native one, with a vanity | |
| 25 | + * URL in the request | |
| 26 | + * | |
| 27 | + * @param HTTPRequest $request | |
| 28 | + * @param Session $session | |
| 29 | + * @param DataModel $model | |
| 30 | + */ | |
| 31 | + public function preRequest(HTTPRequest $request, Session $session, DataModel $model) | |
| 32 | +	{ | |
| 33 | 33 | |
| 34 | -        if (Director::is_cli()) { | |
| 35 | - return; | |
| 36 | - } | |
| 34 | +		if (Director::is_cli()) { | |
| 35 | + return; | |
| 36 | + } | |
| 37 | 37 | |
| 38 | - // Not the best place for validation, but _config.php is too early. | |
| 39 | -        if (!MultiDomain::get_primary_domain()) { | |
| 40 | - throw new Exception( | |
| 41 | - 'MultiDomain must define a "' . MultiDomain::KEY_PRIMARY . '" domain in the config, under "domains"' | |
| 42 | - ); | |
| 43 | - } | |
| 38 | + // Not the best place for validation, but _config.php is too early. | |
| 39 | +		if (!MultiDomain::get_primary_domain()) { | |
| 40 | + throw new Exception( | |
| 41 | + 'MultiDomain must define a "' . MultiDomain::KEY_PRIMARY . '" domain in the config, under "domains"' | |
| 42 | + ); | |
| 43 | + } | |
| 44 | 44 | |
| 45 | -        foreach (MultiDomain::get_all_domains() as $domain) { | |
| 46 | -            if (!$domain->isActive()) { | |
| 47 | - continue; | |
| 48 | - } | |
| 45 | +		foreach (MultiDomain::get_all_domains() as $domain) { | |
| 46 | +			if (!$domain->isActive()) { | |
| 47 | + continue; | |
| 48 | + } | |
| 49 | 49 | |
| 50 | - $url = $this->createNativeURLForDomain($domain); | |
| 51 | -            $parts = explode('?', $url); | |
| 52 | - $request->setURL($parts[0]); | |
| 53 | - } | |
| 54 | - } | |
| 50 | + $url = $this->createNativeURLForDomain($domain); | |
| 51 | +			$parts = explode('?', $url); | |
| 52 | + $request->setURL($parts[0]); | |
| 53 | + } | |
| 54 | + } | |
| 55 | 55 | |
| 56 | - /** | |
| 57 | - * Post request noop | |
| 58 | - * @param HTTPRequest $request | |
| 59 | - * @param HTTPResponse $response | |
| 60 | - * @param DataModel $model | |
| 61 | - */ | |
| 62 | - public function postRequest(HTTPRequest $request, HTTPResponse $response, DataModel $model) | |
| 63 | -    { | |
| 64 | - } | |
| 56 | + /** | |
| 57 | + * Post request noop | |
| 58 | + * @param HTTPRequest $request | |
| 59 | + * @param HTTPResponse $response | |
| 60 | + * @param DataModel $model | |
| 61 | + */ | |
| 62 | + public function postRequest(HTTPRequest $request, HTTPResponse $response, DataModel $model) | |
| 63 | +	{ | |
| 64 | + } | |
| 65 | 65 | |
| 66 | - /** | |
| 67 | - * Creates a native URL for a domain. This functionality is abstracted so | |
| 68 | - * that other modules can overload it, e.g. translatable modules that | |
| 69 | - * have their own custom URLs. | |
| 70 | - * | |
| 71 | - * @param MultiDomainDomain $domain | |
| 72 | - * @return string | |
| 73 | - */ | |
| 74 | - protected function createNativeURLForDomain(MultiDomainDomain $domain) | |
| 75 | -    { | |
| 76 | - return Controller::join_links( | |
| 77 | - Director::baseURL(), | |
| 78 | - $domain->getNativeURL($domain->getRequestUri()) | |
| 79 | - ); | |
| 80 | - } | |
| 66 | + /** | |
| 67 | + * Creates a native URL for a domain. This functionality is abstracted so | |
| 68 | + * that other modules can overload it, e.g. translatable modules that | |
| 69 | + * have their own custom URLs. | |
| 70 | + * | |
| 71 | + * @param MultiDomainDomain $domain | |
| 72 | + * @return string | |
| 73 | + */ | |
| 74 | + protected function createNativeURLForDomain(MultiDomainDomain $domain) | |
| 75 | +	{ | |
| 76 | + return Controller::join_links( | |
| 77 | + Director::baseURL(), | |
| 78 | + $domain->getNativeURL($domain->getRequestUri()) | |
| 79 | + ); | |
| 80 | + } | |
| 81 | 81 | } | 
| @@ -38,7 +38,7 @@ | ||
| 38 | 38 | // Not the best place for validation, but _config.php is too early. | 
| 39 | 39 |          if (!MultiDomain::get_primary_domain()) { | 
| 40 | 40 | throw new Exception( | 
| 41 | - 'MultiDomain must define a "' . MultiDomain::KEY_PRIMARY . '" domain in the config, under "domains"' | |
| 41 | + 'MultiDomain must define a "'.MultiDomain::KEY_PRIMARY.'" domain in the config, under "domains"' | |
| 42 | 42 | ); | 
| 43 | 43 | } | 
| 44 | 44 | |
| @@ -14,77 +14,77 @@ | ||
| 14 | 14 | */ | 
| 15 | 15 | class MultiDomain extends Object | 
| 16 | 16 |  { | 
| 17 | - /** | |
| 18 | - * The key for the "primary" domain | |
| 19 | - * | |
| 20 | - * @var string | |
| 21 | - */ | |
| 22 | - const KEY_PRIMARY = 'primary'; | |
| 17 | + /** | |
| 18 | + * The key for the "primary" domain | |
| 19 | + * | |
| 20 | + * @var string | |
| 21 | + */ | |
| 22 | + const KEY_PRIMARY = 'primary'; | |
| 23 | 23 | |
| 24 | - /** | |
| 25 | - * Given a url, get the domain that maps to it, e.g. | |
| 26 | - * | |
| 27 | - * /company/ -> silverstripe.com | |
| 28 | - * /company/partners -> silverstripe.com | |
| 29 | - * /community/forum -> silverstripe.org | |
| 30 | - * | |
| 31 | - * @param string $url | |
| 32 | - * @return MultiDomainDomain | |
| 33 | - */ | |
| 34 | - public static function domain_for_url($url) | |
| 35 | -    { | |
| 36 | - $url = trim($url, '/'); | |
| 24 | + /** | |
| 25 | + * Given a url, get the domain that maps to it, e.g. | |
| 26 | + * | |
| 27 | + * /company/ -> silverstripe.com | |
| 28 | + * /company/partners -> silverstripe.com | |
| 29 | + * /community/forum -> silverstripe.org | |
| 30 | + * | |
| 31 | + * @param string $url | |
| 32 | + * @return MultiDomainDomain | |
| 33 | + */ | |
| 34 | + public static function domain_for_url($url) | |
| 35 | +	{ | |
| 36 | + $url = trim($url, '/'); | |
| 37 | 37 | |
| 38 | -        foreach (self::get_all_domains() as $domain) { | |
| 39 | -            if ($domain->hasURL($url)) { | |
| 40 | - return $domain; | |
| 41 | - } | |
| 42 | - } | |
| 38 | +		foreach (self::get_all_domains() as $domain) { | |
| 39 | +			if ($domain->hasURL($url)) { | |
| 40 | + return $domain; | |
| 41 | + } | |
| 42 | + } | |
| 43 | 43 | |
| 44 | - return self::get_primary_domain(); | |
| 45 | - } | |
| 44 | + return self::get_primary_domain(); | |
| 45 | + } | |
| 46 | 46 | |
| 47 | - /** | |
| 48 | - * Gets all the domains that have been configured | |
| 49 | - * | |
| 50 | - * @param boolean $includePrimary If true, include the primary domain | |
| 51 | - * @return array | |
| 52 | - */ | |
| 53 | - public static function get_all_domains($includePrimary = false) | |
| 54 | -    { | |
| 55 | - $domains = array (); | |
| 47 | + /** | |
| 48 | + * Gets all the domains that have been configured | |
| 49 | + * | |
| 50 | + * @param boolean $includePrimary If true, include the primary domain | |
| 51 | + * @return array | |
| 52 | + */ | |
| 53 | + public static function get_all_domains($includePrimary = false) | |
| 54 | +	{ | |
| 55 | + $domains = array (); | |
| 56 | 56 | |
| 57 | -        foreach (self::config()->domains as $key => $config) { | |
| 58 | -            if (!$includePrimary && $key === self::KEY_PRIMARY) { | |
| 59 | - continue; | |
| 60 | - } | |
| 61 | - $domains[] = MultiDomainDomain::create($key, $config); | |
| 62 | - } | |
| 57 | +		foreach (self::config()->domains as $key => $config) { | |
| 58 | +			if (!$includePrimary && $key === self::KEY_PRIMARY) { | |
| 59 | + continue; | |
| 60 | + } | |
| 61 | + $domains[] = MultiDomainDomain::create($key, $config); | |
| 62 | + } | |
| 63 | 63 | |
| 64 | - return $domains; | |
| 65 | - } | |
| 64 | + return $domains; | |
| 65 | + } | |
| 66 | 66 | |
| 67 | - /** | |
| 68 | - * Gets the domain marked as "primary" | |
| 69 | - * @return MultiDomainDomain | |
| 70 | - */ | |
| 71 | - public static function get_primary_domain() | |
| 72 | -    { | |
| 73 | - return self::get_domain(self::KEY_PRIMARY); | |
| 74 | - } | |
| 67 | + /** | |
| 68 | + * Gets the domain marked as "primary" | |
| 69 | + * @return MultiDomainDomain | |
| 70 | + */ | |
| 71 | + public static function get_primary_domain() | |
| 72 | +	{ | |
| 73 | + return self::get_domain(self::KEY_PRIMARY); | |
| 74 | + } | |
| 75 | 75 | |
| 76 | - /** | |
| 77 | - * Gets a domain by its key, e.g. 'org','com' | |
| 78 | - * @param string $domain | |
| 79 | - * @return MultiDomainDomain | |
| 80 | - */ | |
| 81 | - public static function get_domain($domain) | |
| 82 | -    { | |
| 83 | -        if (isset(self::config()->domains[$domain])) { | |
| 84 | - return MultiDomainDomain::create( | |
| 85 | - $domain, | |
| 86 | - self::config()->domains[$domain] | |
| 87 | - ); | |
| 88 | - } | |
| 89 | - } | |
| 76 | + /** | |
| 77 | + * Gets a domain by its key, e.g. 'org','com' | |
| 78 | + * @param string $domain | |
| 79 | + * @return MultiDomainDomain | |
| 80 | + */ | |
| 81 | + public static function get_domain($domain) | |
| 82 | +	{ | |
| 83 | +		if (isset(self::config()->domains[$domain])) { | |
| 84 | + return MultiDomainDomain::create( | |
| 85 | + $domain, | |
| 86 | + self::config()->domains[$domain] | |
| 87 | + ); | |
| 88 | + } | |
| 89 | + } | |
| 90 | 90 | } | 
| @@ -16,275 +16,275 @@ | ||
| 16 | 16 | */ | 
| 17 | 17 | class MultiDomainDomain extends Object | 
| 18 | 18 |  { | 
| 19 | - /** | |
| 20 | - * The hostname of the domain, e.g. silverstripe.org | |
| 21 | - * @var string | |
| 22 | - */ | |
| 23 | - protected $hostname; | |
| 24 | - | |
| 25 | - /** | |
| 26 | - * The path that the hostname resolves to | |
| 27 | - * @var string | |
| 28 | - */ | |
| 29 | - protected $url; | |
| 30 | - | |
| 31 | - /** | |
| 32 | - * The identifier of the domain, e.g. 'org','com' | |
| 33 | - * @var string | |
| 34 | - */ | |
| 35 | - protected $key; | |
| 36 | - | |
| 37 | - /** | |
| 38 | - * Paths that are allowed to be accessed on the primary domain | |
| 39 | - * @var array | |
| 40 | - */ | |
| 41 | - protected $allowedPaths; | |
| 42 | - | |
| 43 | - /** | |
| 44 | - * Paths that are forced from the primary domain into a vanity one, | |
| 45 | - * outside the resolves_to path | |
| 46 | - * | |
| 47 | - * @var array | |
| 48 | - */ | |
| 49 | - protected $forcedPaths; | |
| 50 | - | |
| 51 | - /** | |
| 52 | - * The request URI | |
| 53 | - * | |
| 54 | - * @var string | |
| 55 | - */ | |
| 56 | - protected $requestUri; | |
| 57 | - | |
| 58 | - /** | |
| 59 | - * The request HTTP HOST | |
| 60 | - * | |
| 61 | - * @var string | |
| 62 | - */ | |
| 63 | - protected $httpHost; | |
| 64 | - | |
| 65 | - /** | |
| 66 | - * Constructor. Takes a key for the domain and its array of settings from the config | |
| 67 | - * @param string $key | |
| 68 | - * @param array $config | |
| 69 | - */ | |
| 70 | - public function __construct($key, $config) | |
| 71 | -    { | |
| 72 | - $this->key = $key; | |
| 73 | - $this->hostname = $config['hostname']; | |
| 74 | - $this->url = isset($config['resolves_to']) ? $config['resolves_to'] : null; | |
| 75 | - | |
| 76 | - $globalAllowed = (array) Config::inst()->get(MultiDomain::class, 'allow'); | |
| 77 | - $globalForced = (array) Config::inst()->get(MultiDomain::class, 'force'); | |
| 78 | - $myAllowed = isset($config['allow']) ? $config['allow'] : array (); | |
| 79 | - $myForced = isset($config['force']) ? $config['force'] : array (); | |
| 80 | - $this->allowedPaths = array_merge($globalAllowed, $myAllowed); | |
| 81 | - $this->forcedPaths = array_merge($globalForced, $myForced); | |
| 82 | - | |
| 83 | - parent::__construct(); | |
| 84 | - } | |
| 85 | - | |
| 86 | - /** | |
| 87 | - * Gets the hostname for the domain | |
| 88 | - * @return string | |
| 89 | - */ | |
| 90 | - public function getHostname() | |
| 91 | -    { | |
| 92 | - return defined($this->hostname) ? constant($this->hostname) : $this->hostname; | |
| 93 | - } | |
| 94 | - | |
| 95 | - /** | |
| 96 | - * Gets the path that the hostname resolves to | |
| 97 | - * @return string | |
| 98 | - */ | |
| 99 | - public function getURL() | |
| 100 | -    { | |
| 101 | - return $this->url; | |
| 102 | - } | |
| 103 | - | |
| 104 | - /** | |
| 105 | - * Returns true if the domain is currently in the HTTP_HOST | |
| 106 | - * @return boolean | |
| 107 | - */ | |
| 108 | - public function isActive() | |
| 109 | -    { | |
| 110 | -        if ($this->isAllowedPath($this->getRequestUri())) { | |
| 111 | - return false; | |
| 112 | - } | |
| 113 | - | |
| 114 | - $currentHost = $this->getHttpHost(); | |
| 115 | -        if (strpos(':', $currentHost) !== false) { | |
| 116 | -            list($currentHost, $currentPort) = explode(':', $currentHost, 2); | |
| 117 | - } | |
| 118 | - | |
| 119 | - $allow_subdomains = MultiDomain::config()->allow_subdomains; | |
| 120 | - $hostname = $this->getHostname(); | |
| 121 | - | |
| 122 | - return $allow_subdomains | |
| 123 | -            ? (bool) preg_match('/(\.|^)'.$hostname.'$/', $currentHost) | |
| 124 | - : ($currentHost == $hostname); | |
| 125 | - } | |
| 126 | - | |
| 127 | - /** | |
| 128 | - * Returns true if this domain is the primary domain | |
| 129 | - * @return boolean | |
| 130 | - */ | |
| 131 | - public function isPrimary() | |
| 132 | -    { | |
| 133 | - return $this->key === MultiDomain::KEY_PRIMARY; | |
| 134 | - } | |
| 135 | - | |
| 136 | - /** | |
| 137 | - * Gets the native URL for a vanity domain, e.g. /partners/ for .com | |
| 138 | - * returns /company/partners when .com is mapped to /company/. | |
| 139 | - * | |
| 140 | - * @param string $url | |
| 141 | - * @return string | |
| 142 | - */ | |
| 143 | - public function getNativeURL($url) | |
| 144 | -    { | |
| 145 | -        if ($this->isPrimary()) { | |
| 146 | -            throw new Exception("Cannot convert a native URL on the primary domain"); | |
| 147 | - } | |
| 148 | - | |
| 149 | -        if ($this->isAllowedPath($url) || $this->isForcedPath($url)) { | |
| 150 | - return $url; | |
| 151 | - } | |
| 152 | - | |
| 153 | - return Controller::join_links($this->getURL(), $url); | |
| 154 | - } | |
| 155 | - | |
| 156 | - /** | |
| 157 | - * Gets the vanity URL given a native URL. /company/partners returns /partners/ | |
| 158 | - * when .com is mapped to /company/. | |
| 159 | - * | |
| 160 | - * @param string $url | |
| 161 | - * @return string | |
| 162 | - */ | |
| 163 | - public function getVanityURL($url) | |
| 164 | -    { | |
| 165 | -        if ($this->isPrimary() || $this->isAllowedPath($url)) { | |
| 166 | - return $url; | |
| 167 | - } | |
| 168 | - | |
| 169 | -        $domainUrl = str_replace('/', '\/', $this->getURL()); | |
| 170 | -        return preg_replace('/^\/?' . $domainUrl . '\//', '', $url); | |
| 171 | - } | |
| 172 | - | |
| 173 | - /** | |
| 174 | - * Return true if this domain contains the given URL | |
| 175 | - * @param string $url | |
| 176 | - * @return boolean | |
| 177 | - */ | |
| 178 | - public function hasURL($url) | |
| 179 | -    { | |
| 180 | -        if ($this->isForcedPath($url)) { | |
| 181 | - return true; | |
| 182 | - } | |
| 183 | - $domainBaseURL = trim($this->getURL(), '/'); | |
| 184 | -        if (preg_match('/^'.$domainBaseURL.'/', $url)) { | |
| 185 | - return true; | |
| 186 | - } | |
| 187 | - | |
| 188 | - return false; | |
| 189 | - } | |
| 190 | - | |
| 191 | - /** | |
| 192 | - * Returns the key/identifier for this domain | |
| 193 | - * | |
| 194 | - * @return string | |
| 195 | - */ | |
| 196 | - public function getKey() | |
| 197 | -    { | |
| 198 | - return $this->key; | |
| 199 | - } | |
| 200 | - | |
| 201 | - /** | |
| 202 | - * Set the request URI | |
| 203 | - * | |
| 204 | - * @param string $requestUri | |
| 205 | - * @return $this | |
| 206 | - */ | |
| 207 | - public function setRequestUri($requestUri) | |
| 208 | -    { | |
| 209 | - $this->requestUri = (string) $requestUri; | |
| 210 | - return $this; | |
| 211 | - } | |
| 212 | - | |
| 213 | - /** | |
| 214 | - * Return the current request URI, defaulting to retrieving it from the $_SERVER superglobal | |
| 215 | - * | |
| 216 | - * @return string | |
| 217 | - */ | |
| 218 | - public function getRequestUri() | |
| 219 | -    { | |
| 220 | - return $this->requestUri ?: $_SERVER['REQUEST_URI']; | |
| 221 | - } | |
| 222 | - | |
| 223 | - /** | |
| 224 | - * Set the HTTP host in the request | |
| 225 | - * | |
| 226 | - * @param string $httpHost | |
| 227 | - * @return $this | |
| 228 | - */ | |
| 229 | - public function setHttpHost($httpHost) | |
| 230 | -    { | |
| 231 | - $this->httpHost = (string) $httpHost; | |
| 232 | - return $this; | |
| 233 | - } | |
| 234 | - | |
| 235 | - /** | |
| 236 | - * Return the current HTTP host, defaulting to retrieving it from the $_SERVER superglobal | |
| 237 | - * | |
| 238 | - * @return string | |
| 239 | - */ | |
| 240 | - public function getHttpHost() | |
| 241 | -    { | |
| 242 | - return $this->httpHost ?: $_SERVER['HTTP_HOST']; | |
| 243 | - } | |
| 244 | - | |
| 245 | - /** | |
| 246 | - * Checks a given list of wildcard patterns to see if a path is allowed | |
| 247 | - * @param string $url | |
| 248 | - * @return boolean | |
| 249 | - */ | |
| 250 | - protected function isAllowedPath($url) | |
| 251 | -    { | |
| 252 | - return self::match_url($url, $this->allowedPaths); | |
| 253 | - } | |
| 254 | - | |
| 255 | - /** | |
| 256 | - * Checks a given list of wildcard patterns to see if a path is allowed | |
| 257 | - * @param string $url | |
| 258 | - * @return boolean | |
| 259 | - */ | |
| 260 | - protected function isForcedPath($url) | |
| 261 | -    { | |
| 262 | - return self::match_url($url, $this->forcedPaths); | |
| 263 | - } | |
| 264 | - | |
| 265 | - /** | |
| 266 | - * Matches a URL against a list of wildcard patterns | |
| 267 | - * @param string $url | |
| 268 | - * @param array $patterns | |
| 269 | - * @return boolean | |
| 270 | - */ | |
| 271 | - protected static function match_url($url, $patterns) | |
| 272 | -    { | |
| 273 | -        if (!is_array($patterns)) { | |
| 274 | - return false; | |
| 275 | - } | |
| 276 | - | |
| 277 | - $url = ltrim($url, '/'); | |
| 278 | -        if (substr($url, -1) !== '/') { | |
| 279 | - $url .= '/'; | |
| 280 | - } | |
| 281 | - | |
| 282 | -        foreach ($patterns as $pattern) { | |
| 283 | -            if (fnmatch($pattern, $url)) { | |
| 284 | - return true; | |
| 285 | - } | |
| 286 | - } | |
| 287 | - | |
| 288 | - return false; | |
| 289 | - } | |
| 19 | + /** | |
| 20 | + * The hostname of the domain, e.g. silverstripe.org | |
| 21 | + * @var string | |
| 22 | + */ | |
| 23 | + protected $hostname; | |
| 24 | + | |
| 25 | + /** | |
| 26 | + * The path that the hostname resolves to | |
| 27 | + * @var string | |
| 28 | + */ | |
| 29 | + protected $url; | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * The identifier of the domain, e.g. 'org','com' | |
| 33 | + * @var string | |
| 34 | + */ | |
| 35 | + protected $key; | |
| 36 | + | |
| 37 | + /** | |
| 38 | + * Paths that are allowed to be accessed on the primary domain | |
| 39 | + * @var array | |
| 40 | + */ | |
| 41 | + protected $allowedPaths; | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * Paths that are forced from the primary domain into a vanity one, | |
| 45 | + * outside the resolves_to path | |
| 46 | + * | |
| 47 | + * @var array | |
| 48 | + */ | |
| 49 | + protected $forcedPaths; | |
| 50 | + | |
| 51 | + /** | |
| 52 | + * The request URI | |
| 53 | + * | |
| 54 | + * @var string | |
| 55 | + */ | |
| 56 | + protected $requestUri; | |
| 57 | + | |
| 58 | + /** | |
| 59 | + * The request HTTP HOST | |
| 60 | + * | |
| 61 | + * @var string | |
| 62 | + */ | |
| 63 | + protected $httpHost; | |
| 64 | + | |
| 65 | + /** | |
| 66 | + * Constructor. Takes a key for the domain and its array of settings from the config | |
| 67 | + * @param string $key | |
| 68 | + * @param array $config | |
| 69 | + */ | |
| 70 | + public function __construct($key, $config) | |
| 71 | +	{ | |
| 72 | + $this->key = $key; | |
| 73 | + $this->hostname = $config['hostname']; | |
| 74 | + $this->url = isset($config['resolves_to']) ? $config['resolves_to'] : null; | |
| 75 | + | |
| 76 | + $globalAllowed = (array) Config::inst()->get(MultiDomain::class, 'allow'); | |
| 77 | + $globalForced = (array) Config::inst()->get(MultiDomain::class, 'force'); | |
| 78 | + $myAllowed = isset($config['allow']) ? $config['allow'] : array (); | |
| 79 | + $myForced = isset($config['force']) ? $config['force'] : array (); | |
| 80 | + $this->allowedPaths = array_merge($globalAllowed, $myAllowed); | |
| 81 | + $this->forcedPaths = array_merge($globalForced, $myForced); | |
| 82 | + | |
| 83 | + parent::__construct(); | |
| 84 | + } | |
| 85 | + | |
| 86 | + /** | |
| 87 | + * Gets the hostname for the domain | |
| 88 | + * @return string | |
| 89 | + */ | |
| 90 | + public function getHostname() | |
| 91 | +	{ | |
| 92 | + return defined($this->hostname) ? constant($this->hostname) : $this->hostname; | |
| 93 | + } | |
| 94 | + | |
| 95 | + /** | |
| 96 | + * Gets the path that the hostname resolves to | |
| 97 | + * @return string | |
| 98 | + */ | |
| 99 | + public function getURL() | |
| 100 | +	{ | |
| 101 | + return $this->url; | |
| 102 | + } | |
| 103 | + | |
| 104 | + /** | |
| 105 | + * Returns true if the domain is currently in the HTTP_HOST | |
| 106 | + * @return boolean | |
| 107 | + */ | |
| 108 | + public function isActive() | |
| 109 | +	{ | |
| 110 | +		if ($this->isAllowedPath($this->getRequestUri())) { | |
| 111 | + return false; | |
| 112 | + } | |
| 113 | + | |
| 114 | + $currentHost = $this->getHttpHost(); | |
| 115 | +		if (strpos(':', $currentHost) !== false) { | |
| 116 | +			list($currentHost, $currentPort) = explode(':', $currentHost, 2); | |
| 117 | + } | |
| 118 | + | |
| 119 | + $allow_subdomains = MultiDomain::config()->allow_subdomains; | |
| 120 | + $hostname = $this->getHostname(); | |
| 121 | + | |
| 122 | + return $allow_subdomains | |
| 123 | +			? (bool) preg_match('/(\.|^)'.$hostname.'$/', $currentHost) | |
| 124 | + : ($currentHost == $hostname); | |
| 125 | + } | |
| 126 | + | |
| 127 | + /** | |
| 128 | + * Returns true if this domain is the primary domain | |
| 129 | + * @return boolean | |
| 130 | + */ | |
| 131 | + public function isPrimary() | |
| 132 | +	{ | |
| 133 | + return $this->key === MultiDomain::KEY_PRIMARY; | |
| 134 | + } | |
| 135 | + | |
| 136 | + /** | |
| 137 | + * Gets the native URL for a vanity domain, e.g. /partners/ for .com | |
| 138 | + * returns /company/partners when .com is mapped to /company/. | |
| 139 | + * | |
| 140 | + * @param string $url | |
| 141 | + * @return string | |
| 142 | + */ | |
| 143 | + public function getNativeURL($url) | |
| 144 | +	{ | |
| 145 | +		if ($this->isPrimary()) { | |
| 146 | +			throw new Exception("Cannot convert a native URL on the primary domain"); | |
| 147 | + } | |
| 148 | + | |
| 149 | +		if ($this->isAllowedPath($url) || $this->isForcedPath($url)) { | |
| 150 | + return $url; | |
| 151 | + } | |
| 152 | + | |
| 153 | + return Controller::join_links($this->getURL(), $url); | |
| 154 | + } | |
| 155 | + | |
| 156 | + /** | |
| 157 | + * Gets the vanity URL given a native URL. /company/partners returns /partners/ | |
| 158 | + * when .com is mapped to /company/. | |
| 159 | + * | |
| 160 | + * @param string $url | |
| 161 | + * @return string | |
| 162 | + */ | |
| 163 | + public function getVanityURL($url) | |
| 164 | +	{ | |
| 165 | +		if ($this->isPrimary() || $this->isAllowedPath($url)) { | |
| 166 | + return $url; | |
| 167 | + } | |
| 168 | + | |
| 169 | +		$domainUrl = str_replace('/', '\/', $this->getURL()); | |
| 170 | +		return preg_replace('/^\/?' . $domainUrl . '\//', '', $url); | |
| 171 | + } | |
| 172 | + | |
| 173 | + /** | |
| 174 | + * Return true if this domain contains the given URL | |
| 175 | + * @param string $url | |
| 176 | + * @return boolean | |
| 177 | + */ | |
| 178 | + public function hasURL($url) | |
| 179 | +	{ | |
| 180 | +		if ($this->isForcedPath($url)) { | |
| 181 | + return true; | |
| 182 | + } | |
| 183 | + $domainBaseURL = trim($this->getURL(), '/'); | |
| 184 | +		if (preg_match('/^'.$domainBaseURL.'/', $url)) { | |
| 185 | + return true; | |
| 186 | + } | |
| 187 | + | |
| 188 | + return false; | |
| 189 | + } | |
| 190 | + | |
| 191 | + /** | |
| 192 | + * Returns the key/identifier for this domain | |
| 193 | + * | |
| 194 | + * @return string | |
| 195 | + */ | |
| 196 | + public function getKey() | |
| 197 | +	{ | |
| 198 | + return $this->key; | |
| 199 | + } | |
| 200 | + | |
| 201 | + /** | |
| 202 | + * Set the request URI | |
| 203 | + * | |
| 204 | + * @param string $requestUri | |
| 205 | + * @return $this | |
| 206 | + */ | |
| 207 | + public function setRequestUri($requestUri) | |
| 208 | +	{ | |
| 209 | + $this->requestUri = (string) $requestUri; | |
| 210 | + return $this; | |
| 211 | + } | |
| 212 | + | |
| 213 | + /** | |
| 214 | + * Return the current request URI, defaulting to retrieving it from the $_SERVER superglobal | |
| 215 | + * | |
| 216 | + * @return string | |
| 217 | + */ | |
| 218 | + public function getRequestUri() | |
| 219 | +	{ | |
| 220 | + return $this->requestUri ?: $_SERVER['REQUEST_URI']; | |
| 221 | + } | |
| 222 | + | |
| 223 | + /** | |
| 224 | + * Set the HTTP host in the request | |
| 225 | + * | |
| 226 | + * @param string $httpHost | |
| 227 | + * @return $this | |
| 228 | + */ | |
| 229 | + public function setHttpHost($httpHost) | |
| 230 | +	{ | |
| 231 | + $this->httpHost = (string) $httpHost; | |
| 232 | + return $this; | |
| 233 | + } | |
| 234 | + | |
| 235 | + /** | |
| 236 | + * Return the current HTTP host, defaulting to retrieving it from the $_SERVER superglobal | |
| 237 | + * | |
| 238 | + * @return string | |
| 239 | + */ | |
| 240 | + public function getHttpHost() | |
| 241 | +	{ | |
| 242 | + return $this->httpHost ?: $_SERVER['HTTP_HOST']; | |
| 243 | + } | |
| 244 | + | |
| 245 | + /** | |
| 246 | + * Checks a given list of wildcard patterns to see if a path is allowed | |
| 247 | + * @param string $url | |
| 248 | + * @return boolean | |
| 249 | + */ | |
| 250 | + protected function isAllowedPath($url) | |
| 251 | +	{ | |
| 252 | + return self::match_url($url, $this->allowedPaths); | |
| 253 | + } | |
| 254 | + | |
| 255 | + /** | |
| 256 | + * Checks a given list of wildcard patterns to see if a path is allowed | |
| 257 | + * @param string $url | |
| 258 | + * @return boolean | |
| 259 | + */ | |
| 260 | + protected function isForcedPath($url) | |
| 261 | +	{ | |
| 262 | + return self::match_url($url, $this->forcedPaths); | |
| 263 | + } | |
| 264 | + | |
| 265 | + /** | |
| 266 | + * Matches a URL against a list of wildcard patterns | |
| 267 | + * @param string $url | |
| 268 | + * @param array $patterns | |
| 269 | + * @return boolean | |
| 270 | + */ | |
| 271 | + protected static function match_url($url, $patterns) | |
| 272 | +	{ | |
| 273 | +		if (!is_array($patterns)) { | |
| 274 | + return false; | |
| 275 | + } | |
| 276 | + | |
| 277 | + $url = ltrim($url, '/'); | |
| 278 | +		if (substr($url, -1) !== '/') { | |
| 279 | + $url .= '/'; | |
| 280 | + } | |
| 281 | + | |
| 282 | +		foreach ($patterns as $pattern) { | |
| 283 | +			if (fnmatch($pattern, $url)) { | |
| 284 | + return true; | |
| 285 | + } | |
| 286 | + } | |
| 287 | + | |
| 288 | + return false; | |
| 289 | + } | |
| 290 | 290 | } | 
| @@ -75,8 +75,8 @@ discard block | ||
| 75 | 75 | |
| 76 | 76 | $globalAllowed = (array) Config::inst()->get(MultiDomain::class, 'allow'); | 
| 77 | 77 | $globalForced = (array) Config::inst()->get(MultiDomain::class, 'force'); | 
| 78 | - $myAllowed = isset($config['allow']) ? $config['allow'] : array (); | |
| 79 | - $myForced = isset($config['force']) ? $config['force'] : array (); | |
| 78 | + $myAllowed = isset($config['allow']) ? $config['allow'] : array(); | |
| 79 | + $myForced = isset($config['force']) ? $config['force'] : array(); | |
| 80 | 80 | $this->allowedPaths = array_merge($globalAllowed, $myAllowed); | 
| 81 | 81 | $this->forcedPaths = array_merge($globalForced, $myForced); | 
| 82 | 82 | |
| @@ -167,7 +167,7 @@ discard block | ||
| 167 | 167 | } | 
| 168 | 168 | |
| 169 | 169 |          $domainUrl = str_replace('/', '\/', $this->getURL()); | 
| 170 | -        return preg_replace('/^\/?' . $domainUrl . '\//', '', $url); | |
| 170 | +        return preg_replace('/^\/?'.$domainUrl.'\//', '', $url); | |
| 171 | 171 | } | 
| 172 | 172 | |
| 173 | 173 | /** |