PluginProbe
Sessions / 2.3.1
Sessions v2.3.1
2.1.0 2.10.0 2.11.0 2.12.0 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.2.0 2.3.0 2.3.1 2.4.0 2.4.1 2.5.0 2.6.0 2.6.1 2.6.2 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.1.0 3.1.1 All 39 releases
sessions / includes / features / class-capture.php

class-capture.php in Sessions 2.3.1, at includes/features/class-capture.php

358 lines 9.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Sessions event capture
4 *
5 * Handles all captures operations.
6 *
7 * @package Features
8 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
9 * @since 1.0.0
10 */
11
12 namespace POSessions\Plugin\Feature;
13
14
15 use POSessions\System\User;
16 use POSessions\Plugin\Feature\Schema;
17
18 /**
19 * Define the captures functionality.
20 *
21 * Handles all captures operations.
22 *
23 * @package Features
24 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
25 * @since 1.0.0
26 */
27 class Capture {
28
29 /**
30 * The number of expired sessions.
31 *
32 * @since 1.0.0
33 * @var integer $expired The number of expired sessions.
34 */
35 private static $expired = 0;
36
37 /**
38 * The number of idle sessions.
39 *
40 * @since 1.0.0
41 * @var integer $idle The number of idle sessions.
42 */
43 private static $idle = 0;
44
45 /**
46 * The number of forced sessions.
47 *
48 * @since 1.0.0
49 * @var integer $forced The number of forced sessions.
50 */
51 private static $forced = 0;
52
53 /**
54 * The number of registrations.
55 *
56 * @since 1.0.0
57 * @var integer $registration The number of registrations.
58 */
59 private static $registration = 0;
60
61 /**
62 * The number of deleted accounts.
63 *
64 * @since 1.0.0
65 * @var integer $delete The number of deleted accounts.
66 */
67 private static $delete = 0;
68
69 /**
70 * The number of password resets.
71 *
72 * @since 1.0.0
73 * @var integer $reset The number of password resets.
74 */
75 private static $reset = 0;
76
77 /**
78 * The number of logouts.
79 *
80 * @since 1.0.0
81 * @var integer $logout The number of logouts.
82 */
83 private static $logout = 0;
84
85 /**
86 * The number of successful logins.
87 *
88 * @since 1.0.0
89 * @var integer $login_success The number of successful logins.
90 */
91 private static $login_success = 0;
92
93 /**
94 * The number of failed logins.
95 *
96 * @since 1.0.0
97 * @var integer $login_fail The number of failed logins.
98 */
99 private static $login_fail = 0;
100
101 /**
102 * The number of blocked logins.
103 *
104 * @since 1.0.0
105 * @var integer $login_block The number of blocked logins.
106 */
107 private static $login_block = 0;
108
109 /**
110 * Initialize the class and set its properties.
111 *
112 * @since 1.0.0
113 */
114 public function __construct() {
115 }
116
117 /**
118 * Initialize the meta class and set its properties.
119 *
120 * @since 1.0.0
121 */
122 public static function init() {
123 add_action( 'sessions_after_idle_terminate', [ self::class, 'sessions_after_idle_terminate' ], 10, 1 );
124 add_action( 'sessions_after_expired_terminate', [ self::class, 'sessions_after_expired_terminate' ], 10, 1 );
125 add_action( 'auth_cookie_expired', [ self::class, 'auth_cookie_expired' ], 10, 1 );
126 add_action( 'sessions_force_terminate', [ self::class, 'sessions_force_terminate' ], 10, 1 );
127 add_action( 'sessions_force_admin_terminate', [ self::class, 'sessions_force_admin_terminate' ], 10, 1 );
128 add_action( 'delete_user', [ self::class, 'delete_user' ], 10, 2 );
129 add_action( 'user_register', [ self::class, 'user_register' ], 10, 1 );
130 add_action( 'password_reset', [ self::class, 'password_reset' ], 10, 2 );
131 add_action( 'wp_logout', [ self::class, 'wp_logout' ], 10, 0 );
132 add_action( 'wp_login_failed', [ self::class, 'wp_login_failed' ], 10, 1 );
133 add_action( 'wp_login', [ self::class, 'wp_login' ], 10, 2 );
134 add_action( 'jpp_kill_login', [ self::class, 'jpp_kill_login' ], 10, 1 );
135 }
136
137 /**
138 * Initialize the meta class and set its properties.
139 *
140 * @since 1.0.0
141 */
142 public static function late_init() {
143 add_action( 'wordfence_security_event', [ self::class, 'wordfence_security_event' ], 10, 1 );
144 }
145
146 /**
147 * Get the statistics.
148 *
149 * @return array The current statistics.
150 * @since 1.0.0
151 */
152 public static function get_stats() {
153 $result = [];
154 if ( 0 < self::$expired ) {
155 $result['expired'] = self::$expired;
156 }
157 if ( 0 < self::$idle ) {
158 $result['idle'] = self::$idle;
159 }
160 if ( 0 < self::$forced ) {
161 $result['forced'] = self::$forced;
162 }
163 if ( 0 < self::$registration ) {
164 $result['registration'] = self::$registration;
165 }
166 if ( 0 < self::$delete ) {
167 $result['delete'] = self::$delete;
168 }
169 if ( 0 < self::$reset ) {
170 $result['reset'] = self::$reset;
171 }
172 if ( 0 < self::$logout ) {
173 $result['logout'] = self::$logout;
174 }
175 if ( 0 < self::$login_success ) {
176 $result['login_success'] = self::$login_success;
177 }
178 if ( 0 < self::$login_fail ) {
179 $result['login_fail'] = self::$login_fail;
180 }
181 if ( 0 < self::$login_block ) {
182 $result['login_block'] = self::$login_block;
183 }
184 return $result;
185 }
186
187 /**
188 * Post actions for idle session terminated.
189 *
190 * @param integer $user_id The user ID.
191 * @since 1.0.0
192 */
193 public static function sessions_after_idle_terminate( $user_id ) {
194 self::$idle ++;
195 \DecaLog\Engine::eventsLogger( POSE_SLUG )->info( sprintf( 'Idle session terminated for %s.', User::get_user_string( $user_id ) ) );
196 }
197
198 /**
199 * Post actions for cookie expiration.
200 *
201 * @param array $cookie_elements The cookies elements.
202 * @since 1.0.0
203 */
204 public static function auth_cookie_expired( $cookie_elements ) {
205 // Don't allow too much iterations in case Decalog WordPress processor try to use get_current_user_id() and then restarts auth_cookie_expired hook.
206 try {
207 $cpt = 0;
208 // phpcs:ignore
209 foreach ( debug_backtrace( 0, 256 ) as $t ) {
210 if ( array_key_exists( 'function', $t ) && ( false !== strpos( $t['function'], 'auth_cookie_expired' ) ) ) {
211 $cpt++;
212 }
213 if ( 1 < $cpt ++ ) {
214 return;
215 }
216 }
217 } catch ( \Throwable $t ) {
218 return;
219 }
220 self::$forced ++;
221 \DecaLog\Engine::eventsLogger( POSE_SLUG )->info( 'Session cookie is expired.' );
222 }
223
224 /**
225 * Post actions for force session terminated.
226 *
227 * @param integer $user_id The user ID.
228 * @since 1.0.0
229 */
230 public static function sessions_force_terminate( $user_id ) {
231 self::$forced ++;
232 \DecaLog\Engine::eventsLogger( POSE_SLUG )->info( sprintf( 'Old session terminated for %s.', User::get_user_string( $user_id ) ) );
233 }
234
235 /**
236 * Post actions for force session terminated.
237 *
238 * @param integer $count The number of terminated sessions.
239 * @since 1.0.0
240 */
241 public static function sessions_force_admin_terminate( $count ) {
242 self::$forced = self::$forced + $count;
243 \DecaLog\Engine::eventsLogger( POSE_SLUG )->debug( sprintf( 'Batch termination for %d sessions.', $count ) );
244 }
245
246 /**
247 * Post actions for expired session terminated.
248 *
249 * @param integer $user_id The user ID.
250 * @since 1.0.0
251 */
252 public static function sessions_after_expired_terminate( $user_id ) {
253 self::$expired ++;
254 \DecaLog\Engine::eventsLogger( POSE_SLUG )->info( sprintf( 'Expired session terminated for %s.', User::get_user_string( $user_id ) ) );
255 }
256
257 /**
258 * "delete_user" event.
259 *
260 * @since 1.0.0
261 */
262 public static function delete_user( $user_id, $reassign ) {
263 self::$delete ++;
264 \DecaLog\Engine::eventsLogger( POSE_SLUG )->info( sprintf( 'Deleted account for %s.', User::get_user_string( $user_id ) ) );
265 }
266
267 /**
268 * "user_register" and "wpmu_new_user" events.
269 *
270 * @since 1.0.0
271 */
272 public static function user_register( $user_id ) {
273 self::$registration ++;
274 \DecaLog\Engine::eventsLogger( POSE_SLUG )->info( sprintf( 'Created account for %s.', User::get_user_string( $user_id ) ) );
275 }
276
277 /**
278 * "password_reset" event.
279 *
280 * @since 1.0.0
281 */
282 public static function password_reset( $user, $new_pass ) {
283 self::$reset ++;
284 \DecaLog\Engine::eventsLogger( POSE_SLUG )->info( sprintf( 'Password reset for %s.', User::get_user_string( $user instanceof \WP_User ? $user->ID : 0 ) ) );
285 }
286
287 /**
288 * "wp_logout" event.
289 *
290 * @since 1.0.0
291 */
292 public static function wp_logout() {
293 self::$logout ++;
294 \DecaLog\Engine::eventsLogger( POSE_SLUG )->info( 'A user has logged out.' );
295 }
296
297 /**
298 * "wp_login_failed" event.
299 *
300 * @since 1.0.0
301 */
302 public static function wp_login_failed( $username ) {
303 self::$login_fail ++;
304 \DecaLog\Engine::eventsLogger( POSE_SLUG )->info( sprintf( 'Login failed for "%s" username.', $username ) );
305 }
306
307 /**
308 * "wp_login" event.
309 *
310 * @since 1.0.0
311 */
312 public static function wp_login( $user_login, $user = null ) {
313 self::$login_success ++;
314 if ( ! $user ) {
315 $user = get_user_by( 'login', $user_login );
316 }
317 \DecaLog\Engine::eventsLogger( POSE_SLUG )->info( sprintf( 'Login success for %s.', User::get_user_string( $user instanceof \WP_User ? $user->ID : 0 ) ) );
318 }
319
320 /**
321 * "jpp_kill_login" event.
322 *
323 * @since 1.0.0
324 */
325 public static function jpp_kill_login( $ip ) {
326 self::$login_block ++;
327 \DecaLog\Engine::eventsLogger( POSE_SLUG )->info( sprintf( 'Login blocked for "%s".', $ip ) );
328 }
329
330 /**
331 * "wordfence_security_event" filter.
332 *
333 * @since 1.6.0
334 */
335 public static function wordfence_security_event( $event, $details = null, $a = null ) {
336 if ( 'loginLockout' === $event || 'breachLogin' === $event ) {
337 self::$login_block ++;
338 \DecaLog\Engine::eventsLogger( POSE_SLUG )->info( 'Login blocked.' );
339 }
340 }
341
342 /**
343 * "login_block" pseudo event.
344 *
345 * @param integer $user_id The user ID.
346 * @param boolean $dec Optional. Decrements login_failed.
347 * @since 1.0.0
348 */
349 public static function login_block( $user_id, $dec = false ) {
350 self::$login_block ++;
351 if ( $dec ) {
352 self::$login_fail --;
353 }
354 \DecaLog\Engine::eventsLogger( POSE_SLUG )->info( sprintf( 'Login blocked for %s.', User::get_user_string( $user_id ) ) );
355 }
356
357 }
358