| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 6 | | This work is published under the GNU AGPLv3 license with some | |
| 7 | | permitted exceptions and without any warranty. For full license | |
| 8 | | and copyright information, see https://civicrm.org/licensing | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * |
| 14 | * @package CRM |
| 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 16 | */ |
| 17 | |
| 18 | /** |
| 19 | * BAO object for crm_log table |
| 20 | */ |
| 21 | class CRM_Core_BAO_File extends CRM_Core_DAO_File { |
| 22 | |
| 23 | public static $_signableFields = ['entityTable', 'entityID', 'fileID']; |
| 24 | |
| 25 | /** |
| 26 | * If there is no setting configured on the admin screens, maximum number |
| 27 | * of attachments to try to process when given a list of attachments to |
| 28 | * process. |
| 29 | */ |
| 30 | const DEFAULT_MAX_ATTACHMENTS_BACKEND = 100; |
| 31 | |
| 32 | /** |
| 33 | * Takes an associative array and creates a File object. |
| 34 | * |
| 35 | * @param array $params |
| 36 | * (reference ) an assoc array of name/value pairs. |
| 37 | * |
| 38 | * @return CRM_Core_BAO_File |
| 39 | */ |
| 40 | public static function create($params) { |
| 41 | $fileDAO = new CRM_Core_DAO_File(); |
| 42 | |
| 43 | $op = empty($params['id']) ? 'create' : 'edit'; |
| 44 | |
| 45 | CRM_Utils_Hook::pre($op, 'File', CRM_Utils_Array::value('id', $params), $params); |
| 46 | |
| 47 | $fileDAO->copyValues($params); |
| 48 | |
| 49 | if (empty($params['id']) && empty($params['created_id'])) { |
| 50 | $fileDAO->created_id = CRM_Core_Session::getLoggedInContactID(); |
| 51 | } |
| 52 | |
| 53 | $fileDAO->save(); |
| 54 | |
| 55 | CRM_Utils_Hook::post($op, 'File', $fileDAO->id, $fileDAO); |
| 56 | |
| 57 | return $fileDAO; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @param int $fileID |
| 62 | * @param int $entityID |
| 63 | * |
| 64 | * @return array |
| 65 | */ |
| 66 | public static function path($fileID, $entityID) { |
| 67 | $entityFileDAO = new CRM_Core_DAO_EntityFile(); |
| 68 | $entityFileDAO->entity_id = $entityID; |
| 69 | $entityFileDAO->file_id = $fileID; |
| 70 | |
| 71 | if ($entityFileDAO->find(TRUE)) { |
| 72 | $fileDAO = new CRM_Core_DAO_File(); |
| 73 | $fileDAO->id = $fileID; |
| 74 | if ($fileDAO->find(TRUE)) { |
| 75 | $config = CRM_Core_Config::singleton(); |
| 76 | $path = $config->customFileUploadDir . $fileDAO->uri; |
| 77 | |
| 78 | if (file_exists($path) && is_readable($path)) { |
| 79 | return [$path, $fileDAO->mime_type]; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | return [NULL, NULL]; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @param string $data |
| 89 | * @param int $fileTypeID |
| 90 | * @param string $entityTable |
| 91 | * @param int $entityID |
| 92 | * @param string|false $entitySubtype |
| 93 | * @param bool $overwrite |
| 94 | * @param null|array $fileParams |
| 95 | * @param string $uploadName |
| 96 | * @param string $mimeType |
| 97 | * |
| 98 | * @throws Exception |
| 99 | */ |
| 100 | public static function filePostProcess( |
| 101 | $data, |
| 102 | $fileTypeID, |
| 103 | $entityTable, |
| 104 | $entityID, |
| 105 | $entitySubtype = FALSE, |
| 106 | $overwrite = TRUE, |
| 107 | $fileParams = NULL, |
| 108 | $uploadName = 'uploadFile', |
| 109 | $mimeType = NULL |
| 110 | ) { |
| 111 | if (!$mimeType) { |
| 112 | CRM_Core_Error::statusBounce(ts('Mime Type is now a required parameter for file upload')); |
| 113 | } |
| 114 | |
| 115 | $config = CRM_Core_Config::singleton(); |
| 116 | |
| 117 | $path = explode('/', $data); |
| 118 | $filename = $path[count($path) - 1]; |
| 119 | |
| 120 | // rename this file to go into the secure directory |
| 121 | if ($entitySubtype) { |
| 122 | $directoryName = $config->customFileUploadDir . $entitySubtype . DIRECTORY_SEPARATOR . $entityID; |
| 123 | } |
| 124 | else { |
| 125 | $directoryName = $config->customFileUploadDir; |
| 126 | } |
| 127 | |
| 128 | CRM_Utils_File::createDir($directoryName); |
| 129 | |
| 130 | if (!rename($data, $directoryName . DIRECTORY_SEPARATOR . $filename)) { |
| 131 | CRM_Core_Error::statusBounce(ts('Could not move custom file to custom upload directory')); |
| 132 | } |
| 133 | |
| 134 | // to get id's |
| 135 | if ($overwrite && $fileTypeID) { |
| 136 | list($sql, $params) = self::sql($entityTable, $entityID, $fileTypeID); |
| 137 | } |
| 138 | else { |
| 139 | list($sql, $params) = self::sql($entityTable, $entityID, 0); |
| 140 | } |
| 141 | |
| 142 | $dao = CRM_Core_DAO::executeQuery($sql, $params); |
| 143 | $dao->fetch(); |
| 144 | |
| 145 | $fileDAO = new CRM_Core_DAO_File(); |
| 146 | $op = 'create'; |
| 147 | if (isset($dao->cfID) && $dao->cfID) { |
| 148 | $op = 'edit'; |
| 149 | $fileDAO->id = $dao->cfID; |
| 150 | unlink($directoryName . DIRECTORY_SEPARATOR . $dao->uri); |
| 151 | } |
| 152 | |
| 153 | if (!empty($fileParams)) { |
| 154 | $fileDAO->copyValues($fileParams); |
| 155 | } |
| 156 | |
| 157 | $fileDAO->uri = $filename; |
| 158 | $fileDAO->mime_type = $mimeType; |
| 159 | $fileDAO->file_type_id = $fileTypeID; |
| 160 | $fileDAO->upload_date = date('YmdHis'); |
| 161 | $fileDAO->save(); |
| 162 | |
| 163 | // need to add/update civicrm_entity_file |
| 164 | $entityFileDAO = new CRM_Core_DAO_EntityFile(); |
| 165 | if (isset($dao->cefID) && $dao->cefID) { |
| 166 | $entityFileDAO->id = $dao->cefID; |
| 167 | } |
| 168 | $entityFileDAO->entity_table = $entityTable; |
| 169 | $entityFileDAO->entity_id = $entityID; |
| 170 | $entityFileDAO->file_id = $fileDAO->id; |
| 171 | $entityFileDAO->save(); |
| 172 | |
| 173 | //save static tags |
| 174 | if (!empty($fileParams['tag'])) { |
| 175 | CRM_Core_BAO_EntityTag::create($fileParams['tag'], 'civicrm_file', $entityFileDAO->id); |
| 176 | } |
| 177 | |
| 178 | //save free tags |
| 179 | if (isset($fileParams['attachment_taglist']) && !empty($fileParams['attachment_taglist'])) { |
| 180 | CRM_Core_Form_Tag::postProcess($fileParams['attachment_taglist'], $entityFileDAO->id, 'civicrm_file'); |
| 181 | } |
| 182 | |
| 183 | // lets call the post hook here so attachments code can do the right stuff |
| 184 | CRM_Utils_Hook::post($op, 'File', $fileDAO->id, $fileDAO); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * A static function wrapper that deletes the various objects. |
| 189 | * |
| 190 | * Objects are those hat are connected to a file object (i.e. file, entityFile and customValue. |
| 191 | * |
| 192 | * @param int $fileID |
| 193 | * @param int $entityID |
| 194 | * @param int $fieldID |
| 195 | * |
| 196 | * @throws \Exception |
| 197 | */ |
| 198 | public static function deleteFileReferences($fileID, $entityID, $fieldID) { |
| 199 | $fileDAO = new CRM_Core_DAO_File(); |
| 200 | $fileDAO->id = $fileID; |
| 201 | if (!$fileDAO->find(TRUE)) { |
| 202 | throw new CRM_Core_Exception(ts('File not found')); |
| 203 | } |
| 204 | |
| 205 | // lets call a pre hook before the delete, so attachments hooks can get the info before things |
| 206 | // disappear |
| 207 | CRM_Utils_Hook::pre('delete', 'File', $fileID, $fileDAO); |
| 208 | |
| 209 | // get the table and column name |
| 210 | list($tableName, $columnName, $groupID) = CRM_Core_BAO_CustomField::getTableColumnGroup($fieldID); |
| 211 | |
| 212 | $entityFileDAO = new CRM_Core_DAO_EntityFile(); |
| 213 | $entityFileDAO->file_id = $fileID; |
| 214 | $entityFileDAO->entity_id = $entityID; |
| 215 | $entityFileDAO->entity_table = $tableName; |
| 216 | |
| 217 | if (!$entityFileDAO->find(TRUE)) { |
| 218 | throw new CRM_Core_Exception(sprintf('No record found for given file ID - %d and entity ID - %d', $fileID, $entityID)); |
| 219 | } |
| 220 | |
| 221 | $entityFileDAO->delete(); |
| 222 | $fileDAO->delete(); |
| 223 | |
| 224 | // also set the value to null of the table and column |
| 225 | $query = "UPDATE $tableName SET $columnName = null WHERE $columnName = %1"; |
| 226 | $params = [1 => [$fileID, 'Integer']]; |
| 227 | CRM_Core_DAO::executeQuery($query, $params); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * The $useWhere is used so that the signature matches the parent class |
| 232 | * |
| 233 | * public function delete($useWhere = FALSE) { |
| 234 | * list($fileID, $entityID, $fieldID) = func_get_args(); |
| 235 | * |
| 236 | * self::deleteFileReferences($fileID, $entityID, $fieldID); |
| 237 | * } */ |
| 238 | |
| 239 | /** |
| 240 | * Delete all the files and associated object associated with this combination. |
| 241 | * |
| 242 | * @param string $entityTable |
| 243 | * @param int $entityID |
| 244 | * @param int $fileTypeID |
| 245 | * @param int $fileID |
| 246 | * |
| 247 | * @return bool |
| 248 | * Was file deleted? |
| 249 | */ |
| 250 | public static function deleteEntityFile($entityTable, $entityID, $fileTypeID = NULL, $fileID = NULL) { |
| 251 | $isDeleted = FALSE; |
| 252 | if (empty($entityTable) || empty($entityID)) { |
| 253 | return $isDeleted; |
| 254 | } |
| 255 | |
| 256 | $config = CRM_Core_Config::singleton(); |
| 257 | |
| 258 | list($sql, $params) = self::sql($entityTable, $entityID, $fileTypeID, $fileID); |
| 259 | $dao = CRM_Core_DAO::executeQuery($sql, $params); |
| 260 | |
| 261 | $cfIDs = []; |
| 262 | $cefIDs = []; |
| 263 | while ($dao->fetch()) { |
| 264 | $cfIDs[$dao->cfID] = $dao->uri; |
| 265 | $cefIDs[] = $dao->cefID; |
| 266 | } |
| 267 | |
| 268 | // Delete tags from entity tag table. |
| 269 | if (!empty($cfIDs)) { |
| 270 | $deleteFiles = []; |
| 271 | foreach ($cfIDs as $fId => $fUri) { |
| 272 | $tagParams = [ |
| 273 | 'entity_table' => 'civicrm_file', |
| 274 | 'entity_id' => $fId, |
| 275 | ]; |
| 276 | CRM_Core_BAO_EntityTag::del($tagParams); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | // Delete entries from entity file table. |
| 281 | if (!empty($cefIDs)) { |
| 282 | $cefIDs = implode(',', $cefIDs); |
| 283 | $sql = "DELETE FROM civicrm_entity_file where id IN ( $cefIDs )"; |
| 284 | CRM_Core_DAO::executeQuery($sql); |
| 285 | $isDeleted = TRUE; |
| 286 | } |
| 287 | |
| 288 | if (!empty($cfIDs)) { |
| 289 | $deleteFiles = []; |
| 290 | foreach ($cfIDs as $fId => $fUri) { |
| 291 | // Delete file only if there are no longer any entities using this file. |
| 292 | if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_EntityFile', $fId, 'id', 'file_id')) { |
| 293 | unlink($config->customFileUploadDir . DIRECTORY_SEPARATOR . $fUri); |
| 294 | $deleteFiles[$fId] = $fId; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | // Delete entries from file table. |
| 299 | if (!empty($deleteFiles)) { |
| 300 | $deleteFiles = implode(',', $deleteFiles); |
| 301 | $sql = "DELETE FROM civicrm_file where id IN ( $deleteFiles )"; |
| 302 | CRM_Core_DAO::executeQuery($sql); |
| 303 | } |
| 304 | $isDeleted = TRUE; |
| 305 | } |
| 306 | return $isDeleted; |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Get all the files and associated object associated with this combination. |
| 311 | * |
| 312 | * @param string $entityTable |
| 313 | * @param int $entityID |
| 314 | * @param bool $addDeleteArgs |
| 315 | * |
| 316 | * @return array|null |
| 317 | */ |
| 318 | public static function getEntityFile($entityTable, $entityID, $addDeleteArgs = FALSE) { |
| 319 | if (empty($entityTable) || !$entityID) { |
| 320 | $results = NULL; |
| 321 | return $results; |
| 322 | } |
| 323 | |
| 324 | $config = CRM_Core_Config::singleton(); |
| 325 | |
| 326 | list($sql, $params) = self::sql($entityTable, $entityID, NULL); |
| 327 | $dao = CRM_Core_DAO::executeQuery($sql, $params); |
| 328 | $results = []; |
| 329 | while ($dao->fetch()) { |
| 330 | $fileHash = self::generateFileHash($dao->entity_id, $dao->cfID); |
| 331 | $result['fileID'] = $dao->cfID; |
| 332 | $result['entityID'] = $dao->cefID; |
| 333 | $result['mime_type'] = $dao->mime_type; |
| 334 | $result['fileName'] = $dao->uri; |
| 335 | $result['description'] = $dao->description; |
| 336 | $result['cleanName'] = CRM_Utils_File::cleanFileName($dao->uri); |
| 337 | $result['fullPath'] = $config->customFileUploadDir . DIRECTORY_SEPARATOR . $dao->uri; |
| 338 | $result['url'] = CRM_Utils_System::url('civicrm/file', "reset=1&id={$dao->cfID}&eid={$dao->entity_id}&fcs={$fileHash}"); |
| 339 | $result['href'] = "<a href=\"{$result['url']}\">{$result['cleanName']}</a>"; |
| 340 | $result['tag'] = CRM_Core_BAO_EntityTag::getTag($dao->cfID, 'civicrm_file'); |
| 341 | $result['icon'] = CRM_Utils_File::getIconFromMimeType($dao->mime_type); |
| 342 | if ($addDeleteArgs) { |
| 343 | $result['deleteURLArgs'] = self::deleteURLArgs($dao->entity_table, $dao->entity_id, $dao->cfID); |
| 344 | } |
| 345 | $results[$dao->cfID] = $result; |
| 346 | } |
| 347 | |
| 348 | //fix tag names |
| 349 | $tags = CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', ['onlyActive' => FALSE]); |
| 350 | |
| 351 | foreach ($results as &$values) { |
| 352 | if (!empty($values['tag'])) { |
| 353 | $tagNames = []; |
| 354 | foreach ($values['tag'] as $tid) { |
| 355 | $tagNames[] = $tags[$tid]; |
| 356 | } |
| 357 | $values['tag'] = implode(', ', $tagNames); |
| 358 | } |
| 359 | else { |
| 360 | $values['tag'] = ''; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | return $results; |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * @param string $entityTable |
| 369 | * Table-name or "*" (to reference files directly by file-id). |
| 370 | * @param int $entityID |
| 371 | * @param int $fileTypeID |
| 372 | * @param int $fileID |
| 373 | * |
| 374 | * @return array |
| 375 | */ |
| 376 | public static function sql($entityTable, $entityID, $fileTypeID = NULL, $fileID = NULL) { |
| 377 | if ($entityTable == '*') { |
| 378 | // $entityID is the ID of a specific file |
| 379 | $sql = " |
| 380 | SELECT CF.id as cfID, |
| 381 | CF.uri as uri, |
| 382 | CF.mime_type as mime_type, |
| 383 | CF.description as description, |
| 384 | CEF.id as cefID, |
| 385 | CEF.entity_table as entity_table, |
| 386 | CEF.entity_id as entity_id |
| 387 | FROM civicrm_file AS CF |
| 388 | LEFT JOIN civicrm_entity_file AS CEF ON ( CEF.file_id = CF.id ) |
| 389 | WHERE CF.id = %2"; |
| 390 | |
| 391 | } |
| 392 | else { |
| 393 | $sql = " |
| 394 | SELECT CF.id as cfID, |
| 395 | CF.uri as uri, |
| 396 | CF.mime_type as mime_type, |
| 397 | CF.description as description, |
| 398 | CEF.id as cefID, |
| 399 | CEF.entity_table as entity_table, |
| 400 | CEF.entity_id as entity_id |
| 401 | FROM civicrm_file AS CF |
| 402 | LEFT JOIN civicrm_entity_file AS CEF ON ( CEF.file_id = CF.id ) |
| 403 | WHERE CEF.entity_table = %1 |
| 404 | AND CEF.entity_id = %2"; |
| 405 | } |
| 406 | |
| 407 | $params = [ |
| 408 | 1 => [$entityTable, 'String'], |
| 409 | 2 => [$entityID, 'Integer'], |
| 410 | ]; |
| 411 | |
| 412 | if ($fileTypeID !== NULL) { |
| 413 | $sql .= " AND CF.file_type_id = %3"; |
| 414 | $params[3] = [$fileTypeID, 'Integer']; |
| 415 | } |
| 416 | |
| 417 | if ($fileID !== NULL) { |
| 418 | $sql .= " AND CF.id = %4"; |
| 419 | $params[4] = [$fileID, 'Integer']; |
| 420 | } |
| 421 | |
| 422 | return [$sql, $params]; |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * @param CRM_Core_Form $form |
| 427 | * @param string $entityTable |
| 428 | * @param int $entityID |
| 429 | * @param null $numAttachments |
| 430 | * @param bool $ajaxDelete |
| 431 | * |
| 432 | * @throws \CRM_Core_Exception |
| 433 | */ |
| 434 | public static function buildAttachment(&$form, $entityTable, $entityID = NULL, $numAttachments = NULL, $ajaxDelete = FALSE) { |
| 435 | |
| 436 | if (!$numAttachments) { |
| 437 | $numAttachments = Civi::settings()->get('max_attachments'); |
| 438 | } |
| 439 | // Assign maxAttachments count to template for help message |
| 440 | $form->assign('maxAttachments', $numAttachments); |
| 441 | |
| 442 | $config = CRM_Core_Config::singleton(); |
| 443 | // set default max file size as 2MB |
| 444 | $maxFileSize = $config->maxFileSize ? $config->maxFileSize : 2; |
| 445 | |
| 446 | $currentAttachmentInfo = self::getEntityFile($entityTable, $entityID, TRUE); |
| 447 | $totalAttachments = $currentAttachmentInfo ? count($currentAttachmentInfo) : 0; |
| 448 | if ($currentAttachmentInfo) { |
| 449 | $form->add('checkbox', 'is_delete_attachment', ts('Delete All Attachment(s)')); |
| 450 | } |
| 451 | $form->assign('currentAttachmentInfo', $currentAttachmentInfo); |
| 452 | |
| 453 | if ($totalAttachments) { |
| 454 | if ($totalAttachments >= $numAttachments) { |
| 455 | $numAttachments = 0; |
| 456 | } |
| 457 | else { |
| 458 | $numAttachments -= $totalAttachments; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | $form->assign('numAttachments', $numAttachments); |
| 463 | |
| 464 | CRM_Core_BAO_Tag::getTags('civicrm_file', $tags, NULL, |
| 465 | ' ', TRUE); |
| 466 | |
| 467 | // get tagset info |
| 468 | $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_file'); |
| 469 | |
| 470 | // add attachments |
| 471 | for ($i = 1; $i <= $numAttachments; $i++) { |
| 472 | $form->addElement('file', "attachFile_$i", ts('Attach File'), 'size=30 maxlength=221'); |
| 473 | $form->addUploadElement("attachFile_$i"); |
| 474 | $form->setMaxFileSize($maxFileSize * 1024 * 1024); |
| 475 | $form->addRule("attachFile_$i", |
| 476 | ts('File size should be less than %1 MByte(s)', |
| 477 | [1 => $maxFileSize] |
| 478 | ), |
| 479 | 'maxfilesize', |
| 480 | $maxFileSize * 1024 * 1024 |
| 481 | ); |
| 482 | $form->addElement('text', "attachDesc_$i", NULL, [ |
| 483 | 'size' => 40, |
| 484 | 'maxlength' => 255, |
| 485 | 'placeholder' => ts('Description'), |
| 486 | ]); |
| 487 | |
| 488 | $tagField = "tag_$i"; |
| 489 | if (!empty($tags)) { |
| 490 | $form->add('select', $tagField, ts('Tags'), $tags, FALSE, |
| 491 | [ |
| 492 | 'id' => "tags_$i", |
| 493 | 'multiple' => 'multiple', |
| 494 | 'class' => 'huge crm-select2', |
| 495 | 'placeholder' => ts('- none -'), |
| 496 | ] |
| 497 | ); |
| 498 | } |
| 499 | else { |
| 500 | $form->addOptionalQuickFormElement($tagField); |
| 501 | } |
| 502 | CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_file', NULL, FALSE, TRUE, "file_taglist_$i"); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * Return a HTML string, separated by $separator, |
| 508 | * where each item is an anchor link to the file, |
| 509 | * with the filename as the link text. |
| 510 | * |
| 511 | * @param string $entityTable |
| 512 | * The entityTable to which the file is attached. |
| 513 | * @param int $entityID |
| 514 | * The id of the object in the above entityTable. |
| 515 | * @param string $separator |
| 516 | * The string separator where to implode the urls. |
| 517 | * |
| 518 | * @return string|null |
| 519 | * HTML list of attachment links, or null if no attachments |
| 520 | */ |
| 521 | public static function attachmentInfo($entityTable, $entityID, $separator = '<br />') { |
| 522 | if (!$entityID) { |
| 523 | return NULL; |
| 524 | } |
| 525 | |
| 526 | $currentAttachments = self::getEntityFile($entityTable, $entityID); |
| 527 | if (!empty($currentAttachments)) { |
| 528 | $currentAttachmentURL = []; |
| 529 | foreach ($currentAttachments as $fileID => $attach) { |
| 530 | $currentAttachmentURL[] = $attach['href']; |
| 531 | } |
| 532 | return implode($separator, $currentAttachmentURL); |
| 533 | } |
| 534 | return NULL; |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * @param $formValues |
| 539 | * @param array $params |
| 540 | * @param $entityTable |
| 541 | * @param int $entityID |
| 542 | */ |
| 543 | public static function formatAttachment( |
| 544 | &$formValues, |
| 545 | &$params, |
| 546 | $entityTable, |
| 547 | $entityID = NULL |
| 548 | ) { |
| 549 | |
| 550 | // delete current attachments if applicable |
| 551 | if ($entityID && !empty($formValues['is_delete_attachment'])) { |
| 552 | CRM_Core_BAO_File::deleteEntityFile($entityTable, $entityID); |
| 553 | } |
| 554 | |
| 555 | $numAttachments = Civi::settings()->get('max_attachments'); |
| 556 | |
| 557 | // setup all attachments |
| 558 | for ($i = 1; $i <= $numAttachments; $i++) { |
| 559 | $attachName = "attachFile_$i"; |
| 560 | $attachDesc = "attachDesc_$i"; |
| 561 | $attachTags = "tag_$i"; |
| 562 | $attachFreeTags = "file_taglist_$i"; |
| 563 | if (isset($formValues[$attachName]) && !empty($formValues[$attachName])) { |
| 564 | // add static tags if selects |
| 565 | $tagParams = []; |
| 566 | if (!empty($formValues[$attachTags])) { |
| 567 | foreach ($formValues[$attachTags] as $tag) { |
| 568 | $tagParams[$tag] = 1; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | // we dont care if the file is empty or not |
| 573 | // CRM-7448 |
| 574 | $extraParams = [ |
| 575 | 'description' => $formValues[$attachDesc], |
| 576 | 'tag' => $tagParams, |
| 577 | 'attachment_taglist' => $formValues[$attachFreeTags] ?? [], |
| 578 | ]; |
| 579 | |
| 580 | CRM_Utils_File::formatFile($formValues, $attachName, $extraParams); |
| 581 | |
| 582 | // set the formatted attachment attributes to $params, later used |
| 583 | // to send mail with desired attachments |
| 584 | if (!empty($formValues[$attachName])) { |
| 585 | $params[$attachName] = $formValues[$attachName]; |
| 586 | } |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | /** |
| 592 | * @param array $params |
| 593 | * @param $entityTable |
| 594 | * @param int $entityID |
| 595 | */ |
| 596 | public static function processAttachment(&$params, $entityTable, $entityID) { |
| 597 | $numAttachments = Civi::settings()->get('max_attachments_backend') ?? self::DEFAULT_MAX_ATTACHMENTS_BACKEND; |
| 598 | |
| 599 | for ($i = 1; $i <= $numAttachments; $i++) { |
| 600 | if (isset($params["attachFile_$i"])) { |
| 601 | /** |
| 602 | * Moved the second condition into its own if block to avoid changing |
| 603 | * how it works if there happens to be an entry that is not an array, |
| 604 | * since we now might exit loop early via newly added break below. |
| 605 | */ |
| 606 | if (is_array($params["attachFile_$i"])) { |
| 607 | self::filePostProcess( |
| 608 | $params["attachFile_$i"]['location'], |
| 609 | NULL, |
| 610 | $entityTable, |
| 611 | $entityID, |
| 612 | NULL, |
| 613 | TRUE, |
| 614 | $params["attachFile_$i"], |
| 615 | "attachFile_$i", |
| 616 | $params["attachFile_$i"]['type'] |
| 617 | ); |
| 618 | } |
| 619 | } |
| 620 | else { |
| 621 | /** |
| 622 | * No point looping 100 times if there aren't any more. |
| 623 | * This assumes the array is continuous and doesn't skip array keys, |
| 624 | * but (a) where would it be doing that, and (b) it would have caused |
| 625 | * problems before anyway if there were skipped keys. |
| 626 | */ |
| 627 | break; |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | /** |
| 633 | * @return array |
| 634 | */ |
| 635 | public static function uploadNames() { |
| 636 | $numAttachments = Civi::settings()->get('max_attachments'); |
| 637 | |
| 638 | $names = []; |
| 639 | for ($i = 1; $i <= $numAttachments; $i++) { |
| 640 | $names[] = "attachFile_{$i}"; |
| 641 | } |
| 642 | $names[] = 'uploadFile'; |
| 643 | return $names; |
| 644 | } |
| 645 | |
| 646 | /** |
| 647 | * copy/attach an existing file to a different entity |
| 648 | * table and id. |
| 649 | * |
| 650 | * @param $oldEntityTable |
| 651 | * @param int $oldEntityId |
| 652 | * @param $newEntityTable |
| 653 | * @param int $newEntityId |
| 654 | */ |
| 655 | public static function copyEntityFile($oldEntityTable, $oldEntityId, $newEntityTable, $newEntityId) { |
| 656 | $oldEntityFile = new CRM_Core_DAO_EntityFile(); |
| 657 | $oldEntityFile->entity_id = $oldEntityId; |
| 658 | $oldEntityFile->entity_table = $oldEntityTable; |
| 659 | $oldEntityFile->find(); |
| 660 | |
| 661 | while ($oldEntityFile->fetch()) { |
| 662 | $newEntityFile = new CRM_Core_DAO_EntityFile(); |
| 663 | $newEntityFile->entity_id = $newEntityId; |
| 664 | $newEntityFile->entity_table = $newEntityTable; |
| 665 | $newEntityFile->file_id = $oldEntityFile->file_id; |
| 666 | $newEntityFile->save(); |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | /** |
| 671 | * @param $entityTable |
| 672 | * @param int $entityID |
| 673 | * @param int $fileID |
| 674 | * |
| 675 | * @return string |
| 676 | */ |
| 677 | public static function deleteURLArgs($entityTable, $entityID, $fileID) { |
| 678 | $params['entityTable'] = $entityTable; |
| 679 | $params['entityID'] = $entityID; |
| 680 | $params['fileID'] = $fileID; |
| 681 | |
| 682 | $signer = new CRM_Utils_Signer(CRM_Core_Key::privateKey(), self::$_signableFields); |
| 683 | $params['_sgn'] = $signer->sign($params); |
| 684 | return CRM_Utils_System::makeQueryString($params); |
| 685 | } |
| 686 | |
| 687 | /** |
| 688 | * Delete a file attachment from an entity table / entity ID |
| 689 | * @throws CRM_Core_Exception |
| 690 | */ |
| 691 | public static function deleteAttachment() { |
| 692 | $params = []; |
| 693 | $params['entityTable'] = CRM_Utils_Request::retrieve('entityTable', 'String', CRM_Core_DAO::$_nullObject, TRUE); |
| 694 | $params['entityID'] = CRM_Utils_Request::retrieve('entityID', 'Positive', CRM_Core_DAO::$_nullObject, TRUE); |
| 695 | $params['fileID'] = CRM_Utils_Request::retrieve('fileID', 'Positive', CRM_Core_DAO::$_nullObject, TRUE); |
| 696 | |
| 697 | $signature = CRM_Utils_Request::retrieve('_sgn', 'String', CRM_Core_DAO::$_nullObject, TRUE); |
| 698 | |
| 699 | $signer = new CRM_Utils_Signer(CRM_Core_Key::privateKey(), self::$_signableFields); |
| 700 | if (!$signer->validate($signature, $params)) { |
| 701 | throw new CRM_Core_Exception('Request signature is invalid'); |
| 702 | } |
| 703 | |
| 704 | self::deleteEntityFile($params['entityTable'], $params['entityID'], NULL, $params['fileID']); |
| 705 | } |
| 706 | |
| 707 | /** |
| 708 | * Display paper icon for a file attachment -- CRM-13624 |
| 709 | * |
| 710 | * @param string $entityTable |
| 711 | * The entityTable to which the file is attached. eg "civicrm_contact", "civicrm_note", "civicrm_activity". |
| 712 | * If you have the ID of a specific row in civicrm_file, use $entityTable='*' |
| 713 | * @param int $entityID |
| 714 | * The id of the object in the above entityTable. |
| 715 | * |
| 716 | * @return array|NULL |
| 717 | * list of HTML snippets; one HTML snippet for each attachment. If none found, then NULL |
| 718 | * |
| 719 | */ |
| 720 | public static function paperIconAttachment($entityTable, $entityID) { |
| 721 | if (empty($entityTable) || !$entityID) { |
| 722 | $results = NULL; |
| 723 | return $results; |
| 724 | } |
| 725 | $currentAttachmentInfo = self::getEntityFile($entityTable, $entityID); |
| 726 | foreach ($currentAttachmentInfo as $fileKey => $fileValue) { |
| 727 | $fileID = $fileValue['fileID']; |
| 728 | if ($fileID) { |
| 729 | $fileType = $fileValue['mime_type']; |
| 730 | $url = $fileValue['url']; |
| 731 | $title = $fileValue['cleanName']; |
| 732 | if ($fileType == 'image/jpeg' || |
| 733 | $fileType == 'image/pjpeg' || |
| 734 | $fileType == 'image/gif' || |
| 735 | $fileType == 'image/x-png' || |
| 736 | $fileType == 'image/png' |
| 737 | ) { |
| 738 | $file_url[$fileID] = <<<HEREDOC |
| 739 | <a href="$url" class="crm-image-popup" title="$title"> |
| 740 | <i class="crm-i fa-file-image-o" aria-hidden="true"></i> |
| 741 | </a> |
| 742 | HEREDOC; |
| 743 | } |
| 744 | // for non image files |
| 745 | else { |
| 746 | $file_url[$fileID] = <<<HEREDOC |
| 747 | <a href="$url" title="$title"> |
| 748 | <i class="crm-i fa-paperclip" aria-hidden="true"></i> |
| 749 | </a> |
| 750 | HEREDOC; |
| 751 | } |
| 752 | } |
| 753 | } |
| 754 | if (empty($file_url)) { |
| 755 | $results = NULL; |
| 756 | } |
| 757 | else { |
| 758 | $results = $file_url; |
| 759 | } |
| 760 | return $results; |
| 761 | } |
| 762 | |
| 763 | /** |
| 764 | * Get a reference to the file-search service (if one is available). |
| 765 | * |
| 766 | * @return CRM_Core_FileSearchInterface|NULL |
| 767 | */ |
| 768 | public static function getSearchService() { |
| 769 | $fileSearches = []; |
| 770 | CRM_Utils_Hook::fileSearches($fileSearches); |
| 771 | |
| 772 | // use the first available search |
| 773 | foreach ($fileSearches as $fileSearch) { |
| 774 | /** @var $fileSearch CRM_Core_FileSearchInterface */ |
| 775 | return $fileSearch; |
| 776 | } |
| 777 | return NULL; |
| 778 | } |
| 779 | |
| 780 | /** |
| 781 | * Generates an access-token for downloading a specific file. |
| 782 | * |
| 783 | * @param int $entityId entity id the file is attached to |
| 784 | * @param int $fileId file ID |
| 785 | * @param int $genTs |
| 786 | * @param int $life |
| 787 | * @return string |
| 788 | */ |
| 789 | public static function generateFileHash($entityId = NULL, $fileId = NULL, $genTs = NULL, $life = NULL) { |
| 790 | // Use multiple (but stable) inputs for hash information. |
| 791 | $siteKey = CRM_Utils_Constant::value('CIVICRM_SITE_KEY'); |
| 792 | if (!$siteKey) { |
| 793 | throw new \CRM_Core_Exception("Cannot generate file access token. Please set CIVICRM_SITE_KEY."); |
| 794 | } |
| 795 | |
| 796 | if (!$genTs) { |
| 797 | $genTs = time(); |
| 798 | } |
| 799 | if (!$life) { |
| 800 | $days = Civi::settings()->get('checksum_timeout'); |
| 801 | $life = 24 * $days; |
| 802 | } |
| 803 | // Trim 8 chars off the string, make it slightly easier to find |
| 804 | // but reveals less information from the hash. |
| 805 | $cs = hash_hmac('sha256', "entity={$entityId}&file={$fileId}&life={$life}", $siteKey); |
| 806 | return "{$cs}_{$genTs}_{$life}"; |
| 807 | } |
| 808 | |
| 809 | /** |
| 810 | * Validate a file access token. |
| 811 | * |
| 812 | * @param string $hash |
| 813 | * @param int $entityId Entity Id the file is attached to |
| 814 | * @param int $fileId File Id |
| 815 | * @return bool |
| 816 | */ |
| 817 | public static function validateFileHash($hash, $entityId, $fileId) { |
| 818 | $input = CRM_Utils_System::explode('_', $hash, 3); |
| 819 | $inputTs = $input[1] ?? NULL; |
| 820 | $inputLF = $input[2] ?? NULL; |
| 821 | $testHash = CRM_Core_BAO_File::generateFileHash($entityId, $fileId, $inputTs, $inputLF); |
| 822 | if (hash_equals($testHash, $hash)) { |
| 823 | $now = time(); |
| 824 | if ($inputTs + ($inputLF * 60 * 60) >= $now) { |
| 825 | return TRUE; |
| 826 | } |
| 827 | else { |
| 828 | return FALSE; |
| 829 | } |
| 830 | } |
| 831 | return FALSE; |
| 832 | } |
| 833 | |
| 834 | } |