| 1 |
<?php |
| 2 |
defined('ABSPATH') || die('Restricted Access'); |
| 3 |
|
| 4 |
function acym_absoluteURL(string $text): string |
| 5 |
{ |
| 6 |
static $mainurl = ''; |
| 7 |
if (empty($mainurl)) { |
| 8 |
$urls = acym_parseUrl(ACYM_LIVE); |
| 9 |
if (!empty($urls['path'])) { |
| 10 |
$mainurl = substr(ACYM_LIVE, 0, strrpos(ACYM_LIVE, $urls['path'])).'/'; |
| 11 |
} else { |
| 12 |
$mainurl = ACYM_LIVE; |
| 13 |
} |
| 14 |
} |
| 15 |
|
| 16 |
//It will remove the undefined thing added by tinyMCE |
| 17 |
// And URL with twice the domain |
| 18 |
$text = str_replace( |
| 19 |
[ |
| 20 |
'href="../undefined/', |
| 21 |
'href="../../undefined/', |
| 22 |
'href="../../../undefined//', |
| 23 |
'href="undefined/', |
| 24 |
ACYM_LIVE.'http://', |
| 25 |
ACYM_LIVE.'https://', |
| 26 |
], |
| 27 |
[ |
| 28 |
'href="'.$mainurl, |
| 29 |
'href="'.$mainurl, |
| 30 |
'href="'.$mainurl, |
| 31 |
'href="'.ACYM_LIVE, |
| 32 |
'http://', |
| 33 |
'https://', |
| 34 |
], |
| 35 |
$text |
| 36 |
); |
| 37 |
//We remove errors with /administrator links and our tags |
| 38 |
//We replace /{ by { , it's also a bug with the editor... |
| 39 |
$text = preg_replace('#href="(/?administrator)?/({|%7B)#Ui', 'href="$2', $text); |
| 40 |
|
| 41 |
//We replace http:/ by http:// , it will avoid a user bug! seriously we are nice guys... |
| 42 |
$text = preg_replace('#href="http:/([^/])#Ui', 'href="http://$1', $text); |
| 43 |
|
| 44 |
//Sometimes clients add links without http:// but directly with their website url... let's try to catch some of these errors |
| 45 |
$text = preg_replace( |
| 46 |
'#href="'.preg_quote(str_replace(['http://', 'https://'], '', $mainurl), '#').'#Ui', |
| 47 |
'href="'.$mainurl, |
| 48 |
$text |
| 49 |
); |
| 50 |
|
| 51 |
$replace = []; |
| 52 |
$replaceBy = []; |
| 53 |
//We don't convert urls starting with { or [ into absolute url otherwise it could break a tag |
| 54 |
//WE don't modify links starting with \\ as it replaces the protocole http / https |
| 55 |
if ($mainurl !== ACYM_LIVE) { |
| 56 |
//url like ../ your site... |
| 57 |
//We don't transform mailto: # http:// ... |
| 58 |
|
| 59 |
$replace[] = '#(href|src|action|background)[ ]*=[ ]*\"(?!(\{|%7B|\[|\#|\\\\|[a-z]{3,15}:|/))(?:\.\./)#i'; |
| 60 |
$replaceBy[] = '$1="'.substr(ACYM_LIVE, 0, strrpos(rtrim(ACYM_LIVE, '/'), '/') + 1); |
| 61 |
|
| 62 |
//sub folder : substr(ACYM_LIVE,strrpos(rtrim(ACYM_LIVE,'/'),'/')) |
| 63 |
//We remove the sub folder if there is a tag... otherwise we will break the whole thing. |
| 64 |
//We had an issue with that when selecting the readonline link via the front-end |
| 65 |
|
| 66 |
$subfolder = substr(ACYM_LIVE, strrpos(rtrim(ACYM_LIVE, '/'), '/')); |
| 67 |
$replace[] = '#(href|src|action|background)[ ]*=[ ]*\"'.preg_quote($subfolder, '#').'(\{|%7B)#i'; |
| 68 |
$replaceBy[] = '$1="$2'; |
| 69 |
} |
| 70 |
|
| 71 |
$replace[] = '#(href|src|action|background)[ ]*=[ ]*\"(?!(\{|%7B|\[|\#|\\\\|[a-z]{3,15}:|/))(?:\.\./|\./)?#i'; |
| 72 |
$replaceBy[] = '$1="'.ACYM_LIVE; |
| 73 |
$replace[] = '#(href|src|action|background)[ ]*=[ ]*\"(?!(\{|%7B|\[|\#|\\\\|[a-z]{3,15}:))/#i'; |
| 74 |
$replaceBy[] = '$1="'.$mainurl; |
| 75 |
|
| 76 |
//background images for div |
| 77 |
$replace[] = '#((?:background-image|background)[ ]*:[ ]*url\((?:\'|"|")?(?!(\\\\|[a-z]{3,15}:|/|\'|"|"))(?:\.\./|\./)?)#i'; |
| 78 |
$replaceBy[] = '$1'.ACYM_LIVE; |
| 79 |
|
| 80 |
return preg_replace($replace, $replaceBy, $text); |
| 81 |
} |
| 82 |
|
| 83 |
function acym_mainURL(string &$link): string |
| 84 |
{ |
| 85 |
static $baseUrl = ''; |
| 86 |
static $otherArguments = false; |
| 87 |
if (empty($baseUrl)) { |
| 88 |
$urls = acym_parseUrl(ACYM_LIVE); |
| 89 |
if (isset($urls['path']) && strlen($urls['path']) > 0) { |
| 90 |
$baseUrl = substr(ACYM_LIVE, 0, strrpos(ACYM_LIVE, $urls['path'])).'/'; |
| 91 |
$otherArguments = trim(str_replace($baseUrl, '', ACYM_LIVE), '/'); |
| 92 |
if (strlen($otherArguments) > 0) { |
| 93 |
$otherArguments .= '/'; |
| 94 |
} |
| 95 |
} else { |
| 96 |
$baseUrl = ACYM_LIVE; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
if ($otherArguments && strpos($link, $otherArguments) === false) { |
| 101 |
$link = $otherArguments.$link; |
| 102 |
} |
| 103 |
|
| 104 |
return $baseUrl; |
| 105 |
} |
| 106 |
|
| 107 |
function acym_currentURL(): string |
| 108 |
{ |
| 109 |
$protocol = isset($_SERVER['HTTPS']) || !empty($_SERVER['HTTP_UPGRADE_INSECURE_REQUESTS']) ? 'https' : 'http'; |
| 110 |
|
| 111 |
$httpHost = acym_getVar('string', 'HTTP_HOST', '', 'SERVER'); |
| 112 |
$serverName = acym_getVar('string', 'SERVER_NAME', '', 'SERVER'); |
| 113 |
|
| 114 |
$host = empty($httpHost) |
| 115 |
? ( |
| 116 |
empty($serverName) |
| 117 |
? getenv('HTTP_HOST') |
| 118 |
: $serverName |
| 119 |
) |
| 120 |
: $httpHost; |
| 121 |
|
| 122 |
$requestUri = acym_getVar('string', 'REQUEST_URI', '', 'SERVER'); |
| 123 |
|
| 124 |
return $protocol.'://'.$host.$requestUri; |
| 125 |
} |
| 126 |
|
| 127 |
function acym_cleanUrl(string $url, array $parametersToRemove): string |
| 128 |
{ |
| 129 |
$parts = acym_parseUrl($url); |
| 130 |
|
| 131 |
if (empty($parts['query'])) { |
| 132 |
return $url; |
| 133 |
} |
| 134 |
|
| 135 |
$queryParams = []; |
| 136 |
parse_str($parts['query'], $queryParams); |
| 137 |
|
| 138 |
foreach ($parametersToRemove as $parameter) { |
| 139 |
unset($queryParams[$parameter]); |
| 140 |
} |
| 141 |
|
| 142 |
return $parts['scheme'].'://'.$parts['host'].$parts['path'].'?'.http_build_query($queryParams); |
| 143 |
} |
| 144 |
|
| 145 |
function acym_isLocalWebsite(): bool |
| 146 |
{ |
| 147 |
return strpos(ACYM_LIVE, 'localhost') !== false || strpos(ACYM_LIVE, '127.0.0.1') !== false; |
| 148 |
} |
| 149 |
|
| 150 |
function acym_internalUrlToPath(string $url): string |
| 151 |
{ |
| 152 |
$base = str_replace(['http://www.', 'https://www.', 'http://', 'https://'], '', ACYM_LIVE); |
| 153 |
$replacements = ['https://www.'.$base, 'http://www.'.$base, 'https://'.$base, 'http://'.$base]; |
| 154 |
foreach ($replacements as $oneReplacement) { |
| 155 |
$position = strpos($url, $oneReplacement); |
| 156 |
if ($position === false) { |
| 157 |
continue; |
| 158 |
} |
| 159 |
|
| 160 |
// Remove parameters |
| 161 |
$relativeUrl = substr($url, $position + strlen($oneReplacement)); |
| 162 |
$relativeUrl = preg_replace('/[?#].*$/', '', $relativeUrl); |
| 163 |
|
| 164 |
// Remove encoded / and . |
| 165 |
$segments = []; |
| 166 |
foreach (explode('/', $relativeUrl) as $oneSegment) { |
| 167 |
$decodedSegment = urldecode($oneSegment); |
| 168 |
if (preg_match('#[/\\\\?\#]#', $decodedSegment) === 1 || in_array($decodedSegment, ['.', '..'], true)) { |
| 169 |
return $url; |
| 170 |
} |
| 171 |
$segments[] = $decodedSegment; |
| 172 |
} |
| 173 |
|
| 174 |
return ACYM_ROOT.implode(DS, $segments); |
| 175 |
} |
| 176 |
|
| 177 |
return $url; |
| 178 |
} |
| 179 |
|
| 180 |
function acym_isValidUrl(string $url): bool |
| 181 |
{ |
| 182 |
// If this option is not activated in the php.ini file, we return true because the function get_headers will not work |
| 183 |
if (empty(ini_get('allow_url_fopen'))) { |
| 184 |
return true; |
| 185 |
} |
| 186 |
// If we detect youtu.be url, we don't check the headers because it's a redirection to a valid Url |
| 187 |
if (strpos($url, 'youtu.be') !== false) { |
| 188 |
return true; |
| 189 |
} |
| 190 |
|
| 191 |
$headers = @get_headers($url); |
| 192 |
|
| 193 |
return !empty($headers) && strpos($headers[0], '200'); |
| 194 |
} |
| 195 |
|
| 196 |
function acym_isImageUrl(string $url): bool |
| 197 |
{ |
| 198 |
$extension = strtolower(pathinfo($url, PATHINFO_EXTENSION)); |
| 199 |
|
| 200 |
return in_array($extension, acym_getImageFileExtensions(true)); |
| 201 |
} |
| 202 |
|