You are here

function ocupload_upload in One Click Upload 7.2

Same name and namespace in other branches
  1. 7 ocupload.inc \ocupload_upload()

Upload file.

1 string reference to 'ocupload_upload'
ocupload_menu in ./ocupload.module
Implements hook_menu().

File

./ocupload.inc, line 361
One Click Upload includes.

Code

function ocupload_upload() {
  global $user;

  // Check errors [1]
  if (empty($_FILES)) {
    return _ocupload_answer(FALSE, t('File are missing'));
  }
  if ($_FILES['file']['error'] != 0) {
    return _ocupload_answer(FALSE, t('Error #@error in file @filename', array(
      '@error' => $_FILES['file']['error'],
      '@filename' => $_FILES['file']['name'],
    )));
  }
  $file_name = $_FILES['file']['name'];
  $file_extension = _ocupload_get_file_extension($file_name);
  $template = _ocupload_get_appropriate_template($file_extension);

  // Check errors [2]
  if (!$template) {
    return _ocupload_answer(FALSE, t('This file type can not be upload'));
  }
  if ($template->max_filesize && $_FILES['file']['size'] > $template->max_filesize) {
    return _ocupload_answer(FALSE, t('Too large file. Maximum size: @file_size', array(
      '@file_size' => format_size($template->max_filesize),
    )));
  }

  // Process filename
  if ($template->filename) {
    $file_name = token_replace($template->filename, array(
      'user' => $user,
    )) . '.' . $file_extension;
    $file_name = drupal_basename($file_name);
  }
  if ($template->transliterate && module_exists('transliteration')) {
    $file_name = transliteration_clean_filename($file_name);
  }

  // Process temporary path
  $temporary_path = 'temporary://ocupload';
  file_prepare_directory($temporary_path, FILE_CREATE_DIRECTORY);
  $file_destination_temp = file_destination($temporary_path . '/' . $file_name, FILE_EXISTS_RENAME);
  _ocupload_load_flowphp();

  // Save file to temp dir
  if (!\Flow\Basic::save($file_destination_temp, $temporary_path)) {
    return _ocupload_answer(TRUE, t('Chunk saved'));
  }
  $file = new stdClass();
  $file->uid = $user->uid;
  $file->status = 0;
  $file->filename = $_FILES['file']['name'];
  $file->uri = $file_destination_temp;
  $file->filemime = file_get_mimetype($file_destination_temp);
  $file->filesize = filesize($file_destination_temp);

  // Check errors [3]
  $validators = array();
  if ($template->max_filesize) {
    $validators['file_validate_size'] = array(
      $template->max_filesize,
    );
  }
  if (_ocupload_is_image($file->filename)) {
    $validators['file_validate_is_image'] = array();
    if ($template->max_dimensions) {
      $validators['file_validate_image_resolution'] = array(
        $template->max_dimensions,
      );
    }
  }
  if ($errors = file_validate($file, $validators)) {
    file_unmanaged_delete($file->uri);
    $error_message = t('The specified file %name could not be uploaded.', array(
      '%name' => $file->filename,
    ));
    foreach ($errors as $error) {
      $error_message .= "\n- {$error}";
    }
    return _ocupload_answer(FALSE, $error_message);
  }

  // Process upload path
  $upload_path = token_replace($template->path, array(
    'user' => $user,
  ));
  if (!file_uri_scheme($upload_path)) {
    $upload_path = file_default_scheme() . '://' . $upload_path;
  }
  file_prepare_directory($upload_path, FILE_CREATE_DIRECTORY);
  $file_destination = _ocupload_unique_file_uri($upload_path . '/' . $file_name);

  // Move file from temp dir
  file_unmanaged_move($file_destination_temp, $file_destination);
  $file->uri = $file_destination;
  drupal_alter('ocupload_uploaded_file', $file);
  file_save($file);
  \Flow\Uploader::pruneChunks($temporary_path, 60 * 60 * 3);
  $selected_text = !empty($_POST['selectedText']) ? $_POST['selectedText'] : '';
  $cur_template = $selected_text ? $template->template_select : $template->template;
  $filepath = $file->uri;

  // If upload image and template image style not empty
  if ($selected_text == '' && _ocupload_is_image($file->filename) && $template->image_style && module_exists('image')) {
    $filepath = image_style_url($template->image_style, $file->uri);
    if ($template->link_to_original) {
      $wrap_template = TRUE;
      if ($template->link_only_big) {
        $image_style = image_style_load($template->image_style);
        $image_info = image_get_info($file->uri);
        $styled_image_dimensions = array(
          'width' => $image_info['width'],
          'height' => $image_info['height'],
        );
        image_style_transform_dimensions($template->image_style, $styled_image_dimensions);
        if ($image_info['width'] <= $styled_image_dimensions['width'] && $image_info['height'] <= $styled_image_dimensions['height']) {
          $wrap_template = FALSE;
        }
      }
      if ($wrap_template) {
        $original_image_uri = $file->uri;
        if ($template->image_style_original) {
          $original_image_uri = image_style_url($template->image_style_original, $original_image_uri);
        }
        $cur_template = strtr($template->link_template, array(
          '!filepath' => _ocupload_get_local_url($original_image_uri),
          '!image' => $cur_template,
        ));
      }
    }
  }
  $file_url = _ocupload_get_local_url($filepath);
  $data = strtr($cur_template, array(
    '!filefid' => $file->fid,
    '!filepath' => $file_url,
    '!filename' => drupal_basename($file->uri),
    '!originalname' => $file->filename,
    '!fileext' => $file_extension,
    '!filesize' => format_size($file->filesize),
    '!text' => $selected_text,
  ));

  // Save file info in session
  $form_id = check_plain($_POST['formId']);
  $field_name = check_plain($_POST['fieldName']);
  $_SESSION['ocupload'][$form_id][$field_name][$file->fid] = array(
    'uri' => $file->uri,
    'url' => $file_url,
    'uploaded' => $file->timestamp,
    'noticed' => FALSE,
  );
  return _ocupload_answer(TRUE, $data);
}