Allow FileUpload to be used when $fk_element and $element are null but enforce a valid upload_dir

This commit is contained in:
David Pareja Rodriguez 2023-03-01 08:33:52 +01:00
parent 706b95891e
commit c2d6e9adcf

View File

@ -104,6 +104,9 @@ class FileUpload
$dir_output = $conf->$element->dir_output; $dir_output = $conf->$element->dir_output;
} }
// If pathname and filename are null then we can still upload files
// IF we have specified upload_dir on $this->options
if ($pathname !== null && $filename !== null) {
dol_include_once('/'.$pathname.'/class/'.$filename.'.class.php'); dol_include_once('/'.$pathname.'/class/'.$filename.'.class.php');
$classname = ucfirst($filename); $classname = ucfirst($filename);
@ -135,6 +138,7 @@ class FileUpload
} elseif ($element == 'project_task') { } elseif ($element == 'project_task') {
$object_ref = $object->project->ref.'/'.$object_ref; $object_ref = $object->project->ref.'/'.$object_ref;
} }
}
$this->options = array( $this->options = array(
'script_url' => $_SERVER['PHP_SELF'], 'script_url' => $_SERVER['PHP_SELF'],
@ -195,6 +199,21 @@ class FileUpload
if ($options) { if ($options) {
$this->options = array_replace_recursive($this->options, $options); $this->options = array_replace_recursive($this->options, $options);
} }
// At this point we should have a valid upload_dir in options
//if ($pathname === null && $filename === null) { // OR or AND???
if ($pathname === null || $filename === null) {
if (!key_exists("upload_dir", $this->options)) {
setEventMessage('If $fk_element = null or $element = null you must specify upload_dir on $options', 'errors');
throw new Exception('If $fk_element = null or $element = null you must specify upload_dir on $options');
} elseif (is_dir($this->options['upload_dir'])) {
setEventMessage('The directory '$this->options['upload_dir'].' doesn\'t exists', 'errors');
throw new Exception('The directory '$this->options['upload_dir'].' doesn\'t exists');
} elseif (is_writable($this->options['upload_dir'])) {
setEventMessage('The directory '$this->options['upload_dir'].' is not writable', 'errors');
throw new Exception('The directory '$this->options['upload_dir'].' is not writable');
}
}
} }
/** /**