20 use Luracast\Restler\RestException;
21 use Luracast\Restler\Format\UploadFormat;
23 require_once DOL_DOCUMENT_ROOT.
'/main.inc.php';
24 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
38 public static $DOCUMENT_FIELDS = array(
68 public function index($modulepart, $original_file =
'')
72 if (empty($modulepart)) {
73 throw new RestException(400,
'bad value for parameter modulepart');
75 if (empty($original_file)) {
76 throw new RestException(400,
'bad value for parameter original_file');
80 $entity = $conf->entity;
91 $relativefile = $original_file;
94 $accessallowed = $check_access[
'accessallowed'];
95 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
96 $original_file = $check_access[
'original_file'];
98 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
99 throw new RestException(401);
101 if (!$accessallowed) {
102 throw new RestException(401);
105 $filename = basename($original_file);
106 $original_file_osencoded =
dol_osencode($original_file);
108 if (!file_exists($original_file_osencoded)) {
109 dol_syslog(
"Try to download not found file ".$original_file_osencoded, LOG_WARNING);
110 throw new RestException(404,
'File not found');
113 $file_content = file_get_contents($original_file_osencoded);
114 return array(
'filename'=>$filename,
'content-type' =>
dol_mimetype($filename),
'filesize'=>filesize($original_file),
'content'=>base64_encode($file_content),
'encoding'=>
'base64');
137 public function builddoc($modulepart, $original_file =
'', $doctemplate =
'', $langcode =
'')
139 global $conf, $langs;
141 if (empty($modulepart)) {
142 throw new RestException(400,
'bad value for parameter modulepart');
144 if (empty($original_file)) {
145 throw new RestException(400,
'bad value for parameter original_file');
148 $outputlangs = $langs;
149 if ($langcode && $langs->defaultlang != $langcode) {
151 $outputlangs->setDefaultLang($langcode);
155 $entity = $conf->entity;
166 $relativefile = $original_file;
169 $accessallowed = $check_access[
'accessallowed'];
170 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
171 $original_file = $check_access[
'original_file'];
173 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
174 throw new RestException(401);
176 if (!$accessallowed) {
177 throw new RestException(401);
181 $hidedetails = empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS) ? 0 : 1;
182 $hidedesc = empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DESC) ? 0 : 1;
183 $hideref = empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_REF) ? 0 : 1;
187 if ($modulepart ==
'facture' || $modulepart ==
'invoice') {
188 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
190 $result = $this->invoice->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
192 throw new RestException(404,
'Invoice not found');
195 $templateused = $doctemplate ? $doctemplate : $this->invoice->model_pdf;
196 $result = $this->invoice->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
198 throw new RestException(500,
'Error generating document');
200 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
201 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
203 $result = $this->order->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
205 throw new RestException(404,
'Order not found');
207 $templateused = $doctemplate ? $doctemplate : $this->order->model_pdf;
208 $result = $this->order->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
210 throw new RestException(500,
'Error generating document');
212 } elseif ($modulepart ==
'propal' || $modulepart ==
'proposal') {
213 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
214 $this->propal =
new Propal($this->
db);
215 $result = $this->propal->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
217 throw new RestException(404,
'Proposal not found');
219 $templateused = $doctemplate ? $doctemplate : $this->propal->model_pdf;
220 $result = $this->propal->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
222 throw new RestException(500,
'Error generating document');
225 throw new RestException(403,
'Generation not available for this modulepart');
228 $filename = basename($original_file);
229 $original_file_osencoded =
dol_osencode($original_file);
231 if (!file_exists($original_file_osencoded)) {
232 throw new RestException(404,
'File not found');
235 $file_content = file_get_contents($original_file_osencoded);
236 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');
260 if (empty($modulepart)) {
261 throw new RestException(400,
'bad value for parameter modulepart');
264 if (empty($id) && empty($ref)) {
265 throw new RestException(400,
'bad value for parameter id or ref');
268 $id = (empty($id) ? 0 : $id);
272 if ($modulepart ==
'societe' || $modulepart ==
'thirdparty') {
273 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
275 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
276 throw new RestException(401);
280 $result = $object->fetch($id, $ref);
282 throw new RestException(404,
'Thirdparty not found');
285 $upload_dir = $conf->societe->multidir_output[$object->entity].
"/".$object->id;
286 } elseif ($modulepart ==
'user') {
287 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
290 if (!DolibarrApiAccess::$user->rights->user->user->lire && DolibarrApiAccess::$user->id != $id) {
291 throw new RestException(401);
294 $object =
new User($this->
db);
295 $result = $object->fetch($id, $ref);
297 throw new RestException(404,
'User not found');
300 $upload_dir = $conf->user->dir_output.
'/'.
get_exdir(0, 0, 0, 0, $object,
'user').
'/'.$object->id;
301 } elseif ($modulepart ==
'adherent' || $modulepart ==
'member') {
302 require_once DOL_DOCUMENT_ROOT.
'/adherents/class/adherent.class.php';
304 if (!DolibarrApiAccess::$user->rights->adherent->lire) {
305 throw new RestException(401);
309 $result = $object->fetch($id, $ref);
311 throw new RestException(404,
'Member not found');
314 $upload_dir = $conf->adherent->dir_output.
"/".
get_exdir(0, 0, 0, 1, $object,
'member');
315 } elseif ($modulepart ==
'propal' || $modulepart ==
'proposal') {
316 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
318 if (!DolibarrApiAccess::$user->rights->propal->lire) {
319 throw new RestException(401);
323 $result = $object->fetch($id, $ref);
325 throw new RestException(404,
'Proposal not found');
328 $upload_dir = $conf->propal->multidir_output[$object->entity].
"/".
get_exdir(0, 0, 0, 1, $object,
'propal');
329 } elseif ($modulepart ==
'supplier_proposal') {
330 require_once DOL_DOCUMENT_ROOT.
'/supplier_proposal/class/supplier_proposal.class.php';
332 if (!DolibarrApiAccess::$user->rights->supplier_proposal->read) {
333 throw new RestException(401);
337 $result = $object->fetch($id, $ref);
339 throw new RestException(404,
'Supplier proposal not found');
342 $upload_dir = $conf->propal->multidir_output[$object->entity].
"/".
get_exdir(0, 0, 0, 1, $object,
'propal');
343 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
344 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
346 if (!DolibarrApiAccess::$user->rights->commande->lire) {
347 throw new RestException(401);
351 $result = $object->fetch($id, $ref);
353 throw new RestException(404,
'Order not found');
356 $upload_dir = $conf->commande->dir_output.
"/".
get_exdir(0, 0, 0, 1, $object,
'commande');
357 } elseif ($modulepart ==
'commande_fournisseur' || $modulepart ==
'supplier_order') {
358 $modulepart =
'supplier_order';
360 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.class.php';
362 if (empty(DolibarrApiAccess::$user->rights->fournisseur->commande->lire) && empty(DolibarrApiAccess::$user->rights->supplier_order->lire)) {
363 throw new RestException(401);
367 $result = $object->fetch($id, $ref);
369 throw new RestException(404,
'Purchase order not found');
372 $upload_dir = $conf->fournisseur->dir_output.
"/facture/".
get_exdir($object->id, 2, 0, 0, $object,
'invoice_supplier').dol_sanitizeFileName($object->ref);
373 } elseif ($modulepart ==
'shipment' || $modulepart ==
'expedition') {
374 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
376 if (!DolibarrApiAccess::$user->rights->expedition->lire) {
377 throw new RestException(401);
381 $result = $object->fetch($id, $ref);
383 throw new RestException(404,
'Shipment not found');
386 $upload_dir = $conf->expedition->dir_output.
"/sending/".
get_exdir(0, 0, 0, 1, $object,
'shipment');
387 } elseif ($modulepart ==
'facture' || $modulepart ==
'invoice') {
388 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
390 if (!DolibarrApiAccess::$user->rights->facture->lire) {
391 throw new RestException(401);
395 $result = $object->fetch($id, $ref);
397 throw new RestException(404,
'Invoice not found');
400 $upload_dir = $conf->facture->dir_output.
"/".
get_exdir(0, 0, 0, 1, $object,
'invoice');
401 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'supplier_invoice') {
402 $modulepart =
'supplier_invoice';
404 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
406 if (empty(DolibarrApiAccess::$user->rights->fournisseur->facture->lire) && empty(DolibarrApiAccess::$user->rights->supplier_invoice->lire)) {
407 throw new RestException(401);
411 $result = $object->fetch($id, $ref);
413 throw new RestException(404,
'Invoice not found');
416 $upload_dir = $conf->fournisseur->dir_output.
"/facture/".
get_exdir($object->id, 2, 0, 0, $object,
'invoice_supplier').dol_sanitizeFileName($object->ref);
417 } elseif ($modulepart ==
'produit' || $modulepart ==
'product') {
418 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
420 if (!DolibarrApiAccess::$user->rights->produit->lire) {
421 throw new RestException(401);
425 $result = $object->fetch($id, $ref);
427 throw new RestException(404,
'Product not found');
428 } elseif ($result < 0) {
429 throw new RestException(500,
'Error while fetching object: '.$object->error);
432 $upload_dir = $conf->product->multidir_output[$object->entity].
'/'.
get_exdir(0, 0, 0, 1, $object,
'product');
433 } elseif ($modulepart ==
'agenda' || $modulepart ==
'action' || $modulepart ==
'event') {
434 require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
436 if (!DolibarrApiAccess::$user->rights->agenda->myactions->read && !DolibarrApiAccess::$user->rights->agenda->allactions->read) {
437 throw new RestException(401);
441 $result = $object->fetch($id, $ref);
443 throw new RestException(404,
'Event not found');
447 } elseif ($modulepart ==
'expensereport') {
448 require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport.class.php';
450 if (!DolibarrApiAccess::$user->rights->expensereport->read && !DolibarrApiAccess::$user->rights->expensereport->read) {
451 throw new RestException(401);
455 $result = $object->fetch($id, $ref);
457 throw new RestException(404,
'Expense report not found');
461 } elseif ($modulepart ==
'knowledgemanagement') {
462 require_once DOL_DOCUMENT_ROOT.
'/knowledgemanagement/class/knowledgerecord.class.php';
464 if (!DolibarrApiAccess::$user->rights->knowledgemanagement->knowledgerecord->read && !DolibarrApiAccess::$user->rights->knowledgemanagement->knowledgerecord->read) {
465 throw new RestException(401);
469 $result = $object->fetch($id, $ref);
471 throw new RestException(404,
'KM article not found');
474 $upload_dir = $conf->knowledgemanagement->dir_output.
'/knowledgerecord/'.
dol_sanitizeFileName($object->ref);
475 } elseif ($modulepart ==
'categorie' || $modulepart ==
'category') {
476 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
478 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
479 throw new RestException(401);
483 $result = $object->fetch($id, $ref);
485 throw new RestException(404,
'Category not found');
488 $upload_dir = $conf->categorie->multidir_output[$object->entity].
'/'.
get_exdir($object->id, 2, 0, 0, $object,
'category').$object->id.
"/photos/".
dol_sanitizeFileName($object->ref);
489 } elseif ($modulepart ==
'ecm') {
490 throw new RestException(500,
'Modulepart Ecm not implemented yet.');
506 throw new RestException(500,
'Modulepart '.$modulepart.
' not implemented yet.');
509 $objectType = $modulepart;
510 if (! empty($object->id) && ! empty($object->table_element)) {
511 $objectType = $object->table_element;
514 $filearray =
dol_dir_list($upload_dir, $type, $recursive,
'',
'(\.meta|_preview.*\.png)$', $sortfield, (strtolower($sortorder) ==
'desc' ?SORT_DESC:SORT_ASC), 1);
515 if (empty($filearray)) {
516 throw new RestException(404,
'Search for modulepart '.$modulepart.
' with Id '.$object->id.(!empty($object->ref) ?
' or Ref '.$object->ref :
'').
' does not return any document.');
518 if (($object->id) > 0 && !empty($modulepart)) {
519 require_once DOL_DOCUMENT_ROOT.
'/ecm/class/ecmfiles.class.php';
521 $result = $ecmfile->fetchAll(
'',
'', 0, 0, array(
't.src_object_type' => $objectType,
't.src_object_id' => $object->id));
523 throw new RestException(503,
'Error when retrieve ecm list : '.$this->
db->lasterror());
524 } elseif (is_array($ecmfile->lines) && count($ecmfile->lines) > 0) {
525 $count = count($filearray);
526 for ($i = 0 ; $i < $count ; $i++) {
527 if ($filearray[$i][
'name'] == $ecmfile->lines[$i]->filename) $filearray[$i] = array_merge($filearray[$i], (array) $ecmfile->lines[0]);
575 public function post($filename, $modulepart, $ref =
'', $subdir =
'', $filecontent =
'', $fileencoding =
'', $overwriteifexists = 0, $createdirifnotexists = 1)
583 if (empty($modulepart)) {
584 throw new RestException(400,
'Modulepart not provided.');
587 if (!DolibarrApiAccess::$user->rights->ecm->upload) {
588 throw new RestException(401);
591 $newfilecontent =
'';
592 if (empty($fileencoding)) {
593 $newfilecontent = $filecontent;
595 if ($fileencoding ==
'base64') {
596 $newfilecontent = base64_decode($filecontent);
603 $entity = DolibarrApiAccess::$user->entity;
604 if (empty($entity)) {
611 if ($modulepart ==
'facture' || $modulepart ==
'invoice') {
612 $modulepart =
'facture';
614 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
616 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'supplier_invoice') {
617 $modulepart =
'supplier_invoice';
619 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
621 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
622 $modulepart =
'commande';
624 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
626 } elseif ($modulepart ==
'commande_fournisseur' || $modulepart ==
'supplier_order') {
627 $modulepart =
'supplier_order';
629 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.class.php';
631 } elseif ($modulepart ==
'project') {
632 require_once DOL_DOCUMENT_ROOT.
'/projet/class/project.class.php';
634 } elseif ($modulepart ==
'task' || $modulepart ==
'project_task') {
635 $modulepart =
'project_task';
637 require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
638 $object =
new Task($this->
db);
640 $task_result = $object->fetch(
'', $ref);
643 if ($task_result > 0) {
644 $project_result = $object->fetch_projet();
646 if ($project_result >= 0) {
650 throw new RestException(500,
'Error while fetching Task '.$ref);
652 } elseif ($modulepart ==
'product' || $modulepart ==
'produit' || $modulepart ==
'service' || $modulepart ==
'produit|service') {
653 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
655 } elseif ($modulepart ==
'expensereport') {
656 require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport.class.php';
658 } elseif ($modulepart ==
'fichinter') {
659 require_once DOL_DOCUMENT_ROOT.
'/fichinter/class/fichinter.class.php';
661 } elseif ($modulepart ==
'adherent' || $modulepart ==
'member') {
662 $modulepart =
'adherent';
663 require_once DOL_DOCUMENT_ROOT.
'/adherents/class/adherent.class.php';
665 } elseif ($modulepart ==
'proposal' || $modulepart ==
'propal' || $modulepart ==
'propale') {
666 $modulepart =
'propale';
667 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
671 throw new RestException(500,
'Modulepart '.$modulepart.
' not implemented yet.');
674 if (is_object($object)) {
675 $result = $object->fetch(
'', $ref);
678 throw new RestException(404,
"Object with ref '".$ref.
"' was not found.");
679 } elseif ($result < 0) {
680 throw new RestException(500,
'Error while fetching object: '.$object->error);
684 if (!($object->id > 0)) {
685 throw new RestException(404,
'The object '.$modulepart.
" with ref '".$ref.
"' was not found.");
690 if ($modulepart ==
'supplier_invoice') {
691 $tmpreldir =
get_exdir($object->id, 2, 0, 0, $object,
'invoice_supplier');
694 $relativefile = $tmpreldir.dol_sanitizeFileName($object->ref);
697 $upload_dir = $tmp[
'original_file'];
699 if (empty($upload_dir) || $upload_dir ==
'/') {
700 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.');
703 if ($modulepart ==
'invoice') {
704 $modulepart =
'facture';
706 if ($modulepart ==
'member') {
707 $modulepart =
'adherent';
710 $relativefile = $subdir;
712 $upload_dir = $tmp[
'original_file'];
714 if (empty($upload_dir) || $upload_dir ==
'/') {
715 if (!empty($tmp[
'error'])) {
716 throw new RestException(401,
'Error returned by dol_check_secure_access_document: '.$tmp[
'error']);
718 throw new RestException(500,
'This value of modulepart ('.$modulepart.
') is not allowed with this value of subdir ('.$relativefile.
')');
726 if (!empty($createdirifnotexists)) {
728 throw new RestException(500,
'Error while trying to create directory '.$upload_dir);
732 $destfile = $upload_dir.
'/'.$original_file;
733 $destfiletmp = DOL_DATA_ROOT.
'/admin/temp/'.$original_file;
738 throw new RestException(401,
'Directory not exists : '.dirname($destfile));
741 if (!$overwriteifexists &&
dol_is_file($destfile)) {
742 throw new RestException(500,
"File with name '".$original_file.
"' already exists.");
745 $fhandle = @fopen($destfiletmp,
'w');
747 $nbofbyteswrote = fwrite($fhandle, $newfilecontent);
749 @chmod($destfiletmp, octdec($conf->global->MAIN_UMASK));
751 throw new RestException(500,
"Failed to open file '".$destfiletmp.
"' for write");
754 $result =
dol_move($destfiletmp, $destfile, 0, $overwriteifexists, 1, 1);
756 throw new RestException(500,
"Failed to move file into '".$destfile.
"'");
775 public function delete($modulepart, $original_file)
777 global $conf, $langs;
779 if (empty($modulepart)) {
780 throw new RestException(400,
'bad value for parameter modulepart');
782 if (empty($original_file)) {
783 throw new RestException(400,
'bad value for parameter original_file');
787 $entity = $conf->entity;
798 $relativefile = $original_file;
801 $accessallowed = $check_access[
'accessallowed'];
802 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
803 $original_file = $check_access[
'original_file'];
805 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
806 throw new RestException(401);
808 if (!$accessallowed) {
809 throw new RestException(401);
812 $filename = basename($original_file);
813 $original_file_osencoded =
dol_osencode($original_file);
815 if (!file_exists($original_file_osencoded)) {
816 dol_syslog(
"Try to download not found file ".$original_file_osencoded, LOG_WARNING);
817 throw new RestException(404,
'File not found');
820 if (@unlink($original_file_osencoded)) {
824 'message' =>
'Document deleted'
829 throw new RestException(401);
844 foreach (Documents::$DOCUMENT_FIELDS as $field) {
845 if (!isset($data[$field])) {
846 throw new RestException(400,
"$field field missing");
848 $result[$field] = $data[$field];