-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
eme-functions.php
4137 lines (3823 loc) · 162 KB
/
eme-functions.php
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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
function eme_plugin_url() {
$url = wp_cache_get( 'eme_plugin_url' );
if ( $url === false ) {
$url = plugin_dir_url( __FILE__ );
wp_cache_set( 'eme_plugin_url', $url, '', 60 );
}
return $url;
}
function eme_plugin_dir() {
$dir = wp_cache_get( 'eme_plugin_dir' );
if ( $dir === false ) {
$dir = plugin_dir_path( __FILE__ );
wp_cache_set( 'eme_plugin_dir', $dir, '', 60 );
}
return $dir;
}
add_action( 'wp_ajax_eme_client_clock', 'eme_client_clock_ajax' );
add_action( 'wp_ajax_nopriv_eme_client_clock', 'eme_client_clock_ajax' );
function eme_client_clock_ajax() {
$valid = true;
$ret = '0';
// Set php clock values in an array
$phptime_obj = new ExpressiveDate( 'now', EME_TIMEZONE );
// if clock data not set
if ( ! isset( $_COOKIE['eme_client_time'] ) ) {
// no valid data received
$valid = false;
// ret=1 will cause the client browser to refresh, this will allow the cookie to be read upon refresh (and so no global var is needed)
$ret = '1';
// Preset php clock values in client cookie for fall-back if valid client clock data isn't received.
$client_timeinfo['eme_client_unixtime'] = (int) $phptime_obj->format( 'U' ); // Integer seconds since 1/1/1970 @ 12:00 AM
$client_timeinfo['eme_client_seconds'] = (int) $phptime_obj->format( 's' ); // Integer second this minute (0-59)
$client_timeinfo['eme_client_minutes'] = (int) $phptime_obj->format( 'i' ); // Integer minute this hour (0-59)
$client_timeinfo['eme_client_hours'] = (int) $phptime_obj->format( 'h' ); // Integer hour this day (0-23)
$client_timeinfo['eme_client_wday'] = (int) $phptime_obj->format( 'w' ); // Integer day this week (0-6), 0 = Sunday, ... , 6 = Saturday
$client_timeinfo['eme_client_mday'] = (int) $phptime_obj->format( 'j' ); // Integer day this month 1-31)
$client_timeinfo['eme_client_month'] = (int) $phptime_obj->format( 'n' ); // Integer month this year (1-12)
$client_timeinfo['eme_client_fullyear'] = (int) $phptime_obj->format( 'Y' ); // Integer year (1970-9999)
} else {
try {
$client_timeinfo = eme_sanitize_request( json_decode( $_COOKIE['eme_client_time'], true ) );
if ( ! is_array( $client_timeinfo ) ) {
$client_timeinfo = [];
}
$ret = '0';
} catch ( Exception $error ) {
$client_timeinfo = [];
$valid = false;
}
if ( ! isset( $client_timeinfo['eme_client_unixtime'] ) ) {
$client_timeinfo['eme_client_unixtime'] = (int) $phptime_obj->format( 'U' );
}
if ( ! isset( $client_timeinfo['eme_client_seconds'] ) ) {
$client_timeinfo['eme_client_seconds'] = (int) $phptime_obj->format( 's' );
}
if ( ! isset( $client_timeinfo['eme_client_minutes'] ) ) {
$client_timeinfo['eme_client_minutes'] = (int) $phptime_obj->format( 'i' );
}
if ( ! isset( $client_timeinfo['eme_client_hours'] ) ) {
$client_timeinfo['eme_client_hours'] = (int) $phptime_obj->format( 'h' );
}
if ( ! isset( $client_timeinfo['eme_client_wday'] ) ) {
$client_timeinfo['eme_client_wday'] = (int) $phptime_obj->format( 'w' );
}
if ( ! isset( $client_timeinfo['eme_client_mday'] ) ) {
$client_timeinfo['eme_client_mday'] = (int) $phptime_obj->format( 'j' );
}
if ( ! isset( $client_timeinfo['eme_client_month'] ) ) {
$client_timeinfo['eme_client_month'] = (int) $phptime_obj->format( 'n' );
}
if ( ! isset( $client_timeinfo['eme_client_fullyear'] ) ) {
$client_timeinfo['eme_client_fullyear'] = (int) $phptime_obj->format( 'Y' );
}
}
// Cast client clock values as integers to avoid mathematical errors and set in temporary local variables.
$client_unixtime = isset( $_POST['page'] ) ? intval( $_POST['client_unixtime'] ) : 0;
$client_seconds = isset( $_POST['page'] ) ? intval( $_POST['client_seconds'] ) : 0;
$client_minutes = isset( $_POST['page'] ) ? intval( $_POST['client_minutes'] ) : 0;
$client_hours = isset( $_POST['page'] ) ? intval( $_POST['client_hours'] ) : 0;
$client_wday = isset( $_POST['page'] ) ? intval( $_POST['client_wday'] ) : 0;
$client_mday = isset( $_POST['page'] ) ? intval( $_POST['client_mday'] ) : 0;
$client_month = isset( $_POST['client_month'] ) ? intval( $_POST['client_month'] ) : 0;
$client_fullyear = isset( $_POST['client_fullyear'] ) ? intval( $_POST['client_fullyear'] ) : 0;
// Client clock sanity tests
if ( abs( $client_unixtime - $client_timeinfo['eme_client_unixtime'] ) > 300 ) {
$valid = false; // allow +/-5 min difference
}
if ( abs( $client_seconds - 30 ) > 30 ) {
$valid = false; // Seconds <0 or >60
}
if ( abs( $client_minutes - 30 ) > 30 ) {
$valid = false; // Minutes <0 or >60
}
if ( abs( $client_hours - 12 ) > 12 ) {
$valid = false; // Hours <0 or >24
}
if ( abs( $client_wday - 3 ) > 3 ) {
$valid = false; // Weekday <0 or >6
}
if ( abs( $client_mday - $client_timeinfo['eme_client_mday'] ) > 30 ) {
$valid = false; // >30 day difference
}
if ( abs( $client_month - $client_timeinfo['eme_client_month'] ) > 11 ) {
$valid = false; // >11 month difference
}
if ( abs( $client_fullyear - $client_timeinfo['eme_client_fullyear'] ) > 1 ) {
$valid = false; // >1 year difference
}
// To insure mutual consistency, don't use any client values unless they all passed the tests.
if ( $valid ) {
$client_timeinfo['eme_client_unixtime'] = $client_unixtime;
$client_timeinfo['eme_client_seconds'] = $client_seconds;
$client_timeinfo['eme_client_minutes'] = $client_minutes;
$client_timeinfo['eme_client_hours'] = $client_hours;
$client_timeinfo['eme_client_wday'] = $client_wday;
$client_timeinfo['eme_client_mday'] = $client_mday;
$client_timeinfo['eme_client_month'] = $client_month;
$client_timeinfo['eme_client_fullyear'] = $client_fullyear;
}
// client cookie lifetime = 0 (the session)
// the cookie is stored using wp_json_encode and not eme_serialize, to avoid for Object Injection
// See https://www.owasp.org/index.php/PHP_Object_Injection
setcookie( 'eme_client_time', wp_json_encode( $client_timeinfo ), 0, COOKIEPATH, COOKIE_DOMAIN );
echo $ret;
wp_die();
}
function eme_captcha_generate( $file ) {
// 23 letters (not the "l",o","q")
$alphabet = 'abcdefghjkmnpqrstuvwxyz';
$random1 = substr( $alphabet, rand( 1, 23 ) - 1, 1 );
$random2 = rand( 2, 9 );
$rand = rand( 1, 23 ) - 1;
$random3 = substr( $alphabet, rand( 1, 23 ) - 1, 1 );
$random4 = rand( 2, 9 );
$rand = rand( 1, 23 ) - 1;
$random5 = substr( $alphabet, rand( 1, 23 ) - 1, 1 );
$randomtext = $random1 . $random2 . $random3 . $random4 . $random5;
// strtolower not needed, $alphabet lowercase only
//$randomtext=strtolower($randomtext);
$res = 'eme_captcha_' . $file . '-' . md5( $randomtext );
touch( get_temp_dir() . $res );
$im = imagecreatetruecolor( 120, 38 );
// some colors
$white = imagecolorallocate( $im, 255, 255, 255 );
$grey = imagecolorallocate( $im, 128, 128, 128 );
$black = imagecolorallocate( $im, 0, 0, 0 );
$red = imagecolorallocate( $im, 255, 0, 0 );
$blue = imagecolorallocate( $im, 0, 0, 255 );
$green = imagecolorallocate( $im, 0, 255, 0 );
$background_colors = [ $red, $blue, $green, $black ];
// draw rectangle in random color
$background_color = $background_colors[ rand( 0, 3 ) ];
imagefilledrectangle( $im, 0, 0, 120, 38, $background_color );
// replace font.ttf with the location of your own ttf font file
$eme_plugin_dir = eme_plugin_dir();
$font = $eme_plugin_dir . 'font.ttf';
if ( function_exists( 'imagettftext' ) ) {
// add shadow
imagettftext( $im, 25, 8, 15, 28, $grey, $font, $random1 );
imagettftext( $im, 25, -8, 35, 28, $grey, $font, $random2 );
imagettftext( $im, 25, 8, 55, 28, $grey, $font, $random3 );
imagettftext( $im, 25, -8, 75, 28, $grey, $font, $random4 );
imagettftext( $im, 25, 8, 95, 28, $grey, $font, $random5 );
// add text
imagettftext( $im, 25, 8, 8, 30, $white, $font, $random1 );
imagettftext( $im, 25, -8, 28, 30, $white, $font, $random2 );
imagettftext( $im, 25, 8, 48, 30, $white, $font, $random3 );
imagettftext( $im, 25, -8, 68, 30, $white, $font, $random4 );
imagettftext( $im, 25, 8, 88, 30, $white, $font, $random5 );
} else {
// add shadow
imagestring( $im, 5, 15, 5, $random1, $grey );
imagestring( $im, 5, 35, 5, $random2, $grey );
imagestring( $im, 5, 55, 5, $random3, $grey );
imagestring( $im, 5, 75, 5, $random4, $grey );
imagestring( $im, 5, 95, 5, $random5, $grey );
// add text
imagestring( $im, 5, 15, 7, $random1, $white );
imagestring( $im, 5, 35, 7, $random2, $white );
imagestring( $im, 5, 55, 7, $random3, $white );
imagestring( $im, 5, 75, 7, $random4, $white );
imagestring( $im, 5, 95, 7, $random5, $white );
}
// give image back
header( 'Content-type: image/gif' );
imagegif( $im );
imagedestroy( $im );
exit;
}
function eme_load_captcha_html() {
$captcha_id = eme_random_id();
$captcha_url = eme_captcha_url( $captcha_id );
if ( $captcha_url ) {
$replacement = "<input type='hidden' name='eme_captcha_id' value='$captcha_id'><img id='eme_captcha_img' src='$captcha_url'><br><input required='required' type='text' name='captcha_check' id='captcha_check' autocomplete='off' class='nodynamicupdates'>";
} else {
$replacement = __( 'Problem while generating the captcha, please check with the site administrator', 'events-made-easy' );
}
return $replacement;
}
function eme_load_recaptcha_html() {
if ( ! wp_script_is( 'eme-recaptcha', 'enqueued' ) ) {
wp_enqueue_script( 'eme-recaptcha' );
}
$eme_recaptcha_sitekey = get_option( 'eme_recaptcha_site_key' );
return '<!-- Google reCAPTCHA widget -->
<div class="g-recaptcha" data-sitekey="' . $eme_recaptcha_sitekey . '"></div>';
}
function eme_load_hcaptcha_html() {
if ( ! wp_script_is( 'eme-hcaptcha', 'enqueued' ) ) {
wp_enqueue_script( 'eme-hcaptcha' );
}
$eme_hcaptcha_sitekey = get_option( 'eme_hcaptcha_site_key' );
return '<!-- hCaptcha widget -->
<div class="h-captcha" data-sitekey="' . $eme_hcaptcha_sitekey . '"></div>';
}
function eme_load_cfcaptcha_html() {
if ( ! wp_script_is( 'eme-cfcaptcha', 'enqueued' ) ) {
wp_enqueue_script( 'eme-cfcaptcha' );
}
$eme_cfcaptcha_sitekey = get_option( 'eme_cfcaptcha_site_key' );
return '<!-- Cloudflare Turnstile Captcha widget -->
<div class="cf-turnstile" data-sitekey="' . $eme_cfcaptcha_sitekey . '"></div>';
}
function eme_check_recaptcha() {
$eme_recaptcha = get_option( 'eme_recaptcha_for_forms' );
$eme_recaptcha_key = get_option( 'eme_recaptcha_secret_key' );
if ( isset( $_POST['g-recaptcha-response'] ) && ! empty( $eme_recaptcha_key ) && $eme_recaptcha ) {
$url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . $eme_recaptcha_key . '&response=' . eme_sanitize_request( $_POST['g-recaptcha-response'] );
$response = wp_remote_get( $url );
if ( is_wp_error( $response ) ) {
return false;
} else {
$body = wp_remote_retrieve_body( $response );
$responseCaptchaData = json_decode( $body );
if ( $responseCaptchaData->success ) {
return true;
} else {
return false;
}
}
} else {
return false;
}
}
function eme_check_hcaptcha() {
$eme_hcaptcha = get_option( 'eme_hcaptcha_for_forms' );
$eme_hcaptcha_key = get_option( 'eme_hcaptcha_secret_key' );
if ( isset( $_POST['h-captcha-response'] ) && ! empty( $eme_hcaptcha_key ) && $eme_hcaptcha ) {
$url = 'https://hcaptcha.com/siteverify';
$data = [
'secret' => $eme_hcaptcha_key,
'response' => eme_sanitize_request( $_POST['h-captcha-response'] ),
];
$response = wp_remote_post( $url, [ 'body' => $data ] );
if ( is_wp_error( $response ) ) {
return false;
} else {
$body = wp_remote_retrieve_body( $response );
$responseCaptchaData = json_decode( $body );
if ( $responseCaptchaData->success ) {
return true;
} else {
return false;
}
}
} else {
return false;
}
}
function eme_check_cfcaptcha() {
$eme_cfcaptcha = get_option( 'eme_cfcaptcha_for_forms' );
$eme_cfcaptcha_key = get_option( 'eme_cfcaptcha_secret_key' );
if ( isset( $_POST['cf-turnstile-response'] ) && ! empty( $eme_cfcaptcha_key ) && $eme_cfcaptcha ) {
$url = 'https://challenges.cloudflare.com/turnstile/v0/siteverify';
$data = [
'secret' => $eme_cfcaptcha_key,
'response' => eme_sanitize_request( $_POST['cf-turnstile-response'] ),
];
$response = wp_remote_post( $url, [ 'body' => $data ] );
if ( is_wp_error( $response ) ) {
return false;
} else {
$body = wp_remote_retrieve_body( $response );
$responseCaptchaData = json_decode( $body );
if ( $responseCaptchaData->success ) {
return true;
} else {
return false;
}
}
} else {
return false;
}
}
function eme_check_emecaptcha( $remove_upon_success = 0 ) {
if ( empty( $_POST['eme_captcha_id'] ) ) {
return false;
}
if ( empty( $_POST['captcha_check'] ) ) {
return false;
}
$captcha_id = eme_sanitize_filenamechars( $_POST['eme_captcha_id'] );
if ( empty( $captcha_id ) ) {
return false;
}
// make it a bit easier for people and lowercase their response
$captcha_response = eme_sanitize_filenamechars( strtolower( $_POST['captcha_check'] ) );
if ( empty( $captcha_response ) ) {
return false;
}
$res = get_temp_dir() . 'eme_captcha_' . $captcha_id . '-' . md5( $captcha_response );
// normally the captcha gets removed upon validation
// sometimes you do other form validations later on, and then this is not desired
// because if other form validation would fail, the captcha would still be gone
// and people would need to re-enter it
// In such cases, call eme_captcha_remove on the file later on
// (although there's also a cron that removes captchas older than xxx minutes, so no real requirement)
if ( file_exists( $res ) ) {
if ( $remove_upon_success ) {
wp_delete_file( $res );
return true;
} else {
return $res;
}
} else {
return false;
}
}
function eme_check_captcha( $properties = [], $remove_captcha_if_ok = 0 ) {
$captcha_res = false;
if ( (! empty( $properties ) && !empty($properties['captcha_only_logged_out'])) ||
(empty( $properties ) && get_option( 'eme_captcha_only_logged_out' ) ) ) {
if (is_user_logged_in()) {
return true;
}
}
$configured_captchas = eme_get_configured_captchas();
if (! empty( $properties ) && isset($properties['selected_captcha']))
$selected_captcha = $properties['selected_captcha'];
else
$selected_captcha = '';
if ( ( ! empty( $properties ) && $selected_captcha == 'recaptcha' ) || ( empty( $properties ) && isset( $configured_captchas['recaptcha'] ) ) ) {
$captcha_res = eme_check_recaptcha();
if ( ! $captcha_res ) {
$message = esc_html__( 'Please check the Google reCAPTCHA box', 'events-made-easy' );
echo wp_json_encode(
[
'Result' => 'NOK',
'htmlmessage' => $message,
]
);
wp_die();
}
}
if ( ( ! empty( $properties ) && $selected_captcha == 'hcaptcha' ) || ( empty( $properties ) && isset( $configured_captchas['hcaptcha'] ) ) ) {
$captcha_res = eme_check_hcaptcha();
if ( ! $captcha_res ) {
$message = esc_html__( 'Please check the hCaptcha box', 'events-made-easy' );
echo wp_json_encode(
[
'Result' => 'NOK',
'htmlmessage' => $message,
]
);
wp_die();
}
}
if ( ( ! empty( $properties ) && $selected_captcha == 'cfcaptcha' ) || ( empty( $properties ) && isset( $configured_captchas['cfcaptcha'] ) ) ) {
$captcha_res = eme_check_cfcaptcha();
if ( ! $captcha_res ) {
$message = esc_html__( 'Please check the Cloudflare Turnstile box', 'events-made-easy' );
echo wp_json_encode(
[
'Result' => 'NOK',
'htmlmessage' => $message,
]
);
wp_die();
}
}
if ( ( ! empty( $properties ) && $selected_captcha == 'captcha' ) || ( empty( $properties ) && isset( $configured_captchas['captcha'] ) ) ) {
$captcha_res = eme_check_emecaptcha( $remove_captcha_if_ok );
if ( ! $captcha_res ) {
$message = esc_html__( 'You entered an incorrect code', 'events-made-easy' );
echo wp_json_encode(
[
'Result' => 'NOK',
'htmlmessage' => $message,
]
);
wp_die();
}
}
return $captcha_res;
}
function eme_generate_captchas_html($selected_captcha = '') {
if ( eme_is_admin_request() ) {
return '';
}
if ( get_option( 'eme_captcha_only_logged_out' ) && is_user_logged_in() ) {
return '';
}
$configured_captchas = eme_get_configured_captchas();
if (!empty($configured_captchas)) {
if (empty($selected_captcha))
$selected_captcha = array_key_first($configured_captchas);
elseif (!empty($selected_captcha) && !array_key_exists($selected_captcha, $configured_captchas))
$selected_captcha = array_key_first($configured_captchas);
$captcha_function = 'eme_load_'.$selected_captcha.'_html';
if (function_exists($captcha_function))
return $captcha_function();
}
}
function eme_add_captcha_submit( $format, $captcha = '', $add_dyndata = 0 ) {
if ( $add_dyndata && ! preg_match( '/#_DYNAMICDATA/', $format ) ) {
$text = '#_DYNAMICDATA';
if ( preg_match( '/#_SUBMIT/', $format ) ) {
$format = preg_replace( '/#_SUBMIT/', "$text<br>#_SUBMIT", $format );
} else {
$format .= $text;
}
}
$configured_captchas = eme_get_configured_captchas();
if (empty($configured_captchas)) {
$captcha = "";
} elseif (!empty($captcha) && !array_key_exists($captcha, $configured_captchas)) {
$captcha = array_key_first($configured_captchas);
}
if ( !empty($captcha) && ! preg_match( '/#_CFCAPTCHA|#_HCAPTCHA|#_RECAPTCHA|#_CAPTCHA/', $format ) ) {
if ($captcha == 'captcha')
$captcha_text = '<p>' . __( 'Please enter the displayed code.', 'events-made-easy' ) . '</p> #_CAPTCHA';
else
$captcha_text = '#_CAPTCHA';
if ( preg_match( '/#_SUBMIT/', $format ) ) {
$format = preg_replace( '/#_SUBMIT/', "$captcha_text<br>#_SUBMIT", $format );
} else {
$format .= $captcha_text;
}
}
if ( ! preg_match( '/#_SUBMIT/', $format ) ) {
$format .= '<br>#_SUBMIT';
}
return $format;
}
function eme_captcha_remove( $captcha_file ) {
if ( ! empty( $captcha_file ) && is_string( $captcha_file ) && file_exists( $captcha_file ) ) {
wp_delete_file( $captcha_file );
}
}
function eme_if_shortcode($atts, $content) {
$atts = shortcode_atts(
[
'tag' => '',
'value' => '',
'eq' => '',
'notvalue' => '',
'ne' => '',
'lt' => '',
'le' => '',
'gt' => '',
'ge' => '',
'contains' => '',
'notcontains' => '',
'is_empty' => 0,
'incsv' => '',
'notincsv' => '',
],
$atts
);
$tag = eme_replace_generic_placeholders($atts['tag']);
$content = eme_replace_generic_placeholders($content);
if ($atts['is_empty']) {
if (empty($tag)) {
return do_shortcode($content);
}
} elseif (is_numeric($atts['value']) || !empty($atts['value'])) {
if ($tag == $atts['value']) {
return do_shortcode($content);
}
} elseif (is_numeric($atts['eq']) || !empty($atts['eq'])) {
if ($tag == $atts['eq']) {
return do_shortcode($content);
}
} elseif (is_numeric($atts['ne']) || !empty($atts['ne'])) {
if ($tag != $atts['ne']) {
return do_shortcode($content);
}
} elseif (is_numeric($atts['notvalue']) || !empty($atts['notvalue'])) {
if ($tag != $atts['notvalue']) {
return do_shortcode($content);
}
} elseif (is_numeric($atts['lt']) || !empty($atts['lt'])) {
if ($tag < $atts['lt']) {
return do_shortcode($content);
}
} elseif (is_numeric($atts['le']) || !empty($atts['le'])) {
if ($tag <= $atts['le']) {
return do_shortcode($content);
}
} elseif (is_numeric($atts['gt']) || !empty($atts['gt'])) {
if ($tag > $atts['gt']) {
return do_shortcode($content);
}
} elseif (is_numeric($atts['ge']) || !empty($atts['ge'])) {
if ($tag >= $atts['ge']) {
return do_shortcode($content);
}
} elseif (is_numeric($atts['contains']) || !empty($atts['contains'])) {
if (strpos($tag, $atts['contains']) !== false) {
return do_shortcode($content);
}
} elseif (is_numeric($atts['notcontains']) || !empty($atts['notcontains'])) {
if (strpos($tag, $atts['notcontains']) === false) {
return do_shortcode($content);
}
} elseif (is_numeric($atts['incsv']) || !empty($atts['incsv'])) {
if (preg_match('/,/', $atts['incsv'])) {
$incsv_arr = explode(',', $atts['incsv']);
foreach ($incsv_arr as $incsv) {
$incsv = trim($incsv);
if (in_array($incsv, explode(',', $tag)) || in_array($incsv, explode(', ', $tag))) {
return do_shortcode($content);
}
}
} elseif (preg_match('/\+/', $atts['incsv'])) {
$incsv_arr = preg_split('/\+/', $atts['incsv'], 0, PREG_SPLIT_NO_EMPTY);
$found = 1;
foreach ($incsv_arr as $incsv) {
$incsv = trim($incsv);
if (!(in_array($incsv, explode(',', $tag)) || in_array($incsv, explode(', ', $tag)))) {
$found = 0;
}
}
if ($found) {
return do_shortcode($content);
}
} else {
$incsv = trim($atts['incsv']);
if (in_array($incsv, explode(',', $tag)) || in_array($incsv, explode(', ', $tag))) {
return do_shortcode($content);
}
}
} elseif (is_numeric($atts['notincsv']) || !empty($atts['notincsv'])) {
if (preg_match('/,/', $atts['notincsv'])) {
$notincsv_arr = explode(',', $atts['notincsv']);
$found = 0;
foreach ($notincsv_arr as $notincsv) {
$notincsv = trim($notincsv);
if (in_array($notincsv, explode(',', $tag)) || in_array($notincsv, explode(', ', $tag))) {
$found = 1;
}
}
if (!$found) {
return do_shortcode($content);
}
} elseif (preg_match('/\+/', $atts['notincsv'])) {
$notincsv_arr = preg_split('/\+/', $atts['notincsv'], 0, PREG_SPLIT_NO_EMPTY);
$found = 0;
foreach ($notincsv_arr as $notincsv) {
$notincsv = trim($notincsv);
if (in_array($notincsv, explode(',', $tag)) || in_array($notincsv, explode(', ', $tag))) {
$found = 1;
} else {
$found = 0;
}
}
if (!$found) {
return do_shortcode($content);
}
} else {
$notincsv = trim($atts['notincsv']);
if (!(in_array($notincsv, explode(',', $tag)) || in_array($notincsv, explode(', ', $tag)))) {
return do_shortcode($content);
}
}
} elseif (!empty($tag)) {
return do_shortcode($content);
}
// return empty in all other cases
return '';
}
function eme_for_shortcode( $atts, $content ) {
$atts = shortcode_atts(
[
'min' => 1,
'max' => 0,
'list' => '',
'sep' => ',',
],
$atts
);
$min = intval( $atts['min'] );
$max = intval( $atts['max'] );
$result = '';
$loopcounter = 1;
if ( ! empty( $atts['list'] ) ) {
$list = eme_replace_generic_placeholders($atts['list']); // currently mainly usefull to loop over all categories when using #_ALLCATEGORYIDS
$search = [ '#_LOOPVALUE', '#URL_LOOPVALUE', '#_LOOPCOUNTER' ];
$array_values = explode( $atts['sep'], $list );
foreach ( $array_values as $val ) {
$url_val = rawurlencode( $val );
$esc_val = eme_sanitize_request( eme_esc_html( preg_replace( '/\n|\r/', '', $val ) ) );
$replace = [ $esc_val, $url_val, $loopcounter ];
$tmp_content = str_replace( $search, $replace, $content );
$result .= do_shortcode( $tmp_content );
++$loopcounter;
}
} else {
$search = [ '#_LOOPVALUE', '#_LOOPCOUNTER' ];
while ( $min <= $max ) {
$replace = [ $min, $loopcounter ];
$tmp_content = str_replace( $search, $replace, $content );
$result .= do_shortcode( $tmp_content );
++$min;
++$loopcounter;
}
}
return $result;
}
// Returns true if the page in question is the events page
function eme_is_events_page() {
$events_page_id = eme_get_events_page_id();
if ( $events_page_id ) {
return is_page( $events_page_id );
} else {
return false;
}
}
function eme_get_events_page_id() {
// in theory every call to get_option is cached, but since we also check for polylang translation issues
// we don't want to call the pll_ functions too often ...
$page_id = wp_cache_get( "eme_page_id" );
if ( $page_id === false ) {
$page_id = get_option( 'eme_events_page' );
// if a language is set, then remove it
// this needs a bit optimising so it is not executed each time though ...
if ( function_exists( 'pll_get_post_language' ) ) {
$post_lang = pll_get_post_language($page_id);
if (!empty($post_lang)) {
pll_set_post_language($page_id,'');
}
}
wp_cache_set( "eme_page_id", $page_id, '', 5 );
}
return $page_id;
}
function eme_get_events_page( $justurl = 1, $text = '' ) {
$events_page_id = eme_get_events_page_id();
if (!empty($events_page_id)) {
$page_link = get_permalink( $events_page_id );
} else {
$page_link = trailingslashit( home_url() );
}
if ( $justurl || empty( $text ) ) {
$result = $page_link;
} else {
$text = eme_esc_html( $text );
$result = "<a href='$page_link' title='$text'>$text</a>";
}
return $result;
}
function eme_is_single_day_page() {
return ( eme_is_events_page() && ! empty( get_query_var( 'calendar_day' ) ) );
}
function eme_is_single_event_page() {
return ( eme_is_events_page() && ! empty( get_query_var( 'event_id' ) ) );
}
function eme_is_single_location_page() {
return ( eme_is_events_page() && ! empty( get_query_var( 'location_id' ) ) && empty( get_query_var( 'calendar_day' ) ) );
}
function eme_get_contact( $contact_id = 0 ) {
$userinfo = false;
if ($contact_id > 0 ) {
$userinfo = get_userdata( $contact_id );
}
// suppose the user has been deleted ...
if ( $userinfo === false ) {
$contact_id = intval(get_option( 'eme_default_contact_person',0) );
$userinfo = get_userdata( $contact_id );
}
// still false? Means the user doesn't exist and the default contact person neither
if ( $userinfo === false ) {
if ( function_exists( 'is_multisite' ) && is_multisite() ) {
$thisblog = get_current_blog_id();
$userinfo = get_user_by( 'email', get_blog_option( $thisblog, 'admin_email' ) );
} else {
$userinfo = get_user_by( 'email', get_option( 'admin_email' ) );
}
// still nothing ? Can be if the main admin email is not matching a user, so get the first admin
if ( ! $userinfo ) {
$user_query = new WP_User_Query( array( 'role' => 'administrator', 'number' => 1 ) );
$users = $user_query->get_results(); // array of WP_User objects, like get_users
$userinfo = $users[0];
}
}
return $userinfo;
}
function eme_get_event_contact( $event = null ) {
if ( ! is_null( $event ) && ! empty( $event['event_contactperson_id'] ) ) {
$contact_id = $event['event_contactperson_id'];
if ( $contact_id < 1 && isset( $event['event_author'] ) && $event['event_author'] > 0 ) {
$contact_id = $event['event_author'];
}
} else {
$contact_id = intval(get_option( 'eme_default_contact_person',0) );
}
return eme_get_contact( $contact_id );
}
function eme_get_author( $event ) {
$author_id = $event['event_author'];
if ( $author_id < 1 ) {
if ( function_exists( 'is_multisite' ) && is_multisite() ) {
$thisblog = get_current_blog_id();
$userinfo = get_user_by( 'email', get_blog_option( $thisblog, 'admin_email' ) );
} else {
$userinfo = get_user_by( 'email', get_option( 'admin_email' ) );
}
#$contact_id = get_current_user_id();
} else {
$userinfo = get_userdata( $author_id );
}
return $userinfo;
}
function eme_get_user_phone( $user_id ) {
return get_user_meta( $user_id, 'eme_phone', true );
}
function eme_update_user_phone( $user_id, $phone ) {
update_user_meta( $user_id, 'eme_phone', $phone );
}
// got from http://davidwalsh.name/php-email-encode-prevent-spam
function eme_ascii_encode( $e, $target = 'html' ) {
return eme_email_obfuscate( $e, $target );
}
function eme_email_obfuscate( $e, $target = 'html' ) {
if ( $target == 'htmlmail' ) {
return eme_esc_html( $e );
} elseif ( $target == 'text' ) {
return $e;
} else {
$output = '';
if ( has_filter( 'eme_email_obfuscate_filter' ) ) {
$output = apply_filters( 'eme_email_obfuscate_filter', $e );
} else {
for ( $i = 0; $i < strlen( $e ); $i++ ) {
$output .= '&#' . ord( $e[ $i ] ) . ';';
}
}
return $output;
}
}
function eme_unique_slug( $slug, $table, $column, $index_col, $index_colval = 0 ) {
global $wpdb;
$table_name = EME_DB_PREFIX . $table;
$slug = untrailingslashit( $slug );
// code taken from function wp_unique_term_slug
$query = $wpdb->prepare( "SELECT $column FROM $table_name WHERE $index_col<> $index_colval AND $column = %s", $slug );
// it exists .... so strip of the last numbers and start finding new ones
if ( $wpdb->get_var( $query ) ) {
$slug = preg_replace( '/\-\d+$/', '', $slug );
$num = 2;
do {
$alt_slug = $slug . "-$num";
++$num;
$slug_check = $wpdb->get_var( $wpdb->prepare( "SELECT $column FROM $table_name WHERE $index_col<> $index_colval AND $column = %s", $alt_slug ) );
} while ( $slug_check );
$slug = $alt_slug;
}
return $slug;
}
function eme_permalink_convert_noslash( $val ) {
// return the permalink without trailing slash
return eme_permalink_convert( $val, 0 );
}
function eme_permalink_convert( $val, $trailing_slash = 1 ) {
// WP provides a function to convert accents to their ascii counterparts
// called remove_accents, but we also want to replace spaces with "-"
// and trim the last space. sanitize_title_with_dashes does all that
// first call untrailingslashit to remove a trailing '/' if at all present in the value
$val = sanitize_title_with_dashes( remove_accents( untrailingslashit( $val ) ) );
// now remove anything that would be a html char
// from https://stackoverflow.com/questions/2103797/url-friendly-username-in-php
$val = strtolower( trim( preg_replace( '~[^0-9a-z]+~i', '-', html_entity_decode( preg_replace( '~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities( $val, ENT_QUOTES, 'UTF-8' ) ), ENT_QUOTES, 'UTF-8' ) ), '-' ) );
// and then, add a trailing slash
if ( $trailing_slash ) {
return trailingslashit( $val );
} else {
return $val;
}
}
function eme_event_url( $event, $language = '' ) {
global $wp_rewrite;
if ( $event['event_url'] != '' && get_option( 'eme_use_external_url' ) && eme_is_url( $event['event_url'] ) ) {
$the_link = $event['event_url'];
$parsed = parse_url( $the_link );
if ( empty( $parsed['scheme'] ) ) {
$the_link = 'https://' . ltrim( $the_link, '/' );
}
$the_link = esc_url( $the_link );
} else {
if ( empty( $language ) ) {
$language = eme_detect_lang();
}
if ( isset( $wp_rewrite ) && $wp_rewrite->using_permalinks() && get_option( 'eme_seo_permalink' ) ) {
$events_prefixes = explode( ',', get_option( 'eme_permalink_events_prefix', 'events' ) );
if ( ! empty( $event['event_prefix'] ) && in_array( $event['event_prefix'], $events_prefixes ) ) {
$events_prefix = eme_permalink_convert( $event['event_prefix'] );
} else {
$events_prefix = eme_permalink_convert( $events_prefixes[0] );
}
if ( empty( $event['event_slug'] ) ) {
$name = $events_prefix . $event['event_id'] . '/' . eme_permalink_convert_noslash( $event['event_name'] );
} elseif ( substr( $event['event_slug'], -1 ) == '/' ) { // old style stuff
$name = $events_prefix . $event['event_id'] . '/' . eme_permalink_convert_noslash( $event['event_slug'] );
} else {
$name = $events_prefix . eme_permalink_convert_noslash( $event['event_slug'] );
}
$the_link = eme_uri_add_lang( $name, $language );
} else {
$the_link = eme_get_events_page();
$the_link = add_query_arg( [ 'event_id' => $event['event_id'] ], $the_link );
if ( ! empty( $language ) ) {
// some plugins add the lang info to the home_url, remove it so we don't get into trouble or add it twice
$the_link = remove_query_arg( 'lang', $the_link );
$the_link = add_query_arg( [ 'lang' => $language ], $the_link );
}
}
}
return $the_link;
}
function eme_location_url( $location, $language = '' ) {
global $wp_rewrite;
$the_link = '';
if ( $location['location_url'] != '' && ( get_option( 'eme_use_external_url' ) || $location['location_properties']['online_only'] ) && eme_is_url( $location['location_url'] ) ) {
$the_link = $location['location_url'];
$parsed = parse_url( $the_link );
if ( empty( $parsed['scheme'] ) ) {
$the_link = 'https://' . ltrim( $the_link, '/' );
}
$the_link = esc_url( $the_link );
} elseif ( isset( $location['location_id'] ) && isset( $location['location_name'] ) ) {
if ( empty( $language ) ) {
$language = eme_detect_lang();
}
if ( isset( $wp_rewrite ) && $wp_rewrite->using_permalinks() && get_option( 'eme_seo_permalink' ) ) {
$locations_prefixes = explode( ',', get_option( 'eme_permalink_locations_prefix', 'locations' ) );
if ( ! empty( $location['location_prefix'] ) && in_array( $location['location_prefix'], $locations_prefixes ) ) {
$locations_prefix = eme_permalink_convert( $location['location_prefix'] );
} else {
$locations_prefix = eme_permalink_convert( $locations_prefixes[0] );
}
if ( empty( $location['location_slug'] ) ) {
$name = $locations_prefix . $location['location_id'] . '/' . eme_permalink_convert_noslash( $location['location_name'] );
} elseif ( substr( $location['location_slug'], -1 ) == '/' ) {
$name = $locations_prefix . $location['location_id'] . '/' . eme_permalink_convert_noslash( $location['location_slug'] );
} else {
$name = $locations_prefix . eme_permalink_convert_noslash( $location['location_slug'] );
}
$the_link = eme_uri_add_lang( $name, $language );
} else {
$the_link = eme_get_events_page();
$the_link = add_query_arg( [ 'location_id' => $location['location_id'] ], $the_link );
if ( ! empty( $language ) ) {
// some plugins add the lang info to the home_url, remove it so we don't get into trouble or add it twice
$the_link = remove_query_arg( 'lang', $the_link );
$the_link = add_query_arg( [ 'lang' => $language ], $the_link );
}
}
}
return $the_link;
}
function eme_calendar_day_url( $day ) {
global $wp_rewrite;
$language = eme_detect_lang();
if ( isset( $wp_rewrite ) && $wp_rewrite->using_permalinks() && get_option( 'eme_seo_permalink' ) ) {
$cal_prefix_option = get_option( 'eme_permalink_calendar_prefix', '' );
if ( empty( $cal_prefix_option ) ) {
$events_prefixes = explode( ',', get_option( 'eme_permalink_events_prefix', 'events' ) );
$cal_prefix = eme_permalink_convert( $events_prefixes[0] );
} else {
$cal_prefix = eme_permalink_convert( $cal_prefix_option );
}
$name = $cal_prefix . eme_permalink_convert_noslash( $day );
$the_link = eme_uri_add_lang( $name, $language );
} else {
$the_link = eme_get_events_page();
$the_link = add_query_arg( [ 'calendar_day' => $day ], $the_link );
if ( ! empty( $language ) ) {
// some plugins add the lang info to the home_url, remove it so we don't get into trouble or add it twice
$the_link = remove_query_arg( 'lang', $the_link );
$the_link = add_query_arg( [ 'lang' => $language ], $the_link );
}
}
return $the_link;
}
function eme_booking_confirm_url( $payment ) {
// the user confirm url is actually going to the payment page as well, but we use another permalink to be nicer to people
global $wp_rewrite;
$language = eme_detect_lang();
if ( isset( $wp_rewrite ) && $wp_rewrite->using_permalinks() && get_option( 'eme_seo_permalink' ) ) {
$events_prefixes = explode( ',', get_option( 'eme_permalink_events_prefix', 'events' ) );
$confirm_prefix = eme_permalink_convert( $events_prefixes[0] ) . 'confirm/';
$name = $confirm_prefix . $payment['random_id'];
$the_link = eme_uri_add_lang( $name, $language );
} else {
$the_link = eme_get_events_page();
$the_link = add_query_arg(
[
'eme_pmt_rndid' => $payment['random_id'],
'eme_rsvp_confirm' => 1,
],
$the_link
);
if ( ! empty( $language ) ) {
// some plugins add the lang info to the home_url, remove it so we don't get into trouble or add it twice
$the_link = remove_query_arg( 'lang', $the_link );
$the_link = add_query_arg( [ 'lang' => $language ], $the_link );
}
}
return $the_link;
}
function eme_payment_url( $payment, $resultcode = 0 ) {
global $wp_rewrite;
$language = eme_detect_lang();
if ( isset( $wp_rewrite ) && $wp_rewrite->using_permalinks() && get_option( 'eme_seo_permalink' ) ) {
$payments_prefix_option = get_option( 'eme_permalink_payments_prefix', '' );
if ( empty( $payments_prefix_option ) ) {
$events_prefixes = explode( ',', get_option( 'eme_permalink_events_prefix', 'events' ) );
$payments_prefix = eme_permalink_convert( $events_prefixes[0] ) . 'p/';
} else {