0, '911' => 0, 'OUT' => 0, 'INVALID' => 0, 'MAX' => 0 ]; function logMessage($msg, $stdout = true) { global $logFile; $line = date('Y-m-d H:i:s') . " " . $msg . "\n"; file_put_contents($logFile, $line, FILE_APPEND); if ($stdout) { echo $line; } } // Normalise un CID function normalizeCid($cid, $label = '', $extId = 0){ global $counters; if (!$cid) return false; $original = $cid; $cid = preg_replace('/\D/', '', $cid); // ne garder que les chiffres // Si 11 chiffres commençant par 1, retirer le 1 if (strlen($cid) == 11 && $cid[0] == '1') { $cid = substr($cid,1); } if (preg_match('/^[2-9][0-9]{2}[2-9][0-9]{6}$/', $cid)) { return $cid; } // Invalide logMessage("[INVALID] $original -> $cid ext_id: $extId", true); $counters['INVALID']++; return false; } // Fonction pour traiter chaque section function processSection($section, $cidFields) { global $telephony, $counters; $infos = $telephony->getExtensions($section); $total = count($infos); $processed = 0; foreach ($infos as $id => $info) { $changed = false; $extId = $info['ext_id'] ?? $id; // Utilisation de 'ext_id' si disponible, sinon fallback sur $id // Correction spéciale MAX et MIN foreach (['line_out_callerid_invalid_max', 'line_out_callerid_invalid_min'] as $key) { if (isset($info[$key]) && $info[$key] != 9) { $info[$key] = 9; logMessage("CHANGE $key TO 9 for ext_id $extId"); $counters['MAX']++; $changed = true; } } // Normalisation des CIDs foreach ($cidFields as $field => $label) { $cid = $info[$field] ?? false; $ncid = normalizeCid($cid, $label, $extId); if ($ncid && $cid != $ncid) { $info[$field] = $ncid; logMessage("[$label] $cid -> $ncid ext_id: $extId"); $counters[$label]++; $changed = true; } } if ($changed) { $telephony->saveExtension($info); } // Progression en pourcentage $processed++; $percent = round(($processed / $total) * 100); echo "\rProcessing $section: $processed/$total ($percent%)"; flush(); } echo "\n"; // nouvelle ligne après la section } // Section phone processSection('phone', [ 'phone_cid_external_number' => 'EXT', 'phone_cid_emergency_number' => '911' ]); // Section line_out processSection('line_out', [ 'line_out_callerid_num' => 'OUT', 'line_out_callerid_invalid_num' => 'OUT', 'line_out_cid_blacklist_dest_cid' => 'OUT' ]); // Résumé final logMessage("\n=== Résumé ==="); logMessage("EXT fixés : " . $counters['EXT']); logMessage("911 fixés : " . $counters['911']); logMessage("OUT fixés : " . $counters['OUT']); logMessage("MAX corrigés : " . $counters['MAX']); logMessage("INVALID : " . $counters['INVALID']);