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