PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.6.4
VikBooking Hotel Booking Engine & PMS v1.6.4
1.8.16 1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 All 37 releases
vikbooking / libraries / adapter / session / handler.php

handler.php in VikBooking Hotel Booking Engine & PMS 1.6.4, at libraries/adapter/session/handler.php

156 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package VikWP - Libraries
4 * @subpackage adapter.session
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 /**
15 * Class used to handle the PHP session.
16 * Provides the methods to begin a new session and to destroy it.
17 *
18 * @since 10.0
19 */
20 class JSessionHandler
21 {
22 /**
23 * Starts a new session, only if it doesn't already exist.
24 *
25 * @return void
26 */
27 public static function start()
28 {
29 if (!self::isStarted())
30 {
31 session_start();
32 }
33
34 /**
35 * Filters whether to preempt an HTTP request's return value.
36 * It is needed to support concurrent cURL requests (see Site Health).
37 *
38 * Returning a non-false value from the filter will short-circuit the HTTP request and return
39 * early with that value. A filter should return either:
40 *
41 * - An array containing 'headers', 'body', 'response', 'cookies', and 'filename' elements
42 * - A WP_Error instance
43 * - boolean false (to avoid short-circuiting the response)
44 *
45 * Returning any other value may result in unexpected behaviour.
46 *
47 * @since 2.9.0
48 *
49 * @param false|array|WP_Error $preempt Whether to preempt an HTTP request's return value. Default false.
50 * @param array $r HTTP request arguments.
51 * @param string $url The request URL.
52 */
53 add_filter('pre_http_request', function($preempt, $r, $url)
54 {
55 $input = JFactory::getApplication()->input;
56
57 $httpUrl = new JUri($url);
58 $baseUrl = new JUri(site_url());
59
60 /**
61 * As WP Site Health suggests, an active session might close timeouts for loopback connection.
62 * For this reason, every time an HTTP request is made, we should try to detect whether the
63 * provided end-point has the same root of the current website, meaning that we are performing
64 * a self connection, probably through the REST API.
65 *
66 * @since 10.1.39
67 */
68 if (strpos($httpUrl->toString(['host', 'path']), $baseUrl->toString(['host', 'path'])) !== false)
69 {
70 // loopback detected, terminate the session before starting a request
71 session_write_close();
72 }
73
74 return $preempt;
75 }, 10, 3);
76
77 /**
78 * Suppress "session active" critical error when Site Health performs its tests.
79 *
80 * Even if the session is active on Site Health, the plugin always call the
81 * `session_write_close` method before making any HTTP requests.
82 *
83 * @since 5.5.0
84 */
85 add_action('init', function() {
86 global $pagenow;
87
88 // check if the current page is Site Health
89 if (preg_match("/^site-health/i", $pagenow))
90 {
91 // always write session to avoid receiving a critical issue
92 session_write_close();
93 }
94 });
95 }
96
97 /**
98 * Destroys the current active session, only if it already exists.
99 *
100 * @param boolean $restart True to immediately restart a new session.
101 *
102 * @return void
103 */
104 public static function destroy($restart = true)
105 {
106 if (!self::isStarted())
107 {
108 return;
109 }
110
111 session_destroy();
112
113 if ($restart)
114 {
115 self::start();
116 }
117 }
118
119 /**
120 * Checks if a session has been started.
121 *
122 * @return boolean True if active, otherwise false.
123 */
124 public static function isStarted()
125 {
126 /**
127 * Make sure the session status is currently active. This because,
128 * in case someonehad manually closed the session, this method would
129 * have improperly returned true.
130 *
131 * @since 10.1.43
132 */
133 return session_status() === PHP_SESSION_ACTIVE && self::getId();
134 }
135
136 /**
137 * Returns the session ID, if any.
138 *
139 * @return string The unique session ID if active, otherwise an empty string.
140 */
141 public static function getId()
142 {
143 return session_id();
144 }
145
146 /**
147 * Returns the session name, if any.
148 *
149 * @return string The session name.
150 */
151 public static function getName()
152 {
153 return session_name();
154 }
155 }
156