Procházet zdrojové kódy

#3750 Setting to lock site for guests

Alex Trofimov před 4 roky
rodič
revize
1b5f348f5c
3 změnil soubory, kde provedl 32 přidání a 6 odebrání
  1. 18 1
      inc/classes/BxDolPage.php
  2. 12 3
      inc/utils.inc.php
  3. 2 2
      template/scripts/BxBasePage.php

+ 18 - 1
inc/classes/BxDolPage.php

@@ -634,8 +634,25 @@ class BxDolPage extends BxDolFactory implements iBxDolFactoryObject, iBxDolRepla
     /**
      * Check if page is visible.
      */
-    protected function _isVisiblePage ($a)
+    protected function _isVisiblePage ($a, $bRedirectToLoginFormForUnauthenticated = false)
     {
+        if (!isLogged() && getParam('sys_lock_from_unauthenticated') && !defined('BX_DOL_CRON_EXECUTE')) {
+            $aURIs = explode(',', getParam('sys_lock_from_unauthenticated_exceptions'));
+            array_walk($aURIs, function (&$sVal) {
+                $sVal = trim($sVal);
+            });
+            $aI = array_combine($aURIs, array_fill(0, count($aURIs), 1));
+            if (!preg_match('/\/oauth2\//', $_SERVER['REQUEST_URI']) && !preg_match('/searchKeyword.php$/', $_SERVER['PHP_SELF']) && !preg_match('/member.php$/', $_SERVER['PHP_SELF']) && !isset($aI[$this->_aObject['uri']])) {
+                if ($bRedirectToLoginFormForUnauthenticated) {
+                    header("Location: " . bx_append_url_params(BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=login'), ['relocate' => bx_get_self_url()]));
+                    exit;
+                }
+                else {
+                    return false;
+                }
+            }
+        }
+
         return isAdmin() || BxDolAcl::getInstance()->isMemberLevelInSet($a['visible_for_levels']);
     }
 

+ 12 - 3
inc/utils.inc.php

@@ -1779,14 +1779,23 @@ function bx_check_redirect_to_correct_hostname ($bProcessRedirect = false)
     $bRedirectRequired = isset($_SERVER['HTTP_HOST']) && 0 !== strcasecmp($_SERVER['HTTP_HOST'], $aUrl['host']) && 0 !== strcasecmp($_SERVER['HTTP_HOST'], $aUrl['host'] . ':' . (!empty($aUrl['port']) ? $aUrl['port'] : $iPortDefault));
 
     if ($bRedirectRequired && $bProcessRedirect) {
-        $sPort = empty($aUrl['port']) || 80 == $aUrl['port'] || 443 == $aUrl['port'] ? '' : ':' . $aUrl['port'];
-        header("Location:{$aUrl['scheme']}://{$aUrl['host']}{$sPort}{$_SERVER['REQUEST_URI']}", true, 301);
+        header("Location:" . bx_get_self_url(), true, 301);
         exit;
     }
     
     return $bRedirectRequired;
 }
 
+/**
+ * Get URL of current page
+ */
+function bx_get_self_url ()
+{
+    $aUrl = parse_url(BX_DOL_URL_ROOT);
+    $sPort = empty($aUrl['port']) || 80 == $aUrl['port'] || 443 == $aUrl['port'] ? '' : ':' . $aUrl['port'];
+    return "{$aUrl['scheme']}://{$aUrl['host']}{$sPort}{$_SERVER['REQUEST_URI']}";
+}
+
 /**
  * Check if redirect to remove install folder.
  * If BX_SKIP_INSTALL_CHECK is defined then this redirect checking is skipped.
@@ -2124,4 +2133,4 @@ function bx_birthday2age($sBirthday)
     return $iResult;
 }
 
-/** @} */
+/** @} */

+ 2 - 2
template/scripts/BxBasePage.php

@@ -157,7 +157,7 @@ class BxBasePage extends BxDolPage
     {
         if (bx_get('dynamic') && ($iBlockId = (int)bx_get('pageBlock'))) {
 
-            if (!$this->_isVisiblePage($this->_aObject)) {
+            if (!$this->_isVisiblePage($this->_aObject, true)) {
                 header('HTTP/1.0 403 Forbidden');
                 exit;
             }
@@ -183,7 +183,7 @@ class BxBasePage extends BxDolPage
             exit;
         }
 
-        if (!$this->_isVisiblePage($this->_aObject))
+        if (!$this->_isVisiblePage($this->_aObject, true))
             return $this->_getPageAccessDeniedMsg ();
 
         $this->_addJsCss();