69 public function index($modulepart, $original_file =
'')
73 if (empty($modulepart)) {
74 throw new RestException(400,
'bad value for parameter modulepart');
76 if (empty($original_file)) {
77 throw new RestException(400,
'bad value for parameter original_file');
81 $entity = $conf->entity;
92 $relativefile = $original_file;
95 $accessallowed = $check_access[
'accessallowed'];
96 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
97 $original_file = $check_access[
'original_file'];
99 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
100 throw new RestException(401);
102 if (!$accessallowed) {
103 throw new RestException(401);
106 $filename = basename($original_file);
107 $original_file_osencoded =
dol_osencode($original_file);
109 if (!file_exists($original_file_osencoded)) {
110 dol_syslog(
"Try to download not found file ".$original_file_osencoded, LOG_WARNING);
111 throw new RestException(404,
'File not found');
114 $file_content = file_get_contents($original_file_osencoded);
115 return array(
'filename'=>$filename,
'content-type' =>
dol_mimetype($filename),
'filesize'=>filesize($original_file),
'content'=>base64_encode($file_content),
'encoding'=>
'base64');
140 public function builddoc($modulepart, $original_file =
'', $doctemplate =
'', $langcode =
'')
142 global $conf, $langs;
144 if (empty($modulepart)) {
145 throw new RestException(400,
'bad value for parameter modulepart');
147 if (empty($original_file)) {
148 throw new RestException(400,
'bad value for parameter original_file');
151 $outputlangs = $langs;
152 if ($langcode && $langs->defaultlang != $langcode) {
154 $outputlangs->setDefaultLang($langcode);
158 $entity = $conf->entity;
169 $relativefile = $original_file;
172 $accessallowed = $check_access[
'accessallowed'];
173 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
174 $original_file = $check_access[
'original_file'];
176 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
177 throw new RestException(401);
179 if (!$accessallowed) {
180 throw new RestException(401);
190 if ($modulepart ==
'facture' || $modulepart ==
'invoice') {
191 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
192 $tmpobject =
new Facture($this->db);
193 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
195 throw new RestException(404,
'Invoice not found');
198 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
199 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
201 throw new RestException(500,
'Error generating document');
203 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'invoice_supplier') {
204 require_once DOL_DOCUMENT_ROOT .
'/fourn/class/fournisseur.facture.class.php';
206 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
208 throw new RestException(404,
'Supplier invoice not found');
211 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
212 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
214 throw new RestException(500,
'Error generating document');
216 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
217 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
218 $tmpobject =
new Commande($this->db);
219 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
221 throw new RestException(404,
'Order not found');
223 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
224 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
226 throw new RestException(500,
'Error generating document');
228 } elseif ($modulepart ==
'propal' || $modulepart ==
'proposal') {
229 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
230 $tmpobject =
new Propal($this->db);
231 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
233 throw new RestException(404,
'Proposal not found');
235 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
236 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
238 throw new RestException(500,
'Error generating document');
240 } elseif ($modulepart ==
'contrat' || $modulepart ==
'contract') {
241 require_once DOL_DOCUMENT_ROOT .
'/contrat/class/contrat.class.php';
243 $tmpobject =
new Contrat($this->db);
244 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
247 throw new RestException(404,
'Contract not found');
250 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
251 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
254 throw new RestException(500,
'Error generating document missing doctemplate parameter');
256 } elseif ($modulepart ==
'expedition' || $modulepart ==
'shipment') {
257 require_once DOL_DOCUMENT_ROOT .
'/expedition/class/expedition.class.php';
260 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
263 throw new RestException(404,
'Shipment not found');
266 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
267 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
270 throw new RestException(500,
'Error generating document missing doctemplate parameter');
273 throw new RestException(403,
'Generation not available for this modulepart');
276 $filename = basename($original_file);
277 $original_file_osencoded =
dol_osencode($original_file);
279 if (!file_exists($original_file_osencoded)) {
280 throw new RestException(404,
'File not found');
283 $file_content = file_get_contents($original_file_osencoded);
284 return array(
'filename'=>$filename,
'content-type' =>
dol_mimetype($filename),
'filesize'=>filesize($original_file),
'content'=>base64_encode($file_content),
'langcode'=>$outputlangs->defaultlang,
'template'=>$templateused,
'encoding'=>
'base64');
310 if (empty($modulepart)) {
311 throw new RestException(400,
'bad value for parameter modulepart');
314 if (empty($id) && empty($ref)) {
315 throw new RestException(400,
'bad value for parameter id or ref');
318 $id = (empty($id) ? 0 : $id);
322 if ($modulepart ==
'societe' || $modulepart ==
'thirdparty') {
323 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
325 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
326 throw new RestException(401);
329 $object =
new Societe($this->db);
330 $result = $object->fetch($id, $ref);
332 throw new RestException(404,
'Thirdparty not found');
335 $upload_dir = $conf->societe->multidir_output[$object->entity].
"/".$object->id;
336 } elseif ($modulepart ==
'user') {
337 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
340 if (!DolibarrApiAccess::$user->rights->user->user->lire && DolibarrApiAccess::$user->id != $id) {
341 throw new RestException(401);
344 $object =
new User($this->db);
345 $result = $object->fetch($id, $ref);
347 throw new RestException(404,
'User not found');
350 $upload_dir = $conf->user->dir_output.
'/'.
get_exdir(0, 0, 0, 0, $object,
'user').
'/'.$object->id;
351 } elseif ($modulepart ==
'adherent' || $modulepart ==
'member') {
352 require_once DOL_DOCUMENT_ROOT.
'/adherents/class/adherent.class.php';
354 if (!DolibarrApiAccess::$user->rights->adherent->lire) {
355 throw new RestException(401);
359 $result = $object->fetch($id, $ref);
361 throw new RestException(404,
'Member not found');
364 $upload_dir = $conf->adherent->dir_output.
"/".
get_exdir(0, 0, 0, 1, $object,
'member');
365 } elseif ($modulepart ==
'propal' || $modulepart ==
'proposal') {
366 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
368 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
369 throw new RestException(401);
372 $object =
new Propal($this->db);
373 $result = $object->fetch($id, $ref);
375 throw new RestException(404,
'Proposal not found');
378 $upload_dir = $conf->propal->multidir_output[$object->entity].
"/".
get_exdir(0, 0, 0, 1, $object,
'propal');
379 } elseif ($modulepart ==
'supplier_proposal') {
380 require_once DOL_DOCUMENT_ROOT.
'/supplier_proposal/class/supplier_proposal.class.php';
382 if (!DolibarrApiAccess::$user->rights->supplier_proposal->read) {
383 throw new RestException(401);
386 $object =
new Propal($this->db);
387 $result = $object->fetch($id, $ref);
389 throw new RestException(404,
'Supplier proposal not found');
392 $upload_dir = $conf->propal->multidir_output[$object->entity].
"/".
get_exdir(0, 0, 0, 1, $object,
'propal');
393 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
394 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
396 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
397 throw new RestException(401);
401 $result = $object->fetch($id, $ref);
403 throw new RestException(404,
'Order not found');
406 $upload_dir = $conf->commande->dir_output.
"/".
get_exdir(0, 0, 0, 1, $object,
'commande');
407 } elseif ($modulepart ==
'commande_fournisseur' || $modulepart ==
'supplier_order') {
408 $modulepart =
'supplier_order';
410 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.class.php';
412 if (empty(DolibarrApiAccess::$user->rights->fournisseur->commande->lire) && empty(DolibarrApiAccess::$user->rights->supplier_order->lire)) {
413 throw new RestException(401);
417 $result = $object->fetch($id, $ref);
419 throw new RestException(404,
'Purchase order not found');
423 } elseif ($modulepart ==
'shipment' || $modulepart ==
'expedition') {
424 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
426 if (!DolibarrApiAccess::$user->rights->expedition->lire) {
427 throw new RestException(401);
431 $result = $object->fetch($id, $ref);
433 throw new RestException(404,
'Shipment not found');
436 $upload_dir = $conf->expedition->dir_output.
"/sending/".
get_exdir(0, 0, 0, 1, $object,
'shipment');
437 } elseif ($modulepart ==
'facture' || $modulepart ==
'invoice') {
438 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
440 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
441 throw new RestException(401);
444 $object =
new Facture($this->db);
445 $result = $object->fetch($id, $ref);
447 throw new RestException(404,
'Invoice not found');
450 $upload_dir = $conf->facture->dir_output.
"/".
get_exdir(0, 0, 0, 1, $object,
'invoice');
451 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'supplier_invoice') {
452 $modulepart =
'supplier_invoice';
454 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
456 if (empty(DolibarrApiAccess::$user->rights->fournisseur->facture->lire) && empty(DolibarrApiAccess::$user->rights->supplier_invoice->lire)) {
457 throw new RestException(401);
461 $result = $object->fetch($id, $ref);
463 throw new RestException(404,
'Invoice not found');
466 $upload_dir = $conf->fournisseur->dir_output.
"/facture/".
get_exdir($object->id, 2, 0, 0, $object,
'invoice_supplier').dol_sanitizeFileName($object->ref);
467 } elseif ($modulepart ==
'produit' || $modulepart ==
'product') {
468 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
470 if (!DolibarrApiAccess::$user->rights->produit->lire) {
471 throw new RestException(401);
474 $object =
new Product($this->db);
475 $result = $object->fetch($id, $ref);
477 throw new RestException(404,
'Product not found');
478 } elseif ($result < 0) {
479 throw new RestException(500,
'Error while fetching object: '.$object->error);
482 $upload_dir = $conf->product->multidir_output[$object->entity].
'/'.
get_exdir(0, 0, 0, 1, $object,
'product');
483 } elseif ($modulepart ==
'agenda' || $modulepart ==
'action' || $modulepart ==
'event') {
484 require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
486 if (!DolibarrApiAccess::$user->rights->agenda->myactions->read && !DolibarrApiAccess::$user->rights->agenda->allactions->read) {
487 throw new RestException(401);
491 $result = $object->fetch($id, $ref);
493 throw new RestException(404,
'Event not found');
497 } elseif ($modulepart ==
'expensereport') {
498 require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport.class.php';
500 if (!DolibarrApiAccess::$user->rights->expensereport->read && !DolibarrApiAccess::$user->rights->expensereport->read) {
501 throw new RestException(401);
505 $result = $object->fetch($id, $ref);
507 throw new RestException(404,
'Expense report not found');
511 } elseif ($modulepart ==
'knowledgemanagement') {
512 require_once DOL_DOCUMENT_ROOT.
'/knowledgemanagement/class/knowledgerecord.class.php';
514 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read') && !DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read')) {
515 throw new RestException(401);
519 $result = $object->fetch($id, $ref);
521 throw new RestException(404,
'KM article not found');
524 $upload_dir = $conf->knowledgemanagement->dir_output.
'/knowledgerecord/'.
dol_sanitizeFileName($object->ref);
525 } elseif ($modulepart ==
'categorie' || $modulepart ==
'category') {
526 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
528 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
529 throw new RestException(401);
533 $result = $object->fetch($id, $ref);
535 throw new RestException(404,
'Category not found');
538 $upload_dir = $conf->categorie->multidir_output[$object->entity].
'/'.
get_exdir($object->id, 2, 0, 0, $object,
'category').$object->id.
"/photos/".
dol_sanitizeFileName($object->ref);
539 } elseif ($modulepart ==
'ecm') {
540 throw new RestException(500,
'Modulepart Ecm not implemented yet.');
555 } elseif ($modulepart ==
'contrat' || $modulepart ==
'contract') {
556 $modulepart =
'contrat';
557 require_once DOL_DOCUMENT_ROOT .
'/contrat/class/contrat.class.php';
559 $object =
new Contrat($this->db);
560 $result = $object->fetch($id, $ref);
562 throw new RestException(404,
'Contract not found');
565 $upload_dir = $conf->contrat->dir_output .
"/" .
get_exdir(0, 0, 0, 1, $object,
'contract');
566 } elseif ($modulepart ==
'projet' || $modulepart ==
'project') {
567 $modulepart =
'project';
568 require_once DOL_DOCUMENT_ROOT .
'/projet/class/project.class.php';
570 $object =
new Project($this->db);
571 $result = $object->fetch($id, $ref);
573 throw new RestException(404,
'Project not found');
576 $upload_dir = $conf->projet->dir_output .
"/" .
get_exdir(0, 0, 0, 1, $object,
'project');
578 throw new RestException(500,
'Modulepart '.$modulepart.
' not implemented yet.');
581 $objectType = $modulepart;
582 if (! empty($object->id) && ! empty($object->table_element)) {
583 $objectType = $object->table_element;
586 $filearray =
dol_dir_list($upload_dir, $type, $recursive,
'',
'(\.meta|_preview.*\.png)$', $sortfield, (strtolower($sortorder) ==
'desc' ? SORT_DESC : SORT_ASC), 1);
587 if (empty($filearray)) {
588 throw new RestException(404,
'Search for modulepart '.$modulepart.
' with Id '.$object->id.(!empty($object->ref) ?
' or Ref '.$object->ref :
'').
' does not return any document.');
590 if (($object->id) > 0 && !empty($modulepart)) {
591 require_once DOL_DOCUMENT_ROOT.
'/ecm/class/ecmfiles.class.php';
593 $result = $ecmfile->fetchAll(
'',
'', 0, 0, array(
't.src_object_type' => $objectType,
't.src_object_id' => $object->id));
595 throw new RestException(503,
'Error when retrieve ecm list : '.$this->db->lasterror());
596 } elseif (is_array($ecmfile->lines) && count($ecmfile->lines) > 0) {
597 $count = count($filearray);
598 for ($i = 0 ; $i < $count ; $i++) {
599 foreach ($ecmfile->lines as $line) {
600 if ($filearray[$i][
'name'] == $line->filename) {
601 $filearray[$i] = array_merge($filearray[$i], (array) $line);
653 public function post($filename, $modulepart, $ref =
'', $subdir =
'', $filecontent =
'', $fileencoding =
'', $overwriteifexists = 0, $createdirifnotexists = 1)
661 $modulepartorig = $modulepart;
663 if (empty($modulepart)) {
664 throw new RestException(400,
'Modulepart not provided.');
667 $newfilecontent =
'';
668 if (empty($fileencoding)) {
669 $newfilecontent = $filecontent;
671 if ($fileencoding ==
'base64') {
672 $newfilecontent = base64_decode($filecontent);
679 $entity = DolibarrApiAccess::$user->entity;
680 if (empty($entity)) {
688 if ($modulepart ==
'facture' || $modulepart ==
'invoice') {
689 $modulepart =
'facture';
691 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
692 $object =
new Facture($this->db);
693 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'supplier_invoice') {
694 $modulepart =
'supplier_invoice';
696 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
698 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
699 $modulepart =
'commande';
701 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
703 } elseif ($modulepart ==
'commande_fournisseur' || $modulepart ==
'supplier_order') {
704 $modulepart =
'supplier_order';
706 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.class.php';
708 } elseif ($modulepart ==
'projet' || $modulepart ==
'project') {
709 require_once DOL_DOCUMENT_ROOT.
'/projet/class/project.class.php';
710 $object =
new Project($this->db);
711 } elseif ($modulepart ==
'task' || $modulepart ==
'project_task') {
712 $modulepart =
'project_task';
714 require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
715 $object =
new Task($this->db);
717 $task_result = $object->fetch(
'', $ref);
720 if ($task_result > 0) {
721 $project_result = $object->fetch_projet();
723 if ($project_result >= 0) {
727 throw new RestException(500,
'Error while fetching Task '.$ref);
729 } elseif ($modulepart ==
'product' || $modulepart ==
'produit' || $modulepart ==
'service' || $modulepart ==
'produit|service') {
730 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
731 $object =
new Product($this->db);
732 } elseif ($modulepart ==
'expensereport') {
733 require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport.class.php';
735 } elseif ($modulepart ==
'fichinter') {
736 require_once DOL_DOCUMENT_ROOT.
'/fichinter/class/fichinter.class.php';
738 } elseif ($modulepart ==
'adherent' || $modulepart ==
'member') {
739 $modulepart =
'adherent';
740 require_once DOL_DOCUMENT_ROOT.
'/adherents/class/adherent.class.php';
742 } elseif ($modulepart ==
'proposal' || $modulepart ==
'propal' || $modulepart ==
'propale') {
743 $modulepart =
'propale';
744 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
745 $object =
new Propal($this->db);
746 } elseif ($modulepart ==
'agenda' || $modulepart ==
'action' || $modulepart ==
'event') {
747 $modulepart =
'agenda';
748 require_once DOL_DOCUMENT_ROOT .
'/comm/action/class/actioncomm.class.php';
750 } elseif ($modulepart ==
'contact' || $modulepart ==
'socpeople') {
751 $modulepart =
'contact';
752 require_once DOL_DOCUMENT_ROOT.
'/contact/class/contact.class.php';
753 $object =
new Contact($this->db);
755 } elseif ($modulepart ==
'contrat' || $modulepart ==
'contract') {
756 $modulepart =
'contrat';
757 require_once DOL_DOCUMENT_ROOT .
'/contrat/class/contrat.class.php';
758 $object =
new Contrat($this->db);
761 throw new RestException(500,
'Modulepart '.$modulepart.
' not implemented yet.');
764 if (is_object($object)) {
766 $result = $object->fetch($ref);
768 $result = $object->fetch(
'', $ref);
772 throw new RestException(404,
"Object with ref '".$ref.
"' was not found.");
773 } elseif ($result < 0) {
774 throw new RestException(500,
'Error while fetching object: '.$object->error);
778 if (!($object->id > 0)) {
779 throw new RestException(404,
'The object '.$modulepart.
" with ref '".$ref.
"' was not found.");
784 if ($modulepart ==
'supplier_invoice') {
785 $tmpreldir =
get_exdir($object->id, 2, 0, 0, $object,
'invoice_supplier');
789 if ($modulepart !=
'ecm') {
790 $relativefile = $tmpreldir.dol_sanitizeFileName($object->ref);
792 $upload_dir = $tmp[
'original_file'];
794 if (!DolibarrApiAccess::$user->hasRight(
'ecm',
'upload')) {
795 throw new RestException(401,
'Missing permission to upload files in ECM module');
797 $upload_dir = $conf->medias->multidir_output[$conf->entity];
800 if (empty($upload_dir) || $upload_dir ==
'/') {
801 throw new RestException(500,
'This value of modulepart ('.$modulepart.
') does not support yet usage of ref. Check modulepart parameter or try to use subdir parameter instead of ref.');
804 if ($modulepart ==
'invoice') {
805 $modulepart =
'facture';
807 if ($modulepart ==
'member') {
808 $modulepart =
'adherent';
812 if ($modulepart !=
'ecm') {
813 $relativefile = $subdir;
815 $upload_dir = $tmp[
'original_file'];
817 if (!DolibarrApiAccess::$user->hasRight(
'ecm',
'upload')) {
818 throw new RestException(401,
'Missing permission to upload files in ECM module');
820 $upload_dir = $conf->medias->multidir_output[$conf->entity];
823 if (empty($upload_dir) || $upload_dir ==
'/') {
824 if (!empty($tmp[
'error'])) {
825 throw new RestException(401,
'Error returned by dol_check_secure_access_document: '.$tmp[
'error']);
827 throw new RestException(500,
'This value of modulepart ('.$modulepart.
') is not allowed with this value of subdir ('.$relativefile.
')');
835 if (!empty($createdirifnotexists)) {
837 throw new RestException(500,
'Error while trying to create directory '.$upload_dir);
841 $destfile = $upload_dir.
'/'.$original_file;
842 $destfiletmp = DOL_DATA_ROOT.
'/admin/temp/'.$original_file;
847 throw new RestException(401,
'Directory not exists : '.dirname($destfile));
850 if (!$overwriteifexists &&
dol_is_file($destfile)) {
851 throw new RestException(500,
"File with name '".$original_file.
"' already exists.");
859 $fhandle = @fopen($destfiletmp,
'w');
861 $nbofbyteswrote = fwrite($fhandle, $newfilecontent);
865 throw new RestException(500,
"Failed to open file '".$destfiletmp.
"' for write");
868 $disablevirusscan = 0;
869 $src_file = $destfiletmp;
870 $dest_file = $destfile;
874 if (empty($disablevirusscan) && file_exists($src_file)) {
876 if (count($checkvirusarray)) {
877 dol_syslog(
'Files.lib::dol_move_uploaded_file File "'.$src_file.
'" (target name "'.$dest_file.
'") KO with antivirus: errors='.join(
',', $checkvirusarray), LOG_WARNING);
878 throw new RestException(500,
'ErrorFileIsInfectedWithAVirus: '.join(
',', $checkvirusarray));
887 $publicmediasdirwithslash = $conf->medias->multidir_output[$conf->entity];
888 if (!preg_match(
'/\/$/', $publicmediasdirwithslash)) {
889 $publicmediasdirwithslash .=
'/';
892 if (strpos($upload_dir, $publicmediasdirwithslash) !== 0 || !
getDolGlobalInt(
"MAIN_DOCUMENT_DISABLE_NOEXE_IN_MEDIAS_DIR")) {
893 $dest_file .=
'.noexe';
899 if (preg_match(
'/^\./', basename($src_file)) || preg_match(
'/\.\./', $src_file) || preg_match(
'/[<>|]/', $src_file)) {
900 dol_syslog(
"Refused to deliver file ".$src_file, LOG_WARNING);
901 throw new RestException(500,
"Refused to deliver file ".$src_file);
906 if (preg_match(
'/^\./', basename($dest_file)) || preg_match(
'/\.\./', $dest_file) || preg_match(
'/[<>|]/', $dest_file)) {
907 dol_syslog(
"Refused to deliver file ".$dest_file, LOG_WARNING);
908 throw new RestException(500,
"Refused to deliver file ".$dest_file);
911 $moreinfo = array(
'note_private' =>
'File uploaded using API /documents from IP '.
getUserRemoteIP());
912 if (!empty($object) && is_object($object) && $object->id > 0) {
913 $moreinfo[
'src_object_type'] = $object->table_element;
914 $moreinfo[
'src_object_id'] = $object->id;
918 $result =
dol_move($destfiletmp, $dest_file, 0, $overwriteifexists, 1, 1, $moreinfo);
920 throw new RestException(500,
"Failed to move file into '".$dest_file.
"'");