JFIF$        dd7 

Viewing File: /usr/local/cpanel/scripts/process_site_templates

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

# cpanel - scripts/process_site_templates          Copyright 2022 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 strict;

ProcessSiteTemplates::run(@ARGV) unless caller;

package ProcessSiteTemplates;

use Getopt::Long qw(GetOptionsFromArray);
use Try::Tiny;

use File::Basename;

use Cpanel::Template        ();
use Cpanel::JSON            ();
use Cpanel::PwCache         ();
use Cpanel::SafeDir::MK     ();
use Cpanel::SafeRun::Errors ();
use Cpanel::Tar             ();

sub usage {
    my $exit_code = shift || 0;
    print <<EO_USAGE;
process_site_templates --source=/path/to/site/templates --target=/path/to/target/directory

Process Site Publisher templates from a source directory in order to generate HTML files in the target directory.
This script overwrites any existing files in the target directory.

    Options:
      --define para1=val1 Provide a value for a parameter that the template uses.

EO_USAGE

    require POSIX;
    POSIX::_exit($exit_code);
    return;
}

sub bail_out {
    my ($msg) = @_;
    print STDERR "Error: $msg\n\n";
    usage(1);
    return;
}

my ( $source, $target, $config, %parameters, $help, $tt, $bu_file, $config_file, $homedir );

sub run {
    my (@args) = @_;

    die "Do not run this script as the root user" if ( $> == 0 );

    $homedir = Cpanel::PwCache::gethomedir();

    my %parameters = ();

    GetOptionsFromArray(
        \@args,
        'source=s' => \$source,
        'target=s' => \$target,
        'config=s' => \$config,
        'define=s' => \%parameters,
        'help'     => \$help,
    ) || usage(1);

    if ($help) {
        usage(0);
    }

    unless ( $target && -d $target && $target =~ m/^\// ) {
        bail_out("The target directory ($target) is invalid.");
    }

    my $config_dir = $homedir . '/site_publisher/configurations';
    unless ( -d $config_dir ) {
        Cpanel::SafeDir::MK::safemkdir($config_dir);
    }
    chmod 0700, $config_dir;

    $config_file = join( '-', split( /\/+/, $target ) );
    $config_file =~ s{^-}{};
    $config_file .= '.json';
    $config_file = $config_dir . '/' . $config_file;

    my $data_file = $target . '/configurations.json';

    my $current_settings = {};

    if ( -e $config_file ) {
        eval { $current_settings = Cpanel::JSON::LoadFile($config_file) };
    }
    elsif ( -e $data_file ) {
        eval { $current_settings = Cpanel::JSON::LoadFile($data_file) };
        save_settings($current_settings);
    }
    unlink $data_file;

    try {
        do_archive();
    }
    catch {
        die "Unable to create a backup of the target directory: $_";
    };

    if ($config) {
        if ( -e $config ) {
            eval { $current_settings = Cpanel::JSON::LoadFile($config) };
        }
        else {
            die "Unable to read config file: $config";
        }
    }

    unless ( $source && -d $source ) {
        if ( $current_settings->{path} && $current_settings->{template} ) {
            $source = $current_settings->{path} . '/' . $current_settings->{template};
        }

        bail_out("The source directory ($source) is invalid.") unless $source && -d $source;
    }

    foreach my $k ( keys %parameters ) {
        $current_settings->{$k} = $parameters{$k};
    }

    try {
        process_templates($current_settings);
    }
    catch {
        my $msg = $_;

        if ( $bu_file && -e $bu_file ) {
            restore_target();
        }

        die "Publication of site template failed: $msg";
    };

    my $template = basename($source);
    my $path     = dirname($source);
    $current_settings->{'template'} = $template;
    $current_settings->{'path'}     = $path;

    save_settings($current_settings);

    return 1;
}

sub process_templates {
    my ( $data, $current_dir ) = @_;

    my $template_dir = $source;
    $template_dir .= '/' . $current_dir if ($current_dir);
    return unless ( -d $template_dir && opendir my $dh, $template_dir );

    my $dest_dir = $target;
    $dest_dir .= '/' . $current_dir if ($current_dir);
    die "The system could not create the destination directory: $dest_dir." unless ( -d $dest_dir || mkdir $dest_dir, 0755 );

    my @sub_directories = ();
    foreach my $tmpl ( readdir($dh) ) {
        next if $tmpl =~ m/^\./;
        next if $tmpl eq 'meta.json';
        next if $tmpl eq 'preview.png';
        next if $tmpl =~ m/README/i;

        if ( -d $template_dir . '/' . $tmpl ) {
            push @sub_directories, $current_dir . '/' . $tmpl;
            next;
        }

        my $dest = $tmpl;
        if ( $tmpl =~ m/\.tt$/ ) {
            $dest =~ s/\.tt$//;
            $data->{template_file} = $tmpl;
            die unless open( my $fh, '>', $dest_dir . '/' . $dest );
            my ( $success, $output ) = Cpanel::Template::process_template(
                'site_template',
                $data,
                {
                    'print_yn'     => 0,
                    'include_path' => [ $template_dir, $source ],
                }
            );
            print {$fh} $$output;
            close $fh;
        }
        else {
            my $ret = Cpanel::SafeRun::Errors::saferunonlyerrors( '/bin/cp', '-f', $template_dir . '/' . $tmpl, $dest_dir . '/' . $dest );
            die $ret if scalar grep ( /cannot/i, split( /\n/, $ret ) );
        }
    }
    close $dh;

    foreach my $sub (@sub_directories) {
        process_templates( $data, $sub );
    }

    return 1;
}

sub do_archive {
    $bu_file = join( '-', split( /\/+/, $target ) );
    $bu_file =~ s{^-}{};
    $bu_file .= '-' . $$ . '-' . time();

    my $bu_dir = $homedir . '/site_publisher/backups';
    unless ( -d $bu_dir ) {
        Cpanel::SafeDir::MK::safemkdir($bu_dir);
    }
    chmod 0700, $bu_dir;

    # discard things older than 30 days
    if ( opendir( my $dh, $bu_dir ) ) {
        my @old_files = grep /-\d+-\d+\.tar\.gz$/, readdir($dh);
        closedir $dh;

        my $current = time();
        my $cutoff  = $current - 30 * 24 * 60 * 60;

        foreach my $f (@old_files) {
            my @pots  = split( /-/, $f );
            my $stamp = $pots[-1];
            $stamp =~ s{\.tar\.gz$}{};
            if ( $stamp < $cutoff ) {
                unlink $bu_dir . '/' . $f;
            }
        }
    }

    $bu_file = $bu_dir . '/' . $bu_file . '.tar.gz';
    my $ret = Cpanel::SafeRun::Errors::saferunonlyerrors( Cpanel::Tar::load_tarcfg()->{'bin'}, '-c', '-v', '-z', '-f', $bu_file, $target );
    die $ret if scalar grep ( /error/i, split( /\n/, $ret ) );

    return;
}

sub restore_target {
    Cpanel::SafeRun::Errors::saferunnoerror( '/usr/bin/rm', '-rf', '--', $target );
    chdir '/';
    Cpanel::SafeRun::Errors::saferunnoerror( Cpanel::Tar::load_tarcfg()->{'bin'}, '-x', '-v', '-z', '-f', $bu_file );

    return;
}

sub save_settings {
    my ($settings) = @_;
    my $orig_mask = umask 0077;
    my $out_fh;
    unless ( open( $out_fh, '>', $config_file ) ) {
        die "The system could not save settings to the file: $config_file";
    }

    require Cpanel::JSON::Sanitize;
    print {$out_fh} Cpanel::JSON::Dump( Cpanel::JSON::Sanitize::sanitize_for_dumping($settings) );
    close $out_fh;
    umask $orig_mask;

    return;
}

1;
Back to Directory  nL+D550H?Mx ,D"v]qv;6*Zqn)ZP0!1 A "#a$2Qr D8 a Ri[f\mIykIw0cuFcRı?lO7к_f˓[C$殷WF<_W ԣsKcëIzyQy/_LKℂ;C",pFA:/]=H  ~,ls/9ć:[=/#f;)x{ٛEQ )~ =𘙲r*2~ a _V=' kumFD}KYYC)({ *g&f`툪ry`=^cJ.I](*`wq1dđ#̩͑0;H]u搂@:~וKL Nsh}OIR*8:2 !lDJVo(3=M(zȰ+i*NAr6KnSl)!JJӁ* %݉?|D}d5:eP0R;{$X'xF@.ÊB {,WJuQɲRI;9QE琯62fT.DUJ;*cP A\ILNj!J۱+O\͔]ޒS߼Jȧc%ANolՎprULZԛerE2=XDXgVQeӓk yP7U*omQIs,K`)6\G3t?pgjrmۛجwluGtfh9uyP0D;Uڽ"OXlif$)&|ML0Zrm1[HXPlPR0'G=i2N+0e2]]9VTPO׮7h(F*癈'=QVZDF,d߬~TX G[`le69CR(!S2!P <0x<!1AQ "Raq02Br#SCTb ?Ζ"]mH5WR7k.ۛ!}Q~+yԏz|@T20S~Kek *zFf^2X*(@8r?CIuI|֓>^ExLgNUY+{.RѪ τV׸YTD I62'8Y27'\TP.6d&˦@Vqi|8-OΕ]ʔ U=TL8=;6c| !qfF3aů&~$l}'NWUs$Uk^SV:U# 6w++s&r+nڐ{@29 gL u"TÙM=6(^"7r}=6YݾlCuhquympǦ GjhsǜNlɻ}o7#S6aw4!OSrD57%|?x>L |/nD6?/8w#[)L7+6〼T ATg!%5MmZ/c-{1_Je"|^$'O&ޱմTrb$w)R$& N1EtdU3Uȉ1pM"N*(DNyd96.(jQ)X 5cQɎMyW?Q*!R>6=7)Xj5`J]e8%t!+'!1Q5 !1 AQaqё#2"0BRb?Gt^## .llQT $v,,m㵜5ubV =sY+@d{N! dnO<.-B;_wJt6;QJd.Qc%p{ 1,sNDdFHI0ГoXшe黅XۢF:)[FGXƹ/w_cMeD,ʡcc.WDtA$j@:) -# u c1<@ۗ9F)KJ-hpP]_x[qBlbpʖw q"LFGdƶ*s+ډ_Zc"?%t[IP 6J]#=ɺVvvCGsGh1 >)6|ey?Lӣm,4GWUi`]uJVoVDG< SB6ϏQ@ TiUlyOU0kfV~~}SZ@*WUUi##; s/[=!7}"WN]'(L! ~y5g9T̅JkbM' +s:S +B)v@Mj e Cf jE 0Y\QnzG1д~Wo{T9?`Rmyhsy3!HAD]mc1~2LSu7xT;j$`}4->L#vzŏILS ֭T{rjGKC;bpU=-`BsK.SFw4Mq]ZdHS0)tLg