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');
225 if ($modulepart ==
'facture' || $modulepart ==
'invoice') {
226 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
227 $tmpobject =
new Facture($this->db);
228 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
230 throw new RestException(404,
'Invoice not found');
233 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
234 $result = $tmpobject->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 = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
243 throw new RestException(404,
'Supplier invoice not found');
246 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
247 $result = $tmpobject->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 $tmpobject =
new Commande($this->db);
254 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
256 throw new RestException(404,
'Order not found');
258 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
259 $result = $tmpobject->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 $tmpobject =
new Propal($this->db);
266 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
268 throw new RestException(404,
'Proposal not found');
270 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
271 $result = $tmpobject->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 $tmpobject =
new Contrat($this->db);
279 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
282 throw new RestException(404,
'Contract not found');
285 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
286 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
289 throw new RestException(500,
'Error generating document missing doctemplate parameter');
291 } elseif ($modulepart ==
'expedition' || $modulepart ==
'shipment') {
292 require_once DOL_DOCUMENT_ROOT .
'/expedition/class/expedition.class.php';
295 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
298 throw new RestException(404,
'Shipment not found');
301 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
302 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
305 throw new RestException(500,
'Error generating document missing doctemplate parameter');
308 throw new RestException(403,
'Generation not available for this modulepart');
311 $filename = basename($original_file);
312 $original_file_osencoded =
dol_osencode($original_file);
314 if (!file_exists($original_file_osencoded)) {
315 throw new RestException(404,
'File not found');
318 $file_content = file_get_contents($original_file_osencoded);
319 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');
345 if (empty($modulepart)) {
346 throw new RestException(400,
'bad value for parameter modulepart');
349 if (empty($id) && empty($ref)) {
350 throw new RestException(400,
'bad value for parameter id or ref');
353 $id = (empty($id) ? 0 : $id);
357 if ($modulepart ==
'societe' || $modulepart ==
'thirdparty') {
358 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
360 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
361 throw new RestException(401);
364 $object =
new Societe($this->db);
365 $result = $object->fetch($id, $ref);
367 throw new RestException(404,
'Thirdparty not found');
370 $upload_dir = $conf->societe->multidir_output[$object->entity].
"/".$object->id;
371 } elseif ($modulepart ==
'user') {
372 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
375 if (!DolibarrApiAccess::$user->rights->user->user->lire && DolibarrApiAccess::$user->id != $id) {
376 throw new RestException(401);
379 $object =
new User($this->db);
380 $result = $object->fetch($id, $ref);
382 throw new RestException(404,
'User not found');
385 $upload_dir = $conf->user->dir_output.
'/'.
get_exdir(0, 0, 0, 0, $object,
'user').
'/'.$object->id;
386 } elseif ($modulepart ==
'adherent' || $modulepart ==
'member') {
387 require_once DOL_DOCUMENT_ROOT.
'/adherents/class/adherent.class.php';
389 if (!DolibarrApiAccess::$user->rights->adherent->lire) {
390 throw new RestException(401);
394 $result = $object->fetch($id, $ref);
396 throw new RestException(404,
'Member not found');
399 $upload_dir = $conf->adherent->dir_output.
"/".
get_exdir(0, 0, 0, 1, $object,
'member');
400 } elseif ($modulepart ==
'propal' || $modulepart ==
'proposal') {
401 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
403 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
404 throw new RestException(401);
407 $object =
new Propal($this->db);
408 $result = $object->fetch($id, $ref);
410 throw new RestException(404,
'Proposal not found');
413 $upload_dir = $conf->propal->multidir_output[$object->entity].
"/".
get_exdir(0, 0, 0, 1, $object,
'propal');
414 } elseif ($modulepart ==
'supplier_proposal') {
415 require_once DOL_DOCUMENT_ROOT.
'/supplier_proposal/class/supplier_proposal.class.php';
417 if (!DolibarrApiAccess::$user->rights->supplier_proposal->read) {
418 throw new RestException(401);
421 $object =
new Propal($this->db);
422 $result = $object->fetch($id, $ref);
424 throw new RestException(404,
'Supplier proposal not found');
427 $upload_dir = $conf->propal->multidir_output[$object->entity].
"/".
get_exdir(0, 0, 0, 1, $object,
'propal');
428 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
429 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
431 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
432 throw new RestException(401);
436 $result = $object->fetch($id, $ref);
438 throw new RestException(404,
'Order not found');
441 $upload_dir = $conf->commande->dir_output.
"/".
get_exdir(0, 0, 0, 1, $object,
'commande');
442 } elseif ($modulepart ==
'commande_fournisseur' || $modulepart ==
'supplier_order') {
443 $modulepart =
'supplier_order';
445 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.class.php';
447 if (empty(DolibarrApiAccess::$user->rights->fournisseur->commande->lire) && empty(DolibarrApiAccess::$user->rights->supplier_order->lire)) {
448 throw new RestException(401);
452 $result = $object->fetch($id, $ref);
454 throw new RestException(404,
'Purchase order not found');
458 } elseif ($modulepart ==
'shipment' || $modulepart ==
'expedition') {
459 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
461 if (!DolibarrApiAccess::$user->rights->expedition->lire) {
462 throw new RestException(401);
466 $result = $object->fetch($id, $ref);
468 throw new RestException(404,
'Shipment not found');
471 $upload_dir = $conf->expedition->dir_output.
"/sending/".
get_exdir(0, 0, 0, 1, $object,
'shipment');
472 } elseif ($modulepart ==
'facture' || $modulepart ==
'invoice') {
473 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
475 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
476 throw new RestException(401);
479 $object =
new Facture($this->db);
480 $result = $object->fetch($id, $ref);
482 throw new RestException(404,
'Invoice not found');
485 $upload_dir = $conf->facture->dir_output.
"/".
get_exdir(0, 0, 0, 1, $object,
'invoice');
486 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'supplier_invoice') {
487 $modulepart =
'supplier_invoice';
489 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
491 if (empty(DolibarrApiAccess::$user->rights->fournisseur->facture->lire) && empty(DolibarrApiAccess::$user->rights->supplier_invoice->lire)) {
492 throw new RestException(401);
496 $result = $object->fetch($id, $ref);
498 throw new RestException(404,
'Invoice not found');
501 $upload_dir = $conf->fournisseur->dir_output.
"/facture/".
get_exdir($object->id, 2, 0, 0, $object,
'invoice_supplier').dol_sanitizeFileName($object->ref);
502 } elseif ($modulepart ==
'produit' || $modulepart ==
'product') {
503 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
505 if (!DolibarrApiAccess::$user->rights->produit->lire) {
506 throw new RestException(401);
509 $object =
new Product($this->db);
510 $result = $object->fetch($id, $ref);
512 throw new RestException(404,
'Product not found');
513 } elseif ($result < 0) {
514 throw new RestException(500,
'Error while fetching object: '.$object->error);
517 $upload_dir = $conf->product->multidir_output[$object->entity].
'/'.
get_exdir(0, 0, 0, 1, $object,
'product');
518 } elseif ($modulepart ==
'agenda' || $modulepart ==
'action' || $modulepart ==
'event') {
519 require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
521 if (!DolibarrApiAccess::$user->rights->agenda->myactions->read && !DolibarrApiAccess::$user->rights->agenda->allactions->read) {
522 throw new RestException(401);
526 $result = $object->fetch($id, $ref);
528 throw new RestException(404,
'Event not found');
532 } elseif ($modulepart ==
'expensereport') {
533 require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport.class.php';
535 if (!DolibarrApiAccess::$user->rights->expensereport->lire) {
536 throw new RestException(401);
540 $result = $object->fetch($id, $ref);
542 throw new RestException(404,
'Expense report not found');
546 } elseif ($modulepart ==
'knowledgemanagement') {
547 require_once DOL_DOCUMENT_ROOT.
'/knowledgemanagement/class/knowledgerecord.class.php';
549 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read') && !DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read')) {
550 throw new RestException(401);
554 $result = $object->fetch($id, $ref);
556 throw new RestException(404,
'KM article not found');
559 $upload_dir = $conf->knowledgemanagement->dir_output.
'/knowledgerecord/'.
dol_sanitizeFileName($object->ref);
560 } elseif ($modulepart ==
'categorie' || $modulepart ==
'category') {
561 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
563 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
564 throw new RestException(401);
568 $result = $object->fetch($id, $ref);
570 throw new RestException(404,
'Category not found');
573 $upload_dir = $conf->categorie->multidir_output[$object->entity].
'/'.
get_exdir($object->id, 2, 0, 0, $object,
'category').$object->id.
"/photos/".
dol_sanitizeFileName($object->ref);
574 } elseif ($modulepart ==
'ecm') {
575 throw new RestException(500,
'Modulepart Ecm not implemented yet.');
590 } elseif ($modulepart ==
'contrat' || $modulepart ==
'contract') {
591 $modulepart =
'contrat';
592 require_once DOL_DOCUMENT_ROOT .
'/contrat/class/contrat.class.php';
594 $object =
new Contrat($this->db);
595 $result = $object->fetch($id, $ref);
597 throw new RestException(404,
'Contract not found');
600 $upload_dir = $conf->contrat->dir_output .
"/" .
get_exdir(0, 0, 0, 1, $object,
'contract');
601 } elseif ($modulepart ==
'projet' || $modulepart ==
'project') {
602 $modulepart =
'project';
603 require_once DOL_DOCUMENT_ROOT .
'/projet/class/project.class.php';
605 $object =
new Project($this->db);
606 $result = $object->fetch($id, $ref);
608 throw new RestException(404,
'Project not found');
611 $upload_dir = $conf->projet->dir_output .
"/" .
get_exdir(0, 0, 0, 1, $object,
'project');
613 throw new RestException(500,
'Modulepart '.$modulepart.
' not implemented yet.');
616 $objectType = $modulepart;
617 if (! empty($object->id) && ! empty($object->table_element)) {
618 $objectType = $object->table_element;
621 $filearray =
dol_dir_list($upload_dir, $type, $recursive,
'',
'(\.meta|_preview.*\.png)$', $sortfield, (strtolower($sortorder) ==
'desc' ? SORT_DESC : SORT_ASC), 1);
622 if (empty($filearray)) {
623 throw new RestException(404,
'Search for modulepart '.$modulepart.
' with Id '.$object->id.(!empty($object->ref) ?
' or Ref '.$object->ref :
'').
' does not return any document.');
625 if (($object->id) > 0 && !empty($modulepart)) {
626 require_once DOL_DOCUMENT_ROOT.
'/ecm/class/ecmfiles.class.php';
628 $result = $ecmfile->fetchAll(
'',
'', 0, 0, array(
't.src_object_type' => $objectType,
't.src_object_id' => $object->id));
630 throw new RestException(503,
'Error when retrieve ecm list : '.$this->db->lasterror());
631 } elseif (is_array($ecmfile->lines) && count($ecmfile->lines) > 0) {
632 $count = count($filearray);
633 for ($i = 0 ; $i < $count ; $i++) {
634 foreach ($ecmfile->lines as $line) {
635 if ($filearray[$i][
'name'] == $line->filename) {
636 $filearray[$i] = array_merge($filearray[$i], (array) $line);
688 public function post($filename, $modulepart, $ref =
'', $subdir =
'', $filecontent =
'', $fileencoding =
'', $overwriteifexists = 0, $createdirifnotexists = 1)
696 $modulepartorig = $modulepart;
698 if (empty($modulepart)) {
699 throw new RestException(400,
'Modulepart not provided.');
702 $newfilecontent =
'';
703 if (empty($fileencoding)) {
704 $newfilecontent = $filecontent;
706 if ($fileencoding ==
'base64') {
707 $newfilecontent = base64_decode($filecontent);
714 $entity = DolibarrApiAccess::$user->entity;
715 if (empty($entity)) {
723 if ($modulepart ==
'facture' || $modulepart ==
'invoice') {
724 $modulepart =
'facture';
726 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
727 $object =
new Facture($this->db);
728 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'supplier_invoice') {
729 $modulepart =
'supplier_invoice';
731 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
733 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
734 $modulepart =
'commande';
736 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
738 } elseif ($modulepart ==
'commande_fournisseur' || $modulepart ==
'supplier_order') {
739 $modulepart =
'supplier_order';
741 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.class.php';
743 } elseif ($modulepart ==
'projet' || $modulepart ==
'project') {
744 require_once DOL_DOCUMENT_ROOT.
'/projet/class/project.class.php';
745 $object =
new Project($this->db);
746 } elseif ($modulepart ==
'task' || $modulepart ==
'project_task') {
747 $modulepart =
'project_task';
749 require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
750 $object =
new Task($this->db);
752 $task_result = $object->fetch(
'', $ref);
755 if ($task_result > 0) {
756 $project_result = $object->fetch_projet();
758 if ($project_result >= 0) {
762 throw new RestException(500,
'Error while fetching Task '.$ref);
764 } elseif ($modulepart ==
'product' || $modulepart ==
'produit' || $modulepart ==
'service' || $modulepart ==
'produit|service') {
765 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
766 $object =
new Product($this->db);
767 } elseif ($modulepart ==
'expensereport') {
768 require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport.class.php';
770 } elseif ($modulepart ==
'fichinter') {
771 require_once DOL_DOCUMENT_ROOT.
'/fichinter/class/fichinter.class.php';
773 } elseif ($modulepart ==
'adherent' || $modulepart ==
'member') {
774 $modulepart =
'adherent';
775 require_once DOL_DOCUMENT_ROOT.
'/adherents/class/adherent.class.php';
777 } elseif ($modulepart ==
'proposal' || $modulepart ==
'propal' || $modulepart ==
'propale') {
778 $modulepart =
'propale';
779 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
780 $object =
new Propal($this->db);
781 } elseif ($modulepart ==
'agenda' || $modulepart ==
'action' || $modulepart ==
'event') {
782 $modulepart =
'agenda';
783 require_once DOL_DOCUMENT_ROOT .
'/comm/action/class/actioncomm.class.php';
785 } elseif ($modulepart ==
'contact' || $modulepart ==
'socpeople') {
786 $modulepart =
'contact';
787 require_once DOL_DOCUMENT_ROOT.
'/contact/class/contact.class.php';
788 $object =
new Contact($this->db);
790 } elseif ($modulepart ==
'contrat' || $modulepart ==
'contract') {
791 $modulepart =
'contrat';
792 require_once DOL_DOCUMENT_ROOT .
'/contrat/class/contrat.class.php';
793 $object =
new Contrat($this->db);
796 throw new RestException(500,
'Modulepart '.$modulepart.
' not implemented yet.');
799 if (is_object($object)) {
801 $result = $object->fetch($ref);
803 $result = $object->fetch(
'', $ref);
807 throw new RestException(404,
"Object with ref '".$ref.
"' was not found.");
808 } elseif ($result < 0) {
809 throw new RestException(500,
'Error while fetching object: '.$object->error);
813 if (!($object->id > 0)) {
814 throw new RestException(404,
'The object '.$modulepart.
" with ref '".$ref.
"' was not found.");
819 if ($modulepart ==
'supplier_invoice') {
820 $tmpreldir =
get_exdir($object->id, 2, 0, 0, $object,
'invoice_supplier');
824 if ($modulepart !=
'ecm') {
825 $relativefile = $tmpreldir.dol_sanitizeFileName($object->ref);
827 $upload_dir = $tmp[
'original_file'];
829 if (!DolibarrApiAccess::$user->hasRight(
'ecm',
'upload')) {
830 throw new RestException(401,
'Missing permission to upload files in ECM module');
832 $upload_dir = $conf->medias->multidir_output[$conf->entity];
835 if (empty($upload_dir) || $upload_dir ==
'/') {
836 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.');
839 if ($modulepart ==
'invoice') {
840 $modulepart =
'facture';
842 if ($modulepart ==
'member') {
843 $modulepart =
'adherent';
847 if ($modulepart !=
'ecm') {
848 $relativefile = $subdir;
850 $upload_dir = $tmp[
'original_file'];
852 if (!DolibarrApiAccess::$user->hasRight(
'ecm',
'upload')) {
853 throw new RestException(401,
'Missing permission to upload files in ECM module');
855 $upload_dir = $conf->medias->multidir_output[$conf->entity];
858 if (empty($upload_dir) || $upload_dir ==
'/') {
859 if (!empty($tmp[
'error'])) {
860 throw new RestException(401,
'Error returned by dol_check_secure_access_document: '.$tmp[
'error']);
862 throw new RestException(500,
'This value of modulepart ('.$modulepart.
') is not allowed with this value of subdir ('.$relativefile.
')');
870 if (!empty($createdirifnotexists)) {
872 throw new RestException(500,
'Error while trying to create directory '.$upload_dir);
876 $destfile = $upload_dir.
'/'.$original_file;
877 $destfiletmp = DOL_DATA_ROOT.
'/admin/temp/'.$original_file;
882 throw new RestException(401,
'Directory not exists : '.dirname($destfile));
885 if (!$overwriteifexists &&
dol_is_file($destfile)) {
886 throw new RestException(500,
"File with name '".$original_file.
"' already exists.");
894 $fhandle = @fopen($destfiletmp,
'w');
896 $nbofbyteswrote = fwrite($fhandle, $newfilecontent);
900 throw new RestException(500,
"Failed to open file '".$destfiletmp.
"' for write");
903 $disablevirusscan = 0;
904 $src_file = $destfiletmp;
905 $dest_file = $destfile;
909 if (empty($disablevirusscan) && file_exists($src_file)) {
911 if (count($checkvirusarray)) {
912 dol_syslog(
'Files.lib::dol_move_uploaded_file File "'.$src_file.
'" (target name "'.$dest_file.
'") KO with antivirus: errors='.join(
',', $checkvirusarray), LOG_WARNING);
913 throw new RestException(500,
'ErrorFileIsInfectedWithAVirus: '.join(
',', $checkvirusarray));
922 $publicmediasdirwithslash = $conf->medias->multidir_output[$conf->entity];
923 if (!preg_match(
'/\/$/', $publicmediasdirwithslash)) {
924 $publicmediasdirwithslash .=
'/';
927 if (strpos($upload_dir, $publicmediasdirwithslash) !== 0 || !
getDolGlobalInt(
"MAIN_DOCUMENT_DISABLE_NOEXE_IN_MEDIAS_DIR")) {
928 $dest_file .=
'.noexe';
934 if (preg_match(
'/^\./', basename($src_file)) || preg_match(
'/\.\./', $src_file) || preg_match(
'/[<>|]/', $src_file)) {
935 dol_syslog(
"Refused to deliver file ".$src_file, LOG_WARNING);
936 throw new RestException(500,
"Refused to deliver file ".$src_file);
941 if (preg_match(
'/^\./', basename($dest_file)) || preg_match(
'/\.\./', $dest_file) || preg_match(
'/[<>|]/', $dest_file)) {
942 dol_syslog(
"Refused to deliver file ".$dest_file, LOG_WARNING);
943 throw new RestException(500,
"Refused to deliver file ".$dest_file);
946 $moreinfo = array(
'note_private' =>
'File uploaded using API /documents from IP '.
getUserRemoteIP());
947 if (!empty($object) && is_object($object) && $object->id > 0) {
948 $moreinfo[
'src_object_type'] = $object->table_element;
949 $moreinfo[
'src_object_id'] = $object->id;
953 $result =
dol_move($destfiletmp, $dest_file, 0, $overwriteifexists, 1, 1, $moreinfo);
955 throw new RestException(500,
"Failed to move file into '".$dest_file.
"'");
974 public function delete($modulepart, $original_file)
976 global $conf, $langs;
978 if (empty($modulepart)) {
979 throw new RestException(400,
'bad value for parameter modulepart');
981 if (empty($original_file)) {
982 throw new RestException(400,
'bad value for parameter original_file');
986 $entity = $conf->entity;
997 $relativefile = $original_file;
1000 $accessallowed = $check_access[
'accessallowed'];
1001 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
1002 $original_file = $check_access[
'original_file'];
1004 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
1005 throw new RestException(401);
1007 if (!$accessallowed) {
1008 throw new RestException(401);
1011 if (DolibarrApiAccess::$user->socid > 0) {
1012 if ($sqlprotectagainstexternals) {
1013 $resql = $this->db->query($sqlprotectagainstexternals);
1015 $num = $this->db->num_rows($resql);
1018 $obj = $this->db->fetch_object($resql);
1019 if (DolibarrApiAccess::$user->socid != $obj->fk_soc) {
1020 throw new RestException(403,
'Not allowed to download documents with such a ref');
1028 $filename = basename($original_file);
1029 $original_file_osencoded =
dol_osencode($original_file);
1031 if (!file_exists($original_file_osencoded)) {
1032 dol_syslog(
"Try to download not found file ".$original_file_osencoded, LOG_WARNING);
1033 throw new RestException(404,
'File not found');
1036 if (@unlink($original_file_osencoded)) {
1040 'message' =>
'Document deleted'
1045 throw new RestException(401);