You are here

image_fupload.module in Image FUpload 6

Same filename and directory in other branches
  1. 6.3 image_fupload.module
  2. 6.2 image_fupload.module

File

image_fupload.module
View source
<?php

define('IMAGE_UNMACHINED', 'image_raw');
define('IMAGE_HALFPROCESSED', 'image_halfwork');
define('IMAGE_PROCESSED', 'image_processed');

/**
 * Implementation of hook_help
 */
function image_fupload_help($path, $arg) {
  switch ($path) {
    case 'admin/help#image_fupload':
      $output = '<p>' . t("The Image FUpload module is used to provide an alternate upload form to image module itself.") . '</p>';
      $output .= '<p>' . t("This is a great advantage because multiple images can be selected with one click which are automatically uploaded and processed without any further user interaction. Additionally, this module fully integrates in image module. Consequently, all settings made by image module are observed (thumb creation, file size limit etc.).") . '</p>';
      $output .= '<p>' . t("Image FUpload administration allows to define some characters which are replaced in the node title by a whitespace. In addition to that, the option can be selected to show a link to the original upload form to those users whose browser doesn't support this Flash / JS solution.") . '</p>';
      $output .= t('<p>You can</p>
<ul>
<li>create images using F(lash)Upload at <a href="!node-create-image">node &gt;&gt; create &gt;&gt; image</a>.</li>
<li>configure Image FUpload settings at <a href="!admin-settings-image-fupload">administer &gt;&gt; settings &gt;&gt; image &gt;&gt; image_fupload</a>.</li>
', array(
        '!node-create-image' => url('node/add/image'),
        '!admin-image-galleries' => url('admin/image/galleries'),
        '!admin-settings-image-fupload' => url('admin/settings/image/image_fupload'),
      )) . '</ul>';
      $output .= '<p>' . t('For more information please read the configuration and customization handbook <a href="!image">Image FUpload page</a>.', array(
        '!image' => 'http://www.drupal.org/handbook/modules/image/',
      )) . '</p>';
      return $output;
  }
}

/**
 * Implementation of hook_theme() registry.
 **/
function image_fupload_theme() {
  return array(
    'swfupload_settings' => array(
      'template' => 'swfupload-settings',
      'arguments' => array(
        'modulepath' => NULL,
        'uploadpath' => NULL,
        'maxfilesize' => NULL,
        'sessionid' => NULL,
        'uploadlimit' => NULL,
      ),
    ),
    'fupload_create_filename' => array(
      'arguments' => array(
        'image' => NULL,
      ),
    ),
  );
}
function fupload_filetransfer() {

  // huh.. swfUpload sends some data...let's see
  global $user;
  $sid = $_POST['PHPSESSID'];

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

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

    // 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'];

    // Validators for file_save_upload().
    $validators = array(
      'file_validate_is_image' => array(),
      'file_validate_size' => array(
        variable_get('image_max_upload_size', 800) * 1024,
      ),
    );
    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 LIMIT 1", IMAGE_UNMACHINED, $image['mime_type'], $file->fid)) {
        drupal_set_header('HTTP/1.1 405 Upload Error');
      }
      print 'READY';

      // Reply something to satisfy swfUpload
    }
    else {
      drupal_set_header('HTTP/1.1 408 Image Error');
    }
  }
  else {
    drupal_access_denied();
  }
}
function fupload_empty_queue() {
  global $user;

  // Set "processed" flag so that these images aren't processed again; images are deleted later by cron (--> temporary files)
  db_query("UPDATE {files} SET filename = '%s' WHERE uid = %d AND status = %d AND filename = '%s' LIMIT 100", IMAGE_PROCESSED, $user->uid, FILE_STATUS_TEMPORARY, IMAGE_UNMACHINED);

  // Output message to user via AJAX
  drupal_set_message(t('All queued images were deleted.'), 'warning');
  drupal_json(array(
    'status' => TRUE,
    'data' => theme('status_messages'),
  ));
}
function theme_fupload_create_filename($image) {

  // Get filename out of filepath
  $filename = trim(basename($image->filepath), ' ');
  $length1 = strlen(strrchr($filename, '.'));
  $length2 = strlen($filename);
  $image_name = ucfirst(substr($filename, 0, $length2 - $length1));

  // Remove some given (userdefined) elements
  $replacements = explode(';', variable_get('fupload_title_replacements', '_;{;}'));
  $image_name = str_replace($replacements, ' ', $image_name);
  return $image_name;
}