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