#!/usr/bin/php
<?php
  
/*
 * Copyright (C) 2019 Nethesis S.r.l.
 * http://www.nethesis.it - nethserver@nethesis.it
 *
 * This script is part of NethServer.
 *
 * NethServer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License,
 * or any later version.
 *
 * NethServer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * 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 NethServer.  If not, see COPYING.
*/

require_once("/usr/libexec/nethserver/api/lib/Helpers.php");

$data = readInput();
$v = new LegacyValidator($data['configuration']);

$timezones = explode("\n",shell_exec('java -classpath /usr/share/webtop/ ListTimeZones'));
$localelist = explode("\n",shell_exec('/etc/e-smith/events/actions/nethserver-webtop5-locale-tz'));
$iconsizes = array('small','medium','large');

$v->declareParameter('DefaultLocale', $v->createValidator()->memberOf($localelist));
$v->declareParameter('DefaultTimezone', $v->createValidator()->memberOf($timezones));
$v->declareParameter('DefaultToolbarIconsSize', $v->createValidator()->memberOf($iconsizes));

$v->declareParameter('MinMemory', Validate::POSITIVE_INTEGER);
$v->declareParameter('MaxMemory', Validate::POSITIVE_INTEGER);

$virtualHostValidator = $v->createValidator()->orValidator($v->createValidator()->hostname(2), $v->createValidator()->isEmpty());
$v->declareParameter('VirtualHost', $virtualHostValidator);

if ($data['configuration']['PublicUrl']) {
  if (! filter_var($v->parameters['PublicUrl'], FILTER_VALIDATE_URL) || (strpos($v->parameters['PublicUrl'], 'http') !== 0 )) {
    $v->addValidationError('PublicUrl', 'InvalidPublicUrl', $v->parameters['PublicUrl']);
  }
}

if ($data['configuration']['DavServerUrl']) {
  if (! filter_var($v->parameters['DavServerUrl'], FILTER_VALIDATE_URL) || (strpos($v->parameters['DavServerUrl'], 'http') !== 0 )) {
    $v->addValidationError('DavServerUrl', 'InvalidDavServerUrl', $v->parameters['DavServerUrl']);
  }
}

if ($data['configuration']['JitsiUrl']) {
  if (! filter_var($v->parameters['JitsiUrl'], FILTER_VALIDATE_URL) || (strpos($v->parameters['JitsiUrl'], 'http') !== 0 )) {
    $v->addValidationError('JitsiUrl', 'InvalidJitsiUrl', $v->parameters['JitsiUrl']);
  }
}

if ($data['configuration']['PbxProviderNethvoiceWebrestUrl']) {
  if (! filter_var($v->parameters['PbxProviderNethvoiceWebrestUrl'], FILTER_VALIDATE_URL) || (strpos($v->parameters['PbxProviderNethvoiceWebrestUrl'], 'http') !== 0 ) || $v->parameters['PbxProvider'] != "nethvoice") {
    $v->addValidationError('PbxProviderNethvoiceWebrestUrl', 'InvalidNethvoiceWebrestUrl', $v->parameters['PbxProviderNethvoiceWebrestUrl']);
  }
}

if (intval($data['configuration']['MinMemory']) > intval($data['configuration']['MaxMemory'])) {
  $v->addValidationError('MinMemory', 'MaxMemoryHighThanMin', $v->parameters['MinMemory']);
  $v->addValidationError('MaxMemory', 'MaxMemoryHighThanMin', $v->parameters['MaxMemory']);
}

# Validate the input
if ($v->validate()) {
  success();
} else {
  error($v);
}
