/var/www/html_us/wp-content/plugins/contact-form-7/includes/file.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<?php

/**
 * Validates uploaded files and moves them to the temporary directory.
 *
 * @param array $file An item of `$_FILES`.
 * @param string|array $args Optional. Arguments to control behavior.
 * @return array|WP_Error Array of file paths, or WP_Error if validation fails.
 */
function wpcf7_unship_uploaded_file$file$args '' ) {
    
$args wp_parse_args$args, array(
        
'required' => false,
        
'filetypes' => '',
        
'limit' => MB_IN_BYTES,
    ) );

    foreach ( array( 
'name''size''tmp_name''error' ) as $key ) {
        if ( ! isset( 
$file[$key] ) ) {
            
$file[$key] = array();
        }
    }

    
$names wpcf7_array_flatten$file['name'] );
    
$sizes wpcf7_array_flatten$file['size'] );
    
$tmp_names wpcf7_array_flatten$file['tmp_name'] );
    
$errors wpcf7_array_flatten$file['error'] );

    foreach ( 
$errors as $error ) {
        if ( ! empty( 
$error ) and UPLOAD_ERR_NO_FILE !== $error ) {
            return new 
WP_Error'wpcf7_upload_failed_php_error',
                
wpcf7_get_message'upload_failed_php_error' )
            );
        }
    }

    if ( isset( 
$args['schema'] ) and isset( $args['name'] ) ) {
        
$result $args['schema']->validate( array(
            
'file' => true,
            
'field' => $args['name'],
        ) );

        if ( 
is_wp_error$result ) ) {
            return 
$result;
        }
    }

    
// Move uploaded file to tmp dir
    
$uploads_dir wpcf7_upload_tmp_dir();
    
$uploads_dir wpcf7_maybe_add_random_dir$uploads_dir );

    
$uploaded_files = array();

    foreach ( 
$names as $key => $name ) {
        
$tmp_name $tmp_names[$key];

        if ( empty( 
$tmp_name ) or ! is_uploaded_file$tmp_name ) ) {
            continue;
        }

        
$filename $name;
        
$filename wpcf7_canonicalize$filename, array( 'strto' => 'as-is' ) );
        
$filename wpcf7_antiscript_file_name$filename );

        
$filename apply_filters'wpcf7_upload_file_name',
            
$filename$name$args
        
);

        
$filename wp_unique_filename$uploads_dir$filename );
        
$new_file path_join$uploads_dir$filename );

        if ( 
false === @move_uploaded_file$tmp_name$new_file ) ) {
            return new 
WP_Error'wpcf7_upload_failed',
                
wpcf7_get_message'upload_failed' )
            );
        }

        
// Make sure the uploaded file is only readable for the owner process
        
chmod$new_file0400 );

        
$uploaded_files[] = $new_file;
    }

    return 
$uploaded_files;
}


add_filter(
    
'wpcf7_messages',
    
'wpcf7_file_messages',
    
101
);

/**
 * A wpcf7_messages filter callback that adds messages for
 * file-uploading fields.
 */
function wpcf7_file_messages$messages ) {
    return 
array_merge$messages, array(
        
'upload_failed' => array(
            
'description' => __"Uploading a file fails for any reason"'contact-form-7' ),
            
'default' => __"There was an unknown error uploading the file."'contact-form-7' ),
        ),

        
'upload_file_type_invalid' => array(
            
'description' => __"Uploaded file is not allowed for file type"'contact-form-7' ),
            
'default' => __"You are not allowed to upload files of this type."'contact-form-7' ),
        ),

        
'upload_file_too_large' => array(
            
'description' => __"Uploaded file is too large"'contact-form-7' ),
            
'default' => __"The uploaded file is too large."'contact-form-7' ),
        ),

        
'upload_failed_php_error' => array(
            
'description' => __"Uploading a file fails for PHP error"'contact-form-7' ),
            
'default' => __"There was an error uploading the file."'contact-form-7' ),
        ),
    ) );
}


add_filter(
    
'wpcf7_form_enctype',
    
'wpcf7_file_form_enctype_filter',
    
101
);

/**
 * A wpcf7_form_enctype filter callback that sets the enctype attribute
 * to multipart/form-data if the form has file-uploading fields.
 */
function wpcf7_file_form_enctype_filter$enctype ) {
    
$multipart = (bool) wpcf7_scan_form_tags( array(
        
'feature' => 'file-uploading',
    ) );

    if ( 
$multipart ) {
        
$enctype 'multipart/form-data';
    }

    return 
$enctype;
}


/**
 * Converts a MIME type string to an array of corresponding file extensions.
 *
 * @param string $mime MIME type.
 *                     Wildcard (*) is available for the subtype part.
 * @return array Corresponding file extensions.
 */
function wpcf7_convert_mime_to_ext$mime ) {
    static 
$mime_types = array();

    
$mime_types wp_get_mime_types();

    
$results = array();

    if ( 
preg_match'%^([a-z]+)/([*]|[a-z0-9.+-]+)$%i'$mime$matches ) ) {
        foreach ( 
$mime_types as $key => $val ) {
            if ( 
'*' === $matches[2] and str_starts_with$val$matches[1] . '/' )
             or 
$val === $matches[0] ) {
                
$results array_merge$resultsexplode'|'$key ) );
            }
        }
    }

    
$results array_unique$results );
    
$results array_filter$results );
    
$results array_values$results );

    return 
$results;
}


/**
 * Returns a formatted list of acceptable filetypes.
 *
 * @param string|array $types Optional. Array of filetypes.
 * @param string $format Optional. Pre-defined format designator.
 * @return string Formatted list of acceptable filetypes.
 */
function wpcf7_acceptable_filetypes$types 'default'$format 'regex' ) {
    if ( 
'default' === $types or empty( $types ) ) {
        
$types = array(
            
'audio/*',
            
'video/*',
            
'image/*',
        );
    } else {
        
$types array_map(
            function ( 
$type ) {
                if ( 
is_string$type ) ) {
                    return 
preg_split'/[\s|,]+/'strtolower$type ) );
                }
            },
            (array) 
$types
        
);

        
$types wpcf7_array_flatten$types );
        
$types array_filterarray_unique$types ) );
    }

    if ( 
'attr' === $format or 'attribute' === $format ) {
        
$types array_map(
            function ( 
$type ) {
                if ( 
false === strpos$type'/' ) ) {
                    return 
sprintf'.%s'trim$type'.' ) );
                } elseif ( 
preg_match'%^([a-z]+)/[*]$%i'$type$matches ) ) {
                    if ( 
in_array$matches[1], array( 'audio''video''image' ) ) ) {
                        return 
$type;
                    } else {
                        return 
'';
                    }
                } elseif ( 
wpcf7_convert_mime_to_ext$type ) ) {
                    return 
$type;
                }
            },
            
$types
        
);

        
$types array_filter$types );

        return 
implode','$types );

    } elseif ( 
'regex' === $format ) {
        
$types array_map(
            function ( 
$type ) {
                if ( 
false === strpos$type'/' ) ) {
                    return 
preg_quotetrim$type'.' ) );
                } elseif ( 
$type wpcf7_convert_mime_to_ext$type ) ) {
                    return 
$type;
                }
            },
            
$types
        
);

        
$types wpcf7_array_flatten$types );
        
$types array_filterarray_unique$types ) );

        return 
implode'|'$types );
    }

    return 
'';
}


add_action(
    
'wpcf7_init',
    
'wpcf7_init_uploads',
    
100
);

/**
 * Initializes the temporary directory for uploaded files.
 */
function wpcf7_init_uploads() {
    
$dir wpcf7_upload_tmp_dir();

    if ( 
is_dir$dir ) and is_writable$dir ) ) {
        
$htaccess_file path_join$dir'.htaccess' );

        if ( ! 
file_exists$htaccess_file )
        and 
$handle = @fopen$htaccess_file'w' ) ) {
            
fwrite$handle"Deny from all\n" );
            
fclose$handle );
        }
    }
}


/**
 * Creates a child directory with a randomly generated name.
 *
 * @param string $dir The parent directory path.
 * @return string The child directory path if created, otherwise the parent.
 */
function wpcf7_maybe_add_random_dir$dir ) {
    do {
        
$rand_max mt_getrandmax();
        
$rand zeroisemt_rand0$rand_max ), strlen$rand_max ) );
        
$dir_new path_join$dir$rand );
    } while ( 
file_exists$dir_new ) );

    if ( 
wp_mkdir_p$dir_new ) ) {
        return 
$dir_new;
    }

    return 
$dir;
}


/**
 * Returns the directory path for uploaded files.
 *
 * @return string Directory path.
 */
function wpcf7_upload_tmp_dir() {
    if ( 
defined'WPCF7_UPLOADS_TMP_DIR' ) ) {
        
$dir path_joinWP_CONTENT_DIRWPCF7_UPLOADS_TMP_DIR );
        
wp_mkdir_p$dir );

        if ( 
wpcf7_is_file_path_in_content_dir$dir ) ) {
            return 
$dir;
        }
    }

    
$dir path_joinwpcf7_upload_dir'dir' ), 'wpcf7_uploads' );
    
wp_mkdir_p$dir );
    return 
$dir;
}


add_action(
    
'shutdown',
    
'wpcf7_cleanup_upload_files',
    
200
);

/**
 * Cleans up files in the temporary directory for uploaded files.
 *
 * @param int $seconds Files older than this are removed. Default 60.
 * @param int $max Maximum number of files to be removed in a function call.
 *                 Default 100.
 */
function wpcf7_cleanup_upload_files$seconds 60$max 100 ) {
    
$dir trailingslashitwpcf7_upload_tmp_dir() );

    if ( ! 
is_dir$dir )
    or ! 
is_readable$dir )
    or ! 
wp_is_writable$dir ) ) {
        return;
    }

    
$seconds absint$seconds );
    
$max absint$max );
    
$count 0;

    if ( 
$handle opendir$dir ) ) {
        while ( 
false !== ( $file readdir$handle ) ) ) {
            if ( 
'.' == $file
            
or '..' == $file
            
or '.htaccess' == $file ) {
                continue;
            }

            
$mtime = @filemtimepath_join$dir$file ) );

            if ( 
$mtime and time() < $mtime $seconds ) { // less than $seconds old
                
continue;
            }

            
wpcf7_rmdir_ppath_join$dir$file ) );
            
$count += 1;

            if ( 
$max <= $count ) {
                break;
            }
        }

        
closedir$handle );
    }
}


add_action(
    
'wpcf7_admin_warnings',
    
'wpcf7_file_display_warning_message',
    
103
);

/**
 * Displays warning messages about file-uploading fields.
 */
function wpcf7_file_display_warning_message$page$action$object ) {
    if ( 
$object instanceof WPCF7_ContactForm ) {
        
$contact_form $object;
    } else {
        return;
    }

    
$has_tags = (bool) $contact_form->scan_form_tags( array(
        
'feature' => 'file-uploading',
    ) );

    if ( ! 
$has_tags ) {
        return;
    }

    
$uploads_dir wpcf7_upload_tmp_dir();

    if ( ! 
is_dir$uploads_dir ) or ! wp_is_writable$uploads_dir ) ) {
        
$message sprintf(
            
/* translators: %s: the path of the temporary folder */
            
__'This contact form has file uploading fields, but the temporary folder for the files (%s) does not exist or is not writable. You can create the folder or change its permission manually.''contact-form-7' ),
            
$uploads_dir
        
);

        echo 
sprintf(
            
'<div class="notice notice-warning"><p>%s</p></div>',
            
esc_html$message )
        );
    }
}