<?php
// Vervang deze waarden door de juiste informatie
$phone_ip = "IP_ADDRESS_OF_YEALINK_PHONE";
$username = "USERNAME";
$password = "PASSWORD";

// API-endpoint voor het ophalen van de configuratie
$config_endpoint = "http://$phone_ip/YDMP/ExportConfig";
$export_config_data = [
    "AccountName" => $username,
    "Password" => $password
];

// Haal de huidige configuratie van de telefoon op
$response = file_get_contents($config_endpoint, false, stream_context_create([
    'http' => [
        'method' => 'POST',
        'header' => 'Content-Type: application/json',
        'content' => json_encode($export_config_data)
    ]
]));

if ($response === false) {
    die("Kon geen configuratie ophalen.");
}

// Voer de gewenste regex-patronen uit om BLF-toetsen te vinden en wijzig deze in Speed Dial
$regex_patterns = [
    "/^blf\d+\.type=14$/m",  // Zoek naar BLF-toetsen (type 14)
];

foreach ($regex_patterns as $pattern) {
    preg_match_all($pattern, $response, $matches);
    foreach ($matches[0] as $match) {
        // Wijzig het toetstype naar Speed Dial (type 5)
        $replacement = preg_replace('/type=14/', 'type=5', $match);
        $response = str_replace($match, $replacement, $response);
    }
}

// Stuur de gewijzigde configuratie terug naar de telefoon om op te slaan
$config_endpoint = "http://$phone_ip/YDMP/ImportConfig";

$response = file_get_contents($config_endpoint, false, stream_context_create([
    'http' => [
        'method' => 'POST',
        'header' => 'Content-Type: application/json',
        'content' => $response
    ]
]));

if ($response === false) {
    die("Kon de configuratie niet opslaan.");
}

echo "Configuratie bijgewerkt.";
?>