20 use Luracast\Restler\RestException;
21 use Luracast\Restler\Format\UploadFormat;
24 require_once DOL_DOCUMENT_ROOT.
'/main.inc.php';
25 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
39 static $DOCUMENT_FIELDS = array(
69 public function index($modulepart, $original_file =
'')
73 if (empty($modulepart)) {
74 throw new RestException(400,
'bad value for parameter modulepart');
76 if (empty($original_file)) {
77 throw new RestException(400,
'bad value for parameter original_file');
81 $entity = $conf->entity;
92 $relativefile = $original_file;
95 $accessallowed = $check_access[
'accessallowed'];
96 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
97 $original_file = $check_access[
'original_file'];
99 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
100 throw new RestException(401);
102 if (!$accessallowed) {
103 throw new RestException(401);
106 $filename = basename($original_file);
107 $original_file_osencoded =
dol_osencode($original_file);
109 if (!file_exists($original_file_osencoded))
111 dol_syslog(
"Try to download not found file ".$original_file_osencoded, LOG_WARNING);
112 throw new RestException(404,
'File not found');
115 $file_content = file_get_contents($original_file_osencoded);
116 return array(
'filename'=>$filename,
'content-type' =>
dol_mimetype($filename),
'filesize'=>filesize($original_file),
'content'=>base64_encode($file_content),
'encoding'=>
'base64');
139 public function builddoc($modulepart, $original_file =
'', $doctemplate =
'', $langcode =
'')
141 global $conf, $langs;
143 if (empty($modulepart)) {
144 throw new RestException(400,
'bad value for parameter modulepart');
146 if (empty($original_file)) {
147 throw new RestException(400,
'bad value for parameter original_file');
150 $outputlangs = $langs;
151 if ($langcode && $langs->defaultlang != $langcode)
154 $outputlangs->setDefaultLang($langcode);
158 $entity = $conf->entity;
169 $relativefile = $original_file;
172 $accessallowed = $check_access[
'accessallowed'];
173 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
174 $original_file = $check_access[
'original_file'];
176 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
177 throw new RestException(401);
179 if (!$accessallowed) {
180 throw new RestException(401);
184 $hidedetails = empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS) ? 0 : 1;
185 $hidedesc = empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DESC) ? 0 : 1;
186 $hideref = empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_REF) ? 0 : 1;
190 if ($modulepart ==
'facture' || $modulepart ==
'invoice')
192 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
194 $result = $this->invoice->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
196 throw new RestException(404,
'Invoice not found');
199 $templateused = $doctemplate ? $doctemplate : $this->invoice->model_pdf;
200 $result = $this->invoice->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
202 throw new RestException(500,
'Error generating document');
205 elseif ($modulepart ==
'commande' || $modulepart ==
'order')
207 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
209 $result = $this->order->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
211 throw new RestException(404,
'Order not found');
213 $templateused = $doctemplate ? $doctemplate : $this->order->model_pdf;
214 $result = $this->order->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
216 throw new RestException(500,
'Error generating document');
219 elseif ($modulepart ==
'propal' || $modulepart ==
'proposal')
221 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
222 $this->propal =
new Propal($this->
db);
223 $result = $this->propal->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
225 throw new RestException(404,
'Proposal not found');
227 $templateused = $doctemplate ? $doctemplate : $this->propal->model_pdf;
228 $result = $this->propal->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
230 throw new RestException(500,
'Error generating document');
233 throw new RestException(403,
'Generation not available for this modulepart');
236 $filename = basename($original_file);
237 $original_file_osencoded =
dol_osencode($original_file);
239 if (!file_exists($original_file_osencoded))
241 throw new RestException(404,
'File not found');
244 $file_content = file_get_contents($original_file_osencoded);
245 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');
269 if (empty($modulepart)) {
270 throw new RestException(400,
'bad value for parameter modulepart');
273 if (empty($id) && empty($ref)) {
274 throw new RestException(400,
'bad value for parameter id or ref');
277 $id = (empty($id) ? 0 : $id);
281 if ($modulepart ==
'societe' || $modulepart ==
'thirdparty')
283 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
285 if (!DolibarrApiAccess::$user->rights->societe->lire) {
286 throw new RestException(401);
290 $result = $object->fetch($id, $ref);
292 throw new RestException(404,
'Thirdparty not found');
295 $upload_dir = $conf->societe->multidir_output[$object->entity].
"/".$object->id;
297 elseif ($modulepart ==
'user')
299 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
302 if (!DolibarrApiAccess::$user->rights->user->user->lire && DolibarrApiAccess::$user->id != $id) {
303 throw new RestException(401);
306 $object =
new User($this->
db);
307 $result = $object->fetch($id, $ref);
309 throw new RestException(404,
'User not found');
312 $upload_dir = $conf->user->dir_output.
'/'.
get_exdir(0, 0, 0, 0, $object,
'user').
'/'.$object->id;
314 elseif ($modulepart ==
'adherent' || $modulepart ==
'member')
316 require_once DOL_DOCUMENT_ROOT.
'/adherents/class/adherent.class.php';
318 if (!DolibarrApiAccess::$user->rights->adherent->lire) {
319 throw new RestException(401);
323 $result = $object->fetch($id, $ref);
325 throw new RestException(404,
'Member not found');
328 $upload_dir = $conf->adherent->dir_output.
"/".
get_exdir(0, 0, 0, 1, $object,
'member');
330 elseif ($modulepart ==
'propal' || $modulepart ==
'proposal')
332 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
334 if (!DolibarrApiAccess::$user->rights->propal->lire) {
335 throw new RestException(401);
339 $result = $object->fetch($id, $ref);
341 throw new RestException(404,
'Proposal not found');
344 $upload_dir = $conf->propal->multidir_output[$object->entity].
"/".
get_exdir(0, 0, 0, 1, $object,
'propal');
346 elseif ($modulepart ==
'commande' || $modulepart ==
'order')
348 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
350 if (!DolibarrApiAccess::$user->rights->commande->lire) {
351 throw new RestException(401);
355 $result = $object->fetch($id, $ref);
357 throw new RestException(404,
'Order not found');
360 $upload_dir = $conf->commande->dir_output.
"/".
get_exdir(0, 0, 0, 1, $object,
'commande');
362 elseif ($modulepart ==
'shipment' || $modulepart ==
'expedition')
364 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
366 if (!DolibarrApiAccess::$user->rights->expedition->lire) {
367 throw new RestException(401);
371 $result = $object->fetch($id, $ref);
373 throw new RestException(404,
'Shipment not found');
376 $upload_dir = $conf->expedition->dir_output.
"/sending/".
get_exdir(0, 0, 0, 1, $object,
'shipment');
378 elseif ($modulepart ==
'facture' || $modulepart ==
'invoice')
380 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
382 if (!DolibarrApiAccess::$user->rights->facture->lire) {
383 throw new RestException(401);
387 $result = $object->fetch($id, $ref);
389 throw new RestException(404,
'Invoice not found');
392 $upload_dir = $conf->facture->dir_output.
"/".
get_exdir(0, 0, 0, 1, $object,
'invoice');
394 elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'supplier_invoice')
396 $modulepart =
'supplier_invoice';
398 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
400 if (!DolibarrApiAccess::$user->rights->fournisseur->facture->lire) {
401 throw new RestException(401);
405 $result = $object->fetch($id, $ref);
407 throw new RestException(404,
'Invoice not found');
410 $upload_dir = $conf->fournisseur->dir_output.
"/facture/".
get_exdir($object->id, 2, 0, 0, $object,
'invoice_supplier').dol_sanitizeFileName($object->ref);
412 elseif ($modulepart ==
'produit' || $modulepart ==
'product')
414 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
416 if (!DolibarrApiAccess::$user->rights->produit->lire) {
417 throw new RestException(401);
421 $result = $object->fetch($id, $ref);
423 throw new RestException(404,
'Product not found');
424 } elseif ($result < 0) {
425 throw new RestException(500,
'Error while fetching object: '.$object->error);
428 $upload_dir = $conf->product->multidir_output[$object->entity].
'/'.
get_exdir(0, 0, 0, 1, $object,
'product');
430 elseif ($modulepart ==
'agenda' || $modulepart ==
'action' || $modulepart ==
'event')
432 require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
434 if (!DolibarrApiAccess::$user->rights->agenda->myactions->read && !DolibarrApiAccess::$user->rights->agenda->allactions->read) {
435 throw new RestException(401);
439 $result = $object->fetch($id, $ref);
441 throw new RestException(404,
'Event not found');
446 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');
462 elseif ($modulepart ==
'categorie' || $modulepart ==
'category')
464 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
466 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
467 throw new RestException(401);
471 $result = $object->fetch($id, $ref);
473 throw new RestException(404,
'Category not found');
476 $upload_dir = $conf->categorie->multidir_output[$object->entity].
'/'.
get_exdir($object->id, 2, 0, 0, $object,
'category').$object->id.
"/photos/".
dol_sanitizeFileName($object->ref);
477 } elseif ($modulepart ==
'ecm') {
478 throw new RestException(500,
'Modulepart Ecm not implemented yet.');
494 throw new RestException(500,
'Modulepart '.$modulepart.
' not implemented yet.');
497 $filearray =
dol_dir_list($upload_dir, $type, $recursive,
'',
'(\.meta|_preview.*\.png)$', $sortfield, (strtolower($sortorder) ==
'desc' ?SORT_DESC:SORT_ASC), 1);
498 if (empty($filearray)) {
499 throw new RestException(404,
'Search for modulepart '.$modulepart.
' with Id '.$object->id.(!empty($object->ref) ?
' or Ref '.$object->ref :
'').
' does not return any document.');
501 if (($object->id) > 0 && !empty($modulepart)) {
502 require_once DOL_DOCUMENT_ROOT .
'/ecm/class/ecmfiles.class.php';
504 $result = $ecmfile->fetchAll(
'',
'', 0, 0, array(
't.src_object_type' => $modulepart,
't.src_object_id' => $object->id));
506 throw new RestException(503,
'Error when retrieve ecm list : ' . $this->
db->lasterror());
507 } elseif (is_array($ecmfile->lines) && count($ecmfile->lines) > 0) {
508 $filearray[
'ecmfiles_infos'] = $ecmfile->lines;
555 public function post($filename, $modulepart, $ref =
'', $subdir =
'', $filecontent =
'', $fileencoding =
'', $overwriteifexists = 0, $createdirifnotexists = 1)
564 if (empty($modulepart))
566 throw new RestException(400,
'Modulepart not provided.');
569 if (!DolibarrApiAccess::$user->rights->ecm->upload) {
570 throw new RestException(401);
573 $newfilecontent =
'';
574 if (empty($fileencoding)) $newfilecontent = $filecontent;
575 if ($fileencoding ==
'base64') $newfilecontent = base64_decode($filecontent);
581 $entity = DolibarrApiAccess::$user->entity;
582 if (empty($entity)) $entity = 1;
588 if ($modulepart ==
'facture' || $modulepart ==
'invoice')
590 $modulepart =
'facture';
592 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
595 elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'supplier_invoice')
597 $modulepart =
'supplier_invoice';
599 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
602 elseif ($modulepart ==
'project')
604 require_once DOL_DOCUMENT_ROOT.
'/projet/class/project.class.php';
607 elseif ($modulepart ==
'task' || $modulepart ==
'project_task')
609 $modulepart =
'project_task';
611 require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
612 $object =
new Task($this->
db);
614 $task_result = $object->fetch(
'', $ref);
617 if ($task_result > 0)
619 $project_result = $object->fetch_projet();
621 if ($project_result >= 0)
626 throw new RestException(500,
'Error while fetching Task '.$ref);
629 elseif ($modulepart ==
'product' || $modulepart ==
'produit' || $modulepart ==
'service' || $modulepart ==
'produit|service')
631 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
634 elseif ($modulepart ==
'expensereport')
636 require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport.class.php';
639 elseif ($modulepart ==
'adherent' || $modulepart ==
'member')
641 $modulepart =
'adherent';
642 require_once DOL_DOCUMENT_ROOT.
'/adherents/class/adherent.class.php';
645 elseif ($modulepart ==
'proposal' || $modulepart ==
'propal' || $modulepart ==
'propale')
647 $modulepart =
'propale';
648 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
652 throw new RestException(500,
'Modulepart '.$modulepart.
' not implemented yet.');
655 if (is_object($object))
657 $result = $object->fetch(
'', $ref);
661 throw new RestException(404,
"Object with ref '".$ref.
"' was not found.");
665 throw new RestException(500,
'Error while fetching object: '.$object->error);
669 if (!($object->id > 0)) {
670 throw new RestException(404,
'The object '.$modulepart.
" with ref '".$ref.
"' was not found.");
675 if ($modulepart ==
'supplier_invoice') {
676 $tmpreldir =
get_exdir($object->id, 2, 0, 0, $object,
'invoice_supplier');
679 $relativefile = $tmpreldir.dol_sanitizeFileName($object->ref);
682 $upload_dir = $tmp[
'original_file'];
684 if (empty($upload_dir) || $upload_dir ==
'/')
686 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.');
689 if ($modulepart ==
'invoice') $modulepart =
'facture';
690 if ($modulepart ==
'member') $modulepart =
'adherent';
692 $relativefile = $subdir;
694 $upload_dir = $tmp[
'original_file'];
696 if (empty($upload_dir) || $upload_dir ==
'/') {
697 if (!empty($tmp[
'error'])) {
698 throw new RestException(401,
'Error returned by dol_check_secure_access_document: '.$tmp[
'error']);
700 throw new RestException(500,
'This value of modulepart ('.$modulepart.
') is not allowed with this value of subdir ('.$relativefile.
')');
708 if (!empty($createdirifnotexists)) {
710 throw new RestException(500,
'Error while trying to create directory '.$upload_dir);
714 $destfile = $upload_dir.
'/'.$original_file;
715 $destfiletmp = DOL_DATA_ROOT.
'/admin/temp/'.$original_file;
720 throw new RestException(401,
'Directory not exists : '.dirname($destfile));
723 if (!$overwriteifexists &&
dol_is_file($destfile)) {
724 throw new RestException(500,
"File with name '".$original_file.
"' already exists.");
727 $fhandle = @fopen($destfiletmp,
'w');
729 $nbofbyteswrote = fwrite($fhandle, $newfilecontent);
731 @chmod($destfiletmp, octdec($conf->global->MAIN_UMASK));
733 throw new RestException(500,
"Failed to open file '".$destfiletmp.
"' for write");
736 $result =
dol_move($destfiletmp, $destfile, 0, $overwriteifexists, 1);
738 throw new RestException(500,
"Failed to move file into '".$destfile.
"'");
757 public function delete($modulepart, $original_file)
759 global $conf, $langs;
761 if (empty($modulepart)) {
762 throw new RestException(400,
'bad value for parameter modulepart');
764 if (empty($original_file)) {
765 throw new RestException(400,
'bad value for parameter original_file');
769 $entity = $conf->entity;
780 $relativefile = $original_file;
783 $accessallowed = $check_access[
'accessallowed'];
784 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
785 $original_file = $check_access[
'original_file'];
787 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
788 throw new RestException(401);
790 if (!$accessallowed) {
791 throw new RestException(401);
794 $filename = basename($original_file);
795 $original_file_osencoded =
dol_osencode($original_file);
797 if (!file_exists($original_file_osencoded))
799 dol_syslog(
"Try to download not found file ".$original_file_osencoded, LOG_WARNING);
800 throw new RestException(404,
'File not found');
803 if (@unlink($original_file_osencoded)) {
807 'message' =>
'Document deleted'
812 throw new RestException(401);
827 foreach (Documents::$DOCUMENT_FIELDS as $field) {
828 if (!isset($data[$field]))
829 throw new RestException(400,
"$field field missing");
830 $result[$field] = $data[$field];
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
index($modulepart, $original_file= '')
Download a document.
dol_sanitizePathName($str, $newstr= '_', $unaccent=1)
Clean a string to use it as a path name.
_validate_file($data)
Validate fields before create or update object.
Class to manage agenda events (actions)
API class for receive files.
Class to manage products or services.
Class to manage Dolibarr users.
post($filename, $modulepart, $ref= '', $subdir= '', $filecontent= '', $fileencoding= '', $overwriteifexists=0, $createdirifnotexists=1)
Return a document.
dol_is_dir($folder)
Test if filename is a directory.
Class to manage suppliers invoices.
builddoc($modulepart, $original_file= '', $doctemplate= '', $langcode= '')
Build a document.
$conf db
API class for accounts.
dol_move($srcfile, $destfile, $newmask=0, $overwriteifexists=1, $testvirus=0, $indexdatabase=1)
Move a file into another name.
getDocumentsListByElement($modulepart, $id=0, $ref= '', $sortfield= '', $sortorder= '')
Return the list of documents of a dedicated element (from its ID or Ref)
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage categories.
dol_check_secure_access_document($modulepart, $original_file, $entity, $fuser= '', $refname= '', $mode= 'read')
Security check when accessing to a document (used by document.php, viewimage.php and webservices) ...
Class to manage projects.
dol_mimetype($file, $default= 'application/octet-stream', $mode=0)
Return mime type of a file.
Class to manage shipments.
Class to manage customers orders.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart= '')
Return a path to have a the directory according to object where files are stored. ...
Class to manage members of a foundation.
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)
Remove a file or several files with a mask.
Class to manage translations.
dol_sanitizeFileName($str, $newstr= '_', $unaccent=1)
Clean a string to use it as a file name.
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0)
Scan a directory and return a list of files/directories.
Class to manage Trips and Expenses.
dol_is_file($pathoffile)
Return if path is a file.
__construct()
Constructor.
Class to manage invoices.
Class to manage ECM files.
Class to manage proposals.
dol_mkdir($dir, $dataroot= '', $newmask=null)
Creation of a directory (this can create recursive subdir)