$versions{'dates_mgr.pl'} = '06.6.00.0000';

# Contains all sub routines (functions) for dates that the store manager needs.

#######################################################################
#
# AgoraCart and all associated files, except where noted, are
# Copyright 2001 to Present jointly by K-Factor Technologies, Inc.
# and by C E Mayo (aka Mister Ed) at AgoraCart.com & K-Factor.net
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This copyright notice may not be removed or altered in any way.
#
#######################################################################

sub file_open_error {
    local ( $bad_file, $script_section, $this_file, $line_number ) = @_;
    print "FILE OPEN ERROR-$bad_file", $this_file, $line_number, "<br>\n";
    update_error_log( "FILE OPEN ERROR-$bad_file", $this_file, $line_number );
}

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

sub zcode_error {
    local ( $ZCODE, $at, $file, $line ) = @_;
    update_error_log( "zcode compilation error:\n$at\n$ZCODE", $file, $line );
    call_exit();
}

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

sub update_error_log {
    local ( $type_of_error, $file_name, $line_number ) = @_;
    local ( $log_entry, $email_body, $variable, @env_vars );
    @env_vars = sort( keys(%ENV) );
    $date     = get_date();
    if ( $sc_debug_mode eq 'yes' ) {
        if ( $sc_header_printed ne 1 ) {
            if ( $sc_browser_header eq '' ) {
                $sc_browser_header = "Content/type: text/html;\n\n";
            }
            print $sc_browser_header;
        }
        local ($browser_text) = $type_of_error;
        $browser_text =~ s/\|/\<br\>\n/g;
        $browser_text =~ s/\</\&lt;/g;
        $browser_text =~ s/\>/\&gt;/g;
        print q{<div align="left"><table width="665"><tr><td>} . "\n<pre>";
        print "ERROR:$browser_text<br>",
          "FILE: $file_name<br>",
          "LINE: $line_number<br/>\n";
        print '</pre></td></tr></table></div>' . "\n";
    }
    if (   ( $sc_shall_i_log_errors =~ /yes/i )
        || ( $sc_shall_i_log_errors eq '' ) )
    {
        $log_entry =
          "MGR: $type_of_error\|FILE=$file_name\|LINE=$line_number\|";
        $log_entry .= "DATE=$date\|";

        get_file_lock("$sc_error_log_path.lockfile");
        open( ERROR_LOG, ">>$sc_error_log_path" )
          || CgiDie('The Error Log could not be opened');
        foreach $variable (@env_vars) {
            $log_entry .= "$variable: $ENV{$variable}\|";
        }
        $log_entry =~ s/\n/<br>/g;    # do not want newlines!
        print ERROR_LOG "$log_entry\n";
        close(ERROR_LOG);
        release_file_lock("$sc_error_log_path.lockfile");
    }
}

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

sub clear_error_log {
    unlink($sc_error_log_path );
    open( LOGFILE, ">$sc_error_log_path " ) || print q{};
    close(LOGFILE);
    init_convert_menu_item();
    display_error_log();
}

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

sub my_die {
    local ($msg) = @_;
    print 'The manager program cannot continue for the following reason:'
        . "<br>\n$msg\n";
    print '<br><br>\nPermissions are the most common cause of errors. &nbsp;';
    print 'The following directories and files contained within need to be '
        . " made read/write:<br>\n";

    $msg = '&nbsp;&nbsp;&nbsp;&nbsp;';

    print "$msg admin_files <br>\n";
    print "$msg data_files <br>\n";
    print "$msg log_files <br>\n";
    print "$msg pgpfiles \(only when updating public keys)<br>\n";
    print "$msg protected/files <br>\n";
    print "$msg shopping_carts <br>\n";

    $ENV{'PATH'} = "/bin:/usr/bin";
    $result = `id `;    # find out the unix id we are running under

    print "<br>CGI programs run under the following id: &nbsp;$result<br>";
    print 'If your user id is not listed above, then you must chmod 777 ',
    'those directories and files.<br/>';
    print 'If your user id is listed above, then you may chmod 755 ',
    'everything and try to chmod 700 the order log file.<br>';

    call_exit();
}

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

sub no_htaccess_welcome_error {

    $mgr_htaccess_error_text =~ s/\[\[mgrdirname\]\]/$mgrdirname/;
    $scm_no_htaccess_err_msg = <<ENDOFTEXT;
<div class="row col-sm-12 col-md-12 col-lg-12 hidden-xs">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 mgr-danger text-center">
<h3>$mgr_security_notice_header</h3>
<p>$mgr_htaccess_error_text<a href="manager.cgi?mgr_seshun=$mgr_seshun&htaccess_screen=yes">$mgr_click_here</a>.</p>
</div>
<div class="row col-sm-12 col-md-12 col-lg-12 hidden-xs"><p> </p></div>
</div>
ENDOFTEXT

    #scm_no_htaccess_err_msg =~ s/\[\[mgr_seshun\]\]/$mgr_seshun/; #mgr_seshun not ready, replace now.
    print $scm_no_htaccess_err_msg;

}

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

sub html_eval_settings {
    local ($errs) = q{};
    $errs = eval_store_settings();

    if ( $errs ) {
        $errs = qq~
        <div class="row col-xs-12 col-sm-12 col-md-12 col-lg-12 hidden-xs">
            <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 bg-danger">
                <h4 class="text-danger mgr-center">Errors - Store Settings</h4>
                <p class="text-danger">$errs</p>
            </div>
        </div>
        <div class="row col-xs-12 col-sm-12"><p><br><br></p></div>\n~;
    }

    return $errs;
}

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

1;
