21use Luracast\Restler\RestException;
23require_once DOL_DOCUMENT_ROOT.
'/main.inc.php';
24require_once DOL_DOCUMENT_ROOT.
'/api/class/api.class.php';
25require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
61 public function index($modulepart, $original_file =
'')
65 if (empty($modulepart)) {
66 throw new RestException(400,
'bad value for parameter modulepart');
68 if (empty($original_file)) {
69 throw new RestException(400,
'bad value for parameter original_file');
73 $entity = $conf->entity;
84 $relativefile = $original_file;
87 $accessallowed = $check_access[
'accessallowed'];
88 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
89 $original_file = $check_access[
'original_file'];
91 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
92 throw new RestException(403);
94 if (!$accessallowed) {
95 throw new RestException(403);
98 $filename = basename($original_file);
101 if (!file_exists($original_file_osencoded)) {
102 dol_syslog(
"Try to download not found file ".$original_file_osencoded, LOG_WARNING);
103 throw new RestException(404,
'File not found');
106 $file_content = file_get_contents($original_file_osencoded);
107 return array(
'filename'=>$filename,
'content-type' =>
dol_mimetype($filename),
'filesize'=>filesize($original_file),
'content'=>base64_encode($file_content),
'encoding'=>
'base64');
132 public function builddoc($modulepart, $original_file =
'', $doctemplate =
'', $langcode =
'')
134 global $conf, $langs;
136 if (empty($modulepart)) {
137 throw new RestException(400,
'bad value for parameter modulepart');
139 if (empty($original_file)) {
140 throw new RestException(400,
'bad value for parameter original_file');
143 $outputlangs = $langs;
144 if ($langcode && $langs->defaultlang != $langcode) {
146 $outputlangs->setDefaultLang($langcode);
150 $entity = $conf->entity;
161 $relativefile = $original_file;
164 $accessallowed = $check_access[
'accessallowed'];
165 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
166 $original_file = $check_access[
'original_file'];
168 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
169 throw new RestException(403);
171 if (!$accessallowed) {
172 throw new RestException(403);
182 if ($modulepart ==
'facture' || $modulepart ==
'invoice') {
183 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
184 $tmpobject =
new Facture($this->db);
185 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
187 throw new RestException(404,
'Invoice not found');
190 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
191 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
193 throw new RestException(500,
'Error generating document');
195 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'invoice_supplier') {
196 require_once DOL_DOCUMENT_ROOT .
'/fourn/class/fournisseur.facture.class.php';
198 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
200 throw new RestException(404,
'Supplier invoice not found');
203 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
204 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
206 throw new RestException(500,
'Error generating document');
208 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
209 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
210 $tmpobject =
new Commande($this->db);
211 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
213 throw new RestException(404,
'Order not found');
215 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
216 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
218 throw new RestException(500,
'Error generating document');
220 } elseif ($modulepart ==
'propal' || $modulepart ==
'proposal') {
221 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
222 $tmpobject =
new Propal($this->db);
223 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
225 throw new RestException(404,
'Proposal not found');
227 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
228 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
230 throw new RestException(500,
'Error generating document');
232 } elseif ($modulepart ==
'contrat' || $modulepart ==
'contract') {
233 require_once DOL_DOCUMENT_ROOT .
'/contrat/class/contrat.class.php';
235 $tmpobject =
new Contrat($this->db);
236 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
239 throw new RestException(404,
'Contract not found');
242 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
243 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
246 throw new RestException(500,
'Error generating document missing doctemplate parameter');
248 } elseif ($modulepart ==
'expedition' || $modulepart ==
'shipment') {
249 require_once DOL_DOCUMENT_ROOT .
'/expedition/class/expedition.class.php';
252 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
255 throw new RestException(404,
'Shipment not found');
258 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
259 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
262 throw new RestException(500,
'Error generating document missing doctemplate parameter');
264 } elseif ($modulepart ==
'mrp') {
265 require_once DOL_DOCUMENT_ROOT .
'/mrp/class/mo.class.php';
267 $tmpobject =
new Mo($this->db);
268 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
271 throw new RestException(404,
'MO not found');
274 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
275 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
278 throw new RestException(500,
'Error generating document missing doctemplate parameter');
281 throw new RestException(403,
'Generation not available for this modulepart');
284 $filename = basename($original_file);
285 $original_file_osencoded =
dol_osencode($original_file);
287 if (!file_exists($original_file_osencoded)) {
288 throw new RestException(404,
'File not found');
291 $file_content = file_get_contents($original_file_osencoded);
292 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');
319 if (empty($modulepart)) {
320 throw new RestException(400,
'bad value for parameter modulepart');
323 if (empty($id) && empty($ref)) {
324 throw new RestException(400,
'bad value for parameter id or ref');
327 $id = (empty($id) ? 0 : $id);
331 if ($modulepart ==
'societe' || $modulepart ==
'thirdparty') {
332 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
334 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
335 throw new RestException(403);
339 $result =
$object->fetch($id, $ref);
341 throw new RestException(404,
'Thirdparty not found');
344 $upload_dir = $conf->societe->multidir_output[
$object->entity].
"/".
$object->id;
345 } elseif ($modulepart ==
'user') {
346 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
349 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'lire') && DolibarrApiAccess::$user->
id != $id) {
350 throw new RestException(403);
354 $result =
$object->fetch($id, $ref);
356 throw new RestException(404,
'User not found');
360 } elseif ($modulepart ==
'adherent' || $modulepart ==
'member') {
361 require_once DOL_DOCUMENT_ROOT.
'/adherents/class/adherent.class.php';
363 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
364 throw new RestException(403);
368 $result =
$object->fetch($id, $ref);
370 throw new RestException(404,
'Member not found');
373 $upload_dir = $conf->adherent->dir_output.
"/".
get_exdir(0, 0, 0, 1,
$object,
'member');
374 } elseif ($modulepart ==
'propal' || $modulepart ==
'proposal') {
375 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
377 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
378 throw new RestException(403);
382 $result =
$object->fetch($id, $ref);
384 throw new RestException(404,
'Proposal not found');
388 } elseif ($modulepart ==
'supplier_proposal') {
389 require_once DOL_DOCUMENT_ROOT.
'/supplier_proposal/class/supplier_proposal.class.php';
391 if (!DolibarrApiAccess::$user->hasRight(
'supplier_proposal',
'read')) {
392 throw new RestException(403);
396 $result =
$object->fetch($id, $ref);
398 throw new RestException(404,
'Supplier proposal not found');
402 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
403 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
405 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
406 throw new RestException(403);
410 $result =
$object->fetch($id, $ref);
412 throw new RestException(404,
'Order not found');
415 $upload_dir = $conf->commande->dir_output.
"/".
get_exdir(0, 0, 0, 1,
$object,
'commande');
416 } elseif ($modulepart ==
'commande_fournisseur' || $modulepart ==
'supplier_order') {
417 $modulepart =
'supplier_order';
419 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.class.php';
421 if (!DolibarrApiAccess::$user->hasRight(
'fournisseur',
'commande',
'lire') && !DolibarrApiAccess::$user->hasRight(
'supplier_order',
'lire')) {
422 throw new RestException(403);
426 $result =
$object->fetch($id, $ref);
428 throw new RestException(404,
'Purchase order not found');
432 } elseif ($modulepart ==
'shipment' || $modulepart ==
'expedition') {
433 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
435 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'lire')) {
436 throw new RestException(403);
440 $result =
$object->fetch($id, $ref);
442 throw new RestException(404,
'Shipment not found');
445 $upload_dir = $conf->expedition->dir_output.
"/sending/".
get_exdir(0, 0, 0, 1,
$object,
'shipment');
446 } elseif ($modulepart ==
'facture' || $modulepart ==
'invoice') {
447 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
449 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
450 throw new RestException(403);
454 $result =
$object->fetch($id, $ref);
456 throw new RestException(404,
'Invoice not found');
459 $upload_dir = $conf->facture->dir_output.
"/".
get_exdir(0, 0, 0, 1,
$object,
'invoice');
460 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'supplier_invoice') {
461 $modulepart =
'supplier_invoice';
463 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
465 if (!DolibarrApiAccess::$user->hasRight(
'fournisseur',
'facture',
'lire') && !DolibarrApiAccess::$user->hasRight(
'supplier_invoice',
'lire')) {
466 throw new RestException(403);
470 $result =
$object->fetch($id, $ref);
472 throw new RestException(404,
'Invoice not found');
475 $upload_dir = $conf->fournisseur->dir_output.
"/facture/".
get_exdir(
$object->id, 2, 0, 0,
$object,
'invoice_supplier').dol_sanitizeFileName(
$object->ref);
476 } elseif ($modulepart ==
'produit' || $modulepart ==
'product') {
477 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
479 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'lire')) {
480 throw new RestException(403);
484 $result =
$object->fetch($id, $ref);
486 throw new RestException(404,
'Product not found');
487 } elseif ($result < 0) {
488 throw new RestException(500,
'Error while fetching object: '.
$object->error);
492 } elseif ($modulepart ==
'agenda' || $modulepart ==
'action' || $modulepart ==
'event') {
493 require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
495 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'myactions',
'read') && !DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'read')) {
496 throw new RestException(403);
500 $result =
$object->fetch($id, $ref);
502 throw new RestException(404,
'Event not found');
506 } elseif ($modulepart ==
'expensereport') {
507 require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport.class.php';
509 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'read') && !DolibarrApiAccess::$user->hasRight(
'expensereport',
'read')) {
510 throw new RestException(403);
514 $result =
$object->fetch($id, $ref);
516 throw new RestException(404,
'Expense report not found');
520 } elseif ($modulepart ==
'knowledgemanagement') {
521 require_once DOL_DOCUMENT_ROOT.
'/knowledgemanagement/class/knowledgerecord.class.php';
523 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read') && !DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read')) {
524 throw new RestException(403);
528 $result =
$object->fetch($id, $ref);
530 throw new RestException(404,
'KM article not found');
534 } elseif ($modulepart ==
'categorie' || $modulepart ==
'category') {
535 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
537 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
538 throw new RestException(403);
542 $result =
$object->fetch($id, $ref);
544 throw new RestException(404,
'Category not found');
548 } elseif ($modulepart ==
'ecm') {
549 throw new RestException(500,
'Modulepart Ecm not implemented yet.');
564 } elseif ($modulepart ==
'contrat' || $modulepart ==
'contract') {
565 $modulepart =
'contrat';
566 require_once DOL_DOCUMENT_ROOT .
'/contrat/class/contrat.class.php';
569 $result =
$object->fetch($id, $ref);
571 throw new RestException(404,
'Contract not found');
574 $upload_dir = $conf->contrat->dir_output .
"/" .
get_exdir(0, 0, 0, 1,
$object,
'contract');
575 } elseif ($modulepart ==
'projet' || $modulepart ==
'project') {
576 $modulepart =
'project';
577 require_once DOL_DOCUMENT_ROOT .
'/projet/class/project.class.php';
580 $result =
$object->fetch($id, $ref);
582 throw new RestException(404,
'Project not found');
585 $upload_dir = $conf->projet->dir_output .
"/" .
get_exdir(0, 0, 0, 1,
$object,
'project');
586 } elseif ($modulepart ==
'mrp') {
588 require_once DOL_DOCUMENT_ROOT .
'/mrp/class/mo.class.php';
591 $result =
$object->fetch($id, $ref);
593 throw new RestException(404,
'MO not found');
596 $upload_dir = $conf->mrp->dir_output .
"/" .
get_exdir(0, 0, 0, 1,
$object,
'mrp');
598 throw new RestException(500,
'Modulepart '.$modulepart.
' not implemented yet.');
601 $objectType = $modulepart;
603 $objectType =
$object->table_element;
606 $filearray =
dol_dir_list($upload_dir, $type, $recursive,
'',
'(\.meta|_preview.*\.png)$', $sortfield, (strtolower($sortorder) ==
'desc' ? SORT_DESC : SORT_ASC), 1);
607 if (empty($filearray)) {
608 throw new RestException(404,
'Search for modulepart '.$modulepart.
' with Id '.
$object->id.(!empty(
$object->ref) ?
' or Ref '.$object->ref :
'').
' does not return any document.');
610 if ((
$object->id) > 0 && !empty($modulepart)) {
611 require_once DOL_DOCUMENT_ROOT.
'/ecm/class/ecmfiles.class.php';
613 $result = $ecmfile->fetchAll(
'',
'', 0, 0, array(
't.src_object_type' => $objectType,
't.src_object_id' =>
$object->id));
615 throw new RestException(503,
'Error when retrieve ecm list : '.$this->db->lasterror());
616 } elseif (is_array($ecmfile->lines) && count($ecmfile->lines) > 0) {
617 $count = count($filearray);
618 for ($i = 0 ; $i < $count ; $i++) {
619 foreach ($ecmfile->lines as $line) {
620 if ($filearray[$i][
'name'] == $line->filename) {
621 $filearray[$i] = array_merge($filearray[$i], (array) $line);
673 public function post($filename, $modulepart, $ref =
'', $subdir =
'', $filecontent =
'', $fileencoding =
'', $overwriteifexists = 0, $createdirifnotexists = 1)
681 $modulepartorig = $modulepart;
683 if (empty($modulepart)) {
684 throw new RestException(400,
'Modulepart not provided.');
687 $newfilecontent =
'';
688 if (empty($fileencoding)) {
689 $newfilecontent = $filecontent;
691 if ($fileencoding ==
'base64') {
692 $newfilecontent = base64_decode($filecontent);
699 $entity = DolibarrApiAccess::$user->entity;
700 if (empty($entity)) {
708 if ($modulepart ==
'facture' || $modulepart ==
'invoice') {
709 $modulepart =
'facture';
711 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
713 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'supplier_invoice') {
714 $modulepart =
'supplier_invoice';
716 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
718 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
719 $modulepart =
'commande';
721 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
723 } elseif ($modulepart ==
'commande_fournisseur' || $modulepart ==
'supplier_order') {
724 $modulepart =
'supplier_order';
726 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.class.php';
728 } elseif ($modulepart ==
'projet' || $modulepart ==
'project') {
729 require_once DOL_DOCUMENT_ROOT.
'/projet/class/project.class.php';
731 } elseif ($modulepart ==
'task' || $modulepart ==
'project_task') {
732 $modulepart =
'project_task';
734 require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
737 $task_result =
$object->fetch(
'', $ref);
740 if ($task_result > 0) {
741 $project_result =
$object->fetch_projet();
743 if ($project_result >= 0) {
747 throw new RestException(500,
'Error while fetching Task '.$ref);
749 } elseif ($modulepart ==
'product' || $modulepart ==
'produit' || $modulepart ==
'service' || $modulepart ==
'produit|service') {
750 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
752 } elseif ($modulepart ==
'expensereport') {
753 require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport.class.php';
755 } elseif ($modulepart ==
'fichinter') {
756 require_once DOL_DOCUMENT_ROOT.
'/fichinter/class/fichinter.class.php';
758 } elseif ($modulepart ==
'adherent' || $modulepart ==
'member') {
759 $modulepart =
'adherent';
760 require_once DOL_DOCUMENT_ROOT.
'/adherents/class/adherent.class.php';
762 } elseif ($modulepart ==
'proposal' || $modulepart ==
'propal' || $modulepart ==
'propale') {
763 $modulepart =
'propale';
764 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
766 } elseif ($modulepart ==
'agenda' || $modulepart ==
'action' || $modulepart ==
'event') {
767 $modulepart =
'agenda';
768 require_once DOL_DOCUMENT_ROOT .
'/comm/action/class/actioncomm.class.php';
770 } elseif ($modulepart ==
'contact' || $modulepart ==
'socpeople') {
771 $modulepart =
'contact';
772 require_once DOL_DOCUMENT_ROOT.
'/contact/class/contact.class.php';
775 } elseif ($modulepart ==
'contrat' || $modulepart ==
'contract') {
776 $modulepart =
'contrat';
777 require_once DOL_DOCUMENT_ROOT .
'/contrat/class/contrat.class.php';
779 } elseif ($modulepart ==
'mrp') {
781 require_once DOL_DOCUMENT_ROOT .
'/mrp/class/mo.class.php';
785 throw new RestException(500,
'Modulepart '.$modulepart.
' not implemented yet.');
791 $result =
$object->fetch($ref);
793 $result =
$object->fetch(
'', $ref);
797 throw new RestException(404,
"Object with ref '".$ref.
"' was not found.");
798 } elseif ($result < 0) {
799 throw new RestException(500,
'Error while fetching object: '.
$object->error);
804 throw new RestException(404,
'The object '.$modulepart.
" with ref '".$ref.
"' was not found.");
809 if ($modulepart ==
'supplier_invoice') {
814 if ($modulepart !=
'ecm') {
815 $relativefile = $tmpreldir.dol_sanitizeFileName(
$object->ref);
817 $upload_dir = $tmp[
'original_file'];
819 if (!DolibarrApiAccess::$user->hasRight(
'ecm',
'upload')) {
820 throw new RestException(403,
'Missing permission to upload files in ECM module');
822 $upload_dir = $conf->medias->multidir_output[$conf->entity];
825 if (empty($upload_dir) || $upload_dir ==
'/') {
826 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.');
829 if ($modulepart ==
'invoice') {
830 $modulepart =
'facture';
832 if ($modulepart ==
'member') {
833 $modulepart =
'adherent';
837 if ($modulepart !=
'ecm') {
838 $relativefile = $subdir;
840 $upload_dir = $tmp[
'original_file'];
842 if (!DolibarrApiAccess::$user->hasRight(
'ecm',
'upload')) {
843 throw new RestException(403,
'Missing permission to upload files in ECM module');
845 $upload_dir = $conf->medias->multidir_output[$conf->entity];
848 if (empty($upload_dir) || $upload_dir ==
'/') {
849 if (!empty($tmp[
'error'])) {
850 throw new RestException(403,
'Error returned by dol_check_secure_access_document: '.$tmp[
'error']);
852 throw new RestException(400,
'This value of modulepart ('.$modulepart.
') is not allowed with this value of subdir ('.$relativefile.
')');
860 if (!empty($createdirifnotexists)) {
862 throw new RestException(500,
'Error while trying to create directory '.$upload_dir);
866 $destfile = $upload_dir.
'/'.$original_file;
867 $destfiletmp = DOL_DATA_ROOT.
'/admin/temp/'.$original_file;
872 throw new RestException(400,
'Directory does not exists : '.dirname($destfile));
875 if (!$overwriteifexists &&
dol_is_file($destfile)) {
876 throw new RestException(400,
"File with name '".$original_file.
"' already exists.");
884 $fhandle = @fopen($destfiletmp,
'w');
886 $nbofbyteswrote = fwrite($fhandle, $newfilecontent);
890 throw new RestException(500,
"Failed to open file '".$destfiletmp.
"' for write");
893 $disablevirusscan = 0;
894 $src_file = $destfiletmp;
895 $dest_file = $destfile;
899 if (empty($disablevirusscan) && file_exists($src_file)) {
901 if (count($checkvirusarray)) {
902 dol_syslog(
'Files.lib::dol_move_uploaded_file File "'.$src_file.
'" (target name "'.$dest_file.
'") KO with antivirus: errors='.implode(
',', $checkvirusarray), LOG_WARNING);
903 throw new RestException(500,
'ErrorFileIsInfectedWithAVirus: '.implode(
',', $checkvirusarray));
912 $publicmediasdirwithslash = $conf->medias->multidir_output[$conf->entity];
913 if (!preg_match(
'/\/$/', $publicmediasdirwithslash)) {
914 $publicmediasdirwithslash .=
'/';
917 if (strpos($upload_dir, $publicmediasdirwithslash) !== 0 || !
getDolGlobalInt(
"MAIN_DOCUMENT_DISABLE_NOEXE_IN_MEDIAS_DIR")) {
918 $dest_file .=
'.noexe';
924 if (preg_match(
'/^\./', basename($src_file)) || preg_match(
'/\.\./', $src_file) || preg_match(
'/[<>|]/', $src_file)) {
925 dol_syslog(
"Refused to deliver file ".$src_file, LOG_WARNING);
926 throw new RestException(500,
"Refused to deliver file ".$src_file);
931 if (preg_match(
'/^\./', basename($dest_file)) || preg_match(
'/\.\./', $dest_file) || preg_match(
'/[<>|]/', $dest_file)) {
932 dol_syslog(
"Refused to deliver file ".$dest_file, LOG_WARNING);
933 throw new RestException(500,
"Refused to deliver file ".$dest_file);
936 $moreinfo = array(
'note_private' =>
'File uploaded using API /documents from IP '.
getUserRemoteIP());
938 $moreinfo[
'src_object_type'] =
$object->table_element;
939 $moreinfo[
'src_object_id'] =
$object->id;
943 $result =
dol_move($destfiletmp, $dest_file, 0, $overwriteifexists, 1, 1, $moreinfo);
945 throw new RestException(500,
"Failed to move file into '".$dest_file.
"'");
966 public function delete($modulepart, $original_file)
968 global $conf, $langs;
970 if (empty($modulepart)) {
971 throw new RestException(400,
'bad value for parameter modulepart');
973 if (empty($original_file)) {
974 throw new RestException(400,
'bad value for parameter original_file');
978 $entity = $conf->entity;
989 $relativefile = $original_file;
992 $accessallowed = $check_access[
'accessallowed'];
993 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
994 $original_file = $check_access[
'original_file'];
996 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
997 throw new RestException(403);
999 if (!$accessallowed) {
1000 throw new RestException(403);
1003 $filename = basename($original_file);
1004 $original_file_osencoded =
dol_osencode($original_file);
1006 if (!file_exists($original_file_osencoded)) {
1007 dol_syslog(
"Try to download not found file ".$original_file_osencoded, LOG_WARNING);
1008 throw new RestException(404,
'File not found');
1011 if (@unlink($original_file_osencoded)) {
1015 'message' =>
'Document deleted'
1020 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)