config-validator.php
7.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
class WPCF7_ConfigValidator {
const error_maybe_empty = 101;
const error_invalid_syntax = 102;
const error_email_not_in_site_domain = 103;
const error_html_in_message = 104;
private $contact_form;
private $errors = array();
public function __construct( WPCF7_ContactForm $contact_form ) {
$this->contact_form = $contact_form;
$this->errors = (array) get_post_meta(
$this->contact_form->id(), '_config_errors', true );
$this->errors = array_filter( $this->errors );
}
public function is_valid() {
return ! $this->errors;
}
public function get_errors() {
return $this->errors;
}
public function get_error( $section ) {
if ( isset( $this->errors[$section] ) ) {
return $this->errors[$section];
}
return null;
}
public function get_error_message( $section ) {
$code = $this->get_error( $section );
switch ( $code ) {
case self::error_maybe_empty:
return __( "This field can be empty depending on user input.", 'contact-form-7' );
case self::error_invalid_syntax:
return __( "This field has syntax errors.", 'contact-form-7' );
case self::error_email_not_in_site_domain:
return __( "This email address does not belong to the same domain as the site.", 'contact-form-7' );
case self::error_html_in_message:
return __( "HTML tags are not allowed in a message.", 'contact-form-7' );
default:
return '';
}
}
private function add_error( $section, $error ) {
$this->errors[$section] = $error;
}
public function validate() {
$this->errors = array();
$this->validate_mail( 'mail' );
$this->validate_mail( 'mail_2' );
$this->validate_messages();
delete_post_meta( $this->contact_form->id(), '_config_errors' );
if ( $this->errors ) {
update_post_meta( $this->contact_form->id(), '_config_errors',
$this->errors );
return false;
}
return true;
}
public function validate_mail( $template = 'mail' ) {
$components = (array) $this->contact_form->prop( $template );
if ( ! $components ) {
return;
}
if ( 'mail' != $template && empty( $components['active'] ) ) {
return;
}
$components = wp_parse_args( $components, array(
'subject' => '',
'sender' => '',
'recipient' => '',
'additional_headers' => '',
'body' => '' ) );
$callback = array( $this, 'replace_mail_tags_with_minimum_input' );
$subject = $components['subject'];
$subject = new WPCF7_MailTaggedText( $subject,
array( 'callback' => $callback ) );
$subject = $subject->replace_tags();
$subject = wpcf7_strip_newline( $subject );
if ( '' === $subject ) {
$this->add_error( sprintf( '%s.subject', $template ),
self::error_maybe_empty );
}
$sender = $components['sender'];
$sender = new WPCF7_MailTaggedText( $sender,
array( 'callback' => $callback ) );
$sender = $sender->replace_tags();
$sender = wpcf7_strip_newline( $sender );
if ( ! wpcf7_is_mailbox_list( $sender ) ) {
$this->add_error( sprintf( '%s.sender', $template ),
self::error_invalid_syntax );
} elseif ( ! wpcf7_is_email_in_site_domain( $sender ) ) {
$this->add_error( sprintf( '%s.sender', $template ),
self::error_email_not_in_site_domain );
}
$recipient = $components['recipient'];
$recipient = new WPCF7_MailTaggedText( $recipient,
array( 'callback' => $callback ) );
$recipient = $recipient->replace_tags();
$recipient = wpcf7_strip_newline( $recipient );
if ( ! wpcf7_is_mailbox_list( $recipient ) ) {
$this->add_error( sprintf( '%s.recipient', $template ),
self::error_invalid_syntax );
}
$additional_headers = $components['additional_headers'];
$additional_headers = new WPCF7_MailTaggedText( $additional_headers,
array( 'callback' => $callback ) );
$additional_headers = $additional_headers->replace_tags();
if ( ! $this->test_additional_headers_syntax( $additional_headers ) ) {
$this->add_error( sprintf( '%s.additional_headers', $template ),
self::error_invalid_syntax );
}
$body = $components['body'];
$body = new WPCF7_MailTaggedText( $body,
array( 'callback' => $callback ) );
$body = $body->replace_tags();
if ( '' === $body ) {
$this->add_error( sprintf( '%s.body', $template ),
self::error_maybe_empty );
}
}
public function test_additional_headers_syntax( $content ) {
$headers = explode( "\n", $content );
foreach ( $headers as $header ) {
$header = trim( $header );
if ( '' === $header ) {
continue;
}
if ( ! preg_match( '/^([0-9A-Za-z-]+):(.+)$/', $header, $matches ) ) {
return false;
}
$is_mailbox_list_field = in_array( strtolower( $matches[1] ),
array( 'reply-to', 'cc', 'bcc' ) );
if ( $is_mailbox_list_field
&& ! wpcf7_is_mailbox_list( $matches[2] ) ) {
return false;
}
}
return true;
}
public function validate_messages() {
$messages = (array) $this->contact_form->prop( 'messages' );
if ( ! $messages ) {
return;
}
if ( isset( $messages['captcha_not_match'] )
&& ! wpcf7_use_really_simple_captcha() ) {
unset( $messages['captcha_not_match'] );
}
foreach ( $messages as $key => $message ) {
$stripped = wp_strip_all_tags( $message );
if ( $stripped != $message ) {
$this->add_error( sprintf( 'messages.%s', $key ),
self::error_html_in_message );
}
}
}
public function replace_mail_tags_with_minimum_input( $matches ) {
// allow [[foo]] syntax for escaping a tag
if ( $matches[1] == '[' && $matches[4] == ']' ) {
return substr( $matches[0], 1, -1 );
}
$tag = $matches[0];
$tagname = $matches[2];
$values = $matches[3];
if ( ! empty( $values ) ) {
preg_match_all( '/"[^"]*"|\'[^\']*\'/', $values, $matches );
$values = wpcf7_strip_quote_deep( $matches[0] );
}
$do_not_heat = false;
if ( preg_match( '/^_raw_(.+)$/', $tagname, $matches ) ) {
$tagname = trim( $matches[1] );
$do_not_heat = true;
}
$format = '';
if ( preg_match( '/^_format_(.+)$/', $tagname, $matches ) ) {
$tagname = trim( $matches[1] );
$format = $values[0];
}
$example_email = 'example@example.com';
$example_text = 'example';
$example_blank = '';
$form_tags = $this->contact_form->form_scan_shortcode(
array( 'name' => $tagname ) );
if ( $form_tags ) {
$form_tag = new WPCF7_Shortcode( $form_tags[0] );
$is_required = ( $form_tag->is_required() || 'radio' == $form_tag->type );
if ( ! $is_required ) {
return $example_blank;
}
$is_selectable = in_array( $form_tag->basetype,
array( 'radio', 'checkbox', 'select' ) );
if ( $is_selectable ) {
if ( $form_tag->pipes instanceof WPCF7_Pipes ) {
if ( $do_not_heat ) {
$before_pipes = $form_tag->pipes->collect_befores();
$last_item = array_pop( $before_pipes );
} else {
$after_pipes = $form_tag->pipes->collect_afters();
$last_item = array_pop( $after_pipes );
}
} else {
$last_item = array_pop( $form_tag->values );
}
if ( $last_item && wpcf7_is_mailbox_list( $last_item ) ) {
return $example_email;
} else {
return $example_text;
}
}
if ( 'email' == $form_tag->basetype ) {
return $example_email;
} else {
return $example_text;
}
} else {
$tagname = preg_replace( '/^wpcf7\./', '_', $tagname ); // for back-compat
if ( '_post_author_email' == $tagname ) {
return $example_email;
} elseif ( '_' == substr( $tagname, 0, 1 ) ) { // maybe special mail tag
return $example_text;
}
}
return $tag;
}
}