Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
76.92% covered (warning)
76.92%
10 / 13
62.50% covered (warning)
62.50%
5 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbstractBase
76.92% covered (warning)
76.92%
10 / 13
62.50% covered (warning)
62.50%
5 / 8
11.23
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 enableWrites
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 disableWrites
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 open
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 close
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 destroy
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 gc
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 write
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 saveSession
n/a
0 / 0
n/a
0 / 0
0
1<?php
2
3/**
4 * Base class for session handling
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010,
9 *               Leipzig University Library <info@ub.uni-leipzig.de> 2018.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2,
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23 *
24 * @category VuFind
25 * @package  Session_Handlers
26 * @author   Demian Katz <demian.katz@villanova.edu>
27 * @author   Sebastian Kehr <kehr@ub.uni-leipzig.de>
28 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
29 * @link     https://vufind.org/wiki/development:plugins:session_handlers Wiki
30 */
31
32namespace VuFind\Session;
33
34use Laminas\Config\Config;
35use VuFind\Db\Service\DbServiceAwareTrait;
36use VuFind\Db\Service\ExternalSessionServiceInterface;
37use VuFind\Db\Service\SearchServiceInterface;
38
39/**
40 * Base class for session handling
41 *
42 * @category VuFind
43 * @package  Session_Handlers
44 * @author   Demian Katz <demian.katz@villanova.edu>
45 * @author   Sebastian Kehr <kehr@ub.uni-leipzig.de>
46 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
47 * @link     https://vufind.org/wiki/development:plugins:session_handlers Wiki
48 */
49abstract class AbstractBase implements HandlerInterface
50{
51    use \VuFind\Db\Table\DbTableAwareTrait {
52        getDbTable as getTable;
53    }
54    // Note that we intentionally omit the DbServiceAwareInterface above; the service
55    // manager is injected by AbstractBaseFactory explicitly for compatibility with
56    // the secure delegator factory, so we don't need to auto-inject it.
57    use DbServiceAwareTrait;
58
59    /**
60     * Session lifetime in seconds
61     *
62     * @var int
63     */
64    protected $lifetime = 3600;
65
66    /**
67     * Whether writes are disabled, i.e. any changes to the session are not written
68     * to the storage
69     *
70     * @var bool
71     */
72    protected $writesDisabled = false;
73
74    /**
75     * Constructor
76     *
77     * @param Config $config Session configuration ([Session] section of
78     * config.ini)
79     */
80    public function __construct(Config $config = null)
81    {
82        if (isset($config->lifetime)) {
83            $this->lifetime = $config->lifetime;
84        }
85    }
86
87    /**
88     * Enable session writing (default)
89     *
90     * @return void
91     */
92    public function enableWrites()
93    {
94        $this->writesDisabled = false;
95    }
96
97    /**
98     * Disable session writing, i.e. make it read-only
99     *
100     * @return void
101     */
102    public function disableWrites()
103    {
104        $this->writesDisabled = true;
105    }
106
107    /**
108     * Open function, this works like a constructor in classes and is executed
109     * when the session is being opened.
110     *
111     * @param string $sess_path Session save path
112     * @param string $sess_name Session name
113     *
114     * @return bool
115     *
116     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
117     */
118    public function open($sess_path, $sess_name): bool
119    {
120        return true;
121    }
122
123    /**
124     * Close function, this works like a destructor in classes and is executed
125     * when the session operation is done.
126     *
127     * @return bool
128     */
129    public function close(): bool
130    {
131        return true;
132    }
133
134    /**
135     * The destroy handler, this is executed when a session is destroyed with
136     * session_destroy() and takes the session id as its only parameter.
137     *
138     * IMPORTANT:  The functionality defined in this method is global to all session
139     *             mechanisms. If you override this method, be sure to still call
140     *             parent::destroy() in addition to any new behavior.
141     *
142     * @param string $sessId The session ID to destroy
143     *
144     * @return bool
145     */
146    public function destroy($sessId): bool
147    {
148        $this->getDbService(SearchServiceInterface::class)->destroySession($sessId);
149        $this->getDbService(ExternalSessionServiceInterface::class)->destroySession($sessId);
150        return true;
151    }
152
153    /**
154     * The garbage collector, this is executed when the session garbage collector
155     * is executed and takes the max session lifetime as its only parameter.
156     *
157     * @param int $sessMaxLifetime Maximum session lifetime.
158     *
159     * @return bool
160     *
161     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
162     */
163    #[\ReturnTypeWillChange]
164    public function gc($sessMaxLifetime)
165    {
166        // how often does this get called (if at all)?
167
168        // *** 08/Oct/09 - Greg Pendlebury
169        // Clearly this is being called. Production installs with
170        //   thousands of sessions active are showing no old sessions.
171        // What I can't do is reproduce for testing. It might need the
172        //   search delete code from 'destroy()' if it is not calling it.
173        // *** 09/Oct/09 - Greg Pendlebury
174        // Anecdotal testing Today and Yesterday seems to indicate destroy()
175        //   is called by the garbage collector and everything is good.
176        // Something to keep in mind though.
177        return true;
178    }
179
180    /**
181     * Write function that is called when session data is to be saved.
182     *
183     * @param string $sessId The current session ID
184     * @param string $data   The session data to write
185     *
186     * @return bool
187     */
188    public function write($sessId, $data): bool
189    {
190        if ($this->writesDisabled) {
191            return true;
192        }
193        return $this->saveSession($sessId, $data);
194    }
195
196    /**
197     * A function that is called internally when session data is to be saved.
198     *
199     * @param string $sessId The current session ID
200     * @param string $data   The session data to write
201     *
202     * @return bool
203     */
204    abstract protected function saveSession($sessId, $data): bool;
205}