26use Luracast\Restler\RestException;
28require_once DOL_DOCUMENT_ROOT.
'/main.inc.php';
29require_once DOL_DOCUMENT_ROOT.
'/api/class/api.class.php';
30require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
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 $entity =
$conf->entity;
95 $relativefile = $original_file;
98 $accessallowed = $check_access[
'accessallowed'];
99 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
100 $original_file = $check_access[
'original_file'];
102 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
103 throw new RestException(403);
105 if (!$accessallowed) {
106 throw new RestException(403);
109 if (DolibarrApiAccess::$user->socid > 0) {
110 if ($sqlprotectagainstexternals) {
111 $resql = $this->db->query($sqlprotectagainstexternals);
113 $num = $this->db->num_rows($resql);
116 $obj = $this->db->fetch_object($resql);
117 if (DolibarrApiAccess::$user->socid != $obj->fk_soc) {
118 throw new RestException(403,
'Not allowed to download documents with such a ref');
126 $filename = basename($original_file);
127 $original_file_osencoded =
dol_osencode($original_file);
129 if (!file_exists($original_file_osencoded)) {
130 dol_syslog(
"Try to download not found file ".$original_file_osencoded, LOG_WARNING);
131 throw new RestException(404,
'File not found');
134 $file_content = file_get_contents($original_file_osencoded);
135 return array(
'filename' => $filename,
'content-type' =>
dol_mimetype($filename),
'filesize' => filesize($original_file),
'content' => base64_encode($file_content),
'encoding' =>
'base64');
167 public function builddoc($modulepart, $original_file =
'', $doctemplate =
'', $langcode =
'')
169 global
$conf, $langs;
171 if (empty($modulepart)) {
172 throw new RestException(400,
'bad value for parameter modulepart');
174 if (empty($original_file)) {
175 throw new RestException(400,
'bad value for parameter original_file');
178 $outputlangs = $langs;
179 if ($langcode && $langs->defaultlang != $langcode) {
181 $outputlangs->setDefaultLang($langcode);
185 $entity =
$conf->entity;
196 $relativefile = $original_file;
199 $accessallowed = $check_access[
'accessallowed'];
200 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
201 $original_file = $check_access[
'original_file'];
203 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
204 throw new RestException(403);
206 if (!$accessallowed) {
207 throw new RestException(403);
210 if (DolibarrApiAccess::$user->socid > 0) {
211 if ($sqlprotectagainstexternals) {
212 $resql = $this->db->query($sqlprotectagainstexternals);
214 $num = $this->db->num_rows($resql);
217 $obj = $this->db->fetch_object($resql);
218 if (DolibarrApiAccess::$user->socid != $obj->fk_soc) {
219 throw new RestException(403,
'Not allowed to download documents with such a ref');
234 if ($modulepart ==
'facture' || $modulepart ==
'invoice') {
235 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
236 $tmpobject =
new Facture($this->db);
237 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
239 throw new RestException(404,
'Invoice not found');
242 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
243 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
245 throw new RestException(500,
'Error generating document');
247 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'invoice_supplier') {
248 require_once DOL_DOCUMENT_ROOT .
'/fourn/class/fournisseur.facture.class.php';
250 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
252 throw new RestException(404,
'Supplier invoice not found');
255 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
256 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
258 throw new RestException(500,
'Error generating document');
260 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
261 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
262 $tmpobject =
new Commande($this->db);
263 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
265 throw new RestException(404,
'Order not found');
267 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
268 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
270 throw new RestException(500,
'Error generating document');
272 } elseif ($modulepart ==
'propal' || $modulepart ==
'proposal') {
273 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
274 $tmpobject =
new Propal($this->db);
275 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
277 throw new RestException(404,
'Proposal not found');
279 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
280 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
282 throw new RestException(500,
'Error generating document');
284 } elseif ($modulepart ==
'contrat' || $modulepart ==
'contract') {
285 require_once DOL_DOCUMENT_ROOT .
'/contrat/class/contrat.class.php';
287 $tmpobject =
new Contrat($this->db);
288 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
291 throw new RestException(404,
'Contract not found');
294 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
295 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
298 throw new RestException(500,
'Error generating document missing doctemplate parameter');
300 } elseif ($modulepart ==
'expedition' || $modulepart ==
'shipment') {
301 require_once DOL_DOCUMENT_ROOT .
'/expedition/class/expedition.class.php';
304 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
307 throw new RestException(404,
'Shipment 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 missing doctemplate parameter');
316 } elseif ($modulepart ==
'mrp') {
317 require_once DOL_DOCUMENT_ROOT .
'/mrp/class/mo.class.php';
319 $tmpobject =
new Mo($this->db);
320 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
323 throw new RestException(404,
'MO 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 missing doctemplate parameter');
333 throw new RestException(403,
'Generation not available for this modulepart');
336 $filename = basename($original_file);
337 $original_file_osencoded =
dol_osencode($original_file);
339 if (!file_exists($original_file_osencoded)) {
340 throw new RestException(404,
'File not found');
343 $file_content = file_get_contents($original_file_osencoded);
344 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');
376 public function getDocumentsListByElement($modulepart,
$id = 0, $ref =
'', $sortfield =
'', $sortorder =
'', $limit = 100, $page = 0, $content_type =
'', $pagination_data =
false)
381 if (empty($modulepart)) {
382 throw new RestException(400,
'bad value for parameter modulepart');
385 if (empty(
$id) && empty($ref)) {
386 throw new RestException(400,
'bad value for parameter id or ref');
393 if ($modulepart ==
'societe' || $modulepart ==
'thirdparty') {
394 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
396 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
397 throw new RestException(403);
403 throw new RestException(404,
'Thirdparty not found');
407 } elseif ($modulepart ==
'user') {
408 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
411 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'lire') && DolibarrApiAccess::$user->
id !=
$id) {
412 throw new RestException(403);
418 throw new RestException(404,
'User not found');
422 } elseif ($modulepart ==
'adherent' || $modulepart ==
'member') {
423 require_once DOL_DOCUMENT_ROOT.
'/adherents/class/adherent.class.php';
425 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
426 throw new RestException(403);
432 throw new RestException(404,
'Member not found');
436 } elseif ($modulepart ==
'propal' || $modulepart ==
'proposal') {
437 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
439 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
440 throw new RestException(403);
446 throw new RestException(404,
'Proposal not found');
450 } elseif ($modulepart ==
'supplier_proposal') {
451 require_once DOL_DOCUMENT_ROOT.
'/supplier_proposal/class/supplier_proposal.class.php';
453 if (!DolibarrApiAccess::$user->hasRight(
'supplier_proposal',
'read')) {
454 throw new RestException(403);
460 throw new RestException(404,
'Supplier proposal not found');
464 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
465 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
467 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
468 throw new RestException(403);
474 throw new RestException(404,
'Order not found');
478 } elseif ($modulepart ==
'commande_fournisseur' || $modulepart ==
'supplier_order') {
479 $modulepart =
'supplier_order';
481 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.class.php';
483 if (!DolibarrApiAccess::$user->hasRight(
'fournisseur',
'commande',
'lire') && !DolibarrApiAccess::$user->hasRight(
'supplier_order',
'lire')) {
484 throw new RestException(403);
490 throw new RestException(404,
'Purchase order not found');
494 } elseif ($modulepart ==
'shipment' || $modulepart ==
'expedition') {
495 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
497 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'lire')) {
498 throw new RestException(403);
504 throw new RestException(404,
'Shipment not found');
508 } elseif ($modulepart ==
'facture' || $modulepart ==
'invoice') {
509 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
511 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
512 throw new RestException(403);
518 throw new RestException(404,
'Invoice not found');
522 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'supplier_invoice') {
523 $modulepart =
'supplier_invoice';
525 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
527 if (!DolibarrApiAccess::$user->hasRight(
'fournisseur',
'facture',
'lire') && !DolibarrApiAccess::$user->hasRight(
'supplier_invoice',
'lire')) {
528 throw new RestException(403);
534 throw new RestException(404,
'Invoice not found');
538 } elseif ($modulepart ==
'produit' || $modulepart ==
'product') {
539 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
541 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'lire')) {
542 throw new RestException(403);
548 throw new RestException(404,
'Product not found');
549 } elseif ($result < 0) {
550 throw new RestException(500,
'Error while fetching object: '.
$object->error);
554 } elseif ($modulepart ==
'agenda' || $modulepart ==
'action' || $modulepart ==
'event') {
555 require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
557 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'myactions',
'read') && !DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'read')) {
558 throw new RestException(403);
564 throw new RestException(404,
'Event not found');
568 } elseif ($modulepart ==
'expensereport') {
569 require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport.class.php';
571 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'read')) {
572 throw new RestException(403);
578 throw new RestException(404,
'Expense report not found');
582 } elseif ($modulepart ==
'knowledgemanagement') {
583 require_once DOL_DOCUMENT_ROOT.
'/knowledgemanagement/class/knowledgerecord.class.php';
585 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read')) {
586 throw new RestException(403);
592 throw new RestException(404,
'KM article not found');
596 } elseif ($modulepart ==
'categorie' || $modulepart ==
'category') {
597 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
599 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
600 throw new RestException(403);
606 throw new RestException(404,
'Category not found');
610 } elseif ($modulepart ==
'ecm') {
611 throw new RestException(500,
'Modulepart Ecm not implemented yet.');
626 } elseif ($modulepart ==
'contrat' || $modulepart ==
'contract') {
627 $modulepart =
'contrat';
628 require_once DOL_DOCUMENT_ROOT .
'/contrat/class/contrat.class.php';
633 throw new RestException(404,
'Contract not found');
637 } elseif ($modulepart ==
'intervention' || $modulepart ==
'ficheinter') {
638 $modulepart =
'ficheinter';
639 require_once DOL_DOCUMENT_ROOT .
'/fichinter/class/fichinter.class.php';
644 throw new RestException(404,
'Interventional not found');
648 } elseif ($modulepart ==
'projet' || $modulepart ==
'project') {
649 $modulepart =
'project';
650 require_once DOL_DOCUMENT_ROOT .
'/projet/class/project.class.php';
655 throw new RestException(404,
'Project not found');
659 } elseif ($modulepart ==
'mrp') {
661 require_once DOL_DOCUMENT_ROOT .
'/mrp/class/mo.class.php';
666 throw new RestException(404,
'MO not found');
671 throw new RestException(500,
'Modulepart '.$modulepart.
' not implemented yet.');
674 $objectType = $modulepart;
676 $objectType =
$object->table_element;
679 $filearray =
dol_dir_list($upload_dir, $type, $recursive,
'',
'(\.meta|_preview.*\.png)$', $sortfield, (strtolower($sortorder) ==
'desc' ? SORT_DESC : SORT_ASC), 1);
680 $countarray = count($filearray);
681 $filearray = array_slice($filearray, $limit * $page, $limit);
682 if (empty($filearray)) {
683 throw new RestException(404,
'Search for modulepart '.$modulepart.
' with Id '.
$object->id.(!empty(
$object->ref) ?
' or Ref '.$object->ref :
'').
' does not return any document.');
685 if ((
$object->id) > 0 && !empty($modulepart)) {
686 require_once DOL_DOCUMENT_ROOT.
'/ecm/class/ecmfiles.class.php';
688 $result = $ecmfile->fetchAll(
'',
'', 0, 0, array(
't.src_object_type' => $objectType,
't.src_object_id' =>
$object->id));
690 throw new RestException(503,
'Error when retrieve ecm list : '.$this->db->lasterror());
691 } elseif (is_array($ecmfile->lines) && count($ecmfile->lines) > 0) {
692 $count = count($filearray);
693 for ($i = 0 ; $i < $count ; $i++) {
694 foreach ($ecmfile->lines as $line) {
696 if ($filearray[$i][
'name'] == $line->filename) {
698 $filearray[$i] = array_merge($filearray[$i], (array) $line);
701 if (isset($line->filename)) $filearray[$i][
'content-type'] =
dol_mimetype($line->filename);
702 $arraycontenttype = explode(
",", $content_type);
703 if (!empty($content_type) && isset($line->filename) && !in_array(
dol_mimetype($line->filename), $arraycontenttype)) {
704 unset($filearray[$i]);
713 if ($pagination_data) {
716 $filearray[
'data'] = $tmp;
717 $filearray[
'pagination'] = [
718 'total' => (int) $countarray,
720 'page_count' => ceil((
int) $countarray / $limit),
778 public function post($filename, $modulepart, $ref =
'', $subdir =
'', $filecontent =
'', $fileencoding =
'', $overwriteifexists = 0, $createdirifnotexists = 1, $position = 0, $cover =
'', $array_options = [], $generateThumbs = 0)
782 $modulepartorig = $modulepart;
784 if (empty($modulepart)) {
785 throw new RestException(400,
'Modulepart not provided.');
788 $newfilecontent =
'';
789 if (empty($fileencoding)) {
790 $newfilecontent = $filecontent;
792 if ($fileencoding ==
'base64') {
793 $newfilecontent = base64_decode($filecontent);
797 $relativefile =
'UNSET';
801 $entity = DolibarrApiAccess::$user->entity;
802 if (empty($entity)) {
810 if ($modulepart ==
'facture' || $modulepart ==
'invoice') {
811 $modulepart =
'facture';
813 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
815 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'supplier_invoice') {
816 $modulepart =
'supplier_invoice';
818 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
820 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
821 $modulepart =
'commande';
823 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
825 } elseif ($modulepart ==
'commande_fournisseur' || $modulepart ==
'supplier_order') {
826 $modulepart =
'supplier_order';
828 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.class.php';
830 } elseif ($modulepart ==
'projet' || $modulepart ==
'project') {
831 require_once DOL_DOCUMENT_ROOT.
'/projet/class/project.class.php';
833 } elseif ($modulepart ==
'task' || $modulepart ==
'project_task') {
834 $modulepart =
'project_task';
836 require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
839 $task_result =
$object->fetch(0, $ref);
842 if ($task_result > 0) {
843 $project_result =
$object->fetchProject();
845 if ($project_result >= 0) {
849 throw new RestException(500,
'Error while fetching Task '.$ref);
851 } elseif ($modulepart ==
'product' || $modulepart ==
'produit' || $modulepart ==
'service' || $modulepart ==
'produit|service') {
852 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
854 } elseif ($modulepart ==
'expensereport') {
855 require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport.class.php';
857 } elseif ($modulepart ==
'ficheinter' || $modulepart ==
'intervention') {
858 require_once DOL_DOCUMENT_ROOT.
'/fichinter/class/fichinter.class.php';
860 } elseif ($modulepart ==
'adherent' || $modulepart ==
'member') {
861 $modulepart =
'adherent';
862 require_once DOL_DOCUMENT_ROOT.
'/adherents/class/adherent.class.php';
864 } elseif ($modulepart ==
'proposal' || $modulepart ==
'propal' || $modulepart ==
'propale') {
865 $modulepart =
'propale';
866 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
868 } elseif ($modulepart ==
'agenda' || $modulepart ==
'action' || $modulepart ==
'event') {
869 $modulepart =
'agenda';
870 require_once DOL_DOCUMENT_ROOT .
'/comm/action/class/actioncomm.class.php';
872 } elseif ($modulepart ==
'contact' || $modulepart ==
'socpeople') {
873 $modulepart =
'contact';
874 require_once DOL_DOCUMENT_ROOT.
'/contact/class/contact.class.php';
877 } elseif ($modulepart ==
'contrat' || $modulepart ==
'contract') {
878 $modulepart =
'contrat';
879 require_once DOL_DOCUMENT_ROOT .
'/contrat/class/contrat.class.php';
881 } elseif ($modulepart ==
'mrp') {
883 require_once DOL_DOCUMENT_ROOT .
'/mrp/class/mo.class.php';
885 } elseif ($modulepart ==
'ecm') {
886 throw new RestException(500,
'Using a non empty "ref" is not compatible with using modulepart = '.$modulepart);
889 throw new RestException(500,
'Modulepart '.$modulepart.
' not implemented yet.');
895 $result =
$object->fetch((
int) $ref);
897 $result =
$object->fetch(0, $ref);
901 throw new RestException(404,
"Object with ref '".$ref.
"' was not found.");
902 } elseif ($result < 0) {
903 throw new RestException(500,
'Error while fetching object: '.
$object->error);
908 throw new RestException(404,
'The object '.$modulepart.
" with ref '".$ref.
"' was not found.");
913 if ($modulepart ==
'supplier_invoice') {
919 $relativefile = $tmpreldir.dol_sanitizeFileName(
$object->ref);
921 $upload_dir = $tmp[
'original_file'];
929 if (empty($upload_dir) || $upload_dir ==
'/') {
930 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.');
933 if ($modulepart ==
'invoice') {
934 $modulepart =
'facture';
936 if ($modulepart ==
'member') {
937 $modulepart =
'adherent';
941 if ($modulepart !=
'ecm') {
942 $relativefile = $subdir;
944 $upload_dir = $tmp[
'original_file'];
946 if (!DolibarrApiAccess::$user->hasRight(
'ecm',
'upload')) {
947 throw new RestException(403,
'Missing permission to upload files in ECM module');
949 $upload_dir =
$conf->medias->multidir_output[
$conf->entity];
952 if (empty($upload_dir) || $upload_dir ==
'/') {
953 if (!empty($tmp[
'error'])) {
954 throw new RestException(403,
'Error returned by dol_check_secure_access_document: '.$tmp[
'error']);
956 throw new RestException(400,
'This value of modulepart ('.$modulepart.
') is not allowed with this value of subdir ('.$relativefile.
')');
964 if (!empty($createdirifnotexists)) {
966 throw new RestException(500,
'Error while trying to create directory '.$upload_dir);
970 $destfile = $upload_dir.
'/'.$original_file;
971 $destfiletmp = DOL_DATA_ROOT.
'/admin/temp/'.$original_file;
976 throw new RestException(400,
'Directory does not exists : '.dirname($destfile));
979 if (!$overwriteifexists &&
dol_is_file($destfile)) {
980 throw new RestException(400,
"File with name '".$original_file.
"' already exists.");
988 $fhandle = @fopen($destfiletmp,
'w');
990 $nbofbyteswrote = fwrite($fhandle, $newfilecontent);
994 throw new RestException(500,
"Failed to open file '".$destfiletmp.
"' for write");
997 $disablevirusscan = 0;
998 $src_file = $destfiletmp;
999 $dest_file = $destfile;
1003 if (empty($disablevirusscan) && file_exists($src_file)) {
1005 if (count($checkvirusarray)) {
1006 dol_syslog(
'Files.lib::dol_move_uploaded_file File "'.$src_file.
'" (target name "'.$dest_file.
'") KO with antivirus: errors='.implode(
',', $checkvirusarray), LOG_WARNING);
1007 throw new RestException(500,
'ErrorFileIsInfectedWithAVirus: '.implode(
',', $checkvirusarray));
1016 $publicmediasdirwithslash =
$conf->medias->multidir_output[
$conf->entity];
1017 if (!preg_match(
'/\/$/', $publicmediasdirwithslash)) {
1018 $publicmediasdirwithslash .=
'/';
1021 if (strpos($upload_dir, $publicmediasdirwithslash) !== 0 || !
getDolGlobalInt(
"MAIN_DOCUMENT_DISABLE_NOEXE_IN_MEDIAS_DIR")) {
1022 $dest_file .=
'.noexe';
1028 if (preg_match(
'/^\./', basename($src_file)) || preg_match(
'/\.\./', $src_file) || preg_match(
'/[<>|]/', $src_file)) {
1029 dol_syslog(
"Refused to deliver file ".$src_file, LOG_WARNING);
1030 throw new RestException(500,
"Refused to deliver file ".$src_file);
1035 if (preg_match(
'/^\./', basename($dest_file)) || preg_match(
'/\.\./', $dest_file) || preg_match(
'/[<>|]/', $dest_file)) {
1036 dol_syslog(
"Refused to deliver file ".$dest_file, LOG_WARNING);
1037 throw new RestException(500,
"Refused to deliver file ".$dest_file);
1040 $moreinfo = array(
'note_private' =>
'File uploaded using API /documents from IP '.
getUserRemoteIP());
1042 $moreinfo[
'src_object_type'] =
$object->table_element;
1043 $moreinfo[
'src_object_id'] =
$object->id;
1045 if (!empty($array_options)) {
1046 $moreinfo = array_merge($moreinfo, [
"array_options" => $array_options]);
1048 if (!empty($position)) {
1049 $moreinfo = array_merge($moreinfo, [
"position" => $position]);
1051 if (!empty($cover)) {
1052 $moreinfo = array_merge($moreinfo, [
"cover" => $cover]);
1054 $moreinfo[
'gen_or_uploaded'] =
'api';
1057 $result =
dol_move($destfiletmp, $dest_file,
'0', $overwriteifexists, 1, 1, $moreinfo);
1059 throw new RestException(500,
"Failed to move file into '".$dest_file.
"'");
1062 if (is_object(
$object) && $generateThumbs) {
1063 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1065 $object->addThumbs($dest_file);
1091 public function delete($modulepart, $original_file)
1095 if (empty($modulepart)) {
1096 throw new RestException(400,
'bad value for parameter modulepart');
1098 if (empty($original_file)) {
1099 throw new RestException(400,
'bad value for parameter original_file');
1103 $entity =
$conf->entity;
1114 $relativefile = $original_file;
1117 $accessallowed = $check_access[
'accessallowed'];
1118 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
1119 $original_file = $check_access[
'original_file'];
1121 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
1122 throw new RestException(403);
1124 if (!$accessallowed) {
1125 throw new RestException(403);
1128 if (DolibarrApiAccess::$user->socid > 0) {
1129 if ($sqlprotectagainstexternals) {
1130 $resql = $this->db->query($sqlprotectagainstexternals);
1132 $num = $this->db->num_rows($resql);
1135 $obj = $this->db->fetch_object($resql);
1136 if (DolibarrApiAccess::$user->socid != $obj->fk_soc) {
1137 throw new RestException(403,
'Not allowed to download documents with such a ref');
1145 $filename = basename($original_file);
1146 $original_file_osencoded =
dol_osencode($original_file);
1148 if (!file_exists($original_file_osencoded)) {
1149 dol_syslog(
"Try to download not found file ".$original_file_osencoded, LOG_WARNING);
1150 throw new RestException(404,
'File not found');
1153 if (@unlink($original_file_osencoded)) {
1157 'message' =>
'Document deleted'
1162 throw new RestException(403);
$id
Support class for third parties, contacts, members, users or resources.
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Class to manage agenda events (actions)
Class to manage members of a foundation.
Class to manage categories.
Class to manage predefined suppliers products.
Class to manage customers orders.
API class for receive files.
__construct()
Constructor.
index($modulepart, $original_file='')
Download a document.
builddoc($modulepart, $original_file='', $doctemplate='', $langcode='')
Build a document.
post($filename, $modulepart, $ref='', $subdir='', $filecontent='', $fileencoding='', $overwriteifexists=0, $createdirifnotexists=1, $position=0, $cover='', $array_options=[], $generateThumbs=0)
Return a document.
Class to manage ECM files.
Class to manage Trips and Expenses.
Class to manage suppliers invoices.
Class to manage invoices.
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.
Class to manage Dolibarr users.
dol_basename($pathfile)
Make a basename working with all page code (default PHP basenamed fails with cyrillic).
dol_move($srcfile, $destfile, $newmask='0', $overwriteifexists=1, $testvirus=0, $indexdatabase=1, $moreinfo=array(), $entity=0)
Move a file into another name.
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.
dolChmod($filepath, $newmask='')
Change mod of a file.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
getUserRemoteIP($trusted=0)
Return the real IP of remote user.
isAFileWithExecutableContent($filename)
Return if a file can contains executable content.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0)
Clean a string to use it as a file name.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
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_sanitizePathName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a path name.
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...