1: | <?php |
2: | /** |
3: | * This code is licensed under AGPLv3 license or Afterlogic Software License |
4: | * if commercial version of the product was purchased. |
5: | * For full statements of the licenses see LICENSE-AFTERLOGIC and LICENSE-AGPL3 files. |
6: | */ |
7: | |
8: | namespace Aurora\System; |
9: | |
10: | /** |
11: | * @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0 |
12: | * @license https://afterlogic.com/products/common-licensing Afterlogic Software License |
13: | * @copyright Copyright (c) 2019, Afterlogic Corp. |
14: | * |
15: | * @package Api |
16: | */ |
17: | class Session |
18: | { |
19: | /** |
20: | * @var string |
21: | */ |
22: | public static $sSessionName = ''; |
23: | |
24: | /** |
25: | * @var bool |
26: | */ |
27: | public static $bFirstStarted = false; |
28: | |
29: | /** |
30: | * @var bool |
31: | */ |
32: | public static $bStarted = false; |
33: | |
34: | private function __construct() {} |
35: | |
36: | /** |
37: | * @param string $sKey |
38: | * @return bool |
39: | */ |
40: | public static function Has($sKey) |
41: | { |
42: | if (!self::$bFirstStarted) { |
43: | self::Start(); |
44: | } |
45: | return (isset($_SESSION[$sKey])); |
46: | } |
47: | |
48: | /** |
49: | * @param string $sKey |
50: | * @return void |
51: | */ |
52: | public static function clear($sKey) |
53: | { |
54: | self::Start(); |
55: | unset($_SESSION[$sKey]); |
56: | } |
57: | |
58: | /** |
59: | * @return void |
60: | */ |
61: | public static function ClearAll() |
62: | { |
63: | self::Start(); |
64: | $_SESSION = array(); |
65: | } |
66: | |
67: | /** |
68: | * @return void |
69: | */ |
70: | public static function Destroy() |
71: | { |
72: | self::Start(); |
73: | self::$bStarted = false; |
74: | @session_destroy(); |
75: | } |
76: | |
77: | /** |
78: | * @param string $sKey |
79: | * @param mixed $nmDefault = null |
80: | * @return mixed |
81: | */ |
82: | public static function get($sKey, $nmDefault = null) |
83: | { |
84: | if (!self::$bFirstStarted) { |
85: | self::Start(); |
86: | } |
87: | |
88: | return (isset($_SESSION[$sKey])) ? $_SESSION[$sKey] : $nmDefault; |
89: | } |
90: | |
91: | /** |
92: | * @param string $sKey |
93: | * @param mixed $mValue |
94: | */ |
95: | public static function Set($sKey, $mValue) |
96: | { |
97: | self::Start(); |
98: | $_SESSION[$sKey] = $mValue; |
99: | } |
100: | |
101: | /** |
102: | * @return string |
103: | */ |
104: | public static function Id() |
105: | { |
106: | self::Start(); |
107: | return @session_id(); |
108: | } |
109: | |
110: | /** |
111: | * @param string $sId |
112: | * |
113: | * @return string |
114: | */ |
115: | public static function SetId($sId) |
116: | { |
117: | self::Stop(); |
118: | @session_id($sId); |
119: | self::Start(); |
120: | return @session_id(); |
121: | } |
122: | |
123: | /** |
124: | * @return void |
125: | */ |
126: | public static function DestroySessionById($sId) |
127: | { |
128: | self::Stop(); |
129: | @session_id($sId); |
130: | self::Start(); |
131: | self::Destroy(); |
132: | } |
133: | |
134: | /** |
135: | * @return bool |
136: | */ |
137: | public static function Start() |
138: | { |
139: | if (@session_name() !== self::$sSessionName || !self::$bStarted || !self::$bFirstStarted) { |
140: | if (@session_name()) { |
141: | @session_write_close(); |
142: | if (isset($GLOBALS['PROD_NAME']) && false !== strpos($GLOBALS['PROD_NAME'], 'Plesk')) { // Plesk |
143: | @session_module_name('files'); |
144: | } |
145: | } |
146: | |
147: | @session_set_cookie_params(0); |
148: | if (!empty(self::$sSessionName)) { |
149: | @session_name(self::$sSessionName); |
150: | } |
151: | |
152: | self::$bFirstStarted = true; |
153: | self::$bStarted = true; |
154: | |
155: | return @session_start(); |
156: | } |
157: | |
158: | return true; |
159: | } |
160: | |
161: | /** |
162: | * @return void |
163: | */ |
164: | public static function Stop() |
165: | { |
166: | if (self::$bStarted) { |
167: | self::$bStarted = false; |
168: | @session_write_close(); |
169: | } |
170: | } |
171: | } |
172: | |
173: | Session::$sSessionName = AU_API_SESSION_WEBMAIL_NAME; |
174: |