| [ Index ] |
PHP Cross Reference of Drupal 6 (yi-drupal) |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @file 5 * FileField: Defines a CCK file field type. 6 * 7 * Uses content.module to store the fid and field specific metadata, 8 * and Drupal's {files} table to store the actual file data. 9 */ 10 11 // FileField API hooks should always be available. 12 require_once dirname(__FILE__) . '/field_file.inc'; 13 require_once dirname(__FILE__) . '/filefield_widget.inc'; 14 15 /** 16 * Implementation of hook_init(). 17 */ 18 function filefield_init() { 19 // File hooks and callbacks may be used by any module. 20 drupal_add_css(drupal_get_path('module', 'filefield') .'/filefield.css'); 21 22 // Conditional module support. 23 if (module_exists('token')) { 24 module_load_include('inc', 'filefield', 'filefield.token'); 25 } 26 } 27 28 /** 29 * Implementation of hook_menu(). 30 */ 31 function filefield_menu() { 32 $items = array(); 33 34 $items['filefield/ahah/%/%/%'] = array( 35 'page callback' => 'filefield_js', 36 'page arguments' => array(2, 3, 4), 37 'access callback' => 'filefield_edit_access', 38 'access arguments' => array(2, 3), 39 'type' => MENU_CALLBACK, 40 ); 41 $items['filefield/progress'] = array( 42 'page callback' => 'filefield_progress', 43 'access arguments' => array('access content'), 44 'type' => MENU_CALLBACK, 45 ); 46 47 return $items; 48 } 49 50 /** 51 * Implementation of hook_elements(). 52 */ 53 function filefield_elements() { 54 $elements = array(); 55 $elements['filefield_widget'] = array( 56 '#input' => TRUE, 57 '#columns' => array('fid', 'list', 'data'), 58 '#process' => array('filefield_widget_process'), 59 '#value_callback' => 'filefield_widget_value', 60 '#element_validate' => array('filefield_widget_validate'), 61 ); 62 return $elements; 63 } 64 65 /** 66 * Implementation of hook_theme(). 67 * @todo: autogenerate theme registry entrys for widgets. 68 */ 69 function filefield_theme() { 70 return array( 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 ), 79 'filefield_widget' => array( 80 'arguments' => array('element' => NULL), 81 'file' => 'filefield_widget.inc', 82 ), 83 'filefield_widget_item' => array( 84 'arguments' => array('element' => NULL), 85 'file' => 'filefield_widget.inc', 86 ), 87 'filefield_widget_preview' => array( 88 'arguments' => array('element' => NULL), 89 'file' => 'filefield_widget.inc', 90 ), 91 'filefield_widget_file' => array( 92 'arguments' => array('element' => NULL), 93 'file' => 'filefield_widget.inc', 94 ), 95 96 97 'filefield_formatter_default' => array( 98 'arguments' => array('element' => NULL), 99 'file' => 'filefield_formatter.inc', 100 ), 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 ), 109 'filefield_item' => array( 110 'arguments' => array('file' => NULL, 'field' => NULL), 111 'file' => 'filefield_formatter.inc', 112 ), 113 ); 114 } 115 116 /** 117 * Implementation of hook_file_download(). 118 */ 119 function 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 } 129 130 // If the file is not found in the database, we're not responsible for it. 131 if (empty($file)) { 132 return; 133 } 134 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 } 164 } 165 } 166 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 } 171 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 } 185 if (($node = node_load($content['nid'])) && (node_access('view', $node) && filefield_view_access($field_name, $node))) { 186 $denied = FALSE; 187 break 2; 188 } 189 $nodes[$content['nid']] = $node; 190 } 191 } 192 } 193 194 if ($denied) { 195 return -1; 196 } 197 198 // Access is granted. 199 $name = mime_header_encode($file->filename); 200 $type = mime_header_encode($file->filemime); 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 } 211 return array( 212 'Content-Type: ' . $type . '; name="' . $name . '"', 213 'Content-Length: ' . $file->filesize, 214 'Content-Disposition: ' . $disposition . '; filename="' . $name . '"', 215 'Cache-Control: private', 216 ); 217 } 218 219 /** 220 * Implementation of hook_views_api(). 221 */ 222 function filefield_views_api() { 223 return array( 224 'api' => 2.0, 225 'path' => drupal_get_path('module', 'filefield') . '/views', 226 ); 227 } 228 229 /** 230 * Implementation of hook_form_alter(). 231 * 232 * Set the enctype on forms that need to accept file uploads. 233 */ 234 function filefield_form_alter(&$form, $form_state, $form_id) { 235 // Field configuration (for default images). 236 if ($form_id == 'content_field_edit_form' && isset($form['#field']) && $form['#field']['type'] == 'filefield') { 237 $form['#attributes']['enctype'] = 'multipart/form-data'; 238 } 239 240 // Node forms. 241 if (preg_match('/_node_form$/', $form_id)) { 242 $form['#attributes']['enctype'] = 'multipart/form-data'; 243 } 244 } 245 246 /** 247 * Implementation of CCK's hook_field_info(). 248 */ 249 function filefield_field_info() { 250 return array( 251 'filefield' => array( 252 'label' => t('File'), 253 'description' => t('Store an arbitrary file.'), 254 ), 255 ); 256 } 257 258 /** 259 * Implementation of hook_field_settings(). 260 */ 261 function filefield_field_settings($op, $field) { 262 $return = array(); 263 264 module_load_include('inc', 'filefield', 'filefield_field'); 265 $op = str_replace(' ', '_', $op); 266 $function = 'filefield_field_settings_'. $op; 267 if (function_exists($function)) { 268 $result = $function($field); 269 if (isset($result) && is_array($result)) { 270 $return = $result; 271 } 272 } 273 274 return $return; 275 276 } 277 278 /** 279 * Implementation of CCK's hook_field(). 280 */ 281 function filefield_field($op, $node, $field, &$items, $teaser, $page) { 282 module_load_include('inc', 'filefield', 'filefield_field'); 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); 288 } 289 } 290 291 /** 292 * Implementation of CCK's hook_widget_settings(). 293 */ 294 function filefield_widget_settings($op, $widget) { 295 switch ($op) { 296 case 'form': 297 return filefield_widget_settings_form($widget); 298 case 'save': 299 return filefield_widget_settings_save($widget); 300 } 301 } 302 303 /** 304 * Implementation of hook_widget(). 305 */ 306 function filefield_widget(&$form, &$form_state, $field, $items, $delta = 0) { 307 // CCK doesn't give a validate callback at the field level... 308 // and FAPI's #require is naive to complex structures... 309 // we validate at the field level ourselves. 310 if (empty($form['#validate']) || !in_array('filefield_node_form_validate', $form['#validate'])) { 311 $form['#validate'][] = 'filefield_node_form_validate'; 312 } 313 $form['#attributes']['enctype'] = 'multipart/form-data'; 314 315 module_load_include('inc', $field['widget']['module'], $field['widget']['module'] .'_widget'); 316 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 } 321 $element = array( 322 '#title' => $field['widget']['label'], 323 '#type' => $field['widget']['type'], 324 '#default_value' => $item, 325 '#upload_validators' => filefield_widget_upload_validators($field), 326 ); 327 328 return $element; 329 } 330 331 /** 332 * Get the upload validators for a file field. 333 * 334 * @param $field 335 * A CCK field array. 336 * @return 337 * An array suitable for passing to file_save_upload() or the file field 338 * element's '#upload_validators' property. 339 */ 340 function filefield_widget_upload_validators($field) { 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']); 344 } 345 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 351 $validators = array( 352 // associate the field to the file on validation. 353 'filefield_validate_associate_field' => array($field), 354 'filefield_validate_size' => array($max_filesize), 355 // Override core since it excludes uid 1 on the extension check. 356 // Filefield only excuses uid 1 of quota requirements. 357 'filefield_validate_extensions' => array($field['widget']['file_extensions']), 358 ); 359 return $validators; 360 } 361 362 /** 363 * Implementation of CCK's hook_content_is_empty(). 364 * 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. 370 */ 371 function filefield_content_is_empty($item, $field) { 372 return empty($item['fid']) || (int)$item['fid'] == 0; 373 } 374 375 /** 376 * Implementation of CCK's hook_content_diff_values(). 377 */ 378 function 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 /** 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 */ 396 function filefield_default_value(&$form, &$form_state, $field, $delta) { 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']])) { 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 /** 411 * Implementation of CCK's hook_widget_info(). 412 */ 413 function filefield_widget_info() { 414 return array( 415 'filefield_widget' => array( 416 'label' => t('File Upload'), 417 'field types' => array('filefield'), 418 'multiple values' => CONTENT_HANDLE_CORE, 419 'callbacks' => array('default value' => CONTENT_CALLBACK_CUSTOM), 420 'description' => t('A plain file upload widget.'), 421 'file_extensions' => 'txt', 422 ), 423 ); 424 } 425 426 /** 427 * Implementation of CCK's hook_field_formatter_info(). 428 */ 429 function filefield_field_formatter_info() { 430 return array( 431 'default' => array( 432 'label' => t('Generic files'), 433 'field types' => array('filefield'), 434 'multiple values' => CONTENT_HANDLE_CORE, 435 'description' => t('Displays all kinds of files with an icon and a linked file description.'), 436 ), 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 ), 447 ); 448 } 449 450 /** 451 * Implementation of CCK's hook_content_generate(). Used by generate.module. 452 */ 453 function 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 /** 465 * Get a list of possible information stored in a file field "data" column. 466 */ 467 function 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 */ 489 function 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 */ 508 function filefield_filefield_data_info() { 509 return array( 510 'description' => array( 511 'title' => t('Description'), 512 'callback' => 'check_plain', 513 ), 514 ); 515 } 516 517 /** 518 * Determine the most appropriate icon for the given file's mimetype. 519 * 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. 524 */ 525 function filefield_icon_url($file) { 526 module_load_include('inc', 'filefield', 'filefield.theme'); 527 return _filefield_icon_url($file); 528 } 529 530 /** 531 * Implementation of hook_filefield_icon_sets(). 532 * 533 * Define a list of icon sets and directories that contain the icons. 534 */ 535 function filefield_filefield_icon_sets() { 536 return array( 537 'default' => drupal_get_path('module', 'filefield') . '/icons', 538 ); 539 } 540 541 /** 542 * Access callback for AHAH upload/delete callbacks and node form validation. 543 * 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 */ 547 function filefield_edit_access($type_name, $field_name, $node = NULL) { 548 return content_access('edit', content_fields($field_name, $type_name), NULL, $node); 549 } 550 551 /** 552 * Access callback that checks if the current user may view the filefield. 553 */ 554 function filefield_view_access($field_name, $node = NULL) { 555 return content_access('view', content_fields($field_name), NULL, $node); 556 } 557 558 /** 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. 565 */ 566 function filefield_js($type_name, $field_name, $delta) { 567 $field = content_fields($field_name, $type_name); 568 569 // Immediately disable devel shutdown functions so that it doesn't botch our 570 // JSON output. 571 $GLOBALS['devel_shutdown'] = FALSE; 572 573 if (empty($field) || empty($_POST['form_build_id'])) { 574 // Invalid request. 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'))); 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. 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'))); 589 exit; 590 } 591 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. 602 if (module_exists('fieldgroup') && ($group_name = _fieldgroup_field_get_group($type_name, $field_name))) { 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 } 609 } 610 611 if (!isset($form_element)) { 612 $form_element = $form[$field_name][$delta]; 613 } 614 615 if (isset($form_element['_weight'])) { 616 unset($form_element['_weight']); 617 } 618 619 $output = drupal_render($form_element); 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. 624 625 // Loop through the JS settings and find the settings needed for our buttons. 626 $javascript = drupal_add_js(NULL, NULL); 627 $filefield_ahah_settings = array(); 628 if (isset($javascript['setting'])) { 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 } 638 } 639 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 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 } 652 653 /** 654 * Menu callback for upload progress. 655 */ 656 function 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'])) { 666 $progress['message'] = t('Uploading... (@current of @total)', array('@current' => format_size($status['bytes_uploaded']), '@total' => format_size($status['bytes_total']))); 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'])) { 673 $progress['message'] = t('Uploading... (@current of @total)', array('@current' => format_size($status['current']), '@total' => format_size($status['total']))); 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 */ 684 function 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 /** 702 * Implementation of hook_file_references(). 703 */ 704 function filefield_file_references($file) { 705 $count = filefield_get_file_reference_count($file); 706 return $count ? array('filefield' => $count) : NULL; 707 } 708 709 /** 710 * Implementation of hook_file_delete(). 711 */ 712 function filefield_file_delete($file) { 713 filefield_delete_file_references($file); 714 } 715 716 /** 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. 721 * 722 * @param $file 723 * A Drupal file object. 724 * @param $extensions 725 * A string with a space separated list of allowed extensions. 726 * @return 727 * An array of any errors cause by this file if it failed validation. 728 */ 729 function filefield_validate_extensions($file, $extensions) { 730 global $user; 731 $errors = array(); 732 733 if (!empty($extensions)) { 734 $regex = '/\.('. ereg_replace(' +', '|', preg_quote($extensions)) .')$/i'; 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) { 742 $errors[] = t('The file contents (@type) do not match its extension (@extension).', array('@type' => $type, '@extension' => $extension)); 743 } 744 } 745 } 746 else { 747 $errors[] = t('Only files with the following extensions are allowed: %files-allowed.', array('%files-allowed' => $extensions)); 748 } 749 } 750 751 return $errors; 752 } 753 754 /** 755 * Help text automatically appended to fields that have extension validation. 756 */ 757 function filefield_validate_extensions_help($extensions) { 758 if (!empty($extensions)) { 759 return t('Allowed extensions: %ext', array('%ext' => $extensions)); 760 } 761 else { 762 return ''; 763 } 764 } 765 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 */ 779 function 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 798 /** 799 * Automatic help text appended to fields that have file size validation. 800 */ 801 function filefield_validate_size_help($size) { 802 return t('Maximum file size: %size', array('%size' => format_size(parse_size($size)))); 803 } 804 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 */ 819 function filefield_validate_image_resolution(&$file, $maximum_dimensions = 0, $minimum_dimensions = 0) { 820 $errors = array(); 821 822 @list($max_width, $max_height) = explode('x', $maximum_dimensions); 823 @list($min_width, $min_height) = explode('x', $minimum_dimensions); 824 825 // Check first that the file is an image. 826 if ($info = image_get_info($file->filepath)) { 827 if ($maximum_dimensions) { 828 $resized = FALSE; 829 830 // Check that it is smaller than the given dimensions. 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. 838 elseif ((image_get_toolkit() || module_exists('imageapi')) && (($info['width'] * $ratio < $min_width) || ($info['height'] * $ratio < $min_height))) { 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 } 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); 846 $resized = TRUE; 847 } 848 // Try to resize the image to fit the dimensions. 849 elseif (image_get_toolkit() && @image_scale($file->filepath, $file->filepath, $max_width, $max_height)) { 850 $resized = TRUE; 851 } 852 else { 853 $errors[] = t('The image is too large; the maximum dimensions are %dimensions pixels.', array('%dimensions' => $maximum_dimensions)); 854 } 855 } 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 } 863 } 864 865 if ($minimum_dimensions && empty($errors)) { 866 // Check that it is larger than the given dimensions. 867 if ($info['width'] < $min_width || $info['height'] < $min_height) { 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; 874 } 875 876 /** 877 * Automatic help text appended to fields that have image resolution validation. 878 */ 879 function filefield_validate_image_resolution_help($max_size = '0', $min_size = '0') { 880 if (!empty($max_size)) { 881 if (!empty($min_size)) { 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 } 888 } 889 else { 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 } 896 } 897 } 898 if (!empty($min_size)) { 899 return t('Images must be larger than @max_size pixels', array('@max_size' => $min_size)); 900 } 901 } 902 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 */ 919 function filefield_validate_is_image(&$file) { 920 $errors = array(); 921 $info = image_get_info($file->filepath); 922 if (!$info || empty($info['extension'])) { 923 $errors[] = t('The file is not a known image format.'); 924 } 925 return $errors; 926 } 927 928 /** 929 * An #upload_validators callback. Add the field to the file object. 930 * 931 * This validation function adds the field to the file object for later 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. 935 */ 936 function filefield_validate_associate_field(&$file, $field) { 937 $file->field = $field; 938 return array(); 939 } 940 941 /******************************************************************************* 942 * Public API functions for FileField. 943 ******************************************************************************/ 944 945 /** 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 */ 953 function 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 /** 980 * Count the number of times the file is referenced within a field. 981 * 982 * @param $file 983 * A file object. 984 * @param $field 985 * Optional. The CCK field array or field name as a string. 986 * @return 987 * An integer value. 988 */ 989 function 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 } 1017 } 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 */ 1033 function 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; 1044 } 1045 } 1046 1047 return $references; 1048 } 1049 1050 /** 1051 * Get all FileField files connected to a node ID. 1052 * 1053 * @param $node 1054 * The node object. 1055 * @param $field 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 */ 1060 function filefield_get_node_files($node, $field = NULL) { 1061 $fields = filefield_get_field_list($node->type, $field); 1062 $files = array(); 1063 1064 // Get the file rows. 1065 foreach ($fields as $field) { 1066 $db_info = content_database_info($field); 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'; 1071 $result = db_query($sql, $node->vid); 1072 while ($file = db_fetch_array($result)) { 1073 $file['data'] = unserialize($file['data']); 1074 $files[$file['fid']] = $file; 1075 } 1076 } 1077 1078 return $files; 1079 } 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 */ 1089 function 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 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Jul 9 18:01:44 2012 | Cross-referenced by PHPXref 0.7 |