29 list($absUri, $base) = self::normalizePaths(
30 self::removeProtocols(self::normalizeSlashes([$absUri, $base])));
33 if (!preg_match(
'/\/$/', $base)) {
36 $absArray = explode(
'/', $absUri);
37 $baseArray = explode(
'/', $base);
41 if (strrpos($absUri,
'/') !== strlen($absUri)) {
42 $fileName = array_pop($absArray);
44 if (strrpos($base,
'/') !== strlen($base)) {
45 array_pop($baseArray);
49 while (
sizeof($absArray) > 0 &&
sizeof($baseArray) > 0 && $absArray[0] == $baseArray[0]) {
50 array_shift($absArray);
51 array_shift($baseArray);
55 $relUri = self::normalizePaths(str_repeat(
'../',
sizeof($baseArray)).join(
'/', $absArray).
'/'.$fileName);
67 list($relUri, $base) = self::normalizeSlashes([$relUri, $base]);
70 if (parse_url($relUri, PHP_URL_SCHEME) !=
'') {
74 if (!preg_match(
'/\/$/', $base)) {
77 $firstChar = (strlen($relUri) > 0) ? substr($relUri, 0, 1) :
'';
79 if ($firstChar ==
'#' || $firstChar ==
'?') {
83 extract(parse_url($base));
84 $scheme = !isset($scheme) ?
'' : $scheme.
'://';
85 $host = !isset($host) ?
'' : $host;
86 $path = !isset($path) ?
'' : $path;
88 $path = preg_replace(
'#/[^/]*$#',
'', $path);
90 if ($firstChar ==
'/') {
94 $abs =
"$host$path/$relUri";
96 $abs = self::normalizePaths($abs);
111 public static function translate($pathFromA, $pathFromScriptToA) {
112 list($pathFromA, $pathFromScriptToA) = self::normalizeSlashes([$pathFromA, $pathFromScriptToA]);
115 $path = dirname($self).
'/';
119 return [
'absolute' => $absUrl,
'relative' => $relUrl];
130 $urlParts = @parse_url($url);
132 if (empty($urlParts[
"host"])) {
133 $fh = @fopen($url,
"r");
134 return ($fh !==
false);
139 curl_setopt($ch, CURLOPT_URL, $url);
140 curl_setopt($ch, CURLOPT_HEADER,
true);
141 curl_setopt($ch, CURLOPT_NOBODY,
true);
142 curl_setopt($ch, CURLOPT_RETURNTRANSFER,
true);
143 curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
145 $headers = split(
"\n", $r);
147 preg_match(
'/.+ ([0-9]{3}) .+/', $headers[0], $matches);
148 return (intval($matches[1]) < 400);
157 $_SERVER[
'HTTP_X_FORWARDED_PROTO'] ??
158 $_SERVER[
'REQUEST_SCHEME'] ??
162 return $isHttps && (strcasecmp(
'on', $isHttps) == 0 || strcasecmp(
'https', $isHttps) == 0);
177 private static function normalizeSlashes($paths) {
178 return preg_replace([
"/\\\\/"], [
'/'], $paths);
185 private static function removeProtocols($paths) {
186 return preg_replace([
"/^[^:]{1}:/"], [
''], $paths);
193 private static function normalizePaths($paths) {
194 $re = [
'#(/\.?/)#',
'#/(?!\.\.)[^/]+/\.\./#'];
195 for ($n=1; $n>0; $paths=preg_replace($re,
'/', $paths, -1, $n)) {}