function _support_save_attachments in Support Ticketing System 7
Same name and namespace in other branches
- 6 support.module \_support_save_attachments()
1 call to _support_save_attachments()
- support_save_message in ./
support.module - Save the message.
File
- ./
support.module, line 1781 - support.module
Code
function _support_save_attachments($attachments, $account, $entity_type = 'node') {
$files = array();
if (count($attachments)) {
$field = _support_upload_field($entity_type);
if (!$field) {
watchdog('support', 'Unable to save attachments - no field was found to save to!', WATCHDOG_WARNING);
return;
}
$weight = 0;
foreach ($attachments as $attachment) {
$attachment = (object) $attachment;
if (isset($attachment->parameters) && is_array($attachment->parameters)) {
foreach ($attachment->parameters as $parm) {
switch (strtoupper($parm->attribute)) {
case 'NAME':
case 'FILENAME':
$attachment->filename = mb_decode_mimeheader($parm->value);
break;
case 'NAME*1*':
case 'FILENAME*1*':
$attachment->filename = urldecode(mb_decode_mimeheader($parm->value));
break;
default:
// put everything else in the attributes array;
$attachment->attributes[$parm->attribute] = mb_decode_mimeheader($parm->value);
}
}
}
if ($attachment->type != TYPETEXT && isset($attachment->dparameters) && is_array($attachment->dparameters)) {
foreach ($attachment->dparameters as $parm) {
switch (strtoupper($parm->attribute)) {
case 'NAME':
case 'FILENAME':
$attachment->filename = mb_decode_mimeheader($parm->value);
break;
default:
// put everything else in the attributes array;
$attachment->attributes[$parm->attribute] = mb_decode_mimeheader($parm->value);
}
}
}
if (!isset($attachment->filename) || empty($attachment->filename)) {
if ($attachment->subtype == 'HTML') {
$attachment->filename = 'noname.html';
}
else {
$attachment->filename = 'noname';
}
}
// Transliterate special characters if transliteration is enabled.
if (function_exists('transliteration_clean_filename')) {
$attachment->filename = transliteration_clean_filename($attachment->filename, language_default());
}
// Create an appropriate file destination based on the field settings in play.
$destination = $field['settings']['uri_scheme'] . '://' . $field['instance']['settings']['file_directory'] . '/' . utf8_encode($attachment->filename);
$destination = file_stream_wrapper_uri_normalize($destination);
if (file_prepare_directory($destination, $options = FILE_CREATE_DIRECTORY)) {
if ($uri = file_unmanaged_save_data($attachment->attachment, $destination, FILE_EXISTS_RENAME)) {
// Create a file object.
$file = new stdClass();
$file->fid = NULL;
$file->uri = $uri;
// The *display* filename is based on the destination. Avoids _1, etc.
$file->filename = basename($destination);
// Differs from core -- filemime can come from the email directly.
// Don't use core logic unless filemime is missing.
if (isset($attachment->filemime)) {
$file->filemime = $attachment->filemime;
}
else {
$file->filemime = file_get_mimetype($file->uri);
}
$file->uid = $account->uid;
$file->status = FILE_STATUS_PERMANENT;
$file = file_save($file);
$files[LANGUAGE_NONE][] = array(
'fid' => $file->fid,
'description' => $file->filename,
'display' => $field['settings']['display_default'],
'_weight' => $weight,
);
}
else {
watchdog('support', 'Failed to save attachment %file, file_save_data() returned error.', array(
'%file' => utf8_encode($attachment->filename),
));
}
}
else {
watchdog('support', 'Failed to save attachment %file, file_prepare_directory() returned error when preparing %directory.', array(
'%file' => utf8_encode($attachment->filename),
'%directory' => utf8_encode($destination),
));
}
$weight++;
}
}
return $files;
}