| @@ -1,11 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | +defined('ABSPATH') || die('Restricted Access'); | |
| 2 | 3 | |
| 3 | 4 | function acym_absoluteURL(string $text): string |
| 4 | 5 | { |
| 5 | 6 | static $mainurl = ''; |
| 6 | 7 | if (empty($mainurl)) { |
| 7 | - $urls = parse_url(ACYM_LIVE); | |
| 8 | + $urls = acym_parseUrl(ACYM_LIVE); | |
| 8 | 9 | if (!empty($urls['path'])) { |
| 9 | 10 | $mainurl = substr(ACYM_LIVE, 0, strrpos(ACYM_LIVE, $urls['path'])).'/'; |
| 10 | 11 | } else { |
| 11 | 12 | $mainurl = ACYM_LIVE; |
| @@ -11,8 +12,10 @@ | ||
| 11 | 12 | $mainurl = ACYM_LIVE; |
| 12 | 13 | } |
| 13 | 14 | } |
| 14 | 15 | |
| 16 | + //It will remove the undefined thing added by tinyMCE | |
| 17 | + // And URL with twice the domain | |
| 15 | 18 | $text = str_replace( |
| 16 | 19 | [ |
| 17 | 20 | 'href="../undefined/', |
| 18 | 21 | 'href="../../undefined/', |
| @@ -30,12 +33,16 @@ | ||
| 30 | 33 | 'https://', |
| 31 | 34 | ], |
| 32 | 35 | $text |
| 33 | 36 | ); |
| 37 | + //We remove errors with /administrator links and our tags | |
| 38 | + //We replace /{ by { , it's also a bug with the editor... | |
| 34 | 39 | $text = preg_replace('#href="(/?administrator)?/({|%7B)#Ui', 'href="$2', $text); |
| 35 | 40 | |
| 41 | + //We replace http:/ by http:// , it will avoid a user bug! seriously we are nice guys... | |
| 36 | 42 | $text = preg_replace('#href="http:/([^/])#Ui', 'href="http://$1', $text); |
| 37 | 43 | |
| 44 | + //Sometimes clients add links without http:// but directly with their website url... let's try to catch some of these errors | |
| 38 | 45 | $text = preg_replace( |
| 39 | 46 | '#href="'.preg_quote(str_replace(['http://', 'https://'], '', $mainurl), '#').'#Ui', |
| 40 | 47 | 'href="'.$mainurl, |
| 41 | 48 | $text |
| @@ -42,13 +49,20 @@ | ||
| 42 | 49 | ); |
| 43 | 50 | |
| 44 | 51 | $replace = []; |
| 45 | 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 | |
| 46 | 55 | if ($mainurl !== ACYM_LIVE) { |
| 56 | + //url like ../ your site... | |
| 57 | + //We don't transform mailto: # http:// ... | |
| 47 | 58 | |
| 48 | 59 | $replace[] = '#(href|src|action|background)[ ]*=[ ]*\"(?!(\{|%7B|\[|\#|\\\\|[a-z]{3,15}:|/))(?:\.\./)#i'; |
| 49 | 60 | $replaceBy[] = '$1="'.substr(ACYM_LIVE, 0, strrpos(rtrim(ACYM_LIVE, '/'), '/') + 1); |
| 50 | 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 | |
| 51 | 65 | |
| 52 | 66 | $subfolder = substr(ACYM_LIVE, strrpos(rtrim(ACYM_LIVE, '/'), '/')); |
| 53 | 67 | $replace[] = '#(href|src|action|background)[ ]*=[ ]*\"'.preg_quote($subfolder, '#').'(\{|%7B)#i'; |
| 54 | 68 | $replaceBy[] = '$1="$2'; |
| @@ -58,8 +72,9 @@ | ||
| 58 | 72 | $replaceBy[] = '$1="'.ACYM_LIVE; |
| 59 | 73 | $replace[] = '#(href|src|action|background)[ ]*=[ ]*\"(?!(\{|%7B|\[|\#|\\\\|[a-z]{3,15}:))/#i'; |
| 60 | 74 | $replaceBy[] = '$1="'.$mainurl; |
| 61 | 75 | |
| 76 | + //background images for div | |
| 62 | 77 | $replace[] = '#((?:background-image|background)[ ]*:[ ]*url\((?:\'|"|")?(?!(\\\\|[a-z]{3,15}:|/|\'|"|"))(?:\.\./|\./)?)#i'; |
| 63 | 78 | $replaceBy[] = '$1'.ACYM_LIVE; |
| 64 | 79 | |
| 65 | 80 | return preg_replace($replace, $replaceBy, $text); |
| @@ -64,43 +79,55 @@ | ||
| 64 | 79 | |
| 65 | 80 | return preg_replace($replace, $replaceBy, $text); |
| 66 | 81 | } |
| 67 | 82 | |
| 68 | -function acym_mainURL(&$link) | |
| 83 | +function acym_mainURL(string &$link): string | |
| 69 | 84 | { |
| 70 | - static $mainurl = ''; | |
| 71 | - static $otherarguments = false; | |
| 72 | - if (empty($mainurl)) { | |
| 73 | - $urls = parse_url(ACYM_LIVE); | |
| 85 | + static $baseUrl = ''; | |
| 86 | + static $otherArguments = false; | |
| 87 | + if (empty($baseUrl)) { | |
| 88 | + $urls = acym_parseUrl(ACYM_LIVE); | |
| 74 | 89 | 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 .= '/'; | |
| 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 .= '/'; | |
| 79 | 94 | } |
| 80 | 95 | } else { |
| 81 | - $mainurl = ACYM_LIVE; | |
| 96 | + $baseUrl = ACYM_LIVE; | |
| 82 | 97 | } |
| 83 | 98 | } |
| 84 | 99 | |
| 85 | - if ($otherarguments && strpos($link, $otherarguments) === false) { | |
| 86 | - $link = $otherarguments.$link; | |
| 100 | + if ($otherArguments && strpos($link, $otherArguments) === false) { | |
| 101 | + $link = $otherArguments.$link; | |
| 87 | 102 | } |
| 88 | 103 | |
| 89 | - return $mainurl; | |
| 104 | + return $baseUrl; | |
| 90 | 105 | } |
| 91 | 106 | |
| 92 | 107 | function acym_currentURL(): string |
| 93 | 108 | { |
| 94 | 109 | $protocol = isset($_SERVER['HTTPS']) || !empty($_SERVER['HTTP_UPGRADE_INSECURE_REQUESTS']) ? 'https' : 'http'; |
| 95 | - $host = $_SERVER['HTTP_HOST'] ?? ($_SERVER['SERVER_NAME'] ?? getenv('HTTP_HOST')); | |
| 96 | 110 | |
| 97 | - return $protocol.'://'.$host.$_SERVER['REQUEST_URI']; | |
| 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; | |
| 98 | 125 | } |
| 99 | 126 | |
| 100 | 127 | function acym_cleanUrl(string $url, array $parametersToRemove): string |
| 101 | 128 | { |
| 102 | - $parts = parse_url($url); | |
| 129 | + $parts = acym_parseUrl($url); | |
| 103 | 130 | |
| 104 | 131 | if (empty($parts['query'])) { |
| 105 | 132 | return $url; |
| 106 | 133 | } |
| @@ -124,23 +151,40 @@ | ||
| 124 | 151 | { |
| 125 | 152 | $base = str_replace(['http://www.', 'https://www.', 'http://', 'https://'], '', ACYM_LIVE); |
| 126 | 153 | $replacements = ['https://www.'.$base, 'http://www.'.$base, 'https://'.$base, 'http://'.$base]; |
| 127 | 154 | foreach ($replacements as $oneReplacement) { |
| 128 | - if (strpos($url, $oneReplacement) === false) { | |
| 155 | + $position = strpos($url, $oneReplacement); | |
| 156 | + if ($position === false) { | |
| 129 | 157 | continue; |
| 130 | 158 | } |
| 131 | 159 | |
| 132 | - return str_replace([$oneReplacement, '/'], [ACYM_ROOT, DS], urldecode($url)); | |
| 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); | |
| 133 | 175 | } |
| 134 | 176 | |
| 135 | 177 | return $url; |
| 136 | 178 | } |
| 137 | 179 | |
| 138 | -function acym_isValidUrl($url): bool | |
| 180 | +function acym_isValidUrl(string $url): bool | |
| 139 | 181 | { |
| 182 | + // If this option is not activated in the php.ini file, we return true because the function get_headers will not work | |
| 140 | 183 | if (empty(ini_get('allow_url_fopen'))) { |
| 141 | 184 | return true; |
| 142 | 185 | } |
| 186 | + // If we detect youtu.be url, we don't check the headers because it's a redirection to a valid Url | |
| 143 | 187 | if (strpos($url, 'youtu.be') !== false) { |
| 144 | 188 | return true; |
| 145 | 189 | } |
| 146 | 190 | |
| @@ -152,6 +196,6 @@ | ||
| 152 | 196 | function acym_isImageUrl(string $url): bool |
| 153 | 197 | { |
| 154 | 198 | $extension = strtolower(pathinfo($url, PATHINFO_EXTENSION)); |
| 155 | 199 | |
| 156 | - return in_array($extension, acym_getImageFileExtensions()); | |
| 200 | + return in_array($extension, acym_getImageFileExtensions(true)); | |
| 157 | 201 | } |