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