View | Details | Raw Unified | Return to bug 10952
Collapse All | Expand All

(-)a/C4/Auth.pm (-17 / +33 lines)
Lines 49-55 BEGIN { Link Here
49
    @EXPORT      = qw(&checkauth &get_template_and_user &haspermission &get_user_subpermissions);
49
    @EXPORT      = qw(&checkauth &get_template_and_user &haspermission &get_user_subpermissions);
50
    @EXPORT_OK   = qw(&check_api_auth &get_session &check_cookie_auth &checkpw &checkpw_internal &checkpw_hash
50
    @EXPORT_OK   = qw(&check_api_auth &get_session &check_cookie_auth &checkpw &checkpw_internal &checkpw_hash
51
                      &get_all_subpermissions &get_user_subpermissions
51
                      &get_all_subpermissions &get_user_subpermissions
52
                      ParseSearchHistoryCookie
52
                      ParseSearchHistorySession SetSearchHistorySession
53
                   );
53
                   );
54
    %EXPORT_TAGS = ( EditPermissions => [qw(get_all_subpermissions get_user_subpermissions)] );
54
    %EXPORT_TAGS = ( EditPermissions => [qw(get_all_subpermissions get_user_subpermissions)] );
55
    $ldap        = C4::Context->config('useldapserver') || 0;
55
    $ldap        = C4::Context->config('useldapserver') || 0;
Lines 262-268 sub get_template_and_user { Link Here
262
262
263
            # And if there's a cookie with searches performed when the user was not logged in,
263
            # And if there's a cookie with searches performed when the user was not logged in,
264
            # we add them to the logged-in search history
264
            # we add them to the logged-in search history
265
            my @recentSearches = ParseSearchHistoryCookie($in->{'query'});
265
            my @recentSearches = ParseSearchHistorySession($in->{'query'});
266
            if (@recentSearches) {
266
            if (@recentSearches) {
267
                my $sth = $dbh->prepare($SEARCH_HISTORY_INSERT_SQL);
267
                my $sth = $dbh->prepare($SEARCH_HISTORY_INSERT_SQL);
268
                $sth->execute( $borrowernumber,
268
                $sth->execute( $borrowernumber,
Lines 273-286 sub get_template_and_user { Link Here
273
                           $_->{'time'},
273
                           $_->{'time'},
274
                        ) foreach @recentSearches;
274
                        ) foreach @recentSearches;
275
275
276
                # And then, delete the cookie's content
277
                my $newsearchcookie = $in->{'query'}->cookie(
278
                                            -name => 'KohaOpacRecentSearches',
279
                                            -value => encode_json([]),
280
                                            -HttpOnly => 1,
281
                                            -expires => ''
282
                                         );
283
                $cookie = [$cookie, $newsearchcookie];
284
            }
276
            }
285
        }
277
        }
286
    }
278
    }
Lines 297-303 sub get_template_and_user { Link Here
297
     # Anonymous opac search history
289
     # Anonymous opac search history
298
     # If opac search history is enabled and at least one search has already been performed
290
     # If opac search history is enabled and at least one search has already been performed
299
     if (C4::Context->preference('EnableOpacSearchHistory')) {
291
     if (C4::Context->preference('EnableOpacSearchHistory')) {
300
        my @recentSearches = ParseSearchHistoryCookie($in->{'query'}); 
292
        my @recentSearches = ParseSearchHistorySession($in->{'query'});
301
        if (@recentSearches) {
293
        if (@recentSearches) {
302
            $template->param(ShowOpacRecentSearchLink => 1);
294
            $template->param(ShowOpacRecentSearchLink => 1);
303
        }
295
        }
Lines 647-652 sub checkauth { Link Here
647
    my ( $userid, $cookie, $sessionID, $flags, $barshelves, $pubshelves );
639
    my ( $userid, $cookie, $sessionID, $flags, $barshelves, $pubshelves );
648
    my $logout = $query->param('logout.x');
640
    my $logout = $query->param('logout.x');
649
641
642
    my $anon_search_history;
643
650
    # This parameter is the name of the CAS server we want to authenticate against,
644
    # This parameter is the name of the CAS server we want to authenticate against,
651
    # when using authentication against multiple CAS servers, as configured in Auth_cas_servers.yaml
645
    # when using authentication against multiple CAS servers, as configured in Auth_cas_servers.yaml
652
    my $casparam = $query->param('cas');
646
    my $casparam = $query->param('cas');
Lines 695-701 sub checkauth { Link Here
695
            #if a user enters an id ne to the id in the current session, we need to log them in...
689
            #if a user enters an id ne to the id in the current session, we need to log them in...
696
            #first we need to clear the anonymous session...
690
            #first we need to clear the anonymous session...
697
            $debug and warn "query id = $q_userid but session id = $s_userid";
691
            $debug and warn "query id = $q_userid but session id = $s_userid";
698
            $session->flush;      
692
            $anon_search_history = $session->param('search_history');
693
            $session->flush;
699
            $session->delete();
694
            $session->delete();
700
            C4::Context->_unset_userenv($sessionID);
695
            C4::Context->_unset_userenv($sessionID);
701
            $sessionID = undef;
696
            $sessionID = undef;
Lines 755-760 sub checkauth { Link Here
755
750
756
        #we initiate a session prior to checking for a username to allow for anonymous sessions...
751
        #we initiate a session prior to checking for a username to allow for anonymous sessions...
757
        my $session = get_session("") or die "Auth ERROR: Cannot get_session()";
752
        my $session = get_session("") or die "Auth ERROR: Cannot get_session()";
753
754
        # Save anonymous search history in new session so it can be retrieved
755
        # by get_template_and_user to store it in user's search history after
756
        # a successful login.
757
        if ($anon_search_history) {
758
            $session->param('search_history', $anon_search_history);
759
        }
760
758
        my $sessionID = $session->id;
761
        my $sessionID = $session->id;
759
        C4::Context->_new_userenv($sessionID);
762
        C4::Context->_new_userenv($sessionID);
760
        $cookie = $query->cookie(
763
        $cookie = $query->cookie(
Lines 966-971 sub checkauth { Link Here
966
                    $info{'invalid_username_or_password'} = 1;
969
                    $info{'invalid_username_or_password'} = 1;
967
                    C4::Context->_unset_userenv($sessionID);
970
                    C4::Context->_unset_userenv($sessionID);
968
                }
971
                }
972
                $session->param('lasttime',time());
973
                $session->param('ip',$session->remote_addr());
969
            }
974
            }
970
        }    # END if ( $userid    = $query->param('userid') )
975
        }    # END if ( $userid    = $query->param('userid') )
971
        elsif ($type eq "opac") {
976
        elsif ($type eq "opac") {
Lines 1773-1788 sub getborrowernumber { Link Here
1773
    return 0;
1778
    return 0;
1774
}
1779
}
1775
1780
1776
sub ParseSearchHistoryCookie {
1781
sub ParseSearchHistorySession {
1777
    my $input = shift;
1782
    my $cgi = shift;
1778
    my $search_cookie = $input->cookie('KohaOpacRecentSearches');
1783
    my $sessionID = $cgi->cookie('CGISESSID');
1779
    return () unless $search_cookie;
1784
    return () unless $sessionID;
1780
    my $obj = eval { decode_json(uri_unescape($search_cookie)) };
1785
    my $session = get_session($sessionID);
1786
    return () unless $session and $session->param('search_history');
1787
    my $obj = eval { decode_json(uri_unescape($session->param('search_history'))) };
1781
    return () unless defined $obj;
1788
    return () unless defined $obj;
1782
    return () unless ref $obj eq 'ARRAY';
1789
    return () unless ref $obj eq 'ARRAY';
1783
    return @{ $obj };
1790
    return @{ $obj };
1784
}
1791
}
1785
1792
1793
sub SetSearchHistorySession {
1794
    my ($cgi, $search_history) = @_;
1795
    my $sessionID = $cgi->cookie('CGISESSID');
1796
    return () unless $sessionID;
1797
    my $session = get_session($sessionID);
1798
    return () unless $session;
1799
    $session->param('search_history', uri_escape(encode_json($search_history)));
1800
}
1801
1786
END { }    # module clean-up code here (global destructor)
1802
END { }    # module clean-up code here (global destructor)
1787
1;
1803
1;
1788
__END__
1804
__END__
(-)a/koha-tmpl/opac-tmpl/prog/en/modules/opac-search-history.tt (-1 / +1 lines)
Lines 51-57 Link Here
51
		<tbody>
51
		<tbody>
52
		    [% FOREACH recentSearche IN recentSearches %]
52
		    [% FOREACH recentSearche IN recentSearches %]
53
		    <tr>
53
		    <tr>
54
            <td><span title="[% recentSearche.time %]">[% recentSearche.time |$KohaDates with_hours => 1 %]</span></td>
54
            <td><span title="[% recentSearche.time %]">[% recentSearche.time %]</span></td>
55
			<td><a href="/cgi-bin/koha/opac-search.pl?[% recentSearche.query_cgi |html %]">[% recentSearche.query_desc |html %]</a></td>
55
			<td><a href="/cgi-bin/koha/opac-search.pl?[% recentSearche.query_cgi |html %]">[% recentSearche.query_desc |html %]</a></td>
56
			<td>[% recentSearche.total %]</td>
56
			<td>[% recentSearche.total %]</td>
57
		    </tr>
57
		    </tr>
(-)a/opac/opac-search-history.pl (-13 / +8 lines)
Lines 20-26 Link Here
20
use strict;
20
use strict;
21
use warnings;
21
use warnings;
22
22
23
use C4::Auth qw(:DEFAULT get_session ParseSearchHistoryCookie);
23
use C4::Auth qw(:DEFAULT get_session ParseSearchHistorySession SetSearchHistorySession);
24
use CGI;
24
use CGI;
25
use JSON qw/decode_json encode_json/;
25
use JSON qw/decode_json encode_json/;
26
use C4::Context;
26
use C4::Context;
Lines 49-70 if (!$loggedinuser) { Link Here
49
49
50
    # Deleting search history
50
    # Deleting search history
51
    if ($cgi->param('action') && $cgi->param('action') eq 'delete') {
51
    if ($cgi->param('action') && $cgi->param('action') eq 'delete') {
52
	# Deleting cookie's content 
52
        # Deleting cookie's content
53
	my $recentSearchesCookie = $cgi->cookie(
53
        SetSearchHistorySession($cgi, []);
54
	    -name => 'KohaOpacRecentSearches',
54
55
	    -value => encode_json([]),
55
        # Redirecting to this same url with the cookie in the headers so it's deleted immediately
56
	    -expires => ''
56
        my $uri = $cgi->url();
57
	    );
57
        print $cgi->redirect(-uri => $uri);
58
59
	# Redirecting to this same url with the cookie in the headers so it's deleted immediately
60
	my $uri = $cgi->url();
61
	print $cgi->redirect(-uri => $uri,
62
			     -cookie => $recentSearchesCookie);
63
58
64
    # Showing search history
59
    # Showing search history
65
    } else {
60
    } else {
66
61
67
        my @recentSearches = ParseSearchHistoryCookie($cgi);
62
        my @recentSearches = ParseSearchHistorySession($cgi);
68
	    if (@recentSearches) {
63
	    if (@recentSearches) {
69
64
70
		# As the dates are stored as unix timestamps, let's do some formatting
65
		# As the dates are stored as unix timestamps, let's do some formatting
(-)a/opac/opac-search.pl (-11 / +4 lines)
Lines 42-48 for ( $searchengine ) { Link Here
42
}
42
}
43
43
44
use C4::Output;
44
use C4::Output;
45
use C4::Auth qw(:DEFAULT get_session ParseSearchHistoryCookie);
45
use C4::Auth qw(:DEFAULT get_session ParseSearchHistorySession SetSearchHistorySession);
46
use C4::Languages qw(getAllLanguages);
46
use C4::Languages qw(getAllLanguages);
47
use C4::Search;
47
use C4::Search;
48
use C4::Biblio;  # GetBiblioData
48
use C4::Biblio;  # GetBiblioData
Lines 616-622 for (my $i=0;$i<@servers;$i++) { Link Here
616
        # Opac search history
616
        # Opac search history
617
        my $newsearchcookie;
617
        my $newsearchcookie;
618
        if (C4::Context->preference('EnableOpacSearchHistory')) {
618
        if (C4::Context->preference('EnableOpacSearchHistory')) {
619
            my @recentSearches = ParseSearchHistoryCookie($cgi);
619
            my @recentSearches = ParseSearchHistorySession($cgi);
620
620
621
            # Adding the new search if needed
621
            # Adding the new search if needed
622
            my $path_info = $cgi->url(-path_info=>1);
622
            my $path_info = $cgi->url(-path_info=>1);
Lines 626-632 for (my $i=0;$i<@servers;$i++) { Link Here
626
            my $query_desc_history = join ", ", grep { defined $_ } $query_desc, $limit_desc;
626
            my $query_desc_history = join ", ", grep { defined $_ } $query_desc, $limit_desc;
627
627
628
            if (!$borrowernumber || $borrowernumber eq '') {
628
            if (!$borrowernumber || $borrowernumber eq '') {
629
                # To a cookie (the user is not logged in)
629
                # To the session (the user is not logged in)
630
                if (!$offset) {
630
                if (!$offset) {
631
                    push @recentSearches, {
631
                    push @recentSearches, {
632
                                "query_desc" => Encode::decode_utf8($query_desc_history) || "unknown",
632
                                "query_desc" => Encode::decode_utf8($query_desc_history) || "unknown",
Lines 639-651 for (my $i=0;$i<@servers;$i++) { Link Here
639
639
640
                shift @recentSearches if (@recentSearches > 15);
640
                shift @recentSearches if (@recentSearches > 15);
641
                # Pushing the cookie back
641
                # Pushing the cookie back
642
                $newsearchcookie = $cgi->cookie(
642
                SetSearchHistorySession($cgi, \@recentSearches);
643
                            -name => 'KohaOpacRecentSearches',
644
                            # We uri_escape the whole serialized structure so we're sure we won't have any encoding problems
645
                            -value => uri_escape( encode_json(\@recentSearches) ),
646
                            -expires => ''
647
                );
648
                $cookie = [$cookie, $newsearchcookie];
649
            }
643
            }
650
            else {
644
            else {
651
                # To the session (the user is logged in)
645
                # To the session (the user is logged in)
652
- 

Return to bug 10952