From db646ca6fd3b7db3b0cebdd326bba5bd35be5bc8 Mon Sep 17 00:00:00 2001
From: Ulf Seltmann <seltmann@ub.uni-leipzig.de>
Date: Tue, 13 Dec 2016 12:15:14 +0100
Subject: [PATCH] refs #9254 * added functionality of pullrequest #867 to finc
 module

---
 module/finc/config/module.config.php          |  4 +-
 module/finc/src/finc/Cookie/CookieManager.php | 77 +++++++++++++++++++
 module/finc/src/finc/Service/Factory.php      | 35 ++++++++-
 .../finc/src/finc/Session/ManagerFactory.php  | 71 +++++++++++++++++
 4 files changed, 185 insertions(+), 2 deletions(-)
 create mode 100644 module/finc/src/finc/Cookie/CookieManager.php
 create mode 100644 module/finc/src/finc/Session/ManagerFactory.php

diff --git a/module/finc/config/module.config.php b/module/finc/config/module.config.php
index 0240e527d38..2214c7fe670 100644
--- a/module/finc/config/module.config.php
+++ b/module/finc/config/module.config.php
@@ -9,7 +9,9 @@ $config = [
             'VuFind\BranchesReader' => 'finc\Service\Factory::getBranchesReader',
             'VuFind\ILSConnection' => 'finc\Service\Factory::getILSConnection',
             'VuFind\ILSHoldLogic' => 'finc\Service\Factory::getILSHoldLogic',
-            'finc\Rewrite' => 'finc\Rewrite\Factory'
+            'finc\Rewrite' => 'finc\Rewrite\Factory',
+            'VuFind\SessionManager' => 'finc\Session\ManagerFactory',
+            'VuFind\CookieManager' => 'finc\Service\Factory::getCookieManager'
         ]
     ],
     'controllers' => [
diff --git a/module/finc/src/finc/Cookie/CookieManager.php b/module/finc/src/finc/Cookie/CookieManager.php
new file mode 100644
index 00000000000..7584ed0d6eb
--- /dev/null
+++ b/module/finc/src/finc/Cookie/CookieManager.php
@@ -0,0 +1,77 @@
+<?php
+/**
+ * Cookie Manager
+ *
+ * PHP version 5
+ *
+ * Copyright (C) Villanova University 2015.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * @category VuFind
+ * @package  Cookie
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org/wiki/development Wiki
+ */
+namespace finc\Cookie;
+
+/**
+ * Cookie Manager
+ *
+ * @category VuFind
+ * @package  Cookie
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org/wiki/development Wiki
+ */
+class CookieManager extends \VuFind\Cookie\CookieManager
+{
+    /**
+     * The name of the session cookie
+     *
+     * @var string
+     */
+    protected $sessionName;
+
+    /**
+     * Constructor
+     *
+     * @param array  $cookies     Cookie array to manipulate (e.g. $_COOKIE)
+     * @param string $path        Cookie base path (default = /)
+     * @param string $domain      Cookie domain
+     * @param bool   $secure      Are cookies secure only? (default = false)
+     * @param string $sessionName Session cookie name (if null defaults to PHP
+     * settings)
+     */
+    public function __construct($cookies, $path = '/', $domain = null,
+                                $secure = false, $sessionName = null
+    ) {
+        $this->cookies = $cookies;
+        $this->path = $path;
+        $this->domain = $domain;
+        $this->secure = $secure;
+        $this->sessionName = $sessionName;
+    }
+
+    /**
+     * Get the name of the cookie
+     *
+     * @return mixed
+     */
+    public function getSessionName()
+    {
+        return $this->sessionName;
+    }
+}
diff --git a/module/finc/src/finc/Service/Factory.php b/module/finc/src/finc/Service/Factory.php
index 2841fcde82c..4efbc4ba425 100644
--- a/module/finc/src/finc/Service/Factory.php
+++ b/module/finc/src/finc/Service/Factory.php
@@ -69,7 +69,7 @@ class Factory
             $sm->get('VuFind\CacheManager')
         );
     }
-    
+
     /**
      * Construct the ILS connection.
      *
@@ -101,4 +101,37 @@ class Factory
             $sm->get('VuFind\HMAC'), $sm->get('VuFind\Config')->get('config')
         );
     }
+
+    /**
+     * Construct the cookie manager.
+     *
+     * @param ServiceManager $sm Service manager.
+     *
+     * @return \VuFind\Cookie\CookieManager
+     */
+    public static function getCookieManager(ServiceManager $sm)
+    {
+        $config = $sm->get('VuFind\Config')->get('config');
+        $path = '/';
+        if (isset($config->Cookies->limit_by_path)
+            && $config->Cookies->limit_by_path
+        ) {
+            $path = $sm->get('Request')->getBasePath();
+            if (empty($path)) {
+                $path = '/';
+            }
+        }
+        $secure = isset($config->Cookies->only_secure)
+            ? $config->Cookies->only_secure
+            : false;
+        $domain = isset($config->Cookies->domain)
+            ? $config->Cookies->domain
+            : null;
+        $session_name = isset($config->Cookies->session_name)
+            ? $config->Cookies->session_name
+            : null;
+        return new \finc\Cookie\CookieManager(
+            $_COOKIE, $path, $domain, $secure, $session_name
+        );
+    }
 }
diff --git a/module/finc/src/finc/Session/ManagerFactory.php b/module/finc/src/finc/Session/ManagerFactory.php
new file mode 100644
index 00000000000..6dc0836a1e9
--- /dev/null
+++ b/module/finc/src/finc/Session/ManagerFactory.php
@@ -0,0 +1,71 @@
+<?php
+/**
+ * Factory for instantiating Session Manager
+ *
+ * PHP version 5
+ *
+ * Copyright (C) Villanova University 2016.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * @category VuFind
+ * @package  Session_Handlers
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org/wiki/development Wiki
+ */
+namespace finc\Session;
+use Zend\ServiceManager\ServiceLocatorInterface;
+
+/**
+ * Factory for instantiating Session Manager
+ *
+ * @category VuFind
+ * @package  Session_Handlers
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org/wiki/development Wiki
+ *
+ * @codeCoverageIgnore
+ */
+class ManagerFactory extends \VuFind\Session\ManagerFactory
+{
+    /**
+     * Build the options array.
+     *
+     * @param ServiceLocatorInterface $sm Service manager
+     *
+     * @return array
+     */
+    protected function getOptions(ServiceLocatorInterface $sm)
+    {
+        $cookieManager = $sm->get('VuFind\CookieManager');
+        $options = [
+            'cookie_path' => $cookieManager->getPath(),
+            'cookie_secure' => $cookieManager->isSecure()
+        ];
+
+        $domain = $cookieManager->getDomain();
+        if (!empty($domain)) {
+            $options['cookie_domain'] = $domain;
+        }
+
+        $name = $cookieManager->getSessionName();
+        if (!empty($name)) {
+            $options['name'] = $name;
+        }
+
+        return $options;
+    }
+}
-- 
GitLab