-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidation-functions.php
319 lines (252 loc) · 6.66 KB
/
validation-functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<?php
/**
* Checks whether a string is a valid NAME token.
*
* ID and NAME tokens must begin with a letter ([A-Za-z])
* and may be followed by any number of letters, digits ([0-9]),
* hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
*
* @link http://www.w3.org/TR/html401/types.html#h-6.2
*
* @return bool True if it is a valid name, false if not.
*/
function wpcf7_is_name( $text ) {
return preg_match( '/^[A-Za-z][-A-Za-z0-9_:.]*$/', $text );
}
/**
* Checks whether the given text is a well-formed email address.
*/
function wpcf7_is_email( $text ) {
$result = is_email( $text );
return apply_filters( 'wpcf7_is_email', $result, $text );
}
/**
* Checks whether the given text is a well-formed URL.
*/
function wpcf7_is_url( $text ) {
$scheme = wp_parse_url( $text, PHP_URL_SCHEME );
$result = $scheme && in_array( $scheme, wp_allowed_protocols(), true );
return apply_filters( 'wpcf7_is_url', $result, $text );
}
/**
* Checks whether the given text is a well-formed telephone number.
*/
function wpcf7_is_tel( $text ) {
$text = preg_replace( '/[#*].*$/', '', $text ); // Remove extension
$text = preg_replace( '%[()/.*#\s-]+%', '', $text );
$is_international = false ||
str_starts_with( $text, '+' ) ||
str_starts_with( $text, '00' );
if ( $is_international ) {
$text = '+' . preg_replace( '/^[+0]+/', '', $text );
}
$result = true;
if ( ! preg_match( '/^[+]?[0-9]+$/', $text ) ) {
$result = false;
}
if ( ! ( 6 < strlen( $text ) and strlen( $text ) < 16 ) ) {
$result = false;
}
return apply_filters( 'wpcf7_is_tel', $result, $text );
}
/**
* Checks whether the given text is a well-formed number.
*
* @link https://html.spec.whatwg.org/multipage/input.html#number-state-(type=number)
*/
function wpcf7_is_number( $text ) {
$result = false;
$patterns = array(
'/^[-]?[0-9]+(?:[eE][+-]?[0-9]+)?$/',
'/^[-]?(?:[0-9]+)?[.][0-9]+(?:[eE][+-]?[0-9]+)?$/',
);
foreach ( $patterns as $pattern ) {
if ( preg_match( $pattern, $text ) ) {
$result = true;
break;
}
}
return apply_filters( 'wpcf7_is_number', $result, $text );
}
/**
* Checks whether the given text is a valid date.
*
* @link https://html.spec.whatwg.org/multipage/input.html#date-state-(type=date)
*/
function wpcf7_is_date( $text ) {
$result = preg_match(
'/^([0-9]{4,})-([0-9]{2})-([0-9]{2})$/',
$text,
$matches
);
if ( $result ) {
$result = checkdate( $matches[2], $matches[3], $matches[1] );
}
return apply_filters( 'wpcf7_is_date', $result, $text );
}
/**
* Checks whether the given text is a valid time.
*
* @link https://html.spec.whatwg.org/multipage/input.html#time-state-(type=time)
*/
function wpcf7_is_time( $text ) {
$result = preg_match(
'/^([0-9]{2})\:([0-9]{2})(?:\:([0-9]{2}))?$/',
$text,
$matches
);
if ( $result ) {
$hour = (int) $matches[1];
$minute = (int) $matches[2];
$second = empty( $matches[3] ) ? 0 : (int) $matches[3];
$result = 0 <= $hour && $hour <= 23 &&
0 <= $minute && $minute <= 59 &&
0 <= $second && $second <= 59;
}
return apply_filters( 'wpcf7_is_time', $result, $text );
}
/**
* Checks whether the given text is a well-formed mailbox list.
*
* @param string|array $mailbox_list The subject to be checked.
* Comma-separated string or an array of mailboxes.
* @return array|bool Array of email addresses if all items are well-formed
* mailbox, false if not.
*/
function wpcf7_is_mailbox_list( $mailbox_list ) {
if ( ! is_array( $mailbox_list ) ) {
$mailbox_text = (string) $mailbox_list;
$mailbox_text = preg_replace(
'/\\\\(?:\"|\')/',
'esc-quote',
$mailbox_text
);
$mailbox_text = preg_replace(
'/(?:\".*?\"|\'.*?\')/',
'quoted-string',
$mailbox_text
);
$mailbox_list = explode( ',', $mailbox_text );
}
$addresses = array();
foreach ( $mailbox_list as $mailbox ) {
if ( ! is_string( $mailbox ) ) {
return false;
}
$mailbox = trim( $mailbox );
if ( '' === $mailbox ) {
continue;
}
if ( preg_match( '/<(.+)>$/', $mailbox, $matches ) ) {
$addr_spec = $matches[1];
} else {
$addr_spec = $mailbox;
}
if ( ! wpcf7_is_email( $addr_spec ) ) {
return false;
}
$addresses[] = $addr_spec;
}
return $addresses;
}
/**
* Checks whether an email address belongs to a domain.
*
* @param string $email A mailbox or a comma-separated list of mailboxes.
* @param string $domain Internet domain name.
* @return bool True if all of the email addresses belong to the domain,
* false if not.
*/
function wpcf7_is_email_in_domain( $email, $domain ) {
$email_list = wpcf7_is_mailbox_list( $email );
if ( false === $email_list ) {
return false;
}
$domain = strtolower( $domain );
foreach ( $email_list as $email ) {
$email_domain = substr( $email, strrpos( $email, '@' ) + 1 );
$email_domain = strtolower( $email_domain );
$domain_parts = explode( '.', $domain );
do {
$site_domain = implode( '.', $domain_parts );
if ( $site_domain == $email_domain ) {
continue 2;
}
array_shift( $domain_parts );
} while ( $domain_parts );
return false;
}
return true;
}
/**
* Checks whether an email address belongs to the site domain.
*/
function wpcf7_is_email_in_site_domain( $email ) {
if ( wpcf7_is_localhost() ) {
return true;
}
$homes = array(
home_url(),
network_home_url(),
);
$homes = array_unique( $homes );
foreach ( $homes as $home ) {
$sitename = wp_parse_url( $home, PHP_URL_HOST );
if ( WP_Http::is_ip_address( $sitename ) ) {
return true;
}
if ( wpcf7_is_email_in_domain( $email, $sitename ) ) {
return true;
}
}
return false;
}
/**
* Verifies that a given file path is under the directories that WordPress
* manages for user contents.
*
* Returns false if the file at the given path does not exist yet.
*
* @param string $path A file path.
* @return bool True if the path is under the content directories,
* false otherwise.
*/
function wpcf7_is_file_path_in_content_dir( $path ) {
if ( ! is_string( $path ) or '' === $path ) {
return false;
}
$callback = static function ( $path, $dir ) {
if ( $real_path = realpath( $path ) ) {
$path = $real_path;
} else {
return false;
}
if ( $real_dir = realpath( $dir ) ) {
$dir = trailingslashit( $real_dir );
} else {
return false;
}
return str_starts_with(
wp_normalize_path( $path ),
wp_normalize_path( $dir )
);
};
if (
call_user_func( $callback, $path, WP_CONTENT_DIR )
) {
return true;
}
if (
defined( 'UPLOADS' ) and
call_user_func( $callback, $path, ABSPATH . UPLOADS )
) {
return true;
}
if (
defined( 'WP_TEMP_DIR' ) and
call_user_func( $callback, $path, WP_TEMP_DIR )
) {
return true;
}
return false;
}