-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathclass-give-session.php
More file actions
703 lines (611 loc) · 15 KB
/
Copy pathclass-give-session.php
File metadata and controls
703 lines (611 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
<?php
/**
* Session
*
* @since 1.0
* @subpackage Classes/Give_Session
* @copyright Copyright (c) 2016, GiveWP
* @license https://opensource.org/licenses/gpl-license GNU Public License
* @package Give
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class Give_Session
*/
class Give_Session {
/**
* Instance.
*
* @since 2.2.0
* @access private
* @var Give_Session
*/
private static $instance;
/**
* Holds our session data
*
* @since 1.0
* @access private
*
* @var array
*/
private $session = [];
/**
* Holds our session data
*
* @since 1.0
* @access private
*
* @var string
*/
private $session_data_changed = false;
/**
* Cookie Name
*
* @since 1.0
* @access private
*
* @var string
*/
private $cookie_name = '';
/**
* Donor Unique ID
*
* @since 1.0
* @access private
*
* @var string
*/
private $donor_id = '';
/**
* Session expiring time
*
* @since 2.2.0
* @access private
*
* @var string
*/
private $session_expiring = false;
/**
* Session expiration time
*
* @since 2.2.0
* @access private
*
* @var string
*/
private $session_expiration = false;
/**
* Flag to check if donor has cookie or not
*
* @since 2.2.0
* @access private
*
* @var bool
*/
private $has_cookie = false;
/**
* Expiration Time
*
* @since 1.0
* @access private
*
* @var int
*/
private $exp_option = false;
/**
* Expiration Time
*
* @since 2.2.0
* @access private
*
* @var string
*/
private $nonce_cookie_name = '';
/**
* Singleton pattern.
*
* @since 2.2.0
* @access private
*/
private function __construct() {
}
/**
* Get instance.
*
* @since 2.2.0
* @access public
* @return Give_Session
*/
public static function get_instance() {
if ( null === static::$instance ) {
self::$instance = new static();
self::$instance->__setup();
}
return self::$instance;
}
/**
* Setup
*
* @since 2.2.0
* @access public
*/
private function __setup() { // @codingStandardsIgnoreLine
$this->exp_option = give_get_option( 'session_lifetime' );
$this->exp_option = ! empty( $this->exp_option )
? $this->exp_option
: 30 * 60 * 24; // Default expiration time is 12 hours
$this->set_cookie_name();
$cookie = $this->get_session_cookie();
if ( ! empty( $cookie ) ) {
$this->donor_id = $cookie[0];
$this->session_expiration = $cookie[1];
$this->session_expiring = $cookie[2];
$this->has_cookie = true;
// Update session if its close to expiring.
if ( time() > $this->session_expiring ) {
$this->set_expiration_time();
Give()->session_db->update_session_timestamp( $this->donor_id, $this->session_expiration );
}
// Load session.
$this->session = $this->get_session_data();
} else {
$this->generate_donor_id();
}
add_action( 'give_process_donation_after_validation', [ $this, 'maybe_start_session' ] );
add_action( 'wp_login', [ $this, 'startSessionWhenLoginAsWPUser' ], 10, 2 );
add_action( 'shutdown', [ $this, 'save_data' ], 20 );
add_action( 'wp_logout', [ $this, 'destroy_session' ] );
if ( ! is_user_logged_in() ) {
add_filter( 'nonce_user_logged_out', [$this, 'nonce_user_logged_out'] );
}
// Remove old sessions.
Give_Cron::add_daily_event( [$this, 'cleanup_sessions'] );
}
/**
* Get session data
*
* @since 2.2.0
* @access public
*
* @return array
*/
public function get_session_data() {
return $this->has_session() ? (array) Give()->session_db->get_session( $this->donor_id, [] ) : [];
}
/**
* Get session by session id
*
* @since 2.2.0
* @access public
*
* @return array
*/
public function get_session_cookie() {
$session = [];
$cookie_value = isset( $_COOKIE[ $this->cookie_name ] ) ? give_clean( $_COOKIE[ $this->cookie_name ] ) : $this->handle_ajax_cookie(); // @codingStandardsIgnoreLine.
if ( empty( $cookie_value ) || ! is_string( $cookie_value ) ) {
return $session;
}
[ $donor_id, $session_expiration, $session_expiring, $cookie_hash ] = explode( '||', $cookie_value );
if ( empty( $donor_id ) ) {
return $session;
}
require_once ABSPATH . WPINC . '/pluggable.php';
// Validate hash.
$to_hash = $donor_id . '|' . $session_expiration;
$hash = hash_hmac( 'md5', $to_hash, wp_hash( $to_hash ) );
if ( empty( $cookie_hash ) || ! hash_equals( $hash, $cookie_hash ) ) {
return $session;
}
/**
* Filter the session cookie data
*
* @since 2.2.6
*/
$cookie_data = apply_filters(
'give_get_session_cookie',
[ $donor_id, $session_expiration, $session_expiring, $cookie_hash ]
);
return $cookie_data;
}
/**
* Load session cookie by ajax
*
* @since 4.9.0 rename function - PHP 8 compatibility
* @since 2.2.6
* @access private
*
* @return array|bool|string
*/
private function handle_ajax_cookie()
{
$cookie = false;
// @see https://github.com/impress-org/give/issues/3705
if (
empty( $cookie )
&& wp_doing_ajax()
&& isset( $_GET['action'] )
&& 'get_receipt' === $_GET['action']
) {
$cookie = isset( $_GET[ $this->cookie_name ] ) ? give_clean( $_GET[ $this->cookie_name ] ) : false;
}
return $cookie;
}
/**
* Check if session exist for specific session id
*
* @since 2.2.0
* @access public
*
* @return bool
*/
public function has_session() {
return $this->has_cookie;
}
/**
* Set cookie name
*
* @since 2.2.0
* @access private
*
* @return void
*/
private function set_cookie_name() {
/**
* Filter the cookie name
*
* @since 2.2.0
*
* @param string $cookie_name Cookie name.
* @param string $cookie_type Cookie type session or nonce.
*/
$this->cookie_name = apply_filters(
'give_session_cookie',
'wp-give_session_' . COOKIEHASH, // Cookie name.
'session' // Cookie type.
);
$this->nonce_cookie_name = apply_filters(
'give_session_cookie',
'wp-give_session_reset_nonce_' . COOKIEHASH, // Cookie name.
'nonce' // Cookie type.
);
}
/**
* Get session donor id
*
* @since 2.10.0
* @access public
*
* @return int
*/
public function get_donor_id() {
return $this->donor_id;
}
/**
* Get Session
*
* Retrieve session variable for a given session key.
*
* @since 1.0
* @access public
*
* @param string $key Session key.
* @param mixed $default default value.
*
* @return string|array Session variable.
*/
public function get( $key, $default = false ) {
$key = sanitize_key( $key );
return isset( $this->session[ $key ] ) ? maybe_unserialize( $this->session[ $key ] ) : $default;
}
/**
* Set Session
*
* @since 1.0
* @access public
*
* @param string $key Session key.
* @param mixed $value Session variable.
*
* @return string Session variable.
*/
public function set( $key, $value ) {
if ( $value !== $this->get( $key ) ) {
$this->session[ sanitize_key( $key ) ] = maybe_serialize( $value );
$this->session_data_changed = true;
}
return $this->session[ $key ];
}
/**
* Set Session Cookies
*
* Cookies are used to increase the session lifetime using the give setting. This is helpful for when a user closes
* their browser after making a donation and comes back to the site.
*
* @since 1.4
* @access public
*
* @param bool $set Flag to check if set cookie or not.
*/
public function set_session_cookies( $set ) {
if ( $set ) {
$this->set_expiration_time();
$to_hash = $this->donor_id . '|' . $this->session_expiration;
$cookie_hash = hash_hmac( 'md5', $to_hash, wp_hash( $to_hash ) );
$cookie_value = $this->donor_id . '||' . $this->session_expiration . '||' . $this->session_expiring . '||' . $cookie_hash;
$this->has_cookie = true;
give_setcookie( $this->cookie_name, $cookie_value, $this->session_expiration, apply_filters( 'give_session_use_secure_cookie', false ) );
give_setcookie( $this->nonce_cookie_name, '1', $this->session_expiration, apply_filters( 'give_session_use_secure_cookie', false ) );
}
}
/**
* Set Cookie Expiration
*
* Force the cookie expiration time if set, default to 24 hours.
*
* @since 1.0
* @access public
*
* @return int
*/
public function set_expiration_time() {
$this->session_expiring = time() + intval( apply_filters( 'give_session_expiring', ( $this->exp_option - 3600 ) ) ); // Default 11 Hours.
$this->session_expiration = time() + intval( apply_filters( 'give_session_expiration', $this->exp_option ) ); // Default 12 Hours.
return $this->session_expiration;
}
/**
* Get Session Expiration
*
* Looks at the session cookies and returns the expiration date for this session if applicable
*
* @since 2.2.0
* @access public
*
* @return string|bool Formatted expiration date string.
*/
public function get_session_expiration() {
return $this->has_session() ? $this->session_expiration : false;
}
/**
* Maybe Start Session
*
* Starts a new session if one hasn't started yet.
*
* @since 2.2.0
* @access public
*
* @return void
*/
public function maybe_start_session() {
if (
! headers_sent()
&& empty( $this->session )
&& ! $this->has_cookie
) {
$this->set_session_cookies( true );
}
}
/**
* Setup donor session when authorized by WP user credentials
*
* @since 2.7.0
*
* @param WP_User $wpUser
* @param string $wpUserLogin
*/
public function startSessionWhenLoginAsWPUser( $wpUserLogin, $wpUser ) {
$donor = Give()->donors->get_donor_by( 'user_id', $wpUser->ID );
// Setup session only if donor exist for specific WP user.
if ( $donor ) {
$this->maybe_start_session();
$this->set( 'give_email', $donor->email );
}
}
/**
* Generate a unique donor ID.
*
* Uses Portable PHP password hashing framework to generate a unique cryptographically strong ID.
*
* @since 2.2.0
* @access public
*/
public function generate_donor_id() {
require_once ABSPATH . 'wp-includes/class-phpass.php';
$hasher = new PasswordHash( 8, false );
$this->donor_id = md5( $hasher->get_random_bytes( 32 ) );
}
/**
* Save donor session data
*
* @since 2.2.0
* @access public
*/
public function save_data() {
// Dirty if something changed - prevents saving nothing new.
if ( $this->session_data_changed && $this->has_session() ) {
global $wpdb;
Give()->session_db->replace(
Give()->session_db->table_name,
[
'session_key' => $this->donor_id,
'session_value' => maybe_serialize( $this->session ),
'session_expiry' => $this->session_expiration,
],
[
'%s',
'%s',
'%d',
]
);
$this->session_data_changed = false;
}
}
/**
* Destroy all session data.
*
* @since 2.2.0
* @access public
*/
public function destroy_session() {
give_setcookie( 'give_nl', '', time() - YEAR_IN_SECONDS, apply_filters( 'give_session_use_secure_cookie', false ) );
give_setcookie( $this->cookie_name, '', time() - YEAR_IN_SECONDS, apply_filters( 'give_session_use_secure_cookie', false ) );
give_setcookie( $this->nonce_cookie_name, '', time() - YEAR_IN_SECONDS, apply_filters( 'give_session_use_secure_cookie', false ) );
Give()->session_db->delete_session( $this->donor_id );
$this->session = [];
$this->session_data_changed = false;
$this->generate_donor_id();
}
/**
* Delete nonce cookie if generating fresh form html.
*
* @since 2.2.0
* @access public
*
* @return bool
*/
public function is_delete_nonce_cookie() {
$value = false;
if ( Give()->session->has_session() ) {
$value = true;
}
return $value;
}
/**
* Get cookie names
*
* @since 2.2.0
* @access public
*
* @param string $type Nonce type.
*
* @return string Cookie name
*/
public function get_cookie_name( $type = '' ) {
$name = '';
switch ( $type ) {
case 'nonce':
$name = $this->nonce_cookie_name;
break;
case 'session':
$name = $this->cookie_name;
break;
}
return $name;
}
/**
* When a user is logged out, ensure they have a unique nonce by using the donor/session ID.
* Note: for internal logic only.
*
* @since 4.9.0 rename function - PHP 8 compatibility
* @since 2.2.0
* @access public
*
* @param int $uid User ID.
*
* @return string
*/
public function nonce_user_logged_out( $uid ) {
return $this->has_session() && $this->donor_id ? $this->donor_id : $uid;
}
/**
* Cleanup session data from the database and clear caches.
* Note: for internal logic only.
*
* @since 4.9.0 rename function - PHP 8 compatibility
* @since 2.2.0
* @access public
*/
public function cleanup_sessions() { // @codingStandardsIgnoreLine
Give()->session_db->delete_expired_sessions();
}
/**
* Get Session ID
*
* Retrieve session ID.
*
* @since 1.0
* @return string Session ID.
* @deprecated 2.2.0
* @access public
*
*/
public function get_id() {
return $this->get_cookie_name( 'session' );
}
/**
* Set Cookie Variant Time
*
* Force the cookie expiration variant time to custom expiration option, less and hour. defaults to 23 hours
* (set_expiration_variant_time used in WP_Session).
*
* @since 1.0
* @return int
* @deprecated 2.2.0
* @access public
*
*/
public function set_expiration_variant_time() {
return ( ! empty( $this->exp_option ) ? ( intval( $this->exp_option ) - 3600 ) : 30 * 60 * 23 );
}
/**
* Starts a new session if one has not started yet.
*
* Checks to see if the server supports PHP sessions or if the GIVE_USE_PHP_SESSIONS constant is defined.
*
* @since 1.0
* @access public
* @return bool $ret True if we are using PHP sessions, false otherwise.
* @deprecated 2.2.0
*
*/
public function use_php_sessions() {
$ret = false;
give_doing_it_wrong( __FUNCTION__, __( 'We are using database session logic instead of PHP session since GiveWP 2.2.0', 'give' ) );
return (bool) apply_filters( 'give_use_php_sessions', $ret );
}
/**
* Should Start Session
*
* Determines if we should start sessions.
*
* @since 1.4
* @access public
* @return bool
* @deprecated 2.2.0
*
*/
public function should_start_session() {
$start_session = true;
give_doing_it_wrong( __FUNCTION__, __( 'We are using database session logic instead of PHP session since GiveWP 2.2.0', 'give' ) );
if ( ! empty( $_SERVER['REQUEST_URI'] ) ) { // @codingStandardsIgnoreLine
$blacklist = apply_filters(
'give_session_start_uri_blacklist',
[
'feed',
'feed',
'feed/rss',
'feed/rss2',
'feed/rdf',
'feed/atom',
'comments/feed/',
]
);
$uri = ltrim( $_SERVER['REQUEST_URI'], '/' ); // // @codingStandardsIgnoreLine
$uri = untrailingslashit( $uri );
if ( in_array( $uri, $blacklist, true ) ) {
$start_session = false;
}
if ( false !== strpos( $uri, 'feed=' ) ) {
$start_session = false;
}
if ( is_admin() ) {
$start_session = false;
}
}
return apply_filters( 'give_start_session', $start_session );
}
}