Bugzilla – Attachment 21482 Details for
Bug 10952
Store anonymous search history in session
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 10952: Store anonymous search history in session
Bug-10952-Store-anonymous-search-history-in-sessio.patch (text/plain), 11.03 KB, created by
Julian Maurice
on 2013-09-26 10:52:11 UTC
(
hide
)
Description:
Bug 10952: Store anonymous search history in session
Filename:
MIME Type:
Creator:
Julian Maurice
Created:
2013-09-26 10:52:11 UTC
Size:
11.03 KB
patch
obsolete
>From e69dca780957db3cbd293377f7136777f58c830f Mon Sep 17 00:00:00 2001 >From: Julian Maurice <julian.maurice@biblibre.com> >Date: Thu, 26 Sep 2013 11:22:26 +0200 >Subject: [PATCH] Bug 10952: Store anonymous search history in session > >Storing search history into cookie can cause problems, due to the size >limitation of 4KB. >The solution here is to store search history into the CGI::Session >object, so there is no size limitation (but anonymous search history >still remember up to 15 requests max.) > >Test plan: >- Go to OPAC in anonymous mode. >- Check that the "Search history" link is *not* shown in the top right > corner of the page >- Make some searches on /cgi-bin/koha/opac-search.pl >- The "Search history" link must appear. Click. >- Your search history must be displayed. >- Try to log in with invalid username/password >- Go back to search history, it's still there >- Now log in with valid username/password >- Your anonymous search history must be saved into your own search > history. >--- > C4/Auth.pm | 50 +++++++++++++------- > .../prog/en/modules/opac-search-history.tt | 2 +- > opac/opac-search-history.pl | 21 ++++---- > opac/opac-search.pl | 14 ++---- > 4 files changed, 46 insertions(+), 41 deletions(-) > >diff --git a/C4/Auth.pm b/C4/Auth.pm >index 1e17e59..dbdbb65 100644 >--- a/C4/Auth.pm >+++ b/C4/Auth.pm >@@ -47,7 +47,7 @@ BEGIN { > @ISA = qw(Exporter); > @EXPORT = qw(&checkauth &get_template_and_user &haspermission &get_user_subpermissions); > @EXPORT_OK = qw(&check_api_auth &get_session &check_cookie_auth &checkpw &get_all_subpermissions &get_user_subpermissions >- ParseSearchHistoryCookie >+ ParseSearchHistorySession SetSearchHistorySession > ); > %EXPORT_TAGS = ( EditPermissions => [qw(get_all_subpermissions get_user_subpermissions)] ); > $ldap = C4::Context->config('useldapserver') || 0; >@@ -253,7 +253,7 @@ sub get_template_and_user { > > # And if there's a cookie with searches performed when the user was not logged in, > # we add them to the logged-in search history >- my @recentSearches = ParseSearchHistoryCookie($in->{'query'}); >+ my @recentSearches = ParseSearchHistorySession($in->{'query'}); > if (@recentSearches) { > my $sth = $dbh->prepare($SEARCH_HISTORY_INSERT_SQL); > $sth->execute( $borrowernumber, >@@ -264,14 +264,6 @@ sub get_template_and_user { > $_->{'time'}, > ) foreach @recentSearches; > >- # And then, delete the cookie's content >- my $newsearchcookie = $in->{'query'}->cookie( >- -name => 'KohaOpacRecentSearches', >- -value => encode_json([]), >- -HttpOnly => 1, >- -expires => '' >- ); >- $cookie = [$cookie, $newsearchcookie]; > } > } > } >@@ -288,7 +280,7 @@ sub get_template_and_user { > # Anonymous opac search history > # If opac search history is enabled and at least one search has already been performed > if (C4::Context->preference('EnableOpacSearchHistory')) { >- my @recentSearches = ParseSearchHistoryCookie($in->{'query'}); >+ my @recentSearches = ParseSearchHistorySession($in->{'query'}); > if (@recentSearches) { > $template->param(ShowOpacRecentSearchLink => 1); > } >@@ -619,6 +611,8 @@ sub checkauth { > my ( $userid, $cookie, $sessionID, $flags, $barshelves, $pubshelves ); > my $logout = $query->param('logout.x'); > >+ my $anon_search_history; >+ > # This parameter is the name of the CAS server we want to authenticate against, > # when using authentication against multiple CAS servers, as configured in Auth_cas_servers.yaml > my $casparam = $query->param('cas'); >@@ -664,7 +658,8 @@ sub checkauth { > #if a user enters an id ne to the id in the current session, we need to log them in... > #first we need to clear the anonymous session... > $debug and warn "query id = " . $query->param('userid') . " but session id = " . $session->param('id'); >- $session->flush; >+ $anon_search_history = $session->param('search_history'); >+ $session->flush; > $session->delete(); > C4::Context->_unset_userenv($sessionID); > $sessionID = undef; >@@ -724,6 +719,14 @@ sub checkauth { > > #we initiate a session prior to checking for a username to allow for anonymous sessions... > my $session = get_session("") or die "Auth ERROR: Cannot get_session()"; >+ >+ # Save anonymous search history in new session so it can be retrieved >+ # by get_template_and_user to store it in user's search history after >+ # a successful login. >+ if ($anon_search_history) { >+ $session->param('search_history', $anon_search_history); >+ } >+ > my $sessionID = $session->id; > C4::Context->_new_userenv($sessionID); > $cookie = $query->cookie( >@@ -930,6 +933,8 @@ sub checkauth { > $info{'invalid_username_or_password'} = 1; > C4::Context->_unset_userenv($sessionID); > } >+ $session->param('lasttime',time()); >+ $session->param('ip',$session->remote_addr()); > } > } # END if ( $userid = $query->param('userid') ) > elsif ($type eq "opac") { >@@ -1715,16 +1720,27 @@ sub getborrowernumber { > return 0; > } > >-sub ParseSearchHistoryCookie { >- my $input = shift; >- my $search_cookie = $input->cookie('KohaOpacRecentSearches'); >- return () unless $search_cookie; >- my $obj = eval { decode_json(uri_unescape($search_cookie)) }; >+sub ParseSearchHistorySession { >+ my $cgi = shift; >+ my $sessionID = $cgi->cookie('CGISESSID'); >+ return () unless $sessionID; >+ my $session = get_session($sessionID); >+ return () unless $session and $session->param('search_history'); >+ my $obj = eval { decode_json(uri_unescape($session->param('search_history'))) }; > return () unless defined $obj; > return () unless ref $obj eq 'ARRAY'; > return @{ $obj }; > } > >+sub SetSearchHistorySession { >+ my ($cgi, $search_history) = @_; >+ my $sessionID = $cgi->cookie('CGISESSID'); >+ return () unless $sessionID; >+ my $session = get_session($sessionID); >+ return () unless $session; >+ $session->param('search_history', uri_escape(encode_json($search_history))); >+} >+ > END { } # module clean-up code here (global destructor) > 1; > __END__ >diff --git a/koha-tmpl/opac-tmpl/prog/en/modules/opac-search-history.tt b/koha-tmpl/opac-tmpl/prog/en/modules/opac-search-history.tt >index 9042543..4d0fa82 100644 >--- a/koha-tmpl/opac-tmpl/prog/en/modules/opac-search-history.tt >+++ b/koha-tmpl/opac-tmpl/prog/en/modules/opac-search-history.tt >@@ -51,7 +51,7 @@ > <tbody> > [% FOREACH recentSearche IN recentSearches %] > <tr> >- <td><span title="[% recentSearche.time %]">[% recentSearche.time |$KohaDates with_hours => 1 %]</span></td> >+ <td><span title="[% recentSearche.time %]">[% recentSearche.time %]</span></td> > <td><a href="/cgi-bin/koha/opac-search.pl?[% recentSearche.query_cgi |html %]">[% recentSearche.query_desc |html %]</a></td> > <td>[% recentSearche.total %]</td> > </tr> >diff --git a/opac/opac-search-history.pl b/opac/opac-search-history.pl >index 52477c5..dfefab7 100755 >--- a/opac/opac-search-history.pl >+++ b/opac/opac-search-history.pl >@@ -20,7 +20,7 @@ > use strict; > use warnings; > >-use C4::Auth qw(:DEFAULT get_session ParseSearchHistoryCookie); >+use C4::Auth qw(:DEFAULT get_session ParseSearchHistorySession SetSearchHistorySession); > use CGI; > use JSON qw/decode_json encode_json/; > use C4::Context; >@@ -50,22 +50,17 @@ if (!$loggedinuser) { > > # Deleting search history > if ($cgi->param('action') && $cgi->param('action') eq 'delete') { >- # Deleting cookie's content >- my $recentSearchesCookie = $cgi->cookie( >- -name => 'KohaOpacRecentSearches', >- -value => encode_json([]), >- -expires => '' >- ); >- >- # Redirecting to this same url with the cookie in the headers so it's deleted immediately >- my $uri = $cgi->url(); >- print $cgi->redirect(-uri => $uri, >- -cookie => $recentSearchesCookie); >+ # Deleting cookie's content >+ SetSearchHistorySession($cgi, []); >+ >+ # Redirecting to this same url with the cookie in the headers so it's deleted immediately >+ my $uri = $cgi->url(); >+ print $cgi->redirect(-uri => $uri); > > # Showing search history > } else { > >- my @recentSearches = ParseSearchHistoryCookie($cgi); >+ my @recentSearches = ParseSearchHistorySession($cgi); > if (@recentSearches) { > > # As the dates are stored as unix timestamps, let's do some formatting >diff --git a/opac/opac-search.pl b/opac/opac-search.pl >index 2854da6..ffa4c47 100755 >--- a/opac/opac-search.pl >+++ b/opac/opac-search.pl >@@ -42,7 +42,7 @@ for ( $searchengine ) { > } > > use C4::Output; >-use C4::Auth qw(:DEFAULT get_session ParseSearchHistoryCookie); >+use C4::Auth qw(:DEFAULT get_session ParseSearchHistorySession SetSearchHistorySession); > use C4::Languages qw(getAllLanguages); > use C4::Search; > use C4::Biblio; # GetBiblioData >@@ -616,7 +616,7 @@ for (my $i=0;$i<@servers;$i++) { > # Opac search history > my $newsearchcookie; > if (C4::Context->preference('EnableOpacSearchHistory')) { >- my @recentSearches = ParseSearchHistoryCookie($cgi); >+ my @recentSearches = ParseSearchHistorySession($cgi); > > # Adding the new search if needed > my $path_info = $cgi->url(-path_info=>1); >@@ -626,7 +626,7 @@ for (my $i=0;$i<@servers;$i++) { > my $query_desc_history = "$query_desc, $limit_desc"; > > if (!$borrowernumber || $borrowernumber eq '') { >- # To a cookie (the user is not logged in) >+ # To the session (the user is not logged in) > if (!$offset) { > push @recentSearches, { > "query_desc" => Encode::decode_utf8($query_desc_history) || "unknown", >@@ -639,13 +639,7 @@ for (my $i=0;$i<@servers;$i++) { > > shift @recentSearches if (@recentSearches > 15); > # Pushing the cookie back >- $newsearchcookie = $cgi->cookie( >- -name => 'KohaOpacRecentSearches', >- # We uri_escape the whole serialized structure so we're sure we won't have any encoding problems >- -value => uri_escape( encode_json(\@recentSearches) ), >- -expires => '' >- ); >- $cookie = [$cookie, $newsearchcookie]; >+ SetSearchHistorySession($cgi, \@recentSearches); > } > else { > # To the session (the user is logged in) >-- >1.7.10.4
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 10952
:
21482
|
21550
|
22133
|
23618
|
23619
|
23755
|
23756
|
23783
|
23927
|
24137
|
24138
|
24139
|
24156
|
24157
|
24158