1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093 |
- <?php
- require_once DRUPAL_ROOT . '/modules/file/file.field.inc';
- function file_help($path, $arg) {
- switch ($path) {
- case 'admin/help#file':
- $output = '';
- $output .= '<h3>' . t('About') . '</h3>';
- $output .= '<p>' . t('The File module defines a <em>File</em> field type for the Field module, which lets you manage and validate uploaded files attached to content on your site (see the <a href="@field-help">Field module help page</a> for more information about fields). For more information, see the online handbook entry for <a href="@file">File module</a>.', array('@field-help' => url('admin/help/field'), '@file' => 'http://drupal.org/documentation/modules/file')) . '</p>';
- $output .= '<h3>' . t('Uses') . '</h3>';
- $output .= '<dl>';
- $output .= '<dt>' . t('Attaching files to content') . '</dt>';
- $output .= '<dd>' . t('The File module allows users to attach files to content (e.g., PDF files, spreadsheets, etc.), when a <em>File</em> field is added to a given content type using the <a href="@fieldui-help">Field UI module</a>. You can add validation options to your File field, such as specifying a maximum file size and allowed file extensions.', array('@fieldui-help' => url('admin/help/field_ui'))) . '</dd>';
- $output .= '<dt>' . t('Managing attachment display') . '</dt>';
- $output .= '<dd>' . t('When you attach a file to content, you can specify whether it is <em>listed</em> or not. Listed files are displayed automatically in a section at the bottom of your content; non-listed files are available for embedding in your content, but are not included in the list at the bottom.') . '</dd>';
- $output .= '<dt>' . t('Managing file locations') . '</dt>';
- $output .= '<dd>' . t("When you create a File field, you can specify a directory where the files will be stored, which can be within either the <em>public</em> or <em>private</em> files directory. Files in the public directory can be accessed directly through the web server; when public files are listed, direct links to the files are used, and anyone who knows a file's URL can download the file. Files in the private directory are not accessible directly through the web server; when private files are listed, the links are Drupal path requests. This adds to server load and download time, since Drupal must start up and resolve the path for each file download request, but allows for access restrictions.") . '</dd>';
- $output .= '</dl>';
- return $output;
- }
- }
- function file_menu() {
- $items = array();
- $items['file/ajax'] = array(
- 'page callback' => 'file_ajax_upload',
- 'delivery callback' => 'ajax_deliver',
- 'access arguments' => array('access content'),
- 'theme callback' => 'ajax_base_page_theme',
- 'type' => MENU_CALLBACK,
- );
- $items['file/progress'] = array(
- 'page callback' => 'file_ajax_progress',
- 'access arguments' => array('access content'),
- 'theme callback' => 'ajax_base_page_theme',
- 'type' => MENU_CALLBACK,
- );
- return $items;
- }
- function file_element_info() {
- $file_path = drupal_get_path('module', 'file');
- $types['managed_file'] = array(
- '#input' => TRUE,
- '#process' => array('file_managed_file_process'),
- '#value_callback' => 'file_managed_file_value',
- '#element_validate' => array('file_managed_file_validate'),
- '#pre_render' => array('file_managed_file_pre_render'),
- '#theme' => 'file_managed_file',
- '#theme_wrappers' => array('form_element'),
- '#progress_indicator' => 'throbber',
- '#progress_message' => NULL,
- '#upload_validators' => array(),
- '#upload_location' => NULL,
- '#size' => 22,
- '#extended' => FALSE,
- '#attached' => array(
- 'css' => array($file_path . '/file.css'),
- 'js' => array($file_path . '/file.js'),
- ),
- );
- return $types;
- }
- function file_theme() {
- return array(
-
- 'file_link' => array(
- 'variables' => array('file' => NULL, 'icon_directory' => NULL),
- ),
- 'file_icon' => array(
- 'variables' => array('file' => NULL, 'icon_directory' => NULL, 'alt' => ''),
- ),
- 'file_managed_file' => array(
- 'render element' => 'element',
- ),
-
- 'file_widget' => array(
- 'render element' => 'element',
- ),
- 'file_widget_multiple' => array(
- 'render element' => 'element',
- ),
- 'file_formatter_table' => array(
- 'variables' => array('items' => NULL),
- ),
- 'file_upload_help' => array(
- 'variables' => array('description' => NULL, 'upload_validators' => NULL),
- ),
- );
- }
- function file_file_download($uri, $field_type = 'file') {
- global $user;
-
- $files = file_load_multiple(array(), array('uri' => $uri));
- if (count($files)) {
- foreach ($files as $item) {
-
-
- if ($item->uri === $uri) {
- $file = $item;
- break;
- }
- }
- }
- if (!isset($file)) {
- return;
- }
-
- $references = file_get_file_references($file, NULL, FIELD_LOAD_CURRENT, $field_type);
-
-
-
-
-
- if (empty($references) && ($file->status == FILE_STATUS_PERMANENT || $file->uid != $user->uid)) {
- return;
- }
-
- $denied = FALSE;
-
-
-
-
-
-
- foreach ($references as $field_name => $field_references) {
- foreach ($field_references as $entity_type => $type_references) {
- foreach ($type_references as $id => $reference) {
-
- $entity = entity_load($entity_type, array($id));
- $entity = reset($entity);
- $field = field_info_field($field_name);
-
- $field_item = NULL;
- if ($entity) {
-
- $field_items = field_get_items($entity_type, $entity, $field_name);
-
- foreach ($field_items as $item) {
- if ($item['uri'] == $uri) {
- $field_item = $item;
- break;
- }
- }
- }
-
-
-
- if (empty($entity) || empty($field) || empty($field_item) || !field_access('view', $field, $entity_type, $entity)) {
- $denied = TRUE;
- break;
- }
-
-
- $grants = array('system' => FALSE);
- foreach (module_implements('file_download_access') as $module) {
- $grants = array_merge($grants, array($module => module_invoke($module, 'file_download_access', $field_item, $entity_type, $entity)));
- }
-
- drupal_alter('file_download_access', $grants, $field_item, $entity_type, $entity);
- if (in_array(TRUE, $grants)) {
-
-
- $denied = FALSE;
- break 3;
- }
- if (in_array(FALSE, $grants)) {
-
-
-
- $denied = TRUE;
- }
- }
- }
- }
-
- if ($denied) {
- return -1;
- }
-
- $headers = file_get_content_headers($file);
- return $headers;
- }
- function file_ajax_upload() {
- $form_parents = func_get_args();
- $form_build_id = (string) array_pop($form_parents);
- if (empty($_POST['form_build_id']) || $form_build_id != $_POST['form_build_id']) {
-
- 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');
- $commands = array();
- $commands[] = ajax_command_replace(NULL, theme('status_messages'));
- return array('#type' => 'ajax', '#commands' => $commands);
- }
- list($form, $form_state, $form_id, $form_build_id, $commands) = ajax_get_form();
- if (!$form) {
-
- drupal_set_message(t('An unrecoverable error occurred. Use of this form has expired. Try reloading the page and submitting again.'), 'error');
- $commands = array();
- $commands[] = ajax_command_replace(NULL, theme('status_messages'));
- return array('#type' => 'ajax', '#commands' => $commands);
- }
-
- $current_element = $form;
- foreach ($form_parents as $parent) {
- $current_element = $current_element[$parent];
- }
- $current_file_count = isset($current_element['#file_upload_delta']) ? $current_element['#file_upload_delta'] : 0;
-
- drupal_process_form($form['#form_id'], $form, $form_state);
-
- foreach ($form_parents as $parent) {
- $form = $form[$parent];
- }
-
- if (isset($form['#file_upload_delta']) && $current_file_count < $form['#file_upload_delta']) {
- $form[$current_file_count]['#attributes']['class'][] = 'ajax-new-content';
- }
-
- else {
- $form['#suffix'] .= '<span class="ajax-new-content"></span>';
- }
- $form['#prefix'] .= theme('status_messages');
- $output = drupal_render($form);
- $js = drupal_add_js();
- $settings = call_user_func_array('array_merge_recursive', $js['settings']['data']);
- $commands[] = ajax_command_replace(NULL, $output, $settings);
- return array('#type' => 'ajax', '#commands' => $commands);
- }
- function file_ajax_progress($key) {
- $progress = array(
- 'message' => t('Starting upload...'),
- 'percentage' => -1,
- );
- $implementation = file_progress_implementation();
- if ($implementation == 'uploadprogress') {
- $status = uploadprogress_get_info($key);
- if (isset($status['bytes_uploaded']) && !empty($status['bytes_total'])) {
- $progress['message'] = t('Uploading... (@current of @total)', array('@current' => format_size($status['bytes_uploaded']), '@total' => format_size($status['bytes_total'])));
- $progress['percentage'] = round(100 * $status['bytes_uploaded'] / $status['bytes_total']);
- }
- }
- elseif ($implementation == 'apc') {
- $status = apc_fetch('upload_' . $key);
- if (isset($status['current']) && !empty($status['total'])) {
- $progress['message'] = t('Uploading... (@current of @total)', array('@current' => format_size($status['current']), '@total' => format_size($status['total'])));
- $progress['percentage'] = round(100 * $status['current'] / $status['total']);
- }
- }
- drupal_json_output($progress);
- }
- function file_progress_implementation() {
- static $implementation;
- if (!isset($implementation)) {
- $implementation = FALSE;
-
-
- if (extension_loaded('uploadprogress')) {
- $implementation = 'uploadprogress';
- }
- elseif (extension_loaded('apc') && ini_get('apc.rfc1867')) {
- $implementation = 'apc';
- }
- }
- return $implementation;
- }
- function file_file_delete($file) {
-
- }
- function file_managed_file_process($element, &$form_state, $form) {
-
-
- $original_id = $element['#id'];
- $element['#id'] .= '-upload';
- $fid = isset($element['#value']['fid']) ? $element['#value']['fid'] : 0;
-
- $element['#progress_indicator'] = empty($element['#progress_indicator']) ? 'none' : $element['#progress_indicator'];
- $element['#file'] = $fid ? file_load($fid) : FALSE;
- $element['#tree'] = TRUE;
- $ajax_settings = array(
- 'path' => 'file/ajax/' . implode('/', $element['#array_parents']) . '/' . $form['form_build_id']['#value'],
- 'wrapper' => $original_id . '-ajax-wrapper',
- 'effect' => 'fade',
- 'progress' => array(
- 'type' => $element['#progress_indicator'],
- 'message' => $element['#progress_message'],
- ),
- );
-
- $element['upload_button'] = array(
- '#name' => implode('_', $element['#parents']) . '_upload_button',
- '#type' => 'submit',
- '#value' => t('Upload'),
- '#validate' => array(),
- '#submit' => array('file_managed_file_submit'),
- '#limit_validation_errors' => array($element['#parents']),
- '#ajax' => $ajax_settings,
- '#weight' => -5,
- );
-
-
- $ajax_settings['progress']['type'] = ($element['#progress_indicator'] == 'none') ? 'none' : 'throbber';
- $ajax_settings['progress']['message'] = NULL;
- $ajax_settings['effect'] = 'none';
- $element['remove_button'] = array(
- '#name' => implode('_', $element['#parents']) . '_remove_button',
- '#type' => 'submit',
- '#value' => t('Remove'),
- '#validate' => array(),
- '#submit' => array('file_managed_file_submit'),
- '#limit_validation_errors' => array($element['#parents']),
- '#ajax' => $ajax_settings,
- '#weight' => -5,
- );
- $element['fid'] = array(
- '#type' => 'hidden',
- '#value' => $fid,
- );
-
- if ($element['#progress_indicator'] == 'bar' && $implementation = file_progress_implementation()) {
- $upload_progress_key = mt_rand();
- if ($implementation == 'uploadprogress') {
- $element['UPLOAD_IDENTIFIER'] = array(
- '#type' => 'hidden',
- '#value' => $upload_progress_key,
- '#attributes' => array('class' => array('file-progress')),
-
-
- '#weight' => -20,
- );
- }
- elseif ($implementation == 'apc') {
- $element['APC_UPLOAD_PROGRESS'] = array(
- '#type' => 'hidden',
- '#value' => $upload_progress_key,
- '#attributes' => array('class' => array('file-progress')),
-
-
- '#weight' => -20,
- );
- }
-
- $element['upload_button']['#ajax']['progress']['path'] = 'file/progress/' . $upload_progress_key;
- }
-
- $element['upload'] = array(
- '#name' => 'files[' . implode('_', $element['#parents']) . ']',
- '#type' => 'file',
- '#title' => t('Choose a file'),
- '#title_display' => 'invisible',
- '#size' => $element['#size'],
- '#theme_wrappers' => array(),
- '#weight' => -10,
- );
- if ($fid && $element['#file']) {
- $element['filename'] = array(
- '#type' => 'markup',
- '#markup' => theme('file_link', array('file' => $element['#file'])) . ' ',
- '#weight' => -10,
- );
-
-
-
-
-
- if (!$GLOBALS['user']->uid && $element['#file']->status != FILE_STATUS_PERMANENT) {
- $element['fid_token'] = array(
- '#type' => 'hidden',
- '#value' => drupal_hmac_base64('file-' . $fid, drupal_get_private_key() . drupal_get_hash_salt()),
- );
- }
- }
-
- if (isset($element['#upload_validators']['file_validate_extensions'][0])) {
- $extension_list = implode(',', array_filter(explode(' ', $element['#upload_validators']['file_validate_extensions'][0])));
- $element['upload']['#attached']['js'] = array(
- array(
- 'type' => 'setting',
- 'data' => array('file' => array('elements' => array('#' . $element['#id'] => $extension_list)))
- )
- );
- }
-
- $element['#prefix'] = '<div id="' . $original_id . '-ajax-wrapper">';
- $element['#suffix'] = '</div>';
- return $element;
- }
- function file_managed_file_value(&$element, $input = FALSE, $form_state = NULL) {
- $fid = 0;
- $force_default = FALSE;
-
- $form_state_fid = $form_state['values'];
- foreach ($element['#parents'] as $parent) {
- $form_state_fid = isset($form_state_fid[$parent]) ? $form_state_fid[$parent] : 0;
- }
- if ($element['#extended'] && isset($form_state_fid['fid'])) {
- $fid = $form_state_fid['fid'];
- }
- elseif (is_numeric($form_state_fid)) {
- $fid = $form_state_fid;
- }
-
- if ($input !== FALSE) {
- $return = $input;
-
- if ($file = file_managed_file_save_upload($element)) {
- $fid = $file->fid;
- }
- else {
-
-
-
-
- if (isset($element['#file_value_callbacks'])) {
- foreach ($element['#file_value_callbacks'] as $callback) {
- $callback($element, $input, $form_state);
- }
- }
-
-
-
- if (isset($input['fid']) && ($file = file_load($input['fid']))) {
-
-
-
-
-
-
-
-
-
-
- if (!in_array(file_uri_scheme($file->uri), variable_get('file_public_schema', array('public'))) && !file_download_access($file->uri)) {
- $force_default = TRUE;
- }
-
- elseif ($file->status != FILE_STATUS_PERMANENT) {
- if ($GLOBALS['user']->uid && $file->uid != $GLOBALS['user']->uid) {
- $force_default = TRUE;
- }
-
-
-
-
-
- elseif (!$GLOBALS['user']->uid) {
- $token = drupal_array_get_nested_value($form_state['input'], array_merge($element['#parents'], array('fid_token')));
- if ($token !== drupal_hmac_base64('file-' . $file->fid, drupal_get_private_key() . drupal_get_hash_salt())) {
- $force_default = TRUE;
- }
- }
- }
-
- if (!$force_default) {
- $fid = $file->fid;
- }
- }
- }
- }
-
-
- if ($input === FALSE || $force_default) {
- if ($element['#extended']) {
- $default_fid = isset($element['#default_value']['fid']) ? $element['#default_value']['fid'] : 0;
- $return = isset($element['#default_value']) ? $element['#default_value'] : array('fid' => 0);
- }
- else {
- $default_fid = isset($element['#default_value']) ? $element['#default_value'] : 0;
- $return = array('fid' => 0);
- }
-
- if ($default_fid && $file = file_load($default_fid)) {
- $fid = $file->fid;
- }
- }
- $return['fid'] = $fid;
- return $return;
- }
- function file_managed_file_validate(&$element, &$form_state) {
-
-
-
- $clicked_button = end($form_state['triggering_element']['#parents']);
- if ($clicked_button != 'remove_button' && !empty($element['fid']['#value'])) {
- if ($file = file_load($element['fid']['#value'])) {
- if ($file->status == FILE_STATUS_PERMANENT) {
- $references = file_usage_list($file);
- if (empty($references)) {
- form_error($element, t('The file used in the !name field may not be referenced.', array('!name' => $element['#title'])));
- }
- }
- }
- else {
- form_error($element, t('The file referenced by the !name field does not exist.', array('!name' => $element['#title'])));
- }
- }
-
- if ($element['#required'] && empty($element['fid']['#value']) && !in_array($clicked_button, array('upload_button', 'remove_button'))) {
- form_error($element['upload'], t('!name field is required.', array('!name' => $element['#title'])));
- }
-
- if (!$element['#extended']) {
- form_set_value($element, $element['fid']['#value'], $form_state);
- }
- }
- function file_managed_file_submit($form, &$form_state) {
-
-
- $parents = $form_state['triggering_element']['#array_parents'];
- $button_key = array_pop($parents);
- $element = drupal_array_get_nested_value($form, $parents);
-
-
-
-
- if ($button_key == 'remove_button') {
-
-
- if ($element['#file'] && $element['#file']->status == 0) {
- file_delete($element['#file']);
- }
-
-
-
-
-
-
-
- $values_element = $element['#extended'] ? $element['fid'] : $element;
- form_set_value($values_element, NULL, $form_state);
- drupal_array_set_nested_value($form_state['input'], $values_element['#parents'], NULL);
- }
-
-
-
-
-
-
- $form_state['rebuild'] = TRUE;
- }
- function file_managed_file_save_upload($element) {
- $upload_name = implode('_', $element['#parents']);
- if (empty($_FILES['files']['name'][$upload_name])) {
- return FALSE;
- }
- $destination = isset($element['#upload_location']) ? $element['#upload_location'] : NULL;
- if (isset($destination) && !file_prepare_directory($destination, FILE_CREATE_DIRECTORY)) {
- watchdog('file', 'The upload directory %directory for the file field !name could not be created or is not accessible. A newly uploaded file could not be saved in this directory as a consequence, and the upload was canceled.', array('%directory' => $destination, '!name' => $element['#field_name']));
- form_set_error($upload_name, t('The file could not be uploaded.'));
- return FALSE;
- }
- if (!$file = file_save_upload($upload_name, $element['#upload_validators'], $destination)) {
- watchdog('file', 'The file upload failed. %upload', array('%upload' => $upload_name));
- form_set_error($upload_name, t('The file in the !name field was unable to be uploaded.', array('!name' => $element['#title'])));
- return FALSE;
- }
- return $file;
- }
- function theme_file_managed_file($variables) {
- $element = $variables['element'];
- $attributes = array();
- if (isset($element['#id'])) {
- $attributes['id'] = $element['#id'];
- }
- if (!empty($element['#attributes']['class'])) {
- $attributes['class'] = (array) $element['#attributes']['class'];
- }
- $attributes['class'][] = 'form-managed-file';
-
- $output = '';
- $output .= '<div' . drupal_attributes($attributes) . '>';
- $output .= drupal_render_children($element);
- $output .= '</div>';
- return $output;
- }
- function file_managed_file_pre_render($element) {
-
- if (!empty($element['#value']['fid'])) {
- $element['upload']['#access'] = FALSE;
- $element['upload_button']['#access'] = FALSE;
- }
-
- else {
- $element['remove_button']['#access'] = FALSE;
- }
- return $element;
- }
- function theme_file_link($variables) {
- $file = $variables['file'];
- $icon_directory = $variables['icon_directory'];
- $url = file_create_url($file->uri);
-
- $mime_name = array(
- 'application/msword' => t('Microsoft Office document icon'),
- 'application/vnd.ms-excel' => t('Office spreadsheet icon'),
- 'application/vnd.ms-powerpoint' => t('Office presentation icon'),
- 'application/pdf' => t('PDF icon'),
- 'video/quicktime' => t('Movie icon'),
- 'audio/mpeg' => t('Audio icon'),
- 'audio/wav' => t('Audio icon'),
- 'image/jpeg' => t('Image icon'),
- 'image/png' => t('Image icon'),
- 'image/gif' => t('Image icon'),
- 'application/zip' => t('Package icon'),
- 'text/html' => t('HTML icon'),
- 'text/plain' => t('Plain text icon'),
- 'application/octet-stream' => t('Binary Data'),
- );
- $mimetype = file_get_mimetype($file->uri);
- $icon = theme('file_icon', array(
- 'file' => $file,
- 'icon_directory' => $icon_directory,
- 'alt' => !empty($mime_name[$mimetype]) ? $mime_name[$mimetype] : t('File'),
- ));
-
-
- $options = array(
- 'attributes' => array(
- 'type' => $file->filemime . '; length=' . $file->filesize,
- ),
- );
-
- if (empty($file->description)) {
- $link_text = $file->filename;
- }
- else {
- $link_text = $file->description;
- $options['attributes']['title'] = check_plain($file->filename);
- }
- return '<span class="file">' . $icon . ' ' . l($link_text, $url, $options) . '</span>';
- }
- function theme_file_icon($variables) {
- $file = $variables['file'];
- $alt = $variables['alt'];
- $icon_directory = $variables['icon_directory'];
- $mime = check_plain($file->filemime);
- $icon_url = file_icon_url($file, $icon_directory);
- return '<img class="file-icon" alt="' . check_plain($alt) . '" title="' . $mime . '" src="' . $icon_url . '" />';
- }
- function file_icon_url($file, $icon_directory = NULL) {
- if ($icon_path = file_icon_path($file, $icon_directory)) {
- return base_path() . $icon_path;
- }
- return FALSE;
- }
- function file_icon_path($file, $icon_directory = NULL) {
-
- if (!isset($icon_directory)) {
- $icon_directory = variable_get('file_icon_directory', drupal_get_path('module', 'file') . '/icons');
- }
-
- $dashed_mime = strtr($file->filemime, array('/' => '-'));
- $icon_path = $icon_directory . '/' . $dashed_mime . '.png';
- if (file_exists($icon_path)) {
- return $icon_path;
- }
-
- $generic_mime = (string) file_icon_map($file);
- $icon_path = $icon_directory . '/' . $generic_mime . '.png';
- if ($generic_mime && file_exists($icon_path)) {
- return $icon_path;
- }
-
- foreach (array('audio', 'image', 'text', 'video') as $category) {
- if (strpos($file->filemime, $category . '/') === 0) {
- $icon_path = $icon_directory . '/' . $category . '-x-generic.png';
- if (file_exists($icon_path)) {
- return $icon_path;
- }
- }
- }
-
- $icon_path = $icon_directory . '/application-octet-stream.png';
- if (file_exists($icon_path)) {
- return $icon_path;
- }
-
- return FALSE;
- }
- function file_icon_map($file) {
- switch ($file->filemime) {
-
- case 'application/msword':
- case 'application/vnd.ms-word.document.macroEnabled.12':
- case 'application/vnd.oasis.opendocument.text':
- case 'application/vnd.oasis.opendocument.text-template':
- case 'application/vnd.oasis.opendocument.text-master':
- case 'application/vnd.oasis.opendocument.text-web':
- case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
- case 'application/vnd.stardivision.writer':
- case 'application/vnd.sun.xml.writer':
- case 'application/vnd.sun.xml.writer.template':
- case 'application/vnd.sun.xml.writer.global':
- case 'application/vnd.wordperfect':
- case 'application/x-abiword':
- case 'application/x-applix-word':
- case 'application/x-kword':
- case 'application/x-kword-crypt':
- return 'x-office-document';
-
- case 'application/vnd.ms-excel':
- case 'application/vnd.ms-excel.sheet.macroEnabled.12':
- case 'application/vnd.oasis.opendocument.spreadsheet':
- case 'application/vnd.oasis.opendocument.spreadsheet-template':
- case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
- case 'application/vnd.stardivision.calc':
- case 'application/vnd.sun.xml.calc':
- case 'application/vnd.sun.xml.calc.template':
- case 'application/vnd.lotus-1-2-3':
- case 'application/x-applix-spreadsheet':
- case 'application/x-gnumeric':
- case 'application/x-kspread':
- case 'application/x-kspread-crypt':
- return 'x-office-spreadsheet';
-
- case 'application/vnd.ms-powerpoint':
- case 'application/vnd.ms-powerpoint.presentation.macroEnabled.12':
- case 'application/vnd.oasis.opendocument.presentation':
- case 'application/vnd.oasis.opendocument.presentation-template':
- case 'application/vnd.openxmlformats-officedocument.presentationml.presentation':
- case 'application/vnd.stardivision.impress':
- case 'application/vnd.sun.xml.impress':
- case 'application/vnd.sun.xml.impress.template':
- case 'application/x-kpresenter':
- return 'x-office-presentation';
-
- case 'application/zip':
- case 'application/x-zip':
- case 'application/stuffit':
- case 'application/x-stuffit':
- case 'application/x-7z-compressed':
- case 'application/x-ace':
- case 'application/x-arj':
- case 'application/x-bzip':
- case 'application/x-bzip-compressed-tar':
- case 'application/x-compress':
- case 'application/x-compressed-tar':
- case 'application/x-cpio-compressed':
- case 'application/x-deb':
- case 'application/x-gzip':
- case 'application/x-java-archive':
- case 'application/x-lha':
- case 'application/x-lhz':
- case 'application/x-lzop':
- case 'application/x-rar':
- case 'application/x-rpm':
- case 'application/x-tzo':
- case 'application/x-tar':
- case 'application/x-tarz':
- case 'application/x-tgz':
- return 'package-x-generic';
-
- case 'application/ecmascript':
- case 'application/javascript':
- case 'application/mathematica':
- case 'application/vnd.mozilla.xul+xml':
- case 'application/x-asp':
- case 'application/x-awk':
- case 'application/x-cgi':
- case 'application/x-csh':
- case 'application/x-m4':
- case 'application/x-perl':
- case 'application/x-php':
- case 'application/x-ruby':
- case 'application/x-shellscript':
- case 'text/vnd.wap.wmlscript':
- case 'text/x-emacs-lisp':
- case 'text/x-haskell':
- case 'text/x-literate-haskell':
- case 'text/x-lua':
- case 'text/x-makefile':
- case 'text/x-matlab':
- case 'text/x-python':
- case 'text/x-sql':
- case 'text/x-tcl':
- return 'text-x-script';
-
- case 'application/xhtml+xml':
- return 'text-html';
-
- case 'application/x-macbinary':
- case 'application/x-ms-dos-executable':
- case 'application/x-pef-executable':
- return 'application-x-executable';
- default:
- return FALSE;
- }
- }
- function file_get_file_references($file, $field = NULL, $age = FIELD_LOAD_REVISION, $field_type = 'file') {
- $references = drupal_static(__FUNCTION__, array());
- $fields = isset($field) ? array($field['field_name'] => $field) : field_info_fields();
- foreach ($fields as $field_name => $file_field) {
- if ((empty($field_type) || $file_field['type'] == $field_type) && !isset($references[$field_name])) {
-
- $query = new EntityFieldQuery();
- $query
- ->fieldCondition($file_field, 'fid', $file->fid)
- ->age($age);
- $references[$field_name] = $query->execute();
- }
- }
- return isset($field) ? $references[$field['field_name']] : array_filter($references);
- }
|