#$versions{'encryption-ext_lib.pl'} = '06.5.00.0000';

# Contains all sub routines (functions) for GnuPG (and PGP) management.

# UPDATE NEEDED - this file needs serious updates before uncommenting menu items

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

{
    local ($modname) = 'gnupg encryption';
    register_extension( $modname, 'GnuPG Settings', $versions{$modname} );

    add_settings_choice(
        'gnupg editor',
        'GnuPG/PGP Settings',
        'change_PGP_screen'
    );
    register_menu(
        'change_PGP_screen', 'show_encryption_screen',
        $modname,  'Display Encryption Settings'
    );
    register_menu(
        'ConfigPGP', 'write_pgp_settings',
        $modname,  'Write Encryption Settings'
    );
}

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

sub write_pgp_settings {
    local ( $my_message, $my_ans, $my_str, $my_command );
    local ($myset)        = q{};
    local ($pgp_settings) = './admin_files/agora_user_lib.pl';
    local ($order_email);

    $order_email = $in{'email_address_for_orders'};
    $order_email =~ s/\@/\\@/;

    $myset .= "\$sc_use_pgp = \"$in{'pgp_orders_yes_no'}\";\n";
    $myset .= "\$sc_pgp_change_newline = \"$in{'sc_pgp_change_newline'}\";\n";
    $myset .= "\$sc_pgp_or_gpg = \"$in{'pgp_or_gpg'}\";\n";
    $myset .= "\$sc_pgp_or_gpg_path = \"$in{'pgp_or_gpg_path'}\";\n";
    $myset .= "\$sc_pgp_order_email = \"$order_email\";\n";

    update_store_settings( 'pgp', $myset );

    require $pgp_settings;

    if ( $in{'system_edit_success'} ) {
        $my_message = q{};
        if ( -e $sc_pgp_or_gpg_path ) {
            $my_message .= q{};
        }
        else {
            $my_message .=
              qq{<br>Warning: Program not detected at $sc_pgp_or_gpg_path};
        }
        if ( $in{'public_key_to_import'} ) {
            $datafile = './admin_files/import_pgp_temp.txt';
            open( DATABASE, ">$datafile" );
            $my_key = $in{'public_key_to_import'};
            print DATABASE $my_key;
            close(DATABASE);
            $my_str = $sc_pgp_or_gpg_path;
            $my_str =~ /(.*)/;
            $my_str      = $1;
            $old_path    = $ENV{'PATH'};
            $ENV{'PATH'} = q{};

            if ( $sc_pgp_or_gpg eq 'GPG' ) {
                if ( length($my_key) > 15 ) {
                    $my_command = "--home ./pgpfiles";
                    $my_command .= " --allow-non-selfsigned-uid --import";
                    $my_ans = `$my_str $my_command $datafile 2>&1`;
                }
                $my_ans .= "\nLIST OF KEYS ON GPG KEYRING:\n";
                $my_ans .= `$my_str --home ./pgpfiles --list-keys 2>&1`;
                $my_ans =~ s/\</\&\#60;/g;
                $my_ans =~ s/\>/\&\#62;/g;
            }
            else {    # using pgp
                $ENV{'PGPPATH'} = './pgpfiles';
                chop($my_str);
                $my_str .= 'k';    #use same path but the pgpk instead of pgpe
                if ( length($my_key) > 15 ) {
                    $my_ans =
                      `/bin/echo "\n\n\n\n\n"|$my_str -a $datafile 2>&1`;
                }
                $my_ans .= "\nLIST OF KEYS ON PGP KEYRING:\n";
                $my_ans .= `$my_str -l 2>&1`;
                $my_ans =~ s/\</\&\#60;/g;
                $my_ans =~ s/\>/\&\#62;/g;
            }
            $my_message .= "<PRE>$my_ans</PRE>";
            unlink $datafile;
            $pgp_update_message = $my_message;
            $ENV{'PATH'} = $old_path;
        }
        else {
        }
    }

    show_encryption_screen();

}

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

sub show_encryption_screen {

    print &$manager_page_header( 'Encryption', '', '', '', '' );

    $nl_stuff = $sc_pgp_change_newline;
    $nl_stuff = 'Leave Alone' if $nl_stuff eq '';

    print <<ENDOFTEXT;
<form action="manager.cgi" method="post">
<input type="hidden" name="mgr_seshun" value="$in{'mgr_seshun'}">

ENDOFTEXT

    if ( $in{'system_edit_success'} ) {
        print <<ENDOFTEXT;
    <div class='row'>
        <p class='col-md-12 bg-success text-center' style='padding:10px;'>Encryption settings have been successfully updated. <br><br>$pgp_update_message</p>
    </div>
    <p><br></p>
ENDOFTEXT
    }

    print <<ENDOFTEXT;
<h1 class='mgr-page-title'><b>GnuPG / PGP Encryption Settings</b></h1>

<br><br>
<p class="well">This is a legacy and advanced application that requires knowledge of how to install encryption on hosting accounts and web servers. <b>This is Not Required</b> unless you are going to use encryption keys with the Offline gateway in order to store and read the entire order information by emails securely.
<br><br><i>It is recommended that you use other payment gateways such as AuthorizeNet, iTransact or AgoraPay instead of the Offline gateway, even with PGP or GnuPG, as it is no longer PCI compliant.</i>
<br><br>If you are unsure about how to set this up, leave this area as is or you can disable the operation of your cart. For help on this subject, please consult with your hosting company and for DIY information.
<br><br>Versions of PGP vary a bit, hopefully the command flags in pgp-lib.pl won't need to be adjusted.</p>
<p><br></p>

<p><b>Do you wish to have orders encrypted in the log file and email? &nbsp; <a data-toggle="collapse" href="#encryptLogsAndEmails" aria-expanded="false" aria-controls="encryptLogsAndEmails"><i class="fa  fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><select name="pgp_orders_yes_no">
<option>$sc_use_pgp</option>
<option>yes</option>
<option>no</option>
</select></p>
<div class="collapse" id="encryptLogsAndEmails">
     <div class="well">
         <p>It is important to protect the privacy of your customer's credit card information, especially if it is going to be emailed to you for offline processing.</p>
     </div>
</div>
<p><hr></p>


<p><b>Choose how to encrypt and/or verify orders on your host server: &nbsp; <a data-toggle="collapse" href="#encryptChoice" aria-expanded="false" aria-controls="encryptChoice"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><select name=pgp_or_gpg>
<option>$sc_pgp_or_gpg</option>
<option>GPG</option>
<option>PGP</option>
</select></p>
<div class="collapse" id="encryptChoice">
     <div class="well">
         <p><a href="https://www.symantec.com/products/encryption" target="_blank">PGP (Pretty Good Privacy)
         software</a> is available for both servers and the workstation to encrypt and decrypt email. <b<It costs money.</b></p>
         <p><a href="http://www.gnupg.org" target="_blank">GPG, or GNU Privacy Guard</a>, is a free replacement for PGP.</p>
     </div>
</div>
<p><hr></p>

<p><b>Enter the path to the Encruption software on the server: &nbsp; <a data-toggle="collapse" href="#serverSoftPath" aria-expanded="false" aria-controls="serverSoftPath"><i class="fa  fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><input name="pgp_or_gpg_path" type="text" value="$sc_pgp_or_gpg_path" class="form-control" maxlength="75"></p>
<div class="collapse" id="serverSoftPath">
     <div class="well">
         <p>Example: <b>/usr/local/bin/pgpe</b> or <b>/usr/local/bin/gpg</b></p>
     </div>
</div>
<p><br></p>

<p class="well">Note for PGP users only: The path to the key program pgpk is assumed to be obtained by changing the last letter of the path above to the letter 'k'.  If this is not correct, then you will have to use the command line to import public keys into PGP.</p>
<p><br></p>

<p><b>Enter the e-mail address to use to lookup an encryption key on the keyring: &nbsp; <a data-toggle="collapse" href="#keyRingEmail" aria-expanded="false" aria-controls="keyRingEmail"><i class="fa  fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><input name="email_address_for_orders" type="text" value="$sc_pgp_order_email" class="form-control" maxlength="75"></p>
<div class="collapse" id="keyRingEmail">
     <div class="well">
         <p>This will be the Public Key used to encrypt the orders.
         <p>If you have not already added a copy of this public key to the keyring, you may do so in the "Public Key to Add" field below.</p>
         <p>Example: <b>sales\@yourdomain.com</b></p>
     </div>
</div>
<p><br></p>

<p><b>Convert Newlines to anything? </b><span style="padding-left: 25px;"></span><select name=sc_pgp_change_newline>
<option value="$sc_pgp_change_newline">$nl_stuff</option>
<option value="">Leave Alone</option>
<option>\\r\\n</option>
<option>\\n\\r</option>
<option>\\r</option>
</select></p>
<p><br></p>

<p><b>Public Key to Add: &nbsp; <a data-toggle="collapse" href="#publicKeyToAdd" aria-expanded="false" aria-controls="publicKeyToAdd"><i class="fa fa-info-circle"></i></a></b><textarea name="public_key_to_import" class="form-control" rows="4" wrap="off"></textarea></p>
<div class="collapse" id="publicKeyToAdd">
     <div class="well">
         <p>You may paste an "ascii-armored" public key to add to the keyring here. PGP software exports these with a <b>.asc</b> extension.</p>
         <p>Note: PGP and GPG use different keyrings.  If you switch from using one to the other, you will need to repeat this public key import step.</p>
         <p>Also, just by putting a blank space or two here, the list of keys already loaded will be listed.</p>
     </div>
</div>
<p><br></p>

<input type="hidden" name="system_edit_success" value="yes">
<input name="ConfigPGP" type="hidden" value="Submit">
<button class="btn btn-primary btn-lg mgr-center" style="display: block;">Submit Encryption Updates</button>

<p><br><br><br></p>

</form>

ENDOFTEXT
    print &$manager_page_footer;
}

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

1;
