From 2b468e6b0f8c8240e69acedb6f5a4e2a347ead9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Sat, 12 Apr 2025 16:19:45 +0200 Subject: [PATCH] enhance dol_print_ip (#33819) * enhance dol_print_ip * fix cache * fix CI --- htdocs/core/class/commonobject.class.php | 2 +- htdocs/core/lib/functions.lib.php | 27 ++++++++++++++++++------ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index cd4973c07ac..9efa79bcf33 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -1256,7 +1256,7 @@ abstract class CommonObject return -2; } - if ($this->restrictiononfksoc && property_exists('socid', $this) && !empty($this->socid) && !$user->hasRight('societe', 'client', 'voir')) { + if ($this->restrictiononfksoc && property_exists($this, 'socid') && !empty($this->socid) && !$user->hasRight('societe', 'client', 'voir')) { $sql_allowed_contacts = 'SELECT COUNT(*) as cnt FROM '.$this->db->prefix().'societe_commerciaux as sc'; $sql_allowed_contacts.= ' WHERE sc.fk_soc = '.(int) $this->socid; $sql_allowed_contacts.= ' AND sc.fk_user = '.(int) $user->id; diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 8eb808892b2..f8f0c7b863c 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -4769,27 +4769,42 @@ function dol_print_phone($phone, $countrycode = '', $cid = 0, $socid = 0, $addli */ function dol_print_ip($ip, $mode = 0) { - global $langs; + global $conf, $langs; $ret = ''; - - if (empty($mode)) { - $ret .= $ip; + if (!isset($conf->cache['resolveips'])) { + $conf->cache['resolveips'] = []; } if ($mode != 2) { $countrycode = dolGetCountryCodeFromIp($ip); if ($countrycode) { // If success, countrycode is us, fr, ... if (file_exists(DOL_DOCUMENT_ROOT.'/theme/common/flags/'.$countrycode.'.png')) { - $ret .= ' '.img_picto($countrycode.' '.$langs->trans("AccordingToGeoIPDatabase"), DOL_URL_ROOT.'/theme/common/flags/'.$countrycode.'.png', '', 1); + $ret .= picto_from_langcode($countrycode); + // $ret .= img_picto($countrycode.' '.$langs->trans("AccordingToGeoIPDatabase"), DOL_URL_ROOT.'/theme/common/flags/'.$countrycode.'.png', '', 1); } else { - $ret .= ' ('.$countrycode.')'; + $ret .= '('.$countrycode.')'; } + $ret .= ' '; } else { // Nothing } } + if (in_array($mode, [0, 2])) { + if (empty($conf->cache['resolveips'][$ip])) { + $domain = gethostbyaddr($ip); + $conf->cache['resolveips'][$ip] = $domain; // false or domain + } else { + $domain = $conf->cache['resolveips'][$ip]; + } + if ($domain) { + $ret .= $domain; + } else { + $ret .= $ip; + } + } + return $ret; }