You are here

function ocupload_upload in One Click Upload 7

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

Upload file

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

File

./ocupload.inc, line 324
Service functions

Code

function ocupload_upload() {
  global $user, $base_url;
  _ocupload_auth();
  $template = NULL;
  $extension_uploaded_file = _ocupload_get_file_extension($_FILES['files']['name']['file']);
  $selected_text = !empty($_POST['selectedText']) ? $_POST['selectedText'] : '';
  foreach (ocupload_templates() as $value) {
    if (in_array($extension_uploaded_file, explode(',', $value->mask)) && user_access('upload files use template ' . $value->tid)) {
      $template = $value;
      break;
    }
  }
  if (!$template) {
    return _ocupload_answer(FALSE, t('This file type can not be upload'));
  }
  $validators = array(
    'file_validate_extensions' => array(
      str_replace(',', ' ', $template->mask),
    ),
    'file_validate_size' => array(
      file_upload_max_size(),
    ),
  );
  if ($template->max_filesize) {
    $validators['file_validate_size'][0] = $template->max_filesize;
  }
  if (_ocupload_is_image($_FILES['files']['name']['file'])) {
    $validators['file_validate_is_image'] = array();
    if ($template->max_dimensions) {
      $validators['file_validate_image_resolution'] = array(
        $template->max_dimensions,
      );
    }
  }
  $destination = 'public://' . token_replace($template->path, array(
    'user' => $user,
  ));
  file_prepare_directory($destination, FILE_CREATE_DIRECTORY);
  $file = file_save_upload('file', $validators, $destination);
  if ($file) {
    if (!variable_get('ocupload_delete_unused_files', 1)) {
      $file->status = FILE_STATUS_PERMANENT;
      $file = file_save($file);
    }
    if ($template->rename_file) {
      $file = file_move($file, $destination . '/' . format_date(REQUEST_TIME, 'custom', 'YdmHis') . '.' . $extension_uploaded_file);
    }
    drupal_alter('ocupload_uploaded_file', $file);
    $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,
          ));
        }
      }
    }
    $data = strtr($cur_template, array(
      '!filepath' => _ocupload_get_local_url($filepath),
      '!filename' => drupal_basename($file->uri),
      '!originalname' => $file->filename,
      '!fileext' => $extension_uploaded_file,
      '!filesize' => format_size($file->destination),
      '!text' => $selected_text,
    ));
    if (variable_get('ocupload_delete_unused_files', 1)) {
      $cid = 'ocupload:' . check_plain($_POST['formId']) . ':' . $user->uid;
      if ($cache = cache_get($cid)) {
        $files = $cache->data;
      }
      $files[check_plain($_POST['fieldName'])][$file->fid] = drupal_basename($file->uri);
      cache_set($cid, $files, 'cache', REQUEST_TIME + DRUPAL_MAXIMUM_TEMP_FILE_AGE);
    }
    return _ocupload_answer(TRUE, $data);
  }
  else {
    $errors = drupal_get_messages(NULL, TRUE);
    $data = array();
    foreach ($errors as $error_per_type) {
      $data[] = strip_tags(implode("\n\n", $error_per_type));
    }
    return _ocupload_answer(FALSE, implode("\n\n", $data));
  }
}