t('File Attach'), 'label' => t('File Attach'), 'description' => t('Select a file from a local file location on the server.'), 'process' => 'filefield_source_attach_process', 'value' => 'filefield_source_attach_value', ); return $source; } /** * Implementation of hook_theme(). */ function filefield_source_attach_theme() { return array( 'filefield_source_attach_element' => array( 'arguments' => array('element' => NULL), 'file' => 'sources/attach.inc', ), ); } /** * Implementation of hook_filefield_source_settings(). */ function filefield_source_attach_settings($op, $field) { $return = array(); if ($op == 'form') { $return['source_attach'] = array( '#title' => t('File Attach Path'), '#type' => 'fieldset', '#collapsible' => FALSE, '#description' => t('File Attach allows for selecting a file from a local file location on the server, commonly used in combination with FTP.'), ); $return['source_attach']['filefield_source_attach_absolute'] = array( '#type' => 'checkbox', '#title' => t('Use Absolute path'), '#default_value' => $field['filefield_source_attach_absolute'], '#description' => t('If checked file attach will not use Drupal files directory and will try use the file attach path as is.
EG: /nas_storage/user_[uid]/file_attach.
If your File Attach path does not start with a "/" your path will be relative to your sites realpath.
Note: your current realpath=!realpath', array('!realpath' => realpath('./'))), ); $return['source_attach']['filefield_source_attach_path'] = array( '#type' => 'textfield', '#title' => t('File Attach path'), '#default_value' => empty($field['filefield_source_attach_path']) ? 'file_attach' : $field['filefield_source_attach_path'], '#size' => 60, '#maxlength' => 128, '#required' => FALSE, '#description' => t('Subdirectory within the "files/" Directory where files will be stored. If a path is not provided, File Attach will not be active.'), // Future: If you include a preceding slashes it is assumed to be absolute full path EG: "/home/example/myfiles". '#element_validate' => array('_filefield_source_attach_file_path_validate'), '#suffix' => theme('token_help', 'user'), // See: _filefield_source_attach_calc_path() for token replace usage ); } elseif ($op == 'save') { $return[] = 'filefield_source_attach_absolute'; $return[] = 'filefield_source_attach_path'; } return $return; } function _filefield_source_attach_file_path_validate($element, &$form_state) { // Strip slashes from the beginning and end of $widget['file_path'] $form_state['values']['file_path'] = trim($form_state['values']['file_path'], '\\/'); // Do not allow the file path to be the same as the file_directory_path(). // This causes all sorts of problems with things like file_create_url(). if (strpos($form_state['values']['file_path'], file_directory_path()) === 0) { form_error($element, t('The file path (@file_path) cannot start with the system files directory (@files_directory), as this may cause conflicts when building file URLs.', array('@file_path' => $form_state['values']['file_path'], '@files_directory' => file_directory_path()))); } } /** * A #process callback to extend the filefield_widget element type. */ function filefield_source_attach_process($element, $edit, &$form_state, $form) { $field = content_fields($element['#field_name'], $element['#type_name']); $element['filefield_attach'] = array( '#theme' => 'filefield_source_attach_element', '#weight' => 100.5, '#access' => empty($element['fid']['#value']), '#filefield_sources_hint_text' => FILEFIELD_SOURCE_ATTACH_HINT_TEXT, ); $attach_message = FALSE; $allowed_extensions = implode('|', $element['#upload_validators']['filefield_validate_extensions']); $path = _filefield_source_attach_calc_path($field['widget'], $GLOBALS['user']); $options = _filefield_source_attach_get($path, $allowed_extensions); // Error messages if ($options === FALSE || empty($field['widget']['filefield_source_attach_path'])) { $attach_message = t('File attach could not create or locate a valid directory. Please check your settings for this field.'); } elseif (!count($options)) { $attach_message = t('File attach is empty. There are no files to attach.'); } if ($attach_message !== FALSE) { $element['filefield_attach']['attach_message'] = array( '#type' => 'item', '#title' => t('File Attach Error'), '#value' => $attach_message, ); return $element; } $element['filefield_attach']['path'] = array( '#type' => 'select', // '#title' => t('Select attachment'), '#description' => t('Select available files to attach. If a file is larger than the max upload site please use this method to add files. To add files to this select list you need to upload files using FTP.'), '#options' => $options, ); $element['filefield_attach']['transfer'] = array( '#type' => 'submit', '#value' => t('Transfer'), '#submit' => array('node_form_submit_build_node'), '#ahah' => array( 'path' => 'filefield/ahah/'. $element['#type_name'] .'/'. $element['#field_name'] .'/'. $element['#delta'], 'wrapper' => $element['#id'] .'-ahah-wrapper', 'method' => 'replace', 'effect' => 'fade', ), ); return $element; } function _filefield_source_attach_get($path, $allowed_extensions = '.*', $existing = array()) { if (!file_check_directory($path, FILE_CREATE_DIRECTORY)) { drupal_set_message(t('Specified file attach path must exist or be writable.'), 'error'); return FALSE; } $file_exists = array(); $file_attach_options = array(); $file_attach = file_scan_directory($path, '.*', array('.', '..', 'CVS'), 0, TRUE, $key = 'filename', 0, 0); if (count($file_attach)) { $file_attach_options = array(''); foreach ($existing as $file_exist) { if (!in_array(basename($file_exist['filepath']), $file_exists)) { $file_exists[] = basename($file_exist['filepath']); } } foreach ($file_attach as $filename => $fileinfo) { $is_attached = ''; if (in_array($filename, $file_exists)) { $is_attached = '(A)'; } $filepath = str_replace($path, '', $filename); $filepath = trim($filepath, '/'); $file_attach_options[$filepath] = $is_attached . ' ' . $filepath; } } asort($file_attach_options); return $file_attach_options; } function _filefield_source_attach_transliteration_wrapper($file) { require_once (drupal_get_path('module', 'transliteration') . '/transliteration.inc'); $langcode = NULL; if (!empty($_POST['language'])) { $languages = language_list(); $langcode = isset($languages[$_POST['language']]) ? $_POST['language'] : NULL; } $new_file = dirname($file) . '/' . transliteration_clean_filename(basename($file), $langcode); if ($file != $new_file) { rename($file, $new_file); } return $new_file; } /** * A #filefield_value_callback function. */ function filefield_source_attach_value($element, &$item) { if (!empty($item['filefield_attach']['path']) && $item['filefield_attach']['path'] !== '0') { $field = content_fields($element['#field_name'], $element['#type_name']); $attach_path = _filefield_source_attach_calc_path($field['widget']['filefield_source_attach_path'], $GLOBALS['user']); $filename = $attach_path . '/' . $item['filefield_attach']['path']; $temp = file_directory_temp(); if (!file_copy($filename, $temp)) { form_error($element, t('File attach copy error. Please confirm you temp directory is writeable. See !status for setup errors.' , array('!status' => l('status report', 'admin/reports/status')))); // debug error function // form_error($element, t('File attach copy error, debug:
' . '$filename=' . print_r($filename, TRUE) . '$temp=' . print_r($temp, TRUE) . '$element=' . print_r($element, TRUE) . '$item=' . print_r($item, TRUE) . '
')); return; } $filepath = $temp . '/' . basename($filename); // Perform basic extension check on the file before trying to transfer. $extensions = $field['widget']['file_extensions']; $regex = '/\.('. ereg_replace(' +', '|', preg_quote($extensions)) .')$/i'; if (!empty($extensions) && !preg_match($regex, $filename)) { form_error($element, t('Only files with the following extensions are allowed: %files-allowed.', array('%files-allowed' => $extensions))); return; } // transliteration support // We perform transliteration on the temp file location to prevent altering the FTP file store. if (module_exists('transliteration')) { $filepath = _filefield_source_attach_transliteration_wrapper($filepath); } if ($file = field_file_save_file($filepath, array(), filefield_widget_file_path($field))) { $item = array_merge($item, $file); } // Delete the temporary file. @unlink($filepath); } $item['filefield_attach']['path'] = ''; } /** * Theme the output of the autocomplete field. */ function theme_filefield_source_attach_element($element) { if (isset($element['attach_message'])) { $output = '

' . theme('markup', $element['attach_message']) . '

'; } else { $output = theme('select', $element['path']) . theme('submit', $element['transfer']); } return '
' . theme('form_element', $element, $output) . '
'; } function _filefield_source_attach_calc_path($field, $account = NULL) { $account = isset($account) ? $account : $GLOBALS['user']; $path = $field['filefield_source_attach_path']; $absolute = $field['filefield_source_attach_absolute']; // Replace user level tokens. // Node level tokens require a lot of complexity like temporary storage // locations when values don't exist. See the filefield_paths module. if (module_exists('token')) { $path = token_replace($path, 'user', $account); } return $absolute ? $path : file_directory_path() . '/' . $path; }