PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/blog/wp-content/plugins/contact-form-7/modules/file.php

https://gitlab.com/joelopezcuenca/proyectocoin
PHP | 393 lines | 271 code | 108 blank | 14 comment | 55 complexity | 0b14ba6ca79fd0a085d4046c885aed8f MD5 | raw file
  1. <?php
  2. /**
  3. ** A base module for [file] and [file*]
  4. **/
  5. /* Shortcode handler */
  6. add_action( 'wpcf7_init', 'wpcf7_add_shortcode_file' );
  7. function wpcf7_add_shortcode_file() {
  8. wpcf7_add_shortcode( array( 'file', 'file*' ),
  9. 'wpcf7_file_shortcode_handler', true );
  10. }
  11. function wpcf7_file_shortcode_handler( $tag ) {
  12. $tag = new WPCF7_Shortcode( $tag );
  13. if ( empty( $tag->name ) )
  14. return '';
  15. $validation_error = wpcf7_get_validation_error( $tag->name );
  16. $class = wpcf7_form_controls_class( $tag->type );
  17. if ( $validation_error )
  18. $class .= ' wpcf7-not-valid';
  19. $atts = array();
  20. $atts['size'] = $tag->get_size_option( '40' );
  21. $atts['class'] = $tag->get_class_option( $class );
  22. $atts['id'] = $tag->get_id_option();
  23. $atts['tabindex'] = $tag->get_option( 'tabindex', 'int', true );
  24. if ( $tag->is_required() )
  25. $atts['aria-required'] = 'true';
  26. $atts['aria-invalid'] = $validation_error ? 'true' : 'false';
  27. $atts['type'] = 'file';
  28. $atts['name'] = $tag->name;
  29. $atts['value'] = '1';
  30. $atts = wpcf7_format_atts( $atts );
  31. $html = sprintf(
  32. '<span class="wpcf7-form-control-wrap %1$s"><input %2$s />%3$s</span>',
  33. sanitize_html_class( $tag->name ), $atts, $validation_error );
  34. return $html;
  35. }
  36. /* Encode type filter */
  37. add_filter( 'wpcf7_form_enctype', 'wpcf7_file_form_enctype_filter' );
  38. function wpcf7_file_form_enctype_filter( $enctype ) {
  39. $multipart = (bool) wpcf7_scan_shortcode( array( 'type' => array( 'file', 'file*' ) ) );
  40. if ( $multipart ) {
  41. $enctype = 'multipart/form-data';
  42. }
  43. return $enctype;
  44. }
  45. /* Validation + upload handling filter */
  46. add_filter( 'wpcf7_validate_file', 'wpcf7_file_validation_filter', 10, 2 );
  47. add_filter( 'wpcf7_validate_file*', 'wpcf7_file_validation_filter', 10, 2 );
  48. function wpcf7_file_validation_filter( $result, $tag ) {
  49. $tag = new WPCF7_Shortcode( $tag );
  50. $name = $tag->name;
  51. $id = $tag->get_id_option();
  52. $file = isset( $_FILES[$name] ) ? $_FILES[$name] : null;
  53. if ( $file['error'] && UPLOAD_ERR_NO_FILE != $file['error'] ) {
  54. $result->invalidate( $tag, wpcf7_get_message( 'upload_failed_php_error' ) );
  55. return $result;
  56. }
  57. if ( empty( $file['tmp_name'] ) && $tag->is_required() ) {
  58. $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) );
  59. return $result;
  60. }
  61. if ( ! is_uploaded_file( $file['tmp_name'] ) )
  62. return $result;
  63. $allowed_file_types = array();
  64. if ( $file_types_a = $tag->get_option( 'filetypes' ) ) {
  65. foreach ( $file_types_a as $file_types ) {
  66. $file_types = explode( '|', $file_types );
  67. foreach ( $file_types as $file_type ) {
  68. $file_type = trim( $file_type, '.' );
  69. $file_type = str_replace( array( '.', '+', '*', '?' ),
  70. array( '\.', '\+', '\*', '\?' ), $file_type );
  71. $allowed_file_types[] = $file_type;
  72. }
  73. }
  74. }
  75. $allowed_file_types = array_unique( $allowed_file_types );
  76. $file_type_pattern = implode( '|', $allowed_file_types );
  77. $allowed_size = 1048576; // default size 1 MB
  78. if ( $file_size_a = $tag->get_option( 'limit' ) ) {
  79. $limit_pattern = '/^([1-9][0-9]*)([kKmM]?[bB])?$/';
  80. foreach ( $file_size_a as $file_size ) {
  81. if ( preg_match( $limit_pattern, $file_size, $matches ) ) {
  82. $allowed_size = (int) $matches[1];
  83. if ( ! empty( $matches[2] ) ) {
  84. $kbmb = strtolower( $matches[2] );
  85. if ( 'kb' == $kbmb )
  86. $allowed_size *= 1024;
  87. elseif ( 'mb' == $kbmb )
  88. $allowed_size *= 1024 * 1024;
  89. }
  90. break;
  91. }
  92. }
  93. }
  94. /* File type validation */
  95. // Default file-type restriction
  96. if ( '' == $file_type_pattern )
  97. $file_type_pattern = 'jpg|jpeg|png|gif|pdf|doc|docx|ppt|pptx|odt|avi|ogg|m4a|mov|mp3|mp4|mpg|wav|wmv';
  98. $file_type_pattern = trim( $file_type_pattern, '|' );
  99. $file_type_pattern = '(' . $file_type_pattern . ')';
  100. $file_type_pattern = '/\.' . $file_type_pattern . '$/i';
  101. if ( ! preg_match( $file_type_pattern, $file['name'] ) ) {
  102. $result->invalidate( $tag, wpcf7_get_message( 'upload_file_type_invalid' ) );
  103. return $result;
  104. }
  105. /* File size validation */
  106. if ( $file['size'] > $allowed_size ) {
  107. $result->invalidate( $tag, wpcf7_get_message( 'upload_file_too_large' ) );
  108. return $result;
  109. }
  110. wpcf7_init_uploads(); // Confirm upload dir
  111. $uploads_dir = wpcf7_upload_tmp_dir();
  112. $uploads_dir = wpcf7_maybe_add_random_dir( $uploads_dir );
  113. $filename = $file['name'];
  114. $filename = wpcf7_canonicalize( $filename );
  115. $filename = sanitize_file_name( $filename );
  116. $filename = wpcf7_antiscript_file_name( $filename );
  117. $filename = wp_unique_filename( $uploads_dir, $filename );
  118. $new_file = trailingslashit( $uploads_dir ) . $filename;
  119. if ( false === @move_uploaded_file( $file['tmp_name'], $new_file ) ) {
  120. $result->invalidate( $tag, wpcf7_get_message( 'upload_failed' ) );
  121. return $result;
  122. }
  123. // Make sure the uploaded file is only readable for the owner process
  124. @chmod( $new_file, 0400 );
  125. if ( $submission = WPCF7_Submission::get_instance() ) {
  126. $submission->add_uploaded_file( $name, $new_file );
  127. }
  128. return $result;
  129. }
  130. /* Messages */
  131. add_filter( 'wpcf7_messages', 'wpcf7_file_messages' );
  132. function wpcf7_file_messages( $messages ) {
  133. return array_merge( $messages, array(
  134. 'upload_failed' => array(
  135. 'description' => __( "Uploading a file fails for any reason", 'contact-form-7' ),
  136. 'default' => __( 'Failed to upload file.', 'contact-form-7' )
  137. ),
  138. 'upload_file_type_invalid' => array(
  139. 'description' => __( "Uploaded file is not allowed file type", 'contact-form-7' ),
  140. 'default' => __( 'This file type is not allowed.', 'contact-form-7' )
  141. ),
  142. 'upload_file_too_large' => array(
  143. 'description' => __( "Uploaded file is too large", 'contact-form-7' ),
  144. 'default' => __( 'This file is too large.', 'contact-form-7' )
  145. ),
  146. 'upload_failed_php_error' => array(
  147. 'description' => __( "Uploading a file fails for PHP error", 'contact-form-7' ),
  148. 'default' => __( 'Failed to upload file. Error occurred.', 'contact-form-7' )
  149. )
  150. ) );
  151. }
  152. /* Tag generator */
  153. if ( is_admin() ) {
  154. add_action( 'admin_init', 'wpcf7_add_tag_generator_file', 50 );
  155. }
  156. function wpcf7_add_tag_generator_file() {
  157. $tag_generator = WPCF7_TagGenerator::get_instance();
  158. $tag_generator->add( 'file', __( 'file', 'contact-form-7' ),
  159. 'wpcf7_tag_generator_file' );
  160. }
  161. function wpcf7_tag_generator_file( $contact_form, $args = '' ) {
  162. $args = wp_parse_args( $args, array() );
  163. $type = 'file';
  164. $description = __( "Generate a form-tag for a file uploading field. For more details, see %s.", 'contact-form-7' );
  165. $desc_link = wpcf7_link( __( 'http://contactform7.com/file-uploading-and-attachment/', 'contact-form-7' ), __( 'File Uploading and Attachment', 'contact-form-7' ) );
  166. ?>
  167. <div class="control-box">
  168. <fieldset>
  169. <legend><?php echo sprintf( esc_html( $description ), $desc_link ); ?></legend>
  170. <table class="form-table">
  171. <tbody>
  172. <tr>
  173. <th scope="row"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></th>
  174. <td>
  175. <fieldset>
  176. <legend class="screen-reader-text"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></legend>
  177. <label><input type="checkbox" name="required" /> <?php echo esc_html( __( 'Required field', 'contact-form-7' ) ); ?></label>
  178. </fieldset>
  179. </td>
  180. </tr>
  181. <tr>
  182. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-name' ); ?>"><?php echo esc_html( __( 'Name', 'contact-form-7' ) ); ?></label></th>
  183. <td><input type="text" name="name" class="tg-name oneline" id="<?php echo esc_attr( $args['content'] . '-name' ); ?>" /></td>
  184. </tr>
  185. <tr>
  186. <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>
  187. <td><input type="text" name="limit" class="filesize oneline option" id="<?php echo esc_attr( $args['content'] . '-limit' ); ?>" /></td>
  188. </tr>
  189. <tr>
  190. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-filetypes' ); ?>"><?php echo esc_html( __( 'Acceptable file types', 'contact-form-7' ) ); ?></label></th>
  191. <td><input type="text" name="filetypes" class="filetype oneline option" id="<?php echo esc_attr( $args['content'] . '-filetypes' ); ?>" /></td>
  192. </tr>
  193. <tr>
  194. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-id' ); ?>"><?php echo esc_html( __( 'Id attribute', 'contact-form-7' ) ); ?></label></th>
  195. <td><input type="text" name="id" class="idvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-id' ); ?>" /></td>
  196. </tr>
  197. <tr>
  198. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-class' ); ?>"><?php echo esc_html( __( 'Class attribute', 'contact-form-7' ) ); ?></label></th>
  199. <td><input type="text" name="class" class="classvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-class' ); ?>" /></td>
  200. </tr>
  201. </tbody>
  202. </table>
  203. </fieldset>
  204. </div>
  205. <div class="insert-box">
  206. <input type="text" name="<?php echo $type; ?>" class="tag code" readonly="readonly" onfocus="this.select()" />
  207. <div class="submitbox">
  208. <input type="button" class="button button-primary insert-tag" value="<?php echo esc_attr( __( 'Insert Tag', 'contact-form-7' ) ); ?>" />
  209. </div>
  210. <br class="clear" />
  211. <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>
  212. </div>
  213. <?php
  214. }
  215. /* Warning message */
  216. add_action( 'wpcf7_admin_notices', 'wpcf7_file_display_warning_message' );
  217. function wpcf7_file_display_warning_message() {
  218. if ( ! $contact_form = wpcf7_get_current_contact_form() ) {
  219. return;
  220. }
  221. $has_tags = (bool) $contact_form->form_scan_shortcode(
  222. array( 'type' => array( 'file', 'file*' ) ) );
  223. if ( ! $has_tags )
  224. return;
  225. $uploads_dir = wpcf7_upload_tmp_dir();
  226. wpcf7_init_uploads();
  227. if ( ! is_dir( $uploads_dir ) || ! wp_is_writable( $uploads_dir ) ) {
  228. $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 );
  229. echo '<div class="error"><p>' . esc_html( $message ) . '</p></div>';
  230. }
  231. }
  232. /* File uploading functions */
  233. function wpcf7_init_uploads() {
  234. $dir = wpcf7_upload_tmp_dir();
  235. wp_mkdir_p( $dir );
  236. $htaccess_file = trailingslashit( $dir ) . '.htaccess';
  237. if ( file_exists( $htaccess_file ) ) {
  238. return;
  239. }
  240. if ( $handle = @fopen( $htaccess_file, 'w' ) ) {
  241. fwrite( $handle, "Deny from all\n" );
  242. fclose( $handle );
  243. }
  244. }
  245. function wpcf7_maybe_add_random_dir( $dir ) {
  246. do {
  247. $rand_max = mt_getrandmax();
  248. $rand = zeroise( mt_rand( 0, $rand_max ), strlen( $rand_max ) );
  249. $dir_new = path_join( $dir, $rand );
  250. } while ( file_exists( $dir_new ) );
  251. if ( wp_mkdir_p( $dir_new ) ) {
  252. return $dir_new;
  253. }
  254. return $dir;
  255. }
  256. function wpcf7_upload_tmp_dir() {
  257. if ( defined( 'WPCF7_UPLOADS_TMP_DIR' ) )
  258. return WPCF7_UPLOADS_TMP_DIR;
  259. else
  260. return wpcf7_upload_dir( 'dir' ) . '/wpcf7_uploads';
  261. }
  262. add_action( 'template_redirect', 'wpcf7_cleanup_upload_files', 20 );
  263. function wpcf7_cleanup_upload_files() {
  264. if ( is_admin() || 'GET' != $_SERVER['REQUEST_METHOD']
  265. || is_robots() || is_feed() || is_trackback() ) {
  266. return;
  267. }
  268. $dir = trailingslashit( wpcf7_upload_tmp_dir() );
  269. if ( ! is_dir( $dir ) || ! is_readable( $dir ) || ! wp_is_writable( $dir ) ) {
  270. return;
  271. }
  272. if ( $handle = @opendir( $dir ) ) {
  273. while ( false !== ( $file = readdir( $handle ) ) ) {
  274. if ( $file == "." || $file == ".." || $file == ".htaccess" ) {
  275. continue;
  276. }
  277. $mtime = @filemtime( $dir . $file );
  278. if ( $mtime && time() < $mtime + 60 ) { // less than 60 secs old
  279. continue;
  280. }
  281. wpcf7_rmdir_p( path_join( $dir, $file ) );
  282. }
  283. closedir( $handle );
  284. }
  285. }