api_documents.class.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. /**
  23. * API class for receive files
  24. *
  25. * @access protected
  26. * @class Documents {@requires user,external}
  27. */
  28. class Documents extends DolibarrApi
  29. {
  30. /**
  31. * @var array $DOCUMENT_FIELDS Mandatory fields, checked when create and update object
  32. */
  33. static $DOCUMENT_FIELDS = array(
  34. 'modulepart'
  35. );
  36. /**
  37. * Constructor
  38. */
  39. function __construct()
  40. {
  41. global $db;
  42. $this->db = $db;
  43. }
  44. /**
  45. * Return list of documents.
  46. *
  47. * @param string $module_part Name of module or area concerned by file download ('facture', ...)
  48. * @param string $ref Reference of object (This will define subdir automatically)
  49. * @param string $subdir Subdirectory (Only if ref not provided)
  50. * @return array List of documents
  51. *
  52. * @throws RestException
  53. */
  54. public function index($module_part, $ref='', $subdir='') {
  55. return array('note'=>'FeatureNotYetAvailable');
  56. }
  57. /**
  58. * Return a document.
  59. *
  60. * @param int $id ID of document
  61. * @return array Array with data of file
  62. *
  63. * @throws RestException
  64. */
  65. /*
  66. public function get($id) {
  67. return array('note'=>'xxx');
  68. }*/
  69. /**
  70. * Push a file.
  71. * Test sample 1: { "filename": "mynewfile.txt", "modulepart": "facture", "ref": "FA1701-001", "subdir": "", "filecontent": "content text", "fileencoding": "", "overwriteifexists": "0" }.
  72. * Test sample 2: { "filename": "mynewfile.txt", "modulepart": "medias", "ref": "", "subdir": "mysubdir1/mysubdir2", "filecontent": "content text", "fileencoding": "", "overwriteifexists": "0" }.
  73. *
  74. * @param string $filename Name of file to create ('FA1705-0123')
  75. * @param string $modulepart Name of module or area concerned by file upload ('facture', ...)
  76. * @param string $ref Reference of object (This will define subdir automatically and store submited file into it)
  77. * @param string $subdir Subdirectory (Only if ref not provided)
  78. * @param string $filecontent File content (string with file content. An empty file will be created if this parameter is not provided)
  79. * @param string $fileencoding File encoding (''=no encoding, 'base64'=Base 64)
  80. * @param int $overwriteifexists Overwrite file if exists (1 by default)
  81. * @return bool State of copy
  82. * @throws RestException
  83. */
  84. public function post($filename, $modulepart, $ref='', $subdir='', $filecontent='', $fileencoding='', $overwriteifexists=0)
  85. {
  86. global $db, $conf;
  87. /*var_dump($modulepart);
  88. var_dump($filename);
  89. var_dump($filecontent);
  90. exit;*/
  91. require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
  92. if (!DolibarrApiAccess::$user->rights->ecm->upload) {
  93. throw new RestException(401);
  94. }
  95. $newfilecontent = '';
  96. if (empty($fileencoding)) $newfilecontent = $filecontent;
  97. if ($fileencoding == 'base64') $newfilecontent = base64_decode($filecontent);
  98. $original_file = dol_sanitizeFileName($filename);
  99. // Define $uploadir
  100. $object = null;
  101. $entity = $user->entity;
  102. if ($ref)
  103. {
  104. if ($modulepart == 'facture' || $modulepart == 'invoice')
  105. {
  106. $modulepart='facture';
  107. $object=new Facture($db);
  108. $result = $object->fetch('', $ref);
  109. }
  110. if (! ($object->id > 0))
  111. {
  112. throw new RestException(500, 'The object '.$modulepart." with ref '".$ref."' was not found.");
  113. }
  114. $tmp = dol_check_secure_access_document($modulepart, $tmpreldir.$object->ref, $entity, DolibarrApiAccess::$user, $ref, 'write');
  115. $upload_dir = $tmp['original_file'];
  116. if (empty($upload_dir) || $upload_dir == '/')
  117. {
  118. 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.');
  119. }
  120. }
  121. else
  122. {
  123. if ($modulepart == 'invoice') $modulepart ='facture';
  124. $tmp = dol_check_secure_access_document($modulepart, $subdir, $entity, DolibarrApiAccess::$user, '', 'write');
  125. $upload_dir = $tmp['original_file'];
  126. if (empty($upload_dir) || $upload_dir == '/')
  127. {
  128. 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.');
  129. }
  130. }
  131. $upload_dir = dol_sanitizePathName($upload_dir);
  132. $destfile = $upload_dir . '/' . $original_file;
  133. $destfiletmp = DOL_DATA_ROOT.'/admin/temp/' . $original_file;
  134. dol_delete_file($destfiletmp);
  135. if (!dol_is_dir($upload_dir)) {
  136. throw new RestException(401,'Directory not exists : '.$upload_dir);
  137. }
  138. if (! $overwriteifexists && dol_is_file($destfile))
  139. {
  140. throw new RestException(500, "File with name '".$original_file."' already exists.");
  141. }
  142. $fhandle = @fopen($destfiletmp, 'w');
  143. if ($fhandle)
  144. {
  145. $nbofbyteswrote = fwrite($fhandle, $newfilecontent);
  146. fclose($fhandle);
  147. @chmod($destfiletmp, octdec($conf->global->MAIN_UMASK));
  148. }
  149. else
  150. {
  151. throw new RestException(500, "Failed to open file '".$destfiletmp."' for write");
  152. }
  153. $result = dol_move($destfiletmp, $destfile, 0, $overwriteifexists, 1);
  154. return $result;
  155. }
  156. /**
  157. * Validate fields before create or update object
  158. *
  159. * @param array $data Array with data to verify
  160. * @return array
  161. * @throws RestException
  162. */
  163. function _validate_file($data) {
  164. $result = array();
  165. foreach (Documents::$DOCUMENT_FIELDS as $field) {
  166. if (!isset($data[$field]))
  167. throw new RestException(400, "$field field missing");
  168. $result[$field] = $data[$field];
  169. }
  170. return $result;
  171. }
  172. }