PluginProbe ʕ •ᴥ•ʔ
LiteSpeed Cache / 7.0.0.1
LiteSpeed Cache v7.0.0.1
trunk 1.0.15 1.9.1.1 2.9.9.2 3.6.4 4.6 5.7.0.1 6.5.4 7.0.0.1 7.0.1 7.1 7.2 7.3 7.3.0.1 7.4 7.5 7.5.0.1 7.6 7.6.1 7.6.2 7.7 7.8 7.8.0.1 7.8.1
litespeed-cache / lib / guest.cls.php
litespeed-cache / lib Last commit date
css_js_min 1 year ago guest.cls.php 1 year ago html-min.cls.php 1 year ago object-cache.php 1 year ago php-compatibility.func.php 1 year ago urirewriter.cls.php 1 year ago
guest.cls.php
199 lines
1 <?php
2
3 namespace LiteSpeed\Lib;
4
5 /**
6 * Update guest vary
7 *
8 * @since 4.1
9 */
10 class Guest
11 {
12 const CONF_FILE = '.litespeed_conf.dat';
13 const HASH = 'hash'; // Not set-able
14 const O_CACHE_LOGIN_COOKIE = 'cache-login_cookie';
15 const O_DEBUG = 'debug';
16 const O_DEBUG_IPS = 'debug-ips';
17 const O_UTIL_NO_HTTPS_VARY = 'util-no_https_vary';
18 const O_GUEST_UAS = 'guest_uas';
19 const O_GUEST_IPS = 'guest_ips';
20
21 private static $_ip;
22 private static $_vary_name = '_lscache_vary'; // this default vary cookie is used for logged in status check
23 private $_conf = false;
24
25 /**
26 * Constructor
27 *
28 * @since 4.1
29 */
30 public function __construct()
31 {
32 !defined('LSCWP_CONTENT_FOLDER') && define('LSCWP_CONTENT_FOLDER', dirname(dirname(dirname(__DIR__))));
33 // Load config
34 $this->_conf = file_get_contents(LSCWP_CONTENT_FOLDER . '/' . self::CONF_FILE);
35 if ($this->_conf) {
36 $this->_conf = json_decode($this->_conf, true);
37 }
38
39 if (!empty($this->_conf[self::O_CACHE_LOGIN_COOKIE])) {
40 self::$_vary_name = $this->_conf[self::O_CACHE_LOGIN_COOKIE];
41 }
42 }
43
44 /**
45 * Update Guest vary
46 *
47 * @since 4.0
48 */
49 public function update_guest_vary()
50 {
51 // This process must not be cached
52 /**
53 * @reference https://wordpress.org/support/topic/soft-404-from-google-search-on-litespeed-cache-guest-vary-php/#post-16838583
54 */
55 header('X-Robots-Tag: noindex');
56 header('X-LiteSpeed-Cache-Control: no-cache');
57
58 if ($this->always_guest()) {
59 echo '[]';
60 exit;
61 }
62
63 // If contains vary already, don't reload to avoid infinite loop when parent page having browser cache
64 if ($this->_conf && self::has_vary()) {
65 echo '[]';
66 exit;
67 }
68
69 // Send vary cookie
70 $vary = 'guest_mode:1';
71 if ($this->_conf && empty($this->_conf[self::O_DEBUG])) {
72 $vary = md5($this->_conf[self::HASH] . $vary);
73 }
74
75 $expire = time() + 2 * 86400;
76 $is_ssl = !empty($this->_conf[self::O_UTIL_NO_HTTPS_VARY]) ? false : $this->is_ssl();
77 setcookie(self::$_vary_name, $vary, $expire, '/', false, $is_ssl, true);
78
79 // return json
80 echo json_encode(array('reload' => 'yes'));
81 exit;
82 }
83
84 /**
85 * WP's is_ssl() func
86 *
87 * @since 4.1
88 */
89 private function is_ssl()
90 {
91 if (isset($_SERVER['HTTPS'])) {
92 if ('on' === strtolower($_SERVER['HTTPS'])) {
93 return true;
94 }
95
96 if ('1' == $_SERVER['HTTPS']) {
97 return true;
98 }
99 } elseif (isset($_SERVER['SERVER_PORT']) && ('443' == $_SERVER['SERVER_PORT'])) {
100 return true;
101 }
102 return false;
103 }
104
105 /**
106 * Check if default vary has a value
107 *
108 * @since 1.1.3
109 * @access public
110 */
111 public static function has_vary()
112 {
113 if (empty($_COOKIE[self::$_vary_name])) {
114 return false;
115 }
116 return $_COOKIE[self::$_vary_name];
117 }
118
119 /**
120 * Detect if is a guest visitor or not
121 *
122 * @since 4.0
123 */
124 public function always_guest()
125 {
126 if (empty($_SERVER['HTTP_USER_AGENT'])) {
127 return false;
128 }
129
130 if ($this->_conf[self::O_GUEST_UAS]) {
131 $quoted_uas = array();
132 foreach ($this->_conf[self::O_GUEST_UAS] as $v) {
133 $quoted_uas[] = preg_quote($v, '#');
134 }
135 $match = preg_match('#' . implode('|', $quoted_uas) . '#i', $_SERVER['HTTP_USER_AGENT']);
136 if ($match) {
137 return true;
138 }
139 }
140
141 if ($this->ip_access($this->_conf[self::O_GUEST_IPS])) {
142 return true;
143 }
144
145 return false;
146 }
147
148 /**
149 * Check if the ip is in the range
150 *
151 * @since 1.1.0
152 * @access public
153 */
154 public function ip_access($ip_list)
155 {
156 if (!$ip_list) {
157 return false;
158 }
159 if (!isset(self::$_ip)) {
160 self::$_ip = self::get_ip();
161 }
162 // $uip = explode('.', $_ip);
163 // if(empty($uip) || count($uip) != 4) Return false;
164 // foreach($ip_list as $key => $ip) $ip_list[$key] = explode('.', trim($ip));
165 // foreach($ip_list as $key => $ip) {
166 // if(count($ip) != 4) continue;
167 // for($i = 0; $i <= 3; $i++) if($ip[$i] == '*') $ip_list[$key][$i] = $uip[$i];
168 // }
169 return in_array(self::$_ip, $ip_list);
170 }
171
172 /**
173 * Get client ip
174 *
175 * @since 1.1.0
176 * @since 1.6.5 changed to public
177 * @access public
178 * @return string
179 */
180 public static function get_ip()
181 {
182 $_ip = '';
183 if (function_exists('apache_request_headers')) {
184 $apache_headers = apache_request_headers();
185 $_ip = !empty($apache_headers['True-Client-IP']) ? $apache_headers['True-Client-IP'] : false;
186 if (!$_ip) {
187 $_ip = !empty($apache_headers['X-Forwarded-For']) ? $apache_headers['X-Forwarded-For'] : false;
188 $_ip = explode(',', $_ip);
189 $_ip = $_ip[0];
190 }
191 }
192
193 if (!$_ip) {
194 $_ip = !empty($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : false;
195 }
196 return $_ip;
197 }
198 }
199