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);
115 throw new RestException(403,
'Access not allowed to the object of this document');
118 if (DolibarrApiAccess::$user->socid > 0) {
119 if ($sqlprotectagainstexternals) {
120 $resql = $this->db->query($sqlprotectagainstexternals);
122 $num = $this->db->num_rows($resql);
125 $obj = $this->db->fetch_object($resql);
126 if (DolibarrApiAccess::$user->socid != $obj->fk_soc) {
127 throw new RestException(403,
'Not allowed to download documents with such a ref');
135 $filename = basename($original_file);
136 $original_file_osencoded =
dol_osencode($original_file);
138 if (!file_exists($original_file_osencoded)) {
139 dol_syslog(
"Try to download not found file ".$original_file_osencoded, LOG_WARNING);
140 throw new RestException(404,
'File not found');
143 $file_content = file_get_contents($original_file_osencoded);
144 return array(
'filename' => $filename,
'content-type' =>
dol_mimetype($filename),
'filesize' => filesize($original_file),
'content' => base64_encode($file_content),
'encoding' =>
'base64');
178 public function builddoc($modulepart, $original_file =
'', $doctemplate =
'', $langcode =
'')
180 global
$conf, $langs;
182 if (empty($modulepart)) {
183 throw new RestException(400,
'bad value for parameter modulepart');
185 if (empty($original_file)) {
186 throw new RestException(400,
'bad value for parameter original_file');
189 $outputlangs = $langs;
190 if ($langcode && $langs->defaultlang != $langcode) {
192 $outputlangs->setDefaultLang($langcode);
196 $entity =
$conf->entity;
207 $relativefile = $original_file;
210 $accessallowed = $check_access[
'accessallowed'];
211 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
212 $original_file = $check_access[
'original_file'];
214 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
215 throw new RestException(403);
217 if (!$accessallowed) {
218 throw new RestException(403);
222 throw new RestException(403,
'Access not allowed to the object of this document');
225 if (DolibarrApiAccess::$user->socid > 0) {
226 if ($sqlprotectagainstexternals) {
227 $resql = $this->db->query($sqlprotectagainstexternals);
229 $num = $this->db->num_rows($resql);
232 $obj = $this->db->fetch_object($resql);
233 if (DolibarrApiAccess::$user->socid != $obj->fk_soc) {
234 throw new RestException(403,
'Not allowed to download documents with such a ref');
249 if ($modulepart ==
'facture' || $modulepart ==
'invoice') {
250 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
251 $tmpobject =
new Facture($this->db);
252 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
254 throw new RestException(404,
'Invoice not found');
257 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
258 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
260 throw new RestException(500,
'Error generating document');
262 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'invoice_supplier') {
263 require_once DOL_DOCUMENT_ROOT .
'/fourn/class/fournisseur.facture.class.php';
265 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
267 throw new RestException(404,
'Supplier invoice 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 ==
'commande' || $modulepart ==
'order') {
276 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
277 $tmpobject =
new Commande($this->db);
278 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
280 throw new RestException(404,
'Order not found');
282 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
283 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
285 throw new RestException(500,
'Error generating document');
287 } elseif ($modulepart ==
'propal' || $modulepart ==
'proposal') {
288 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
289 $tmpobject =
new Propal($this->db);
290 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
292 throw new RestException(404,
'Proposal not found');
294 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
295 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
297 throw new RestException(500,
'Error generating document');
299 } elseif ($modulepart ==
'contrat' || $modulepart ==
'contract') {
300 require_once DOL_DOCUMENT_ROOT .
'/contrat/class/contrat.class.php';
302 $tmpobject =
new Contrat($this->db);
303 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
306 throw new RestException(404,
'Contract not found');
309 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
310 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
313 throw new RestException(500,
'Error generating document');
315 } elseif ($modulepart ==
'expedition' || $modulepart ==
'shipment') {
316 require_once DOL_DOCUMENT_ROOT .
'/expedition/class/expedition.class.php';
319 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
322 throw new RestException(404,
'Shipment not found');
325 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
326 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
329 throw new RestException(500,
'Error generating document');
331 } elseif ($modulepart ==
'mrp') {
332 require_once DOL_DOCUMENT_ROOT .
'/mrp/class/mo.class.php';
334 $tmpobject =
new Mo($this->db);
335 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
338 throw new RestException(404,
'MO not found');
341 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
342 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
345 throw new RestException(500,
'Error generating document');
347 } elseif ($modulepart ==
'expensereport') {
348 require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport.class.php';
351 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
354 throw new RestException(404,
'Expense report not found');
357 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
358 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
361 throw new RestException(500,
'Error generating document');
363 } elseif ($modulepart ==
'holiday') {
364 require_once DOL_DOCUMENT_ROOT.
'/holiday/class/holiday.class.php';
366 $tmpobject =
new Holiday($this->db);
367 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
370 throw new RestException(404,
'Holiday not found');
373 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
374 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
377 throw new RestException(500,
'Error generating document');
379 } elseif ($modulepart ==
'product') {
380 require_once DOL_DOCUMENT_ROOT .
'/product/class/product.class.php';
382 $tmpobject =
new Product($this->db);
383 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
386 throw new RestException(404,
'Product not found');
389 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
390 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
393 throw new RestException(500,
'Error generating document');
395 } elseif ($modulepart ==
'stock' || $modulepart ==
'entrepot') {
396 require_once DOL_DOCUMENT_ROOT .
'/product/stock/class/entrepot.class.php';
398 $tmpobject =
new Entrepot($this->db);
399 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
402 throw new RestException(404,
'Warehouse not found');
405 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
406 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
409 throw new RestException(500,
'Error generating document');
411 } elseif ($modulepart ==
'fichinter' || $modulepart ==
'intervention') {
412 require_once DOL_DOCUMENT_ROOT .
'/fichinter/class/fichinter.class.php';
415 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
418 throw new RestException(404,
'Intervention not found');
421 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
422 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
425 throw new RestException(500,
'Error generating document');
428 throw new RestException(403,
'Generation not available for this modulepart');
431 $filename = basename($original_file);
432 $original_file_osencoded =
dol_osencode($original_file);
434 if (!file_exists($original_file_osencoded)) {
435 throw new RestException(404,
'File not found');
438 $file_content = file_get_contents($original_file_osencoded);
439 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');
471 public function getDocumentsListByElement($modulepart,
$id = 0, $ref =
'', $sortfield =
'', $sortorder =
'', $limit = 100, $page = 0, $content_type =
'', $pagination_data =
false)
473 if (empty($modulepart)) {
474 throw new RestException(400,
'bad value for parameter modulepart');
477 if (empty(
$id) && empty($ref)) {
478 throw new RestException(400,
'bad value for parameter id or ref');
483 if ($modulepart ==
'facture_fournisseur') {
484 $modulepart =
'supplier_invoice';
488 $object = fetchObjectByElement(
$id, $modulepart, $ref);
490 throw new RestException(404,
'Module for modulepart = '.$modulepart.
" is not enabled (or not yet supported by API");
504 throw new RestException(403,
'Access not allowed to this object (refused by checkUserAccessToObject)');
508 if ($modulepart ==
'societe' || $modulepart ==
'thirdparty') {
509 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
510 throw new RestException(403);
512 } elseif ($modulepart ==
'user') {
514 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'lire') && DolibarrApiAccess::$user->
id !=
$id) {
515 throw new RestException(403);
517 } elseif ($modulepart ==
'adherent' || $modulepart ==
'member') {
518 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
519 throw new RestException(403);
521 } elseif ($modulepart ==
'propal' || $modulepart ==
'proposal') {
522 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
523 throw new RestException(403);
525 } elseif ($modulepart ==
'supplier_proposal') {
526 if (!DolibarrApiAccess::$user->hasRight(
'supplier_proposal',
'read')) {
527 throw new RestException(403);
529 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
530 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
531 throw new RestException(403);
533 } elseif ($modulepart ==
'commande_fournisseur' || $modulepart ==
'supplier_order') {
534 $modulepart =
'supplier_order';
535 if (!DolibarrApiAccess::$user->hasRight(
'fournisseur',
'commande',
'lire') && !DolibarrApiAccess::$user->hasRight(
'supplier_order',
'lire')) {
536 throw new RestException(403);
538 } elseif ($modulepart ==
'shipment' || $modulepart ==
'expedition') {
539 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'lire')) {
540 throw new RestException(403);
542 } elseif ($modulepart ==
'facture' || $modulepart ==
'invoice') {
543 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
544 throw new RestException(403);
546 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'supplier_invoice') {
547 $modulepart =
'supplier_invoice';
548 if (!DolibarrApiAccess::$user->hasRight(
'fournisseur',
'facture',
'lire') && !DolibarrApiAccess::$user->hasRight(
'supplier_invoice',
'lire')) {
549 throw new RestException(403);
551 } elseif ($modulepart ==
'produit' || $modulepart ==
'product' || $modulepart ==
'service') {
552 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'lire')) {
553 throw new RestException(403);
555 } elseif ($modulepart ==
'agenda' || $modulepart ==
'action' || $modulepart ==
'event' || $modulepart ==
'actioncomm') {
556 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'myactions',
'read') && !DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'read')) {
557 throw new RestException(403);
559 } elseif ($modulepart ==
'expensereport') {
560 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'read')) {
561 throw new RestException(403);
563 } elseif ($modulepart ==
'holiday') {
564 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'read')) {
565 throw new RestException(403);
567 } elseif ($modulepart ==
'ticket') {
568 if (!DolibarrApiAccess::$user->hasRight(
'ticket',
'read')) {
569 throw new RestException(403);
571 } elseif ($modulepart ==
'knowledgemanagement') {
572 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read')) {
573 throw new RestException(403);
575 } elseif ($modulepart ==
'categorie' || $modulepart ==
'category') {
576 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
577 throw new RestException(403);
579 } elseif ($modulepart ==
'ecm') {
580 throw new RestException(500,
'Modulepart Ecm not implemented yet.');
584 } elseif ($modulepart ==
'contrat' || $modulepart ==
'contract') {
585 $modulepart =
'contrat';
586 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'lire')) {
587 throw new RestException(403);
589 } elseif ($modulepart ==
'intervention' || $modulepart ==
'ficheinter') {
590 $modulepart =
'ficheinter';
591 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'lire')) {
592 throw new RestException(403);
594 } elseif ($modulepart ==
'projet' || $modulepart ==
'project') {
595 $modulepart =
'project';
596 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
597 throw new RestException(403);
599 } elseif ($modulepart ==
'task' || $modulepart ==
'project_task') {
600 $modulepart =
'project_task';
601 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
602 throw new RestException(403);
604 } elseif ($modulepart ==
'mrp') {
606 if (!DolibarrApiAccess::$user->hasRight(
'mrp',
'read')) {
607 throw new RestException(403);
609 } elseif ($modulepart ==
'contact' || $modulepart ==
'socpeople') {
610 $modulepart =
'contact';
611 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'lire')) {
612 throw new RestException(403);
614 } elseif ($modulepart ==
'stock') {
615 if (!DolibarrApiAccess::$user->hasRight(
'stock',
'lire')) {
616 throw new RestException(403);
619 throw new RestException(500,
'Modulepart '.$modulepart.
' not implemented yet.');
626 $objectType = $modulepart;
628 $objectType =
$object->table_element;
631 $filearray =
dol_dir_list($upload_dir, $type, $recursive,
'',
'(\.meta|_preview.*\.png)$', $sortfield, (strtolower($sortorder) ==
'desc' ? SORT_DESC : SORT_ASC), 1);
633 $countarray = is_array($filearray) ? count($filearray) : 0;
635 if (empty($filearray)) {
636 throw new RestException(404,
'Search for modulepart '.$modulepart.
' with Id '.
$id.(!empty($ref) ?
' or Ref '.$ref :
'').
' does not return any document.');
638 $filearray = array_slice($filearray, $limit * $page, $limit);
639 if ((
$object->id) > 0 && !empty($modulepart)) {
640 require_once DOL_DOCUMENT_ROOT.
'/ecm/class/ecmfiles.class.php';
642 $result = $ecmfile->fetchAll(
'',
'', 0, 0, array(
't.src_object_type' => $objectType,
't.src_object_id' =>
$object->id));
644 throw new RestException(503,
'Error when retrieve ecm list : '.$this->db->lasterror());
645 } elseif (is_array($ecmfile->lines) && count($ecmfile->lines) > 0) {
646 foreach ($filearray as &$fileitem) {
647 foreach ($ecmfile->lines as $line) {
648 if ($fileitem[
'name'] == $line->filename) {
651 $fileitem[
'ref'] = $line->ref;
652 $fileitem[
'label'] = $line->label;
653 $fileitem[
'filepath'] = $line->filepath;
654 $fileitem[
'filename'] = $line->filename;
655 $fileitem[
'fullpath_orig'] = $line->fullpath_orig;
656 $fileitem[
'position'] = $line->position;
657 $fileitem[
'gen_or_uploaded'] = $line->gen_or_uploaded;
658 $fileitem[
'description'] = $line->desc;
659 $fileitem[
'keywords'] = $line->keywords;
660 $fileitem[
'cover'] = $line->cover;
661 $fileitem[
'share'] = $line->share;
662 $fileitem[
'date_c'] = $line->date_c;
663 $fileitem[
'agenda_id'] = $line->agenda_id;
664 $fileitem[
'fk_user_c'] = $line->fk_user_c;
665 $fileitem[
'fk_user_m'] = $line->fk_user_m;
666 $fileitem[
'note_private'] = $line->note_private;
667 $fileitem[
'note_public'] = $line->note_public;
670 if (isset($fileitem[
'relativename'])) {
671 $fileitem[
'content-type'] =
dol_mimetype((
string) $fileitem[
'relativename']);
676 $arraycontenttype = explode(
",", $content_type);
677 if (!empty($content_type)) {
678 $filearray = array_filter(
684 static function ($fileitem) use (&$arraycontenttype) {
685 return in_array(($fileitem[
'content-type'] ?:
'UNKNOWN'), $arraycontenttype);
694 foreach ($filearray as $tmpkey => $tmpval) {
695 unset($filearray[$tmpkey][
'path']);
696 unset($filearray[$tmpkey][
'fullname']);
700 if ($pagination_data) {
702 'data' => $filearray,
704 'total' => (int) $countarray,
706 'page_count' => (
int) ceil((
int) $countarray / $limit),
766 public function post($filename, $modulepart, $ref =
'', $subdir =
'', $filecontent =
'', $fileencoding =
'', $overwriteifexists = 0, $createdirifnotexists = 1, $position = 0, $cover =
'', $array_options = [], $generateThumbs = 0, $share = 0)
770 $modulepartorig = $modulepart;
772 if (empty($modulepart)) {
773 throw new RestException(400,
'Modulepart not provided.');
776 $newfilecontent =
'';
777 if (empty($fileencoding)) {
778 $newfilecontent = $filecontent;
780 if ($fileencoding ==
'base64') {
781 $newfilecontent = base64_decode($filecontent);
785 $relativefile =
'UNSET';
789 $entity = DolibarrApiAccess::$user->entity;
790 if (empty($entity)) {
798 if ($modulepart ==
'facture' || $modulepart ==
'invoice') {
799 $modulepart =
'facture';
801 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
803 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'supplier_invoice') {
804 $modulepart =
'supplier_invoice';
806 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
808 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
809 $modulepart =
'commande';
811 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
813 } elseif ($modulepart ==
'commande_fournisseur' || $modulepart ==
'supplier_order') {
814 $modulepart =
'supplier_order';
816 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.class.php';
818 } elseif ($modulepart ==
'projet' || $modulepart ==
'project') {
819 require_once DOL_DOCUMENT_ROOT.
'/projet/class/project.class.php';
821 } elseif ($modulepart ==
'task' || $modulepart ==
'project_task') {
822 $modulepart =
'project_task';
824 require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
827 $task_result =
$object->fetch(0, $ref);
830 if ($task_result > 0) {
831 $project_result =
$object->fetchProject();
833 if ($project_result >= 0) {
837 throw new RestException(500,
'Error while fetching Task '.$ref);
839 } elseif ($modulepart ==
'product' || $modulepart ==
'produit' || $modulepart ==
'service' || $modulepart ==
'produit|service') {
840 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
842 } elseif ($modulepart ==
'expensereport') {
843 require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport.class.php';
845 } elseif ($modulepart ==
'holiday') {
846 require_once DOL_DOCUMENT_ROOT.
'/holiday/class/holiday.class.php';
848 } elseif ($modulepart ==
'ficheinter' || $modulepart ==
'intervention') {
849 require_once DOL_DOCUMENT_ROOT.
'/fichinter/class/fichinter.class.php';
851 } elseif ($modulepart ==
'shipment' || $modulepart ==
'expedition') {
852 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
854 } elseif ($modulepart ==
'adherent' || $modulepart ==
'member') {
855 $modulepart =
'adherent';
856 require_once DOL_DOCUMENT_ROOT.
'/adherents/class/adherent.class.php';
858 } elseif ($modulepart ==
'proposal' || $modulepart ==
'propal' || $modulepart ==
'propale') {
859 $modulepart =
'propale';
860 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
862 } elseif ($modulepart ==
'agenda' || $modulepart ==
'action' || $modulepart ==
'event') {
863 $modulepart =
'agenda';
864 require_once DOL_DOCUMENT_ROOT .
'/comm/action/class/actioncomm.class.php';
866 } elseif ($modulepart ==
'contact' || $modulepart ==
'socpeople') {
867 $modulepart =
'contact';
868 require_once DOL_DOCUMENT_ROOT.
'/contact/class/contact.class.php';
871 } elseif ($modulepart ==
'societe' || $modulepart ==
'company') {
872 $modulepart =
'societe';
873 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
876 } elseif ($modulepart ==
'knowledgemanagement') {
877 $modulepart =
'knowledgemanagement';
878 require_once DOL_DOCUMENT_ROOT.
'/knowledgemanagement/class/knowledgerecord.class.php';
881 } elseif ($modulepart ==
'ticket') {
882 $modulepart =
'ticket';
883 require_once DOL_DOCUMENT_ROOT.
'/ticket/class/ticket.class.php';
886 } elseif ($modulepart ==
'contrat' || $modulepart ==
'contract') {
887 $modulepart =
'contrat';
888 require_once DOL_DOCUMENT_ROOT .
'/contrat/class/contrat.class.php';
890 } elseif ($modulepart ==
'mrp') {
892 require_once DOL_DOCUMENT_ROOT .
'/mrp/class/mo.class.php';
894 } elseif ($modulepart ==
'stock') {
895 $modulepart =
'stock';
896 require_once DOL_DOCUMENT_ROOT .
'/product/stock/class/entrepot.class.php';
898 } elseif ($modulepart ==
'ecm') {
899 throw new RestException(500,
'Using a non empty "ref" is not compatible with using modulepart = '.$modulepart);
902 throw new RestException(500,
'Modulepart '.$modulepart.
' not implemented yet.');
908 $result =
$object->fetch((
int) $ref);
910 $result =
$object->fetch(0, $ref);
914 throw new RestException(404,
"Object with ref '".$ref.
"' was not found.");
915 } elseif ($result < 0) {
916 throw new RestException(500,
'Error while fetching object: '.
$object->error);
920 throw new RestException(404,
'The object '.$modulepart.
" with ref '".$ref.
"' was not found.");
925 if ($modulepart ==
'supplier_invoice') {
931 if ($modulepart ==
'societe') {
932 $relativefile = $tmpreldir.dol_sanitizeFileName((
string)
$object->id);
934 $relativefile = $tmpreldir.dol_sanitizeFileName((
string)
$object->ref);
937 if (empty($tmp[
'accessallowed'])) {
938 throw new RestException(403,
'Access not allowed to upload file into this directory');
942 throw new RestException(403,
'Access not allowed to the object of this document');
944 $upload_dir = $tmp[
'original_file'];
952 if (empty($upload_dir) || $upload_dir ==
'/') {
953 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.');
956 if ($modulepart ==
'invoice') {
957 $modulepart =
'facture';
959 if ($modulepart ==
'member') {
960 $modulepart =
'adherent';
964 if ($modulepart !=
'ecm') {
965 $relativefile = $subdir;
967 if (empty($tmp[
'accessallowed'])) {
968 throw new RestException(403,
'Access not allowed to upload file into this directory');
972 throw new RestException(403,
'Access not allowed to the object of this document');
974 $upload_dir = $tmp[
'original_file'];
976 if (!DolibarrApiAccess::$user->hasRight(
'ecm',
'upload')) {
977 throw new RestException(403,
'Missing permission to upload files in ECM module');
979 $upload_dir =
$conf->medias->multidir_output[
$conf->entity];
982 if (empty($upload_dir) || $upload_dir ==
'/') {
983 if (!empty($tmp[
'error'])) {
984 throw new RestException(403,
'Error returned by dol_check_secure_access_document: '.$tmp[
'error']);
986 throw new RestException(400,
'This value of modulepart ('.$modulepart.
') is not allowed with this value of subdir ('.$relativefile.
')');
994 if (!empty($createdirifnotexists)) {
996 throw new RestException(500,
'Error while trying to create directory '.$upload_dir);
1000 $destfile = $upload_dir.
'/'.$original_file;
1001 $destfiletmp = DOL_DATA_ROOT.
'/admin/temp/'.$original_file;
1006 throw new RestException(400,
'Directory does not exists : '.dirname($destfile));
1009 if (!$overwriteifexists &&
dol_is_file($destfile)) {
1010 throw new RestException(400,
"File with name '".$original_file.
"' already exists.");
1018 $fhandle = @fopen($destfiletmp,
'w');
1020 $nbofbyteswrote = fwrite($fhandle, $newfilecontent);
1024 throw new RestException(500,
"Failed to open file '".$destfiletmp.
"' for write");
1027 $disablevirusscan = 0;
1028 $src_file = $destfiletmp;
1029 $dest_file = $destfile;
1033 if (empty($disablevirusscan) && file_exists($src_file)) {
1035 if (count($checkvirusarray)) {
1036 dol_syslog(
'Files.lib::dol_move_uploaded_file File "'.$src_file.
'" (target name "'.$dest_file.
'") KO with antivirus: errors='.implode(
',', $checkvirusarray), LOG_WARNING);
1037 throw new RestException(500,
'ErrorFileIsInfectedWithAVirus: '.implode(
',', $checkvirusarray));
1046 $publicmediasdirwithslash =
$conf->medias->multidir_output[
$conf->entity];
1047 if (!preg_match(
'/\/$/', $publicmediasdirwithslash)) {
1048 $publicmediasdirwithslash .=
'/';
1051 if (strpos($upload_dir, $publicmediasdirwithslash) !== 0 || !
getDolGlobalInt(
"MAIN_DOCUMENT_DISABLE_NOEXE_IN_MEDIAS_DIR")) {
1052 $dest_file .=
'.noexe';
1058 if (preg_match(
'/^\./', basename($src_file)) || preg_match(
'/\.\./', $src_file) || preg_match(
'/[<>|]/', $src_file)) {
1059 dol_syslog(
"Refused to deliver file ".$src_file, LOG_WARNING);
1060 throw new RestException(500,
"Refused to deliver file ".$src_file);
1065 if (preg_match(
'/^\./', basename($dest_file)) || preg_match(
'/\.\./', $dest_file) || preg_match(
'/[<>|]/', $dest_file)) {
1066 dol_syslog(
"Refused to deliver file ".$dest_file, LOG_WARNING);
1067 throw new RestException(500,
"Refused to deliver file ".$dest_file);
1070 $moreinfo = array(
'note_private' =>
'File uploaded using API /documents from IP '.
getUserRemoteIP());
1073 $moreinfo[
'src_object_type'] =
$object->table_element;
1074 $moreinfo[
'src_object_id'] =
$object->id;
1076 if (!empty($array_options)) {
1077 $moreinfo = array_merge($moreinfo, [
"array_options" => $array_options]);
1079 if (!empty($position)) {
1080 $moreinfo = array_merge($moreinfo, [
"position" => $position]);
1082 if (!empty($cover)) {
1083 $moreinfo = array_merge($moreinfo, [
"cover" => $cover]);
1085 if (!empty($share)) {
1086 require_once DOL_DOCUMENT_ROOT.
'/core/lib/security2.lib.php';
1089 $moreinfo[
'gen_or_uploaded'] =
'api';
1092 $result =
dol_move($destfiletmp, $dest_file,
'0', $overwriteifexists, 1, 1, $moreinfo);
1094 throw new RestException(500,
"Failed to move file into '".$dest_file.
"'");
1097 if (is_object(
$object) && $generateThumbs) {
1098 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1099 require_once DOL_DOCUMENT_ROOT.
'/core/lib/images.lib.php';
1101 $object->addThumbs($dest_file);
1127 public function delete($modulepart, $original_file)
1131 if (empty($modulepart)) {
1132 throw new RestException(400,
'bad value for parameter modulepart');
1134 if (empty($original_file)) {
1135 throw new RestException(400,
'bad value for parameter original_file');
1139 if ($modulepart ==
'task') {
1140 $modulepart =
'project_task';
1144 $entity =
$conf->entity;
1155 $relativefile = $original_file;
1158 $accessallowed = $check_access[
'accessallowed'];
1159 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
1160 $original_file = $check_access[
'original_file'];
1162 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
1163 throw new RestException(403);
1165 if (!$accessallowed) {
1166 throw new RestException(403);
1170 throw new RestException(403,
'Access not allowed to the object of this document');
1173 if (DolibarrApiAccess::$user->socid > 0) {
1174 if ($sqlprotectagainstexternals) {
1175 $resql = $this->db->query($sqlprotectagainstexternals);
1177 $num = $this->db->num_rows($resql);
1180 $obj = $this->db->fetch_object($resql);
1181 if (DolibarrApiAccess::$user->socid != $obj->fk_soc) {
1182 throw new RestException(403,
'Not allowed to download documents with such a ref');
1190 $filename = basename($original_file);
1191 $original_file_osencoded =
dol_osencode($original_file);
1193 if (!file_exists($original_file_osencoded)) {
1194 dol_syslog(
"Try to download not found file ".$original_file_osencoded, LOG_WARNING);
1195 throw new RestException(404,
'File not found');
1198 if (@unlink($original_file_osencoded)) {
1202 'message' =>
'Document deleted'
1207 throw new RestException(403);