file-collection.php
11 months ago
file-tools.php
3 months ago
file.php
11 months ago
has-error-file.php
2 years ago
media-block-value.php
11 months ago
sanitize-file-exception.php
2 years ago
upload-dir.php
1 year ago
upload-exception.php
2 years ago
upload-permission-exception.php
2 years ago
uploaded-collection.php
11 months ago
uploaded-file-path.php
2 years ago
uploaded-file.php
2 months ago
upload-dir.php
138 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Classes\Resources; |
| 5 | |
| 6 | use Jet_Form_Builder\Live_Form; |
| 7 | |
| 8 | // If this file is called directly, abort. |
| 9 | if ( ! defined( 'WPINC' ) ) { |
| 10 | die; |
| 11 | } |
| 12 | |
| 13 | class Upload_Dir { |
| 14 | |
| 15 | /** |
| 16 | * Change upload directory for JetEngine uploads |
| 17 | * |
| 18 | * @param [type] $pathdata [description] |
| 19 | * |
| 20 | * @return [type] [description] |
| 21 | */ |
| 22 | public static function apply_upload_dir( $pathdata ) { |
| 23 | // jet-form-builder |
| 24 | $base = static::upload_base(); |
| 25 | |
| 26 | // user-based dynamic dirname |
| 27 | $dir = static::get_upload_dir(); |
| 28 | |
| 29 | if ( empty( $pathdata['subdir'] ) ) { |
| 30 | $path = $pathdata['path'] . '/' . $base; |
| 31 | |
| 32 | $pathdata['subdir'] = '/' . $base . '/' . $dir; |
| 33 | $pathdata['path'] = $pathdata['path'] . $pathdata['subdir']; |
| 34 | $pathdata['url'] = $pathdata['url'] . $pathdata['subdir']; |
| 35 | } else { |
| 36 | $path = $pathdata['basedir'] . '/' . $base; |
| 37 | |
| 38 | $pathdata['subdir'] = '/' . $base . '/' . $dir . $pathdata['subdir']; |
| 39 | $pathdata['path'] = $pathdata['basedir'] . $pathdata['subdir']; |
| 40 | $pathdata['url'] = $pathdata['baseurl'] . $pathdata['subdir']; |
| 41 | } |
| 42 | self::create_index( $path ); |
| 43 | self::create_index( $pathdata['path'] ); |
| 44 | |
| 45 | self::create_htaccess( $path ); |
| 46 | |
| 47 | return $pathdata; |
| 48 | } |
| 49 | |
| 50 | public static function apply_temp_dir( $pathdata ) { |
| 51 | // jet-form-builder |
| 52 | $base = static::upload_base(); |
| 53 | |
| 54 | $pathdata['subdir'] = '/' . $base . '/temp'; |
| 55 | $pathdata['path'] = $pathdata['path'] . $pathdata['subdir']; |
| 56 | $pathdata['url'] = $pathdata['url'] . $pathdata['subdir']; |
| 57 | |
| 58 | return $pathdata; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Returns upload subdirectory |
| 63 | * |
| 64 | * @return [type] [description] |
| 65 | */ |
| 66 | public static function get_upload_dir() { |
| 67 | $user_id = get_current_user_id(); |
| 68 | $user_dir_name = md5( $user_id . Live_Form::instance()->form_id ); |
| 69 | |
| 70 | return apply_filters( 'jet-form-builder/file-upload/user-dir-name', $user_dir_name ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Returns upload base directory |
| 75 | * |
| 76 | * @return [type] [description] |
| 77 | */ |
| 78 | public static function upload_base() { |
| 79 | return apply_filters( 'jet-form-builder/file-upload/dir', 'jet-form-builder' ); |
| 80 | } |
| 81 | |
| 82 | public static function create_index( $path ) { |
| 83 | if ( ! is_dir( $path ) ) { |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | $path = trailingslashit( $path ) . 'index.html'; |
| 88 | $index = wp_normalize_path( $path ); |
| 89 | |
| 90 | if ( file_exists( $index ) ) { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | // phpcs:ignore WordPress.WP.AlternativeFunctions |
| 95 | return file_put_contents( $index, '' ); |
| 96 | } |
| 97 | |
| 98 | public static function create_htaccess( $path ) { |
| 99 | if ( ! is_dir( $path ) ) { |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | $content = apply_filters( |
| 104 | 'jet-form-builder/file-upload/htaccess-content', |
| 105 | self::htaccess_content() |
| 106 | ); |
| 107 | |
| 108 | if ( ! $content ) { |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | $path = trailingslashit( $path ) . '.htaccess'; |
| 113 | $index = wp_normalize_path( $path ); |
| 114 | |
| 115 | if ( file_exists( $index ) ) { |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | // phpcs:ignore WordPress.WP.AlternativeFunctions |
| 120 | return file_put_contents( $index, $content ); |
| 121 | } |
| 122 | |
| 123 | protected static function htaccess_content(): string { |
| 124 | return ' |
| 125 | # Disable directory browsing |
| 126 | Options -Indexes |
| 127 | |
| 128 | # Hide the contents of directories |
| 129 | IndexIgnore * |
| 130 | |
| 131 | <IfModule mod_headers.c> |
| 132 | Header set X-Robots-Tag "noindex, nofollow" |
| 133 | </IfModule> |
| 134 | '; |
| 135 | } |
| 136 | |
| 137 | } |
| 138 |