wpcf7_file_validation_filter

The Contact Form 7 wpcf7 file validation filter function.

Description

wpcf7_file_validation_filter( $result, $tag ); 

Parameters (2)

0. $result
The result.
1. $tag
The tag.

Usage

  1. if ( !function_exists( 'wpcf7_file_validation_filter' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'contact-form-7/modules/file.php'; 
  3.  
  4. // The result. 
  5. $result = null; 
  6.  
  7. // The tag. 
  8. $tag = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wpcf7_file_validation_filter($result, $tag); 
  12.  

Defined (1)

The function is defined in the following location(s).

/modules/file.php  
  1. function wpcf7_file_validation_filter( $result, $tag ) { 
  2. $tag = new WPCF7_FormTag( $tag ); 
  3.  
  4. $name = $tag->name; 
  5. $id = $tag->get_id_option(); 
  6.  
  7. $file = isset( $_FILES[$name] ) ? $_FILES[$name] : null; 
  8.  
  9. if ( $file[error] && UPLOAD_ERR_NO_FILE != $file[error] ) { 
  10. $result->invalidate( $tag, wpcf7_get_message( 'upload_failed_php_error' ) ); 
  11. return $result; 
  12.  
  13. if ( empty( $file['tmp_name'] ) && $tag->is_required() ) { 
  14. $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) ); 
  15. return $result; 
  16.  
  17. if ( ! is_uploaded_file( $file['tmp_name'] ) ) { 
  18. return $result; 
  19.  
  20. $allowed_file_types = array(); 
  21.  
  22. if ( $file_types_a = $tag->get_option( 'filetypes' ) ) { 
  23. foreach ( $file_types_a as $file_types ) { 
  24. $file_types = explode( '|', $file_types ); 
  25.  
  26. foreach ( $file_types as $file_type ) { 
  27. $file_type = trim( $file_type, '.' ); 
  28. $file_type = str_replace( array( '.', '+', '*', '?' ),  
  29. array( '\.', '\+', '\*', '\?' ), $file_type ); 
  30. $allowed_file_types[] = $file_type; 
  31.  
  32. $allowed_file_types = array_unique( $allowed_file_types ); 
  33. $file_type_pattern = implode( '|', $allowed_file_types ); 
  34.  
  35. $allowed_size = 1048576; // default size 1 MB 
  36.  
  37. if ( $file_size_a = $tag->get_option( 'limit' ) ) { 
  38. $limit_pattern = '/^([1-9][0-9]*)([kKmM]?[bB])?$/'; 
  39.  
  40. foreach ( $file_size_a as $file_size ) { 
  41. if ( preg_match( $limit_pattern, $file_size, $matches ) ) { 
  42. $allowed_size = (int) $matches[1]; 
  43.  
  44. if ( ! empty( $matches[2] ) ) { 
  45. $kbmb = strtolower( $matches[2] ); 
  46.  
  47. if ( 'kb' == $kbmb ) { 
  48. $allowed_size *= 1024; 
  49. } elseif ( 'mb' == $kbmb ) { 
  50. $allowed_size *= 1024 * 1024; 
  51.  
  52. break; 
  53.  
  54. /** File type validation */ 
  55.  
  56. // Default file-type restriction 
  57. if ( '' == $file_type_pattern ) { 
  58. $file_type_pattern = 'jpg|jpeg|png|gif|pdf|doc|docx|ppt|pptx|odt|avi|ogg|m4a|mov|mp3|mp4|mpg|wav|wmv'; 
  59.  
  60. $file_type_pattern = trim( $file_type_pattern, '|' ); 
  61. $file_type_pattern = '(' . $file_type_pattern . ')'; 
  62. $file_type_pattern = '/\.' . $file_type_pattern . '$/i'; 
  63.  
  64. if ( ! preg_match( $file_type_pattern, $file['name'] ) ) { 
  65. $result->invalidate( $tag, wpcf7_get_message( 'upload_file_type_invalid' ) ); 
  66. return $result; 
  67.  
  68. /** File size validation */ 
  69.  
  70. if ( $file['size'] > $allowed_size ) { 
  71. $result->invalidate( $tag, wpcf7_get_message( 'upload_file_too_large' ) ); 
  72. return $result; 
  73.  
  74. wpcf7_init_uploads(); // Confirm upload dir 
  75. $uploads_dir = wpcf7_upload_tmp_dir(); 
  76. $uploads_dir = wpcf7_maybe_add_random_dir( $uploads_dir ); 
  77.  
  78. $filename = $file['name']; 
  79. $filename = wpcf7_canonicalize( $filename, 'as-is' ); 
  80. $filename = sanitize_file_name( $filename ); 
  81. $filename = wpcf7_antiscript_file_name( $filename ); 
  82. $filename = wp_unique_filename( $uploads_dir, $filename ); 
  83.  
  84. $new_file = trailingslashit( $uploads_dir ) . $filename; 
  85.  
  86. if ( false === @move_uploaded_file( $file['tmp_name'], $new_file ) ) { 
  87. $result->invalidate( $tag, wpcf7_get_message( 'upload_failed' ) ); 
  88. return $result; 
  89.  
  90. // Make sure the uploaded file is only readable for the owner process 
  91. @chmod( $new_file, 0400 ); 
  92.  
  93. if ( $submission = WPCF7_Submission::get_instance() ) { 
  94. $submission->add_uploaded_file( $name, $new_file ); 
  95.  
  96. return $result;