summaryrefslogtreecommitdiffstats
path: root/filefield.widget.inc
blob: 2a1f7a06d78bd9cb6cce4486e65f01f05d3a202c (plain)
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
<?php
/**
 * @file
 * FileField: Defines a CCK file field type.
 *
 * Uses content.module to store the fid and field specific metadata,
 * and Drupal's {files} table to store the actual file data.
 *
 * This file contains CCK widget related functionality.
 */

/**
 * Implementation of hook_widget() - the one in filefield.module
 * is just there to include this one on demand.
 */
function filefield_widget(&$form, &$form_state, $field, $items, $delta = 0) {
  // JavaScript might reload the form element but not the CSS that has been
  // defined inside the process hook of mimetype specific widgets. As we don't
  // know which widget will show up, just include all of their CSS files.
  $file_widget_info = _filefield_file_widget_info($field['widget']);
  _filefield_add_css($file_widget_info);
  drupal_add_css(drupal_get_path('module', 'filefield') .'/filefield.css');

  if (!empty($items[$delta]) && $file = field_file_load($items[$delta]['fid'])) {
    $file = array_merge($items[$delta], $file);
    return filefield_file_edit_form($form, $form_state, $field, $delta, $file);
  }
  return filefield_file_upload_form($form, $form_state, $field, $delta, $items);
}

/**
 * The filefield widget for not (yet) existing files.
 */
function filefield_file_upload_form(&$form, &$form_state, $field, $delta, $items) {
  $form['#attributes']['enctype'] = 'multipart/form-data';

  // Include JavaScript for client-side file validation.
  drupal_add_js(drupal_get_path('module', 'filefield') .'/filefield.js');

  $field_name_css = str_replace('_', '-', $field['field_name']);
  $id = 'filefield-'. $field_name_css .'-'. $delta .'-form';

  $replaced_file = (isset($items[$delta]) && isset($items[$delta]['replaced_file']))
                    ? $items[$delta]['replaced_file'] : NULL;

  $requirements = _filefield_upload_requirements($field, $field['widget'], $items);

  $widget = array(
    '#type'   => 'filefield_file_upload',
    '#field' => $field,
    '#delta' => $delta,
    '#replaced_file' => $replaced_file,
    '#prefix' => '<div id="'. $id .'" class="filefield-file-form"><div class="filefield-file-upload">',
    '#suffix' => '</div></div>',
    '#upload_requirements' => $requirements,
  );

  if ($requirements['upload possible']) {
    // Buttons inside custom form elements are not registered by the Forms API,
    // so we make the "Upload" button a regular child element and not a part
    // of the filefield_file_upload widget.
    $widget[$field['field_name'] .'_'. $delta .'_upload'] = array(
      '#name' => $field['field_name'] .'_'. $delta .'_upload',
      '#type' => 'submit',
      '#value' => t('Upload'),
      '#submit' => array('filefield_file_upload_submit'), // without JavaScript
      '#ahah' => array( // with JavaScript
        'path' => 'filefield/js/upload/'. $field['field_name'] .'/'. $field['type_name'] .'/'. $delta,
        'wrapper' => $id,
        'method' => 'replace',
        'effect' => 'fade',
      ),
      '#weight' => 10,
      '#field' => $field,
      '#delta' => $delta,
    );
  }
  return $widget;
}

/**
 * The 'process' callback for 'filefield_file_upload' form elements.
 * Called after defining the form and while building it, transforms the
 * barebone element array into a file selection widget.
 */
function filefield_file_upload_process($element, $edit, &$form_state, $form) {
  // Before the element user gets to do his validation, make sure we do ours.
  array_unshift($element['#element_validate'], 'filefield_file_upload_validate');

  $field = $element['#field'];
  $field_name = $field['field_name'];
  $upload_name = $field_name .'_'. $element['#delta'];

  $requirements = $element['#upload_requirements'];

  // Construct the upload description out of user supplied text,
  // maximum upload file size, and (optionally) allowed extensions.
  if ($requirements['upload possible'] == FALSE) {
    $element[$upload_name] = array(
      '#type' => 'markup',
      '#value' => t('!errors No new files can be uploaded anymore.', array(
        '!errors' => implode(' ', $requirements['messages']),
      )),
    );
    return $element;
  }

  // Make a list out of the messages if there are too many restrictions.
  // Looks better than a concatenated sequence of sentences.
  $upload_description = (count($requirements['messages']) > 2)
    ? '<ul><li>'. implode('</li><li>', $requirements['messages']) .'</li></ul>'
    : implode(' ', $requirements['messages']);

  $element[$upload_name] = array(
    '#type' => 'file',
    '#title' => t('Attach new file'),
    '#description' => $upload_description,
    '#weight' => -1,
    // Emulate how FAPI normalizes the _FILES array since this won't go through form_builder
    '#name' => 'files['. $upload_name .']',
  );

  // User 1 may even upload files with extensions that are not allowed.
  // (At least, that's how core's file_validate_extensions() thinks about it.)
  // So only add the JavaScript extension check for other users.
  global $user;
  if ($user->uid != 1) {
    $element[$upload_name]['#attributes'] = array(
      'accept' => str_replace(' ', '|', trim($field['widget']['file_extensions']))
    );
  }

  return $element;
}

/**
 * Theme function for the file upload container element.
 */
function theme_filefield_file_upload($element) {
  return theme('filefield_container_item', $element);
}

/**
 * Value callback for 'filefield_upload' form elements.
 * Uploads and validates a file if one has been specified,
 * and returns the fid of that file as result value.
 */
function filefield_file_upload_value($element, $edit = FALSE) {
  return empty($element['#value'])
    ? array('fid' => 0, 'replaced_file' => $element['#replaced_file'])
    : $element['#value'];
}

/**
 * The 'validate' callback for 'duration_combo' form elements.
 * Called after values are assigned, before form validate and submit are called.
 */
function filefield_file_upload_validate($element, &$form_state) {
  if (!empty($element['#required']) && empty($element['#value']['fid'])) {
    form_error($element, t('You need to upload a file to the %field field.', array(
      '%field' => $element['#field']['widget']['label']
    )));
  }
}

/**
 * Submit callback for the "Upload" button next to each file upload field.
 */
function filefield_file_upload_submit($form, &$form_state) {
  $field = $form_state['clicked_button']['#field'];
  $delta = $form_state['clicked_button']['#delta'];
  _filefield_file_upload($form_state, $field, $delta);

  // Rebuild the form with the new uploaded-file state (hopefully).
  node_form_submit_build_node($form, $form_state);
}

/**
 * Form callback for the "Upload" button with JavaScript enabled,
 * invoked by filefield_js(). Uploads a file to the given field and delta.
 */
function filefield_file_upload_js(&$form, &$form_state, $field, $delta) {
  _filefield_file_upload($form_state, $field, $delta);
}

/**
 * Upload a file to the given field and delta (or try to, at least), and
 * update the corresponding part of the form state with the new file data.
 */
function _filefield_file_upload(&$form_state, $field, $delta) {
  $field_name = $field['field_name'];

  if (module_exists('token')) {
    global $user;
    $widget_file_path = token_replace($field['widget']['file_path'], 'user', $user);
  }
  else {
    $widget_file_path = $field['widget']['file_path'];
  }

  // Let modules provide their own validators.
  $validators = _filefield_upload_validators(
    $field, $field['widget'], $form_state['values'][$field_name]
  );
  $upload_name = $field_name .'_'. $delta;
  $complete_file_path = file_directory_path() .'/'. $widget_file_path;

  $file = &$form_state['values'][$field_name][$delta];
  $replaced_file = $file['replaced_file'];

  if (!filefield_check_directory($widget_file_path, $upload_name)) {
    watchdog('file', 'The upload directory %directory for the file field %field (content type %type) could not be created or is not accessible. A newly uploaded file could not be saved in this directory as a consequence, and the upload was canceled.', array('%directory' => $widget_file_path, '%field' => $field_name, '%type' => $field['type_name']));
    $file = array('fid' => 0, 'replaced_file' => $replaced_file);
    return $file;
  }
  if (!$file = field_file_save_upload($upload_name, $validators, $complete_file_path)) {
    watchdog('file', 'The file %file could not be saved as addition to the file field %field (content type %type). This can be a consequence of the file failing validation, or if it can\'t be moved to the file directory, or whatever reason the file framework comes up with. No further information is available to the filefield module, but if you\'re lucky then that function left one or more hints in the log as well (directly before this log entry).', array('%file' => $complete_file_path, '%field' => $field_name, '%type' => $field['type_name']));
    $file = array('fid' => 0, 'replaced_file' => $replaced_file);
    return $file;
  }

  $file_default_properties = array(
    'list' => 1,
    'description' => $file['filename'],
  );
  $file = array_merge($file_default_properties, $file);
  $file['replaced_file'] = $replaced_file;
  return $file;
}

/**
 * Retrieve a list of file validator functions (and arguments) that can be
 * passed to file_save_upload() (or field_file_save_upload()) as is.
 */
function _filefield_upload_validators($field, $widget, $items) {
  $validator_info = _filefield_validator_info($field, $widget, $items);
  $validators = array();

  foreach ($validator_info as $validator_function => $info) {
    $validators[$validator_function] = $info['validator arguments'];
  }
  return $validators;
}

/**
 * Retrieve a list of upload requirement strings for the various upload
 * restrictions that this module and possible extensions provide.
 */
function _filefield_upload_requirements($field, $widget, $items) {
  $validator_info = _filefield_validator_info($field, $widget, $items);
  $messages = array();
  $errors = array();

  foreach ($validator_info as $validator_function => $info) {
    $messages[] = $info['requirement message'];

    if (isset($info['upload possible']) && $info['upload possible'] == FALSE) {
      $errors[] = $info['requirement message'];
    }
  }
  return array(
    'messages' => empty($errors) ? $messages : $errors,
    'upload possible' => empty($errors),
  );
}

/**
 * Retrieve an array of file validators and their associated requirement
 * messages (placing filefield's own validators first in the result array).
 */
function _filefield_validator_info($field, $widget, $items) {
  // Clean out empty items, so that they're not taken into account by
  // implementations of hook_filefield_validators() - they're irrelevant here.
  // Also, objectify items - because we're leaving the pure filefield realm.
  $existing_files = array();
  foreach ($items as $delta => $item) {
    if (is_array($item) && !empty($item['fid'])) {
      $existing_files[] = (object) $item;
    }
  }
  return array_merge(
    _filefield_filefield_validators($field, $widget, $existing_files),
    module_invoke_all('filefield_validators', $field, $widget, $existing_files)
  );
}

/**
 * Implementation of hook_filefield_validators():
 * Upload restrictions for file size, file extension and supported file widgets.
 * Implemented as private function instead of as a real hook, because we want
 * to make an exception so that these requirements appear first in any list.
 */
function _filefield_filefield_validators($field, $widget, $existing_files) {
  $validators = array();

  // Thanks to the php.ini restrictions, there is always a maximum file size.
  // Therefore we can rely on at least one restriction always being in force.
  $max_filesize = _filefield_maximum_filesize(
    $field, $widget, $existing_files
  );
  $filesize_message = ($max_filesize > 0)
    ? t('Maximum file size: !size.', array('!size' => format_size($max_filesize)))
    : t('The allowed maximum file size total has been exceeded.');

  $validators['file_validate_size'] = array(
    'validator arguments' => array($max_filesize),
    'requirement message' => $filesize_message,
    'upload possible' => ($max_filesize > 0),
  );

  if (!empty($widget['file_extensions'])) {
    $validators['file_validate_extensions'] = array(
      'validator arguments' => array($widget['file_extensions']),
      'requirement message' => t('Allowed extensions: %ext.', array(
        '%ext' => $widget['file_extensions'],
      )),
    );
  }

  $supported_file_widgets = _filefield_supported_file_widgets($widget);
  if (!empty($supported_file_widgets)) {
    $validators['filefield_validate_file_widget_support'] = array(
      'validator arguments' => array($field, $widget, $supported_file_widgets),
      'requirement message' => t('Uploads are restricted to the following categories: !widgets.', array(
        '!widgets' => implode(', ', $supported_file_widgets),
      )),
    );
  }
  return $validators;
}

/**
 * Check that a file is supported by at least one of the widgets that are
 * enabled for the field instance in question.
 *
 * @return
 *   An array. If the file is not allowed, it will contain an error message.
 */
function filefield_validate_file_widget_support($file, $field, $field_widget, $supported_file_widgets) {
  $errors = array();

  // No widgets at all means the widget settings db entry does not exist,
  // so we fall back to "accept this file and use the generic edit widget".
  if (empty($field_widget['file_widgets'])) {
    return $errors;
  }
  // In the common case, we only accept a file if an enabled widget
  // wants to handle it.
  $edit_widget_info = filefield_widget_for_file($file, $field, $field_widget);
  if (empty($edit_widget_info)) {
    $errors[] = t('Uploaded files are restricted to the following categories: !widgets.', array(
      '!widgets' => implode(', ', $supported_file_widgets),
    ));
  }
  return $errors;
}

/**
 * If not all file types might be handled by the enabled set of file widgets,
 * return an array of widget titles specifying which ones are allowed for the
 * given field. If a widget is enabled which handles all files, return an empty array.
 */
function _filefield_supported_file_widgets($field_widget) {
  if (empty($field_widget['file_widgets'])) {
    return array();
  }
  $titles = array();
  $file_widget_info = _filefield_file_widget_info($field_widget);

  foreach ($file_widget_info as $widget_name => $info) {
    if (!$info['enabled']) {
      continue;
    }
    if ($info['suitability callback'] === TRUE) {
      // Handles all kinds of files, no need for requiring any other widget.
      return array();
    }
    $titles[] = $info['title'];
  }
  return $titles;
}

/**
 * Get the maximum file size that is allowed for a new upload.
 *
 * @return
 *   -1 for "no more files allowed", or any positive value as the number
 *   of bytes that may still be uploaded. A result of 0 ("unlimited") will
 *   never happen because of PHP's upload limits.)
 */
function _filefield_maximum_filesize($field, $widget, $existing_files) {
  // Calculate the maximum file size - the least of all returned values.
  $max_filesize = FALSE;
  $restrictions = module_invoke_all(
    'filefield_filesize_restrictions', $field, $widget, $existing_files
  );
  foreach ($restrictions as $value) {
    if ($max_filesize === FALSE || $value < $max_filesize) {
      $max_filesize = $value;
    }
  }

  // Return -1 if any restriction value was not a positive number.
  if ($max_filesize === FALSE || $max_filesize <= 0) {
    return -1;
  }
  return $max_filesize;
}

/**
 * Implementation of hook_filefield_filesize_restrictions():
 * Specify how large a newly uploaded file may be, in bytes.
 * (The smallest size of all hook implementations will be applied in the end).
 */
function filefield_filefield_filesize_restrictions($field, $widget, $existing_files) {
  $filesize_restrictions = array(file_upload_max_size());

  // Maximum file size for each file separately.
  if (!empty($widget['max_filesize_per_file'])) {
    $filesize_restrictions[] = parse_size($widget['max_filesize_per_file']);
  }

  // Maximum file size for all files in the node (for this field).
  if (!empty($widget['max_filesize_per_node'])) {
    $allowed_total_size = parse_size($widget['max_filesize_per_node']);
    $total_size = 0;

    foreach ($existing_files as $delta => $file) {
      if (!empty($file->filesize)) {
        $total_size += $file->filesize;
      }
    }
    if (!empty($total_size)) {
      $filesize_restrictions[] = $allowed_total_size - $total_size;
    }
  }
  return $filesize_restrictions;
}

/**
 * Create the file directory relative to the 'files' dir recursively for every
 * directory in the path.
 *
 * @param $directory
 *   The directory path under files to check, such as 'photo/path/here'.
 * @param $form_item
 *   An optional string containing the name of a form item that any errors
 *   will be attached to. (See field_file_check_directory() for more details.)
 */
function filefield_check_directory($directory, $form_item = NULL) {
  $directory = field_file_strip_path($directory);

  foreach (explode('/', $directory) as $dir) {
    $dirs[] = $dir;
    $path = file_create_path(implode($dirs, '/'));
    if (!field_file_check_directory($path, FILE_CREATE_DIRECTORY, $form_item)) {
      watchdog('filefield', t('FileField failed to create directory (%d) at (%p).', array('%d' => $directory, '%p' => $path)), WATCHDOG_ERROR);
      return FALSE;
    }
  }
  return TRUE;
}


/**
 * The filefield widget for previously uploaded files.
 */
function filefield_file_edit_form(&$form, &$form_state, $field, $delta, $file) {
  $field_name_css = str_replace('_', '-', $field['field_name']);
  $id = 'filefield-'. $field_name_css .'-'. $delta .'-form';

  $classes = array(
    'filefield-'. $field_name_css .'-form',
    'filefield-file-form',
  );
  $widget = array(
    '#type' => 'filefield_file_edit',
    '#default_value' => $file,
    '#field' => $field,
    '#prefix' => '<div id="'. $id .'" class="'. implode(' ', $classes) .'"><div class="filefield-file-edit">',
    '#suffix' => '</div></div>',
  );

  // Buttons inside custom form elements are not registered by the Forms API,
  // so we make the "Delete" button a regular child element and not a part
  // of the filefield_file_upload widget.
  $widget['flags'] = array(
    '#type' => 'markup',
    '#value' => '',
    '#prefix' => '<div class="filefield-file-edit-flags">',
    '#suffix' => '</div>',
  );
  $widget['flags'][$field['field_name'] .'_'. $delta .'_delete'] = array(
    '#name' => $field['field_name'] .'_'. $delta .'_delete',
    '#type' => 'submit',
    '#value' => t('Delete'),
    '#submit' => array('filefield_file_edit_delete_submit'), // without JavaScript
    '#ahah' => array( // with JavaScript
      'path' => 'filefield/js/delete/'. $field['field_name'] .'/'. $field['type_name'] .'/'. $delta,
      'wrapper' => $id,
      'method' => 'replace',
      'effect' => 'fade',
    ),
    '#field' => $field,
    '#delta' => $delta,
    '#file' => $file,
  );
  // Only show the list checkbox if files are not forced to be listed.
  if (!$field['force_list']) {
    $widget['flags']['list'] = array(
      '#type' => 'checkbox',
      '#title' => t('List'),
      '#default_value' => $file['list'],
    );
  }

  $edit_widget_info = filefield_widget_for_file($file, $field, $field['widget']);
  $widget['edit'] = array(
    '#type' => empty($edit_widget_info)
                ? 'filefield_generic_edit' // as last-resort fallback
                : $edit_widget_info['form element'],
    '#field' => $field,
    '#delta' => $delta,
    '#file' => (object) $file,
    '#default_value' => $file,
    '#prefix' => '<div class="filefield-file-edit-widget">',
    '#suffix' => '</div>',
  );

  return $widget;
}

/**
 * Theme function for the file edit container element.
 */
function theme_filefield_file_edit($element) {
  return theme('filefield_container_item', $element);
}

/**
 * Custom value callback for file edit widgets, so that we don't need to rely
 * on a tree structure but can assemble the file to our likings.
 */
function filefield_file_edit_value($element, $edit = FALSE) {
  $file = $element['#default_value'];

  if (!is_array($edit)) {
    return $file;
  }

  // Form API is being mean to us and doesn't include the 'list' value
  // for checkboxes if the checkbox is disabled (0), only if it's enabled (1).
  // That means we need to go the extra mile.
  $list = isset($element['flags']['list'])
          ? !empty($edit['flags']['list'])
          : $file['list'];

  $file_fixed_properties = array(
    'list'      => $list,
    'delete'    => 0,
    'fid'       => $file['fid'],
    'uid'       => $file['uid'],
    'status'    => $file['status'],
    'filename'  => $file['filename'],
    'filepath'  => $file['filepath'],
    'filemime'  => $file['filemime'],
    'filesize'  => $file['filesize'],
    'timestamp' => $file['timestamp'],
  );

  if (is_array($edit['edit'])) {
    $file = array_merge($file, $edit['edit']);
  }
  $file = array_merge($file, $file_fixed_properties);

  // Also merge in other values that might come from other child form elements,
  // like the '_weight' property that CCK adds to this field.
  unset($edit['flags']);
  unset($edit['edit']);
  $file = array_merge($edit, $file);

  return $file;
}

/**
 * Submit callback for the "Delete" button next to each file item.
 */
function filefield_file_edit_delete_submit($form, &$form_state) {
  $field = $form_state['clicked_button']['#field'];
  $delta = $form_state['clicked_button']['#delta'];
  filefield_file_edit_delete($form_state, $field, $delta);

  // Rebuild the form with the new deleted-file state.
  node_form_submit_build_node($form, $form_state);
}

/**
 * Form callback for the "Delete" button with JavaScript enabled,
 * invoked by filefield_js(). Marks the file in the given field and delta
 * as deleted, or deletes it right away (depending on the context).
 */
function filefield_file_edit_delete_js(&$form, &$form_state, $field, $delta) {
  filefield_file_edit_delete($form_state, $field, $delta);
}

/**
 * Update the form state so that the file for the given field and delta
 * is marked as deleted.
 */
function filefield_file_edit_delete(&$form_state, $field, $delta) {
  $field_name = $field['field_name'];
  $file = &$form_state['values'][$field_name][$delta];

  if (isset($file['status']) && $file['status'] == FILE_STATUS_PERMANENT) {
    $file['delete'] = 1;
    $file = array(
      'fid' => 0,
      'replaced_file' => $file,
    );
  }
  else { // temporary file, get rid of it before it's even saved
    $empty_file = array(
      'fid' => 0,
      'replaced_file' => $file['replaced_file'], // remember permanent files from before
    );
    field_file_delete($file);
    $file = $empty_file;
  }
  return $file;
}

/**
 * Shared AHAH callback for uploads and deletions. It just differs in a few
 * unimportant details (what happens to the file, and which form is used as
 * a replacement) so these details are taken care of by a form callback.
 */
function filefield_js($field_name, $type_name, $delta, $form_callback) {
  $field = content_fields($field_name, $type_name);

  if (empty($field) || empty($_POST['form_build_id'])) {
    // Invalid request.
    print drupal_to_js(array('data' => ''));
    exit;
  }

  // Build the new form.
  $form_state = array('submitted' => FALSE);
  $form_build_id = $_POST['form_build_id'];
  $form = form_get_cache($form_build_id, $form_state);

  if (!$form) {
    // Invalid form_build_id.
    print drupal_to_js(array('data' => ''));
    exit;
  }
  // form_get_cache() doesn't yield the original $form_state,
  // but form_builder() does. Needed for retrieving the file array.
  $built_form = $form;
  $built_form_state = $form_state;
  $built_form += array('#post' => $_POST);
  $built_form = form_builder($_POST['form_id'], $built_form, $built_form_state);

  // Clean ids, so that the same element doesn't get a different element id
  // when rendered once more further down.
  form_clean_id(NULL, TRUE);

  // Perform the action for this AHAH callback.
  $form_callback($built_form, $built_form_state, $field, $delta);

  // Ask CCK for the replacement form element. Going through CCK gets us
  // the benefit of nice stuff like '#required' merged in correctly.
  module_load_include('inc', 'content', 'includes/content.node_form');
  $field_element = content_field_form($form, $built_form_state, $field, $delta);
  $delta_element = $field_element[$field_name][0]; // there's only one element in there

  // Add the new element at the right place in the form.
  if (module_exists('fieldgroup') && ($group_name = _fieldgroup_field_get_group($type_name, $field_name))) {
    $form[$group_name][$field_name][$delta] = $delta_element;
  }
  else {
    $form[$field_name][$delta] = $delta_element;
  }

  // Write the (unbuilt, updated) form back to the form cache.
  form_set_cache($form_build_id, $form, $form_state);

  // Render the form for output.
  $form += array(
    '#post' => $_POST,
    '#programmed' => FALSE,
  );
  drupal_alter('form', $form, array(), 'filefield_js');
  $form_state = array('submitted' => FALSE);
  $form = form_builder('filefield_js', $form, $form_state);
  $field_form = empty($group_name) ? $form[$field_name] : $form[$group_name][$field_name];

  // We add a div around the new content to tell AHAH to let this fade in.
  $field_form[$delta]['#prefix'] = '<div class="ahah-new-content">'. (isset($field_form[$delta]['#prefix']) ? $field_form[$delta]['#prefix'] : '');
  $field_form[$delta]['#suffix'] = (isset($field_form[$delta]['#suffix']) ? $field_form[$delta]['#suffix'] : '') .'</div>';

  $output = theme('status_messages') . drupal_render($field_form[$delta]);

  // AHAH is not being nice to us and doesn't know the "other" button (that is,
  // either "Upload" or "Delete") yet. Which in turn causes it not to attach
  // AHAH behaviours after replacing the element. So we need to tell it first.
  $javascript = drupal_add_js(NULL, NULL);
  if (isset($javascript['setting'])) {
    $output .= '<script type="text/javascript">jQuery.extend(Drupal.settings, '. drupal_to_js(call_user_func_array('array_merge_recursive', $javascript['setting'])) .');</script>';
  }

  // For some reason, file uploads don't like drupal_json() with its manual
  // setting of the text/javascript HTTP header. So use this one instead.
  print drupal_to_js(array('status' => TRUE, 'data' => $output));
  exit;
}