api_documents.class.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. <?php
  2. /* Copyright (C) 2016 Xebax Christy <xebax@wanadoo.fr>
  3. * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2016 Jean-François Ferry <jfefe@aternatik.fr>
  5. *
  6. * This program is free software you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. use Luracast\Restler\RestException;
  20. use Luracast\Restler\Format\UploadFormat;
  21. require_once DOL_DOCUMENT_ROOT.'/main.inc.php';
  22. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  23. /**
  24. * API class for receive files
  25. *
  26. * @access protected
  27. * @class Documents {@requires user,external}
  28. */
  29. class Documents extends DolibarrApi
  30. {
  31. /**
  32. * @var array $DOCUMENT_FIELDS Mandatory fields, checked when create and update object
  33. */
  34. static $DOCUMENT_FIELDS = array(
  35. 'modulepart'
  36. );
  37. /**
  38. * Constructor
  39. */
  40. function __construct()
  41. {
  42. global $db;
  43. $this->db = $db;
  44. }
  45. /**
  46. * Download a document.
  47. *
  48. * Note that, this API is similar to using the wrapper link "documents.php" to download a file (used for
  49. * internal HTML links of documents into application), but with no need to have a session cookie (the token is used instead).
  50. *
  51. * @param string $module_part Name of module or area concerned by file download ('facture', ...)
  52. * @param string $original_file Relative path with filename, relative to modulepart (for example: IN201701-999/IN201701-999.pdf)
  53. * @return array List of documents
  54. *
  55. * @throws 400
  56. * @throws 401
  57. * @throws 404
  58. * @throws 200
  59. *
  60. * @url GET /download
  61. */
  62. public function index($module_part, $original_file='')
  63. {
  64. global $conf, $langs;
  65. if (empty($module_part)) {
  66. throw new RestException(400, 'bad value for parameter modulepart');
  67. }
  68. if (empty($original_file)) {
  69. throw new RestException(400, 'bad value for parameter original_file');
  70. }
  71. //--- Finds and returns the document
  72. $entity=$conf->entity;
  73. $check_access = dol_check_secure_access_document($module_part, $original_file, $entity, DolibarrApiAccess::$user, '', 'read');
  74. $accessallowed = $check_access['accessallowed'];
  75. $sqlprotectagainstexternals = $check_access['sqlprotectagainstexternals'];
  76. $original_file = $check_access['original_file'];
  77. if (preg_match('/\.\./',$original_file) || preg_match('/[<>|]/',$original_file))
  78. {
  79. throw new RestException(401);
  80. }
  81. if (!$accessallowed) {
  82. throw new RestException(401);
  83. }
  84. $filename = basename($original_file);
  85. $original_file_osencoded=dol_osencode($original_file); // New file name encoded in OS encoding charset
  86. if (! file_exists($original_file_osencoded))
  87. {
  88. throw new RestException(404, 'File not found');
  89. }
  90. $file_content=file_get_contents($original_file_osencoded);
  91. return array('filename'=>$filename, 'content-type' => dol_mimetype($filename), 'filesize'=>filesize($original_file), 'content'=>base64_encode($file_content), 'encoding'=>'base64' );
  92. }
  93. /**
  94. * Build a document.
  95. *
  96. * Test sample 1: { "module_part": "invoice", "original_file": "FA1701-001/FA1701-001.pdf", "doctemplate": "crabe", "langcode": "fr_FR" }.
  97. *
  98. * @param string $module_part Name of module or area concerned by file download ('invoice', 'order', ...).
  99. * @param string $original_file Relative path with filename, relative to modulepart (for example: IN201701-999/IN201701-999.pdf).
  100. * @param string $doctemplate Set here the doc template to use for document generation (If not set, use the default template).
  101. * @param string $langcode Language code like 'en_US', 'fr_FR', 'es_ES', ... (If not set, use the default language).
  102. * @return array List of documents
  103. *
  104. * @throws 500
  105. * @throws 501
  106. * @throws 400
  107. * @throws 401
  108. * @throws 404
  109. * @throws 200
  110. *
  111. * @url PUT /builddoc
  112. */
  113. public function builddoc($module_part, $original_file='', $doctemplate='', $langcode='')
  114. {
  115. global $conf, $langs;
  116. if (empty($module_part)) {
  117. throw new RestException(400, 'bad value for parameter modulepart');
  118. }
  119. if (empty($original_file)) {
  120. throw new RestException(400, 'bad value for parameter original_file');
  121. }
  122. $outputlangs = $langs;
  123. if ($langcode && $langs->defaultlang != $langcode)
  124. {
  125. $outputlangs=new Translate('', $conf);
  126. $outputlangs->setDefaultLang($langcode);
  127. }
  128. //--- Finds and returns the document
  129. $entity=$conf->entity;
  130. $check_access = dol_check_secure_access_document($module_part, $original_file, $entity, DolibarrApiAccess::$user, '', 'write');
  131. $accessallowed = $check_access['accessallowed'];
  132. $sqlprotectagainstexternals = $check_access['sqlprotectagainstexternals'];
  133. $original_file = $check_access['original_file'];
  134. if (preg_match('/\.\./',$original_file) || preg_match('/[<>|]/',$original_file)) {
  135. throw new RestException(401);
  136. }
  137. if (!$accessallowed) {
  138. throw new RestException(401);
  139. }
  140. // --- Generates the document
  141. $hidedetails = empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS) ? 0 : 1;
  142. $hidedesc = empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DESC) ? 0 : 1;
  143. $hideref = empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_REF) ? 0 : 1;
  144. $templateused='';
  145. if ($module_part == 'facture' || $module_part == 'invoice')
  146. {
  147. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  148. $this->invoice = new Facture($this->db);
  149. $result = $this->invoice->fetch(0, preg_replace('/\.[^\.]+$/', '', basename($original_file)));
  150. if( ! $result ) {
  151. throw new RestException(404, 'Invoice not found');
  152. }
  153. $templateused = $doctemplate?$doctemplate:$this->invoice->modelpdf;
  154. $result = $this->invoice->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
  155. if( $result <= 0 ) {
  156. throw new RestException(500, 'Error generating document');
  157. }
  158. }
  159. elseif ($module_part == 'commande' || $module_part == 'order')
  160. {
  161. require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
  162. $this->order = new Commande($this->db);
  163. $result = $this->order->fetch(0, preg_replace('/\.[^\.]+$/', '', basename($original_file)));
  164. if( ! $result ) {
  165. throw new RestException(404, 'Order not found');
  166. }
  167. $templateused = $doctemplate?$doctemplate:$this->order->modelpdf;
  168. $result = $this->order->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
  169. if( $result <= 0 ) {
  170. throw new RestException(500, 'Error generating document');
  171. }
  172. }
  173. elseif ($module_part == 'propal' || $module_part == 'proposal')
  174. {
  175. require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
  176. $this->propal = new Propal($this->db);
  177. $result = $this->propal->fetch(0, preg_replace('/\.[^\.]+$/', '', basename($original_file)));
  178. if( ! $result ) {
  179. throw new RestException(404, 'Proposal not found');
  180. }
  181. $templateused = $doctemplate?$doctemplate:$this->propal->modelpdf;
  182. $result = $this->propal->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref);
  183. if( $result <= 0 ) {
  184. throw new RestException(500, 'Error generating document');
  185. }
  186. }
  187. else
  188. {
  189. throw new RestException(403, 'Generation not available for this modulepart');
  190. }
  191. $filename = basename($original_file);
  192. $original_file_osencoded=dol_osencode($original_file); // New file name encoded in OS encoding charset
  193. if (! file_exists($original_file_osencoded))
  194. {
  195. throw new RestException(404, 'File not found');
  196. }
  197. $file_content=file_get_contents($original_file_osencoded);
  198. 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' );
  199. }
  200. /**
  201. * Return the list of documents of a dedicated element (from its ID or Ref)
  202. *
  203. * @param string $modulepart Name of module or area concerned ('thirdparty', 'member', 'proposal', 'order', 'invoice', 'shipment', 'project', ...)
  204. * @param int $id ID of element
  205. * @param string $ref Ref of element
  206. * @param string $sortfield Sort criteria ('','fullname','relativename','name','date','size')
  207. * @param string $sortorder Sort order ('asc' or 'desc')
  208. * @return array Array of documents with path
  209. *
  210. * @throws 200
  211. * @throws 400
  212. * @throws 401
  213. * @throws 404
  214. * @throws 500
  215. *
  216. * @url GET /
  217. */
  218. function getDocumentsListByElement($modulepart, $id=0, $ref='', $sortfield='', $sortorder='')
  219. {
  220. global $conf;
  221. if (empty($modulepart)) {
  222. throw new RestException(400, 'bad value for parameter modulepart');
  223. }
  224. if (empty($id) && empty($ref)) {
  225. throw new RestException(400, 'bad value for parameter id or ref');
  226. }
  227. $id = (empty($id)?0:$id);
  228. if ($modulepart == 'societe' || $modulepart == 'thirdparty')
  229. {
  230. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  231. if (!DolibarrApiAccess::$user->rights->societe->lire) {
  232. throw new RestException(401);
  233. }
  234. $object = new Societe($this->db);
  235. $result=$object->fetch($id, $ref);
  236. if ( ! $result ) {
  237. throw new RestException(404, 'Thirdparty not found');
  238. }
  239. $upload_dir = $conf->societe->multidir_output[$object->entity] . "/" . $object->id;
  240. }
  241. else if ($modulepart == 'adherent' || $modulepart == 'member')
  242. {
  243. require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
  244. if (!DolibarrApiAccess::$user->rights->adherent->lire) {
  245. throw new RestException(401);
  246. }
  247. $object = new Adherent($this->db);
  248. $result=$object->fetch($id, $ref);
  249. if ( ! $result ) {
  250. throw new RestException(404, 'Member not found');
  251. }
  252. $upload_dir = $conf->adherent->dir_output . "/" . get_exdir(0, 0, 0, 1, $object, 'member');
  253. }
  254. else if ($modulepart == 'propal' || $modulepart == 'proposal')
  255. {
  256. require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
  257. if (!DolibarrApiAccess::$user->rights->propal->lire) {
  258. throw new RestException(401);
  259. }
  260. $object = new Propal($this->db);
  261. $result=$object->fetch($id, $ref);
  262. if ( ! $result ) {
  263. throw new RestException(404, 'Proposal not found');
  264. }
  265. $upload_dir = $conf->propal->multidir_output[$object->entity] . "/" . get_exdir(0, 0, 0, 1, $object, 'propal');
  266. }
  267. else if ($modulepart == 'commande' || $modulepart == 'order')
  268. {
  269. require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
  270. if (!DolibarrApiAccess::$user->rights->commande->lire) {
  271. throw new RestException(401);
  272. }
  273. $object = new Commande($this->db);
  274. $result=$object->fetch($id, $ref);
  275. if ( ! $result ) {
  276. throw new RestException(404, 'Order not found');
  277. }
  278. $upload_dir = $conf->commande->dir_output . "/" . get_exdir(0, 0, 0, 1, $object, 'commande');
  279. }
  280. else if ($modulepart == 'shipment' || $modulepart == 'expedition')
  281. {
  282. require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
  283. if (!DolibarrApiAccess::$user->rights->expedition->lire) {
  284. throw new RestException(401);
  285. }
  286. $object = new Expedition($this->db);
  287. $result=$object->fetch($id, $ref);
  288. if ( ! $result ) {
  289. throw new RestException(404, 'Shipment not found');
  290. }
  291. $upload_dir = $conf->expedition->dir_output . "/sending/" . get_exdir(0, 0, 0, 1, $object, 'shipment');
  292. }
  293. else if ($modulepart == 'facture' || $modulepart == 'invoice')
  294. {
  295. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  296. if (!DolibarrApiAccess::$user->rights->facture->lire) {
  297. throw new RestException(401);
  298. }
  299. $object = new Facture($this->db);
  300. $result=$object->fetch($id, $ref);
  301. if ( ! $result ) {
  302. throw new RestException(404, 'Invoice not found');
  303. }
  304. $upload_dir = $conf->facture->dir_output . "/" . get_exdir(0, 0, 0, 1, $object, 'invoice');
  305. }
  306. else if ($modulepart == 'agenda' || $modulepart == 'action' || $modulepart == 'event')
  307. {
  308. require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
  309. if (!DolibarrApiAccess::$user->rights->agenda->myactions->read && !DolibarrApiAccess::$user->rights->agenda->allactions->read) {
  310. throw new RestException(401);
  311. }
  312. $object = new ActionComm($this->db);
  313. $result=$object->fetch($id, $ref);
  314. if ( ! $result ) {
  315. throw new RestException(404, 'Event not found');
  316. }
  317. $upload_dir = $conf->agenda->dir_output.'/'.dol_sanitizeFileName($object->ref);
  318. }
  319. else
  320. {
  321. throw new RestException(500, 'Modulepart '.$modulepart.' not implemented yet.');
  322. }
  323. $filearray=dol_dir_list($upload_dir,"files",0,'','(\.meta|_preview.*\.png)$',$sortfield,(strtolower($sortorder)=='desc'?SORT_DESC:SORT_ASC),1);
  324. if (empty($filearray)) {
  325. throw new RestException(404, 'Search for modulepart '.$modulepart.' with Id '.$object->id.(! empty($object->Ref)?' or Ref '.$object->ref:'').' does not return any document.');
  326. }
  327. return $filearray;
  328. }
  329. /**
  330. * Return a document.
  331. *
  332. * @param int $id ID of document
  333. * @return array Array with data of file
  334. *
  335. * @throws RestException
  336. */
  337. /*
  338. public function get($id) {
  339. return array('note'=>'xxx');
  340. }*/
  341. /**
  342. * Upload a file.
  343. *
  344. * Test sample 1: { "filename": "mynewfile.txt", "modulepart": "facture", "ref": "FA1701-001", "subdir": "", "filecontent": "content text", "fileencoding": "", "overwriteifexists": "0" }.
  345. * Test sample 2: { "filename": "mynewfile.txt", "modulepart": "medias", "ref": "", "subdir": "image/mywebsite", "filecontent": "Y29udGVudCB0ZXh0Cg==", "fileencoding": "base64", "overwriteifexists": "0" }.
  346. *
  347. * @param string $filename Name of file to create ('FA1705-0123.txt')
  348. * @param string $modulepart Name of module or area concerned by file upload ('facture', 'project', 'project_task', ...)
  349. * @param string $ref Reference of object (This will define subdir automatically and store submited file into it)
  350. * @param string $subdir Subdirectory (Only if ref not provided)
  351. * @param string $filecontent File content (string with file content. An empty file will be created if this parameter is not provided)
  352. * @param string $fileencoding File encoding (''=no encoding, 'base64'=Base 64) {@example '' or 'base64'}
  353. * @param int $overwriteifexists Overwrite file if exists (1 by default)
  354. *
  355. * @throws 200
  356. * @throws 400
  357. * @throws 401
  358. * @throws 404
  359. * @throws 500
  360. *
  361. * @url POST /upload
  362. */
  363. public function post($filename, $modulepart, $ref='', $subdir='', $filecontent='', $fileencoding='', $overwriteifexists=0)
  364. {
  365. global $db, $conf;
  366. /*var_dump($modulepart);
  367. var_dump($filename);
  368. var_dump($filecontent);
  369. exit;*/
  370. if(empty($modulepart))
  371. {
  372. throw new RestException(400, 'Modulepart not provided.');
  373. }
  374. if (!DolibarrApiAccess::$user->rights->ecm->upload) {
  375. throw new RestException(401);
  376. }
  377. $newfilecontent = '';
  378. if (empty($fileencoding)) $newfilecontent = $filecontent;
  379. if ($fileencoding == 'base64') $newfilecontent = base64_decode($filecontent);
  380. $original_file = dol_sanitizeFileName($filename);
  381. // Define $uploadir
  382. $object = null;
  383. $entity = DolibarrApiAccess::$user->entity;
  384. if ($ref)
  385. {
  386. $tmpreldir='';
  387. if ($modulepart == 'facture' || $modulepart == 'invoice')
  388. {
  389. $modulepart='facture';
  390. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  391. $object = new Facture($this->db);
  392. }
  393. elseif ($modulepart == 'project')
  394. {
  395. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  396. $object = new Project($this->db);
  397. }
  398. elseif ($modulepart == 'task' || $modulepart == 'project_task')
  399. {
  400. $modulepart = 'project_task';
  401. require_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php';
  402. $object = new Task($this->db);
  403. $task_result = $object->fetch('', $ref);
  404. // Fetching the tasks project is required because its out_dir might be a sub-directory of the project
  405. if($task_result > 0)
  406. {
  407. $project_result = $object->fetch_projet();
  408. if($project_result >= 0)
  409. {
  410. $tmpreldir = dol_sanitizeFileName($object->project->ref).'/';
  411. }
  412. }
  413. else
  414. {
  415. throw new RestException(500, 'Error while fetching Task '.$ref);
  416. }
  417. }
  418. // TODO Implement additional moduleparts
  419. else
  420. {
  421. throw new RestException(500, 'Modulepart '.$modulepart.' not implemented yet.');
  422. }
  423. if(is_object($object))
  424. {
  425. $result = $object->fetch('', $ref);
  426. if($result == 0)
  427. {
  428. throw new RestException(404, "Object with ref '".$ref."' was not found.");
  429. }
  430. elseif ($result < 0)
  431. {
  432. throw new RestException(500, 'Error while fetching object.');
  433. }
  434. }
  435. if (! ($object->id > 0))
  436. {
  437. throw new RestException(404, 'The object '.$modulepart." with ref '".$ref."' was not found.");
  438. }
  439. $relativefile = $tmpreldir.dol_sanitizeFileName($object->ref);
  440. $tmp = dol_check_secure_access_document($modulepart, $relativefile, $entity, DolibarrApiAccess::$user, $ref, 'write');
  441. $upload_dir = $tmp['original_file']; // No dirname here, tmp['original_file'] is already the dir because dol_check_secure_access_document was called with param original_file that is only the dir
  442. if (empty($upload_dir) || $upload_dir == '/')
  443. {
  444. throw new RestException(500, 'This value of modulepart does not support yet usage of ref. Check modulepart parameter or try to use subdir parameter instead of ref.');
  445. }
  446. }
  447. else
  448. {
  449. if ($modulepart == 'invoice') $modulepart ='facture';
  450. $relativefile = $subdir;
  451. $tmp = dol_check_secure_access_document($modulepart, $relativefile, $entity, DolibarrApiAccess::$user, '', 'write');
  452. $upload_dir = $tmp['original_file']; // No dirname here, tmp['original_file'] is already the dir because dol_check_secure_access_document was called with param original_file that is only the dir
  453. if (empty($upload_dir) || $upload_dir == '/')
  454. {
  455. throw new RestException(500, 'This value of modulepart does not support yet usage of ref. Check modulepart parameter or try to use subdir parameter instead of ref.');
  456. }
  457. }
  458. // $original_file here is still value of filename without any dir.
  459. $upload_dir = dol_sanitizePathName($upload_dir);
  460. $destfile = $upload_dir . '/' . $original_file;
  461. $destfiletmp = DOL_DATA_ROOT.'/admin/temp/' . $original_file;
  462. dol_delete_file($destfiletmp);
  463. //var_dump($original_file);exit;
  464. if (!dol_is_dir(dirname($destfile))) {
  465. throw new RestException(401, 'Directory not exists : '.dirname($destfile));
  466. }
  467. if (! $overwriteifexists && dol_is_file($destfile))
  468. {
  469. throw new RestException(500, "File with name '".$original_file."' already exists.");
  470. }
  471. $fhandle = @fopen($destfiletmp, 'w');
  472. if ($fhandle)
  473. {
  474. $nbofbyteswrote = fwrite($fhandle, $newfilecontent);
  475. fclose($fhandle);
  476. @chmod($destfiletmp, octdec($conf->global->MAIN_UMASK));
  477. }
  478. else
  479. {
  480. throw new RestException(500, "Failed to open file '".$destfiletmp."' for write");
  481. }
  482. $result = dol_move($destfiletmp, $destfile, 0, $overwriteifexists, 1);
  483. if (! $result)
  484. {
  485. throw new RestException(500, "Failed to move file into '".$destfile."'");
  486. }
  487. return dol_basename($destfile);
  488. }
  489. /**
  490. * Validate fields before create or update object
  491. *
  492. * @param array $data Array with data to verify
  493. * @return array
  494. * @throws RestException
  495. */
  496. function _validate_file($data) {
  497. $result = array();
  498. foreach (Documents::$DOCUMENT_FIELDS as $field) {
  499. if (!isset($data[$field]))
  500. throw new RestException(400, "$field field missing");
  501. $result[$field] = $data[$field];
  502. }
  503. return $result;
  504. }
  505. }