#!/usr/local/cpanel/3rdparty/bin/perl
#                                      Copyright 2025 WebPros International, LLC
#                                                           All rights reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited.

package autofixer2::set_autossl_to_lets_encrypt_2;

use strict;
use warnings;

use Carp;
use Try::Tiny;

use Cpanel::iContact                 ();
use Cpanel::JSON                     ();
use Cpanel::Plugins                  ();
use Cpanel::SSL::Auto::Loader        ();
use Whostmgr::API::1::Utils::Execute ();

BEGIN { unshift @INC, '/usr/local/cpanel'; }

exit run() unless caller;

use constant _AUTOSSL_CONF  => '/var/cpanel/autossl.json';
use constant _PROVIDER_NAME => 'LetsEncrypt';
use constant _CPANEL_NAME   => 'cPanel';
use constant _DNSONLY_FILE  => '/var/cpanel/dnsonly';
use constant _PLUGIN_NAME   => 'cpanel-letsencrypt-v2';

sub run {

    return 0 unless supported_on_this_major( 94, 116 );     # Set this to the Min/Max we support this autofixer on.

    return 0 if -e _DNSONLY_FILE;                           # No AutoSSL on DNSOnly
    return 0 if !-e _AUTOSSL_CONF();                        # No AutoSSL configuration so it's disabled

    my $autossl_conf = Cpanel::JSON::LoadFile( _AUTOSSL_CONF() );
    return 0 if !length $autossl_conf->{'provider'};              # AutoSSL is disabled
    return 0 if $autossl_conf->{'provider'} ne _CPANEL_NAME();    # AutoSSL is not using the Sectigo provider

    try {
        Cpanel::Plugins::install_plugins( _PLUGIN_NAME() );
    }
    catch {
        croak sprintf( 'The system encountered the following error while installing the “%s” provider: %s', _PROVIDER_NAME(), $_ );
    };

    my $tos = try {
        my $ns    = Cpanel::SSL::Auto::Loader::get_and_load( _PROVIDER_NAME() );
        my %props = $ns->PROPERTIES();
        $props{terms_of_service};
    }
    catch {
        croak sprintf( 'The system encountered the following error while activating the “%s” provider: %s', _PROVIDER_NAME(), $_ );
    };

    croak sprintf( 'The system failed to determine the terms of service for the “%s” provider', _PROVIDER_NAME() ) unless $tos;

    try {
        Whostmgr::API::1::Utils::Execute::execute_or_die(
            'SSL' => 'set_autossl_provider',
            {
                'provider'                    => _PROVIDER_NAME(),
                'x_terms_of_service_accepted' => $tos,
            },
        );
    }
    catch {
        croak sprintf( 'The system encountered the following error while activating the “%s” provider: %s', _PROVIDER_NAME(), $_ );
    };

    my $body = <<~BODY;
    <body style="padding: 10px; background: #F4F4F4;">
        <div style="border: 2px solid #E8E8E8; width: 0 auto; margin: 0 auto; max-width: 680px; padding: 10px 10px 0 10px; background: #FFFFFF; font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; border-bottom: 2px solid #FF6C2C;">
            <h2>The AutoSSL configuration has changed</h2>
            <p>The system has been reconfigured to use Let’s Encrypt™ as the AutoSSL provider instead of Sectigo.</p>
            <p>The Sectigo AutoSSL provider has been deprecated. It will no longer function at a future date.</p>
            <p>See the <a href="https://go.cpanel.net/deprecation">cPanel Deprecation Plan</a> for more information.</p>
            <p>Do not reply to this automated message.</p>
        </div>
        <div style="text-align: center; margin: 10px;">
            <p>Copyright©&nbsp;2024 cPanel, L.L.C.<p>
        </div>
    </body>
    BODY

    Cpanel::iContact::icontact(
        'subject'      => 'The AutoSSL configuration has changed',
        'app'          => 'Application',
        'message'      => $body,
        'content-type' => 'text/html',
    );

    return 0;    # exit(0);
}

# Do we run this code?
sub supported_on_this_major {
    my ( $min_ver, $max_ver ) = @_;

    my $major = get_major_version();

    return 0 if $major < $min_ver;
    return 0 if $major > $max_ver;

    return 1;
}

sub get_major_version {
    my $major_version;

    if ( open( my $fh, '<', '/usr/local/cpanel/version' ) ) {
        my $full_version = <$fh>;
        close($fh);
        if ( length $full_version ) {
            chomp $full_version;
            ($major_version) = $full_version =~ /^[0-9]+\.([0-9]+)/;
        }
    }

    # Safe default.
    return $major_version || 30;
}

1;