'mfupload',
'description' => 'Easy multiple file uploader.',
'page callback' => 'mfupload_admin',
'access arguments' => array('administer mfupload'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Form: Module configuration settings
*/
function mfupload_admin() {
$output['form'] = drupal_get_form('mfupload_admin_form');
return $output;
}
function mfupload_file_directory_path()
{
// This is the alternative function of file_directory_path() which is
// obsolete with Drupal 7.
//
// file_build_uri('')
// Returns Drupal's default file upload directory in stream format.
// As default, "public://" is returned.
//
// drupal_realpath()
// Returns absolute system path of the specified uri.
// Example: /www/drupal/html/sites/default/files
//
// returns:
// Path of the Drupal default file upload directory starting
// from the DOCUMENT_ROOT
// Example: /sites/default/files/
//
// NOTE: it adds leading and trailing slash
//
/*
$document_root = $_SERVER['DOCUMENT_ROOT'];
$path = drupal_realpath(file_build_uri(''));
$path = str_replace($document_root, '', $path);
$path = '/' . $path . '/';
$path = preg_replace("/\/\//", '/', $path);
*/
// The code below does the same thing as the code above and more compatible
// with future version of Drupal since drupal_realpath() is getting obsolete.
$path = file_stream_wrapper_get_instance_by_scheme(file_default_scheme())->getDirectoryPath();
$path = '/' . $path . '/';
$path = preg_replace("/\/\//", '/', $path);
return $path;
}
function mfupload_admin_form($form, &$form_state) {
drupal_add_js(drupal_get_path('module', 'mfupload') . '/mfupload-ui.js');
$default_file_path = mfupload_file_directory_path();
//-------- General options --------
$form['general'] = array(
'#type' => 'fieldset',
'#title' => t('General options'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['general']['path_note'] = array(
'#type' => 'item',
'#markup' => '
',
);
$form['general']['mfupload_image_path'] = array(
'#type' => 'textfield',
'#title' => t('Image upload directory'),
'#size' => 80,
'#maxlength' => 255,
'#default_value' => variable_get('mfupload_image_path', $default_file_path),
);
$form['general']['mfupload_file_path'] = array(
'#type' => 'textfield',
'#title' => t('File upload directory'),
'#size' => 80,
'#maxlength' => 255,
'#default_value' => variable_get('mfupload_file_path', $default_file_path),
);
$form['general']['mfupload_url_type'] = array(
'#type' => 'radios',
'#title' => 'URL type to use',
'#default_value' => variable_get('mfupload_url_type', 'absolute'),
'#options' => array(
'absolute' => t('Absolute URL (e.g: http://example.com/sites/default/files/sample1.png)'),
'relative' => t('Relative URL (e.g: /sites/default/files/sample1.png)'),
),
);
$form['general']['mfupload_valid_ext'] = array(
'#type' => 'textfield',
'#title' => t('Valid file name extentions for both image and file'),
'#size' => 80,
'#maxlength' => 1024,
'#default_value' => variable_get('mfupload_valid_ext', 'gif jpeg jpg png 7z bz bz2 gz gzip hqx iso lha lzh pkg pdf rpm sea sit tar tbz tgz zip patch txt js doc xls'),
'#description' => t('Separate extentions with a space and do not include the leading dot. For images, only \'gif\', \'png\', \'jpg\' and \'jpeg\' are allowed. All other extensions are for files.'),
);
$upload_max_filesize = ini_get('upload_max_filesize');
// mfupload_debugout("upload_max_filesize: " . $upload_max_filesize);
$form['general']['mfupload_max_filesize'] = array(
'#type' => 'textfield',
'#title' => t('Maximum file size'),
'#size' => 10,
'#maxlength' => 1024,
'#default_value' => variable_get('mfupload_max_filesize', ''),
'#description' => t('Specify the maximum file size that can be uploaded. You can use abbriviated size with K(Kilobyte), M(Megabyte) and G(Gigabyte). Leave this field blank or set 0 to use the current system\'s max size (=!sysmax). If you specify a bigger size than system\'s max size, the system\'s max size is used as a limit. Files that exceed this max size are ignored.', array('!sysmax'=> $upload_max_filesize)),
);
$form['general']['mfupload_overwrite_mode'] = array(
'#type' => 'select',
'#title' => t('If the same file already exists'),
'#default_value' => variable_get('mfupload_overwrite_mode', 'overwrite'),
'#options' => array('overwrite' => t('Overwrite - replace the existing file with the new one'),
'rename' => t('Rename - keep the existing file and rename the new one')),
);
//-------- Image options --------
$form['image'] = array(
'#type' => 'fieldset',
'#title' => t('Image Options'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['image']['mfupload_scaling_mode'] = array(
'#type' => 'select',
'#title' => t('Scaling mode'),
'#default_value' => variable_get('mfupload_scaling_mode', 'scale_and_link'),
'#options' => array('scale_and_link' => t('Scale image and use it with a link to the original image.'),
'scale_only' => t('Scale image and use it. The original image is deleted.'),
'no_scale' => t('No scaling. Use the original image as it is.'),
),
'#description' => t('Scaling mode is for large image only.'),
);
$form['image']['mfupload_max_dimensions'] = array(
'#type' => 'textfield',
'#title' => t('Maximum image dimensions'),
'#size' => 20,
'#maxlength' => 20,
'#field_suffix' => t('WIDTHxHEIGHT'),
'#default_value' => variable_get('mfupload_max_dimensions', '640x480'),
'#description' => t('Image is scaled to fix this dimensions when the width or the height exceeds it.'),
);
$form['image']['mfupload_anchor_class'] = array(
'#type' => 'textfield',
'#title' => t('Anchor tag class'),
'#size' => 20,
'#maxlength' => 20,
'#default_value' => variable_get('mfupload_anchor_class', ''),
'#description' => t('Specify the class value you want to add the anchor tag (e.g: lightbox). This is mainly used lightbox type plugins. Since this is not a jQuery selector, do not addd leading dot for the class name'),
);
$form['image']['mfupload_anchor_rel'] = array(
'#type' => 'textfield',
'#title' => t('Anchor tag rel'),
'#size' => 20,
'#maxlength' => 20,
'#default_value' => variable_get('mfupload_anchor_rel', ''),
'#description' => t('Specify the rel value you want to add the anchor tag (e.g: group). This is mainly used for grouping images with lightbox type plugins.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save configuration'),
);
$form['#submit'][] = 'mfupload_admin_submit';
$form['reset'] = array(
'#type' => 'submit',
'#value' => t('Reset to default'),
);
return $form;
}
function mfupload_admin_submit($form, &$form_state)
{
$op = $form_state['values']['op'];
if ($op == t('Reset to default')) {
$default_file_path = mfupload_file_directory_path();
variable_set('mfupload_image_path', $default_file_path);
variable_set('mfupload_file_path', $default_file_path);
variable_set('mfupload_url_type', 'absolute');
variable_set('mfupload_valid_ext', 'gif jpeg jpg png 7z bz bz2 gz gzip hqx iso lha lzh pkg pdf rpm sea sit tar tbz tgz zip patch txt js doc xls');
variable_set('mfupload_max_filesize', '');
variable_set('mfupload_overwrite_mode', 'overwrite');
variable_set('mfupload_scaling_mode', 'scale_and_link');
variable_set('mfupload_max_dimensions', '640x480');
variable_set('mfupload_max_anchor_class', '');
variable_set('mfupload_max_anchor_rel', '');
drupal_set_message(t('Settings are reset to default.'));
}
else if ($op == t('Save configuration')) {
variable_set('mfupload_image_path', $form_state['values']['mfupload_image_path']);
variable_set('mfupload_file_path', $form_state['values']['mfupload_file_path']);
variable_set('mfupload_url_type', $form_state['values']['mfupload_url_type']);
variable_set('mfupload_valid_ext', $form_state['values']['mfupload_valid_ext']);
variable_set('mfupload_max_filesize', $form_state['values']['mfupload_max_filesize']);
variable_set('mfupload_overwrite_mode', $form_state['values']['mfupload_overwrite_mode']);
variable_set('mfupload_scaling_mode', $form_state['values']['mfupload_scaling_mode']);
variable_set('mfupload_max_dimensions', $form_state['values']['mfupload_max_dimensions']);
variable_set('mfupload_anchor_class', $form_state['values']['mfupload_anchor_class']);
variable_set('mfupload_anchor_rel', $form_state['values']['mfupload_anchor_rel']);
drupal_set_message(t('The changes has been saved.'));
}
}
/**
* Implementation of hook_validate()
*/
function mfupload_admin_form_validate($form_id, $form_values) {
$op = $form_values['values']['op'];
if ($op == t('Cancel')) return;
if (!preg_match('/^\/.*\/$/', $form_values['values']['mfupload_image_path'])) {
form_set_error('mfupload_image_path', t('The image upload directory path must start with a slash and also must end with a slash'));
}
if (!preg_match('/^\/.*\/$/', $form_values['values']['mfupload_file_path'])) {
form_set_error('mfupload_file_path', t('The file upload directory path must start with a slash and also must end with a slash'));
}
if (preg_match('/\.,/', $form_values['values']['mfupload_valid_ext'])) {
form_set_error('mfupload_valid_ext', t('Do not include leading dot, and separate them with a spece character'));
}
}
/**
* Implementation of hook_element_info()
*/
function mfupload_element_info() {
return array(
'textarea' => array('#process' => array('mfupload_textarea')),
'textfield' => array('#process' => array('mfupload_textarea'))
);
}
/**
* Inline insertion to textareas
*/
function mfupload_textarea($element, $form_state, $complete_form) {
static $ids;
global $base_url;
global $user;
if (!isset($ids)) {
$ids = FALSE;
if ($setting = str_replace(' ', '', variable_get('mfupload_textarea', 'edit-body'))) {
$ids = array();
foreach (explode(',', $setting) as $id) {
$ids[$id] = 1;
}
}
}
// mfupload_debugout("=========mfupload_textarea=========\n");
// mfupload_debugout("element['#id']: " . $element['#id'] . "\n");
if ($ids && (isset($ids[$element['#id']]))||(mfupload_id_match($ids, $element['#id']))) {
mfupload_debugout("=========mfupload_textarea=========\n");
mfupload_debugout("element['#id']: " . $element['#id'] . "\n");
$js_installed = &drupal_static(__FUNCTION__);
if (!isset($js_installed)) {
// mfupload_debugout("js_installed is not set! set 0\n");
$js_installed = 0;
}
else {
// mfupload_debugout("js_installed is set, value is " . $js_installed . "\n");
}
$default_file_path = mfupload_file_directory_path();
$file_path = variable_get('mfupload_file_path', $default_file_path);
$file_path = str_replace("%u", $user->uid, $file_path);
$file_url = $file_path;
$image_path = variable_get('mfupload_image_path', $default_file_path);
$image_path = str_replace("%u", $user->uid, $image_path);
$image_url = $image_path;
$url_type = variable_get('mfupload_url_type', 'absolute');
if($url_type == 'absolute') {
mfupload_debugout("Relative image_url: " . $image_url);
mfupload_debugout($image_url);
$image_url = $base_url . $image_url;
mfupload_debugout("Absolute image_url: " . $image_url);
}
$document_root = $_SERVER['DOCUMENT_ROOT'];
$file_fullpath = $document_root . $file_path;
$file_fullpath = preg_replace("/\/\//", '/', $file_fullpath);
$image_fullpath = $document_root . $image_path;
$image_fullpath = preg_replace("/\/\//", '/', $image_fullpath);
$uploader_url = $base_url . '/' . drupal_get_path('module', 'mfupload') . '/php/upload.php';
$uploader_path = '/' . drupal_get_path('module', 'mfupload') . '/php/upload.php';
$deleter_url = $base_url . '/' . drupal_get_path('module', 'mfupload') . '/php/delete.php';
$deleter_path = '/' . drupal_get_path('module', 'mfupload') . '/php/delete.php';
$icon_path = '/' . drupal_get_path('module', 'mfupload') . '/';
$max_filesize = variable_get('mfupload_max_filesize', '');
if(empty($max_filesize)) {
$max_filesize = ini_get('upload_max_filesize');
}
$maxsize = (mfupload_return_bytes($max_filesize) / 1048576);
mfupload_debugout("maxsize=" . $maxsize . " (MB)/file");
$max_file_uploads = ini_get('max_file_uploads');
mfupload_debugout("max_file_uploads=" . $max_file_uploads . " files/upload");
$overwrite_mode = variable_get('mfupload_overwrite_mode', 'overwrite');
$overwrite = ($overwrite_mode == 'overwrite')? 1:0;
$scaling_mode = variable_get('mfupload_scaling_mode', 'scale_and_link');
$max_dimensions = variable_get('mfupload_max_dimensions', '640x480');
$max_dimensions = preg_replace('/\s*/', '', $max_dimensions);
$max_dimensions = strtolower($max_dimensions);
list($max_width, $max_height) = explode('x', $max_dimensions);
$anchor_class = variable_get('mfupload_anchor_class', '');
$anchor_rel = variable_get('mfupload_anchor_rel', '');
mfupload_debugout("scaling_mode=" . $scaling_mode);
mfupload_debugout("max_width=" . $max_width);
mfupload_debugout("max_height=" . $max_height);
mfupload_debugout("anchor_class=" . $anchor_class);
mfupload_debugout("anchor_rel=" . $anchor_rel);
$valid_ext = variable_get('mfupload_valid_ext', '');
$valid_ext = preg_replace('/\s+/ims', ',', $valid_ext);
$eid = $element['#id'];
$dropzone_id = "upload-" . $eid;
$filelist_id = "uploaded-" . $eid;
// mfupload_debugout("valid_ext [" . $valid_ext . "]\n");
drupal_add_css(drupal_get_path('module', 'mfupload') . '/css/mfupload.css');
drupal_add_js(drupal_get_path('module', 'mfupload') . '/js/insert_textarea.js');
drupal_add_js(drupal_get_path('module', 'mfupload') . '/js/mfupload.js');
if(!$js_installed) {
// mfupload_debugout("installing inline javascript\n");
mfupload_debugout("----------------_SESSION------------------");
mfupload_debugout($_SESSION);
mfupload_debugout("----------------_COOKIE------------------");
mfupload_debugout($_COOKIE);
//phpinfo();
drupal_add_js ('
(function($) {
Drupal.behaviors.mfuploadModule = {
attach: function(context, settings) {
$(document).bind("drop dragover", function(e) {
// prevent browsers default drag drop behavior
//---NOTE: this prevent other drag drop imagefield file uploads
// e.preventDefault();
});
$(function() {
$(".upload").each(function(index) {
var errors="";
var zebra = 0;
$(this).mfupload({
type : "' . $valid_ext . '",
maxsize : "' . $maxsize . '",
overwrite : "' . $overwrite . '",
post_upload : "' . $uploader_path . '",
file_folder : "' . $file_fullpath . '",
image_folder : "' . $image_fullpath . '",
scaling_mode : "' . $scaling_mode . '",
max_width : "' . $max_width . '",
max_height : "' . $max_height . '",
ini_text : "Drag your files to here or click to upload (max: '.$maxsize.'MB/file, '.$max_file_uploads.' files/upload)",
over_text : "Drop Here",
over_col : "#fff",
over_bkcol : "#ccc",
init: function(){
var uploaded;
var id = $(this).attr(\'file_elem\');
id = id.slice(15); // mf_file_upload-XXXXXXXXX
uploaded = $(\'#uploaded-\'+id);
uploaded.empty();
},
start: function(result){
var uploaded;
var id = $(this).attr(\'file_elem\');
id = id.slice(15); // mf_file_upload-XXXXXXXXX
uploaded = $(\'#uploaded-\'+id);
var li_tag = zebra? \'\' : \'\';
zebra = 1 - zebra;
uploaded.append(li_tag+"");
},
loaded: function(result){
var element;
var tag;
var dest_url;
var fileType = result.filename.split(".").pop().toLowerCase();
var scaled = result.scaled;
var scaling_mode = result.scaling_mode;
var id = $(this).attr(\'file_elem\');
id = id.slice(15); // mf_file_upload-XXXXXXXXX
if (fileType == \'gif\' || fileType == \'jpg\' ||
fileType == \'jpeg\' || fileType == \'png\') {
if (scaled) {
dest_url = \'' . $image_url . 'scaled/\' + result.filename;
orig_url = \'' . $image_url . '\' + result.filename;
if (scaling_mode == \'scale_only\') {
tag = \'
\r\n\';
}
else { // scale_and_link
tag = \'
\r\n\';
}
}
else {
dest_url = \'' . $image_url . '\' + result.filename;
tag = \'
\r\n\';
}
}
else {
dest_url = \'' . $file_url . '\' + result.filename;
tag = \'\' + result.filename + \'\r\n\';
}
$("#PRO"+id+result.fileno).fadeOut(1000).remove();
element = document.getElementById(id);
insertAtCursor(element, tag);
delete_url = \' Delete \';
$("#FILE"+id+result.fileno).html("x "+result.filename+" ("+result.size+") is uploaded ");
// alert(result.path + result.filename + \' is uploaded\');
if (scaled) {
$("#FILE"+id+result.fileno).append(" and scaled. ");
}
// add event handler to show the img or anchor tag
$("#FILE"+id+result.fileno+" .showtag").bind("click", function() {
prompt("Tag:", tag);
return false;
});
// add event handler to delete the file
$("#FILE"+id+result.fileno+" .delete").bind("click", function() {
var container = $(this).parent();
var param = \'file=\'+result.path+result.filename;
var msg = "Are you sure to delete " + result.filename;
msg += (scaled)? " and scaled image ?" : " ?";
if (confirm(msg)) {
$.ajax({
type : "POST",
url : "' . $deleter_url . '",
data : param,
cache : false,
error: function(jqXHR, status, error) {
alert(status.toUpperCase() + ": " + error);
},
success: function() {
container.slideUp("slow", function() { $(this).remove(); });
},
});
}
return false;
});
},
progress: function(result){
var id = $(this).attr(\'file_elem\');
id = id.slice(15); // mf_file_upload-XXXXXXXXX
$("#PRO"+id+result.fileno).css("width", result.perc+"%");
},
error: function(error){
errors += error.filename+": "+error.err_des+"\n";
},
completed: function(){
if (errors != "") {
alert(errors);
errors = "";
}
}
});
});
});
}
};
})(jQuery)
', 'inline');
}
else {
// mfupload_debugout("skip installing inline javascript\n");
}
$js_installed = 1;
$myform = "\n\n";
$myform .= '';
$myform .= '
';
$myform .= '
';
$myform .= '
';
$myform .= "\n\n";
if(!isset($element['#description'])) $element['#description'] = '';
$element['#description'] .= $myform;
}
return $element;
}
/**
* Check if specified element ID and current element ID matches or not
*
* For example, we handle these case as a match
* user specified ID: edit-body
* current element ID: edit-body-und-0-summary or edit-body-und-0-value
*/
function mfupload_id_match($ids, $target_id) {
foreach ($ids as $id => $value) {
if (preg_match('/^' . $id . '/i', $target_id)) {
/*
if(preg_match('/-summary/i', $target_id)) {
return FALSE;
}
*/
return TRUE;
}
}
return FALSE;
}
/**
* Convert PHP.INI's size value (200K, 1M, 0.25M, 3G) to bytes
*/
function mfupload_return_bytes($val) {
$val = trim($val);
$last = strtolower($val[strlen($val)-1]);
switch($last) {
// The 'G' modifier is available since PHP 5.1.0
case 'g':
$val *= 1024;
case 'm':
$val *= 1024;
case 'k':
$val *= 1024;
}
return $val;
}
/**
* Debug function - output specified string to debug file
*/
function mfupload_debugout($str) {
return; // for production
$outfile = "/tmp/debug.txt";
if(is_array($str) || is_object($str)) {
error_log(print_r($str, TRUE) . "\n", 3, $outfile);
}
else {
error_log($str . "\n", 3, $outfile);
}
}