| 1 |
<?php |
| 2 |
|
| 3 |
function acym_absoluteURL(string $text): string |
| 4 |
{ |
| 5 |
static $mainurl = ''; |
| 6 |
if (empty($mainurl)) { |
| 7 |
$urls = parse_url(ACYM_LIVE); |
| 8 |
if (!empty($urls['path'])) { |
| 9 |
$mainurl = substr(ACYM_LIVE, 0, strrpos(ACYM_LIVE, $urls['path'])).'/'; |
| 10 |
} else { |
| 11 |
$mainurl = ACYM_LIVE; |
| 12 |
} |
| 13 |
} |
| 14 |
|
| 15 |
$text = str_replace( |
| 16 |
[ |
| 17 |
'href="../undefined/', |
| 18 |
'href="../../undefined/', |
| 19 |
'href="../../../undefined//', |
| 20 |
'href="undefined/', |
| 21 |
ACYM_LIVE.'http://', |
| 22 |
ACYM_LIVE.'https://', |
| 23 |
], |
| 24 |
[ |
| 25 |
'href="'.$mainurl, |
| 26 |
'href="'.$mainurl, |
| 27 |
'href="'.$mainurl, |
| 28 |
'href="'.ACYM_LIVE, |
| 29 |
'http://', |
| 30 |
'https://', |
| 31 |
], |
| 32 |
$text |
| 33 |
); |
| 34 |
$text = preg_replace('#href="(/?administrator)?/({|%7B)#Ui', 'href="$2', $text); |
| 35 |
|
| 36 |
$text = preg_replace('#href="http:/([^/])#Ui', 'href="http://$1', $text); |
| 37 |
|
| 38 |
$text = preg_replace( |
| 39 |
'#href="'.preg_quote(str_replace(['http://', 'https://'], '', $mainurl), '#').'#Ui', |
| 40 |
'href="'.$mainurl, |
| 41 |
$text |
| 42 |
); |
| 43 |
|
| 44 |
$replace = []; |
| 45 |
$replaceBy = []; |
| 46 |
if ($mainurl !== ACYM_LIVE) { |
| 47 |
|
| 48 |
$replace[] = '#(href|src|action|background)[ ]*=[ ]*\"(?!(\{|%7B|\[|\#|\\\\|[a-z]{3,15}:|/))(?:\.\./)#i'; |
| 49 |
$replaceBy[] = '$1="'.substr(ACYM_LIVE, 0, strrpos(rtrim(ACYM_LIVE, '/'), '/') + 1); |
| 50 |
|
| 51 |
|
| 52 |
$subfolder = substr(ACYM_LIVE, strrpos(rtrim(ACYM_LIVE, '/'), '/')); |
| 53 |
$replace[] = '#(href|src|action|background)[ ]*=[ ]*\"'.preg_quote($subfolder, '#').'(\{|%7B)#i'; |
| 54 |
$replaceBy[] = '$1="$2'; |
| 55 |
} |
| 56 |
|
| 57 |
$replace[] = '#(href|src|action|background)[ ]*=[ ]*\"(?!(\{|%7B|\[|\#|\\\\|[a-z]{3,15}:|/))(?:\.\./|\./)?#i'; |
| 58 |
$replaceBy[] = '$1="'.ACYM_LIVE; |
| 59 |
$replace[] = '#(href|src|action|background)[ ]*=[ ]*\"(?!(\{|%7B|\[|\#|\\\\|[a-z]{3,15}:))/#i'; |
| 60 |
$replaceBy[] = '$1="'.$mainurl; |
| 61 |
|
| 62 |
$replace[] = '#((?:background-image|background)[ ]*:[ ]*url\((?:\'|"|")?(?!(\\\\|[a-z]{3,15}:|/|\'|"|"))(?:\.\./|\./)?)#i'; |
| 63 |
$replaceBy[] = '$1'.ACYM_LIVE; |
| 64 |
|
| 65 |
return preg_replace($replace, $replaceBy, $text); |
| 66 |
} |
| 67 |
|
| 68 |
function acym_mainURL(&$link) |
| 69 |
{ |
| 70 |
static $mainurl = ''; |
| 71 |
static $otherarguments = false; |
| 72 |
if (empty($mainurl)) { |
| 73 |
$urls = parse_url(ACYM_LIVE); |
| 74 |
if (isset($urls['path']) && strlen($urls['path']) > 0) { |
| 75 |
$mainurl = substr(ACYM_LIVE, 0, strrpos(ACYM_LIVE, $urls['path'])).'/'; |
| 76 |
$otherarguments = trim(str_replace($mainurl, '', ACYM_LIVE), '/'); |
| 77 |
if (strlen($otherarguments) > 0) { |
| 78 |
$otherarguments .= '/'; |
| 79 |
} |
| 80 |
} else { |
| 81 |
$mainurl = ACYM_LIVE; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
if ($otherarguments && strpos($link, $otherarguments) === false) { |
| 86 |
$link = $otherarguments.$link; |
| 87 |
} |
| 88 |
|
| 89 |
return $mainurl; |
| 90 |
} |
| 91 |
|
| 92 |
function acym_currentURL(): string |
| 93 |
{ |
| 94 |
$protocol = isset($_SERVER['HTTPS']) || !empty($_SERVER['HTTP_UPGRADE_INSECURE_REQUESTS']) ? 'https' : 'http'; |
| 95 |
$host = $_SERVER['HTTP_HOST'] ?? ($_SERVER['SERVER_NAME'] ?? getenv('HTTP_HOST')); |
| 96 |
|
| 97 |
return $protocol.'://'.$host.$_SERVER['REQUEST_URI']; |
| 98 |
} |
| 99 |
|
| 100 |
function acym_cleanUrl(string $url, array $parametersToRemove): string |
| 101 |
{ |
| 102 |
$parts = parse_url($url); |
| 103 |
|
| 104 |
if (empty($parts['query'])) { |
| 105 |
return $url; |
| 106 |
} |
| 107 |
|
| 108 |
$queryParams = []; |
| 109 |
parse_str($parts['query'], $queryParams); |
| 110 |
|
| 111 |
foreach ($parametersToRemove as $parameter) { |
| 112 |
unset($queryParams[$parameter]); |
| 113 |
} |
| 114 |
|
| 115 |
return $parts['scheme'].'://'.$parts['host'].$parts['path'].'?'.http_build_query($queryParams); |
| 116 |
} |
| 117 |
|
| 118 |
function acym_isLocalWebsite(): bool |
| 119 |
{ |
| 120 |
return strpos(ACYM_LIVE, 'localhost') !== false || strpos(ACYM_LIVE, '127.0.0.1') !== false; |
| 121 |
} |
| 122 |
|
| 123 |
function acym_internalUrlToPath(string $url): string |
| 124 |
{ |
| 125 |
$base = str_replace(['http://www.', 'https://www.', 'http://', 'https://'], '', ACYM_LIVE); |
| 126 |
$replacements = ['https://www.'.$base, 'http://www.'.$base, 'https://'.$base, 'http://'.$base]; |
| 127 |
foreach ($replacements as $oneReplacement) { |
| 128 |
if (strpos($url, $oneReplacement) === false) { |
| 129 |
continue; |
| 130 |
} |
| 131 |
|
| 132 |
return str_replace([$oneReplacement, '/'], [ACYM_ROOT, DS], urldecode($url)); |
| 133 |
} |
| 134 |
|
| 135 |
return $url; |
| 136 |
} |
| 137 |
|
| 138 |
function acym_isValidUrl($url): bool |
| 139 |
{ |
| 140 |
if (empty(ini_get('allow_url_fopen'))) { |
| 141 |
return true; |
| 142 |
} |
| 143 |
if (strpos($url, 'youtu.be') !== false) { |
| 144 |
return true; |
| 145 |
} |
| 146 |
|
| 147 |
$headers = @get_headers($url); |
| 148 |
|
| 149 |
return !empty($headers) && strpos($headers[0], '200'); |
| 150 |
} |
| 151 |
|
| 152 |
function acym_isImageUrl(string $url): bool |
| 153 |
{ |
| 154 |
$extension = strtolower(pathinfo($url, PATHINFO_EXTENSION)); |
| 155 |
|
| 156 |
return in_array($extension, acym_getImageFileExtensions()); |
| 157 |
} |
| 158 |
|