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

# Contains all sub routines (functions) to dipsplay and manage the visitors
# and manager access logs.

#######################################################################
#
# 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 clear_access_log {
    unlink($sc_access_log_path);
    open( ACCESSFILE, ">$sc_access_log_path" ) || print q{};
    close(ACCESSFILE);
    init_convert_menu_item();
    visitor_access_display();
}

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

sub visitor_access_display {

    print &$manager_page_header( 'Visitor Access Metrics', '', '', '', '' );

    $| = 1;

    print <<ENDOFTEXT;
<h1 class='mgr-page-title'>Visitor Access Log</h1>
<p><br></p>
<p class="well">Here you will learn some important information about your visitors and your store. Note: Clearing the access log will delete all data and cannot be recovered.</p>


<a class="btn btn-primary mgr-center btn-block" href="manager.cgi?mgr_seshun=$mgr_seshun&clear_access_log=yes" role="button"><b>CLEAR ACCESS LOG</b></a><br>
<hr width=665>

<h3 class='mgr-page-title'>These are the pages that are linking to your store</h3>
<p><br></p>

ENDOFTEXT

    open( DATABASE, "$sc_access_log_path" ) || die "Can't Open $sc_access_log_path";

    while (<DATABASE>) {
        ( $url, $shortdate, $requested_page, $visit_number, $ip_address,
            $browser_type, $referring_page, $unix_date ) = split( /\|/, $_ );
        $referring_page = substr( $referring_page, 0, 110 );
        $requested_page = substr( $requested_page, 0, 110 );

        foreach ($referring_page) {
            if ( $requested_page ) {
                $referring_page_count{$referring_page}++;
            }
        }
        foreach ($requested_page) {
            if ( $referring_page eq 'possible bookmarks' ) {
                if ( $requested_page ) {
                    $count_bookmarked_pages{$requested_page}++;
                }
            }
            foreach ($requested_page) {
                if ( $requested_page ) {
                    $count_first_hit_pages{$requested_page}++;
                }
            }
            foreach ($ip_address) {
                if ( $ip_address ) {
                    $count_ip{$ip_address}++;
                }
            }
            foreach ($visit_number) {
                if ( $visit_number ) {
                    $count_visit{$visit_number}++;
                }
            }
            foreach ($browser_type) {
                if ( $browser_type ) {
                    $count_browser{$browser_type}++;
                }
            }
        }
    }

    close DATABASE;

#####

    foreach $referring_page (
        sort { $referring_page_count{$b} <=> $referring_page_count{$a} }
        keys %referring_page_count )
    {

        if ( $referring_page_count{$referring_page} > 1 ) {
            if ( $referring_page =~ /possible bookmarks/i ) {
                print <<ENDOFTEXT;
<p>$referring_page_count{$referring_page} visits from $referring_page</p>
ENDOFTEXT
            }
            else {
                print <<ENDOFTEXT;
<p>$referring_page_count{$referring_page} visits from <a href=$referring_page>$referring_page</a></p>
ENDOFTEXT
            }

        }
    }

#####

    print <<ENDOFTEXT;
<p><hr></p>
<h3 class='mgr-page-title'>These pages appear to be accessed directly, possibly through a bookmark.</h3>
<p><br></p>
ENDOFTEXT

    foreach $requested_page (
        sort { $count_bookmarked_pages{$b} <=> $count_bookmarked_pages{$a} }
        keys %count_bookmarked_pages )
    {

        if ( $count_bookmarked_pages{$requested_page} > 1 )

        {
            print <<ENDOFTEXT;
<p>$count_bookmarked_pages{$requested_page} visits to <a href=$requested_page>$requested_page</a></p>
ENDOFTEXT
        }

    }

#####

    print <<ENDOFTEXT;
<p><hr></p>
<h3 class='mgr-page-title'>These pages were accessed first during visits to your store.</h3>
<p><br></p>
ENDOFTEXT

    foreach $requested_page (
        sort { $count_first_hit_pages{$b} <=> $count_first_hit_pages{$a} }
        keys %count_first_hit_pages )
    {

        if ( $count_first_hit_pages{$requested_page} > 1 ) {
            print <<ENDOFTEXT;
<p>$count_first_hit_pages{$requested_page} first visits to <a href=$requested_page>$requested_page</a></p>
ENDOFTEXT
        }

    }

#####

    print <<ENDOFTEXT;
<br>
<p><hr></p>
<h3 class='mgr-page-title'>I.P. Addresses of the visitors to your store.</h3>
<p><br></p>
ENDOFTEXT

    foreach $ip_address ( sort { $count_ip{$b} <=> $count_ip{$a} }
        keys %count_ip )
    {

        if ( $count_ip{$ip_address} > 1 ) {
            print <<ENDOFTEXT;
<p>$count_ip{$ip_address} visitors from I.P. address <a href=http://$ip_address>$ip_address</a>.</p>
ENDOFTEXT
        }
    }

#####

    print <<ENDOFTEXT;
<p><hr></p>
<h3 class='mgr-page-title'>Web browsers your visitors are using.</h3>
<p><br></p>
ENDOFTEXT

    foreach $browser_type ( sort { $count_browser{$b} <=> $count_browser{$a} }
        keys %count_browser )
    {

        if ( $count_browser{$browser_type} > 1 ) {
            print <<ENDOFTEXT;
<p>$count_browser{$browser_type} visitors use $browser_type as a web browser.</p>
ENDOFTEXT
        }
    }
    print '<p><br><br><br></p>';
    print &$manager_page_footer;

}

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

1;
