dolibarr 24.0.3
geturl.lib.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2008-2020 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2025-2026 Frédéric France <frederic.france@free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 * or see https://www.gnu.org/
19 */
20
51function getURLContent($url, $postorget = 'GET', $param = '', $followlocation = 1, $addheaders = array(), $allowedschemes = array('http', 'https'), $localurl = 0, $ssl_verifypeer = -1, $timeoutconnect = 0, $timeoutresponse = 0, $otherCurlOptions = array(), $morelogsuffix = '')
52{
53 // Get global variables for proxy use
54 $USE_PROXY = getDolGlobalInt('MAIN_PROXY_USE');
55 $PROXY_HOST = getDolGlobalString('MAIN_PROXY_HOST');
56 $PROXY_PORT = getDolGlobalInt('MAIN_PROXY_PORT');
57 $PROXY_USER = getDolGlobalString('MAIN_PROXY_USER');
58 $PROXY_PASS = getDolGlobalString('MAIN_PROXY_PASS');
59
60 dol_syslog("getURLContent postorget=".$postorget." URL=".$url);
61 if (getDolGlobalInt('MAIN_CURL_DEBUG')) {
62 dol_syslog("getURLContent postorget=".$postorget." URL=".$url." json_encode(param)=".json_encode($param), LOG_DEBUG, 0, '_curl');
63 }
64 if ($morelogsuffix) {
65 dol_syslog("getURLContent postorget=".$postorget." URL=".$url." json_encode(param)=".json_encode($param), LOG_DEBUG, 0, $morelogsuffix);
66 }
67
68 if (!function_exists('curl_init')) {
69 if (getDolGlobalInt('MAIN_CURL_DEBUG')) {
70 dol_syslog("getURLContent PHP curl library must be installed", LOG_DEBUG, 0, '_curl');
71 }
72 if ($morelogsuffix) {
73 dol_syslog("getURLContent PHP curl library must be installed", LOG_DEBUG, 0, $morelogsuffix);
74 }
75
76 return array('http_code' => 500, 'content' => '', 'curl_error_no' => 1, 'curl_error_msg' => 'PHP curl library must be installed');
77 }
78
79 //setting the curl parameters.
80 $ch = curl_init();
81
82 /*print $API_Endpoint."-".$API_version."-".$PAYPAL_API_USER."-".$PAYPAL_API_PASSWORD."-".$PAYPAL_API_SIGNATURE."<br>";
83 print $USE_PROXY."-".$gv_ApiErrorURL."<br>";
84 print $nvpStr;
85 exit;*/
86 curl_setopt($ch, CURLOPT_VERBOSE, true);
87 curl_setopt($ch, CURLOPT_USERAGENT, 'Dolibarr geturl function'); // set the Dolibarr user agent name
88
89 // We use @ here because this may return warning if safe mode is on or open_basedir is on (following location is forbidden when safe mode is on).
90 // We force value to false so we will manage redirection ourself later.
91 @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
92
93 if (is_array($addheaders) && count($addheaders)) {
94 curl_setopt($ch, CURLOPT_HTTPHEADER, $addheaders);
95 }
96 curl_setopt($ch, CURLINFO_HEADER_OUT, true); // To be able to retrieve request header and log it
97
98 if (getDolGlobalInt('MAIN_CURL_GET_RESPONSE_HEADER')) {
99 curl_setopt($ch, CURLOPT_HEADER, true); // To be able to retrieve response header
100 }
101
102 // By default use the TLS version decided by PHP.
103 // You can force, if supported a version like TLSv1 or TLSv1.2
104 if (getDolGlobalString('MAIN_CURL_SSLVERSION')) {
105 $sslversion = is_numeric(getDolGlobalString('MAIN_CURL_SSLVERSION')) ? getDolGlobalInt('MAIN_CURL_SSLVERSION') : constant(getDolGlobalString('MAIN_CURL_SSLVERSION'));
106 curl_setopt($ch, CURLOPT_SSLVERSION, (int) $sslversion);
107 }
108 //curl_setopt($ch, CURLOPT_SSLVERSION, 6); for tls 1.2
109
110 // Turning on or off the ssl target certificate
111 if ($ssl_verifypeer < 0) {
112 global $dolibarr_main_prod;
113 $ssl_verifypeer = ($dolibarr_main_prod ? true : false);
114 }
115 if (getDolGlobalString('MAIN_CURL_DISABLE_VERIFYPEER')) {
116 $ssl_verifypeer = 0;
117 }
118
119 // Turning off the server and peer verification(TrustManager Concept).
120 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, ($ssl_verifypeer ? true : false));
121
122 // 0 to not check the names
123 // 1 to check the existence of a common name in the SSL peer certificate
124 // 2 to check the existence of a common name and also verify that it matches the hostname provided.
125 // In production environments the value of this option should be kept at 2 (default value).
126 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, ($ssl_verifypeer ? 2 : 0));
127
128 // Restrict use to some protocols only
129 $protocols = 0;
130 $redir_list = array();
131 if (is_array($allowedschemes)) {
132 foreach ($allowedschemes as $allowedscheme) {
133 if ($allowedscheme == 'http') {
134 $protocols |= CURLPROTO_HTTP;
135 $redir_list["HTTP"] = 1;
136 } elseif ($allowedscheme == 'https') {
137 $protocols |= CURLPROTO_HTTPS;
138 $redir_list["HTTPS"] = 1;
139 } elseif ($allowedscheme == 'ftp') {
140 $protocols |= CURLPROTO_FTP;
141 $redir_list["FTP"] = 1;
142 } elseif ($allowedscheme == 'ftps') {
143 $protocols |= CURLPROTO_FTPS;
144 $redir_list["FTPS"] = 1;
145 }
146 }
147 } else {
148 return array('http_code' => 500, 'content' => '', 'curl_error_no' => 1, 'curl_error_msg' => 'Parameter allowedschemes of getURLContent must be an array of protocol schemes');
149 }
150
151 $newtimeoutconnect = ($timeoutconnect ? $timeoutconnect : getDolGlobalInt('MAIN_USE_CONNECT_TIMEOUT', 5));
152 $newtimeoutresponse = ($timeoutresponse ? $timeoutresponse : getDolGlobalInt('MAIN_USE_RESPONSE_TIMEOUT', 30));
153
154 if (getDolGlobalInt('MAIN_CURL_DEBUG')) {
155 dol_syslog("getURLContent newtimeoutconnect=".$newtimeoutconnect." newtimeoutresponse=".$newtimeoutresponse, LOG_DEBUG, 0, '_curl');
156 }
157 if ($morelogsuffix) {
158 dol_syslog("getURLContent newtimeoutconnect=".$newtimeoutconnect." newtimeoutresponse=".$newtimeoutresponse, LOG_DEBUG, 0, $morelogsuffix);
159 }
160
161 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $newtimeoutconnect); // Timeout for connection
162 curl_setopt($ch, CURLOPT_TIMEOUT, $newtimeoutresponse); // Timeout for total time including connection
163
164 // limit size of downloaded files.
165 $maxsize = getDolGlobalInt('MAIN_SECURITY_MAXFILESIZE_DOWNLOADED');
166 if ($maxsize && defined('CURLOPT_MAXFILESIZE_LARGE')) {
167 curl_setopt($ch, CURLOPT_MAXFILESIZE_LARGE, $maxsize * 1024); // @phan-suppress-current-line PhanTypeMismatchArgumentNullableInternal
168 }
169 if ($maxsize && defined('CURLOPT_MAXFILESIZE')) {
170 curl_setopt($ch, CURLOPT_MAXFILESIZE, $maxsize * 1024);
171 }
172
173 //curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true); // PHP 5.5
174 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // We want response
175 if ($postorget == 'POST') {
176 curl_setopt($ch, CURLOPT_POST, true); // POST
177 curl_setopt($ch, CURLOPT_POSTFIELDS, $param); // Setting param x=a&y=z as POST fields
178 } elseif ($postorget == 'POSTALREADYFORMATED') {
179 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); // HTTP request is 'POST' but param string is taken as it is
180 curl_setopt($ch, CURLOPT_POSTFIELDS, $param); // param = content of post, like a xml string
181 } elseif ($postorget == 'PUT') {
182 $array_param = array();
183 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); // HTTP request is 'PUT'
184 if (!is_array($param)) {
185 parse_str($param, $array_param);
186 } else {
187 dol_syslog("parameter param must be a string", LOG_WARNING);
188 $array_param = $param;
189 }
190 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($array_param)); // Setting param x=a&y=z as PUT fields
191 } elseif ($postorget == 'PUTALREADYFORMATED') {
192 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); // HTTP request is 'PUT'
193 curl_setopt($ch, CURLOPT_POSTFIELDS, $param); // param = content of post, like a xml string
194 } elseif ($postorget == 'PATCH') {
195 $array_param = array();
196 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH'); // RFC 5789
197 if (!is_array($param)) {
198 parse_str($param, $array_param);
199 } else {
200 dol_syslog("parameter param must be a string", LOG_WARNING);
201 $array_param = $param;
202 }
203 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($array_param));
204 } elseif ($postorget == 'PATCHALREADYFORMATED') {
205 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH'); // RFC 5789
206 curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
207 } elseif ($postorget == 'HEAD') {
208 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD'); // HTTP request is 'HEAD'
209 curl_setopt($ch, CURLOPT_NOBODY, true);
210 curl_setopt($ch, CURLOPT_HEADER, true);
211 } elseif ($postorget == 'DELETE') {
212 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); // POST
213 } else {
214 curl_setopt($ch, CURLOPT_POST, false); // GET
215 }
216
217 //if USE_PROXY constant set at begin of this method.
218 if ($USE_PROXY) {
219 dol_syslog("getURLContent set proxy to ".$PROXY_HOST.":".$PROXY_PORT." - ".$PROXY_USER.":".$PROXY_PASS);
220 //curl_setopt ($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); // Curl 7.10
221 curl_setopt($ch, CURLOPT_PROXY, $PROXY_HOST.":".$PROXY_PORT);
222 if ($PROXY_USER) {
223 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $PROXY_USER.":".$PROXY_PASS);
224 }
225 }
226
227 if (is_array($otherCurlOptions)) {
228 foreach ($otherCurlOptions as $option => $value) {
229 curl_setopt($ch, $option, $value);
230 }
231 }
232
233 $newUrl = $url;
234 $maxRedirection = 5;
235 $info = array();
236 $response = '';
237
238 do {
239 if ($maxRedirection < 1) {
240 if (getDolGlobalInt('MAIN_CURL_DEBUG')) {
241 dol_syslog("getURLContent http_code=400 Maximum number of redirections reached", LOG_DEBUG, 0, '_curl');
242 }
243 return array('http_code' => 400, 'content' => 'Maximum number of redirections reached', 'curl_error_no' => 1, 'curl_error_msg' => 'Maximum number of redirections reached');
244 }
245
246 curl_setopt($ch, CURLOPT_URL, $newUrl);
247
248 // Parse $newUrl
249 $newUrlArray = parse_url($newUrl);
250 $hosttocheck = $newUrlArray['host'] ?: $newUrlArray['path'];
251 $hosttocheck = str_replace(array('[', ']'), '', $hosttocheck); // Remove brackets of IPv6
252
253 // Deny some reserved host names
254 if (in_array($hosttocheck, array('metadata.google.internal'))) {
255 $info['http_code'] = 400;
256 $info['content'] = 'Error bad hostname '.$hosttocheck.' (Used by Google metadata). This value for hostname is not allowed.';
257 if (getDolGlobalInt('MAIN_CURL_DEBUG')) {
258 dol_syslog("getURLContent http_code=400 ".$info['content'], LOG_DEBUG, 0, '_curl');
259 }
260 return array('http_code' => 400, 'content' => $info['content'], 'curl_error_no' => 1, 'curl_error_msg' => $info['content']);
261 }
262
263 /* Discard full numeric hostname */
264 if (preg_match('/^[x0-9a-f]+$/', $hosttocheck)) {
265 return array('http_code' => 400, 'content' => '', 'curl_error_no' => 1, 'curl_error_msg' => 'Host is a numeric address that is not allowed');
266 }
267
268 // Clean host name $hosttocheck to convert it into an IP $iptocheck
269 if (in_array($hosttocheck, array('localhost', 'localhost.domain'))) {
270 $iptocheck = '127.0.0.1';
271 } elseif (in_array($hosttocheck, array('ip6-localhost', 'ip6-loopback'))) {
272 $iptocheck = '::1';
273 } else {
274 // Resolve $hosttocheck to get the IP $iptocheck
275 // Not that a bad numeric hostname like 2130706433 will be resolved int 127.0.0.1 but
276 // this case is filtered previously.
277 $iptocheck = resolveDns($hosttocheck);
278 }
279
280 // Check $iptocheck is an IP (v4 or v6), if not clear value.
281 if (!filter_var($iptocheck, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) { // This is not an IP
282 $iptocheck = '0'; // will disabled check on IP
283 return array('http_code' => 400, 'content' => '', 'curl_error_no' => 1, 'curl_error_msg' => 'Host is a numeric address that is not allowed');
284 }
285
286 if ($iptocheck) {
287 $tmpresult = isIPAllowed($iptocheck, $localurl);
288 if ($tmpresult) {
289 $info['http_code'] = 400;
290 $info['content'] = $tmpresult;
291 if (getDolGlobalInt('MAIN_CURL_DEBUG')) {
292 dol_syslog("getURLContent http_code=400 ".$info['content'], LOG_DEBUG, 0, '_curl');
293 }
294 return array('http_code' => 400, 'content' => $tmpresult, 'curl_error_no' => 1, 'curl_error_msg' => $tmpresult);
295 }
296 }
297
298 if ($iptocheck) {
299 // Set CURLOPT_CONNECT_TO so curl will not try another resolution that may give a different result. Possible only on PHP v7+
300 // Format is "host:port:ip:port". Port fields MUST use %s, not %d: an empty port (default 80/443) must stay empty so it matches "any port" and pins to the same port. With %d the empty string becomes 0 ("host:0:ip:0"), never matches the real port, and libcurl ignores the pin, re-opening a DNS-rebinding SSRF bypass.
301 if (defined('CURLOPT_CONNECT_TO')) {
302 $connect_to = array(sprintf("%s:%s:%s:%s", $newUrlArray['host'], empty($newUrlArray['port']) ? '' : $newUrlArray['port'], $iptocheck, empty($newUrlArray['port']) ? '' : $newUrlArray['port']));
303 //var_dump($newUrlArray);
304 //var_dump($connect_to);
305 curl_setopt($ch, CURLOPT_CONNECT_TO, $connect_to);
306 }
307 }
308
309 // Moving these just before the curl_exec option really limits
310 // on windows PHP 7.4.
311 curl_setopt($ch, CURLOPT_PROTOCOLS, $protocols);
312 curl_setopt($ch, CURLOPT_REDIR_PROTOCOLS, $protocols);
313 /* CURLOPT_REDIR_PROTOCOLS_STR available from PHP 7.85.0
314 if (version_compare(PHP_VERSION, '8.3.0', '>=') && version_compare(curl_version()['version'], '7.85.0', '>=')) {
315 curl_setopt($ch, CURLOPT_REDIR_PROTOCOLS_STR, implode(",", array_keys($redir_list)));
316 }
317 */
318
319 // Getting response from server
320 $response = curl_exec($ch); // return false on error, result on success
321
322 $info = curl_getinfo($ch); // Reading of request must be done after sending request
323 $http_code = $info['http_code'];
324
325 if ($followlocation && ($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307)) {
326 $newUrl = $info['redirect_url'];
327 $maxRedirection--;
328 // TODO Use $info['local_ip'] and $info['primary_ip'] ?
329 continue;
330 }
331
332 $http_code = 0;
333 } while ($http_code); // Stop if http_code is 0
334
335 $request = curl_getinfo($ch, CURLINFO_HEADER_OUT); // Reading of request must be done after sending request
336
337 dol_syslog("getURLContent request without content body=".$request);
338 if (getDolGlobalInt('MAIN_CURL_DEBUG')) {
339 // This may contains binary data, so we don't output response by default.
340 dol_syslog("getURLContent request without body=".$request, LOG_DEBUG, 0, '_curl');
341 dol_syslog("getURLContent response=".$response, LOG_DEBUG, 0, '_curl');
342 }
343 if ($morelogsuffix) {
344 // This may contains binary data, so we don't output response by default.
345 dol_syslog("getURLContent request without body=".$request, LOG_DEBUG, 0, $morelogsuffix);
346 dol_syslog("getURLContent response=".$response, LOG_DEBUG, 0, $morelogsuffix);
347 }
348
349 dol_syslog("getURLContent response size=".strlen($response)); // This $response may contains binary data, so we don't output it
350
351 $rep = array();
352 if (curl_errno($ch)) {
353 // Add keys to $rep
354 if ($response) {
355 $rep['content'] = (string) $response;
356 } else {
357 $rep['content'] = '';
358 }
359
360 $rep['http_code'] = 0;
361 $rep['curl_error_no'] = curl_errno($ch);
362 $rep['curl_error_msg'] = curl_error($ch);
363
364 dol_syslog("getURLContent response array is ".implode(',', $rep));
365
366 if (getDolGlobalInt('MAIN_CURL_DEBUG')) {
367 dol_syslog("getURLContent curl_error_no=".$rep['curl_error_no']." curl_error_msg=".$rep['curl_error_msg'], LOG_DEBUG, 0, '_curl');
368 }
369 if ($morelogsuffix) {
370 dol_syslog("getURLContent curl_error_no=".$rep['curl_error_no']." curl_error_msg=".$rep['curl_error_msg'], LOG_DEBUG, 0, $morelogsuffix);
371 }
372 } else {
373 //$info = curl_getinfo($ch);
374
375 // Return all fields found into $info.
376 $rep = $info;
377 //$rep['header_size'] = $info['header_size'];
378 //$rep['http_code'] = $info['http_code'];
379 //$rep['content_type'] = $info['http_code'];
380
381 dol_syslog("getURLContent http_code=".$rep['http_code']);
382
383 // Add more keys to $rep
384 if ($response) {
385 $rep['content'] = (string) $response;
386 if ($postorget == 'HEAD' || getDolGlobalInt('MAIN_CURL_GET_RESPONSE_HEADER')) { // In this case, response contains header + body
387 $rep['header'] = substr($rep['content'], 0, intval($rep['header_size']));
388 $rep['content'] = substr($rep['content'], intval($rep['header_size']));
389 }
390 } else {
391 $rep['content'] = '';
392 }
393
394 $rep['curl_error_no'] = 0;
395 $rep['curl_error_msg'] = '';
396 }
397
398 //closing the curl
399 curl_close($ch);
400
401 // We must exclude phpstant wwarning, because all fields found in result of curl_getinfo may not be all defined into description of this method.
402 // @phpstan-ignore-next-line
403 return $rep;
404}
405
406
413function resolveDns($hosttocheck)
414{
415 $iptocheck = null;
416
417 // Resolve $hosttocheck to get the IP $iptocheck
418 if (function_exists('dns_get_record') && !getDolGlobalString('MAIN_DISABLE_DNS_GET_RECORD_FOR_IP_RESOLUTION')) {
419 try {
420 $records = dns_get_record($hosttocheck, DNS_A + DNS_AAAA);
421
422 if (!empty($records[0]) && is_array($records[0]) && !empty($records[0]['ip'])) { // We take the first one
423 $iptocheck = $records[0]['ip'];
424 } elseif (!empty($records[0]) && is_array($records[0]) && !empty($records[0]['ipv6'])) { // We take the first one
425 $iptocheck = $records[0]['ipv6'];
426 }
427 } catch (Exception $e) {
428 // Nothing done
429 }
430 } elseif (function_exists('gethostbyname')) { // resolve only ipv4
431 $iptocheck = gethostbyname($hosttocheck);
432 } else {
433 $iptocheck = $hosttocheck;
434 }
435
436 if ($iptocheck === null) {
437 $iptocheck = $hosttocheck;
438 }
439 return $iptocheck;
440}
441
442
451{
452 if (strpos($ip, ':') === false) {
453 return '';
454 }
455 // Native strlen()/substr() are used on purpose: $packed is binary data, not a text string.
456 $packed = @inet_pton($ip);
457 if ($packed === false || strlen($packed) != 16) {
458 return '';
459 }
460
461 $ipv4bytes = '';
462 if (substr($packed, 0, 10) === str_repeat("\x00", 10) && substr($packed, 10, 2) === "\xff\xff") {
463 $ipv4bytes = substr($packed, 12, 4); // ::ffff:0:0/96 IPv4-mapped
464 } elseif (substr($packed, 0, 12) === str_repeat("\x00", 12) && substr($packed, 12, 3) !== str_repeat("\x00", 3)) {
465 $ipv4bytes = substr($packed, 12, 4); // ::/96 IPv4-compatible (:: and ::1 are excluded, filter_var() knows them as reserved)
466 } elseif (substr($packed, 0, 12) === "\x00\x64\xff\x9b".str_repeat("\x00", 8)) {
467 $ipv4bytes = substr($packed, 12, 4); // 64:ff9b::/96 NAT64
468 } elseif (substr($packed, 0, 2) === "\x20\x02") {
469 $ipv4bytes = substr($packed, 2, 4); // 2002::/16 6to4
470 }
471
472 return ($ipv4bytes !== '' ? (string) inet_ntop($ipv4bytes) : '');
473}
474
475
483function isIPAllowed($iptocheck, $localurl)
484{
485 // An IPv6 that carries an IPv4 (like ::ffff:127.0.0.1) reaches this IPv4, but filter_var() does not see it as private or reserved.
486 // So checks are done on the carried IPv4.
487 $embeddedipv4 = getIPv4EmbeddedInIPv6($iptocheck);
488 if ($embeddedipv4 !== '') {
489 $iptocheck = $embeddedipv4;
490 }
491
492 if ($localurl == 0) { // Only external url allowed (dangerous, may allow to get malware)
493 if (!filter_var($iptocheck, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
494 // Deny ips like 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, 0.0.0.0/8, 169.254.0.0/16, 127.0.0.0/8 et 240.0.0.0/4, ::1/128, ::/128, fe80::/10...
495 $errormsg = 'Error bad hostname IP (private or reserved range). Must be an external URL.';
496 return $errormsg;
497 }
498 // Ranges not known as reserved by filter_var(): shared address space 100.64.0.0/10 (carriers, clouds) and benchmarking 198.18.0.0/15.
499 $iplong = ip2long($iptocheck);
500 if ($iplong !== false && ((($iplong & 0xFFC00000) === 0x64400000) || (($iplong & 0xFFFE0000) === 0xC6120000))) {
501 $errormsg = 'Error bad hostname IP (shared or benchmarking range). Must be an external URL.';
502 return $errormsg;
503 }
504 if (!empty($_SERVER["SERVER_ADDR"]) && $iptocheck == $_SERVER["SERVER_ADDR"]) {
505 $errormsg = 'Error bad hostname IP (IP is a local IP). Must be an external URL.';
506 return $errormsg;
507 }
508 if (getDolGlobalString('MAIN_SECURITY_ANTI_SSRF_SERVER_IP') && in_array($iptocheck, explode(',', getDolGlobalString('MAIN_SECURITY_ANTI_SSRF_SERVER_IP')))) {
509 $errormsg = 'Error bad hostname IP (IP is a local IP defined into MAIN_SECURITY_SERVER_IP). Must be an external URL.';
510 return $errormsg;
511 }
512 }
513 if ($localurl == 1) { // Only local url allowed (dangerous, may allow to get metadata on server or make internal port scanning)
514 // Deny ips NOT like 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, 0.0.0.0/8, 169.254.0.0/16, 127.0.0.0/8 et 240.0.0.0/4, ::1/128, ::/128, ::ffff:0:0/96, fe80::/10...
515 if (filter_var($iptocheck, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
516 $errormsg = 'Error bad hostname '.$iptocheck.'. Must be a local URL.';
517 return $errormsg;
518 }
519 if (getDolGlobalString('MAIN_SECURITY_ANTI_SSRF_SERVER_IP') && !in_array($iptocheck, explode(',', getDolGlobalString('MAIN_SECURITY_ANTI_SSRF_SERVER_IP')))) {
520 $errormsg = 'Error bad hostname IP (IP is not a local IP defined into list MAIN_SECURITY_SERVER_IP). Must be a local URL in allowed list.';
521 return $errormsg;
522 }
523 }
524
525 // Common check on ip (local and external)
526 // See list on https://tagmerge.com/gist/a7b9d57ff8ec11d63642f8778609a0b8
527 // Not evasive url that ar enot IP are excluded by test on IP v4/v6 validity.
528 $arrayofmetadataserver = array(
529 '100.100.100.200' => 'Alibaba',
530 '192.0.0.192' => 'Oracle',
531 '192.80.8.124' => 'Packet',
532 '100.88.222.5' => 'Tencent cloud',
533 );
534 foreach ($arrayofmetadataserver as $ipofmetadataserver => $nameofmetadataserver) {
535 if ($iptocheck == $ipofmetadataserver) {
536 $errormsg = 'Error bad hostname IP (Used by '.$nameofmetadataserver.' metadata server). This IP is forbidden.';
537 return $errormsg;
538 }
539 }
540
541 return '';
542}
543
554function getDomainFromURL($url, $mode = 0)
555{
556 $arrayof2levetopdomain = array(
557 'co.at', 'or.at', 'gv.at',
558 'avocat.fr', 'aeroport.fr', 'veterinaire.fr',
559 'com.ng', 'gov.ng', 'gov.ua', 'com.ua', 'in.ua', 'org.ua', 'edu.ua', 'net.ua',
560 'net.uk', 'org.uk', 'gov.uk', 'co.uk',
561 'com.mx'
562 );
563
564 // Set if tld is on 2 levels
565 $tldon2level = 0;
566 $parts = array_reverse(explode('.', $url));
567 if (!empty($parts[1]) && in_array($parts[1].'.'.$parts[0], $arrayof2levetopdomain)) {
568 $tldon2level = 1;
569 }
570
571 if ($tldon2level && $mode > 0) {
572 $mode++;
573 }
574
575 $tmpdomain = preg_replace('/^https?:\/\/[^:]+:[^@]+@/i', '', $url); // Remove http(s)://login@pass in https://login@pass:mydomain.com/path, so we now got mydomain.com/path
576 $tmpdomain = preg_replace('/^https?:\/\//i', '', $tmpdomain); // Remove http(s)://
577 $tmpdomain = preg_replace('/\/.*$/i', '', $tmpdomain); // Remove part after /
578 $tmpdomain = preg_replace('/^[^@]+@/i', '', $tmpdomain); // Remove part1@ in part1@part2 (for emails)
579 if ($mode == 3) {
580 $tmpdomain = preg_replace('/^.*\.([^\.]+)\.([^\.]+)\.([^\.]+)\.([^\.]+)$/', '\1.\2.\3.\4', $tmpdomain);
581 } elseif ($mode == 2) {
582 $tmpdomain = preg_replace('/^.*\.([^\.]+)\.([^\.]+)\.([^\.]+)$/', '\1.\2.\3', $tmpdomain); // Remove part 'www.' before 'abc.mydomain.com'
583 } elseif ($mode == 1) {
584 $tmpdomain = preg_replace('/^.*\.([^\.]+)\.([^\.]+)$/', '\1.\2', $tmpdomain); // Remove part 'www.abc.' before 'mydomain.com'
585 }
586
587 if (empty($mode)) {
588 if ($tldon2level) {
589 $tmpdomain = preg_replace('/^.*\.([^\.]+)\.([^\.]+)\.([^\.]+)$/', '\1.\2.\3', $tmpdomain); // Remove part 'www.abc.' before 'mydomain.com'
590 $tmpdomain = preg_replace('/\.[^\.]+\.[^\.]+$/', '', $tmpdomain); // Remove TLD (.com.mx, .co.uk, ...)
591 } else {
592 $tmpdomain = preg_replace('/^.*\.([^\.]+)\.([^\.]+)$/', '\1.\2', $tmpdomain); // Remove part 'www.abc.' before 'mydomain.com'
593 $tmpdomain = preg_replace('/\.[^\.]+$/', '', $tmpdomain); // Remove TLD (.com, .net, ...)
594 }
595 }
596
597 return $tmpdomain;
598}
599
610function getRootURLFromURL($url)
611{
612 return preg_replace('/^([a-z]*:\/\/[^\/]*).*/i', '$1', $url);
613}
614
621function removeHtmlComment($content)
622{
623 $content = preg_replace('/<!--[^\-]+-->/', '', $content);
624 return $content;
625}
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getDomainFromURL($url, $mode=0)
Function get second level domain name.
isIPAllowed($iptocheck, $localurl)
Is IP allowed.
getIPv4EmbeddedInIPv6($ip)
Return the IPv4 address carried by an IPv6 address, if any (the IPv6 address is then a way to reach t...
getRootURLFromURL($url)
Function root url from a long url For example: https://www.abc.mydomain.com/dir/page....
resolveDns($hosttocheck)
Resolve a hostname into its IP.
removeHtmlComment($content)
Function to remove comments into HTML content.
getURLContent($url, $postorget='GET', $param='', $followlocation=1, $addheaders=array(), $allowedschemes=array('http', 'https'), $localurl=0, $ssl_verifypeer=-1, $timeoutconnect=0, $timeoutresponse=0, $otherCurlOptions=array(), $morelogsuffix='')
Function to get a content from an URL (use proxy if proxy defined).
print $langs trans("Show") . '< td style="' . $timeColor . '" align="center"> s</td > badge status0 badge status4 badge status3 Error badge status8< td align="center">< span class="badge ' . $badge . '"></span ></td >< td align="center">< a href="#" class="button button-small" onclick="openLogModal(this)" data-req="' . dol_escape_htmltag($reqSafe) . '" data-res="' . dol_escape_htmltag($resSafe) . '" data-err="' . dol_escape_htmltag($errSafe) . '">< span class="fa fa-search-plus"></span ></a ></td ></tr >< tr >< td colspan="' . $colspan . '" class="opacitymedium"></td ></tr ></table ></div ></form > logModal none logModal none s a JSON string
buildzip.php