Math Captcha - Version 1.2.6

Version Description

  • Fix: Contact form 7 compatibility fix
Download this release

Release Info

Developer dfactory
Plugin Icon 128x128 Math Captcha
Version 1.2.6
Comparing to
See all releases

Code changes from version 1.2.5.1 to 1.2.6

includes/class-cookie-session.php CHANGED
@@ -1,79 +1,67 @@
1
  <?php
2
- if(!defined('ABSPATH')) exit;
 
3
 
4
  new Math_Captcha_Cookie_Session();
5
 
6
- class Math_Captcha_Cookie_Session
7
- {
8
- public $session_ids;
9
 
 
10
 
11
- public function __construct()
12
- {
13
  // sets instance
14
  Math_Captcha()->cookie_session = $this;
15
 
16
  // actions
17
- add_action('plugins_loaded', array(&$this, 'init_session'), 1);
18
  }
19
 
20
-
21
  /**
22
  * Initializes cookie-session
23
- */
24
- public function init_session()
25
- {
26
- if(is_admin())
27
  return;
28
 
29
- if(isset($_COOKIE['mc_session_ids']))
30
  $this->session_ids = $_COOKIE['mc_session_ids'];
31
- else
32
- {
33
- foreach(array('default', 'multi') as $place)
34
- {
35
- switch($place)
36
- {
37
  case 'multi':
38
- for($i = 0; $i < 5; $i++)
39
- {
40
- $this->session_ids[$place][$i] = sha1($this->generate_password());
41
  }
42
  break;
43
 
44
  case 'default':
45
- $this->session_ids[$place] = sha1($this->generate_password());
46
  break;
47
  }
48
  }
49
  }
50
 
51
- if(!isset($_COOKIE['mc_session_ids']))
52
- {
53
- setcookie('mc_session_ids[default]', $this->session_ids['default'], current_time('timestamp', true) + apply_filters('math_captcha_time', Math_Captcha()->options['general']['time']), COOKIEPATH, COOKIE_DOMAIN, (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? true : false), true);
54
 
55
- for($i = 0; $i < 5; $i++)
56
- {
57
- setcookie('mc_session_ids[multi]['.$i.']', $this->session_ids['multi'][$i], current_time('timestamp', true) + apply_filters('math_captcha_time', Math_Captcha()->options['general']['time']), COOKIEPATH, COOKIE_DOMAIN);
58
  }
59
  }
60
  }
61
 
62
-
63
  /**
64
  * Generate password helper, without wp_rand() call
65
- */
66
- private function generate_password($length = 64)
67
- {
68
  $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
69
  $password = '';
70
 
71
- for($i = 0; $i < $length; $i++)
72
- {
73
- $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
74
  }
75
 
76
  return $password;
77
  }
78
- }
79
- ?>
1
  <?php
2
+ if ( ! defined( 'ABSPATH' ) )
3
+ exit;
4
 
5
  new Math_Captcha_Cookie_Session();
6
 
7
+ class Math_Captcha_Cookie_Session {
 
 
8
 
9
+ public $session_ids;
10
 
11
+ public function __construct() {
 
12
  // sets instance
13
  Math_Captcha()->cookie_session = $this;
14
 
15
  // actions
16
+ add_action( 'plugins_loaded', array( &$this, 'init_session' ), 1 );
17
  }
18
 
 
19
  /**
20
  * Initializes cookie-session
21
+ */
22
+ public function init_session() {
23
+ if ( is_admin() )
 
24
  return;
25
 
26
+ if ( isset( $_COOKIE['mc_session_ids'] ) )
27
  $this->session_ids = $_COOKIE['mc_session_ids'];
28
+ else {
29
+ foreach ( array( 'default', 'multi' ) as $place ) {
30
+ switch ( $place ) {
 
 
 
31
  case 'multi':
32
+ for ( $i = 0; $i < 5; $i ++ ) {
33
+ $this->session_ids[$place][$i] = sha1( $this->generate_password() );
 
34
  }
35
  break;
36
 
37
  case 'default':
38
+ $this->session_ids[$place] = sha1( $this->generate_password() );
39
  break;
40
  }
41
  }
42
  }
43
 
44
+ if ( ! isset( $_COOKIE['mc_session_ids'] ) ) {
45
+ setcookie( 'mc_session_ids[default]', $this->session_ids['default'], current_time( 'timestamp', true ) + apply_filters( 'math_captcha_time', Math_Captcha()->options['general']['time'] ), COOKIEPATH, COOKIE_DOMAIN, (isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] !== 'off' ? true : false ), true );
 
46
 
47
+ for ( $i = 0; $i < 5; $i ++ ) {
48
+ setcookie( 'mc_session_ids[multi][' . $i . ']', $this->session_ids['multi'][$i], current_time( 'timestamp', true ) + apply_filters( 'math_captcha_time', Math_Captcha()->options['general']['time'] ), COOKIEPATH, COOKIE_DOMAIN );
 
49
  }
50
  }
51
  }
52
 
 
53
  /**
54
  * Generate password helper, without wp_rand() call
55
+ */
56
+ private function generate_password( $length = 64 ) {
 
57
  $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
58
  $password = '';
59
 
60
+ for ( $i = 0; $i < $length; $i ++ ) {
61
+ $password .= substr( $chars, mt_rand( 0, strlen( $chars ) - 1 ), 1 );
 
62
  }
63
 
64
  return $password;
65
  }
66
+
67
+ }
includes/class-core.php CHANGED
@@ -1,282 +1,231 @@
1
  <?php
2
- if(!defined('ABSPATH')) exit;
 
3
 
4
  new Math_Captcha_Core();
5
 
6
- class Math_Captcha_Core
7
- {
8
  public $session_number = 0;
9
  public $login_failed = false;
10
  public $error_messages;
11
  public $errors;
12
 
13
-
14
  /**
15
  *
16
- */
17
- public function __construct()
18
- {
19
  // sets instance
20
  Math_Captcha()->core = $this;
21
 
22
  // actions
23
- add_action('init', array(&$this, 'load_actions_filters'), 1);
24
- add_action('plugins_loaded', array(&$this, 'load_defaults'));
25
- add_action('admin_init', array(&$this, 'flush_rewrites'));
26
 
27
  // filters
28
- add_filter('shake_error_codes', array(&$this, 'add_shake_error_codes'), 1);
29
- add_filter('mod_rewrite_rules', array(&$this, 'block_direct_comments'));
30
  }
31
 
32
-
33
  /**
34
  * Load defaults
35
- */
36
- public function load_defaults()
37
- {
38
  $this->error_messages = array(
39
- 'fill' => '<strong>'. __('ERROR', 'math-captcha').'</strong>: '.__('Please enter captcha value.', 'math-captcha'),
40
- 'wrong' => '<strong>'. __('ERROR', 'math-captcha').'</strong>: '.__('Invalid captcha value.', 'math-captcha'),
41
- 'time' => '<strong>'. __('ERROR', 'math-captcha').'</strong>: '.__('Captcha time expired.', 'math-captcha')
42
  );
43
  }
44
 
45
-
46
  /**
47
  * Loads required filters
48
- */
49
- public function load_actions_filters()
50
- {
51
  // Contact Form 7
52
- if(Math_Captcha()->options['general']['enable_for']['contact_form_7'] && class_exists('WPCF7_ContactForm'))
53
- include_once(MATH_CAPTCHA_PATH.'includes/integrations/contact-form-7.php');
54
 
55
- if(is_admin())
56
  return;
57
 
58
- $action = (isset($_GET['action']) && $_GET['action'] !== '' ? $_GET['action'] : null);
59
 
60
  // comments
61
- if(Math_Captcha()->options['general']['enable_for']['comment_form'])
62
- {
63
- if(!is_user_logged_in())
64
- add_action('comment_form_after_fields', array(&$this, 'add_captcha_form'));
65
- elseif(!Math_Captcha()->options['general']['hide_for_logged_users'])
66
- add_action('comment_form_logged_in_after', array(&$this, 'add_captcha_form'));
67
-
68
- add_filter('preprocess_comment', array(&$this, 'add_comment_with_captcha'));
69
  }
70
 
71
  // registration
72
- if(Math_Captcha()->options['general']['enable_for']['registration_form'] && (!is_user_logged_in() || (is_user_logged_in() && !Math_Captcha()->options['general']['hide_for_logged_users'])) && $action === 'register')
73
- {
74
- add_action('register_form', array(&$this, 'add_captcha_form'));
75
- add_action('register_post', array(&$this, 'add_user_with_captcha'), 10, 3);
76
- add_action('signup_extra_fields', array(&$this, 'add_captcha_form'));
77
- add_filter('wpmu_validate_user_signup', array(&$this, 'validate_user_with_captcha'));
78
  }
79
 
80
  // lost password
81
- if(Math_Captcha()->options['general']['enable_for']['reset_password_form'] && (!is_user_logged_in() || (is_user_logged_in() && !Math_Captcha()->options['general']['hide_for_logged_users'])) && $action === 'lostpassword')
82
- {
83
- add_action('lostpassword_form', array(&$this, 'add_captcha_form'));
84
- add_action('lostpassword_post', array(&$this, 'check_lost_password_with_captcha'));
85
  }
86
 
87
  // login
88
- if(Math_Captcha()->options['general']['enable_for']['login_form'] && (!is_user_logged_in() || (is_user_logged_in() && !Math_Captcha()->options['general']['hide_for_logged_users'])) && $action === null)
89
- {
90
- add_action('login_form', array(&$this, 'add_captcha_form'));
91
- add_filter('login_redirect', array(&$this, 'redirect_login_with_captcha'), 10, 3);
92
- add_filter('authenticate', array(&$this, 'authenticate_user'), 1000, 3);
93
  }
94
 
95
  // bbPress
96
- if(Math_Captcha()->options['general']['enable_for']['bbpress'] && class_exists('bbPress') && (!is_user_logged_in() || (is_user_logged_in() && !Math_Captcha()->options['general']['hide_for_logged_users'])))
97
- {
98
- add_action('bbp_theme_after_reply_form_content', array(&$this, 'add_bbp_captcha_form'));
99
- add_action('bbp_theme_after_topic_form_content', array(&$this, 'add_bbp_captcha_form'));
100
- add_action('bbp_new_reply_pre_extras', array(&$this, 'check_bbpress_captcha'));
101
- add_action('bbp_new_topic_pre_extras', array(&$this, 'check_bbpress_captcha'));
102
  }
103
  }
104
 
105
-
106
  /**
107
  * Adds lost password errors
108
- */
109
- public function add_lostpassword_captcha_message($errors)
110
- {
111
- return $errors.$this->errors->errors['math-captcha-error'][0];
112
  }
113
 
114
-
115
  /**
116
  * Adds lost password errors (special way)
117
- */
118
- public function add_lostpassword_wp_message()
119
- {
120
  return $this->errors;
121
  }
122
 
123
-
124
  /**
125
  * Validates lost password form
126
- */
127
- public function check_lost_password_with_captcha()
128
- {
129
  $this->errors = new WP_Error();
130
  $user_error = false;
131
  $user_data = null;
132
 
133
  // checks captcha
134
- if(isset($_POST['mc-value']) && $_POST['mc-value'] !== '')
135
- {
136
- if(Math_Captcha()->cookie_session->session_ids['default'] !== '' && get_transient('mc_'.Math_Captcha()->cookie_session->session_ids['default']) !== false)
137
- {
138
- if(strcmp(get_transient('mc_'.Math_Captcha()->cookie_session->session_ids['default']), sha1(AUTH_KEY.$_POST['mc-value'].Math_Captcha()->cookie_session->session_ids['default'], false)) !== 0)
139
- $this->errors->add('math-captcha-error', $this->error_messages['wrong']);
140
- }
141
- else
142
- $this->errors->add('math-captcha-error', $this->error_messages['time']);
143
- }
144
- else
145
- $this->errors->add('math-captcha-error', $this->error_messages['fill']);
146
 
147
  // checks user_login (from wp-login.php)
148
- if(empty($_POST['user_login']))
149
  $user_error = true;
150
- elseif(strpos($_POST['user_login'], '@'))
151
- {
152
- $user_data = get_user_by('email', trim($_POST['user_login']));
153
 
154
- if(empty($user_data))
155
  $user_error = true;
156
- }
157
- else
158
- $user_data = get_user_by('login', trim($_POST['user_login']));
159
 
160
- if(!$user_data)
161
  $user_error = true;
162
 
163
  // something went wrong?
164
- if(!empty($this->errors->errors))
165
- {
166
  // nasty hack (captcha is wrong but user_login is fine)
167
- if($user_error === false)
168
- add_filter('allow_password_reset', array(&$this, 'add_lostpassword_wp_message'));
169
  else
170
- add_filter('login_errors', array(&$this, 'add_lostpassword_captcha_message'));
171
  }
172
  }
173
 
174
-
175
  /**
176
  * Validates register form
177
- */
178
- public function add_user_with_captcha($login, $email, $errors)
179
- {
180
- if(isset($_POST['mc-value']) && $_POST['mc-value'] !== '')
181
- {
182
- if(Math_Captcha()->cookie_session->session_ids['default'] !== '' && get_transient('mc_'.Math_Captcha()->cookie_session->session_ids['default']) !== false)
183
- {
184
- if(strcmp(get_transient('mc_'.Math_Captcha()->cookie_session->session_ids['default']), sha1(AUTH_KEY.$_POST['mc-value'].Math_Captcha()->cookie_session->session_ids['default'], false)) !== 0)
185
- $errors->add('math-captcha-error', $this->error_messages['wrong']);
186
- }
187
- else
188
- $errors->add('math-captcha-error', $this->error_messages['time']);
189
- }
190
- else
191
- $errors->add('math-captcha-error', $this->error_messages['fill']);
192
 
193
  return $errors;
194
  }
195
 
196
-
197
  /**
198
  * Validates register form
199
- */
200
- public function validate_user_with_captcha($result)
201
- {
202
- if(isset($_POST['mc-value']) && $_POST['mc-value'] !== '')
203
- {
204
- if(Math_Captcha()->cookie_session->session_ids['default'] !== '' && get_transient('mc_'.Math_Captcha()->cookie_session->session_ids['default']) !== false)
205
- {
206
- if(strcmp(get_transient('mc_'.Math_Captcha()->cookie_session->session_ids['default']), sha1(AUTH_KEY.$_POST['mc-value'].Math_Captcha()->cookie_session->session_ids['default'], false)) !== 0)
207
- $result['errors']->add('math-captcha-error', $this->error_messages['wrong']);
208
- }
209
- else
210
- $result['errors']->add('math-captcha-error', $this->error_messages['time']);
211
- }
212
- else
213
- $result['errors']->add('math-captcha-error', $this->error_messages['fill']);
214
 
215
  return $result;
216
  }
217
 
218
-
219
  /**
220
  * Posts login form
221
- */
222
- public function redirect_login_with_captcha($redirect, $bool, $errors)
223
- {
224
- if($this->login_failed === false && !empty($_POST))
225
- {
226
  $error = '';
227
 
228
- if(isset($_POST['mc-value']) && $_POST['mc-value'] !== '')
229
- {
230
- if(Math_Captcha()->cookie_session->session_ids['default'] !== '' && get_transient('mc_'.Math_Captcha()->cookie_session->session_ids['default']) !== false)
231
- {
232
- if(strcmp(get_transient('mc_'.Math_Captcha()->cookie_session->session_ids['default']), sha1(AUTH_KEY.$_POST['mc-value'].Math_Captcha()->cookie_session->session_ids['default'], false)) !== 0)
233
  $error = 'wrong';
234
- }
235
- else
236
  $error = 'time';
237
- }
238
- else
239
  $error = 'fill';
240
 
241
- if(is_wp_error($errors) && !empty($error))
242
- $errors->add('math-captcha-error', $this->error_messages[$error]);
243
  }
244
 
245
  return $redirect;
246
  }
247
 
248
-
249
  /**
250
  * Authenticate user
251
  */
252
- public function authenticate_user($user, $username, $password)
253
- {
254
  // user gave us valid login and password
255
- if(!is_wp_error($user))
256
- {
257
- if(!empty($_POST))
258
- {
259
- if(isset($_POST['mc-value']) && $_POST['mc-value'] !== '')
260
- {
261
- if(Math_Captcha()->cookie_session->session_ids['default'] !== '' && get_transient('mc_'.Math_Captcha()->cookie_session->session_ids['default']) !== false)
262
- {
263
- if(strcmp(get_transient('mc_'.Math_Captcha()->cookie_session->session_ids['default']), sha1(AUTH_KEY.$_POST['mc-value'].Math_Captcha()->cookie_session->session_ids['default'], false)) !== 0)
264
  $error = 'wrong';
265
- }
266
- else
267
  $error = 'time';
268
- }
269
- else
270
  $error = 'fill';
271
  }
272
 
273
- if(!empty($error))
274
- {
275
  // destroy cookie
276
  wp_clear_auth_cookie();
277
 
278
  $user = new WP_Error();
279
- $user->add('math-captcha-error', $this->error_messages[$error]);
280
 
281
  // inform redirect function that we failed to login
282
  $this->login_failed = true;
@@ -286,182 +235,158 @@ class Math_Captcha_Core
286
  return $user;
287
  }
288
 
289
-
290
  /**
291
  * Adds shake
292
- */
293
- public function add_shake_error_codes($codes)
294
- {
295
  $codes[] = 'math-captcha-error';
296
 
297
  return $codes;
298
  }
299
 
300
-
301
  /**
302
  * Adds captcha to comment form
303
- */
304
- public function add_comment_with_captcha($comment)
305
- {
306
- if(isset($_POST['mc-value']) && (!is_admin() || DOING_AJAX) && ($comment['comment_type'] === '' || $comment['comment_type'] === 'comment'))
307
- {
308
- if($_POST['mc-value'] !== '')
309
- {
310
- if(Math_Captcha()->cookie_session->session_ids['default'] !== '' && get_transient('mc_'.Math_Captcha()->cookie_session->session_ids['default']) !== false)
311
- {
312
- if(strcmp(get_transient('mc_'.Math_Captcha()->cookie_session->session_ids['default']), sha1(AUTH_KEY.$_POST['mc-value'].Math_Captcha()->cookie_session->session_ids['default'], false)) === 0)
313
  return $comment;
314
  else
315
- wp_die($this->error_messages['wrong']);
316
- }
317
- else
318
- wp_die($this->error_messages['time']);
319
- }
320
- else
321
- wp_die($this->error_messages['fill']);
322
- }
323
- else
324
  return $comment;
325
  }
326
 
327
-
328
  /**
329
  * Shows and generates captcha
330
- */
331
- public function add_captcha_form()
332
- {
333
- if(is_admin())
334
  return;
335
 
336
- $captcha_title = apply_filters('math_captcha_title', Math_Captcha()->options['general']['title']);
337
 
338
  echo '
339
  <p class="math-captcha-form">';
340
 
341
- if(!empty($captcha_title))
342
  echo '
343
- <label>'.$captcha_title.'<br/></label>';
344
 
345
  echo '
346
- <span>'.$this->generate_captcha_phrase('default').'</span>
347
  </p>';
348
  }
349
 
350
-
351
  /**
352
  * Shows and generates captcha for bbPress
353
- */
354
- public function add_bbp_captcha_form()
355
- {
356
- if(is_admin())
357
  return;
358
 
359
- $captcha_title = apply_filters('math_captcha_title', Math_Captcha()->options['general']['title']);
360
 
361
  echo '
362
  <p class="math-captcha-form">';
363
 
364
- if(!empty($captcha_title))
365
  echo '
366
- <label>'.$captcha_title.'<br/></label>';
367
 
368
  echo '
369
- <span>'.$this->generate_captcha_phrase('bbpress').'</span>
370
  </p>';
371
  }
372
 
373
-
374
  /**
375
  * Validates bbpress topics and replies
376
- */
377
- public function check_bbpress_captcha()
378
- {
379
- if(isset($_POST['mc-value']) && $_POST['mc-value'] !== '')
380
- {
381
- if(Math_Captcha()->cookie_session->session_ids['default'] !== '' && get_transient('bbp_'.Math_Captcha()->cookie_session->session_ids['default']) !== false)
382
- {
383
- if(strcmp(get_transient('bbp_'.Math_Captcha()->cookie_session->session_ids['default']), sha1(AUTH_KEY.$_POST['mc-value'].Math_Captcha()->cookie_session->session_ids['default'], false)) !== 0)
384
- bbp_add_error('math-captcha-wrong', $this->error_messages['wrong']);
385
- }
386
- else
387
- bbp_add_error('math-captcha-wrong', $this->error_messages['time']);
388
- }
389
- else
390
- bbp_add_error('math-captcha-wrong', $this->error_messages['fill']);
391
  }
392
 
393
-
394
  /**
395
  * Encodes chars
396
- */
397
- private function encode_operation($string)
398
- {
399
- $chars = str_split($string);
400
- $seed = mt_rand(0, (int)abs(crc32($string) / strlen($string)));
401
 
402
- foreach($chars as $key => $char)
403
- {
404
- $ord = ord($char);
405
 
406
  // ignore non-ascii chars
407
- if($ord < 128)
408
- {
409
  // pseudo "random function"
410
  $r = ($seed * (1 + $key)) % 100;
411
 
412
- if($r > 60 && $char !== '@') {} // plain character (not encoded), if not @-sign
413
- elseif($r < 45) $chars[$key] = '&#x'.dechex($ord).';'; // hexadecimal
414
- else $chars[$key] = '&#'.$ord.';'; // decimal (ascii)
 
 
 
 
415
  }
416
  }
417
 
418
- return implode('', $chars);
419
  }
420
 
421
-
422
  /**
423
  * Converts numbers to words
424
- */
425
- private function numberToWords($number)
426
- {
427
  $words = array(
428
- 1 => __('one', 'math-captcha'),
429
- 2 => __('two', 'math-captcha'),
430
- 3 => __('three', 'math-captcha'),
431
- 4 => __('four', 'math-captcha'),
432
- 5 => __('five', 'math-captcha'),
433
- 6 => __('six', 'math-captcha'),
434
- 7 => __('seven', 'math-captcha'),
435
- 8 => __('eight', 'math-captcha'),
436
- 9 => __('nine', 'math-captcha'),
437
- 10 => __('ten', 'math-captcha'),
438
- 11 => __('eleven', 'math-captcha'),
439
- 12 => __('twelve', 'math-captcha'),
440
- 13 => __('thirteen', 'math-captcha'),
441
- 14 => __('fourteen', 'math-captcha'),
442
- 15 => __('fifteen', 'math-captcha'),
443
- 16 => __('sixteen', 'math-captcha'),
444
- 17 => __('seventeen', 'math-captcha'),
445
- 18 => __('eighteen', 'math-captcha'),
446
- 19 => __('nineteen', 'math-captcha'),
447
- 20 => __('twenty', 'math-captcha'),
448
- 30 => __('thirty', 'math-captcha'),
449
- 40 => __('forty', 'math-captcha'),
450
- 50 => __('fifty', 'math-captcha'),
451
- 60 => __('sixty', 'math-captcha'),
452
- 70 => __('seventy', 'math-captcha'),
453
- 80 => __('eighty', 'math-captcha'),
454
- 90 => __('ninety', 'math-captcha')
455
  );
456
 
457
- if(isset($words[$number]))
458
  return $words[$number];
459
- else
460
- {
461
  $reverse = false;
462
 
463
- switch(get_bloginfo('language'))
464
- {
465
  case 'de-DE':
466
  $spacer = 'und';
467
  $reverse = true;
@@ -471,7 +396,7 @@ class Math_Captcha_Core
471
  $spacer = 'en';
472
  $reverse = true;
473
  break;
474
-
475
  case 'ru-RU':
476
  case 'pl-PL':
477
  case 'en-EN':
@@ -479,269 +404,214 @@ class Math_Captcha_Core
479
  $spacer = ' ';
480
  }
481
 
482
- $first = (int)(substr($number, 0, 1) * 10);
483
- $second = (int)substr($number, -1);
484
 
485
- return ($reverse === false ? $words[$first].$spacer.$words[$second] : $words[$second].$spacer.$words[$first]);
486
  }
487
  }
488
 
489
-
490
  /**
491
  * Generates captcha
492
- */
493
- public function generate_captcha_phrase($form = '')
494
- {
495
  $ops = array(
496
- 'addition' => '+',
497
- 'subtraction' => '&#8722;',
498
  'multiplication' => '&#215;',
499
- 'division' => '&#247;',
500
  );
501
 
502
  $operations = $groups = array();
503
  $input = '<input type="text" size="2" length="2" id="mc-input" class="mc-input" name="mc-value" value="" aria-required="true"/>';
504
 
505
  // available operations
506
- foreach(Math_Captcha()->options['general']['mathematical_operations'] as $operation => $enable)
507
- {
508
- if($enable === true)
509
  $operations[] = $operation;
510
  }
511
 
512
  // available groups
513
- foreach(Math_Captcha()->options['general']['groups'] as $group => $enable)
514
- {
515
- if($enable === true)
516
  $groups[] = $group;
517
  }
518
 
519
  // number of groups
520
- $ao = count($groups);
521
 
522
  // operation
523
- $rnd_op = $operations[mt_rand(0, count($operations) - 1)];
524
  $number[3] = $ops[$rnd_op];
525
 
526
  // place where to put empty input
527
- $rnd_input = mt_rand(0, 2);
528
 
529
  // which random operation
530
- switch($rnd_op)
531
- {
532
  case 'addition':
533
- if($rnd_input === 0)
534
- {
535
- $number[0] = mt_rand(1, 10);
536
- $number[1] = mt_rand(1, 89);
537
- }
538
- elseif($rnd_input === 1)
539
- {
540
- $number[0] = mt_rand(1, 89);
541
- $number[1] = mt_rand(1, 10);
542
- }
543
- elseif($rnd_input === 2)
544
- {
545
- $number[0] = mt_rand(1, 9);
546
- $number[1] = mt_rand(1, 10 - $number[0]);
547
  }
548
 
549
  $number[2] = $number[0] + $number[1];
550
  break;
551
 
552
  case 'subtraction':
553
- if($rnd_input === 0)
554
- {
555
- $number[0] = mt_rand(2, 10);
556
- $number[1] = mt_rand(1, $number[0] - 1);
557
- }
558
- elseif($rnd_input === 1)
559
- {
560
- $number[0] = mt_rand(11, 99);
561
- $number[1] = mt_rand(1, 10);
562
- }
563
- elseif($rnd_input === 2)
564
- {
565
- $number[0] = mt_rand(11, 99);
566
- $number[1] = mt_rand($number[0] - 10, $number[0] - 1);
567
  }
568
 
569
  $number[2] = $number[0] - $number[1];
570
  break;
571
 
572
  case 'multiplication':
573
- if($rnd_input === 0)
574
- {
575
- $number[0] = mt_rand(1, 10);
576
- $number[1] = mt_rand(1, 9);
577
- }
578
- elseif($rnd_input === 1)
579
- {
580
- $number[0] = mt_rand(1, 9);
581
- $number[1] = mt_rand(1, 10);
582
- }
583
- elseif($rnd_input === 2)
584
- {
585
- $number[0] = mt_rand(1, 10);
586
- $number[1] = ($number[0] > 5 ? 1 : ($number[0] === 4 && $number[0] === 5 ? mt_rand(1, 2) : ($number[0] === 3 ? mt_rand(1, 3) : ($number[0] === 2 ? mt_rand(1, 5) : mt_rand(1, 10)))));
587
  }
588
 
589
  $number[2] = $number[0] * $number[1];
590
  break;
591
 
592
  case 'division':
593
- $divide = array(1 => 99, 2 => 49, 3 => 33, 4 => 24, 5 => 19, 6 => 16, 7 => 14, 8 => 12, 9 => 11, 10 => 9);
594
-
595
- if($rnd_input === 0)
596
- {
597
- $divide = array(2 => array(1, 2), 3 => array(1, 3), 4 => array(1, 2, 4), 5 => array(1, 5), 6 => array(1, 2, 3, 6), 7 => array(1, 7), 8 => array(1, 2, 4, 8), 9 => array(1, 3, 9), 10 => array(1, 2, 5, 10));
598
- $number[0] = mt_rand(2, 10);
599
- $number[1] = $divide[$number[0]][mt_rand(0, count($divide[$number[0]]) - 1)];
600
- }
601
- elseif($rnd_input === 1)
602
- {
603
- $number[1] = mt_rand(1, 10);
604
- $number[0] = $number[1] * mt_rand(1, $divide[$number[1]]);
605
- }
606
- elseif($rnd_input === 2)
607
- {
608
- $number[2] = mt_rand(1, 10);
609
- $number[0] = $number[2] * mt_rand(1, $divide[$number[2]]);
610
- $number[1] = (int)($number[0] / $number[2]);
611
  }
612
 
613
- if(!isset($number[2]))
614
- $number[2] = (int)($number[0] / $number[1]);
615
 
616
  break;
617
  }
618
 
619
  // words
620
- if($ao === 1 && $groups[0] === 'words')
621
- {
622
- if($rnd_input === 0)
623
- {
624
- $number[1] = $this->numberToWords($number[1]);
625
- $number[2] = $this->numberToWords($number[2]);
626
- }
627
- elseif($rnd_input === 1)
628
- {
629
- $number[0] = $this->numberToWords($number[0]);
630
- $number[2] = $this->numberToWords($number[2]);
631
- }
632
- elseif($rnd_input === 2)
633
- {
634
- $number[0] = $this->numberToWords($number[0]);
635
- $number[1] = $this->numberToWords($number[1]);
636
  }
637
  }
638
  // numbers and words
639
- elseif($ao === 2)
640
- {
641
- if($rnd_input === 0)
642
- {
643
- if(mt_rand(1, 2) === 2)
644
- {
645
- $number[1] = $this->numberToWords($number[1]);
646
- $number[2] = $this->numberToWords($number[2]);
647
- }
648
- else
649
- $number[$tmp = mt_rand(1, 2)] = $this->numberToWords($number[$tmp]);
650
  }
651
- elseif($rnd_input === 1)
652
- {
653
- if(mt_rand(1, 2) === 2)
654
- {
655
- $number[0] = $this->numberToWords($number[0]);
656
- $number[2] = $this->numberToWords($number[2]);
657
- }
658
- else
659
- $number[$tmp = array_rand(array(0 => 0, 2 => 2), 1)] = $this->numberToWords($number[$tmp]);
660
  }
661
- elseif($rnd_input === 2)
662
- {
663
- if(mt_rand(1, 2) === 2)
664
- {
665
- $number[0] = $this->numberToWords($number[0]);
666
- $number[1] = $this->numberToWords($number[1]);
667
- }
668
- else
669
- $number[$tmp = mt_rand(0, 1)] = $this->numberToWords($number[$tmp]);
670
  }
671
  }
672
 
673
- if(in_array($form, array('default', 'bbpress'), true))
674
- {
675
  // position of empty input
676
- if($rnd_input === 0)
677
- $return = $input.' '.$number[3].' '.$this->encode_operation($number[1]).' = '.$this->encode_operation($number[2]);
678
- elseif($rnd_input === 1)
679
- $return = $this->encode_operation($number[0]).' '.$number[3].' '.$input.' = '.$this->encode_operation($number[2]);
680
- elseif($rnd_input === 2)
681
- $return = $this->encode_operation($number[0]).' '.$number[3].' '.$this->encode_operation($number[1]).' = '.$input;
682
 
683
  $transient_name = ($form === 'bbpress' ? 'bbp' : 'mc');
684
  $session_id = Math_Captcha()->cookie_session->session_ids['default'];
685
  }
686
- elseif($form === 'cf7')
687
- {
688
  $return = array();
689
 
690
- if($rnd_input === 0)
691
- {
692
  $return['input'] = 1;
693
- $return[2] = ' '.$number[3].' '.$this->encode_operation($number[1]).' = ';
694
- $return[3] = $this->encode_operation($number[2]);
695
- }
696
- elseif($rnd_input === 1)
697
- {
698
- $return[1] = $this->encode_operation($number[0]).' '.$number[3].' ';
699
  $return['input'] = 2;
700
- $return[3] = ' = '.$this->encode_operation($number[2]);
701
- }
702
- elseif($rnd_input === 2)
703
- {
704
- $return[1] = $this->encode_operation($number[0]).' '.$number[3].' ';
705
- $return[2] = $this->encode_operation($number[1]).' = ';
706
  $return['input'] = 3;
707
  }
708
 
709
  $transient_name = 'cf7';
710
- $session_id = Math_Captcha()->cookie_session->session_ids['multi'][$this->session_number++];
711
  }
712
 
713
- set_transient($transient_name.'_'.$session_id, sha1(AUTH_KEY.$number[$rnd_input].$session_id, false), apply_filters('math_captcha_time', Math_Captcha()->options['general']['time']));
714
 
715
  return $return;
716
  }
717
 
718
-
719
  /**
720
  *
721
- */
722
- public function flush_rewrites()
723
- {
724
- if(Math_Captcha()->options['general']['flush_rules'])
725
- {
726
  global $wp_rewrite;
727
 
728
  $wp_rewrite->flush_rules();
729
 
730
  Math_Captcha()->options['general']['flush_rules'] = false;
731
- update_option('math_captcha_options', Math_Captcha()->options['general']);
732
  }
733
  }
734
 
735
-
736
  /**
737
  *
738
- */
739
- public function block_direct_comments($rules)
740
- {
741
- if(Math_Captcha()->options['general']['block_direct_comments'])
742
- {
743
- $new_rules =
744
- <<<EOT
745
  \n# BEGIN Math Captcha
746
  <IfModule mod_rewrite.c>
747
  RewriteEngine On
@@ -754,10 +624,10 @@ RewriteRule (.*) ^http://%{REMOTE_ADDR}/$ [R=301,L]
754
  # END Math Captcha\n\n
755
  EOT;
756
 
757
- return $new_rules.$rules;
758
  }
759
 
760
  return $rules;
761
  }
762
- }
763
- ?>
1
  <?php
2
+ if ( ! defined( 'ABSPATH' ) )
3
+ exit;
4
 
5
  new Math_Captcha_Core();
6
 
7
+ class Math_Captcha_Core {
8
+
9
  public $session_number = 0;
10
  public $login_failed = false;
11
  public $error_messages;
12
  public $errors;
13
 
 
14
  /**
15
  *
16
+ */
17
+ public function __construct() {
 
18
  // sets instance
19
  Math_Captcha()->core = $this;
20
 
21
  // actions
22
+ add_action( 'init', array( &$this, 'load_actions_filters' ), 1 );
23
+ add_action( 'plugins_loaded', array( &$this, 'load_defaults' ) );
24
+ add_action( 'admin_init', array( &$this, 'flush_rewrites' ) );
25
 
26
  // filters
27
+ add_filter( 'shake_error_codes', array( &$this, 'add_shake_error_codes' ), 1 );
28
+ add_filter( 'mod_rewrite_rules', array( &$this, 'block_direct_comments' ) );
29
  }
30
 
 
31
  /**
32
  * Load defaults
33
+ */
34
+ public function load_defaults() {
 
35
  $this->error_messages = array(
36
+ 'fill' => '<strong>' . __( 'ERROR', 'math-captcha' ) . '</strong>: ' . __( 'Please enter captcha value.', 'math-captcha' ),
37
+ 'wrong' => '<strong>' . __( 'ERROR', 'math-captcha' ) . '</strong>: ' . __( 'Invalid captcha value.', 'math-captcha' ),
38
+ 'time' => '<strong>' . __( 'ERROR', 'math-captcha' ) . '</strong>: ' . __( 'Captcha time expired.', 'math-captcha' )
39
  );
40
  }
41
 
 
42
  /**
43
  * Loads required filters
44
+ */
45
+ public function load_actions_filters() {
 
46
  // Contact Form 7
47
+ if ( Math_Captcha()->options['general']['enable_for']['contact_form_7'] && class_exists( 'WPCF7_ContactForm' ) )
48
+ include_once(MATH_CAPTCHA_PATH . 'includes/integrations/contact-form-7.php');
49
 
50
+ if ( is_admin() )
51
  return;
52
 
53
+ $action = (isset( $_GET['action'] ) && $_GET['action'] !== '' ? $_GET['action'] : null);
54
 
55
  // comments
56
+ if ( Math_Captcha()->options['general']['enable_for']['comment_form'] ) {
57
+ if ( ! is_user_logged_in() )
58
+ add_action( 'comment_form_after_fields', array( &$this, 'add_captcha_form' ) );
59
+ elseif ( ! Math_Captcha()->options['general']['hide_for_logged_users'] )
60
+ add_action( 'comment_form_logged_in_after', array( &$this, 'add_captcha_form' ) );
61
+
62
+ add_filter( 'preprocess_comment', array( &$this, 'add_comment_with_captcha' ) );
 
63
  }
64
 
65
  // registration
66
+ if ( Math_Captcha()->options['general']['enable_for']['registration_form'] && ( ! is_user_logged_in() || (is_user_logged_in() && ! Math_Captcha()->options['general']['hide_for_logged_users'])) && $action === 'register' ) {
67
+ add_action( 'register_form', array( &$this, 'add_captcha_form' ) );
68
+ add_action( 'register_post', array( &$this, 'add_user_with_captcha' ), 10, 3 );
69
+ add_action( 'signup_extra_fields', array( &$this, 'add_captcha_form' ) );
70
+ add_filter( 'wpmu_validate_user_signup', array( &$this, 'validate_user_with_captcha' ) );
 
71
  }
72
 
73
  // lost password
74
+ if ( Math_Captcha()->options['general']['enable_for']['reset_password_form'] && ( ! is_user_logged_in() || (is_user_logged_in() && ! Math_Captcha()->options['general']['hide_for_logged_users'])) && $action === 'lostpassword' ) {
75
+ add_action( 'lostpassword_form', array( &$this, 'add_captcha_form' ) );
76
+ add_action( 'lostpassword_post', array( &$this, 'check_lost_password_with_captcha' ) );
 
77
  }
78
 
79
  // login
80
+ if ( Math_Captcha()->options['general']['enable_for']['login_form'] && ( ! is_user_logged_in() || (is_user_logged_in() && ! Math_Captcha()->options['general']['hide_for_logged_users'])) && $action === null ) {
81
+ add_action( 'login_form', array( &$this, 'add_captcha_form' ) );
82
+ add_filter( 'login_redirect', array( &$this, 'redirect_login_with_captcha' ), 10, 3 );
83
+ add_filter( 'authenticate', array( &$this, 'authenticate_user' ), 1000, 3 );
 
84
  }
85
 
86
  // bbPress
87
+ if ( Math_Captcha()->options['general']['enable_for']['bbpress'] && class_exists( 'bbPress' ) && ( ! is_user_logged_in() || (is_user_logged_in() && ! Math_Captcha()->options['general']['hide_for_logged_users'])) ) {
88
+ add_action( 'bbp_theme_after_reply_form_content', array( &$this, 'add_bbp_captcha_form' ) );
89
+ add_action( 'bbp_theme_after_topic_form_content', array( &$this, 'add_bbp_captcha_form' ) );
90
+ add_action( 'bbp_new_reply_pre_extras', array( &$this, 'check_bbpress_captcha' ) );
91
+ add_action( 'bbp_new_topic_pre_extras', array( &$this, 'check_bbpress_captcha' ) );
 
92
  }
93
  }
94
 
 
95
  /**
96
  * Adds lost password errors
97
+ */
98
+ public function add_lostpassword_captcha_message( $errors ) {
99
+ return $errors . $this->errors->errors['math-captcha-error'][0];
 
100
  }
101
 
 
102
  /**
103
  * Adds lost password errors (special way)
104
+ */
105
+ public function add_lostpassword_wp_message() {
 
106
  return $this->errors;
107
  }
108
 
 
109
  /**
110
  * Validates lost password form
111
+ */
112
+ public function check_lost_password_with_captcha() {
 
113
  $this->errors = new WP_Error();
114
  $user_error = false;
115
  $user_data = null;
116
 
117
  // checks captcha
118
+ if ( isset( $_POST['mc-value'] ) && $_POST['mc-value'] !== '' ) {
119
+ if ( Math_Captcha()->cookie_session->session_ids['default'] !== '' && get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ) !== false ) {
120
+ if ( strcmp( get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ), sha1( AUTH_KEY . $_POST['mc-value'] . Math_Captcha()->cookie_session->session_ids['default'], false ) ) !== 0 )
121
+ $this->errors->add( 'math-captcha-error', $this->error_messages['wrong'] );
122
+ } else
123
+ $this->errors->add( 'math-captcha-error', $this->error_messages['time'] );
124
+ } else
125
+ $this->errors->add( 'math-captcha-error', $this->error_messages['fill'] );
 
 
 
 
126
 
127
  // checks user_login (from wp-login.php)
128
+ if ( empty( $_POST['user_login'] ) )
129
  $user_error = true;
130
+ elseif ( strpos( $_POST['user_login'], '@' ) ) {
131
+ $user_data = get_user_by( 'email', trim( $_POST['user_login'] ) );
 
132
 
133
+ if ( empty( $user_data ) )
134
  $user_error = true;
135
+ } else
136
+ $user_data = get_user_by( 'login', trim( $_POST['user_login'] ) );
 
137
 
138
+ if ( ! $user_data )
139
  $user_error = true;
140
 
141
  // something went wrong?
142
+ if ( ! empty( $this->errors->errors ) ) {
 
143
  // nasty hack (captcha is wrong but user_login is fine)
144
+ if ( $user_error === false )
145
+ add_filter( 'allow_password_reset', array( &$this, 'add_lostpassword_wp_message' ) );
146
  else
147
+ add_filter( 'login_errors', array( &$this, 'add_lostpassword_captcha_message' ) );
148
  }
149
  }
150
 
 
151
  /**
152
  * Validates register form
153
+ */
154
+ public function add_user_with_captcha( $login, $email, $errors ) {
155
+ if ( isset( $_POST['mc-value'] ) && $_POST['mc-value'] !== '' ) {
156
+ if ( Math_Captcha()->cookie_session->session_ids['default'] !== '' && get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ) !== false ) {
157
+ if ( strcmp( get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ), sha1( AUTH_KEY . $_POST['mc-value'] . Math_Captcha()->cookie_session->session_ids['default'], false ) ) !== 0 )
158
+ $errors->add( 'math-captcha-error', $this->error_messages['wrong'] );
159
+ } else
160
+ $errors->add( 'math-captcha-error', $this->error_messages['time'] );
161
+ } else
162
+ $errors->add( 'math-captcha-error', $this->error_messages['fill'] );
 
 
 
 
 
163
 
164
  return $errors;
165
  }
166
 
 
167
  /**
168
  * Validates register form
169
+ */
170
+ public function validate_user_with_captcha( $result ) {
171
+ if ( isset( $_POST['mc-value'] ) && $_POST['mc-value'] !== '' ) {
172
+ if ( Math_Captcha()->cookie_session->session_ids['default'] !== '' && get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ) !== false ) {
173
+ if ( strcmp( get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ), sha1( AUTH_KEY . $_POST['mc-value'] . Math_Captcha()->cookie_session->session_ids['default'], false ) ) !== 0 )
174
+ $result['errors']->add( 'math-captcha-error', $this->error_messages['wrong'] );
175
+ } else
176
+ $result['errors']->add( 'math-captcha-error', $this->error_messages['time'] );
177
+ } else
178
+ $result['errors']->add( 'math-captcha-error', $this->error_messages['fill'] );
 
 
 
 
 
179
 
180
  return $result;
181
  }
182
 
 
183
  /**
184
  * Posts login form
185
+ */
186
+ public function redirect_login_with_captcha( $redirect, $bool, $errors ) {
187
+ if ( $this->login_failed === false && ! empty( $_POST ) ) {
 
 
188
  $error = '';
189
 
190
+ if ( isset( $_POST['mc-value'] ) && $_POST['mc-value'] !== '' ) {
191
+ if ( Math_Captcha()->cookie_session->session_ids['default'] !== '' && get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ) !== false ) {
192
+ if ( strcmp( get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ), sha1( AUTH_KEY . $_POST['mc-value'] . Math_Captcha()->cookie_session->session_ids['default'], false ) ) !== 0 )
 
 
193
  $error = 'wrong';
194
+ } else
 
195
  $error = 'time';
196
+ } else
 
197
  $error = 'fill';
198
 
199
+ if ( is_wp_error( $errors ) && ! empty( $error ) )
200
+ $errors->add( 'math-captcha-error', $this->error_messages[$error] );
201
  }
202
 
203
  return $redirect;
204
  }
205
 
 
206
  /**
207
  * Authenticate user
208
  */
209
+ public function authenticate_user( $user, $username, $password ) {
 
210
  // user gave us valid login and password
211
+ if ( ! is_wp_error( $user ) ) {
212
+ if ( ! empty( $_POST ) ) {
213
+ if ( isset( $_POST['mc-value'] ) && $_POST['mc-value'] !== '' ) {
214
+ if ( Math_Captcha()->cookie_session->session_ids['default'] !== '' && get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ) !== false ) {
215
+ if ( strcmp( get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ), sha1( AUTH_KEY . $_POST['mc-value'] . Math_Captcha()->cookie_session->session_ids['default'], false ) ) !== 0 )
 
 
 
 
216
  $error = 'wrong';
217
+ } else
 
218
  $error = 'time';
219
+ } else
 
220
  $error = 'fill';
221
  }
222
 
223
+ if ( ! empty( $error ) ) {
 
224
  // destroy cookie
225
  wp_clear_auth_cookie();
226
 
227
  $user = new WP_Error();
228
+ $user->add( 'math-captcha-error', $this->error_messages[$error] );
229
 
230
  // inform redirect function that we failed to login
231
  $this->login_failed = true;
235
  return $user;
236
  }
237
 
 
238
  /**
239
  * Adds shake
240
+ */
241
+ public function add_shake_error_codes( $codes ) {
 
242
  $codes[] = 'math-captcha-error';
243
 
244
  return $codes;
245
  }
246
 
 
247
  /**
248
  * Adds captcha to comment form
249
+ */
250
+ public function add_comment_with_captcha( $comment ) {
251
+ if ( isset( $_POST['mc-value'] ) && ( ! is_admin() || DOING_AJAX) && ($comment['comment_type'] === '' || $comment['comment_type'] === 'comment') ) {
252
+ if ( $_POST['mc-value'] !== '' ) {
253
+ if ( Math_Captcha()->cookie_session->session_ids['default'] !== '' && get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ) !== false ) {
254
+ if ( strcmp( get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ), sha1( AUTH_KEY . $_POST['mc-value'] . Math_Captcha()->cookie_session->session_ids['default'], false ) ) === 0 )
 
 
 
 
255
  return $comment;
256
  else
257
+ wp_die( $this->error_messages['wrong'] );
258
+ } else
259
+ wp_die( $this->error_messages['time'] );
260
+ } else
261
+ wp_die( $this->error_messages['fill'] );
262
+ } else
 
 
 
263
  return $comment;
264
  }
265
 
 
266
  /**
267
  * Shows and generates captcha
268
+ */
269
+ public function add_captcha_form() {
270
+ if ( is_admin() )
 
271
  return;
272
 
273
+ $captcha_title = apply_filters( 'math_captcha_title', Math_Captcha()->options['general']['title'] );
274
 
275
  echo '
276
  <p class="math-captcha-form">';
277
 
278
+ if ( ! empty( $captcha_title ) )
279
  echo '
280
+ <label>' . $captcha_title . '<br/></label>';
281
 
282
  echo '
283
+ <span>' . $this->generate_captcha_phrase( 'default' ) . '</span>
284
  </p>';
285
  }
286
 
 
287
  /**
288
  * Shows and generates captcha for bbPress
289
+ */
290
+ public function add_bbp_captcha_form() {
291
+ if ( is_admin() )
 
292
  return;
293
 
294
+ $captcha_title = apply_filters( 'math_captcha_title', Math_Captcha()->options['general']['title'] );
295
 
296
  echo '
297
  <p class="math-captcha-form">';
298
 
299
+ if ( ! empty( $captcha_title ) )
300
  echo '
301
+ <label>' . $captcha_title . '<br/></label>';
302
 
303
  echo '
304
+ <span>' . $this->generate_captcha_phrase( 'bbpress' ) . '</span>
305
  </p>';
306
  }
307
 
 
308
  /**
309
  * Validates bbpress topics and replies
310
+ */
311
+ public function check_bbpress_captcha() {
312
+ if ( isset( $_POST['mc-value'] ) && $_POST['mc-value'] !== '' ) {
313
+ if ( Math_Captcha()->cookie_session->session_ids['default'] !== '' && get_transient( 'bbp_' . Math_Captcha()->cookie_session->session_ids['default'] ) !== false ) {
314
+ if ( strcmp( get_transient( 'bbp_' . Math_Captcha()->cookie_session->session_ids['default'] ), sha1( AUTH_KEY . $_POST['mc-value'] . Math_Captcha()->cookie_session->session_ids['default'], false ) ) !== 0 )
315
+ bbp_add_error( 'math-captcha-wrong', $this->error_messages['wrong'] );
316
+ } else
317
+ bbp_add_error( 'math-captcha-wrong', $this->error_messages['time'] );
318
+ } else
319
+ bbp_add_error( 'math-captcha-wrong', $this->error_messages['fill'] );
 
 
 
 
 
320
  }
321
 
 
322
  /**
323
  * Encodes chars
324
+ */
325
+ private function encode_operation( $string ) {
326
+ $chars = str_split( $string );
327
+ $seed = mt_rand( 0, (int) abs( crc32( $string ) / strlen( $string ) ) );
 
328
 
329
+ foreach ( $chars as $key => $char ) {
330
+ $ord = ord( $char );
 
331
 
332
  // ignore non-ascii chars
333
+ if ( $ord < 128 ) {
 
334
  // pseudo "random function"
335
  $r = ($seed * (1 + $key)) % 100;
336
 
337
+ if ( $r > 60 && $char !== '@' ) {
338
+
339
+ } // plain character (not encoded), if not @-sign
340
+ elseif ( $r < 45 )
341
+ $chars[$key] = '&#x' . dechex( $ord ) . ';'; // hexadecimal
342
+ else
343
+ $chars[$key] = '&#' . $ord . ';'; // decimal (ascii)
344
  }
345
  }
346
 
347
+ return implode( '', $chars );
348
  }
349
 
 
350
  /**
351
  * Converts numbers to words
352
+ */
353
+ private function numberToWords( $number ) {
 
354
  $words = array(
355
+ 1 => __( 'one', 'math-captcha' ),
356
+ 2 => __( 'two', 'math-captcha' ),
357
+ 3 => __( 'three', 'math-captcha' ),
358
+ 4 => __( 'four', 'math-captcha' ),
359
+ 5 => __( 'five', 'math-captcha' ),
360
+ 6 => __( 'six', 'math-captcha' ),
361
+ 7 => __( 'seven', 'math-captcha' ),
362
+ 8 => __( 'eight', 'math-captcha' ),
363
+ 9 => __( 'nine', 'math-captcha' ),
364
+ 10 => __( 'ten', 'math-captcha' ),
365
+ 11 => __( 'eleven', 'math-captcha' ),
366
+ 12 => __( 'twelve', 'math-captcha' ),
367
+ 13 => __( 'thirteen', 'math-captcha' ),
368
+ 14 => __( 'fourteen', 'math-captcha' ),
369
+ 15 => __( 'fifteen', 'math-captcha' ),
370
+ 16 => __( 'sixteen', 'math-captcha' ),
371
+ 17 => __( 'seventeen', 'math-captcha' ),
372
+ 18 => __( 'eighteen', 'math-captcha' ),
373
+ 19 => __( 'nineteen', 'math-captcha' ),
374
+ 20 => __( 'twenty', 'math-captcha' ),
375
+ 30 => __( 'thirty', 'math-captcha' ),
376
+ 40 => __( 'forty', 'math-captcha' ),
377
+ 50 => __( 'fifty', 'math-captcha' ),
378
+ 60 => __( 'sixty', 'math-captcha' ),
379
+ 70 => __( 'seventy', 'math-captcha' ),
380
+ 80 => __( 'eighty', 'math-captcha' ),
381
+ 90 => __( 'ninety', 'math-captcha' )
382
  );
383
 
384
+ if ( isset( $words[$number] ) )
385
  return $words[$number];
386
+ else {
 
387
  $reverse = false;
388
 
389
+ switch ( get_bloginfo( 'language' ) ) {
 
390
  case 'de-DE':
391
  $spacer = 'und';
392
  $reverse = true;
396
  $spacer = 'en';
397
  $reverse = true;
398
  break;
399
+
400
  case 'ru-RU':
401
  case 'pl-PL':
402
  case 'en-EN':
404
  $spacer = ' ';
405
  }
406
 
407
+ $first = (int) (substr( $number, 0, 1 ) * 10);
408
+ $second = (int) substr( $number, -1 );
409
 
410
+ return ($reverse === false ? $words[$first] . $spacer . $words[$second] : $words[$second] . $spacer . $words[$first]);
411
  }
412
  }
413
 
 
414
  /**
415
  * Generates captcha
416
+ */
417
+ public function generate_captcha_phrase( $form = '' ) {
 
418
  $ops = array(
419
+ 'addition' => '+',
420
+ 'subtraction' => '&#8722;',
421
  'multiplication' => '&#215;',
422
+ 'division' => '&#247;',
423
  );
424
 
425
  $operations = $groups = array();
426
  $input = '<input type="text" size="2" length="2" id="mc-input" class="mc-input" name="mc-value" value="" aria-required="true"/>';
427
 
428
  // available operations
429
+ foreach ( Math_Captcha()->options['general']['mathematical_operations'] as $operation => $enable ) {
430
+ if ( $enable === true )
 
431
  $operations[] = $operation;
432
  }
433
 
434
  // available groups
435
+ foreach ( Math_Captcha()->options['general']['groups'] as $group => $enable ) {
436
+ if ( $enable === true )
 
437
  $groups[] = $group;
438
  }
439
 
440
  // number of groups
441
+ $ao = count( $groups );
442
 
443
  // operation
444
+ $rnd_op = $operations[mt_rand( 0, count( $operations ) - 1 )];
445
  $number[3] = $ops[$rnd_op];
446
 
447
  // place where to put empty input
448
+ $rnd_input = mt_rand( 0, 2 );
449
 
450
  // which random operation
451
+ switch ( $rnd_op ) {
 
452
  case 'addition':
453
+ if ( $rnd_input === 0 ) {
454
+ $number[0] = mt_rand( 1, 10 );
455
+ $number[1] = mt_rand( 1, 89 );
456
+ } elseif ( $rnd_input === 1 ) {
457
+ $number[0] = mt_rand( 1, 89 );
458
+ $number[1] = mt_rand( 1, 10 );
459
+ } elseif ( $rnd_input === 2 ) {
460
+ $number[0] = mt_rand( 1, 9 );
461
+ $number[1] = mt_rand( 1, 10 - $number[0] );
 
 
 
 
 
462
  }
463
 
464
  $number[2] = $number[0] + $number[1];
465
  break;
466
 
467
  case 'subtraction':
468
+ if ( $rnd_input === 0 ) {
469
+ $number[0] = mt_rand( 2, 10 );
470
+ $number[1] = mt_rand( 1, $number[0] - 1 );
471
+ } elseif ( $rnd_input === 1 ) {
472
+ $number[0] = mt_rand( 11, 99 );
473
+ $number[1] = mt_rand( 1, 10 );
474
+ } elseif ( $rnd_input === 2 ) {
475
+ $number[0] = mt_rand( 11, 99 );
476
+ $number[1] = mt_rand( $number[0] - 10, $number[0] - 1 );
 
 
 
 
 
477
  }
478
 
479
  $number[2] = $number[0] - $number[1];
480
  break;
481
 
482
  case 'multiplication':
483
+ if ( $rnd_input === 0 ) {
484
+ $number[0] = mt_rand( 1, 10 );
485
+ $number[1] = mt_rand( 1, 9 );
486
+ } elseif ( $rnd_input === 1 ) {
487
+ $number[0] = mt_rand( 1, 9 );
488
+ $number[1] = mt_rand( 1, 10 );
489
+ } elseif ( $rnd_input === 2 ) {
490
+ $number[0] = mt_rand( 1, 10 );
491
+ $number[1] = ($number[0] > 5 ? 1 : ($number[0] === 4 && $number[0] === 5 ? mt_rand( 1, 2 ) : ($number[0] === 3 ? mt_rand( 1, 3 ) : ($number[0] === 2 ? mt_rand( 1, 5 ) : mt_rand( 1, 10 )))));
 
 
 
 
 
492
  }
493
 
494
  $number[2] = $number[0] * $number[1];
495
  break;
496
 
497
  case 'division':
498
+ $divide = array( 1 => 99, 2 => 49, 3 => 33, 4 => 24, 5 => 19, 6 => 16, 7 => 14, 8 => 12, 9 => 11, 10 => 9 );
499
+
500
+ if ( $rnd_input === 0 ) {
501
+ $divide = array( 2 => array( 1, 2 ), 3 => array( 1, 3 ), 4 => array( 1, 2, 4 ), 5 => array( 1, 5 ), 6 => array( 1, 2, 3, 6 ), 7 => array( 1, 7 ), 8 => array( 1, 2, 4, 8 ), 9 => array( 1, 3, 9 ), 10 => array( 1, 2, 5, 10 ) );
502
+ $number[0] = mt_rand( 2, 10 );
503
+ $number[1] = $divide[$number[0]][mt_rand( 0, count( $divide[$number[0]] ) - 1 )];
504
+ } elseif ( $rnd_input === 1 ) {
505
+ $number[1] = mt_rand( 1, 10 );
506
+ $number[0] = $number[1] * mt_rand( 1, $divide[$number[1]] );
507
+ } elseif ( $rnd_input === 2 ) {
508
+ $number[2] = mt_rand( 1, 10 );
509
+ $number[0] = $number[2] * mt_rand( 1, $divide[$number[2]] );
510
+ $number[1] = (int) ($number[0] / $number[2]);
 
 
 
 
 
511
  }
512
 
513
+ if ( ! isset( $number[2] ) )
514
+ $number[2] = (int) ($number[0] / $number[1]);
515
 
516
  break;
517
  }
518
 
519
  // words
520
+ if ( $ao === 1 && $groups[0] === 'words' ) {
521
+ if ( $rnd_input === 0 ) {
522
+ $number[1] = $this->numberToWords( $number[1] );
523
+ $number[2] = $this->numberToWords( $number[2] );
524
+ } elseif ( $rnd_input === 1 ) {
525
+ $number[0] = $this->numberToWords( $number[0] );
526
+ $number[2] = $this->numberToWords( $number[2] );
527
+ } elseif ( $rnd_input === 2 ) {
528
+ $number[0] = $this->numberToWords( $number[0] );
529
+ $number[1] = $this->numberToWords( $number[1] );
 
 
 
 
 
 
530
  }
531
  }
532
  // numbers and words
533
+ elseif ( $ao === 2 ) {
534
+ if ( $rnd_input === 0 ) {
535
+ if ( mt_rand( 1, 2 ) === 2 ) {
536
+ $number[1] = $this->numberToWords( $number[1] );
537
+ $number[2] = $this->numberToWords( $number[2] );
538
+ } else
539
+ $number[$tmp = mt_rand( 1, 2 )] = $this->numberToWords( $number[$tmp] );
 
 
 
 
540
  }
541
+ elseif ( $rnd_input === 1 ) {
542
+ if ( mt_rand( 1, 2 ) === 2 ) {
543
+ $number[0] = $this->numberToWords( $number[0] );
544
+ $number[2] = $this->numberToWords( $number[2] );
545
+ } else
546
+ $number[$tmp = array_rand( array( 0 => 0, 2 => 2 ), 1 )] = $this->numberToWords( $number[$tmp] );
 
 
 
547
  }
548
+ elseif ( $rnd_input === 2 ) {
549
+ if ( mt_rand( 1, 2 ) === 2 ) {
550
+ $number[0] = $this->numberToWords( $number[0] );
551
+ $number[1] = $this->numberToWords( $number[1] );
552
+ } else
553
+ $number[$tmp = mt_rand( 0, 1 )] = $this->numberToWords( $number[$tmp] );
 
 
 
554
  }
555
  }
556
 
557
+ if ( in_array( $form, array( 'default', 'bbpress' ), true ) ) {
 
558
  // position of empty input
559
+ if ( $rnd_input === 0 )
560
+ $return = $input . ' ' . $number[3] . ' ' . $this->encode_operation( $number[1] ) . ' = ' . $this->encode_operation( $number[2] );
561
+ elseif ( $rnd_input === 1 )
562
+ $return = $this->encode_operation( $number[0] ) . ' ' . $number[3] . ' ' . $input . ' = ' . $this->encode_operation( $number[2] );
563
+ elseif ( $rnd_input === 2 )
564
+ $return = $this->encode_operation( $number[0] ) . ' ' . $number[3] . ' ' . $this->encode_operation( $number[1] ) . ' = ' . $input;
565
 
566
  $transient_name = ($form === 'bbpress' ? 'bbp' : 'mc');
567
  $session_id = Math_Captcha()->cookie_session->session_ids['default'];
568
  }
569
+ elseif ( $form === 'cf7' ) {
 
570
  $return = array();
571
 
572
+ if ( $rnd_input === 0 ) {
 
573
  $return['input'] = 1;
574
+ $return[2] = ' ' . $number[3] . ' ' . $this->encode_operation( $number[1] ) . ' = ';
575
+ $return[3] = $this->encode_operation( $number[2] );
576
+ } elseif ( $rnd_input === 1 ) {
577
+ $return[1] = $this->encode_operation( $number[0] ) . ' ' . $number[3] . ' ';
 
 
578
  $return['input'] = 2;
579
+ $return[3] = ' = ' . $this->encode_operation( $number[2] );
580
+ } elseif ( $rnd_input === 2 ) {
581
+ $return[1] = $this->encode_operation( $number[0] ) . ' ' . $number[3] . ' ';
582
+ $return[2] = $this->encode_operation( $number[1] ) . ' = ';
 
 
583
  $return['input'] = 3;
584
  }
585
 
586
  $transient_name = 'cf7';
587
+ $session_id = Math_Captcha()->cookie_session->session_ids['multi'][$this->session_number ++];
588
  }
589
 
590
+ set_transient( $transient_name . '_' . $session_id, sha1( AUTH_KEY . $number[$rnd_input] . $session_id, false ), apply_filters( 'math_captcha_time', Math_Captcha()->options['general']['time'] ) );
591
 
592
  return $return;
593
  }
594
 
 
595
  /**
596
  *
597
+ */
598
+ public function flush_rewrites() {
599
+ if ( Math_Captcha()->options['general']['flush_rules'] ) {
 
 
600
  global $wp_rewrite;
601
 
602
  $wp_rewrite->flush_rules();
603
 
604
  Math_Captcha()->options['general']['flush_rules'] = false;
605
+ update_option( 'math_captcha_options', Math_Captcha()->options['general'] );
606
  }
607
  }
608
 
 
609
  /**
610
  *
611
+ */
612
+ public function block_direct_comments( $rules ) {
613
+ if ( Math_Captcha()->options['general']['block_direct_comments'] ) {
614
+ $new_rules = <<<EOT
 
 
 
615
  \n# BEGIN Math Captcha
616
  <IfModule mod_rewrite.c>
617
  RewriteEngine On
624
  # END Math Captcha\n\n
625
  EOT;
626
 
627
+ return $new_rules . $rules;
628
  }
629
 
630
  return $rules;
631
  }
632
+
633
+ }
includes/class-settings.php CHANGED
@@ -1,117 +1,106 @@
1
  <?php
2
- if(!defined('ABSPATH')) exit;
 
3
 
4
  new Math_Captcha_Settings();
5
 
6
- class Math_Captcha_Settings
7
- {
8
  public $mathematical_operations;
9
  public $groups;
10
  public $forms;
11
 
12
-
13
  /**
14
  *
15
- */
16
- public function __construct()
17
- {
18
  // actions
19
- add_action('init', array(&$this, 'load_defaults'));
20
- add_action('admin_init', array(&$this, 'register_settings'));
21
- add_action('admin_menu', array(&$this, 'admin_menu_options'));
22
  }
23
 
24
-
25
  /**
26
  *
27
- */
28
- public function load_defaults()
29
- {
30
- if(!is_admin())
31
  return;
32
 
33
  $this->forms = array(
34
- 'login_form' => __('login form', 'math-captcha'),
35
- 'registration_form' => __('registration form', 'math-captcha'),
36
- 'reset_password_form' => __('reset password form', 'math-captcha'),
37
- 'comment_form' => __('comment form', 'math-captcha'),
38
- 'bbpress' => __('bbpress', 'math-captcha'),
39
- 'contact_form_7' => __('contact form 7', 'math-captcha')
40
  );
41
 
42
  $this->mathematical_operations = array(
43
- 'addition' => __('addition (+)', 'math-captcha'),
44
- 'subtraction' => __('subtraction (-)', 'math-captcha'),
45
- 'multiplication' => __('multiplication (&#215;)', 'math-captcha'),
46
- 'division' => __('division (&#247;)', 'math-captcha')
47
  );
48
 
49
  $this->groups = array(
50
- 'numbers' => __('numbers', 'math-captcha'),
51
- 'words' => __('words', 'math-captcha')
52
  );
53
  }
54
 
55
-
56
  /**
57
  * Adds options menu
58
- */
59
- public function admin_menu_options()
60
- {
61
  add_options_page(
62
- __('Math Captcha', 'math-captcha'),
63
- __('Math Captcha', 'math-captcha'),
64
- 'manage_options',
65
- 'math-captcha',
66
- array(&$this, 'options_page')
67
  );
68
  }
69
 
70
-
71
  /**
72
  * Shows options page
73
- */
74
- public function options_page()
75
- {
76
  echo '
77
  <div class="wrap">
78
- <h2>'.__('Math Captcha', 'math-captcha').'</h2>
79
  <div class="math-captcha-settings">
80
  <div class="df-credits">
81
- <h3 class="hndle">'.__('Math Captcha', 'math-captcha').' '.Math_Captcha()->defaults['version'].'</h3>
82
  <div class="inside">
83
- <h4 class="inner">'.__('Need support?', 'math-captcha').'</h4>
84
- <p class="inner">'.__('If you are having problems with this plugin, please talk about them in the', 'math-captcha').' <a href="http://www.dfactory.eu/support/?utm_source=math-captcha-settings&utm_medium=link&utm_campaign=support" target="_blank" title="'.__('Support forum','math-captcha').'">'.__('Support forum', 'math-captcha').'</a></p>
85
  <hr/>
86
- <h4 class="inner">'.__('Do you like this plugin?', 'math-captcha').'</h4>
87
  <form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_blank" class="inner">
88
  <input type="hidden" name="cmd" value="_s-xclick">
89
  <input type="hidden" name="hosted_button_id" value="BJSHR9GS5QJTC">
90
  <input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
91
  <img alt="" border="0" src="https://www.paypalobjects.com/pl_PL/i/scr/pixel.gif" width="1" height="1">
92
  </form>
93
- <p class="inner"><a href="http://wordpress.org/support/view/plugin-reviews/wp-math-captcha" target="_blank" title="'.__('Rate it 5', 'math-captcha').'">'.__('Rate it 5', 'math-captcha').'</a> '.__('on WordPress.org', 'math-captcha').'<br/>'.
94
- __('Blog about it & link to the', 'math-captcha').' <a href="http://www.dfactory.eu/plugins/math-captcha/?utm_source=math-captcha-settings&utm_medium=link&utm_campaign=blog-about" target="_blank" title="'.__('plugin page', 'math-captcha').'">'.__('plugin page', 'math-captcha').'</a><br/>'.
95
- __('Check out our other', 'math-captcha').' <a href="http://www.dfactory.eu/plugins/?utm_source=math-captcha-settings&utm_medium=link&utm_campaign=other-plugins" target="_blank" title="'.__('WordPress plugins', 'math-captcha').'">'.__('WordPress plugins', 'math-captcha').'</a>
96
  </p>
97
  <hr/>
98
- <p class="df-link inner">Created by <a href="http://www.dfactory.eu/?utm_source=math-captcha-settings&utm_medium=link&utm_campaign=created-by" target="_blank" title="dFactory - Quality plugins for WordPress"><img src="'.MATH_CAPTCHA_URL.'/images/logo-dfactory.png" title="dFactory - Quality plugins for WordPress" alt="dFactory - Quality plugins for WordPress"/></a></p>
99
  </div>
100
  </div>
101
  <form action="options.php" method="post">';
102
 
103
- wp_nonce_field('update-options');
104
- settings_fields('math_captcha_options');
105
- do_settings_sections('math_captcha_options');
106
 
107
  echo '
108
  <p class="submit">';
109
 
110
- submit_button('', 'primary', 'save_mc_general', false);
111
 
112
  echo ' ';
113
 
114
- submit_button(__('Reset to defaults', 'math-captcha'), 'secondary reset_mc_settings', 'reset_mc_general', false);
115
 
116
  echo '
117
  </p>
@@ -121,222 +110,189 @@ class Math_Captcha_Settings
121
  </div>';
122
  }
123
 
124
-
125
  /**
126
  *
127
- */
128
- public function register_settings()
129
- {
130
  // general settings
131
- register_setting('math_captcha_options', 'math_captcha_options', array(&$this, 'validate_settings'));
132
- add_settings_section('math_captcha_settings', __('Math Captcha settings', 'math-captcha'), '', 'math_captcha_options');
133
- add_settings_field('mc_general_enable_captcha_for', __('Enable Math Captcha for', 'math-captcha'), array(&$this, 'mc_general_enable_captcha_for'), 'math_captcha_options', 'math_captcha_settings');
134
- add_settings_field('mc_general_hide_for_logged_users', __('Hide for logged in users', 'math-captcha'), array(&$this, 'mc_general_hide_for_logged_users'), 'math_captcha_options', 'math_captcha_settings');
135
- add_settings_field('mc_general_mathematical_operations', __('Mathematical operations', 'math-captcha'), array(&$this, 'mc_general_mathematical_operations'), 'math_captcha_options', 'math_captcha_settings');
136
- add_settings_field('mc_general_groups', __('Display captcha as', 'math-captcha'), array(&$this, 'mc_general_groups'), 'math_captcha_options', 'math_captcha_settings');
137
- add_settings_field('mc_general_title', __('Captcha field title', 'math-captcha'), array(&$this, 'mc_general_title'), 'math_captcha_options', 'math_captcha_settings');
138
- add_settings_field('mc_general_time', __('Captcha time', 'math-captcha'), array(&$this, 'mc_general_time'), 'math_captcha_options', 'math_captcha_settings');
139
- add_settings_field('mc_general_block_direct_comments', __('Block Direct Comments', 'math-captcha'), array(&$this, 'mc_general_block_direct_comments'), 'math_captcha_options', 'math_captcha_settings');
140
- add_settings_field('mc_general_deactivation_delete', __('Deactivation', 'math-captcha'), array(&$this, 'mc_general_deactivation_delete'), 'math_captcha_options', 'math_captcha_settings');
141
  }
142
 
143
-
144
  /**
145
  *
146
- */
147
- public function mc_general_enable_captcha_for()
148
- {
149
  echo '
150
  <div id="mc_general_enable_captcha_for">
151
  <fieldset>';
152
 
153
- foreach($this->forms as $val => $trans)
154
- {
155
  echo '
156
- <input id="mc-general-enable-captcha-for-'.$val.'" type="checkbox" name="math_captcha_options[enable_for][]" value="'.$val.'" '.checked(true, Math_Captcha()->options['general']['enable_for'][$val], false).' '.disabled((($val === 'contact_form_7' && !class_exists('WPCF7_ContactForm')) || ($val === 'bbpress' && !class_exists('bbPress'))), true, false).'/><label for="mc-general-enable-captcha-for-'.$val.'">'.esc_html($trans).'</label>';
157
  }
158
 
159
  echo '
160
  <br/>
161
- <span class="description">'.__('Select where you\'d like to use Math Captcha.', 'math-captcha').'</span>
162
  </fieldset>
163
  </div>';
164
  }
165
 
166
-
167
  /**
168
  *
169
- */
170
- public function mc_general_hide_for_logged_users()
171
- {
172
  echo '
173
  <div id="mc_general_hide_for_logged_users">
174
  <fieldset>
175
- <input id="mc-general-hide-for-logged" type="checkbox" name="math_captcha_options[hide_for_logged_users]" '.checked(true, Math_Captcha()->options['general']['hide_for_logged_users'], false).'/><label for="mc-general-hide-for-logged">'.__('Enable to hide captcha for logged in users.', 'math-captcha').'</label>
176
  <br/>
177
- <span class="description">'.__('Would you like to hide captcha for logged in users?', 'math-captcha').'</span>
178
  </fieldset>
179
  </div>';
180
  }
181
 
182
-
183
  /**
184
  *
185
- */
186
- public function mc_general_mathematical_operations()
187
- {
188
  echo '
189
  <div id="mc_general_mathematical_operations">
190
  <fieldset>';
191
 
192
- foreach($this->mathematical_operations as $val => $trans)
193
- {
194
  echo '
195
- <input id="mc-general-mathematical-operations-'.$val.'" type="checkbox" name="math_captcha_options[mathematical_operations][]" value="'.$val.'" '.checked(true, Math_Captcha()->options['general']['mathematical_operations'][$val], false).'/><label for="mc-general-mathematical-operations-'.$val.'">'.esc_html($trans).'</label>';
196
  }
197
 
198
  echo '
199
  <br/>
200
- <span class="description">'.__('Select which mathematical operations to use in your captcha.', 'math-captcha').'</span>
201
  </fieldset>
202
  </div>';
203
  }
204
 
205
-
206
  /**
207
  *
208
- */
209
- public function mc_general_groups()
210
- {
211
  echo '
212
  <div id="mc_general_groups">
213
  <fieldset>';
214
 
215
- foreach($this->groups as $val => $trans)
216
- {
217
  echo '
218
- <input id="mc-general-groups-'.$val.'" type="checkbox" name="math_captcha_options[groups][]" value="'.$val.'" '.checked(true, Math_Captcha()->options['general']['groups'][$val], false).'/><label for="mc-general-groups-'.$val.'">'.esc_html($trans).'</label>';
219
  }
220
 
221
  echo '
222
  <br/>
223
- <span class="description">'.__('Select how you\'d like to display you captcha.', 'math-captcha').'</span>
224
  </fieldset>
225
  </div>';
226
  }
227
 
228
-
229
  /**
230
  *
231
- */
232
- public function mc_general_title()
233
- {
234
  echo '
235
  <div id="mc_general_title">
236
  <fieldset>
237
- <input type="text" name="math_captcha_options[title]" value="'.Math_Captcha()->options['general']['title'].'"/>
238
  <br/>
239
- <span class="description">'.__('How to entitle field with captcha?', 'math-captcha').'</span>
240
  </fieldset>
241
  </div>';
242
  }
243
 
244
-
245
  /**
246
  *
247
- */
248
- public function mc_general_time()
249
- {
250
  echo '
251
  <div id="mc_general_time">
252
  <fieldset>
253
- <input type="text" name="math_captcha_options[time]" value="'.Math_Captcha()->options['general']['time'].'"/>
254
  <br/>
255
- <span class="description">'.__('Enter the time (in seconds) a user has to enter captcha value.', 'math-captcha').'</span>
256
  </fieldset>
257
  </div>';
258
  }
259
 
260
-
261
  /**
262
  *
263
- */
264
- public function mc_general_block_direct_comments()
265
- {
266
  echo '
267
  <div id="mc_general_block_direct_comments">
268
  <fieldset>
269
- <input id="mc-general-block-direct-comments" type="checkbox" name="math_captcha_options[block_direct_comments]" '.checked(true, Math_Captcha()->options['general']['block_direct_comments'], false).'/><label for="mc-general-block-direct-comments">'.__('Block direct access to wp-comments-post.php.', 'math-captcha').'</label>
270
  <br/>
271
- <span class="description">'.__('Enable this to prevent spambots from posting to Wordpress via a URL.', 'math-captcha').'</span>
272
  </fieldset>
273
  </div>';
274
  }
275
 
276
-
277
  /**
278
  *
279
- */
280
- public function mc_general_deactivation_delete()
281
- {
282
  echo '
283
  <div id="mc_general_deactivation_delete">
284
  <fieldset>
285
- <input id="mc-general-deactivation-delete" type="checkbox" name="math_captcha_options[deactivation_delete]" '.checked(true, Math_Captcha()->options['general']['deactivation_delete'], false).'/><label for="mc-general-deactivation-delete">'.__('Delete settings on plugin deactivation.', 'math-captcha').'</label>
286
  <br/>
287
- <span class="description">'.__('Delete settings on plugin deactivation', 'math-captcha').'</span>
288
  </fieldset>
289
  </div>';
290
  }
291
 
292
-
293
  /**
294
  * Validates settings
295
- */
296
- public function validate_settings($input)
297
- {
298
- if(isset($_POST['save_mc_general']))
299
- {
300
  // enable captcha forms
301
  $enable_for = array();
302
 
303
- if(empty($input['enable_for']))
304
- {
305
- foreach(Math_Captcha()->defaults['general']['enable_for'] as $enable => $bool)
306
- {
307
  $input['enable_for'][$enable] = false;
308
  }
309
- }
310
- else
311
- {
312
- foreach($this->forms as $enable => $trans)
313
- {
314
- $enable_for[$enable] = (in_array($enable, $input['enable_for']) ? true : false);
315
  }
316
 
317
  $input['enable_for'] = $enable_for;
318
  }
319
 
320
- if(!class_exists('WPCF7_ContactForm') && Math_Captcha()->options['general']['enable_for']['contact_form_7'])
321
  $input['enable_for']['contact_form_7'] = true;
322
 
323
- if(!class_exists('bbPress') && Math_Captcha()->options['general']['enable_for']['bbpress'])
324
  $input['enable_for']['bbpress'] = true;
325
 
326
  // enable mathematical operations
327
  $mathematical_operations = array();
328
 
329
- if(empty($input['mathematical_operations']))
330
- {
331
- add_settings_error('empty-operations', 'settings_updated', __('You need to check at least one mathematical operation. Defaults settings of this option restored.', 'math-captcha'), 'error');
332
 
333
  $input['mathematical_operations'] = Math_Captcha()->defaults['general']['mathematical_operations'];
334
- }
335
- else
336
- {
337
- foreach($this->mathematical_operations as $operation => $trans)
338
- {
339
- $mathematical_operations[$operation] = (in_array($operation, $input['mathematical_operations']) ? true : false);
340
  }
341
 
342
  $input['mathematical_operations'] = $mathematical_operations;
@@ -345,49 +301,43 @@ class Math_Captcha_Settings
345
  // enable groups
346
  $groups = array();
347
 
348
- if(empty($input['groups']))
349
- {
350
- add_settings_error('empty-groups', 'settings_updated', __('You need to check at least one group. Defaults settings of this option restored.', 'math-captcha'), 'error');
351
 
352
  $input['groups'] = Math_Captcha()->defaults['general']['groups'];
353
- }
354
- else
355
- {
356
- foreach($this->groups as $group => $trans)
357
- {
358
- $groups[$group] = (in_array($group, $input['groups']) ? true : false);
359
  }
360
 
361
  $input['groups'] = $groups;
362
  }
363
 
364
  // hide for logged in users
365
- $input['hide_for_logged_users'] = isset($input['hide_for_logged_users']);
366
 
367
  // block direct comments access
368
- $input['block_direct_comments'] = isset($input['block_direct_comments']);
369
 
370
  // deactivation delete
371
- $input['deactivation_delete'] = isset($input['deactivation_delete']);
372
 
373
  // captcha title
374
- $input['title'] = trim($input['title']);
375
 
376
  // captcha time
377
- $time = (int)$input['time'];
378
  $input['time'] = ($time < 0 ? Math_Captcha()->defaults['general']['time'] : $time);
379
 
380
  // flush rules
381
  $input['flush_rules'] = true;
382
- }
383
- elseif(isset($_POST['reset_mc_general']))
384
- {
385
  $input = Math_Captcha()->defaults['general'];
386
 
387
- add_settings_error('settings', 'settings_reset', __('Settings restored to defaults.', 'math-captcha'), 'updated');
388
  }
389
 
390
  return $input;
391
  }
392
- }
393
- ?>
1
  <?php
2
+ if ( ! defined( 'ABSPATH' ) )
3
+ exit;
4
 
5
  new Math_Captcha_Settings();
6
 
7
+ class Math_Captcha_Settings {
8
+
9
  public $mathematical_operations;
10
  public $groups;
11
  public $forms;
12
 
 
13
  /**
14
  *
15
+ */
16
+ public function __construct() {
 
17
  // actions
18
+ add_action( 'init', array( &$this, 'load_defaults' ) );
19
+ add_action( 'admin_init', array( &$this, 'register_settings' ) );
20
+ add_action( 'admin_menu', array( &$this, 'admin_menu_options' ) );
21
  }
22
 
 
23
  /**
24
  *
25
+ */
26
+ public function load_defaults() {
27
+ if ( ! is_admin() )
 
28
  return;
29
 
30
  $this->forms = array(
31
+ 'login_form' => __( 'login form', 'math-captcha' ),
32
+ 'registration_form' => __( 'registration form', 'math-captcha' ),
33
+ 'reset_password_form' => __( 'reset password form', 'math-captcha' ),
34
+ 'comment_form' => __( 'comment form', 'math-captcha' ),
35
+ 'bbpress' => __( 'bbpress', 'math-captcha' ),
36
+ 'contact_form_7' => __( 'contact form 7', 'math-captcha' )
37
  );
38
 
39
  $this->mathematical_operations = array(
40
+ 'addition' => __( 'addition (+)', 'math-captcha' ),
41
+ 'subtraction' => __( 'subtraction (-)', 'math-captcha' ),
42
+ 'multiplication' => __( 'multiplication (&#215;)', 'math-captcha' ),
43
+ 'division' => __( 'division (&#247;)', 'math-captcha' )
44
  );
45
 
46
  $this->groups = array(
47
+ 'numbers' => __( 'numbers', 'math-captcha' ),
48
+ 'words' => __( 'words', 'math-captcha' )
49
  );
50
  }
51
 
 
52
  /**
53
  * Adds options menu
54
+ */
55
+ public function admin_menu_options() {
 
56
  add_options_page(
57
+ __( 'Math Captcha', 'math-captcha' ), __( 'Math Captcha', 'math-captcha' ), 'manage_options', 'math-captcha', array( &$this, 'options_page' )
 
 
 
 
58
  );
59
  }
60
 
 
61
  /**
62
  * Shows options page
63
+ */
64
+ public function options_page() {
 
65
  echo '
66
  <div class="wrap">
67
+ <h2>' . __( 'Math Captcha', 'math-captcha' ) . '</h2>
68
  <div class="math-captcha-settings">
69
  <div class="df-credits">
70
+ <h3 class="hndle">' . __( 'Math Captcha', 'math-captcha' ) . ' ' . Math_Captcha()->defaults['version'] . '</h3>
71
  <div class="inside">
72
+ <h4 class="inner">' . __( 'Need support?', 'math-captcha' ) . '</h4>
73
+ <p class="inner">' . __( 'If you are having problems with this plugin, please talk about them in the', 'math-captcha' ) . ' <a href="http://www.dfactory.eu/support/?utm_source=math-captcha-settings&utm_medium=link&utm_campaign=support" target="_blank" title="' . __( 'Support forum', 'math-captcha' ) . '">' . __( 'Support forum', 'math-captcha' ) . '</a></p>
74
  <hr/>
75
+ <h4 class="inner">' . __( 'Do you like this plugin?', 'math-captcha' ) . '</h4>
76
  <form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_blank" class="inner">
77
  <input type="hidden" name="cmd" value="_s-xclick">
78
  <input type="hidden" name="hosted_button_id" value="BJSHR9GS5QJTC">
79
  <input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
80
  <img alt="" border="0" src="https://www.paypalobjects.com/pl_PL/i/scr/pixel.gif" width="1" height="1">
81
  </form>
82
+ <p class="inner"><a href="http://wordpress.org/support/view/plugin-reviews/wp-math-captcha" target="_blank" title="' . __( 'Rate it 5', 'math-captcha' ) . '">' . __( 'Rate it 5', 'math-captcha' ) . '</a> ' . __( 'on WordPress.org', 'math-captcha' ) . '<br/>' .
83
+ __( 'Blog about it & link to the', 'math-captcha' ) . ' <a href="http://www.dfactory.eu/plugins/math-captcha/?utm_source=math-captcha-settings&utm_medium=link&utm_campaign=blog-about" target="_blank" title="' . __( 'plugin page', 'math-captcha' ) . '">' . __( 'plugin page', 'math-captcha' ) . '</a><br/>' .
84
+ __( 'Check out our other', 'math-captcha' ) . ' <a href="http://www.dfactory.eu/plugins/?utm_source=math-captcha-settings&utm_medium=link&utm_campaign=other-plugins" target="_blank" title="' . __( 'WordPress plugins', 'math-captcha' ) . '">' . __( 'WordPress plugins', 'math-captcha' ) . '</a>
85
  </p>
86
  <hr/>
87
+ <p class="df-link inner">Created by <a href="http://www.dfactory.eu/?utm_source=math-captcha-settings&utm_medium=link&utm_campaign=created-by" target="_blank" title="dFactory - Quality plugins for WordPress"><img src="' . MATH_CAPTCHA_URL . '/images/logo-dfactory.png" title="dFactory - Quality plugins for WordPress" alt="dFactory - Quality plugins for WordPress"/></a></p>
88
  </div>
89
  </div>
90
  <form action="options.php" method="post">';
91
 
92
+ wp_nonce_field( 'update-options' );
93
+ settings_fields( 'math_captcha_options' );
94
+ do_settings_sections( 'math_captcha_options' );
95
 
96
  echo '
97
  <p class="submit">';
98
 
99
+ submit_button( '', 'primary', 'save_mc_general', false );
100
 
101
  echo ' ';
102
 
103
+ submit_button( __( 'Reset to defaults', 'math-captcha' ), 'secondary reset_mc_settings', 'reset_mc_general', false );
104
 
105
  echo '
106
  </p>
110
  </div>';
111
  }
112
 
 
113
  /**
114
  *
115
+ */
116
+ public function register_settings() {
 
117
  // general settings
118
+ register_setting( 'math_captcha_options', 'math_captcha_options', array( &$this, 'validate_settings' ) );
119
+ add_settings_section( 'math_captcha_settings', __( 'Math Captcha settings', 'math-captcha' ), '', 'math_captcha_options' );
120
+ add_settings_field( 'mc_general_enable_captcha_for', __( 'Enable Math Captcha for', 'math-captcha' ), array( &$this, 'mc_general_enable_captcha_for' ), 'math_captcha_options', 'math_captcha_settings' );
121
+ add_settings_field( 'mc_general_hide_for_logged_users', __( 'Hide for logged in users', 'math-captcha' ), array( &$this, 'mc_general_hide_for_logged_users' ), 'math_captcha_options', 'math_captcha_settings' );
122
+ add_settings_field( 'mc_general_mathematical_operations', __( 'Mathematical operations', 'math-captcha' ), array( &$this, 'mc_general_mathematical_operations' ), 'math_captcha_options', 'math_captcha_settings' );
123
+ add_settings_field( 'mc_general_groups', __( 'Display captcha as', 'math-captcha' ), array( &$this, 'mc_general_groups' ), 'math_captcha_options', 'math_captcha_settings' );
124
+ add_settings_field( 'mc_general_title', __( 'Captcha field title', 'math-captcha' ), array( &$this, 'mc_general_title' ), 'math_captcha_options', 'math_captcha_settings' );
125
+ add_settings_field( 'mc_general_time', __( 'Captcha time', 'math-captcha' ), array( &$this, 'mc_general_time' ), 'math_captcha_options', 'math_captcha_settings' );
126
+ add_settings_field( 'mc_general_block_direct_comments', __( 'Block Direct Comments', 'math-captcha' ), array( &$this, 'mc_general_block_direct_comments' ), 'math_captcha_options', 'math_captcha_settings' );
127
+ add_settings_field( 'mc_general_deactivation_delete', __( 'Deactivation', 'math-captcha' ), array( &$this, 'mc_general_deactivation_delete' ), 'math_captcha_options', 'math_captcha_settings' );
128
  }
129
 
 
130
  /**
131
  *
132
+ */
133
+ public function mc_general_enable_captcha_for() {
 
134
  echo '
135
  <div id="mc_general_enable_captcha_for">
136
  <fieldset>';
137
 
138
+ foreach ( $this->forms as $val => $trans ) {
 
139
  echo '
140
+ <input id="mc-general-enable-captcha-for-' . $val . '" type="checkbox" name="math_captcha_options[enable_for][]" value="' . $val . '" ' . checked( true, Math_Captcha()->options['general']['enable_for'][$val], false ) . ' ' . disabled( (($val === 'contact_form_7' && ! class_exists( 'WPCF7_ContactForm' )) || ($val === 'bbpress' && ! class_exists( 'bbPress' )) ), true, false ) . '/><label for="mc-general-enable-captcha-for-' . $val . '">' . esc_html( $trans ) . '</label>';
141
  }
142
 
143
  echo '
144
  <br/>
145
+ <span class="description">' . __( 'Select where you\'d like to use Math Captcha.', 'math-captcha' ) . '</span>
146
  </fieldset>
147
  </div>';
148
  }
149
 
 
150
  /**
151
  *
152
+ */
153
+ public function mc_general_hide_for_logged_users() {
 
154
  echo '
155
  <div id="mc_general_hide_for_logged_users">
156
  <fieldset>
157
+ <input id="mc-general-hide-for-logged" type="checkbox" name="math_captcha_options[hide_for_logged_users]" ' . checked( true, Math_Captcha()->options['general']['hide_for_logged_users'], false ) . '/><label for="mc-general-hide-for-logged">' . __( 'Enable to hide captcha for logged in users.', 'math-captcha' ) . '</label>
158
  <br/>
159
+ <span class="description">' . __( 'Would you like to hide captcha for logged in users?', 'math-captcha' ) . '</span>
160
  </fieldset>
161
  </div>';
162
  }
163
 
 
164
  /**
165
  *
166
+ */
167
+ public function mc_general_mathematical_operations() {
 
168
  echo '
169
  <div id="mc_general_mathematical_operations">
170
  <fieldset>';
171
 
172
+ foreach ( $this->mathematical_operations as $val => $trans ) {
 
173
  echo '
174
+ <input id="mc-general-mathematical-operations-' . $val . '" type="checkbox" name="math_captcha_options[mathematical_operations][]" value="' . $val . '" ' . checked( true, Math_Captcha()->options['general']['mathematical_operations'][$val], false ) . '/><label for="mc-general-mathematical-operations-' . $val . '">' . esc_html( $trans ) . '</label>';
175
  }
176
 
177
  echo '
178
  <br/>
179
+ <span class="description">' . __( 'Select which mathematical operations to use in your captcha.', 'math-captcha' ) . '</span>
180
  </fieldset>
181
  </div>';
182
  }
183
 
 
184
  /**
185
  *
186
+ */
187
+ public function mc_general_groups() {
 
188
  echo '
189
  <div id="mc_general_groups">
190
  <fieldset>';
191
 
192
+ foreach ( $this->groups as $val => $trans ) {
 
193
  echo '
194
+ <input id="mc-general-groups-' . $val . '" type="checkbox" name="math_captcha_options[groups][]" value="' . $val . '" ' . checked( true, Math_Captcha()->options['general']['groups'][$val], false ) . '/><label for="mc-general-groups-' . $val . '">' . esc_html( $trans ) . '</label>';
195
  }
196
 
197
  echo '
198
  <br/>
199
+ <span class="description">' . __( 'Select how you\'d like to display you captcha.', 'math-captcha' ) . '</span>
200
  </fieldset>
201
  </div>';
202
  }
203
 
 
204
  /**
205
  *
206
+ */
207
+ public function mc_general_title() {
 
208
  echo '
209
  <div id="mc_general_title">
210
  <fieldset>
211
+ <input type="text" name="math_captcha_options[title]" value="' . Math_Captcha()->options['general']['title'] . '"/>
212
  <br/>
213
+ <span class="description">' . __( 'How to entitle field with captcha?', 'math-captcha' ) . '</span>
214
  </fieldset>
215
  </div>';
216
  }
217
 
 
218
  /**
219
  *
220
+ */
221
+ public function mc_general_time() {
 
222
  echo '
223
  <div id="mc_general_time">
224
  <fieldset>
225
+ <input type="text" name="math_captcha_options[time]" value="' . Math_Captcha()->options['general']['time'] . '"/>
226
  <br/>
227
+ <span class="description">' . __( 'Enter the time (in seconds) a user has to enter captcha value.', 'math-captcha' ) . '</span>
228
  </fieldset>
229
  </div>';
230
  }
231
 
 
232
  /**
233
  *
234
+ */
235
+ public function mc_general_block_direct_comments() {
 
236
  echo '
237
  <div id="mc_general_block_direct_comments">
238
  <fieldset>
239
+ <input id="mc-general-block-direct-comments" type="checkbox" name="math_captcha_options[block_direct_comments]" ' . checked( true, Math_Captcha()->options['general']['block_direct_comments'], false ) . '/><label for="mc-general-block-direct-comments">' . __( 'Block direct access to wp-comments-post.php.', 'math-captcha' ) . '</label>
240
  <br/>
241
+ <span class="description">' . __( 'Enable this to prevent spambots from posting to Wordpress via a URL.', 'math-captcha' ) . '</span>
242
  </fieldset>
243
  </div>';
244
  }
245
 
 
246
  /**
247
  *
248
+ */
249
+ public function mc_general_deactivation_delete() {
 
250
  echo '
251
  <div id="mc_general_deactivation_delete">
252
  <fieldset>
253
+ <input id="mc-general-deactivation-delete" type="checkbox" name="math_captcha_options[deactivation_delete]" ' . checked( true, Math_Captcha()->options['general']['deactivation_delete'], false ) . '/><label for="mc-general-deactivation-delete">' . __( 'Delete settings on plugin deactivation.', 'math-captcha' ) . '</label>
254
  <br/>
255
+ <span class="description">' . __( 'Delete settings on plugin deactivation', 'math-captcha' ) . '</span>
256
  </fieldset>
257
  </div>';
258
  }
259
 
 
260
  /**
261
  * Validates settings
262
+ */
263
+ public function validate_settings( $input ) {
264
+ if ( isset( $_POST['save_mc_general'] ) ) {
 
 
265
  // enable captcha forms
266
  $enable_for = array();
267
 
268
+ if ( empty( $input['enable_for'] ) ) {
269
+ foreach ( Math_Captcha()->defaults['general']['enable_for'] as $enable => $bool ) {
 
 
270
  $input['enable_for'][$enable] = false;
271
  }
272
+ } else {
273
+ foreach ( $this->forms as $enable => $trans ) {
274
+ $enable_for[$enable] = (in_array( $enable, $input['enable_for'] ) ? true : false);
 
 
 
275
  }
276
 
277
  $input['enable_for'] = $enable_for;
278
  }
279
 
280
+ if ( ! class_exists( 'WPCF7_ContactForm' ) && Math_Captcha()->options['general']['enable_for']['contact_form_7'] )
281
  $input['enable_for']['contact_form_7'] = true;
282
 
283
+ if ( ! class_exists( 'bbPress' ) && Math_Captcha()->options['general']['enable_for']['bbpress'] )
284
  $input['enable_for']['bbpress'] = true;
285
 
286
  // enable mathematical operations
287
  $mathematical_operations = array();
288
 
289
+ if ( empty( $input['mathematical_operations'] ) ) {
290
+ add_settings_error( 'empty-operations', 'settings_updated', __( 'You need to check at least one mathematical operation. Defaults settings of this option restored.', 'math-captcha' ), 'error' );
 
291
 
292
  $input['mathematical_operations'] = Math_Captcha()->defaults['general']['mathematical_operations'];
293
+ } else {
294
+ foreach ( $this->mathematical_operations as $operation => $trans ) {
295
+ $mathematical_operations[$operation] = (in_array( $operation, $input['mathematical_operations'] ) ? true : false);
 
 
 
296
  }
297
 
298
  $input['mathematical_operations'] = $mathematical_operations;
301
  // enable groups
302
  $groups = array();
303
 
304
+ if ( empty( $input['groups'] ) ) {
305
+ add_settings_error( 'empty-groups', 'settings_updated', __( 'You need to check at least one group. Defaults settings of this option restored.', 'math-captcha' ), 'error' );
 
306
 
307
  $input['groups'] = Math_Captcha()->defaults['general']['groups'];
308
+ } else {
309
+ foreach ( $this->groups as $group => $trans ) {
310
+ $groups[$group] = (in_array( $group, $input['groups'] ) ? true : false);
 
 
 
311
  }
312
 
313
  $input['groups'] = $groups;
314
  }
315
 
316
  // hide for logged in users
317
+ $input['hide_for_logged_users'] = isset( $input['hide_for_logged_users'] );
318
 
319
  // block direct comments access
320
+ $input['block_direct_comments'] = isset( $input['block_direct_comments'] );
321
 
322
  // deactivation delete
323
+ $input['deactivation_delete'] = isset( $input['deactivation_delete'] );
324
 
325
  // captcha title
326
+ $input['title'] = trim( $input['title'] );
327
 
328
  // captcha time
329
+ $time = (int) $input['time'];
330
  $input['time'] = ($time < 0 ? Math_Captcha()->defaults['general']['time'] : $time);
331
 
332
  // flush rules
333
  $input['flush_rules'] = true;
334
+ } elseif ( isset( $_POST['reset_mc_general'] ) ) {
 
 
335
  $input = Math_Captcha()->defaults['general'];
336
 
337
+ add_settings_error( 'settings', 'settings_reset', __( 'Settings restored to defaults.', 'math-captcha' ), 'updated' );
338
  }
339
 
340
  return $input;
341
  }
342
+
343
+ }
includes/class-update.php CHANGED
@@ -1,43 +1,39 @@
1
  <?php
2
- if(!defined('ABSPATH')) exit;
 
3
 
4
  new Math_Captcha_Update();
5
 
6
- class Math_Captcha_Update
7
- {
8
  /**
9
  *
10
- */
11
- public function __construct()
12
- {
13
  // actions
14
- add_action('init', array(&$this, 'check_update'));
15
  }
16
 
17
-
18
  /**
19
  *
20
- */
21
- public function check_update()
22
- {
23
- if(!is_admin() || !current_user_can('manage_options'))
24
  return;
25
 
26
  // gets current database version
27
- $current_db_version = get_option('math_captcha_version', '1.0.0');
28
 
29
  // new version?
30
- if(version_compare($current_db_version, Math_Captcha()->defaults['version'], '<'))
31
- {
32
- if(version_compare($current_db_version, '1.0.9', '<'))
33
- {
34
- update_option('math_captcha_options', Math_Captcha()->options['general']);
35
- delete_option('mc_options');
36
  }
37
 
38
  // updates plugin version
39
- update_option('math_captcha_version', Math_Captcha()->defaults['version']);
40
  }
41
  }
42
- }
43
- ?>
1
  <?php
2
+ if ( ! defined( 'ABSPATH' ) )
3
+ exit;
4
 
5
  new Math_Captcha_Update();
6
 
7
+ class Math_Captcha_Update {
8
+
9
  /**
10
  *
11
+ */
12
+ public function __construct() {
 
13
  // actions
14
+ add_action( 'init', array( &$this, 'check_update' ) );
15
  }
16
 
 
17
  /**
18
  *
19
+ */
20
+ public function check_update() {
21
+ if ( ! is_admin() || ! current_user_can( 'manage_options' ) )
 
22
  return;
23
 
24
  // gets current database version
25
+ $current_db_version = get_option( 'math_captcha_version', '1.0.0' );
26
 
27
  // new version?
28
+ if ( version_compare( $current_db_version, Math_Captcha()->defaults['version'], '<' ) ) {
29
+ if ( version_compare( $current_db_version, '1.0.9', '<' ) ) {
30
+ update_option( 'math_captcha_options', Math_Captcha()->options['general'] );
31
+ delete_option( 'mc_options' );
 
 
32
  }
33
 
34
  // updates plugin version
35
+ update_option( 'math_captcha_version', Math_Captcha()->defaults['version'] );
36
  }
37
  }
38
+
39
+ }
includes/integrations/contact-form-7.php CHANGED
@@ -1,105 +1,88 @@
1
  <?php
2
  /**
3
- A module for [mathcaptcha]
4
- **/
5
 
6
  // shortcode handler
7
- add_action('init', 'wpcf7_add_shortcode_mathcaptcha', 5);
8
 
9
- function wpcf7_add_shortcode_mathcaptcha()
10
- {
11
- wpcf7_add_shortcode('mathcaptcha', 'wpcf7_mathcaptcha_shortcode_handler', true);
12
  }
13
 
 
 
 
14
 
15
- function wpcf7_mathcaptcha_shortcode_handler($tag)
16
- {
17
- if(!is_user_logged_in() || (is_user_logged_in() && !Math_Captcha()->options['general']['hide_for_logged_users']))
18
- {
19
- $tag = new WPCF7_Shortcode($tag);
20
-
21
- if(empty($tag->name))
22
  return '';
23
 
24
- $validation_error = wpcf7_get_validation_error($tag->name);
25
- $class = wpcf7_form_controls_class($tag->type);
26
 
27
- if($validation_error)
28
  $class .= ' wpcf7-not-valid';
29
 
30
  $atts = array();
31
  $atts['size'] = 2;
32
  $atts['maxlength'] = 2;
33
- $atts['class'] = $tag->get_class_option($class);
34
- $atts['id'] = $tag->get_option('id', 'id', true);
35
- $atts['tabindex'] = $tag->get_option('tabindex', 'int', true);
36
  $atts['aria-required'] = 'true';
37
  $atts['type'] = 'text';
38
  $atts['name'] = $tag->name;
39
  $atts['value'] = '';
40
- $atts = wpcf7_format_atts($atts);
41
 
42
- $mc_form = Math_Captcha()->core->generate_captcha_phrase('cf7');
43
  $mc_form[$mc_form['input']] = '<input %2$s />';
44
 
45
- $math_captcha_title = apply_filters('math_captcha_title', Math_Captcha()->options['general']['title']);
46
 
47
- return sprintf(((empty($math_captcha_title)) ? '' : $math_captcha_title).'<span class="wpcf7-form-control-wrap %1$s">'.$mc_form[1].$mc_form[2].$mc_form[3].'%3$s</span><input type="hidden" value="'.(Math_Captcha()->core->session_number - 1).'" name="'.$tag->name.'-sn" />', $tag->name, $atts, $validation_error);
48
  }
49
  }
50
 
51
-
52
  // validation
53
- add_filter('wpcf7_validate_mathcaptcha', 'wpcf7_mathcaptcha_validation_filter', 10, 2);
54
 
55
- function wpcf7_mathcaptcha_validation_filter($result, $tag)
56
- {
57
- $tag = new WPCF7_Shortcode($tag);
58
  $name = $tag->name;
59
 
60
- if(!is_admin() && isset($_POST[$name]))
61
- {
62
- $cf7_version = get_option('wpcf7', '1.0.0');
63
 
64
- if(is_array($cf7_version) && isset($cf7_version['version']))
65
  $cf7_version = $cf7_version['version'];
66
 
67
- if($_POST[$name] !== '')
68
- {
69
- $session_id = (isset($_POST[$name.'-sn']) && $_POST[$name.'-sn'] !== '' ? Math_Captcha()->cookie_session->session_ids['multi'][$_POST[$name.'-sn']] : '');
70
-
71
- if($session_id !== '' && get_transient('cf7_'.$session_id) !== false)
72
- {
73
- if(strcmp(get_transient('cf7_'.$session_id), sha1(AUTH_KEY.$_POST[$name].$session_id, false)) !== 0)
74
- {
75
- if(version_compare($cf7_version, '4.1.0', '>='))
76
- $result->invalidate($tag, wpcf7_get_message('wrong_mathcaptcha'));
77
- else
78
- {
79
  $result['valid'] = false;
80
- $result['reason'][$name] = wpcf7_get_message('wrong_mathcaptcha');
81
  }
82
  }
83
- }
84
- else
85
- {
86
- if(version_compare($cf7_version, '4.1.0', '>='))
87
- $result->invalidate($tag, wpcf7_get_message('time_mathcaptcha'));
88
- else
89
- {
90
  $result['valid'] = false;
91
- $result['reason'][$name] = wpcf7_get_message('time_mathcaptcha');
92
  }
93
  }
94
- }
95
- else
96
- {
97
- if(version_compare($cf7_version, '4.1.0', '>='))
98
- $result->invalidate($tag, wpcf7_get_message('fill_mathcaptcha'));
99
- else
100
- {
101
  $result['valid'] = false;
102
- $result['reason'][$name] = wpcf7_get_message('fill_mathcaptcha');
103
  }
104
  }
105
  }
@@ -107,90 +90,90 @@ function wpcf7_mathcaptcha_validation_filter($result, $tag)
107
  return $result;
108
  }
109
 
110
-
111
  // messages
112
- add_filter('wpcf7_messages', 'wpcf7_mathcaptcha_messages');
113
 
114
- function wpcf7_mathcaptcha_messages($messages)
115
- {
116
  return array_merge(
117
- $messages,
118
- array(
119
- 'wrong_mathcaptcha' => array(
120
- 'description' => __('Invalid captcha value.', 'math-captcha'),
121
- 'default' => Math_Captcha()->core->error_messages['wrong']
122
- ),
123
- 'fill_mathcaptcha' => array(
124
- 'description' => __('Please enter captcha value.', 'math-captcha'),
125
- 'default' => Math_Captcha()->core->error_messages['fill']
126
- ),
127
- 'time_mathcaptcha' => array(
128
- 'description' => __('Captcha time expired.', 'math-captcha'),
129
- 'default' => Math_Captcha()->core->error_messages['time']
130
- )
131
  )
132
  );
133
  }
134
 
135
-
136
  // warning message
137
- add_action('wpcf7_admin_notices', 'wpcf7_mathcaptcha_display_warning_message');
138
 
139
- function wpcf7_mathcaptcha_display_warning_message()
140
- {
141
- if(empty($_GET['post']) || !($contact_form = wpcf7_contact_form( $_GET['post'])))
142
  return;
143
 
144
- $has_tags = (bool)$contact_form->form_scan_shortcode(array('type' => array('mathcaptcha')));
145
 
146
- if(!$has_tags)
147
  return;
148
  }
149
 
150
-
151
  // tag generator
152
- add_action('admin_init', 'wpcf7_add_tag_generator_mathcaptcha', 45);
153
 
154
- function wpcf7_add_tag_generator_mathcaptcha()
155
- {
156
- if(!function_exists('wpcf7_add_tag_generator'))
157
  return;
158
 
159
- wpcf7_add_tag_generator('mathcaptcha', __('Math Captcha', 'math-captcha'), 'wpcf7-mathcaptcha', 'wpcf7_tg_pane_mathcaptcha');
160
  }
161
 
162
-
163
- function wpcf7_tg_pane_mathcaptcha($contact_form)
164
- {
165
  echo '
166
- <div id="wpcf7-mathcaptcha" class="hidden">
167
- <form action="">
168
- <table>
169
- <tr>
170
- <td>
171
- '.esc_html(__('Name', 'math-captcha')).'<br />
172
- <input type="text" name="name" class="tg-name oneline" />
173
- </td>
174
- </tr>
175
- </table>
176
- <table class="scope mathcaptcha">
177
- <caption>'.esc_html(__('Input field settings', 'math-captcha')).'</caption>
178
- <tr>
179
- <td>
180
- <code>id</code> ('.esc_html(__('optional', 'math-captcha')).')<br />
181
- <input type="text" name="id" class="idvalue oneline option" />
182
- </td>
183
- <td>
184
- <code>class</code> ('.esc_html(__('optional', 'math-captcha')).')<br />
185
- <input type="text" name="class" class="classvalue oneline option" />
186
- </td>
187
- </tr>
 
 
 
 
 
 
 
188
  </table>
189
- <div class="tg-tag">
190
- '.esc_html(__('Copy this code and paste it into the form left.', 'math-captcha')).'<br />
191
- <input type="text" name="mathcaptcha" class="tag" readonly="readonly" onfocus="this.select()" />
192
- </div>
193
- </form>
 
 
 
194
  </div>';
195
- }
196
- ?>
1
  <?php
2
  /**
3
+ * A module for [mathcaptcha]
4
+ */
5
 
6
  // shortcode handler
7
+ add_action( 'init', 'wpcf7_add_shortcode_mathcaptcha', 5 );
8
 
9
+ function wpcf7_add_shortcode_mathcaptcha() {
10
+ wpcf7_add_shortcode( 'mathcaptcha', 'wpcf7_mathcaptcha_shortcode_handler', true );
 
11
  }
12
 
13
+ function wpcf7_mathcaptcha_shortcode_handler( $tag ) {
14
+ if ( ! is_user_logged_in() || (is_user_logged_in() && ! Math_Captcha()->options['general']['hide_for_logged_users']) ) {
15
+ $tag = new WPCF7_Shortcode( $tag );
16
 
17
+ if ( empty( $tag->name ) )
 
 
 
 
 
 
18
  return '';
19
 
20
+ $validation_error = wpcf7_get_validation_error( $tag->name );
21
+ $class = wpcf7_form_controls_class( $tag->type );
22
 
23
+ if ( $validation_error )
24
  $class .= ' wpcf7-not-valid';
25
 
26
  $atts = array();
27
  $atts['size'] = 2;
28
  $atts['maxlength'] = 2;
29
+ $atts['class'] = $tag->get_class_option( $class );
30
+ $atts['id'] = $tag->get_option( 'id', 'id', true );
31
+ $atts['tabindex'] = $tag->get_option( 'tabindex', 'int', true );
32
  $atts['aria-required'] = 'true';
33
  $atts['type'] = 'text';
34
  $atts['name'] = $tag->name;
35
  $atts['value'] = '';
36
+ $atts = wpcf7_format_atts( $atts );
37
 
38
+ $mc_form = Math_Captcha()->core->generate_captcha_phrase( 'cf7' );
39
  $mc_form[$mc_form['input']] = '<input %2$s />';
40
 
41
+ $math_captcha_title = apply_filters( 'math_captcha_title', Math_Captcha()->options['general']['title'] );
42
 
43
+ return sprintf( ((empty( $math_captcha_title )) ? '' : $math_captcha_title) . '<span class="wpcf7-form-control-wrap %1$s">' . $mc_form[1] . $mc_form[2] . $mc_form[3] . '%3$s</span><input type="hidden" value="' . (Math_Captcha()->core->session_number - 1) . '" name="' . $tag->name . '-sn" />', $tag->name, $atts, $validation_error );
44
  }
45
  }
46
 
 
47
  // validation
48
+ add_filter( 'wpcf7_validate_mathcaptcha', 'wpcf7_mathcaptcha_validation_filter', 10, 2 );
49
 
50
+ function wpcf7_mathcaptcha_validation_filter( $result, $tag ) {
51
+ $tag = new WPCF7_Shortcode( $tag );
 
52
  $name = $tag->name;
53
 
54
+ if ( ! is_admin() && isset( $_POST[$name] ) ) {
55
+ $cf7_version = get_option( 'wpcf7', '1.0.0' );
 
56
 
57
+ if ( is_array( $cf7_version ) && isset( $cf7_version['version'] ) )
58
  $cf7_version = $cf7_version['version'];
59
 
60
+ if ( $_POST[$name] !== '' ) {
61
+ $session_id = (isset( $_POST[$name . '-sn'] ) && $_POST[$name . '-sn'] !== '' ? Math_Captcha()->cookie_session->session_ids['multi'][$_POST[$name . '-sn']] : '');
62
+
63
+ if ( $session_id !== '' && get_transient( 'cf7_' . $session_id ) !== false ) {
64
+ if ( strcmp( get_transient( 'cf7_' . $session_id ), sha1( AUTH_KEY . $_POST[$name] . $session_id, false ) ) !== 0 ) {
65
+ if ( version_compare( $cf7_version, '4.1.0', '>=' ) )
66
+ $result->invalidate( $tag, wpcf7_get_message( 'wrong_mathcaptcha' ) );
67
+ else {
 
 
 
 
68
  $result['valid'] = false;
69
+ $result['reason'][$name] = wpcf7_get_message( 'wrong_mathcaptcha' );
70
  }
71
  }
72
+ } else {
73
+ if ( version_compare( $cf7_version, '4.1.0', '>=' ) )
74
+ $result->invalidate( $tag, wpcf7_get_message( 'time_mathcaptcha' ) );
75
+ else {
 
 
 
76
  $result['valid'] = false;
77
+ $result['reason'][$name] = wpcf7_get_message( 'time_mathcaptcha' );
78
  }
79
  }
80
+ } else {
81
+ if ( version_compare( $cf7_version, '4.1.0', '>=' ) )
82
+ $result->invalidate( $tag, wpcf7_get_message( 'fill_mathcaptcha' ) );
83
+ else {
 
 
 
84
  $result['valid'] = false;
85
+ $result['reason'][$name] = wpcf7_get_message( 'fill_mathcaptcha' );
86
  }
87
  }
88
  }
90
  return $result;
91
  }
92
 
 
93
  // messages
94
+ add_filter( 'wpcf7_messages', 'wpcf7_mathcaptcha_messages' );
95
 
96
+ function wpcf7_mathcaptcha_messages( $messages ) {
 
97
  return array_merge(
98
+ $messages, array(
99
+ 'wrong_mathcaptcha' => array(
100
+ 'description' => __( 'Invalid captcha value.', 'math-captcha' ),
101
+ 'default' => Math_Captcha()->core->error_messages['wrong']
102
+ ),
103
+ 'fill_mathcaptcha' => array(
104
+ 'description' => __( 'Please enter captcha value.', 'math-captcha' ),
105
+ 'default' => Math_Captcha()->core->error_messages['fill']
106
+ ),
107
+ 'time_mathcaptcha' => array(
108
+ 'description' => __( 'Captcha time expired.', 'math-captcha' ),
109
+ 'default' => Math_Captcha()->core->error_messages['time']
110
+ )
 
111
  )
112
  );
113
  }
114
 
 
115
  // warning message
116
+ add_action( 'wpcf7_admin_notices', 'wpcf7_mathcaptcha_display_warning_message' );
117
 
118
+ function wpcf7_mathcaptcha_display_warning_message() {
119
+ if ( empty( $_GET['post'] ) || ! ($contact_form = wpcf7_contact_form( $_GET['post'] )) )
 
120
  return;
121
 
122
+ $has_tags = (bool) $contact_form->form_scan_shortcode( array( 'type' => array( 'mathcaptcha' ) ) );
123
 
124
+ if ( ! $has_tags )
125
  return;
126
  }
127
 
 
128
  // tag generator
129
+ add_action( 'admin_init', 'wpcf7_add_tag_generator_mathcaptcha', 45 );
130
 
131
+ function wpcf7_add_tag_generator_mathcaptcha() {
132
+ if ( ! function_exists( 'wpcf7_add_tag_generator' ) )
 
133
  return;
134
 
135
+ wpcf7_add_tag_generator( 'mathcaptcha', __( 'Math Captcha', 'math-captcha' ), 'wpcf7-mathcaptcha', 'wpcf7_tg_pane_mathcaptcha' );
136
  }
137
 
138
+ function wpcf7_tg_pane_mathcaptcha( $contact_form ) {
 
 
139
  echo '
140
+ <div class="control-box">
141
+ <fieldset>
142
+ <table class="form-table">
143
+ <tbody>
144
+ <tr>
145
+ <th scope="row">
146
+ <label for="tag-generator-panel-mathcaptcha-name">' . esc_html__( 'Name', 'contact-form-7' ) . '</label>
147
+ </th>
148
+ <td>
149
+ <input type="text" name="name" class="tg-name oneline" id="tag-generator-panel-mathcaptcha-name" />
150
+ </td>
151
+ </tr>
152
+ <tr>
153
+ <th scope="row">
154
+ <label for="tag-generator-panel-mathcaptcha-id">' . esc_html__( 'Id attribute', 'contact-form-7' ) . '</label>
155
+ </th>
156
+ <td>
157
+ <input type="text" name="id" class="idvalue oneline option" id="tag-generator-panel-mathcaptcha-id" />
158
+ </td>
159
+ </tr>
160
+ <tr>
161
+ <th scope="row">
162
+ <label for="tag-generator-panel-mathcaptcha-class">' . esc_html__( 'Class attribute', 'contact-form-7' ) . '</label>
163
+ </th>
164
+ <td>
165
+ <input type="text" name="class" class="classvalue oneline option" id="tag-generator-panel-mathcaptcha-class" />
166
+ </td>
167
+ </tr>
168
+ </tbody>
169
  </table>
170
+ </fieldset>
171
+ </div>
172
+ <div class="insert-box">
173
+ <input type="text" name="mathcaptcha" class="tag code" readonly="readonly" onfocus="this.select();">
174
+ <div class="submitbox">
175
+ <input type="button" class="button button-primary insert-tag" value="' . esc_attr__( 'Insert Tag', 'contact-form-7' ) . '">
176
+ </div>