access.php
8 months ago
builder.php
11 months ago
checks.php
6 months ago
colors.php
2 years ago
data-presets.php
6 months ago
date-time.php
1 year ago
debug.php
9 months ago
education.php
11 months ago
escape-sanitize.php
6 months ago
filesystem-media.php
1 year ago
form-fields.php
8 months ago
forms.php
10 months ago
list.php
1 year ago
payments.php
10 months ago
plugins.php
1 year ago
privacy.php
1 year ago
providers.php
1 year ago
utilities.php
5 months ago
filesystem-media.php
302 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Helper functions to work with filesystem, uploads and media files. |
| 4 | * |
| 5 | * @since 1.8.0 |
| 6 | */ |
| 7 | |
| 8 | use WPForms\Helpers\File; |
| 9 | |
| 10 | /** |
| 11 | * Get WPForms upload root path (e.g. /wp-content/uploads/wpforms). |
| 12 | * |
| 13 | * As of 1.7.0, you can pass in your own value that matches the output of wp_upload_dir() |
| 14 | * in order to use this function inside of a filter without infinite looping. |
| 15 | * |
| 16 | * @since 1.6.1 |
| 17 | * |
| 18 | * @return array WPForms upload root path (no trailing slash). |
| 19 | */ |
| 20 | function wpforms_upload_dir() { |
| 21 | |
| 22 | $upload_dir = wp_upload_dir(); |
| 23 | |
| 24 | if ( ! empty( $upload_dir['error'] ) ) { |
| 25 | return [ 'error' => $upload_dir['error'] ]; |
| 26 | } |
| 27 | |
| 28 | $basedir = wp_is_stream( $upload_dir['basedir'] ) ? $upload_dir['basedir'] : realpath( $upload_dir['basedir'] ); |
| 29 | $wpforms_upload_root = trailingslashit( $basedir ) . 'wpforms'; |
| 30 | |
| 31 | /** |
| 32 | * Allow developers to change a directory where cache and uploaded files will be stored. |
| 33 | * |
| 34 | * @since 1.5.2 |
| 35 | * |
| 36 | * @param string $wpforms_upload_root WPForms upload root directory. |
| 37 | */ |
| 38 | $custom_uploads_root = apply_filters( 'wpforms_upload_root', $wpforms_upload_root ); |
| 39 | |
| 40 | if ( is_dir( $custom_uploads_root ) && wp_is_writable( $custom_uploads_root ) ) { |
| 41 | $wpforms_upload_root = wp_is_stream( $custom_uploads_root ) |
| 42 | ? $custom_uploads_root |
| 43 | : realpath( $custom_uploads_root ); |
| 44 | } |
| 45 | |
| 46 | return [ |
| 47 | 'path' => $wpforms_upload_root, |
| 48 | 'url' => trailingslashit( $upload_dir['baseurl'] ) . 'wpforms', |
| 49 | 'error' => false, |
| 50 | ]; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Create index.html file in the specified directory if it doesn't exist. |
| 55 | * |
| 56 | * @since 1.6.1 |
| 57 | * |
| 58 | * @param string $path Path to the directory. |
| 59 | * |
| 60 | * @return int|false Number of bytes that were written to the file, or false on failure. |
| 61 | */ |
| 62 | function wpforms_create_index_html_file( $path ) { |
| 63 | |
| 64 | if ( ! is_dir( $path ) || is_link( $path ) ) { |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | $index_file = wp_normalize_path( trailingslashit( $path ) . 'index.html' ); |
| 69 | |
| 70 | // Do nothing if index.html exists in the directory. |
| 71 | if ( file_exists( $index_file ) ) { |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | // Create empty index.html. |
| 76 | return file_put_contents( $index_file, '' ); // phpcs:ignore WordPress.WP.AlternativeFunctions |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Create index.php file in the specified directory if it doesn't exist. |
| 81 | * |
| 82 | * @since 1.8.7 |
| 83 | * |
| 84 | * @param string $path Path to the directory. |
| 85 | * |
| 86 | * @return int|false Number of bytes that were written to the file, or false on failure. |
| 87 | */ |
| 88 | function wpforms_create_index_php_file( string $path ) { |
| 89 | |
| 90 | if ( ! is_dir( $path ) || is_link( $path ) ) { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | $index_file = wp_normalize_path( trailingslashit( $path ) . 'index.php' ); |
| 95 | |
| 96 | // Do nothing if index.php exists in the directory. |
| 97 | if ( file_exists( $index_file ) ) { |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | $data = '<?php |
| 102 | header( $_SERVER[\'SERVER_PROTOCOL\'] . \' 404 Not Found\' ); |
| 103 | header( \'Status: 404 Not Found\' ); |
| 104 | '; |
| 105 | |
| 106 | // Create index.php. |
| 107 | return file_put_contents( $index_file, $data ); // phpcs:ignore WordPress.WP.AlternativeFunctions |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Create .htaccess file in the WPForms upload directory. |
| 112 | * |
| 113 | * @since 1.6.1 |
| 114 | * |
| 115 | * @return bool True when the .htaccess file exists, false on failure. |
| 116 | */ |
| 117 | function wpforms_create_upload_dir_htaccess_file(): bool { |
| 118 | |
| 119 | /** |
| 120 | * Whether to create upload dir .htaccess file. |
| 121 | * |
| 122 | * @since 1.6.1 |
| 123 | * |
| 124 | * @param bool $allow True or false. |
| 125 | */ |
| 126 | if ( ! apply_filters( 'wpforms_create_upload_dir_htaccess_file', true ) ) { |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | $htaccess_file = File::get_upload_dir() . '.htaccess'; |
| 131 | $cache_key = 'upload_htaccess_file'; |
| 132 | |
| 133 | if ( File::is_file_updated( $htaccess_file, $cache_key ) ) { |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | if ( ! function_exists( 'insert_with_markers' ) ) { |
| 138 | require_once ABSPATH . 'wp-admin/includes/misc.php'; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Filters upload dir .htaccess file content. |
| 143 | * |
| 144 | * @since 1.6.1 |
| 145 | * |
| 146 | * @param bool $allow True or false. |
| 147 | */ |
| 148 | $contents = apply_filters( |
| 149 | 'wpforms_create_upload_dir_htaccess_file_content', |
| 150 | '# Disable PHP and Python scripts parsing. |
| 151 | <Files *> |
| 152 | SetHandler none |
| 153 | SetHandler default-handler |
| 154 | RemoveHandler .cgi .php .php3 .php4 .php5 .phtml .pl .py .pyc .pyo |
| 155 | RemoveType .cgi .php .php3 .php4 .php5 .phtml .pl .py .pyc .pyo |
| 156 | </Files> |
| 157 | <IfModule mod_php5.c> |
| 158 | php_flag engine off |
| 159 | </IfModule> |
| 160 | <IfModule mod_php7.c> |
| 161 | php_flag engine off |
| 162 | </IfModule> |
| 163 | <IfModule mod_php8.c> |
| 164 | php_flag engine off |
| 165 | </IfModule> |
| 166 | <IfModule headers_module> |
| 167 | Header set X-Robots-Tag "noindex" |
| 168 | </IfModule>' |
| 169 | ); |
| 170 | |
| 171 | $created = insert_with_markers( $htaccess_file, 'WPForms', $contents ); |
| 172 | |
| 173 | if ( $created ) { |
| 174 | File::save_file_updated_stat( $htaccess_file, $cache_key ); |
| 175 | } |
| 176 | |
| 177 | return $created; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Create .htaccess file in the WPForms cache directory. |
| 182 | * |
| 183 | * @since 1.8.7 |
| 184 | * |
| 185 | * @return bool True when the .htaccess file exists, false on failure. |
| 186 | */ |
| 187 | function wpforms_create_cache_dir_htaccess_file(): bool { |
| 188 | |
| 189 | /** |
| 190 | * Whether to create cache dir .htaccess file. |
| 191 | * |
| 192 | * @since 1.8.7 |
| 193 | * |
| 194 | * @param bool $allow True or false. |
| 195 | */ |
| 196 | if ( ! apply_filters( 'wpforms_create_cache_dir_htaccess_file', true ) ) { |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | $htaccess_file = File::get_cache_dir() . '.htaccess'; |
| 201 | |
| 202 | if ( File::is_file_updated( $htaccess_file, 'cache_htaccess_file' ) ) { |
| 203 | return true; |
| 204 | } |
| 205 | |
| 206 | if ( ! function_exists( 'insert_with_markers' ) ) { |
| 207 | require_once ABSPATH . 'wp-admin/includes/misc.php'; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Filters cache dir .htaccess file content. |
| 212 | * |
| 213 | * @since 1.8.7 |
| 214 | * |
| 215 | * @param bool $allow True or false. |
| 216 | */ |
| 217 | $contents = apply_filters( |
| 218 | 'wpforms_create_cache_dir_htaccess_file_content', |
| 219 | '# Disable access for any file in the cache dir. |
| 220 | # Apache 2.2 |
| 221 | <IfModule !authz_core_module> |
| 222 | Deny from all |
| 223 | </IfModule> |
| 224 | |
| 225 | # Apache 2.4+ |
| 226 | <IfModule authz_core_module> |
| 227 | Require all denied |
| 228 | </IfModule>' |
| 229 | ); |
| 230 | |
| 231 | $created = insert_with_markers( $htaccess_file, 'WPForms', $contents ); |
| 232 | |
| 233 | if ( $created ) { |
| 234 | File::save_file_updated_stat( $htaccess_file ); |
| 235 | } |
| 236 | |
| 237 | return $created; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Convert a file size provided, such as "2M", to bytes. |
| 242 | * |
| 243 | * @link http://stackoverflow.com/a/22500394 |
| 244 | * |
| 245 | * @since 1.0.0 |
| 246 | * |
| 247 | * @param string $size File size. |
| 248 | * |
| 249 | * @return int |
| 250 | */ |
| 251 | function wpforms_size_to_bytes( $size ) { |
| 252 | |
| 253 | if ( is_numeric( $size ) ) { |
| 254 | return $size; |
| 255 | } |
| 256 | |
| 257 | $suffix = substr( $size, - 1 ); |
| 258 | $value = substr( $size, 0, - 1 ); |
| 259 | |
| 260 | switch ( strtoupper( $suffix ) ) { |
| 261 | case 'P': |
| 262 | $value *= 1024; |
| 263 | |
| 264 | case 'T': |
| 265 | $value *= 1024; |
| 266 | |
| 267 | case 'G': |
| 268 | $value *= 1024; |
| 269 | |
| 270 | case 'M': |
| 271 | $value *= 1024; |
| 272 | |
| 273 | case 'K': |
| 274 | $value *= 1024; |
| 275 | break; |
| 276 | } |
| 277 | |
| 278 | return $value; |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Convert a file size provided, such as "2M", to bytes. |
| 283 | * |
| 284 | * @link http://stackoverflow.com/a/22500394 |
| 285 | * |
| 286 | * @since 1.0.0 |
| 287 | * |
| 288 | * @param bool $bytes Whether the value should be in bytes or formatted. |
| 289 | * |
| 290 | * @return false|string|int |
| 291 | */ |
| 292 | function wpforms_max_upload( $bytes = false ) { |
| 293 | |
| 294 | $max = wp_max_upload_size(); |
| 295 | |
| 296 | if ( $bytes ) { |
| 297 | return $max; |
| 298 | } |
| 299 | |
| 300 | return size_format( $max ); |
| 301 | } |
| 302 |