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