|
1
|
|
|
<?php |
|
2
|
|
|
/* Copyright (C) 2016 Xebax Christy <[email protected]> |
|
3
|
|
|
* Copyright (C) 2016 Laurent Destailleur <[email protected]> |
|
4
|
|
|
* Copyright (C) 2016 Jean-François Ferry <[email protected]> |
|
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 <https://www.gnu.org/licenses/>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
use Luracast\Restler\RestException; |
|
21
|
|
|
use Luracast\Restler\Format\UploadFormat; |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
require_once DOL_DOCUMENT_ROOT.'/main.inc.php'; |
|
25
|
|
|
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* API class for receive files |
|
29
|
|
|
* |
|
30
|
|
|
* @access protected |
|
31
|
|
|
* @class Documents {@requires user,external} |
|
32
|
|
|
*/ |
|
33
|
|
|
class Documents extends DolibarrApi |
|
34
|
|
|
{ |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var array $DOCUMENT_FIELDS Mandatory fields, checked when create and update object |
|
38
|
|
|
*/ |
|
39
|
|
|
static $DOCUMENT_FIELDS = array( |
|
40
|
|
|
'modulepart' |
|
41
|
|
|
); |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Constructor |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct() |
|
47
|
|
|
{ |
|
48
|
|
|
global $db; |
|
49
|
|
|
$this->db = $db; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Download a document. |
|
55
|
|
|
* |
|
56
|
|
|
* Note that, this API is similar to using the wrapper link "documents.php" to download a file (used for |
|
57
|
|
|
* internal HTML links of documents into application), but with no need to have a session cookie (the token is used instead). |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $modulepart Name of module or area concerned by file download ('facture', ...) |
|
60
|
|
|
* @param string $original_file Relative path with filename, relative to modulepart (for example: IN201701-999/IN201701-999.pdf) |
|
61
|
|
|
* @return array List of documents |
|
62
|
|
|
* |
|
63
|
|
|
* @throws RestException 400 |
|
64
|
|
|
* @throws RestException 401 |
|
65
|
|
|
* @throws RestException 404 |
|
66
|
|
|
* |
|
67
|
|
|
* @url GET /download |
|
68
|
|
|
*/ |
|
69
|
|
|
public function index($modulepart, $original_file = '') |
|
70
|
|
|
{ |
|
71
|
|
|
global $conf, $langs; |
|
72
|
|
|
|
|
73
|
|
|
if (empty($modulepart)) { |
|
74
|
|
|
throw new RestException(400, 'bad value for parameter modulepart'); |
|
75
|
|
|
} |
|
76
|
|
|
if (empty($original_file)) { |
|
77
|
|
|
throw new RestException(400, 'bad value for parameter original_file'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
//--- Finds and returns the document |
|
81
|
|
|
$entity = $conf->entity; |
|
82
|
|
|
|
|
83
|
|
|
// Special cases that need to use get_exdir to get real dir of object |
|
84
|
|
|
// If future, all object should use this to define path of documents. |
|
85
|
|
|
/* |
|
86
|
|
|
$tmpreldir = ''; |
|
87
|
|
|
if ($modulepart == 'supplier_invoice') { |
|
88
|
|
|
$tmpreldir = get_exdir($object->id, 2, 0, 0, $object, 'invoice_supplier'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$relativefile = $tmpreldir.dol_sanitizeFileName($object->ref); */ |
|
92
|
|
|
$relativefile = $original_file; |
|
93
|
|
|
|
|
94
|
|
|
$check_access = dol_check_secure_access_document($modulepart, $relativefile, $entity, DolibarrApiAccess::$user, '', 'read'); |
|
95
|
|
|
$accessallowed = $check_access['accessallowed']; |
|
96
|
|
|
$sqlprotectagainstexternals = $check_access['sqlprotectagainstexternals']; |
|
97
|
|
|
$original_file = $check_access['original_file']; |
|
98
|
|
|
|
|
99
|
|
|
if (preg_match('/\.\./', $original_file) || preg_match('/[<>|]/', $original_file)) { |
|
100
|
|
|
throw new RestException(401); |
|
101
|
|
|
} |
|
102
|
|
|
if (!$accessallowed) { |
|
103
|
|
|
throw new RestException(401); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$filename = basename($original_file); |
|
107
|
|
|
$original_file_osencoded = dol_osencode($original_file); // New file name encoded in OS encoding charset |
|
108
|
|
|
|
|
109
|
|
|
if (!file_exists($original_file_osencoded)) |
|
110
|
|
|
{ |
|
111
|
|
|
dol_syslog("Try to download not found file ".$original_file_osencoded, LOG_WARNING); |
|
112
|
|
|
throw new RestException(404, 'File not found'); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
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'); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Build a document. |
|
122
|
|
|
* |
|
123
|
|
|
* Test sample 1: { "module_part": "invoice", "original_file": "FA1701-001/FA1701-001.pdf", "doctemplate": "crabe", "langcode": "fr_FR" }. |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $modulepart Name of module or area concerned by file download ('invoice', 'order', ...). |
|
126
|
|
|
* @param string $original_file Relative path with filename, relative to modulepart (for example: IN201701-999/IN201701-999.pdf). |
|
127
|
|
|
* @param string $doctemplate Set here the doc template to use for document generation (If not set, use the default template). |
|
128
|
|
|
* @param string $langcode Language code like 'en_US', 'fr_FR', 'es_ES', ... (If not set, use the default language). |
|
129
|
|
|
* @return array List of documents |
|
130
|
|
|
* |
|
131
|
|
|
* @throws RestException 500 |
|
132
|
|
|
* @throws RestException 501 |
|
133
|
|
|
* @throws RestException 400 |
|
134
|
|
|
* @throws RestException 401 |
|
135
|
|
|
* @throws RestException 404 |
|
136
|
|
|
* |
|
137
|
|
|
* @url PUT /builddoc |
|
138
|
|
|
*/ |
|
139
|
|
|
public function builddoc($modulepart, $original_file = '', $doctemplate = '', $langcode = '') |
|
140
|
|
|
{ |
|
141
|
|
|
global $conf, $langs; |
|
142
|
|
|
|
|
143
|
|
|
if (empty($modulepart)) { |
|
144
|
|
|
throw new RestException(400, 'bad value for parameter modulepart'); |
|
145
|
|
|
} |
|
146
|
|
|
if (empty($original_file)) { |
|
147
|
|
|
throw new RestException(400, 'bad value for parameter original_file'); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
$outputlangs = $langs; |
|
151
|
|
|
if ($langcode && $langs->defaultlang != $langcode) |
|
152
|
|
|
{ |
|
153
|
|
|
$outputlangs = new Translate('', $conf); |
|
154
|
|
|
$outputlangs->setDefaultLang($langcode); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
//--- Finds and returns the document |
|
158
|
|
|
$entity = $conf->entity; |
|
159
|
|
|
|
|
160
|
|
|
// Special cases that need to use get_exdir to get real dir of object |
|
161
|
|
|
// If future, all object should use this to define path of documents. |
|
162
|
|
|
/* |
|
163
|
|
|
$tmpreldir = ''; |
|
164
|
|
|
if ($modulepart == 'supplier_invoice') { |
|
165
|
|
|
$tmpreldir = get_exdir($object->id, 2, 0, 0, $object, 'invoice_supplier'); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
$relativefile = $tmpreldir.dol_sanitizeFileName($object->ref); */ |
|
169
|
|
|
$relativefile = $original_file; |
|
170
|
|
|
|
|
171
|
|
|
$check_access = dol_check_secure_access_document($modulepart, $relativefile, $entity, DolibarrApiAccess::$user, '', 'write'); |
|
172
|
|
|
$accessallowed = $check_access['accessallowed']; |
|
173
|
|
|
$sqlprotectagainstexternals = $check_access['sqlprotectagainstexternals']; |
|
174
|
|
|
$original_file = $check_access['original_file']; |
|
175
|
|
|
|
|
176
|
|
|
if (preg_match('/\.\./', $original_file) || preg_match('/[<>|]/', $original_file)) { |
|
177
|
|
|
throw new RestException(401); |
|
178
|
|
|
} |
|
179
|
|
|
if (!$accessallowed) { |
|
180
|
|
|
throw new RestException(401); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
// --- Generates the document |
|
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; |
|
187
|
|
|
|
|
188
|
|
|
$templateused = ''; |
|
189
|
|
|
|
|
190
|
|
|
if ($modulepart == 'facture' || $modulepart == 'invoice') |
|
191
|
|
|
{ |
|
192
|
|
|
require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php'; |
|
193
|
|
|
$this->invoice = new Facture($this->db); |
|
194
|
|
|
$result = $this->invoice->fetch(0, preg_replace('/\.[^\.]+$/', '', basename($original_file))); |
|
195
|
|
|
if (!$result) { |
|
196
|
|
|
throw new RestException(404, 'Invoice not found'); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
$templateused = $doctemplate ? $doctemplate : $this->invoice->modelpdf; |
|
200
|
|
|
$result = $this->invoice->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref); |
|
201
|
|
|
if ($result <= 0) { |
|
202
|
|
|
throw new RestException(500, 'Error generating document'); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
elseif ($modulepart == 'commande' || $modulepart == 'order') |
|
206
|
|
|
{ |
|
207
|
|
|
require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php'; |
|
208
|
|
|
$this->order = new Commande($this->db); |
|
209
|
|
|
$result = $this->order->fetch(0, preg_replace('/\.[^\.]+$/', '', basename($original_file))); |
|
210
|
|
|
if (!$result) { |
|
211
|
|
|
throw new RestException(404, 'Order not found'); |
|
212
|
|
|
} |
|
213
|
|
|
$templateused = $doctemplate ? $doctemplate : $this->order->modelpdf; |
|
214
|
|
|
$result = $this->order->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref); |
|
215
|
|
|
if ($result <= 0) { |
|
216
|
|
|
throw new RestException(500, 'Error generating document'); |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
elseif ($modulepart == 'propal' || $modulepart == 'proposal') |
|
220
|
|
|
{ |
|
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))); |
|
224
|
|
|
if (!$result) { |
|
225
|
|
|
throw new RestException(404, 'Proposal not found'); |
|
226
|
|
|
} |
|
227
|
|
|
$templateused = $doctemplate ? $doctemplate : $this->propal->modelpdf; |
|
228
|
|
|
$result = $this->propal->generateDocument($templateused, $outputlangs, $hidedetails, $hidedesc, $hideref); |
|
229
|
|
|
if ($result <= 0) { |
|
230
|
|
|
throw new RestException(500, 'Error generating document'); |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
else |
|
234
|
|
|
{ |
|
235
|
|
|
throw new RestException(403, 'Generation not available for this modulepart'); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
$filename = basename($original_file); |
|
239
|
|
|
$original_file_osencoded = dol_osencode($original_file); // New file name encoded in OS encoding charset |
|
240
|
|
|
|
|
241
|
|
|
if (!file_exists($original_file_osencoded)) |
|
242
|
|
|
{ |
|
243
|
|
|
throw new RestException(404, 'File not found'); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
$file_content = file_get_contents($original_file_osencoded); |
|
247
|
|
|
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'); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Return the list of documents of a dedicated element (from its ID or Ref) |
|
252
|
|
|
* |
|
253
|
|
|
* @param string $modulepart Name of module or area concerned ('thirdparty', 'member', 'proposal', 'order', 'invoice', 'supplier_invoice', 'shipment', 'project', ...) |
|
254
|
|
|
* @param int $id ID of element |
|
255
|
|
|
* @param string $ref Ref of element |
|
256
|
|
|
* @param string $sortfield Sort criteria ('','fullname','relativename','name','date','size') |
|
257
|
|
|
* @param string $sortorder Sort order ('asc' or 'desc') |
|
258
|
|
|
* @return array Array of documents with path |
|
259
|
|
|
* |
|
260
|
|
|
* @throws RestException 400 |
|
261
|
|
|
* @throws RestException 401 |
|
262
|
|
|
* @throws RestException 404 |
|
263
|
|
|
* @throws RestException 500 |
|
264
|
|
|
* |
|
265
|
|
|
* @url GET / |
|
266
|
|
|
*/ |
|
267
|
|
|
public function getDocumentsListByElement($modulepart, $id = 0, $ref = '', $sortfield = '', $sortorder = '') |
|
268
|
|
|
{ |
|
269
|
|
|
global $conf; |
|
270
|
|
|
|
|
271
|
|
|
if (empty($modulepart)) { |
|
272
|
|
|
throw new RestException(400, 'bad value for parameter modulepart'); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
if (empty($id) && empty($ref)) { |
|
276
|
|
|
throw new RestException(400, 'bad value for parameter id or ref'); |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
$id = (empty($id) ? 0 : $id); |
|
280
|
|
|
|
|
281
|
|
|
if ($modulepart == 'societe' || $modulepart == 'thirdparty') |
|
282
|
|
|
{ |
|
283
|
|
|
require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php'; |
|
284
|
|
|
|
|
285
|
|
|
if (!DolibarrApiAccess::$user->rights->societe->lire) { |
|
286
|
|
|
throw new RestException(401); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
$object = new Societe($this->db); |
|
290
|
|
|
$result = $object->fetch($id, $ref); |
|
291
|
|
|
if (!$result) { |
|
292
|
|
|
throw new RestException(404, 'Thirdparty not found'); |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
$upload_dir = $conf->societe->multidir_output[$object->entity]."/".$object->id; |
|
296
|
|
|
} |
|
297
|
|
|
elseif ($modulepart == 'user') |
|
298
|
|
|
{ |
|
299
|
|
|
require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php'; |
|
300
|
|
|
|
|
301
|
|
|
// Can get doc if has permission to read all user or if it is user itself |
|
302
|
|
|
if (!DolibarrApiAccess::$user->rights->user->user->lire && DolibarrApiAccess::$user->id != $id) { |
|
303
|
|
|
throw new RestException(401); |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
$object = new User($this->db); |
|
307
|
|
|
$result = $object->fetch($id, $ref); |
|
308
|
|
|
if (!$result) { |
|
309
|
|
|
throw new RestException(404, 'User not found'); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
$upload_dir = $conf->user->dir_output.'/'.get_exdir(0, 0, 0, 0, $object, 'user').'/'.$object->id; |
|
313
|
|
|
} |
|
314
|
|
|
elseif ($modulepart == 'adherent' || $modulepart == 'member') |
|
315
|
|
|
{ |
|
316
|
|
|
require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php'; |
|
317
|
|
|
|
|
318
|
|
|
if (!DolibarrApiAccess::$user->rights->adherent->lire) { |
|
319
|
|
|
throw new RestException(401); |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
$object = new Adherent($this->db); |
|
323
|
|
|
$result = $object->fetch($id, $ref); |
|
324
|
|
|
if (!$result) { |
|
325
|
|
|
throw new RestException(404, 'Member not found'); |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
$upload_dir = $conf->adherent->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'member'); |
|
329
|
|
|
} |
|
330
|
|
|
elseif ($modulepart == 'propal' || $modulepart == 'proposal') |
|
331
|
|
|
{ |
|
332
|
|
|
require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php'; |
|
333
|
|
|
|
|
334
|
|
|
if (!DolibarrApiAccess::$user->rights->propal->lire) { |
|
335
|
|
|
throw new RestException(401); |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
$object = new Propal($this->db); |
|
339
|
|
|
$result = $object->fetch($id, $ref); |
|
340
|
|
|
if (!$result) { |
|
341
|
|
|
throw new RestException(404, 'Proposal not found'); |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
$upload_dir = $conf->propal->multidir_output[$object->entity]."/".get_exdir(0, 0, 0, 1, $object, 'propal'); |
|
345
|
|
|
} |
|
346
|
|
|
elseif ($modulepart == 'commande' || $modulepart == 'order') |
|
347
|
|
|
{ |
|
348
|
|
|
require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php'; |
|
349
|
|
|
|
|
350
|
|
|
if (!DolibarrApiAccess::$user->rights->commande->lire) { |
|
351
|
|
|
throw new RestException(401); |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
|
|
$object = new Commande($this->db); |
|
355
|
|
|
$result = $object->fetch($id, $ref); |
|
356
|
|
|
if (!$result) { |
|
357
|
|
|
throw new RestException(404, 'Order not found'); |
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
|
|
$upload_dir = $conf->commande->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'commande'); |
|
361
|
|
|
} |
|
362
|
|
|
elseif ($modulepart == 'shipment' || $modulepart == 'expedition') |
|
363
|
|
|
{ |
|
364
|
|
|
require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php'; |
|
365
|
|
|
|
|
366
|
|
|
if (!DolibarrApiAccess::$user->rights->expedition->lire) { |
|
367
|
|
|
throw new RestException(401); |
|
368
|
|
|
} |
|
369
|
|
|
|
|
370
|
|
|
$object = new Expedition($this->db); |
|
371
|
|
|
$result = $object->fetch($id, $ref); |
|
372
|
|
|
if (!$result) { |
|
373
|
|
|
throw new RestException(404, 'Shipment not found'); |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
$upload_dir = $conf->expedition->dir_output."/sending/".get_exdir(0, 0, 0, 1, $object, 'shipment'); |
|
377
|
|
|
} |
|
378
|
|
|
elseif ($modulepart == 'facture' || $modulepart == 'invoice') |
|
379
|
|
|
{ |
|
380
|
|
|
require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php'; |
|
381
|
|
|
|
|
382
|
|
|
if (!DolibarrApiAccess::$user->rights->facture->lire) { |
|
383
|
|
|
throw new RestException(401); |
|
384
|
|
|
} |
|
385
|
|
|
|
|
386
|
|
|
$object = new Facture($this->db); |
|
387
|
|
|
$result = $object->fetch($id, $ref); |
|
388
|
|
|
if (!$result) { |
|
389
|
|
|
throw new RestException(404, 'Invoice not found'); |
|
390
|
|
|
} |
|
391
|
|
|
|
|
392
|
|
|
$upload_dir = $conf->facture->dir_output."/".get_exdir(0, 0, 0, 1, $object, 'invoice'); |
|
393
|
|
|
} |
|
394
|
|
|
elseif ($modulepart == 'facture_fournisseur' || $modulepart == 'supplier_invoice') |
|
395
|
|
|
{ |
|
396
|
|
|
$modulepart = 'supplier_invoice'; |
|
397
|
|
|
|
|
398
|
|
|
require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php'; |
|
399
|
|
|
|
|
400
|
|
|
if (!DolibarrApiAccess::$user->rights->fournisseur->facture->lire) { |
|
401
|
|
|
throw new RestException(401); |
|
402
|
|
|
} |
|
403
|
|
|
|
|
404
|
|
|
$object = new FactureFournisseur($this->db); |
|
405
|
|
|
$result = $object->fetch($id, $ref); |
|
406
|
|
|
if (!$result) { |
|
407
|
|
|
throw new RestException(404, 'Invoice not found'); |
|
408
|
|
|
} |
|
409
|
|
|
|
|
410
|
|
|
$upload_dir = $conf->fournisseur->dir_output."/facture/".get_exdir($object->id, 2, 0, 0, $object, 'invoice_supplier').dol_sanitizeFileName($object->ref); |
|
411
|
|
|
} |
|
412
|
|
|
elseif ($modulepart == 'produit' || $modulepart == 'product') |
|
413
|
|
|
{ |
|
414
|
|
|
require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; |
|
415
|
|
|
|
|
416
|
|
|
if (!DolibarrApiAccess::$user->rights->produit->lire) { |
|
417
|
|
|
throw new RestException(401); |
|
418
|
|
|
} |
|
419
|
|
|
|
|
420
|
|
|
$object = new Product($this->db); |
|
421
|
|
|
$result = $object->fetch($id, $ref); |
|
422
|
|
|
if (!$result) { |
|
423
|
|
|
throw new RestException(404, 'Product not found'); |
|
424
|
|
|
} |
|
425
|
|
|
|
|
426
|
|
|
$upload_dir = $conf->product->multidir_output[$object->entity].'/'.get_exdir(0, 0, 0, 0, $object, 'product').dol_sanitizeFileName($object->ref); |
|
427
|
|
|
} |
|
428
|
|
|
elseif ($modulepart == 'agenda' || $modulepart == 'action' || $modulepart == 'event') |
|
429
|
|
|
{ |
|
430
|
|
|
require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php'; |
|
431
|
|
|
|
|
432
|
|
|
if (!DolibarrApiAccess::$user->rights->agenda->myactions->read && !DolibarrApiAccess::$user->rights->agenda->allactions->read) { |
|
433
|
|
|
throw new RestException(401); |
|
434
|
|
|
} |
|
435
|
|
|
|
|
436
|
|
|
$object = new ActionComm($this->db); |
|
437
|
|
|
$result = $object->fetch($id, $ref); |
|
438
|
|
|
if (!$result) { |
|
439
|
|
|
throw new RestException(404, 'Event not found'); |
|
440
|
|
|
} |
|
441
|
|
|
|
|
442
|
|
|
$upload_dir = $conf->agenda->dir_output.'/'.dol_sanitizeFileName($object->ref); |
|
443
|
|
|
} |
|
444
|
|
|
elseif ($modulepart == 'expensereport') |
|
445
|
|
|
{ |
|
446
|
|
|
require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php'; |
|
447
|
|
|
|
|
448
|
|
|
if (!DolibarrApiAccess::$user->rights->expensereport->read && !DolibarrApiAccess::$user->rights->expensereport->read) { |
|
449
|
|
|
throw new RestException(401); |
|
450
|
|
|
} |
|
451
|
|
|
|
|
452
|
|
|
$object = new ExpenseReport($this->db); |
|
453
|
|
|
$result = $object->fetch($id, $ref); |
|
454
|
|
|
if (!$result) { |
|
455
|
|
|
throw new RestException(404, 'Expense report not found'); |
|
456
|
|
|
} |
|
457
|
|
|
|
|
458
|
|
|
$upload_dir = $conf->expensereport->dir_output.'/'.dol_sanitizeFileName($object->ref); |
|
459
|
|
|
} |
|
460
|
|
|
else |
|
461
|
|
|
{ |
|
462
|
|
|
throw new RestException(500, 'Modulepart '.$modulepart.' not implemented yet.'); |
|
463
|
|
|
} |
|
464
|
|
|
|
|
465
|
|
|
$filearray = dol_dir_list($upload_dir, "files", 0, '', '(\.meta|_preview.*\.png)$', $sortfield, (strtolower($sortorder) == 'desc' ?SORT_DESC:SORT_ASC), 1); |
|
466
|
|
|
if (empty($filearray)) { |
|
467
|
|
|
throw new RestException(404, 'Search for modulepart '.$modulepart.' with Id '.$object->id.(!empty($object->Ref) ? ' or Ref '.$object->ref : '').' does not return any document.'); |
|
468
|
|
|
} |
|
469
|
|
|
|
|
470
|
|
|
return $filearray; |
|
471
|
|
|
} |
|
472
|
|
|
|
|
473
|
|
|
|
|
474
|
|
|
/** |
|
475
|
|
|
* Return a document. |
|
476
|
|
|
* |
|
477
|
|
|
* @param int $id ID of document |
|
478
|
|
|
* @return array Array with data of file |
|
479
|
|
|
* |
|
480
|
|
|
* @throws RestException |
|
481
|
|
|
*/ |
|
482
|
|
|
/* |
|
483
|
|
|
public function get($id) { |
|
484
|
|
|
return array('note'=>'xxx'); |
|
485
|
|
|
}*/ |
|
486
|
|
|
|
|
487
|
|
|
|
|
488
|
|
|
/** |
|
489
|
|
|
* Upload a file. |
|
490
|
|
|
* |
|
491
|
|
|
* Test sample for invoice: { "filename": "mynewfile.txt", "modulepart": "invoice", "ref": "FA1701-001", "subdir": "", "filecontent": "content text", "fileencoding": "", "overwriteifexists": "0" }. |
|
492
|
|
|
* Test sample for supplier invoice: { "filename": "mynewfile.txt", "modulepart": "supplier_invoice", "ref": "FA1701-001", "subdir": "", "filecontent": "content text", "fileencoding": "", "overwriteifexists": "0" }. |
|
493
|
|
|
* Test sample for medias file: { "filename": "mynewfile.txt", "modulepart": "medias", "ref": "", "subdir": "image/mywebsite", "filecontent": "Y29udGVudCB0ZXh0Cg==", "fileencoding": "base64", "overwriteifexists": "0" }. |
|
494
|
|
|
* |
|
495
|
|
|
* @param string $filename Name of file to create ('FA1705-0123.txt') |
|
496
|
|
|
* @param string $modulepart Name of module or area concerned by file upload ('facture', 'project', 'project_task', ...) |
|
497
|
|
|
* @param string $ref Reference of object (This will define subdir automatically and store submited file into it) |
|
498
|
|
|
* @param string $subdir Subdirectory (Only if ref not provided) |
|
499
|
|
|
* @param string $filecontent File content (string with file content. An empty file will be created if this parameter is not provided) |
|
500
|
|
|
* @param string $fileencoding File encoding (''=no encoding, 'base64'=Base 64) |
|
501
|
|
|
* @param int $overwriteifexists Overwrite file if exists (1 by default) |
|
502
|
|
|
* @return string |
|
503
|
|
|
* |
|
504
|
|
|
* @throws RestException 400 |
|
505
|
|
|
* @throws RestException 401 |
|
506
|
|
|
* @throws RestException 404 |
|
507
|
|
|
* @throws RestException 500 |
|
508
|
|
|
* |
|
509
|
|
|
* @url POST /upload |
|
510
|
|
|
*/ |
|
511
|
|
|
public function post($filename, $modulepart, $ref = '', $subdir = '', $filecontent = '', $fileencoding = '', $overwriteifexists = 0) |
|
512
|
|
|
{ |
|
513
|
|
|
global $db, $conf; |
|
514
|
|
|
|
|
515
|
|
|
/*var_dump($modulepart); |
|
516
|
|
|
var_dump($filename); |
|
517
|
|
|
var_dump($filecontent); |
|
518
|
|
|
exit;*/ |
|
519
|
|
|
|
|
520
|
|
|
if (empty($modulepart)) |
|
521
|
|
|
{ |
|
522
|
|
|
throw new RestException(400, 'Modulepart not provided.'); |
|
523
|
|
|
} |
|
524
|
|
|
|
|
525
|
|
|
if (!DolibarrApiAccess::$user->rights->ecm->upload) { |
|
526
|
|
|
throw new RestException(401); |
|
527
|
|
|
} |
|
528
|
|
|
|
|
529
|
|
|
$newfilecontent = ''; |
|
530
|
|
|
if (empty($fileencoding)) $newfilecontent = $filecontent; |
|
531
|
|
|
if ($fileencoding == 'base64') $newfilecontent = base64_decode($filecontent); |
|
532
|
|
|
|
|
533
|
|
|
$original_file = dol_sanitizeFileName($filename); |
|
534
|
|
|
|
|
535
|
|
|
// Define $uploadir |
|
536
|
|
|
$object = null; |
|
537
|
|
|
$entity = DolibarrApiAccess::$user->entity; |
|
538
|
|
|
if ($ref) |
|
539
|
|
|
{ |
|
540
|
|
|
$tmpreldir = ''; |
|
541
|
|
|
|
|
542
|
|
|
if ($modulepart == 'facture' || $modulepart == 'invoice') |
|
543
|
|
|
{ |
|
544
|
|
|
$modulepart = 'facture'; |
|
545
|
|
|
|
|
546
|
|
|
require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php'; |
|
547
|
|
|
$object = new Facture($this->db); |
|
548
|
|
|
} |
|
549
|
|
|
elseif ($modulepart == 'facture_fournisseur' || $modulepart == 'supplier_invoice') |
|
550
|
|
|
{ |
|
551
|
|
|
$modulepart = 'supplier_invoice'; |
|
552
|
|
|
|
|
553
|
|
|
require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php'; |
|
554
|
|
|
$object = new FactureFournisseur($this->db); |
|
555
|
|
|
} |
|
556
|
|
|
elseif ($modulepart == 'project') |
|
557
|
|
|
{ |
|
558
|
|
|
require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php'; |
|
559
|
|
|
$object = new Project($this->db); |
|
560
|
|
|
} |
|
561
|
|
|
elseif ($modulepart == 'task' || $modulepart == 'project_task') |
|
562
|
|
|
{ |
|
563
|
|
|
$modulepart = 'project_task'; |
|
564
|
|
|
|
|
565
|
|
|
require_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php'; |
|
566
|
|
|
$object = new Task($this->db); |
|
567
|
|
|
|
|
568
|
|
|
$task_result = $object->fetch('', $ref); |
|
569
|
|
|
|
|
570
|
|
|
// Fetching the tasks project is required because its out_dir might be a sub-directory of the project |
|
571
|
|
|
if ($task_result > 0) |
|
572
|
|
|
{ |
|
573
|
|
|
$project_result = $object->fetch_projet(); |
|
574
|
|
|
|
|
575
|
|
|
if ($project_result >= 0) |
|
576
|
|
|
{ |
|
577
|
|
|
$tmpreldir = dol_sanitizeFileName($object->project->ref).'/'; |
|
578
|
|
|
} |
|
579
|
|
|
} |
|
580
|
|
|
else |
|
581
|
|
|
{ |
|
582
|
|
|
throw new RestException(500, 'Error while fetching Task '.$ref); |
|
583
|
|
|
} |
|
584
|
|
|
} |
|
585
|
|
|
elseif ($modulepart == 'product' || $modulepart == 'produit' || $modulepart == 'service' || $modulepart == 'produit|service') |
|
586
|
|
|
{ |
|
587
|
|
|
require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; |
|
588
|
|
|
$object = new Product($this->db); |
|
589
|
|
|
} |
|
590
|
|
|
elseif ($modulepart == 'expensereport') |
|
591
|
|
|
{ |
|
592
|
|
|
require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php'; |
|
593
|
|
|
$object = new ExpenseReport($this->db); |
|
594
|
|
|
} |
|
595
|
|
|
// TODO Implement additional moduleparts |
|
596
|
|
|
else |
|
597
|
|
|
{ |
|
598
|
|
|
throw new RestException(500, 'Modulepart '.$modulepart.' not implemented yet.'); |
|
599
|
|
|
} |
|
600
|
|
|
|
|
601
|
|
|
if (is_object($object)) |
|
602
|
|
|
{ |
|
603
|
|
|
$result = $object->fetch('', $ref); |
|
604
|
|
|
|
|
605
|
|
|
if ($result == 0) |
|
606
|
|
|
{ |
|
607
|
|
|
throw new RestException(404, "Object with ref '".$ref."' was not found."); |
|
608
|
|
|
} |
|
609
|
|
|
elseif ($result < 0) |
|
610
|
|
|
{ |
|
611
|
|
|
throw new RestException(500, 'Error while fetching object.'); |
|
612
|
|
|
} |
|
613
|
|
|
} |
|
614
|
|
|
|
|
615
|
|
|
if (!($object->id > 0)) |
|
616
|
|
|
{ |
|
617
|
|
|
throw new RestException(404, 'The object '.$modulepart." with ref '".$ref."' was not found."); |
|
618
|
|
|
} |
|
619
|
|
|
|
|
620
|
|
|
// Special cases that need to use get_exdir to get real dir of object |
|
621
|
|
|
// If future, all object should use this to define path of documents. |
|
622
|
|
|
if ($modulepart == 'supplier_invoice') { |
|
623
|
|
|
$tmpreldir = get_exdir($object->id, 2, 0, 0, $object, 'invoice_supplier'); |
|
624
|
|
|
} |
|
625
|
|
|
|
|
626
|
|
|
$relativefile = $tmpreldir.dol_sanitizeFileName($object->ref); |
|
627
|
|
|
|
|
628
|
|
|
$tmp = dol_check_secure_access_document($modulepart, $relativefile, $entity, DolibarrApiAccess::$user, $ref, 'write'); |
|
629
|
|
|
$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 |
|
630
|
|
|
|
|
631
|
|
|
if (empty($upload_dir) || $upload_dir == '/') |
|
632
|
|
|
{ |
|
633
|
|
|
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.'); |
|
634
|
|
|
} |
|
635
|
|
|
} |
|
636
|
|
|
else |
|
637
|
|
|
{ |
|
638
|
|
|
if ($modulepart == 'invoice') $modulepart = 'facture'; |
|
639
|
|
|
|
|
640
|
|
|
$relativefile = $subdir; |
|
641
|
|
|
|
|
642
|
|
|
$tmp = dol_check_secure_access_document($modulepart, $relativefile, $entity, DolibarrApiAccess::$user, '', 'write'); |
|
643
|
|
|
$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 |
|
644
|
|
|
|
|
645
|
|
|
if (empty($upload_dir) || $upload_dir == '/') |
|
646
|
|
|
{ |
|
647
|
|
|
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.'); |
|
648
|
|
|
} |
|
649
|
|
|
} |
|
650
|
|
|
// $original_file here is still value of filename without any dir. |
|
651
|
|
|
|
|
652
|
|
|
$upload_dir = dol_sanitizePathName($upload_dir); |
|
653
|
|
|
|
|
654
|
|
|
if (dol_mkdir($upload_dir) < 0) // needed by products |
|
655
|
|
|
{ |
|
656
|
|
|
throw new RestException(500, 'Error while trying to create directory.'); |
|
657
|
|
|
} |
|
658
|
|
|
|
|
659
|
|
|
$destfile = $upload_dir.'/'.$original_file; |
|
660
|
|
|
$destfiletmp = DOL_DATA_ROOT.'/admin/temp/'.$original_file; |
|
661
|
|
|
dol_delete_file($destfiletmp); |
|
662
|
|
|
//var_dump($original_file);exit; |
|
663
|
|
|
|
|
664
|
|
|
if (!dol_is_dir(dirname($destfile))) { |
|
665
|
|
|
throw new RestException(401, 'Directory not exists : '.dirname($destfile)); |
|
666
|
|
|
} |
|
667
|
|
|
|
|
668
|
|
|
if (!$overwriteifexists && dol_is_file($destfile)) |
|
669
|
|
|
{ |
|
670
|
|
|
throw new RestException(500, "File with name '".$original_file."' already exists."); |
|
671
|
|
|
} |
|
672
|
|
|
|
|
673
|
|
|
$fhandle = @fopen($destfiletmp, 'w'); |
|
674
|
|
|
if ($fhandle) |
|
675
|
|
|
{ |
|
676
|
|
|
$nbofbyteswrote = fwrite($fhandle, $newfilecontent); |
|
677
|
|
|
fclose($fhandle); |
|
678
|
|
|
@chmod($destfiletmp, octdec($conf->global->MAIN_UMASK)); |
|
679
|
|
|
} |
|
680
|
|
|
else |
|
681
|
|
|
{ |
|
682
|
|
|
throw new RestException(500, "Failed to open file '".$destfiletmp."' for write"); |
|
683
|
|
|
} |
|
684
|
|
|
|
|
685
|
|
|
$result = dol_move($destfiletmp, $destfile, 0, $overwriteifexists, 1); |
|
686
|
|
|
if (!$result) |
|
687
|
|
|
{ |
|
688
|
|
|
throw new RestException(500, "Failed to move file into '".$destfile."'"); |
|
689
|
|
|
} |
|
690
|
|
|
|
|
691
|
|
|
return dol_basename($destfile); |
|
692
|
|
|
} |
|
693
|
|
|
|
|
694
|
|
|
/** |
|
695
|
|
|
* Delete a document. |
|
696
|
|
|
* |
|
697
|
|
|
* @param string $modulepart Name of module or area concerned by file download ('product', ...) |
|
698
|
|
|
* @param string $original_file Relative path with filename, relative to modulepart (for example: PRODUCT-REF-999/IMAGE-999.jpg) |
|
699
|
|
|
* @return array List of documents |
|
700
|
|
|
* |
|
701
|
|
|
* @throws RestException 400 |
|
702
|
|
|
* @throws RestException 401 |
|
703
|
|
|
* @throws RestException 404 |
|
704
|
|
|
* |
|
705
|
|
|
* @url DELETE / |
|
706
|
|
|
*/ |
|
707
|
|
|
public function delete($modulepart, $original_file) |
|
708
|
|
|
{ |
|
709
|
|
|
global $conf, $langs; |
|
710
|
|
|
|
|
711
|
|
|
if (empty($modulepart)) { |
|
712
|
|
|
throw new RestException(400, 'bad value for parameter modulepart'); |
|
713
|
|
|
} |
|
714
|
|
|
if (empty($original_file)) { |
|
715
|
|
|
throw new RestException(400, 'bad value for parameter original_file'); |
|
716
|
|
|
} |
|
717
|
|
|
|
|
718
|
|
|
//--- Finds and returns the document |
|
719
|
|
|
$entity = $conf->entity; |
|
720
|
|
|
|
|
721
|
|
|
// Special cases that need to use get_exdir to get real dir of object |
|
722
|
|
|
// If future, all object should use this to define path of documents. |
|
723
|
|
|
/* |
|
724
|
|
|
$tmpreldir = ''; |
|
725
|
|
|
if ($modulepart == 'supplier_invoice') { |
|
726
|
|
|
$tmpreldir = get_exdir($object->id, 2, 0, 0, $object, 'invoice_supplier'); |
|
727
|
|
|
} |
|
728
|
|
|
|
|
729
|
|
|
$relativefile = $tmpreldir.dol_sanitizeFileName($object->ref); */ |
|
730
|
|
|
$relativefile = $original_file; |
|
731
|
|
|
|
|
732
|
|
|
$check_access = dol_check_secure_access_document($modulepart, $relativefile, $entity, DolibarrApiAccess::$user, '', 'read'); |
|
733
|
|
|
$accessallowed = $check_access['accessallowed']; |
|
734
|
|
|
$sqlprotectagainstexternals = $check_access['sqlprotectagainstexternals']; |
|
735
|
|
|
$original_file = $check_access['original_file']; |
|
736
|
|
|
|
|
737
|
|
|
if (preg_match('/\.\./', $original_file) || preg_match('/[<>|]/', $original_file)) { |
|
738
|
|
|
throw new RestException(401); |
|
739
|
|
|
} |
|
740
|
|
|
if (!$accessallowed) { |
|
741
|
|
|
throw new RestException(401); |
|
742
|
|
|
} |
|
743
|
|
|
|
|
744
|
|
|
$filename = basename($original_file); |
|
745
|
|
|
$original_file_osencoded = dol_osencode($original_file); // New file name encoded in OS encoding charset |
|
746
|
|
|
|
|
747
|
|
|
if (!file_exists($original_file_osencoded)) |
|
748
|
|
|
{ |
|
749
|
|
|
dol_syslog("Try to download not found file ".$original_file_osencoded, LOG_WARNING); |
|
750
|
|
|
throw new RestException(404, 'File not found'); |
|
751
|
|
|
} |
|
752
|
|
|
|
|
753
|
|
|
if (@unlink($original_file_osencoded)) { |
|
754
|
|
|
return array( |
|
755
|
|
|
'success' => array( |
|
756
|
|
|
'code' => 200, |
|
757
|
|
|
'message' => 'Document deleted' |
|
758
|
|
|
) |
|
759
|
|
|
); |
|
760
|
|
|
} |
|
761
|
|
|
|
|
762
|
|
|
throw new RestException(401); |
|
763
|
|
|
} |
|
764
|
|
|
|
|
765
|
|
|
// phpcs:disable PEAR.NamingConventions.ValidFunctionName |
|
766
|
|
|
/** |
|
767
|
|
|
* Validate fields before create or update object |
|
768
|
|
|
* |
|
769
|
|
|
* @param array $data Array with data to verify |
|
770
|
|
|
* @return array |
|
771
|
|
|
* @throws RestException |
|
772
|
|
|
*/ |
|
773
|
|
|
private function _validate_file($data) |
|
774
|
|
|
{ |
|
775
|
|
|
// phpcs:enable |
|
776
|
|
|
$result = array(); |
|
777
|
|
|
foreach (Documents::$DOCUMENT_FIELDS as $field) { |
|
778
|
|
|
if (!isset($data[$field])) |
|
779
|
|
|
throw new RestException(400, "$field field missing"); |
|
780
|
|
|
$result[$field] = $data[$field]; |
|
781
|
|
|
} |
|
782
|
|
|
return $result; |
|
783
|
|
|
} |
|
784
|
|
|
} |
|
785
|
|
|
|