3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * "Attachment" is a pseudo-entity which represents a record in civicrm_file
30 * combined with a record in civicrm_entity_file as well as the underlying
34 * $result = civicrm_api3('Attachment', 'create', array(
35 * 'entity_table' => 'civicrm_activity',
37 * 'name' => 'README.txt',
38 * 'mime_type' => 'text/plain',
39 * 'content' => 'Please to read the README',
41 * $attachment = $result['values'][$result['id']];
42 * echo sprintf("<a href='%s'>View %s</a>", $attachment['url'], $attachment['name']);
46 * $result = civicrm_api3('Attachment', 'create', array(
47 * 'entity_table' => 'civicrm_activity',
49 * 'name' => 'README.txt',
50 * 'mime_type' => 'text/plain',
52 * 'move-file' => '/tmp/upload1a2b3c4d',
55 * $attachment = $result['values'][$result['id']];
56 * echo sprintf("<a href='%s'>View %s</a>", $attachment['url'], $attachment['name']);
60 * - File content is not returned by default. One must specify 'return => content'.
61 * - Features which deal with local file system (e.g. passing "options.move-file"
62 * or returning a "path") are only valid when executed as a local API (ie
63 * "check_permissions"==false)
65 * @package CiviCRM_APIv3
66 * @subpackage API_Attachment
67 * @copyright CiviCRM LLC (c) 2004-2014
73 * Adjust metadata for "create" action
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';
88 * Create an attachment
90 * @param array $params
92 * Array of newly created file property values.
93 * @throws API_Exception validation errors
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);
99 $fileDao = new CRM_Core_BAO_File();
100 $entityFileDao = new CRM_Core_DAO_EntityFile();
104 if (!$fileDao->find(TRUE)) {
105 throw new API_Exception("Invalid ID");
108 $entityFileDao->file_id
= $id;
109 if (!$entityFileDao->find(TRUE)) {
110 throw new API_Exception("Cannot modify orphaned file");
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'");
117 if (!$isTrusted && $moveFile) {
118 throw new API_Exception("options.move-file is only supported on secure calls");
120 if (is_string($content) && is_string($moveFile)) {
121 throw new API_Exception("'content' and 'options.move-file' are mutually exclusive");
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));
126 if ($id && $name && $name != CRM_Utils_File
::cleanFileName($fileDao->uri
)) {
127 throw new API_Exception("Cannot modify name");
130 $fileDao->copyValues($file);
132 $fileDao->uri
= CRM_Utils_File
::makeFileName($name);
136 $entityFileDao->copyValues($entityFile);
137 $entityFileDao->file_id
= $fileDao->id
;
138 $entityFileDao->save();
140 $path = $config->customFileUploadDir
. DIRECTORY_SEPARATOR
. $fileDao->uri
;
141 if (is_string($content)) {
142 file_put_contents($path, $content);
144 elseif (is_string($moveFile)) {
145 rename($moveFile, $path);
149 $fileDao->id
=> _civicrm_api3_attachment_format_result($fileDao, $entityFileDao, $returnContent, $isTrusted),
151 return civicrm_api3_create_success($result, $params, 'Attachment', 'create');
155 * Adjust metadata for "create" action
160 function _civicrm_api3_attachment_get_spec(&$spec) {
161 $spec = array_merge($spec, _civicrm_api3_attachment_getfields());
165 * @param array $params
168 * @throws API_Exception validation errors
170 function civicrm_api3_attachment_get($params) {
171 list($id, $file, $entityFile, $name, $content, $moveFile, $isTrusted, $returnContent) = _civicrm_api3_attachment_parse_params($params);
173 $dao = __civicrm_api3_attachment_find($params, $id, $file, $entityFile, $isTrusted);
175 while ($dao->fetch()) {
176 $result[$dao->id
] = _civicrm_api3_attachment_format_result($dao, $dao, $returnContent, $isTrusted);
178 return civicrm_api3_create_success($result, $params, 'Attachment', 'create');
184 function _civicrm_api3_attachment_delete_spec(&$spec) {
185 unset($spec['id']['api.required']);
186 $entityFileFields = CRM_Core_DAO_EntityFile
::fields();
187 $spec['entity_table'] = $entityFileFields['entity_table'];
188 $spec['entity_table']['title'] = CRM_Utils_Array
::value('title', $spec['entity_table'], 'Entity Table') . ' (write-once)';
189 $spec['entity_id'] = $entityFileFields['entity_id'];
190 $spec['entity_id']['title'] = CRM_Utils_Array
::value('title', $spec['entity_id'], 'Entity ID') . ' (write-once)';
194 * @param array $params
196 * @throws API_Exception
198 function civicrm_api3_attachment_delete($params) {
199 if (!empty($params['id'])) {
202 elseif (!empty($params['entity_table']) && !empty($params['entity_id'])) {
206 throw new API_Exception("Mandatory key(s) missing from params array: id or entity_table+entity_table");
209 $config = CRM_Core_Config
::singleton();
210 list($id, $file, $entityFile, $name, $content, $moveFile, $isTrusted, $returnContent) = _civicrm_api3_attachment_parse_params($params);
211 $dao = __civicrm_api3_attachment_find($params, $id, $file, $entityFile, $isTrusted);
213 $filePaths = array();
215 while ($dao->fetch()) {
216 $filePaths[] = $config->customFileUploadDir
. DIRECTORY_SEPARATOR
. $dao->uri
;
217 $fileIds[] = $dao->id
;
220 if (!empty($fileIds)) {
221 $idString = implode(',', array_filter($fileIds, 'is_numeric'));
222 CRM_Core_DAO
::executeQuery("DELETE FROM civicrm_entity_file WHERE file_id in ($idString)");
223 CRM_Core_DAO
::executeQuery("DELETE FROM civicrm_file WHERE id in ($idString)");
226 // unlink is non-transactional, so we do this as the last step -- just in case the other steps produce errors
227 if (!empty($filePaths)) {
228 foreach ($filePaths as $filePath) {
234 return civicrm_api3_create_success($result, $params, 'Attachment', 'create');
238 * @param array $params
239 * @param int|null $id the user-supplied ID of the attachment record
241 * The user-supplied vales for the file (mime_type, description, upload_date).
242 * @param array $entityFile
243 * The user-supplied values of the entity-file (entity_table, entity_id).
244 * @param bool $isTrusted
245 * @return CRM_Core_DAO
246 * @throws API_Exception
248 function __civicrm_api3_attachment_find($params, $id, $file, $entityFile, $isTrusted) {
249 foreach (array('name', 'content', 'path', 'url') as $unsupportedFilter) {
250 if (!empty($params[$unsupportedFilter])) {
251 throw new API_Exception("Get by $unsupportedFilter is not currently supported");
255 $select = CRM_Utils_SQL_Select
::from('civicrm_file cf')
256 ->join('cef', 'INNER JOIN civicrm_entity_file cef ON cf.id = cef.file_id')
268 $select->where('cf.id = #id', array('#id' => $id));
270 // recall: $file is filtered by parse_params
271 foreach ($file as $key => $value) {
272 $select->where('cf.!field = @value', array(
277 // recall: $entityFile is filtered by parse_params
278 foreach ($entityFile as $key => $value) {
279 $select->where('cef.!field = @value', array(
285 // FIXME ACLs: Add any JOIN or WHERE clauses needed to enforce access-controls for the target entity.
287 // The target entity is identified by "cef.entity_table" (aka $entityFile['entity_table']) and "cef.entity_id".
289 // As a simplification, we *require* the "get" actions to filter on a single "entity_table" which should
290 // avoid the complexity of matching ACL's against multiple entity types.
293 $dao = CRM_Core_DAO
::executeQuery($select->toSQL());
298 * @param array $params
299 * @return array (0 => int $id, 1 => array $file, 2 => array $entityFile, 3 => string $name, 4 => string $content,
300 * 5 => string $moveFile, 6 => $isTrusted, 7 => bool $returnContent)
301 * - array $file: whitelisted fields that can pass through directly to civicrm_file
302 * - array $entityFile: whitelisted fields that can pass through directly to civicrm_entity_file
303 * - string $name: the printable name
304 * - string $moveFile: the full path to a local file whose content should be loaded
305 * - bool $isTrusted: whether we trust the requester to do sketchy things (like moving files or reassigning entities)
306 * - bool $returnContent: whether we are expected to return the full content of the file
307 * @throws API_Exception validation errors
309 function _civicrm_api3_attachment_parse_params($params) {
310 $id = CRM_Utils_Array
::value('id', $params, NULL);
311 if ($id && !is_numeric($id)) {
312 throw new API_Exception("Malformed id");
316 foreach (array('mime_type', 'description', 'upload_date') as $field) {
317 if (array_key_exists($field, $params)) {
318 $file[$field] = $params[$field];
322 $entityFile = array();
323 foreach (array('entity_table', 'entity_id') as $field) {
324 if (array_key_exists($field, $params)) {
325 $entityFile[$field] = $params[$field];
330 if (array_key_exists('name', $params)) {
331 if ($params['name'] != basename($params['name']) ||
preg_match(':[/\\\\]:', $params['name'])) {
332 throw new API_Exception('Malformed name');
334 $name = $params['name'];
338 if (isset($params['content'])) {
339 $content = $params['content'];
343 if (isset($params['options']['move-file'])) {
344 $moveFile = $params['options']['move-file'];
346 elseif (isset($params['options.move-file'])) {
347 $moveFile = $params['options.move-file'];
350 $isTrusted = empty($params['check_permissions']);
352 $returns = isset($params['return']) ?
$params['return'] : array();
353 $returns = is_array($returns) ?
$returns : array($returns);
354 $returnContent = in_array('content', $returns);
356 return array($id, $file, $entityFile, $name, $content, $moveFile, $isTrusted, $returnContent);
360 * @param CRM_Core_DAO_File $fileDao
361 * Maybe "File" or "File JOIN EntityFile".
362 * @param CRM_Core_DAO_EntityFile $entityFileDao
363 * Maybe "EntityFile" or "File JOIN EntityFile".
364 * @param bool $returnContent
365 * Whether to return the full content of the file.
366 * @param bool $isTrusted
367 * Whether the current request is trusted to perform file-specific operations.
371 function _civicrm_api3_attachment_format_result($fileDao, $entityFileDao, $returnContent, $isTrusted) {
372 $config = CRM_Core_Config
::singleton();
373 $path = $config->customFileUploadDir
. DIRECTORY_SEPARATOR
. $fileDao->uri
;
376 'id' => $fileDao->id
,
377 'name' => CRM_Utils_File
::cleanFileName($fileDao->uri
),
378 'mime_type' => $fileDao->mime_type
,
379 'description' => $fileDao->description
,
380 'upload_date' => is_numeric($fileDao->upload_date
) ? CRM_Utils_Date
::mysqlToIso($fileDao->upload_date
) : $fileDao->upload_date
,
381 'entity_table' => $entityFileDao->entity_table
,
382 'entity_id' => $entityFileDao->entity_id
,
384 $result['url'] = CRM_Utils_System
::url(
385 'civicrm/file', 'reset=1&id=' . $result['id'] . '&eid=' . $result['entity_id'],
392 $result['path'] = $path;
394 if ($returnContent) {
395 $result['content'] = file_get_contents($path);
402 * list of fields (indexed by name)
404 function _civicrm_api3_attachment_getfields() {
405 $fileFields = CRM_Core_DAO_File
::fields();
406 $entityFileFields = CRM_Core_DAO_EntityFile
::fields();
409 $spec['id'] = $fileFields['id'];
410 $spec['name'] = array(
411 'title' => 'Name (write-once)',
412 'description' => 'The logical file name (not searchable)',
413 'type' => CRM_Utils_Type
::T_STRING
,
415 $spec['mime_type'] = $fileFields['mime_type'];
416 $spec['description'] = $fileFields['description'];
417 $spec['upload_date'] = $fileFields['upload_date'];
418 $spec['entity_table'] = $entityFileFields['entity_table'];
419 $spec['entity_table']['title'] = CRM_Utils_Array
::value('title', $spec['entity_table'], 'Entity Table') . ' (write-once)'; // would be hard to securely handle changes
420 $spec['entity_id'] = $entityFileFields['entity_id'];
421 $spec['entity_id']['title'] = CRM_Utils_Array
::value('title', $spec['entity_id'], 'Entity ID') . ' (write-once)'; // would be hard to securely handle changes
422 $spec['url'] = array(
423 'title' => 'URL (read-only)',
424 'description' => 'URL for downloading the file (not searchable, expire-able)',
425 'type' => CRM_Utils_Type
::T_STRING
,
427 $spec['path'] = array(
428 'title' => 'Path (read-only)',
429 'description' => 'Local file path (not searchable, local-only)',
430 'type' => CRM_Utils_Type
::T_STRING
,
432 $spec['content'] = array(
433 'title' => 'Content',
434 'description' => 'File content (not searchable, not returned by default)',
435 'type' => CRM_Utils_Type
::T_STRING
,