From 172752a9f41c64bf2c519887b5e3d0e7f0d3d2b7 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Wed, 15 Aug 2012 11:11:22 +0200 Subject: [PATCH] NEW Access to non-public subsites for logged-in users Also added caching subsite domain mapping --- README.md | 21 +++++++++++++++++ code/LeftAndMainSubsites.php | 2 +- code/Subsite.php | 45 +++++++++++++++++++++++++----------- 3 files changed, 53 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 424b2c1..ef948a7 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,27 @@ Not all themes might be suitable or adapted for all subsites. You can optionally :::php Subsite::set_allowed_themes(array('blackcandy','mytheme')); +### Public display of a subsite + +By default, each subsite is available to the public (= not logged-in), +provided a correct host mapping is set up. A subsite can be marked as non-public +in its settings, in which case it only shows if a user with CMS permissions is logged in. +This is useful to create and check subsites on a live system before publishing them. + +Please note that you need to filter for this manually in your own queries: + + $publicSubsites = DataObject::get( + 'Subsite', + Subsite::$check_is_public ? '"IsPublic"=1' : ''; + ); + +To ensure the logged-in status of a member is carried across to subdomains, +you also need to configure PHP session cookies to be set +for all subdomains: + + // Example matching subsite1.example.org and www.example.org + Session::set_cookie_domain('.example.org'); + ## Screenshots ![](docs/en/_images/subsites-module-adminscreenshot-new.png) diff --git a/code/LeftAndMainSubsites.php b/code/LeftAndMainSubsites.php index a1cf504..0545221 100644 --- a/code/LeftAndMainSubsites.php +++ b/code/LeftAndMainSubsites.php @@ -53,7 +53,7 @@ class LeftAndMainSubsites extends Extension { case "CMSMain": // If there's a default site then main site has no meaning - $showMainSite = !DataObject::get_one('Subsite',"\"DefaultSite\"=1 AND \"IsPublic\"=1"); + $showMainSite = !DataObject::get_one('Subsite',"\"DefaultSite\"=1"); $subsites = Subsite::accessible_sites($accessPerm, $showMainSite); break; diff --git a/code/Subsite.php b/code/Subsite.php index 617a40e..72c96ed 100644 --- a/code/Subsite.php +++ b/code/Subsite.php @@ -76,6 +76,8 @@ class Subsite extends DataObject implements PermissionProvider { */ private static $_cache_accessible_sites = array(); + private static $_cache_subsite_for_domain = array(); + /** * @var array $allowed_themes Numeric array of all themes which are allowed to be selected for all subsites. * Corresponds to subfolder names within the /themes folder. By default, all themes contained in this folder @@ -90,6 +92,11 @@ class Subsite extends DataObject implements PermissionProvider { */ static $strict_subdomain_matching = false; + /** + * @var boolean Respects the IsPublic flag when retrieving subsites + */ + static $check_is_public = true; + static function set_allowed_domains($domain){ user_error('Subsite::set_allowed_domains() is deprecated; it is no longer necessary ' . 'because users can now enter any domain name', E_USER_NOTICE); @@ -334,15 +341,22 @@ JS; * @param $host The host to find the subsite for. If not specified, $_SERVER['HTTP_HOST'] is used. * @return int Subsite ID */ - static function getSubsiteIDForDomain($host = null, $returnMainIfNotFound = true) { + static function getSubsiteIDForDomain($host = null, $checkPermissions = true) { if($host == null) $host = $_SERVER['HTTP_HOST']; - - if(!Subsite::$strict_subdomain_matching) $host = preg_replace('/^www\./', '', $host); - $SQL_host = Convert::raw2sql($host); - $matchingDomains = DataObject::get("SubsiteDomain", "'$SQL_host' LIKE replace(\"SubsiteDomain\".\"Domain\",'*','%')", - "\"IsPrimary\" DESC", "INNER JOIN \"Subsite\" ON \"Subsite\".\"ID\" = \"SubsiteDomain\".\"SubsiteID\" AND - \"Subsite\".\"IsPublic\"=1"); + if(!Subsite::$strict_subdomain_matching) $host = preg_replace('/^www\./', '', $host); + + $cacheKey = implode('_', array($host, Member::currentUserID(), Subsite::$check_is_public)); + if(isset(self::$_cache_subsite_for_domain[$cacheKey])) return self::$_cache_subsite_for_domain[$cacheKey]; + + $SQL_host = Convert::raw2sql($host); + $joinFilter = self::$check_is_public ? "AND \"Subsite\".\"IsPublic\"=1" : ''; + $matchingDomains = DataObject::get( + "SubsiteDomain", + "'$SQL_host' LIKE replace(\"SubsiteDomain\".\"Domain\",'*','%')", + "\"IsPrimary\" DESC", + "INNER JOIN \"Subsite\" ON \"Subsite\".\"ID\" = \"SubsiteDomain\".\"SubsiteID\" $joinFilter" + ); if($matchingDomains) { $subsiteIDs = array_unique($matchingDomains->column('SubsiteID')); @@ -355,16 +369,18 @@ JS; )); } - return $subsiteIDs[0]; + $subsiteID = $subsiteIDs[0]; + } else if($default = DataObject::get_one('Subsite', "\"DefaultSite\" = 1")) { + // Check for a 'default' subsite + $subsiteID = $default->ID; + } else { + // Default subsite id = 0, the main site + $subsiteID = 0; } - // Check for a 'default' subsite - if ($default = DataObject::get_one('Subsite', "\"DefaultSite\" = 1")) { - return $default->ID; - } + self::$_cache_subsite_for_domain[$cacheKey] = $subsiteID; - // Default subsite id = 0, the main site - return 0; + return $subsiteID; } function getMembersByPermission($permissionCodes = array('ADMIN')){ @@ -629,5 +645,6 @@ JS; */ static function on_db_reset() { self::$_cache_accessible_sites = array(); + self::$_cache_subsite_for_domain = array(); } }