This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsi-captcha.php
1322 lines (1089 loc) · 57.7 KB
/
si-captcha.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
/*
Plugin Name: SI CAPTCHA Anti-Spam
Plugin URI: http://www.642weather.com/weather/scripts-wordpress-captcha.php
Description: Adds CAPTCHA anti-spam methods to WordPress forms for comments, registration, lost password, login, or all. This prevents spam from automated bots. WP, WPMU, and BuddyPress compatible. <a href="plugins.php?page=si-captcha-for-wordpress/si-captcha.php">Settings</a> | <a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=KXJWLPPWZG83S">Donate</a>
Version: 2.7.7.5
Author: Mike Challis
Author URI: http://www.642weather.com/weather/scripts.php
*/
$si_captcha_version = '2.7.7.5';
/* Copyright (C) 2008-2014 Mike Challis (http://www.642weather.com/weather/contact_us.php)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// settings get deleted when plugin is deleted from admin plugins page
// this must be outside the class or it does not work
function si_captcha_unset_options() {
if (basename(dirname(__FILE__)) != "mu-plugins")
delete_option('si_captcha');
}
if (!class_exists('siCaptcha')) {
class siCaptcha {
var $si_captcha_add_script;
var $si_captcha_version;
function si_captcha_add_tabs() {
global $wpmu, $wp_version;
// for WP 3.0+ ONLY!
if( $wpmu == 1 && version_compare($wp_version,'3','>=') && is_multisite() && is_super_admin() ) { // wp 3.0 +
add_submenu_page('ms-admin.php', __('SI Captcha Options', 'si-captcha'), __('SI Captcha Options', 'si-captcha'), 'manage_options', __FILE__,array(&$this,'si_captcha_options_page'));
add_options_page( __('SI Captcha Options', 'si-captcha'), __('SI Captcha Options', 'si-captcha'), 'manage_options', __FILE__,array(&$this,'si_captcha_options_page'));
}
else if ($wpmu == 1 && function_exists('is_site_admin') && is_site_admin()) {
add_submenu_page('wpmu-admin.php', __('SI Captcha Options', 'si-captcha'), __('SI Captcha Options', 'si-captcha'), 'manage_options', __FILE__,array(&$this,'si_captcha_options_page'));
add_options_page( __('SI Captcha Options', 'si-captcha'), __('SI Captcha Options', 'si-captcha'), 'manage_options', __FILE__,array(&$this,'si_captcha_options_page'));
}
else if ($wpmu != 1) {
add_submenu_page('plugins.php', __('SI Captcha Options', 'si-captcha'), __('SI Captcha Options', 'si-captcha'), 'manage_options', __FILE__,array(&$this,'si_captcha_options_page'));
}
}
function si_captcha_get_options() {
global $wpmu, $si_captcha_opt, $si_captcha_option_defaults;
$default_position = ( function_exists('bp_loaded') ) ? 'label-required-input' : 'input-label-required';
$si_captcha_option_defaults = array(
'si_captcha_donated' => 'false',
'si_captcha_perm' => 'true',
'si_captcha_perm_level' => 'read',
'si_captcha_comment' => 'true',
'si_captcha_comment_label_position' => $default_position,
'si_captcha_login' => 'false',
'si_captcha_register' => 'true',
'si_captcha_lostpwd' => 'true',
'si_captcha_rearrange' => 'true',
'si_captcha_enable_session' => 'false',
'si_captcha_captcha_small' => 'false',
'si_captcha_honeypot_enable' => 'false',
'si_captcha_aria_required' => 'false',
'si_captcha_external_style' => 'false',
'si_captcha_captcha_div_style' => 'display:block;',
'si_captcha_captcha_div_style_sm' => 'width:175px; height:45px; padding-top:10px;',
'si_captcha_captcha_div_style_m' => 'width:250px; height:60px; padding-top:10px;',
'si_captcha_captcha_image_style' => 'border-style:none; margin:0; padding-right:5px; float:left;',
'si_captcha_refresh_image_style' => 'border-style:none; margin:0; vertical-align:bottom;',
'si_captcha_captcha_input_div_style' => 'display:block; padding-top:15px; padding-bottom:5px;',
'si_captcha_comment_label_style' => 'margin:0;',
'si_captcha_comment_field_style' => 'width:65px;',
'si_captcha_label_captcha' => '',
'si_captcha_error_spambot' => '',
'si_captcha_error_incorrect' => '',
'si_captcha_error_empty' => '',
'si_captcha_error_token' => '',
'si_captcha_error_unreadable' => '',
'si_captcha_error_cookie' => '',
'si_captcha_error_error' => '',
'si_captcha_required_indicator' => ' *',
'si_captcha_tooltip_captcha' => '',
'si_captcha_tooltip_refresh' => '',
);
// upgrade path from old version
if ($wpmu != 1 && !get_option('si_captcha') && get_option('si_captcha_comment')) {
// just now updating, migrate settings
$si_captcha_option_defaults = $this->si_captcha_migrate($si_captcha_option_defaults);
}
// install the option defaults
if ($wpmu == 1) {
if( !get_site_option('si_captcha') ) {
add_site_option('si_captcha', $si_captcha_option_defaults, '', 'yes');
}
}else{
add_option('si_captcha', $si_captcha_option_defaults, '', 'yes');
}
// get the options from the database
if ($wpmu == 1)
$si_captcha_opt = get_site_option('si_captcha'); // get the options from the database
else
$si_captcha_opt = get_option('si_captcha');
// array merge incase this version has added new options
$si_captcha_opt = array_merge($si_captcha_option_defaults, $si_captcha_opt);
// strip slashes on get options array
foreach($si_captcha_opt as $key => $val) {
$si_captcha_opt[$key] = $this->si_stripslashes($val);
}
if ($si_captcha_opt['si_captcha_captcha_image_style'] == '') {
// if default styles are missing, reset styles
$style_resets_arr = array('si_captcha_comment_label_style','si_captcha_comment_field_style','si_captcha_captcha_div_style','si_captcha_captcha_div_style_sm','si_captcha_captcha_div_style_m','si_captcha_captcha_input_div_style','si_captcha_captcha_image_style','si_captcha_refresh_image_style');
foreach($style_resets_arr as $style_reset) {
$si_captcha_opt[$style_reset] = $si_captcha_option_defaults[$style_reset];
}
}
if ( defined('XMLRPC_REQUEST') && XMLRPC_REQUEST )
$si_captcha_opt['si_captcha_login'] = 'false'; // disable captcha on xmlrpc connections
} // end function si_captcha_get_options
function si_captcha_migrate($si_captcha_option_defaults) {
// read the options from the prior version
$new_options = array ();
foreach($si_captcha_option_defaults as $key => $val) {
$new_options[$key] = get_option( "$key" );
// now delete the options from the prior version
delete_option("$key");
}
// now the old settings will carry over to the new version
return $new_options;
} // end function si_captcha_migrate
function si_captcha_options_page() {
global $wpmu, $si_captcha_dir, $si_captcha_url, $si_captcha_url_ns, $si_captcha_dir_ns, $si_captcha_opt, $si_captcha_option_defaults, $si_captcha_version;
$si_captcha_admin_path = str_replace('/captcha','',$si_captcha_dir);
if ($wpmu == 1)
$si_captcha_admin_path = 'si-captcha-for-wordpress';
require_once($si_captcha_admin_path . '/si-captcha-admin.php');
}// end function si_captcha_options_page
function si_captcha_perm_dropdown($select_name, $checked_value='') {
// choices: Display text => permission_level
$choices = array (
__('All registered users', 'si-captcha') => 'read',
__('Edit posts', 'si-captcha') => 'edit_posts',
__('Publish Posts', 'si-captcha') => 'publish_posts',
__('Moderate Comments', 'si-captcha') => 'moderate_comments',
__('Administer site', 'si-captcha') => 'level_10'
);
// print the <select> and loop through <options>
echo '<select name="' . esc_attr($select_name) . '" id="' . esc_attr($select_name) . '">' . "\n";
foreach ($choices as $text => $capability) :
if ($capability == $checked_value) $checked = ' selected="selected" ';
echo "\t". '<option value="' . esc_attr($capability) . '"' . $checked . '>'.esc_html($text)."</option>\n";
$checked = '';
endforeach;
echo "\t</select>\n";
} // end function si_captcha_perm_dropdown
function si_captcha_check_requires() {
global $si_captcha_dir, $si_captcha_add_script;
$ok = 'ok';
// Test for some required things, print error message if not OK.
if ( !extension_loaded('gd') || !function_exists('gd_info') ) {
echo '<p style="color:maroon">'.__('ERROR: si-captcha.php plugin says GD image support not detected in PHP!', 'si-captcha').'</p>';
echo '<p>'.__('Contact your web host and ask them why GD image support is not enabled for PHP.', 'si-captcha').'</p>';
$ok = 'no';
}
if ( !function_exists('imagepng') ) {
echo '<p style="color:maroon">'.__('ERROR: si-captcha.php plugin says imagepng function not detected in PHP!', 'si-captcha').'</p>';
echo '<p>'.__('Contact your web host and ask them why imagepng function is not enabled for PHP.', 'si-captcha').'</p>';
$ok = 'no';
}
if ( !@strtolower(ini_get('safe_mode')) == 'on' && !file_exists("$si_captcha_dir/securimage.php") ) {
echo '<p style="color:maroon">'.__('ERROR: si-captcha.php plugin says captcha_library not found.', 'si-captcha').'</p>';
$ok = 'no';
}
if ($ok == 'no') return false;
$si_captcha_add_script = true;
return true;
} // end function si_captcha_check_requires
// this function adds the captcha to the comment form
function si_captcha_comment_form() {
global $si_captcha_url, $si_captcha_opt;
// skip the captcha if user is logged in and the settings allow
if (is_user_logged_in() && $si_captcha_opt['si_captcha_perm'] == 'true') {
// skip the CAPTCHA display if the minimum capability is met
if ( current_user_can( $si_captcha_opt['si_captcha_perm_level'] ) ) {
// skip capthca
return true;
}
}
// the captcha html
echo '
<div id="captchaImgDiv">
';
// Test for some required things, print error message right here if not OK.
if ($this->si_captcha_check_requires()) {
$si_aria_required = ($si_captcha_opt['si_captcha_aria_required'] == 'true') ? ' aria-required="true" ' : '';
// the captcha html - comment form 2.0
echo '
<div ';
echo ($si_captcha_opt['si_captcha_captcha_small'] == 'true') ? 'class="captchaSizeDivSmall"' : 'class="captchaSizeDivLarge"';
echo '>';
$this->si_captcha_captcha_html('si_image_com','com');
echo '</div>
<div id="captchaInputDiv">';
$label_string = ' <label id="captcha_code_label" for="captcha_code">';
$label_string .= ($si_captcha_opt['si_captcha_label_captcha'] != '') ? $si_captcha_opt['si_captcha_label_captcha'] : __('CAPTCHA Code', 'si-captcha');
$label_string .= '</label>';
$required_string = '<span class="required">'.$si_captcha_opt['si_captcha_required_indicator']."</span>\n";
$input_string = '<input id="captcha_code" name="captcha_code" type="text" value="" tabindex="4" '.$si_aria_required.' />
';
if ($si_captcha_opt['si_captcha_comment_label_position'] == 'label-required-input' || $si_captcha_opt['si_captcha_comment_label_position'] == 'left' ) { // buddypress (label-required-input)(label left)
echo $label_string . $required_string . $input_string; // BP
} else if ($si_captcha_opt['si_captcha_comment_label_position'] == 'label-required-linebreak-input' || $si_captcha_opt['si_captcha_comment_label_position'] == 'top' ) {
echo $label_string . $required_string .'<br />'. $input_string; // regular WP - twenty ten
} else if ($si_captcha_opt['si_captcha_comment_label_position'] == 'label-input-required' || $si_captcha_opt['si_captcha_comment_label_position'] == 'right' ) {
echo $label_string . $input_string . $required_string; // suffusion
} else if ($si_captcha_opt['si_captcha_comment_label_position'] == 'input-label-required' ) {
echo $input_string . $label_string . $required_string; // regular WP
} else {
echo $input_string . $label_string . $required_string; // regular WP
}
echo ' </div>
</div>
';
// rearrange submit button display order
if ($si_captcha_opt['si_captcha_rearrange'] == 'true') {
print <<<EOT
<script type='text/javascript'>
var sUrlInput = document.getElementById("comment");
var oParent = sUrlInput.parentNode;
var sSubstitue = document.getElementById("captchaImgDiv");
oParent.appendChild(sSubstitue, sUrlInput);
</script>
<noscript>
<style type='text/css'>#submit {display:none;}</style><br />
EOT;
echo ' <input name="submit" type="submit" id="submit-alt" tabindex="6" value="'.__('Submit Comment', 'si-captcha').'" />
</noscript>
';
}
}else{
echo '</div>';
}
return true;
} // end function si_captcha_comment_form 2.0
// this function adds the captcha to the comment form WP3
function si_captcha_comment_form_wp3() {
global $si_captcha_url, $si_captcha_opt;
// skip the captcha if user is logged in and the settings allow
if (is_user_logged_in() && $si_captcha_opt['si_captcha_perm'] == 'true') {
// skip the CAPTCHA display if the minimum capability is met
if ( current_user_can( $si_captcha_opt['si_captcha_perm_level'] ) ) {
// skip capthca
return true;
}
}
// the captch html
// Test for some required things, print error message right here if not OK.
if ($this->si_captcha_check_requires()) {
$si_aria_required = ($si_captcha_opt['si_captcha_aria_required'] == 'true') ? ' aria-required="true" ' : '';
// the captcha html - comment form 3.0+
if (is_user_logged_in()) {
echo '<br />';
}
echo '
<div ';
echo ($si_captcha_opt['si_captcha_captcha_small'] == 'true') ? 'class="captchaSizeDivSmall"' : 'class="captchaSizeDivLarge"';
echo '>';
$this->si_captcha_captcha_html('si_image_com','com');
echo '</div>
<br />
';
echo '<p>';
$label_string = '<label id="captcha_code_label" for="captcha_code" >';
$label_string .= ($si_captcha_opt['si_captcha_label_captcha'] != '') ? $si_captcha_opt['si_captcha_label_captcha'] : __('CAPTCHA Code', 'si-captcha');
$label_string .= '</label>';
$required_string = '<span class="required">'.$si_captcha_opt['si_captcha_required_indicator']."</span>\n";
$input_string = '<input id="captcha_code" name="captcha_code" type="text" size="6" ' . $si_aria_required . ' />
';
if ($si_captcha_opt['si_captcha_comment_label_position'] == 'label-required-input' || $si_captcha_opt['si_captcha_comment_label_position'] == 'left' ) { // buddypress (label-required-input)(label left)
echo $label_string . $required_string . $input_string; // BP
} else if ($si_captcha_opt['si_captcha_comment_label_position'] == 'label-required-linebreak-input' || $si_captcha_opt['si_captcha_comment_label_position'] == 'top' ) {
echo $label_string . $required_string .'<br />'. $input_string; // regular WP - twenty ten
} else if ($si_captcha_opt['si_captcha_comment_label_position'] == 'label-input-required' || $si_captcha_opt['si_captcha_comment_label_position'] == 'right' ) {
echo $label_string . $input_string . $required_string; // suffusion
} else if ($si_captcha_opt['si_captcha_comment_label_position'] == 'input-label-required' ) {
echo $input_string . $label_string . $required_string; // regular WP
} else {
echo $input_string . $label_string . $required_string; // regular WP
}
echo '</p>';
}
// prevent double captcha fields
remove_action('comment_form', array(&$this, 'si_captcha_comment_form'), 1);
return true;
} // end function si_captcha_comment_form_wp3
// this function adds the captcha to the login form
function si_captcha_login_form() {
global $si_captcha_url, $si_captcha_opt;
if ($si_captcha_opt['si_captcha_login'] != 'true') {
return true; // captcha setting is disabled for login
}
// Test for some required things, print error message right here if not OK.
if ($this->si_captcha_check_requires()) {
$si_aria_required = ($si_captcha_opt['si_captcha_aria_required'] == 'true') ? ' aria-required="true" ' : '';
// the captcha html - login form
echo '
<br />
<div ';
echo ($si_captcha_opt['si_captcha_captcha_small'] == 'true') ? 'class="captchaSizeDivSmall"' : 'class="captchaSizeDivLarge"';
echo '>';
$this->si_captcha_captcha_html('si_image_log','log');
echo '</div>
<p>
<label>';
echo ($si_captcha_opt['si_captcha_label_captcha'] != '') ? $si_captcha_opt['si_captcha_label_captcha'] : __('CAPTCHA Code', 'si-captcha');
echo '<br />
<input id="captcha_code" name="captcha_code" class="input" type="text" value="" size="12" tabindex="30" '.$si_aria_required.' style="font-size: 24px; width: 97%; padding: 3px; margin-top: 2px; margin-right: 6px; margin-bottom: 16px; border: 1px solid #e5e5e5; background: #fbfbfb;" /></label>
</p>
<br />
';
}
return true;
} // end function si_captcha_login_form
// this function adds the captcha to the login bar form of all buddypress versions
function si_captcha_bp_login_form() {
global $si_captcha_url, $si_captcha_opt;
if ($si_captcha_opt['si_captcha_login'] != 'true') {
return true; // captcha setting is disabled for login
}
// Test for some required things, print error message right here if not OK.
if ($this->si_captcha_check_requires()) {
$si_aria_required = ($si_captcha_opt['si_captcha_aria_required'] == 'true') ? ' aria-required="true" ' : '';
// the captcha html - buddypress login form
echo '
<div style="width:440px; height:45px">';
$this->si_captcha_captcha_html('si_image_log','log');
echo '<input id="captcha_code" name="captcha_code" class="input" type="text" value="" '.$si_aria_required.' />
<label for="captcha_code">';
echo ($si_captcha_opt['si_captcha_label_captcha'] != '') ? $si_captcha_opt['si_captcha_label_captcha'] : __('CAPTCHA Code', 'si-captcha');
echo '</label>
</div>
</div>
';
}
return true;
} // end function si_captcha_bp_login_form
// this function adds the captcha to the login sidebar form of all buddypress versions
function si_captcha_bp_login_sidebar_form() {
global $si_captcha_url, $si_captcha_opt;
if ($si_captcha_opt['si_captcha_login'] != 'true') {
return true; // captcha setting is disabled for login
}
// Test for some required things, print error message right here if not OK.
if ($this->si_captcha_check_requires()) {
$si_aria_required = ($si_captcha_opt['si_captcha_aria_required'] == 'true') ? ' aria-required="true" ' : '';
// the captcha html - buddypress sidebar login form
echo '
<div class="captchaSizeDivSmall">
';
$this->si_captcha_captcha_html('si_image_side_login','log');
echo '
</div>
<label for="captcha_code_side_login">';
echo ($si_captcha_opt['si_captcha_label_captcha'] != '') ? $si_captcha_opt['si_captcha_label_captcha'] : __('CAPTCHA Code', 'si-captcha');
echo '</label>
<input style="width=145px;" id="captcha_code_side_login" name="captcha_code" class="input" type="text" value="" '.$si_aria_required.' />
<br />
<br />
';
}
return true;
} // end function si_captcha_bp_login_sidebar_form
// this function adds the captcha to the login form any time wp_login_form is called
function si_captcha_inline_login_form() {
global $si_captcha_url, $si_captcha_opt;
if ($si_captcha_opt['si_captcha_login'] != 'true') {
return true; // captcha setting is disabled for login
}
// Test for some required things, print error message right here if not OK.
if ($this->si_captcha_check_requires()) {
$si_aria_required = ($si_captcha_opt['si_captcha_aria_required'] == 'true') ? ' aria-required="true" ' : '';
// the captcha html - buddypress sidebar login form
$si_html = '
<div class="captchaSizeDivSmall">
';
$si_html .= $this->si_captcha_captcha_html('si_image_side_login','log', true);
$si_html .= '
</div>
<label for="captcha_code_side_login">';
$si_html .= ($si_captcha_opt['si_captcha_label_captcha'] != '') ? $si_captcha_opt['si_captcha_label_captcha'] : __('CAPTCHA Code', 'si-captcha');
$si_html .= '</label>
<input style="width=145px;" id="captcha_code_side_login" name="captcha_code" class="input" type="text" value="" '.$si_aria_required.' />
<br />
<br />
';
}
return $si_html;
} // end function si_captcha_inline_login_form
// this function adds the captcha to the register form
function si_captcha_register_form() {
global $si_captcha_url, $si_captcha_opt;
if ($si_captcha_opt['si_captcha_register'] != 'true') {
return true; // captcha setting is disabled for registration
}
// Test for some required things, print error message right here if not OK.
if ($this->si_captcha_check_requires()) {
$si_aria_required = ($si_captcha_opt['si_captcha_aria_required'] == 'true') ? ' aria-required="true" ' : '';
// the captcha html - register form
echo '
<br />
<div ';
echo ($si_captcha_opt['si_captcha_captcha_small'] == 'true') ? 'class="captchaSizeDivSmall"' : 'class="captchaSizeDivLarge"';
echo '>';
$this->si_captcha_captcha_html('si_image_reg','reg');
echo '</div>
<p>
<label>';
echo ($si_captcha_opt['si_captcha_label_captcha'] != '') ? $si_captcha_opt['si_captcha_label_captcha'] : __('CAPTCHA Code', 'si-captcha');
echo '<br />
<input id="captcha_code" name="captcha_code" class="input" type="text" value="" tabindex="30" '.$si_aria_required.' style="font-size: 24px; width: 97%; padding: 3px; margin-top: 2px; margin-right: 6px; margin-bottom: 16px; border: 1px solid #e5e5e5; background: #fbfbfb;" /></label>
</p>
';
}
return true;
} // end function si_captcha_register_form
// this function adds the captcha to the lostpassword form
function si_captcha_lostpassword_form() {
global $si_captcha_url, $si_captcha_opt;
// Test for some required things, print error message right here if not OK.
if ($this->si_captcha_check_requires()) {
$si_aria_required = ($si_captcha_opt['si_captcha_aria_required'] == 'true') ? ' aria-required="true" ' : '';
// the captcha html - lostpassword form
echo '
<br />
<div ';
echo ($si_captcha_opt['si_captcha_captcha_small'] == 'true') ? 'class="captchaSizeDivSmall"' : 'class="captchaSizeDivLarge"';
echo '>';
$this->si_captcha_captcha_html('si_image_reg','reg');
echo '</div>
<p>
<label>';
echo ($si_captcha_opt['si_captcha_label_captcha'] != '') ? $si_captcha_opt['si_captcha_label_captcha'] : __('CAPTCHA Code', 'si-captcha');
echo '<br />
<input id="captcha_code" name="captcha_code" class="input" type="text" value="" tabindex="30" '.$si_aria_required.' style="font-size: 24px; width: 97%; padding: 3px; margin-top: 2px; margin-right: 6px; margin-bottom: 16px; border: 1px solid #e5e5e5; background: #fbfbfb;" /></label>
</p>
';
}
return true;
} // end function si_captcha_lostpassword_form
// for wpmu and buddypress before 1.1
function si_captcha_wpmu_signup_form( $errors ) {
global $si_captcha_url, $si_captcha_opt;
if ($si_captcha_opt['si_captcha_register'] != 'true') {
return true; // captcha setting is disabled for registration
}
$error = $errors->get_error_message('captcha');
if( isset($error) && $error != '') {
echo '<p class="error">' . $error . '</p>';
}
// Test for some required things, print error message right here if not OK.
if ($this->si_captcha_check_requires()) {
$si_aria_required = ($si_captcha_opt['si_captcha_aria_required'] == 'true') ? ' aria-required="true" ' : '';
// the captcha html - wpmu register form
echo '
<div ';
echo ($si_captcha_opt['si_captcha_captcha_small'] == 'true') ? 'class="captchaSizeDivSmall"' : 'class="captchaSizeDivLarge"';
echo '>';
$this->si_captcha_captcha_html('si_image_reg','reg');
echo '</div>
<label for="captcha_code">';
echo ($si_captcha_opt['si_captcha_label_captcha'] != '') ? $si_captcha_opt['si_captcha_label_captcha'] : __('CAPTCHA Code', 'si-captcha');
echo '</label>
<input id="captcha_code" name="captcha_code" type="text" value="" '.$si_aria_required.' />
';
}
} // end function si_captcha_wpmu_signup_form
// for buddypress 1.1+ only
// hooks into register.php do_action( 'bp_before_registration_submit_buttons' )
// and bp-core-signup.php add_action( 'bp_' . $fieldname . '_errors', ...
function si_captcha_bp_signup_form() {
global $si_captcha_url, $si_captcha_opt;
if ($si_captcha_opt['si_captcha_register'] != 'true') {
return true; // captcha setting is disabled for registration
}
// Test for some required things, print error message right here if not OK.
if ($this->si_captcha_check_requires()) {
$si_aria_required = ($si_captcha_opt['si_captcha_aria_required'] == 'true') ? ' aria-required="true" ' : '';
// the captcha html - buddypress 1.1 register form
echo '
<div class="register-section" style="clear:left; margin-top:-10px;">
<div ';
echo ($si_captcha_opt['si_captcha_captcha_small'] == 'true') ? 'class="captchaSizeDivSmall"' : 'class="captchaSizeDivLarge"';
echo '>';
$this->si_captcha_captcha_html('si_image_reg','reg');
echo '</div>
<label for="captcha_code">';
do_action( 'bp_captcha_code_errors' );
echo ($si_captcha_opt['si_captcha_label_captcha'] != '') ? $si_captcha_opt['si_captcha_label_captcha'] : __('CAPTCHA Code', 'si-captcha');
echo '</label>
<input style="width:145px;" id="captcha_code" name="captcha_code" type="text" value="" '.$si_aria_required.' />
</div>
';
}
} // end function si_captcha_wpmu_signup_form
// this function checks the captcha posted with registration on BuddyPress 1.1+
// hooks into bp-core-signup.php do_action( 'bp_signup_validate' );
function si_captcha_bp_signup_validate() {
global $bp, $si_captcha_dir, $si_captcha_dir_ns, $si_captcha_opt;
$validate_result = $this->si_captcha_validate_code('reg', 'unlink');
if($validate_result != 'valid') {
$bp->signup->errors['captcha_code'] = $validate_result;
return;
}
return;
} // end function si_captcha_bp_signup_validate
// this function checks the captcha posted with registration on wpmu and buddypress before 1.1
function si_captcha_wpmu_signup_post($errors) {
global $si_captcha_dir, $si_captcha_dir_ns, $si_captcha_opt;
if ($_POST['stage'] == 'validate-user-signup') {
$validate_result = $this->si_captcha_validate_code('reg', 'unlink');
if($validate_result != 'valid') {
$error = ($si_captcha_opt['si_captcha_error_error'] != '') ? $si_captcha_opt['si_captcha_error_error'] : __('ERROR', 'si-captcha');
$errors['errors']->add('captcha', "<strong>$error</strong>: $validate_result");
return $errors;
}
}
return($errors);
} // end function si_captcha_wpmu_signup_post
// this function checks the captcha posted with registration
function si_captcha_register_post($errors) {
global $si_captcha_dir, $si_captcha_dir_ns, $si_captcha_opt;
$validate_result = $this->si_captcha_validate_code('reg', 'unlink');
if($validate_result != 'valid') {
$error = ($si_captcha_opt['si_captcha_error_error'] != '') ? $si_captcha_opt['si_captcha_error_error'] : __('ERROR', 'si-captcha');
$errors->add('captcha_error', "<strong>$error</strong>: $validate_result");
return $errors;
}
return($errors);
} // end function si_captcha_register_post
function si_captcha_lostpassword_post() {
global $si_captcha_dir, $si_captcha_dir_ns, $si_captcha_opt;
$validate_result = $this->si_captcha_validate_code('reg', 'unlink');
if($validate_result != 'valid') {
$error = ($si_captcha_opt['si_captcha_error_error'] != '') ? $si_captcha_opt['si_captcha_error_error'] : __('ERROR', 'si-captcha');
wp_die( "<strong>$error</strong>: $validate_result" );
}
return;
} // function si_captcha_lostpassword_post
// this function checks the captcha posted with the comment
function si_captcha_comment_post($comment) {
global $si_captcha_dir, $si_captcha_dir_ns, $si_captcha_opt;
// added for compatibility with WP Wall plugin
// this does NOT add CAPTCHA to WP Wall plugin,
// it just prevents the "Empty CAPTCHA" when submitting a WP Wall comment
if ( function_exists('WPWall_Widget') && isset($_POST['wpwall_comment']) ) {
// skip capthca
return $comment;
}
// skip the captcha if user is logged in and the settings allow
if (is_user_logged_in() && $si_captcha_opt['si_captcha_perm'] == 'true') {
// skip the CAPTCHA display if the minimum capability is met
if ( current_user_can( $si_captcha_opt['si_captcha_perm_level'] ) ) {
// skip capthca
return $comment;
}
}
// skip captcha for comment replies from admin menu
if ( isset($_POST['action']) && $_POST['action'] == 'replyto-comment' &&
( check_ajax_referer( 'replyto-comment', '_ajax_nonce', false ) || check_ajax_referer( 'replyto-comment', '_ajax_nonce-replyto-comment', false )) ) {
// skip capthca
return $comment;
}
// Skip captcha for trackback or pingback
if ( $comment['comment_type'] != '' && $comment['comment_type'] != 'comment' ) {
// skip capthca
return $comment;
}
$validate_result = $this->si_captcha_validate_code('com', 'unlink');
if($validate_result != 'valid') {
$error = ($si_captcha_opt['si_captcha_error_error'] != '') ? $si_captcha_opt['si_captcha_error_error'] : __('ERROR', 'si-captcha');
wp_die( "<strong>$error</strong>: $validate_result" );
}
return($comment);
} // end function si_captcha_comment_post
function si_wp_authenticate_username_password($user, $username, $password) {
global $si_captcha_dir, $si_captcha_dir_ns, $si_captcha_opt, $wp_version;
if ( is_a($user, 'WP_User') ) { return $user; }
if ( empty($username) || empty($password) || isset($_POST['captcha_code']) && empty($_POST['captcha_code'])) {
$error = new WP_Error();
if ( empty($username) )
$error->add('empty_username', __('<strong>ERROR</strong>: The username field is empty.'));
if ( empty($password) )
$error->add('empty_password', __('<strong>ERROR</strong>: The password field is empty.'));
if (isset($_POST['captcha_code']) && empty($_POST['captcha_code'])) {
remove_filter('authenticate', 'wp_authenticate_username_password', 20, 3);
$print_error = ($si_captcha_opt['si_captcha_error_error'] != '') ? $si_captcha_opt['si_captcha_error_error'] : __('ERROR', 'si-captcha');
$empty_captcha = ($si_captcha_opt['si_captcha_error_empty'] != '') ? $si_captcha_opt['si_captcha_error_empty'] : __('Empty CAPTCHA', 'si-captcha');
$error->add('empty_captcha', "<strong>$print_error</strong>: $empty_captcha");
}
return $error;
}
// begin si captcha check
$validate_result = $this->si_captcha_validate_code('log', 'unlink');
if($validate_result != 'valid') {
remove_filter('authenticate', 'wp_authenticate_username_password', 20, 3);
$print_error = ($si_captcha_opt['si_captcha_error_error'] != '') ? $si_captcha_opt['si_captcha_error_error'] : __('ERROR', 'si-captcha');
return new WP_Error('captcha_error', "<strong>$print_error</strong>: $validate_result");
}
// end si captcha check
$userdata = get_user_by('login', $username);
if ( !$userdata ) {
return new WP_Error('invalid_username', sprintf(__('<strong>ERROR</strong>: Invalid username. <a href="%s" title="Password Lost and Found">Lost your password</a>?'), site_url('wp-login.php?action=lostpassword', 'login')));
}
// for WP 3.0+ ONLY!
if( version_compare($wp_version,'3','>=') ) { // wp 3.0 +
if ( is_multisite() ) {
// Is user marked as spam?
if ( 1 == $userdata->spam)
return new WP_Error('invalid_username', __('<strong>ERROR</strong>: Your account has been marked as a spammer.'));
// Is a user's blog marked as spam?
if ( !is_super_admin( $userdata->ID ) && isset($userdata->primary_blog) ) {
$details = get_blog_details( $userdata->primary_blog );
if ( is_object( $details ) && $details->spam == 1 )
return new WP_Error('blog_suspended', __('Site Suspended.'));
}
}
}
$userdata = apply_filters('wp_authenticate_user', $userdata, $password);
if ( is_wp_error($userdata) ) {
return $userdata;
}
if ( !wp_check_password($password, $userdata->user_pass, $userdata->ID) ) {
return new WP_Error('incorrect_password', sprintf(__('<strong>ERROR</strong>: Incorrect password. <a href="%s" title="Password Lost and Found">Lost your password</a>?'), site_url('wp-login.php?action=lostpassword', 'login')));
}
$user = new WP_User($userdata->ID);
return $user;
} // end function si_wp_authenticate_username_password
// check the honeypot trap for spam bots
// hidden empty field honyepot trap for spam bots
function si_captcha_check_honeypot($form_id = 'com') {
global $si_captcha_opt;
if ($si_captcha_opt['si_captcha_honeypot_enable'] == 'false')
return 'ok';
// validate hidden honeypot field
if( isset($_POST["email_$form_id"]) && trim($_POST["email_$form_id"]) != '')
return 'failed honeypot';
return 'ok';
} // end function si_captcha_check_honeypot
// check if the posted capcha code was valid
function si_captcha_validate_code($form_id = 'com', $unlink = 'unlink') {
global $si_captcha_dir, $si_captcha_dir_ns, $si_captcha_opt;
if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'lostpassword' && $form_id == 'log')
return 'valid'; // fixes lostpassword page because add_filter('login_errors' is also being called before
if (isset($_POST['captcha_code']) && empty($_POST['captcha_code']))
return ($si_captcha_opt['si_captcha_error_empty'] != '') ? $si_captcha_opt['si_captcha_error_empty'] : __('Empty CAPTCHA', 'si-captcha');
if($si_captcha_opt['si_captcha_enable_session'] != 'true') {
//captcha without sessions
if (empty($_POST['captcha_code']) || $_POST['captcha_code'] == '') {
return ($si_captcha_opt['si_captcha_error_empty'] != '') ? $si_captcha_opt['si_captcha_error_empty'] : __('Empty CAPTCHA', 'si-captcha');
}else if (!isset($_POST["si_code_$form_id"]) || empty($_POST["si_code_$form_id"])) {
return ($si_captcha_opt['si_captcha_error_token'] != '') ? $si_captcha_opt['si_captcha_error_token'] : __('Missing CAPTCHA token', 'si-captcha');
}else{
$prefix = 'xxxxxx';
if ( isset($_POST["si_code_$form_id"]) && is_string($_POST["si_code_$form_id"]) && preg_match('/^[a-zA-Z0-9]{15,17}$/',$_POST["si_code_$form_id"]) ){
$prefix = $_POST["si_code_$form_id"];
}
if ( is_readable( $si_captcha_dir_ns . $prefix . '.php' ) ) {
include( $si_captcha_dir_ns . $prefix . '.php' );
if ( 0 == strcasecmp( trim(strip_tags($_POST['captcha_code'])), $captcha_word ) ) {
// captcha was matched
if($unlink == 'unlink')
@unlink ($si_captcha_dir_ns . $prefix . '.php');
// empty field honyepot trap for spam bots
$hp_check = $this->si_captcha_check_honeypot("$form_id");
if($hp_check != 'ok')
return ($si_captcha_opt['si_captcha_error_spambot'] != '') ? $si_captcha_opt['si_captcha_error_spambot'] : __('Possible spam bot', 'si-captcha');
return 'valid';
} else {
return ($si_captcha_opt['si_captcha_error_incorrect'] != '') ? $si_captcha_opt['si_captcha_error_incorrect'] : __('Wrong CAPTCHA', 'si-captcha');
}
} else {
return ($si_captcha_opt['si_captcha_error_unreadable'] != '') ? $si_captcha_opt['si_captcha_error_unreadable'] : __('Unreadable CAPTCHA token file', 'si-captcha');
//$this->si_captcha_token_error();
}
}
}else{
//captcha with PHP sessions
if (!isset($_SESSION["securimage_code_si_$form_id"]) || empty($_SESSION["securimage_code_si_$form_id"])) {
return ($si_captcha_opt['si_captcha_error_cookie'] != '') ? $si_captcha_opt['si_captcha_error_cookie'] : __('Unreadable CAPTCHA cookie', 'si-captcha');
}else{
$captcha_code = trim(strip_tags($_POST['captcha_code']));
require_once "$si_captcha_dir/securimage.php";
$img = new Securimage_si();
$img->form_id = $form_id; // makes compatible with multi-forms on same page
$valid = $img->check("$captcha_code");
// Check, that the right CAPTCHA password has been entered, display an error message otherwise.
if($valid == true) {
// empty field honyepot trap for spam bots
$hp_check= $this->si_captcha_check_honeypot("$form_id");
if($hp_check != 'ok')
return ($si_captcha_opt['si_captcha_error_spambot'] != '') ? $si_captcha_opt['si_captcha_error_spambot'] : __('Possible spam bot', 'si-captcha');
// ok can continue
return 'valid';
} else {
return ($si_captcha_opt['si_captcha_error_incorrect'] != '') ? $si_captcha_opt['si_captcha_error_incorrect'] : __('Wrong CAPTCHA', 'si-captcha');
}
}
}
} // end function si_captcha_validate_code
// displays the CAPTCHA in the forms
function si_captcha_captcha_html($label = 'si_image', $form_id = 'com', $no_echo = false) {
global $si_captcha_url, $si_captcha_dir, $si_captcha_url_ns, $si_captcha_dir_ns, $si_captcha_opt;
$capt_disable_sess = 0;
if ($si_captcha_opt['si_captcha_enable_session'] != 'true')
$capt_disable_sess = 1;
// url for no session captcha image
$securimage_show_url = $si_captcha_url .'/securimage_show.php?';
$securimage_size = 'width="175" height="60"';
if($si_captcha_opt['si_captcha_captcha_small'] == 'true' || $label == 'si_image_side_login' ) {
$securimage_show_url .= 'si_sm_captcha=1&';
$securimage_size = 'width="132" height="45"';
}
$parseUrl = parse_url($si_captcha_url);
$securimage_url = $parseUrl['path'];
$securimage_show_url .= 'si_form_id=' .$form_id;
if($capt_disable_sess) {
// clean out old captcha no session temp files
$this->si_captcha_clean_temp_dir($si_captcha_dir_ns, 30);
// pick new prefix token
$prefix_length = 16;
$prefix_characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz';
$prefix = '';
$prefix_count = strlen($prefix_characters);
while ($prefix_length--) {
$prefix .= $prefix_characters[mt_rand(0, $prefix_count-1)];
}
$securimage_show_rf_url = $securimage_show_url . '&prefix=';
$securimage_show_url .= '&prefix='.$prefix;
}
$si_html = '';
if($si_captcha_opt['si_captcha_honeypot_enable'] == 'true' ) {
// hidden empty honeypot field
$si_html .= '
<div style="display:none;">
<label for="email_'.$form_id.'"><small>'.__('Leave this field empty', 'si-captcha').'</small></label>
<input type="text" name="email_'.$form_id.'" id="email_'.$form_id.'" value="" />
</div>
';
}
$si_html .= '<img id="'.$label.'" class="si-captcha" src="'.$securimage_show_url.'" '.$securimage_size.' alt="';
$si_html .= ($si_captcha_opt['si_captcha_tooltip_captcha'] != '') ? esc_attr( $si_captcha_opt['si_captcha_tooltip_captcha'] ) : esc_attr(__('CAPTCHA Image', 'si-captcha'));
$si_html .= '" title="';
$si_html .= ($si_captcha_opt['si_captcha_tooltip_captcha'] != '') ? esc_attr( $si_captcha_opt['si_captcha_tooltip_captcha'] ) : esc_attr(__('CAPTCHA Image', 'si-captcha'));
$si_html .= '" />'."\n";
if($capt_disable_sess) {
$si_html .= ' <input id="si_code_'.$form_id.'" name="si_code_'.$form_id.'" type="hidden" value="'.$prefix.'" />'."\n";
}
$si_html .= ' <div id="si_refresh_'.$form_id.'">'."\n";
$si_html .= '<a href="#" rel="nofollow" title="';
$si_html .= ($si_captcha_opt['si_captcha_tooltip_refresh'] != '') ? esc_attr( $si_captcha_opt['si_captcha_tooltip_refresh'] ) : esc_attr(__('Refresh Image', 'si-captcha'));
if($capt_disable_sess) {
$si_html .= '" onclick="si_captcha_refresh(\''.$label.'\',\''.$form_id.'\',\''.$securimage_url.'\',\''.$securimage_show_rf_url.'\'); return false;">'."\n";
}else{
$si_html .= '" onclick="document.getElementById(\''.$label.'\').src = \''.$securimage_show_url.'&sid=\''.' + Math.random(); return false;">'."\n";
}
$si_html .= ' <img class="captchaImgRefresh" src="'.$si_captcha_url.'/images/refresh.png" width="22" height="20" alt="';
$si_html .= ($si_captcha_opt['si_captcha_tooltip_refresh'] != '') ? esc_attr( $si_captcha_opt['si_captcha_tooltip_refresh'] ) : esc_attr(__('Refresh Image', 'si-captcha'));
$si_html .= '" onclick="this.blur();" /></a>
</div>
';
if ( $no_echo ) return $si_html;
echo $si_html;
} // end function si_captcha_captcha_html
function si_captcha_plugin_action_links( $links, $file ) {
//Static so we don't call plugin_basename on every plugin row.
static $this_plugin;
if ( ! $this_plugin ) $this_plugin = plugin_basename(__FILE__);
if ( $file == $this_plugin ){
$settings_link = '<a href="plugins.php?page=si-captcha-for-wordpress/si-captcha.php">' . __('Settings', 'si-captcha') . '</a>';
array_unshift( $links, $settings_link );
}
return $links;
} // end function si_captcha_plugin_action_links
function si_captcha_init() {
global $wpmu;
if (function_exists('load_plugin_textdomain')) {
if ($wpmu == 1) {
load_plugin_textdomain('si-captcha', false, dirname(plugin_basename(__FILE__)).'/si-captcha-for-wordpress/languages' );
} else {
load_plugin_textdomain('si-captcha', false, dirname(plugin_basename(__FILE__)).'/languages' );
}
}
} // end function si_captcha_init
function si_captcha_start_session() {
// a PHP session cookie is set so that the captcha can be remembered and function
// this has to be set before any header output
//echo "before starting session si captcha";
if( !isset( $_SESSION ) ) { // play nice with other plugins
if ( !defined('XMLRPC_REQUEST') ) { // buddypress fix
//set the $_SESSION cookie into HTTPOnly mode for better security
if (version_compare(PHP_VERSION, '5.2.0') >= 0) // supported on PHP version 5.2.0 and higher
@ini_set("session.cookie_httponly", 1);
session_cache_limiter ('private, must-revalidate');
session_start();
//echo "session started si captcha";
}
}
} // function si_captcha_start_session
// needed for making temp directories for attachments and captcha session files
function si_captcha_init_temp_dir($dir) {
$dir = trailingslashit( $dir );
// make the temp directory
wp_mkdir_p( $dir );
//@chmod( $dir, 0733 );
$htaccess_file = $dir . '.htaccess';
if ( !file_exists( $htaccess_file ) ) {
if ( $handle = @fopen( $htaccess_file, 'w' ) ) {
fwrite( $handle, "Deny from all\n" );
fclose( $handle );