<?php session_start();if(!isset($_SESSION['authenticated'])||$_SESSION['authenticated']!==true){header('HTTP/1.1 403 Forbidden');exit('Non autorisé');}$data=json_decode(file_get_contents('php://input'),true);$countriesFile='allowed_countries.php';if(!isset($data['action'])||!isset($data['countryCode'])){echo json_encode(['status'=>'error','message'=>'Données manquantes']);exit;}$countryCode=strtolower(trim($data['countryCode']));$currentContent=file_exists($countriesFile)?file_get_contents($countriesFile):'';if(empty($currentContent)){$currentContent="<?php\n\$country = array();\n?>";}preg_match('/\$country\s*=\s*array\(([^)]*)\);/',$currentContent,$matches);$currentCountries=[];if(isset($matches[1])){$countriesString=$matches[1];$currentCountries=array_map('trim',explode(',',$countriesString));$currentCountries=array_filter($currentCountries,function($item){return!empty($item)&&$item!=="''"&&$item!=='""';});$currentCountries=array_map(function($item){return trim($item,"'\"");},$currentCountries);}if($data['action']==='add'){if(!in_array($countryCode,$currentCountries)){$currentCountries[]=$countryCode;}}elseif($data['action']==='remove'){$currentCountries=array_filter($currentCountries,function($item)use($countryCode){return$item!==$countryCode;});}$newCountriesString="'".implode("','",array_unique($currentCountries))."'";$newContent="<?php\n\$country = array($newCountriesString); #Country's Allowed To Access\n?>";if(file_put_contents($countriesFile,$newContent)){echo json_encode(['status'=>'success']);}else{echo json_encode(['status'=>'error','message'=>'Erreur d\'écriture']);}?>