From 7b29d33ced7331ec8dcbb30d0d42637fe572d340 Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Wed, 18 Dec 2024 11:15:36 -0300
Subject: [PATCH] Bug 28478: Make opac-*detail.pl scripts use
 Koha::Biblio->opac_suppressed()

This patch makes the following scripts use the new method for checking
suppression:

* opac/opac-ISBDdetail.pl
* opac/opac-MARCdetail.pl
* opac/opac-detail.pl

In the `opac-detail.pl` case, it is a simple change by removing MARC
data traversal in favor of the new method. The code checking
suppression gets moved up so we don't process or make any other
calculations if the record is suppressed.

The other two scripts where completely missing the check and thus
leaking suppressed records.

To test:
1. Pick two records, one marked as suppressed, and the other not
   suppressed.
2. Try acessing them in the OPAC detail page.
=> SUCCESS: Suppressed records are suppressed, and not suppressed ones
are not.
3. Try the same records on the ISBD and MARC view
=> FAIL: They are not suppressed!
4. Apply this patch
5. Repeat 2
=> SUCCESS: Suppression is still respected
6. Repeat 3
=> SUCCESS: Suppression is respected on the ISBD and MARC views
7. Sign off :-D
---
 opac/opac-ISBDdetail.pl | 27 +++++++++++++++++++
 opac/opac-MARCdetail.pl | 27 +++++++++++++++++++
 opac/opac-detail.pl     | 58 +++++++++++++++++++----------------------
 3 files changed, 81 insertions(+), 31 deletions(-)

diff --git a/opac/opac-ISBDdetail.pl b/opac/opac-ISBDdetail.pl
index 7ebef0432fa..3bc55cb0f77 100755
--- a/opac/opac-ISBDdetail.pl
+++ b/opac/opac-ISBDdetail.pl
@@ -76,6 +76,33 @@ if( !$biblio ) {
     exit;
 }
 
+# If record should be suppressed, handle it early
+if ( C4::Context->preference('OpacSuppression') ) {
+
+    # redirect to opac-blocked info page or 404?
+    my $redirect_url;
+    if ( C4::Context->preference("OpacSuppressionRedirect") ) {
+        $redirect_url = "/cgi-bin/koha/opac-blocked.pl";
+    } else {
+        $redirect_url = "/cgi-bin/koha/errors/404.pl";
+    }
+    if ( $biblio->opac_suppressed() ) {
+
+        # if OPAC suppression by IP address
+        if ( C4::Context->preference('OpacSuppressionByIPRange') ) {
+            my $IPAddress = $ENV{'REMOTE_ADDR'};
+            my $IPRange   = C4::Context->preference('OpacSuppressionByIPRange');
+            if ( $IPAddress !~ /^$IPRange/ ) {
+                print $query->redirect($redirect_url);
+                exit;
+            }
+        } else {
+            print $query->redirect($redirect_url);
+            exit;
+        }
+    }
+}
+
 #open template
 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
     {
diff --git a/opac/opac-MARCdetail.pl b/opac/opac-MARCdetail.pl
index 63957e7d0b7..8b3cb6ec085 100755
--- a/opac/opac-MARCdetail.pl
+++ b/opac/opac-MARCdetail.pl
@@ -97,6 +97,33 @@ if ( !$biblio ) {
     exit;
 }
 
+# If record should be suppressed, handle it early
+if ( C4::Context->preference('OpacSuppression') ) {
+
+    # redirect to opac-blocked info page or 404?
+    my $redirect_url;
+    if ( C4::Context->preference("OpacSuppressionRedirect") ) {
+        $redirect_url = "/cgi-bin/koha/opac-blocked.pl";
+    } else {
+        $redirect_url = "/cgi-bin/koha/errors/404.pl";
+    }
+    if ( $biblio->opac_suppressed() ) {
+
+        # if OPAC suppression by IP address
+        if ( C4::Context->preference('OpacSuppressionByIPRange') ) {
+            my $IPAddress = $ENV{'REMOTE_ADDR'};
+            my $IPRange   = C4::Context->preference('OpacSuppressionByIPRange');
+            if ( $IPAddress !~ /^$IPRange/ ) {
+                print $query->redirect($redirect_url);
+                exit;
+            }
+        } else {
+            print $query->redirect($redirect_url);
+            exit;
+        }
+    }
+}
+
 unless ( $patron and $patron->category->override_hidden_items ) {
     # only skip this check if there's a logged in user
     # and its category overrides OpacHiddenItems
diff --git a/opac/opac-detail.pl b/opac/opac-detail.pl
index 348e509f6af..ba5238b1c10 100755
--- a/opac/opac-detail.pl
+++ b/opac/opac-detail.pl
@@ -112,6 +112,33 @@ unless ( $biblio && $record ) {
     exit;
 }
 
+# If record should be suppressed, handle it early
+if ( C4::Context->preference('OpacSuppression') ) {
+
+    # redirect to opac-blocked info page or 404?
+    my $redirect_url;
+    if ( C4::Context->preference("OpacSuppressionRedirect") ) {
+        $redirect_url = "/cgi-bin/koha/opac-blocked.pl";
+    } else {
+        $redirect_url = "/cgi-bin/koha/errors/404.pl";
+    }
+    if ( $biblio->opac_suppressed() ) {
+
+        # if OPAC suppression by IP address
+        if ( C4::Context->preference('OpacSuppressionByIPRange') ) {
+            my $IPAddress = $ENV{'REMOTE_ADDR'};
+            my $IPRange   = C4::Context->preference('OpacSuppressionByIPRange');
+            if ( $IPAddress !~ /^$IPRange/ ) {
+                print $query->redirect($redirect_url);
+                exit;
+            }
+        } else {
+            print $query->redirect($redirect_url);
+            exit;
+        }
+    }
+}
+
 my $metadata_extractor = $biblio->metadata_extractor;
 
 my $items = $biblio->items->search_ordered;
@@ -142,37 +169,6 @@ my $record_processor = Koha::RecordProcessor->new({
 });
 $record_processor->process($record);
 
-# redirect if opacsuppression is enabled and biblio is suppressed
-if (C4::Context->preference('OpacSuppression')) {
-    # FIXME hardcoded; the suppression flag ought to be materialized
-    # as a column on biblio or the like
-    my $opacsuppressionfield = '942';
-    my $opacsuppressionfieldvalue = $record->field($opacsuppressionfield);
-    # redirect to opac-blocked info page or 404?
-    my $opacsuppressionredirect;
-    if ( C4::Context->preference("OpacSuppressionRedirect") ) {
-        $opacsuppressionredirect = "/cgi-bin/koha/opac-blocked.pl";
-    } else {
-        $opacsuppressionredirect = "/cgi-bin/koha/errors/404.pl";
-    }
-    if ( $opacsuppressionfieldvalue &&
-         $opacsuppressionfieldvalue->subfield("n") &&
-         $opacsuppressionfieldvalue->subfield("n") == 1) {
-        # if OPAC suppression by IP address
-        if (C4::Context->preference('OpacSuppressionByIPRange')) {
-            my $IPAddress = $ENV{'REMOTE_ADDR'};
-            my $IPRange = C4::Context->preference('OpacSuppressionByIPRange');
-            if ($IPAddress !~ /^$IPRange/)  {
-                print $query->redirect($opacsuppressionredirect);
-                exit;
-            }
-        } else {
-            print $query->redirect($opacsuppressionredirect);
-            exit;
-        }
-    }
-}
-
 $template->param(
     biblio => $biblio
 );
-- 
2.47.1