Handle legacy - multiple files name ends in []

This commit is contained in:
Nathan Gray 2012-04-24 20:27:05 +00:00
parent 28fa402b36
commit cf34943445

View File

@ -20,6 +20,12 @@ class etemplate_widget_file extends etemplate_widget
public function __construct($xml='') { public function __construct($xml='') {
if($xml) parent::__construct($xml); if($xml) parent::__construct($xml);
// Legacy multiple - id ends in []
if(substr($this->id,-2) == '[]')
{
$this->attrs['multiple'] = true;
}
} }
/** /**
@ -49,28 +55,37 @@ class etemplate_widget_file extends etemplate_widget
} }
$file_data = array(); $file_data = array();
foreach ($_FILES as $field => &$file) {
if ($file['error'] == UPLOAD_ERR_OK && trim($file['name']) != '' && $file['size'] > 0) {
if (is_dir($GLOBALS['egw_info']['server']['temp_dir']) && is_writable($GLOBALS['egw_info']['server']['temp_dir']))
{
$new_file = tempnam($GLOBALS['egw_info']['server']['temp_dir'],'egw_');
}
else
{
$new_file = $value['file']['tmp_name'].'+';
}
if(move_uploaded_file($file['tmp_name'], $new_file)) {
$file['tmp_name'] = $new_file;
// Data to send back to client // There should only be one file, as they're sent one at a time
$temp_name = basename($file['tmp_name']); foreach ($_FILES as $field => &$files)
$file_data[$temp_name] = array( {
'name' => $file['name'], $widget = $template->getElementById($widget_id ? $widget_id : $field);
'type' => $file['type'] if($widget && $widget->attrs['mime']) {
); $mime = $widget->attrs['mime'];
}
// Check for legacy [] in id to indicate multiple - it changes format
if(is_array($files['name'])) {
$file_list = array();
foreach($files as $f_field => $values)
{
foreach($values as $key => $f_value) {
$file_list[$key][$f_field] = $f_value;
}
}
foreach($file_list as $file)
{
self::process_uploaded_file($field, $file, $mime, $file_data);
} }
} }
else
{
// Just one file
self::process_uploaded_file($field, $files, $mime, $file_data);
}
} }
// Set up response
$response->data($file_data); $response->data($file_data);
// Check for a callback, call it if there is one // Check for a callback, call it if there is one
@ -87,6 +102,45 @@ class etemplate_widget_file extends etemplate_widget
} }
} }
/**
* Process one uploaded file. There should only be one per request...
*/
protected static function process_uploaded_file($field, Array &$file, $mime, Array &$file_data)
{
if ($file['error'] == UPLOAD_ERR_OK && trim($file['name']) != '' && $file['size'] > 0 && is_uploaded_file($file['tmp_name'])) {
// Mime check
if($mime)
{
$type = $file['type'];
$is_preg = $mime[0] == '/';
if (!$is_preg && strcasecmp($mime[0],$type) ||
$is_preg && !preg_match($mime,$type))
{
$file_data[$file['name']] = lang('File is of wrong type (%1 != %2)!',$type,$mime);
continue;
}
}
if (is_dir($GLOBALS['egw_info']['server']['temp_dir']) && is_writable($GLOBALS['egw_info']['server']['temp_dir']))
{
$new_file = tempnam($GLOBALS['egw_info']['server']['temp_dir'],'egw_');
}
else
{
$new_file = $file['tmp_name'].'+';
}
if(move_uploaded_file($file['tmp_name'], $new_file)) {
$file['tmp_name'] = $new_file;
// Data to send back to client
$temp_name = basename($file['tmp_name']);
$file_data[$temp_name] = array(
'name' => basename($file['name']),
'type' => $file['type']
);
}
}
}
/** /**
* Validate input * Validate input
* Merge any already uploaded files into the content array * Merge any already uploaded files into the content array
@ -102,6 +156,10 @@ class etemplate_widget_file extends etemplate_widget
$valid =& self::get_array($validated, $form_name, true); $valid =& self::get_array($validated, $form_name, true);
if(!is_array($value)) $value = array(); if(!is_array($value)) $value = array();
// Incoming values indexed by temp name
if($value[0]) $value = $value[0];
foreach($value as $tmp => $file) foreach($value as $tmp => $file)
{ {
if (is_dir($GLOBALS['egw_info']['server']['temp_dir']) && is_writable($GLOBALS['egw_info']['server']['temp_dir'])) if (is_dir($GLOBALS['egw_info']['server']['temp_dir']) && is_writable($GLOBALS['egw_info']['server']['temp_dir']))