MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Parpaing.  If not, see <http://www.gnu.org/licenses/>.
*/
$path = "../../";
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
set_time_limit(0);
require_once 'config/config.php';
require_once 'vpn/api_client.php';
// Make the api call
//set POST variables
//$url = 'https://www.armagnet.fr/vpn/api.php';
//$url = 'http://127.0.0.1/vpn/api.php';
$apiClient = new ArmagnetVpnApiClient($config["armagnet"]["api_url"]);
$person = array();
$account = array();
$account["login"] = $_REQUEST["loginInput"];
$account["password"] = $_REQUEST["passwordInput"];
if (isset($_REQUEST["hasAccount"]) && $_REQUEST["hasAccount"] == 1) {
    $response = $apiClient->authenticate($account);
    // If bad login, don't go further
    if (isset($response["ko"])) {
        echo json_encode(array("ko" => "ko", "message" => $response["message"]));
        exit;
    }
    $person = $response["person"];
} else {
    $account["confirmPassword"] = $_REQUEST["confirmInput"];
    if ($account["confirmPassword"] != $account["password"]) {
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Parpaing.  If not, see <http://www.gnu.org/licenses/>.
*/
session_start();
$path = "../../";
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
require_once 'config/config.php';
require_once 'vpn/api_client.php';
$account = array();
$account["login"] = $_REQUEST["loginInput"];
$account["password"] = $_REQUEST["passwordInput"];
// Make the api call
//set POST variables
$apiClient = new ArmagnetVpnApiClient($config["armagnet"]["api_url"]);
$result = $apiClient->retrieveConfigurations($account);
//error_log($result["json"]["dev"]);
//error_log(print_r($result, true));
$_SESSION["VPN_CONFIGURATIONS"] = json_encode($result["configurations"]);
foreach ($result["configurations"] as $index => $configuration) {
    $result["configurations"][$index]["hasKey"] = false;
    if ($result["configurations"][$index]["key"]) {
        $result["configurations"][$index]["hasKey"] = true;
    }
    unset($result["configurations"][$index]["cert"]);
    unset($result["configurations"][$index]["cacrt"]);
    unset($result["configurations"][$index]["dh"]);
    unset($result["configurations"][$index]["key"]);
}
echo json_encode($result);