new method Api\Framework::getUrl() to get a full / externally usable Url

This commit is contained in:
Ralf Becker 2018-03-21 12:40:59 +01:00 committed by Hadi Nategh
parent 21e123ef84
commit 1d835928d2
7 changed files with 46 additions and 41 deletions

View File

@ -170,6 +170,39 @@ abstract class Framework extends Framework\Extra
return $GLOBALS['egw']->session->link($url, $extravars); return $GLOBALS['egw']->session->link($url, $extravars);
} }
/**
* Get a full / externally usable URL from an EGroupware link
*
* Code is only used, if the Setup defined webserver_url is only a path!
*
* The following HTTP Headers / $_SERVER variables and EGroupware configuration
* is taken into account to determine if URL should use https schema:
* - $_SERVER['HTTPS'] !== off
* - $GLOBALS['egw_info']['server']['enforce_ssl'] (EGroupware Setup)
* - $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' (X-Forwarded-Proto HTTP header)
*
* Host is determined in the following order / priority:
* 1. $GLOBALS['egw_info']['server']['hostname'] !== 'localhost' (EGroupware Setup)
* 2. $_SERVER['HTTP_X_FORWARDED_HOST'] (X-Forwarded-Host HTTP header)
* 3. $_SERVER['HTTP_HOST'] (Host HTTP header)
*
* @param string $link
*/
static function getUrl($link)
{
if ($link[0] === '/')
{
$link = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443 ||
!empty($GLOBALS['egw_info']['server']['enforce_ssl']) || $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' ?
'https://' : 'http://').
($GLOBALS['egw_info']['server']['hostname'] && $GLOBALS['egw_info']['server']['hostname'] !== 'localhost' ?
$GLOBALS['egw_info']['server']['hostname'] :
(isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : $_SERVER['HTTP_HOST'])).
$link;
}
return $link;
}
/** /**
* Handles redirects under iis and apache, it does NOT return (calls exit) * Handles redirects under iis and apache, it does NOT return (calls exit)
* *

View File

@ -358,11 +358,8 @@ abstract class Merge
$link = Api\Framework::link($link, array()); $link = Api\Framework::link($link, array());
} }
// Prepend site // Prepend site
if ($link{0} == '/') if ($link[0] == '/') $link = Api\Framework::getUrl($link);
{
$link = ($_SERVER['HTTPS'] || $GLOBALS['egw_info']['server']['enforce_ssl'] ? 'https://' : 'http://').
($GLOBALS['egw_info']['server']['hostname'] ? $GLOBALS['egw_info']['server']['hostname'] : $_SERVER['HTTP_HOST']).$link;
}
$title = $style == 'href' ? Api\Html::a_href(Api\Html::htmlspecialchars($title), $link) : $link; $title = $style == 'href' ? Api\Html::a_href(Api\Html::htmlspecialchars($title), $link) : $link;
} }
$link_titles[] = $title; $link_titles[] = $title;
@ -417,11 +414,8 @@ abstract class Merge
$link = Api\Framework::link($link, array()); $link = Api\Framework::link($link, array());
} }
// Prepend site // Prepend site
if ($link{0} == '/') if ($link[0] == '/') $link = Api\Framework::getUrl($link);
{
$link = ($_SERVER['HTTPS'] || $GLOBALS['egw_info']['server']['enforce_ssl'] ? 'https://' : 'http://').
($GLOBALS['egw_info']['server']['hostname'] ? $GLOBALS['egw_info']['server']['hostname'] : $_SERVER['HTTP_HOST']).$link;
}
$array[($prefix?$prefix.'/':'').$placeholder] = Api\Html::a_href(Api\Html::htmlspecialchars($title), $link); $array[($prefix?$prefix.'/':'').$placeholder] = Api\Html::a_href(Api\Html::htmlspecialchars($title), $link);
break; break;
case 'links': case 'links':

View File

@ -967,15 +967,12 @@ abstract class Tracking
{ {
if (($view = Api\Link::view($this->app,$data[$this->id_field]))) if (($view = Api\Link::view($this->app,$data[$this->id_field])))
{ {
$link = $GLOBALS['egw']->link('/index.php',$view); $link = Api\Framework::link('/index.php',$view);
$popup = Api\Link::is_popup($this->app,'view'); $popup = Api\Link::is_popup($this->app,'view');
} }
} }
if ($link[0] == '/') if ($link[0] == '/') Api\Framework::getUrl($link);
{
$link = ($_SERVER['HTTPS'] || $GLOBALS['egw_info']['server']['enforce_ssl'] ? 'https://' : 'http://').
($GLOBALS['egw_info']['server']['hostname'] ? $GLOBALS['egw_info']['server']['hostname'] : $_SERVER['HTTP_HOST']).$link;
}
if (!$allow_popup) if (!$allow_popup)
{ {
// remove the session-id in the notification mail! // remove the session-id in the notification mail!

View File

@ -675,21 +675,13 @@ class Sharing
* Generate link from share or share-token * Generate link from share or share-token
* *
* @param string|array $share share or share-token * @param string|array $share share or share-token
* @return string * @return string full Url incl. schema and host
*/ */
public static function share2link($share) public static function share2link($share)
{ {
if (is_array($share)) $share = $share['share_token']; if (is_array($share)) $share = $share['share_token'];
$link = Api\Framework::link('/share.php').'/'.$share; return Api\Framework::getUrl(Api\Framework::link('/share.php')).'/'.$share;
if ($link[0] == '/')
{
$link = ($_SERVER['HTTPS'] ? 'https://' : 'http://').
($GLOBALS['egw_info']['server']['hostname'] && $GLOBALS['egw_info']['server']['hostname'] !== 'localhost' ?
$GLOBALS['egw_info']['server']['hostname'] : $_SERVER['HTTP_HOST']).
$link;
}
return $link;
} }
} }

View File

@ -1796,12 +1796,7 @@ class calendar_boupdate extends calendar_bo
$eventStart_arr = $this->date2array($event['start']); // give this as 'date' to the link to pick the right recurrence for the participants state $eventStart_arr = $this->date2array($event['start']); // give this as 'date' to the link to pick the right recurrence for the participants state
$link = $GLOBALS['egw_info']['server']['webserver_url'].'/index.php?menuaction=calendar.calendar_uiforms.edit&cal_id='.$event['id'].'&date='.$eventStart_arr['full'].'&no_popup=1&ajax=true'; $link = $GLOBALS['egw_info']['server']['webserver_url'].'/index.php?menuaction=calendar.calendar_uiforms.edit&cal_id='.$event['id'].'&date='.$eventStart_arr['full'].'&no_popup=1&ajax=true';
// if url is only a path, try guessing the rest ;-) // if url is only a path, try guessing the rest ;-)
if ($link[0] == '/') if ($link[0] == '/') $link = Api\Framework::getUrl($link);
{
$link = ($GLOBALS['egw_info']['server']['enforce_ssl'] || $_SERVER['HTTPS'] ? 'https://' : 'http://').
($GLOBALS['egw_info']['server']['hostname'] ? $GLOBALS['egw_info']['server']['hostname'] : $_SERVER['HTTP_HOST']).
$link;
}
$event_arr['link']['data'] = $details['link'] = $link; $event_arr['link']['data'] = $details['link'] = $link;
/* this is needed for notification-app /* this is needed for notification-app

View File

@ -200,11 +200,8 @@ class filemanager_merge extends Api\Storage\Merge
} }
// Prepend site, if missing // Prepend site, if missing
if ($link{0} == '/') if ($link[0] == '/') $link = Api\Framework::getUrl($link);
{
$link = ($_SERVER['HTTPS'] || $GLOBALS['egw_info']['server']['enforce_ssl'] ? 'https://' : 'http://').
($GLOBALS['egw_info']['server']['hostname'] ? $GLOBALS['egw_info']['server']['hostname'] : $_SERVER['HTTP_HOST']).$link;
}
$file['link'] = Api\Html::a_href(Api\Html::htmlspecialchars($file['name']), $link); $file['link'] = Api\Html::a_href(Api\Html::htmlspecialchars($file['name']), $link);
$file['url'] = $link; $file['url'] = $link;

View File

@ -157,10 +157,7 @@ class notifications_email implements notifications_iface {
// do not expose sensitive data // do not expose sensitive data
$url = preg_replace('/(sessionid|kp3|domain)=[^&]+&?/','',Api\Html::link('/index.php', $link->view)); $url = preg_replace('/(sessionid|kp3|domain)=[^&]+&?/','',Api\Html::link('/index.php', $link->view));
// complete missing protocol and domain part if needed // complete missing protocol and domain part if needed
if ($url{0} == '/' && $_render_external) { if ($url[0] == '/' && $_render_external) $url = Api\Framework::getUrl($url);
$url = ($_SERVER['HTTPS'] || $GLOBALS['egw_info']['server']['enforce_ssl'] ? 'https://' : 'http://').
($GLOBALS['egw_info']['server']['hostname'] ? $GLOBALS['egw_info']['server']['hostname'] : $_SERVER['HTTP_HOST']).$url;
}
$a_href = '<a href="'.$url.'" target="_blank">'.$link->text.'</a>'; $a_href = '<a href="'.$url.'" target="_blank">'.$link->text.'</a>';
$rendered_links[] = $_render_html ? $a_href : $url; $rendered_links[] = $_render_html ? $a_href : $url;
} }