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');
476 $object = fetchObjectByElement(
$id, $modulepart, $ref);
478 throw new RestException(404,
'Module for modulepart = '.$modulepart.
" is not enabled (or not yet supported by API");
487 throw new RestException(403,
'Access not allowed to this object (refused by checkUserAccessToObject)');
491 if ($modulepart ==
'societe' || $modulepart ==
'thirdparty') {
492 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
493 throw new RestException(403);
495 } elseif ($modulepart ==
'user') {
497 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'lire') && DolibarrApiAccess::$user->
id !=
$id) {
498 throw new RestException(403);
500 } elseif ($modulepart ==
'adherent' || $modulepart ==
'member') {
501 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
502 throw new RestException(403);
504 } elseif ($modulepart ==
'propal' || $modulepart ==
'proposal') {
505 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
506 throw new RestException(403);
508 } elseif ($modulepart ==
'supplier_proposal') {
509 if (!DolibarrApiAccess::$user->hasRight(
'supplier_proposal',
'read')) {
510 throw new RestException(403);
512 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
513 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
514 throw new RestException(403);
516 } elseif ($modulepart ==
'commande_fournisseur' || $modulepart ==
'supplier_order') {
517 $modulepart =
'supplier_order';
518 if (!DolibarrApiAccess::$user->hasRight(
'fournisseur',
'commande',
'lire') && !DolibarrApiAccess::$user->hasRight(
'supplier_order',
'lire')) {
519 throw new RestException(403);
521 } elseif ($modulepart ==
'shipment' || $modulepart ==
'expedition') {
522 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'lire')) {
523 throw new RestException(403);
525 } elseif ($modulepart ==
'facture' || $modulepart ==
'invoice') {
526 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
527 throw new RestException(403);
529 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'supplier_invoice') {
530 $modulepart =
'supplier_invoice';
531 if (!DolibarrApiAccess::$user->hasRight(
'fournisseur',
'facture',
'lire') && !DolibarrApiAccess::$user->hasRight(
'supplier_invoice',
'lire')) {
532 throw new RestException(403);
534 } elseif ($modulepart ==
'produit' || $modulepart ==
'product' || $modulepart ==
'service') {
535 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'lire')) {
536 throw new RestException(403);
538 } elseif ($modulepart ==
'agenda' || $modulepart ==
'action' || $modulepart ==
'event' || $modulepart ==
'actioncomm') {
539 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'myactions',
'read') && !DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'read')) {
540 throw new RestException(403);
542 } elseif ($modulepart ==
'expensereport') {
543 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'read')) {
544 throw new RestException(403);
546 } elseif ($modulepart ==
'holiday') {
547 if (!DolibarrApiAccess::$user->hasRight(
'holiday',
'read')) {
548 throw new RestException(403);
550 } elseif ($modulepart ==
'ticket') {
551 if (!DolibarrApiAccess::$user->hasRight(
'ticket',
'read')) {
552 throw new RestException(403);
554 } elseif ($modulepart ==
'knowledgemanagement') {
555 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read')) {
556 throw new RestException(403);
558 } elseif ($modulepart ==
'categorie' || $modulepart ==
'category') {
559 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
560 throw new RestException(403);
562 } elseif ($modulepart ==
'ecm') {
563 throw new RestException(500,
'Modulepart Ecm not implemented yet.');
567 } elseif ($modulepart ==
'contrat' || $modulepart ==
'contract') {
568 $modulepart =
'contrat';
569 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'lire')) {
570 throw new RestException(403);
572 } elseif ($modulepart ==
'intervention' || $modulepart ==
'ficheinter') {
573 $modulepart =
'ficheinter';
574 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'lire')) {
575 throw new RestException(403);
577 } elseif ($modulepart ==
'projet' || $modulepart ==
'project') {
578 $modulepart =
'project';
579 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
580 throw new RestException(403);
582 } elseif ($modulepart ==
'task' || $modulepart ==
'project_task') {
583 $modulepart =
'project_task';
584 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
585 throw new RestException(403);
587 } elseif ($modulepart ==
'mrp') {
589 if (!DolibarrApiAccess::$user->hasRight(
'mrp',
'read')) {
590 throw new RestException(403);
592 } elseif ($modulepart ==
'contact' || $modulepart ==
'socpeople') {
593 $modulepart =
'contact';
594 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'lire')) {
595 throw new RestException(403);
597 } elseif ($modulepart ==
'stock') {
598 if (!DolibarrApiAccess::$user->hasRight(
'stock',
'lire')) {
599 throw new RestException(403);
602 throw new RestException(500,
'Modulepart '.$modulepart.
' not implemented yet.');
609 $objectType = $modulepart;
611 $objectType =
$object->table_element;
614 $filearray =
dol_dir_list($upload_dir, $type, $recursive,
'',
'(\.meta|_preview.*\.png)$', $sortfield, (strtolower($sortorder) ==
'desc' ? SORT_DESC : SORT_ASC), 1);
616 $countarray = is_array($filearray) ? count($filearray) : 0;
618 if (empty($filearray)) {
619 throw new RestException(404,
'Search for modulepart '.$modulepart.
' with Id '.
$id.(!empty($ref) ?
' or Ref '.$ref :
'').
' does not return any document.');
621 $filearray = array_slice($filearray, $limit * $page, $limit);
622 if ((
$object->id) > 0 && !empty($modulepart)) {
623 require_once DOL_DOCUMENT_ROOT.
'/ecm/class/ecmfiles.class.php';
625 $result = $ecmfile->fetchAll(
'',
'', 0, 0, array(
't.src_object_type' => $objectType,
't.src_object_id' =>
$object->id));
627 throw new RestException(503,
'Error when retrieve ecm list : '.$this->db->lasterror());
628 } elseif (is_array($ecmfile->lines) && count($ecmfile->lines) > 0) {
629 foreach ($filearray as &$fileitem) {
630 foreach ($ecmfile->lines as $line) {
631 if ($fileitem[
'name'] == $line->filename) {
634 $fileitem[
'ref'] = $line->ref;
635 $fileitem[
'label'] = $line->label;
636 $fileitem[
'filepath'] = $line->filepath;
637 $fileitem[
'filename'] = $line->filename;
638 $fileitem[
'fullpath_orig'] = $line->fullpath_orig;
639 $fileitem[
'position'] = $line->position;
640 $fileitem[
'gen_or_uploaded'] = $line->gen_or_uploaded;
641 $fileitem[
'description'] = $line->desc;
642 $fileitem[
'keywords'] = $line->keywords;
643 $fileitem[
'cover'] = $line->cover;
644 $fileitem[
'share'] = $line->share;
645 $fileitem[
'date_c'] = $line->date_c;
646 $fileitem[
'agenda_id'] = $line->agenda_id;
647 $fileitem[
'fk_user_c'] = $line->fk_user_c;
648 $fileitem[
'fk_user_m'] = $line->fk_user_m;
649 $fileitem[
'note_private'] = $line->note_private;
650 $fileitem[
'note_public'] = $line->note_public;
653 if (isset($fileitem[
'relativename'])) {
654 $fileitem[
'content-type'] =
dol_mimetype((
string) $fileitem[
'relativename']);
659 $arraycontenttype = explode(
",", $content_type);
660 if (!empty($content_type)) {
661 $filearray = array_filter(
667 static function ($fileitem) use (&$arraycontenttype) {
668 return in_array(($fileitem[
'content-type'] ?:
'UNKNOWN'), $arraycontenttype);
677 foreach ($filearray as $tmpkey => $tmpval) {
678 unset($filearray[$tmpkey][
'path']);
679 unset($filearray[$tmpkey][
'fullname']);
683 if ($pagination_data) {
685 'data' => $filearray,
687 'total' => (int) $countarray,
689 'page_count' => (
int) ceil((
int) $countarray / $limit),
749 public function post($filename, $modulepart, $ref =
'', $subdir =
'', $filecontent =
'', $fileencoding =
'', $overwriteifexists = 0, $createdirifnotexists = 1, $position = 0, $cover =
'', $array_options = [], $generateThumbs = 0, $share = 0)
753 $modulepartorig = $modulepart;
755 if (empty($modulepart)) {
756 throw new RestException(400,
'Modulepart not provided.');
759 $newfilecontent =
'';
760 if (empty($fileencoding)) {
761 $newfilecontent = $filecontent;
763 if ($fileencoding ==
'base64') {
764 $newfilecontent = base64_decode($filecontent);
768 $relativefile =
'UNSET';
772 $entity = DolibarrApiAccess::$user->entity;
773 if (empty($entity)) {
781 if ($modulepart ==
'facture' || $modulepart ==
'invoice') {
782 $modulepart =
'facture';
784 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
786 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'supplier_invoice') {
787 $modulepart =
'supplier_invoice';
789 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
791 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
792 $modulepart =
'commande';
794 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
796 } elseif ($modulepart ==
'commande_fournisseur' || $modulepart ==
'supplier_order') {
797 $modulepart =
'supplier_order';
799 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.class.php';
801 } elseif ($modulepart ==
'projet' || $modulepart ==
'project') {
802 require_once DOL_DOCUMENT_ROOT.
'/projet/class/project.class.php';
804 } elseif ($modulepart ==
'task' || $modulepart ==
'project_task') {
805 $modulepart =
'project_task';
807 require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
810 $task_result =
$object->fetch(0, $ref);
813 if ($task_result > 0) {
814 $project_result =
$object->fetchProject();
816 if ($project_result >= 0) {
820 throw new RestException(500,
'Error while fetching Task '.$ref);
822 } elseif ($modulepart ==
'product' || $modulepart ==
'produit' || $modulepart ==
'service' || $modulepart ==
'produit|service') {
823 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
825 } elseif ($modulepart ==
'expensereport') {
826 require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport.class.php';
828 } elseif ($modulepart ==
'holiday') {
829 require_once DOL_DOCUMENT_ROOT.
'/holiday/class/holiday.class.php';
831 } elseif ($modulepart ==
'ficheinter' || $modulepart ==
'intervention') {
832 require_once DOL_DOCUMENT_ROOT.
'/fichinter/class/fichinter.class.php';
834 } elseif ($modulepart ==
'shipment' || $modulepart ==
'expedition') {
835 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
837 } elseif ($modulepart ==
'adherent' || $modulepart ==
'member') {
838 $modulepart =
'adherent';
839 require_once DOL_DOCUMENT_ROOT.
'/adherents/class/adherent.class.php';
841 } elseif ($modulepart ==
'proposal' || $modulepart ==
'propal' || $modulepart ==
'propale') {
842 $modulepart =
'propale';
843 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
845 } elseif ($modulepart ==
'agenda' || $modulepart ==
'action' || $modulepart ==
'event') {
846 $modulepart =
'agenda';
847 require_once DOL_DOCUMENT_ROOT .
'/comm/action/class/actioncomm.class.php';
849 } elseif ($modulepart ==
'contact' || $modulepart ==
'socpeople') {
850 $modulepart =
'contact';
851 require_once DOL_DOCUMENT_ROOT.
'/contact/class/contact.class.php';
854 } elseif ($modulepart ==
'societe' || $modulepart ==
'company') {
855 $modulepart =
'societe';
856 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
859 } elseif ($modulepart ==
'knowledgemanagement') {
860 $modulepart =
'knowledgemanagement';
861 require_once DOL_DOCUMENT_ROOT.
'/knowledgemanagement/class/knowledgerecord.class.php';
864 } elseif ($modulepart ==
'ticket') {
865 $modulepart =
'ticket';
866 require_once DOL_DOCUMENT_ROOT.
'/ticket/class/ticket.class.php';
869 } elseif ($modulepart ==
'contrat' || $modulepart ==
'contract') {
870 $modulepart =
'contrat';
871 require_once DOL_DOCUMENT_ROOT .
'/contrat/class/contrat.class.php';
873 } elseif ($modulepart ==
'mrp') {
875 require_once DOL_DOCUMENT_ROOT .
'/mrp/class/mo.class.php';
877 } elseif ($modulepart ==
'stock') {
878 $modulepart =
'stock';
879 require_once DOL_DOCUMENT_ROOT .
'/product/stock/class/entrepot.class.php';
881 } elseif ($modulepart ==
'ecm') {
882 throw new RestException(500,
'Using a non empty "ref" is not compatible with using modulepart = '.$modulepart);
885 throw new RestException(500,
'Modulepart '.$modulepart.
' not implemented yet.');
891 $result =
$object->fetch((
int) $ref);
893 $result =
$object->fetch(0, $ref);
897 throw new RestException(404,
"Object with ref '".$ref.
"' was not found.");
898 } elseif ($result < 0) {
899 throw new RestException(500,
'Error while fetching object: '.
$object->error);
903 throw new RestException(404,
'The object '.$modulepart.
" with ref '".$ref.
"' was not found.");
908 if ($modulepart ==
'supplier_invoice') {
914 if ($modulepart ==
'societe') {
915 $relativefile = $tmpreldir.dol_sanitizeFileName((
string)
$object->id);
917 $relativefile = $tmpreldir.dol_sanitizeFileName((
string)
$object->ref);
920 if (empty($tmp[
'accessallowed'])) {
921 throw new RestException(403,
'Access not allowed to upload file into this directory');
923 $upload_dir = $tmp[
'original_file'];
931 if (empty($upload_dir) || $upload_dir ==
'/') {
932 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.');
935 if ($modulepart ==
'invoice') {
936 $modulepart =
'facture';
938 if ($modulepart ==
'member') {
939 $modulepart =
'adherent';
943 if ($modulepart !=
'ecm') {
944 $relativefile = $subdir;
946 if (empty($tmp[
'accessallowed'])) {
947 throw new RestException(403,
'Access not allowed to upload file into this directory');
949 $upload_dir = $tmp[
'original_file'];
951 if (!DolibarrApiAccess::$user->hasRight(
'ecm',
'upload')) {
952 throw new RestException(403,
'Missing permission to upload files in ECM module');
954 $upload_dir =
$conf->medias->multidir_output[
$conf->entity];
957 if (empty($upload_dir) || $upload_dir ==
'/') {
958 if (!empty($tmp[
'error'])) {
959 throw new RestException(403,
'Error returned by dol_check_secure_access_document: '.$tmp[
'error']);
961 throw new RestException(400,
'This value of modulepart ('.$modulepart.
') is not allowed with this value of subdir ('.$relativefile.
')');
969 if (!empty($createdirifnotexists)) {
971 throw new RestException(500,
'Error while trying to create directory '.$upload_dir);
975 $destfile = $upload_dir.
'/'.$original_file;
976 $destfiletmp = DOL_DATA_ROOT.
'/admin/temp/'.$original_file;
981 throw new RestException(400,
'Directory does not exists : '.dirname($destfile));
984 if (!$overwriteifexists &&
dol_is_file($destfile)) {
985 throw new RestException(400,
"File with name '".$original_file.
"' already exists.");
993 $fhandle = @fopen($destfiletmp,
'w');
995 $nbofbyteswrote = fwrite($fhandle, $newfilecontent);
999 throw new RestException(500,
"Failed to open file '".$destfiletmp.
"' for write");
1002 $disablevirusscan = 0;
1003 $src_file = $destfiletmp;
1004 $dest_file = $destfile;
1008 if (empty($disablevirusscan) && file_exists($src_file)) {
1010 if (count($checkvirusarray)) {
1011 dol_syslog(
'Files.lib::dol_move_uploaded_file File "'.$src_file.
'" (target name "'.$dest_file.
'") KO with antivirus: errors='.implode(
',', $checkvirusarray), LOG_WARNING);
1012 throw new RestException(500,
'ErrorFileIsInfectedWithAVirus: '.implode(
',', $checkvirusarray));
1021 $publicmediasdirwithslash =
$conf->medias->multidir_output[
$conf->entity];
1022 if (!preg_match(
'/\/$/', $publicmediasdirwithslash)) {
1023 $publicmediasdirwithslash .=
'/';
1026 if (strpos($upload_dir, $publicmediasdirwithslash) !== 0 || !
getDolGlobalInt(
"MAIN_DOCUMENT_DISABLE_NOEXE_IN_MEDIAS_DIR")) {
1027 $dest_file .=
'.noexe';
1033 if (preg_match(
'/^\./', basename($src_file)) || preg_match(
'/\.\./', $src_file) || preg_match(
'/[<>|]/', $src_file)) {
1034 dol_syslog(
"Refused to deliver file ".$src_file, LOG_WARNING);
1035 throw new RestException(500,
"Refused to deliver file ".$src_file);
1040 if (preg_match(
'/^\./', basename($dest_file)) || preg_match(
'/\.\./', $dest_file) || preg_match(
'/[<>|]/', $dest_file)) {
1041 dol_syslog(
"Refused to deliver file ".$dest_file, LOG_WARNING);
1042 throw new RestException(500,
"Refused to deliver file ".$dest_file);
1045 $moreinfo = array(
'note_private' =>
'File uploaded using API /documents from IP '.
getUserRemoteIP());
1048 $moreinfo[
'src_object_type'] =
$object->table_element;
1049 $moreinfo[
'src_object_id'] =
$object->id;
1051 if (!empty($array_options)) {
1052 $moreinfo = array_merge($moreinfo, [
"array_options" => $array_options]);
1054 if (!empty($position)) {
1055 $moreinfo = array_merge($moreinfo, [
"position" => $position]);
1057 if (!empty($cover)) {
1058 $moreinfo = array_merge($moreinfo, [
"cover" => $cover]);
1060 if (!empty($share)) {
1061 require_once DOL_DOCUMENT_ROOT.
'/core/lib/security2.lib.php';
1064 $moreinfo[
'gen_or_uploaded'] =
'api';
1067 $result =
dol_move($destfiletmp, $dest_file,
'0', $overwriteifexists, 1, 1, $moreinfo);
1069 throw new RestException(500,
"Failed to move file into '".$dest_file.
"'");
1072 if (is_object(
$object) && $generateThumbs) {
1073 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1074 require_once DOL_DOCUMENT_ROOT.
'/core/lib/images.lib.php';
1076 $object->addThumbs($dest_file);
1102 public function delete($modulepart, $original_file)
1106 if (empty($modulepart)) {
1107 throw new RestException(400,
'bad value for parameter modulepart');
1109 if (empty($original_file)) {
1110 throw new RestException(400,
'bad value for parameter original_file');
1114 if ($modulepart ==
'task') {
1115 $modulepart =
'project_task';
1119 $entity =
$conf->entity;
1130 $relativefile = $original_file;
1133 $accessallowed = $check_access[
'accessallowed'];
1134 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
1135 $original_file = $check_access[
'original_file'];
1137 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
1138 throw new RestException(403);
1140 if (!$accessallowed) {
1141 throw new RestException(403);
1144 if (DolibarrApiAccess::$user->socid > 0) {
1145 if ($sqlprotectagainstexternals) {
1146 $resql = $this->db->query($sqlprotectagainstexternals);
1148 $num = $this->db->num_rows($resql);
1151 $obj = $this->db->fetch_object($resql);
1152 if (DolibarrApiAccess::$user->socid != $obj->fk_soc) {
1153 throw new RestException(403,
'Not allowed to download documents with such a ref');
1161 $filename = basename($original_file);
1162 $original_file_osencoded =
dol_osencode($original_file);
1164 if (!file_exists($original_file_osencoded)) {
1165 dol_syslog(
"Try to download not found file ".$original_file_osencoded, LOG_WARNING);
1166 throw new RestException(404,
'File not found');
1169 if (@unlink($original_file_osencoded)) {
1173 'message' =>
'Document deleted'
1178 throw new RestException(403);