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:
38: /**
39: * @param string $sKey
40: * @return bool
41: */
42: public static function Has($sKey)
43: {
44: if (!self::$bFirstStarted) {
45: self::Start();
46: }
47: return (isset($_SESSION[$sKey]));
48: }
49:
50: /**
51: * @param string $sKey
52: * @return void
53: */
54: public static function clear($sKey)
55: {
56: self::Start();
57: unset($_SESSION[$sKey]);
58: }
59:
60: /**
61: * @return void
62: */
63: public static function ClearAll()
64: {
65: self::Start();
66: $_SESSION = array();
67: }
68:
69: /**
70: * @return void
71: */
72: public static function Destroy()
73: {
74: self::Start();
75: self::$bStarted = false;
76: @session_destroy();
77: }
78:
79: /**
80: * @param string $sKey
81: * @param mixed $nmDefault = null
82: * @return mixed
83: */
84: public static function get($sKey, $nmDefault = null)
85: {
86: if (!self::$bFirstStarted) {
87: self::Start();
88: }
89:
90: return (isset($_SESSION[$sKey])) ? $_SESSION[$sKey] : $nmDefault;
91: }
92:
93: /**
94: * @param string $sKey
95: * @param mixed $mValue
96: */
97: public static function Set($sKey, $mValue)
98: {
99: self::Start();
100: $_SESSION[$sKey] = $mValue;
101: }
102:
103: /**
104: * @return string
105: */
106: public static function Id()
107: {
108: self::Start();
109: return @session_id();
110: }
111:
112: /**
113: * @param string $sId
114: *
115: * @return string
116: */
117: public static function SetId($sId)
118: {
119: self::Stop();
120: @session_id($sId);
121: self::Start();
122: return @session_id();
123: }
124:
125: /**
126: * @return void
127: */
128: public static function DestroySessionById($sId)
129: {
130: self::Stop();
131: @session_id($sId);
132: self::Start();
133: self::Destroy();
134: }
135:
136: /**
137: * @return bool
138: */
139: public static function Start()
140: {
141: if (@session_name() !== self::$sSessionName || !self::$bStarted || !self::$bFirstStarted) {
142: if (@session_name()) {
143: @session_write_close();
144: if (isset($GLOBALS['PROD_NAME']) && false !== strpos($GLOBALS['PROD_NAME'], 'Plesk')) { // Plesk
145: @session_module_name('files');
146: }
147: }
148:
149: @session_set_cookie_params(0);
150: if (!empty(self::$sSessionName)) {
151: @session_name(self::$sSessionName);
152: }
153:
154: self::$bFirstStarted = true;
155: self::$bStarted = true;
156:
157: return @session_start();
158: }
159:
160: return true;
161: }
162:
163: /**
164: * @return void
165: */
166: public static function Stop()
167: {
168: if (self::$bStarted) {
169: self::$bStarted = false;
170: @session_write_close();
171: }
172: }
173: }
174:
175: Session::$sSessionName = AU_API_SESSION_WEBMAIL_NAME;
176: