29require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
30require_once DOL_DOCUMENT_ROOT.
'/core/lib/images.lib.php';
45 protected $fk_element;
62 public function __construct($options =
null, $fk_element =
null, $element =
null)
66 $hookmanager->initHooks(array(
'fileupload'));
71 $this->fk_element = $fk_element;
72 $this->element = $element;
74 $pathname = str_replace(
'/class',
'', $element_prop[
'classpath']);
81 $object_ref =
'UndefinedReference';
84 if ($pathname !==
null && $filename !==
null) {
86 $object = fetchObjectByElement($fk_element, $element);
92 dol_syslog(get_class($this).
"::__construct object ".$element.
" with id ".((
int) $fk_element).
" was not found", LOG_WARNING);
106 if (!empty($tmpdir) && preg_match(
'/^([a-z]:)?[\\\\\/]/i', $tmpdir)) {
128 if (in_array($element, array(
'invoice_supplier',
'supplier_invoice'))) {
129 $object_ref .=
'/'.dol_sanitizeFileName(
$object->ref);
135 if (empty($dir_output)) {
136 dol_syslog(get_class($this).
"::__construct element ".$element.
" is not supported for uploading file, dir_output is unknown", LOG_WARNING);
137 throw new Exception(
'elementnotsupported');
144 $this->options = array(
145 'script_url' => $_SERVER[
'PHP_SELF'],
146 'upload_dir' => $dir_output.
'/'.$object_ref.
'/',
147 'upload_url' => DOL_URL_ROOT.
'/document.php?modulepart='.$element.
'&attachment=1&file=/'.$object_ref.
'/',
148 'saving_doc_mask' => $savingDocMask,
149 'param_name' =>
'files',
152 'delete_type' =>
'DELETE',
155 'max_file_size' =>
null,
156 'min_file_size' => 1,
157 'accept_file_types' =>
'/.+$/i',
159 'max_number_of_files' =>
null,
162 'max_height' =>
null,
166 'discard_aborted_uploads' =>
true,
167 'image_versions' => array(
180 'thumbnail' => array(
181 'upload_dir' => $dir_output.
'/'.$object_ref.
'/thumbs/',
182 'upload_url' => DOL_URL_ROOT.
'/document.php?modulepart='.urlencode($element).
'&attachment=1&file='.urlencode(
'/'.$object_ref.
'/thumbs/'),
191 $hookmanager->executeHooks(
192 'overrideUploadOptions',
194 'options' => &$options,
195 'element' => $element
202 $this->options = array_replace_recursive($this->options, $options);
206 if (empty($pathname) || empty($filename)) {
207 if (!array_key_exists(
"upload_dir", $this->options)) {
208 setEventMessage(
'If $fk_element = null or $element = null you must specify upload_dir on $options',
'errors');
209 throw new Exception(
'If $fk_element = null or $element = null you must specify upload_dir on $options');
210 } elseif (!is_dir($this->options[
'upload_dir'])) {
211 setEventMessage(
'The directory '.$this->options[
'upload_dir'].
' doesn\'t exists',
'errors');
212 throw new Exception(
'The directory '.$this->options[
'upload_dir'].
' doesn\'t exists');
213 } elseif (!is_writable($this->options[
'upload_dir'])) {
214 setEventMessage(
'The directory '.$this->options[
'upload_dir'].
' is not writable',
'errors');
215 throw new Exception(
'The directory '.$this->options[
'upload_dir'].
' is not writable');
227 $https = !empty($_SERVER[
'HTTPS']) && $_SERVER[
'HTTPS'] !==
'off';
229 ($https ?
'https://' :
'http://').
230 (!empty($_SERVER[
'REMOTE_USER']) ? $_SERVER[
'REMOTE_USER'].
'@' :
'').
231 (isset($_SERVER[
'HTTP_HOST']) ? $_SERVER[
'HTTP_HOST'] : ($_SERVER[
'SERVER_NAME'].
232 ($https && $_SERVER[
'SERVER_PORT'] === 443 ||
233 $_SERVER[
'SERVER_PORT'] === 80 ?
'' :
':'.$_SERVER[
'SERVER_PORT']))).
234 substr($_SERVER[
'SCRIPT_NAME'], 0, strrpos($_SERVER[
'SCRIPT_NAME'],
'/'));
245 $file->delete_url = $this->options[
'script_url'].
'?file='.urlencode((
string) ($file->name)).
'&fk_element='.urlencode((
string) ($this->fk_element)).
'&element='.urlencode((
string) ($this->element));
246 $file->delete_type = $this->options[
'delete_type'];
247 if ($file->delete_type !==
'DELETE') {
248 $file->delete_url .=
'&_method=DELETE';
260 $file_path = $this->options[
'upload_dir'].dol_sanitizeFileName($file_name);
262 if (
dol_is_file($file_path) && $file_name[0] !==
'.') {
264 $file->name = $file_name;
266 $file->size = filesize($file_path);
267 $file->url = $this->options[
'upload_url'].urlencode($file->name);
269 foreach ($this->options[
'image_versions'] as $version => $options) {
270 if (
dol_is_file($options[
'upload_dir'].$file_name)) {
271 $tmp = explode(
'.', $file->name);
274 $keyforfile = $version.
'_url';
275 $file->$keyforfile = $options[
'upload_url'].urlencode($tmp[0].
'_mini.'.$tmp[1]);
291 return array_values(array_filter(array_map(array($this,
'getFileObject'), scandir($this->options[
'upload_dir']))));
303 global $maxwidthmini, $maxheightmini, $maxwidthsmall, $maxheightsmall;
305 $file_path = $this->options[
'upload_dir'].$file_name;
306 $new_file_path = $options[
'upload_dir'].$file_name;
308 if (
dol_mkdir($options[
'upload_dir']) >= 0) {
309 list($img_width, $img_height) = @getimagesize($file_path);
310 if (!$img_width || !$img_height) {
314 $res =
vignette($file_path, $maxwidthmini, $maxheightmini,
'_mini');
315 if (preg_match(
'/error/i', $res)) {
319 $res =
vignette($file_path, $maxwidthsmall, $maxheightsmall,
'_small');
320 if (preg_match(
'/error/i', $res)) {
339 protected function validate($uploaded_file, $file, $error, $index)
342 $file->error = $error;
346 $file->error =
'missingFileName';
349 if (!preg_match($this->options[
'accept_file_types'], $file->name)) {
350 $file->error =
'acceptFileTypes';
353 if ($uploaded_file && is_uploaded_file($uploaded_file)) {
356 $file_size = $_SERVER[
'CONTENT_LENGTH'];
358 if ($this->options[
'max_file_size'] && (
359 $file_size > $this->options[
'max_file_size'] ||
360 $file->size > $this->options[
'max_file_size']
363 $file->error =
'maxFileSize';
366 if ($this->options[
'min_file_size'] &&
367 $file_size < $this->options[
'min_file_size']) {
368 $file->error =
'minFileSize';
371 if (is_numeric($this->options[
'max_number_of_files']) && (
372 count($this->
getFileObjects()) >= $this->options[
'max_number_of_files']
375 $file->error =
'maxNumberOfFiles';
378 list($img_width, $img_height) = @getimagesize($uploaded_file);
379 if (is_numeric($img_width)) {
380 if ($this->options[
'max_width'] && $img_width > $this->options[
'max_width'] ||
381 $this->options[
'max_height'] && $img_height > $this->options[
'max_height']) {
382 $file->error =
'maxResolution';
385 if ($this->options[
'min_width'] && $img_width < $this->options[
'min_width'] ||
386 $this->options[
'min_height'] && $img_height < $this->options[
'min_height']) {
387 $file->error =
'minResolution';
402 $index = isset($matches[1]) ? intval($matches[1]) + 1 : 1;
403 $ext = isset($matches[2]) ? $matches[2] :
'';
404 return ' ('.$index.
')'.$ext;
415 return preg_replace_callback(
'/(?:(?: \(([\d]+)\))?(\.[^.]+))?$/', array($this,
'upcountNameCallback'), $name, 1);
431 $file_name = preg_replace(
'/ {2,}/',
' ', $file_name);
434 if (strpos($file_name,
'.') ===
false && preg_match(
'/^image\/(gif|jpe?g|png)/', $type, $matches)) {
435 $file_name .=
'.'.$matches[1];
437 if ($this->options[
'discard_aborted_uploads']) {
438 while (
dol_is_file($this->options[
'upload_dir'].$file_name)) {
461 $file->name = $this->
trimFileName($name, $type, $index);
463 $file->size = intval($size);
468 $uploaded_file = preg_replace(
'/\s*(http|ftp|sftp|)s?:/i',
'', $uploaded_file);
469 $uploaded_file = realpath($uploaded_file);
471 $validate = $this->
validate($uploaded_file, $file, $error, $index);
474 if (
dol_mkdir($this->options[
'upload_dir']) >= 0) {
476 $fileNameWithoutExt = preg_replace(
'/\.[^\.]+$/',
'', $file->name);
477 $savingDocMask = $this->options[
'saving_doc_mask'];
478 if ($savingDocMask && strpos($savingDocMask, $fileNameWithoutExt) !== 0) {
479 $fileNameWithPrefix = preg_replace(
'/__file__/', $file->name, $savingDocMask);
480 $file->name = $fileNameWithPrefix;
490 if ($this->options[
'discard_aborted_uploads']) {
492 while (
dol_is_file($tmppath.dol_sanitizeFileName($file->name)) ||
dol_is_file($tmppath.dol_sanitizeFileName($file->name).
'.noexe')) {
497 $file_path =
dol_sanitizePathName($this->options[
'upload_dir']).dol_sanitizeFileName($file->name);
498 $append_file = !$this->options[
'discard_aborted_uploads'] &&
dol_is_file($file_path) && $file->size >
dol_filesize($file_path);
502 if ($uploaded_file && is_uploaded_file($uploaded_file)) {
505 file_put_contents($file_path, fopen($uploaded_file,
'r'), FILE_APPEND);
514 $file->name .=
'.noexe';
515 $file_path .=
'.noexe';
520 file_put_contents($file_path, fopen(
'php://input',
'r'), $append_file ? FILE_APPEND : 0);
525 if ($file_size === $file->size) {
526 $file->url = $this->options[
'upload_url'].urlencode($file->name);
527 foreach ($this->options[
'image_versions'] as $version => $options) {
529 $tmp = explode(
'.', $file->name);
532 $keyforfile = $version.
'_url';
533 $file->$keyforfile = $options[
'upload_url'].urlencode($tmp[0].
'_mini.'.$tmp[1]);
536 } elseif ($this->options[
'discard_aborted_uploads']) {
538 $file->error =
'abort';
540 $file->size = $file_size;
543 $file->error =
'failedtocreatedestdir';
580 $upload = isset($_FILES[$this->options[
'param_name']]) ? $_FILES[$this->options[
'param_name']] :
null;
583 if ($upload && is_array($upload[
'tmp_name'])) {
586 foreach ($upload[
'tmp_name'] as $index => $value) {
588 $upload[
'tmp_name'][$index],
589 isset($_SERVER[
'HTTP_X_FILE_NAME']) ? $_SERVER[
'HTTP_X_FILE_NAME'] : $upload[
'name'][$index],
590 isset($_SERVER[
'HTTP_X_FILE_SIZE']) ? $_SERVER[
'HTTP_X_FILE_SIZE'] : $upload[
'size'][$index],
591 isset($_SERVER[
'HTTP_X_FILE_TYPE']) ? $_SERVER[
'HTTP_X_FILE_TYPE'] : $upload[
'type'][$index],
592 $upload[
'error'][$index],
595 if (!empty($tmpres->error)) {
600 } elseif ($upload || isset($_SERVER[
'HTTP_X_FILE_NAME'])) {
604 isset($upload[
'tmp_name']) ? $upload[
'tmp_name'] :
null,
605 isset($_SERVER[
'HTTP_X_FILE_NAME']) ? $_SERVER[
'HTTP_X_FILE_NAME'] : (isset($upload[
'name']) ? $upload[
'name'] :
null),
606 isset($_SERVER[
'HTTP_X_FILE_SIZE']) ? $_SERVER[
'HTTP_X_FILE_SIZE'] : (isset($upload[
'size']) ? $upload[
'size'] :
null),
607 isset($_SERVER[
'HTTP_X_FILE_TYPE']) ? $_SERVER[
'HTTP_X_FILE_TYPE'] : (isset($upload[
'type']) ? $upload[
'type'] :
null),
608 isset($upload[
'error']) ? $upload[
'error'] :
null,
611 if (!empty($tmpres->error)) {
617 header(
'Vary: Accept');
618 $json = json_encode($info);
628 if (isset($_SERVER[
'HTTP_ACCEPT']) && (strpos($_SERVER[
'HTTP_ACCEPT'],
'application/json') !==
false)) {
629 header(
'Content-type: application/json');
631 header(
'Content-type: text/plain');
if(! $sortfield) if(! $sortorder) $object
This class is used to manage file upload using ajax.
getFileObjects()
getFileObjects
setFileDeleteUrl($file)
Set delete url.
__construct($options=null, $fk_element=null, $element=null)
Constructor.
handleFileUpload($uploaded_file, $name, $size, $type, $error, $index)
handleFileUpload.
upcountName($name)
Enter description here ...
getFileObject($file_name)
getFileObject
upcountNameCallback($matches)
Enter description here ...
createScaledImage($file_name, $options)
Create thumbs of a file uploaded.
getFullUrl()
Return full URL.
trimFileName($name, $type, $index)
trimFileName
validate($uploaded_file, $file, $error, $index)
Make validation on an uploaded file.
dol_filesize($pathoffile)
Return size of a file.
dol_is_file($pathoffile)
Return if path is a file.
dol_move_uploaded_file($src_file, $dest_file, $allowoverwrite, $disablevirusscan=0, $uploaderrorcode=0, $nohook=0, $keyforsourcefile='addedfile', $upload_dir='', $mode=0)
Check validity of a file upload from an GUI page, and move it to its final destination.
dol_mimetype($file, $default='application/octet-stream', $mode=0)
Return MIME type of a file from its name with extension.
dol_sanitizePathName($str, $newstr='_', $unaccent=0, $allowdash=0)
Clean a string to use it as a path name.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
dolChmod($filepath, $newmask='')
Change mod of a file.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
getElementProperties($elementType)
Get an array with properties of an element.
getMultidirOutput($object, $module='', $forobject=0, $mode='output')
Return the full path of the directory where a module (or an object of a module) stores its files.
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart='')
Return a path to have a the directory according to object where files are stored.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
setEventMessage($mesgs, $style='mesgs', $noduplicate=0, $attop=0)
Set event message in dol_events session object.
vignette($file, $maxWidth=160, $maxHeight=120, $extName='_small', $quality=50, $outdir='thumbs', $targetformat=0)
Create a thumbnail from an image file (Supported extensions are gif, jpg, png and bmp).
print $langs trans("Show") . '< td style="' . $timeColor . '" align="center"> s</td > badge status0 badge status4 badge status3 Error badge status8< td align="center">< span class="badge ' . $badge . '"></span ></td >< td align="center">< a href="#" class="button button-small" onclick="openLogModal(this)" data-req="' . dol_escape_htmltag($reqSafe) . '" data-res="' . dol_escape_htmltag($resSafe) . '" data-err="' . dol_escape_htmltag($errSafe) . '">< span class="fa fa-search-plus"></span ></a ></td ></tr >< tr >< td colspan="' . $colspan . '" class="opacitymedium"></td ></tr ></table ></div ></form > logModal none logModal none s a JSON string