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