summaryrefslogtreecommitdiffstats
path: root/system/classes/session.php
blob: f08b2b77b6d66bece97d18005c8b0dd6f5139956 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php

/**
 * Simple class for accessing session data
 * @package Core
 */
class Session
{

	/**
	 * Makes sure the session is initialized
	 *
	 * @return void
	 * @access private
	 * @static
	 */
	private static function check()
	{
		if (!session_id())
		{
			session_start();
		}
	}

	/**
	 * Gets a session variable
	 *
	 * @param string $key     Variable name
	 * @param mixed $default Default value
	 * @return mixed Session value
	 * @access public
	 * @static
	 */
	public static function get($key, $default = null)
	{
		Session::check();
		return Misc::arr($_SESSION, $key, $default);
	}

	/**
	 * Sets a session variable
	 *
	 * @param string $key Variable name
	 * @param mixed $val Variable value
	 * @return void
	 * @access public
	 * @static
	 */
	public static function set($key, $val)
	{
		Session::check();
		$_SESSION[$key] = $val;
	}

	/**
	 * Removes a session variable
	 *
	 * @param string $key Variable name
	 * @return void
	 * @access public
	 * @static
	 */
	public static function remove($key)
	{
		Session::check();

		if (!isset($_SESSION[$key]))
			return;

		$var = $_SESSION[$key];
		unset($_SESSION[$key], $var);
	}

	/**
	 * Resets the session
	 *
	 * @return void
	 * @access public
	 * @static
	 */
	public static function reset()
	{
		Session::check();
		$_SESSION = array();
	}

	/**
	 * Gets ot sets flash messages.
	 * If the value parameter is passed the message is set, otherwise it is retrieved.
	 * After the message is retrieved for the first time it is removed.
	 *
	 * @param $key  The name of the flash message
	 * @param $val  Flash message content
	 * @return mixed
	 * @access public
	 * @static
	 */
	public static function flash($key, $val = null)
	{
		Session::check();
		$key = "flash_{$key}";
		if ($val != null)
		{
			Session::set($key, $val);
		}
		else
		{
			$val = Session::get($key);
			Session::remove($key);
		}

		return $val;
	}

}