xref: /webtrees/app/Session.php (revision cbc1590a8c715aa2d88bd745610b899587bd9563)
1<?php
2namespace Fisharebest\Webtrees;
3
4/**
5 * webtrees: online genealogy
6 * Copyright (C) 2015 webtrees development team
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/**
20 * Class Session - temporary class to migrate away from Zend_Session
21 * to Symfony-based sessions, which need PHP 5.4
22 */
23class Session {
24	/**
25	 * Start a session
26	 *
27	 * @param array $config
28	 */
29	public static function start(array $config = array()) {
30		$default_config = array(
31			'use_cookies'     => 1,
32			'name'            => 'WT_SESSION',
33			'cookie_lifetime' => 0,
34			'gc_maxlifetime'  => 7200,
35			'gc_probability'  => 1,
36			'gc_divisor'      => 100,
37			'cookie_path'     => '',
38			'cookie_httponly' => true,
39		);
40		session_register_shutdown();
41		foreach ($config + $default_config as $key => $value) {
42			ini_set('session.' . $key, $value);
43		}
44		session_start();
45	}
46
47	/**
48	 * Read a value from the session
49	 *
50	 * @param string $name
51	 * @param mixed  $default
52	 *
53	 * @return mixed
54	 */
55	public static function get($name, $default = null) {
56		if (isset($_SESSION[$name])) {
57			return $_SESSION[$name];
58		} else {
59			return $default;
60		}
61	}
62
63	/**
64	 * Write a value to the session
65	 *
66	 * @param string $name
67	 * @param mixed  $value
68	 */
69	public static function put($name, $value) {
70		$_SESSION[$name] = $value;
71	}
72
73	/**
74	 * Remove a value from the session
75	 *
76	 * @param string $name
77	 */
78	public static function forget($name) {
79		unset($_SESSION[$name]);
80	}
81
82	/**
83	 * Does a session variable exist?
84	 *
85	 * @param string $name
86	 *
87	 * @return bool
88	 */
89	public static function has($name) {
90		return array_key_exists($name, $_SESSION);
91	}
92
93	/**
94	 * Remove all stored data from the session.
95	 */
96	public static function clear() {
97		$_SESSION = array();
98	}
99
100	/**
101	 * After any change in authentication level, we should use a new session ID.
102	 *
103	 * @param bool $destroy
104	 */
105	public static function regenerate($destroy = false) {
106		if ($destroy) {
107			self::clear();
108		}
109		session_regenerate_id($destroy);
110	}
111
112	/**
113	 * Set an explicit session ID.  Typically used for search robots.
114	 *
115	 * @param string $id
116	 */
117	public static function setId($id) {
118		session_id($id);
119	}
120}
121