#!/usr/bin/perl

#
# 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.
#

use strict;
use esmith::ConfigDB;
use esmith::event;
use JSON;

require '/usr/libexec/nethserver/api/lib/helper_functions.pl';

my $eventName = undef;
my $ret;
my $exitCode = 0;

my $db = esmith::ConfigDB->open();

my $input = readInput();
my %props = ();

     %props = (
        'MaxExecutionTime' => $input->{'configuration'}->{'MaxExecutionTime'},
        'MemoryLimit' => $input->{'configuration'}->{'MemoryLimit'},
        'PostMaxSize' => $input->{'configuration'}->{'PostMaxSize'},
        'UploadMaxFilesize' => $input->{'configuration'}->{'UploadMaxFilesize'},
    );
if($input->{'action'} eq 'edit') {
    $db->get('php')->merge_props(%props);
} else {
    error();
}

esmith::event::set_json_log(1);

if(esmith::event::event_signal('nethserver-php-update')) {
    $ret = {'state' => 'success'};
} else {
    $ret = {
        'type' => 'Error',
        'message' => 'php.api_update_failed',
        'attributes' => {
            'event_name' => 'nethserver-php-update'
        }
    };
    $exitCode = 1;
}

print encode_json($ret);
exit($exitCode);