modules ) )->build_report(); $host = sanitize_file_name( (string) wp_parse_url( home_url(), PHP_URL_HOST ) ); $name = 'system-report-' . ( '' !== $host ? $host . '-' : '' ) . gmdate( 'Y-m-d' ); if ( 'pdf' === $format ) { return array( 'name' => $name . '.pdf', 'body' => TextPdf::render( $report, 'System Report - ' . home_url() ), 'type' => 'application/pdf', ); } return array( 'name' => $name . '.txt', 'body' => $report, 'type' => 'text/plain; charset=utf-8', ); } /** * The posted attachment format. * * @return string 'txt' or 'pdf'. */ private static function posted_format(): string { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- each caller verifies its nonce first. return isset( $_POST['format'] ) && 'pdf' === sanitize_key( wp_unslash( $_POST['format'] ) ) ? 'pdf' : 'txt'; } /** * Download the report. * * @return void */ public function handle_download(): void { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'all-in-one-utilities' ), '', array( 'response' => 403 ) ); } check_admin_referer( 'aiou_download_report' ); $file = $this->report_file( self::posted_format() ); nocache_headers(); header( 'Content-Type: ' . $file['type'] ); header( 'Content-Disposition: attachment; filename="' . $file['name'] . '"' ); header( 'Content-Length: ' . strlen( $file['body'] ) ); echo $file['body']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- a file download, not HTML. exit; } /** * Email the message and the report to support. * * @return void */ public function handle_send(): void { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'all-in-one-utilities' ), '', array( 'response' => 403 ) ); } check_admin_referer( 'aiou_send_support' ); $user = wp_get_current_user(); $reply = isset( $_POST['reply_to'] ) ? sanitize_email( wp_unslash( $_POST['reply_to'] ) ) : ''; $subject = isset( $_POST['subject'] ) ? sanitize_text_field( wp_unslash( $_POST['subject'] ) ) : ''; $message = isset( $_POST['message'] ) ? sanitize_textarea_field( wp_unslash( $_POST['message'] ) ) : ''; $attach = ! empty( $_POST['attach'] ); $format = self::posted_format(); // Keep what was typed if the form has to be shown again. $draft = array( 'reply_to' => $reply, 'subject' => $subject, 'message' => $message, 'format' => $format, 'attach' => $attach, ); if ( ! is_email( $reply ) ) { $this->back( 'error', __( 'Enter a valid email address for the reply.', 'all-in-one-utilities' ), $draft ); } if ( '' === trim( $message ) ) { $this->back( 'error', __( 'Write a message describing the problem.', 'all-in-one-utilities' ), $draft ); } if ( mb_strlen( $message ) > self::MAX_MESSAGE ) { /* translators: %d: maximum number of characters */ $this->back( 'error', sprintf( __( 'The message is too long (%d characters at most).', 'all-in-one-utilities' ), self::MAX_MESSAGE ), $draft ); } $lock = 'aiou_support_sent_' . $user->ID; if ( get_transient( $lock ) ) { $this->back( 'error', __( 'A message was just sent. Please wait a minute before sending another.', 'all-in-one-utilities' ), $draft ); } $site = wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ); $subject = '' !== $subject ? $subject : __( 'Support request', 'all-in-one-utilities' ); $body = $message . "\n\n-- \n" . implode( "\n", array( 'From: ' . $user->display_name . ' <' . $reply . '>', 'Site: ' . $site . ' (' . home_url() . ')', 'All-in-One Utilities: ' . AIOU_VERSION, $attach ? 'System report: attached (' . strtoupper( $format ) . ')' : 'System report: not attached', ) ); $attachments = array(); $tmp_dir = ''; if ( $attach ) { $file = $this->report_file( $format ); $tmp_dir = trailingslashit( get_temp_dir() ) . 'aiou-support-' . wp_generate_password( 12, false ); if ( wp_mkdir_p( $tmp_dir ) && false !== file_put_contents( $tmp_dir . '/' . $file['name'], $file['body'] ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents -- a short-lived temp file for wp_mail(). $attachments[ $file['name'] ] = $tmp_dir . '/' . $file['name']; } } $sent = wp_mail( self::support_email(), '[' . $site . '] ' . $subject, $body, array( 'Reply-To: ' . $user->display_name . ' <' . $reply . '>' ), $attachments ); foreach ( $attachments as $path ) { wp_delete_file( $path ); } if ( '' !== $tmp_dir && is_dir( $tmp_dir ) ) { rmdir( $tmp_dir ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_rmdir -- removing our own empty temp folder. } if ( ! $sent ) { $this->back( 'error', __( 'The email could not be sent. Check that this site can send email (see the Email Log), or download the report and email it yourself.', 'all-in-one-utilities' ), $draft ); } set_transient( $lock, 1, self::COOLDOWN ); $this->back( 'success', sprintf( /* translators: %s: support email address */ __( 'Sent to %s. Replies will come to the address you entered.', 'all-in-one-utilities' ), self::support_email() ) ); } /** * Store a one-time notice (and the draft, after an error) and go back * to the System Report screen. * * @param string $type 'success' or 'error'. * @param string $message Notice text. * @param array $draft What was typed, to show again. * @return never */ private function back( string $type, string $message, array $draft = array() ) { set_transient( 'aiou_support_notice_' . get_current_user_id(), array( 'type' => $type, 'message' => $message, 'draft' => $draft, ), 5 * MINUTE_IN_SECONDS ); wp_safe_redirect( admin_url( 'admin.php?page=' . Menu::SLUG . '-system-report#aiou-support' ) ); exit; } }