/
add_file.php
224 lines (201 loc) · 8.58 KB
/
add_file.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
namespace Concrete\Controller\Frontend\Conversations;
use Concrete\Core\Conversation\Conversation;
use Concrete\Core\Conversation\FrontendController;
use Concrete\Core\Entity\File\Version;
use Concrete\Core\Error\UserMessageException;
use Concrete\Core\File\Import\FileImporter;
use Concrete\Core\File\Import\ImportException;
use Concrete\Core\File\Set\Set as FileSet;
use Concrete\Core\Http\ResponseFactoryInterface;
use Concrete\Core\User\User;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Response;
defined('C5_EXECUTE') or die('Access Denied.');
class AddFile extends FrontendController
{
public function view(): Response
{
$responseFactory = $this->app->make(ResponseFactoryInterface::class);
$post = $this->request->request;
try {
$this->checkToken();
$conversation = $this->getBlockConversation();
$this->checkConversation($conversation);
$file = $this->getPostedFile();
$this->checkPostedFileLimits($conversation, $file);
$this->checkPostedFileExtension($conversation, $file);
$fileVersion = $this->importFile($conversation, $file);
return $responseFactory->json([
'id' => (int) $fileVersion->getFileID(),
'timestamp' => $post->get('timestamp'),
'tag' => $post->get('tag'),
]);
} catch (UserMessageException $x) {
return $responseFactory->json([
'error' => $x->getMessage(),
'timestamp' => $post->get('timestamp'),
]);
}
}
/**
* @throws \Concrete\Core\Error\UserMessageException
*/
protected function checkToken(): void
{
$val = $this->app->make('token');
if (!$val->validate('add_conversations_file')) {
throw new UserMessageException($val->getErrorMessage());
}
}
/**
* @throws \Concrete\Core\Error\UserMessageException
*/
protected function checkConversation(Conversation $conversation): void
{
if ($conversation->getConversationAttachmentOverridesEnabled() > 0) {
// Check individual conversation for allowing attachments.
if (!$conversation->getConversationAttachmentsEnabled()) {
throw new UserMessageException(t('This conversation does not allow file attachments.'));
}
} else {
// Check global config settings for whether or not file attachments should be allowed.
$config = $this->app->make('config');
if (!$config->get('conversations.attachments_enabled')) {
throw new UserMessageException(t('This conversation does not allow file attachments.'));
}
}
}
/**
* @throws \Concrete\Core\Error\UserMessageException
*/
protected function getPostedFile(): UploadedFile
{
$file = $this->request->files->get('file');
if (!($file instanceof UploadedFile)) {
throw new UserMessageException(t('File not received'));
}
if (!$file->isValid()) {
throw new UserMessageException(ImportException::describeErrorCode($file->getError()));
}
return $file;
}
/**
* @throws \Concrete\Core\Error\UserMessageException
*/
protected function checkPostedFileLimits(Conversation $conversation, UploadedFile $file): void
{
list($maxFileSize, $maxQuantity) = $this->getAttachmentLimits($conversation);
if ($maxFileSize !== null && $file->getSize() > $maxFileSize * 1000000) {
throw new UserMessageException(t('File size exceeds limit'));
}
// check file count (this is just for presentation, final count check is done on message submit).
if ($maxQuantity !== null && (int) $this->request->request->get('fileCount') > $maxQuantity) {
throw new UserMessageException(tt('Attachment limit reached'));
}
}
/**
* @return array First element: max file size (int; NULL: unlimited), second element: max quantity (int, or NULL if unlimited)
*/
protected function getAttachmentLimits(Conversation $conversation): array
{
$u = $this->app->make(User::class);
$config = $this->app->make('config');
if ($u->isRegistered()) {
if ($conversation->getConversationAttachmentOverridesEnabled()) {
$maxFileSize = $conversation->getConversationMaxFileSizeRegistered();
$maxQuantity = $conversation->getConversationMaxFilesRegistered();
} else {
$maxFileSize = $config->get('conversations.files.registered.max_size');
$maxQuantity = $config->get('conversations.files.registered.max');
}
} else {
if ($conversation->getConversationAttachmentOverridesEnabled()) {
$maxFileSize = $conversation->getConversationMaxFilesGuest();
$maxQuantity = $conversation->getConversationMaxFilesGuest();
} else {
$maxFileSize = $config->get('conversations.files.guest.max_size');
$maxQuantity = $config->get('conversations.files.guest.max');
}
}
return [
(int) $maxFileSize <= 0 ? null : (int) $maxFileSize,
(string) $maxQuantity === '' ? null : (int) $maxQuantity,
];
}
/**
* @throws \Concrete\Core\Error\UserMessageException
*/
protected function checkPostedFileExtension(Conversation $conversation, UploadedFile $file): void
{
$incomingExtension = mb_strtolower($file->getClientOriginalExtension());
if ($incomingExtension === '') {
return;
}
$extensionIsValid = false;
$denylistedExtensions = $this->getDenylistedFileExtensions($conversation);
if (!in_array($incomingExtension, $denylistedExtensions, true)) {
$allowedExtensions = $this->getAllowedFileExtensions($conversation);
$extensionIsValid = $allowedExtensions === [] || in_array($incomingExtension, $allowedExtensions, true);
}
if ($extensionIsValid === false) {
throw new UserMessageException(t('Invalid File Extension'));
}
}
/**
* Get the list of denylisted file extensions (lower case).
*
* @return string[]
*/
protected function getDenylistedFileExtensions(Conversation $conversation): array
{
$config = $this->app->make('config');
$extensions = $config->get('conversations.files.disallowed_types');
if ($extensions === null) {
$extensions = $config->get('concrete.upload.extensions_denylist', $config->get('concrete.upload.extensions_blacklist'));
}
$helperFile = $this->app->make('helper/concrete/file');
return array_map('mb_strtolower', $helperFile->unserializeUploadFileExtensions($extensions));
}
/**
* Get the list of allowed file extensions (lower case).
*
* @return string[]
*/
protected function getAllowedFileExtensions(Conversation $conversation): array
{
if ($conversation->getConversationAttachmentOverridesEnabled()) {
$extensions = $conversation->getConversationFileExtensions();
} else {
$config = $this->app->make('config');
$extensions = $config->get('conversations.files.allowed_types');
}
$helperFile = $this->app->make('helper/concrete/file');
return array_map('mb_strtolower', $helperFile->unserializeUploadFileExtensions($extensions));
}
protected function importFile(Conversation $conversation, UploadedFile $file): Version
{
$fileImporter = $this->app->make(FileImporter::class);
$fileVersion = $fileImporter->importUploadedFile($file);
$this->assignImportedFileToFileSet($conversation, $fileVersion);
return $fileVersion;
}
protected function assignImportedFileToFileSet(Conversation $conversation, Version $fileVersion): ?FileSet
{
$fileSet = $this->getFileSetForUploadedFile($conversation);
if ($fileSet !== null) {
$fileSet->addFileToSet($fileVersion);
}
return $fileSet;
}
protected function getFileSetForUploadedFile(Conversation $conversation): ?FileSet
{
$config = $this->app->make('config');
$fileSetName = $config->get('conversations.attachments_pending_file_set');
$fileSet = FileSet::getByName($fileSetName);
if ($fileSet === null) {
$fileSet = FileSet::createAndGetSet($fileSetName, FileSet::TYPE_PUBLIC, USER_SUPER_ID);
}
return $fileSet;
}
}