/**
  * This method should be used to built navigation menus in templates, 
  * instead of ContentController->getMenu()
  * @return ArrayList
  **/
 public function getSiteMenu($level = 1)
 {
     $site = Multisites::inst()->getCurrentSite();
     $page = $this->owner->data();
     $result = new ArrayList();
     if ($level == 1) {
         $pages = SiteTree::get()->filter(array('ParentID' => $site ? $site->ID : 0, 'ShowInMenus' => true));
     } else {
         $parent = $page;
         $stack = array($page);
         while (($parent = $parent->Parent()) && $parent->ID > 0 && !$parent instanceof Site) {
             array_unshift($stack, $parent);
         }
         if (!isset($stack[$level - 2])) {
             return;
         }
         $pages = $stack[$level - 2]->Children();
     }
     foreach ($pages as $page) {
         if ($page->canView()) {
             $result->push($page);
         }
     }
     return $result;
 }
 public function UpdateForm()
 {
     $member = singleton('SecurityContext')->getMember();
     if (!$member) {
         return '';
     }
     // if there's a member profile page availble, use it
     $filter = array();
     if (class_exists('Multisites')) {
         $filter = array('SiteID' => Multisites::inst()->getCurrentSiteId());
     }
     // use member profile page if possible
     if (class_exists('MemberProfilePage') && ($profilePage = MemberProfilePage::get()->filter($filter)->first())) {
         $controller = MemberProfilePage_Controller::create($profilePage);
         $form = $controller->ProfileForm();
         $form->addExtraClass('ajax-form');
         $form->loadDataFrom($member);
         return $form;
     } else {
         $password = new ConfirmedPasswordField('Password', $member->fieldLabel('Password'), null, null, (bool) $this->ID);
         $password->setCanBeEmpty(true);
         $fields = FieldList::create(TextField::create('FirstName', $member->fieldLabel('FirstName')), TextField::create('Surname', $member->fieldLabel('Surname')), EmailField::create('Email', $member->fieldLabel('Email')), $password);
         $actions = FieldList::create($update = FormAction::create('updateprofile', 'Update'));
         $form = Form::create($this, 'UpdateForm', $fields, $actions);
         $form->loadDataFrom($member);
         $this->extend('updateProfileDashletForm', $form);
         return $form;
     }
     return;
 }
 /**
  * Sets the theme to the current site theme
  **/
 function onBeforeSecurityLogin()
 {
     $site = Multisites::inst()->getCurrentSite();
     if ($site && $site->Theme) {
         SSViewer::set_theme($site->Theme);
     }
 }
 protected function getConf() {
     $conf = SiteConfig::current_site_config();
     if (class_exists('Multisites')) {
         $conf = Multisites::inst()->getCurrentSite();
     }
     return $conf;
 }
 /**
  * @return Multisites
  */
 public static function inst()
 {
     if (!self::$inst) {
         self::$inst = new self();
         self::$inst->init();
     }
     return self::$inst;
 }
 /**
  * Makes the default page id the first child of the current site
  * This makes the site tree view load with the current site open instead of just the first one
  **/
 public function updateCurrentPageID(&$id)
 {
     if (!$id) {
         if ($site = Multisites::inst()->getCurrentSite()) {
             $id = $site->Children()->first();
         }
     }
 }
    public function getResults($pageLength = null, $data = null)
    {
        // legacy usage: $data was defaulting to $_REQUEST, parameter not passed in doc.silverstripe.org tutorials
        if (!isset($data) || !is_array($data)) {
            $data = $_REQUEST;
        }
        // set language (if present)
        if (class_exists('Translatable') && singleton('SiteTree')->hasExtension('Translatable') && isset($data['locale'])) {
            $origLocale = Translatable::get_current_locale();
            Translatable::set_current_locale($data['locale']);
        }
        $keywords = $data['Search'];
        $andProcessor = create_function('$matches', '
	 		return " +" . $matches[2] . " +" . $matches[4] . " ";
	 	');
        $notProcessor = create_function('$matches', '
	 		return " -" . $matches[3];
	 	');
        $keywords = preg_replace_callback('/()("[^()"]+")( and )("[^"()]+")()/i', $andProcessor, $keywords);
        $keywords = preg_replace_callback('/(^| )([^() ]+)( and )([^ ()]+)( |$)/i', $andProcessor, $keywords);
        $keywords = preg_replace_callback('/(^| )(not )("[^"()]+")/i', $notProcessor, $keywords);
        $keywords = preg_replace_callback('/(^| )(not )([^() ]+)( |$)/i', $notProcessor, $keywords);
        $keywords = $this->addStarsToKeywords($keywords);
        if (!$pageLength) {
            $pageLength = $this->pageLength;
        }
        $start = isset($_GET['start']) ? (int) $_GET['start'] : 0;
        $siteFilter = '';
        $fileFilter = "ID != 0";
        $site = Multisites::inst()->getCurrentSite();
        $siteFilter = 'SiteID = ' . $site->ID;
        if ($this->config()->restrict_files_by_site) {
            if ($site->FolderID) {
                $prefix = $site->Folder()->Filename;
                if (strlen($prefix)) {
                    $fileFilter .= ' AND "Filename" LIKE \'' . Convert::raw2sql($prefix) . '%\'';
                }
            }
        }
        if (strpos($keywords, '"') !== false || strpos($keywords, '+') !== false || strpos($keywords, '-') !== false || strpos($keywords, '*') !== false) {
            $results = DB::getConn()->searchEngine($this->classesToSearch, $keywords, $start, $pageLength, "\"Relevance\" DESC", $siteFilter, true, $fileFilter);
        } else {
            $results = DB::getConn()->searchEngine($this->classesToSearch, $keywords, $start, $pageLength, '', $siteFilter, false, $fileFilter);
        }
        // filter by permission
        if ($results) {
            foreach ($results as $result) {
                if (!$result->canView()) {
                    $results->remove($result);
                }
            }
        }
        // reset locale
        if (class_exists('Translatable') && singleton('SiteTree')->hasExtension('Translatable') && isset($data['locale'])) {
            Translatable::set_current_locale($origLocale);
        }
        return $results;
    }
 /**
  * Returns the default search page for this site
  *
  * @return ExtensibleSearchPage
  */
 public function getSearchPage()
 {
     $page = ExtensibleSearchPage::get();
     if (class_exists('Multisites')) {
         $siteID = Multisites::inst()->getCurrentSiteId();
         $page = $page->filter('SiteID', $siteID);
     }
     return $page->first();
 }
 public function onRenameLinkedAsset($original)
 {
     if (SiteConfig::current_site_config()->DisableSiteCache) {
         return;
     }
     if (class_exists('Multisites') && Multisites::inst()->getActiveSite()->DisableSiteCache) {
         return;
     }
     $this->cachePublisher->publishDataObject($this->owner);
 }
 /**
  *	Retrieve the correct error page for the current multisite instance.
  *	@param integer
  *	@param SS_HTTPRequest
  *	@throws SS_HTTPResponse_Exception
  */
 public function onBeforeHTTPError($code, $request)
 {
     $errorPage = ErrorPage::get()->filter(array('ErrorCode' => $code, 'SiteID' => Multisites::inst()->getCurrentSiteId()))->first();
     if ($errorPage) {
         Requirements::clear();
         Requirements::clear_combined_files();
         $response = ModelAsController::controller_for($errorPage)->handleRequest($request, DataModel::inst());
         throw new SS_HTTPResponse_Exception($response, $code);
     }
 }
 /**
  * Provide a Site filter
  **/
 public function updateSearchForm($form)
 {
     if (singleton($this->owner->getList()->dataClass())->hasExtension('MultisitesAware')) {
         $managedSites = Multisites::inst()->sitesManagedByMember();
         $source = Site::get()->filter('ID', Multisites::inst()->sitesManagedByMember())->map('ID', 'Title')->toArray();
         $plural = singleton($this->owner->modelClass)->plural_name();
         if (count($source)) {
             $form->Fields()->push(DropdownField::create('SiteID', "Site: ", $source, Session::get('MultisitesModelAdmin_SiteID')));
         }
     }
 }
 /**
  * prepends an assets/currentsite folder to the upload folder name.
  **/
 public function useMultisitesFolder()
 {
     $site = Multisites::inst()->getActiveSite();
     $multisiteFolder = $site->Folder();
     if (!$multisiteFolder->exists()) {
         $site->createAssetsSubfolder(true);
         $multisiteFolder = $site->Folder();
     }
     $this->owner->setFolderName($multisiteFolder->Name . '/' . $this->owner->getFolderName());
     return $this->owner;
 }
 /**
  *	The test to ensure the request filter is functioning correctly.
  */
 public function testRequestFilter()
 {
     // Instantiate link mappings to use.
     $mapping = LinkMapping::create(array('LinkType' => 'Simple', 'MappedLink' => 'wrong/page', 'RedirectLink' => 'pending'));
     $mapping->write();
     LinkMapping::create(array('LinkType' => 'Simple', 'MappedLink' => 'pending', 'RedirectLink' => 'correct/page'))->write();
     // The CMS module needs to be present to test page behaviour.
     if (ClassInfo::exists('SiteTree')) {
         // This is required to support multiple sites.
         $this->logInAs(Member::default_admin());
         $parentID = ClassInfo::exists('Multisites') ? Multisites::inst()->getCurrentSiteId() : 0;
         // Instantiate pages to use.
         $first = SiteTree::create(array('URLSegment' => 'wrong', 'ParentID' => $parentID));
         $first->writeToStage('Stage');
         $first->writeToStage('Live');
         $second = SiteTree::create(array('URLSegment' => 'page', 'ParentID' => $first->ID));
         $second->writeToStage('Stage');
         $second->writeToStage('Live');
     }
     // Determine whether the enforce misdirection is functioning correctly.
     $response = $this->get('wrong/page');
     $this->assertEquals($response->getStatusCode(), 303);
     $this->assertEquals($response->getHeader('Location'), '/correct/page');
     // The CMS module needs to be present to test page behaviour.
     if (ClassInfo::exists('SiteTree')) {
         // Update the default enforce misdirection.
         Config::inst()->update('MisdirectionRequestFilter', 'enforce_misdirection', false);
         // Determine whether the page is now matched.
         $response = $this->get('wrong/page');
         $this->assertEquals($response->getStatusCode(), 200);
         $this->assertEquals($response->getHeader('Location'), null);
         // Instantiate a fallback to use.
         $first->Fallback = 'Nearest';
         $first->writeToStage('Stage');
         $first->writeToStage('Live');
         // The database needs to be cleaned up to prevent further testing conflict.
         $second->deleteFromStage('Live');
         $second->deleteFromStage('Stage');
         $mapping->delete();
         // Determine whether the fallback is matched.
         $response = $this->get('wrong/page');
         $this->assertEquals($response->getStatusCode(), 303);
         $this->assertEquals($response->getHeader('Location'), '/wrong/?misdirected=1');
     }
     // Instantiate a director rule to use.
     Config::inst()->update('Director', 'rules', array('wrong/page' => 'Controller'));
     // Determine whether the director rule is matched.
     $response = $this->get('wrong/page');
     $this->assertEquals($response->getStatusCode(), 200);
     $this->assertEquals($response->getHeader('Location'), null);
     // The database needs to be emptied to prevent further testing conflict.
     self::empty_temp_db();
 }
 /**
  * Check to see if the current user has permission to edit this MultisitesAware object
  * On the site this object is associated with.
  * @return boolean|null
  **/
 public function canEdit($member = null)
 {
     $managedSites = Multisites::inst()->sitesManagedByMember();
     if (count($managedSites) && in_array($this->owner->SiteID, $managedSites)) {
         // member has permission to manage MultisitesAware objects on this site,
         // hand over to the object's canEdit method
         return null;
     } else {
         // member does not have permission to edit objects on this object's Site
         return false;
     }
 }
 public function onAfterInit()
 {
     if (!Director::isDev()) {
         // Only on live site
         $errorcode = $this->owner->failover->ErrorCode ? $this->owner->failover->ErrorCode : 404;
         $extract = preg_match('/^([a-z0-9\\.\\_\\-\\/]+)/i', $_SERVER['REQUEST_URI'], $rawString);
         if ($errorcode == 404 && $extract) {
             $uri = preg_replace('/\\.(aspx?|html?|php[34]?)$/i', '', $rawString[0]);
             $parts = preg_split('/\\//', $uri, -1, PREG_SPLIT_NO_EMPTY);
             $page_key = array_pop($parts);
             $sounds_like = soundex($page_key);
             // extend ignored classes with child classes
             $ignoreClassNames = array();
             if ($configClasses = Config::inst()->get('Intelligent404', 'intelligent_404_ignored_classes')) {
                 foreach ($configClasses as $class) {
                     $ignoreClassNames = array_merge($ignoreClassNames, array_values(ClassInfo::subclassesFor($class)));
                 }
             }
             // get all pages
             $SiteTree = SiteTree::get()->exclude('ClassName', $ignoreClassNames);
             // Translatable support
             if (class_exists('Translatable')) {
                 $SiteTree = $SiteTree->filter('Locale', Translatable::get_current_locale());
             }
             // Multisites support
             if (class_exists('Multisites')) {
                 $SiteTree = $SiteTree->filter('SiteID', Multisites::inst()->getCurrentSiteId());
             }
             $ExactMatches = new ArrayList();
             $PossibleMatches = new ArrayList();
             foreach ($SiteTree as $page) {
                 if ($page->URLSegment == $page_key) {
                     $ExactMatches->push($page);
                 } elseif ($sounds_like == soundex($page->URLSegment)) {
                     $PossibleMatches->push($page);
                 }
             }
             $ExactCount = $ExactMatches->Count();
             $PossibleCount = $PossibleMatches->Count();
             $redirectOnSingleMatch = Config::inst()->get('Intelligent404', 'redirect_on_single_match');
             if ($ExactCount == 1 && $redirectOnSingleMatch) {
                 return $this->RedirectToPage($ExactMatches->First()->Link());
             } elseif ($ExactCount == 0 && $PossibleCount == 1 && $redirectOnSingleMatch) {
                 return $this->RedirectToPage($PossibleMatches->First()->Link());
             } elseif ($ExactCount > 1 || $PossibleCount > 1 || !$redirectOnSingleMatch) {
                 $ExactMatches->merge($PossibleMatches);
                 $content = $this->owner->customise(array('Pages' => $ExactMatches))->renderWith(array('Intelligent404Options'));
                 $this->owner->Content .= $content;
             }
         }
     }
 }
 public function alternateFilepathForErrorcode($code, $locale)
 {
     $path = ErrorPage::get_static_filepath();
     $parts = array();
     if ($site = Multisites::inst()->getActiveSite()) {
         $parts[] = $site->Host;
     }
     $parts[] = $code;
     if ($locale && $this->owner->hasExtension('Translatable') && $locale != Translatable::default_locale()) {
         $parts[] = $locale;
     }
     return sprintf("%s/error-%s.html", $path, implode('-', $parts));
 }
 public function AddForm()
 {
     $form = parent::AddForm();
     $fields = $form->Fields();
     $fields->push(new HiddenField('Parent', null, true));
     // Enforce a parent mode of "child" to correctly read the "allowed children".
     $fields->dataFieldByName('ParentModeField')->setValue('child');
     $fields->insertAfter($parent = new TreeDropdownField('ParentID', '', 'SiteTree', 'ID', 'TreeTitle'), 'ParentModeField');
     $parentID = $this->request->getVar('ParentID');
     $parentID = $parentID ? $parentID : Multisites::inst()->getCurrentSiteId();
     $parent->setForm($form);
     $parent->setValue((int) $parentID);
     $form->setValidator(new RequiredFields('ParentID'));
     return $form;
 }
 /**
  * Get the key to use for this cached fragment
  * 
  * If we're forcing the regen, 
  * 
  * @param boolean $forCacheGen
  * @return string
  */
 protected function getKey($forCacheGen = false)
 {
     $key = get_class($this) . '-' . $this->templateName;
     // detect site specific behaviour
     if (class_exists('Multisites')) {
         $key .= '-' . Multisites::inst()->getCurrentSiteID();
     }
     return $key;
     /// WHAT is this doing? I can't remember, leaving here for later
     // "oh that's right" moment.
     if (defined('PROXY_CACHE_GENERATING') || $forCacheGen) {
         // use the fragment key instead
         return $key . '-fragment';
     }
 }
 /**
  * Check to see if the current user needs to agree to the terms and conditions 
  * of their group before proceeding
  **/
 public function onAfterInit()
 {
     $member = Member::currentUser();
     if ($member && Session::get('RequiresAgreement')) {
         // <-- set on user login
         if ($this->owner->class != 'Security' && $this->owner->class != 'RestrictedSecurityController' && $this->owner->ClassName != 'UserAgreementPage') {
             if (class_exists('Multisites') && DataObject::get_one('Site')) {
                 $agreementPage = DataObject::get_one('UserAgreementPage', 'ParentID = ' . Multisites::inst()->getCurrentSiteId());
             } else {
                 $agreementPage = DataObject::get_one('UserAgreementPage');
             }
             return $this->owner->redirect($agreementPage->Link());
         }
     }
 }
 /**
  * Implement this method in the task subclass to
  * execute via the TaskRunner
  */
 public function run($request)
 {
     $currentSite = Multisites::inst()->getCurrentSite();
     $folderName = $currentSite->Host ? $currentSite->Host : "site-{$currentSite->ID}";
     $folder = Folder::find_or_make($folderName);
     $files = File::get()->filter('ParentID', array(0))->exclude('ID', $folder->ID);
     if (!$files->count()) {
         return;
     }
     foreach ($files as $file) {
         if (file_exists($file->getFullPath())) {
             $file->ParentID = $folder->ID;
             $file->write();
             echo $file->Filename . ' moved <br />';
         }
     }
 }
 public function handleRequest(SS_HTTPRequest $request, DataModel $model = null)
 {
     self::$is_at_root = true;
     $this->setDataModel($model);
     $this->pushCurrent();
     $this->init();
     if (!($site = Multisites::inst()->getCurrentSiteId())) {
         return $this->httpError(404);
     }
     $page = SiteTree::get()->filter(array('ParentID' => $site, 'URLSegment' => 'home'));
     if (!($page = $page->first())) {
         return $this->httpError(404);
     }
     $request = new SS_HTTPRequest($request->httpMethod(), $page->RelativeLink(), $request->getVars(), $request->postVars());
     $request->match('$URLSegment//$Action', true);
     $front = new MultisitesFrontController();
     $response = $front->handleRequest($request, $model);
     $this->popCurrent();
     return $response;
 }
 public function generateMenuItems()
 {
     $all = array();
     $allids = array();
     if (class_exists('Multisites')) {
         $site = Multisites::inst()->getCurrentSite();
         $all[] = array('ID' => $site->ID, 'ClassName' => 'Site', 'Title' => $site->Title, 'ParentID' => 0, 'MenuTitle' => $site->Title, 'URLSegment' => '', 'CanViewType' => $site->CanViewType);
         $allids[$site->ID] = true;
     }
     $public = $this->getPublicNodes();
     foreach ($public as $row) {
         $allids[$row['ID']] = true;
         $all[] = $row;
     }
     // and private nodes
     $private = $this->getPrivateNodes();
     foreach ($private as $row) {
         $allids[$row['ID']] = true;
         $all[] = $row;
     }
     $others = $this->getAdditionalNodes();
     foreach ($others as $row) {
         $allids[$row['ID']] = true;
         $all[] = $row;
     }
     $deferred = array();
     $final = array();
     $hierarchy = array();
     $counter = 0;
     $this->iterateNodes($final, $all, $allids, 0);
     $ordered = ArrayList::create();
     // start at 0
     if (isset($final[0]['kids'])) {
         foreach ($final[0]['kids'] as $id) {
             $node = $final[$id];
             $this->buildLinks($node, null, $ordered, $final);
         }
     }
 }
 public function index()
 {
     $site = Multisites::inst()->getCurrentSiteId();
     if (!$site) {
         return $this->httpError(404);
     }
     $page = Site::get()->filter(array('ID' => $site));
     $page = $page->first();
     if (!$page) {
         return $this->httpError(404);
     }
     /*
      * Trim the RobotsTxt field because it may be an empty string.
      * and since SilverStripe doesn't ship with a default robots.txt
      * file, we'll want to return a 404 if there isn't any text for
      * the site's robots.txt file.
      */
     $text = trim($page->RobotsTxt);
     if (empty($text)) {
         return $this->httpError(404);
     }
     $this->getResponse()->addHeader('Content-Type', 'text/plain; charset="utf-8"');
     return $text;
 }
 protected function publishUrls($urls, $keyPrefix = '', $domain = null)
 {
     if (defined('PROXY_CONFIG_FILE') && !isset($PROXY_CACHE_HOSTMAP)) {
         include_once BASE_PATH . '/' . PROXY_CONFIG_FILE;
     }
     $config = SiteConfig::current_site_config();
     if ($config->DisableSiteCache) {
         return;
     }
     $urls = array_unique($urls);
     // Do we need to map these?
     // Detect a numerically indexed arrays
     if (is_numeric(join('', array_keys($urls)))) {
         $urls = $this->urlsToPaths($urls);
     }
     // This can be quite memory hungry and time-consuming
     // @todo - Make a more memory efficient publisher
     increase_time_limit_to();
     increase_memory_limit_to();
     $currentBaseURL = Director::baseURL();
     $files = array();
     $i = 0;
     $totalURLs = sizeof($urls);
     $cache = $this->getCache();
     if (!defined('PROXY_CACHE_GENERATING')) {
         define('PROXY_CACHE_GENERATING', true);
     }
     foreach ($urls as $url => $path) {
         // work around bug introduced in ss3 whereby top level /bathroom.html would be changed to ./bathroom.html
         $path = ltrim($path, './');
         $url = rtrim($url, '/');
         // TODO: Detect the scheme + host URL from the URL's absolute path
         // and set that as the base URL appropriately
         $baseUrlSrc = $this->staticBaseUrl ? $this->staticBaseUrl : $url;
         $urlBits = parse_url($baseUrlSrc);
         if (isset($urlBits['scheme']) && isset($urlBits['host'])) {
             // now see if there's a host mapping
             // we want to set the base URL correctly
             Config::inst()->update('Director', 'alternate_base_url', $urlBits['scheme'] . '://' . $urlBits['host'] . '/');
         }
         $i++;
         if ($url && !is_string($url)) {
             user_error("Bad url:" . var_export($url, true), E_USER_WARNING);
             continue;
         }
         Requirements::clear();
         if (strrpos($url, '/home') == strlen($url) - 5) {
             $url = substr($url, 0, strlen($url) - 5);
         }
         if ($url == "" || $url == 'home') {
             $url = "/";
         }
         if (Director::is_relative_url($url)) {
             $url = Director::absoluteURL($url);
         }
         $stage = Versioned::current_stage();
         Versioned::reading_stage('Live');
         $GLOBALS[self::CACHE_PUBLISH] = 1;
         Config::inst()->update('SSViewer', 'theme_enabled', true);
         if (class_exists('Multisites')) {
             Multisites::inst()->resetCurrentSite();
         }
         $response = Director::test(str_replace('+', ' ', $url));
         Config::inst()->update('SSViewer', 'theme_enabled', false);
         unset($GLOBALS[self::CACHE_PUBLISH]);
         Versioned::reading_stage($stage);
         Requirements::clear();
         singleton('DataObject')->flushCache();
         $contentType = null;
         // Generate file content
         if (is_object($response)) {
             if ($response->getStatusCode() == '301' || $response->getStatusCode() == '302') {
                 $absoluteURL = Director::absoluteURL($response->getHeader('Location'));
                 $content = null;
             } else {
                 $content = $response->getBody();
                 $type = $response->getHeader('Content-type');
                 $contentType = $type ? $type : $contentType;
             }
         } else {
             $content = $response . '';
         }
         if (!$content) {
             continue;
         }
         if (isset($urlBits['host'])) {
             $domain = $urlBits['host'];
         }
         if ($domain && !$keyPrefix) {
             $keyPrefix = $domain;
         }
         $path = trim($path, '/');
         if ($path == 'home') {
             $path = '';
         }
         $data = new stdClass();
         $data->Content = $content;
         $data->LastModified = date('Y-m-d H:i:s');
         $cacheAge = SiteConfig::current_site_config()->CacheAge;
         if ($cacheAge) {
             $data->Age = $cacheAge;
         } else {
             $data->Age = HTTP::get_cache_age();
         }
         if (!empty($contentType)) {
             $data->ContentType = $contentType;
         }
         $key = $keyPrefix . '/' . $path;
         $cache->store($key, $data);
         if ($domain && isset($PROXY_CACHE_HOSTMAP) && isset($PROXY_CACHE_HOSTMAP[$domain])) {
             $hosts = $PROXY_CACHE_HOSTMAP[$domain];
             foreach ($hosts as $otherDomain) {
                 $key = $otherDomain . '/' . $path;
                 $storeData = clone $data;
                 $storeData->Content = str_replace($domain, $otherDomain, $storeData->Content);
                 $cache->store($key, $storeData);
             }
         }
     }
     Director::setBaseURL($currentBaseURL);
 }
 /**
  * Alternative implementation that takes into account the current site
  * as the root
  *
  * @param type $link
  * @param type $cache
  * @return boolean
  */
 public static function get_by_link($link, $cache = true)
 {
     $current = Multisites::inst()->getCurrentSiteId();
     if (trim($link, '/')) {
         $link = trim(Director::makeRelative($link), '/');
     } else {
         $link = RootURLController::get_homepage_link();
     }
     $parts = Convert::raw2sql(preg_split('|/+|', $link));
     // Grab the initial root level page to traverse down from.
     $URLSegment = array_shift($parts);
     $sitetree = DataObject::get_one('SiteTree', "\"URLSegment\" = '{$URLSegment}' AND \"ParentID\" = " . $current, $cache);
     if (!$sitetree) {
         return false;
     }
     /// Fall back on a unique URLSegment for b/c.
     if (!$sitetree && self::nested_urls() && ($page = DataObject::get('SiteTree', "\"URLSegment\" = '{$URLSegment}'")->First())) {
         return $page;
     }
     // Check if we have any more URL parts to parse.
     if (!count($parts)) {
         return $sitetree;
     }
     // Traverse down the remaining URL segments and grab the relevant SiteTree objects.
     foreach ($parts as $segment) {
         $next = DataObject::get_one('SiteTree', "\"URLSegment\" = '{$segment}' AND \"ParentID\" = {$sitetree->ID}", $cache);
         if (!$next) {
             return false;
         }
         $sitetree->destroy();
         $sitetree = $next;
     }
     return $sitetree;
 }
 /**
  * Set the active site for this model admin
  *
  * @param int $siteID
  * @return void
  **/
 public function setActiveSite($siteID)
 {
     if ($this->owner->config()->use_active_site_session) {
         Multisites::inst()->setActiveSite($siteID);
     } else {
         Session::set($this->getActiveSiteSessionKey(), $siteID);
     }
 }
 /**
  * Basically works like SiteTree::get_by_link but is capable of accounting for Multisites.
  *
  * @return SiteTree
  */
 public function getByLink($link, $findOldPageFallback = true)
 {
     $prefix = '';
     if (class_exists('Multisites')) {
         $site = Multisites::inst()->getCurrentSite();
         if ($site) {
             $prefix = $site->URLSegment . '/';
         }
     }
     $link = trim($link, '/');
     $linkDirParts = explode('/', $link);
     $result = SiteTree::get_by_link($prefix . $link . '/');
     if ($result) {
         // Check if URLSegment matches the last part of the URL, as get_by_link
         // will return the parent page if there's no match.
         if ($result->URLSegment && $result->URLSegment === end($linkDirParts)) {
             return $result;
         }
         return false;
     }
     if ($findOldPageFallback) {
         $url = OldPageRedirector::find_old_page($linkDirParts);
         if ($url) {
             $result = $this->getByLink($url, false);
             return $result;
         }
     }
     return false;
 }
 /**
  * Overrides ModelAsController->getNestedController to find the nested controller
  * on a per-site basis
  **/
 public function getNestedController()
 {
     $request = $this->request;
     $segment = $request->param('URLSegment');
     $site = Multisites::inst()->getCurrentSiteId();
     if (!$site) {
         return $this->httpError(404);
     }
     if (class_exists('Translatable')) {
         Translatable::disable_locale_filter();
     }
     $page = SiteTree::get()->filter(array('ParentID' => $site, 'URLSegment' => rawurlencode($segment)));
     $page = $page->first();
     if (class_exists('Translatable')) {
         Translatable::enable_locale_filter();
     }
     if (!$page) {
         // Check to see if linkmapping module is installed and if so, if there a map for this request.
         if (class_exists('LinkMapping')) {
             if ($request->requestVars()) {
                 $queryString = '?';
                 foreach ($request->requestVars() as $key => $value) {
                     if ($key != 'url') {
                         $queryString .= $key . '=' . $value . '&';
                     }
                 }
                 $queryString = rtrim($queryString, '&');
             }
             $link = $queryString != '?' ? $request->getURL() . $queryString : $request->getURL();
             $link = trim(Director::makeRelative($link));
             $map = LinkMapping::get()->filter('MappedLink', $link)->first();
             if ($map) {
                 $this->response = new SS_HTTPResponse();
                 $this->response->redirect($map->getLink(), 301);
                 return $this->response;
             }
         }
         // use OldPageRedirector if it exists, to find old page
         if (class_exists('OldPageRedirector')) {
             if ($redirect = OldPageRedirector::find_old_page(array($segment), Multisites::inst()->getCurrentSite())) {
                 $redirect = SiteTree::get_by_link($redirect);
             }
         } else {
             $redirect = self::find_old_page($segment, $site);
         }
         if ($redirect) {
             $getVars = $request->getVars();
             //remove the url var as it confuses the routing
             unset($getVars['url']);
             $url = Controller::join_links($redirect->Link(Controller::join_links($request->param('Action'), $request->param('ID'), $request->param('OtherID'))));
             if (!empty($getVars)) {
                 $url .= '?' . http_build_query($getVars);
             }
             $this->response->redirect($url, 301);
             return $this->response;
         }
         return $this->httpError(404);
     }
     if (class_exists('Translatable') && $page->Locale) {
         Translatable::set_current_locale($page->Locale);
     }
     return self::controller_for($page, $request->param('Action'));
 }
 public function PostForm()
 {
     $fields = FieldList::create();
     if ($this->Options()->UserTitle) {
         $fields->push($title = TextField::create('Title', _t('MicroBlog.TITLE', 'Title')));
         $title->setAttribute('placeholder', _t('MicroBlog.TITLE_PLACEHOLDER', 'Title (optional)'));
     }
     $fields->push($taf = new TextareaField('Content', _t('MicroBlog.POST', 'Post')));
     $taf->setAttribute('placeholder', _t('MicroBlog.CONTENT_PLACEHOLDER', 'Add content here, eg text or a link'));
     $taf->setRows(3);
     $taf->addExtraClass('expandable');
     $taf->addExtraClass('postContent');
     $taf->addExtraClass('preview');
     $public = CheckboxField::create('PublicUsers', 'Public users', Config::inst()->get('TimelineController', 'default_public'));
     $loggedIn = CheckboxField::create('LoggedInUsers', "Logged in users", Config::inst()->get('TimelineController', 'default_logged_in'));
     $groups = Group::get()->filter("ParentID", 0);
     $specificGroups = null;
     if (class_exists('Multisites')) {
         $specificGroups = Multisites::inst()->getCurrentSite()->TargetedGroups();
     } else {
         $specificGroups = SiteConfig::current_site_config()->TargetedGroups();
     }
     if (count($specificGroups)) {
         $groups = $specificGroups;
     }
     $members = Member::get();
     if ($members->count() > $this->ajaxMemberLimit) {
         $member = AjaxSelect2Field::create('Members', "To")->setConfig('classToSearch', 'Member')->setConfig('multiple', true)->setConfig('searchFields', array('FirstName', 'Surname', 'Email'))->setConfig('resultsLimit', $this->ajaxMemberLimit);
     } else {
         $member = MultiSelect2Field::create('Members', "To", $members->map()->toArray())->setMultiple(true);
     }
     $group = MultiSelect2Field::create("Groups", "To Groups", $groups->map()->toArray())->setMultiple(true);
     $fields->push($public);
     $fields->push($loggedIn);
     $fields->push($member);
     $fields->push($group);
     $target = $this->getTargetFilter();
     if ($target) {
         $fields->push(HiddenField::create('PostTarget', '', $target));
     }
     $actions = new FieldList(new FormAction('savepost', _t('MicroBlog.SAVE', 'Add')));
     $form = new Form($this, 'PostForm', $fields, $actions);
     $this->extend('updatePostForm', $form);
     return $form;
 }
 /**
  *	Determine the fallback for a URL when the CMS module is present.
  *
  *	@parameter <{URL}> string
  *	@return array(string, integer)
  */
 public function determineFallback($URL)
 {
     // Make sure the CMS module is present.
     if (ClassInfo::exists('SiteTree') && $URL) {
         // Instantiate the required variables.
         $segments = explode('/', self::unify_URL($URL));
         $applicableRule = null;
         $nearestParent = null;
         $thisPage = null;
         $toURL = null;
         $responseCode = 303;
         // Retrieve the default site configuration fallback.
         $config = SiteConfig::current_site_config();
         if ($config && $config->Fallback) {
             $applicableRule = $config->Fallback;
             $nearestParent = $thisPage = Director::baseURL();
             $toURL = $config->FallbackLink;
             $responseCode = $config->FallbackResponseCode;
         }
         // This is required to support multiple sites.
         $parentID = ClassInfo::exists('Multisites') ? Multisites::inst()->getCurrentSiteId() : 0;
         // Determine the page specific fallback.
         $apply = false;
         for ($iteration = 0; $iteration < count($segments); $iteration++) {
             $page = SiteTree::get()->filter(array('URLSegment' => $segments[$iteration], 'ParentID' => $parentID))->first();
             if ($page) {
                 // Determine the home page URL when appropriate.
                 $link = $page->Link() === Director::baseURL() ? Controller::join_links(Director::baseURL(), 'home/') : $page->Link();
                 $nearestParent = $link;
                 // Keep track of the current page fallback.
                 if ($page->Fallback) {
                     $applicableRule = $page->Fallback;
                     $thisPage = $link;
                     $toURL = $page->FallbackLink;
                     $responseCode = $page->FallbackResponseCode;
                 }
                 $parentID = $page->ID;
             } else {
                 // The bottom of the chain has been reached.
                 $apply = true;
                 break;
             }
         }
         // Determine the applicable fallback.
         if ($apply && $applicableRule) {
             $link = null;
             switch ($applicableRule) {
                 case 'Nearest':
                     $link = $nearestParent;
                     break;
                 case 'This':
                     $link = $thisPage;
                     break;
                 case 'URL':
                     $link = $toURL;
                     break;
             }
             if ($link) {
                 return array('link' => self::is_external_URL($link) ? $link : Controller::join_links(Director::baseURL(), HTTP::setGetVar('misdirected', true, $link)), 'code' => (int) $responseCode);
             }
         }
     }
     // No fallback has been found.
     return null;
 }