Skip to content

Commit

Permalink
Fixed issue 18821: Public url is not used for SURVEYURL (#3229)
Browse files Browse the repository at this point in the history
* Fixed issue 18821: Public url is not used for SURVEYURL

* Fixed issue 18821: Public url is not used for SURVEYURL (moved and renamed function)
  • Loading branch information
twilligls committed Jun 23, 2023
1 parent bc2bbb9 commit 3c70454
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 18 deletions.
40 changes: 40 additions & 0 deletions application/core/LSYii_Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -666,4 +666,44 @@ private function setSessionByDB($aApplicationConfig)
'lifetime' => $lifetime
]);
}

/**
* Creates an absolute URL based on the given controller and action information.
* @param string $route the URL route. This should be in the format of 'ControllerID/ActionID'.
* @param array $params additional GET parameters (name=>value). Both the name and value will be URL-encoded.
* @param string $schema schema to use (e.g. http, https). If empty, the schema used for the current request will be used.
* @param string $ampersand the token separating name-value pairs in the URL.
* @return string the constructed URL
*/
public function createPublicUrl($route, $params = array(), $schema = '', $ampersand = '&')
{
$sPublicUrl = $this->getPublicBaseUrl(true);
$sActualBaseUrl = Yii::app()->getBaseUrl(true);
if ($sPublicUrl !== $sActualBaseUrl) {
$url = parent::createAbsoluteUrl($route, $params, $schema, $ampersand);
if (substr((string)$url, 0, strlen((string)$sActualBaseUrl)) == $sActualBaseUrl) {
$url = substr((string)$url, strlen((string)$sActualBaseUrl));
}
return trim((string)$sPublicUrl, "/") . $url;
} else {
return parent::createAbsoluteUrl($route, $params, $schema, $ampersand);
}
}

/**
* Returns the relative URL for the application while
* considering if a "publicurl" config parameter is set to a valid url
* @param boolean $absolute whether to return an absolute URL. Defaults to false, meaning returning a relative one.
* @return string the relative or the configured public URL for the application
*/
public function getPublicBaseUrl($absolute = false)
{
$sPublicUrl = Yii::app()->getConfig("publicurl");
$aPublicUrl = parse_url($sPublicUrl);
$baseUrl = parent::getBaseUrl($absolute);
if (isset($aPublicUrl['scheme']) && isset($aPublicUrl['host'])) {
$baseUrl = $sPublicUrl;
}
return $baseUrl;
}
}
19 changes: 4 additions & 15 deletions application/core/LSYii_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ protected function customInit()
}

/**
* Creates an absolute URL based on the given controller and action information.
* Returns an absolute URL based on the given controller and action information.
* The functionalty was moved to
* \LSYii_Application::createPublicUrl, to be safe the function remains here.
* @param string $route the URL route. This should be in the format of 'ControllerID/ActionID'.
* @param array $params additional GET parameters (name=>value). Both the name and value will be URL-encoded.
* @param string $schema schema to use (e.g. http, https). If empty, the schema used for the current request will be used.
Expand All @@ -165,20 +167,7 @@ protected function customInit()
*/
public function createAbsoluteUrl($route, $params = array(), $schema = '', $ampersand = '&')
{
$sPublicUrl = Yii::app()->getConfig("publicurl");
// Control if public url are really public : need scheme and host
// If yes: use it
$aPublicUrl = parse_url((string) $sPublicUrl);
if (isset($aPublicUrl['scheme']) && isset($aPublicUrl['host'])) {
$url = parent::createAbsoluteUrl($route, $params, $schema, $ampersand);
$sActualBaseUrl = Yii::app()->getBaseUrl(true);
if (substr((string) $url, 0, strlen((string) $sActualBaseUrl)) == $sActualBaseUrl) {
$url = substr((string) $url, strlen((string) $sActualBaseUrl));
}
return trim((string) $sPublicUrl, "/") . $url;
} else {
return parent::createAbsoluteUrl($route, $params, $schema, $ampersand);
}
return App()->createPublicUrl($route, $params, $schema, $ampersand);
}

/**
Expand Down
8 changes: 5 additions & 3 deletions application/models/Survey.php
Original file line number Diff line number Diff line change
Expand Up @@ -2333,13 +2333,14 @@ public function getSurveyUrl($language = null, $params = [], $preferShortUrl = t
}

// Create the URL according to the configured format
$baseUrl = App()->getPublicBaseUrl(true);
$urlManager = Yii::app()->getUrlManager();
$urlFormat = $urlManager->getUrlFormat();
if ($urlFormat == CUrlManager::GET_FORMAT) {
$url = Yii::app()->getBaseUrl(true);
$url = $baseUrl;
$params = [$urlManager->routeVar => $alias] + $params;
} else {
$url = Yii::app()->getBaseUrl(true) . '/' . $alias;
$url = $baseUrl . '/' . $alias;
}
$query = $urlManager->createPathInfo($params, '=', '&');
if (!empty($query)) {
Expand All @@ -2351,7 +2352,8 @@ public function getSurveyUrl($language = null, $params = [], $preferShortUrl = t

// If short url is not preferred or no alias is found, return a traditional URL
$urlParams = array_merge($params, ['sid' => $this->sid, 'lang' => $language]);
$url = Yii::app()->createAbsoluteUrl('survey/index', $urlParams);
$url = App()->createPublicUrl('survey/index', $urlParams);

return $url;
}

Expand Down

0 comments on commit 3c70454

Please sign in to comment.