From 9aa67a4ddeed921fd3f07899d547edf6fff7d417 Mon Sep 17 00:00:00 2001
From: Pasi Kallinen <pasi.kallinen@joensuu.fi>
Date: Thu, 21 Jun 2018 11:15:23 +0300
Subject: [PATCH] Bug 20971: Prevent Storable::thaw to fail on LastCreatedItem

Sometimes additem.pl will complain about "Storable::thaw failed to
thaw LastCreatedItem-cookie.", see bug 14844. Now, actually fix the bug.

The bug is caused by trying to URI (un)escape MARC::Record, binary data.
We'll use a base64 url-safe version instead.

Test plan:
1: Set PrefillItem to 'The new item is prefilled...'
2: Set a SubfieldsToUseWhenPrefill, 'c' for example
3: Add a new item for biblio A with 'c' set.
4: Double check 'c' value is set for next new item A.
5: search and add a new item for biblio B
6: 'c' is not set
Apply patch
7: logout, login
8: repeat steps 3, 4, and 5 .
9: now 'c' is set.

Signed-off-by: Pasi Kallinen <pasi.kallinen@joensuu.fi>
Signed-off-by: James O'Keeffe <jamespfk@gmail.com>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>

JD amended patch: fix commit title and add test plan
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
---
 cataloguing/additem.pl | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/cataloguing/additem.pl b/cataloguing/additem.pl
index b8face6d22..b42d3d3765 100755
--- a/cataloguing/additem.pl
+++ b/cataloguing/additem.pl
@@ -44,6 +44,7 @@ use C4::Members;
 
 use MARC::File::XML;
 use URI::Escape;
+use MIME::Base64 qw(decode_base64url encode_base64url);
 
 our $dbh = C4::Context->dbh;
 
@@ -467,7 +468,7 @@ my $cookieitemrecord;
 if ($prefillitem) {
     my $lastitemcookie = $input->cookie('LastCreatedItem');
     if ($lastitemcookie) {
-        $lastitemcookie = uri_unescape($lastitemcookie);
+        $lastitemcookie = decode_base64url($lastitemcookie);
         eval {
             if ( thaw($lastitemcookie) ) {
                 $cookieitemrecord = thaw($lastitemcookie);
@@ -476,7 +477,7 @@ if ($prefillitem) {
         };
         if ($@) {
             $lastitemcookie = 'undef' unless $lastitemcookie;
-            warn "Storable::thaw failed to thaw LastCreatedItem-cookie. Cookie value '$lastitemcookie'. Caught error follows: '$@'";
+            warn "Storable::thaw failed to thaw LastCreatedItem-cookie. Cookie value '".encode_base64url($lastitemcookie)."'. Caught error follows: '$@'";
         }
     }
 }
@@ -532,8 +533,8 @@ if ($op eq "additem") {
             if ($prefillitem && defined $record) {
                 my $itemcookie = $input->cookie(
                     -name => 'LastCreatedItem',
-                    # We uri_escape the whole freezed structure so we're sure we won't have any encoding problems
-                    -value   => uri_escape_utf8( freeze( $record ) ),
+                    # We encode_base64url the whole freezed structure so we're sure we won't have any encoding problems
+                    -value   => encode_base64url( freeze( $record ) ),
                     -HttpOnly => 1,
                     -expires => ''
                 );
-- 
2.20.1