23use Luracast\Restler\RestException;
25require_once DOL_DOCUMENT_ROOT.
'/main.inc.php';
26require_once DOL_DOCUMENT_ROOT.
'/api/class/api.class.php';
27require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
63 public function index($modulepart, $original_file =
'')
67 if (empty($modulepart)) {
68 throw new RestException(400,
'bad value for parameter modulepart');
70 if (empty($original_file)) {
71 throw new RestException(400,
'bad value for parameter original_file');
75 $entity =
$conf->entity;
86 $relativefile = $original_file;
89 $accessallowed = $check_access[
'accessallowed'];
90 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
91 $original_file = $check_access[
'original_file'];
93 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
94 throw new RestException(403);
96 if (!$accessallowed) {
97 throw new RestException(403);
100 if (DolibarrApiAccess::$user->socid > 0) {
101 if ($sqlprotectagainstexternals) {
102 $resql = $this->db->query($sqlprotectagainstexternals);
104 $num = $this->db->num_rows($resql);
107 $obj = $this->db->fetch_object($resql);
108 if (DolibarrApiAccess::$user->socid != $obj->fk_soc) {
109 throw new RestException(403,
'Not allowed to download documents with such a ref');
117 $filename = basename($original_file);
118 $original_file_osencoded =
dol_osencode($original_file);
120 if (!file_exists($original_file_osencoded)) {
121 dol_syslog(
"Try to download not found file ".$original_file_osencoded, LOG_WARNING);
122 throw new RestException(404,
'File not found');
125 $file_content = file_get_contents($original_file_osencoded);
126 return array(
'filename'=>$filename,
'content-type' =>
dol_mimetype($filename),
'filesize'=>filesize($original_file),
'content'=>base64_encode($file_content),
'encoding'=>
'base64');
151 public function builddoc($modulepart, $original_file =
'', $doctemplate =
'', $langcode =
'')
153 global
$conf, $langs;
155 if (empty($modulepart)) {
156 throw new RestException(400,
'bad value for parameter modulepart');
158 if (empty($original_file)) {
159 throw new RestException(400,
'bad value for parameter original_file');
162 $outputlangs = $langs;
163 if ($langcode && $langs->defaultlang != $langcode) {
165 $outputlangs->setDefaultLang($langcode);
169 $entity =
$conf->entity;
180 $relativefile = $original_file;
183 $accessallowed = $check_access[
'accessallowed'];
184 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
185 $original_file = $check_access[
'original_file'];
187 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
188 throw new RestException(403);
190 if (!$accessallowed) {
191 throw new RestException(403);
194 if (DolibarrApiAccess::$user->socid > 0) {
195 if ($sqlprotectagainstexternals) {
196 $resql = $this->db->query($sqlprotectagainstexternals);
198 $num = $this->db->num_rows($resql);
201 $obj = $this->db->fetch_object($resql);
202 if (DolibarrApiAccess::$user->socid != $obj->fk_soc) {
203 throw new RestException(403,
'Not allowed to download documents with such a ref');
218 if ($modulepart ==
'facture' || $modulepart ==
'invoice') {
219 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
220 $tmpobject =
new Facture($this->db);
221 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
223 throw new RestException(404,
'Invoice not found');
226 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
227 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
229 throw new RestException(500,
'Error generating document');
231 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'invoice_supplier') {
232 require_once DOL_DOCUMENT_ROOT .
'/fourn/class/fournisseur.facture.class.php';
234 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
236 throw new RestException(404,
'Supplier invoice not found');
239 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
240 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
242 throw new RestException(500,
'Error generating document');
244 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
245 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
246 $tmpobject =
new Commande($this->db);
247 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
249 throw new RestException(404,
'Order not found');
251 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
252 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
254 throw new RestException(500,
'Error generating document');
256 } elseif ($modulepart ==
'propal' || $modulepart ==
'proposal') {
257 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
258 $tmpobject =
new Propal($this->db);
259 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
261 throw new RestException(404,
'Proposal not found');
263 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
264 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
266 throw new RestException(500,
'Error generating document');
268 } elseif ($modulepart ==
'contrat' || $modulepart ==
'contract') {
269 require_once DOL_DOCUMENT_ROOT .
'/contrat/class/contrat.class.php';
271 $tmpobject =
new Contrat($this->db);
272 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
275 throw new RestException(404,
'Contract not found');
278 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
279 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
282 throw new RestException(500,
'Error generating document missing doctemplate parameter');
284 } elseif ($modulepart ==
'expedition' || $modulepart ==
'shipment') {
285 require_once DOL_DOCUMENT_ROOT .
'/expedition/class/expedition.class.php';
288 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
291 throw new RestException(404,
'Shipment 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 ==
'mrp') {
301 require_once DOL_DOCUMENT_ROOT .
'/mrp/class/mo.class.php';
303 $tmpobject =
new Mo($this->db);
304 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
307 throw new RestException(404,
'MO 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');
317 throw new RestException(403,
'Generation not available for this modulepart');
320 $filename = basename($original_file);
321 $original_file_osencoded =
dol_osencode($original_file);
323 if (!file_exists($original_file_osencoded)) {
324 throw new RestException(404,
'File not found');
327 $file_content = file_get_contents($original_file_osencoded);
328 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');
355 if (empty($modulepart)) {
356 throw new RestException(400,
'bad value for parameter modulepart');
359 if (empty(
$id) && empty($ref)) {
360 throw new RestException(400,
'bad value for parameter id or ref');
367 if ($modulepart ==
'societe' || $modulepart ==
'thirdparty') {
368 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
370 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
371 throw new RestException(403);
377 throw new RestException(404,
'Thirdparty not found');
381 } elseif ($modulepart ==
'user') {
382 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
385 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'lire') && DolibarrApiAccess::$user->
id !=
$id) {
386 throw new RestException(403);
392 throw new RestException(404,
'User not found');
396 } elseif ($modulepart ==
'adherent' || $modulepart ==
'member') {
397 require_once DOL_DOCUMENT_ROOT.
'/adherents/class/adherent.class.php';
399 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
400 throw new RestException(403);
406 throw new RestException(404,
'Member not found');
410 } elseif ($modulepart ==
'propal' || $modulepart ==
'proposal') {
411 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
413 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
414 throw new RestException(403);
420 throw new RestException(404,
'Proposal not found');
424 } elseif ($modulepart ==
'supplier_proposal') {
425 require_once DOL_DOCUMENT_ROOT.
'/supplier_proposal/class/supplier_proposal.class.php';
427 if (!DolibarrApiAccess::$user->hasRight(
'supplier_proposal',
'read')) {
428 throw new RestException(403);
434 throw new RestException(404,
'Supplier proposal not found');
438 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
439 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
441 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
442 throw new RestException(403);
448 throw new RestException(404,
'Order not found');
452 } elseif ($modulepart ==
'commande_fournisseur' || $modulepart ==
'supplier_order') {
453 $modulepart =
'supplier_order';
455 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.class.php';
457 if (!DolibarrApiAccess::$user->hasRight(
'fournisseur',
'commande',
'lire') && !DolibarrApiAccess::$user->hasRight(
'supplier_order',
'lire')) {
458 throw new RestException(403);
464 throw new RestException(404,
'Purchase order not found');
468 } elseif ($modulepart ==
'shipment' || $modulepart ==
'expedition') {
469 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
471 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'lire')) {
472 throw new RestException(403);
478 throw new RestException(404,
'Shipment not found');
482 } elseif ($modulepart ==
'facture' || $modulepart ==
'invoice') {
483 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
485 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
486 throw new RestException(403);
492 throw new RestException(404,
'Invoice not found');
496 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'supplier_invoice') {
497 $modulepart =
'supplier_invoice';
499 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
501 if (!DolibarrApiAccess::$user->hasRight(
'fournisseur',
'facture',
'lire') && !DolibarrApiAccess::$user->hasRight(
'supplier_invoice',
'lire')) {
502 throw new RestException(403);
508 throw new RestException(404,
'Invoice not found');
512 } elseif ($modulepart ==
'produit' || $modulepart ==
'product') {
513 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
515 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'lire')) {
516 throw new RestException(403);
522 throw new RestException(404,
'Product not found');
523 } elseif ($result < 0) {
524 throw new RestException(500,
'Error while fetching object: '.
$object->error);
528 } elseif ($modulepart ==
'agenda' || $modulepart ==
'action' || $modulepart ==
'event') {
529 require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
531 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'myactions',
'read') && !DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'read')) {
532 throw new RestException(403);
538 throw new RestException(404,
'Event not found');
542 } elseif ($modulepart ==
'expensereport') {
543 require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport.class.php';
545 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'read')) {
546 throw new RestException(403);
552 throw new RestException(404,
'Expense report not found');
556 } elseif ($modulepart ==
'knowledgemanagement') {
557 require_once DOL_DOCUMENT_ROOT.
'/knowledgemanagement/class/knowledgerecord.class.php';
559 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read') && !DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read')) {
560 throw new RestException(403);
566 throw new RestException(404,
'KM article not found');
570 } elseif ($modulepart ==
'categorie' || $modulepart ==
'category') {
571 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
573 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
574 throw new RestException(403);
580 throw new RestException(404,
'Category not found');
584 } elseif ($modulepart ==
'ecm') {
585 throw new RestException(500,
'Modulepart Ecm not implemented yet.');
600 } elseif ($modulepart ==
'contrat' || $modulepart ==
'contract') {
601 $modulepart =
'contrat';
602 require_once DOL_DOCUMENT_ROOT .
'/contrat/class/contrat.class.php';
607 throw new RestException(404,
'Contract not found');
611 } elseif ($modulepart ==
'projet' || $modulepart ==
'project') {
612 $modulepart =
'project';
613 require_once DOL_DOCUMENT_ROOT .
'/projet/class/project.class.php';
618 throw new RestException(404,
'Project not found');
622 } elseif ($modulepart ==
'mrp') {
624 require_once DOL_DOCUMENT_ROOT .
'/mrp/class/mo.class.php';
629 throw new RestException(404,
'MO not found');
634 throw new RestException(500,
'Modulepart '.$modulepart.
' not implemented yet.');
637 $objectType = $modulepart;
639 $objectType =
$object->table_element;
642 $filearray =
dol_dir_list($upload_dir, $type, $recursive,
'',
'(\.meta|_preview.*\.png)$', $sortfield, (strtolower($sortorder) ==
'desc' ? SORT_DESC : SORT_ASC), 1);
643 if (empty($filearray)) {
644 throw new RestException(404,
'Search for modulepart '.$modulepart.
' with Id '.
$object->id.(!empty(
$object->ref) ?
' or Ref '.$object->ref :
'').
' does not return any document.');
646 if ((
$object->id) > 0 && !empty($modulepart)) {
647 require_once DOL_DOCUMENT_ROOT.
'/ecm/class/ecmfiles.class.php';
649 $result = $ecmfile->fetchAll(
'',
'', 0, 0, array(
't.src_object_type' => $objectType,
't.src_object_id' =>
$object->id));
651 throw new RestException(503,
'Error when retrieve ecm list : '.$this->db->lasterror());
652 } elseif (is_array($ecmfile->lines) && count($ecmfile->lines) > 0) {
653 $count = count($filearray);
654 for ($i = 0 ; $i < $count ; $i++) {
655 foreach ($ecmfile->lines as $line) {
656 if ($filearray[$i][
'name'] == $line->filename) {
657 $filearray[$i] = array_merge($filearray[$i], (array) $line);
712 public function post($filename, $modulepart, $ref =
'', $subdir =
'', $filecontent =
'', $fileencoding =
'', $overwriteifexists = 0, $createdirifnotexists = 1, $position = 0, $cover =
'', $array_options = [])
716 $modulepartorig = $modulepart;
718 if (empty($modulepart)) {
719 throw new RestException(400,
'Modulepart not provided.');
722 $newfilecontent =
'';
723 if (empty($fileencoding)) {
724 $newfilecontent = $filecontent;
726 if ($fileencoding ==
'base64') {
727 $newfilecontent = base64_decode($filecontent);
734 $entity = DolibarrApiAccess::$user->entity;
735 if (empty($entity)) {
743 if ($modulepart ==
'facture' || $modulepart ==
'invoice') {
744 $modulepart =
'facture';
746 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
748 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'supplier_invoice') {
749 $modulepart =
'supplier_invoice';
751 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
753 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
754 $modulepart =
'commande';
756 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
758 } elseif ($modulepart ==
'commande_fournisseur' || $modulepart ==
'supplier_order') {
759 $modulepart =
'supplier_order';
761 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.class.php';
763 } elseif ($modulepart ==
'projet' || $modulepart ==
'project') {
764 require_once DOL_DOCUMENT_ROOT.
'/projet/class/project.class.php';
766 } elseif ($modulepart ==
'task' || $modulepart ==
'project_task') {
767 $modulepart =
'project_task';
769 require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
772 $task_result =
$object->fetch(0, $ref);
775 if ($task_result > 0) {
776 $project_result =
$object->fetchProject();
778 if ($project_result >= 0) {
782 throw new RestException(500,
'Error while fetching Task '.$ref);
784 } elseif ($modulepart ==
'product' || $modulepart ==
'produit' || $modulepart ==
'service' || $modulepart ==
'produit|service') {
785 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
787 } elseif ($modulepart ==
'expensereport') {
788 require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport.class.php';
790 } elseif ($modulepart ==
'fichinter') {
791 require_once DOL_DOCUMENT_ROOT.
'/fichinter/class/fichinter.class.php';
793 } elseif ($modulepart ==
'adherent' || $modulepart ==
'member') {
794 $modulepart =
'adherent';
795 require_once DOL_DOCUMENT_ROOT.
'/adherents/class/adherent.class.php';
797 } elseif ($modulepart ==
'proposal' || $modulepart ==
'propal' || $modulepart ==
'propale') {
798 $modulepart =
'propale';
799 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
801 } elseif ($modulepart ==
'agenda' || $modulepart ==
'action' || $modulepart ==
'event') {
802 $modulepart =
'agenda';
803 require_once DOL_DOCUMENT_ROOT .
'/comm/action/class/actioncomm.class.php';
805 } elseif ($modulepart ==
'contact' || $modulepart ==
'socpeople') {
806 $modulepart =
'contact';
807 require_once DOL_DOCUMENT_ROOT.
'/contact/class/contact.class.php';
810 } elseif ($modulepart ==
'contrat' || $modulepart ==
'contract') {
811 $modulepart =
'contrat';
812 require_once DOL_DOCUMENT_ROOT .
'/contrat/class/contrat.class.php';
814 } elseif ($modulepart ==
'mrp') {
816 require_once DOL_DOCUMENT_ROOT .
'/mrp/class/mo.class.php';
820 throw new RestException(500,
'Modulepart '.$modulepart.
' not implemented yet.');
826 $result =
$object->fetch($ref);
828 $result =
$object->fetch(0, $ref);
832 throw new RestException(404,
"Object with ref '".$ref.
"' was not found.");
833 } elseif ($result < 0) {
834 throw new RestException(500,
'Error while fetching object: '.
$object->error);
839 throw new RestException(404,
'The object '.$modulepart.
" with ref '".$ref.
"' was not found.");
844 if ($modulepart ==
'supplier_invoice') {
849 if ($modulepart !=
'ecm') {
850 $relativefile = $tmpreldir.dol_sanitizeFileName(
$object->ref);
852 $upload_dir = $tmp[
'original_file'];
854 if (!DolibarrApiAccess::$user->hasRight(
'ecm',
'upload')) {
855 throw new RestException(403,
'Missing permission to upload files in ECM module');
857 $upload_dir =
$conf->medias->multidir_output[
$conf->entity];
860 if (empty($upload_dir) || $upload_dir ==
'/') {
861 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.');
864 if ($modulepart ==
'invoice') {
865 $modulepart =
'facture';
867 if ($modulepart ==
'member') {
868 $modulepart =
'adherent';
872 if ($modulepart !=
'ecm') {
873 $relativefile = $subdir;
875 $upload_dir = $tmp[
'original_file'];
877 if (!DolibarrApiAccess::$user->hasRight(
'ecm',
'upload')) {
878 throw new RestException(403,
'Missing permission to upload files in ECM module');
880 $upload_dir =
$conf->medias->multidir_output[
$conf->entity];
883 if (empty($upload_dir) || $upload_dir ==
'/') {
884 if (!empty($tmp[
'error'])) {
885 throw new RestException(403,
'Error returned by dol_check_secure_access_document: '.$tmp[
'error']);
887 throw new RestException(400,
'This value of modulepart ('.$modulepart.
') is not allowed with this value of subdir ('.$relativefile.
')');
895 if (!empty($createdirifnotexists)) {
897 throw new RestException(500,
'Error while trying to create directory '.$upload_dir);
901 $destfile = $upload_dir.
'/'.$original_file;
902 $destfiletmp = DOL_DATA_ROOT.
'/admin/temp/'.$original_file;
907 throw new RestException(400,
'Directory does not exists : '.dirname($destfile));
910 if (!$overwriteifexists &&
dol_is_file($destfile)) {
911 throw new RestException(400,
"File with name '".$original_file.
"' already exists.");
919 $fhandle = @fopen($destfiletmp,
'w');
921 $nbofbyteswrote = fwrite($fhandle, $newfilecontent);
925 throw new RestException(500,
"Failed to open file '".$destfiletmp.
"' for write");
928 $disablevirusscan = 0;
929 $src_file = $destfiletmp;
930 $dest_file = $destfile;
934 if (empty($disablevirusscan) && file_exists($src_file)) {
936 if (count($checkvirusarray)) {
937 dol_syslog(
'Files.lib::dol_move_uploaded_file File "'.$src_file.
'" (target name "'.$dest_file.
'") KO with antivirus: errors='.implode(
',', $checkvirusarray), LOG_WARNING);
938 throw new RestException(500,
'ErrorFileIsInfectedWithAVirus: '.implode(
',', $checkvirusarray));
947 $publicmediasdirwithslash =
$conf->medias->multidir_output[
$conf->entity];
948 if (!preg_match(
'/\/$/', $publicmediasdirwithslash)) {
949 $publicmediasdirwithslash .=
'/';
952 if (strpos($upload_dir, $publicmediasdirwithslash) !== 0 || !
getDolGlobalInt(
"MAIN_DOCUMENT_DISABLE_NOEXE_IN_MEDIAS_DIR")) {
953 $dest_file .=
'.noexe';
959 if (preg_match(
'/^\./', basename($src_file)) || preg_match(
'/\.\./', $src_file) || preg_match(
'/[<>|]/', $src_file)) {
960 dol_syslog(
"Refused to deliver file ".$src_file, LOG_WARNING);
961 throw new RestException(500,
"Refused to deliver file ".$src_file);
966 if (preg_match(
'/^\./', basename($dest_file)) || preg_match(
'/\.\./', $dest_file) || preg_match(
'/[<>|]/', $dest_file)) {
967 dol_syslog(
"Refused to deliver file ".$dest_file, LOG_WARNING);
968 throw new RestException(500,
"Refused to deliver file ".$dest_file);
971 $moreinfo = array(
'note_private' =>
'File uploaded using API /documents from IP '.
getUserRemoteIP());
973 $moreinfo[
'src_object_type'] =
$object->table_element;
974 $moreinfo[
'src_object_id'] =
$object->id;
976 if (!empty($array_options)) {
977 $moreinfo = array_merge($moreinfo, [
"array_options" => $array_options]);
979 if (!empty($position)) {
980 $moreinfo = array_merge($moreinfo, [
"position" => $position]);
982 if (!empty($cover)) {
983 $moreinfo = array_merge($moreinfo, [
"cover" => $cover]);
987 $result =
dol_move($destfiletmp, $dest_file,
'0', $overwriteifexists, 1, 1, $moreinfo);
989 throw new RestException(500,
"Failed to move file into '".$dest_file.
"'");
1010 public function delete($modulepart, $original_file)
1012 global
$conf, $langs;
1014 if (empty($modulepart)) {
1015 throw new RestException(400,
'bad value for parameter modulepart');
1017 if (empty($original_file)) {
1018 throw new RestException(400,
'bad value for parameter original_file');
1022 $entity =
$conf->entity;
1033 $relativefile = $original_file;
1036 $accessallowed = $check_access[
'accessallowed'];
1037 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
1038 $original_file = $check_access[
'original_file'];
1040 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
1041 throw new RestException(403);
1043 if (!$accessallowed) {
1044 throw new RestException(403);
1047 if (DolibarrApiAccess::$user->socid > 0) {
1048 if ($sqlprotectagainstexternals) {
1049 $resql = $this->db->query($sqlprotectagainstexternals);
1051 $num = $this->db->num_rows($resql);
1054 $obj = $this->db->fetch_object($resql);
1055 if (DolibarrApiAccess::$user->socid != $obj->fk_soc) {
1056 throw new RestException(403,
'Not allowed to download documents with such a ref');
1064 $filename = basename($original_file);
1065 $original_file_osencoded =
dol_osencode($original_file);
1067 if (!file_exists($original_file_osencoded)) {
1068 dol_syslog(
"Try to download not found file ".$original_file_osencoded, LOG_WARNING);
1069 throw new RestException(404,
'File not found');
1072 if (@unlink($original_file_osencoded)) {
1076 'message' =>
'Document deleted'
1081 throw new RestException(403);
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.
post($filename, $modulepart, $ref='', $subdir='', $filecontent='', $fileencoding='', $overwriteifexists=0, $createdirifnotexists=1, $position=0, $cover='', $array_options=[])
Return a document.
__construct()
Constructor.
index($modulepart, $original_file='')
Download a document.
getDocumentsListByElement($modulepart, $id=0, $ref='', $sortfield='', $sortorder='')
Return the list of documents of a dedicated element (from its ID or Ref)
builddoc($modulepart, $original_file='', $doctemplate='', $langcode='')
Build 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)
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...