class Url
{
- public static function isLocal($url, $site_url=null)
- {
- if (null!==$site_url && strpos($url, $site_url) === 0) {
- return true;
- }
+ public static function isLocal($url, $site_url = null)
+ {
+ if (null !== $site_url && strpos($url, $site_url) === 0) {
+ return true;
+ }
- if (strpos($url, "//") === 0) {
- return false;
- }
- $u = parse_url($url);
+ if (strpos($url, "//") === 0) {
+ return false;
+ }
+ $u = parse_url($url);
return !(isset($u['scheme']) && $u['scheme'] != '');
}
- public static function extractPathComponents($path)
- {
- $path = urldecode($path);
- $path = trim($path, '/');
- if ($path == '') {
- return [];
- }
+ public static function extractPathComponents($path)
+ {
+ $path = urldecode($path);
+ $path = trim($path, '/');
+ if ($path == '') {
+ return [];
+ }
return explode('/', $path);
}
- public static function isDistant($url, $site_url=null)
- {
- return !self::isLocal($url, $site_url);
- }
+ public static function isDistant($url, $site_url = null)
+ {
+ return !self::isLocal($url, $site_url);
+ }
- public static function toAbsolute($url, $site_url=null)
- {
- if (strpos($url, '/') === 0) {
- $url = $site_url . $url;
- }
- return $url;
- }
+ public static function toAbsolute($url, $site_url = null)
+ {
+ if (strpos($url, '/') === 0) {
+ $url = $site_url . $url;
+ }
+ return $url;
+ }
- public static function createGetUrl($url, $options)
- {
- $res = $url;
- $o = array();
- foreach ($options as $k => $v) {
- $o[] = $k . '=' . rawurlencode($v);
- }
- if (count($o)) {
- $res .= '?' . implode('&', $o);
- }
- return $res;
- }
+ public static function createGetUrl($url, $options)
+ {
+ $res = $url;
+ $o = array();
+ foreach ($options as $k => $v) {
+ $o[] = $k . '=' . rawurlencode($v);
+ }
+ if (count($o)) {
+ $res .= '?' . implode('&', $o);
+ }
+ return $res;
+ }
- public static function sameDomain($url, $domain, $site_url, $acceptSubdomain = false)
- {
- if (self::isLocal($url, $site_url)) {
- return true;
- }
+ public static function sameDomain($url, $domain, $site_url, $acceptSubdomain = false)
+ {
+ if (self::isLocal($url, $site_url)) {
+ return true;
+ }
- $host = parse_url($url, PHP_URL_HOST);
- if (!$acceptSubdomain) {
- return $host === $domain;
- }
- return preg_match("/" . $domain . "$/", $host);
- }
+ $host = parse_url($url, PHP_URL_HOST);
+ if (!$acceptSubdomain) {
+ return $host === $domain;
+ }
+ return preg_match("/" . $domain . "$/", $host);
+ }
- public static function getFilePath($url, $site_url, $public_path)
- {
- if (self::isLocal($url, $site_url)) {
- return $public_path . $url;
- }
- }
+ public static function getFilePath($url, $site_url, $public_path)
+ {
+ if (self::isLocal($url, $site_url)) {
+ return $public_path . $url;
+ }
+ }
+
+ public static function linkIfisURL($str, $target = "_self", $rel = "noopenner")
+ {
+ if (self::isDistant($str)) {
+ return '<a href="' . e($str) . '" target="' . $target . '" rel="' . $rel . '">' . e($str) . '</a>';
+ }
+ return $str;
+ }
}