uploads.php
7.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class NF_FU_Admin_Controllers_Uploads {
/**
* @var string
*/
protected static $base_dir;
/**
* @var string
*/
protected static $base_url;
/**
* @var string
*/
protected static $tmp_dir;
/**
* NF_FU_Admin_Controllers_Uploads constructor.
*/
public function __construct() {
add_action( 'ninja_forms_after_submission', array( $this, 'remove_files_from_server') );
add_action( 'ninja_forms_after_submission', array( $this, 'record_submission_id'), 11 );
}
/**
* Remove the file from the server if the field is not to be saved to the server.
* This happens on the latest hook possible after actions have been run.
*
* @param array $data
*/
public function remove_files_from_server( $data ) {
if ( ! isset( $data['fields'] ) || ! is_array( $data['fields'] ) ) {
return;
}
foreach( $data['fields'] as $field ) {
if ( NF_FU_File_Uploads::TYPE !== $field['type'] ) {
continue;
}
if ( ! isset( $field['save_to_server'] ) ) {
continue;
}
if ( "1" == $field['save_to_server'] ) {
continue;
}
if ( empty( $field['value'] ) ) {
continue;
}
foreach ( $field['value'] as $upload_id => $url ) {
$upload = $this->get( $upload_id );
$file_path = $upload->file_path;
if ( $this->is_uploading_to_external( $upload ) ) {
// File being uploaded to external services in the background
// Do not delete, the background jobs will take care of deleting the local file
continue;
}
if ( ! file_exists( $file_path ) ) {
continue;
}
// Delete local file
$result = unlink( $file_path );
if ( $result ) {
$upload_data = $upload->data;
$upload_data['removed_from_server'] = true;
NF_File_Uploads()->model->update( $upload_id, $upload_data );
}
}
}
}
/**
* Check the file is being uploaded to an external service
*
* @param $upload
*
* @return bool
*/
protected function is_uploading_to_external( $upload ) {
if ( ! isset( $upload->external_locations ) || ! is_array( $upload->external_locations ) ) {
return false;
}
foreach ( $upload->external_locations as $location => $uploaded ) {
if ( 0 == $uploaded ) {
return true;
}
}
return false;
}
/**
* Get the base upload directory
*
* @return string
*/
public function get_base_dir() {
if ( is_null( self::$base_dir ) ) {
$base_upload_dir = wp_upload_dir();
$base_upload_dir = $base_upload_dir['basedir'] . '/ninja-forms';
wp_mkdir_p( $base_upload_dir );
self::$base_dir = $base_upload_dir;
}
return self::$base_dir;
}
/**
* Get the URL base upload directory
*
* @return string
*/
public function get_base_url() {
if ( is_null( self::$base_url ) ) {
$base_upload_url = wp_upload_dir();
$base_upload_url = $base_upload_url['baseurl'] . '/ninja-forms';
self::$base_url = $base_upload_url;
}
return self::$base_url;
}
/**
* Get the temp upload directory
*
* @return string
*/
public function get_temp_dir() {
if ( is_null( self::$tmp_dir ) ) {
$base_upload_dir = $this->get_base_dir();
$tmp_upload_dir = $base_upload_dir . '/tmp';
$tmp_upload_dir = apply_filters( 'ninja_forms_uploads_tmp_dir', $tmp_upload_dir );
wp_mkdir_p( $tmp_upload_dir );
$this->maybe_create_tmp_htaccess( $tmp_upload_dir );
self::$tmp_dir = $tmp_upload_dir;
}
return self::$tmp_dir;
}
/**
* Copy .htaccess file to tmp directory for security
* https://github.com/blueimp/jQuery-File-Upload/wiki/Security#php
*
* @param string $tmp_upload_dir
*/
protected function maybe_create_tmp_htaccess( $tmp_upload_dir ) {
$dest = $tmp_upload_dir . '/.htaccess';
if ( file_exists( $dest ) ) {
return;
}
$source = dirname( NF_File_Uploads()->plugin_file_path ) . '/includes/.htaccess.txt';
@copy( $source, $dest );
}
/**
* Get the file path for the temp file
*
* @param string $filename
* @param bool $temp Use temp path
*
* @return string
*/
public function get_path( $filename = '', $temp = false ) {
$file_path = $temp ? $this->get_temp_dir() : $this->get_base_dir();
$field_id = isset( $this->field_id ) ? $this->field_id : null;
$file_path = apply_filters( 'ninja_forms_uploads_dir', $file_path, $field_id );
return trailingslashit( $file_path ) . $filename;
}
/**
* Get the URL of a file
*
* @param string $filename
*
* @return string
*/
public function get_url( $filename ) {
$field_id = isset( $this->field_id ) ? $this->field_id : null;
$file_url = apply_filters( 'ninja_forms_uploads_url', $this->get_base_url(), $field_id );
return trailingslashit( $file_url ) . $filename;
}
/**
* Get a file upload from the table
*
* @param int $id
*
* @return object|false
*/
public function get( $id ) {
$upload = NF_File_Uploads()->model->get( $id );
if ( is_null( $upload ) ) {
return false;
}
$data = unserialize( $upload->data );
foreach ( $data as $key => $value ) {
$upload->$key = $value;
}
$upload->data = $data;
return $upload;
}
/**
* Get the file URL for an upload
*
* @param string $url
* @param array $data
*
* @return string
*/
public function get_file_url( $url, $data ) {
return apply_filters( 'ninja_forms_uploads_file_url', $url, $data );
}
/**
* Create attachment in media library from the file
*
* @param string $file
* @param null|string $file_name
*
* @return array
*/
public function create_attachment( $file, $file_name = null ) {
if ( is_null( $file_name ) ) {
$file_name = basename( $file );
}
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
$wp_filetype = wp_check_filetype( $file_name );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', $file_name ),
'post_content' => '',
'post_status' => 'inherit',
);
$attach_id = wp_insert_attachment( $attachment, $file );
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
$attach_data['ninja_forms_upload_field'] = true;
wp_update_attachment_metadata( $attach_id, $attach_data );
return $attach_id;
}
/**
* Check if the uploaded file exists.
*
* @param $upload_data
*
* @return bool
*/
public function file_exists( $upload_data ) {
if ( 'server' !== $upload_data['upload_location'] ) {
// For now assume external files still exist
return true;
}
if ( isset( $upload_data['removed_from_server'] ) && $upload_data['removed_from_server'] ) {
return false;
}
return file_exists( $upload_data['file_path']);
}
/**
* Save the submission ID for each upload object.
*
* @param array $data
*/
public function record_submission_id( $data ) {
if ( ! isset( $data['actions']['save']['sub_id'] ) ) {
return;
}
if ( ! isset( $data['fields'] ) || ! is_array( $data['fields'] ) ) {
return;
}
$sub_id = $data['actions']['save']['sub_id'];
foreach ( $data['fields'] as $field ) {
if ( NF_FU_File_Uploads::TYPE !== $field['type'] ) {
continue;
}
if ( empty( $field['value'] ) ) {
continue;
}
foreach ( $field['value'] as $upload_id => $url ) {
$upload = $this->get( $upload_id );
$upload_data = $upload->data;
$upload_data['sub_id'] = $sub_id;
NF_File_Uploads()->model->update( $upload_id, $upload_data );
}
}
}
/**
* Get all uploads for a specific field and submission
*
* @param int $field_id
* @param int $submission_id
*
* @return array
*/
public function get_field_uploads_by_submission_id( $field_id, $submission_id ) {
global $wpdb;
$where = $wpdb->prepare( 'WHERE field_id = %1$d AND data like \'%%%"sub_id";i:%2$d%%%\'', $field_id, $submission_id );
return NF_File_Uploads()->model->fetch( $where );
}
}