/usr/local/cpanel/3rdparty/bin/perl <<'EOF'
use strict;
use cPanel::PublicAPI;
use Capture::Tiny ':all';
use JSON;

sub main {
    my ($domain, $phpversion) = @_;
    my $_multiphp = 0;

    if (-e '/opt/eig_linux/etc/puppetfeatures/eig_php_ea_compat' && -e '/etc/cpanel/ea4/is_ea4' ) {
        $_multiphp=0;
    } elsif ( -e '/usr/local/cpanel/bin/update_php_handler_hg' ) {
        $_multiphp=0;
    } elsif ( -e '/etc/cpanel/ea4/is_ea4' && ! -e '/opt/eig_linux/etc/puppetfeatures/eig_php_ea_compat' ) {
        $_multiphp=1;
    } else {
        print "[!] Update script was not found!\n";
        exit;
    }

    if ( $phpversion eq 'all' ) {
        print "[!] Invalid option\n";
        exit;
    }

    my $user = _getuser($domain);

    if ( $_multiphp == 0 ) {
        my $docroot = _getdoc($user, $domain);
        if ( -f $docroot . '/.htaccess' ) {
            system("cp -pv $docroot/.htaccess $docroot/.htaccess.phphandler.". time());
        }
        if ( $phpversion eq 'strip' ) {
            system('/usr/local/cpanel/bin/update_php_handler_hg','--verbose','--recurse=0',"--strip","--user=$user","--directory=$docroot","\n");
        } else {
           system('/usr/local/cpanel/bin/update_php_handler_hg','--verbose','--recurse=0',"--force=$phpversion","--user=$user","--directory=$docroot","\n");
        }
    } elsif ( $_multiphp == 1 ) {
        if ( $phpversion eq 'strip' ) {
            print("[!] It is currently not possible to strip the handler from MultiPHP configurations with this tool. Try either configuring in cPanel directly, or set the specific version you need\n");
            exit;
        } elsif ( $phpversion =~ /^\d+$/ ) {
            my ($json, undef, $exit) = capture {
                system("uapi --output=json --user=$user LangPHP php_set_vhost_versions vhost=$domain version=ea-php$phpversion");
            };
            $exit = $exit >> 8;
            if ($exit) {
                print("[!] Error $exit occurred\n");
                exit;
            }
            $json = decode_json($json);
            if($json->{result}->{status} eq 1){
                print("Successfully set $domain to PHP $phpversion\n");
            } else {
                print("[!] Error: Could not set $domain to PHP $phpversion\n");
            }
        } else {
            print("[!] You can only specify numbdered versions of PHP with this tool for MultiPHP confiurations\n");
            exit;
        }
    }
}

sub _getuser {
    my $domain = shift;
    chomp(my $user = `egrep ^$domain:.+\$ /etc/userdomains | cut -d: -f2`);
    $user =~ s/^\s+//;
    if ( ! $user ) {
        print "[!] User not found.\n";
        exit;
    }
    return($user);
}


sub _getdoc {
    my ($user, $domain) = @_;
    my $accesshash = _getah();
    my $cp = cPanel::PublicAPI->new( 'user' => 'root', 'accesshash' => $accesshash, 'ssl_verify_mode' => 0 );
    my $docroot = $cp->cpanel_api2_request('whostmgr', { 'module' => 'DomainLookup', 'func' => 'getdocroot', 'user' => "$user" },
        {
                'domain' => "$domain",
        }
    );
    $docroot = $docroot->{cpanelresult}{data}[0]{docroot};
    if ( ! $docroot ) {
        print "[!] Docroot not found.\n";
        exit;
    }
    return $docroot;
}

sub _getah {
    my $accesshash;
    if ( ! -f '/root/.accesshash' ) {
        $ENV{REMOTE_USER} = 'root';
        system('/usr/local/cpanel/bin/realmkaccesshash');
    }
    if ( -f '/root/.accesshash' ) {
               open my $hash, "<", "/root/.accesshash";
                my @accesshash = <$hash>;
            close $hash;
            foreach(@accesshash){
                    $accesshash .= $_
            }
            $accesshash =~ s/\n//g;
            return $accesshash;
    }
}

main('domain_placeholder', 'php_placeholder');
EOF
