PluginProbe
Yatra – Travel Booking & Tour Operator Software / 2.2.10
Yatra – Travel Booking & Tour Operator Software v2.2.10
3.0.14 3.0.14.1 3.0.14.2 3.0.12 3.0.13 3.0.11 3.0.10 3.0.9 3.0.8 3.0.7 3.0.6 3.0.5 3.0.5.1 3.0.4 3.0.3 3.0.2.9 3.0.2.7 3.0.2.8 3.0.2.6 trunk 1.0.0 2.0.0 2.0.1 2.0.10 2.0.11 All 82 releases
yatra / core / Session.php

Session.php in Yatra – Travel Booking & Tour Operator Software 2.2.10, at core/Session.php

431 lines 11.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yatra\Core;
4
5 // Exit if accessed directly
6 defined('ABSPATH') || exit;
7
8 /**
9 * Session Class
10 *
11 * @since 2.1.12
12 */
13 class Session
14 {
15
16 /**
17 * Holds our session data.
18 *
19 * @var array
20 * @access private
21 * @since 2.1.12
22 */
23 private $session;
24
25 /**
26 * Whether to use PHP $_SESSION or WP_Session.
27 *
28 * @var bool
29 * @access private
30 * @since 2.1.12
31 */
32 private $use_php_sessions = false;
33
34 /**
35 * Session index prefix
36 *
37 * @var string
38 * @access private
39 * @since 2.1.12
40 */
41 private $prefix = '';
42
43 /**
44 * Constructor.
45 *
46 * Defines our WP_Session constants, includes the necessary libraries and
47 * retrieves the WP Session instance.
48 *
49 * @since 2.1.12
50 */
51 public function __construct()
52 {
53 $this->use_php_sessions = $this->use_php_sessions();
54
55 if ($this->use_php_sessions) {
56 if (is_multisite()) {
57 $this->prefix = '_' . get_current_blog_id();
58 }
59
60 // Use PHP SESSION (must be enabled via the YATRA_USE_PHP_SESSIONS constant)
61 add_action('init', array($this, 'maybe_start_session'), -2);
62 } else {
63 if (!$this->should_start_session()) {
64 return;
65 }
66
67 // Use WP_Session (default)
68 if (!defined('WP_SESSION_COOKIE')) {
69 define('WP_SESSION_COOKIE', 'yatra_wp_session');
70 }
71
72 if (!class_exists('Recursive_ArrayAccess')) {
73 require_once YATRA_ABSPATH . 'core/Libraries/class-recursive-arrayaccess.php';
74 }
75
76 if (!class_exists('WP_Session')) {
77 require_once YATRA_ABSPATH . 'core/Libraries/class-wp-session.php';
78 require_once YATRA_ABSPATH . 'core/Libraries/wp-session.php';
79 }
80
81 add_filter('wp_session_expiration_variant', array($this, 'set_expiration_variant_time'), 99999);
82 add_filter('wp_session_expiration', array($this, 'set_expiration_time'), 99999);
83 }
84
85 // Based off our session handling, we need to use different hooks and priorities.
86 if (empty($this->session) && !$this->use_php_sessions) {
87 $hook = 'plugins_loaded';
88 $priority = 10;
89 } else {
90 $hook = 'init';
91 $priority = -1;
92 }
93
94 add_action($hook, array($this, 'init'), $priority);
95 }
96
97 /**
98 * Setup the WP_Session instance.
99 *
100 * @since 2.1.12
101 */
102 public function init()
103 {
104 if ($this->use_php_sessions) {
105 $key = 'yatra' . $this->prefix;
106 $this->session = isset($_SESSION[$key]) && is_array($_SESSION[$key])
107 ? $_SESSION[$key]
108 : array();
109 } else {
110 $this->session = \WP_Session::get_instance();
111 }
112
113 $use_cookie = $this->use_cart_cookie();
114 $cart = $this->get('yatra_tour_cart');
115 $purchase = $this->get('yatra_tour_purchase');
116
117 if ($use_cookie) {
118 if (!empty($cart) || !empty($purchase)) {
119 $this->set_cart_cookie();
120 } else {
121 $this->set_cart_cookie(false);
122 }
123 }
124
125 return $this->session;
126 }
127
128 /**
129 * Retrieve session ID.
130 *
131 * @return string Session ID
132 * @since 2.1.12
133 *
134 */
135 public function get_id()
136 {
137 return $this->session->session_id;
138 }
139
140 /**
141 * Retrieve a session variable.
142 *
143 * @param string $key Session key.
144 * @return mixed Session variable.
145 * @since 2.1.12
146 *
147 */
148 public function get($key)
149 {
150 $key = sanitize_key($key);
151 $return = false;
152
153 if (isset($this->session[$key]) && !empty($this->session[$key])) {
154 preg_match('/[oO]\s*:\s*\d+\s*:\s*"\s*(?!(?i)(stdClass))/', $this->session[$key], $matches);
155
156 if (!empty($matches)) {
157 $this->set($key, null);
158 return false;
159 }
160 if (is_numeric($this->session[$key])) {
161 $return = $this->session[$key];
162 } else {
163 $maybe_json = json_decode($this->session[$key]);
164
165 // Since json_last_error is PHP 5.3+, we have to rely on a `null` value for failing to parse JSON.
166 if (is_null($maybe_json)) {
167 $is_serialized = is_serialized($this->session[$key]);
168 if ($is_serialized) {
169 $value = @unserialize($this->session[$key]);
170 $this->set($key, (array)$value);
171 $return = $value;
172 } else {
173 $return = $this->session[$key];
174 }
175 } else {
176 $return = json_decode($this->session[$key], true);
177 }
178 }
179
180 }
181
182
183 return $return;
184 }
185
186 /**
187 * Set a session variable.
188 *
189 * @param string $key Session key.
190 * @param int|string|array $value Session variable.
191 *
192 * @return mixed Session variable
193 * @since 2.1.12
194 *
195 */
196 public function set($key, $value)
197 {
198 $key = sanitize_key($key);
199
200 if (is_array($value)) {
201 $this->session[$key] = wp_json_encode($value);
202 } else {
203 $this->session[$key] = esc_attr($value);
204 }
205
206 if ($this->use_php_sessions) {
207 $_SESSION['yatra' . $this->prefix] = $this->session;
208 }
209
210 return $this->session[$key];
211 }
212
213 /**
214 * Set a cookie to identify whether the cart is empty or not.
215 *
216 * This is for hosts and caching plugins to identify if caching should be disabled.
217 *
218 * @param bool $set Whether to set or destroy. Default true.
219 * @since 2.1.12
220 *
221 */
222 public function set_cart_cookie($set = true)
223 {
224
225 // Bail if headers already sent.
226 if (headers_sent()) {
227 return;
228 }
229
230 if ($set) {
231 @setcookie('yatra_items_in_cart', '1', time() + 30 * 60, COOKIEPATH, COOKIE_DOMAIN, is_ssl());
232 } elseif (isset($_COOKIE['yatra_items_in_cart'])) {
233 @setcookie('yatra_items_in_cart', '', time() - 3600, COOKIEPATH, COOKIE_DOMAIN, is_ssl());
234 }
235 }
236
237 /**
238 * Force the cookie expiration variant time to 23 hours.
239 *
240 * @param int $exp Default expiration (1 hour).
241 * @return int Cookie expiration variant time.
242 * @since 2.1.12
243 *
244 */
245 public function set_expiration_variant_time($exp = 1)
246 {
247 return HOUR_IN_SECONDS * 23;
248 }
249
250 /**
251 * Force the cookie expiration time to 24 hours.
252 *
253 * @param int $exp Default expiration (1 hour).
254 * @return int Cookie expiration time.
255 * @since 2.1.12
256 *
257 */
258 public function set_expiration_time($exp = 1)
259 {
260 return HOUR_IN_SECONDS * 24;
261 }
262
263 /**
264 * Starts a new session if one hasn't started yet.
265 *
266 * Checks to see if the server supports PHP sessions
267 * or if the YATRA_USE_PHP_SESSIONS constant is defined
268 *
269 * @return bool $ret True if we are using PHP sessions, false otherwise.
270 * @since 2.1.12
271 */
272 public function use_php_sessions()
273 {
274
275 // Set default return value to false.
276 $ret = false;
277
278 // If the database variable is already set, no need to run autodetection.
279 $yatra_use_php_sessions = (bool)get_option('yatra_use_php_sessions');
280
281 if (!$yatra_use_php_sessions) {
282
283 // Attempt to detect if the server supports PHP sessions
284 if (function_exists('session_start')) {
285 $this->set('yatra_use_php_sessions', 1);
286
287 if ($this->get('yatra_use_php_sessions')) {
288 $ret = true;
289
290 // Set the database option
291 update_option('yatra_use_php_sessions', true);
292 }
293 }
294 } else {
295 $ret = $yatra_use_php_sessions;
296 }
297
298 // Enable or disable PHP Sessions based on the YATRA_USE_PHP_SESSIONS constant.
299 if (defined('YATRA_USE_PHP_SESSIONS') && YATRA_USE_PHP_SESSIONS) {
300 $ret = true;
301 } else if (defined('YATRA_USE_PHP_SESSIONS') && !YATRA_USE_PHP_SESSIONS) {
302 $ret = false;
303 }
304
305 // Filter & return.
306 return (bool)apply_filters('yatra_use_php_sessions', $ret);
307 }
308
309 /**
310 * Determines if a user has set the YATRA_USE_CART_COOKIE.
311 *
312 * @return bool If the store should use the yatra_items_in_cart cookie to help avoid caching
313 * @since 2.1.12
314 *
315 */
316 public function use_cart_cookie()
317 {
318
319 // Set default return value to true.
320 $ret = true;
321
322 if (defined('YATRA_USE_CART_COOKIE') && !YATRA_USE_CART_COOKIE) {
323 $ret = false;
324 }
325
326 // Filter & return.
327 return (bool)apply_filters('yatra_use_cart_cookie', $ret);
328 }
329
330 /**
331 * Determines if we should start sessions.
332 *
333 * @return bool True if sessions should start, false otherwise.
334 * @since 2.1.12
335 *
336 */
337 public function should_start_session()
338 {
339
340 // Set default return value to true.
341 $start_session = true;
342
343 if (!empty($_SERVER['REQUEST_URI'])) {
344 $blacklist = $this->get_blacklist();
345 $uri = ltrim($_SERVER['REQUEST_URI'], '/');
346 $uri = untrailingslashit($uri);
347
348 if (in_array($uri, $blacklist, true)) {
349 $start_session = false;
350 }
351
352 if (false !== strpos($uri, 'feed=')) {
353 $start_session = false;
354 }
355
356 // We do not want to start sessions in the admin unless we're processing an ajax request.
357 if (is_admin() && false === strpos($uri, 'wp-admin/admin-ajax.php')) {
358 $start_session = false;
359 }
360
361 // Starting sessions while saving the file editor can break the save process, so don't start.
362 if (false !== strpos($uri, 'wp_scrape_key')) {
363 $start_session = false;
364 }
365 }
366
367 // Filter & return.
368 return (bool)apply_filters('yatra_start_session', $start_session);
369 }
370
371 /**
372 * Retrieve the URI blacklist.
373 *
374 * These are the URIs where we never start sessions.
375 *
376 * @return array URI blacklist.
377 * @since 2.1.12
378 *
379 */
380 public function get_blacklist()
381 {
382 $blacklist = apply_filters('yatra_session_start_uri_blacklist', array(
383 'feed',
384 'feed/rss',
385 'feed/rss2',
386 'feed/rdf',
387 'feed/atom',
388 'comments/feed'
389 ));
390
391 // Look to see if WordPress is in a sub folder or this is a network site that uses sub folders
392 $folder = str_replace(network_home_url(), '', get_site_url());
393
394 if (!empty($folder)) {
395 foreach ($blacklist as $path) {
396 $blacklist[] = $folder . '/' . $path;
397 }
398 }
399
400 return $blacklist;
401 }
402
403 /**
404 * Starts a new session if one hasn't started yet.
405 *
406 * @since 2.1.12
407 */
408 public function maybe_start_session()
409 {
410
411 // Bail if should not start session.
412 if (!$this->should_start_session()) {
413 return;
414 }
415
416 // Bail if headers already sent.
417 if (headers_sent()) {
418 return;
419 }
420
421 // Start if old version of PHP & no session ID exists.
422 if (version_compare(PHP_VERSION, '5.4', '<') && !session_id()) {
423 session_start();
424
425 // Start if modern PHP and session-status is not active.
426 } elseif (defined('PHP_SESSION_ACTIVE') && (session_status() !== PHP_SESSION_ACTIVE)) {
427 session_start();
428 }
429 }
430 }
431