$versions{'password_editor-ext_lib.pl'} = '06.6.00.0000';

# Contains all sub routines (functions) for setting the manager side credentials
# and any AgoraCart membership credentials.

#######################################################################
#
# 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) = 'password_editor';
    register_extension( $modname, 'Password Manager', $versions{$modname} );
    add_settings_choice(
        'Password Settings',
        ' Password Settings ',
        'change_Pass_screen'
    );
    register_menu(
        'PasswordSettings', 'write_Pass_settings',
        $modname, 'Write Password Settings'
    );
    register_menu( 'change_Pass_screen', 'display_Pass_screen', $modname,
        'Display Password Manager' );
}

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

sub write_Pass_settings {
    local ($myset) = q{};
    $myset .= "\$username = \"$in{'username'}\";\n";
    $myset .= "\$password = \"$in{'password'}\";\n";
    $myset .= "\$mc_use_cookie_login = \"$in{'mc_use_cookie_login'}\";\n";
    $myset .= "\$a_unique_name = \"$in{'a_unique_name'}\";\n";
    $username            = "$in{'username'}";
    $password            = "$in{'password'}";
    $mc_use_cookie_login = "$in{'mc_use_cookie_login'}";
    $a_unique_name       = "$in{'a_unique_name'}";
    update_Pass_settings( $mc_mgrpassfile, $myset );
    display_Pass_screen();
}

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

sub display_Pass_screen {
    local ($filename) = "$mgrdir/";
    print &$manager_page_header( 'Password Editor', '', '', '', '' );
    print <<ENDOFTEXT;
<h1 class='mgr-page-title'>Password Editor</h1>
<p><br></p>

<p class="well">This area allows you to edit the login information and passwords for your store manager files.  .htaccess (server/site directory) protection is handled separately.  If you wish to add more login users and also allow or turn off certain management functions for each user, please purchase the multi-manager login add-on found in the AgoraCart.com Marketplace. Click on the <i class="fa fa-info-circle"></i> icon next to any setting for more information on the setting.</p>
<p><br></p>
ENDOFTEXT
    if ( $in{'system_edit_success'} ) {
        print <<ENDOFTEXT;
<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-success">
        <h4 class="text-primary mgr-center">Password File has been successfully updated.</h4>
    </div>
</div>
<div class="row col-xs-12 col-sm-12"><p><br></p></div>
ENDOFTEXT
    }
    print qq~
<form action="manager.cgi" method="post">
<input type="hidden" name="mgr_seshun" value="$in{'mgr_seshun'}">

<p><b>Enter a Username: &nbsp; <a data-toggle="collapse" href="#userName" aria-expanded="false" aria-controls="userName"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 20px;"></span><input name="username" size="12" type="text" maxlength="12" value="$username" required></p>
<div class="collapse" id="userName">
     <div class="well">
         <p>12 characters max.</p>
         <p>Do not use spaces, dashes nor meta characters (\$,#,%,&amp;,\@,*,',",-,<,>,?,/,etc).</p>
     </div>
</div>
<p><hr></p>

<p><b>Enter a Password: &nbsp; <a data-toggle="collapse" href="#password" aria-expanded="false" aria-controls="password"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 20px;"></span><input name="password" size="15" type="text" maxlength="15" value="$password" required></p>
<div class="collapse" id="password">
     <div class="well">
         <p>15 characters max.</p>
         <p>Do not use spaces, dashes nor meta characters (\$,#,%,&amp;,\@,*,',",-,<,>,?,/,etc).</p>
     </div>
</div>
<p><hr></p>

<p><b>Do you wish to use browser cookies for manager login?  &nbsp; <a data-toggle="collapse" href="#useMgrCookies" aria-expanded="false" aria-controls="useMgrCookies"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 20px;"></span><select name=mc_use_cookie_login>
<option>$mc_use_cookie_login</option>
<option>yes</option>
<option>no</option>
</select></p>
<div class="collapse" id="useMgrCookies">
     <div class="well">
         <p>Default is YES.</p>
         <p>Requires cookies to be allowed in browser. Allows multiple store manager logins.</p>
         <p><b>LEGACY Note</b>: This is a legacy feature that should be left to the default setting in most cases.</p>
     </div>
</div>
<p><hr></p>

<p><b>Manager Cookies Fallback File Name?  &nbsp; <a data-toggle="collapse" href="#uniqueName" aria-expanded="false" aria-controls="uniqueName"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 20px;"></span><input name="a_unique_name" type="text" class="form-control" maxlength="50" value="$a_unique_name"</p>
<div class="collapse" id="uniqueName">
     <div class="well">
         <p>Pick a unique character string with at least 8 characters.</p>
         <p>Do not use spaces nor meta characters (\$,#,%,&amp;,\@,*,',",<,>,?,/,etc).</p>
         <p> This unique string is used as a backup in case cookies are not enabled above nor if the browser accepts the cookies for the manager.</p>
         <p><b>NOTE</b>: If manager cookies are not available and this fallback feature is used, then only one manager login session is accepted at a time.</p>
     </div>
</div>
<p><br></p>

<input type="hidden" name="system_edit_success" value="yes">
<button name="PasswordSettings" class="btn btn-primary btn-lg btn-block mgr-center" value="Submit">Update Manager Credentials</button>

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

~;
    print &$manager_page_footer;
}

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

sub update_Pass_settings {
    local ( $item, $stuff ) = @_;
    $pass_file_settings{$item} = $stuff;
    local ( $item, $zitem );
    get_file_lock("$mc_mgrpassfile.lockfile");
    open( PASSFILE, ">$mc_mgrpassfile" ) || my_die("Can't Open $mc_mgrpassfile");
    foreach $zitem ( sort( keys %pass_file_settings ) ) {
        $item = $zitem;
        print( PASSFILE $pass_file_settings{$zitem} );
    }
    close(PASSFILE);
    release_file_lock("$mc_mgrpassfile.lockfile");
    require_supporting_libraries( __FILE__, __LINE__,  "$mc_mgrpassfile" );
}

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

1;
