From 1dc17c4e43131373f8c704df8ee86d389eb75fe6 Mon Sep 17 00:00:00 2001
From: Ere Maijala <ere.maijala@helsinki.fi>
Date: Fri, 30 Nov 2018 14:41:35 +0200
Subject: [PATCH] Don't set cookies or cookie options when running in CLI.
 (#1279)

Otherwise Zend\Session\Config\SessionConfig::setStorageOption() throws an exception when it tries to call ini_set with session-specific settings. This would have happened at least when creating a \Zend\Session\Container.
---
 module/VuFind/src/VuFind/Cookie/CookieManager.php   | 4 ++--
 module/VuFind/src/VuFind/Session/ManagerFactory.php | 5 +++--
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/module/VuFind/src/VuFind/Cookie/CookieManager.php b/module/VuFind/src/VuFind/Cookie/CookieManager.php
index 89760e1cba9..fb28e15413b 100644
--- a/module/VuFind/src/VuFind/Cookie/CookieManager.php
+++ b/module/VuFind/src/VuFind/Cookie/CookieManager.php
@@ -163,8 +163,8 @@ class CookieManager
      */
     public function proxySetCookie()
     {
-        // Special case: in test suite -- don't actually write headers!
-        return defined('VUFIND_PHPUNIT_RUNNING')
+        // Special case: in test suite or CLI -- don't actually write headers!
+        return defined('VUFIND_PHPUNIT_RUNNING') || 'cli' === PHP_SAPI
             ? true : call_user_func_array('setcookie', func_get_args());
     }
 
diff --git a/module/VuFind/src/VuFind/Session/ManagerFactory.php b/module/VuFind/src/VuFind/Session/ManagerFactory.php
index f61fc206937..10757084262 100644
--- a/module/VuFind/src/VuFind/Session/ManagerFactory.php
+++ b/module/VuFind/src/VuFind/Session/ManagerFactory.php
@@ -54,11 +54,12 @@ class ManagerFactory implements FactoryInterface
     protected function getOptions(ContainerInterface $container)
     {
         $cookieManager = $container->get('VuFind\Cookie\CookieManager');
-        $options = [
+        // Set options only if we are not running from CLI
+        $options = 'cli' !== PHP_SAPI ? [
             'cookie_httponly' => $cookieManager->isHttpOnly(),
             'cookie_path' => $cookieManager->getPath(),
             'cookie_secure' => $cookieManager->isSecure()
-        ];
+        ] : [];
 
         $domain = $cookieManager->getDomain();
         if (!empty($domain)) {
-- 
GitLab