72 public function index($modulepart, $original_file =
'')
76 if (empty($modulepart)) {
77 throw new RestException(400,
'bad value for parameter modulepart');
79 if (empty($original_file)) {
80 throw new RestException(400,
'bad value for parameter original_file');
84 if ($modulepart ==
'task' || $modulepart ==
'project_task') {
85 $modulepart =
'project_task';
89 $entity = $conf->entity;
100 $relativefile = $original_file;
103 $accessallowed = $check_access[
'accessallowed'];
104 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
105 $original_file = $check_access[
'original_file'];
107 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
108 throw new RestException(403);
110 if (!$accessallowed) {
111 throw new RestException(403);
114 if (DolibarrApiAccess::$user->socid > 0) {
115 if ($sqlprotectagainstexternals) {
116 $resql = $this->db->query($sqlprotectagainstexternals);
118 $num = $this->db->num_rows($resql);
121 $obj = $this->db->fetch_object($resql);
122 if (DolibarrApiAccess::$user->socid != $obj->fk_soc) {
123 throw new RestException(403,
'Not allowed to download documents with such a ref');
131 $filename = basename($original_file);
132 $original_file_osencoded =
dol_osencode($original_file);
134 if (!file_exists($original_file_osencoded)) {
135 dol_syslog(
"Try to download not found file ".$original_file_osencoded, LOG_WARNING);
136 throw new RestException(404,
'File not found');
139 $file_content = file_get_contents($original_file_osencoded);
140 return array(
'filename' => $filename,
'content-type' =>
dol_mimetype($filename),
'filesize' => filesize($original_file),
'content' => base64_encode($file_content),
'encoding' =>
'base64');
174 public function builddoc($modulepart, $original_file =
'', $doctemplate =
'', $langcode =
'')
176 global $conf, $langs;
178 if (empty($modulepart)) {
179 throw new RestException(400,
'bad value for parameter modulepart');
181 if (empty($original_file)) {
182 throw new RestException(400,
'bad value for parameter original_file');
185 $outputlangs = $langs;
186 if ($langcode && $langs->defaultlang != $langcode) {
188 $outputlangs->setDefaultLang($langcode);
192 $entity = $conf->entity;
203 $relativefile = $original_file;
206 $accessallowed = $check_access[
'accessallowed'];
207 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
208 $original_file = $check_access[
'original_file'];
210 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
211 throw new RestException(403);
213 if (!$accessallowed) {
214 throw new RestException(403);
217 if (DolibarrApiAccess::$user->socid > 0) {
218 if ($sqlprotectagainstexternals) {
219 $resql = $this->db->query($sqlprotectagainstexternals);
221 $num = $this->db->num_rows($resql);
224 $obj = $this->db->fetch_object($resql);
225 if (DolibarrApiAccess::$user->socid != $obj->fk_soc) {
226 throw new RestException(403,
'Not allowed to download documents with such a ref');
241 if ($modulepart ==
'facture' || $modulepart ==
'invoice') {
242 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
243 $tmpobject =
new Facture($this->db);
244 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
246 throw new RestException(404,
'Invoice not found');
249 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
250 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
252 throw new RestException(500,
'Error generating document');
254 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'invoice_supplier') {
255 require_once DOL_DOCUMENT_ROOT .
'/fourn/class/fournisseur.facture.class.php';
257 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
259 throw new RestException(404,
'Supplier invoice 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 ==
'commande' || $modulepart ==
'order') {
268 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
269 $tmpobject =
new Commande($this->db);
270 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
272 throw new RestException(404,
'Order not found');
274 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
275 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
277 throw new RestException(500,
'Error generating document');
279 } elseif ($modulepart ==
'propal' || $modulepart ==
'proposal') {
280 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
281 $tmpobject =
new Propal($this->db);
282 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
284 throw new RestException(404,
'Proposal not found');
286 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
287 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
289 throw new RestException(500,
'Error generating document');
291 } elseif ($modulepart ==
'contrat' || $modulepart ==
'contract') {
292 require_once DOL_DOCUMENT_ROOT .
'/contrat/class/contrat.class.php';
294 $tmpobject =
new Contrat($this->db);
295 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
298 throw new RestException(404,
'Contract not found');
301 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
302 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
305 throw new RestException(500,
'Error generating document');
307 } elseif ($modulepart ==
'expedition' || $modulepart ==
'shipment') {
308 require_once DOL_DOCUMENT_ROOT .
'/expedition/class/expedition.class.php';
311 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
314 throw new RestException(404,
'Shipment not found');
317 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
318 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
321 throw new RestException(500,
'Error generating document');
323 } elseif ($modulepart ==
'mrp') {
324 require_once DOL_DOCUMENT_ROOT .
'/mrp/class/mo.class.php';
326 $tmpobject =
new Mo($this->db);
327 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
330 throw new RestException(404,
'MO not found');
333 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
334 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
337 throw new RestException(500,
'Error generating document');
339 } elseif ($modulepart ==
'expensereport') {
340 require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport.class.php';
343 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
346 throw new RestException(404,
'Expense report not found');
349 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
350 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
353 throw new RestException(500,
'Error generating document');
355 } elseif ($modulepart ==
'product') {
356 require_once DOL_DOCUMENT_ROOT .
'/product/class/product.class.php';
358 $tmpobject =
new Product($this->db);
359 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
362 throw new RestException(404,
'Product not found');
365 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
366 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
369 throw new RestException(500,
'Error generating document');
371 } elseif ($modulepart ==
'stock' || $modulepart ==
'entrepot') {
372 require_once DOL_DOCUMENT_ROOT .
'/product/stock/class/entrepot.class.php';
374 $tmpobject =
new Entrepot($this->db);
375 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
378 throw new RestException(404,
'Warehouse not found');
381 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
382 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
385 throw new RestException(500,
'Error generating document');
387 } elseif ($modulepart ==
'fichinter' || $modulepart ==
'intervention') {
388 require_once DOL_DOCUMENT_ROOT .
'/fichinter/class/fichinter.class.php';
391 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
394 throw new RestException(404,
'Intervention not found');
397 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
398 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
401 throw new RestException(500,
'Error generating document');
404 throw new RestException(403,
'Generation not available for this modulepart');
407 $filename = basename($original_file);
408 $original_file_osencoded =
dol_osencode($original_file);
410 if (!file_exists($original_file_osencoded)) {
411 throw new RestException(404,
'File not found');
414 $file_content = file_get_contents($original_file_osencoded);
415 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');
906 public function post($filename, $modulepart, $ref =
'', $subdir =
'', $filecontent =
'', $fileencoding =
'', $overwriteifexists = 0, $createdirifnotexists = 1, $position = 0, $cover =
'', $array_options = [], $generateThumbs = 0)
910 $modulepartorig = $modulepart;
912 if (empty($modulepart)) {
913 throw new RestException(400,
'Modulepart not provided.');
916 $newfilecontent =
'';
917 if (empty($fileencoding)) {
918 $newfilecontent = $filecontent;
920 if ($fileencoding ==
'base64') {
921 $newfilecontent = base64_decode($filecontent);
925 $relativefile =
'UNSET';
929 $entity = DolibarrApiAccess::$user->entity;
930 if (empty($entity)) {
938 if ($modulepart ==
'facture' || $modulepart ==
'invoice') {
939 $modulepart =
'facture';
941 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
943 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'supplier_invoice') {
944 $modulepart =
'supplier_invoice';
946 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
948 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
949 $modulepart =
'commande';
951 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
953 } elseif ($modulepart ==
'commande_fournisseur' || $modulepart ==
'supplier_order') {
954 $modulepart =
'supplier_order';
956 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.class.php';
958 } elseif ($modulepart ==
'projet' || $modulepart ==
'project') {
959 require_once DOL_DOCUMENT_ROOT.
'/projet/class/project.class.php';
961 } elseif ($modulepart ==
'task' || $modulepart ==
'project_task') {
962 $modulepart =
'project_task';
964 require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
967 $task_result =
$object->fetch(0, $ref);
970 if ($task_result > 0) {
971 $project_result =
$object->fetchProject();
973 if ($project_result >= 0) {
977 throw new RestException(500,
'Error while fetching Task '.$ref);
979 } elseif ($modulepart ==
'product' || $modulepart ==
'produit' || $modulepart ==
'service' || $modulepart ==
'produit|service') {
980 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
982 } elseif ($modulepart ==
'expensereport') {
983 require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport.class.php';
985 } elseif ($modulepart ==
'ficheinter' || $modulepart ==
'intervention') {
986 require_once DOL_DOCUMENT_ROOT.
'/fichinter/class/fichinter.class.php';
988 } elseif ($modulepart ==
'shipment' || $modulepart ==
'expedition') {
989 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
991 } elseif ($modulepart ==
'adherent' || $modulepart ==
'member') {
992 $modulepart =
'adherent';
993 require_once DOL_DOCUMENT_ROOT.
'/adherents/class/adherent.class.php';
995 } elseif ($modulepart ==
'proposal' || $modulepart ==
'propal' || $modulepart ==
'propale') {
996 $modulepart =
'propale';
997 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
999 } elseif ($modulepart ==
'agenda' || $modulepart ==
'action' || $modulepart ==
'event') {
1000 $modulepart =
'agenda';
1001 require_once DOL_DOCUMENT_ROOT .
'/comm/action/class/actioncomm.class.php';
1003 } elseif ($modulepart ==
'contact' || $modulepart ==
'socpeople') {
1004 $modulepart =
'contact';
1005 require_once DOL_DOCUMENT_ROOT.
'/contact/class/contact.class.php';
1008 } elseif ($modulepart ==
'societe' || $modulepart ==
'company') {
1009 $modulepart =
'societe';
1010 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
1013 } elseif ($modulepart ==
'knowledgemanagement' ) {
1014 $modulepart =
'knowledgemanagement';
1015 require_once DOL_DOCUMENT_ROOT.
'/knowledgemanagement/class/knowledgerecord.class.php';
1018 } elseif ($modulepart ==
'ticket' ) {
1019 $modulepart =
'ticket';
1020 require_once DOL_DOCUMENT_ROOT.
'/ticket/class/ticket.class.php';
1023 } elseif ($modulepart ==
'contrat' || $modulepart ==
'contract') {
1024 $modulepart =
'contrat';
1025 require_once DOL_DOCUMENT_ROOT .
'/contrat/class/contrat.class.php';
1027 } elseif ($modulepart ==
'mrp') {
1028 $modulepart =
'mrp';
1029 require_once DOL_DOCUMENT_ROOT .
'/mrp/class/mo.class.php';
1031 } elseif ($modulepart ==
'stock') {
1032 $modulepart =
'stock';
1033 require_once DOL_DOCUMENT_ROOT .
'/product/stock/class/entrepot.class.php';
1035 } elseif ($modulepart ==
'ecm') {
1036 throw new RestException(500,
'Using a non empty "ref" is not compatible with using modulepart = '.$modulepart);
1039 throw new RestException(500,
'Modulepart '.$modulepart.
' not implemented yet.');
1045 $result =
$object->fetch((
int) $ref);
1047 $result =
$object->fetch(0, $ref);
1051 throw new RestException(404,
"Object with ref '".$ref.
"' was not found.");
1052 } elseif ($result < 0) {
1053 throw new RestException(500,
'Error while fetching object: '.
$object->error);
1057 throw new RestException(404,
'The object '.$modulepart.
" with ref '".$ref.
"' was not found.");
1062 if ($modulepart ==
'supplier_invoice') {
1068 if ($modulepart ==
'societe') {
1069 $relativefile = $tmpreldir.dol_sanitizeFileName((
string)
$object->id);
1071 $relativefile = $tmpreldir.dol_sanitizeFileName(
$object->ref);
1074 $upload_dir = $tmp[
'original_file'];
1082 if (empty($upload_dir) || $upload_dir ==
'/') {
1083 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.');
1086 if ($modulepart ==
'invoice') {
1087 $modulepart =
'facture';
1089 if ($modulepart ==
'member') {
1090 $modulepart =
'adherent';
1094 if ($modulepart !=
'ecm') {
1095 $relativefile = $subdir;
1097 $upload_dir = $tmp[
'original_file'];
1099 if (!DolibarrApiAccess::$user->hasRight(
'ecm',
'upload')) {
1100 throw new RestException(403,
'Missing permission to upload files in ECM module');
1102 $upload_dir = $conf->medias->multidir_output[$conf->entity];
1105 if (empty($upload_dir) || $upload_dir ==
'/') {
1106 if (!empty($tmp[
'error'])) {
1107 throw new RestException(403,
'Error returned by dol_check_secure_access_document: '.$tmp[
'error']);
1109 throw new RestException(400,
'This value of modulepart ('.$modulepart.
') is not allowed with this value of subdir ('.$relativefile.
')');
1117 if (!empty($createdirifnotexists)) {
1119 throw new RestException(500,
'Error while trying to create directory '.$upload_dir);
1123 $destfile = $upload_dir.
'/'.$original_file;
1124 $destfiletmp = DOL_DATA_ROOT.
'/admin/temp/'.$original_file;
1129 throw new RestException(400,
'Directory does not exists : '.dirname($destfile));
1132 if (!$overwriteifexists &&
dol_is_file($destfile)) {
1133 throw new RestException(400,
"File with name '".$original_file.
"' already exists.");
1141 $fhandle = @fopen($destfiletmp,
'w');
1143 $nbofbyteswrote = fwrite($fhandle, $newfilecontent);
1147 throw new RestException(500,
"Failed to open file '".$destfiletmp.
"' for write");
1150 $disablevirusscan = 0;
1151 $src_file = $destfiletmp;
1152 $dest_file = $destfile;
1156 if (empty($disablevirusscan) && file_exists($src_file)) {
1158 if (count($checkvirusarray)) {
1159 dol_syslog(
'Files.lib::dol_move_uploaded_file File "'.$src_file.
'" (target name "'.$dest_file.
'") KO with antivirus: errors='.implode(
',', $checkvirusarray), LOG_WARNING);
1160 throw new RestException(500,
'ErrorFileIsInfectedWithAVirus: '.implode(
',', $checkvirusarray));
1169 $publicmediasdirwithslash = $conf->medias->multidir_output[$conf->entity];
1170 if (!preg_match(
'/\/$/', $publicmediasdirwithslash)) {
1171 $publicmediasdirwithslash .=
'/';
1174 if (strpos($upload_dir, $publicmediasdirwithslash) !== 0 || !
getDolGlobalInt(
"MAIN_DOCUMENT_DISABLE_NOEXE_IN_MEDIAS_DIR")) {
1175 $dest_file .=
'.noexe';
1181 if (preg_match(
'/^\./', basename($src_file)) || preg_match(
'/\.\./', $src_file) || preg_match(
'/[<>|]/', $src_file)) {
1182 dol_syslog(
"Refused to deliver file ".$src_file, LOG_WARNING);
1183 throw new RestException(500,
"Refused to deliver file ".$src_file);
1188 if (preg_match(
'/^\./', basename($dest_file)) || preg_match(
'/\.\./', $dest_file) || preg_match(
'/[<>|]/', $dest_file)) {
1189 dol_syslog(
"Refused to deliver file ".$dest_file, LOG_WARNING);
1190 throw new RestException(500,
"Refused to deliver file ".$dest_file);
1193 $moreinfo = array(
'note_private' =>
'File uploaded using API /documents from IP '.
getUserRemoteIP());
1196 $moreinfo[
'src_object_type'] =
$object->table_element;
1197 $moreinfo[
'src_object_id'] =
$object->id;
1199 if (!empty($array_options)) {
1200 $moreinfo = array_merge($moreinfo, [
"array_options" => $array_options]);
1202 if (!empty($position)) {
1203 $moreinfo = array_merge($moreinfo, [
"position" => $position]);
1205 if (!empty($cover)) {
1206 $moreinfo = array_merge($moreinfo, [
"cover" => $cover]);
1208 $moreinfo[
'gen_or_uploaded'] =
'api';
1211 $result =
dol_move($destfiletmp, $dest_file,
'0', $overwriteifexists, 1, 1, $moreinfo);
1213 throw new RestException(500,
"Failed to move file into '".$dest_file.
"'");
1216 if (is_object(
$object) && $generateThumbs) {
1217 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1219 $object->addThumbs($dest_file);
1245 public function delete($modulepart, $original_file)
1249 if (empty($modulepart)) {
1250 throw new RestException(400,
'bad value for parameter modulepart');
1252 if (empty($original_file)) {
1253 throw new RestException(400,
'bad value for parameter original_file');
1257 if ($modulepart ==
'task') {
1258 $modulepart =
'project_task';
1262 $entity = $conf->entity;
1273 $relativefile = $original_file;
1276 $accessallowed = $check_access[
'accessallowed'];
1277 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
1278 $original_file = $check_access[
'original_file'];
1280 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
1281 throw new RestException(403);
1283 if (!$accessallowed) {
1284 throw new RestException(403);
1287 if (DolibarrApiAccess::$user->socid > 0) {
1288 if ($sqlprotectagainstexternals) {
1289 $resql = $this->db->query($sqlprotectagainstexternals);
1291 $num = $this->db->num_rows($resql);
1294 $obj = $this->db->fetch_object($resql);
1295 if (DolibarrApiAccess::$user->socid != $obj->fk_soc) {
1296 throw new RestException(403,
'Not allowed to download documents with such a ref');
1304 $filename = basename($original_file);
1305 $original_file_osencoded =
dol_osencode($original_file);
1307 if (!file_exists($original_file_osencoded)) {
1308 dol_syslog(
"Try to download not found file ".$original_file_osencoded, LOG_WARNING);
1309 throw new RestException(404,
'File not found');
1312 if (@unlink($original_file_osencoded)) {
1316 'message' =>
'Document deleted'
1321 throw new RestException(403);