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