package cPanel::Ecommerce::OSCommerce;

# cpanel - cPanel/Ecommerce/OSCommerce.pm         Copyright(c) 2013 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

use strict;
use warnings;

our $VERSION = 0.56;
my $pkg = __PACKAGE__;

require Digest::MD5;

our $meta_info = {
    setphpsuexecvar => 1,
    adminuser_pass  => 1,
    admin_email     => 1,
    installdir      => 'osc',
    description     => 'PHP/MySQL based Ecommerce',    # required
    security        => q(
Configuration has to be world readable, that is 0444.
<br />
Requires register_globals to be on in your php.ini, of which the php.ini says:
<pre>
; You should do your best to write your scripts so that they do not require
; register_globals to be on;  Using form variables as globals can easily lead
; to possible security problems, if the code is not very well thought of.
</pre>
<br /> Subject to all common PHP security problems
                      ),

    version       => '2.3.4.cp2',                                                 # ms2-060817', # required
    security_rank => 1,
    website       => 'http://www.oscommerce.com/',
    config_files  => [qw(includes/configure.php admin/includes/configure.php)],
    mysql         => ['osc'],
    chmod         => {
        '0777' => [qw(images admin/images/graphs admin/backups)],
        '0644' => [qw(admin/includes/configure.php)],
        '0444' => [qw(includes/configure.php)],
    },
    adminarea_name => 'OSCommerce Admin area',
    adminarea_path => 'admin',                                                    # admin/_work if SQL auth is used (IE shanna)
};

# see http://forums.oscommerce.com/index.php?showtopic=181939 regarding why there is no table_prefix

if ( defined &Cpanel::cPAddons::proc_keys_named_after_version ) {
    Cpanel::cPAddons::proc_keys_named_after_version( $meta_info, __PACKAGE__ );
}

#### action functions ##
sub install {
    my $cpo  = shift;
    my $salt = $cpo->{'salt'};
    $salt =~ s/^(..).*$/$1/;
    $cpo->{'password_md5_salt'} = Digest::MD5::md5_hex( $salt . $cpo->{'password'} ) . ':' . $salt;
    $cpo->{'installpath_with_slash'} = $cpo->{'installpath'} eq './' ? "" : "$cpo->{'installpath'}/";
    $cpo->stdinstall(@_);
    $cpo->_do_phpsuexec_perms();
}

sub upgrade {
    my $obj = shift;

    # patch --basename-prefix fix, see case 4085
    if ( !$Cpanel::cPAddons::VERSION ) {
        print "cPAddons.pm version could not be determined!";
        return;
    }

    my $ok = 0;
    my ( $maj, $min, $rev ) = split /\./, $Cpanel::cPAddons::VERSION;
    if ( "$maj.$min" > 0.7 ) {
        $ok = 1;
    }
    elsif ( "$maj.$min" < 0.7 ) {
        $ok = 0;
    }
    else {
        if ( $rev > 0 ) {
            $ok = 1;
        }
        else {
            $ok = 0;
        }
    }

    if ( !$ok ) {
        print "Sorry, the OSC cPAddon requires Cpanel::cPAddons v0.7.1 or newer for upgrades. This server only has v$Cpanel::cPAddons::VERSION";
        return;
    }

    my $install = $obj->{'installed'}->{ $obj->{'workinginstall'} };
    my $salt    = $install->{'salt'};
    $salt =~ s/^(..).*$/$1/;
    $install->{'installdir'} = Cpanel::cPAddons::_untaint( $install->{'installdir'} );
    if ( $install->{'installpath'} eq './' ) {
        $install->{'installdir'} .= './' if $install->{'installdir'} !~ m{\./$};    # This is a work around to deal with case 149145.
        $install->{'installdir'} .= '/';                                            # The final slash will be stripped off by stdupgrade().
        $install->{'installpath_with_slash'} = '';
    }
    else {
        $install->{'installpath_with_slash'} = "$install->{'installpath'}/";
    }
    foreach my $file ( @{ $meta_info->{'config_files'} } ) {
        $file = Cpanel::cPAddons::_untaint($file);
        if ( sprintf "%04o", ( stat( $install->{'installdir'} . "/" . $file ) )[2] & 07777 eq '0444' ) {
            chmod 0644, $install->{'installdir'} . "/" . $file;
        }
    }
    $obj->stdupgrade(@_);
}
sub uninstall { shift->stduninstall(@_) }

#### non action functions ##
sub installform   { print shift->{installform} }
sub upgradeform   { print shift->{upgradeform} }
sub uninstallform { print shift->{uninstallform} }

1;
