From 50caa6186b26642965d017d9e075d898b447c06a Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 14 Jun 2023 11:42:52 +0200 Subject: [PATCH] wip #6014 @0.5 --- src/Files/VirtualDirectory.php | 2 +- src/Url.php | 120 ++++++++++++++++++--------------- 2 files changed, 65 insertions(+), 57 deletions(-) diff --git a/src/Files/VirtualDirectory.php b/src/Files/VirtualDirectory.php index 3d2d4c9..607a2fc 100644 --- a/src/Files/VirtualDirectory.php +++ b/src/Files/VirtualDirectory.php @@ -26,7 +26,7 @@ class VirtualDirectory public function copy($from, $to, $prepend = false) { if (!file_exists($from)) { - throw new \Exception(sprintf('File %s doesn\'t exist (to %s)', $from, $to)); + throw new \Exception(sprintf('File "%s" doesn\'t exist (to %s)', $from, $to)); } $realto = $this->path($to); if (!$realto) { diff --git a/src/Url.php b/src/Url.php index 1a83c0f..d273c6e 100644 --- a/src/Url.php +++ b/src/Url.php @@ -4,74 +4,82 @@ namespace Cubist\Util; 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 '' . e($str) . ''; + } + return $str; + } } -- 2.39.5