Guest User

Fix deprecation warnings in Multifile Upload Field plugin

a guest
Jul 4th, 2017
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 15.34 KB | None | 0 0
  1. <?php
  2. /**
  3. * Plugin Name: Multifile Upload Field for Contact Form 7
  4. * Plugin URI: https://wordpress.org/plugins/multifile-upload-field-for-contact-form-7/
  5. * Description: Adds multiple file field for contact form 7
  6. * Version: 1.0.1
  7. * Author: Spyros Vlachopoulos
  8. * Contributors: KacperSzarek
  9. * Author URI: https://codeable.io/developers/spyros-vlachopoulos/
  10. * License: GPL2
  11. */
  12.  
  13. /**
  14. ** A base module for [multifile] and [multifile*]
  15. **/
  16.  
  17. /* Shortcode handler */
  18.  
  19. add_action( 'wpcf7_init', 'wpcf7_add_shortcode_multifile' );
  20.  
  21. function wpcf7_add_shortcode_multifile() {
  22.     wpcf7_add_form_tag( array( 'multifile', 'multifile*' ),
  23.         'wpcf7_multifile_shortcode_handler', true );
  24. }
  25.  
  26. function wpcf7_multifile_shortcode_handler( $tag ) {
  27.     $tag = new WPCF7_FormTag( $tag );
  28.  
  29.     if ( empty( $tag->name ) ) {
  30.         return '';
  31.     }
  32.  
  33.     $validation_error = wpcf7_get_validation_error( $tag->name );
  34.  
  35.     $class = wpcf7_form_controls_class( $tag->type );
  36.  
  37.     if ( $validation_error ) {
  38.         $class .= ' wpcf7-not-valid';
  39.     }
  40.  
  41.     $atts = array();
  42.  
  43.     $atts['size'] = $tag->get_size_option( '40' );
  44.     $atts['class'] = $tag->get_class_option( $class );
  45.     $atts['id'] = $tag->get_id_option();
  46.     $atts['tabindex'] = $tag->get_option( 'tabindex', 'int', true );
  47.     $atts['accept'] = $tag->get_option( 'accept', null, true);
  48.     $atts['multiple'] = 'multiple';
  49.  
  50.   $accept_wildcard = '';
  51.   $accept_wildcard = $tag->get_option( 'accept_wildcard');
  52.  
  53.     if ( !empty($accept_wildcard)) {
  54.     $atts['accept'] = $atts['accept'] .'/*';
  55.   }
  56.     if ( $tag->is_required() ) {
  57.         $atts['aria-required'] = 'true';
  58.     }
  59.  
  60.     $atts['aria-invalid'] = $validation_error ? 'true' : 'false';
  61.  
  62.     $atts['type'] = 'file';
  63.     $atts['name'] = $tag->name.'[]';
  64.  
  65.   $atts = apply_filters('cf7_multifile_atts', $atts);
  66.  
  67.     $atts = wpcf7_format_atts( $atts );
  68.  
  69.     $html = sprintf(
  70.         apply_filters('cf7_multifile_input', '<span class="wpcf7-form-control-wrap %1$s"><input %2$s />%3$s</span>', $atts),
  71.         sanitize_html_class( $tag->name ), $atts, $validation_error );
  72.  
  73.     return $html;
  74. }
  75.  
  76.  
  77. /* Encode type filter */
  78.  
  79. add_filter( 'wpcf7_form_enctype', 'wpcf7_multifile_form_enctype_filter' );
  80.  
  81. function wpcf7_multifile_form_enctype_filter( $enctype ) {
  82.     $multipart = (bool) wpcf7_scan_form_tags( array( 'type' => array( 'multifile', 'multifile*' ) ) );
  83.  
  84.     if ( $multipart ) {
  85.         $enctype = 'multipart/form-data';
  86.     }
  87.  
  88.     return $enctype;
  89. }
  90.  
  91.  
  92. /* Validation + upload handling filter */
  93.  
  94. add_filter( 'wpcf7_validate_multifile', 'wpcf7_multifile_validation_filter', 10, 2 );
  95. add_filter( 'wpcf7_validate_multifile*', 'wpcf7_multifile_validation_filter', 10, 2 );
  96.  
  97. function wpcf7_multifile_validation_filter( $result, $tag ) {
  98.     $tag = new WPCF7_FormTag( $tag );
  99.  
  100.     $name = $tag->name;
  101.     $id = $tag->get_id_option();
  102.   $uniqid = uniqid();
  103.  
  104.     $original_files_array = isset( $_FILES[$name] ) ? $_FILES[$name] : null;
  105.  
  106.   if ($original_files_array === null) {
  107.     return $result;
  108.   }
  109.  
  110.   $total = count($_FILES[$name]['name']);
  111.  
  112.   $files = array();
  113.   $new_files = array();
  114.  
  115.   for ($i=0; $i<$total; $i++) {
  116.     $files[] = array(
  117.       'name'      => $original_files_array['name'][$i],
  118.       'type'      => $original_files_array['type'][$i],
  119.       'tmp_name'  => $original_files_array['tmp_name'][$i],
  120.       'error'     => $original_files_array['error'][$i],
  121.       'size'      => $original_files_array['size'][$i]
  122.     );
  123.   }
  124.  
  125.   // file loop start
  126.   foreach ($files as $file) {
  127.    
  128.    
  129.     if ( $file['error'] && UPLOAD_ERR_NO_FILE != $file['error'] ) {
  130.       $result->invalidate( $tag, wpcf7_get_message( 'upload_failed_php_error' ) );
  131.       multifile_remove($new_files);
  132.       return $result;
  133.     }
  134.  
  135.     if ( empty( $file['tmp_name'] ) && $tag->is_required() ) {
  136.       $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) );
  137.       return $result;
  138.     }
  139.  
  140.     if ( ! is_uploaded_file( $file['tmp_name'] ) )
  141.       return $result;
  142.  
  143.     $allowed_file_types = array();
  144.  
  145.     if ( $file_types_a = $tag->get_option( 'filetypes' ) ) {
  146.       foreach ( $file_types_a as $file_types ) {
  147.         $file_types = explode( '|', $file_types );
  148.  
  149.         foreach ( $file_types as $file_type ) {
  150.           $file_type = trim( $file_type, '.' );
  151.           $file_type = str_replace( array( '.', '+', '*', '?' ),
  152.             array( '\.', '\+', '\*', '\?' ), $file_type );
  153.           $allowed_file_types[] = $file_type;
  154.         }
  155.       }
  156.     }
  157.  
  158.     $allowed_file_types = array_unique( $allowed_file_types );
  159.     $file_type_pattern = implode( '|', $allowed_file_types );
  160.  
  161.     $allowed_size = apply_filters('cf7_multifile_max_size', 10048576); // default size 1 MB
  162.  
  163.     if ( $file_size_a = $tag->get_option( 'limit' ) ) {
  164.       $limit_pattern = '/^([1-9][0-9]*)([kKmM]?[bB])?$/';
  165.  
  166.       foreach ( $file_size_a as $file_size ) {
  167.         if ( preg_match( $limit_pattern, $file_size, $matches ) ) {
  168.           $allowed_size = (int) $matches[1];
  169.  
  170.           if ( ! empty( $matches[2] ) ) {
  171.             $kbmb = strtolower( $matches[2] );
  172.  
  173.             if ( 'kb' == $kbmb )
  174.               $allowed_size *= 1024;
  175.             elseif ( 'mb' == $kbmb )
  176.               $allowed_size *= 1024 * 1024;
  177.           }
  178.  
  179.           break;
  180.         }
  181.       }
  182.     }
  183.  
  184.     /* File type validation */
  185.  
  186.     // Default file-type restriction
  187.     if ( '' == $file_type_pattern )
  188.       $file_type_pattern = 'jpg|jpeg|png|gif|pdf|doc|docx|ppt|pptx|odt|avi|ogg|m4a|mov|mp3|mp4|mpg|wav|wmv';
  189.  
  190.     $file_type_pattern = trim( $file_type_pattern, '|' );
  191.     $file_type_pattern = '(' . $file_type_pattern . ')';
  192.     $file_type_pattern = '/\.' . $file_type_pattern . '$/i';
  193.  
  194.     if ( ! preg_match( $file_type_pattern, $file['name'] ) ) {
  195.       $result->invalidate( $tag, wpcf7_get_message( 'upload_file_type_invalid' ) );
  196.       multifile_remove($new_files);
  197.       return $result;
  198.     }
  199.  
  200.     /* File size validation */
  201.  
  202.     if ( $file['size'] > $allowed_size ) {
  203.       $result->invalidate( $tag, wpcf7_get_message( 'upload_file_too_large' ) );
  204.       multifile_remove($new_files);
  205.       return $result;
  206.     }
  207.  
  208.     wpcf7_init_uploads(); // Confirm upload dir
  209.     $uploads_dir = wpcf7_upload_tmp_dir();
  210.     $uploads_dir = wpcf7_maybe_add_random_dir( $uploads_dir );
  211.  
  212.     $filename = $file['name'];
  213.     $filename = wpcf7_canonicalize( $filename );
  214.     $filename = sanitize_file_name( $filename );
  215.     $filename = wpcf7_antiscript_file_name( $filename );
  216.     $filename = wp_unique_filename( $uploads_dir, $filename );
  217.  
  218.     $new_file = trailingslashit( $uploads_dir ) . $filename;
  219.  
  220.     if ( false === @move_uploaded_file( $file['tmp_name'], $new_file ) ) {
  221.       $result->invalidate( $tag, wpcf7_get_message( 'upload_failed' ) );
  222.       multifile_remove($new_files);
  223.       return $result;
  224.     }
  225.    
  226.     $new_files[] = $new_file;
  227.  
  228.     // Make sure the uploaded file is only readable for the owner process
  229.     @chmod( $new_file, 0400 );
  230.  
  231.  
  232.  
  233.   }
  234.  
  235.   // file loop end
  236.   $zipped_files = trailingslashit( $uploads_dir ).$uniqid.'.zip';
  237.   $zipping = multifile_create_zip($new_files, $zipped_files);
  238.   @chmod( $zipped_files, 0400 );
  239.  
  240.   if ($zipping === false) {
  241.     $result->invalidate( $tag, wpcf7_get_message( 'zipping_failed' ) );
  242.     multifile_remove($new_files);
  243.     return $result;
  244.   }
  245.  
  246.   multifile_remove($new_files);
  247.  
  248.   if ( $submission = WPCF7_Submission::get_instance() ) {
  249.     $submission->add_uploaded_file( $name, $zipped_files );
  250.   }
  251.  
  252.     return $result;
  253. }
  254.  
  255.  
  256. /* Messages */
  257.  
  258. add_filter( 'wpcf7_messages', 'wpcf7_multifile_messages' );
  259.  
  260. function wpcf7_multifile_messages( $messages ) {
  261.     return array_merge( $messages, array(
  262.         'upload_failed' => array(
  263.             'description' => __( "Uploading a file fails for any reason", 'contact-form-7' ),
  264.             'default' => __( "There was an unknown error uploading the file.", 'contact-form-7' )
  265.         ),
  266.    
  267.         'zipping_failed' => array(
  268.             'description' => __( "Zipping files fails for any reason", 'contact-form-7' ),
  269.             'default' => __( "There was an unknown error zippng the files.", 'contact-form-7' )
  270.         ),
  271.  
  272.         'upload_file_type_invalid' => array(
  273.             'description' => __( "Uploaded file is not allowed for file type", 'contact-form-7' ),
  274.             'default' => __( "You are not allowed to upload files of this type.", 'contact-form-7' )
  275.         ),
  276.  
  277.         'upload_file_too_large' => array(
  278.             'description' => __( "Uploaded file is too large", 'contact-form-7' ),
  279.             'default' => __( "The file is too big.", 'contact-form-7' )
  280.         ),
  281.  
  282.         'upload_failed_php_error' => array(
  283.             'description' => __( "Uploading a file fails for PHP error", 'contact-form-7' ),
  284.             'default' => __( "There was an error uploading the file.", 'contact-form-7' )
  285.         )
  286.     ) );
  287. }
  288.  
  289.  
  290. /* Tag generator */
  291.  
  292. add_action( 'wpcf7_admin_init', 'wpcf7_add_tag_generator_multifile', 50 );
  293.  
  294. function wpcf7_add_tag_generator_multifile() {
  295.     $tag_generator = WPCF7_TagGenerator::get_instance();
  296.     $tag_generator->add( 'multifile', __( 'multifile', 'contact-form-7' ),
  297.         'wpcf7_tag_generator_multifile' );
  298. }
  299.  
  300. function wpcf7_tag_generator_multifile( $contact_form, $args = '' ) {
  301.     $args = wp_parse_args( $args, array() );
  302.     $type = 'multifile';
  303.  
  304.     $description = __( "Generate a form-tag for a file uploading field. For more details, see %s.", 'contact-form-7' );
  305.  
  306.     $desc_link = wpcf7_link( __( 'http://contactform7.com/file-uploading-and-attachment/', 'contact-form-7' ), __( 'File Uploading and Attachment', 'contact-form-7' ) );
  307.  
  308. ?>
  309. <div class="control-box">
  310. <fieldset>
  311. <legend><?php echo sprintf( esc_html( $description ), $desc_link ); ?></legend>
  312.  
  313. <table class="form-table">
  314. <tbody>
  315.     <tr>
  316.     <th scope="row"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></th>
  317.     <td>
  318.         <fieldset>
  319.         <legend class="screen-reader-text"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></legend>
  320.         <label><input type="checkbox" name="required" /> <?php echo esc_html( __( 'Required field', 'contact-form-7' ) ); ?></label>
  321.         </fieldset>
  322.     </td>
  323.     </tr>
  324.  
  325.     <tr>
  326.     <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-name' ); ?>"><?php echo esc_html( __( 'Name', 'contact-form-7' ) ); ?></label></th>
  327.     <td><input type="text" name="name" class="tg-name oneline" id="<?php echo esc_attr( $args['content'] . '-name' ); ?>" /></td>
  328.     </tr>
  329.  
  330.     <tr>
  331.     <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-limit' ); ?>"><?php echo esc_html( __( "File size limit (bytes)", 'contact-form-7' ) ); ?></label></th>
  332.     <td><input type="text" name="limit" class="filesize oneline option" id="<?php echo esc_attr( $args['content'] . '-limit' ); ?>" /></td>
  333.     </tr>
  334.  
  335.     <tr>
  336.     <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-filetypes' ); ?>"><?php echo esc_html( __( 'Acceptable file types', 'contact-form-7' ) ); ?></label></th>
  337.     <td><input type="text" name="filetypes" class="filetype oneline option" id="<?php echo esc_attr( $args['content'] . '-filetypes' ); ?>" /></td>
  338.     </tr>
  339.  
  340.  
  341.     <tr>
  342.     <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-accept' ); ?>"><?php echo esc_html( __( 'Accept input attribute', 'contact-form-7' ) ); ?></label></th>
  343.     <td><input type="text" name="accept" class="filetype oneline option" id="<?php echo esc_attr( $args['content'] . '-accept' ); ?>" /></td>
  344.     </tr>
  345.  
  346.   <tr>
  347.   <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-accept_wildcard' ); ?>"><?php echo esc_html( __( 'Add  accept wildcard /*', 'contact-form-7' ) ); ?></label></th>
  348.     <td>
  349.         <fieldset>
  350.     <input type="text" name="accept_wildcard" class="filetype oneline option" id="<?php echo esc_attr( $args['content'] . '-accept_wildcard' ); ?>" /><small><?php echo __('Type "yes" to add wildcard'); ?></small>
  351.         </fieldset>
  352.     </td>
  353.     </tr>
  354.  
  355.     <tr>
  356.     <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-id' ); ?>"><?php echo esc_html( __( 'Id attribute', 'contact-form-7' ) ); ?></label></th>
  357.     <td><input type="text" name="id" class="idvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-id' ); ?>" /></td>
  358.     </tr>
  359.  
  360.     <tr>
  361.     <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-class' ); ?>"><?php echo esc_html( __( 'Class attribute', 'contact-form-7' ) ); ?></label></th>
  362.     <td><input type="text" name="class" class="classvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-class' ); ?>" /></td>
  363.     </tr>
  364.  
  365. </tbody>
  366. </table>
  367. </fieldset>
  368. </div>
  369.  
  370. <div class="insert-box">
  371.     <input type="text" name="<?php echo $type; ?>" class="tag code" readonly="readonly" onfocus="this.select()" />
  372.  
  373.     <div class="submitbox">
  374.     <input type="button" class="button button-primary insert-tag" value="<?php echo esc_attr( __( 'Insert Tag', 'contact-form-7' ) ); ?>" />
  375.     </div>
  376.  
  377.     <br class="clear" />
  378.  
  379.     <p class="description mail-tag"><label for="<?php echo esc_attr( $args['content'] . '-mailtag' ); ?>"><?php echo sprintf( esc_html( __( "To attach the file uploaded through this field to mail, you need to insert the corresponding mail-tag (%s) into the File Attachments field on the Mail tab.", 'contact-form-7' ) ), '<strong><span class="mail-tag"></span></strong>' ); ?><input type="text" class="mail-tag code hidden" readonly="readonly" id="<?php echo esc_attr( $args['content'] . '-mailtag' ); ?>" /></label></p>
  380. </div>
  381. <?php
  382. }
  383.  
  384.  
  385. /* Warning message */
  386.  
  387. add_action( 'wpcf7_admin_notices', 'wpcf7_multifile_display_warning_message' );
  388.  
  389. function wpcf7_multifile_display_warning_message() {
  390.     if ( ! $contact_form = wpcf7_get_current_contact_form() ) {
  391.         return;
  392.     }
  393.  
  394.     $has_tags = (bool) $contact_form->form_scan_shortcode(
  395.         array( 'type' => array( 'multifile', 'multifile*' ) ) );
  396.  
  397.     if ( ! $has_tags ) {
  398.         return;
  399.     }
  400.  
  401.     $uploads_dir = wpcf7_upload_tmp_dir();
  402.     wpcf7_init_uploads();
  403.  
  404.     if ( ! is_dir( $uploads_dir ) || ! wp_is_writable( $uploads_dir ) ) {
  405.         $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 );
  406.  
  407.         echo '<div class="notice notice-error is-dismissible"><p>' . esc_html( $message ) . '</p></div>';
  408.     }
  409. }
  410.  
  411.  
  412.  
  413. /* creates a compressed zip file */
  414. function multifile_create_zip($files = array(),$destination = '',$overwrite = false) {
  415.     //if the zip file already exists and overwrite is false, return false
  416.     if(file_exists($destination) && !$overwrite) { return false; }
  417.     //vars
  418.     $valid_files = array();
  419.     //if files were passed in...
  420.     if(is_array($files)) {
  421.         //cycle through each file
  422.         foreach($files as $file) {
  423.             //make sure the file exists
  424.             if(file_exists($file)) {
  425.                 $valid_files[] = $file;
  426.             }
  427.         }
  428.     }
  429.     //if we have good files...
  430.     if(count($valid_files)) {
  431.         //create the archive
  432.         $zip = new ZipArchive();
  433.         if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
  434.             return false;
  435.         }
  436.         //add the files
  437.         foreach($valid_files as $file) {
  438.             $zip->addFile($file,basename($file));
  439.         }
  440.         //debug
  441.         //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
  442.        
  443.         //close the zip -- done!
  444.         $zip->close();
  445.        
  446.         //check to make sure the file exists
  447.         return file_exists($destination);
  448.     }
  449.     else
  450.     {
  451.         return false;
  452.     }
  453. }
  454.  
  455.  
  456. function multifile_remove($new_files) {
  457.   if (!empty($new_files)) {
  458.     foreach($new_files as $to_delete) {
  459.       @unlink( $to_delete );
  460.       @rmdir( dirname( $to_delete ) ); // remove parent dir if it's removable (empty).
  461.     }
  462.   }
  463. }
Advertisement
Add Comment
Please, Sign In to add comment