= 0). * @return array{items: array, total: int}|\WP_Error */ public static function list_entries( string $which, int $limit = 50, int $offset = 0 ) { $which = self::BACKUPS_DIR === $which ? self::BACKUPS_DIR : self::STAGING_DIR; $dir = self::resolve_dir( $which ); if ( false === $dir ) { return new \WP_Error( 'uploads_unavailable', __( 'Directory is unavailable on this site.', 'acrossai-abilities-manager' ) ); } $limit = max( 1, min( 200, $limit ) ); $offset = max( 0, $offset ); $files = glob( trailingslashit( $dir ) . '*.zip' ); if ( ! is_array( $files ) ) { $files = array(); } usort( $files, static function ( string $a, string $b ): int { $ma = (int) @filemtime( $a ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged $mb = (int) @filemtime( $b ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged return $mb <=> $ma; } ); $total = count( $files ); $slice = array_slice( $files, $offset, $limit ); $uploads_base = wp_upload_dir( null, false ); $uploads_basedir = ! empty( $uploads_base['basedir'] ) ? rtrim( (string) $uploads_base['basedir'], '/' ) : ''; $uploads_baseurl = ! empty( $uploads_base['baseurl'] ) ? rtrim( (string) $uploads_base['baseurl'], '/' ) : ''; $abspath = rtrim( realpath( ABSPATH ) ?: ABSPATH, '/' ); $items = array(); foreach ( $slice as $abs ) { $rel = $abs; if ( '' !== $abspath && 0 === strpos( $abs, $abspath . '/' ) ) { $rel = substr( $abs, strlen( $abspath ) + 1 ); } $url = ''; if ( '' !== $uploads_basedir && 0 === strpos( $abs, $uploads_basedir . '/' ) ) { $url = $uploads_baseurl . '/' . ltrim( substr( $abs, strlen( $uploads_basedir ) ), '/' ); } $items[] = array( 'file_path' => $rel, 'file_url' => $url, 'size' => (int) @filesize( $abs ), // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged 'sha256' => self::sha256_of( $abs ), 'created_at' => gmdate( 'c', (int) @filemtime( $abs ) ), // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged ); } return array( 'items' => $items, 'total' => $total, ); } /** * Compute the SHA-256 of a file, or '' when it cannot be read. Skips large * files gracefully. */ public static function sha256_of( string $abs_path ): string { if ( ! is_file( $abs_path ) || ! is_readable( $abs_path ) ) { return ''; } $hash = @hash_file( 'sha256', $abs_path ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged return is_string( $hash ) ? $hash : ''; } /** * Convert an absolute path inside uploads/ into ABSPATH-relative form for * transport to callers. Returns the input unchanged when it is already * outside ABSPATH. */ public static function to_abspath_relative( string $abs_path ): string { $abspath = rtrim( realpath( ABSPATH ) ?: ABSPATH, '/' ); if ( '' !== $abspath && 0 === strpos( $abs_path, $abspath . '/' ) ) { return substr( $abs_path, strlen( $abspath ) + 1 ); } return $abs_path; } /** * Return the wp-uploads URL for an absolute path inside uploads/, or '' * when it is not under uploads/. */ public static function url_for( string $abs_path ): string { $uploads = wp_upload_dir( null, false ); if ( empty( $uploads['basedir'] ) || empty( $uploads['baseurl'] ) ) { return ''; } $basedir = rtrim( (string) $uploads['basedir'], '/' ); $baseurl = rtrim( (string) $uploads['baseurl'], '/' ); if ( 0 === strpos( $abs_path, $basedir . '/' ) ) { return $baseurl . '/' . ltrim( substr( $abs_path, strlen( $basedir ) ), '/' ); } return ''; } /** * Ensure the given managed sub-dir exists inside uploads/ with hardening * files. Returns the absolute path or false on failure. * * @return string|false */ private static function resolve_dir( string $subdir ) { $uploads = wp_upload_dir( null, false ); if ( ! empty( $uploads['error'] ) || empty( $uploads['basedir'] ) ) { return false; } $dir = trailingslashit( (string) $uploads['basedir'] ) . $subdir; if ( ! wp_mkdir_p( $dir ) ) { return false; } $htaccess = $dir . '/.htaccess'; if ( ! file_exists( $htaccess ) ) { // Block PHP execution and disable directory listing, but allow // direct downloads of the finalized .zip files so the URLs returned // by Create_Zip_Backup / Download_Zip_Backup remain fetchable. Random filenames // provide the enumeration defense. $rules = "Options -Indexes\n" . "\n" . "\tDeny from all\n" . "\t\n" . "\t\tRequire all denied\n" . "\t\n" . "\n"; @file_put_contents( $htaccess, $rules ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents, WordPress.PHP.NoSilencedErrors.Discouraged } $index_php = $dir . '/index.php'; if ( ! file_exists( $index_php ) ) { @file_put_contents( $index_php, "