MOON
Server: Apache/2.2.31 (Unix) mod_ssl/2.2.31 OpenSSL/0.9.8e-fips-rhel5 mod_bwlimited/1.4
System: Linux csr818.wilogic.com 2.6.18-419.el5xen #1 SMP Fri Feb 24 22:50:37 UTC 2017 x86_64
User: digitals (531)
PHP: 5.4.45
Disabled: NONE
Upload Files
File: //scripts.20110531.215904.25158/locale_export
#!/usr/bin/perl
# cpanel - locale_export                         Copyright(c) 2010 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

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

use strict;
use warnings;
use Cpanel::CPAN::Getopt::Param    ();
use Cpanel::SafeDir                ();
use Cpanel::Locale                 ();
use Cpanel::Locale::Utils::Display ();
use Cpanel::Locale::Utils::XML     ();

my $prm = Cpanel::CPAN::Getopt::Param->new(
    {
        'no_args_help' => 1,
        'help_coderef' => sub {
            print <<"END_USAGE";
$0: Export locales from the Locale database in XML format.

  --help        - Show this help screen

  --quiet       - By default there is output regarding what the script is doing. This flag turns off that default verbosity.

  --locale=XX   - (required) Specify a locale to export. You can pass the flag multiple times in order to export multiple locales. 
      In this example 'XX' is the locale tag of the locale to export.
      This creates files in a default location that `/scripts/locale_import --locale=XX` knows to use.

  --export-XX=/path/to/the/xml/file - Save the export of the locale XX to this file instead of the standard place. 
      This creates files that can be imported with `/scripts/locale_import --import=/path/to/the/xml/file`
 
The XML files that this script creates can be imported via /scripts/locale_import

If there were problems exporting the script will exit with an error status. To examine the details of what happened do not use --quiet.

END_USAGE
            exit;
        },
    }
);

my $verbose = $prm->get_param('quiet') ? 0 : 1;
my $count   = 0;
my $failed  = 0;
my $locale  = Cpanel::Locale->get_handle();

my %locale_lookup;

@locale_lookup{ Cpanel::Locale::Utils::Display::get_locale_list($locale) } = ();
my $euid_home;
foreach my $loc ( $prm->get_param('locale') ) {
    $count++;

    if ( !exists $locale_lookup{$loc} ) {
        print "Invalid locale\n" if $verbose;
        $failed++;
        next;
    }

    my $path = $prm->get_param("export-$loc") || "/var/cpanel/locale/export/$loc.xml";

    # expand ~/ that was treated as a string and not expanded by the shell before being put in @ARGV
    if ( substr( $path, 0, 2 ) eq '~/' ) {
        $euid_home ||= ( getpwuid($>) )[7];    # only look it up if needed, and only look it up one time
        substr( $path, 0, 2, "$euid_home/" );
    }

    my ( $head, @dir ) = reverse( split( /\//, $path ) );
    my $dir = join( '/', reverse(@dir) );
    if ( $dir && !-d $dir ) {
        if ( !Cpanel::SafeDir::MK::safemkdir($dir) ) {
            print "Could not create path '$dir': $!\n" if $verbose;
            $failed++;
            next;
        }
    }

    if ( -e $path ) {
        print "Replacing existing export file '$path'\n" if $verbose;
        unlink($path);
        if ( -e $path ) {
            print "Could not unlink '$path': $!\n" if $verbose;
            $failed++;
            next;
        }
    }

    my $error = '';

    if ( Cpanel::Locale::Utils::XML::locale_to_xml( $path, $loc, \$error ) ) {
        print "'$loc' has been exported to '$path'.\n" if $verbose;
    }
    else {
        print "Failed to create XML file for locale '$loc': $error\n" if $verbose;
        $failed++;
        next;
    }
}

if ( !$count || $failed ) {
    if ($verbose) {
        if ($failed) {
            print "$failed out of $count failed to export.\n";
        }
        elsif ( !$count ) {
            print "No locales to export.\n";
        }
    }
    exit 1;
}