xref: /webtrees/app/Session.php (revision f5004097ad6ca7a7c4d85c316fcd5cfb3c7342f2)
131bc7874SGreg Roach<?php
231bc7874SGreg Roachnamespace Fisharebest\Webtrees;
331bc7874SGreg Roach
431bc7874SGreg Roach/**
531bc7874SGreg Roach * webtrees: online genealogy
631bc7874SGreg Roach * Copyright (C) 2015 webtrees development team
731bc7874SGreg Roach * This program is free software: you can redistribute it and/or modify
831bc7874SGreg Roach * it under the terms of the GNU General Public License as published by
931bc7874SGreg Roach * the Free Software Foundation, either version 3 of the License, or
1031bc7874SGreg Roach * (at your option) any later version.
1131bc7874SGreg Roach * This program is distributed in the hope that it will be useful,
1231bc7874SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1331bc7874SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1431bc7874SGreg Roach * GNU General Public License for more details.
1531bc7874SGreg Roach * You should have received a copy of the GNU General Public License
1631bc7874SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
1731bc7874SGreg Roach */
1831bc7874SGreg Roach
1931bc7874SGreg Roach/**
2031bc7874SGreg Roach * Class Session - temporary class to migrate away from Zend_Session
2131bc7874SGreg Roach * to Symfony-based sessions, which need PHP 5.4
2231bc7874SGreg Roach */
2331bc7874SGreg Roachclass Session {
2431bc7874SGreg Roach	/**
2531bc7874SGreg Roach	 * Start a session
2631bc7874SGreg Roach	 *
2731bc7874SGreg Roach	 * @param array $config
2831bc7874SGreg Roach	 */
2931bc7874SGreg Roach	public static function start(array $config = array()) {
3031bc7874SGreg Roach		$default_config = array(
3131bc7874SGreg Roach			'session.use_cookies' => 1,
3231bc7874SGreg Roach			'name' => 'WT_SESSION',
3331bc7874SGreg Roach			'cookie_lifetime' => 0,
3431bc7874SGreg Roach			'gc_maxlifetime' => 7200,
3531bc7874SGreg Roach			'gc_probability' => 1,
3631bc7874SGreg Roach			'gc_divisor' => 100,
3731bc7874SGreg Roach			'cookie_path' => '',
3831bc7874SGreg Roach			'cookie_httponly' => true,
3931bc7874SGreg Roach		);
4031bc7874SGreg Roach		session_register_shutdown();
4131bc7874SGreg Roach		foreach ($config + $default_config as $key => $value) {
4231bc7874SGreg Roach			ini_set($key, $value);
4331bc7874SGreg Roach		}
4431bc7874SGreg Roach		session_start();
4531bc7874SGreg Roach	}
4631bc7874SGreg Roach
4731bc7874SGreg Roach	/**
4831bc7874SGreg Roach	 * Read a value from the session
4931bc7874SGreg Roach	 *
5031bc7874SGreg Roach	 * @param string $name
5131bc7874SGreg Roach	 * @param mixed  $default
5231bc7874SGreg Roach	 *
5331bc7874SGreg Roach	 * @return mixed
5431bc7874SGreg Roach	 */
5531bc7874SGreg Roach	public static function get($name, $default = null) {
5631bc7874SGreg Roach		if (isset($_SESSION[$name])) {
5731bc7874SGreg Roach			return $_SESSION[$name];
5831bc7874SGreg Roach		} else {
5931bc7874SGreg Roach			return $default;
6031bc7874SGreg Roach		}
6131bc7874SGreg Roach	}
6231bc7874SGreg Roach
6331bc7874SGreg Roach	/**
6431bc7874SGreg Roach	 * Write a value to the session
6531bc7874SGreg Roach	 *
6631bc7874SGreg Roach	 * @param string $name
6731bc7874SGreg Roach	 * @param mixed  $value
6831bc7874SGreg Roach	 */
6931bc7874SGreg Roach	public static function put($name, $value) {
7031bc7874SGreg Roach		$_SESSION[$name] = $value;
7131bc7874SGreg Roach	}
7231bc7874SGreg Roach
7331bc7874SGreg Roach	/**
7431bc7874SGreg Roach	 * Remove a value from the session
7531bc7874SGreg Roach	 *
7631bc7874SGreg Roach	 * @param string $name
7731bc7874SGreg Roach	 */
7831bc7874SGreg Roach	public static function forget($name) {
7931bc7874SGreg Roach		unset($_SESSION[$name]);
8031bc7874SGreg Roach	}
8131bc7874SGreg Roach
8231bc7874SGreg Roach	/**
8331bc7874SGreg Roach	 * Does a session variable exist?
8431bc7874SGreg Roach	 *
8531bc7874SGreg Roach	 * @param string $name
8631bc7874SGreg Roach	 *
8731bc7874SGreg Roach	 * @return boolean
8831bc7874SGreg Roach	 */
8931bc7874SGreg Roach	public static function has($name) {
9031bc7874SGreg Roach		return array_key_exists($name, $_SESSION);
9131bc7874SGreg Roach	}
9231bc7874SGreg Roach
9331bc7874SGreg Roach	/**
94*f5004097SGreg Roach	 * Remove all stored data from the session.
95*f5004097SGreg Roach	 */
96*f5004097SGreg Roach	public static function clear() {
97*f5004097SGreg Roach		$_SESSION = array();
98*f5004097SGreg Roach	}
99*f5004097SGreg Roach
100*f5004097SGreg Roach	/**
10131bc7874SGreg Roach	 * After any change in authentication level, we should use a new session ID.
10231bc7874SGreg Roach	 *
10331bc7874SGreg Roach	 * @param bool $destroy
10431bc7874SGreg Roach	 */
10531bc7874SGreg Roach	public static function regenerate($destroy = false) {
106*f5004097SGreg Roach		if ($destroy) {
107*f5004097SGreg Roach			self::clear();
108*f5004097SGreg Roach		}
10931bc7874SGreg Roach		session_regenerate_id($destroy);
11031bc7874SGreg Roach	}
11131bc7874SGreg Roach
11231bc7874SGreg Roach	/**
11331bc7874SGreg Roach	 * Set an explicit session ID.  Typically used for search robots.
11431bc7874SGreg Roach	 *
11531bc7874SGreg Roach	 * @param string $id
11631bc7874SGreg Roach	 */
11731bc7874SGreg Roach	public static function setId($id) {
11831bc7874SGreg Roach		session_id($id);
11931bc7874SGreg Roach	}
12031bc7874SGreg Roach}
121