Merge pull request #15306 from bhahumanists/bhahumanists-mailingID
[civicrm-core.git] / CRM / Core / Page / File.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 * $Id$
17 *
18 */
19class CRM_Core_Page_File extends CRM_Core_Page {
6a488035 20
8246bca4 21 /**
22 * Run page.
23 */
00be9182 24 public function run() {
6a488035 25 $action = CRM_Utils_Request::retrieve('action', 'String', $this);
3159f5dc 26 $download = CRM_Utils_Request::retrieve('download', 'Integer', $this, FALSE, 1);
10b710f9 27 $disposition = $download == 0 ? 'inline' : 'download';
6a488035 28
518fa0ee
SL
29 // Entity ID (e.g. Contact ID)
30 $entityId = CRM_Utils_Request::retrieve('eid', 'Positive', $this, FALSE);
31 // Field ID
32 $fieldId = CRM_Utils_Request::retrieve('fid', 'Positive', $this, FALSE);
33 // File ID
34 $fileId = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE);
a0013a8d
SL
35 $fileName = CRM_Utils_Request::retrieve('filename', 'String', $this, FALSE);
36 if (empty($fileName) && (empty($entityId) || empty($fileId))) {
6fcdf748 37 CRM_Core_Error::statusBounce("Cannot access file: Must pass either \"Filename\" or the combination of \"Entity ID\" + \"File ID\"");
a0013a8d 38 }
acac898b
TO
39
40 if (empty($fileName)) {
41 $hash = CRM_Utils_Request::retrieve('fcs', 'Alphanumeric', $this);
42 if (!CRM_Core_BAO_File::validateFileHash($hash, $entityId, $fileId)) {
43 CRM_Core_Error::statusBounce('URL for file is not valid');
44 }
45
46 list($path, $mimeType) = CRM_Core_BAO_File::path($fileId, $entityId);
ff9aeadb 47 }
acac898b 48 else {
a7762e79 49 if (!CRM_Utils_File::isValidFileName($fileName)) {
e536c4d4
TO
50 throw new CRM_Core_Exception("Malformed filename");
51 }
a0013a8d
SL
52 $mimeType = '';
53 $path = CRM_Core_Config::singleton()->customFileUploadDir . $fileName;
54 }
f3726153 55
6a488035
TO
56 if (!$path) {
57 CRM_Core_Error::statusBounce('Could not retrieve the file');
58 }
6cb3fe2e
SL
59
60 if (empty($mimeType)) {
ce5b9953 61 $passedInMimeType = self::convertBadMimeAliasTypes(CRM_Utils_Request::retrieveValue('mime-type', 'String', $mimeType, FALSE));
6cb3fe2e
SL
62 if (!in_array($passedInMimeType, explode(',', Civi::settings()->get('requestableMimeTypes')))) {
63 throw new CRM_Core_Exception("Supplied mime-type is not accepted");
430858da 64 }
6cb3fe2e
SL
65 $extension = CRM_Utils_File::getExtensionFromPath($path);
66 $candidateExtensions = CRM_Utils_File::getAcceptableExtensionsForMimeType($passedInMimeType);
9a0e7039 67 if (!in_array(strtolower($extension), array_map('strtolower', $candidateExtensions))) {
6cb3fe2e 68 throw new CRM_Core_Exception("Supplied mime-type does not match file extension");
430858da 69 }
6cb3fe2e 70 // Now that we have validated mime-type supplied as much as possible lets now set the MimeType variable/
430858da 71 $mimeType = $passedInMimeType;
2d20f3a9
SL
72 }
73
6a488035
TO
74 $buffer = file_get_contents($path);
75 if (!$buffer) {
76 CRM_Core_Error::statusBounce('The file is either empty or you do not have permission to retrieve the file');
77 }
78
79 if ($action & CRM_Core_Action::DELETE) {
a3d827a7 80 if (CRM_Utils_Request::retrieve('confirmed', 'Boolean')) {
7618eb9d 81 CRM_Core_BAO_File::deleteFileReferences($fileId, $entityId, $fieldId);
6a488035
TO
82 CRM_Core_Session::setStatus(ts('The attached file has been deleted.'), ts('Complete'), 'success');
83
84 $session = CRM_Core_Session::singleton();
85 $toUrl = $session->popUserContext();
86 CRM_Utils_System::redirect($toUrl);
87 }
6a488035
TO
88 }
89 else {
b3fdbf3d
DL
90 CRM_Utils_System::download(
91 CRM_Utils_File::cleanFileName(basename($path)),
6a488035 92 $mimeType,
3159f5dc 93 $buffer,
94 NULL,
95 TRUE,
96 $disposition
6a488035
TO
97 );
98 }
99 }
96025800 100
ce5b9953
TO
101 /**
102 * Translate one mime type to another.
103 *
104 * Certain non-standard/weird MIME types have been common. Unfortunately, because
105 * of the way this controller is used, the weird types may baked-into URLs.
106 * We clean these up for compatibility.
107 *
108 * @param string $type
109 * Ex: 'image/jpg'
110 * @return string
111 * Ex: 'image/jpeg'.
112 */
113 protected static function convertBadMimeAliasTypes($type) {
114 $badTypes = [
115 // Before PNG format was ubiquitous, it was image/x-png?
116 'image/x-png' => 'image/png',
117
118 // People see "image/gif" and "image/png" and wrongly guess "image/jpg"?
119 'image/jpg' => 'image/jpeg',
120 'image/tif' => 'image/tiff',
121 'image/svg' => 'image/svg+xml',
122
123 // StackExchange attributes "pjpeg" to some quirk in an old version of IE?
124 'image/pjpeg' => 'image/jpeg',
125
126 ];
127 return isset($badTypes[$type]) ? $badTypes[$type] : $type;
128 }
129
6a488035 130}