1
|
|
|
<?php |
2
|
|
|
// Exit if accessed directly. |
3
|
|
|
if (!defined( 'ABSPATH' ) ) exit; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Geodir_Session Class. |
7
|
|
|
* |
8
|
|
|
* @since 1.5.7 |
9
|
|
|
*/ |
10
|
|
|
class Geodir_Session { |
11
|
|
|
/** |
12
|
|
|
* Holds our session data. |
13
|
|
|
* |
14
|
|
|
* @var array |
15
|
|
|
* @access private |
16
|
|
|
* @since 1.5.7 |
17
|
|
|
*/ |
18
|
|
|
private $session; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Whether to use PHP $_SESSION or WP_Session. |
22
|
|
|
* |
23
|
|
|
* @var bool |
24
|
|
|
* @access private |
25
|
|
|
* @since 1.5.7 |
26
|
|
|
*/ |
27
|
|
|
private $use_php_sessions = false; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Session index prefix. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
* @access private |
34
|
|
|
* @since 1.5.7 |
35
|
|
|
*/ |
36
|
|
|
private $prefix = ''; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get things started. |
40
|
|
|
* |
41
|
|
|
* Defines our WP_Session constants, includes the necessary libraries and |
42
|
|
|
* retrieves the WP Session instance. |
43
|
|
|
* |
44
|
|
|
* @since 1.5.7 |
45
|
|
|
*/ |
46
|
|
|
public function __construct() { |
47
|
|
|
$this->use_php_sessions = $this->use_php_sessions(); |
48
|
|
|
|
49
|
|
|
if ( $this->use_php_sessions ) { |
50
|
|
|
if ( is_multisite() ) { |
51
|
|
|
$this->prefix = '_' . get_current_blog_id(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// Use PHP SESSION (must be enabled via the GEODIR_USE_PHP_SESSIONS constant) |
55
|
|
|
add_action( 'init', array( $this, 'maybe_start_session' ), -2 ); |
56
|
|
|
} else { |
57
|
|
|
// Use WP_Session (default) |
|
|
|
|
58
|
|
|
if ( !defined( 'WP_SESSION_COOKIE' ) ) { |
59
|
|
|
define( 'WP_SESSION_COOKIE', 'geodir_wp_session' ); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ( !class_exists( 'Recursive_ArrayAccess' ) ) { |
63
|
|
|
require_once GEODIRECTORY_PLUGIN_DIR . 'geodirectory-functions/wp-session/class-recursive-arrayaccess.php'; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ( !class_exists( 'WP_Session' ) ) { |
67
|
|
|
require_once GEODIRECTORY_PLUGIN_DIR . 'geodirectory-functions/wp-session/class-wp-session.php'; |
68
|
|
|
require_once GEODIRECTORY_PLUGIN_DIR . 'geodirectory-functions/wp-session/wp-session.php'; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
add_filter( 'wp_session_expiration_variant', array( $this, 'set_expiration_variant_time' ), 99999 ); |
72
|
|
|
add_filter( 'wp_session_expiration', array( $this, 'set_expiration_time' ), 99999 ); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if ( empty( $this->session ) && ! $this->use_php_sessions ) { |
76
|
|
|
add_action( 'plugins_loaded', array( $this, 'init' ), -1 ); |
77
|
|
|
} else { |
78
|
|
|
add_action( 'init', array( $this, 'init' ), -1 ); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Setup the WP_Session instance. |
84
|
|
|
* |
85
|
|
|
* @access public |
86
|
|
|
* @since 1.5.7 |
87
|
|
|
* @return void |
88
|
|
|
*/ |
89
|
|
|
public function init() { |
90
|
|
|
if ( $this->use_php_sessions ) { |
91
|
|
|
$this->session = isset( $_SESSION['gd' . $this->prefix ] ) && is_array( $_SESSION['gd' . $this->prefix ] ) ? $_SESSION['gd' . $this->prefix ] : array(); |
92
|
|
|
} else { |
93
|
|
|
$this->session = WP_Session::get_instance(); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $this->session; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Retrieve session ID. |
101
|
|
|
* |
102
|
|
|
* @access public |
103
|
|
|
* @since 1.5.7 |
104
|
|
|
* @return string Session ID |
105
|
|
|
*/ |
106
|
|
|
public function get_id() { |
107
|
|
|
return $this->session->session_id; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Retrieve a session variable. |
112
|
|
|
* |
113
|
|
|
* @access public |
114
|
|
|
* @since 1.5.7 |
115
|
|
|
* @param string $key Session key |
116
|
|
|
* @return string Session variable |
117
|
|
|
*/ |
118
|
26 |
|
public function get( $key ) { |
119
|
26 |
|
$key = sanitize_key( $key ); |
120
|
26 |
|
return isset( $this->session[ $key ] ) ? maybe_unserialize( $this->session[ $key ] ) : false; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Set a session variable |
125
|
|
|
* |
126
|
|
|
* @since 1.5.7 |
127
|
|
|
* |
128
|
|
|
* @param string $key Session key |
129
|
|
|
* @param integer $value Session variable |
130
|
|
|
* @return string Session variable |
131
|
|
|
*/ |
132
|
3 |
|
public function set( $key, $value ) { |
133
|
3 |
|
$key = sanitize_key( $key ); |
134
|
|
|
|
135
|
3 |
|
if ( is_array( $value ) ) { |
136
|
1 |
|
$this->session[ $key ] = maybe_serialize( $value ); |
137
|
1 |
|
} else { |
138
|
2 |
|
$this->session[ $key ] = $value; |
139
|
|
|
} |
140
|
|
|
|
141
|
3 |
|
if ( $this->use_php_sessions ) { |
142
|
|
|
$_SESSION['gd' . $this->prefix ] = $this->session; |
143
|
|
|
} |
144
|
|
|
|
145
|
3 |
|
return $this->session[ $key ]; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Unset a session variable. |
150
|
|
|
* |
151
|
|
|
* @since 1.5.7 |
152
|
|
|
* |
153
|
|
|
* @param string|array $key Session key. |
154
|
|
|
* @param integer $value Session variable. |
|
|
|
|
155
|
|
|
* @return string Session variable. |
156
|
|
|
*/ |
157
|
10 |
|
public function un_set( $key ) { |
158
|
10 |
|
if ( empty( $key ) ) { |
159
|
|
|
return false; |
160
|
|
|
} |
161
|
|
|
|
162
|
10 |
|
if ( is_array( $key ) ) { |
163
|
|
|
foreach ($key as $index) { |
164
|
|
|
$index = sanitize_key( $index ); |
165
|
|
|
|
166
|
|
|
if ( $index && isset( $this->session[ $index ] ) ) { |
167
|
|
|
unset( $this->session[ $index ] ); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
} else { |
171
|
10 |
|
$key = sanitize_key( $key ); |
172
|
|
|
|
173
|
10 |
|
if ( isset( $this->session[ $key ] ) ) { |
174
|
3 |
|
unset( $this->session[ $key ] ); |
175
|
3 |
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
10 |
|
if ( $this->use_php_sessions ) { |
179
|
|
|
$_SESSION['gd' . $this->prefix ] = $this->session; |
180
|
|
|
} |
181
|
|
|
|
182
|
10 |
|
return true; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Check a session variable is set or not. |
187
|
|
|
* |
188
|
|
|
* @since 1.5.7 |
189
|
|
|
* |
190
|
|
|
* @param string $key Session key. |
191
|
|
|
* @param integer $value Session variable. |
|
|
|
|
192
|
|
|
* @return string Session variable. |
193
|
|
|
*/ |
194
|
|
|
public function is_set( $key ) { |
195
|
|
|
$key = sanitize_key( $key ); |
196
|
|
|
|
197
|
|
|
if ( empty( $key ) ) { |
198
|
|
|
return false; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
if ( isset( $this->session[ $key ] ) ) { |
202
|
|
|
return true; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return false; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Force the cookie expiration variant time to 23 hours |
210
|
|
|
* |
211
|
|
|
* @access public |
212
|
|
|
* @since 1.5.7 |
213
|
|
|
* @param int $exp Default expiration (1 hour) |
214
|
|
|
* @return int |
215
|
|
|
*/ |
216
|
|
|
public function set_expiration_variant_time( $exp ) { |
|
|
|
|
217
|
|
|
return ( 30 * 60 * 23 ); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Force the cookie expiration time to 24 hours |
222
|
|
|
* |
223
|
|
|
* @access public |
224
|
|
|
* @since 1.5.7 |
225
|
|
|
* @param int $exp Default expiration (1 hour) |
226
|
|
|
* @return int |
227
|
|
|
*/ |
228
|
|
|
public function set_expiration_time( $exp ) { |
|
|
|
|
229
|
|
|
return ( 30 * 60 * 24 ); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Starts a new session if one hasn't started yet. |
234
|
|
|
* |
235
|
|
|
* @return boolean |
236
|
|
|
* Checks to see if the server supports PHP sessions |
237
|
|
|
* or if the GEODIR_USE_PHP_SESSIONS constant is defined |
238
|
|
|
* |
239
|
|
|
* @access public |
240
|
|
|
* @since 1.5.7 |
241
|
|
|
* @return boolean $ret True if we are using PHP sessions, false otherwise |
242
|
|
|
*/ |
243
|
|
|
public function use_php_sessions() { |
244
|
|
|
$ret = false; |
245
|
|
|
|
246
|
|
|
// If the database variable is already set, no need to run autodetection |
247
|
|
|
$geodir_use_php_sessions = (bool)get_option( 'geodir_use_php_sessions' ); |
248
|
|
|
|
249
|
|
|
if (!$geodir_use_php_sessions ) { |
250
|
|
|
// Attempt to detect if the server supports PHP sessions |
251
|
|
|
if ( function_exists( 'session_start' ) && ! ini_get( 'safe_mode' ) ) { |
252
|
|
|
$this->set( 'geodir_use_php_sessions', 1 ); |
253
|
|
|
|
254
|
|
|
if ( $this->get( 'geodir_use_php_sessions' ) ) { |
255
|
|
|
$ret = true; |
256
|
|
|
|
257
|
|
|
// Set the database option |
258
|
|
|
update_option( 'geodir_use_php_sessions', true ); |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
} else { |
262
|
|
|
$ret = $geodir_use_php_sessions; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
// Enable or disable PHP Sessions based on the GEODIR_USE_PHP_SESSIONS constant |
266
|
|
|
if ( defined( 'GEODIR_USE_PHP_SESSIONS' ) && GEODIR_USE_PHP_SESSIONS ) { |
267
|
|
|
$ret = true; |
268
|
|
|
} else if ( defined( 'GEODIR_USE_PHP_SESSIONS' ) && ! GEODIR_USE_PHP_SESSIONS ) { |
269
|
|
|
$ret = false; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
return (bool) apply_filters( 'geodir_use_php_sessions', $ret ); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Starts a new session if one hasn't started yet. |
277
|
|
|
*/ |
278
|
|
|
public function maybe_start_session() { |
279
|
|
|
if ( !session_id() && !headers_sent() ) { |
280
|
|
|
session_start(); |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Determines if a user has set the GEODIR_USE_CART_COOKIE |
286
|
|
|
* |
287
|
|
|
* @since 1.5.7 |
288
|
|
|
* @return bool If the store should use the geodir_items_in_cart cookie to help avoid caching |
289
|
|
|
*/ |
290
|
|
|
public function use_cart_cookie() { |
291
|
|
|
$ret = true; |
292
|
|
|
|
293
|
|
|
if ( defined( 'GEODIR_USE_CART_COOKIE' ) && ! GEODIR_USE_CART_COOKIE ) { |
294
|
|
|
$ret = false; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
return (bool) apply_filters( 'geodir_use_cart_cookie', $ret ); |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
global $gd_session; |
302
|
|
|
$gd_session = new Geodir_Session(); |
303
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.