/var/www/(Del)hsihk.com/wp-content/plugins/contact-form-7/modules/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
<?php
/**
** A base module for [file] and [file*]
**/

/* Shortcode handler */

add_action'wpcf7_init''wpcf7_add_shortcode_file' );

function 
wpcf7_add_shortcode_file() {
    
wpcf7_add_shortcode( array( 'file''file*' ),
        
'wpcf7_file_shortcode_handler'true );
}

function 
wpcf7_file_shortcode_handler$tag ) {
    
$tag = new WPCF7_Shortcode$tag );

    if ( empty( 
$tag->name ) )
        return 
'';

    
$validation_error wpcf7_get_validation_error$tag->name );

    
$class wpcf7_form_controls_class$tag->type );

    if ( 
$validation_error )
        
$class .= ' wpcf7-not-valid';

    
$atts = array();

    
$atts['size'] = $tag->get_size_option'40' );
    
$atts['class'] = $tag->get_class_option$class );
    
$atts['id'] = $tag->get_id_option();
    
$atts['tabindex'] = $tag->get_option'tabindex''int'true );

    if ( 
$tag->is_required() )
        
$atts['aria-required'] = 'true';

    
$atts['aria-invalid'] = $validation_error 'true' 'false';

    
$atts['type'] = 'file';
    
$atts['name'] = $tag->name;
    
$atts['value'] = '1';

    
$atts wpcf7_format_atts$atts );

    
$html sprintf(
        
'<span class="wpcf7-form-control-wrap %1$s"><input %2$s />%3$s</span>',
        
sanitize_html_class$tag->name ), $atts$validation_error );

    return 
$html;
}


/* Encode type filter */

add_filter'wpcf7_form_enctype''wpcf7_file_form_enctype_filter' );

function 
wpcf7_file_form_enctype_filter$enctype ) {
    
$multipart = (bool) wpcf7_scan_shortcode( array( 'type' => array( 'file''file*' ) ) );

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

    return 
$enctype;
}


/* Validation + upload handling filter */

add_filter'wpcf7_validate_file''wpcf7_file_validation_filter'10);
add_filter'wpcf7_validate_file*''wpcf7_file_validation_filter'10);

function 
wpcf7_file_validation_filter$result$tag ) {
    
$tag = new WPCF7_Shortcode$tag );

    
$name $tag->name;
    
$id $tag->get_id_option();

    
$file = isset( $_FILES[$name] ) ? $_FILES[$name] : null;

    if ( 
$file['error'] && UPLOAD_ERR_NO_FILE != $file['error'] ) {
        
$result['valid'] = false;
        
$result['reason'][$name] = wpcf7_get_message'upload_failed_php_error' );
        
$result['idref'][$name] = $id $id null;
        return 
$result;
    }

    if ( empty( 
$file['tmp_name'] ) && $tag->is_required() ) {
        
$result['valid'] = false;
        
$result['reason'][$name] = wpcf7_get_message'invalid_required' );
        
$result['idref'][$name] = $id $id null;
        return 
$result;
    }

    if ( ! 
is_uploaded_file$file['tmp_name'] ) )
        return 
$result;

    
$allowed_file_types = array();

    if ( 
$file_types_a $tag->get_option'filetypes' ) ) {
        foreach ( 
$file_types_a as $file_types ) {
            
$file_types explode'|'$file_types );

            foreach ( 
$file_types as $file_type ) {
                
$file_type trim$file_type'.' );
                
$file_type str_replace( array( '.''+''*''?' ),
                    array( 
'\.''\+''\*''\?' ), $file_type );
                
$allowed_file_types[] = $file_type;
            }
        }
    }

    
$allowed_file_types array_unique$allowed_file_types );
    
$file_type_pattern implode'|'$allowed_file_types );

    
$allowed_size 1048576// default size 1 MB

    
if ( $file_size_a $tag->get_option'limit' ) ) {
        
$limit_pattern '/^([1-9][0-9]*)([kKmM]?[bB])?$/';

        foreach ( 
$file_size_a as $file_size ) {
            if ( 
preg_match$limit_pattern$file_size$matches ) ) {
                
$allowed_size = (int) $matches[1];

                if ( ! empty( 
$matches[2] ) ) {
                    
$kbmb strtolower$matches[2] );

                    if ( 
'kb' == $kbmb )
                        
$allowed_size *= 1024;
                    elseif ( 
'mb' == $kbmb )
                        
$allowed_size *= 1024 1024;
                }

                break;
            }
        }
    }

    
/* File type validation */

    // Default file-type restriction
    
if ( '' == $file_type_pattern )
        
$file_type_pattern 'jpg|jpeg|png|gif|pdf|doc|docx|ppt|pptx|odt|avi|ogg|m4a|mov|mp3|mp4|mpg|wav|wmv';

    
$file_type_pattern trim$file_type_pattern'|' );
    
$file_type_pattern '(' $file_type_pattern ')';
    
$file_type_pattern '/\.' $file_type_pattern '$/i';

    if ( ! 
preg_match$file_type_pattern$file['name'] ) ) {
        
$result['valid'] = false;
        
$result['reason'][$name] = wpcf7_get_message'upload_file_type_invalid' );
        
$result['idref'][$name] = $id $id null;
        return 
$result;
    }

    
/* File size validation */

    
if ( $file['size'] > $allowed_size ) {
        
$result['valid'] = false;
        
$result['reason'][$name] = wpcf7_get_message'upload_file_too_large' );
        
$result['idref'][$name] = $id $id null;
        return 
$result;
    }

    
wpcf7_init_uploads(); // Confirm upload dir
    
$uploads_dir wpcf7_upload_tmp_dir();
    
$uploads_dir wpcf7_maybe_add_random_dir$uploads_dir );

    
$filename $file['name'];
    
$filename wpcf7_antiscript_file_name$filename );
    
$filename wp_unique_filename$uploads_dir$filename );

    
$new_file trailingslashit$uploads_dir ) . $filename;

    if ( 
false === @move_uploaded_file$file['tmp_name'], $new_file ) ) {
        
$result['valid'] = false;
        
$result['reason'][$name] = wpcf7_get_message'upload_failed' );
        
$result['idref'][$name] = $id $id null;
        return 
$result;
    }

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

    if ( 
$submission WPCF7_Submission::get_instance() ) {
        
$submission->add_uploaded_file$name$new_file );
    }

    return 
$result;
}


/* Messages */

add_filter'wpcf7_messages''wpcf7_file_messages' );

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' => __'Failed to upload file.''contact-form-7' )
        ),

        
'upload_file_type_invalid' => array(
            
'description' => __"Uploaded file is not allowed file type"'contact-form-7' ),
            
'default' => __'This file type is not allowed.''contact-form-7' )
        ),

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

        
'upload_failed_php_error' => array(
            
'description' => __"Uploading a file fails for PHP error"'contact-form-7' ),
            
'default' => __'Failed to upload file. Error occurred.''contact-form-7' )
        )
    ) );
}


/* Tag generator */

add_action'admin_init''wpcf7_add_tag_generator_file'50 );

function 
wpcf7_add_tag_generator_file() {
    if ( ! 
function_exists'wpcf7_add_tag_generator' ) )
        return;

    
wpcf7_add_tag_generator'file'__'File upload''contact-form-7' ),
        
'wpcf7-tg-pane-file''wpcf7_tg_pane_file' );
}

function 
wpcf7_tg_pane_file$contact_form ) {
?>
<div id="wpcf7-tg-pane-file" class="hidden">
<form action="">
<table>
<tr><td><input type="checkbox" name="required" />&nbsp;<?php echo esc_html__'Required field?''contact-form-7' ) ); ?></td></tr>
<tr><td><?php echo esc_html__'Name''contact-form-7' ) ); ?><br /><input type="text" name="name" class="tg-name oneline" /></td><td></td></tr>
</table>

<table>
<tr>
<td><code>id</code> (<?php echo esc_html__'optional''contact-form-7' ) ); ?>)<br />
<input type="text" name="id" class="idvalue oneline option" /></td>

<td><code>class</code> (<?php echo esc_html__'optional''contact-form-7' ) ); ?>)<br />
<input type="text" name="class" class="classvalue oneline option" /></td>
</tr>

<tr>
<td><?php echo esc_html__"File size limit"'contact-form-7' ) ); ?> (<?php echo esc_html__'bytes''contact-form-7' ) ); ?>) (<?php echo esc_html__'optional''contact-form-7' ) ); ?>)<br />
<input type="text" name="limit" class="filesize oneline option" /></td>

<td><?php echo esc_html__"Acceptable file types"'contact-form-7' ) ); ?> (<?php echo esc_html__'optional''contact-form-7' ) ); ?>)<br />
<input type="text" name="filetypes" class="filetype oneline option" /></td>
</tr>
</table>

<div class="tg-tag"><?php echo esc_html__"Copy this code and paste it into the form left."'contact-form-7' ) ); ?><br /><input type="text" name="file" class="tag wp-ui-text-highlight code" readonly="readonly" onfocus="this.select()" /></div>

<div class="tg-mail-tag"><?php echo esc_html__"And, put this code into the File Attachments field below."'contact-form-7' ) ); ?><br /><input type="text" class="mail-tag wp-ui-text-highlight code" readonly="readonly" onfocus="this.select()" /></div>
</form>
</div>
<?php
}


/* Warning message */

add_action'wpcf7_admin_notices''wpcf7_file_display_warning_message' );

function 
wpcf7_file_display_warning_message() {
    if ( ! 
$contact_form wpcf7_get_current_contact_form() ) {
        return;
    }

    
$has_tags = (bool) $contact_form->form_scan_shortcode(
        array( 
'type' => array( 'file''file*' ) ) );

    if ( ! 
$has_tags )
        return;

    
$uploads_dir wpcf7_upload_tmp_dir();
    
wpcf7_init_uploads();

    if ( ! 
is_dir$uploads_dir ) || ! wp_is_writable$uploads_dir ) ) {
        
$message sprintf__'This contact form contains 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 
'<div class="error"><p><strong>' esc_html$message ) . '</strong></p></div>';
    }
}


/* File uploading functions */

function wpcf7_init_uploads() {
    
$dir wpcf7_upload_tmp_dir();
    
wp_mkdir_p$dir );

    
$htaccess_file trailingslashit$dir ) . '.htaccess';

    if ( 
file_exists$htaccess_file ) ) {
        return;
    }

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

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;
}

function 
wpcf7_upload_tmp_dir() {
    if ( 
defined'WPCF7_UPLOADS_TMP_DIR' ) )
        return 
WPCF7_UPLOADS_TMP_DIR;
    else
        return 
wpcf7_upload_dir'dir' ) . '/wpcf7_uploads';
}

add_action'template_redirect''wpcf7_cleanup_upload_files'20 );

function 
wpcf7_cleanup_upload_files() {
    if ( 
is_admin() || 'GET' != $_SERVER['REQUEST_METHOD']
    || 
is_robots() || is_feed() || is_trackback() ) {
        return;
    }

    
$dir trailingslashitwpcf7_upload_tmp_dir() );

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

    if ( 
$handle = @opendir$dir ) ) {
        while ( 
false !== ( $file readdir$handle ) ) ) {
            if ( 
$file == "." || $file == ".." || $file == ".htaccess" ) {
                continue;
            }

            
$mtime = @filemtime$dir $file );

            if ( 
$mtime && time() < $mtime 60 ) { // less than 60 secs old
                
continue;
            }

            
wpcf7_rmdir_ppath_join$dir$file ) );
        }

        
closedir$handle );
    }
}

?>