View source
<?php
define('IMAGE_UNMACHINED', 'image_raw');
define('IMAGE_HALFPROCESSED', 'image_halfwork');
define('IMAGE_PROCESSED', 'image_processed');
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 >> create >> image</a>.</li>
<li>configure Image FUpload settings at <a href="!admin-settings-image-fupload">administer >> settings >> image >> 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;
}
}
function image_fupload_menu() {
$items['admin/settings/image/image_fupload'] = array(
'title' => 'Image FUpload Settings',
'access arguments' => array(
'administer site configuration',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'image_fupload_admin',
),
'description' => 'Configure FUpload Settings',
'type' => MENU_LOCAL_TASK,
'file' => 'image_fupload.admin.inc',
);
$items['fupload/js'] = array(
'title' => 'Image FUpload',
'page callback' => 'fupload_node_create',
'access arguments' => array(
'create images',
),
'type' => MENU_CALLBACK,
'file' => 'node.pages.inc',
'file path' => drupal_get_path('module', 'node'),
);
$items['fupload/js/deletequeue'] = array(
'title' => 'Image FUpload',
'page callback' => 'fupload_empty_queue',
'access arguments' => array(
'create images',
),
'type' => MENU_CALLBACK,
);
$items['fupload/flash'] = array(
'title' => 'Image FUpload',
'page callback' => 'fupload_filetransfer',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
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 image_fupload_form_alter(&$form, $form_state, $form_id) {
global $user;
if ($form_id == "image_node_form" && !isset($form['#node']->nid) && arg(3) != "noflash") {
drupal_add_js(drupal_get_path('module', 'image_fupload') . '/swfupload/swfupload.js', 'module');
drupal_add_js(drupal_get_path('module', 'image_fupload') . '/swfupload/swfupload.queue.js', 'module');
drupal_add_js(drupal_get_path('module', 'image_fupload') . '/swfupload/fileprogress.js', 'module');
drupal_add_js(drupal_get_path('module', 'image_fupload') . '/swfupload/handlers.js', 'module');
drupal_add_js(theme('swfupload_settings', base_path() . drupal_get_path('module', 'image_fupload'), url('fupload/flash'), round(variable_get('image_max_upload_size', 800)), $user->sid, 100), 'inline');
unset($form['title'], $form['rebuild_images'], $form['buttons']['preview']);
isset($form['taxonomy']) ? $form['taxonomy']['#weight'] = -8 : "";
$form['message'] = array(
'#value' => '<div id="jsstatus"></div>',
'#weight' => -7,
);
$form['image'] = array(
'#type' => 'fieldset',
'#title' => t('Images'),
'#weight' => -6,
'#attributes' => array(
'class' => 'flash',
'id' => 'fsUploadProgress',
),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
);
$form['upload_info'] = array(
'#value' => t('0 Files uploaded.'),
'#prefix' => '<div id="divStatus">',
'#suffix' => '</div>',
'#weight' => -5,
);
$form['upload_buttons'] = array(
'#prefix' => '<div>',
'#value' => '<input type="button" value="' . t('Select Images') . '" onclick="swfu.selectFiles()" /> <input id="btnCancel" type="button" value="' . t('Cancel All Uploads') . '" onclick="swfu.cancelQueue();" disabled="disabled" /> ',
'#suffix' => '</div>',
'#weight' => -4,
);
$form['upload_buttons']['node_create'] = array(
'#type' => 'submit',
'#value' => t('Process queued images'),
'#weight' => -3,
'#ahah' => array(
'path' => 'fupload/js',
'event' => 'click',
'method' => 'replace',
'wrapper' => 'jsstatus',
'progress' => array(
'type' => 'bar',
'message' => t('Images in queue are processed...'),
),
),
);
$form['upload_buttons']['delete_queue'] = array(
'#type' => 'submit',
'#value' => t('Delete queued images'),
'#weight' => -2,
'#ahah' => array(
'path' => 'fupload/js/deletequeue',
'event' => 'click',
'method' => 'append',
'wrapper' => 'jsstatus',
),
);
$form['buttons']['submit'] = array(
'#value' => '<input type="button" value="' . t('Upload Images') . '" id="startuploadbutton" onclick="startUploadProcess()" />',
'#weight' => 5,
'#submit' => array(
'node_form_submit',
),
);
$form['#redirect'] = FALSE;
$form['#submit'][0] = '_image_node_form_submit';
}
}
function fupload_node_create() {
global $user;
global $file_cache;
db_query("DELETE FROM {files} WHERE uid = %d AND status = %d AND filename = '%s' LIMIT 3", $user->uid, FILE_STATUS_TEMPORARY, IMAGE_HALFPROCESSED);
$form_build_id = $_POST['form_build_id'];
$form_id = $_POST['form_id'];
if (isset($form_build_id) && isset($form_id)) {
$form_error = 0;
$message = '';
$cache = cache_get('form_' . $form_build_id, 'cache_form');
$form = $cache->data;
$form['#post'] = $_POST;
$form['#post']['title'] = 'Image';
$form_state = array(
'rebuild' => TRUE,
);
$form['#post']['form_id'] = $cache->data['form_id']['#value'];
$form['title'] = array(
'#type' => 'textfield',
'#title' => 'Image',
'#default_value' => t('Image'),
);
drupal_process_form($form_id, $form, $form_state);
if (!form_get_errors()) {
$result = db_query("SELECT * FROM {files} WHERE uid = %d AND status = %d AND filename = '%s' LIMIT 0 , 3", $user->uid, FILE_STATUS_TEMPORARY, IMAGE_UNMACHINED);
while ($image = db_fetch_object($result)) {
$file_cache['image'] = $image;
db_query("UPDATE {files} SET filename = '%s' WHERE fid = %d LIMIT 1", IMAGE_HALFPROCESSED, $image->fid);
$form['title']['#value'] = check_plain(theme('fupload_create_filename', $image));
$form = form_builder($form_id, $form, $form_state);
node_form_submit($form, $form_state);
db_query("UPDATE {files} SET filename = '%s' WHERE fid = %d LIMIT 1", IMAGE_PROCESSED, $image->fid);
}
if (empty($file_cache['image'])) {
drupal_set_message(t('No images yet in queue.'));
}
$result = db_fetch_object(db_query("SELECT COUNT(*) AS img_count FROM {files} WHERE uid = %d AND status = %d AND filename = '%s'", $user->uid, FILE_STATUS_TEMPORARY, IMAGE_UNMACHINED));
$message .= '<input type="hidden" name="num_queued_images" id="num_queued_images" value="' . $result->img_count . '" />';
}
else {
$form_error = 1;
$message .= '<input type="hidden" name="num_queued_images" id="num_queued_images" value="0" />';
}
$message .= '<input type="hidden" name="form_errors" id="form_errors" value="' . $form_error . '" />';
$message .= theme('status_messages');
drupal_json(array(
'status' => TRUE,
'data' => $message,
));
}
else {
drupal_json(array(
'status' => TRUE,
'data' => t('Error: No or wrong POST Data'),
));
}
}
function fupload_filetransfer() {
global $user;
$sid = $_POST['PHPSESSID'];
$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)) {
$user = user_load(array(
'uid' => $upload_user->uid,
));
$_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 = 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);
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 'OK';
}
else {
drupal_set_header('HTTP/1.1 408 Image Error');
}
}
else {
drupal_access_denied();
}
}
function fupload_empty_queue() {
global $user;
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);
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) {
$filename = trim(basename($image->filepath), ' ');
$length1 = strlen(strrchr($filename, '.'));
$length2 = strlen($filename);
$image_name = ucwords(substr($filename, 0, $length2 - $length1));
$replacements = explode(';', variable_get('fupload_title_replacements', '_;{;}'));
$image_name = str_replace($replacements, ' ', $image_name);
return $image_name;
}
function _image_node_form_submit($form, &$form_state) {
global $file_cache;
$nid = 'new_node';
$file = $file_cache['image'];
$image_info = image_get_info($file->filepath);
$aspect_ratio = $image_info['height'] / $image_info['width'];
$original_size = image_get_sizes(IMAGE_ORIGINAL, $aspect_ratio);
if (!empty($original_size['width']) && !empty($original_size['height'])) {
$result = image_scale($file->filepath, $file->filepath, $original_size['width'], $original_size['height']);
if ($result) {
clearstatcache();
$file->filesize = filesize($file->filepath);
drupal_set_message(t('The original image was resized to fit within the maximum allowed resolution of %width x %height pixels.', array(
'%width' => $original_size['width'],
'%height' => $original_size['height'],
)));
}
}
$form_state['values']['images'][IMAGE_ORIGINAL] = $file->filepath;
$form_state['values']['rebuild_images'] = FALSE;
$form_state['values']['new_file'] = TRUE;
module_invoke_all('image_alter', $form_state['values'], $file->filepath, IMAGE_ORIGINAL);
$form_state['values']['images'] = _image_build_derivatives((object) $form_state['values'], TRUE);
$_SESSION['image_new_files'][$nid] = $form_state['values']['images'];
}