#!/usr/local/cpanel/3rdparty/bin/perl
# cpanel - scripts/whmcs_setup_php_ini               Copyright 2018 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

package scripts::whmcs_setup_php_ini;

use strict;
use warnings;
use Cpanel::API::LangPHP    ();
use Cpanel::API::DomainInfo ();
use Cpanel::Args            ();
use Cpanel::Result          ();
use Getopt::Long            ();
use Pod::Usage              ();

# we don't care about the feature check since we are
# just trying to reuse the same php.ini writer, not
# giving the user a generalized accesss.
local *main::hasfeature = sub { return 1; };

exit( __PACKAGE__->run(@ARGV) ) if !caller;

=head1 DESCRIPTION

This script generates a php.ini file for whmcs to set the minimum memory_limit
needed to run the whmcs application at install time. These values can be adjusted
using the Multi-PHP-INI editor in cPanel or WHM by the user later if more or less
memory is desired.

=head1 USAGE

whmcs_setup_php_ini --domain={string} [--memory-limit={string}]

Where:

=over

=item * --domain={string} - The domain name for which to set the new php memory limit.

=item * --memory-limit={string} - Optional. The maximum amount of memory in bytes allocated for php scripts to run.
See: http://php.net/manual/en/ini.core.php#ini.memory-limit for more information on acceptable values.
Defaults to 256M.

=back

=cut

sub run {
    my ( $self, @args ) = @_;
    my ( $help, $man, $domain, $memory_limit, $verbose ) = ( 0, 0, '', '256M', 0 );

    Getopt::Long::GetOptionsFromArray(
        \@args,
        'help|?'         => \$help,
        'man'            => \$man,
        'verbose|v'      => \$verbose,
        'domain=s'       => \$domain,
        'memory-limit=s' => \$memory_limit,
    ) or Pod::Usage::pod2usage(2);
    Pod::Usage::pod2usage(1) if $help;
    Pod::Usage::pod2usage( -exitval => 0, -verbose => 2 ) if $man;

    if ( !defined $domain || $domain eq '' ) {
        die 'You must pass a --domain value';
    }

    if ( !defined $memory_limit || $memory_limit !~ m/^(-1|\d+[M]?)$/ ) {
        die 'You must pass a --memory-limit as a number or a string that php will convert to a number: -1, 256M, 512M, ...';
    }

    # Remove leading/trailing whitespace
    $domain =~ s/^\s+|\s+$//g;
    $memory_limit =~ s/^\s+|\s+$//g;

    if ($verbose) {
        print "Setting the php memory-limit for $domain to: $memory_limit\n";
    }

    local $Cpanel::user = ( getpwuid( int($<) ) )[0];

    my $vhost = $domain;

    my $domain_info_res = Cpanel::Result->new();
    if ( !Cpanel::API::DomainInfo::list_domains( Cpanel::Args->new(), $domain_info_res ) ) {
        die "Can not setup the php.ini since we can not lookup the domain information with error: " . $domain_info_res->error_as_string();
    }

    my $domain_info    = $domain_info_res->data();
    my $parked_domains = $domain_info->{parked_domains};
    if ( $parked_domains && ref $parked_domains eq 'ARRAY' && grep { $_ eq $domain } @$parked_domains ) {
        $vhost = $domain_info->{main_domain};
    }

    my $args = Cpanel::Args->new(
        {
            'type'        => 'vhost',
            'vhost'       => $vhost,
            'directive-0' => "memory_limit:$memory_limit",
        }
    );
    my $result = Cpanel::Result->new();

    my $ok = eval { Cpanel::API::LangPHP::php_ini_set_user_basic_directives( $args, $result ) };
    if ( my $exception = $@ ) {
        die $exception;
    }
    if ( !$ok ) {
        my $error = $result->error_as_string();
        die $error if $error;
        die "Unknown error.";
    }

    if ($verbose) {
        print "Success\n";
    }
    return 0;
}

