#!/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();
$vdb = new EsmithDatabase('vhosts');
$hdb = new EsmithDatabase('hosts');
$v = new LegacyValidator($data['virtualhost']);

// check if key exists
if($data['action'] == 'create') {
    if($vdb->getKey($data['virtualhost']['name'])) {
        $v->addValidationError('name', "key_already_exists");
    }
}

$v->declareParameter('name', Validate::NOTEMPTY);

$v->declareParameter('FtpStatus', Validate::SERVICESTATUS);
if($data['virtualhost']['FtpStatus'] == 'enabled') {
    $v->declareParameter('FtpPassword', Validate::NOTEMPTY);
}

if($data['virtualhost']['name'] != 'default') {
    $v->declareParameter('ServerNames', Validate::NOTEMPTY);
    $v->declareParameter('Indexes', Validate::SERVICESTATUS);
    $v->declareParameter('Access', $v->createValidator()->memberOf('public', 'private'));
    $v->declareParameter('ForceSslStatus', Validate::SERVICESTATUS);
    $v->declareParameter('SslCertificate', Validate::ANYTHING);

    $v->declareParameter('PasswordStatus', Validate::SERVICESTATUS);
    if($data['virtualhost']['PasswordStatus'] == 'enabled') {
        $v->declareParameter('PasswordValue', Validate::NOTEMPTY);
    }
    # phpSettings
    $v->declareParameter('PhpRhVersion', $v->createValidator()->memberOf('default', 'php71', 'php72','php73','php80'));
    $v->declareParameter('MaxExecutionTime', $v->createValidator()->integer()->greatThan(-1));
    $v->declareParameter('MemoryLimit', Validate::POSITIVE_INTEGER);
    $v->declareParameter('PostMaxSize', Validate::POSITIVE_INTEGER);
    $v->declareParameter('UploadMaxFilesize', Validate::POSITIVE_INTEGER);
    $v->declareParameter('PhpCustomSettings', Validate::SERVICESTATUS);

    if  ($data['virtualhost']['MemoryLimit'] <= $data['virtualhost']['PostMaxSize']) {
            $v->addValidationError('PostMaxSize', 'MemoryLimitLabelMustBeSuperiorThanPostMaxSize');
    } 
    if  ($data['virtualhost']['PostMaxSize'] <= $data['virtualhost']['UploadMaxFilesize']) {
            $v->addValidationError('UploadMaxFilesize', 'PostMaxSizeMustBeSuperiorThanUpMaxFileSize');
    }

    $serverList = $data['virtualhost']['ServerNames'];
    $Validator = $v->createValidator()->hostname();
    foreach ($serverList  as $el) {
        if (!$Validator->evaluate($el) ) {
            $v->addValidationError('ServerNames', "valid_fqdn", $el);
        }
        #Valid if the fqdn is not already used
        foreach ($vdb->getAll() as $key => $props) {
            $vhost = explode(",", $vdb->getProp($key, 'ServerNames'));
            if ($key === $data['virtualhost']['name']) {
                continue;
            }
            if (in_array ($el, $vhost)) {
                $v->addValidationError('ServerNames', 'valid_fqdn_already_used',$el);
            }
        }
    }
}

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