'image/jpeg', 'png' => 'image/png', 'gif' => 'image/gif', 'webp' => 'image/webp', ); } function velischat_support_max_file_size(){ // The smaller of our limit and the one of the server's PHP return min( VELISCHAT_SUPPORT_MAX_FILE_SIZE, wp_max_upload_size() ); } function velischat_support_max_total_size(){ // Above PHP's post_max_size the whole request is dropped, text included, and the form can't even say why: // stay below it, leaving room for the message $post_max = wp_convert_hr_to_bytes( ini_get( 'post_max_size' ) ); return $post_max > 0 ? min( VELISCHAT_SUPPORT_MAX_TOTAL_SIZE, $post_max - 256 * KB_IN_BYTES ) : VELISCHAT_SUPPORT_MAX_TOTAL_SIZE; } function velischat_support_draft_key(){ // What the user wrote, kept for a few minutes when the request can't be sent, so it isn't lost return 'velischat_support_draft_' . get_current_user_id(); } function velischat_support_fail( $status, $fields ){ // Back to the page with an error, keeping the text written so far set_transient( velischat_support_draft_key(), $fields, 15 * MINUTE_IN_SECONDS ); wp_safe_redirect( add_query_arg( 'velischat_status', $status, admin_url( 'admin.php?page=velischat-support' ) ) ); exit; } function velischat_support_collect_files( $dir ){ // Paths of the screenshots uploaded with the request, copied into $dir with their own names, or the status of the error $files = $_FILES[ 'velischat_support_files' ] ?? null; // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- checked by velischat_support_send(); each file is checked below if( ! is_array( $files ) || ! is_array( $files[ 'name' ] ?? null ) ){ return array(); } $uploads = array(); foreach( $files[ 'name' ] as $i => $name ){ $error = (int) $files[ 'error' ][ $i ]; if( UPLOAD_ERR_NO_FILE === $error ){ continue; } if( in_array( $error, array( UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE ), true ) ){ return 'file_too_big'; } if( UPLOAD_ERR_OK !== $error || ! is_uploaded_file( $files[ 'tmp_name' ][ $i ] ) ){ return 'upload_error'; } $uploads[] = array( 'name' => sanitize_file_name( wp_unslash( $name ) ), 'tmp' => $files[ 'tmp_name' ][ $i ], 'size' => (int) $files[ 'size' ][ $i ], ); } if( count( $uploads ) > VELISCHAT_SUPPORT_MAX_FILES ){ return 'too_many_files'; } $total = 0; foreach( $uploads as &$upload ){ $total += $upload[ 'size' ]; if( $upload[ 'size' ] > velischat_support_max_file_size() || $total > velischat_support_max_total_size() ){ return 'file_too_big'; } // The real content must be an image, whatever the name says $check = wp_check_filetype_and_ext( $upload[ 'tmp' ], $upload[ 'name' ], velischat_support_image_types() ); if( empty( $check[ 'type' ] ) ){ return 'bad_file_type'; } $upload[ 'type' ] = $check[ 'type' ]; } unset( $upload ); if( empty( $uploads ) ){ return array(); } if( ! wp_mkdir_p( $dir ) ){ return 'upload_error'; } if( ! function_exists( 'wp_handle_upload' ) ){ require_once ABSPATH . 'wp-admin/includes/file.php'; } // The screenshots go to the request's own folder, not to the media library. Same name twice: the second one gets // a number, so it doesn't overwrite the first $to_dir = function( $uploads ) use ( $dir ){ return array_merge( $uploads, array( 'path' => $dir, 'url' => '', 'subdir' => '', 'error' => false ) ); }; add_filter( 'upload_dir', $to_dir ); $paths = array(); foreach( $uploads as $upload ){ // Passed by reference: a variable, not the array itself $file = array( 'name' => $upload[ 'name' ] ?: 'screenshot.png', 'type' => $upload[ 'type' ], 'tmp_name' => $upload[ 'tmp' ], 'error' => UPLOAD_ERR_OK, 'size' => $upload[ 'size' ], ); $result = wp_handle_upload( $file, array( 'test_form' => false, 'mimes' => velischat_support_image_types() ) ); if( empty( $result[ 'file' ] ) ){ remove_filter( 'upload_dir', $to_dir ); return 'upload_error'; } $paths[] = $result[ 'file' ]; } remove_filter( 'upload_dir', $to_dir ); return $paths; } function velischat_support_remove_files( $dir ){ // Delete the copies of the screenshots once the email is gone velischat_delete_folder( $dir ); } function velischat_support_send(){ // "Invia richiesta" button: emails the request to VelisLab's support, with the site's address and the plugin version if( ! current_user_can( 'manage_options' ) ){ wp_die( esc_html__( 'You don\'t have permission to do this.', 'velischat' ) ); } check_admin_referer( 'velischat_support_send' ); $fields = array( 'name' => sanitize_text_field( wp_unslash( $_POST[ 'velischat_support_name' ] ?? '' ) ), 'subject' => sanitize_text_field( wp_unslash( $_POST[ 'velischat_support_subject' ] ?? '' ) ), 'message' => sanitize_textarea_field( wp_unslash( $_POST[ 'velischat_support_message' ] ?? '' ) ), ); if( '' === $fields[ 'name' ] || '' === $fields[ 'subject' ] || '' === $fields[ 'message' ] ){ velischat_support_fail( 'missing_fields', $fields ); } // A folder of its own, so the screenshots keep their names in the email $dir = get_temp_dir() . 'velischat-support-' . wp_generate_password( 12, false ); $attachments = velischat_support_collect_files( $dir ); if( is_string( $attachments ) ){ velischat_support_remove_files( $dir ); velischat_support_fail( $attachments, $fields ); } // Add-ons mark the requests of their customers in the subject, e.g. "[PRO] VelisChat 1.2.0 support request: ...", // and add their own details to the message $prefix = (string) apply_filters( 'velischat_support_subject_prefix', '' ); $subject = $prefix . sprintf( 'VelisChat %s support request: %s', velischat_get_plugin_version(), $fields[ 'subject' ] ); $user = wp_get_current_user(); $body = sprintf( "Site: %s\n", home_url() ) . (string) apply_filters( 'velischat_support_details', "Version: free\n" ) . sprintf( "Name: %s\n", $fields[ 'name' ] ) . sprintf( "Email: %s\n", $user->user_email ) . str_repeat( '=', 48 ) . "\n\n" . $fields[ 'message' ] . "\n"; // Replies go to the administrator who wrote the request. wp_mail splits this header at the commas: none in the name $reply_name = trim( str_replace( array( ',', ';', '"', '<', '>' ), ' ', $fields[ 'name' ] ) ); $headers = array( 'Reply-To: ' . $reply_name . ' <' . $user->user_email . '>' ); $sent = wp_mail( VELISCHAT_SUPPORT_EMAIL, $subject, $body, $headers, $attachments ); velischat_support_remove_files( $dir ); if( ! $sent ){ velischat_support_fail( 'send_error', $fields ); } delete_transient( velischat_support_draft_key() ); wp_safe_redirect( add_query_arg( 'velischat_status', 'sent', admin_url( 'admin.php?page=velischat-support' ) ) ); exit; } add_action( 'admin_post_velischat_support_send', 'velischat_support_send' ); function velischat_render_support_page(){ // Render the "Supporto" admin page if( ! current_user_can( 'manage_options' ) ){ return; } $status = isset( $_GET[ 'velischat_status' ] ) ? sanitize_key( wp_unslash( $_GET[ 'velischat_status' ] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- which notice to show after a redirect $draft = get_transient( velischat_support_draft_key() ); $draft = is_array( $draft ) ? $draft : array( 'name' => wp_get_current_user()->display_name, 'subject' => '', 'message' => '' ); $errors = array( 'missing_fields' => __( 'Please fill in name, subject and message.', 'velischat' ), 'too_many_files' => sprintf( /* translators: %d: number of images */ __( 'You can attach up to %d images.', 'velischat' ), VELISCHAT_SUPPORT_MAX_FILES ), 'file_too_big' => sprintf( /* translators: 1: size of one image, e.g. "5 MB", 2: size of all the images */ __( 'The attachments are too large: at most %1$s per image and %2$s in total.', 'velischat' ), size_format( velischat_support_max_file_size() ), size_format( velischat_support_max_total_size() ) ), 'bad_file_type' => __( 'You can only attach JPG, PNG, GIF or WebP images.', 'velischat' ), 'upload_error' => __( 'The attachments could not be uploaded. Please try again.', 'velischat' ), 'send_error' => sprintf( /* translators: %s: email address of VelisLab's support */ __( 'The email could not be sent from this site. Try again, or write to us directly at %s.', 'velischat' ), VELISCHAT_SUPPORT_EMAIL ), ); ?>

user_email ) ); ?>