Issue #1021356: Notice: undefined $item['fid'] in filefield_widget.inc
[project/filefield.git] / filefield.module
CommitLineData
a679aaae 1<?php
d2f124d5 2
d93afe47
DP
3/**
4 * @file
0da420f9 5 * FileField: Defines a CCK file field type.
6413f0f6 6 *
945e9f37
JP
7 * Uses content.module to store the fid and field specific metadata,
8 * and Drupal's {files} table to store the actual file data.
d93afe47
DP
9 */
10
8bc19ff9 11// FileField API hooks should always be available.
437de506
NH
12require_once dirname(__FILE__) . '/field_file.inc';
13require_once dirname(__FILE__) . '/filefield_widget.inc';
c7acc8fb 14
d2f124d5
DP
15/**
16 * Implementation of hook_init().
17 */
db33b245 18function filefield_init() {
6040fe25 19 // File hooks and callbacks may be used by any module.
bad2b17f 20 drupal_add_css(drupal_get_path('module', 'filefield') .'/filefield.css');
6040fe25
NH
21
22 // Conditional module support.
23 if (module_exists('token')) {
24 module_load_include('inc', 'filefield', 'filefield.token');
25 }
d2f124d5 26}
9412a7cc 27
382b6b6f
JP
28/**
29 * Implementation of hook_menu().
30 */
31function filefield_menu() {
d93afe47
DP
32 $items = array();
33
aa3a8ca0 34 $items['filefield/ahah/%/%/%'] = array(
382b6b6f 35 'page callback' => 'filefield_js',
e76d77f0 36 'page arguments' => array(2, 3, 4),
f1113abd 37 'access callback' => 'filefield_edit_access',
8c7a91fb 38 'access arguments' => array(2, 3),
382b6b6f
JP
39 'type' => MENU_CALLBACK,
40 );
e3623331
NH
41 $items['filefield/progress'] = array(
42 'page callback' => 'filefield_progress',
43 'access arguments' => array('access content'),
44 'type' => MENU_CALLBACK,
45 );
46
d93afe47
DP
47 return $items;
48}
49
50/**
0c08790f
JP
51 * Implementation of hook_elements().
52 */
53function filefield_elements() {
54 $elements = array();
b5b36882 55 $elements['filefield_widget'] = array(
0c08790f 56 '#input' => TRUE,
28b1c58e 57 '#columns' => array('fid', 'list', 'data'),
d2f124d5 58 '#process' => array('filefield_widget_process'),
d2f124d5 59 '#value_callback' => 'filefield_widget_value',
ecbb2c26 60 '#element_validate' => array('filefield_widget_validate'),
0c08790f 61 );
0c08790f
JP
62 return $elements;
63}
64
65/**
382b6b6f 66 * Implementation of hook_theme().
22e736e6 67 * @todo: autogenerate theme registry entrys for widgets.
382b6b6f
JP
68 */
69function filefield_theme() {
70 return array(
c147cceb
DP
71 'filefield_file' => array(
72 'arguments' => array('file' => NULL),
73 'file' => 'filefield_formatter.inc',
74 ),
75 'filefield_icon' => array(
76 'arguments' => array('file' => NULL),
77 'file' => 'filefield.theme.inc',
78 ),
d2f124d5 79 'filefield_widget' => array(
5983a6bc 80 'arguments' => array('element' => NULL),
d2f124d5 81 'file' => 'filefield_widget.inc',
5983a6bc 82 ),
c4c7e990 83 'filefield_widget_item' => array(
bad2b17f
DP
84 'arguments' => array('element' => NULL),
85 'file' => 'filefield_widget.inc',
86 ),
c4c7e990 87 'filefield_widget_preview' => array(
8a154cf2 88 'arguments' => array('element' => NULL),
db33b245 89 'file' => 'filefield_widget.inc',
8a154cf2 90 ),
3897ed27
NH
91 'filefield_widget_file' => array(
92 'arguments' => array('element' => NULL),
93 'file' => 'filefield_widget.inc',
94 ),
c4c7e990
DP
95
96
bc63d6a9 97 'filefield_formatter_default' => array(
382b6b6f 98 'arguments' => array('element' => NULL),
c147cceb 99 'file' => 'filefield_formatter.inc',
382b6b6f 100 ),
3dd27e3f
NH
101 'filefield_formatter_url_plain' => array(
102 'arguments' => array('element' => NULL),
103 'file' => 'filefield_formatter.inc',
104 ),
105 'filefield_formatter_path_plain' => array(
106 'arguments' => array('element' => NULL),
107 'file' => 'filefield_formatter.inc',
108 ),
c4c7e990 109 'filefield_item' => array(
a1f1332d 110 'arguments' => array('file' => NULL, 'field' => NULL),
c147cceb 111 'file' => 'filefield_formatter.inc',
b85662ff 112 ),
c4c7e990 113 );
cdf9e275
JP
114}
115
116/**
a4d358d4 117 * Implementation of hook_file_download().
f1113abd 118 */
e98b8da6
NH
119function filefield_file_download($filepath) {
120 $filepath = file_create_path($filepath);
121 $result = db_query("SELECT * FROM {files} WHERE filepath = '%s'", $filepath);
122
123 // Ensure case-sensitivity of uploaded file names.
124 while ($file = db_fetch_object($result)) {
125 if (strcmp($file->filepath, $filepath) == 0) {
126 break;
127 }
128 }
8a29f595 129
e98b8da6 130 // If the file is not found in the database, we're not responsible for it.
098d829b 131 if (empty($file)) {
cdf9e275
JP
132 return;
133 }
8a29f595 134
de0b787f
NH
135 // See if this is a file on a newly created node, on which the user who
136 // uploaded it will immediately have access.
137 $new_node_file = $file->status == 0 && isset($_SESSION['filefield_access']) && in_array($file->fid, $_SESSION['filefield_access']);
138 if ($new_node_file) {
139 $denied = FALSE;
140 }
141 // Loop through all fields and find if this file is used by FileField.
142 else {
143 // Find out if any file field contains this file, and if so, which field
144 // and node it belongs to. Required for later access checking.
145 $cck_files = array();
146 foreach (content_fields() as $field) {
147 if ($field['type'] == 'filefield' || $field['type'] == 'image') {
148 $db_info = content_database_info($field);
149 $table = $db_info['table'];
150 $fid_column = $db_info['columns']['fid']['column'];
151
152 $columns = array('vid', 'nid');
153 foreach ($db_info['columns'] as $property_name => $column_info) {
154 $columns[] = $column_info['column'] .' AS '. $property_name;
155 }
156 $result = db_query("SELECT ". implode(', ', $columns) ."
157 FROM {". $table ."}
158 WHERE ". $fid_column ." = %d", $file->fid);
159
160 while ($content = db_fetch_array($result)) {
161 $content['field'] = $field;
162 $cck_files[$field['field_name']][$content['vid']] = $content;
163 }
f1113abd
JP
164 }
165 }
0b168c78 166
de0b787f
NH
167 // If no file field item is involved with this file, we don't care about it.
168 if (empty($cck_files)) {
169 return;
170 }
f1113abd 171
de0b787f
NH
172 // So the overall field view permissions are not denied, but if access is
173 // denied for ALL nodes containing the file, deny the download as well.
174 // Node access checks also include checking for 'access content'.
175 $nodes = array();
176 $denied = TRUE;
177 foreach ($cck_files as $field_name => $field_files) {
178 foreach ($field_files as $revision_id => $content) {
179 // Checking separately for each revision is probably not the best idea -
180 // what if 'view revisions' is disabled? So, let's just check for the
181 // current revision of that node.
182 if (isset($nodes[$content['nid']])) {
183 continue; // Don't check the same node twice.
184 }
7b27aa5e 185 if (($node = node_load($content['nid'])) && (node_access('view', $node) && filefield_view_access($field_name, $node))) {
de0b787f
NH
186 $denied = FALSE;
187 break 2;
188 }
189 $nodes[$content['nid']] = $node;
f1113abd 190 }
f1113abd 191 }
07083e2f
NH
192 }
193
194 if ($denied) {
195 return -1;
59b7732d 196 }
cdf9e275 197
e59fd0df 198 // Access is granted.
cdf9e275
JP
199 $name = mime_header_encode($file->filename);
200 $type = mime_header_encode($file->filemime);
f460ded7
NH
201 // By default, serve images, text, and flash content for display rather than
202 // download. Or if variable 'filefield_inline_types' is set, use its patterns.
203 $inline_types = variable_get('filefield_inline_types', array('^text/', '^image/', 'flash$'));
204 $disposition = 'attachment';
205 foreach ($inline_types as $inline_type) {
206 // Exclamation marks are used as delimiters to avoid escaping slashes.
207 if (preg_match('!' . $inline_type . '!', $file->filemime)) {
208 $disposition = 'inline';
209 }
210 }
cdf9e275 211 return array(
bfad3097 212 'Content-Type: ' . $type . '; name="' . $name . '"',
798efe06
NH
213 'Content-Length: ' . $file->filesize,
214 'Content-Disposition: ' . $disposition . '; filename="' . $name . '"',
cdf9e275
JP
215 'Cache-Control: private',
216 );
59b7732d
DP
217}
218
175b9f21 219/**
f6e58c1c
NH
220 * Implementation of hook_views_api().
221 */
222function filefield_views_api() {
223 return array(
224 'api' => 2.0,
225 'path' => drupal_get_path('module', 'filefield') . '/views',
226 );
227}
228
229/**
175b9f21 230 * Implementation of hook_form_alter().
231 *
fb9dd5b8 232 * Set the enctype on forms that need to accept file uploads.
175b9f21 233 */
234function filefield_form_alter(&$form, $form_state, $form_id) {
fb9dd5b8 235 // Field configuration (for default images).
d8161e7c
NH
236 if ($form_id == 'content_field_edit_form' && isset($form['#field']) && $form['#field']['type'] == 'filefield') {
237 $form['#attributes']['enctype'] = 'multipart/form-data';
238 }
239
fb9dd5b8 240 // Node forms.
d8161e7c
NH
241 if (preg_match('/_node_form$/', $form_id)) {
242 $form['#attributes']['enctype'] = 'multipart/form-data';
175b9f21 243 }
244}
c985d8bc 245
45f2943f 246/**
d2f124d5
DP
247 * Implementation of CCK's hook_field_info().
248 */
249function filefield_field_info() {
250 return array(
251 'filefield' => array(
7982f68b 252 'label' => t('File'),
d2f124d5
DP
253 'description' => t('Store an arbitrary file.'),
254 ),
255 );
256}
257
258/**
259 * Implementation of hook_field_settings().
260 */
261function filefield_field_settings($op, $field) {
b4620340
DP
262 $return = array();
263
56f3f61b 264 module_load_include('inc', 'filefield', 'filefield_field');
d2f124d5 265 $op = str_replace(' ', '_', $op);
d2f124d5
DP
266 $function = 'filefield_field_settings_'. $op;
267 if (function_exists($function)) {
90b9a66c 268 $result = $function($field);
269 if (isset($result) && is_array($result)) {
270 $return = $result;
271 }
d2f124d5 272 }
b4620340 273
b4620340
DP
274 return $return;
275
d2f124d5
DP
276}
277
278/**
8a669752 279 * Implementation of CCK's hook_field().
45f2943f 280 */
d2f124d5 281function filefield_field($op, $node, $field, &$items, $teaser, $page) {
56f3f61b 282 module_load_include('inc', 'filefield', 'filefield_field');
d2f124d5
DP
283 $op = str_replace(' ', '_', $op);
284 // add filefield specific handlers...
285 $function = 'filefield_field_'. $op;
286 if (function_exists($function)) {
287 return $function($node, $field, $items, $teaser, $page);
cdf9e275
JP
288 }
289}
290
c985d8bc 291/**
d2f124d5 292 * Implementation of CCK's hook_widget_settings().
c985d8bc 293 */
d2f124d5 294function filefield_widget_settings($op, $widget) {
eb21e5bd
NH
295 switch ($op) {
296 case 'form':
297 return filefield_widget_settings_form($widget);
298 case 'save':
299 return filefield_widget_settings_save($widget);
29d050ce 300 }
cdf9e275 301}
d2f124d5
DP
302
303/**
304 * Implementation of hook_widget().
305 */
306function filefield_widget(&$form, &$form_state, $field, $items, $delta = 0) {
0b168c78
NH
307 if (module_exists('devel_themer') && (user_access('access devel theme information') || user_access('access devel information'))) {
308 drupal_set_message(t('Files may not be uploaded while the Theme Developer tool is enabled. It is highly recommended to <a href="!url">disable this module</a> unless it is actively being used.', array('!url' => url('admin/build/modules'))), 'error');
309 }
310
2be7e4b0 311 // CCK doesn't give a validate callback at the field level...
0b168c78 312 // and FAPI's #require is naive to complex structures...
2be7e4b0 313 // we validate at the field level ourselves.
195dd796 314 if (empty($form['#validate']) || !in_array('filefield_node_form_validate', $form['#validate'])) {
2be7e4b0
DP
315 $form['#validate'][] = 'filefield_node_form_validate';
316 }
17b5e95f 317 $form['#attributes']['enctype'] = 'multipart/form-data';
5af49125 318
c02fd5eb 319 module_load_include('inc', $field['widget']['module'], $field['widget']['module'] .'_widget');
6f0f10d4 320
c02fd5eb 321 $item = array('fid' => 0, 'list' => $field['list_default'], 'data' => array('description' => ''));
322 if (isset($items[$delta])) {
323 $item = array_merge($item, $items[$delta]);
324 }
d2f124d5 325 $element = array(
6e7782b4 326 '#title' => $field['widget']['label'],
d2f124d5 327 '#type' => $field['widget']['type'],
c02fd5eb 328 '#default_value' => $item,
dce479a8 329 '#upload_validators' => filefield_widget_upload_validators($field),
d2f124d5 330 );
c02fd5eb 331
d2f124d5
DP
332 return $element;
333}
334
335/**
dce479a8 336 * Get the upload validators for a file field.
337 *
87d3e491
NH
338 * @param $field
339 * A CCK field array.
340 * @return
341 * An array suitable for passing to file_save_upload() or the file field
dce479a8 342 * element's '#upload_validators' property.
343 */
344function filefield_widget_upload_validators($field) {
f92ea317
DP
345 $max_filesize = parse_size(file_upload_max_size());
346 if (!empty($field['widget']['max_filesize_per_file']) && parse_size($field['widget']['max_filesize_per_file']) < $max_filesize) {
347 $max_filesize = parse_size($field['widget']['max_filesize_per_file']);
dce479a8 348 }
349
1370eafc
NH
350 // Match the default value if no file extensions have been saved at all.
351 if (!isset($field['widget']['file_extensions'])) {
352 $field['widget']['file_extensions'] = 'txt';
353 }
354
dce479a8 355 $validators = array(
ad6bb31f
DP
356 // associate the field to the file on validation.
357 'filefield_validate_associate_field' => array($field),
f92ea317 358 'filefield_validate_size' => array($max_filesize),
fc412e8f 359 // Override core since it excludes uid 1 on the extension check.
ad6bb31f 360 // Filefield only excuses uid 1 of quota requirements.
dce479a8 361 'filefield_validate_extensions' => array($field['widget']['file_extensions']),
362 );
363 return $validators;
364}
365
dce479a8 366/**
d2f124d5
DP
367 * Implementation of CCK's hook_content_is_empty().
368 *
87d3e491
NH
369 * The result of this determines whether content.module will save the value of
370 * the field. Note that content module has some interesting behaviors for empty
371 * values. It will always save at least one record for every node revision,
372 * even if the values are all NULL. If it is a multi-value field with an
373 * explicit limit, CCK will save that number of empty entries.
d2f124d5
DP
374 */
375function filefield_content_is_empty($item, $field) {
6f0f10d4 376 return empty($item['fid']) || (int)$item['fid'] == 0;
d2f124d5
DP
377}
378
d2f124d5 379/**
a5e8cb10
NH
380 * Implementation of CCK's hook_content_diff_values().
381 */
382function filefield_content_diff_values($node, $field, $items) {
383 $return = array();
384 foreach ($items as $item) {
385 if (is_array($item) && !empty($item['filepath'])) {
386 $return[] = $item['filepath'];
387 }
388 }
389 return $return;
390}
391
392/**
8a669752
NH
393 * Implementation of CCK's hook_default_value().
394 *
395 * Note this is a widget-level hook, so it does not affect ImageField or other
396 * modules that extend FileField.
397 *
398 * @see content_default_value()
399 */
400function filefield_default_value(&$form, &$form_state, $field, $delta) {
00f6ebad
NH
401 // Reduce the default number of upload fields to one. CCK 2 (but not 3) will
402 // automatically add one more field than necessary. We use the
403 // content_multiple_value_after_build function to determine the version.
404 if (!function_exists('content_multiple_value_after_build') && !isset($form_state['item_count'][$field['field_name']])) {
8a669752
NH
405 $form_state['item_count'][$field['field_name']] = 0;
406 }
407
408 // The default value is actually handled in hook_widget().
409 // hook_default_value() is only helpful for new nodes, and we need to affect
410 // all widgets, such as when a new field is added via "Add another item".
411 return array();
412}
413
414/**
d2f124d5
DP
415 * Implementation of CCK's hook_widget_info().
416 */
417function filefield_widget_info() {
418 return array(
419 'filefield_widget' => array(
420 'label' => t('File Upload'),
57d2397f 421 'field types' => array('filefield'),
d2f124d5
DP
422 'multiple values' => CONTENT_HANDLE_CORE,
423 'callbacks' => array('default value' => CONTENT_CALLBACK_CUSTOM),
424 'description' => t('A plain file upload widget.'),
1370eafc 425 'file_extensions' => 'txt',
d2f124d5 426 ),
d2f124d5
DP
427 );
428}
429
430/**
431 * Implementation of CCK's hook_field_formatter_info().
432 */
433function filefield_field_formatter_info() {
434 return array(
bc63d6a9 435 'default' => array(
d2f124d5 436 'label' => t('Generic files'),
eb21e5bd 437 'field types' => array('filefield'),
d2f124d5
DP
438 'multiple values' => CONTENT_HANDLE_CORE,
439 'description' => t('Displays all kinds of files with an icon and a linked file description.'),
440 ),
3dd27e3f
NH
441 'path_plain' => array(
442 'label' => t('Path to file'),
443 'field types' => array('filefield'),
444 'description' => t('Displays the file system path to the file.'),
445 ),
446 'url_plain' => array(
447 'label' => t('URL to file'),
448 'field types' => array('filefield'),
449 'description' => t('Displays a full URL to the file.'),
450 ),
d2f124d5
DP
451 );
452}
453
d2f124d5 454/**
293cf420
NH
455 * Implementation of CCK's hook_content_generate(). Used by generate.module.
456 */
457function filefield_content_generate($node, $field) {
458 module_load_include('inc', 'filefield', 'filefield.devel');
459
460 if (content_handle('widget', 'multiple values', $field) == CONTENT_HANDLE_MODULE) {
461 return content_devel_multiple('_filefield_content_generate', $node, $field);
462 }
463 else {
464 return _filefield_content_generate($node, $field);
465 }
466}
467
468/**
0aee4560
NH
469 * Get a list of possible information stored in a file field "data" column.
470 */
471function filefield_data_info() {
472 static $columns;
473
474 if (!isset($columns)) {
475 $columns = array();
476 foreach (module_implements('filefield_data_info') as $module) {
477 $function = $module . '_filefield_data_info';
478 $data = (array) $function();
479 foreach ($data as $key => $value) {
480 $data[$key] = $value;
481 $data[$key]['module'] = $module;
482 }
483 $columns = array_merge($columns, $data);
484 }
485 }
486
487 return $columns;
488}
489
490/**
491 * Given an array of data options, dispatch the necessary callback function.
492 */
493function filefield_data_value($key, $value) {
494 $info = filefield_data_info();
495 if (isset($info[$key]['callback'])) {
496 $callback = $info[$key]['callback'];
497 $value = $callback($value);
498 }
499 else {
500 $value = check_plain((string) $value);
501 }
502 return $value;
503}
504
505/**
506 * Implementation of hook_filefield_data_info().
507 *
508 * Define a list of values that this module stores in the "data" column of a
509 * file field. The callback function receives the portion of the data column
510 * defined by key and should return a value suitable for printing to the page.
511 */
512function filefield_filefield_data_info() {
513 return array(
514 'description' => array(
515 'title' => t('Description'),
516 'callback' => 'check_plain',
517 ),
518 );
519}
520
521/**
d2f124d5
DP
522 * Determine the most appropriate icon for the given file's mimetype.
523 *
2c8026f2
NH
524 * @param $file
525 * A file object.
526 * @return
527 * The URL of the icon image file, or FALSE if no icon could be found.
d2f124d5
DP
528 */
529function filefield_icon_url($file) {
437de506 530 module_load_include('inc', 'filefield', 'filefield.theme');
d2f124d5
DP
531 return _filefield_icon_url($file);
532}
533
38fde101
NH
534/**
535 * Implementation of hook_filefield_icon_sets().
536 *
537 * Define a list of icon sets and directories that contain the icons.
538 */
539function filefield_filefield_icon_sets() {
540 return array(
541 'default' => drupal_get_path('module', 'filefield') . '/icons',
542 );
543}
544
d2f124d5
DP
545/**
546 * Access callback for the JavaScript upload and deletion AHAH callbacks.
87d3e491 547 *
d2f124d5
DP
548 * The content_permissions module provides nice fine-grained permissions for
549 * us to check, so we can make sure that the user may actually edit the file.
550 */
8c7a91fb
NH
551function filefield_edit_access($type_name, $field_name) {
552 if (!content_access('edit', content_fields($field_name, $type_name))) {
ba13e2d4 553 return FALSE;
d2f124d5
DP
554 }
555 // No content permissions to check, so let's fall back to a more general permission.
8c7a91fb 556 return user_access('access content') || user_access('administer nodes');
d2f124d5
DP
557}
558
559/**
560 * Access callback that checks if the current user may view the filefield.
561 */
7b27aa5e
NH
562function filefield_view_access($field_name, $node = NULL) {
563 if (!content_access('view', content_fields($field_name), NULL, $node)) {
ba13e2d4 564 return FALSE;
d2f124d5
DP
565 }
566 // No content permissions to check, so let's fall back to a more general permission.
8c7a91fb 567 return user_access('access content') || user_access('administer nodes');
d2f124d5
DP
568}
569
22e736e6 570/**
87d3e491
NH
571 * Menu callback; Shared AHAH callback for uploads and deletions.
572 *
573 * This rebuilds the form element for a particular field item. As long as the
574 * form processing is properly encapsulated in the widget element the form
575 * should rebuild correctly using FAPI without the need for additional callbacks
576 * or processing.
22e736e6 577 */
aa3a8ca0 578function filefield_js($type_name, $field_name, $delta) {
22e736e6 579 $field = content_fields($field_name, $type_name);
7e784adc 580
fc412e8f
NH
581 // Immediately disable devel shutdown functions so that it doesn't botch our
582 // JSON output.
583 $GLOBALS['devel_shutdown'] = FALSE;
584
22e736e6
DP
585 if (empty($field) || empty($_POST['form_build_id'])) {
586 // Invalid request.
a1144a54
NH
587 drupal_set_message(t('An unrecoverable error occurred. The uploaded file likely exceeded the maximum file size (@size) that this server supports.', array('@size' => format_size(file_upload_max_size()))), 'error');
588 print drupal_to_js(array('data' => theme('status_messages')));
22e736e6
DP
589 exit;
590 }
591
592 // Build the new form.
593 $form_state = array('submitted' => FALSE);
594 $form_build_id = $_POST['form_build_id'];
595 $form = form_get_cache($form_build_id, $form_state);
596
597 if (!$form) {
598 // Invalid form_build_id.
a1144a54
NH
599 drupal_set_message(t('An unrecoverable error occurred. This form was missing from the server cache. Try reloading the page and submitting again.'), 'error');
600 print drupal_to_js(array('data' => theme('status_messages')));
22e736e6
DP
601 exit;
602 }
aa3a8ca0 603
4b58ccde
NH
604 // Build the form. This calls the file field's #value_callback function and
605 // saves the uploaded file. Since this form is already marked as cached
606 // (the #cache property is TRUE), the cache is updated automatically and we
607 // don't need to call form_set_cache().
608 $args = $form['#parameters'];
609 $form_id = array_shift($args);
610 $form['#post'] = $_POST;
611 $form = form_builder($form_id, $form, $form_state);
612
613 // Update the cached form with the new element at the right place in the form.
22e736e6 614 if (module_exists('fieldgroup') && ($group_name = _fieldgroup_field_get_group($type_name, $field_name))) {
0ad570e2
NH
615 if (isset($form['#multigroups']) && isset($form['#multigroups'][$group_name][$field_name])) {
616 $form_element = $form[$group_name][$delta][$field_name];
617 }
618 else {
619 $form_element = $form[$group_name][$field_name][$delta];
620 }
22e736e6
DP
621 }
622 else {
4b58ccde 623 $form_element = $form[$field_name][$delta];
22e736e6
DP
624 }
625
4b58ccde
NH
626 if (isset($form_element['_weight'])) {
627 unset($form_element['_weight']);
628 }
22e736e6 629
4b58ccde 630 $output = drupal_render($form_element);
22e736e6
DP
631
632 // AHAH is not being nice to us and doesn't know the "other" button (that is,
633 // either "Upload" or "Delete") yet. Which in turn causes it not to attach
634 // AHAH behaviours after replacing the element. So we need to tell it first.
ad98c19d
NH
635
636 // Loop through the JS settings and find the settings needed for our buttons.
22e736e6 637 $javascript = drupal_add_js(NULL, NULL);
ad98c19d 638 $filefield_ahah_settings = array();
22e736e6 639 if (isset($javascript['setting'])) {
ad98c19d
NH
640 foreach ($javascript['setting'] as $settings) {
641 if (isset($settings['ahah'])) {
642 foreach ($settings['ahah'] as $id => $ahah_settings) {
643 if (strpos($id, 'filefield-upload') || strpos($id, 'filefield-remove')) {
644 $filefield_ahah_settings[$id] = $ahah_settings;
645 }
646 }
647 }
648 }
22e736e6
DP
649 }
650
ad98c19d
NH
651 // Add the AHAH settings needed for our new buttons.
652 if (!empty($filefield_ahah_settings)) {
653 $output .= '<script type="text/javascript">jQuery.extend(Drupal.settings.ahah, '. drupal_to_js($filefield_ahah_settings) .');</script>';
654 }
655
656 $output = theme('status_messages') . $output;
657
22e736e6
DP
658 // For some reason, file uploads don't like drupal_json() with its manual
659 // setting of the text/javascript HTTP header. So use this one instead.
660 print drupal_to_js(array('status' => TRUE, 'data' => $output));
661 exit;
662}
1bffec9e 663
7e784adc 664/**
e3623331
NH
665 * Menu callback for upload progress.
666 */
667function filefield_progress($key) {
668 $progress = array(
669 'message' => t('Starting upload...'),
670 'percentage' => -1,
671 );
672
673 $implementation = filefield_progress_implementation();
674 if ($implementation == 'uploadprogress') {
675 $status = uploadprogress_get_info($key);
676 if (isset($status['bytes_uploaded']) && !empty($status['bytes_total'])) {
5c4d0dd8 677 $progress['message'] = t('Uploading... (@current of @total)', array('@current' => format_size($status['bytes_uploaded']), '@total' => format_size($status['bytes_total'])));
e3623331
NH
678 $progress['percentage'] = round(100 * $status['bytes_uploaded'] / $status['bytes_total']);
679 }
680 }
681 elseif ($implementation == 'apc') {
682 $status = apc_fetch('upload_' . $key);
683 if (isset($status['current']) && !empty($status['total'])) {
5c4d0dd8 684 $progress['message'] = t('Uploading... (@current of @total)', array('@current' => format_size($status['current']), '@total' => format_size($status['total'])));
e3623331
NH
685 $progress['percentage'] = round(100 * $status['current'] / $status['total']);
686 }
687 }
688
689 drupal_json($progress);
690}
691
692/**
693 * Determine which upload progress implementation to use, if any available.
694 */
695function filefield_progress_implementation() {
696 static $implementation;
697 if (!isset($implementation)) {
698 $implementation = FALSE;
699
700 // We prefer the PECL extension uploadprogress because it supports multiple
701 // simultaneous uploads. APC only supports one at a time.
702 if (extension_loaded('uploadprogress')) {
703 $implementation = 'uploadprogress';
704 }
705 elseif (extension_loaded('apc') && ini_get('apc.rfc1867')) {
706 $implementation = 'apc';
707 }
708 }
709 return $implementation;
710}
711
712/**
9ff38ea7 713 * Implementation of hook_file_references().
714 */
715function filefield_file_references($file) {
dd21ac56
NH
716 $count = filefield_get_file_reference_count($file);
717 return $count ? array('filefield' => $count) : NULL;
ad6c5ff0
DP
718}
719
d6caa409 720/**
9ff38ea7 721 * Implementation of hook_file_delete().
722 */
723function filefield_file_delete($file) {
6a189547 724 filefield_delete_file_references($file);
9ff38ea7 725}
726
9ff38ea7 727/**
87d3e491
NH
728 * An #upload_validators callback. Check the file matches an allowed extension.
729 *
730 * If the mimedetect module is available, this will also validate that the
731 * content of the file matches the extension. User #1 is included in this check.
d6caa409
DP
732 *
733 * @param $file
734 * A Drupal file object.
735 * @param $extensions
87d3e491 736 * A string with a space separated list of allowed extensions.
d6caa409 737 * @return
87d3e491 738 * An array of any errors cause by this file if it failed validation.
d6caa409
DP
739 */
740function filefield_validate_extensions($file, $extensions) {
741 global $user;
742 $errors = array();
743
76cc1a41
DP
744 if (!empty($extensions)) {
745 $regex = '/\.('. ereg_replace(' +', '|', preg_quote($extensions)) .')$/i';
6e2a8176
NH
746 $matches = array();
747 if (preg_match($regex, $file->filename, $matches)) {
748 $extension = $matches[1];
749 // If the extension validates, check that the mimetype matches.
750 if (module_exists('mimedetect')) {
751 $type = mimedetect_mime($file);
752 if ($type != $file->filemime) {
f4dd3d44 753 $errors[] = t('The file contents (@type) do not match its extension (@extension).', array('@type' => $type, '@extension' => $extension));
6e2a8176
NH
754 }
755 }
756 }
757 else {
76cc1a41
DP
758 $errors[] = t('Only files with the following extensions are allowed: %files-allowed.', array('%files-allowed' => $extensions));
759 }
d6caa409 760 }
76cc1a41 761
d6caa409
DP
762 return $errors;
763}
764
87d3e491
NH
765/**
766 * Help text automatically appended to fields that have extension validation.
767 */
d6caa409 768function filefield_validate_extensions_help($extensions) {
76cc1a41 769 if (!empty($extensions)) {
6bae6134 770 return t('Allowed extensions: %ext', array('%ext' => $extensions));
76cc1a41
DP
771 }
772 else {
773 return '';
774 }
d6caa409
DP
775}
776
87d3e491
NH
777/**
778 * An #upload_validators callback. Check the file size does not exceed a limit.
779 *
780 * @param $file
781 * A Drupal file object.
782 * @param $file_limit
783 * An integer value limiting the maximum file size in bytes.
784 * @param $file_limit
785 * An integer value limiting the maximum size in bytes a user can upload on
786 * the entire site.
787 * @return
788 * An array of any errors cause by this file if it failed validation.
789 */
f92ea317
DP
790function filefield_validate_size($file, $file_limit = 0, $user_limit = 0) {
791 global $user;
792
793 $errors = array();
794
795 if ($file_limit && $file->filesize > $file_limit) {
796 $errors[] = t('The file is %filesize exceeding the maximum file size of %maxsize.', array('%filesize' => format_size($file->filesize), '%maxsize' => format_size($file_limit)));
797 }
798
799 // Bypass user limits for uid = 1.
800 if ($user->uid != 1) {
801 $total_size = file_space_used($user->uid) + $file->filesize;
802 if ($user_limit && $total_size > $user_limit) {
803 $errors[] = t('The file is %filesize which would exceed your disk quota of %quota.', array('%filesize' => format_size($file->filesize), '%quota' => format_size($user_limit)));
804 }
805 }
806 return $errors;
807}
808
87d3e491
NH
809/**
810 * Automatic help text appended to fields that have file size validation.
811 */
f92ea317 812function filefield_validate_size_help($size) {
6bae6134 813 return t('Maximum file size: %size', array('%size' => format_size(parse_size($size))));
f92ea317
DP
814}
815
87d3e491
NH
816/**
817 * An #upload_validators callback. Check an image resolution.
818 *
819 * @param $file
820 * A Drupal file object.
821 * @param $max_size
822 * A string in the format WIDTHxHEIGHT. If the image is larger than this size
823 * the image will be scaled to fit within these dimensions.
824 * @param $min_size
825 * A string in the format WIDTHxHEIGHT. If the image is smaller than this size
826 * a validation error will be returned.
827 * @return
828 * An array of any errors cause by this file if it failed validation.
829 */
f92ea317
DP
830function filefield_validate_image_resolution(&$file, $maximum_dimensions = 0, $minimum_dimensions = 0) {
831 $errors = array();
832
894255af
NH
833 @list($max_width, $max_height) = explode('x', $maximum_dimensions);
834 @list($min_width, $min_height) = explode('x', $minimum_dimensions);
d4081684 835
f92ea317
DP
836 // Check first that the file is an image.
837 if ($info = image_get_info($file->filepath)) {
838 if ($maximum_dimensions) {
e6289e71
NH
839 $resized = FALSE;
840
f92ea317 841 // Check that it is smaller than the given dimensions.
d4081684
NH
842 if ($info['width'] > $max_width || $info['height'] > $max_height) {
843 $ratio = min($max_width/$info['width'], $max_height/$info['height']);
844 // Check for exact dimension requirements (scaling allowed).
845 if (strcmp($minimum_dimensions, $maximum_dimensions) == 0 && $info['width']/$max_width != $info['height']/$max_height) {
846 $errors[] = t('The image must be exactly %dimensions pixels.', array('%dimensions' => $maximum_dimensions));
847 }
848 // Check that scaling won't drop the image below the minimum dimensions.
4d5dcc26 849 elseif ((image_get_toolkit() || module_exists('imageapi')) && (($info['width'] * $ratio < $min_width) || ($info['height'] * $ratio < $min_height))) {
d4081684
NH
850 $errors[] = t('The image will not fit between the dimensions of %min_dimensions and %max_dimensions pixels.', array('%min_dimensions' => $minimum_dimensions, '%max_dimensions' => $maximum_dimensions));
851 }
4d5dcc26
NH
852 // Try resizing the image with ImageAPI if available.
853 elseif (module_exists('imageapi') && imageapi_default_toolkit()) {
854 $res = imageapi_image_open($file->filepath);
855 imageapi_image_scale($res, $max_width, $max_height);
856 imageapi_image_close($res, $file->filepath);
e6289e71 857 $resized = TRUE;
4d5dcc26 858 }
f92ea317 859 // Try to resize the image to fit the dimensions.
596fff08 860 elseif (image_get_toolkit() && @image_scale($file->filepath, $file->filepath, $max_width, $max_height)) {
e6289e71 861 $resized = TRUE;
f92ea317
DP
862 }
863 else {
864 $errors[] = t('The image is too large; the maximum dimensions are %dimensions pixels.', array('%dimensions' => $maximum_dimensions));
865 }
866 }
e6289e71
NH
867
868 // Clear the cached filesize and refresh the image information.
869 if ($resized) {
870 drupal_set_message(t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', array('%dimensions' => $maximum_dimensions)));
871 clearstatcache();
872 $file->filesize = filesize($file->filepath);
873 }
f92ea317
DP
874 }
875
d4081684 876 if ($minimum_dimensions && empty($errors)) {
f92ea317 877 // Check that it is larger than the given dimensions.
d4081684 878 if ($info['width'] < $min_width || $info['height'] < $min_height) {
f92ea317
DP
879 $errors[] = t('The image is too small; the minimum dimensions are %dimensions pixels.', array('%dimensions' => $minimum_dimensions));
880 }
881 }
882 }
883
884 return $errors;
d6caa409 885}
6ce28b69 886
87d3e491
NH
887/**
888 * Automatic help text appended to fields that have image resolution validation.
889 */
f92ea317 890function filefield_validate_image_resolution_help($max_size = '0', $min_size = '0') {
6ce28b69
DP
891 if (!empty($max_size)) {
892 if (!empty($min_size)) {
a5811c0f 893 if ($max_size == $min_size) {
894 return t('Images must be exactly @min_size pixels', array('@min_size' => $min_size));
895 }
896 else {
897 return t('Images must be between @min_size pixels and @max_size', array('@max_size' => $max_size, '@min_size' => $min_size));
898 }
6ce28b69
DP
899 }
900 else {
4ece7959 901 if (image_get_toolkit()) {
902 return t('Images larger than @max_size pixels will be scaled', array('@max_size' => $max_size));
903 }
904 else {
905 return t('Images must be smaller than @max_size pixels', array('@max_size' => $max_size));
906 }
6ce28b69
DP
907 }
908 }
909 if (!empty($min_size)) {
4ece7959 910 return t('Images must be larger than @max_size pixels', array('@max_size' => $min_size));
6ce28b69
DP
911 }
912}
b7ed071d 913
87d3e491
NH
914
915/**
916 * An #upload_validators callback. Check that a file is an image.
917 *
918 * This check should allow any image that PHP can identify, including png, jpg,
919 * gif, tif, bmp, psd, swc, iff, jpc, jp2, jpx, jb2, xbm, and wbmp.
920 *
921 * This check should be combined with filefield_validate_extensions() to ensure
922 * only web-based images are allowed, however it provides a better check than
923 * extension checking alone if the mimedetect module is not available.
924 *
925 * @param $file
926 * A Drupal file object.
927 * @return
928 * An array of any errors cause by this file if it failed validation.
929 */
f92ea317
DP
930function filefield_validate_is_image(&$file) {
931 $errors = array();
f92ea317
DP
932 $info = image_get_info($file->filepath);
933 if (!$info || empty($info['extension'])) {
6e2a8176 934 $errors[] = t('The file is not a known image format.');
f92ea317 935 }
f92ea317
DP
936 return $errors;
937}
938
87d3e491
NH
939/**
940 * An #upload_validators callback. Add the field to the file object.
941 *
fc412e8f 942 * This validation function adds the field to the file object for later
87d3e491
NH
943 * use in field aware modules implementing hook_file. It's not truly a
944 * validation at all, rather a convient way to add properties to the uploaded
945 * file.
ad6bb31f
DP
946 */
947function filefield_validate_associate_field(&$file, $field) {
948 $file->field = $field;
949 return array();
950}
ab6a2015
NH
951
952/*******************************************************************************
953 * Public API functions for FileField.
954 ******************************************************************************/
955
956/**
f800ae83
NH
957 * Return an array of file fields within a node type or by field name.
958 *
959 * @param $field
960 * Optional. May be either a field array or a field name.
961 * @param $node_type
962 * Optional. The node type to filter the list of fields.
963 */
964function filefield_get_field_list($node_type = NULL, $field = NULL) {
965 // Build the list of fields to be used for retrieval.
966 if (isset($field)) {
967 if (is_string($field)) {
968 $field = content_fields($field, $node_type);
969 }
970 $fields = array($field['field_name'] => $field);
971 }
972 elseif (isset($node_type)) {
973 $type = content_types($node_type);
974 $fields = $type['fields'];
975 }
976 else {
977 $fields = content_fields();
978 }
979
980 // Filter down the list just to file fields.
981 foreach ($fields as $key => $field) {
982 if ($field['type'] != 'filefield') {
983 unset($fields[$key]);
984 }
985 }
986
987 return $fields;
988}
989
990/**
ab6a2015
NH
991 * Count the number of times the file is referenced within a field.
992 *
993 * @param $file
994 * A file object.
995 * @param $field
f800ae83 996 * Optional. The CCK field array or field name as a string.
ab6a2015
NH
997 * @return
998 * An integer value.
999 */
f800ae83
NH
1000function filefield_get_file_reference_count($file, $field = NULL) {
1001 $fields = filefield_get_field_list(NULL, $field);
1002 $file = (object) $file;
1003
1004 $references = 0;
1005 foreach ($fields as $field) {
1006 $db_info = content_database_info($field);
1007 $references += db_result(db_query(
1008 'SELECT count('. $db_info['columns']['fid']['column'] .')
1009 FROM {'. $db_info['table'] .'}
1010 WHERE '. $db_info['columns']['fid']['column'] .' = %d', $file->fid
1011 ));
1012
1013 // If a field_name is present in the file object, the file is being deleted
1014 // from this field.
1015 if (isset($file->field_name) && $field['field_name'] == $file->field_name) {
1016 // If deleting the entire node, count how many references to decrement.
1017 if (isset($file->delete_nid)) {
1018 $node_references = db_result(db_query(
1019 'SELECT count('. $db_info['columns']['fid']['column'] .')
1020 FROM {'. $db_info['table'] .'}
1021 WHERE '. $db_info['columns']['fid']['column'] .' = %d AND nid = %d', $file->fid, $file->delete_nid
1022 ));
1023 $references = $references - $node_references;
1024 }
1025 else {
1026 $references = $references - 1;
1027 }
ab6a2015 1028 }
f800ae83
NH
1029 }
1030
1031 return $references;
1032}
1033
1034/**
1035 * Get a list of node IDs that reference a file.
1036 *
1037 * @param $file
1038 * The file object for which to find references.
1039 * @param $field
1040 * Optional. The CCK field array or field name as a string.
1041 * @return
1042 * An array of IDs grouped by NID: array([nid] => array([vid1], [vid2])).
1043 */
1044function filefield_get_file_references($file, $field = NULL) {
1045 $fields = filefield_get_field_list(NULL, $field);
1046 $file = (object) $file;
1047
1048 $references = array();
1049 foreach ($fields as $field) {
1050 $db_info = content_database_info($field);
1051 $sql = 'SELECT nid, vid FROM {'. $db_info['table'] .'} WHERE '. $db_info['columns']['fid']['column'] .' = %d';
1052 $result = db_query($sql, $file->fid);
1053 while ($row = db_fetch_object($result)) {
1054 $references[$row->nid][$row->vid] = $row->vid;
ab6a2015
NH
1055 }
1056 }
f800ae83 1057
ab6a2015
NH
1058 return $references;
1059}
f800ae83
NH
1060
1061/**
1062 * Get all FileField files connected to a node ID.
1063 *
1064 * @param $nid
1065 * The node object.
1066 * @param $field_name
1067 * Optional. The CCK field array or field name as a string.
1068 * @return
1069 * An array of all files attached to that field (or all fields).
1070 */
1071function filefield_get_node_files($node, $field = NULL) {
1072 $fields = filefield_get_field_list($node->type, $field);
263f62ad 1073 $files = array();
f800ae83
NH
1074
1075 // Get the file rows.
1076 foreach ($fields as $field) {
1077 $db_info = content_database_info($field);
65dac21c
NH
1078 $fields = 'f.*';
1079 $fields .= ', c.'. $db_info['columns']['list']['column'] .' AS list';
1080 $fields .= ', c.'. $db_info['columns']['data']['column'] .' AS data';
1081 $sql = 'SELECT '. $fields .' FROM {files} f INNER JOIN {' . $db_info['table'] . '} c ON f.fid = c.' . $db_info['columns']['fid']['column'] . ' AND c.vid = %d';
f800ae83
NH
1082 $result = db_query($sql, $node->vid);
1083 while ($file = db_fetch_array($result)) {
65dac21c 1084 $file['data'] = unserialize($file['data']);
f800ae83
NH
1085 $files[$file['fid']] = $file;
1086 }
1087 }
1088
1089 return $files;
1090}
6a189547
NH
1091
1092/**
1093 * Delete all node references of a file.
1094 *
1095 * @param $file
1096 * The file object for which to find references.
1097 * @param $field
1098 * Optional. The CCK field array or field name as a string.
1099 */
1100function filefield_delete_file_references($file, $field = NULL) {
1101 $fields = filefield_get_field_list(NULL, $field);
1102 $file = (object) $file;
1103
1104 $references = filefield_get_file_references($file, $field);
1105 foreach ($references as $nid => $node_references) {
1106 // Do not update a node if it is already being deleted directly by the user.
1107 if (isset($file->delete_nid) && $file->delete_nid == $nid) {
1108 continue;
1109 }
1110
1111 foreach ($node_references as $vid) {
1112 // Do not update the node revision if that revision is already being
1113 // saved or deleted directly by the user.
1114 if (isset($file->delete_vid) && $file->delete_vid == $vid) {
1115 continue;
1116 }
1117
1118 $node = node_load(array('vid' => $vid));
1119 foreach ($fields as $field_name => $field) {
1120 if (isset($node->$field_name)) {
1121 foreach ($node->$field_name as $delta => $item) {
1122 if ($item['fid'] == $file->fid) {
1123 unset($node->{$field_name}[$delta]);
1124 }
1125 }
1126 $node->$field_name = array_values(array_filter($node->$field_name));
1127 }
1128 }
1129
1130 // Save the node after removing the file references. This flag prevents
1131 // FileField from attempting to delete the file again.
1132 $node->skip_filefield_delete = TRUE;
1133 node_save($node);
1134 }
1135 }
1136}