1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Webform module file component.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements _webform_defaults_component().
|
10
|
*/
|
11
|
function _webform_defaults_file() {
|
12
|
// If private file storage is enabled, make it the default for security
|
13
|
// reasons. See: https://www.drupal.org/psa-2016-003
|
14
|
$available_schemes = file_get_stream_wrappers(STREAM_WRAPPERS_WRITE_VISIBLE);
|
15
|
$scheme = isset($available_schemes['private']) ? 'private' : 'public';
|
16
|
|
17
|
return array(
|
18
|
'name' => '',
|
19
|
'form_key' => NULL,
|
20
|
'required' => 0,
|
21
|
'pid' => 0,
|
22
|
'weight' => 0,
|
23
|
'extra' => array(
|
24
|
'filtering' => array(
|
25
|
'types' => array('gif', 'jpg', 'png'),
|
26
|
'addextensions' => '',
|
27
|
'size' => '2 MB',
|
28
|
),
|
29
|
'rename' => '',
|
30
|
'scheme' => $scheme,
|
31
|
'directory' => '',
|
32
|
'progress_indicator' => 'throbber',
|
33
|
'title_display' => 0,
|
34
|
'description' => '',
|
35
|
'description_above' => FALSE,
|
36
|
'attributes' => array(),
|
37
|
'private' => FALSE,
|
38
|
'analysis' => FALSE,
|
39
|
),
|
40
|
);
|
41
|
}
|
42
|
|
43
|
/**
|
44
|
* Implements _webform_theme_component().
|
45
|
*/
|
46
|
function _webform_theme_file() {
|
47
|
return array(
|
48
|
'webform_edit_file_extensions' => array(
|
49
|
'render element' => 'element',
|
50
|
'file' => 'components/file.inc',
|
51
|
),
|
52
|
'webform_display_file' => array(
|
53
|
'render element' => 'element',
|
54
|
'file' => 'components/file.inc',
|
55
|
),
|
56
|
'webform_managed_file' => array(
|
57
|
'render element' => 'element',
|
58
|
'file' => 'components/file.inc',
|
59
|
),
|
60
|
);
|
61
|
}
|
62
|
|
63
|
/**
|
64
|
* Implements _webform_edit_component().
|
65
|
*/
|
66
|
function _webform_edit_file($component) {
|
67
|
$form = array();
|
68
|
$form['#element_validate'] = array('_webform_edit_file_check_directory');
|
69
|
$form['#after_build'] = array('_webform_edit_file_check_directory');
|
70
|
|
71
|
$form['validation']['size'] = array(
|
72
|
'#type' => 'textfield',
|
73
|
'#title' => t('Max upload size'),
|
74
|
'#default_value' => $component['extra']['filtering']['size'],
|
75
|
'#description' => t('Enter the max file size a user may upload such as 2 MB or 800 KB. Your server has a max upload size of @size.', array('@size' => format_size(file_upload_max_size()))),
|
76
|
'#size' => 10,
|
77
|
'#parents' => array('extra', 'filtering', 'size'),
|
78
|
'#element_validate' => array('_webform_edit_file_size_validate'),
|
79
|
'#weight' => 1,
|
80
|
);
|
81
|
|
82
|
$form['validation']['extensions'] = array(
|
83
|
'#element_validate' => array('_webform_edit_file_extensions_validate'),
|
84
|
'#parents' => array('extra', 'filtering'),
|
85
|
'#theme' => 'webform_edit_file_extensions',
|
86
|
'#theme_wrappers' => array('form_element'),
|
87
|
'#title' => t('Allowed file extensions'),
|
88
|
'#attached' => array(
|
89
|
'js' => array(drupal_get_path('module', 'webform') . '/js/webform-admin.js'),
|
90
|
'css' => array(drupal_get_path('module', 'webform') . '/css/webform-admin.css'),
|
91
|
),
|
92
|
'#type' => 'webform_file_extensions',
|
93
|
'#weight' => 2,
|
94
|
);
|
95
|
|
96
|
// Find the list of all currently valid extensions.
|
97
|
$current_types = isset($component['extra']['filtering']['types']) ? $component['extra']['filtering']['types'] : array();
|
98
|
|
99
|
$types = array('gif', 'jpg', 'png');
|
100
|
$form['validation']['extensions']['types']['webimages'] = array(
|
101
|
'#type' => 'checkboxes',
|
102
|
'#title' => t('Web images'),
|
103
|
'#options' => drupal_map_assoc($types),
|
104
|
'#default_value' => array_intersect($current_types, $types),
|
105
|
);
|
106
|
|
107
|
$types = array('bmp', 'eps', 'tif', 'pict', 'psd');
|
108
|
$form['validation']['extensions']['types']['desktopimages'] = array(
|
109
|
'#type' => 'checkboxes',
|
110
|
'#title' => t('Desktop images'),
|
111
|
'#options' => drupal_map_assoc($types),
|
112
|
'#default_value' => array_intersect($current_types, $types),
|
113
|
);
|
114
|
|
115
|
$types = array('txt', 'rtf', 'html', 'pdf', 'doc', 'docx', 'odt', 'ppt', 'pptx', 'odp', 'xls', 'xlsx', 'ods', 'xml');
|
116
|
$form['validation']['extensions']['types']['documents'] = array(
|
117
|
'#type' => 'checkboxes',
|
118
|
'#title' => t('Documents'),
|
119
|
'#options' => drupal_map_assoc($types),
|
120
|
'#default_value' => array_intersect($current_types, $types),
|
121
|
);
|
122
|
|
123
|
$types = array('avi', 'mov', 'mp3', 'ogg', 'wav');
|
124
|
$form['validation']['extensions']['types']['media'] = array(
|
125
|
'#type' => 'checkboxes',
|
126
|
'#title' => t('Media'),
|
127
|
'#options' => drupal_map_assoc($types),
|
128
|
'#default_value' => array_intersect($current_types, $types),
|
129
|
);
|
130
|
|
131
|
$types = array('bz2', 'dmg', 'gz', 'jar', 'rar', 'sit', 'tar', 'zip');
|
132
|
$form['validation']['extensions']['types']['archives'] = array(
|
133
|
'#type' => 'checkboxes',
|
134
|
'#title' => t('Archives'),
|
135
|
'#options' => drupal_map_assoc($types),
|
136
|
'#default_value' => array_intersect($current_types, $types),
|
137
|
);
|
138
|
|
139
|
$form['validation']['extensions']['addextensions'] = array(
|
140
|
'#type' => 'textfield',
|
141
|
'#title' => t('Additional extensions'),
|
142
|
'#default_value' => $component['extra']['filtering']['addextensions'],
|
143
|
'#description' => t('Enter a list of additional file extensions for this upload field, separated by commas.<br /> Entered extensions will be appended to checked items above.'),
|
144
|
'#size' => 20,
|
145
|
'#weight' => 3,
|
146
|
);
|
147
|
|
148
|
$scheme_options = array();
|
149
|
foreach (file_get_stream_wrappers(STREAM_WRAPPERS_WRITE_VISIBLE) as $scheme => $stream_wrapper) {
|
150
|
$scheme_options[$scheme] = $stream_wrapper['name'];
|
151
|
}
|
152
|
$form['extra']['scheme'] = array(
|
153
|
'#type' => 'radios',
|
154
|
'#title' => t('Upload destination'),
|
155
|
'#options' => $scheme_options,
|
156
|
'#default_value' => $component['extra']['scheme'],
|
157
|
'#description' => t('Public files upload destination is dangerous for forms that are available to anonymous and/or untrusted users. For more information, see <a href="@psa">DRUPAL-PSA-2016-003</a>.', array('@psa' => 'https://www.drupal.org/psa-2016-003')),
|
158
|
'#weight' => 4,
|
159
|
'#access' => count($scheme_options) > 1,
|
160
|
);
|
161
|
$form['extra']['directory'] = array(
|
162
|
'#type' => 'textfield',
|
163
|
'#title' => t('Upload directory'),
|
164
|
'#default_value' => $component['extra']['directory'],
|
165
|
'#description' => t('You may optionally specify a sub-directory to store your files.') . ' ' . theme('webform_token_help'),
|
166
|
'#weight' => 5,
|
167
|
'#field_prefix' => 'webform/',
|
168
|
);
|
169
|
|
170
|
$form['extra']['rename'] = array(
|
171
|
'#type' => 'textfield',
|
172
|
'#title' => t('Rename files'),
|
173
|
'#default_value' => $component['extra']['rename'],
|
174
|
'#description' => t('You may optionally use tokens to create a pattern used to rename files upon submission. Omit the extension; it will be added automatically.') . ' ' . theme('webform_token_help', array('groups' => array('node', 'submission'))),
|
175
|
'#weight' => 6,
|
176
|
'#element_validate' => array('_webform_edit_file_rename_validate'),
|
177
|
'#access' => webform_variable_get('webform_token_access'),
|
178
|
);
|
179
|
|
180
|
$form['display']['progress_indicator'] = array(
|
181
|
'#type' => 'radios',
|
182
|
'#title' => t('Progress indicator'),
|
183
|
'#options' => array(
|
184
|
'throbber' => t('Throbber'),
|
185
|
'bar' => t('Bar with progress meter'),
|
186
|
),
|
187
|
'#default_value' => $component['extra']['progress_indicator'],
|
188
|
'#description' => t('The throbber display does not show the status of uploads but takes up less space. The progress bar is helpful for monitoring progress on large uploads.'),
|
189
|
'#weight' => 16,
|
190
|
'#access' => file_progress_implementation(),
|
191
|
'#parents' => array('extra', 'progress_indicator'),
|
192
|
);
|
193
|
|
194
|
// @todo: Make managed_file respect the "size" parameter.
|
195
|
/*
|
196
|
$form['display']['width'] = array(
|
197
|
'#type' => 'textfield',
|
198
|
'#title' => t('Width'),
|
199
|
'#default_value' => $component['extra']['width'],
|
200
|
'#description' => t('Width of the file field.') . ' ' .
|
201
|
t('Leaving blank will use the default size.'),
|
202
|
'#size' => 5,
|
203
|
'#maxlength' => 10,
|
204
|
'#weight' => 4,
|
205
|
'#parents' => array('extra', 'width')
|
206
|
);
|
207
|
*/
|
208
|
|
209
|
return $form;
|
210
|
}
|
211
|
|
212
|
/**
|
213
|
* Form API validator ensures rename string is empty or contains one token.
|
214
|
*
|
215
|
* A Form API element validate function to ensure that the rename string is
|
216
|
* either empty or contains at least one token.
|
217
|
*/
|
218
|
function _webform_edit_file_rename_validate($element, &$form_state, $form) {
|
219
|
$rename = trim($form_state['values']['extra']['rename']);
|
220
|
form_set_value($element, $rename, $form_state);
|
221
|
if (strlen($rename) && !count(token_scan($rename))) {
|
222
|
form_error($element, t('To create unique file names, use at least one token in the file name pattern.'));
|
223
|
}
|
224
|
}
|
225
|
|
226
|
/**
|
227
|
* A Form API element validate function to check filesize is valid.
|
228
|
*/
|
229
|
function _webform_edit_file_size_validate($element) {
|
230
|
if (!empty($element['#value'])) {
|
231
|
$set_filesize = parse_size($element['#value']);
|
232
|
if ($set_filesize == FALSE) {
|
233
|
form_error($element, t('File size @value is not a valid file size. Use a value such as 2 MB or 800 KB.', array('@value' => $element['#value'])));
|
234
|
}
|
235
|
else {
|
236
|
$max_filesize = parse_size(file_upload_max_size());
|
237
|
if ($max_filesize < $set_filesize) {
|
238
|
form_error($element, t('An upload size of @value is too large. You are allowed to upload files that are @max or less.', array('@value' => $element['#value'], '@max' => format_size($max_filesize))));
|
239
|
}
|
240
|
}
|
241
|
}
|
242
|
}
|
243
|
|
244
|
/**
|
245
|
* A Form API after build and validate function.
|
246
|
*
|
247
|
* Ensure that the destination directory exists and is writable.
|
248
|
*/
|
249
|
function _webform_edit_file_check_directory($element) {
|
250
|
$scheme = $element['extra']['scheme']['#value'];
|
251
|
$directory = $element['extra']['directory']['#value'];
|
252
|
|
253
|
$destination_dir = file_stream_wrapper_uri_normalize($scheme . '://webform/' . $directory);
|
254
|
$tokenized_dir = drupal_strtolower(webform_replace_tokens($destination_dir, $element['#node']));
|
255
|
|
256
|
// Sanity check input to prevent use parent (../) directories.
|
257
|
if (preg_match('/\.\.[\/\\\]/', $tokenized_dir . '/')) {
|
258
|
form_error($element['extra']['directory'], t('The save directory %directory is not valid.', array('%directory' => $tokenized_dir)));
|
259
|
}
|
260
|
else {
|
261
|
if (!file_prepare_directory($tokenized_dir, FILE_CREATE_DIRECTORY)) {
|
262
|
form_error($element['extra']['directory'], t('The save directory %directory could not be created. Check that the webform files directory is writable.', array('%directory' => $tokenized_dir)));
|
263
|
}
|
264
|
}
|
265
|
|
266
|
return $element;
|
267
|
}
|
268
|
|
269
|
/**
|
270
|
* A Form API element validate function.
|
271
|
*
|
272
|
* Change the submitted values of the component so that all filtering extensions
|
273
|
* are saved as a single array.
|
274
|
*/
|
275
|
function _webform_edit_file_extensions_validate($element, &$form_state) {
|
276
|
// Predefined types.
|
277
|
$extensions = array();
|
278
|
foreach (element_children($element['types']) as $category) {
|
279
|
foreach (array_keys($element['types'][$category]['#value']) as $extension) {
|
280
|
if ($element['types'][$category][$extension]['#value']) {
|
281
|
$extensions[] = $extension;
|
282
|
// "jpeg" is an exception. It is allowed anytime "jpg" is allowed.
|
283
|
if ($extension == 'jpg') {
|
284
|
$extensions[] = 'jpeg';
|
285
|
}
|
286
|
}
|
287
|
}
|
288
|
}
|
289
|
|
290
|
// Additional types.
|
291
|
$additional_extensions = explode(',', $element['addextensions']['#value']);
|
292
|
foreach ($additional_extensions as $extension) {
|
293
|
$clean_extension = drupal_strtolower(trim($extension));
|
294
|
if (!empty($clean_extension) && !in_array($clean_extension, $extensions)) {
|
295
|
$extensions[] = $clean_extension;
|
296
|
}
|
297
|
}
|
298
|
|
299
|
form_set_value($element['types'], $extensions, $form_state);
|
300
|
}
|
301
|
|
302
|
/**
|
303
|
* Output the list of allowed extensions as checkboxes.
|
304
|
*/
|
305
|
function theme_webform_edit_file_extensions($variables) {
|
306
|
$element = $variables['element'];
|
307
|
|
308
|
// Format the components into a table.
|
309
|
$rows = array();
|
310
|
foreach (element_children($element['types']) as $filtergroup) {
|
311
|
$row = array();
|
312
|
if ($element['types'][$filtergroup]['#type'] == 'checkboxes') {
|
313
|
$select_link = ' <a href="#" class="webform-select-link webform-select-link-' . $filtergroup . '">(' . t('select') . ')</a>';
|
314
|
$row[] = $element['types'][$filtergroup]['#title'];
|
315
|
$row[] = array('data' => $select_link, 'width' => 40);
|
316
|
$row[] = array('data' => drupal_render_children($element['types'][$filtergroup]), 'class' => array('webform-file-extensions', 'webform-select-group-' . $filtergroup));
|
317
|
$rows[] = array('data' => $row);
|
318
|
unset($element['types'][$filtergroup]);
|
319
|
}
|
320
|
}
|
321
|
|
322
|
// Add the row for additional types.
|
323
|
if (!isset($element['addextensions']['#access']) || $element['addextensions']['#access']) {
|
324
|
$row = array();
|
325
|
$title = $element['addextensions']['#title'];
|
326
|
$element['addextensions']['#title'] = NULL;
|
327
|
$row[] = array('data' => $title, 'colspan' => 2);
|
328
|
$row[] = drupal_render($element['addextensions']);
|
329
|
$rows[] = $row;
|
330
|
}
|
331
|
|
332
|
$header = array(array('data' => t('Category'), 'colspan' => '2'), array('data' => t('Types')));
|
333
|
|
334
|
// Create the table inside the form.
|
335
|
$element['types']['table'] = array(
|
336
|
'#theme' => 'table',
|
337
|
'#header' => $header,
|
338
|
'#rows' => $rows,
|
339
|
'#attributes' => array('class' => array('webform-file-extensions')),
|
340
|
);
|
341
|
|
342
|
return drupal_render_children($element);
|
343
|
}
|
344
|
|
345
|
/**
|
346
|
* Implements _webform_render_component().
|
347
|
*/
|
348
|
function _webform_render_file($component, $value = NULL, $filter = TRUE, $submission = NULL) {
|
349
|
$node = isset($component['nid']) ? node_load($component['nid']) : NULL;
|
350
|
|
351
|
// Cap the upload size according to the PHP limit.
|
352
|
$max_filesize = parse_size(file_upload_max_size());
|
353
|
$set_filesize = $component['extra']['filtering']['size'];
|
354
|
if (!empty($set_filesize) && parse_size($set_filesize) < $max_filesize) {
|
355
|
$max_filesize = parse_size($set_filesize);
|
356
|
}
|
357
|
|
358
|
$element = array(
|
359
|
'#type' => 'managed_file',
|
360
|
'#theme' => 'webform_managed_file',
|
361
|
'#title' => $filter ? webform_filter_xss($component['name']) : $component['name'],
|
362
|
'#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
|
363
|
'#required' => $component['required'],
|
364
|
'#default_value' => isset($value[0]) ? $value[0] : NULL,
|
365
|
'#attributes' => $component['extra']['attributes'],
|
366
|
'#upload_validators' => array(
|
367
|
'file_validate_size' => array($max_filesize),
|
368
|
'file_validate_extensions' => array(implode(' ', $component['extra']['filtering']['types'])),
|
369
|
),
|
370
|
'#pre_render' => array_merge(element_info_property('managed_file', '#pre_render'), array('webform_file_allow_access')),
|
371
|
'#upload_location' => $component['extra']['scheme'] . '://webform/' . ($filter
|
372
|
? drupal_strtolower(webform_replace_tokens($component['extra']['directory'], $node))
|
373
|
: $component['extra']['directory']),
|
374
|
'#progress_indicator' => $component['extra']['progress_indicator'],
|
375
|
'#description' => $filter ? webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
|
376
|
'#weight' => $component['weight'],
|
377
|
'#theme_wrappers' => array('webform_element'),
|
378
|
'#translatable' => array('title', 'description'),
|
379
|
);
|
380
|
|
381
|
if ($filter) {
|
382
|
$element['#description'] = theme('file_upload_help', array('description' => $element['#description'], 'upload_validators' => $element['#upload_validators']));
|
383
|
}
|
384
|
|
385
|
return $element;
|
386
|
}
|
387
|
|
388
|
/**
|
389
|
* Returns HTML for a webform managed file element.
|
390
|
*
|
391
|
* See #2495821 and #2497909. The core theme_file_managed_file creates a
|
392
|
* wrapper around the element with the element's id, thereby creating 2 elements
|
393
|
* with the same id.
|
394
|
*
|
395
|
* @param $variables
|
396
|
* An associative array containing:
|
397
|
* - element: A render element representing the file.
|
398
|
*
|
399
|
* @return string
|
400
|
* The HTML.
|
401
|
*/
|
402
|
function theme_webform_managed_file($variables) {
|
403
|
$element = $variables['element'];
|
404
|
|
405
|
$attributes = array();
|
406
|
|
407
|
// For webform use, do not add the id to the wrapper.
|
408
|
// @code
|
409
|
// if (isset($element['#id'])) {
|
410
|
// $attributes['id'] = $element['#id'];
|
411
|
// }
|
412
|
// @endcode
|
413
|
if (!empty($element['#attributes']['class'])) {
|
414
|
$attributes['class'] = (array) $element['#attributes']['class'];
|
415
|
}
|
416
|
$attributes['class'][] = 'form-managed-file';
|
417
|
|
418
|
// This wrapper is required to apply JS behaviors and CSS styling.
|
419
|
$output = '';
|
420
|
$output .= '<div' . drupal_attributes($attributes) . '>';
|
421
|
$output .= drupal_render_children($element);
|
422
|
$output .= '</div>';
|
423
|
return $output;
|
424
|
}
|
425
|
|
426
|
/**
|
427
|
* Implements _webform_submit_component().
|
428
|
*/
|
429
|
function _webform_submit_file($component, $value) {
|
430
|
$fid = is_array($value)
|
431
|
? (!empty($value['fid']) ? $value['fid'] : '')
|
432
|
: (!empty($value) ? $value : '');
|
433
|
// Extend access to this file, even if the submission has not been saved yet.
|
434
|
// This may happen when previewing a private file which was selected but not
|
435
|
// explicitly uploaded, and then previewed.
|
436
|
if ($fid) {
|
437
|
$_SESSION['webform_files'][$fid] = $fid;
|
438
|
}
|
439
|
return $fid;
|
440
|
}
|
441
|
|
442
|
/**
|
443
|
* Pre-render callback to allow access to uploaded files.
|
444
|
*
|
445
|
* Files that have not yet been saved into a submission must be accessible to
|
446
|
* the user who uploaded it, but no one else. After the submission is saved,
|
447
|
* access is granted through the file_usage table. Before then, we use a
|
448
|
* $_SESSION value to record a user's upload.
|
449
|
*
|
450
|
* @see webform_file_download()
|
451
|
*/
|
452
|
function webform_file_allow_access($element) {
|
453
|
if (!empty($element['#value']['fid'])) {
|
454
|
$fid = $element['#value']['fid'];
|
455
|
$_SESSION['webform_files'][$fid] = $fid;
|
456
|
}
|
457
|
|
458
|
return $element;
|
459
|
}
|
460
|
|
461
|
/**
|
462
|
* Implements _webform_display_component().
|
463
|
*/
|
464
|
function _webform_display_file($component, $value, $format = 'html', $submission = array()) {
|
465
|
$fid = isset($value[0]) ? $value[0] : NULL;
|
466
|
return array(
|
467
|
'#title' => $component['name'],
|
468
|
'#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
|
469
|
'#value' => $fid ? webform_get_file($fid) : NULL,
|
470
|
'#weight' => $component['weight'],
|
471
|
'#theme' => 'webform_display_file',
|
472
|
'#theme_wrappers' => $format == 'text' ? array('webform_element_text') : array('webform_element'),
|
473
|
'#format' => $format,
|
474
|
'#translatable' => array('title'),
|
475
|
);
|
476
|
}
|
477
|
|
478
|
/**
|
479
|
* Format the output of text data for this component.
|
480
|
*/
|
481
|
function theme_webform_display_file($variables) {
|
482
|
$element = $variables['element'];
|
483
|
|
484
|
$file = $element['#value'];
|
485
|
$url = !empty($file) ? webform_file_url($file->uri) : t('no upload');
|
486
|
return !empty($file) ? ($element['#format'] == 'text' ? $url : l($file->filename, $url)) : ' ';
|
487
|
}
|
488
|
|
489
|
/**
|
490
|
* Implements _webform_delete_component().
|
491
|
*/
|
492
|
function _webform_delete_file($component, $value) {
|
493
|
// Delete an individual submission file.
|
494
|
if (!empty($value[0]) && ($file = webform_get_file($value[0]))) {
|
495
|
file_usage_delete($file, 'webform');
|
496
|
file_delete($file);
|
497
|
}
|
498
|
}
|
499
|
|
500
|
/**
|
501
|
* Implements _webform_attachments_component().
|
502
|
*/
|
503
|
function _webform_attachments_file($component, $value) {
|
504
|
$file = (array) webform_get_file($value[0]);
|
505
|
$files = array($file);
|
506
|
return $files;
|
507
|
}
|
508
|
|
509
|
/**
|
510
|
* Implements _webform_analysis_component().
|
511
|
*/
|
512
|
function _webform_analysis_file($component, $sids = array(), $single = FALSE, $join = NULL) {
|
513
|
$query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC))
|
514
|
->fields('wsd', array('no', 'data'))
|
515
|
->condition('wsd.nid', $component['nid'])
|
516
|
->condition('wsd.cid', $component['cid']);
|
517
|
|
518
|
if (count($sids)) {
|
519
|
$query->condition('wsd.sid', $sids, 'IN');
|
520
|
}
|
521
|
|
522
|
if ($join) {
|
523
|
$query->innerJoin($join, 'ws2_', 'wsd.sid = ws2_.sid');
|
524
|
}
|
525
|
|
526
|
$nonblanks = 0;
|
527
|
$sizetotal = 0;
|
528
|
$submissions = 0;
|
529
|
|
530
|
$result = $query->execute();
|
531
|
foreach ($result as $data) {
|
532
|
$file = webform_get_file($data['data']);
|
533
|
if (isset($file->filesize)) {
|
534
|
$nonblanks++;
|
535
|
$sizetotal += $file->filesize;
|
536
|
}
|
537
|
$submissions++;
|
538
|
}
|
539
|
|
540
|
$rows[0] = array(t('Left Blank'), ($submissions - $nonblanks));
|
541
|
$rows[1] = array(t('User uploaded file'), $nonblanks);
|
542
|
$other[0] = array(t('Average uploaded file size'), ($sizetotal != 0 ? (int) (($sizetotal / $nonblanks) / 1024) . ' KB' : '0'));
|
543
|
return array(
|
544
|
'table_rows' => $rows,
|
545
|
'other_data' => $other,
|
546
|
);
|
547
|
}
|
548
|
|
549
|
/**
|
550
|
* Implements _webform_table_component().
|
551
|
*/
|
552
|
function _webform_table_file($component, $value) {
|
553
|
$output = '';
|
554
|
$file = webform_get_file($value[0]);
|
555
|
if (!empty($file->fid)) {
|
556
|
$output = '<a href="' . webform_file_url($file->uri) . '">' . check_plain(webform_file_name($file->uri)) . '</a>';
|
557
|
$output .= ' (' . (int) ($file->filesize / 1024) . ' KB)';
|
558
|
}
|
559
|
return $output;
|
560
|
}
|
561
|
|
562
|
/**
|
563
|
* Implements _webform_csv_headers_component().
|
564
|
*/
|
565
|
function _webform_csv_headers_file($component, $export_options) {
|
566
|
$header = array();
|
567
|
// Two columns in header.
|
568
|
$header[0] = array('', '');
|
569
|
$header[1] = array($export_options['header_keys'] ? $component['form_key'] : $component['name'], '');
|
570
|
$header[2] = array(t('Name'), t('Filesize (KB)'));
|
571
|
return $header;
|
572
|
}
|
573
|
|
574
|
/**
|
575
|
* Implements _webform_csv_data_component().
|
576
|
*/
|
577
|
function _webform_csv_data_file($component, $export_options, $value) {
|
578
|
$file = webform_get_file($value[0]);
|
579
|
return empty($file->filename) ? array('', '') : array(webform_file_url($file->uri), (int) ($file->filesize / 1024));
|
580
|
}
|
581
|
|
582
|
/**
|
583
|
* Helper function to create proper file names for uploaded file.
|
584
|
*/
|
585
|
function webform_file_name($filepath) {
|
586
|
if (!empty($filepath)) {
|
587
|
$info = pathinfo($filepath);
|
588
|
$file_name = $info['basename'];
|
589
|
}
|
590
|
return isset($file_name) ? $file_name : '';
|
591
|
}
|
592
|
|
593
|
/**
|
594
|
* Helper function to create proper URLs for uploaded file.
|
595
|
*/
|
596
|
function webform_file_url($uri) {
|
597
|
if (!empty($uri)) {
|
598
|
$file_url = file_create_url($uri);
|
599
|
}
|
600
|
return isset($file_url) ? $file_url : '';
|
601
|
}
|
602
|
|
603
|
/**
|
604
|
* Helper function to load a file from the database.
|
605
|
*/
|
606
|
function webform_get_file($fid) {
|
607
|
// Simple check to prevent loading of NULL values, which throws an entity
|
608
|
// system error.
|
609
|
return $fid ? file_load($fid) : FALSE;
|
610
|
}
|
611
|
|
612
|
/**
|
613
|
* Given a submission with file_usage set, add or remove file usage entries.
|
614
|
*/
|
615
|
function webform_file_usage_adjust($submission) {
|
616
|
if (isset($submission->file_usage)) {
|
617
|
$files = file_load_multiple($submission->file_usage['added_fids']);
|
618
|
foreach ($files as $file) {
|
619
|
$file->status = 1;
|
620
|
file_save($file);
|
621
|
file_usage_add($file, 'webform', 'submission', $submission->sid);
|
622
|
}
|
623
|
|
624
|
$files = file_load_multiple($submission->file_usage['deleted_fids']);
|
625
|
foreach ($files as $file) {
|
626
|
file_usage_delete($file, 'webform', 'submission', $submission->sid);
|
627
|
file_delete($file);
|
628
|
}
|
629
|
}
|
630
|
}
|
631
|
|
632
|
/**
|
633
|
* Rename any files which are eligible for renaming.
|
634
|
*
|
635
|
* Renames if this submission is being submitted for the first time.
|
636
|
*/
|
637
|
function webform_file_rename($node, $submission) {
|
638
|
if (isset($submission->file_usage)) {
|
639
|
foreach ($submission->file_usage['renameable'] as $cid => $fids) {
|
640
|
foreach ($fids as $fid) {
|
641
|
webform_file_process_rename($node, $submission, $node->webform['components'][$cid], $fid);
|
642
|
}
|
643
|
}
|
644
|
}
|
645
|
}
|
646
|
|
647
|
/**
|
648
|
* Renames the uploaded file name using tokens.
|
649
|
*
|
650
|
* @param $node
|
651
|
* The webform node object.
|
652
|
* @param $submission
|
653
|
* The webform submission object.
|
654
|
* @param $component
|
655
|
* Component settings array for which fid is going to be processed.
|
656
|
* @param $fid
|
657
|
* A file id to be processed.
|
658
|
*/
|
659
|
function webform_file_process_rename($node, $submission, $component, $fid) {
|
660
|
$file = webform_get_file($fid);
|
661
|
|
662
|
if ($file) {
|
663
|
// Get the destination uri.
|
664
|
$destination_dir = $component['extra']['scheme'] . '://webform/' . drupal_strtolower(webform_replace_tokens($component['extra']['directory'], $node));
|
665
|
$destination_dir = file_stream_wrapper_uri_normalize($destination_dir);
|
666
|
|
667
|
// Get the file extension.
|
668
|
$info = pathinfo($file->uri);
|
669
|
$extension = $info['extension'];
|
670
|
|
671
|
// Prepare new file name without extension.
|
672
|
$new_file_name = webform_replace_tokens($component['extra']['rename'], $node, $submission, NULL, TRUE);
|
673
|
$new_file_name = trim($new_file_name);
|
674
|
$new_file_name = _webform_transliterate($new_file_name);
|
675
|
$new_file_name = str_replace('/', '_', $new_file_name);
|
676
|
$new_file_name = preg_replace('/[^a-zA-Z0-9_\- ]/', '', $new_file_name);
|
677
|
if (strlen($new_file_name)) {
|
678
|
// Prepare the new uri with new filename.
|
679
|
$destination = "$destination_dir/$new_file_name.$extension";
|
680
|
|
681
|
// Compare the uri and Rename the file name.
|
682
|
if ($file->uri != $destination) {
|
683
|
file_move($file, $destination, FILE_EXISTS_RENAME);
|
684
|
}
|
685
|
}
|
686
|
}
|
687
|
}
|