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 642-647 sub checkauth { Link Here
642
    my ( $userid, $cookie, $sessionID, $flags, $barshelves, $pubshelves );
634
    my ( $userid, $cookie, $sessionID, $flags, $barshelves, $pubshelves );
643
    my $logout = $query->param('logout.x');
635
    my $logout = $query->param('logout.x');
644
636
637
    my $anon_search_history;
638
645
    # This parameter is the name of the CAS server we want to authenticate against,
639
    # This parameter is the name of the CAS server we want to authenticate against,
646
    # when using authentication against multiple CAS servers, as configured in Auth_cas_servers.yaml
640
    # when using authentication against multiple CAS servers, as configured in Auth_cas_servers.yaml
647
    my $casparam = $query->param('cas');
641
    my $casparam = $query->param('cas');
Lines 687-693 sub checkauth { Link Here
687
            #if a user enters an id ne to the id in the current session, we need to log them in...
681
            #if a user enters an id ne to the id in the current session, we need to log them in...
688
            #first we need to clear the anonymous session...
682
            #first we need to clear the anonymous session...
689
            $debug and warn "query id = " . $query->param('userid') . " but session id = " . $session->param('id');
683
            $debug and warn "query id = " . $query->param('userid') . " but session id = " . $session->param('id');
690
            $session->flush;      
684
            $anon_search_history = $session->param('search_history');
685
            $session->flush;
691
            $session->delete();
686
            $session->delete();
692
            C4::Context->_unset_userenv($sessionID);
687
            C4::Context->_unset_userenv($sessionID);
693
            $sessionID = undef;
688
            $sessionID = undef;
Lines 747-752 sub checkauth { Link Here
747
742
748
        #we initiate a session prior to checking for a username to allow for anonymous sessions...
743
        #we initiate a session prior to checking for a username to allow for anonymous sessions...
749
        my $session = get_session("") or die "Auth ERROR: Cannot get_session()";
744
        my $session = get_session("") or die "Auth ERROR: Cannot get_session()";
745
746
        # Save anonymous search history in new session so it can be retrieved
747
        # by get_template_and_user to store it in user's search history after
748
        # a successful login.
749
        if ($anon_search_history) {
750
            $session->param('search_history', $anon_search_history);
751
        }
752
750
        my $sessionID = $session->id;
753
        my $sessionID = $session->id;
751
        C4::Context->_new_userenv($sessionID);
754
        C4::Context->_new_userenv($sessionID);
752
        $cookie = $query->cookie(
755
        $cookie = $query->cookie(
Lines 953-958 sub checkauth { Link Here
953
                    $info{'invalid_username_or_password'} = 1;
956
                    $info{'invalid_username_or_password'} = 1;
954
                    C4::Context->_unset_userenv($sessionID);
957
                    C4::Context->_unset_userenv($sessionID);
955
                }
958
                }
959
                $session->param('lasttime',time());
960
                $session->param('ip',$session->remote_addr());
956
            }
961
            }
957
        }    # END if ( $userid    = $query->param('userid') )
962
        }    # END if ( $userid    = $query->param('userid') )
958
        elsif ($type eq "opac") {
963
        elsif ($type eq "opac") {
Lines 1760-1775 sub getborrowernumber { Link Here
1760
    return 0;
1765
    return 0;
1761
}
1766
}
1762
1767
1763
sub ParseSearchHistoryCookie {
1768
sub ParseSearchHistorySession {
1764
    my $input = shift;
1769
    my $cgi = shift;
1765
    my $search_cookie = $input->cookie('KohaOpacRecentSearches');
1770
    my $sessionID = $cgi->cookie('CGISESSID');
1766
    return () unless $search_cookie;
1771
    return () unless $sessionID;
1767
    my $obj = eval { decode_json(uri_unescape($search_cookie)) };
1772
    my $session = get_session($sessionID);
1773
    return () unless $session and $session->param('search_history');
1774
    my $obj = eval { decode_json(uri_unescape($session->param('search_history'))) };
1768
    return () unless defined $obj;
1775
    return () unless defined $obj;
1769
    return () unless ref $obj eq 'ARRAY';
1776
    return () unless ref $obj eq 'ARRAY';
1770
    return @{ $obj };
1777
    return @{ $obj };
1771
}
1778
}
1772
1779
1780
sub SetSearchHistorySession {
1781
    my ($cgi, $search_history) = @_;
1782
    my $sessionID = $cgi->cookie('CGISESSID');
1783
    return () unless $sessionID;
1784
    my $session = get_session($sessionID);
1785
    return () unless $session;
1786
    $session->param('search_history', uri_escape(encode_json($search_history)));
1787
}
1788
1773
END { }    # module clean-up code here (global destructor)
1789
END { }    # module clean-up code here (global destructor)
1774
1;
1790
1;
1775
__END__
1791
__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 = "$query_desc, $limit_desc";
626
            my $query_desc_history = "$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