dolibarr 24.0.0-beta
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 // Clean host name $hosttocheck to convert it into an IP $iptocheck
264 if (in_array($hosttocheck, array('localhost', 'localhost.domain'))) {
265 $iptocheck = '127.0.0.1';
266 } elseif (in_array($hosttocheck, array('ip6-localhost', 'ip6-loopback'))) {
267 $iptocheck = '::1';
268 } else {
269 // Resolve $hosttocheck to get the IP $iptocheck
270 $iptocheck = resolveDns($hosttocheck);
271 }
272
273 // Check $iptocheck is an IP (v4 or v6), if not clear value.
274 if (!filter_var($iptocheck, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) { // This is not an IP, we clean data
275 $iptocheck = '0'; // will disabled check on IP
276 }
277
278 if ($iptocheck) {
279 $tmpresult = isIPAllowed($iptocheck, $localurl);
280 if ($tmpresult) {
281 $info['http_code'] = 400;
282 $info['content'] = $tmpresult;
283 if (getDolGlobalInt('MAIN_CURL_DEBUG')) {
284 dol_syslog("getURLContent http_code=400 ".$info['content'], LOG_DEBUG, 0, '_curl');
285 }
286 return array('http_code' => 400, 'content' => $tmpresult, 'curl_error_no' => 1, 'curl_error_msg' => $tmpresult);
287 }
288 }
289
290 if ($iptocheck) {
291 // Set CURLOPT_CONNECT_TO so curl will not try another resolution that may give a different result. Possible only on PHP v7+
292 if (defined('CURLOPT_CONNECT_TO')) {
293 $connect_to = array(sprintf("%s:%d:%s:%d", $newUrlArray['host'], empty($newUrlArray['port']) ? '' : $newUrlArray['port'], $iptocheck, empty($newUrlArray['port']) ? '' : $newUrlArray['port']));
294 //var_dump($newUrlArray);
295 //var_dump($connect_to);
296 curl_setopt($ch, CURLOPT_CONNECT_TO, $connect_to);
297 }
298 }
299
300 // Moving these just before the curl_exec option really limits
301 // on windows PHP 7.4.
302 curl_setopt($ch, CURLOPT_PROTOCOLS, $protocols);
303 curl_setopt($ch, CURLOPT_REDIR_PROTOCOLS, $protocols);
304 /* CURLOPT_REDIR_PROTOCOLS_STR available from PHP 7.85.0
305 if (version_compare(PHP_VERSION, '8.3.0', '>=') && version_compare(curl_version()['version'], '7.85.0', '>=')) {
306 curl_setopt($ch, CURLOPT_REDIR_PROTOCOLS_STR, implode(",", array_keys($redir_list)));
307 }
308 */
309
310 // Getting response from server
311 $response = curl_exec($ch); // return false on error, result on success
312
313 $info = curl_getinfo($ch); // Reading of request must be done after sending request
314 $http_code = $info['http_code'];
315
316 if ($followlocation && ($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307)) {
317 $newUrl = $info['redirect_url'];
318 $maxRedirection--;
319 // TODO Use $info['local_ip'] and $info['primary_ip'] ?
320 continue;
321 }
322
323 $http_code = 0;
324 } while ($http_code); // Stop if http_code is 0
325
326 $request = curl_getinfo($ch, CURLINFO_HEADER_OUT); // Reading of request must be done after sending request
327
328 dol_syslog("getURLContent request without content body=".$request);
329 if (getDolGlobalInt('MAIN_CURL_DEBUG')) {
330 // This may contains binary data, so we don't output response by default.
331 dol_syslog("getURLContent request without body=".$request, LOG_DEBUG, 0, '_curl');
332 dol_syslog("getURLContent response=".$response, LOG_DEBUG, 0, '_curl');
333 }
334 if ($morelogsuffix) {
335 // This may contains binary data, so we don't output response by default.
336 dol_syslog("getURLContent request without body=".$request, LOG_DEBUG, 0, $morelogsuffix);
337 dol_syslog("getURLContent response=".$response, LOG_DEBUG, 0, $morelogsuffix);
338 }
339
340 dol_syslog("getURLContent response size=".strlen($response)); // This $response may contains binary data, so we don't output it
341
342 $rep = array();
343 if (curl_errno($ch)) {
344 // Add keys to $rep
345 if ($response) {
346 $rep['content'] = (string) $response;
347 } else {
348 $rep['content'] = '';
349 }
350
351 $rep['http_code'] = 0;
352 $rep['curl_error_no'] = curl_errno($ch);
353 $rep['curl_error_msg'] = curl_error($ch);
354
355 dol_syslog("getURLContent response array is ".implode(',', $rep));
356
357 if (getDolGlobalInt('MAIN_CURL_DEBUG')) {
358 dol_syslog("getURLContent curl_error_no=".$rep['curl_error_no']." curl_error_msg=".$rep['curl_error_msg'], LOG_DEBUG, 0, '_curl');
359 }
360 if ($morelogsuffix) {
361 dol_syslog("getURLContent curl_error_no=".$rep['curl_error_no']." curl_error_msg=".$rep['curl_error_msg'], LOG_DEBUG, 0, $morelogsuffix);
362 }
363 } else {
364 //$info = curl_getinfo($ch);
365
366 // Return all fields found into $info.
367 $rep = $info;
368 //$rep['header_size'] = $info['header_size'];
369 //$rep['http_code'] = $info['http_code'];
370 //$rep['content_type'] = $info['http_code'];
371
372 dol_syslog("getURLContent http_code=".$rep['http_code']);
373
374 // Add more keys to $rep
375 if ($response) {
376 $rep['content'] = (string) $response;
377 if ($postorget == 'HEAD' || getDolGlobalInt('MAIN_CURL_GET_RESPONSE_HEADER')) { // In this case, response contains header + body
378 $rep['header'] = substr($rep['content'], 0, intval($rep['header_size']));
379 $rep['content'] = substr($rep['content'], intval($rep['header_size']));
380 }
381 } else {
382 $rep['content'] = '';
383 }
384
385 $rep['curl_error_no'] = 0;
386 $rep['curl_error_msg'] = '';
387 }
388
389 //closing the curl
390 curl_close($ch);
391
392 // We must exclude phpstant wwarning, because all fields found in result of curl_getinfo may not be all defined into description of this method.
393 // @phpstan-ignore-next-line
394 return $rep;
395}
396
397
404function resolveDns($hosttocheck)
405{
406 $iptocheck = null;
407
408 // Resolve $hosttocheck to get the IP $iptocheck
409 if (function_exists('dns_get_record') && !getDolGlobalString('MAIN_DISABLE_DNS_GET_RECORD_FOR_IP_RESOLUTION')) {
410 try {
411 $records = dns_get_record($hosttocheck, DNS_A + DNS_AAAA);
412
413 if (!empty($records[0]) && is_array($records[0]) && !empty($records[0]['ip'])) { // We take the first one
414 $iptocheck = $records[0]['ip'];
415 } elseif (!empty($records[0]) && is_array($records[0]) && !empty($records[0]['ipv6'])) { // We take the first one
416 $iptocheck = $records[0]['ipv6'];
417 }
418 } catch (Exception $e) {
419 // Nothing done
420 }
421 } elseif (function_exists('gethostbyname')) { // resolve only ipv4
422 $iptocheck = gethostbyname($hosttocheck);
423 } else {
424 $iptocheck = $hosttocheck;
425 }
426
427 if ($iptocheck === null) {
428 $iptocheck = $hosttocheck;
429 }
430 return $iptocheck;
431}
432
433
441function isIPAllowed($iptocheck, $localurl)
442{
443 if ($localurl == 0) { // Only external url allowed (dangerous, may allow to get malware)
444 if (!filter_var($iptocheck, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
445 // 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, ::ffff:0:0/96, fe80::/10...
446 $errormsg = 'Error bad hostname IP (private or reserved range). Must be an external URL.';
447 return $errormsg;
448 }
449 if (!empty($_SERVER["SERVER_ADDR"]) && $iptocheck == $_SERVER["SERVER_ADDR"]) {
450 $errormsg = 'Error bad hostname IP (IP is a local IP). Must be an external URL.';
451 return $errormsg;
452 }
453 if (getDolGlobalString('MAIN_SECURITY_ANTI_SSRF_SERVER_IP') && in_array($iptocheck, explode(',', getDolGlobalString('MAIN_SECURITY_ANTI_SSRF_SERVER_IP')))) {
454 $errormsg = 'Error bad hostname IP (IP is a local IP defined into MAIN_SECURITY_SERVER_IP). Must be an external URL.';
455 return $errormsg;
456 }
457 }
458 if ($localurl == 1) { // Only local url allowed (dangerous, may allow to get metadata on server or make internal port scanning)
459 // 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...
460 if (filter_var($iptocheck, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
461 $errormsg = 'Error bad hostname '.$iptocheck.'. Must be a local URL.';
462 return $errormsg;
463 }
464 if (getDolGlobalString('MAIN_SECURITY_ANTI_SSRF_SERVER_IP') && !in_array($iptocheck, explode(',', getDolGlobalString('MAIN_SECURITY_ANTI_SSRF_SERVER_IP')))) {
465 $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.';
466 return $errormsg;
467 }
468 }
469
470 // Common check on ip (local and external)
471 // See list on https://tagmerge.com/gist/a7b9d57ff8ec11d63642f8778609a0b8
472 // Not evasive url that ar enot IP are excluded by test on IP v4/v6 validity.
473 $arrayofmetadataserver = array(
474 '100.100.100.200' => 'Alibaba',
475 '192.0.0.192' => 'Oracle',
476 '192.80.8.124' => 'Packet',
477 '100.88.222.5' => 'Tencent cloud',
478 );
479 foreach ($arrayofmetadataserver as $ipofmetadataserver => $nameofmetadataserver) {
480 if ($iptocheck == $ipofmetadataserver) {
481 $errormsg = 'Error bad hostname IP (Used by '.$nameofmetadataserver.' metadata server). This IP is forbidden.';
482 return $errormsg;
483 }
484 }
485
486 return '';
487}
488
499function getDomainFromURL($url, $mode = 0)
500{
501 $arrayof2levetopdomain = array(
502 'co.at', 'or.at', 'gv.at',
503 'avocat.fr', 'aeroport.fr', 'veterinaire.fr',
504 'com.ng', 'gov.ng', 'gov.ua', 'com.ua', 'in.ua', 'org.ua', 'edu.ua', 'net.ua',
505 'net.uk', 'org.uk', 'gov.uk', 'co.uk',
506 'com.mx'
507 );
508
509 // Set if tld is on 2 levels
510 $tldon2level = 0;
511 $parts = array_reverse(explode('.', $url));
512 if (!empty($parts[1]) && in_array($parts[1].'.'.$parts[0], $arrayof2levetopdomain)) {
513 $tldon2level = 1;
514 }
515
516 if ($tldon2level && $mode > 0) {
517 $mode++;
518 }
519
520 $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
521 $tmpdomain = preg_replace('/^https?:\/\//i', '', $tmpdomain); // Remove http(s)://
522 $tmpdomain = preg_replace('/\/.*$/i', '', $tmpdomain); // Remove part after /
523 $tmpdomain = preg_replace('/^[^@]+@/i', '', $tmpdomain); // Remove part1@ in part1@part2 (for emails)
524 if ($mode == 3) {
525 $tmpdomain = preg_replace('/^.*\.([^\.]+)\.([^\.]+)\.([^\.]+)\.([^\.]+)$/', '\1.\2.\3.\4', $tmpdomain);
526 } elseif ($mode == 2) {
527 $tmpdomain = preg_replace('/^.*\.([^\.]+)\.([^\.]+)\.([^\.]+)$/', '\1.\2.\3', $tmpdomain); // Remove part 'www.' before 'abc.mydomain.com'
528 } elseif ($mode == 1) {
529 $tmpdomain = preg_replace('/^.*\.([^\.]+)\.([^\.]+)$/', '\1.\2', $tmpdomain); // Remove part 'www.abc.' before 'mydomain.com'
530 }
531
532 if (empty($mode)) {
533 if ($tldon2level) {
534 $tmpdomain = preg_replace('/^.*\.([^\.]+)\.([^\.]+)\.([^\.]+)$/', '\1.\2.\3', $tmpdomain); // Remove part 'www.abc.' before 'mydomain.com'
535 $tmpdomain = preg_replace('/\.[^\.]+\.[^\.]+$/', '', $tmpdomain); // Remove TLD (.com.mx, .co.uk, ...)
536 } else {
537 $tmpdomain = preg_replace('/^.*\.([^\.]+)\.([^\.]+)$/', '\1.\2', $tmpdomain); // Remove part 'www.abc.' before 'mydomain.com'
538 $tmpdomain = preg_replace('/\.[^\.]+$/', '', $tmpdomain); // Remove TLD (.com, .net, ...)
539 }
540 }
541
542 return $tmpdomain;
543}
544
555function getRootURLFromURL($url)
556{
557 return preg_replace('/^([a-z]*:\/\/[^\/]*).*/i', '$1', $url);
558}
559
566function removeHtmlComment($content)
567{
568 $content = preg_replace('/<!--[^\-]+-->/', '', $content);
569 return $content;
570}
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.
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