'filefield/js', 'callback' => 'filefield_js', //'access' => user_access(), 'access' => TRUE, 'type' => MENU_CALLBACK ); } else if ($_SESSION['filefield']) { // Add handlers for previewing new uploads. foreach ($_SESSION['filefield'] as $fieldname => $files) { if (is_array($files)) { foreach($files as $delta => $file) { if ($file['preview']) { $items[] = array( 'path' => $file['preview'], 'callback' => '_filefield_preview', 'access' => TRUE, 'type' => MENU_CALLBACK ); } } } } } return $items; } /** * transfer a file that is in a 'preview' state. * @todo multiple support */ function _filefield_preview() { foreach ($_SESSION['filefield'] as $fieldname => $files) { foreach ($files as $delta => $file) { if ($file['preview'] == $_GET['q']) { file_transfer($file['filepath'], array('Content-Type: '. mime_header_encode($file['filemime']), 'Content-Length: '. $file['filesize'])); exit(); } } } } function filefield_perm() { return array('view filefield uploads'); } /** * Implementation of hook_field_info(). */ function filefield_field_info() { return array( 'file' => array('label' => 'File'), ); } /** * Implementation of hook_field_settings(). */ function filefield_field_settings($op, $field) { switch ($op) { case 'form': $form = array(); return $form; case 'validate': break; case 'save': return array(); case 'database columns': $columns = array( 'fid' => array('type' => 'int', 'not null' => TRUE, 'default' => '0'), 'description' => array('type' => 'varchar', length => 255, 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE), 'list' => array('type' => 'int', 'not null' => TRUE, 'default' => '0'), ); return $columns; } } function filefield_default_item() { return array( 'fid' => 0, 'description' => '', 'list' => 0, ); } /** * insert a file into the database. * @param $node * node object file will be associated with. * @param $file * file to be inserted, passed by reference since fid should be attached. * */ function filefield_file_insert($node, $field, &$file) { $fieldname = $field['field_name']; // allow tokenized paths. if (function_exists('token_replace')) { global $user; $widget_file_path = token_replace($field['widget']['file_path'], 'user', $user); } else { $widget_file_path = $field['widget']['file_path']; } $filepath = file_create_path($widget_file_path) . '/' . $file['filename']; if (filefield_check_directory($widget_file_path) && $file = file_save_upload((object)$file, $filepath)) { $file = (array)$file; $file['fid'] = db_next_id('{files}_fid'); db_query('INSERT into {files} (fid, nid, filename, filepath, filemime, filesize) VALUES (%d, %d, "%s","%s","%s",%d)', $file['fid'], $node->nid, $file['filename'], $file['filepath'], $file['filemime'], $file['filesize']); module_invoke_all('filefield', 'file_save', $node, $field, $file); return (array)$file; } else { // Include file name in upload error. form_set_error(NULL, t('File upload was unsuccessful.')); return FALSE; } } /** * update the file record if necessary * @param $node * @param $file * @param $field */ function filefield_file_update($node, $field, &$file) { $file = (array)$file; if ($file['delete'] == TRUE) { module_invoke_all('filefield', 'file_delete', $node, $field, $file); _filefield_file_delete($node, $field, $file); return array(); } if ($file['fid'] == 'upload') { return filefield_file_insert($node, $field, $file); } else { // if fid is not numeric here we should complain. // else we update the file table. } return $file; } /** * Implementation of hook_field(). */ function filefield_field($op, $node, $field, &$node_field, $teaser, $page) { $fieldname = $field['field_name']; switch ($op) { // called after content.module loads default data. case 'load': if (count($node_field)) { foreach ($node_field as $delta => $file) { if (!empty($file)) { $file = _filefield_file_load($file['fid']); // In certain cases, content.module calls this function with // $node_field[$delta] set, even if the field has not yet been // stored at all or has already been deleted. In that case, // $file['fid'] == 0 and file_load() returns an empty array. // When that happens, unset() the delta so that subsequent hooks // are not bothered. if (empty($file)) { unset($node_field[$delta]); } else { // otherwise, merge our info with CCK's, and all is fine. $node_field[$delta] = array_merge((array)$node_field[$delta], $file); } } } $node_field = array_values($node_field); // compact deltas return array($fieldname => $node_field); } return array(); // called before content.module defaults. case 'insert': foreach ($node_field as $delta => $item) { $node_field[$delta] = filefield_file_insert($node, $field, $item); // Remove non-existant files from node_field if (empty($node_field[$delta])) { unset($node_field[$delta]); } } $node_field = array_values($node_field); // compact deltas filefield_clear_field_session($fieldname); break; // called before content.module defaults. case 'update': foreach ($node_field as $delta => $item) { $node_field[$delta] = filefield_file_update($node, $field, $item); // If the file has been deleted, unset the file entry so that it's // actually deleted from the database, or at least set it to a // default item if CCK won't delete it. if (empty($node_field[$delta])) { if ($field['multiple']) { unset($node_field[$delta]); } else { $node_field[$delta] = filefield_default_item(); } } } $node_field = array_values($node_field); // compact deltas filefield_clear_field_session($fieldname); break; case 'delete': foreach ($node_field as $delta => $item) { _filefield_file_delete($node, $field, $item); } break; } } /** * Implementation of hook_widget_info(). */ function filefield_widget_info() { return array( 'file' => array( 'label' => 'File', 'field types' => array('file'), ), ); } /** * Implementation of hook_widget_settings(). */ function filefield_widget_settings($op, $widget) { switch ($op) { case 'callbacks': return array('default value' => CONTENT_CALLBACK_CUSTOM); case 'form': $form = array(); $form['file_extensions'] = array ( '#type' => 'textfield', '#title' => t('Permitted upload file extensions'), '#default_value' => $widget['file_extensions'] ? $widget['file_extensions'] : 'txt', '#size' => 64, '#description' => t('Extensions a user can upload to this field. Separate extensions with a space and do not include the leading dot.'), ); $form['file_path'] = array( '#type' => 'textfield', '#title' => t('File path'), '#default_value' => $widget['file_path'] ? $widget['file_path'] : '', '#description' => t('Optional subdirectory within the "%dir" directory where files will be stored. Do not include trailing slash.', array('%dir' => variable_get('file_directory_path', 'files'))), '#after_build' => array('filefield_form_check_directory'), ); if (function_exists('token_replace')) { $form['file_path']['#description'] .= theme('token_help', 'user'); } $form['show_list'] = array( '#type' => 'checkbox', '#title' => t('Show "List" option'), '#default_value' => isset($widget['show_list']) ? $widget['show_list'] : 1, '#description' => t('If enabled, the user can choose for each file whether it should be listed or not. If disabled, the "List" checkbox will be hidden and files are always listed.'), ); return $form; case 'validate': break; case 'save': return array('file_extensions', 'file_path', 'show_list'); } } function filefield_clear_session() { if (is_array($_SESSION['filefield']) && count($_SESSION['filefield'])) { foreach (array_keys($_SESSION['filefield']) as $fieldname) { filefield_clear_field_session($fieldname); } unset($_SESSION['filefield']); } } function filefield_clear_field_session($fieldname) { if (is_array($_SESSION['filefield'][$fieldname]) && count($_SESSION['filefield'][$fieldname])) { foreach ($_SESSION['filefield'][$fieldname] as $delta => $file) { if (is_file($file['filepath'])) { file_delete($file['filepath']); } } unset($_SESSION['filefield'][$fieldname]); } } function _filefield_file_delete($node, $field, $file) { if (is_numeric($file['fid'])) { db_query('DELETE FROM {files} WHERE fid = %d', $file['fid']); } else { unset($_SESSION['filefield'][$field['field_name']][$file['sessionid']]); } module_invoke_all('filefield', 'delete', $node, $field, $file); return file_delete($file['filepath']); } /** * Implementation of hook_widget(). */ function filefield_widget($op, $node, $field, &$node_field) { $fieldname = $field['field_name']; switch ($op) { case 'default value': return array(); case 'prepare form values': _filefield_widget_prepare_form_values($node, $field, $node_field); break; case 'form': return _filefield_widget_form($node, $field, $node_field); case 'validate': _filefield_widget_validate($node, $field, $node_field); break; } } function _filefield_widget_prepare_form_values($node, $field, &$node_field) { $fieldname = $field['field_name']; // @todo split this into its own function. determine if we can make it a form element. if (!count($_POST)) { filefield_clear_session(); } // Attach new files if ($file = file_check_upload($fieldname . '_upload')) { $file = (array)$file; // test allowed extensions. We do this when the file is uploaded, rather than waiting for the // field itseld to reach op==validate. $last_ext = array_pop(explode('.', $file['filename'])); $allowed_extensions = array_unique(explode(' ', trim($field['widget']['file_extensions']))); $valid = FALSE; foreach ($allowed_extensions as $allowed_ext) { $allowed_ext = '.'. $allowed_ext; $file_ext = substr($file['filename'], strlen($file['filename']) - strlen($allowed_ext)); if (strtolower($file_ext) == strtolower($allowed_ext)) { $valid = TRUE; break; } } if (!$valid) { // set base validation error message. form_set_error($field['field_name'] .'_upload', t('Files with the extension %ext are not allowed. Please upload a file with an extension from the following list: %allowed_extensions', array('%ext' => $last_ext, '%allowed_extensions' => $field['widget']['file_extensions']))); } // let extended validation from other module happen so we get all error messages. // if you implement hook_filefield_file() return FALSE to stop the upload. if (in_array(FALSE, module_invoke_all('filefield', 'file_validate', $node, $field, $file))) { $valid = FALSE; } if (!$valid) { return FALSE; } // let modules massage act on the file. module_invoke_all('filefield', 'file_prepare', $node, $field, $file); $filepath = file_create_filename($file['filename'], file_create_path($field['widget']['file_path'])); if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE) { if (strpos($filepath, file_directory_path()) !== FALSE) { $filepath = trim(substr($filepath, strlen(file_directory_path())), '\\/'); } $filepath = 'system/files/'. $filepath; }; // prepare file array. $file['fid'] = 'upload'; $file['preview'] = $filepath; // if this is a single value filefield mark any other files for deletion. if (!$field['multiple']) { if (is_array($node_field)) { foreach($node_field as $delta => $session_file) { $node_field[$delta]['delete'] = TRUE; } } // Remove old temporary file from session. filefield_clear_field_session($fieldname); } $file_id = count($node_field) + count($_SESSION['filefield'][$fieldname]); $_SESSION['filefield'][$fieldname][$file_id] = $file; } // Load files from preview state. before committing actions. if (is_array($_SESSION['filefield'][$fieldname]) && count($_SESSION['filefield'][$fieldname])) { foreach($_SESSION['filefield'][$fieldname] as $delta => $file) { $node_field[] = $file; } } } function _filefield_widget_form($node, $field, &$node_field) { drupal_add_js('misc/progress.js'); drupal_add_js('misc/upload.js'); $fieldname = $field['field_name']; drupal_add_css(drupal_get_path('module', 'filefield') .'/filefield.css'); $form = array(); $form[$fieldname] = array( '#type' => 'fieldset', '#title' => t($field['widget']['label']), '#description' => t('Changes made to the attachments are not permanent until you save this post.'), '#weight' => $field['widget']['weight'], '#collapsible' => TRUE, '#collapsed' => FALSE, '#tree' => TRUE, '#prefix' => '
', '#suffix' => '
', ); $form[$fieldname]['new'] = array( '#tree' => FALSE, '#prefix' => '
', '#suffix' => '
', '#weight' => 100, ); // Separate from tree becase of that silly things won't be displayed // if they are a child of '#type' = form issue $form[$fieldname]['new'][$fieldname .'_upload'] = array( '#type' => 'file', '#title' => t('Attach new file'), '#description' => $field['widget']['description'] . t('
Allowed extensions: %ext', array('%ext' => $field['widget']['file_extensions'])), '#weight' => 9, '#tree' => FALSE, ); $form[$fieldname]['new']['upload'] = array( '#type' => 'button', '#value' => t('Upload'), '#name' => 'cck_filefield_'. $fieldname .'_op', '#id' => form_clean_id($fieldname .'-attach-button'), '#tree' => FALSE, '#weight' => 10, ); if (is_array($node_field) && count($node_field)) { $form[$fieldname]['files'] = array( '#parents' => array($fieldname), '#theme' => 'filefield_form_current', // remember the show_list setting so that the theme function knows '#show_list' => _filefield_is_show_list($field['widget']), ); foreach($node_field as $delta => $file) { // @todo: split into its own form and theme functions per file like imagefield if ($file['filepath'] && !$file['delete']) { $form[$fieldname]['files'][$delta] = _filefield_file_form($node, $field, $file); } else if ($file['filepath'] && $file['delete']) { $form[$fieldname]['files'][$delta]['delete'] = array( '#type' => 'hidden', '#value' => $file['delete'], ); } } // Special handling for single value fields. if (!$field['multiple']) { $form[$fieldname]['replace'] = array( '#type' => 'markup', '#value' => '

'. t('If a new file is uploaded, this file will be replaced upon submitting this form.') .'

', '#prefix' => '
', '#suffix' => '
', ); } } // The class triggers the js upload behaviour. $form[$fieldname.'-attach-url'] = array( '#type' => 'hidden', '#value' => url('filefield/js', NULL, NULL, TRUE), '#attributes' => array('class' => 'upload'), ); // Some useful info for our js callback. $form['vid'] = array( '#type' => 'hidden', '#value' => $node->vid, '#tree' => FALSE, ); $form['nid'] = array( '#type' => 'hidden', '#value' => $node->nid, '#tree' => FALSE, ); $form['type'] = array( '#type' => 'hidden', '#value' => $node->type, '#tree' => FALSE, ); return $form; } function _filefield_file_form($node, $field, $file) { // Lets be a good boy and initialize our variables. $form = array(); $form['icon'] = array( '#type' => 'markup', '#value' => theme('filefield_icon', $file), ); $filepath = ($file['fid'] == 'upload') ? file_create_filename($file['filename'], file_create_path($field['widget']['file_path'])) : $file['filepath']; $url = file_create_url($filepath); $url = "". check_plain($url) .""; $form['description'] = array( '#type' => 'textfield', '#default_value' => (strlen($file['description'])) ? $file['description'] : $file['filename'], '#maxlength' => 256, ); $form['url'] = array( '#type' => 'markup', '#value' => t('URL') .': '. $url, '#prefix' => '
', '#suffix' => '
', ); $form['size'] = array( '#type' => 'markup', '#value' => format_size($file['filesize']), '#prefix' => '
', '#suffix' => '
', ); $form['delete'] = array( '#type' => 'checkbox', '#default_value' => $file['delete'], ); // Only show the list checkbox if the corresponding widget option is enabled. if (_filefield_is_show_list($field['widget'])) { $form['list'] = array( '#type' => 'checkbox', '#default_value' => $file['list'], ); } else { $form['list'] = array( '#type' => 'value', '#value' => isset($file['list']) ? $file['list'] : 1, ); } $form['filename'] = array('#type' => 'value', '#value' => $file['filename']); $form['filepath'] = array('#type' => 'value', '#value' => $file['filepath']); $form['filemime'] = array('#type' => 'value', '#value' => $file['filemime']); $form['filesize'] = array('#type' => 'value', '#value' => $file['filesize']); $form['fid'] = array('#type' => 'value', '#value' => $file['fid']); foreach (module_implements('filefield') as $module) { $function = $module .'_filefield'; $function('file_form', $node, $field, $file, $form); } return $form; } /** * Validate the form widget. */ function _filefield_widget_validate($node, $field, $node_field) { if ($field['required']) { // Sum all the items marked for deletion, so we can make sure the end user // isn't deleting all of the files. $count_deleted = 0; foreach($node_field as $item) { if ($item['delete']) { $count_deleted++; } } if (!count($node_field)) { form_set_error($field['field_name'], $field['widget']['label'] .' is required. Please upload a file.'); } else if (count($node_field) == $count_deleted) { form_set_error($field['field_name'], $field['widget']['label'] .' is required. Please keep at least one file or upload a new one.'); } } } /** * Determine whether to show the 'List' checkbox or not. */ function _filefield_is_show_list($widget) { if (isset($widget['show_list']) && !$widget['show_list']) { return FALSE; } return TRUE; } /** * Implementation of hook_field formatter. * @todo: finish transformer.module and integrate like imagecache with imagefield. */ function filefield_field_formatter_info() { $formatters = array( 'default' => array( 'label' => t('Default'), 'field types' => array('file'), ), ); return $formatters; } function filefield_field_formatter($field, $item, $formatter) { if(!empty($item['fid']) && $item['list']) { $file = _filefield_file_load($item['fid']); } if (!empty($file['filepath'])) { return theme('filefield', array_merge($file, $item)); } } function _filefield_file_load($fid = NULL) { // Don't bother if we weren't passed and fid. if (!empty($fid) && is_numeric($fid)) { $result = db_query('SELECT * FROM {files} WHERE fid = %d', $fid); $file = db_fetch_array($result); if ($file) { // let modules load extended attributes. $file += module_invoke_all('filefield', 'file_load', $node, $field, $file); return $file; } } // return an empty array if nothing was found. return array(); } function theme_filefield_form_current($form) { $header = $form['#show_list'] ? array(t('Delete'), t('List'), t('Description'), t('Size')) : array(t('Delete'), t('Description'), t('Size')); foreach (element_children($form) as $key) { // Don't display (hidden) replaced items. if ($form[$key]['delete']['#type'] == 'hidden') { continue; } $row = array(); $row[] = drupal_render($form[$key]['delete']); if ($form['#show_list']) { $row[] = drupal_render($form[$key]['list']); } $row[] = drupal_render($form[$key]['icon']). drupal_render($form[$key]['description']). drupal_render($form[$key]['url']); $row[] = drupal_render($form[$key]['size']); $rows[] = $row; } $output = theme('table', $header, $rows); $output .= drupal_render($form); return $output; } function theme_filefield_icon($file) { $ext = array_pop(explode('.',$file['filename'])); $known_extensions = array('0','ace','aif','ai','ani','asf','asp','avi','bak','bat','bin','bmp','bz2','bz','cab','cdr','cfg','com','conf','cpt','css','cur','dat','db','dcr','dic','diff','dir','dll','dmg','doc','dwg','edir','eml','eps','exe','fla','flv','fon','gif','gz','hqx','html','htm','ico','inc','ini','iso','jpeg','jpg','js','lnk','log','m3u','mdb','midi','mid','mov','mp3','mpeg','mpg','nfo','odb','odc','odf','odg','odm','odp','ods','odt','ogg','otg','oth','otp','ots','ott','patch','pdf','php3','php','phtml','pl','png','pps','ppt','psd','pwl','qt','ram','ra','rar','reg','rpm','rtf','sh','shtml','sit','sql','svg','swf','sxc','sxi','sxw','sys','tar','tgz','tiff','tif','tmp','tpl','ttf','txt','wav','wma','wmv','wp','xls','xml','zip'); if (!in_array($ext, $known_extensions)) { $ext = 0; } $imagepath = base_path() . drupal_get_path('module','filefield') .'/ico/'. $ext .'.png'; return '
'; //return '
 
'."\n"; } function theme_filefield_view_file($file) { return theme('filefield', $file); } function theme_filefield($file) { if (is_file($file['filepath']) && $file['list']) { $path = ($file['fid'] == 'upload') ? file_create_filename($file['filename'], file_create_path($field['widget']['file_path'])) : $file['filepath']; $url = file_create_url($path); $name = $file['filename']; $desc = $file['description']; return ''. check_plain($desc) .''; } return ''; } if (!function_exists('upload_file_download')) { function filefield_file_download($file) { $file = file_create_path($file); $result = db_query("SELECT * FROM {files} WHERE filepath = '%s'", $file); if ($file = db_fetch_object($result)) { if (user_access('view filefield uploads')) { $node = node_load($file->nid); if (node_access('view', $node)) { $name = mime_header_encode($file->filename); $type = mime_header_encode($file->filemime); // Serve images and text inline for the browser to display rather than download. $disposition = ereg('^(text/|image/)', $file->filemime) ? 'inline' : 'attachment'; return array( 'Content-Type: '. $type .'; name='. $name, 'Content-Length: '. $file->filesize, 'Content-Disposition: '. $disposition .'; filename='. $name, 'Cache-Control: private' ); } else { return -1; } } else { return -1; } } } } /** * Wrapper function for filefield_check_directory that accepts a form element * to validate - if user specified one. Won't allow form submit unless the * directory exists & is writable * * @param $form_element * The form element containing the name of the directory to check. */ function filefield_form_check_directory($form_element) { if(!empty($form_element['#value'])) { filefield_check_directory($form_element['#value'], $form_element); } return $form_element; } /** * Create the file directory relative to the 'files' dir recursively for every * directory in the path. * * @param $directory * The directory path under files to check, such as 'photo/path/here' * @param $form_element * A form element to throw an error on if the directory is not writable */ function filefield_check_directory($directory, $form_element = array()) { foreach(explode('/', $directory) as $dir) { $dirs[] = $dir; $path = file_create_path(implode($dirs,'/')); file_check_directory($path, FILE_CREATE_DIRECTORY, $form_element['#parents'][0]); } return TRUE; } /** * Menu callback for JavaScript-based uploads. */ function filefield_js() { // Parse fieldname from submit button. $matches = array(); foreach(array_keys($_POST) as $key) { if (preg_match('/cck_filefield_(.*)_op/', $key, $matches)) { $fieldname = $matches[1]; break; } } $node = (object)$_POST; $field = content_fields($fieldname, $node->type); // load field data // Load fids stored by content.module. $node_field = array(); $values = content_field('load', $node, $field, $node_field, FALSE, FALSE); $node_field = $values[$fieldname]; // Load additional field data. filefield_field('load', $node, $field, $node_field, FALSE, FALSE); // Handle uploads and validation. _filefield_widget_prepare_form_values($node, $field, $node_field); _filefield_widget_validate($node, $field, $node_field); // Get our new form baby, yeah tiger, get em! $form = _filefield_widget_form($node, $field, $node_field); foreach (module_implements('form_alter') as $module) { $function = $module .'_form_alter'; $function('filefield_js', $form); } $form = form_builder('filefield_js', $form); $output = theme('status_messages') . drupal_render($form); // Send the updated file attachments form. print drupal_to_js(array('status' => TRUE, 'data' => $output)); exit(); }