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 ==
'holiday') {
356 require_once DOL_DOCUMENT_ROOT.
'/holiday/class/holiday.class.php';
358 $tmpobject =
new Holiday($this->db);
359 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
362 throw new RestException(404,
'Holiday 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 ==
'product') {
372 require_once DOL_DOCUMENT_ROOT .
'/product/class/product.class.php';
374 $tmpobject =
new Product($this->db);
375 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
378 throw new RestException(404,
'Product 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 ==
'stock' || $modulepart ==
'entrepot') {
388 require_once DOL_DOCUMENT_ROOT .
'/product/stock/class/entrepot.class.php';
390 $tmpobject =
new Entrepot($this->db);
391 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
394 throw new RestException(404,
'Warehouse 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');
403 } elseif ($modulepart ==
'fichinter' || $modulepart ==
'intervention') {
404 require_once DOL_DOCUMENT_ROOT .
'/fichinter/class/fichinter.class.php';
407 $result = $tmpobject->fetch(0, preg_replace(
'/\.[^\.]+$/',
'', basename($original_file)));
410 throw new RestException(404,
'Intervention not found');
413 $templateused = $doctemplate ? $doctemplate : $tmpobject->model_pdf;
414 $result = $tmpobject->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
417 throw new RestException(500,
'Error generating document');
420 throw new RestException(403,
'Generation not available for this modulepart');
423 $filename = basename($original_file);
424 $original_file_osencoded =
dol_osencode($original_file);
426 if (!file_exists($original_file_osencoded)) {
427 throw new RestException(404,
'File not found');
430 $file_content = file_get_contents($original_file_osencoded);
431 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');
936 public function post($filename, $modulepart, $ref =
'', $subdir =
'', $filecontent =
'', $fileencoding =
'', $overwriteifexists = 0, $createdirifnotexists = 1, $position = 0, $cover =
'', $array_options = [], $generateThumbs = 0)
940 $modulepartorig = $modulepart;
942 if (empty($modulepart)) {
943 throw new RestException(400,
'Modulepart not provided.');
946 $newfilecontent =
'';
947 if (empty($fileencoding)) {
948 $newfilecontent = $filecontent;
950 if ($fileencoding ==
'base64') {
951 $newfilecontent = base64_decode($filecontent);
955 $relativefile =
'UNSET';
959 $entity = DolibarrApiAccess::$user->entity;
960 if (empty($entity)) {
968 if ($modulepart ==
'facture' || $modulepart ==
'invoice') {
969 $modulepart =
'facture';
971 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
973 } elseif ($modulepart ==
'facture_fournisseur' || $modulepart ==
'supplier_invoice') {
974 $modulepart =
'supplier_invoice';
976 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
978 } elseif ($modulepart ==
'commande' || $modulepart ==
'order') {
979 $modulepart =
'commande';
981 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
983 } elseif ($modulepart ==
'commande_fournisseur' || $modulepart ==
'supplier_order') {
984 $modulepart =
'supplier_order';
986 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.class.php';
988 } elseif ($modulepart ==
'projet' || $modulepart ==
'project') {
989 require_once DOL_DOCUMENT_ROOT.
'/projet/class/project.class.php';
991 } elseif ($modulepart ==
'task' || $modulepart ==
'project_task') {
992 $modulepart =
'project_task';
994 require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
997 $task_result =
$object->fetch(0, $ref);
1000 if ($task_result > 0) {
1001 $project_result =
$object->fetchProject();
1003 if ($project_result >= 0) {
1007 throw new RestException(500,
'Error while fetching Task '.$ref);
1009 } elseif ($modulepart ==
'product' || $modulepart ==
'produit' || $modulepart ==
'service' || $modulepart ==
'produit|service') {
1010 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
1012 } elseif ($modulepart ==
'expensereport') {
1013 require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport.class.php';
1015 } elseif ($modulepart ==
'holiday') {
1016 require_once DOL_DOCUMENT_ROOT.
'/holiday/class/holiday.class.php';
1018 } elseif ($modulepart ==
'ficheinter' || $modulepart ==
'intervention') {
1019 require_once DOL_DOCUMENT_ROOT.
'/fichinter/class/fichinter.class.php';
1021 } elseif ($modulepart ==
'shipment' || $modulepart ==
'expedition') {
1022 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1024 } elseif ($modulepart ==
'adherent' || $modulepart ==
'member') {
1025 $modulepart =
'adherent';
1026 require_once DOL_DOCUMENT_ROOT.
'/adherents/class/adherent.class.php';
1028 } elseif ($modulepart ==
'proposal' || $modulepart ==
'propal' || $modulepart ==
'propale') {
1029 $modulepart =
'propale';
1030 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
1032 } elseif ($modulepart ==
'agenda' || $modulepart ==
'action' || $modulepart ==
'event') {
1033 $modulepart =
'agenda';
1034 require_once DOL_DOCUMENT_ROOT .
'/comm/action/class/actioncomm.class.php';
1036 } elseif ($modulepart ==
'contact' || $modulepart ==
'socpeople') {
1037 $modulepart =
'contact';
1038 require_once DOL_DOCUMENT_ROOT.
'/contact/class/contact.class.php';
1041 } elseif ($modulepart ==
'societe' || $modulepart ==
'company') {
1042 $modulepart =
'societe';
1043 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
1046 } elseif ($modulepart ==
'knowledgemanagement' ) {
1047 $modulepart =
'knowledgemanagement';
1048 require_once DOL_DOCUMENT_ROOT.
'/knowledgemanagement/class/knowledgerecord.class.php';
1051 } elseif ($modulepart ==
'ticket' ) {
1052 $modulepart =
'ticket';
1053 require_once DOL_DOCUMENT_ROOT.
'/ticket/class/ticket.class.php';
1056 } elseif ($modulepart ==
'contrat' || $modulepart ==
'contract') {
1057 $modulepart =
'contrat';
1058 require_once DOL_DOCUMENT_ROOT .
'/contrat/class/contrat.class.php';
1060 } elseif ($modulepart ==
'mrp') {
1061 $modulepart =
'mrp';
1062 require_once DOL_DOCUMENT_ROOT .
'/mrp/class/mo.class.php';
1064 } elseif ($modulepart ==
'stock') {
1065 $modulepart =
'stock';
1066 require_once DOL_DOCUMENT_ROOT .
'/product/stock/class/entrepot.class.php';
1068 } elseif ($modulepart ==
'ecm') {
1069 throw new RestException(500,
'Using a non empty "ref" is not compatible with using modulepart = '.$modulepart);
1072 throw new RestException(500,
'Modulepart '.$modulepart.
' not implemented yet.');
1078 $result =
$object->fetch((
int) $ref);
1080 $result =
$object->fetch(0, $ref);
1084 throw new RestException(404,
"Object with ref '".$ref.
"' was not found.");
1085 } elseif ($result < 0) {
1086 throw new RestException(500,
'Error while fetching object: '.
$object->error);
1090 throw new RestException(404,
'The object '.$modulepart.
" with ref '".$ref.
"' was not found.");
1095 if ($modulepart ==
'supplier_invoice') {
1101 if ($modulepart ==
'societe') {
1102 $relativefile = $tmpreldir.dol_sanitizeFileName((
string)
$object->id);
1104 $relativefile = $tmpreldir.dol_sanitizeFileName((
string)
$object->ref);
1107 $upload_dir = $tmp[
'original_file'];
1115 if (empty($upload_dir) || $upload_dir ==
'/') {
1116 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.');
1119 if ($modulepart ==
'invoice') {
1120 $modulepart =
'facture';
1122 if ($modulepart ==
'member') {
1123 $modulepart =
'adherent';
1127 if ($modulepart !=
'ecm') {
1128 $relativefile = $subdir;
1130 $upload_dir = $tmp[
'original_file'];
1132 if (!DolibarrApiAccess::$user->hasRight(
'ecm',
'upload')) {
1133 throw new RestException(403,
'Missing permission to upload files in ECM module');
1135 $upload_dir =
$conf->medias->multidir_output[
$conf->entity];
1138 if (empty($upload_dir) || $upload_dir ==
'/') {
1139 if (!empty($tmp[
'error'])) {
1140 throw new RestException(403,
'Error returned by dol_check_secure_access_document: '.$tmp[
'error']);
1142 throw new RestException(400,
'This value of modulepart ('.$modulepart.
') is not allowed with this value of subdir ('.$relativefile.
')');
1150 if (!empty($createdirifnotexists)) {
1152 throw new RestException(500,
'Error while trying to create directory '.$upload_dir);
1156 $destfile = $upload_dir.
'/'.$original_file;
1157 $destfiletmp = DOL_DATA_ROOT.
'/admin/temp/'.$original_file;
1162 throw new RestException(400,
'Directory does not exists : '.dirname($destfile));
1165 if (!$overwriteifexists &&
dol_is_file($destfile)) {
1166 throw new RestException(400,
"File with name '".$original_file.
"' already exists.");
1174 $fhandle = @fopen($destfiletmp,
'w');
1176 $nbofbyteswrote = fwrite($fhandle, $newfilecontent);
1180 throw new RestException(500,
"Failed to open file '".$destfiletmp.
"' for write");
1183 $disablevirusscan = 0;
1184 $src_file = $destfiletmp;
1185 $dest_file = $destfile;
1189 if (empty($disablevirusscan) && file_exists($src_file)) {
1191 if (count($checkvirusarray)) {
1192 dol_syslog(
'Files.lib::dol_move_uploaded_file File "'.$src_file.
'" (target name "'.$dest_file.
'") KO with antivirus: errors='.implode(
',', $checkvirusarray), LOG_WARNING);
1193 throw new RestException(500,
'ErrorFileIsInfectedWithAVirus: '.implode(
',', $checkvirusarray));
1200 if (isAFileWithExecutableContent($dest_file) && !
getDolGlobalString(
'MAIN_DOCUMENT_IS_OUTSIDE_WEBROOT_SO_NOEXE_NOT_REQUIRED')) {
1202 $publicmediasdirwithslash =
$conf->medias->multidir_output[
$conf->entity];
1203 if (!preg_match(
'/\/$/', $publicmediasdirwithslash)) {
1204 $publicmediasdirwithslash .=
'/';
1207 if (strpos($upload_dir, $publicmediasdirwithslash) !== 0 || !
getDolGlobalInt(
"MAIN_DOCUMENT_DISABLE_NOEXE_IN_MEDIAS_DIR")) {
1208 $dest_file .=
'.noexe';
1214 if (preg_match(
'/^\./', basename($src_file)) || preg_match(
'/\.\./', $src_file) || preg_match(
'/[<>|]/', $src_file)) {
1215 dol_syslog(
"Refused to deliver file ".$src_file, LOG_WARNING);
1216 throw new RestException(500,
"Refused to deliver file ".$src_file);
1221 if (preg_match(
'/^\./', basename($dest_file)) || preg_match(
'/\.\./', $dest_file) || preg_match(
'/[<>|]/', $dest_file)) {
1222 dol_syslog(
"Refused to deliver file ".$dest_file, LOG_WARNING);
1223 throw new RestException(500,
"Refused to deliver file ".$dest_file);
1226 $moreinfo = array(
'note_private' =>
'File uploaded using API /documents from IP '.
getUserRemoteIP());
1229 $moreinfo[
'src_object_type'] =
$object->table_element;
1230 $moreinfo[
'src_object_id'] =
$object->id;
1232 if (!empty($array_options)) {
1233 $moreinfo = array_merge($moreinfo, [
"array_options" => $array_options]);
1235 if (!empty($position)) {
1236 $moreinfo = array_merge($moreinfo, [
"position" => $position]);
1238 if (!empty($cover)) {
1239 $moreinfo = array_merge($moreinfo, [
"cover" => $cover]);
1241 $moreinfo[
'gen_or_uploaded'] =
'api';
1244 $result =
dol_move($destfiletmp, $dest_file,
'0', $overwriteifexists, 1, 1, $moreinfo);
1246 throw new RestException(500,
"Failed to move file into '".$dest_file.
"'");
1249 if (is_object(
$object) && $generateThumbs) {
1250 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1252 $object->addThumbs($dest_file);
1278 public function delete($modulepart, $original_file)
1282 if (empty($modulepart)) {
1283 throw new RestException(400,
'bad value for parameter modulepart');
1285 if (empty($original_file)) {
1286 throw new RestException(400,
'bad value for parameter original_file');
1290 if ($modulepart ==
'task') {
1291 $modulepart =
'project_task';
1295 $entity =
$conf->entity;
1306 $relativefile = $original_file;
1309 $accessallowed = $check_access[
'accessallowed'];
1310 $sqlprotectagainstexternals = $check_access[
'sqlprotectagainstexternals'];
1311 $original_file = $check_access[
'original_file'];
1313 if (preg_match(
'/\.\./', $original_file) || preg_match(
'/[<>|]/', $original_file)) {
1314 throw new RestException(403);
1316 if (!$accessallowed) {
1317 throw new RestException(403);
1320 if (DolibarrApiAccess::$user->socid > 0) {
1321 if ($sqlprotectagainstexternals) {
1322 $resql = $this->db->query($sqlprotectagainstexternals);
1324 $num = $this->db->num_rows($resql);
1327 $obj = $this->db->fetch_object($resql);
1328 if (DolibarrApiAccess::$user->socid != $obj->fk_soc) {
1329 throw new RestException(403,
'Not allowed to download documents with such a ref');
1337 $filename = basename($original_file);
1338 $original_file_osencoded =
dol_osencode($original_file);
1340 if (!file_exists($original_file_osencoded)) {
1341 dol_syslog(
"Try to download not found file ".$original_file_osencoded, LOG_WARNING);
1342 throw new RestException(404,
'File not found');
1345 if (@unlink($original_file_osencoded)) {
1349 'message' =>
'Document deleted'
1354 throw new RestException(403);