You are here

function _support_save_attachments in Support Ticketing System 6

Same name and namespace in other branches
  1. 7 support.module \_support_save_attachments()
1 call to _support_save_attachments()
support_save_message in ./support.module
Save the message.

File

./support.module, line 1435
support.module

Code

function _support_save_attachments($attachments, $account, $manual = FALSE) {
  $files = array();
  if (count($attachments)) {
    foreach ($attachments as $attachment) {
      $attachment = (object) $attachment;
      if (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 && 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 ($manual && !empty($attachment->filename)) {
        drupal_set_message(t('Saved attachment: %filename', array(
          '%filename' => $attachment->filename,
        )));
      }
      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());
      }
      $attachment->filepath = file_save_data($attachment->attachment, file_directory_path() . '/' . utf8_encode($attachment->filename));
      if ($attachment->filepath) {
        watchdog('support', 'Saved %size byte %type attachment %file to %path.', array(
          '%size' => number_format($attachment->bytes),
          '%type' => $attachment->filemime,
          '%file' => utf8_encode($attachment->filename),
          '%path' => $attachment->filepath,
        ), WATCHDOG_INFO);
        db_query("INSERT INTO {files} (uid, filename, filepath, filemime, filesize, status, timestamp) VALUES(%d, '%s', '%s', '%s', %d, %d, %d)", $account->uid, utf8_encode($attachment->filename), $attachment->filepath, $attachment->filemime, $attachment->bytes, 1, time());
        $attachment->fid = db_last_insert_id('files', 'fid');
        $attachment->new = $attachment->list = TRUE;
        $attachment->weight = 0;
        $attachment->description = $attachment->filename;
        $files[$attachment->fid] = $attachment;
      }
      else {
        watchdog('support', 'Failed to save attachment %file, file_save_data() returned error.', array(
          '%file' => utf8_encode($attachment->filename),
        ));
      }
    }
  }
  return $files;
}