array(), 'imported' => 0, 'skipped' => 0, 'invalid' => 0, ); if ( empty( $file_info['tmp_name'] ) ) { return $empty; } // Security check: Verify file size (hard limit 5MB). if ( $file_info['size'] > 5 * 1024 * 1024 ) { return $empty; } // Security check: Verify file extension. $file_ext = pathinfo( $file_info['name'], PATHINFO_EXTENSION ); if ( 'csv' !== strtolower( $file_ext ) ) { return $empty; } // Security check: Verify MIME type if possible. $finfo = finfo_open( FILEINFO_MIME_TYPE ); $mime_type = finfo_file( $finfo, $file_info['tmp_name'] ); finfo_close( $finfo ); $valid_mimes = array( 'text/csv', 'text/plain', 'application/csv', 'text/comma-separated-values', 'application/excel', 'application/vnd.ms-excel', 'application/vnd.msexcel' ); if ( ! in_array( $mime_type, $valid_mimes, true ) ) { return $empty; } $seen = array(); $invalid = 0; $skipped = 0; $count = 0; $limit = 10000; // Hard limit on rows for safety. $existing_map = array_flip( array_map( 'strtolower', (array) $existing_domains ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen $handle = fopen( $file_info['tmp_name'], 'r' ); if ( false !== $handle ) { $data = fgetcsv( $handle, 1000, ',' ); while ( false !== $data ) { ++$count; if ( $count > $limit ) { break; } foreach ( $data as $cell ) { $raw = trim( $cell ); $lower = strtolower( $raw ); if ( ! $this->is_valid_rule( $raw ) ) { ++$invalid; continue; } // Duplicate within this CSV upload. if ( isset( $seen[ $lower ] ) ) { ++$skipped; continue; } // Duplicate against already-saved rules. if ( isset( $existing_map[ $lower ] ) ) { ++$skipped; continue; } $seen[ $lower ] = true; } $data = fgetcsv( $handle, 1000, ',' ); } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose fclose( $handle ); } $domains = array_keys( $seen ); $imported = count( $domains ); return array( 'domains' => $domains, 'imported' => $imported, 'skipped' => $skipped, 'invalid' => $invalid, ); } /** * Export domains to CSV. * * @param array $domains List of domains to export. * @return void * @since 1.2.0 */ public function export_domains_to_csv( $domains ) { if ( ! is_array( $domains ) ) { return; } $filename = 'aedr-allowed-domains-' . gmdate( 'Y-m-d' ) . '.csv'; // Force download headers. header( 'Content-Type: text/csv; charset=utf-8' ); header( 'Content-Disposition: attachment; filename=' . $filename ); header( 'Pragma: no-cache' ); header( 'Expires: 0' ); $output = fopen( 'php://output', 'w' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen // Header row. fputcsv( $output, array( __( 'Domain', 'advanced-email-domain-restriction' ) ) ); foreach ( $domains as $domain ) { fputcsv( $output, array( $domain ) ); } fclose( $output ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose exit; } /** * Validate a single rule string. * * Accepts domains, TLDs (starting with '.'), exact email addresses, * and wildcard rules (starting with '*.'). * * @param string $rule The rule value to validate. * @return bool True if the rule is a recognised format. * @since 1.4.0 */ private function is_valid_rule( $rule ) { if ( empty( $rule ) ) { return false; } // Wildcard rule, e.g. *.example.com. if ( 0 === strpos( $rule, '*.' ) ) { return (bool) preg_match( '/^\*\.([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$/i', $rule ); } // TLD rule, e.g. .edu or .co.uk. if ( '.' === $rule[0] ) { return (bool) preg_match( '/^\.[a-z]{2,}(\.[a-z]{2,})?$/i', $rule ); } // Exact email address. if ( strpos( $rule, '@' ) !== false ) { return (bool) is_email( $rule ); } // Plain domain. return (bool) preg_match( '/^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$/i', $rule ); } } }