27use Luracast\Restler\RestException;
29require_once DOL_DOCUMENT_ROOT.
'/main.inc.php';
30require_once DOL_DOCUMENT_ROOT.
'/api/class/api.class.php';
31require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
73 public function index($modulepart, $original_file =
'')
77 if (empty($modulepart)) {
78 throw new RestException(400,
'bad value for parameter modulepart');
80 if (empty($original_file)) {
81 throw new RestException(400,
'bad value for parameter original_file');
85 if ($modulepart ==
'task' || $modulepart ==
'project_task') {
86 $modulepart =
'project_task';
90 $entity =
$conf->entity;
101 $relativefile = $original_file;
104 $accessallowed = $check_access[
'accessallowed'];
105 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
106 $original_file = $check_access[
'original_file'];
108 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
109 throw new RestException(403);
111 if (!$accessallowed) {
112 throw new RestException(403);
116 throw new RestException(403,
'Access not allowed to the object of this document');
119 if (DolibarrApiAccess::$user->socid > 0) {
120 if ($sqlprotectagainstexternals) {
121 $resql = $this->db->query($sqlprotectagainstexternals);
123 $num = $this->db->num_rows($resql);
126 $obj = $this->db->fetch_object($resql);
127 if (DolibarrApiAccess::$user->socid != $obj->fk_soc) {
128 throw new RestException(403,
'Not allowed to download documents with such a ref');
136 $filename = basename($original_file);
137 $original_file_osencoded =
dol_osencode($original_file);
139 if (!file_exists($original_file_osencoded)) {
140 dol_syslog(
"Try to download not found file ".$original_file_osencoded, LOG_WARNING);
141 throw new RestException(404,
'File not found');
144 $file_content = file_get_contents($original_file_osencoded);
145 return array(
'filename' => $filename,
'content-type' =>
dol_mimetype($filename),
'filesize' => filesize($original_file),
'content' => base64_encode($file_content),
'encoding' =>
'base64');
179 public function builddoc($modulepart, $original_file =
'', $doctemplate =
'', $langcode =
'')
181 global
$conf, $langs;
183 if (empty($modulepart)) {
184 throw new RestException(400,
'bad value for parameter modulepart');
186 if (empty($original_file)) {
187 throw new RestException(400,
'bad value for parameter original_file');
190 $outputlangs = $langs;
191 if ($langcode && $langs->defaultlang != $langcode) {
193 $outputlangs->setDefaultLang($langcode);
197 $entity =
$conf->entity;
208 $relativefile = $original_file;
211 $accessallowed = $check_access[
'accessallowed'];
212 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
213 $original_file = $check_access[
'original_file'];
215 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
216 throw new RestException(403);
218 if (!$accessallowed) {
219 throw new RestException(403);
223 throw new RestException(403,
'Access not allowed to the object of this document');
226 if (DolibarrApiAccess::$user->socid > 0) {
227 if ($sqlprotectagainstexternals) {
228 $resql = $this->db->query($sqlprotectagainstexternals);
230 $num = $this->db->num_rows($resql);
233 $obj = $this->db->fetch_object($resql);
234 if (DolibarrApiAccess::$user->socid != $obj->fk_soc) {
235 throw new RestException(403,
'Not allowed to download documents with such a ref');
250 if ($modulepart ==
'facture' || $modulepart ==
'invoice') {
251 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
252 $tmpobject =
new Facture($this->db);
253 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
255 throw new RestException(404,
'Invoice 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 ==
'facture_fournisseur' || $modulepart ==
'invoice_supplier') {
264 require_once DOL_DOCUMENT_ROOT .
'/fourn/class/fournisseur.facture.class.php';
266 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
268 throw new RestException(404,
'Supplier invoice not found');
271 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
272 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
274 throw new RestException(500,
'Error generating document');
276 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
277 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
278 $tmpobject =
new Commande($this->db);
279 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
281 throw new RestException(404,
'Order not found');
283 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
284 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
286 throw new RestException(500,
'Error generating document');
288 } elseif ($modulepart ==
'propal' || $modulepart ==
'proposal') {
289 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
290 $tmpobject =
new Propal($this->db);
291 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
293 throw new RestException(404,
'Proposal not found');
295 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
296 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
298 throw new RestException(500,
'Error generating document');
300 } elseif ($modulepart ==
'contrat' || $modulepart ==
'contract') {
301 require_once DOL_DOCUMENT_ROOT .
'/contrat/class/contrat.class.php';
303 $tmpobject =
new Contrat($this->db);
304 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
307 throw new RestException(404,
'Contract not found');
310 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
311 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
314 throw new RestException(500,
'Error generating document');
316 } elseif ($modulepart ==
'expedition' || $modulepart ==
'shipment') {
317 require_once DOL_DOCUMENT_ROOT .
'/expedition/class/expedition.class.php';
320 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
323 throw new RestException(404,
'Shipment not found');
326 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
327 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
330 throw new RestException(500,
'Error generating document');
332 } elseif ($modulepart ==
'mrp') {
333 require_once DOL_DOCUMENT_ROOT .
'/mrp/class/mo.class.php';
335 $tmpobject =
new Mo($this->db);
336 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
339 throw new RestException(404,
'MO not found');
342 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
343 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
346 throw new RestException(500,
'Error generating document');
348 } elseif ($modulepart ==
'expensereport') {
349 require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport.class.php';
352 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
355 throw new RestException(404,
'Expense report not found');
358 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
359 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
362 throw new RestException(500,
'Error generating document');
364 } elseif ($modulepart ==
'holiday') {
365 require_once DOL_DOCUMENT_ROOT.
'/holiday/class/holiday.class.php';
367 $tmpobject =
new Holiday($this->db);
368 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
371 throw new RestException(404,
'Holiday not found');
374 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
375 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
378 throw new RestException(500,
'Error generating document');
380 } elseif ($modulepart ==
'product') {
381 require_once DOL_DOCUMENT_ROOT .
'/product/class/product.class.php';
383 $tmpobject =
new Product($this->db);
384 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
387 throw new RestException(404,
'Product not found');
390 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
391 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
394 throw new RestException(500,
'Error generating document');
396 } elseif ($modulepart ==
'stock' || $modulepart ==
'entrepot') {
397 require_once DOL_DOCUMENT_ROOT .
'/product/stock/class/entrepot.class.php';
399 $tmpobject =
new Entrepot($this->db);
400 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
403 throw new RestException(404,
'Warehouse not found');
406 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
407 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
410 throw new RestException(500,
'Error generating document');
412 } elseif ($modulepart ==
'fichinter' || $modulepart ==
'intervention') {
413 require_once DOL_DOCUMENT_ROOT .
'/fichinter/class/fichinter.class.php';
416 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
419 throw new RestException(404,
'Intervention not found');
422 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
423 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
426 throw new RestException(500,
'Error generating document');
429 throw new RestException(403,
'Generation not available for this modulepart');
432 $filename = basename($original_file);
433 $original_file_osencoded =
dol_osencode($original_file);
435 if (!file_exists($original_file_osencoded)) {
436 throw new RestException(404,
'File not found');
439 $file_content = file_get_contents($original_file_osencoded);
440 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');
472 public function getDocumentsListByElement($modulepart,
$id = 0, $ref =
'', $sortfield =
'', $sortorder =
'', $limit = 100, $page = 0, $content_type =
'', $pagination_data =
false)
474 if (empty($modulepart)) {
475 throw new RestException(400,
'bad value for parameter modulepart');
478 if (empty(
$id) && empty($ref)) {
479 throw new RestException(400,
'bad value for parameter id or ref');
484 if ($modulepart ==
'facture_fournisseur') {
485 $modulepart =
'supplier_invoice';
489 $object = fetchObjectByElement(
$id, $modulepart, $ref);
491 throw new RestException(404,
'Module for modulepart = '.$modulepart.
" is not enabled (or not yet supported by API");
505 throw new RestException(403,
'Access not allowed to this object (refused by checkUserAccessToObject)');
509 if ($modulepart ==
'societe' || $modulepart ==
'thirdparty') {
510 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
511 throw new RestException(403);
513 } elseif ($modulepart ==
'user') {
515 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'lire') && DolibarrApiAccess::$user->
id !=
$id) {
516 throw new RestException(403);
518 } elseif ($modulepart ==
'adherent' || $modulepart ==
'member') {
519 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
520 throw new RestException(403);
522 } elseif ($modulepart ==
'propal' || $modulepart ==
'proposal') {
523 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
524 throw new RestException(403);
526 } elseif ($modulepart ==
'supplier_proposal') {
527 if (!DolibarrApiAccess::$user->hasRight(
'supplier_proposal',
'read')) {
528 throw new RestException(403);
530 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
531 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
532 throw new RestException(403);
534 } elseif ($modulepart ==
'commande_fournisseur' || $modulepart ==
'supplier_order') {
535 $modulepart =
'supplier_order';
536 if (!DolibarrApiAccess::$user->hasRight(
'fournisseur',
'commande',
'lire') && !DolibarrApiAccess::$user->hasRight(
'supplier_order',
'lire')) {
537 throw new RestException(403);
539 } elseif ($modulepart ==
'shipment' || $modulepart ==
'expedition') {
540 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'lire')) {
541 throw new RestException(403);
543 } elseif ($modulepart ==
'reception') {
544 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'lire')) {
545 throw new RestException(403);
547 } elseif ($modulepart ==
'facture' || $modulepart ==
'invoice') {
548 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
549 throw new RestException(403);
551 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'supplier_invoice') {
552 $modulepart =
'supplier_invoice';
553 if (!DolibarrApiAccess::$user->hasRight(
'fournisseur',
'facture',
'lire') && !DolibarrApiAccess::$user->hasRight(
'supplier_invoice',
'lire')) {
554 throw new RestException(403);
556 } elseif ($modulepart ==
'produit' || $modulepart ==
'product' || $modulepart ==
'service') {
557 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'lire')) {
558 throw new RestException(403);
560 } elseif ($modulepart ==
'agenda' || $modulepart ==
'action' || $modulepart ==
'event' || $modulepart ==
'actioncomm') {
561 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'myactions',
'read') && !DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'read')) {
562 throw new RestException(403);
564 } elseif ($modulepart ==
'expensereport') {
565 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'read')) {
566 throw new RestException(403);
568 } elseif ($modulepart ==
'holiday') {
569 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'read')) {
570 throw new RestException(403);
572 } elseif ($modulepart ==
'ticket') {
573 if (!DolibarrApiAccess::$user->hasRight(
'ticket',
'read')) {
574 throw new RestException(403);
576 } elseif ($modulepart ==
'knowledgemanagement') {
577 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read')) {
578 throw new RestException(403);
580 } elseif ($modulepart ==
'categorie' || $modulepart ==
'category') {
581 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
582 throw new RestException(403);
584 } elseif ($modulepart ==
'ecm') {
585 throw new RestException(500,
'Modulepart Ecm not implemented yet.');
589 } elseif ($modulepart ==
'contrat' || $modulepart ==
'contract') {
590 $modulepart =
'contrat';
591 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'lire')) {
592 throw new RestException(403);
594 } elseif ($modulepart ==
'intervention' || $modulepart ==
'ficheinter') {
595 $modulepart =
'ficheinter';
596 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'lire')) {
597 throw new RestException(403);
599 } elseif ($modulepart ==
'projet' || $modulepart ==
'project') {
600 $modulepart =
'project';
601 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
602 throw new RestException(403);
604 } elseif ($modulepart ==
'task' || $modulepart ==
'project_task') {
605 $modulepart =
'project_task';
606 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
607 throw new RestException(403);
609 } elseif ($modulepart ==
'mrp') {
611 if (!DolibarrApiAccess::$user->hasRight(
'mrp',
'read')) {
612 throw new RestException(403);
614 } elseif ($modulepart ==
'contact' || $modulepart ==
'socpeople') {
615 $modulepart =
'contact';
616 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'lire')) {
617 throw new RestException(403);
619 } elseif ($modulepart ==
'stock') {
620 if (!DolibarrApiAccess::$user->hasRight(
'stock',
'lire')) {
621 throw new RestException(403);
624 throw new RestException(500,
'Modulepart '.$modulepart.
' not implemented yet.');
631 $objectType = $modulepart;
633 $objectType =
$object->table_element;
636 $filearray =
dol_dir_list($upload_dir, $type, $recursive,
'',
'(\.meta|_preview.*\.png)$', $sortfield, (strtolower($sortorder) ==
'desc' ? SORT_DESC : SORT_ASC), 1);
638 $countarray = is_array($filearray) ? count($filearray) : 0;
640 if (empty($filearray)) {
641 throw new RestException(404,
'Search for modulepart '.$modulepart.
' with Id '.
$id.(!empty($ref) ?
' or Ref '.$ref :
'').
' does not return any document.');
643 $filearray = array_slice($filearray, $limit * $page, $limit);
644 if ((
$object->id) > 0 && !empty($modulepart)) {
645 require_once DOL_DOCUMENT_ROOT.
'/ecm/class/ecmfiles.class.php';
647 $result = $ecmfile->fetchAll(
'',
'', 0, 0, array(
't.src_object_type' => $objectType,
't.src_object_id' =>
$object->id));
649 throw new RestException(503,
'Error when retrieve ecm list : '.$this->db->lasterror());
650 } elseif (is_array($ecmfile->lines) && count($ecmfile->lines) > 0) {
651 foreach ($filearray as &$fileitem) {
652 foreach ($ecmfile->lines as $line) {
653 if ($fileitem[
'name'] == $line->filename) {
656 $fileitem[
'ref'] = $line->ref;
657 $fileitem[
'label'] = $line->label;
658 $fileitem[
'filepath'] = $line->filepath;
659 $fileitem[
'filename'] = $line->filename;
660 $fileitem[
'fullpath_orig'] = $line->fullpath_orig;
661 $fileitem[
'position'] = $line->position;
662 $fileitem[
'gen_or_uploaded'] = $line->gen_or_uploaded;
663 $fileitem[
'description'] = $line->desc;
664 $fileitem[
'keywords'] = $line->keywords;
665 $fileitem[
'cover'] = $line->cover;
666 $fileitem[
'share'] = $line->share;
667 $fileitem[
'date_c'] = $line->date_c;
668 $fileitem[
'agenda_id'] = $line->agenda_id;
669 $fileitem[
'fk_user_c'] = $line->fk_user_c;
670 $fileitem[
'fk_user_m'] = $line->fk_user_m;
671 $fileitem[
'note_private'] = $line->note_private;
672 $fileitem[
'note_public'] = $line->note_public;
675 if (isset($fileitem[
'relativename'])) {
676 $fileitem[
'content-type'] =
dol_mimetype((
string) $fileitem[
'relativename']);
681 $arraycontenttype = explode(
",", $content_type);
682 if (!empty($content_type)) {
683 $filearray = array_filter(
689 static function ($fileitem) use (&$arraycontenttype) {
690 return in_array(($fileitem[
'content-type'] ?:
'UNKNOWN'), $arraycontenttype);
699 foreach ($filearray as $tmpkey => $tmpval) {
700 unset($filearray[$tmpkey][
'path']);
701 unset($filearray[$tmpkey][
'fullname']);
705 if ($pagination_data) {
707 'data' => $filearray,
709 'total' => (int) $countarray,
711 'page_count' => (
int) ceil((
int) $countarray / $limit),
747 public function getDocumentsListByDirectory($modulepart, $subdir =
'', $recursive = 0, $search =
'', $sortfield =
'name', $sortorder =
'asc', $limit = 100, $page = 0, $pagination_data =
false)
751 if (!in_array($modulepart, array(
'ecm',
'medias'))) {
752 throw new RestException(400,
'bad value for parameter modulepart, only ecm and medias are supported');
755 throw new RestException(400,
'Module ECM is not enabled');
757 $subdir = trim((
string) $subdir,
'/');
758 if (preg_match(
'/\.\./', $subdir) || preg_match(
'/[<>|]/', $subdir)) {
759 throw new RestException(400,
'bad value for parameter subdir');
761 if (!in_array($sortfield, array(
'name',
'date',
'size'))) {
766 if (empty($check_access[
'accessallowed'])) {
767 throw new RestException(403);
769 $dir = rtrim($check_access[
'original_file'],
'/');
771 throw new RestException(404,
'Directory '.$subdir.
' not found in area '.$modulepart);
774 $filter = ($search !==
'' ? preg_quote($search,
'/') :
'');
775 $excludefilter = array(
'^\.',
'(\.meta|_preview.*\.png)$',
'^temp$',
'^CVS$',
'^thumbs$');
776 $filearray =
dol_dir_list($dir,
'all', $recursive ? 1 : 0, $filter, $excludefilter, $sortfield, (strtolower($sortorder) ==
'desc' ? SORT_DESC : SORT_ASC), 1);
778 $countarray = count($filearray);
779 $filearray = array_slice($filearray, $limit * $page, $limit);
783 foreach ($filearray as $fileitem) {
785 'name' => $fileitem[
'name'],
786 'relativename' => ($subdir !==
'' ? $subdir.
'/' :
'').$fileitem[
'relativename'],
787 'type' => ($fileitem[
'type'] ==
'dir' ?
'dir' :
'file'),
788 'date' => (
int) $fileitem[
'date'],
789 'size' => (int) $fileitem[
'size']
793 if ($pagination_data) {
796 'pagination' => array(
797 'total' => (
int) $countarray,
798 'page' => (
int) $page,
799 'page_count' => (
int) ceil((
int) $countarray / max(1, (
int) $limit)),
800 'limit' => (
int) $limit
859 public function post($filename, $modulepart, $ref =
'', $subdir =
'', $filecontent =
'', $fileencoding =
'', $overwriteifexists = 0, $createdirifnotexists = 1, $position = 0, $cover =
'', $array_options = [], $generateThumbs = 0, $share = 0)
862 global
$conf, $langs;
865 $modulepartorig = $modulepart;
867 if (empty($modulepart)) {
868 throw new RestException(400,
'Modulepart not provided.');
871 $newfilecontent =
'';
872 if (empty($fileencoding)) {
873 $newfilecontent = $filecontent;
875 if ($fileencoding ==
'base64') {
876 $newfilecontent = base64_decode($filecontent);
880 $relativefile =
'UNSET';
884 $entity = DolibarrApiAccess::$user->entity;
885 if (empty($entity)) {
893 if ($modulepart ==
'facture' || $modulepart ==
'invoice') {
894 $modulepart =
'facture';
896 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
898 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'supplier_invoice') {
899 $modulepart =
'supplier_invoice';
901 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
903 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
904 $modulepart =
'commande';
906 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
908 } elseif ($modulepart ==
'commande_fournisseur' || $modulepart ==
'supplier_order') {
909 $modulepart =
'supplier_order';
911 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.class.php';
913 } elseif ($modulepart ==
'projet' || $modulepart ==
'project') {
914 require_once DOL_DOCUMENT_ROOT.
'/projet/class/project.class.php';
916 } elseif ($modulepart ==
'task' || $modulepart ==
'project_task') {
917 $modulepart =
'project_task';
919 require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
922 $task_result =
$object->fetch(0, $ref);
925 if ($task_result > 0) {
926 $project_result =
$object->fetchProject();
928 if ($project_result >= 0) {
932 throw new RestException(500,
'Error while fetching Task '.$ref);
934 } elseif ($modulepart ==
'product' || $modulepart ==
'produit' || $modulepart ==
'service' || $modulepart ==
'produit|service') {
935 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
937 } elseif ($modulepart ==
'expensereport') {
938 require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport.class.php';
940 } elseif ($modulepart ==
'holiday') {
941 require_once DOL_DOCUMENT_ROOT.
'/holiday/class/holiday.class.php';
943 } elseif ($modulepart ==
'ficheinter' || $modulepart ==
'intervention') {
944 require_once DOL_DOCUMENT_ROOT.
'/fichinter/class/fichinter.class.php';
946 } elseif ($modulepart ==
'shipment' || $modulepart ==
'expedition') {
947 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
949 } elseif ($modulepart ==
'adherent' || $modulepart ==
'member') {
950 $modulepart =
'adherent';
951 require_once DOL_DOCUMENT_ROOT.
'/adherents/class/adherent.class.php';
953 } elseif ($modulepart ==
'proposal' || $modulepart ==
'propal' || $modulepart ==
'propale') {
954 $modulepart =
'propale';
955 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
957 } elseif ($modulepart ==
'agenda' || $modulepart ==
'action' || $modulepart ==
'event') {
958 $modulepart =
'agenda';
959 require_once DOL_DOCUMENT_ROOT .
'/comm/action/class/actioncomm.class.php';
961 } elseif ($modulepart ==
'contact' || $modulepart ==
'socpeople') {
962 $modulepart =
'contact';
963 require_once DOL_DOCUMENT_ROOT.
'/contact/class/contact.class.php';
966 } elseif ($modulepart ==
'societe' || $modulepart ==
'company') {
967 $modulepart =
'societe';
968 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
971 } elseif ($modulepart ==
'knowledgemanagement') {
972 $modulepart =
'knowledgemanagement';
973 require_once DOL_DOCUMENT_ROOT.
'/knowledgemanagement/class/knowledgerecord.class.php';
976 } elseif ($modulepart ==
'ticket') {
977 $modulepart =
'ticket';
978 require_once DOL_DOCUMENT_ROOT.
'/ticket/class/ticket.class.php';
981 } elseif ($modulepart ==
'contrat' || $modulepart ==
'contract') {
982 $modulepart =
'contrat';
983 require_once DOL_DOCUMENT_ROOT .
'/contrat/class/contrat.class.php';
985 } elseif ($modulepart ==
'mrp') {
987 require_once DOL_DOCUMENT_ROOT .
'/mrp/class/mo.class.php';
989 } elseif ($modulepart ==
'stock') {
990 $modulepart =
'stock';
991 require_once DOL_DOCUMENT_ROOT .
'/product/stock/class/entrepot.class.php';
993 } elseif ($modulepart ==
'ecm') {
994 throw new RestException(500,
'Using a non empty "ref" is not compatible with using modulepart = '.$modulepart);
997 throw new RestException(500,
'Modulepart '.$modulepart.
' not implemented yet.');
1003 $result =
$object->fetch((
int) $ref);
1005 $result =
$object->fetch(0, $ref);
1009 throw new RestException(404,
"Object with ref '".$ref.
"' was not found.");
1010 } elseif ($result < 0) {
1011 throw new RestException(500,
'Error while fetching object: '.
$object->errorsToString());
1015 throw new RestException(404,
'The object '.$modulepart.
" with ref '".$ref.
"' was not found.");
1020 if ($modulepart ==
'supplier_invoice') {
1026 if ($modulepart ==
'societe') {
1027 $relativefile = $tmpreldir.dol_sanitizeFileName((
string)
$object->id);
1029 $relativefile = $tmpreldir.dol_sanitizeFileName((
string)
$object->ref);
1032 if (empty($tmp[
'accessallowed'])) {
1033 throw new RestException(403,
'Access not allowed to upload file into this directory');
1037 throw new RestException(403,
'Access not allowed to the object of this document');
1039 $upload_dir = $tmp[
'original_file'];
1047 if (empty($upload_dir) || $upload_dir ==
'/') {
1048 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.');
1051 if ($modulepart ==
'invoice') {
1052 $modulepart =
'facture';
1054 if ($modulepart ==
'member') {
1055 $modulepart =
'adherent';
1059 if ($modulepart !=
'ecm') {
1060 $relativefile = $subdir;
1062 if (empty($tmp[
'accessallowed'])) {
1063 throw new RestException(403,
'Access not allowed to upload file into this directory');
1067 throw new RestException(403,
'Access not allowed to the object of this document');
1069 $upload_dir = $tmp[
'original_file'];
1071 if (!DolibarrApiAccess::$user->hasRight(
'ecm',
'upload')) {
1072 throw new RestException(403,
'Missing permission to upload files in ECM module');
1075 throw new RestException(400,
'Module ECM is not enabled');
1077 $relativefile = $subdir;
1078 if (preg_match(
'/\.\./', $subdir) || preg_match(
'/[<>|]/', $subdir)) {
1079 throw new RestException(400,
'Bad value for parameter subdir');
1082 $upload_dir =
$conf->ecm->dir_output.($subdir !==
'' ?
'/'.$subdir :
'');
1085 if (empty($upload_dir) || $upload_dir ==
'/') {
1086 if (!empty($tmp[
'error'])) {
1087 throw new RestException(403,
'Error returned by dol_check_secure_access_document: '.$tmp[
'error']);
1089 throw new RestException(400,
'This value of modulepart ('.$modulepart.
') is not allowed with this value of subdir ('.$relativefile.
')');
1097 if (!empty($createdirifnotexists)) {
1099 throw new RestException(500,
'Error while trying to create directory '.$upload_dir);
1103 $destfile = $upload_dir.
'/'.$original_file;
1104 $destfiletmp = DOL_DATA_ROOT.
'/admin/temp/'.$original_file;
1109 throw new RestException(400,
'Directory does not exists : '.dirname($destfile));
1112 if (!$overwriteifexists &&
dol_is_file($destfile)) {
1113 throw new RestException(400,
"File with name '".$original_file.
"' already exists.");
1121 $fhandle = @fopen($destfiletmp,
'w');
1123 $nbofbyteswrote = fwrite($fhandle, $newfilecontent);
1127 throw new RestException(500,
"Failed to open file '".$destfiletmp.
"' for write");
1130 $disablevirusscan = 0;
1131 $src_file = $destfiletmp;
1132 $dest_file = $destfile;
1136 if (empty($disablevirusscan) && file_exists($src_file)) {
1138 if (count($checkvirusarray)) {
1139 dol_syslog(
'Files.lib::dol_move_uploaded_file File "'.$src_file.
'" (target name "'.$dest_file.
'") KO with antivirus: errors='.implode(
',', $checkvirusarray), LOG_WARNING);
1141 $errorkey = array_keys($checkvirusarray)[0];
1142 $langs->load(
'errors');
1143 if (in_array($errorkey, array(
'ErrorFileIsInfectedKnownVirus',
'ErrorAntivirusNotAvailable',
'ErrorFileIsInfectedWithAVirus'))) {
1144 $restmsg = $langs->trans($errorkey);
1146 $restmsg = $langs->trans(
'ErrorFileIsInfectedWithAVirus');
1148 throw new RestException(500, $restmsg .
': ' . implode(
',', $checkvirusarray));
1157 $publicmediasdirwithslash =
$conf->medias->multidir_output[
$conf->entity];
1158 if (!preg_match(
'/\/$/', $publicmediasdirwithslash)) {
1159 $publicmediasdirwithslash .=
'/';
1162 if (strpos($upload_dir, $publicmediasdirwithslash) !== 0 || !
getDolGlobalInt(
"MAIN_DOCUMENT_DISABLE_NOEXE_IN_MEDIAS_DIR")) {
1163 $dest_file .=
'.noexe';
1169 if (preg_match(
'/^\./', basename($src_file)) || preg_match(
'/\.\./', $src_file) || preg_match(
'/[<>|]/', $src_file)) {
1170 dol_syslog(
"Refused to deliver file ".$src_file, LOG_WARNING);
1171 throw new RestException(500,
"Refused to deliver file ".$src_file);
1176 if (preg_match(
'/^\./', basename($dest_file)) || preg_match(
'/\.\./', $dest_file) || preg_match(
'/[<>|]/', $dest_file)) {
1177 dol_syslog(
"Refused to deliver file ".$dest_file, LOG_WARNING);
1178 throw new RestException(500,
"Refused to deliver file ".$dest_file);
1181 $moreinfo = array(
'note_private' =>
'File uploaded using API /documents from IP '.
getUserRemoteIP());
1184 $moreinfo[
'src_object_type'] =
$object->table_element;
1185 $moreinfo[
'src_object_id'] =
$object->id;
1187 if (!empty($array_options)) {
1188 $moreinfo = array_merge($moreinfo, [
"array_options" => $array_options]);
1190 if (!empty($position)) {
1191 $moreinfo = array_merge($moreinfo, [
"position" => $position]);
1193 if (!empty($cover)) {
1194 $moreinfo = array_merge($moreinfo, [
"cover" => $cover]);
1196 if (!empty($share)) {
1197 require_once DOL_DOCUMENT_ROOT.
'/core/lib/security2.lib.php';
1200 $moreinfo[
'gen_or_uploaded'] =
'api';
1203 $result =
dol_move($destfiletmp, $dest_file,
'0', $overwriteifexists, 1, 1, $moreinfo);
1205 throw new RestException(500,
"Failed to move file into '".$dest_file.
"'");
1208 if (is_object(
$object) && $generateThumbs) {
1209 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1210 require_once DOL_DOCUMENT_ROOT.
'/core/lib/images.lib.php';
1212 $object->addThumbs($dest_file);
1238 public function delete($modulepart, $original_file)
1242 if (empty($modulepart)) {
1243 throw new RestException(400,
'bad value for parameter modulepart');
1245 if (empty($original_file)) {
1246 throw new RestException(400,
'bad value for parameter original_file');
1250 if ($modulepart ==
'task') {
1251 $modulepart =
'project_task';
1255 $entity =
$conf->entity;
1266 $relativefile = $original_file;
1269 $accessallowed = $check_access[
'accessallowed'];
1270 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
1271 $original_file = $check_access[
'original_file'];
1273 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
1274 throw new RestException(403);
1276 if (!$accessallowed) {
1277 throw new RestException(403);
1281 throw new RestException(403,
'Access not allowed to the object of this document');
1284 if (DolibarrApiAccess::$user->socid > 0) {
1285 if ($sqlprotectagainstexternals) {
1286 $resql = $this->db->query($sqlprotectagainstexternals);
1288 $num = $this->db->num_rows($resql);
1291 $obj = $this->db->fetch_object($resql);
1292 if (DolibarrApiAccess::$user->socid != $obj->fk_soc) {
1293 throw new RestException(403,
'Not allowed to download documents with such a ref');
1301 $filename = basename($original_file);
1302 $original_file_osencoded =
dol_osencode($original_file);
1304 if (!file_exists($original_file_osencoded)) {
1305 dol_syslog(
"Try to download not found file ".$original_file_osencoded, LOG_WARNING);
1306 throw new RestException(404,
'File not found');
1309 if (@unlink($original_file_osencoded)) {
1313 'message' =>
'Document deleted'
1318 throw new RestException(403);
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
Class to manage agenda events (actions)
Class to manage members of a foundation.
Class to manage predefined suppliers products.
Class to manage customers orders.
API class to receive files.
post($filename, $modulepart, $ref='', $subdir='', $filecontent='', $fileencoding='', $overwriteifexists=0, $createdirifnotexists=1, $position=0, $cover='', $array_options=[], $generateThumbs=0, $share=0)
Return a document.
__construct()
Constructor.
getDocumentsListByElement($modulepart, $id=0, $ref='', $sortfield='', $sortorder='', $limit=100, $page=0, $content_type='', $pagination_data=false)
List all documents of an element.
index($modulepart, $original_file='')
Download a given document.
getDocumentsListByDirectory($modulepart, $subdir='', $recursive=0, $search='', $sortfield='name', $sortorder='asc', $limit=100, $page=0, $pagination_data=false)
Return the list of files and subdirectories of a directory of a documents area not linked to an objec...
builddoc($modulepart, $original_file='', $doctemplate='', $langcode='')
Build a document.
Class to manage ECM files.
Class to manage warehouses.
Class to manage Trips and Expenses.
Class to manage suppliers invoices.
Class to manage invoices.
Class of the module paid holiday.
Class for KnowledgeRecord.
Class to manage products or services.
Class to manage projects.
Class to manage proposals.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage translations.
dol_move($srcfile, $destfile, $newmask='0', $overwriteifexists=1, $testvirus=0, $indexdatabase=1, $moreinfo=array(), $entity=null)
Move a file into another name.
dol_basename($pathfile)
Make a basename working with all page code (default PHP basenamed fails with cyrillic).
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1, $nolog=0)
Remove a file or several files with a mask.
dol_check_secure_access_document($modulepart, $original_file, $entity, $fuser=null, $refname='', $mode='read')
Security check when accessing to a document (used by document.php, viewimage.php and webservices to g...
dol_is_file($pathoffile)
Return if path is a file.
dolCheckVirus($src_file, $dest_file='')
Check virus into a file.
dol_dir_list($utf8_path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
dol_is_dir($folder)
Test if filename is a directory.
dol_mimetype($file, $default='application/octet-stream', $mode=0)
Return MIME type of a file from its name with extension.
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
dol_sanitizePathName($str, $newstr='_', $unaccent=0, $allowdash=0)
Clean a string to use it as a path name.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
dolChmod($filepath, $newmask='')
Change mod of a file.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
getMultidirOutput($object, $module='', $forobject=0, $mode='output')
Return the full path of the directory where a module (or an object of a module) stores its files.
getUserRemoteIP($trusted=0)
Return the real IP of remote user.
isAFileWithExecutableContent($filename)
Return if a file can contains executable content.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart='')
Return a path to have a the directory according to object where files are stored.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
image_format_supported($file, $acceptsvg=0)
Return if a filename is file name of a supported image format.
Class to generate the form for creating a new ticket.
getRandomPassword($generic=false, $replaceambiguouschars=null, $length=32)
Return a generated password using default module.
checkUserAccessToObject($user, array $featuresarray, $object=0, $tableandshare='', $feature2='', $dbt_keyfield='', $dbt_select='rowid', $parenttableforentity='')
Check that access by a given user to an object is ok.
checkUserAccessToDocumentObject(User $user, $modulepart, $original_file, $mode='read')
Check that a user can access the object a document is attached to.