Issue #1125368 by tacituseu: hook_file_delete() called without valid
[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) {
2be7e4b0 307 // CCK doesn't give a validate callback at the field level...
0b168c78 308 // and FAPI's #require is naive to complex structures...
2be7e4b0 309 // we validate at the field level ourselves.
195dd796 310 if (empty($form['#validate']) || !in_array('filefield_node_form_validate', $form['#validate'])) {
2be7e4b0
DP
311 $form['#validate'][] = 'filefield_node_form_validate';
312 }
17b5e95f 313 $form['#attributes']['enctype'] = 'multipart/form-data';
5af49125 314
c02fd5eb 315 module_load_include('inc', $field['widget']['module'], $field['widget']['module'] .'_widget');
6f0f10d4 316
c02fd5eb 317 $item = array('fid' => 0, 'list' => $field['list_default'], 'data' => array('description' => ''));
318 if (isset($items[$delta])) {
319 $item = array_merge($item, $items[$delta]);
320 }
d2f124d5 321 $element = array(
6e7782b4 322 '#title' => $field['widget']['label'],
d2f124d5 323 '#type' => $field['widget']['type'],
c02fd5eb 324 '#default_value' => $item,
dce479a8 325 '#upload_validators' => filefield_widget_upload_validators($field),
d2f124d5 326 );
c02fd5eb 327
d2f124d5
DP
328 return $element;
329}
330
331/**
dce479a8 332 * Get the upload validators for a file field.
333 *
87d3e491
NH
334 * @param $field
335 * A CCK field array.
336 * @return
337 * An array suitable for passing to file_save_upload() or the file field
dce479a8 338 * element's '#upload_validators' property.
339 */
340function filefield_widget_upload_validators($field) {
f92ea317
DP
341 $max_filesize = parse_size(file_upload_max_size());
342 if (!empty($field['widget']['max_filesize_per_file']) && parse_size($field['widget']['max_filesize_per_file']) < $max_filesize) {
343 $max_filesize = parse_size($field['widget']['max_filesize_per_file']);
dce479a8 344 }
345
1370eafc
NH
346 // Match the default value if no file extensions have been saved at all.
347 if (!isset($field['widget']['file_extensions'])) {
348 $field['widget']['file_extensions'] = 'txt';
349 }
350
dce479a8 351 $validators = array(
ad6bb31f
DP
352 // associate the field to the file on validation.
353 'filefield_validate_associate_field' => array($field),
f92ea317 354 'filefield_validate_size' => array($max_filesize),
fc412e8f 355 // Override core since it excludes uid 1 on the extension check.
ad6bb31f 356 // Filefield only excuses uid 1 of quota requirements.
dce479a8 357 'filefield_validate_extensions' => array($field['widget']['file_extensions']),
358 );
359 return $validators;
360}
361
dce479a8 362/**
d2f124d5
DP
363 * Implementation of CCK's hook_content_is_empty().
364 *
87d3e491
NH
365 * The result of this determines whether content.module will save the value of
366 * the field. Note that content module has some interesting behaviors for empty
367 * values. It will always save at least one record for every node revision,
368 * even if the values are all NULL. If it is a multi-value field with an
369 * explicit limit, CCK will save that number of empty entries.
d2f124d5
DP
370 */
371function filefield_content_is_empty($item, $field) {
6f0f10d4 372 return empty($item['fid']) || (int)$item['fid'] == 0;
d2f124d5
DP
373}
374
d2f124d5 375/**
a5e8cb10
NH
376 * Implementation of CCK's hook_content_diff_values().
377 */
378function filefield_content_diff_values($node, $field, $items) {
379 $return = array();
380 foreach ($items as $item) {
381 if (is_array($item) && !empty($item['filepath'])) {
382 $return[] = $item['filepath'];
383 }
384 }
385 return $return;
386}
387
388/**
8a669752
NH
389 * Implementation of CCK's hook_default_value().
390 *
391 * Note this is a widget-level hook, so it does not affect ImageField or other
392 * modules that extend FileField.
393 *
394 * @see content_default_value()
395 */
396function filefield_default_value(&$form, &$form_state, $field, $delta) {
00f6ebad
NH
397 // Reduce the default number of upload fields to one. CCK 2 (but not 3) will
398 // automatically add one more field than necessary. We use the
399 // content_multiple_value_after_build function to determine the version.
400 if (!function_exists('content_multiple_value_after_build') && !isset($form_state['item_count'][$field['field_name']])) {
8a669752
NH
401 $form_state['item_count'][$field['field_name']] = 0;
402 }
403
404 // The default value is actually handled in hook_widget().
405 // hook_default_value() is only helpful for new nodes, and we need to affect
406 // all widgets, such as when a new field is added via "Add another item".
407 return array();
408}
409
410/**
d2f124d5
DP
411 * Implementation of CCK's hook_widget_info().
412 */
413function filefield_widget_info() {
414 return array(
415 'filefield_widget' => array(
416 'label' => t('File Upload'),
57d2397f 417 'field types' => array('filefield'),
d2f124d5
DP
418 'multiple values' => CONTENT_HANDLE_CORE,
419 'callbacks' => array('default value' => CONTENT_CALLBACK_CUSTOM),
420 'description' => t('A plain file upload widget.'),
1370eafc 421 'file_extensions' => 'txt',
d2f124d5 422 ),
d2f124d5
DP
423 );
424}
425
426/**
427 * Implementation of CCK's hook_field_formatter_info().
428 */
429function filefield_field_formatter_info() {
430 return array(
bc63d6a9 431 'default' => array(
d2f124d5 432 'label' => t('Generic files'),
eb21e5bd 433 'field types' => array('filefield'),
d2f124d5
DP
434 'multiple values' => CONTENT_HANDLE_CORE,
435 'description' => t('Displays all kinds of files with an icon and a linked file description.'),
436 ),
3dd27e3f
NH
437 'path_plain' => array(
438 'label' => t('Path to file'),
439 'field types' => array('filefield'),
440 'description' => t('Displays the file system path to the file.'),
441 ),
442 'url_plain' => array(
443 'label' => t('URL to file'),
444 'field types' => array('filefield'),
445 'description' => t('Displays a full URL to the file.'),
446 ),
d2f124d5
DP
447 );
448}
449
d2f124d5 450/**
293cf420
NH
451 * Implementation of CCK's hook_content_generate(). Used by generate.module.
452 */
453function filefield_content_generate($node, $field) {
454 module_load_include('inc', 'filefield', 'filefield.devel');
455
456 if (content_handle('widget', 'multiple values', $field) == CONTENT_HANDLE_MODULE) {
457 return content_devel_multiple('_filefield_content_generate', $node, $field);
458 }
459 else {
460 return _filefield_content_generate($node, $field);
461 }
462}
463
464/**
0aee4560
NH
465 * Get a list of possible information stored in a file field "data" column.
466 */
467function filefield_data_info() {
468 static $columns;
469
470 if (!isset($columns)) {
471 $columns = array();
472 foreach (module_implements('filefield_data_info') as $module) {
473 $function = $module . '_filefield_data_info';
474 $data = (array) $function();
475 foreach ($data as $key => $value) {
476 $data[$key] = $value;
477 $data[$key]['module'] = $module;
478 }
479 $columns = array_merge($columns, $data);
480 }
481 }
482
483 return $columns;
484}
485
486/**
487 * Given an array of data options, dispatch the necessary callback function.
488 */
489function filefield_data_value($key, $value) {
490 $info = filefield_data_info();
491 if (isset($info[$key]['callback'])) {
492 $callback = $info[$key]['callback'];
493 $value = $callback($value);
494 }
495 else {
496 $value = check_plain((string) $value);
497 }
498 return $value;
499}
500
501/**
502 * Implementation of hook_filefield_data_info().
503 *
504 * Define a list of values that this module stores in the "data" column of a
505 * file field. The callback function receives the portion of the data column
506 * defined by key and should return a value suitable for printing to the page.
507 */
508function filefield_filefield_data_info() {
509 return array(
510 'description' => array(
511 'title' => t('Description'),
512 'callback' => 'check_plain',
513 ),
514 );
515}
516
517/**
d2f124d5
DP
518 * Determine the most appropriate icon for the given file's mimetype.
519 *
2c8026f2
NH
520 * @param $file
521 * A file object.
522 * @return
523 * The URL of the icon image file, or FALSE if no icon could be found.
d2f124d5
DP
524 */
525function filefield_icon_url($file) {
437de506 526 module_load_include('inc', 'filefield', 'filefield.theme');
d2f124d5
DP
527 return _filefield_icon_url($file);
528}
529
38fde101
NH
530/**
531 * Implementation of hook_filefield_icon_sets().
532 *
533 * Define a list of icon sets and directories that contain the icons.
534 */
535function filefield_filefield_icon_sets() {
536 return array(
537 'default' => drupal_get_path('module', 'filefield') . '/icons',
538 );
539}
540
d2f124d5 541/**
4ba3fc3f 542 * Access callback for AHAH upload/delete callbacks and node form validation.
87d3e491 543 *
d2f124d5
DP
544 * The content_permissions module provides nice fine-grained permissions for
545 * us to check, so we can make sure that the user may actually edit the file.
546 */
4ba3fc3f 547function filefield_edit_access($type_name, $field_name, $node = NULL) {
044b4484 548 return content_access('edit', content_fields($field_name, $type_name), NULL, $node);
d2f124d5
DP
549}
550
551/**
552 * Access callback that checks if the current user may view the filefield.
553 */
7b27aa5e 554function filefield_view_access($field_name, $node = NULL) {
044b4484 555 return content_access('view', content_fields($field_name), NULL, $node);
d2f124d5
DP
556}
557
22e736e6 558/**
87d3e491
NH
559 * Menu callback; Shared AHAH callback for uploads and deletions.
560 *
561 * This rebuilds the form element for a particular field item. As long as the
562 * form processing is properly encapsulated in the widget element the form
563 * should rebuild correctly using FAPI without the need for additional callbacks
564 * or processing.
22e736e6 565 */
aa3a8ca0 566function filefield_js($type_name, $field_name, $delta) {
22e736e6 567 $field = content_fields($field_name, $type_name);
7e784adc 568
fc412e8f
NH
569 // Immediately disable devel shutdown functions so that it doesn't botch our
570 // JSON output.
571 $GLOBALS['devel_shutdown'] = FALSE;
572
22e736e6
DP
573 if (empty($field) || empty($_POST['form_build_id'])) {
574 // Invalid request.
a1144a54
NH
575 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');
576 print drupal_to_js(array('data' => theme('status_messages')));
22e736e6
DP
577 exit;
578 }
579
580 // Build the new form.
581 $form_state = array('submitted' => FALSE);
582 $form_build_id = $_POST['form_build_id'];
583 $form = form_get_cache($form_build_id, $form_state);
584
585 if (!$form) {
586 // Invalid form_build_id.
a1144a54
NH
587 drupal_set_message(t('An unrecoverable error occurred. This form was missing from the server cache. Try reloading the page and submitting again.'), 'error');
588 print drupal_to_js(array('data' => theme('status_messages')));
22e736e6
DP
589 exit;
590 }
aa3a8ca0 591
4b58ccde
NH
592 // Build the form. This calls the file field's #value_callback function and
593 // saves the uploaded file. Since this form is already marked as cached
594 // (the #cache property is TRUE), the cache is updated automatically and we
595 // don't need to call form_set_cache().
596 $args = $form['#parameters'];
597 $form_id = array_shift($args);
598 $form['#post'] = $_POST;
599 $form = form_builder($form_id, $form, $form_state);
600
601 // Update the cached form with the new element at the right place in the form.
22e736e6 602 if (module_exists('fieldgroup') && ($group_name = _fieldgroup_field_get_group($type_name, $field_name))) {
0ad570e2
NH
603 if (isset($form['#multigroups']) && isset($form['#multigroups'][$group_name][$field_name])) {
604 $form_element = $form[$group_name][$delta][$field_name];
605 }
606 else {
607 $form_element = $form[$group_name][$field_name][$delta];
608 }
22e736e6 609 }
ac31ee6a
NH
610
611 if (!isset($form_element)) {
4b58ccde 612 $form_element = $form[$field_name][$delta];
22e736e6
DP
613 }
614
4b58ccde
NH
615 if (isset($form_element['_weight'])) {
616 unset($form_element['_weight']);
617 }
22e736e6 618
4b58ccde 619 $output = drupal_render($form_element);
22e736e6
DP
620
621 // AHAH is not being nice to us and doesn't know the "other" button (that is,
622 // either "Upload" or "Delete") yet. Which in turn causes it not to attach
623 // AHAH behaviours after replacing the element. So we need to tell it first.
ad98c19d
NH
624
625 // Loop through the JS settings and find the settings needed for our buttons.
22e736e6 626 $javascript = drupal_add_js(NULL, NULL);
ad98c19d 627 $filefield_ahah_settings = array();
22e736e6 628 if (isset($javascript['setting'])) {
ad98c19d
NH
629 foreach ($javascript['setting'] as $settings) {
630 if (isset($settings['ahah'])) {
631 foreach ($settings['ahah'] as $id => $ahah_settings) {
632 if (strpos($id, 'filefield-upload') || strpos($id, 'filefield-remove')) {
633 $filefield_ahah_settings[$id] = $ahah_settings;
634 }
635 }
636 }
637 }
22e736e6
DP
638 }
639
ad98c19d
NH
640 // Add the AHAH settings needed for our new buttons.
641 if (!empty($filefield_ahah_settings)) {
642 $output .= '<script type="text/javascript">jQuery.extend(Drupal.settings.ahah, '. drupal_to_js($filefield_ahah_settings) .');</script>';
643 }
644
645 $output = theme('status_messages') . $output;
646
22e736e6
DP
647 // For some reason, file uploads don't like drupal_json() with its manual
648 // setting of the text/javascript HTTP header. So use this one instead.
649 print drupal_to_js(array('status' => TRUE, 'data' => $output));
650 exit;
651}
1bffec9e 652
7e784adc 653/**
e3623331
NH
654 * Menu callback for upload progress.
655 */
656function filefield_progress($key) {
657 $progress = array(
658 'message' => t('Starting upload...'),
659 'percentage' => -1,
660 );
661
662 $implementation = filefield_progress_implementation();
663 if ($implementation == 'uploadprogress') {
664 $status = uploadprogress_get_info($key);
665 if (isset($status['bytes_uploaded']) && !empty($status['bytes_total'])) {
5c4d0dd8 666 $progress['message'] = t('Uploading... (@current of @total)', array('@current' => format_size($status['bytes_uploaded']), '@total' => format_size($status['bytes_total'])));
e3623331
NH
667 $progress['percentage'] = round(100 * $status['bytes_uploaded'] / $status['bytes_total']);
668 }
669 }
670 elseif ($implementation == 'apc') {
671 $status = apc_fetch('upload_' . $key);
672 if (isset($status['current']) && !empty($status['total'])) {
5c4d0dd8 673 $progress['message'] = t('Uploading... (@current of @total)', array('@current' => format_size($status['current']), '@total' => format_size($status['total'])));
e3623331
NH
674 $progress['percentage'] = round(100 * $status['current'] / $status['total']);
675 }
676 }
677
678 drupal_json($progress);
679}
680
681/**
682 * Determine which upload progress implementation to use, if any available.
683 */
684function filefield_progress_implementation() {
685 static $implementation;
686 if (!isset($implementation)) {
687 $implementation = FALSE;
688
689 // We prefer the PECL extension uploadprogress because it supports multiple
690 // simultaneous uploads. APC only supports one at a time.
691 if (extension_loaded('uploadprogress')) {
692 $implementation = 'uploadprogress';
693 }
694 elseif (extension_loaded('apc') && ini_get('apc.rfc1867')) {
695 $implementation = 'apc';
696 }
697 }
698 return $implementation;
699}
700
701/**
9ff38ea7 702 * Implementation of hook_file_references().
703 */
704function filefield_file_references($file) {
dd21ac56
NH
705 $count = filefield_get_file_reference_count($file);
706 return $count ? array('filefield' => $count) : NULL;
ad6c5ff0
DP
707}
708
d6caa409 709/**
9ff38ea7 710 * Implementation of hook_file_delete().
711 */
712function filefield_file_delete($file) {
6a189547 713 filefield_delete_file_references($file);
9ff38ea7 714}
715
9ff38ea7 716/**
87d3e491
NH
717 * An #upload_validators callback. Check the file matches an allowed extension.
718 *
719 * If the mimedetect module is available, this will also validate that the
720 * content of the file matches the extension. User #1 is included in this check.
d6caa409
DP
721 *
722 * @param $file
723 * A Drupal file object.
724 * @param $extensions
87d3e491 725 * A string with a space separated list of allowed extensions.
d6caa409 726 * @return
87d3e491 727 * An array of any errors cause by this file if it failed validation.
d6caa409
DP
728 */
729function filefield_validate_extensions($file, $extensions) {
730 global $user;
731 $errors = array();
732
76cc1a41
DP
733 if (!empty($extensions)) {
734 $regex = '/\.('. ereg_replace(' +', '|', preg_quote($extensions)) .')$/i';
6e2a8176
NH
735 $matches = array();
736 if (preg_match($regex, $file->filename, $matches)) {
737 $extension = $matches[1];
738 // If the extension validates, check that the mimetype matches.
739 if (module_exists('mimedetect')) {
740 $type = mimedetect_mime($file);
741 if ($type != $file->filemime) {
f4dd3d44 742 $errors[] = t('The file contents (@type) do not match its extension (@extension).', array('@type' => $type, '@extension' => $extension));
6e2a8176
NH
743 }
744 }
745 }
746 else {
76cc1a41
DP
747 $errors[] = t('Only files with the following extensions are allowed: %files-allowed.', array('%files-allowed' => $extensions));
748 }
d6caa409 749 }
76cc1a41 750
d6caa409
DP
751 return $errors;
752}
753
87d3e491
NH
754/**
755 * Help text automatically appended to fields that have extension validation.
756 */
d6caa409 757function filefield_validate_extensions_help($extensions) {
76cc1a41 758 if (!empty($extensions)) {
6bae6134 759 return t('Allowed extensions: %ext', array('%ext' => $extensions));
76cc1a41
DP
760 }
761 else {
762 return '';
763 }
d6caa409
DP
764}
765
87d3e491
NH
766/**
767 * An #upload_validators callback. Check the file size does not exceed a limit.
768 *
769 * @param $file
770 * A Drupal file object.
771 * @param $file_limit
772 * An integer value limiting the maximum file size in bytes.
773 * @param $file_limit
774 * An integer value limiting the maximum size in bytes a user can upload on
775 * the entire site.
776 * @return
777 * An array of any errors cause by this file if it failed validation.
778 */
f92ea317
DP
779function filefield_validate_size($file, $file_limit = 0, $user_limit = 0) {
780 global $user;
781
782 $errors = array();
783
784 if ($file_limit && $file->filesize > $file_limit) {
785 $errors[] = t('The file is %filesize exceeding the maximum file size of %maxsize.', array('%filesize' => format_size($file->filesize), '%maxsize' => format_size($file_limit)));
786 }
787
788 // Bypass user limits for uid = 1.
789 if ($user->uid != 1) {
790 $total_size = file_space_used($user->uid) + $file->filesize;
791 if ($user_limit && $total_size > $user_limit) {
792 $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)));
793 }
794 }
795 return $errors;
796}
797
87d3e491
NH
798/**
799 * Automatic help text appended to fields that have file size validation.
800 */
f92ea317 801function filefield_validate_size_help($size) {
6bae6134 802 return t('Maximum file size: %size', array('%size' => format_size(parse_size($size))));
f92ea317
DP
803}
804
87d3e491
NH
805/**
806 * An #upload_validators callback. Check an image resolution.
807 *
808 * @param $file
809 * A Drupal file object.
810 * @param $max_size
811 * A string in the format WIDTHxHEIGHT. If the image is larger than this size
812 * the image will be scaled to fit within these dimensions.
813 * @param $min_size
814 * A string in the format WIDTHxHEIGHT. If the image is smaller than this size
815 * a validation error will be returned.
816 * @return
817 * An array of any errors cause by this file if it failed validation.
818 */
f92ea317
DP
819function filefield_validate_image_resolution(&$file, $maximum_dimensions = 0, $minimum_dimensions = 0) {
820 $errors = array();
821
894255af
NH
822 @list($max_width, $max_height) = explode('x', $maximum_dimensions);
823 @list($min_width, $min_height) = explode('x', $minimum_dimensions);
d4081684 824
f92ea317
DP
825 // Check first that the file is an image.
826 if ($info = image_get_info($file->filepath)) {
827 if ($maximum_dimensions) {
e6289e71
NH
828 $resized = FALSE;
829
f92ea317 830 // Check that it is smaller than the given dimensions.
d4081684
NH
831 if ($info['width'] > $max_width || $info['height'] > $max_height) {
832 $ratio = min($max_width/$info['width'], $max_height/$info['height']);
833 // Check for exact dimension requirements (scaling allowed).
834 if (strcmp($minimum_dimensions, $maximum_dimensions) == 0 && $info['width']/$max_width != $info['height']/$max_height) {
835 $errors[] = t('The image must be exactly %dimensions pixels.', array('%dimensions' => $maximum_dimensions));
836 }
837 // Check that scaling won't drop the image below the minimum dimensions.
4d5dcc26 838 elseif ((image_get_toolkit() || module_exists('imageapi')) && (($info['width'] * $ratio < $min_width) || ($info['height'] * $ratio < $min_height))) {
d4081684
NH
839 $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));
840 }
4d5dcc26
NH
841 // Try resizing the image with ImageAPI if available.
842 elseif (module_exists('imageapi') && imageapi_default_toolkit()) {
843 $res = imageapi_image_open($file->filepath);
844 imageapi_image_scale($res, $max_width, $max_height);
845 imageapi_image_close($res, $file->filepath);
e6289e71 846 $resized = TRUE;
4d5dcc26 847 }
f92ea317 848 // Try to resize the image to fit the dimensions.
596fff08 849 elseif (image_get_toolkit() && @image_scale($file->filepath, $file->filepath, $max_width, $max_height)) {
e6289e71 850 $resized = TRUE;
f92ea317
DP
851 }
852 else {
853 $errors[] = t('The image is too large; the maximum dimensions are %dimensions pixels.', array('%dimensions' => $maximum_dimensions));
854 }
855 }
e6289e71
NH
856
857 // Clear the cached filesize and refresh the image information.
858 if ($resized) {
859 drupal_set_message(t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', array('%dimensions' => $maximum_dimensions)));
860 clearstatcache();
861 $file->filesize = filesize($file->filepath);
862 }
f92ea317
DP
863 }
864
d4081684 865 if ($minimum_dimensions && empty($errors)) {
f92ea317 866 // Check that it is larger than the given dimensions.
d4081684 867 if ($info['width'] < $min_width || $info['height'] < $min_height) {
f92ea317
DP
868 $errors[] = t('The image is too small; the minimum dimensions are %dimensions pixels.', array('%dimensions' => $minimum_dimensions));
869 }
870 }
871 }
872
873 return $errors;
d6caa409 874}
6ce28b69 875
87d3e491
NH
876/**
877 * Automatic help text appended to fields that have image resolution validation.
878 */
f92ea317 879function filefield_validate_image_resolution_help($max_size = '0', $min_size = '0') {
6ce28b69
DP
880 if (!empty($max_size)) {
881 if (!empty($min_size)) {
a5811c0f 882 if ($max_size == $min_size) {
883 return t('Images must be exactly @min_size pixels', array('@min_size' => $min_size));
884 }
885 else {
886 return t('Images must be between @min_size pixels and @max_size', array('@max_size' => $max_size, '@min_size' => $min_size));
887 }
6ce28b69
DP
888 }
889 else {
4ece7959 890 if (image_get_toolkit()) {
891 return t('Images larger than @max_size pixels will be scaled', array('@max_size' => $max_size));
892 }
893 else {
894 return t('Images must be smaller than @max_size pixels', array('@max_size' => $max_size));
895 }
6ce28b69
DP
896 }
897 }
898 if (!empty($min_size)) {
4ece7959 899 return t('Images must be larger than @max_size pixels', array('@max_size' => $min_size));
6ce28b69
DP
900 }
901}
b7ed071d 902
87d3e491
NH
903
904/**
905 * An #upload_validators callback. Check that a file is an image.
906 *
907 * This check should allow any image that PHP can identify, including png, jpg,
908 * gif, tif, bmp, psd, swc, iff, jpc, jp2, jpx, jb2, xbm, and wbmp.
909 *
910 * This check should be combined with filefield_validate_extensions() to ensure
911 * only web-based images are allowed, however it provides a better check than
912 * extension checking alone if the mimedetect module is not available.
913 *
914 * @param $file
915 * A Drupal file object.
916 * @return
917 * An array of any errors cause by this file if it failed validation.
918 */
f92ea317
DP
919function filefield_validate_is_image(&$file) {
920 $errors = array();
f92ea317
DP
921 $info = image_get_info($file->filepath);
922 if (!$info || empty($info['extension'])) {
6e2a8176 923 $errors[] = t('The file is not a known image format.');
f92ea317 924 }
f92ea317
DP
925 return $errors;
926}
927
87d3e491
NH
928/**
929 * An #upload_validators callback. Add the field to the file object.
930 *
fc412e8f 931 * This validation function adds the field to the file object for later
87d3e491
NH
932 * use in field aware modules implementing hook_file. It's not truly a
933 * validation at all, rather a convient way to add properties to the uploaded
934 * file.
ad6bb31f
DP
935 */
936function filefield_validate_associate_field(&$file, $field) {
937 $file->field = $field;
938 return array();
939}
ab6a2015
NH
940
941/*******************************************************************************
942 * Public API functions for FileField.
943 ******************************************************************************/
944
945/**
f800ae83
NH
946 * Return an array of file fields within a node type or by field name.
947 *
948 * @param $field
949 * Optional. May be either a field array or a field name.
950 * @param $node_type
951 * Optional. The node type to filter the list of fields.
952 */
953function filefield_get_field_list($node_type = NULL, $field = NULL) {
954 // Build the list of fields to be used for retrieval.
955 if (isset($field)) {
956 if (is_string($field)) {
957 $field = content_fields($field, $node_type);
958 }
959 $fields = array($field['field_name'] => $field);
960 }
961 elseif (isset($node_type)) {
962 $type = content_types($node_type);
963 $fields = $type['fields'];
964 }
965 else {
966 $fields = content_fields();
967 }
968
969 // Filter down the list just to file fields.
970 foreach ($fields as $key => $field) {
971 if ($field['type'] != 'filefield') {
972 unset($fields[$key]);
973 }
974 }
975
976 return $fields;
977}
978
979/**
ab6a2015
NH
980 * Count the number of times the file is referenced within a field.
981 *
982 * @param $file
983 * A file object.
984 * @param $field
f800ae83 985 * Optional. The CCK field array or field name as a string.
ab6a2015
NH
986 * @return
987 * An integer value.
988 */
f800ae83
NH
989function filefield_get_file_reference_count($file, $field = NULL) {
990 $fields = filefield_get_field_list(NULL, $field);
991 $file = (object) $file;
992
993 $references = 0;
994 foreach ($fields as $field) {
995 $db_info = content_database_info($field);
996 $references += db_result(db_query(
997 'SELECT count('. $db_info['columns']['fid']['column'] .')
998 FROM {'. $db_info['table'] .'}
999 WHERE '. $db_info['columns']['fid']['column'] .' = %d', $file->fid
1000 ));
1001
1002 // If a field_name is present in the file object, the file is being deleted
1003 // from this field.
1004 if (isset($file->field_name) && $field['field_name'] == $file->field_name) {
1005 // If deleting the entire node, count how many references to decrement.
1006 if (isset($file->delete_nid)) {
1007 $node_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 AND nid = %d', $file->fid, $file->delete_nid
1011 ));
1012 $references = $references - $node_references;
1013 }
1014 else {
1015 $references = $references - 1;
1016 }
ab6a2015 1017 }
f800ae83
NH
1018 }
1019
1020 return $references;
1021}
1022
1023/**
1024 * Get a list of node IDs that reference a file.
1025 *
1026 * @param $file
1027 * The file object for which to find references.
1028 * @param $field
1029 * Optional. The CCK field array or field name as a string.
1030 * @return
1031 * An array of IDs grouped by NID: array([nid] => array([vid1], [vid2])).
1032 */
1033function filefield_get_file_references($file, $field = NULL) {
1034 $fields = filefield_get_field_list(NULL, $field);
1035 $file = (object) $file;
1036
1037 $references = array();
1038 foreach ($fields as $field) {
1039 $db_info = content_database_info($field);
1040 $sql = 'SELECT nid, vid FROM {'. $db_info['table'] .'} WHERE '. $db_info['columns']['fid']['column'] .' = %d';
1041 $result = db_query($sql, $file->fid);
1042 while ($row = db_fetch_object($result)) {
1043 $references[$row->nid][$row->vid] = $row->vid;
ab6a2015
NH
1044 }
1045 }
f800ae83 1046
ab6a2015
NH
1047 return $references;
1048}
f800ae83
NH
1049
1050/**
1051 * Get all FileField files connected to a node ID.
1052 *
6b703b01 1053 * @param $node
f800ae83 1054 * The node object.
6b703b01 1055 * @param $field
f800ae83
NH
1056 * Optional. The CCK field array or field name as a string.
1057 * @return
1058 * An array of all files attached to that field (or all fields).
1059 */
1060function filefield_get_node_files($node, $field = NULL) {
1061 $fields = filefield_get_field_list($node->type, $field);
263f62ad 1062 $files = array();
f800ae83
NH
1063
1064 // Get the file rows.
1065 foreach ($fields as $field) {
1066 $db_info = content_database_info($field);
65dac21c
NH
1067 $fields = 'f.*';
1068 $fields .= ', c.'. $db_info['columns']['list']['column'] .' AS list';
1069 $fields .= ', c.'. $db_info['columns']['data']['column'] .' AS data';
1070 $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
1071 $result = db_query($sql, $node->vid);
1072 while ($file = db_fetch_array($result)) {
65dac21c 1073 $file['data'] = unserialize($file['data']);
f800ae83
NH
1074 $files[$file['fid']] = $file;
1075 }
1076 }
1077
1078 return $files;
1079}
6a189547
NH
1080
1081/**
1082 * Delete all node references of a file.
1083 *
1084 * @param $file
1085 * The file object for which to find references.
1086 * @param $field
1087 * Optional. The CCK field array or field name as a string.
1088 */
1089function filefield_delete_file_references($file, $field = NULL) {
1090 $fields = filefield_get_field_list(NULL, $field);
1091 $file = (object) $file;
1092
1093 $references = filefield_get_file_references($file, $field);
1094 foreach ($references as $nid => $node_references) {
1095 // Do not update a node if it is already being deleted directly by the user.
1096 if (isset($file->delete_nid) && $file->delete_nid == $nid) {
1097 continue;
1098 }
1099
1100 foreach ($node_references as $vid) {
1101 // Do not update the node revision if that revision is already being
1102 // saved or deleted directly by the user.
1103 if (isset($file->delete_vid) && $file->delete_vid == $vid) {
1104 continue;
1105 }
1106
1107 $node = node_load(array('vid' => $vid));
1108 foreach ($fields as $field_name => $field) {
1109 if (isset($node->$field_name)) {
1110 foreach ($node->$field_name as $delta => $item) {
1111 if ($item['fid'] == $file->fid) {
1112 unset($node->{$field_name}[$delta]);
1113 }
1114 }
1115 $node->$field_name = array_values(array_filter($node->$field_name));
1116 }
1117 }
1118
1119 // Save the node after removing the file references. This flag prevents
1120 // FileField from attempting to delete the file again.
1121 $node->skip_filefield_delete = TRUE;
1122 node_save($node);
1123 }
1124 }
1125}