file-collection.php
2 years ago
file-tools.php
2 years ago
file.php
2 years ago
has-error-file.php
2 years ago
media-block-value.php
2 years ago
sanitize-file-exception.php
2 years ago
upload-dir.php
2 years ago
upload-exception.php
2 years ago
upload-permission-exception.php
2 years ago
uploaded-collection.php
2 years ago
uploaded-file-path.php
2 years ago
uploaded-file.php
2 years ago
file.php
194 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Classes\Resources; |
| 5 | |
| 6 | use Jet_Form_Builder\Classes\Arrayable\Arrayable; |
| 7 | |
| 8 | // If this file is called directly, abort. |
| 9 | if ( ! defined( 'WPINC' ) ) { |
| 10 | die; |
| 11 | } |
| 12 | |
| 13 | class File implements Arrayable, Media_Block_Value, Has_Error_File { |
| 14 | |
| 15 | protected $error = 0; |
| 16 | protected $size = 0; |
| 17 | protected $name = ''; |
| 18 | protected $tmp_name = ''; |
| 19 | protected $type = ''; |
| 20 | |
| 21 | /** |
| 22 | * File constructor. |
| 23 | * |
| 24 | * @param array $file |
| 25 | * |
| 26 | * @throws Sanitize_File_Exception |
| 27 | */ |
| 28 | public function __construct( array $file ) { |
| 29 | $this->set_error( $file['error'] ); |
| 30 | $this->set_size( $file['size'] ); |
| 31 | $this->set_name( $file['name'] ); |
| 32 | $this->set_tmp_name( $file['tmp_name'] ); |
| 33 | $this->set_type( $file['type'] ); |
| 34 | |
| 35 | $this->sanitize_filename(); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @param mixed $error |
| 40 | * |
| 41 | * @return File |
| 42 | */ |
| 43 | public function set_error( $error ): File { |
| 44 | $this->error = absint( $error ); |
| 45 | |
| 46 | return $this; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @param mixed $name |
| 51 | * |
| 52 | * @return File |
| 53 | */ |
| 54 | public function set_name( $name ): File { |
| 55 | $this->name = sanitize_file_name( $name ); |
| 56 | |
| 57 | return $this; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @param mixed $size |
| 62 | * |
| 63 | * @return File |
| 64 | */ |
| 65 | public function set_size( $size ): File { |
| 66 | $this->size = absint( $size ); |
| 67 | |
| 68 | return $this; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @param mixed $tmp_name |
| 73 | * |
| 74 | * @return File |
| 75 | */ |
| 76 | public function set_tmp_name( $tmp_name ): File { |
| 77 | $this->tmp_name = $tmp_name; |
| 78 | |
| 79 | return $this; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @param mixed $type |
| 84 | * |
| 85 | * @return File |
| 86 | */ |
| 87 | public function set_type( $type ): File { |
| 88 | $this->type = sanitize_mime_type( $type ); |
| 89 | |
| 90 | return $this; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @return string |
| 95 | */ |
| 96 | public function get_type(): string { |
| 97 | return $this->type; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @return int |
| 102 | */ |
| 103 | public function get_error(): int { |
| 104 | return $this->error; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @return string |
| 109 | */ |
| 110 | public function get_name(): string { |
| 111 | return $this->name; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @return int |
| 116 | */ |
| 117 | public function get_size(): int { |
| 118 | return $this->size; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @return string |
| 123 | */ |
| 124 | public function get_tmp_name(): string { |
| 125 | return $this->tmp_name; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @throws Sanitize_File_Exception |
| 130 | */ |
| 131 | protected function sanitize_filename() { |
| 132 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 133 | |
| 134 | $validate = wp_check_filetype_and_ext( $this->tmp_name, $this->name ); |
| 135 | |
| 136 | $ext = $validate['ext'] ?? false; |
| 137 | $type = $validate['type'] ?? false; |
| 138 | $proper_filename = $validate['proper_filename'] ?? false; |
| 139 | |
| 140 | if ( $proper_filename ) { |
| 141 | $this->name = $proper_filename; |
| 142 | } |
| 143 | |
| 144 | if ( ( ! $type || ! $ext ) && ! current_user_can( 'unfiltered_upload' ) ) { |
| 145 | throw new Sanitize_File_Exception( 'Incorrect extension or mime type' ); |
| 146 | } |
| 147 | |
| 148 | $this->type = $type; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * @return array |
| 153 | */ |
| 154 | public function to_array(): array { |
| 155 | return array( |
| 156 | 'name' => $this->get_name(), |
| 157 | 'type' => $this->get_type(), |
| 158 | 'size' => $this->get_size(), |
| 159 | 'error' => $this->get_error(), |
| 160 | 'tmp_name' => $this->get_tmp_name(), |
| 161 | ); |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | * Realisation of |
| 166 | * \Jet_Form_Builder\Classes\Resources\Media_Block_Value |
| 167 | */ |
| 168 | |
| 169 | /** |
| 170 | * @return string |
| 171 | */ |
| 172 | public function get_attachment_id(): string { |
| 173 | return ''; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * @return string |
| 178 | */ |
| 179 | public function get_attachment_url(): string { |
| 180 | return $this->get_tmp_name(); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * @return array |
| 185 | */ |
| 186 | public function get_attachment_both(): array { |
| 187 | return array(); |
| 188 | } |
| 189 | |
| 190 | public function has_error(): bool { |
| 191 | return 0 < $this->get_error(); |
| 192 | } |
| 193 | } |
| 194 |