Commit | Line | Data |
---|---|---|
56154d36 TO |
1 | <?php |
2 | ||
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
5 | | CiviCRM version 4.5 | | |
6 | +--------------------------------------------------------------------+ | |
7 | | Copyright CiviCRM LLC (c) 2004-2014 | | |
8 | +--------------------------------------------------------------------+ | |
9 | | This file is a part of CiviCRM. | | |
10 | | | | |
11 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
12 | | under the terms of the GNU Affero General Public License | | |
13 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
14 | | | | |
15 | | CiviCRM is distributed in the hope that it will be useful, but | | |
16 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
17 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
18 | | See the GNU Affero General Public License for more details. | | |
19 | | | | |
20 | | You should have received a copy of the GNU Affero General Public | | |
21 | | License and the CiviCRM Licensing Exception along | | |
22 | | with this program; if not, contact CiviCRM LLC | | |
23 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
24 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
25 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
26 | +--------------------------------------------------------------------+ | |
27 | */ | |
28 | ||
29 | /** | |
30 | * "Attachment" is a pseudo-entity which represents a record in civicrm_file | |
31 | * combined with a record in civicrm_entity_file as well as the underlying | |
32 | * file content. | |
33 | * | |
34 | * @code | |
35 | * $result = civicrm_api3('Attachment', 'create', array( | |
36 | * 'entity_table' => 'civicrm_activity', | |
37 | * 'entity_id' => 123, | |
38 | * 'name' => 'README.txt', | |
39 | * 'mime_type' => 'text/plain', | |
40 | * 'content' => 'Please to read the README', | |
41 | * )); | |
42 | * $attachment = $result['values'][$result['id']]; | |
43 | * echo sprintf("<a href='%s'>View %s</a>", $attachment['url'], $attachment['name']); | |
44 | * @endcode | |
45 | * | |
46 | * @code | |
47 | * $result = civicrm_api3('Attachment', 'create', array( | |
48 | * 'entity_table' => 'civicrm_activity', | |
49 | * 'entity_id' => 123, | |
50 | * 'name' => 'README.txt', | |
51 | * 'mime_type' => 'text/plain', | |
52 | * 'options' => array( | |
53 | * 'move-file' => '/tmp/upload1a2b3c4d', | |
54 | * ), | |
55 | * )); | |
56 | * $attachment = $result['values'][$result['id']]; | |
57 | * echo sprintf("<a href='%s'>View %s</a>", $attachment['url'], $attachment['name']); | |
58 | * @endcode | |
59 | * | |
60 | * Notes: | |
61 | * - File content is not returned by default. One must specify 'return => content'. | |
62 | * - Features which deal with local file system (e.g. passing "options.move-file" | |
63 | * or returning a "path") are only valid when executed as a local API (ie | |
64 | * "check_permissions"==false) | |
65 | * | |
66 | * @package CiviCRM_APIv3 | |
67 | * @subpackage API_Attachment | |
68 | * @copyright CiviCRM LLC (c) 2004-2014 | |
69 | * $Id: $ | |
70 | * | |
71 | */ | |
72 | ||
73 | /** | |
74 | * Adjust metadata for "create" action | |
75 | * | |
76 | * @param array $spec list of fields | |
77 | */ | |
78 | function _civicrm_api3_attachment_create_spec(&$spec) { | |
79 | $spec = array_merge($spec, _civicrm_api3_attachment_getfields()); | |
80 | $spec['name']['api.required'] = 1; | |
81 | $spec['mime_type']['api.required'] = 1; | |
82 | $spec['entity_table']['api.required'] = 1; | |
83 | $spec['entity_id']['api.required'] = 1; | |
84 | $spec['upload_date']['api.default'] = 'now'; | |
85 | } | |
86 | ||
87 | /** | |
88 | * Create an attachment | |
89 | * | |
90 | * @param array $params | |
91 | * @return array of newly created file property values. | |
92 | * @access public | |
93 | * @throws API_Exception validation errors | |
94 | */ | |
95 | function civicrm_api3_attachment_create($params) { | |
96 | $config = CRM_Core_Config::singleton(); | |
97 | list($id, $file, $entityFile, $name, $content, $moveFile, $isTrusted, $returnContent) = _civicrm_api3_attachment_parse_params($params); | |
98 | ||
99 | $fileDao = new CRM_Core_BAO_File(); | |
100 | $entityFileDao = new CRM_Core_DAO_EntityFile(); | |
101 | ||
102 | if ($id) { | |
103 | $fileDao->id = $id; | |
104 | if (!$fileDao->find(TRUE)) { | |
105 | throw new API_Exception("Invalid ID"); | |
106 | } | |
107 | ||
108 | $entityFileDao->file_id = $id; | |
109 | if (!$entityFileDao->find(TRUE)) { | |
110 | throw new API_Exception("Cannot modify orphaned file"); | |
111 | } | |
112 | } | |
113 | ||
114 | if (!$id && !is_string($content) && !is_string($moveFile)) { | |
115 | throw new API_Exception("Mandatory key(s) missing from params array: 'id' or 'content' or 'options.move-file'"); | |
116 | } | |
117 | if (!$isTrusted && $moveFile) { | |
118 | throw new API_Exception("options.move-file is only supported on secure calls"); | |
119 | } | |
120 | if (is_string($content) && is_string($moveFile)) { | |
121 | throw new API_Exception("'content' and 'options.move-file' are mutually exclusive"); | |
122 | } | |
123 | if ($id && !$isTrusted && isset($file['upload_date']) && $file['upload_date'] != CRM_Utils_Date::isoToMysql($fileDao->upload_date)) { | |
124 | throw new API_Exception("Cannot modify upload_date" . var_export(array($file['upload_date'], $fileDao->upload_date, CRM_Utils_Date::isoToMysql($fileDao->upload_date)), TRUE)); | |
125 | } | |
126 | if ($id && $name && $name != CRM_Utils_File::cleanFileName($fileDao->uri)) { | |
127 | throw new API_Exception("Cannot modify name"); | |
128 | } | |
129 | ||
130 | $fileDao->copyValues($file); | |
131 | if (!$id) { | |
132 | $fileDao->uri = CRM_Utils_File::makeFileName($name); | |
133 | } | |
134 | $fileDao->save(); | |
135 | ||
136 | $entityFileDao->copyValues($entityFile); | |
137 | $entityFileDao->file_id = $fileDao->id; | |
138 | $entityFileDao->save(); | |
139 | ||
140 | $path = $config->customFileUploadDir . DIRECTORY_SEPARATOR . $fileDao->uri; | |
141 | if (is_string($content)) { | |
142 | file_put_contents($path, $content); | |
143 | } | |
144 | elseif (is_string($moveFile)) { | |
145 | rename($moveFile, $path); | |
146 | } | |
147 | ||
148 | $result = array( | |
149 | $fileDao->id => _civicrm_api3_attachment_format_result($fileDao, $entityFileDao, $returnContent, $isTrusted) | |
150 | ); | |
151 | return civicrm_api3_create_success($result, $params, 'Attachment', 'create'); | |
152 | } | |
153 | ||
154 | /** | |
155 | * Adjust metadata for "create" action | |
156 | * | |
157 | * @param array $spec list of fields | |
158 | */ | |
159 | function _civicrm_api3_attachment_get_spec(&$spec) { | |
160 | $spec = array_merge($spec, _civicrm_api3_attachment_getfields()); | |
161 | } | |
162 | ||
163 | /** | |
164 | * @param array $params | |
165 | * @return array per APIv3 | |
166 | * @throws API_Exception validation errors | |
167 | */ | |
168 | function civicrm_api3_attachment_get($params) { | |
169 | list($id, $file, $entityFile, $name, $content, $moveFile, $isTrusted, $returnContent) = _civicrm_api3_attachment_parse_params($params); | |
170 | ||
171 | $dao = __civicrm_api3_attachment_find($params, $id, $file, $entityFile, $isTrusted); | |
172 | $result = array(); | |
173 | while ($dao->fetch()) { | |
174 | $result[$dao->id] = _civicrm_api3_attachment_format_result($dao, $dao, $returnContent, $isTrusted); | |
175 | } | |
176 | return civicrm_api3_create_success($result, $params, 'Attachment', 'create'); | |
177 | } | |
178 | ||
179 | function _civicrm_api3_attachment_delete_spec(&$spec) { | |
180 | unset($spec['id']['api.required']); | |
181 | $entityFileFields = CRM_Core_DAO_EntityFile::fields(); | |
182 | $spec['entity_table'] = $entityFileFields['entity_table']; | |
183 | $spec['entity_table']['title'] = CRM_Utils_Array::value('title', $spec['entity_table'], 'Entity Table') . ' (write-once)'; | |
184 | $spec['entity_id'] = $entityFileFields['entity_id']; | |
185 | $spec['entity_id']['title'] = CRM_Utils_Array::value('title', $spec['entity_id'], 'Entity ID') . ' (write-once)'; | |
186 | } | |
187 | ||
188 | /** | |
189 | * @param $params | |
190 | * @return array | |
191 | * @throws API_Exception | |
192 | */ | |
193 | function civicrm_api3_attachment_delete($params) { | |
194 | if (!empty($params['id'])) { | |
195 | // ok | |
196 | } | |
197 | elseif (!empty($params['entity_table']) && !empty($params['entity_id'])) { | |
198 | // ok | |
199 | } | |
200 | else { | |
201 | throw new API_Exception("Mandatory key(s) missing from params array: id or entity_table+entity_table"); | |
202 | } | |
203 | ||
204 | $config = CRM_Core_Config::singleton(); | |
205 | list($id, $file, $entityFile, $name, $content, $moveFile, $isTrusted, $returnContent) = _civicrm_api3_attachment_parse_params($params); | |
206 | $dao = __civicrm_api3_attachment_find($params, $id, $file, $entityFile, $isTrusted); | |
207 | ||
208 | $filePaths = array(); | |
209 | $fileIds = array(); | |
210 | while ($dao->fetch()) { | |
211 | $filePaths [] = $config->customFileUploadDir . DIRECTORY_SEPARATOR . $dao->uri; | |
212 | $fileIds[] = $dao->id; | |
213 | } | |
214 | ||
215 | if (!empty($fileIds)) { | |
216 | $idString = implode(',', array_filter($fileIds, 'is_numeric')); | |
217 | CRM_Core_DAO::executeQuery("DELETE FROM civicrm_entity_file WHERE file_id in ($idString)"); | |
218 | CRM_Core_DAO::executeQuery("DELETE FROM civicrm_file WHERE id in ($idString)"); | |
219 | } | |
220 | ||
221 | // unlink is non-transactional, so we do this as the last step -- just in case the other steps produce errors | |
222 | if (!empty($filePaths)) { | |
223 | foreach ($filePaths as $filePath) { | |
224 | unlink($filePath); | |
225 | } | |
226 | } | |
227 | ||
228 | $result = array(); | |
229 | return civicrm_api3_create_success($result, $params, 'Attachment', 'create'); | |
230 | } | |
231 | ||
232 | /** | |
233 | * @param array $params | |
234 | * @param int|null $id the user-supplied ID of the attachment record | |
235 | * @param array $file the user-supplied vales for the file (mime_type, description, upload_date) | |
236 | * @param array $entityFile the user-supllied values of the entity-file (entity_table, entity_id) | |
237 | * @param bool $isTrusted | |
238 | * @return CRM_Core_DAO | |
239 | * @throws API_Exception | |
240 | */ | |
241 | function __civicrm_api3_attachment_find($params, $id, $file, $entityFile, $isTrusted) { | |
242 | foreach (array('name', 'content', 'path', 'url') as $unsupportedFilter) { | |
243 | if (!empty($params[$unsupportedFilter])) { | |
244 | throw new API_Exception("Get by $unsupportedFilter is not currently supported"); | |
245 | } | |
246 | } | |
247 | ||
248 | $select = CRM_Utils_SQL_Select::from('civicrm_file cf') | |
249 | ->join('cef', 'INNER JOIN civicrm_entity_file cef ON cf.id = cef.file_id') | |
250 | ->select(array( | |
251 | 'cf.id', | |
252 | 'cf.uri', | |
253 | 'cf.mime_type', | |
254 | 'cf.description', | |
255 | 'cf.upload_date', | |
256 | 'cef.entity_table', | |
257 | 'cef.entity_id', | |
258 | )); | |
259 | ||
260 | if ($id) { | |
261 | $select->where('cf.id = #id', array('#id' => $id)); | |
262 | } | |
263 | // recall: $file is filtered by parse_params | |
264 | foreach ($file as $key => $value) { | |
265 | $select->where('cf.!field = @value', array( | |
266 | '!field' => $key, | |
267 | '@value' => $value, | |
268 | )); | |
269 | } | |
270 | // recall: $entityFile is filtered by parse_params | |
271 | foreach ($entityFile as $key => $value) { | |
272 | $select->where('cef.!field = @value', array( | |
273 | '!field' => $key, | |
274 | '@value' => $value, | |
275 | )); | |
276 | } | |
277 | if (!$isTrusted) { | |
278 | // FIXME ACLs: Add any JOIN or WHERE clauses needed to enforce access-controls for the target entity. | |
279 | // | |
280 | // The target entity is identified by "cef.entity_table" (aka $entityFile['entity_table']) and "cef.entity_id". | |
281 | // | |
282 | // As a simplification, we *require* the "get" actions to filter on a single "entity_table" which should | |
283 | // avoid the complexity of matching ACL's against multiple entity types. | |
284 | } | |
285 | ||
286 | $dao = CRM_Core_DAO::executeQuery($select->toSQL()); | |
287 | return $dao; | |
288 | } | |
289 | ||
290 | /** | |
291 | * @param array $params | |
292 | * @return array (0 => int $id, 1 => array $file, 2 => array $entityFile, 3 => string $name, 4 => string $content, 5 => string $moveFile, 6 => $isTrusted, 7 => bool $returnContent) | |
293 | * - array $file: whitelisted fields that can pass through directly to civicrm_file | |
294 | * - array $entityFile: whitelisted fields that can pass through directly to civicrm_entity_file | |
295 | * - string $name: the printable name | |
296 | * - string $moveFile: the full path to a local file whose content should be loaded | |
297 | * - bool $isTrusted: whether we trust the requester to do sketchy things (like moving files or reassigning entities) | |
298 | * - bool $returnContent: whether we are expected to return the full content of the file | |
299 | * @throws API_Exception validation errors | |
300 | */ | |
301 | function _civicrm_api3_attachment_parse_params($params) { | |
302 | $id = CRM_Utils_Array::value('id', $params, NULL); | |
303 | if ($id && !is_numeric($id)) { | |
304 | throw new API_Exception("Malformed id"); | |
305 | } | |
306 | ||
307 | $file = array(); | |
308 | foreach (array('mime_type', 'description', 'upload_date') as $field) { | |
309 | if (array_key_exists($field, $params)) { | |
310 | $file[$field] = $params[$field]; | |
311 | } | |
312 | } | |
313 | ||
314 | $entityFile = array(); | |
315 | foreach (array('entity_table', 'entity_id') as $field) { | |
316 | if (array_key_exists($field, $params)) { | |
317 | $entityFile[$field] = $params[$field]; | |
318 | } | |
319 | } | |
320 | ||
321 | $name = NULL; | |
322 | if (array_key_exists('name', $params)) { | |
323 | if ($params['name'] != basename($params['name']) || preg_match(':[/\\\\]:', $params['name'])) { | |
324 | throw new API_Exception('Malformed name'); | |
325 | } | |
326 | $name = $params['name']; | |
327 | } | |
328 | ||
329 | $content = NULL; | |
330 | if (isset($params['content'])) { | |
331 | $content = $params['content']; | |
332 | } | |
333 | ||
334 | $moveFile = NULL; | |
335 | if (isset($params['options']['move-file'])) { | |
336 | $moveFile = $params['options']['move-file']; | |
337 | } | |
338 | elseif (isset($params['options.move-file'])) { | |
339 | $moveFile = $params['options.move-file']; | |
340 | } | |
341 | ||
342 | $isTrusted = empty($params['check_permissions']); | |
343 | ||
344 | $returns = isset($params['return']) ? $params['return'] : array(); | |
345 | $returns = is_array($returns) ? $returns : array($returns); | |
346 | $returnContent = in_array('content', $returns); | |
347 | ||
348 | return array($id, $file, $entityFile, $name, $content, $moveFile, $isTrusted, $returnContent); | |
349 | } | |
350 | ||
351 | /** | |
352 | * @param CRM_Core_DAO_File $fileDao maybe "File" or "File JOIN EntityFile" | |
353 | * @param CRM_Core_DAO_EntityFile $entityFileDao maybe "EntityFile" or "File JOIN EntityFile" | |
354 | * @param bool $returnContent whether to return the full content of the file | |
355 | * @param bool $isTrusted whether the current request is trusted to perform file-specific operations | |
356 | * @return array | |
357 | */ | |
358 | function _civicrm_api3_attachment_format_result($fileDao, $entityFileDao, $returnContent, $isTrusted) { | |
359 | $config = CRM_Core_Config::singleton(); | |
360 | $path = $config->customFileUploadDir . DIRECTORY_SEPARATOR . $fileDao->uri; | |
361 | ||
362 | $result = array( | |
363 | 'id' => $fileDao->id, | |
364 | 'name' => CRM_Utils_File::cleanFileName($fileDao->uri), | |
365 | 'mime_type' => $fileDao->mime_type, | |
366 | 'description' => $fileDao->description, | |
367 | 'upload_date' => is_numeric($fileDao->upload_date) ? CRM_Utils_Date::mysqlToIso($fileDao->upload_date) : $fileDao->upload_date, | |
368 | 'entity_table' => $entityFileDao->entity_table, | |
369 | 'entity_id' => $entityFileDao->entity_id, | |
370 | ); | |
371 | $result['url'] = CRM_Utils_System::url( | |
372 | 'civicrm/file', 'reset=1&id=' . $result['id'] . '&eid=' . $result['entity_id'], | |
373 | TRUE, | |
374 | NULL, | |
375 | FALSE, | |
376 | TRUE | |
377 | ); | |
378 | if ($isTrusted) { | |
379 | $result['path'] = $path; | |
380 | } | |
381 | if ($returnContent) { | |
382 | $result['content'] = file_get_contents($path); | |
383 | } | |
384 | return $result; | |
385 | } | |
386 | ||
387 | /** | |
388 | * @return array list of fields (indexed by name) | |
389 | */ | |
390 | function _civicrm_api3_attachment_getfields() { | |
391 | $fileFields = CRM_Core_DAO_File::fields(); | |
392 | $entityFileFields = CRM_Core_DAO_EntityFile::fields(); | |
393 | ||
394 | $spec = array(); | |
395 | $spec['id'] = $fileFields['id']; | |
396 | $spec['name'] = array( | |
397 | 'title' => 'Name (write-once)', | |
398 | 'description' => 'The logical file name (not searchable)', | |
399 | 'type' => CRM_Utils_Type::T_STRING, | |
400 | ); | |
401 | $spec['mime_type'] = $fileFields['mime_type']; | |
402 | $spec['description'] = $fileFields['description']; | |
403 | $spec['upload_date'] = $fileFields['upload_date']; | |
404 | $spec['entity_table'] = $entityFileFields['entity_table']; | |
405 | $spec['entity_table']['title'] = CRM_Utils_Array::value('title', $spec['entity_table'], 'Entity Table') . ' (write-once)'; // would be hard to securely handle changes | |
406 | $spec['entity_id'] = $entityFileFields['entity_id']; | |
407 | $spec['entity_id']['title'] = CRM_Utils_Array::value('title', $spec['entity_id'], 'Entity ID') . ' (write-once)'; // would be hard to securely handle changes | |
408 | $spec['url'] = array( | |
409 | 'title' => 'URL (read-only)', | |
410 | 'description' => 'URL for downloading the file (not searchable, expire-able)', | |
411 | 'type' => CRM_Utils_Type::T_STRING, | |
412 | ); | |
413 | $spec['path'] = array( | |
414 | 'title' => 'Path (read-only)', | |
415 | 'description' => 'Local file path (not searchable, local-only)', | |
416 | 'type' => CRM_Utils_Type::T_STRING, | |
417 | ); | |
418 | $spec['content'] = array( | |
419 | 'title' => 'Content', | |
420 | 'description' => 'File content (not searchable, not returned by default)', | |
421 | 'type' => CRM_Utils_Type::T_STRING, | |
422 | ); | |
423 | ||
424 | return $spec; | |
425 | } |