%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /scripts/
Upload File :
Create Path :
Current File : //scripts/wpt_convert

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

# cpanel - scripts/wpt_convert                     Copyright 2021 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 scripts::wpt_convert;

use cPstrict;
use Cpanel::Autodie                ();
use Cpanel::Config::LoadCpUserFile ();
use Cpanel::Config::Users          ();
use Cpanel::Features               ();
use Cpanel::Features::Load         ();
use Cpanel::FileUtils::Modify      ();
use Cpanel::PwCache                ();
use Whostmgr::AccountEnhancements  ();
use Getopt::Long                   ();

exit( run(@ARGV) ) unless caller;

=head1 run

Runs the conversion script which will:
1. Remove WordPress Toolkit Deluxe (WPTD) as an option in the feature list.
2. Assign the WPTD enhancement specified to the accounts that were on a package that included WPTD.

=cut

sub run {
    my @args = @_;

    # Needed for assignment
    $ENV{'REMOTE_USER'} = Cpanel::PwCache::getpwuid($<);

    my ( $enhancement_name, $dryrun, $update, $help );

    my %opts = (
        'name=s'       => \$enhancement_name,
        'dry-run'      => \$dryrun,
        'update'       => \$update,
        'help|usage|?' => \$help,
    );

    return _help(1) if !Getopt::Long::GetOptionsFromArray( \@args, %opts );
    return _help(0) if $help;
    return _help(1) if !_validate($enhancement_name);
    if ( $dryrun && $update ) {
        print STDERR "\nERROR: The --dry-run and --update options are mutually exclusive.\n";
        return _help(1);
    }

    $dryrun = 1 if !$update;

    # Get changes
    my $accounts_affected     = _get_users_affected();
    my $featurelists_affected = _get_featurelists_affected();

    my %changes = (
        name         => $enhancement_name,
        accounts     => $accounts_affected,
        featurelists => $featurelists_affected,
    );

    if ( !$dryrun && ( scalar @$accounts_affected != 0 || scalar @$featurelists_affected != 0 ) ) {
        _apply_changes(%changes);
    }

    print "Conversion complete!\n\n"                                                                  if !$dryrun;
    print "Dry run conversion complete! No changes were applied. Pass --update to apply changes.\n\n" if $dryrun;

    _output(%changes);

    print "\nDry run conversion complete! No changes were applied. Pass --update to apply changes.\n\n" if $dryrun;

    return 0;
}

# Returns an arrayref of Accounts affected.
sub _get_users_affected() {
    my @accounts_affected = ();

    foreach my $user ( Cpanel::Config::Users::getcpusers() ) {
        my $cpuserfile                     = Cpanel::Config::LoadCpUserFile::loadcpuserfile($user);
        my $has_wptd_from_feature_settings = Cpanel::Features::get_user_feature_settings( $user, ['wp-toolkit-deluxe'] )->[0]{'feature_list_setting'} if -e '/usr/local/cpanel/whostmgr/addonfeatures/wp-toolkit-deluxe';
        my $has_wptd_from_override         = $cpuserfile->{'FEATURE-WP-TOOLKIT-DELUXE'};

        push( @accounts_affected, $user ) if ( $has_wptd_from_feature_settings || $has_wptd_from_override );
    }

    return \@accounts_affected;
}

# Returns a listref of featurelists that include WPTD.
sub _get_featurelists_affected() {
    my @featurelists_affected = ();

    foreach ( Cpanel::Features::get_user_feature_lists( 'root', 1 ) ) {
        my $featurelist_data = Cpanel::Features::get_featurelist_information($_);
        my ($wptd_data) = grep { 'wp-toolkit-deluxe' eq $_->{id} } @$featurelist_data;

        my $is_disabled_featurelist_affected = $_ eq 'disabled' && defined $wptd_data->{'value'} && $wptd_data->{'value'} == 0;
        my $is_featurelist_affected = $_ ne 'disabled' && $wptd_data->{'value'};

        push( @featurelists_affected, $_ ) if $is_featurelist_affected || $is_disabled_featurelist_affected;
    }

    return \@featurelists_affected;
}

# Applies WPTD changes to affected users and featurelists.
sub _apply_changes (%args) {
    my $accounts_affected     = $args{'accounts'};
    my $featurelists_affected = $args{'featurelists'};
    my $enhancement_name      = $args{'name'};

    foreach (@$accounts_affected) {
        eval { Whostmgr::AccountEnhancements::assign( $enhancement_name, $_ ); };
        my $err = $@;

        # If the eval dies for any reason other than already assigned, die for real.
        die $err if $err && !eval { $err->isa('Cpanel::Exception::AccountAccessAlreadyExists') };

        Cpanel::Features::remove_override_features_for_user( $_, ['wp-toolkit-deluxe'] );
    }

    _remove_wptd_from_featurelist($_) foreach @$featurelists_affected;

    Cpanel::Autodie::unlink_if_exists('/usr/local/cpanel/whostmgr/addonfeatures/wp-toolkit-deluxe');

    return;
}

# Will update all featurelists unchecking the wp-toolkit-deluxe option.
# For the 'disabled' featurelist it needs the opposite value hence 1.
sub _remove_wptd_from_featurelist ( $featurelist_name ) {
    my $featurelist = Cpanel::Features::Load::load_feature_file($featurelist_name);
    $featurelist->{'wp-toolkit-deluxe'} = $featurelist_name eq 'disabled' ? 1 : 0;
    Cpanel::Features::update_featurelist( $featurelist_name, $featurelist, 'root', 1 );
    Cpanel::Features::_clear_feature_cache($featurelist_name);
    Cpanel::FileUtils::Modify::remlinefile( "/var/cpanel/features/$featurelist_name", "wp-toolkit-deluxe", 'begin' );
    return 1;
}

# Nice print stuff
sub _output (%args) {
    my $accounts_affected     = $args{'accounts'};
    my $featurelists_affected = $args{'featurelists'};
    my $enhancement_name      = $args{'name'};

    if ( scalar @$accounts_affected == 0 ) {
        print "No accounts found to convert.\n";
    }

    if ( scalar @$featurelists_affected == 0 ) {
        print "No feature-lists found to convert.\n";
    }

    if ( scalar @$accounts_affected != 0 ) {
        my $number_of_accounts_affected = scalar @$accounts_affected;
        print "$number_of_accounts_affected account(s) are now using the Account Enhancement \"$enhancement_name\":\n";
        print "$_\n" foreach @$accounts_affected;
    }

    if ( scalar @$featurelists_affected != 0 ) {
        print "\nThe following feature-lists were changed:\n";
        print "$_\n" foreach @$featurelists_affected;
    }

    if ( scalar @$featurelists_affected != 0 || scalar @$accounts_affected != 0 ) {
        print "\nThe limit for reseller assignment of the Account Enhancement \"$enhancement_name\" is set to 0.\n";
    }

    return 1;
}

# Script validations
#
# Returns
# 1 - validations pass
# 0 - any error/fail
sub _validate {
    my $name = shift;

    if ( !$name ) {
        print STDERR "\nERROR: You must pass the --name=\'NAME\' option, where 'NAME' is the name of the WordPress Toolkit Deluxe enhancement.\n";
        return 0;
    }

    eval { Whostmgr::AccountEnhancements::find($name) };
    my $err = $@;

    if ($err) {
        print STDERR $err->to_string_no_id();
        print STDERR "\nTip: Use the WHM API 1 'list_account_enhancements' function to list the available account enhancements.\n";
        return 0;
    }

    return 1;
}

# --help output
# Returns the intended script exit value
sub _help {
    my $exit_val = shift;

    my $out_fh = $exit_val ? *STDERR : *STDOUT;

    print $out_fh <<"END_HELP";

Usage:
    $0 --name="NAME" --dry-run | --update

Description:
    The script removes WordPress Toolkit Deluxe from all feature lists and assigns an Account Enhancement to cPanel accounts.

Options:
    --help          Display the script's help documentation.
    --name='NAME'   The name of the WordPress Toolkit Deluxe Account Enhancement.
                    This option is required.
    --dry-run       Run the script to see the potential results without making changes.
                    Without any options, the script returns the --dry-run option's results.
    --update        Convert cPanel accounts to use the Account Enhancements feature.

END_HELP

    return $exit_val;
}

1;

Zerion Mini Shell 1.0