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/',
183 $hookmanager->executeHooks(
184 'overrideUploadOptions',
186 'options' => &$options,
187 'element' => $element
195 $this->options = array_replace_recursive($this->options, $options);
206 $https = !empty($_SERVER[
'HTTPS']) && $_SERVER[
'HTTPS'] !==
'off';
208 ($https ?
'https://' :
'http://').
209 (!empty($_SERVER[
'REMOTE_USER']) ? $_SERVER[
'REMOTE_USER'].
'@' :
'').
210 (isset($_SERVER[
'HTTP_HOST']) ? $_SERVER[
'HTTP_HOST'] : ($_SERVER[
'SERVER_NAME'].
211 ($https && $_SERVER[
'SERVER_PORT'] === 443 ||
212 $_SERVER[
'SERVER_PORT'] === 80 ?
'' :
':'.$_SERVER[
'SERVER_PORT']))).
213 substr($_SERVER[
'SCRIPT_NAME'], 0, strrpos($_SERVER[
'SCRIPT_NAME'],
'/'));
224 $file->delete_url = $this->options[
'script_url']
225 .
'?file='.urlencode($file->name).
'&fk_element='.urlencode($this->fk_element).
'&element='.urlencode($this->element);
226 $file->delete_type = $this->options[
'delete_type'];
227 if ($file->delete_type !==
'DELETE') {
228 $file->delete_url .=
'&_method=DELETE';
240 $file_path = $this->options[
'upload_dir'].$file_name;
241 if (is_file($file_path) && $file_name[0] !==
'.') {
242 $file =
new stdClass();
243 $file->name = $file_name;
245 $file->size = filesize($file_path);
246 $file->url = $this->options[
'upload_url'].rawurlencode($file->name);
247 foreach ($this->options[
'image_versions'] as $version => $options) {
248 if (is_file($options[
'upload_dir'].$file_name)) {
249 $tmp = explode(
'.', $file->name);
250 $file->{$version.
'_url'} = $options[
'upload_url'].rawurlencode($tmp[0].
'_mini.'.$tmp[1]);
266 return array_values(array_filter(array_map(array($this,
'getFileObject'), scandir($this->options[
'upload_dir']))));
278 global $maxwidthmini, $maxheightmini;
280 $file_path = $this->options[
'upload_dir'].$file_name;
281 $new_file_path = $options[
'upload_dir'].$file_name;
283 if (
dol_mkdir($options[
'upload_dir']) >= 0) {
284 list($img_width, $img_height) = @getimagesize($file_path);
285 if (!$img_width || !$img_height) {
289 $res =
vignette($file_path, $maxwidthmini, $maxheightmini,
'_mini');
291 if (preg_match(
'/error/i', $res)) {
309 protected function validate($uploaded_file, $file, $error, $index)
312 $file->error = $error;
316 $file->error =
'missingFileName';
319 if (!preg_match($this->options[
'accept_file_types'], $file->name)) {
320 $file->error =
'acceptFileTypes';
323 if ($uploaded_file && is_uploaded_file($uploaded_file)) {
324 $file_size = filesize($uploaded_file);
326 $file_size = $_SERVER[
'CONTENT_LENGTH'];
328 if ($this->options[
'max_file_size'] && (
329 $file_size > $this->options[
'max_file_size'] ||
330 $file->size > $this->options[
'max_file_size'])
332 $file->error =
'maxFileSize';
335 if ($this->options[
'min_file_size'] &&
336 $file_size < $this->options[
'min_file_size']) {
337 $file->error =
'minFileSize';
340 if (is_numeric($this->options[
'max_number_of_files']) && (
341 count($this->getFileObjects()) >= $this->options[
'max_number_of_files'])
343 $file->error =
'maxNumberOfFiles';
346 list($img_width, $img_height) = @getimagesize($uploaded_file);
347 if (is_numeric($img_width)) {
348 if ($this->options[
'max_width'] && $img_width > $this->options[
'max_width'] ||
349 $this->options[
'max_height'] && $img_height > $this->options[
'max_height']) {
350 $file->error =
'maxResolution';
353 if ($this->options[
'min_width'] && $img_width < $this->options[
'min_width'] ||
354 $this->options[
'min_height'] && $img_height < $this->options[
'min_height']) {
355 $file->error =
'minResolution';
370 $index = isset($matches[1]) ? intval($matches[1]) + 1 : 1;
371 $ext = isset($matches[2]) ? $matches[2] :
'';
372 return ' ('.$index.
')'.$ext;
383 return preg_replace_callback(
'/(?:(?: \(([\d]+)\))?(\.[^.]+))?$/', array($this,
'upcountNameCallback'), $name, 1);
399 $file_name = trim(basename(stripslashes($name)),
".\x00..\x20");
401 if (strpos($file_name,
'.') ===
false &&
402 preg_match(
'/^image\/(gif|jpe?g|png)/', $type, $matches)) {
403 $file_name .=
'.'.$matches[1];
405 if ($this->options[
'discard_aborted_uploads']) {
406 while (is_file($this->options[
'upload_dir'].$file_name)) {
407 $file_name = $this->upcountName($file_name);
426 $file =
new stdClass();
427 $file->name = $this->trimFileName($name, $type, $index);
429 $file->size = intval($size);
431 if ($this->validate($uploaded_file, $file, $error, $index) &&
dol_mkdir($this->options[
'upload_dir']) >= 0) {
432 $file_path = $this->options[
'upload_dir'].$file->name;
433 $append_file = !$this->options[
'discard_aborted_uploads'] && is_file($file_path) && $file->size > filesize($file_path);
435 if ($uploaded_file && is_uploaded_file($uploaded_file)) {
438 file_put_contents($file_path, fopen($uploaded_file,
'r'), FILE_APPEND);
444 file_put_contents($file_path, fopen(
'php://input',
'r'), $append_file ? FILE_APPEND : 0);
446 $file_size = filesize($file_path);
447 if ($file_size === $file->size) {
448 $file->url = $this->options[
'upload_url'].rawurlencode($file->name);
449 foreach ($this->options[
'image_versions'] as $version => $options) {
450 if ($this->createScaledImage($file->name, $options)) {
451 $tmp = explode(
'.', $file->name);
452 $file->{$version.
'_url'} = $options[
'upload_url'].rawurlencode($tmp[0].
'_mini.'.$tmp[1]);
455 } elseif ($this->options[
'discard_aborted_uploads']) {
457 $file->error =
'abort';
459 $file->size = $file_size;
460 $this->setFileDeleteUrl($file);
470 public function get()
472 $file_name = isset($_REQUEST[
'file']) ?
473 basename(stripslashes($_REQUEST[
'file'])) :
null;
475 $info = $this->getFileObject($file_name);
477 $info = $this->getFileObjects();
479 header(
'Content-type: application/json');
480 echo json_encode($info);
490 if (isset($_REQUEST[
'_method']) && $_REQUEST[
'_method'] ===
'DELETE') {
491 return $this->
delete();
493 $upload = isset($_FILES[$this->options[
'param_name']]) ?
494 $_FILES[$this->options[
'param_name']] :
null;
496 if ($upload && is_array($upload[
'tmp_name'])) {
499 foreach ($upload[
'tmp_name'] as $index => $value) {
500 $info[] = $this->handleFileUpload(
501 $upload[
'tmp_name'][$index],
502 isset($_SERVER[
'HTTP_X_FILE_NAME']) ? $_SERVER[
'HTTP_X_FILE_NAME'] : $upload[
'name'][$index],
503 isset($_SERVER[
'HTTP_X_FILE_SIZE']) ? $_SERVER[
'HTTP_X_FILE_SIZE'] : $upload[
'size'][$index],
504 isset($_SERVER[
'HTTP_X_FILE_TYPE']) ? $_SERVER[
'HTTP_X_FILE_TYPE'] : $upload[
'type'][$index],
505 $upload[
'error'][$index],
509 } elseif ($upload || isset($_SERVER[
'HTTP_X_FILE_NAME'])) {
512 $info[] = $this->handleFileUpload(
513 isset($upload[
'tmp_name']) ? $upload[
'tmp_name'] :
null,
514 isset($_SERVER[
'HTTP_X_FILE_NAME']) ? $_SERVER[
'HTTP_X_FILE_NAME'] : (isset($upload[
'name']) ? $upload[
'name'] :
null),
515 isset($_SERVER[
'HTTP_X_FILE_SIZE']) ? $_SERVER[
'HTTP_X_FILE_SIZE'] : (isset($upload[
'size']) ? $upload[
'size'] :
null),
516 isset($_SERVER[
'HTTP_X_FILE_TYPE']) ? $_SERVER[
'HTTP_X_FILE_TYPE'] : (isset($upload[
'type']) ? $upload[
'type'] :
null),
517 isset($upload[
'error']) ? $upload[
'error'] :
null,
521 header(
'Vary: Accept');
522 $json = json_encode($info);
523 $redirect = isset($_REQUEST[
'redirect']) ?
524 stripslashes($_REQUEST[
'redirect']) :
null;
526 header(
'Location: '.sprintf($redirect, rawurlencode($json)));
529 if (isset($_SERVER[
'HTTP_ACCEPT']) &&
530 (strpos($_SERVER[
'HTTP_ACCEPT'],
'application/json') !==
false)) {
531 header(
'Content-type: application/json');
533 header(
'Content-type: text/plain');
543 public function delete()
545 $file_name = isset($_REQUEST[
'file']) ?
546 basename(stripslashes($_REQUEST[
'file'])) :
null;
547 $file_path = $this->options[
'upload_dir'].$file_name;
548 $success = is_file($file_path) && $file_name[0] !==
'.' && unlink($file_path);
550 foreach ($this->options[
'image_versions'] as $version => $options) {
551 $file = $options[
'upload_dir'].$file_name;
552 if (is_file($file)) {
557 header(
'Content-type: application/json');
558 echo json_encode($success);