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)
49 $hookmanager->initHooks(array(
'fileupload'));
51 $this->fk_element = $fk_element;
52 $this->element = $element;
54 $pathname = $filename = $element;
55 if (preg_match(
'/^([^_]+)_([^_]+)/i', $element, $regs)) {
60 $parentForeignKey =
'';
63 if ($element ==
'propal') {
64 $pathname =
'comm/propal';
65 $dir_output = $conf->$element->dir_output;
66 } elseif ($element ==
'facture') {
67 $pathname =
'compta/facture';
68 $dir_output = $conf->$element->dir_output;
69 } elseif ($element ==
'project') {
70 $element = $pathname =
'projet';
71 $dir_output = $conf->$element->dir_output;
72 } elseif ($element ==
'project_task') {
75 $dir_output = $conf->project->dir_output;
76 $parentForeignKey =
'fk_project';
77 $parentClass =
'Project';
78 $parentElement =
'projet';
79 $parentObject =
'project';
80 } elseif ($element ==
'fichinter') {
81 $element =
'ficheinter';
82 $dir_output = $conf->$element->dir_output;
83 } elseif ($element ==
'order_supplier') {
85 $filename =
'fournisseur.commande';
86 $dir_output = $conf->fournisseur->commande->dir_output;
87 } elseif ($element ==
'invoice_supplier') {
89 $filename =
'fournisseur.facture';
90 $dir_output = $conf->fournisseur->facture->dir_output;
91 } elseif ($element ==
'product') {
92 $dir_output = $conf->product->multidir_output[$conf->entity];
93 } elseif ($element ==
'productbatch') {
94 $dir_output = $conf->productbatch->multidir_output[$conf->entity];
95 } elseif ($element ==
'action') {
96 $pathname =
'comm/action';
97 $filename =
'actioncomm';
98 $dir_output = $conf->agenda->dir_output;
99 } elseif ($element ==
'chargesociales') {
100 $pathname =
'compta/sociales';
101 $filename =
'chargesociales';
102 $dir_output = $conf->tax->dir_output;
104 $dir_output = $conf->$element->dir_output;
109 $classname = ucfirst($filename);
111 if ($element ==
'order_supplier') {
112 $classname =
'CommandeFournisseur';
113 } elseif ($element ==
'invoice_supplier') {
114 $classname =
'FactureFournisseur';
117 $object =
new $classname($db);
119 $object->fetch($fk_element);
120 if (!empty($parentForeignKey)) {
122 $parent =
new $parentClass($db);
123 $parent->fetch($object->$parentForeignKey);
124 if (!empty($parent->socid)) {
125 $parent->fetch_thirdparty();
127 $object->$parentObject = clone $parent;
129 $object->fetch_thirdparty();
133 if ($element ==
'invoice_supplier') {
134 $object_ref =
get_exdir($object->id, 2, 0, 0, $object,
'invoice_supplier').$object_ref;
135 } elseif ($element ==
'project_task') {
136 $object_ref = $object->project->ref.
'/'.$object_ref;
139 $this->options = array(
140 'script_url' => $_SERVER[
'PHP_SELF'],
141 'upload_dir' => $dir_output.
'/'.$object_ref.
'/',
142 'upload_url' => DOL_URL_ROOT.
'/document.php?modulepart='.$element.
'&attachment=1&file=/'.$object_ref.
'/',
143 'param_name' =>
'files',
146 'delete_type' =>
'DELETE',
149 'max_file_size' =>
null,
150 'min_file_size' => 1,
151 'accept_file_types' =>
'/.+$/i',
153 'max_number_of_files' =>
null,
156 'max_height' =>
null,
160 'discard_aborted_uploads' =>
true,
161 'image_versions' => array(
174 'thumbnail' => array(
175 'upload_dir' => $dir_output.
'/'.$object_ref.
'/thumbs/',
176 'upload_url' => DOL_URL_ROOT.
'/document.php?modulepart='.$element.
'&attachment=1&file=/'.$object_ref.
'/thumbs/',
185 $hookmanager->executeHooks(
186 'overrideUploadOptions',
188 'options' => &$options,
189 'element' => $element
196 $this->options = array_replace_recursive($this->options, $options);
207 $https = !empty($_SERVER[
'HTTPS']) && $_SERVER[
'HTTPS'] !==
'off';
209 ($https ?
'https://' :
'http://').
210 (!empty($_SERVER[
'REMOTE_USER']) ? $_SERVER[
'REMOTE_USER'].
'@' :
'').
211 (isset($_SERVER[
'HTTP_HOST']) ? $_SERVER[
'HTTP_HOST'] : ($_SERVER[
'SERVER_NAME'].
212 ($https && $_SERVER[
'SERVER_PORT'] === 443 ||
213 $_SERVER[
'SERVER_PORT'] === 80 ?
'' :
':'.$_SERVER[
'SERVER_PORT']))).
214 substr($_SERVER[
'SCRIPT_NAME'], 0, strrpos($_SERVER[
'SCRIPT_NAME'],
'/'));
225 $file->delete_url = $this->options[
'script_url']
226 .
'?file='.urlencode($file->name).
'&fk_element='.urlencode($this->fk_element).
'&element='.urlencode($this->element);
227 $file->delete_type = $this->options[
'delete_type'];
228 if ($file->delete_type !==
'DELETE') {
229 $file->delete_url .=
'&_method=DELETE';
241 $file_path = $this->options[
'upload_dir'].$file_name;
242 if (is_file($file_path) && $file_name[0] !==
'.') {
243 $file =
new stdClass();
244 $file->name = $file_name;
246 $file->size = filesize($file_path);
247 $file->url = $this->options[
'upload_url'].rawurlencode($file->name);
248 foreach ($this->options[
'image_versions'] as $version => $options) {
249 if (is_file($options[
'upload_dir'].$file_name)) {
250 $tmp = explode(
'.', $file->name);
251 $file->{$version.
'_url'} = $options[
'upload_url'].rawurlencode($tmp[0].
'_mini.'.$tmp[1]);
267 return array_values(array_filter(array_map(array($this,
'getFileObject'), scandir($this->options[
'upload_dir']))));
279 global $maxwidthmini, $maxheightmini;
281 $file_path = $this->options[
'upload_dir'].$file_name;
282 $new_file_path = $options[
'upload_dir'].$file_name;
284 if (
dol_mkdir($options[
'upload_dir']) >= 0) {
285 list($img_width, $img_height) = @getimagesize($file_path);
286 if (!$img_width || !$img_height) {
290 $res =
vignette($file_path, $maxwidthmini, $maxheightmini,
'_mini');
292 if (preg_match(
'/error/i', $res)) {
310 protected function validate($uploaded_file, $file, $error, $index)
313 $file->error = $error;
317 $file->error =
'missingFileName';
320 if (!preg_match($this->options[
'accept_file_types'], $file->name)) {
321 $file->error =
'acceptFileTypes';
324 if ($uploaded_file && is_uploaded_file($uploaded_file)) {
325 $file_size = filesize($uploaded_file);
327 $file_size = $_SERVER[
'CONTENT_LENGTH'];
329 if ($this->options[
'max_file_size'] && (
330 $file_size > $this->options[
'max_file_size'] ||
331 $file->size > $this->options[
'max_file_size'])
333 $file->error =
'maxFileSize';
336 if ($this->options[
'min_file_size'] &&
337 $file_size < $this->options[
'min_file_size']) {
338 $file->error =
'minFileSize';
341 if (is_numeric($this->options[
'max_number_of_files']) && (
342 count($this->getFileObjects()) >= $this->options[
'max_number_of_files'])
344 $file->error =
'maxNumberOfFiles';
347 list($img_width, $img_height) = @getimagesize($uploaded_file);
348 if (is_numeric($img_width)) {
349 if ($this->options[
'max_width'] && $img_width > $this->options[
'max_width'] ||
350 $this->options[
'max_height'] && $img_height > $this->options[
'max_height']) {
351 $file->error =
'maxResolution';
354 if ($this->options[
'min_width'] && $img_width < $this->options[
'min_width'] ||
355 $this->options[
'min_height'] && $img_height < $this->options[
'min_height']) {
356 $file->error =
'minResolution';
371 $index = isset($matches[1]) ? intval($matches[1]) + 1 : 1;
372 $ext = isset($matches[2]) ? $matches[2] :
'';
373 return ' ('.$index.
')'.$ext;
384 return preg_replace_callback(
'/(?:(?: \(([\d]+)\))?(\.[^.]+))?$/', array($this,
'upcountNameCallback'), $name, 1);
400 $file_name = trim(basename(stripslashes($name)),
".\x00..\x20");
402 if (strpos($file_name,
'.') ===
false &&
403 preg_match(
'/^image\/(gif|jpe?g|png)/', $type, $matches)) {
404 $file_name .=
'.'.$matches[1];
406 if ($this->options[
'discard_aborted_uploads']) {
407 while (is_file($this->options[
'upload_dir'].$file_name)) {
408 $file_name = $this->upcountName($file_name);
427 $file =
new stdClass();
428 $file->name = $this->trimFileName($name, $type, $index);
430 $file->size = intval($size);
432 if ($this->validate($uploaded_file, $file, $error, $index) &&
dol_mkdir($this->options[
'upload_dir']) >= 0) {
433 $file_path = $this->options[
'upload_dir'].$file->name;
434 $append_file = !$this->options[
'discard_aborted_uploads'] && is_file($file_path) && $file->size > filesize($file_path);
436 if ($uploaded_file && is_uploaded_file($uploaded_file)) {
439 file_put_contents($file_path, fopen($uploaded_file,
'r'), FILE_APPEND);
445 file_put_contents($file_path, fopen(
'php://input',
'r'), $append_file ? FILE_APPEND : 0);
447 $file_size = filesize($file_path);
448 if ($file_size === $file->size) {
449 $file->url = $this->options[
'upload_url'].rawurlencode($file->name);
450 foreach ($this->options[
'image_versions'] as $version => $options) {
451 if ($this->createScaledImage($file->name, $options)) {
452 $tmp = explode(
'.', $file->name);
453 $file->{$version.
'_url'} = $options[
'upload_url'].rawurlencode($tmp[0].
'_mini.'.$tmp[1]);
456 } elseif ($this->options[
'discard_aborted_uploads']) {
458 $file->error =
'abort';
460 $file->size = $file_size;
461 $this->setFileDeleteUrl($file);
471 public function get()
473 $file_name = isset($_REQUEST[
'file']) ?
474 basename(stripslashes($_REQUEST[
'file'])) :
null;
476 $info = $this->getFileObject($file_name);
478 $info = $this->getFileObjects();
480 header(
'Content-type: application/json');
481 echo json_encode($info);
491 if (isset($_REQUEST[
'_method']) && $_REQUEST[
'_method'] ===
'DELETE') {
492 return $this->
delete();
494 $upload = isset($_FILES[$this->options[
'param_name']]) ?
495 $_FILES[$this->options[
'param_name']] :
null;
497 if ($upload && is_array($upload[
'tmp_name'])) {
500 foreach ($upload[
'tmp_name'] as $index => $value) {
501 $info[] = $this->handleFileUpload(
502 $upload[
'tmp_name'][$index],
503 isset($_SERVER[
'HTTP_X_FILE_NAME']) ? $_SERVER[
'HTTP_X_FILE_NAME'] : $upload[
'name'][$index],
504 isset($_SERVER[
'HTTP_X_FILE_SIZE']) ? $_SERVER[
'HTTP_X_FILE_SIZE'] : $upload[
'size'][$index],
505 isset($_SERVER[
'HTTP_X_FILE_TYPE']) ? $_SERVER[
'HTTP_X_FILE_TYPE'] : $upload[
'type'][$index],
506 $upload[
'error'][$index],
510 } elseif ($upload || isset($_SERVER[
'HTTP_X_FILE_NAME'])) {
513 $info[] = $this->handleFileUpload(
514 isset($upload[
'tmp_name']) ? $upload[
'tmp_name'] :
null,
515 isset($_SERVER[
'HTTP_X_FILE_NAME']) ? $_SERVER[
'HTTP_X_FILE_NAME'] : (isset($upload[
'name']) ? $upload[
'name'] :
null),
516 isset($_SERVER[
'HTTP_X_FILE_SIZE']) ? $_SERVER[
'HTTP_X_FILE_SIZE'] : (isset($upload[
'size']) ? $upload[
'size'] :
null),
517 isset($_SERVER[
'HTTP_X_FILE_TYPE']) ? $_SERVER[
'HTTP_X_FILE_TYPE'] : (isset($upload[
'type']) ? $upload[
'type'] :
null),
518 isset($upload[
'error']) ? $upload[
'error'] :
null,
522 header(
'Vary: Accept');
523 $json = json_encode($info);
524 $redirect = isset($_REQUEST[
'redirect']) ?
525 stripslashes($_REQUEST[
'redirect']) :
null;
527 header(
'Location: '.sprintf($redirect, rawurlencode($json)));
530 if (isset($_SERVER[
'HTTP_ACCEPT']) &&
531 (strpos($_SERVER[
'HTTP_ACCEPT'],
'application/json') !==
false)) {
532 header(
'Content-type: application/json');
534 header(
'Content-type: text/plain');
544 public function delete()
546 $file_name = isset($_REQUEST[
'file']) ?
547 basename(stripslashes($_REQUEST[
'file'])) :
null;
548 $file_path = $this->options[
'upload_dir'].$file_name;
549 $success = is_file($file_path) && $file_name[0] !==
'.' && unlink($file_path);
551 foreach ($this->options[
'image_versions'] as $version => $options) {
552 $file = $options[
'upload_dir'].$file_name;
553 if (is_file($file)) {
558 header(
'Content-type: application/json');
559 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.
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).