Admin
4 years ago
Compatibility
4 years ago
Helpers
4 years ago
Providers
4 years ago
Reports
4 years ago
Tasks
4 years ago
UsageTracking
4 years ago
Conflicts.php
4 years ago
Connect.php
4 years ago
Core.php
4 years ago
Debug.php
4 years ago
Geo.php
4 years ago
MailCatcher.php
4 years ago
MailCatcherInterface.php
4 years ago
MailCatcherV6.php
4 years ago
Migration.php
4 years ago
MigrationAbstract.php
4 years ago
Options.php
4 years ago
Processor.php
4 years ago
SiteHealth.php
4 years ago
Upgrade.php
4 years ago
Uploads.php
4 years ago
WP.php
4 years ago
Uploads.php
200 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPMailSMTP; |
| 4 | |
| 5 | use WP_Error; |
| 6 | |
| 7 | /** |
| 8 | * WPMailSMTP uploads. |
| 9 | * |
| 10 | * @since 2.8.0 |
| 11 | */ |
| 12 | class Uploads { |
| 13 | |
| 14 | /** |
| 15 | * Get WPMailSMTP upload root path (e.g. /wp-content/uploads/wp-mail-smtp). |
| 16 | * |
| 17 | * @since 2.8.0 |
| 18 | * |
| 19 | * @return array|WP_Error WPMailSMTP upload root path (no trailing slash). |
| 20 | */ |
| 21 | public static function upload_dir() { |
| 22 | |
| 23 | $upload_dir = wp_upload_dir(); |
| 24 | |
| 25 | if ( ! empty( $upload_dir['error'] ) ) { |
| 26 | return new WP_Error( 'wp_upload_dir_error', $upload_dir['error'] ); |
| 27 | } |
| 28 | |
| 29 | $dir = 'wp-mail-smtp'; |
| 30 | |
| 31 | $upload_root = trailingslashit( realpath( $upload_dir['basedir'] ) ) . $dir; |
| 32 | |
| 33 | /** |
| 34 | * Filters upload dir path. |
| 35 | * |
| 36 | * @since 2.8.0 |
| 37 | * |
| 38 | * @param string $upload_root Upload dir path. |
| 39 | */ |
| 40 | $custom_uploads_root = apply_filters( 'wp_mail_smtp_uploads_upload_dir_root', $upload_root ); |
| 41 | if ( wp_is_writable( $custom_uploads_root ) ) { |
| 42 | $upload_root = $custom_uploads_root; |
| 43 | } |
| 44 | |
| 45 | if ( ! file_exists( $upload_root ) && ! wp_mkdir_p( $custom_uploads_root ) ) { |
| 46 | return new WP_Error( |
| 47 | 'wp_mail_smtp_upload_dir_unable_create', |
| 48 | sprintf( |
| 49 | /* translators: %s: Directory path. */ |
| 50 | __( 'Unable to create directory %s. Is its parent directory writable by the server?', 'wp-mail-smtp' ), |
| 51 | esc_html( $upload_root ) |
| 52 | ) |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | if ( ! wp_is_writable( $custom_uploads_root ) ) { |
| 57 | return new WP_Error( |
| 58 | 'wp_mail_smtp_upload_dir_not_writable', |
| 59 | sprintf( |
| 60 | /* translators: %s: Directory path. */ |
| 61 | __( 'Unable to write in WPMailSMTP upload directory %s. Is it writable by the server?', 'wp-mail-smtp' ), |
| 62 | esc_html( $upload_root ) |
| 63 | ) |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | return [ |
| 68 | 'path' => $upload_root, |
| 69 | 'url' => trailingslashit( $upload_dir['baseurl'] ) . $dir, |
| 70 | ]; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | /** |
| 75 | * Create .htaccess file in the WPMailSMTP upload directory. |
| 76 | * |
| 77 | * @since 2.8.0 |
| 78 | * |
| 79 | * @return bool True when the .htaccess file exists, false on failure. |
| 80 | */ |
| 81 | public static function create_upload_dir_htaccess_file() { |
| 82 | |
| 83 | /** |
| 84 | * Filters create upload dir htaccess file. |
| 85 | * |
| 86 | * @since 2.8.0 |
| 87 | * |
| 88 | * @param bool $is_create Creates upload dir htaccess file. |
| 89 | */ |
| 90 | if ( ! apply_filters( 'wp_mail_smtp_uploads_create_upload_dir_htaccess_file', true ) ) { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | $upload_dir = self::upload_dir(); |
| 95 | |
| 96 | if ( is_wp_error( $upload_dir ) ) { |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | $htaccess_file = wp_normalize_path( trailingslashit( $upload_dir['path'] ) . '.htaccess' ); |
| 101 | $cache_key = 'wp_mail_smtp_upload_dir_htaccess_file'; |
| 102 | |
| 103 | if ( is_file( $htaccess_file ) ) { |
| 104 | $cached_stat = get_transient( $cache_key ); |
| 105 | $stat = array_intersect_key( |
| 106 | stat( $htaccess_file ), |
| 107 | [ |
| 108 | 'size' => 0, |
| 109 | 'mtime' => 0, |
| 110 | 'ctime' => 0, |
| 111 | ] |
| 112 | ); |
| 113 | |
| 114 | if ( $cached_stat === $stat ) { |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | @unlink( $htaccess_file ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 119 | } |
| 120 | |
| 121 | if ( ! function_exists( 'insert_with_markers' ) ) { |
| 122 | require_once ABSPATH . 'wp-admin/includes/misc.php'; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Filters upload dir htaccess file content. |
| 127 | * |
| 128 | * @since 2.8.0 |
| 129 | * |
| 130 | * @param bool $content Upload dir htaccess file content. |
| 131 | */ |
| 132 | $contents = apply_filters( |
| 133 | 'wp_mail_smtp_uploads_create_upload_dir_htaccess_file_content', |
| 134 | '# Disable PHP and Python scripts parsing. |
| 135 | <Files *> |
| 136 | SetHandler none |
| 137 | SetHandler default-handler |
| 138 | RemoveHandler .cgi .php .php3 .php4 .php5 .phtml .pl .py .pyc .pyo |
| 139 | RemoveType .cgi .php .php3 .php4 .php5 .phtml .pl .py .pyc .pyo |
| 140 | </Files> |
| 141 | <IfModule mod_php5.c> |
| 142 | php_flag engine off |
| 143 | </IfModule> |
| 144 | <IfModule mod_php7.c> |
| 145 | php_flag engine off |
| 146 | </IfModule> |
| 147 | <IfModule mod_php8.c> |
| 148 | php_flag engine off |
| 149 | </IfModule> |
| 150 | <IfModule headers_module> |
| 151 | Header set X-Robots-Tag "noindex" |
| 152 | </IfModule>' |
| 153 | ); |
| 154 | |
| 155 | $created = insert_with_markers( $htaccess_file, 'WPMailSMTP', $contents ); |
| 156 | |
| 157 | if ( $created ) { |
| 158 | clearstatcache( true, $htaccess_file ); |
| 159 | $stat = array_intersect_key( |
| 160 | stat( $htaccess_file ), |
| 161 | [ |
| 162 | 'size' => 0, |
| 163 | 'mtime' => 0, |
| 164 | 'ctime' => 0, |
| 165 | ] |
| 166 | ); |
| 167 | |
| 168 | set_transient( $cache_key, $stat ); |
| 169 | } |
| 170 | |
| 171 | return $created; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Create index.html file in the specified directory if it doesn't exist. |
| 176 | * |
| 177 | * @since 2.8.0 |
| 178 | * |
| 179 | * @param string $path Path to the directory. |
| 180 | * |
| 181 | * @return int|false Number of bytes that were written to the file, or false on failure. |
| 182 | */ |
| 183 | public static function create_index_html_file( $path ) { |
| 184 | |
| 185 | if ( ! is_dir( $path ) || is_link( $path ) ) { |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | $index_file = wp_normalize_path( trailingslashit( $path ) . 'index.html' ); |
| 190 | |
| 191 | // Do nothing if index.html exists in the directory. |
| 192 | if ( file_exists( $index_file ) ) { |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | // Create empty index.html. |
| 197 | return file_put_contents( $index_file, '' ); // phpcs:ignore WordPress.WP.AlternativeFunctions |
| 198 | } |
| 199 | } |
| 200 |