function getRealMacAddress($portalfolder) {
global $suserData;
$macFilePath = __DIR__ . "/$portalfolder/working_macs.json";
$clientIP = getClientIP();
$workingMacs = [];
$currentTime = time();
$expiryTime = 24 * 60 * 60;
$portalURL = '';
foreach ($suserData as $portal) {
if ($portal['portalfolder'] === $portalfolder) {
$portalURL = $portal['portal'];
break;
}
}
if (!$portalURL) {
sendErrorResponse(500, "Portal URL not found for $portalfolder.");
}
if (file_exists($macFilePath)) {
$jsonData = file_get_contents($macFilePath);
$workingMacs = json_decode($jsonData, true);
}
foreach ($workingMacs as $ip => $data) {
if ($currentTime - $data['timestamp'] > $expiryTime) {
unset($workingMacs[$ip]);
}
}
if (isset($workingMacs[$clientIP])) {
$workingMac = $workingMacs[$clientIP]['mac'];
$playerApiUrl = $portalURL . "/player_api.php?action=handshake&mac=" . urlencode($workingMac);
$responsePlayerApi = @file_get_contents($playerApiUrl);
$portalApiUrl = $portalURL . "/portal.php?type=stb&action=handshake&mac=" . urlencode($workingMac);
$responsePortalApi = @file_get_contents($portalApiUrl);
if (($responsePlayerApi !== false && strpos($responsePlayerApi, 'valid response') !== false) ||
($responsePortalApi !== false && strpos($responsePortalApi, 'valid response') !== false)) {
return $workingMac;
}
unset($workingMacs[$clientIP]);
}
$macListFilePath = __DIR__ . "/$portalfolder/{$portalfolder}mac.txt";
if (!file_exists($macListFilePath)) {
sendErrorResponse(500, "MAC address file not found for $portalfolder.");
}
$macs = file($macListFilePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if (empty($macs)) {
sendErrorResponse(500, "No valid MAC addresses found for $portalfolder.");
}
shuffle($macs);
$newMac = $macs[0];
$workingMacs[$clientIP] = [
'mac' => $newMac,
'timestamp' => $currentTime
];
file_put_contents($macFilePath, json_encode($workingMacs, JSON_PRETTY_PRINT));
return $newMac;
}