72 public function index($modulepart, $original_file =
'')
76 if (empty($modulepart)) {
77 throw new RestException(400,
'bad value for parameter modulepart');
79 if (empty($original_file)) {
80 throw new RestException(400,
'bad value for parameter original_file');
84 if ($modulepart ==
'task' || $modulepart ==
'project_task') {
85 $modulepart =
'project_task';
89 $entity =
$conf->entity;
100 $relativefile = $original_file;
103 $accessallowed = $check_access[
'accessallowed'];
104 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
105 $original_file = $check_access[
'original_file'];
107 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
108 throw new RestException(403);
110 if (!$accessallowed) {
111 throw new RestException(403);
114 if (DolibarrApiAccess::$user->socid > 0) {
115 if ($sqlprotectagainstexternals) {
116 $resql = $this->db->query($sqlprotectagainstexternals);
118 $num = $this->db->num_rows($resql);
121 $obj = $this->db->fetch_object($resql);
122 if (DolibarrApiAccess::$user->socid != $obj->fk_soc) {
123 throw new RestException(403,
'Not allowed to download documents with such a ref');
131 $filename = basename($original_file);
132 $original_file_osencoded =
dol_osencode($original_file);
134 if (!file_exists($original_file_osencoded)) {
135 dol_syslog(
"Try to download not found file ".$original_file_osencoded, LOG_WARNING);
136 throw new RestException(404,
'File not found');
139 $file_content = file_get_contents($original_file_osencoded);
140 return array(
'filename' => $filename,
'content-type' =>
dol_mimetype($filename),
'filesize' => filesize($original_file),
'content' => base64_encode($file_content),
'encoding' =>
'base64');
174 public function builddoc($modulepart, $original_file =
'', $doctemplate =
'', $langcode =
'')
176 global
$conf, $langs;
178 if (empty($modulepart)) {
179 throw new RestException(400,
'bad value for parameter modulepart');
181 if (empty($original_file)) {
182 throw new RestException(400,
'bad value for parameter original_file');
185 $outputlangs = $langs;
186 if ($langcode && $langs->defaultlang != $langcode) {
188 $outputlangs->setDefaultLang($langcode);
192 $entity =
$conf->entity;
203 $relativefile = $original_file;
206 $accessallowed = $check_access[
'accessallowed'];
207 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
208 $original_file = $check_access[
'original_file'];
210 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
211 throw new RestException(403);
213 if (!$accessallowed) {
214 throw new RestException(403);
217 if (DolibarrApiAccess::$user->socid > 0) {
218 if ($sqlprotectagainstexternals) {
219 $resql = $this->db->query($sqlprotectagainstexternals);
221 $num = $this->db->num_rows($resql);
224 $obj = $this->db->fetch_object($resql);
225 if (DolibarrApiAccess::$user->socid != $obj->fk_soc) {
226 throw new RestException(403,
'Not allowed to download documents with such a ref');
241 if ($modulepart ==
'facture' || $modulepart ==
'invoice') {
242 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
243 $tmpobject =
new Facture($this->db);
244 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
246 throw new RestException(404,
'Invoice not found');
249 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
250 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
252 throw new RestException(500,
'Error generating document');
254 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'invoice_supplier') {
255 require_once DOL_DOCUMENT_ROOT .
'/fourn/class/fournisseur.facture.class.php';
257 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
259 throw new RestException(404,
'Supplier invoice not found');
262 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
263 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
265 throw new RestException(500,
'Error generating document');
267 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
268 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
269 $tmpobject =
new Commande($this->db);
270 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
272 throw new RestException(404,
'Order not found');
274 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
275 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
277 throw new RestException(500,
'Error generating document');
279 } elseif ($modulepart ==
'propal' || $modulepart ==
'proposal') {
280 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
281 $tmpobject =
new Propal($this->db);
282 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
284 throw new RestException(404,
'Proposal not found');
286 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
287 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
289 throw new RestException(500,
'Error generating document');
291 } elseif ($modulepart ==
'contrat' || $modulepart ==
'contract') {
292 require_once DOL_DOCUMENT_ROOT .
'/contrat/class/contrat.class.php';
294 $tmpobject =
new Contrat($this->db);
295 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
298 throw new RestException(404,
'Contract 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');
307 } elseif ($modulepart ==
'expedition' || $modulepart ==
'shipment') {
308 require_once DOL_DOCUMENT_ROOT .
'/expedition/class/expedition.class.php';
311 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
314 throw new RestException(404,
'Shipment not found');
317 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
318 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
321 throw new RestException(500,
'Error generating document');
323 } elseif ($modulepart ==
'mrp') {
324 require_once DOL_DOCUMENT_ROOT .
'/mrp/class/mo.class.php';
326 $tmpobject =
new Mo($this->db);
327 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
330 throw new RestException(404,
'MO not found');
333 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
334 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
337 throw new RestException(500,
'Error generating document');
339 } elseif ($modulepart ==
'expensereport') {
340 require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport.class.php';
343 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
346 throw new RestException(404,
'Expense report not found');
349 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
350 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
353 throw new RestException(500,
'Error generating document');
355 } elseif ($modulepart ==
'holiday') {
356 require_once DOL_DOCUMENT_ROOT.
'/holiday/class/holiday.class.php';
358 $tmpobject =
new Holiday($this->db);
359 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
362 throw new RestException(404,
'Holiday not found');
365 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
366 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
369 throw new RestException(500,
'Error generating document');
371 } elseif ($modulepart ==
'product') {
372 require_once DOL_DOCUMENT_ROOT .
'/product/class/product.class.php';
374 $tmpobject =
new Product($this->db);
375 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
378 throw new RestException(404,
'Product not found');
381 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
382 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
385 throw new RestException(500,
'Error generating document');
387 } elseif ($modulepart ==
'stock' || $modulepart ==
'entrepot') {
388 require_once DOL_DOCUMENT_ROOT .
'/product/stock/class/entrepot.class.php';
390 $tmpobject =
new Entrepot($this->db);
391 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
394 throw new RestException(404,
'Warehouse not found');
397 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
398 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
401 throw new RestException(500,
'Error generating document');
403 } elseif ($modulepart ==
'fichinter' || $modulepart ==
'intervention') {
404 require_once DOL_DOCUMENT_ROOT .
'/fichinter/class/fichinter.class.php';
407 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
410 throw new RestException(404,
'Intervention not found');
413 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
414 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
417 throw new RestException(500,
'Error generating document');
420 throw new RestException(403,
'Generation not available for this modulepart');
423 $filename = basename($original_file);
424 $original_file_osencoded =
dol_osencode($original_file);
426 if (!file_exists($original_file_osencoded)) {
427 throw new RestException(404,
'File not found');
430 $file_content = file_get_contents($original_file_osencoded);
431 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');
463 public function getDocumentsListByElement($modulepart,
$id = 0, $ref =
'', $sortfield =
'', $sortorder =
'', $limit = 100, $page = 0, $content_type =
'', $pagination_data =
false)
465 if (empty($modulepart)) {
466 throw new RestException(400,
'bad value for parameter modulepart');
469 if (empty(
$id) && empty($ref)) {
470 throw new RestException(400,
'bad value for parameter id or ref');
477 $object = fetchObjectByElement(
$id, $modulepart, $ref);
479 throw new RestException(404,
'Object with (id, ref) = ('.
$id.
', '.$ref.
') not found or not allowed for modulepart = '.$modulepart);
488 throw new RestException(403,
'Access not allowed to this object (refused by checkUserAccessToObject)');
492 if ($modulepart ==
'societe' || $modulepart ==
'thirdparty') {
493 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
494 throw new RestException(403);
496 } elseif ($modulepart ==
'user') {
498 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'lire') && DolibarrApiAccess::$user->
id !=
$id) {
499 throw new RestException(403);
501 } elseif ($modulepart ==
'adherent' || $modulepart ==
'member') {
502 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
503 throw new RestException(403);
505 } elseif ($modulepart ==
'propal' || $modulepart ==
'proposal') {
506 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
507 throw new RestException(403);
509 } elseif ($modulepart ==
'supplier_proposal') {
510 if (!DolibarrApiAccess::$user->hasRight(
'supplier_proposal',
'read')) {
511 throw new RestException(403);
513 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
514 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
515 throw new RestException(403);
517 } elseif ($modulepart ==
'commande_fournisseur' || $modulepart ==
'supplier_order') {
518 $modulepart =
'supplier_order';
519 if (!DolibarrApiAccess::$user->hasRight(
'fournisseur',
'commande',
'lire') && !DolibarrApiAccess::$user->hasRight(
'supplier_order',
'lire')) {
520 throw new RestException(403);
522 } elseif ($modulepart ==
'shipment' || $modulepart ==
'expedition') {
523 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'lire')) {
524 throw new RestException(403);
526 } elseif ($modulepart ==
'facture' || $modulepart ==
'invoice') {
527 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
528 throw new RestException(403);
530 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'supplier_invoice') {
531 $modulepart =
'supplier_invoice';
532 if (!DolibarrApiAccess::$user->hasRight(
'fournisseur',
'facture',
'lire') && !DolibarrApiAccess::$user->hasRight(
'supplier_invoice',
'lire')) {
533 throw new RestException(403);
535 } elseif ($modulepart ==
'produit' || $modulepart ==
'product' || $modulepart ==
'service') {
536 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'lire')) {
537 throw new RestException(403);
539 } elseif ($modulepart ==
'agenda' || $modulepart ==
'action' || $modulepart ==
'event' || $modulepart ==
'actioncomm') {
540 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'myactions',
'read') && !DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'read')) {
541 throw new RestException(403);
543 } elseif ($modulepart ==
'expensereport') {
544 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'read')) {
545 throw new RestException(403);
547 } elseif ($modulepart ==
'holiday') {
548 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'read')) {
549 throw new RestException(403);
551 } elseif ($modulepart ==
'ticket') {
552 if (!DolibarrApiAccess::$user->hasRight(
'ticket',
'read')) {
553 throw new RestException(403);
555 } elseif ($modulepart ==
'knowledgemanagement') {
556 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read')) {
557 throw new RestException(403);
559 } elseif ($modulepart ==
'categorie' || $modulepart ==
'category') {
560 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
561 throw new RestException(403);
563 } elseif ($modulepart ==
'ecm') {
564 throw new RestException(500,
'Modulepart Ecm not implemented yet.');
568 } elseif ($modulepart ==
'contrat' || $modulepart ==
'contract') {
569 $modulepart =
'contrat';
570 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'lire')) {
571 throw new RestException(403);
573 } elseif ($modulepart ==
'intervention' || $modulepart ==
'ficheinter') {
574 $modulepart =
'ficheinter';
575 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'lire')) {
576 throw new RestException(403);
578 } elseif ($modulepart ==
'projet' || $modulepart ==
'project') {
579 $modulepart =
'project';
580 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
581 throw new RestException(403);
583 } elseif ($modulepart ==
'task' || $modulepart ==
'project_task') {
584 $modulepart =
'project_task';
585 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
586 throw new RestException(403);
588 } elseif ($modulepart ==
'mrp') {
590 if (!DolibarrApiAccess::$user->hasRight(
'mrp',
'read')) {
591 throw new RestException(403);
593 } elseif ($modulepart ==
'contact' || $modulepart ==
'socpeople') {
594 $modulepart =
'contact';
595 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'lire')) {
596 throw new RestException(403);
598 } elseif ($modulepart ==
'stock') {
599 if (!DolibarrApiAccess::$user->hasRight(
'stock',
'lire')) {
600 throw new RestException(403);
603 throw new RestException(500,
'Modulepart '.$modulepart.
' not implemented yet.');
610 $objectType = $modulepart;
612 $objectType =
$object->table_element;
615 $filearray =
dol_dir_list($upload_dir, $type, $recursive,
'',
'(\.meta|_preview.*\.png)$', $sortfield, (strtolower($sortorder) ==
'desc' ? SORT_DESC : SORT_ASC), 1);
617 $countarray = is_array($filearray) ? count($filearray) : 0;
619 if (empty($filearray)) {
620 throw new RestException(404,
'Search for modulepart '.$modulepart.
' with Id '.
$object->id.(!empty(
$object->ref) ?
' or Ref '.$object->ref :
'').
' does not return any document.');
622 $filearray = array_slice($filearray, $limit * $page, $limit);
623 if ((
$object->id) > 0 && !empty($modulepart)) {
624 require_once DOL_DOCUMENT_ROOT.
'/ecm/class/ecmfiles.class.php';
626 $result = $ecmfile->fetchAll(
'',
'', 0, 0, array(
't.src_object_type' => $objectType,
't.src_object_id' =>
$object->id));
628 throw new RestException(503,
'Error when retrieve ecm list : '.$this->db->lasterror());
629 } elseif (is_array($ecmfile->lines) && count($ecmfile->lines) > 0) {
630 foreach ($filearray as &$fileitem) {
631 foreach ($ecmfile->lines as $line) {
632 if ($fileitem[
'name'] == $line->filename) {
635 $fileitem[
'ref'] = $line->ref;
636 $fileitem[
'label'] = $line->label;
637 $fileitem[
'filepath'] = $line->filepath;
638 $fileitem[
'filename'] = $line->filename;
639 $fileitem[
'fullpath_orig'] = $line->fullpath_orig;
640 $fileitem[
'position'] = $line->position;
641 $fileitem[
'gen_or_uploaded'] = $line->gen_or_uploaded;
642 $fileitem[
'description'] = $line->desc;
643 $fileitem[
'keywords'] = $line->keywords;
644 $fileitem[
'cover'] = $line->cover;
645 $fileitem[
'share'] = $line->share;
646 $fileitem[
'date_c'] = $line->date_c;
647 $fileitem[
'agenda_id'] = $line->agenda_id;
648 $fileitem[
'fk_user_c'] = $line->fk_user_c;
649 $fileitem[
'fk_user_m'] = $line->fk_user_m;
650 $fileitem[
'note_private'] = $line->note_private;
651 $fileitem[
'note_public'] = $line->note_public;
654 if (isset($fileitem[
'relativename'])) {
655 $fileitem[
'content-type'] =
dol_mimetype((
string) $fileitem[
'relativename']);
660 $arraycontenttype = explode(
",", $content_type);
661 if (!empty($arraycontenttype)) {
662 $filearray = array_filter(
668 static function ($fileitem) use (&$arraycontenttype) {
669 return in_array(($fileitem[
'content-type'] ?:
'UNKNOWN'), $arraycontenttype);
678 if ($pagination_data) {
680 'data' => $filearray,
682 'total' => (int) $countarray,
684 'page_count' => (
int) ceil((
int) $countarray / $limit),
743 public function post($filename, $modulepart, $ref =
'', $subdir =
'', $filecontent =
'', $fileencoding =
'', $overwriteifexists = 0, $createdirifnotexists = 1, $position = 0, $cover =
'', $array_options = [], $generateThumbs = 0)
747 $modulepartorig = $modulepart;
749 if (empty($modulepart)) {
750 throw new RestException(400,
'Modulepart not provided.');
753 $newfilecontent =
'';
754 if (empty($fileencoding)) {
755 $newfilecontent = $filecontent;
757 if ($fileencoding ==
'base64') {
758 $newfilecontent = base64_decode($filecontent);
762 $relativefile =
'UNSET';
766 $entity = DolibarrApiAccess::$user->entity;
767 if (empty($entity)) {
775 if ($modulepart ==
'facture' || $modulepart ==
'invoice') {
776 $modulepart =
'facture';
778 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
780 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'supplier_invoice') {
781 $modulepart =
'supplier_invoice';
783 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
785 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
786 $modulepart =
'commande';
788 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
790 } elseif ($modulepart ==
'commande_fournisseur' || $modulepart ==
'supplier_order') {
791 $modulepart =
'supplier_order';
793 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.class.php';
795 } elseif ($modulepart ==
'projet' || $modulepart ==
'project') {
796 require_once DOL_DOCUMENT_ROOT.
'/projet/class/project.class.php';
798 } elseif ($modulepart ==
'task' || $modulepart ==
'project_task') {
799 $modulepart =
'project_task';
801 require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
804 $task_result =
$object->fetch(0, $ref);
807 if ($task_result > 0) {
808 $project_result =
$object->fetchProject();
810 if ($project_result >= 0) {
814 throw new RestException(500,
'Error while fetching Task '.$ref);
816 } elseif ($modulepart ==
'product' || $modulepart ==
'produit' || $modulepart ==
'service' || $modulepart ==
'produit|service') {
817 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
819 } elseif ($modulepart ==
'expensereport') {
820 require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport.class.php';
822 } elseif ($modulepart ==
'holiday') {
823 require_once DOL_DOCUMENT_ROOT.
'/holiday/class/holiday.class.php';
825 } elseif ($modulepart ==
'ficheinter' || $modulepart ==
'intervention') {
826 require_once DOL_DOCUMENT_ROOT.
'/fichinter/class/fichinter.class.php';
828 } elseif ($modulepart ==
'shipment' || $modulepart ==
'expedition') {
829 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
831 } elseif ($modulepart ==
'adherent' || $modulepart ==
'member') {
832 $modulepart =
'adherent';
833 require_once DOL_DOCUMENT_ROOT.
'/adherents/class/adherent.class.php';
835 } elseif ($modulepart ==
'proposal' || $modulepart ==
'propal' || $modulepart ==
'propale') {
836 $modulepart =
'propale';
837 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
839 } elseif ($modulepart ==
'agenda' || $modulepart ==
'action' || $modulepart ==
'event') {
840 $modulepart =
'agenda';
841 require_once DOL_DOCUMENT_ROOT .
'/comm/action/class/actioncomm.class.php';
843 } elseif ($modulepart ==
'contact' || $modulepart ==
'socpeople') {
844 $modulepart =
'contact';
845 require_once DOL_DOCUMENT_ROOT.
'/contact/class/contact.class.php';
848 } elseif ($modulepart ==
'societe' || $modulepart ==
'company') {
849 $modulepart =
'societe';
850 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
853 } elseif ($modulepart ==
'knowledgemanagement') {
854 $modulepart =
'knowledgemanagement';
855 require_once DOL_DOCUMENT_ROOT.
'/knowledgemanagement/class/knowledgerecord.class.php';
858 } elseif ($modulepart ==
'ticket') {
859 $modulepart =
'ticket';
860 require_once DOL_DOCUMENT_ROOT.
'/ticket/class/ticket.class.php';
863 } elseif ($modulepart ==
'contrat' || $modulepart ==
'contract') {
864 $modulepart =
'contrat';
865 require_once DOL_DOCUMENT_ROOT .
'/contrat/class/contrat.class.php';
867 } elseif ($modulepart ==
'mrp') {
869 require_once DOL_DOCUMENT_ROOT .
'/mrp/class/mo.class.php';
871 } elseif ($modulepart ==
'stock') {
872 $modulepart =
'stock';
873 require_once DOL_DOCUMENT_ROOT .
'/product/stock/class/entrepot.class.php';
875 } elseif ($modulepart ==
'ecm') {
876 throw new RestException(500,
'Using a non empty "ref" is not compatible with using modulepart = '.$modulepart);
879 throw new RestException(500,
'Modulepart '.$modulepart.
' not implemented yet.');
885 $result =
$object->fetch((
int) $ref);
887 $result =
$object->fetch(0, $ref);
891 throw new RestException(404,
"Object with ref '".$ref.
"' was not found.");
892 } elseif ($result < 0) {
893 throw new RestException(500,
'Error while fetching object: '.
$object->error);
897 throw new RestException(404,
'The object '.$modulepart.
" with ref '".$ref.
"' was not found.");
902 if ($modulepart ==
'supplier_invoice') {
908 if ($modulepart ==
'societe') {
909 $relativefile = $tmpreldir.dol_sanitizeFileName((
string)
$object->id);
911 $relativefile = $tmpreldir.dol_sanitizeFileName((
string)
$object->ref);
914 if (empty($tmp[
'accessallowed'])) {
915 throw new RestException(403,
'Access not allowed to upload file into this directory');
917 $upload_dir = $tmp[
'original_file'];
925 if (empty($upload_dir) || $upload_dir ==
'/') {
926 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.');
929 if ($modulepart ==
'invoice') {
930 $modulepart =
'facture';
932 if ($modulepart ==
'member') {
933 $modulepart =
'adherent';
937 if ($modulepart !=
'ecm') {
938 $relativefile = $subdir;
940 if (empty($tmp[
'accessallowed'])) {
941 throw new RestException(403,
'Access not allowed to upload file into this directory');
943 $upload_dir = $tmp[
'original_file'];
945 if (!DolibarrApiAccess::$user->hasRight(
'ecm',
'upload')) {
946 throw new RestException(403,
'Missing permission to upload files in ECM module');
948 $upload_dir =
$conf->medias->multidir_output[
$conf->entity];
951 if (empty($upload_dir) || $upload_dir ==
'/') {
952 if (!empty($tmp[
'error'])) {
953 throw new RestException(403,
'Error returned by dol_check_secure_access_document: '.$tmp[
'error']);
955 throw new RestException(400,
'This value of modulepart ('.$modulepart.
') is not allowed with this value of subdir ('.$relativefile.
')');
963 if (!empty($createdirifnotexists)) {
965 throw new RestException(500,
'Error while trying to create directory '.$upload_dir);
969 $destfile = $upload_dir.
'/'.$original_file;
970 $destfiletmp = DOL_DATA_ROOT.
'/admin/temp/'.$original_file;
975 throw new RestException(400,
'Directory does not exists : '.dirname($destfile));
978 if (!$overwriteifexists &&
dol_is_file($destfile)) {
979 throw new RestException(400,
"File with name '".$original_file.
"' already exists.");
987 $fhandle = @fopen($destfiletmp,
'w');
989 $nbofbyteswrote = fwrite($fhandle, $newfilecontent);
993 throw new RestException(500,
"Failed to open file '".$destfiletmp.
"' for write");
996 $disablevirusscan = 0;
997 $src_file = $destfiletmp;
998 $dest_file = $destfile;
1002 if (empty($disablevirusscan) && file_exists($src_file)) {
1004 if (count($checkvirusarray)) {
1005 dol_syslog(
'Files.lib::dol_move_uploaded_file File "'.$src_file.
'" (target name "'.$dest_file.
'") KO with antivirus: errors='.implode(
',', $checkvirusarray), LOG_WARNING);
1006 throw new RestException(500,
'ErrorFileIsInfectedWithAVirus: '.implode(
',', $checkvirusarray));
1015 $publicmediasdirwithslash =
$conf->medias->multidir_output[
$conf->entity];
1016 if (!preg_match(
'/\/$/', $publicmediasdirwithslash)) {
1017 $publicmediasdirwithslash .=
'/';
1020 if (strpos($upload_dir, $publicmediasdirwithslash) !== 0 || !
getDolGlobalInt(
"MAIN_DOCUMENT_DISABLE_NOEXE_IN_MEDIAS_DIR")) {
1021 $dest_file .=
'.noexe';
1027 if (preg_match(
'/^\./', basename($src_file)) || preg_match(
'/\.\./', $src_file) || preg_match(
'/[<>|]/', $src_file)) {
1028 dol_syslog(
"Refused to deliver file ".$src_file, LOG_WARNING);
1029 throw new RestException(500,
"Refused to deliver file ".$src_file);
1034 if (preg_match(
'/^\./', basename($dest_file)) || preg_match(
'/\.\./', $dest_file) || preg_match(
'/[<>|]/', $dest_file)) {
1035 dol_syslog(
"Refused to deliver file ".$dest_file, LOG_WARNING);
1036 throw new RestException(500,
"Refused to deliver file ".$dest_file);
1039 $moreinfo = array(
'note_private' =>
'File uploaded using API /documents from IP '.
getUserRemoteIP());
1042 $moreinfo[
'src_object_type'] =
$object->table_element;
1043 $moreinfo[
'src_object_id'] =
$object->id;
1045 if (!empty($array_options)) {
1046 $moreinfo = array_merge($moreinfo, [
"array_options" => $array_options]);
1048 if (!empty($position)) {
1049 $moreinfo = array_merge($moreinfo, [
"position" => $position]);
1051 if (!empty($cover)) {
1052 $moreinfo = array_merge($moreinfo, [
"cover" => $cover]);
1054 $moreinfo[
'gen_or_uploaded'] =
'api';
1057 $result =
dol_move($destfiletmp, $dest_file,
'0', $overwriteifexists, 1, 1, $moreinfo);
1059 throw new RestException(500,
"Failed to move file into '".$dest_file.
"'");
1062 if (is_object(
$object) && $generateThumbs) {
1063 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1065 $object->addThumbs($dest_file);
1091 public function delete($modulepart, $original_file)
1095 if (empty($modulepart)) {
1096 throw new RestException(400,
'bad value for parameter modulepart');
1098 if (empty($original_file)) {
1099 throw new RestException(400,
'bad value for parameter original_file');
1103 if ($modulepart ==
'task') {
1104 $modulepart =
'project_task';
1108 $entity =
$conf->entity;
1119 $relativefile = $original_file;
1122 $accessallowed = $check_access[
'accessallowed'];
1123 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
1124 $original_file = $check_access[
'original_file'];
1126 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
1127 throw new RestException(403);
1129 if (!$accessallowed) {
1130 throw new RestException(403);
1133 if (DolibarrApiAccess::$user->socid > 0) {
1134 if ($sqlprotectagainstexternals) {
1135 $resql = $this->db->query($sqlprotectagainstexternals);
1137 $num = $this->db->num_rows($resql);
1140 $obj = $this->db->fetch_object($resql);
1141 if (DolibarrApiAccess::$user->socid != $obj->fk_soc) {
1142 throw new RestException(403,
'Not allowed to download documents with such a ref');
1150 $filename = basename($original_file);
1151 $original_file_osencoded =
dol_osencode($original_file);
1153 if (!file_exists($original_file_osencoded)) {
1154 dol_syslog(
"Try to download not found file ".$original_file_osencoded, LOG_WARNING);
1155 throw new RestException(404,
'File not found');
1158 if (@unlink($original_file_osencoded)) {
1162 'message' =>
'Document deleted'
1167 throw new RestException(403);