From ec2dffe46bd0b895701e64f82fb28dde3238ec91 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Fri, 6 Nov 2015 11:23:59 -0500
Subject: [PATCH] Implemented dynamic debug mode.

---
 config/vufind/config.ini                  |  3 +-
 config/vufind/permissions.ini             |  6 ++++
 module/VuFind/src/VuFind/Bootstrapper.php | 19 ++++++++++++
 module/VuFind/src/VuFind/Log/Logger.php   | 37 ++++++++++++++++-------
 4 files changed, 53 insertions(+), 12 deletions(-)

diff --git a/config/vufind/config.ini b/config/vufind/config.ini
index 9a28d271c49..d2472498d45 100644
--- a/config/vufind/config.ini
+++ b/config/vufind/config.ini
@@ -9,7 +9,8 @@
 ; up during ILS maintenance.
 available       = true
 ; Change to true to see messages about the behavior of the system as part of the
-; output -- only for use when troubleshooting problems:
+; output -- only for use when troubleshooting problems. See also the access.DebugMode
+; setting in permissions.ini to turn on debug using a GET parameter in the request.
 debug           = false
 ; This setting should be set to false after auto-configuration is complete
 autoConfigure = true
diff --git a/config/vufind/permissions.ini b/config/vufind/permissions.ini
index 1cf354c1aa2..d0950159d3e 100644
--- a/config/vufind/permissions.ini
+++ b/config/vufind/permissions.ini
@@ -61,6 +61,7 @@
 ; List of permissions that you may wish to configure:
 ;
 ; access.AdminModule - Controls access to the admin panel (if enabled in config.ini)
+; access.DebugMode - Allows ?debug=true GET parameter to turn on debug mode
 ; access.EITModule - Controls access to the EBSCO EIT module (if active)
 ; access.StaffViewTab - Controls access to the staff view tab in record mode
 ; access.SummonExtendedResults - Controls visibility of protected Summon results
@@ -76,6 +77,11 @@ role[] = guest
 role[] = loggedin
 permission = access.StaffViewTab
 
+; Example for dynamic debug mode
+;[default.DebugMode]
+;username[] = admin
+;permission = access.DebugMode
+
 ; Examples for Shibboleth
 ;
 ; Only users that have either common-lib-terms and entityid from idp1 or 
diff --git a/module/VuFind/src/VuFind/Bootstrapper.php b/module/VuFind/src/VuFind/Bootstrapper.php
index 261c1ef3596..f8722c436f1 100644
--- a/module/VuFind/src/VuFind/Bootstrapper.php
+++ b/module/VuFind/src/VuFind/Bootstrapper.php
@@ -146,6 +146,25 @@ class Bootstrapper
         $serviceManager->get('VuFind\AuthManager')->checkForExpiredCredentials();
     }
 
+    /**
+     * Initialize dynamic debug mode.
+     *
+     * @return void
+     */
+    protected function initDynamicDebug()
+    {
+        $app = $this->event->getApplication();
+        $sm = $app->getServiceManager();
+        $debugOverride = $sm->get('Request')->getQuery()->get('debug');
+        if ($debugOverride) {
+            $auth = $sm->get('ZfcRbac\Service\AuthorizationService');
+            if ($auth->isGranted('access.DebugMode')) {
+                $logger = $sm->get('VuFind\Logger');
+                $logger->addDebugWriter($debugOverride);
+            }
+        }
+    }
+
     /**
      * If the system is offline, set up a handler to override the routing output.
      *
diff --git a/module/VuFind/src/VuFind/Log/Logger.php b/module/VuFind/src/VuFind/Log/Logger.php
index e4fad47c0b8..a225463a8b8 100644
--- a/module/VuFind/src/VuFind/Log/Logger.php
+++ b/module/VuFind/src/VuFind/Log/Logger.php
@@ -61,18 +61,9 @@ class Logger extends BaseLogger implements ServiceLocatorAwareInterface
     {
         // DEBUGGER
         if (!$config->System->debug == false) {
-            $writer = new Writer\Stream('php://output');
-            $formatter = new \Zend\Log\Formatter\Simple(
-                '<pre>%timestamp% %priorityName%: %message%</pre>' . PHP_EOL
-            );
-            $writer->setFormatter($formatter);
-            $this->addWriters(
-                $writer,
-                'debug-'
-                . (is_int($config->System->debug) ? $config->System->debug : '5')
-            );
+            $this->addDebugWriter($config->System->debug);
         }
-        
+
         // Activate database logging, if applicable:
         if (isset($config->Logging->database)) {
             $parts = explode(':', $config->Logging->database);
@@ -135,6 +126,30 @@ class Logger extends BaseLogger implements ServiceLocatorAwareInterface
         }
     }
 
+    /**
+     * Add the standard debug stream writer.
+     *
+     * @param bool|int $debug Debug mode/level
+     *
+     * @return void
+     */
+    public function addDebugWriter($debug)
+    {
+        // Only add debug writer ONCE!
+        static $hasDebugWriter = false;
+        if ($hasDebugWriter) {
+            return;
+        }
+
+        $hasDebugWriter = true;
+        $writer = new Writer\Stream('php://output');
+        $formatter = new \Zend\Log\Formatter\Simple(
+            '<pre>%timestamp% %priorityName%: %message%</pre>' . PHP_EOL
+        );
+        $writer->setFormatter($formatter);
+        $this->addWriters($writer, 'debug-' . (is_int($debug) ? $debug : '5'));
+    }
+
     /**
      * Applies an array of filters to a writer
      *
-- 
GitLab