<?php
$http_origin = $_SERVER['HTTP_ORIGIN'];

if ($http_origin == "https://crm.cadline.hu" || $http_origin == "https://www.archline.hu" || $http_origin == "https://secondary.cadline.hu") {
    header("Access-Control-Allow-Origin: $http_origin");
} else {
    header('Access-Control-Allow-Origin: https://www.archline.hu');
}

header('Access-Control-Allow-Headers: X-Requested-With, Content-Type, Accept');

$name           = $_POST['name'];
$serverName     = $_SERVER['SERVER_NAME'];
$primaryServers = array('secondary.dev.cadline.hu', 'www.archline.hu', 'www.archlinexp.com');
$secServers     = array('secondary.cadline.hu');
$txtResult      = '';

switch ($name) {
    case 'activateSecondary':
        if (in_array($serverName, $primaryServers))
            $txtResult = 'Inactive';

        if (in_array($serverName, $secServers))
            $txtResult = 'Active';
        break;

    case 'activatePrimary':
        if (in_array($serverName, $primaryServers))
            $txtResult = 'Active';

        if (in_array($serverName, $secServers))
            $txtResult = 'Inactive';
        break;

    default:
        die();
        break;
}

if ($txtResult == '')
    die();

$txtString = 'Success';

$filename = $_SERVER['DOCUMENT_ROOT'] . "/activateServer.txt";
$closed = file_put_contents($filename, $txtResult);

$i = 0;

while (!$closed && $i < 10) {
    $closed = file_put_contents($filename, $txtResult);
    $i++;
}

if (!$closed)
    $txtString = 'Failed';

$result = new stdClass();
$result->message = $txtString;

echo json_encode($result);
