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