70 public function index($modulepart, $original_file =
'')
74 if (empty($modulepart)) {
75 throw new RestException(400,
'bad value for parameter modulepart');
77 if (empty($original_file)) {
78 throw new RestException(400,
'bad value for parameter original_file');
82 $entity = $conf->entity;
93 $relativefile = $original_file;
96 $accessallowed = $check_access[
'accessallowed'];
97 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
98 $original_file = $check_access[
'original_file'];
100 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
101 throw new RestException(401);
103 if (!$accessallowed) {
104 throw new RestException(401);
107 if (DolibarrApiAccess::$user->socid > 0) {
108 if ($sqlprotectagainstexternals) {
109 $resql = $this->db->query($sqlprotectagainstexternals);
111 $num = $this->db->num_rows($resql);
114 $obj = $this->db->fetch_object($resql);
115 if (DolibarrApiAccess::$user->socid != $obj->fk_soc) {
116 throw new RestException(403,
'Not allowed to download documents with such a ref');
124 $filename = basename($original_file);
125 $original_file_osencoded =
dol_osencode($original_file);
127 if (!file_exists($original_file_osencoded)) {
128 dol_syslog(
"Try to download not found file ".$original_file_osencoded, LOG_WARNING);
129 throw new RestException(404,
'File not found');
132 $file_content = file_get_contents($original_file_osencoded);
133 return array(
'filename'=>$filename,
'content-type' =>
dol_mimetype($filename),
'filesize'=>filesize($original_file),
'content'=>base64_encode($file_content),
'encoding'=>
'base64');
158 public function builddoc($modulepart, $original_file =
'', $doctemplate =
'', $langcode =
'')
160 global $conf, $langs;
162 if (empty($modulepart)) {
163 throw new RestException(400,
'bad value for parameter modulepart');
165 if (empty($original_file)) {
166 throw new RestException(400,
'bad value for parameter original_file');
169 $outputlangs = $langs;
170 if ($langcode && $langs->defaultlang != $langcode) {
172 $outputlangs->setDefaultLang($langcode);
176 $entity = $conf->entity;
187 $relativefile = $original_file;
190 $accessallowed = $check_access[
'accessallowed'];
191 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
192 $original_file = $check_access[
'original_file'];
194 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
195 throw new RestException(401);
197 if (!$accessallowed) {
198 throw new RestException(401);
201 if (DolibarrApiAccess::$user->socid > 0) {
202 if ($sqlprotectagainstexternals) {
203 $resql = $this->db->query($sqlprotectagainstexternals);
205 $num = $this->db->num_rows($resql);
208 $obj = $this->db->fetch_object($resql);
209 if (DolibarrApiAccess::$user->socid != $obj->fk_soc) {
210 throw new RestException(403,
'Not allowed to download documents with such a ref');
219 $hidedetails = empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS) ? 0 : 1;
220 $hidedesc = empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DESC) ? 0 : 1;
221 $hideref = empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_REF) ? 0 : 1;
225 if ($modulepart ==
'facture' || $modulepart ==
'invoice') {
226 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
227 $this->invoice =
new Facture($this->db);
228 $result = $this->invoice->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
230 throw new RestException(404,
'Invoice not found');
233 $templateused = $doctemplate ? $doctemplate : $this->invoice->model_pdf;
234 $result = $this->invoice->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
236 throw new RestException(500,
'Error generating document');
238 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'invoice_supplier') {
239 require_once DOL_DOCUMENT_ROOT .
'/fourn/class/fournisseur.facture.class.php';
241 $result = $this->supplier_invoice->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
243 throw new RestException(404,
'Supplier invoice not found');
246 $templateused = $doctemplate ? $doctemplate : $this->supplier_invoice->model_pdf;
247 $result = $this->supplier_invoice->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
249 throw new RestException(500,
'Error generating document');
251 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
252 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
253 $this->order =
new Commande($this->db);
254 $result = $this->order->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
256 throw new RestException(404,
'Order not found');
258 $templateused = $doctemplate ? $doctemplate : $this->order->model_pdf;
259 $result = $this->order->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
261 throw new RestException(500,
'Error generating document');
263 } elseif ($modulepart ==
'propal' || $modulepart ==
'proposal') {
264 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
265 $this->propal =
new Propal($this->db);
266 $result = $this->propal->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
268 throw new RestException(404,
'Proposal not found');
270 $templateused = $doctemplate ? $doctemplate : $this->propal->model_pdf;
271 $result = $this->propal->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
273 throw new RestException(500,
'Error generating document');
275 } elseif ($modulepart ==
'contrat' || $modulepart ==
'contract') {
276 require_once DOL_DOCUMENT_ROOT .
'/contrat/class/contrat.class.php';
278 $this->contract =
new Contrat($this->db);
279 $result = $this->contract->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
282 throw new RestException(404,
'Contract not found');
285 $templateused = $doctemplate ? $doctemplate : $this->contract->model_pdf;
286 $result = $this->contract->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
289 throw new RestException(500,
'Error generating document missing doctemplate parameter');
292 throw new RestException(403,
'Generation not available for this modulepart');
295 $filename = basename($original_file);
296 $original_file_osencoded =
dol_osencode($original_file);
298 if (!file_exists($original_file_osencoded)) {
299 throw new RestException(404,
'File not found');
302 $file_content = file_get_contents($original_file_osencoded);
303 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');
329 if (empty($modulepart)) {
330 throw new RestException(400,
'bad value for parameter modulepart');
333 if (empty($id) && empty($ref)) {
334 throw new RestException(400,
'bad value for parameter id or ref');
337 $id = (empty($id) ? 0 : $id);
341 if ($modulepart ==
'societe' || $modulepart ==
'thirdparty') {
342 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
344 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
345 throw new RestException(401);
348 $object =
new Societe($this->db);
349 $result = $object->fetch($id, $ref);
351 throw new RestException(404,
'Thirdparty not found');
354 $upload_dir = $conf->societe->multidir_output[$object->entity].
"/".$object->id;
355 } elseif ($modulepart ==
'user') {
356 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
359 if (!DolibarrApiAccess::$user->rights->user->user->lire && DolibarrApiAccess::$user->id != $id) {
360 throw new RestException(401);
363 $object =
new User($this->db);
364 $result = $object->fetch($id, $ref);
366 throw new RestException(404,
'User not found');
369 $upload_dir = $conf->user->dir_output.
'/'.
get_exdir(0, 0, 0, 0, $object,
'user').
'/'.$object->id;
370 } elseif ($modulepart ==
'adherent' || $modulepart ==
'member') {
371 require_once DOL_DOCUMENT_ROOT.
'/adherents/class/adherent.class.php';
373 if (!DolibarrApiAccess::$user->rights->adherent->lire) {
374 throw new RestException(401);
378 $result = $object->fetch($id, $ref);
380 throw new RestException(404,
'Member not found');
383 $upload_dir = $conf->adherent->dir_output.
"/".
get_exdir(0, 0, 0, 1, $object,
'member');
384 } elseif ($modulepart ==
'propal' || $modulepart ==
'proposal') {
385 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
387 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
388 throw new RestException(401);
391 $object =
new Propal($this->db);
392 $result = $object->fetch($id, $ref);
394 throw new RestException(404,
'Proposal not found');
397 $upload_dir = $conf->propal->multidir_output[$object->entity].
"/".
get_exdir(0, 0, 0, 1, $object,
'propal');
398 } elseif ($modulepart ==
'supplier_proposal') {
399 require_once DOL_DOCUMENT_ROOT.
'/supplier_proposal/class/supplier_proposal.class.php';
401 if (!DolibarrApiAccess::$user->rights->supplier_proposal->read) {
402 throw new RestException(401);
405 $object =
new Propal($this->db);
406 $result = $object->fetch($id, $ref);
408 throw new RestException(404,
'Supplier proposal not found');
411 $upload_dir = $conf->propal->multidir_output[$object->entity].
"/".
get_exdir(0, 0, 0, 1, $object,
'propal');
412 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
413 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
415 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
416 throw new RestException(401);
420 $result = $object->fetch($id, $ref);
422 throw new RestException(404,
'Order not found');
425 $upload_dir = $conf->commande->dir_output.
"/".
get_exdir(0, 0, 0, 1, $object,
'commande');
426 } elseif ($modulepart ==
'commande_fournisseur' || $modulepart ==
'supplier_order') {
427 $modulepart =
'supplier_order';
429 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.class.php';
431 if (empty(DolibarrApiAccess::$user->rights->fournisseur->commande->lire) && empty(DolibarrApiAccess::$user->rights->supplier_order->lire)) {
432 throw new RestException(401);
436 $result = $object->fetch($id, $ref);
438 throw new RestException(404,
'Purchase order not found');
442 } elseif ($modulepart ==
'shipment' || $modulepart ==
'expedition') {
443 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
445 if (!DolibarrApiAccess::$user->rights->expedition->lire) {
446 throw new RestException(401);
450 $result = $object->fetch($id, $ref);
452 throw new RestException(404,
'Shipment not found');
455 $upload_dir = $conf->expedition->dir_output.
"/sending/".
get_exdir(0, 0, 0, 1, $object,
'shipment');
456 } elseif ($modulepart ==
'facture' || $modulepart ==
'invoice') {
457 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
459 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
460 throw new RestException(401);
463 $object =
new Facture($this->db);
464 $result = $object->fetch($id, $ref);
466 throw new RestException(404,
'Invoice not found');
469 $upload_dir = $conf->facture->dir_output.
"/".
get_exdir(0, 0, 0, 1, $object,
'invoice');
470 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'supplier_invoice') {
471 $modulepart =
'supplier_invoice';
473 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
475 if (empty(DolibarrApiAccess::$user->rights->fournisseur->facture->lire) && empty(DolibarrApiAccess::$user->rights->supplier_invoice->lire)) {
476 throw new RestException(401);
480 $result = $object->fetch($id, $ref);
482 throw new RestException(404,
'Invoice not found');
485 $upload_dir = $conf->fournisseur->dir_output.
"/facture/".
get_exdir($object->id, 2, 0, 0, $object,
'invoice_supplier').dol_sanitizeFileName($object->ref);
486 } elseif ($modulepart ==
'produit' || $modulepart ==
'product') {
487 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
489 if (!DolibarrApiAccess::$user->rights->produit->lire) {
490 throw new RestException(401);
493 $object =
new Product($this->db);
494 $result = $object->fetch($id, $ref);
496 throw new RestException(404,
'Product not found');
497 } elseif ($result < 0) {
498 throw new RestException(500,
'Error while fetching object: '.$object->error);
501 $upload_dir = $conf->product->multidir_output[$object->entity].
'/'.
get_exdir(0, 0, 0, 1, $object,
'product');
502 } elseif ($modulepart ==
'agenda' || $modulepart ==
'action' || $modulepart ==
'event') {
503 require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
505 if (!DolibarrApiAccess::$user->rights->agenda->myactions->read && !DolibarrApiAccess::$user->rights->agenda->allactions->read) {
506 throw new RestException(401);
510 $result = $object->fetch($id, $ref);
512 throw new RestException(404,
'Event not found');
516 } elseif ($modulepart ==
'expensereport') {
517 require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport.class.php';
519 if (!DolibarrApiAccess::$user->rights->expensereport->lire) {
520 throw new RestException(401);
524 $result = $object->fetch($id, $ref);
526 throw new RestException(404,
'Expense report not found');
530 } elseif ($modulepart ==
'knowledgemanagement') {
531 require_once DOL_DOCUMENT_ROOT.
'/knowledgemanagement/class/knowledgerecord.class.php';
533 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read') && !DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read')) {
534 throw new RestException(401);
538 $result = $object->fetch($id, $ref);
540 throw new RestException(404,
'KM article not found');
543 $upload_dir = $conf->knowledgemanagement->dir_output.
'/knowledgerecord/'.
dol_sanitizeFileName($object->ref);
544 } elseif ($modulepart ==
'categorie' || $modulepart ==
'category') {
545 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
547 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
548 throw new RestException(401);
552 $result = $object->fetch($id, $ref);
554 throw new RestException(404,
'Category not found');
557 $upload_dir = $conf->categorie->multidir_output[$object->entity].
'/'.
get_exdir($object->id, 2, 0, 0, $object,
'category').$object->id.
"/photos/".
dol_sanitizeFileName($object->ref);
558 } elseif ($modulepart ==
'ecm') {
559 throw new RestException(500,
'Modulepart Ecm not implemented yet.');
574 } elseif ($modulepart ==
'contrat' || $modulepart ==
'contract') {
575 $modulepart =
'contrat';
576 require_once DOL_DOCUMENT_ROOT .
'/contrat/class/contrat.class.php';
578 $object =
new Contrat($this->db);
579 $result = $object->fetch($id, $ref);
581 throw new RestException(404,
'Contract not found');
584 $upload_dir = $conf->contrat->dir_output .
"/" .
get_exdir(0, 0, 0, 1, $object,
'contract');
585 } elseif ($modulepart ==
'projet' || $modulepart ==
'project') {
586 $modulepart =
'project';
587 require_once DOL_DOCUMENT_ROOT .
'/projet/class/project.class.php';
589 $object =
new Project($this->db);
590 $result = $object->fetch($id, $ref);
592 throw new RestException(404,
'Project not found');
595 $upload_dir = $conf->projet->dir_output .
"/" .
get_exdir(0, 0, 0, 1, $object,
'project');
597 throw new RestException(500,
'Modulepart '.$modulepart.
' not implemented yet.');
600 $objectType = $modulepart;
601 if (! empty($object->id) && ! empty($object->table_element)) {
602 $objectType = $object->table_element;
605 $filearray =
dol_dir_list($upload_dir, $type, $recursive,
'',
'(\.meta|_preview.*\.png)$', $sortfield, (strtolower($sortorder) ==
'desc' ?SORT_DESC:SORT_ASC), 1);
606 if (empty($filearray)) {
607 throw new RestException(404,
'Search for modulepart '.$modulepart.
' with Id '.$object->id.(!empty($object->ref) ?
' or Ref '.$object->ref :
'').
' does not return any document.');
609 if (($object->id) > 0 && !empty($modulepart)) {
610 require_once DOL_DOCUMENT_ROOT.
'/ecm/class/ecmfiles.class.php';
612 $result = $ecmfile->fetchAll(
'',
'', 0, 0, array(
't.src_object_type' => $objectType,
't.src_object_id' => $object->id));
614 throw new RestException(503,
'Error when retrieve ecm list : '.$this->db->lasterror());
615 } elseif (is_array($ecmfile->lines) && count($ecmfile->lines) > 0) {
616 $count = count($filearray);
617 for ($i = 0 ; $i < $count ; $i++) {
618 foreach ($ecmfile->lines as $line) {
619 if ($filearray[$i][
'name'] == $line->filename) {
620 $filearray[$i] = array_merge($filearray[$i], (array) $line);
672 public function post($filename, $modulepart, $ref =
'', $subdir =
'', $filecontent =
'', $fileencoding =
'', $overwriteifexists = 0, $createdirifnotexists = 1)
680 $modulepartorig = $modulepart;
682 if (empty($modulepart)) {
683 throw new RestException(400,
'Modulepart not provided.');
686 if (!DolibarrApiAccess::$user->rights->ecm->upload) {
687 throw new RestException(401);
690 $newfilecontent =
'';
691 if (empty($fileencoding)) {
692 $newfilecontent = $filecontent;
694 if ($fileencoding ==
'base64') {
695 $newfilecontent = base64_decode($filecontent);
702 $entity = DolibarrApiAccess::$user->entity;
703 if (empty($entity)) {
711 if ($modulepart ==
'facture' || $modulepart ==
'invoice') {
712 $modulepart =
'facture';
714 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
715 $object =
new Facture($this->db);
716 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'supplier_invoice') {
717 $modulepart =
'supplier_invoice';
719 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
721 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
722 $modulepart =
'commande';
724 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
726 } elseif ($modulepart ==
'commande_fournisseur' || $modulepart ==
'supplier_order') {
727 $modulepart =
'supplier_order';
729 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.class.php';
731 } elseif ($modulepart ==
'projet' || $modulepart ==
'project') {
732 require_once DOL_DOCUMENT_ROOT.
'/projet/class/project.class.php';
733 $object =
new Project($this->db);
734 } elseif ($modulepart ==
'task' || $modulepart ==
'project_task') {
735 $modulepart =
'project_task';
737 require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
738 $object =
new Task($this->db);
740 $task_result = $object->fetch(
'', $ref);
743 if ($task_result > 0) {
744 $project_result = $object->fetch_projet();
746 if ($project_result >= 0) {
750 throw new RestException(500,
'Error while fetching Task '.$ref);
752 } elseif ($modulepart ==
'product' || $modulepart ==
'produit' || $modulepart ==
'service' || $modulepart ==
'produit|service') {
753 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
754 $object =
new Product($this->db);
755 } elseif ($modulepart ==
'expensereport') {
756 require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport.class.php';
758 } elseif ($modulepart ==
'fichinter') {
759 require_once DOL_DOCUMENT_ROOT.
'/fichinter/class/fichinter.class.php';
761 } elseif ($modulepart ==
'adherent' || $modulepart ==
'member') {
762 $modulepart =
'adherent';
763 require_once DOL_DOCUMENT_ROOT.
'/adherents/class/adherent.class.php';
765 } elseif ($modulepart ==
'proposal' || $modulepart ==
'propal' || $modulepart ==
'propale') {
766 $modulepart =
'propale';
767 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
768 $object =
new Propal($this->db);
769 } elseif ($modulepart ==
'agenda' || $modulepart ==
'action' || $modulepart ==
'event') {
770 $modulepart =
'agenda';
771 require_once DOL_DOCUMENT_ROOT .
'/comm/action/class/actioncomm.class.php';
773 } elseif ($modulepart ==
'contact' || $modulepart ==
'socpeople') {
774 $modulepart =
'contact';
775 require_once DOL_DOCUMENT_ROOT.
'/contact/class/contact.class.php';
776 $object =
new Contact($this->db);
778 } elseif ($modulepart ==
'contrat' || $modulepart ==
'contract') {
779 $modulepart =
'contrat';
780 require_once DOL_DOCUMENT_ROOT .
'/contrat/class/contrat.class.php';
781 $object =
new Contrat($this->db);
784 throw new RestException(500,
'Modulepart '.$modulepart.
' not implemented yet.');
787 if (is_object($object)) {
789 $result = $object->fetch($ref);
791 $result = $object->fetch(
'', $ref);
795 throw new RestException(404,
"Object with ref '".$ref.
"' was not found.");
796 } elseif ($result < 0) {
797 throw new RestException(500,
'Error while fetching object: '.$object->error);
801 if (!($object->id > 0)) {
802 throw new RestException(404,
'The object '.$modulepart.
" with ref '".$ref.
"' was not found.");
807 if ($modulepart ==
'supplier_invoice') {
808 $tmpreldir =
get_exdir($object->id, 2, 0, 0, $object,
'invoice_supplier');
811 $relativefile = $tmpreldir.dol_sanitizeFileName($object->ref);
814 $upload_dir = $tmp[
'original_file'];
816 if (empty($upload_dir) || $upload_dir ==
'/') {
817 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.');
820 if ($modulepart ==
'invoice') {
821 $modulepart =
'facture';
823 if ($modulepart ==
'member') {
824 $modulepart =
'adherent';
827 $relativefile = $subdir;
829 $upload_dir = $tmp[
'original_file'];
831 if (empty($upload_dir) || $upload_dir ==
'/') {
832 if (!empty($tmp[
'error'])) {
833 throw new RestException(401,
'Error returned by dol_check_secure_access_document: '.$tmp[
'error']);
835 throw new RestException(500,
'This value of modulepart ('.$modulepart.
') is not allowed with this value of subdir ('.$relativefile.
')');
843 if (!empty($createdirifnotexists)) {
845 throw new RestException(500,
'Error while trying to create directory '.$upload_dir);
849 $destfile = $upload_dir.
'/'.$original_file;
850 $destfiletmp = DOL_DATA_ROOT.
'/admin/temp/'.$original_file;
855 throw new RestException(401,
'Directory not exists : '.dirname($destfile));
858 if (!$overwriteifexists &&
dol_is_file($destfile)) {
859 throw new RestException(500,
"File with name '".$original_file.
"' already exists.");
867 $fhandle = @fopen($destfiletmp,
'w');
869 $nbofbyteswrote = fwrite($fhandle, $newfilecontent);
873 throw new RestException(500,
"Failed to open file '".$destfiletmp.
"' for write");
876 $disablevirusscan = 0;
877 $src_file = $destfiletmp;
878 $dest_file = $destfile;
882 if (empty($disablevirusscan) && file_exists($src_file)) {
884 if (count($checkvirusarray)) {
885 dol_syslog(
'Files.lib::dol_move_uploaded_file File "'.$src_file.
'" (target name "'.$dest_file.
'") KO with antivirus: errors='.join(
',', $checkvirusarray), LOG_WARNING);
886 throw new RestException(500,
'ErrorFileIsInfectedWithAVirus: '.join(
',', $checkvirusarray));
895 $publicmediasdirwithslash = $conf->medias->multidir_output[$conf->entity];
896 if (!preg_match(
'/\/$/', $publicmediasdirwithslash)) {
897 $publicmediasdirwithslash .=
'/';
900 if (strpos($upload_dir, $publicmediasdirwithslash) !== 0 || !
getDolGlobalInt(
"MAIN_DOCUMENT_DISABLE_NOEXE_IN_MEDIAS_DIR")) {
901 $dest_file .=
'.noexe';
907 if (preg_match(
'/^\./', basename($src_file)) || preg_match(
'/\.\./', $src_file) || preg_match(
'/[<>|]/', $src_file)) {
908 dol_syslog(
"Refused to deliver file ".$src_file, LOG_WARNING);
909 throw new RestException(500,
"Refused to deliver file ".$src_file);
914 if (preg_match(
'/^\./', basename($dest_file)) || preg_match(
'/\.\./', $dest_file) || preg_match(
'/[<>|]/', $dest_file)) {
915 dol_syslog(
"Refused to deliver file ".$dest_file, LOG_WARNING);
916 throw new RestException(500,
"Refused to deliver file ".$dest_file);
919 $moreinfo = array(
'note_private' =>
'File uploaded using API /documents from IP '.
getUserRemoteIP());
920 if (!empty($object) && is_object($object) && $object->id > 0) {
921 $moreinfo[
'src_object_type'] = $object->table_element;
922 $moreinfo[
'src_object_id'] = $object->id;
926 $result =
dol_move($destfiletmp, $dest_file, 0, $overwriteifexists, 1, 1, $moreinfo);
928 throw new RestException(500,
"Failed to move file into '".$dest_file.
"'");
947 public function delete($modulepart, $original_file)
949 global $conf, $langs;
951 if (empty($modulepart)) {
952 throw new RestException(400,
'bad value for parameter modulepart');
954 if (empty($original_file)) {
955 throw new RestException(400,
'bad value for parameter original_file');
959 $entity = $conf->entity;
970 $relativefile = $original_file;
973 $accessallowed = $check_access[
'accessallowed'];
974 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
975 $original_file = $check_access[
'original_file'];
977 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
978 throw new RestException(401);
980 if (!$accessallowed) {
981 throw new RestException(401);
984 if (DolibarrApiAccess::$user->socid > 0) {
985 if ($sqlprotectagainstexternals) {
986 $resql = $this->db->query($sqlprotectagainstexternals);
988 $num = $this->db->num_rows($resql);
991 $obj = $this->db->fetch_object($resql);
992 if (DolibarrApiAccess::$user->socid != $obj->fk_soc) {
993 throw new RestException(403,
'Not allowed to download documents with such a ref');
1001 $filename = basename($original_file);
1002 $original_file_osencoded =
dol_osencode($original_file);
1004 if (!file_exists($original_file_osencoded)) {
1005 dol_syslog(
"Try to download not found file ".$original_file_osencoded, LOG_WARNING);
1006 throw new RestException(404,
'File not found');
1009 if (@unlink($original_file_osencoded)) {
1013 'message' =>
'Document deleted'
1018 throw new RestException(401);