| 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 apply_filters( |
| 48 |
'jet-form-builder/file-upload/upload-dir', |
| 49 |
$pathdata, |
| 50 |
jet_fb_context()->get_request() |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
public static function apply_temp_dir( $pathdata ) { |
| 55 |
// jet-form-builder |
| 56 |
$base = static::upload_base(); |
| 57 |
|
| 58 |
$pathdata['subdir'] = '/' . $base . '/temp'; |
| 59 |
$pathdata['path'] = $pathdata['path'] . $pathdata['subdir']; |
| 60 |
$pathdata['url'] = $pathdata['url'] . $pathdata['subdir']; |
| 61 |
|
| 62 |
return $pathdata; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Returns upload subdirectory |
| 67 |
* |
| 68 |
* @return [type] [description] |
| 69 |
*/ |
| 70 |
public static function get_upload_dir() { |
| 71 |
$user_id = get_current_user_id(); |
| 72 |
$user_dir_name = md5( $user_id . Live_Form::instance()->form_id ); |
| 73 |
|
| 74 |
return apply_filters( 'jet-form-builder/file-upload/user-dir-name', $user_dir_name ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Returns upload base directory |
| 79 |
* |
| 80 |
* @return [type] [description] |
| 81 |
*/ |
| 82 |
public static function upload_base() { |
| 83 |
return apply_filters( 'jet-form-builder/file-upload/dir', 'jet-form-builder' ); |
| 84 |
} |
| 85 |
|
| 86 |
public static function create_index( $path ) { |
| 87 |
if ( ! is_dir( $path ) ) { |
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
$path = trailingslashit( $path ) . 'index.html'; |
| 92 |
$index = wp_normalize_path( $path ); |
| 93 |
|
| 94 |
if ( file_exists( $index ) ) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
// phpcs:ignore WordPress.WP.AlternativeFunctions |
| 99 |
return file_put_contents( $index, '' ); |
| 100 |
} |
| 101 |
|
| 102 |
public static function create_htaccess( $path ) { |
| 103 |
if ( ! is_dir( $path ) ) { |
| 104 |
return false; |
| 105 |
} |
| 106 |
|
| 107 |
$content = apply_filters( |
| 108 |
'jet-form-builder/file-upload/htaccess-content', |
| 109 |
self::htaccess_content() |
| 110 |
); |
| 111 |
|
| 112 |
if ( ! $content ) { |
| 113 |
return false; |
| 114 |
} |
| 115 |
|
| 116 |
$path = trailingslashit( $path ) . '.htaccess'; |
| 117 |
$index = wp_normalize_path( $path ); |
| 118 |
|
| 119 |
if ( file_exists( $index ) ) { |
| 120 |
return false; |
| 121 |
} |
| 122 |
|
| 123 |
// phpcs:ignore WordPress.WP.AlternativeFunctions |
| 124 |
return file_put_contents( $index, $content ); |
| 125 |
} |
| 126 |
|
| 127 |
protected static function htaccess_content(): string { |
| 128 |
return ' |
| 129 |
# Disable directory browsing |
| 130 |
Options -Indexes |
| 131 |
|
| 132 |
# Hide the contents of directories |
| 133 |
IndexIgnore * |
| 134 |
|
| 135 |
<IfModule mod_headers.c> |
| 136 |
Header set X-Robots-Tag "noindex, nofollow" |
| 137 |
</IfModule> |
| 138 |
'; |
| 139 |
} |
| 140 |
|
| 141 |
} |
| 142 |
|