24 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
25 require_once DOL_DOCUMENT_ROOT.
'/core/lib/images.lib.php';
34 protected $fk_element;
44 public function __construct($options =
null, $fk_element =
null, $element =
null)
55 $hookmanager->initHooks(array(
'fileupload'));
57 $this->fk_element = $fk_element;
58 $this->element = $element;
60 $pathname = $filename = $element;
61 if (preg_match(
'/^([^_]+)_([^_]+)/i', $element, $regs)) {
66 $parentForeignKey =
'';
69 if ($element ==
'propal') {
70 $pathname =
'comm/propal';
71 $dir_output = $conf->$element->dir_output;
72 } elseif ($element ==
'facture') {
73 $pathname =
'compta/facture';
74 $dir_output = $conf->$element->dir_output;
75 } elseif ($element ==
'project') {
76 $element = $pathname =
'projet';
77 $dir_output = $conf->$element->dir_output;
78 } elseif ($element ==
'project_task') {
81 $dir_output = $conf->project->dir_output;
82 $parentForeignKey =
'fk_project';
83 $parentClass =
'Project';
84 $parentElement =
'projet';
85 $parentObject =
'project';
86 } elseif ($element ==
'fichinter') {
87 $element =
'ficheinter';
88 $dir_output = $conf->$element->dir_output;
89 } elseif ($element ==
'order_supplier') {
91 $filename =
'fournisseur.commande';
92 $dir_output = $conf->fournisseur->commande->dir_output;
93 } elseif ($element ==
'invoice_supplier') {
95 $filename =
'fournisseur.facture';
96 $dir_output = $conf->fournisseur->facture->dir_output;
97 } elseif ($element ==
'product') {
98 $dir_output = $conf->product->multidir_output[$conf->entity];
99 } elseif ($element ==
'productbatch') {
100 $dir_output = $conf->productbatch->multidir_output[$conf->entity];
101 } elseif ($element ==
'action') {
102 $pathname =
'comm/action';
103 $filename =
'actioncomm';
104 $dir_output = $conf->agenda->dir_output;
105 } elseif ($element ==
'chargesociales') {
106 $pathname =
'compta/sociales';
107 $filename =
'chargesociales';
108 $dir_output = $conf->tax->dir_output;
110 $dir_output = $conf->$element->dir_output;
115 $classname = ucfirst($filename);
117 if ($element ==
'order_supplier') {
118 $classname =
'CommandeFournisseur';
119 } elseif ($element ==
'invoice_supplier') {
120 $classname =
'FactureFournisseur';
123 $object =
new $classname($db);
125 $object->fetch($fk_element);
126 if (!empty($parentForeignKey)) {
128 $parent =
new $parentClass($db);
129 $parent->fetch($object->$parentForeignKey);
130 if (!empty($parent->socid)) {
131 $parent->fetch_thirdparty();
133 $object->$parentObject = clone $parent;
135 $object->fetch_thirdparty();
139 if ($element ==
'invoice_supplier') {
140 $object_ref =
get_exdir($object->id, 2, 0, 0, $object,
'invoice_supplier').$object_ref;
141 } elseif ($element ==
'project_task') {
142 $object_ref = $object->project->ref.
'/'.$object_ref;
145 $this->options = array(
146 'script_url' => $_SERVER[
'PHP_SELF'],
147 'upload_dir' => $dir_output.
'/'.$object_ref.
'/',
148 'upload_url' => DOL_URL_ROOT.
'/document.php?modulepart='.$element.
'&attachment=1&file=/'.$object_ref.
'/',
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='.$element.
'&attachment=1&file=/'.$object_ref.
'/thumbs/',
191 $hookmanager->executeHooks(
192 'overrideUploadOptions',
194 'options' => &$options,
195 'element' => $element
202 $this->options = array_replace_recursive($this->options, $options);
213 $https = !empty($_SERVER[
'HTTPS']) && $_SERVER[
'HTTPS'] !==
'off';
215 ($https ?
'https://' :
'http://').
216 (!empty($_SERVER[
'REMOTE_USER']) ? $_SERVER[
'REMOTE_USER'].
'@' :
'').
217 (isset($_SERVER[
'HTTP_HOST']) ? $_SERVER[
'HTTP_HOST'] : ($_SERVER[
'SERVER_NAME'].
218 ($https && $_SERVER[
'SERVER_PORT'] === 443 ||
219 $_SERVER[
'SERVER_PORT'] === 80 ?
'' :
':'.$_SERVER[
'SERVER_PORT']))).
220 substr($_SERVER[
'SCRIPT_NAME'], 0, strrpos($_SERVER[
'SCRIPT_NAME'],
'/'));
231 $file->delete_url = $this->options[
'script_url']
232 .
'?file='.urlencode($file->name).
'&fk_element='.urlencode($this->fk_element).
'&element='.urlencode($this->element);
233 $file->delete_type = $this->options[
'delete_type'];
234 if ($file->delete_type !==
'DELETE') {
235 $file->delete_url .=
'&_method=DELETE';
251 $file_path = $this->options[
'upload_dir'].$file_name;
252 if (is_file($file_path) && $file_name[0] !==
'.') {
253 $file =
new stdClass();
254 $file->name = $file_name;
256 $file->size = filesize($file_path);
257 $file->url = $this->options[
'upload_url'].rawurlencode($file->name);
258 foreach ($this->options[
'image_versions'] as $version => $options) {
259 if (is_file($options[
'upload_dir'].$file_name)) {
260 $tmp = explode(
'.', $file->name);
261 $file->{$version.
'_url'} = $options[
'upload_url'].rawurlencode($tmp[0].
'_mini.'.$tmp[1]);
277 return array_values(array_filter(array_map(array($this,
'getFileObject'), scandir($this->options[
'upload_dir']))));
289 global $maxwidthmini, $maxheightmini;
295 $file_path = $this->options[
'upload_dir'].$file_name;
296 $new_file_path = $options[
'upload_dir'].$file_name;
298 if (
dol_mkdir($options[
'upload_dir']) >= 0) {
299 list($img_width, $img_height) = @getimagesize($file_path);
300 if (!$img_width || !$img_height) {
304 $res =
vignette($file_path, $maxwidthmini, $maxheightmini,
'_mini');
306 if (preg_match(
'/error/i', $res)) {
324 protected function validate($uploaded_file, $file, $error, $index)
331 $file->error = $error;
335 $file->error =
'missingFileName';
338 if (!preg_match($this->options[
'accept_file_types'], $file->name)) {
339 $file->error =
'acceptFileTypes';
342 if ($uploaded_file && is_uploaded_file($uploaded_file)) {
343 $file_size = filesize($uploaded_file);
345 $file_size = $_SERVER[
'CONTENT_LENGTH'];
347 if ($this->options[
'max_file_size'] && (
348 $file_size > $this->options[
'max_file_size'] ||
349 $file->size > $this->options[
'max_file_size'])
351 $file->error =
'maxFileSize';
354 if ($this->options[
'min_file_size'] &&
355 $file_size < $this->options[
'min_file_size']) {
356 $file->error =
'minFileSize';
359 if (is_numeric($this->options[
'max_number_of_files']) && (
360 count($this->getFileObjects()) >= $this->options[
'max_number_of_files'])
362 $file->error =
'maxNumberOfFiles';
365 list($img_width, $img_height) = @getimagesize($uploaded_file);
366 if (is_numeric($img_width)) {
367 if ($this->options[
'max_width'] && $img_width > $this->options[
'max_width'] ||
368 $this->options[
'max_height'] && $img_height > $this->options[
'max_height']) {
369 $file->error =
'maxResolution';
372 if ($this->options[
'min_width'] && $img_width < $this->options[
'min_width'] ||
373 $this->options[
'min_height'] && $img_height < $this->options[
'min_height']) {
374 $file->error =
'minResolution';
389 $index = isset($matches[1]) ? intval($matches[1]) + 1 : 1;
390 $ext = isset($matches[2]) ? $matches[2] :
'';
391 return ' ('.$index.
')'.$ext;
402 return preg_replace_callback(
'/(?:(?: \(([\d]+)\))?(\.[^.]+))?$/', array($this,
'upcountNameCallback'), $name, 1);
418 $file_name = trim(basename(stripslashes($name)),
".\x00..\x20");
421 if (strpos($file_name,
'.') ===
false && preg_match(
'/^image\/(gif|jpe?g|png)/', $type, $matches)) {
422 $file_name .=
'.'.$matches[1];
424 if ($this->options[
'discard_aborted_uploads']) {
425 while (is_file($this->options[
'upload_dir'].$file_name)) {
426 $file_name = $this->upcountName($file_name);
449 $file =
new stdClass();
450 $file->name = $this->trimFileName($name, $type, $index);
452 $file->size = intval($size);
454 if ($this->validate($uploaded_file, $file, $error, $index) &&
dol_mkdir($this->options[
'upload_dir']) >= 0) {
455 $file_path = $this->options[
'upload_dir'].$file->name;
456 $append_file = !$this->options[
'discard_aborted_uploads'] && is_file($file_path) && $file->size > filesize($file_path);
458 if ($uploaded_file && is_uploaded_file($uploaded_file)) {
461 file_put_contents($file_path, fopen($uploaded_file,
'r'), FILE_APPEND);
467 file_put_contents($file_path, fopen(
'php://input',
'r'), $append_file ? FILE_APPEND : 0);
469 $file_size = filesize($file_path);
470 if ($file_size === $file->size) {
471 $file->url = $this->options[
'upload_url'].rawurlencode($file->name);
472 foreach ($this->options[
'image_versions'] as $version => $options) {
473 if ($this->createScaledImage($file->name, $options)) {
474 $tmp = explode(
'.', $file->name);
475 $file->{$version.
'_url'} = $options[
'upload_url'].rawurlencode($tmp[0].
'_mini.'.$tmp[1]);
478 } elseif ($this->options[
'discard_aborted_uploads']) {
480 $file->error =
'abort';
482 $file->size = $file_size;
483 $this->setFileDeleteUrl($file);
493 public function get()
499 $file_name = isset($_REQUEST[
'file']) ?
500 basename(stripslashes($_REQUEST[
'file'])) :
null;
502 $info = $this->getFileObject($file_name);
504 $info = $this->getFileObjects();
506 header(
'Content-type: application/json');
507 echo json_encode($info);
521 if (isset($_REQUEST[
'_method']) && $_REQUEST[
'_method'] ===
'DELETE') {
522 return $this->
delete();
524 $upload = isset($_FILES[$this->options[
'param_name']]) ?
525 $_FILES[$this->options[
'param_name']] :
null;
527 if ($upload && is_array($upload[
'tmp_name'])) {
530 foreach ($upload[
'tmp_name'] as $index => $value) {
531 $info[] = $this->handleFileUpload(
532 $upload[
'tmp_name'][$index],
533 isset($_SERVER[
'HTTP_X_FILE_NAME']) ? $_SERVER[
'HTTP_X_FILE_NAME'] : $upload[
'name'][$index],
534 isset($_SERVER[
'HTTP_X_FILE_SIZE']) ? $_SERVER[
'HTTP_X_FILE_SIZE'] : $upload[
'size'][$index],
535 isset($_SERVER[
'HTTP_X_FILE_TYPE']) ? $_SERVER[
'HTTP_X_FILE_TYPE'] : $upload[
'type'][$index],
536 $upload[
'error'][$index],
540 } elseif ($upload || isset($_SERVER[
'HTTP_X_FILE_NAME'])) {
543 $info[] = $this->handleFileUpload(
544 isset($upload[
'tmp_name']) ? $upload[
'tmp_name'] :
null,
545 isset($_SERVER[
'HTTP_X_FILE_NAME']) ? $_SERVER[
'HTTP_X_FILE_NAME'] : (isset($upload[
'name']) ? $upload[
'name'] :
null),
546 isset($_SERVER[
'HTTP_X_FILE_SIZE']) ? $_SERVER[
'HTTP_X_FILE_SIZE'] : (isset($upload[
'size']) ? $upload[
'size'] :
null),
547 isset($_SERVER[
'HTTP_X_FILE_TYPE']) ? $_SERVER[
'HTTP_X_FILE_TYPE'] : (isset($upload[
'type']) ? $upload[
'type'] :
null),
548 isset($upload[
'error']) ? $upload[
'error'] :
null,
552 header(
'Vary: Accept');
553 $json = json_encode($info);
554 $redirect = isset($_REQUEST[
'redirect']) ?
555 stripslashes($_REQUEST[
'redirect']) :
null;
557 header(
'Location: '.sprintf($redirect, rawurlencode($json)));
560 if (isset($_SERVER[
'HTTP_ACCEPT']) &&
561 (strpos($_SERVER[
'HTTP_ACCEPT'],
'application/json') !==
false)) {
562 header(
'Content-type: application/json');
564 header(
'Content-type: text/plain');
574 public function delete()
580 $file_name = isset($_REQUEST[
'file']) ?
581 basename(stripslashes($_REQUEST[
'file'])) :
null;
582 $file_path = $this->options[
'upload_dir'].$file_name;
583 $success = is_file($file_path) && $file_name[0] !==
'.' && unlink($file_path);
585 foreach ($this->options[
'image_versions'] as $version => $options) {
586 $file = $options[
'upload_dir'].$file_name;
587 if (is_file($file)) {
592 header(
'Content-type: application/json');
593 echo json_encode($success);
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)
Enter description here ...
dol_move_uploaded_file($src_file, $dest_file, $allowoverwrite, $disablevirusscan=0, $uploaderrorcode=0, $nohook=0, $varfiles='addedfile', $upload_dir='')
Make control on an uploaded file from an GUI page and move it to final destination.
dol_mimetype($file, $default='application/octet-stream', $mode=0)
Return MIME type of a file from its name with extension.
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart='')
Return a path to have a the directory according to object where files are stored.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
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).