perl <<'EOF'
use strict;

sub main {
        my %phpfile;
        my $whatwedo;
        my ($domain, $phpversion) = @_;
        my $user = _getuser($domain);
        my $user_selector_conf_file = '/home/'.$user.'/.cpanel/.php_selector.conf';
        if($phpversion !~ /^(\d{2}|all)$/){
                print "[!] Invalid parameter\n";
                exit;
        }
        if(! -d '/home/'.$user.'/.cpanel'){
                print "[!] Cannot find users cpanel folder!\n";
                exit;
        }

        # Read in current values if they exist
        if(-e $user_selector_conf_file){
                open(my $user_selector_conf, "<", $user_selector_conf_file);
                while(<$user_selector_conf>){
                        $phpfile{$1} = $2 if(/^\s*(php_allow_(?:\d{2}|all))\s*=\s*([\d\.]+)/);
                }
                close($user_selector_conf);
        }

        # Toggle
        if(exists($phpfile{'php_allow_'.$phpversion})){
                delete($phpfile{'php_allow_'.$phpversion});
                $whatwedo = "removed";
        } else {
                $phpfile{'php_allow_'.$phpversion} = 1;
                $whatwedo = "set";
        }

        # Write out file
        open(my $user_selector_conf, ">", $user_selector_conf_file);
        foreach my $phpvar (keys %phpfile){
                print($user_selector_conf $phpvar." = ".$phpfile{$phpvar}."\n");
        }
        close($user_selector_conf);
        if(-e $user_selector_conf_file){
                my (undef,undef,$uid, $gid,undef,undef,undef,undef,undef) = getpwnam($user);
                chown($uid, $gid, $user_selector_conf_file);
        }
        print("PHP allow ".$whatwedo." for ".$phpversion."\n");
}

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";
		next;
	}
	return($user);
}

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