package Cpanel::UpdateGatherer::modules::Plugins;

# cpanel - SOURCES/Plugins.pm                      Copyright 2023 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

use Cpanel::UpdateGatherer::Std;

use Cpanel::SafeRun::Object ();
use JSON                    ();

sub compile ( $self, $meta ) {

    my $plugins = {};
    foreach my $plugin (qw{ installatron softaculous }) {
        my $has_plugin = eval { $self->can("_check_$plugin")->($self) } // 0;

        $plugins->{"has_$plugin"}           = $has_plugin;
        $plugins->{"count_wp_from_$plugin"} = 0;
        next unless $has_plugin;
        $plugins->{"count_wp_from_$plugin"} = eval { $self->can("_count_wp_from_$plugin")->($self) } // 0;
    }

    $meta->{plugins} = $plugins;

    return 1;
}

sub _check_installatron ($self) {

    # can be installed from a package or not...
    my @files = qw{
      /usr/local/cpanel/3rdparty/installatron/index.cgi
      /usr/local/cpanel/whostmgr/docroot/cgi/addon_installatron.cgi
    };
    foreach my $f (@files) {
        return 1 if -e $f;
    }

    my $pkg_name = q[installatron-plugin-cpanel];

    my $check = Cpanel::SafeRun::Object->new(
        program => '/bin/rpm',
        args    => [ '-qa', $pkg_name ],
    );
    my $out = $check->stdout // '';

    return 1 if $out =~ /^$pkg_name/;
    return;
}

sub _check_softaculous ($self) {
    return 1 if -e q[/usr/local/cpanel/whostmgr/docroot/cgi/softaculous/index.cgi];
    return;
}

sub _count_wp_from_installatron ($self) {

    # https://installatron.com/docs/admin/commandline#installs
    # /usr/local/installatron/installatron --installs
    # {
    #     "result": true,
    #     "message": "The task is complete.\n",
    #     "errcode": null,
    #     "errfield": null,
    #     "data": [
    #        {
    #           "id": "deadbeef",
    #           "file": "\/home\/grav\/.appdata\/current\/deadbeef",
    #           "installer": "grav",
    #           "version": "1.6.8",
    #           "language": "en",
    #           "time": "1557088835",
    #           "agent": "9.1.44",
    #           "owner": "grav",
    #           "path": "\/home\/grav\/public_html\/resume",
    #           "path-created": "1",
    #           "url": "https:\/\/grav.myhost.com\/resume",
    #           "url-domain": "grav.myhost.com",
    #           "title": "My title",
    #           ...
    #        },
    #
    #      ]
    # }

    my $run = Cpanel::SafeRun::Object->new(
        program => '/usr/local/installatron/installatron',
        args    => [qw{ --installs }],
    );

    my $json = eval { JSON::decode_json( $run->stdout ) } // {};

    my $data = $json->{data} // [];

    my $wordpress_installations = 0;

    # check 'wp-config.php' from the path for each installations
    #   to know if it's a WordPress installation
    foreach my $install (@$data) {
        my $path = $install->{path};
        next unless length $path && -d $path;
        ++$wordpress_installations if -e qq[$path/wp-config.php];
    }

    return $wordpress_installations;
}

sub _count_wp_from_softaculous ($self) {

    # Upstream Doc: https://www.softaculous.com/docs/cli/list-installations-from-cli/
    # note: SoftwareID:26 is the WordPress id

    my $run = Cpanel::SafeRun::Object->new(
        program => '/usr/local/cpanel/3rdparty/bin/php',
        args    => [
            qw{
              /usr/local/cpanel/whostmgr/docroot/cgi/softaculous/cli.php
              --list_ins
              --sid=26
              --count=1
            }
        ],
    );

    my $count;
    {
        no warnings;
        $count = 0 + $run->stdout;
    }

    return $count;
}

1;
