You are here

function fupload_filetransfer in Image FUpload 6.3

Same name and namespace in other branches
  1. 6 image_fupload.module \fupload_filetransfer()
  2. 6.2 image_fupload.module \fupload_filetransfer()
1 string reference to 'fupload_filetransfer'
image_fupload_menu in ./image_fupload.module
Implementation of hook_menu().

File

./image_fupload.module, line 133

Code

function fupload_filetransfer() {

  // huh.. swfUpload sends some data...let's see
  global $user;
  $sid = $_POST['PHPSESSID'];
  $node_type = check_plain($_POST['nodetype']);
  $field_name = check_plain($_POST['fieldname']);

  // validate given session id
  $result = db_query("SELECT * FROM {sessions} WHERE sid = '%s' AND hostname = '%s' LIMIT 1", $sid, ip_address());
  $upload_user = db_fetch_object($result);

  // Get users profile
  $user = user_load(array(
    'uid' => $upload_user->uid,
  ));

  // Do some checks before upload
  $field_access = FALSE;
  if (!empty($upload_user) && !empty($node_type) && !empty($field_name)) {

    // user valid? node_type & field_name received?
    // Check if given node_type & field_name combination is possible for user
    switch ($node_type) {
      case "image":

        // image node type
        if ($field_name == "images") {
          $field_access = node_access('create', $node_type, $user);

          // get suitable validators for our upload: image module
          $validators = array(
            'file_validate_is_image' => array(),
            'file_validate_size' => array(
              variable_get('image_max_upload_size', 800) * 1024,
            ),
          );
        }
        break;
      default:

        // probably cck image node type
        if (module_exists('content')) {
          if (image_node_type_load($node_type, TRUE) && ($field = content_fields($field_name, $node_type))) {
            $field_access = node_access('create', $node_type, $user);

            // also get suitable validators for our upload: cck imagefield module
            $validators = array_merge(filefield_widget_upload_validators($field), imagefield_widget_upload_validators($field));
          }
        }
        break;
    }
  }
  if (user_access('mass upload images', $user) && $field_access) {

    // Adapt to drupal files structure
    $_FILES['files']['name']['image'] = $_FILES['Filedata']['name'];
    $_FILES['files']['type']['image'] = $_FILES['Filedata']['type'];
    $_FILES['files']['tmp_name']['image'] = $_FILES['Filedata']['tmp_name'];
    $_FILES['files']['error']['image'] = $_FILES['Filedata']['error'];
    $_FILES['files']['size']['image'] = $_FILES['Filedata']['size'];

    // do some checks via transliteration module if available
    if (module_exists('transliteration')) {
      require_once drupal_get_path('module', 'transliteration') . '/transliteration.inc';
      $_FILES['files']['name']['image'] = transliteration_clean_filename($_FILES['Filedata']['name']);
    }
    if ($file = file_save_upload('image', $validators)) {
      $image = image_get_info($file->filepath);

      // Get real mime-type
      if (!db_query("UPDATE {files} SET filename = '%s', filemime = '%s' WHERE fid = %d", image_fupload_image_status($field_name, IMAGE_UNMACHINED), $image['mime_type'], $file->fid)) {
        drupal_json(array(
          'status' => FALSE,
          'data' => t('A database related problem appeared during upload. Please inform the site administrator if this is a permanent problem.'),
        ));
        exit;
      }

      // Get all status messages and add them to server message for swfUpload --> inform client
      $messages = drupal_get_messages('status');
      drupal_json(array(
        'status' => TRUE,
        'data' => t('Complete. !messages', array(
          '!messages' => !empty($messages['status']) ? implode(' ', $messages['status']) : '',
        )),
      ));

      // Reply a status message to satisfy swfUpload
    }
    else {

      // Get responsible error messages and send it to swfUpload
      $messages = form_get_errors();
      drupal_json(array(
        'status' => FALSE,
        'data' => t('Upload failed: !errors', array(
          '!errors' => implode(' ', $messages),
        )),
      ));
    }
  }
  else {
    drupal_access_denied();
  }
}