dolibarr 24.0.3
phone.lib.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2026 Open-Dsi <support@open-dsi.fr>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
37function dol_parse_phone($phone, $db = null)
38{
39 $phone = trim((string) $phone);
40 if ($phone === '') {
41 return array('code' => '', 'number' => '');
42 }
43
44 if (strpos($phone, '+') === 0) {
45 $pos = strpos($phone, ' ');
46 if ($pos !== false && preg_match('/^\+[0-9]+$/', substr($phone, 0, $pos))) {
47 return array(
48 'code' => substr($phone, 0, $pos),
49 'number' => substr($phone, $pos + 1)
50 );
51 }
52 // Code glued to the number or followed by formatting (e.g. "+33612345678", "+33(0)9 72 36 76 46"):
53 // look for the longest known phone code
54 if ($db !== null) {
55 $digits = preg_replace('/[^0-9]/', '', str_replace('(0)', '', substr($phone, 1)));
56 $phonecodes = dol_get_all_phone_codes($db);
57 for ($len = 4; $len >= 1; $len--) {
58 $candidate = substr($digits, 0, $len);
59 if (strlen($digits) > $len && in_array($candidate, $phonecodes, true)) {
60 return array('code' => '+'.$candidate, 'number' => substr($digits, $len));
61 }
62 }
63 }
64 return array('code' => '', 'number' => $phone);
65 }
66
67 return array('code' => '', 'number' => $phone);
68}
69
77{
78 static $cache = null;
79 if ($cache !== null) {
80 return $cache;
81 }
82
83 $cache = array();
84 $sql = "SELECT DISTINCT phone_code FROM ".$db->prefix()."c_country";
85 $sql .= " WHERE phone_code IS NOT NULL AND phone_code != 0";
86 $resql = $db->query($sql);
87 if ($resql) {
88 while ($obj = $db->fetch_object($resql)) {
89 $cache[] = (string) $obj->phone_code;
90 }
91 $db->free($resql);
92 } else {
93 dol_syslog("dol_get_all_phone_codes: ".$db->lasterror(), LOG_ERR);
94 }
95
96 return $cache;
97}
98
112function dol_build_phone($db, $code, $number)
113{
114 $number = preg_replace('/[\s\-\.\‍(\‍)]/', '', trim((string) $number));
115 $code = trim((string) $code);
116
117 if ($number === '') {
118 return '';
119 }
120
121 if ($code !== '') {
122 // Strip national trunk prefix (e.g. leading "0" for France)
123 $sql = "SELECT trunk_prefix FROM ".$db->prefix()."c_country";
124 $sql .= " WHERE phone_code = ".((int) ltrim($code, '+'));
125 $sql .= " AND trunk_prefix IS NOT NULL AND trunk_prefix != ''";
126 $sql .= " LIMIT 1";
127 $resql = $db->query($sql);
128 if ($resql) {
129 $obj = $db->fetch_object($resql);
130 if ($obj && isset($obj->trunk_prefix) && $obj->trunk_prefix !== '' && $obj->trunk_prefix !== null) {
131 $prefix = $obj->trunk_prefix;
132 if (strpos($number, $prefix) === 0) {
133 $number = substr($number, strlen($prefix));
134 }
135 }
136 $db->free($resql);
137 }
138 }
139
140 if ($code !== '') {
141 return $code.' '.$number;
142 }
143
144 return $number;
145}
146
154function dol_get_trunk_prefix($db, $phone_code)
155{
156 $phone_code = trim((string) $phone_code);
157 if ($phone_code === '') {
158 return '';
159 }
160
161 $sql = "SELECT trunk_prefix FROM ".$db->prefix()."c_country";
162 $sql .= " WHERE phone_code = ".((int) ltrim($phone_code, '+'));
163 $sql .= " AND trunk_prefix IS NOT NULL AND trunk_prefix != ''";
164 $sql .= " LIMIT 1";
165 $resql = $db->query($sql);
166 if ($resql) {
167 $obj = $db->fetch_object($resql);
168 if ($obj && isset($obj->trunk_prefix) && $obj->trunk_prefix !== '' && $obj->trunk_prefix !== null) {
169 $db->free($resql);
170 return $obj->trunk_prefix;
171 }
172 $db->free($resql);
173 }
174
175 return '';
176}
177
186{
187 $country_id = (int) $country_id;
188 if ($country_id <= 0) {
189 return '';
190 }
191
192 $sql = "SELECT phone_code FROM ".$db->prefix()."c_country WHERE rowid = ".((int) $country_id);
193 $resql = $db->query($sql);
194 if ($resql) {
195 $obj = $db->fetch_object($resql);
196 if ($obj && !empty($obj->phone_code)) {
197 $db->free($resql);
198 return '+'.$obj->phone_code;
199 }
200 $db->free($resql);
201 }
202
203 return '';
204}
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
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
dol_get_trunk_prefix($db, $phone_code)
Get the national trunk prefix for a phone code.
dol_build_phone($db, $code, $number)
Build a normalized phone string from code and number parts.
dol_get_all_phone_codes($db)
Get the list of all distinct phone codes known in llx_c_country.
Definition phone.lib.php:76
dol_parse_phone($phone, $db=null)
Parse a stored phone number into country code and number parts.
Definition phone.lib.php:37
dol_get_phone_code_from_country($db, $country_id)
Get the phone calling code for a country.