/**
  * Alter file path to generated a static (static) error page file to handle error page template on different sub-sites 
  *
  * @see Error::get_filepath_for_errorcode()
  *
  * FIXME since {@link Subsite::currentSubsite()} partly relies on Session, viewing other sub-site (including main site) between 
  * opening ErrorPage in the CMS and publish ErrorPage causes static error page to get generated incorrectly. 
  */
 function alternateFilepathForErrorcode($statusCode, $locale = null)
 {
     $static_filepath = Object::get_static($this->owner->ClassName, 'static_filepath');
     $subdomainPart = "";
     // Try to get current subsite from session
     $subsite = Subsite::currentSubsite(false);
     // since this function is called from Page class before the controller is created, we have to get subsite from domain instead
     if (!$subsite) {
         $subsiteID = Subsite::getSubsiteIDForDomain();
         if ($subsiteID != 0) {
             $subsite = DataObject::get_by_id("Subsite", $subsiteID);
         } else {
             $subsite = null;
         }
     }
     if ($subsite) {
         $subdomain = $subsite->domain();
         $subdomainPart = "-{$subdomain}";
     }
     if (singleton('SiteTree')->hasExtension('Translatable') && $locale && $locale != Translatable::default_locale()) {
         $filepath = $static_filepath . "/error-{$statusCode}-{$locale}{$subdomainPart}.html";
     } else {
         $filepath = $static_filepath . "/error-{$statusCode}{$subdomainPart}.html";
     }
     return $filepath;
 }
 /**
  * Confirm that domain lookup is working
  */
 function testDomainLookup()
 {
     $this->assertEquals($this->idFromFixture('Subsite', 'domaintest1'), Subsite::getSubsiteIDForDomain('one.example.org'));
     $this->assertEquals($this->idFromFixture('Subsite', 'domaintest1'), Subsite::getSubsiteIDForDomain('one.localhost'));
     $this->assertEquals($this->idFromFixture('Subsite', 'domaintest2'), Subsite::getSubsiteIDForDomain('two.mysite.com'));
     $this->assertEquals($this->idFromFixture('Subsite', 'domaintest2'), Subsite::getSubsiteIDForDomain('other.mysite.com'));
     $this->assertEquals(0, Subsite::getSubsiteIDForDomain('other.example.com'));
     $this->assertEquals(0, Subsite::getSubsiteIDForDomain('two.example.com'));
 }
 /**
  * Alter file path to generated a static (static) error page file to handle error page template on different sub-sites 
  *
  * {@see Error::get_error_filename()}
  *
  * FIXME since {@link Subsite::currentSubsite()} partly relies on Session, viewing other sub-site (including main site) between 
  * opening ErrorPage in the CMS and publish ErrorPage causes static error page to get generated incorrectly.
  *
  * @param string $name Filename to write to
  * @param int $statusCode Integer error code
  */
 public function updateErrorFilename(&$name, $statusCode)
 {
     // Try to get current subsite from session
     $subsite = Subsite::currentSubsite(false);
     // since this function is called from Page class before the controller is created, we have to get subsite from domain instead
     if (!$subsite) {
         $subsiteID = Subsite::getSubsiteIDForDomain();
         if ($subsiteID != 0) {
             $subsite = DataObject::get_by_id("Subsite", $subsiteID);
         }
     }
     // Without subsite, don't rewrite
     if ($subsite) {
         // Add subdomain to end of filename, just before .html
         // This should preserve translatable locale in the filename as well
         $subdomain = $subsite->domain();
         $name = substr($name, 0, -5) . "-{$subdomain}.html";
     }
 }
 public function augmentSyncLinkTracking()
 {
     // Set LinkTracking appropriately
     $links = HTTP::getLinksIn($this->owner->Content);
     $linkedPages = array();
     if ($links) {
         foreach ($links as $link) {
             if (substr($link, 0, strlen('http://')) == 'http://') {
                 $withoutHttp = substr($link, strlen('http://'));
                 if (strpos($withoutHttp, '/') && strpos($withoutHttp, '/') < strlen($withoutHttp)) {
                     $domain = substr($withoutHttp, 0, strpos($withoutHttp, '/'));
                     $rest = substr($withoutHttp, strpos($withoutHttp, '/') + 1);
                     $subsiteID = Subsite::getSubsiteIDForDomain($domain);
                     if ($subsiteID == 0) {
                         continue;
                     }
                     // We have no idea what the domain for the main site is, so cant track links to it
                     $origDisableSubsiteFilter = Subsite::$disable_subsite_filter;
                     Subsite::disable_subsite_filter(true);
                     $candidatePage = DataObject::get_one("SiteTree", "\"URLSegment\" = '" . Convert::raw2sql(urldecode($rest)) . "' AND \"SubsiteID\" = " . $subsiteID, false);
                     Subsite::disable_subsite_filter($origDisableSubsiteFilter);
                     if ($candidatePage) {
                         $linkedPages[] = $candidatePage->ID;
                     } else {
                         $this->owner->HasBrokenLink = true;
                     }
                 }
             }
         }
     }
     $this->owner->CrossSubsiteLinkTracking()->setByIDList($linkedPages);
 }
 function testStrictSubdomainMatching()
 {
     // Clear existing fixtures
     foreach (DataObject::get('Subsite') as $subsite) {
         $subsite->delete();
     }
     foreach (DataObject::get('SubsiteDomain') as $domain) {
         $domain->delete();
     }
     // Much more expressive than YML in this case
     $subsite1 = $this->createSubsiteWithDomains(array('example.org' => true, 'example.com' => false, '*.wildcard.com' => false));
     $subsite2 = $this->createSubsiteWithDomains(array('www.example.org' => true, 'www.wildcard.com' => false));
     Subsite::$strict_subdomain_matching = false;
     $this->assertEquals($subsite1->ID, Subsite::getSubsiteIDForDomain('example.org'), 'Exact matches without strict checking when not using www prefix');
     $this->assertEquals($subsite1->ID, Subsite::getSubsiteIDForDomain('www.example.org'), 'Matches without strict checking when using www prefix, still matching first domain regardless of www prefix  (falling back to subsite primary key ordering)');
     $this->assertEquals($subsite1->ID, Subsite::getSubsiteIDForDomain('www.example.com'), 'Fuzzy matches without strict checking with www prefix');
     $this->assertEquals(0, Subsite::getSubsiteIDForDomain('www.wildcard.com'), 'Doesn\'t match www prefix without strict check, even if a wildcard subdomain is in place');
     Subsite::$strict_subdomain_matching = true;
     $this->assertEquals($subsite1->ID, Subsite::getSubsiteIDForDomain('example.org'), 'Matches with strict checking when not using www prefix');
     $this->assertEquals($subsite2->ID, Subsite::getSubsiteIDForDomain('www.example.org'), 'Matches with strict checking when using www prefix');
     $this->assertEquals(0, Subsite::getSubsiteIDForDomain('www.example.com'), 'Doesn\'t fuzzy match with strict checking when using www prefix');
     $failed = false;
     try {
         Subsite::getSubsiteIDForDomain('www.wildcard.com');
     } catch (UnexpectedValueException $e) {
         $failed = true;
     }
     $this->assertTrue($failed, 'Fails on multiple matches with strict checking and wildcard vs. www');
 }