rocklobster-in / contact-form-7

Contact Form 7 - Just another contact form plugin for WordPress.
Other
290 stars 144 forks source link

Can't attach file due to /uploads folder being a symlink #1506

Closed axi closed 2 weeks ago

axi commented 2 weeks ago

Describe the bug This seems to have previously been addressed in #707, #475 but it still doesn't work.

Here is my config:

user@server:/path/to/physical/deployments/2024-11-08-dev-08acad54# ls -l web/app/
total 5584
drwxrwxr-x  3 user psacln    4096 Nov  8 19:07 mu-plugins
drwxrwxr-x 16 user psacln    4096 Nov  8 19:07 plugins
drwxrwxr-x  5 user psacln    4096 Nov  8 19:07 themes
lrwxrwxrwx  1 user psacln      52 Nov  8 19:06 uploads -> /path/to/physical/environments/uploads_dev
user@server:/path/to/physical/deployments/2024-11-08-dev-08acad54# 

I'm using the wpcf7_before_send_mail to add an attachment (my usecase is a bit more complexe but simplified example is valid):

add_action(
    'wpcf7_before_send_mail',
    static function ($contact_form, $abort, $submission): void {
        $pdfPath = "uploads/2024/11/my.pdf";
        $path = path_join(WP_CONTENT_DIR, $pdfPath);
        if (file_exists($path)) {
            $submission->add_extra_attachments($pdfPath, 'mail');
        }
    },
    10,
    3
);

That means that realpaths found withing the static callback function in function wpcf7_is_file_path_in_content_dir() method are:

# $real_path = realpath( $path );
/path/to/physical/environments/uploads_dev/2024/11/my.pdf
# $real_dir = realpath( $dir );
/path/to/physical/deployments/2024-11-08-dev-08acad54/web/app/

and that wpcf7_is_file_path_in_content_dir() return false.

I don't know what would be the best way to fix it. Would a new filter be appropriate ? I mean something like:

function wpcf7_is_file_path_in_content_dir( $path ) {
    // ...
    if (
        defined( 'WP_TEMP_DIR' ) and
        call_user_func( $callback, $path, WP_TEMP_DIR )
    ) {
        return true;
    }

    if (apply_filters( 'wpcf7_custom_file_path_allowed', $path)) {
        return true;
        }

    return false;
}
axi commented 1 week ago

Here is how I "fixed" it. I set the WP_TEMP_DIR to something.

define('WP_TEMP_DIR', '/path/to/physical/temp_dir');

and

add_action(
    'wpcf7_before_send_mail',
    static function ($contact_form, $abort, $submission): void {
        $pdfPath = "uploads/2024/11/my.pdf";
        $path = path_join(WP_CONTENT_DIR, $pdfPath);

        if (file_exists($path)) {
            // Create temp file path
            $tmpFilePath = path_join(WP_TEMP_DIR, basename($pdfPath));

            // Copy file to a place cf7 will accept
            if (!file_exists($tmpFilePath)) {
                // Make sure the temp dir exists
                wp_mkdir_p(WP_TEMP_DIR);
                copy($path, $tmpFilePath);
            }

            $submission->add_extra_attachments($tmpFilePath, 'mail');
        }
    },
    10,
    3
);