22use Luracast\Restler\RestException;
24require_once DOL_DOCUMENT_ROOT.
'/main.inc.php';
25require_once DOL_DOCUMENT_ROOT.
'/api/class/api.class.php';
26require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
62 public function index($modulepart, $original_file =
'')
66 if (empty($modulepart)) {
67 throw new RestException(400,
'bad value for parameter modulepart');
69 if (empty($original_file)) {
70 throw new RestException(400,
'bad value for parameter original_file');
74 $entity = $conf->entity;
85 $relativefile = $original_file;
88 $accessallowed = $check_access[
'accessallowed'];
89 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
90 $original_file = $check_access[
'original_file'];
92 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
93 throw new RestException(403);
95 if (!$accessallowed) {
96 throw new RestException(403);
99 if (DolibarrApiAccess::$user->socid > 0) {
100 if ($sqlprotectagainstexternals) {
101 $resql = $this->db->query($sqlprotectagainstexternals);
103 $num = $this->db->num_rows($resql);
106 $obj = $this->db->fetch_object($resql);
107 if (DolibarrApiAccess::$user->socid != $obj->fk_soc) {
108 throw new RestException(403,
'Not allowed to download documents with such a ref');
116 $filename = basename($original_file);
117 $original_file_osencoded =
dol_osencode($original_file);
119 if (!file_exists($original_file_osencoded)) {
120 dol_syslog(
"Try to download not found file ".$original_file_osencoded, LOG_WARNING);
121 throw new RestException(404,
'File not found');
124 $file_content = file_get_contents($original_file_osencoded);
125 return array(
'filename'=>$filename,
'content-type' =>
dol_mimetype($filename),
'filesize'=>filesize($original_file),
'content'=>base64_encode($file_content),
'encoding'=>
'base64');
150 public function builddoc($modulepart, $original_file =
'', $doctemplate =
'', $langcode =
'')
152 global $conf, $langs;
154 if (empty($modulepart)) {
155 throw new RestException(400,
'bad value for parameter modulepart');
157 if (empty($original_file)) {
158 throw new RestException(400,
'bad value for parameter original_file');
161 $outputlangs = $langs;
162 if ($langcode && $langs->defaultlang != $langcode) {
164 $outputlangs->setDefaultLang($langcode);
168 $entity = $conf->entity;
179 $relativefile = $original_file;
182 $accessallowed = $check_access[
'accessallowed'];
183 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
184 $original_file = $check_access[
'original_file'];
186 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
187 throw new RestException(403);
189 if (!$accessallowed) {
190 throw new RestException(403);
193 if (DolibarrApiAccess::$user->socid > 0) {
194 if ($sqlprotectagainstexternals) {
195 $resql = $this->db->query($sqlprotectagainstexternals);
197 $num = $this->db->num_rows($resql);
200 $obj = $this->db->fetch_object($resql);
201 if (DolibarrApiAccess::$user->socid != $obj->fk_soc) {
202 throw new RestException(403,
'Not allowed to download documents with such a ref');
217 if ($modulepart ==
'facture' || $modulepart ==
'invoice') {
218 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
219 $tmpobject =
new Facture($this->db);
220 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
222 throw new RestException(404,
'Invoice not found');
225 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
226 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
228 throw new RestException(500,
'Error generating document');
230 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'invoice_supplier') {
231 require_once DOL_DOCUMENT_ROOT .
'/fourn/class/fournisseur.facture.class.php';
233 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
235 throw new RestException(404,
'Supplier invoice not found');
238 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
239 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
241 throw new RestException(500,
'Error generating document');
243 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
244 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
245 $tmpobject =
new Commande($this->db);
246 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
248 throw new RestException(404,
'Order not found');
250 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
251 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
253 throw new RestException(500,
'Error generating document');
255 } elseif ($modulepart ==
'propal' || $modulepart ==
'proposal') {
256 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
257 $tmpobject =
new Propal($this->db);
258 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
260 throw new RestException(404,
'Proposal 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 ==
'contrat' || $modulepart ==
'contract') {
268 require_once DOL_DOCUMENT_ROOT .
'/contrat/class/contrat.class.php';
270 $tmpobject =
new Contrat($this->db);
271 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
274 throw new RestException(404,
'Contract not found');
277 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
278 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
281 throw new RestException(500,
'Error generating document missing doctemplate parameter');
283 } elseif ($modulepart ==
'expedition' || $modulepart ==
'shipment') {
284 require_once DOL_DOCUMENT_ROOT .
'/expedition/class/expedition.class.php';
287 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
290 throw new RestException(404,
'Shipment not found');
293 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
294 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
297 throw new RestException(500,
'Error generating document missing doctemplate parameter');
299 } elseif ($modulepart ==
'mrp') {
300 require_once DOL_DOCUMENT_ROOT .
'/mrp/class/mo.class.php';
302 $tmpobject =
new Mo($this->db);
303 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
306 throw new RestException(404,
'MO not found');
309 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
310 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
313 throw new RestException(500,
'Error generating document missing doctemplate parameter');
316 throw new RestException(403,
'Generation not available for this modulepart');
319 $filename = basename($original_file);
320 $original_file_osencoded =
dol_osencode($original_file);
322 if (!file_exists($original_file_osencoded)) {
323 throw new RestException(404,
'File not found');
326 $file_content = file_get_contents($original_file_osencoded);
327 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');
354 if (empty($modulepart)) {
355 throw new RestException(400,
'bad value for parameter modulepart');
358 if (empty($id) && empty($ref)) {
359 throw new RestException(400,
'bad value for parameter id or ref');
362 $id = (empty($id) ? 0 : $id);
366 if ($modulepart ==
'societe' || $modulepart ==
'thirdparty') {
367 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
369 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
370 throw new RestException(403);
374 $result =
$object->fetch($id, $ref);
376 throw new RestException(404,
'Thirdparty not found');
379 $upload_dir = $conf->societe->multidir_output[
$object->entity].
"/".
$object->id;
380 } elseif ($modulepart ==
'user') {
381 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
384 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'lire') && DolibarrApiAccess::$user->
id != $id) {
385 throw new RestException(403);
389 $result =
$object->fetch($id, $ref);
391 throw new RestException(404,
'User not found');
395 } elseif ($modulepart ==
'adherent' || $modulepart ==
'member') {
396 require_once DOL_DOCUMENT_ROOT.
'/adherents/class/adherent.class.php';
398 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
399 throw new RestException(403);
403 $result =
$object->fetch($id, $ref);
405 throw new RestException(404,
'Member not found');
408 $upload_dir = $conf->adherent->dir_output.
"/".
get_exdir(0, 0, 0, 1,
$object,
'member');
409 } elseif ($modulepart ==
'propal' || $modulepart ==
'proposal') {
410 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
412 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
413 throw new RestException(403);
417 $result =
$object->fetch($id, $ref);
419 throw new RestException(404,
'Proposal not found');
423 } elseif ($modulepart ==
'supplier_proposal') {
424 require_once DOL_DOCUMENT_ROOT.
'/supplier_proposal/class/supplier_proposal.class.php';
426 if (!DolibarrApiAccess::$user->hasRight(
'supplier_proposal',
'read')) {
427 throw new RestException(403);
431 $result =
$object->fetch($id, $ref);
433 throw new RestException(404,
'Supplier proposal not found');
437 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
438 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
440 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
441 throw new RestException(403);
445 $result =
$object->fetch($id, $ref);
447 throw new RestException(404,
'Order not found');
450 $upload_dir = $conf->commande->dir_output.
"/".
get_exdir(0, 0, 0, 1,
$object,
'commande');
451 } elseif ($modulepart ==
'commande_fournisseur' || $modulepart ==
'supplier_order') {
452 $modulepart =
'supplier_order';
454 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.class.php';
456 if (!DolibarrApiAccess::$user->hasRight(
'fournisseur',
'commande',
'lire') && !DolibarrApiAccess::$user->hasRight(
'supplier_order',
'lire')) {
457 throw new RestException(403);
461 $result =
$object->fetch($id, $ref);
463 throw new RestException(404,
'Purchase order not found');
467 } elseif ($modulepart ==
'shipment' || $modulepart ==
'expedition') {
468 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
470 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'lire')) {
471 throw new RestException(403);
475 $result =
$object->fetch($id, $ref);
477 throw new RestException(404,
'Shipment not found');
480 $upload_dir = $conf->expedition->dir_output.
"/sending/".
get_exdir(0, 0, 0, 1,
$object,
'shipment');
481 } elseif ($modulepart ==
'facture' || $modulepart ==
'invoice') {
482 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
484 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
485 throw new RestException(403);
489 $result =
$object->fetch($id, $ref);
491 throw new RestException(404,
'Invoice not found');
494 $upload_dir = $conf->facture->dir_output.
"/".
get_exdir(0, 0, 0, 1,
$object,
'invoice');
495 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'supplier_invoice') {
496 $modulepart =
'supplier_invoice';
498 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
500 if (!DolibarrApiAccess::$user->hasRight(
'fournisseur',
'facture',
'lire') && !DolibarrApiAccess::$user->hasRight(
'supplier_invoice',
'lire')) {
501 throw new RestException(403);
505 $result =
$object->fetch($id, $ref);
507 throw new RestException(404,
'Invoice not found');
510 $upload_dir = $conf->fournisseur->dir_output.
"/facture/".
get_exdir(
$object->id, 2, 0, 0,
$object,
'invoice_supplier').dol_sanitizeFileName(
$object->ref);
511 } elseif ($modulepart ==
'produit' || $modulepart ==
'product') {
512 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
514 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'lire')) {
515 throw new RestException(403);
519 $result =
$object->fetch($id, $ref);
521 throw new RestException(404,
'Product not found');
522 } elseif ($result < 0) {
523 throw new RestException(500,
'Error while fetching object: '.
$object->error);
527 } elseif ($modulepart ==
'agenda' || $modulepart ==
'action' || $modulepart ==
'event') {
528 require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
530 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'myactions',
'read') && !DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'read')) {
531 throw new RestException(403);
535 $result =
$object->fetch($id, $ref);
537 throw new RestException(404,
'Event not found');
541 } elseif ($modulepart ==
'expensereport') {
542 require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport.class.php';
544 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'read')) {
545 throw new RestException(403);
549 $result =
$object->fetch($id, $ref);
551 throw new RestException(404,
'Expense report not found');
555 } elseif ($modulepart ==
'knowledgemanagement') {
556 require_once DOL_DOCUMENT_ROOT.
'/knowledgemanagement/class/knowledgerecord.class.php';
558 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read') && !DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read')) {
559 throw new RestException(403);
563 $result =
$object->fetch($id, $ref);
565 throw new RestException(404,
'KM article not found');
569 } elseif ($modulepart ==
'categorie' || $modulepart ==
'category') {
570 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
572 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
573 throw new RestException(403);
577 $result =
$object->fetch($id, $ref);
579 throw new RestException(404,
'Category not found');
583 } elseif ($modulepart ==
'ecm') {
584 throw new RestException(500,
'Modulepart Ecm not implemented yet.');
599 } elseif ($modulepart ==
'contrat' || $modulepart ==
'contract') {
600 $modulepart =
'contrat';
601 require_once DOL_DOCUMENT_ROOT .
'/contrat/class/contrat.class.php';
604 $result =
$object->fetch($id, $ref);
606 throw new RestException(404,
'Contract not found');
609 $upload_dir = $conf->contrat->dir_output .
"/" .
get_exdir(0, 0, 0, 1,
$object,
'contract');
610 } elseif ($modulepart ==
'projet' || $modulepart ==
'project') {
611 $modulepart =
'project';
612 require_once DOL_DOCUMENT_ROOT .
'/projet/class/project.class.php';
615 $result =
$object->fetch($id, $ref);
617 throw new RestException(404,
'Project not found');
620 $upload_dir = $conf->projet->dir_output .
"/" .
get_exdir(0, 0, 0, 1,
$object,
'project');
621 } elseif ($modulepart ==
'mrp') {
623 require_once DOL_DOCUMENT_ROOT .
'/mrp/class/mo.class.php';
626 $result =
$object->fetch($id, $ref);
628 throw new RestException(404,
'MO not found');
631 $upload_dir = $conf->mrp->dir_output .
"/" .
get_exdir(0, 0, 0, 1,
$object,
'mrp');
633 throw new RestException(500,
'Modulepart '.$modulepart.
' not implemented yet.');
636 $objectType = $modulepart;
638 $objectType =
$object->table_element;
641 $filearray =
dol_dir_list($upload_dir, $type, $recursive,
'',
'(\.meta|_preview.*\.png)$', $sortfield, (strtolower($sortorder) ==
'desc' ? SORT_DESC : SORT_ASC), 1);
642 if (empty($filearray)) {
643 throw new RestException(404,
'Search for modulepart '.$modulepart.
' with Id '.
$object->id.(!empty(
$object->ref) ?
' or Ref '.$object->ref :
'').
' does not return any document.');
645 if ((
$object->id) > 0 && !empty($modulepart)) {
646 require_once DOL_DOCUMENT_ROOT.
'/ecm/class/ecmfiles.class.php';
648 $result = $ecmfile->fetchAll(
'',
'', 0, 0, array(
't.src_object_type' => $objectType,
't.src_object_id' =>
$object->id));
650 throw new RestException(503,
'Error when retrieve ecm list : '.$this->db->lasterror());
651 } elseif (is_array($ecmfile->lines) && count($ecmfile->lines) > 0) {
652 $count = count($filearray);
653 for ($i = 0 ; $i < $count ; $i++) {
654 foreach ($ecmfile->lines as $line) {
655 if ($filearray[$i][
'name'] == $line->filename) {
656 $filearray[$i] = array_merge($filearray[$i], (array) $line);
708 public function post($filename, $modulepart, $ref =
'', $subdir =
'', $filecontent =
'', $fileencoding =
'', $overwriteifexists = 0, $createdirifnotexists = 1)
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(
'', $ref);
775 if ($task_result > 0) {
776 $project_result =
$object->fetch_projet();
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(
'', $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;
978 $result =
dol_move($destfiletmp, $dest_file, 0, $overwriteifexists, 1, 1, $moreinfo);
980 throw new RestException(500,
"Failed to move file into '".$dest_file.
"'");
1001 public function delete($modulepart, $original_file)
1003 global $conf, $langs;
1005 if (empty($modulepart)) {
1006 throw new RestException(400,
'bad value for parameter modulepart');
1008 if (empty($original_file)) {
1009 throw new RestException(400,
'bad value for parameter original_file');
1013 $entity = $conf->entity;
1024 $relativefile = $original_file;
1027 $accessallowed = $check_access[
'accessallowed'];
1028 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
1029 $original_file = $check_access[
'original_file'];
1031 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
1032 throw new RestException(403);
1034 if (!$accessallowed) {
1035 throw new RestException(403);
1038 if (DolibarrApiAccess::$user->socid > 0) {
1039 if ($sqlprotectagainstexternals) {
1040 $resql = $this->db->query($sqlprotectagainstexternals);
1042 $num = $this->db->num_rows($resql);
1045 $obj = $this->db->fetch_object($resql);
1046 if (DolibarrApiAccess::$user->socid != $obj->fk_soc) {
1047 throw new RestException(403,
'Not allowed to download documents with such a ref');
1055 $filename = basename($original_file);
1056 $original_file_osencoded =
dol_osencode($original_file);
1058 if (!file_exists($original_file_osencoded)) {
1059 dol_syslog(
"Try to download not found file ".$original_file_osencoded, LOG_WARNING);
1060 throw new RestException(404,
'File not found');
1063 if (@unlink($original_file_osencoded)) {
1067 'message' =>
'Document deleted'
1072 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.
Class to manage contracts.
API class for receive files.
post($filename, $modulepart, $ref='', $subdir='', $filecontent='', $fileencoding='', $overwriteifexists=0, $createdirifnotexists=1)
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 shipments.
Class to manage Trips and Expenses.
Class to manage suppliers invoices.
Class to manage invoices.
Class to manage interventions.
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_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_move($srcfile, $destfile, $newmask='0', $overwriteifexists=1, $testvirus=0, $indexdatabase=1, $moreinfo=array())
Move a file into another name.
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.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
isAFileWithExecutableContent($filename)
Return if a file can contains executable content.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
getUserRemoteIP()
Return the IP of remote user.
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)