file_header = array(); $this->data_key = array(); return; } $this->file_header = array_values( $mortgage_application_form_fields ); $this->data_key = array_keys( $mortgage_application_form_fields ); // 3.0: add every answer the 2.x field list does not know (new short-app // questions and the full 1003), with its English label. if ( empty( $export_fields ) && class_exists( '\Mapp\Schema\Schema' ) ) { foreach ( \Mapp\Schema\Schema::questions() as $key => $def ) { if ( isset( $mortgage_application_form_fields[ $key ] ) || ! \Mapp\Schema\Schema::is_asked( $def ) || ( isset( $def['type'] ) && 'consent' === $def['type'] ) ) { continue; } $this->data_key[] = $key; $this->file_header[] = \Mapp\Schema\Schema::text( $def['label'] ); } $this->data_key[] = 'loan_amount'; $this->file_header[] = 'Estimated loan amount'; } // check specific fields are selected if ( ! empty( $export_fields ) && is_array( $export_fields ) ) { $this->data_key = $export_fields; $this->file_header = array(); foreach ( $this->data_key as $key ) { if ( isset( $mortgage_application_form_fields[ $key ] ) ) { $this->file_header[] = $mortgage_application_form_fields[ $key ]; } } } } public function create_file( $name, $data ) {} /** @var string|null Private working folder for this export request. */ protected static $run_dir = null; /** * 3.0: exports are built in a private, single-use folder (deny-all, outside * the public uploads month folders) and deleted after download. 2.6 wrote * them, SSNs included, to wp-content/uploads/YYYY/MM/ and left them there. */ public function get_dir_path( $path = '' ) { if ( null === self::$run_dir ) { $base = \Mapp\Files\Store::base_dir() . 'exports/'; wp_mkdir_p( $base ); \Mapp\Files\Store::protect( $base ); self::cleanup_stale( $base ); self::$run_dir = $base . gmdate( 'YmdHis' ) . '-' . wp_generate_password( 12, false ) . '/'; wp_mkdir_p( self::$run_dir ); } return trailingslashit( self::$run_dir . ( ! empty( $path ) ? $path : '' ) ); } /** * Remove export folders left behind by failed requests (older than an hour). * * @param string $base Exports folder. */ protected static function cleanup_stale( $base ) { foreach ( (array) glob( $base . '*', GLOB_ONLYDIR ) as $dir ) { if ( filemtime( $dir ) < time() - HOUR_IN_SECONDS ) { self::delete_tree( $dir ); } } } /** * @param string $dir Directory. */ protected static function delete_tree( $dir ) { if ( ! is_dir( $dir ) ) { return; } $items = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir, FilesystemIterator::SKIP_DOTS ), RecursiveIteratorIterator::CHILD_FIRST ); foreach ( $items as $item ) { $item->isDir() ? @rmdir( $item->getPathname() ) : @unlink( $item->getPathname() ); // phpcs:ignore } @rmdir( $dir ); // phpcs:ignore } /** * File name for one application's export: "Lopez-Maria-1003-12", no * email address (file names end up in download folders and syncs). * * @param int $application_id Application ID. * @return string Without extension. */ public static function file_label( $application_id ) { $name = trim( get_post_meta( $application_id, 'last_name', true ) . '-' . get_post_meta( $application_id, 'first_name', true ), '-' ); $name = sanitize_file_name( remove_accents( $name ) ); return ( '' !== $name ? $name . '-' : '' ) . '1003-' . (int) $application_id; } /** * Send what the export wrote: one file as itself (the LOS imports it * directly), several as a zip. Then nothing is left on disk. * * @param string $folder Folder holding the export's files. * @param string $zip_name Zip file name when there are several. */ public function deliver( $folder, $zip_name ) { $files = array_values( array_filter( (array) glob( trailingslashit( $folder ) . '*' ), 'is_file' ) ); if ( ! $files ) { if ( null !== self::$run_dir ) { self::delete_tree( self::$run_dir ); } wp_die( esc_html__( 'There was nothing to export for those applications.', '1003-mortgage-application' ), '', array( 'response' => 404 ) ); } if ( 1 === count( $files ) ) { $types = array( 'xml' => 'application/xml', 'fnm' => 'application/octet-stream', 'csv' => 'text/csv', ); $ext = strtolower( pathinfo( $files[0], PATHINFO_EXTENSION ) ); $this->stream( $files[0], isset( $types[ $ext ] ) ? $types[ $ext ] : 'application/octet-stream' ); } $zip = $this->create_zip( realpath( $folder ) ); $this->stream( $zip, 'application/zip', $zip_name ); } /** * @param string $path File. * @param string $type MIME type. * @param string $name Download name (default: the file's). */ private function stream( $path, $type, $name = '' ) { nocache_headers(); header( 'Content-Type: ' . $type . ( 0 === strpos( $type, 'text/' ) ? '; charset=UTF-8' : '' ) ); header( 'Content-Disposition: attachment; filename="' . str_replace( array( '"', "\r", "\n" ), '', '' !== $name ? $name : basename( $path ) ) . '"' ); header( 'Content-Length: ' . filesize( $path ) ); header( 'X-Content-Type-Options: nosniff' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_readfile readfile( $path ); if ( null !== self::$run_dir ) { self::delete_tree( self::$run_dir ); } exit; } public function create_zip( $file_folder ) { // Initialize archive object $zip = new ZipArchive(); // The zip goes next to (not inside) the folder being zipped. $dir_path = $this->get_dir_path(); // create zip archive $zip->open( $dir_path . 'applications.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE ); // Create recursive directory iterator $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $file_folder ), RecursiveIteratorIterator::LEAVES_ONLY ); if ( ! empty( $files ) && isset( $files ) ) { foreach ( $files as $name => $file ) { // Skip directories (they would be added automatically) if ( ! $file->isDir() ) { // Get real and relative path for current file $filePath = $file->getRealPath(); $relativePath = substr( $filePath, strlen( $file_folder ) + 1 ); // Add current file to archive $zip->addFile( $filePath, $relativePath ); } } } // Zip archive will be created only after closing object $zip->close(); return $dir_path . 'applications.zip'; } }