Viewing File: /usr/local/cpanel/3rdparty/wp-toolkit/scripts/cantrack.pl

#!/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.

# Much of this script is almost an exact copy/paste of the relevant functions from Cpanel::Analytics.
# It is being provided for use with WP Toolkit so that these checks may be performed independently
# of cPanel versions. Once these functions are available in all supported versions of cPanel, and
# ideally can_track_user_analytics should be exposed via WHM API 1, this script will no longer be
# necessary.

use strict;
use warnings;
use Getopt::Long                  ();
use Cpanel::AccessIds             ();
use Cpanel::Analytics::UiIncludes ();
use Cpanel::App                   ();
use Cpanel::API::NVData           ();
use Cpanel::JSON                  ();
use Whostmgr::NVData              ();

my ( $app, $user, $verbose, $help );
Getopt::Long::GetOptions(
    'app=s'   => \$app,
    'user=s'  => \$user,
    'verbose' => \$verbose,
    'help|h'  => \$help,
) or die_usage("Error parsing command line arguments");

if ($help) {
    print_usage();
    exit 0;
}

die_usage("Missing required argument: --app (cpanel or whm)")    unless defined $app;
die_usage("Missing required argument: --user")                   unless defined $user;
die_usage("Invalid app value: $app (must be 'cpanel' or 'whm')") unless $app eq 'cpanel' || $app eq 'whm';

# Set global variables based on arguments
$Cpanel::App::appname = $Cpanel::appname = $app;
$ENV{REMOTE_USER} = $Cpanel::authuser = $user;

my $outcome = $app eq 'cpanel'
  ? Cpanel::AccessIds::do_as_user(
    $user,
    sub {
        verbosef( "User %s exists\n", $user );
        can_track_user_analytics();
    }
  )
  : can_track_user_analytics();

print( $outcome ? "yes\n" : "no\n" );

#################################################################################

sub die_usage {
    my $error_msg = shift;
    print_usage(1);
    die "\n\nError: $error_msg\n\n";
}

sub print_usage {
    my ($stderr) = @_;
    my $fh = $stderr ? \*STDERR : \*STDOUT;
    print {$fh} <<"END_USAGE";
Usage: $0 --app=<cpanel|whm> --user=<username> [--help]

Options:
  --app=<cpanel|whm>  Specify the application (cpanel or whm)
  --user=<username>   Specify the username
  --verbose           Print out details of how the outcome was determined
  --help, -h          Show this help message

Examples:
  $0 --app=cpanel --user=testuser
  $0 --app=whm --user=root
END_USAGE
    return;
}

sub verbosef {
    my ( $formatstr, @args ) = @_;
    print STDERR sprintf( '* ' . $formatstr, @args ) if $verbose;
    return;
}

sub can_track_user_analytics {

    # Allow analytics tracking if Leika says analytics consent is disabled.
    # This means ALL other checks AND user consent are ignored if Leika says
    # we do not need consent.
    my $required_by_leika = is_user_analytics_consent_required_by_leika();
    verbosef( "Consent required by Leika: %s\n", $required_by_leika ? 'yes' : 'no' );
    return 1 if !$required_by_leika;

    # If UI analytics are disabled by the system administrator, do not allow tracking.
    my $enabled = is_ui_analytics_enabled();
    verbosef( "Analytics enabled: %s\n", $enabled ? 'yes' : 'no' );
    return 0 if !$enabled;

    if ( Cpanel::App::is_whm() ) {
        my $whm_user_consent = is_whm_user_analytics_enabled();
        verbosef( "WHM user consent via NVData: %s\n", $whm_user_consent ? 'yes' : 'no' );
        return $whm_user_consent;
    }
    elsif ( Cpanel::App::is_cpanel() || Cpanel::App::is_webmail() ) {
        my $cpanel_user_consent = is_cpanel_user_analytics_enabled();
        verbosef( "cPanel user consent via NVData: %s\n", $cpanel_user_consent ? 'yes' : 'no' );
        return $cpanel_user_consent;
    }

    # appname is either not set or an unexpected value, do not allow tracking in this case.
    return 0;

}

sub is_user_analytics_consent_required_by_leika {
    eval { require Cpanel::Leika; };
    if ($@) {

        # If Leika is not available to tell us otherwise, assume consent is required
        return 1;
    }

    my $consent = Cpanel::Leika::get_config('global.analytics_consent.enable');
    verbosef( "Leika analytics consent config value: %s\n", defined $consent ? $consent : 'undefined' );
    return 1 if !defined $consent || $consent eq Cpanel::JSON::true();
    return 0;
}

sub is_ui_analytics_enabled {
    return Cpanel::Analytics::UiIncludes::are_enabled() ? 1 : 0;
}

sub is_whm_user_analytics_enabled {
    my $value = Whostmgr::NVData::get('analytics');
    return 0 if !$value;

    # for some reason the two possible values are the strings 'on' and 'off'
    return $value eq 'on' ? 1 : 0;
}

sub is_cpanel_user_analytics_enabled {
    my $value = Cpanel::API::NVData::_get('analytics')->[0]{'value'};
    return 0 if !$value;

    # for some reason the two possible values are the strings 'on' and 'off'
    return $value eq 'on' ? 1 : 0;
}
Back to Directory File Manager