diff -N -u filefield.inc filefield.inc --- filefield.inc 1970-01-01 01:00:00.000000000 +0100 +++ filefield.inc 2007-04-10 18:00:38.000000000 +0200 @@ -0,0 +1,109 @@ +nid, $file['filename'], $file['filepath'], $file['filemime'], $file['filesize']); + db_query('INSERT into {file_revisions} (fid, vid, description, list) + VALUES (%d, %d, "%s", %d)', + $file['fid'], $node->vid, $file['filename'], 0); + 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, &$file, $field) { + $file = (array)$file; + if ($file['remove'] == TRUE) { + filefield_file_delete($file, $field['field_name']); + // should I return an array here instead as imagefield does, or is that a bug in + // in imagefield. I remember I was working on a content.module patch that would + // delete multivalue fields whose value was NULL. Maybe a leftover. + return NULL; + } + if ($file['fid'] == 'upload') { + return filefield_file_insert($node, $file, $field); + } + else { + // if fid is not numeric here we should complain. + // else we update the file table. + } + return $file; +} + +function filefield_file_delete($file, $fieldname) { + if (is_numeric($file['fid'])) { + db_query('DELETE FROM {files} WHERE fid = %d', $file['fid']); + db_query('DELETE FROM {file_revisions} WHERE fid = %d', $file['fid']); + } + else { + unset($_SESSION['filefield'][$fieldname][$file['sessionid']]); + } + return file_delete($file['filepath']); +} + +function filefield_file_load($fid = NULL) { + if (isset($fid)) { + if (is_numeric($fid)) { + $result = db_query('SELECT * FROM {files} WHERE fid = %d', $fid); + $file = db_fetch_array($result); + return ($file) ? $file : array(); + } + } + return array(); +} + +if (!function_exists('upload_file_download')) { + function filefield_file_download($file) { + $file = file_create_path($file); + $result = db_query("SELECT f.* FROM {files} f WHERE filepath = '%s'", $file); + if ($file = db_fetch_object($result)) { + if (user_access('view uploaded files')) { + $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; + } + } + } + +} diff -N -u filefield.module filefield.module --- filefield.module 2007-04-10 17:02:25.000000000 +0200 +++ filefield.module 2007-04-10 17:58:45.000000000 +0200 @@ -8,6 +8,7 @@ * uses content.module to store the fid, and the drupal files * table to store the actual file data. */ +include_once(drupal_get_path('module', 'filefield') .'/filefield.inc'); function filefield_menu($maycache) { @@ -84,60 +85,10 @@ } /** - * 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, &$file, $field) { - $fieldname = $field['field_name']; - if ($file = file_save_upload((object)$file, file_directory_path() . '/'.$file['filename'])) { - $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']); - 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, &$file, $field) { - $file = (array)$file; - if ($file['remove'] == TRUE) { - _filefield_file_delete($file, $field['field_name']); - // should I return an array here instead as imagefield does, or is that a bug in - // in imagefield. I remember I was working on a content.module patch that would - // delete multivalue fields whose value was NULL. Maybe a leftover. - return NULL; - } - if ($file['fid'] == 'upload') { - return filefield_file_insert($node, $file, $field); - } - 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) { + require_once(drupal_get_path('module', 'filefield') . '/filefield.inc'); $fieldname = $field['field_name']; switch ($op) { case 'load': @@ -146,7 +97,7 @@ $values = array(); foreach ($node_field as $delta => $file) { if (!empty($file)) { - $node_field[$delta] = array_merge($node_field[$delta], _filefield_file_load($file['fid'])); + $node_field[$delta] = array_merge($node_field[$delta], filefield_file_load($file['fid'])); } $output = array($fieldname => $node_field); } @@ -181,7 +132,7 @@ case 'delete': foreach ($node_field as $delta => $item) { - _filefield_file_delete($item, $field['field_name']); + filefield_file_delete($item, $field['field_name']); } break; } @@ -246,16 +197,6 @@ } } -function _filefield_file_delete($file, $fieldname) { - if (is_numeric($file['fid'])) { - db_query('DELETE FROM {files} WHERE fid = %d', $file['fid']); - } - else { - unset($_SESSION['filefield'][$fieldname][$file['sessionid']]); - } - return file_delete($file['filepath']); -} - /** * Implementation of hook_widget(). */ @@ -450,24 +391,14 @@ } function filefield_field_formatter($field, $item, $formatter) { + require_once(drupal_get_path('module', 'filefield') .'/filefield.inc'); if(!isset($item['fid'])) { return ''; } - $file = _filefield_file_load($item['fid']); + $file = filefield_file_load($item['fid']); return theme('filefield', $file, $item); } -function _filefield_file_load($fid = NULL) { - if (isset($fid)) { - if (is_numeric($fid)) { - $result = db_query('SELECT * FROM {files} WHERE fid = %d', $fid); - $file = db_fetch_array($result); - return ($file) ? $file : array(); - } - } - return array(); -} - 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'); @@ -499,35 +430,3 @@ return ''.check_plain($name).''; } } - -if (!function_exists('upload_file_download')) { - function filefield_file_download($file) { - $file = file_create_path($file); - $result = db_query("SELECT f.* FROM {files} f WHERE filepath = '%s'", $file); - if ($file = db_fetch_object($result)) { - if (user_access('view uploaded files')) { - $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; - } - } - } - -} -