@@ -506,8 +506,8 @@ function sse_create_site_archive( $export_paths, $database_file ) {
506506/**
507507 * Adds WordPress files to the zip archive.
508508 *
509- * @param ZipArchive $zip The zip archive object.
510- * @param string $export_dir The export directory to exclude.
509+ * @param ZipArchive $zip The zip archive object.
510+ * @param string $export_dir The export directory to exclude.
511511 * @return true|WP_Error True on success, WP_Error on failure.
512512 */
513513function sse_add_wordpress_files_to_zip ( $ zip , $ export_dir ) {
@@ -573,10 +573,10 @@ function sse_process_file_for_zip( $zip, $file_info, $source_path, $export_dir )
573573/**
574574 * Adds a file or directory to the zip archive.
575575 *
576- * @param ZipArchive $zip The zip archive object.
577- * @param SplFileInfo $file_info File information object.
578- * @param string|false $file Real file path or false if getRealPath() failed.
579- * @param string $pathname Original pathname.
576+ * @param ZipArchive $zip The zip archive object.
577+ * @param SplFileInfo $file_info File information object.
578+ * @param string|false $file Real file path or false if getRealPath() failed.
579+ * @param string $pathname Original pathname.
580580 * @param string $relative_path Relative path in archive.
581581 * @return true
582582 */
@@ -811,7 +811,7 @@ function sse_check_path_traversal( $normalized_file_path ) {
811811
812812/**
813813 * Resolves real file path, handling non-existent files securely.
814- *
814+ *
815815 * @param string $normalized_file_path The normalized file path.
816816 * @return string|false Real file path on success, false on failure.
817817 */
@@ -1015,7 +1015,7 @@ function sse_sanitize_filename( $filename ) {
10151015 * Checks if a file path is within the allowed base directory.
10161016 *
10171017 * @param string|false $real_file_path The real file path or false if resolution failed.
1018- * @param string $real_base_dir The real base directory path.
1018+ * @param string $real_base_dir The real base directory path.
10191019 * @return bool True if the file is within the base directory, false otherwise.
10201020 */
10211021function sse_check_path_within_base ( $ real_file_path , $ real_base_dir ) {
@@ -1275,7 +1275,7 @@ function sse_handle_secure_download() { // phpcs:ignore WordPress.Security.Nonce
12751275 wp_die ( esc_html ( $ validation ->get_error_message () ), 404 );
12761276 }
12771277
1278- // Rate limiting check
1278+ // Rate limiting check.
12791279 if ( ! sse_check_download_rate_limit () ) {
12801280 wp_die ( esc_html__ ( 'Too many download requests. Please wait before trying again. ' , 'simple-wp-site-exporter ' ), 429 );
12811281 }
@@ -1371,62 +1371,62 @@ function sse_check_download_rate_limit() {
13711371
13721372/**
13731373 * Validates file data for download operations.
1374- *
1375- * @param array $fileData File data array to validate.
1374+ *
1375+ * @param array $file_data File data array to validate.
13761376 * @return array Sanitized file data on success.
13771377 * @throws Exception If validation fails.
13781378 */
1379- function sse_validate_download_file_data ( $ fileData ) {
1380- // Additional security validation
1381- if ( ! is_array ( $ fileData ) ||
1382- ! isset ( $ fileData ['filepath ' ], $ fileData ['filename ' ], $ fileData ['filesize ' ] ) ) {
1379+ function sse_validate_download_file_data ( $ file_data ) {
1380+ // Additional security validation.
1381+ if ( ! is_array ( $ file_data ) ||
1382+ ! isset ( $ file_data ['filepath ' ], $ file_data ['filename ' ], $ file_data ['filesize ' ] ) ) {
13831383 sse_log ( 'Invalid file data provided for download ' , 'error ' );
13841384 wp_die ( esc_html__ ( 'Invalid file data. ' , 'simple-wp-site-exporter ' ) );
13851385 }
13861386
13871387 return array (
1388- 'filepath ' => sanitize_text_field ( $ fileData ['filepath ' ] ),
1389- 'filename ' => sanitize_file_name ( $ fileData ['filename ' ] ),
1390- 'filesize ' => absint ( $ fileData ['filesize ' ] )
1388+ 'filepath ' => sanitize_text_field ( $ file_data ['filepath ' ] ),
1389+ 'filename ' => sanitize_file_name ( $ file_data ['filename ' ] ),
1390+ 'filesize ' => absint ( $ file_data ['filesize ' ] )
13911391 );
13921392}
13931393
13941394/**
13951395 * Validates file path and accessibility for download.
1396- *
1396+ *
13971397 * @param string $filepath The file path to validate.
13981398 * @return void
13991399 * @throws Exception If validation fails.
14001400 */
14011401function sse_validate_download_file_access ( $ filepath ) {
1402- // Security: Whitelist approach - only allow files in our controlled export directory
1403- $ upload_dir = wp_upload_dir ();
1404- $ export_dir = trailingslashit ( $ upload_dir ['basedir ' ] ) . 'simple-wp-site-exporter-exports ' ;
1402+ // Security: Whitelist approach - only allow files in our controlled export directory.
1403+ $ upload_dir = wp_upload_dir ();
1404+ $ export_dir = trailingslashit ( $ upload_dir ['basedir ' ] ) . 'simple-wp-site-exporter-exports ' ;
14051405
1406- // Security: Additional validation to prevent SSRF attacks
1407- // Ensure file extension is in our allowed list
1406+ // Security: Additional validation to prevent SSRF attacks.
1407+ // Ensure file extension is in our allowed list.
14081408 $ allowed_extensions = array ( 'zip ' , 'sql ' );
1409- $ file_extension = strtolower ( pathinfo ( $ filepath , PATHINFO_EXTENSION ) );
1409+ $ file_extension = strtolower ( pathinfo ( $ filepath , PATHINFO_EXTENSION ) );
14101410 if ( ! in_array ( $ file_extension , $ allowed_extensions , true ) ) {
14111411 sse_log ( 'Security: Attempted access to file with disallowed extension: ' . $ file_extension , 'security ' );
14121412 wp_die ( esc_html__ ( 'Access denied - invalid file type. ' , 'simple-wp-site-exporter ' ) );
14131413 }
14141414
1415- // Security: Ensure file path is within our controlled export directory (prevents SSRF)
1415+ // Security: Ensure file path is within our controlled export directory (prevents SSRF).
14161416 if ( ! sse_validate_filepath ( $ filepath , $ export_dir ) ) {
14171417 sse_log ( 'Security: Attempted access to file outside allowed directory: ' . $ filepath , 'security ' );
14181418 wp_die ( esc_html__ ( 'Access denied. ' , 'simple-wp-site-exporter ' ) );
14191419 }
14201420
1421- // Security: Final verification - file exists, is readable, and is a regular file (not symlink/device)
1421+ // Security: Final verification - file exists, is readable, and is a regular file (not symlink/device).
14221422 if ( ! file_exists ( $ filepath ) || ! is_readable ( $ filepath ) || ! is_file ( $ filepath ) ) {
14231423 sse_log ( 'Security: File validation failed for: ' . $ filepath , 'security ' );
14241424 wp_die ( esc_html__ ( 'File not found. ' , 'simple-wp-site-exporter ' ) );
14251425 }
14261426
1427- // Security: Additional check to prevent access to sensitive files
1427+ // Security: Additional check to prevent access to sensitive files.
14281428 $ real_file_path = realpath ( $ filepath );
1429- if ( $ real_file_path === false || $ real_file_path !== $ filepath ) {
1429+ if ( false === $ real_file_path || $ real_file_path !== $ filepath ) {
14301430 sse_log ( 'Security: File path validation failed - potential symlink or path manipulation ' , 'security ' );
14311431 wp_die ( esc_html__ ( 'Access denied. ' , 'simple-wp-site-exporter ' ) );
14321432 }
@@ -1435,12 +1435,12 @@ function sse_validate_download_file_access( $filepath ) {
14351435/**
14361436 * Sets appropriate headers for file download.
14371437 *
1438- * @param string $filename The filename for download.
1439- * @param int $filesize The file size in bytes.
1438+ * @param string $filename The filename for download.
1439+ * @param int $filesize The file size in bytes.
14401440 * @return void
14411441 */
14421442function sse_set_download_headers ( $ filename , $ filesize ) {
1443- // Security: Set safe Content-Type based on file extension to prevent XSS
1443+ // Security: Set safe Content-Type based on file extension to prevent XSS.
14441444 $ file_extension = strtolower ( pathinfo ( $ filename , PATHINFO_EXTENSION ) );
14451445 switch ( $ file_extension ) {
14461446 case 'zip ' :
@@ -1450,22 +1450,22 @@ function sse_set_download_headers( $filename, $filesize ) {
14501450 $ content_type = 'application/sql ' ;
14511451 break ;
14521452 default :
1453- // Security: Default to octet-stream for unknown types to prevent execution
1453+ // Security: Default to octet-stream for unknown types to prevent execution.
14541454 $ content_type = 'application/octet-stream ' ;
14551455 break ;
14561456 }
14571457
1458- // Security: Set headers to prevent XSS and ensure proper download behavior
1458+ // Security: Set headers to prevent XSS and ensure proper download behavior.
14591459 header ( 'Content-Type: ' . $ content_type );
14601460 header ( 'Content-Disposition: attachment; filename=" ' . esc_attr ( $ filename ) . '" ' );
14611461 header ( 'Content-Length: ' . absint ( $ filesize ) );
14621462 header ( 'Cache-Control: no-cache, no-store, must-revalidate ' );
14631463 header ( 'Pragma: no-cache ' );
14641464 header ( 'Expires: 0 ' );
1465- header ( 'X-Content-Type-Options: nosniff ' ); // Security: Prevent MIME sniffing
1466- header ( 'X-Frame-Options: DENY ' ); // Security: Prevent framing
1465+ header ( 'X-Content-Type-Options: nosniff ' ); // Security: Prevent MIME sniffing.
1466+ header ( 'X-Frame-Options: DENY ' ); // Security: Prevent framing.
14671467
1468- // Disable output buffering for large files
1468+ // Disable output buffering for large files.
14691469 if ( ob_get_level () ) {
14701470 ob_end_clean ();
14711471 }
@@ -1478,21 +1478,21 @@ function sse_set_download_headers( $filename, $filesize ) {
14781478 * @return bool True if file passes security checks, false otherwise.
14791479 */
14801480function sse_validate_file_output_security ( $ filepath ) {
1481- // Security: Final validation before file output to prevent SSRF
1481+ // Security: Final validation before file output to prevent SSRF.
14821482 $ allowed_extensions = array ( 'zip ' , 'sql ' );
14831483 $ file_extension = strtolower ( pathinfo ( $ filepath , PATHINFO_EXTENSION ) );
14841484 if ( ! in_array ( $ file_extension , $ allowed_extensions , true ) ) {
14851485 sse_log ( 'Security: Blocked attempt to serve file with invalid extension: ' . $ file_extension , 'security ' );
14861486 wp_die ( esc_html__ ( 'Access denied - invalid file type. ' , 'simple-wp-site-exporter ' ) );
14871487 }
14881488
1489- // Security: Ensure file is within our controlled directory before serving
1490- $ upload_dir = wp_upload_dir ();
1491- $ export_dir = trailingslashit ( $ upload_dir ['basedir ' ] ) . 'simple-wp-site-exporter-exports ' ;
1489+ // Security: Ensure file is within our controlled directory before serving.
1490+ $ upload_dir = wp_upload_dir ();
1491+ $ export_dir = trailingslashit ( $ upload_dir ['basedir ' ] ) . 'simple-wp-site-exporter-exports ' ;
14921492 $ real_export_dir = realpath ( $ export_dir );
1493- $ real_file_path = realpath ( $ filepath );
1493+ $ real_file_path = realpath ( $ filepath );
14941494
1495- if ( $ real_export_dir === false || $ real_file_path === false || strpos ( $ real_file_path , $ real_export_dir ) !== 0 ) {
1495+ if ( false === $ real_export_dir || false === $ real_file_path || 0 !== strpos ( $ real_file_path , $ real_export_dir ) ) {
14961496 sse_log ( 'Security: File not within controlled export directory: ' . $ filepath , 'security ' );
14971497 wp_die ( esc_html__ ( 'Access denied. ' , 'simple-wp-site-exporter ' ) );
14981498 }
@@ -1509,10 +1509,10 @@ function sse_validate_file_output_security( $filepath ) {
15091509 * @throws Exception If file cannot be served.
15101510 */
15111511function sse_output_file_content ( $ filepath , $ filename ) {
1512- // Security: Validate file before output
1513- sse_validate_file_output_security ($ filepath );
1512+ // Security: Validate file before output.
1513+ sse_validate_file_output_security ( $ filepath );
15141514
1515- // Use readfile() for secure file download
1515+ // Use readfile() for secure file download.
15161516 if ( function_exists ( 'readfile ' ) && is_readable ( $ filepath ) && is_file ( $ filepath ) ) {
15171517 // Security: This readfile() call is safe - file path has been thoroughly validated.
15181518 readfile ( $ filepath ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_readfile -- Security validated export file download.
@@ -1527,19 +1527,19 @@ function sse_output_file_content( $filepath, $filename ) {
15271527/**
15281528 * Serves a file download with enhanced security validation.
15291529 *
1530- * @param array $fileData Validated file information array.
1530+ * @param array $file_data Validated file information array.
15311531 * @return void
15321532 */
1533- function sse_serve_file_download ( $ fileData ) {
1534- // Validate and sanitize file data
1535- $ sanitizedData = sse_validate_download_file_data ( $ fileData );
1533+ function sse_serve_file_download ( $ file_data ) {
1534+ // Validate and sanitize file data.
1535+ $ sanitized_data = sse_validate_download_file_data ( $ file_data );
15361536
1537- // Validate file access permissions
1538- sse_validate_download_file_access ( $ sanitizedData ['filepath ' ] );
1537+ // Validate file access permissions.
1538+ sse_validate_download_file_access ( $ sanitized_data ['filepath ' ] );
15391539
1540- // Set download headers
1541- sse_set_download_headers ( $ sanitizedData ['filename ' ], $ sanitizedData ['filesize ' ] );
1540+ // Set download headers.
1541+ sse_set_download_headers ( $ sanitized_data ['filename ' ], $ sanitized_data ['filesize ' ] );
15421542
1543- // Output file content
1544- sse_output_file_content ( $ sanitizedData ['filepath ' ], $ sanitizedData ['filename ' ] );
1543+ // Output file content.
1544+ sse_output_file_content ( $ sanitized_data ['filepath ' ], $ sanitized_data ['filename ' ] );
15451545}
0 commit comments