Просмотр исходного кода

Ticket #4971 - Build-in API support: Redirect to logged in profile.

AntonLV 11 месяцев назад
Родитель
Сommit
714ffa5e67
2 измененных файлов с 40 добавлено и 16 удалено
  1. 36 10
      inc/classes/BxDolPage.php
  2. 4 6
      modules/base/profile/classes/BxBaseModProfilePageEntry.php

+ 36 - 10
inc/classes/BxDolPage.php

@@ -116,7 +116,9 @@ class BxDolPage extends BxDolFactory implements iBxDolFactoryObject, iBxDolRepla
     protected $_sObject;
     protected $_aObject;
     protected $_oQuery;
-    protected $_aMarkers = array ();
+    protected $_aMarkers = [];
+
+    protected $_sForceRedirect = false;
 
     /**
      * Constructor
@@ -156,21 +158,35 @@ class BxDolPage extends BxDolFactory implements iBxDolFactoryObject, iBxDolRepla
      */
     static public function getObjectInstanceByURI($sURI = '', $oTemplate = false, $bRedirectCheck = false)
     {
-    	if(empty($sURI) && bx_get('i') !== false)
-    		$sURI = bx_process_input(bx_get('i'));
+        if(empty($sURI) && bx_get('i') !== false)
+            $sURI = bx_process_input(bx_get('i'));
 
-    	if(empty($sURI))
-			return false;
+        if(empty($sURI))
+            return false;
 
         $sObject = BxDolPageQuery::getPageObjectNameByURI($sURI);
-        if ($bRedirectCheck && !$sObject && '/' == substr($sURI, -1)) {
-            header("HTTP/1.1 301 Moved Permanently");
+        if($bRedirectCheck && !$sObject && '/' == substr($sURI, -1)) {
             unset($_GET['i']);
-            header ('Location:' . bx_append_url_params(bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . trim($sURI, '/'))), $_GET));
+
+            header("HTTP/1.1 301 Moved Permanently");
+            header('Location:' . bx_append_url_params(bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . trim($sURI, '/'))), $_GET));
             exit;
         }
 
-        return $sObject ? self::getObjectInstance($sObject, $oTemplate) : false;
+        if(!$sObject)
+            return false;
+
+        $oObject = self::getObjectInstance($sObject, $oTemplate);
+        if(($sRedirectUrl = $oObject->getForceRedirect()) !== false) {
+            if(!bx_is_api()) {
+                header("Location: " . $sRedirectUrl);
+                exit;
+            }
+            else 
+                $oObject = bx_api_get_relative_url($sRedirectUrl);
+        }
+
+        return $oObject;
     }
 
     /**
@@ -794,7 +810,7 @@ class BxDolPage extends BxDolFactory implements iBxDolFactoryObject, iBxDolRepla
     	return $this->_aObject;
     }
     
-    public function getInjections()
+    public function getInjections ()
     {
         $aResult = array();
 
@@ -805,6 +821,16 @@ class BxDolPage extends BxDolFactory implements iBxDolFactoryObject, iBxDolRepla
         return $aResult;
     }
 
+    public function getForceRedirect ()
+    {
+        return $this->_sForceRedirect ?: false;
+    }
+
+    public function setForceRedirect ($sUrl)
+    {
+        $this->_sForceRedirect = $sUrl;
+    }
+
     public function setTitle ($sTitle)
     {
         return $this->_aObject['title'] = $sTitle;

+ 4 - 6
modules/base/profile/classes/BxBaseModProfilePageEntry.php

@@ -56,13 +56,11 @@ class BxBaseModProfilePageEntry extends BxBaseModGeneralPageEntry
                 $a[$CNF['URI_VIEW_FRIENDS']] = ['profile_id', 'id'];
             if (isset($CNF['URI_VIEW_RELATIONS']))
                 $a[$CNF['URI_VIEW_RELATIONS']] = ['profile_id', 'id'];
-            
+
             foreach ($a as $sUri => $r) {
-                if ($this->_aObject['uri'] == $sUri) {
-                    $oProfile = BxDolProfile::getInstance();
-                    $sUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $sUri . '&' . $r[0] . '=' . $oProfile->{$r[1]}()));
-                    header("Location: " . $sUrl);
-                    exit;
+                if ($this->_aObject['uri'] == $sUri && ($oProfile = BxDolProfile::getInstance()) !== false) {
+                    $this->setForceRedirect(bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $sUri . '&' . $r[0] . '=' . $oProfile->{$r[1]}())));
+                    return;
                 }
             }
         }