Merge pull request #24079 from Daviid-P/allow_file_upload_not_linked_to_dolibarr_class

Allow FileUpload to be used when $fk_element and $element are null
This commit is contained in:
Laurent Destailleur 2023-03-12 14:15:05 +01:00 committed by GitHub
commit 4b2a2c530e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -110,6 +110,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);
@ -141,6 +144,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'],
@ -201,6 +205,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');
}
}
} }
/** /**