#!/usr/local/cpanel/3rdparty/bin/perl

# cpanel - bin/admin/Cpanel/nginx                    Copyright 2019 cPanel L.L.C.
#                                                           All rights reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

package bin::admin::Cpanel::nginx;

use strict;

use base qw( Cpanel::AdminBin::Script::Call );

use lib '/var/cpanel/perl5/lib';
use Cpanel::Debug ();
use Cpanel        ();
use Capture::Tiny 'capture_merged';
use Cpanel::Rlimit ();

eval { require '/usr/local/cpanel/scripts/ea-nginx' };    # Prefer the package’s script …
if ($@) {                                                 # … if its not installed do the devbox version
    use FindBin ();
    require "$FindBin::Bin/../SOURCES/cpanel-scripts-ea-nginx";
}

__PACKAGE__->run( 'alarm' => 120 ) unless caller;

sub _actions {
    return qw(UPDATE_CONFIG CLEAR_CACHE RESET_CACHE_CONFIG ENABLE_CACHE DISABLE_CACHE RELOAD_LOGS RELOAD_SERVICE);
}

sub UPDATE_CONFIG {
    my ($self) = @_;
    my $cpuser = $self->_debug_and_user(0);

    require '/var/cpanel/perl5/lib/NginxHooks.pm';

    local $@;
    eval {
        require Cpanel::ServerTasks;
        Cpanel::ServerTasks::schedule_task( ['NginxTasks'], NginxHooks::get_time_to_wait(0), "rebuild_user $cpuser" );
    };

    return 1;
}

sub RELOAD_LOGS {
    my ($self) = @_;
    require '/var/cpanel/perl5/lib/NginxHooks.pm';
    NginxHooks::_reload_logs();
    return 1;
}

sub RELOAD_SERVICE {
    my ($self) = @_;

    local $@;
    eval {
        require Cpanel::ServerTasks;
        Cpanel::ServerTasks::schedule_task( ['NginxTasks'], 30, 'reload_service' );
    };

    return 1;
}

sub CLEAR_CACHE {
    my ($self) = @_;
    my $cpuser = $self->_debug_and_user(0);
    return _silent_running( clear_cache => $cpuser );
}

sub RESET_CACHE_CONFIG {
    my ($self) = @_;
    my $cpuser = $self->_debug_and_user(1);
    return _silent_running( cache => $cpuser, '--reset' );
}

sub ENABLE_CACHE {
    my ($self) = @_;
    my $cpuser = $self->_debug_and_user(1);
    return _silent_running( cache => $cpuser, '--enabled=1' );
}

sub DISABLE_CACHE {
    my ($self) = @_;
    my $cpuser = $self->_debug_and_user(1);
    return _silent_running( cache => $cpuser, '--enabled=0' );
}

###############
#### helpers ##
###############

sub _silent_running {
    my (@args) = @_;

    my $rv;

    my $limits_hr;
    if ( -f '/etc/cpanel/ea4/option-flags/give-cpsrvd-nginx-operations-unlimited-memory' ) {
        warn "Doing NGINX operation without memory restriction per /etc/cpanel/ea4/option-flags/give-cpsrvd-nginx-operations-unlimited-memory\n";
        $limits_hr = Cpanel::Rlimit::get_current_rlimits();
        Cpanel::Rlimit::set_rlimit_to_infinity();
    }

    capture_merged { $rv = scripts::ea_nginx::run(@args) };

    Cpanel::Rlimit::restore_rlimits($limits_hr) if $limits_hr;

    return $rv;
}

sub _debug_and_user {
    my ( $self, $requires_feature ) = @_;

    my @caller = caller(1);
    my $method = $caller[3];
    $method =~ s/^.*::([^:]+)$/$1/;

    if ($Cpanel::Debug::level) {
        Cpanel::Debug::log_info("$method() called");
    }

    my $cpuser = $self->get_caller_username();
    if ($requires_feature) {
        Cpanel::initcp($cpuser);
        exit(1) if !Cpanel::hasfeature('toggle_nginx_caching');
    }

    return $cpuser;
}

1;

__END__

=encoding utf-8

=head1 NAME

nginx

=head1 SYNOPSIS

my $nginx = bin::admin::Cpanel::nginx->new ();

@actions = $nginx->_actions ();

$nginx->UPDATE_CONFIG ();

=head1 DESCRIPTION

This is the nginx hooks admin bin, allowing the various cpusers
via API1/API2/UAPI to cause changes to the Nginx configuration.

nginx is deployed by the RPM to /usr/local/cpanel/bin/admin/Cpanel.

=head1 SUBROUTINES

=head2 _actions

Lists the actions allowed by this admin bin script.

=head2 UPDATE_CONFIG

This function determines the user calling, and then schedules a
rebuild of the user's Nginx configuration to happen in the time
needed depending on whether PHP-FPM is defaulted to on or not.

=head2 CLEAR_CACHE

Clears the user’s NGINX cache.

=head2 RESET_CACHE_CONFIG

Resets the user’s NGINX cache config to default to what is set at the system level.

Requires the `toggle_nginx_caching` feature.

=head2 ENABLE_CACHE

Enable NGINX caching for the user.

Requires the `toggle_nginx_caching` feature.

=head2 DISABLE_CACHE

Disable NGINX caching for the user.

Requires the `toggle_nginx_caching` feature.

=head2 RELOAD_LOGS

Send SIGUSR1 to the nginx process which informs it to reload its log files.

=head2 RELOAD_SERVICE

Restart the nginx service

=cut

