Skip to content

Commit 493634d

Browse files
committed
refactor: switch to use Symfony HTTP foundation (#3338)
1 parent 586754d commit 493634d

4 files changed

Lines changed: 43 additions & 53 deletions

File tree

phpmyfaq/attachment.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Symfony\Component\DependencyInjection\ContainerBuilder;
2727
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
2828
use Symfony\Component\HttpFoundation\Request;
29+
use Symfony\Component\HttpFoundation\StreamedResponse;
2930

3031
if (!defined('IS_VALID_PHPMYFAQ')) {
3132
http_response_code(400);
@@ -113,11 +114,19 @@
113114
$attachment && $attachment->getRecordId() > 0 && ($faqConfig->get('records.allowDownloadsForGuests') ||
114115
(($groupPermission || ($groupPermission && $userPermission)) && isset($permission['dlattachment'])))
115116
) {
116-
try {
117+
$response = new StreamedResponse(function () use ($attachment) {
117118
$attachment->rawOut();
118-
} catch (AttachmentException|ErrorException $e) {
119-
$attachmentErrors[] = $e->getMessage();
120-
}
119+
});
120+
121+
$response->headers->set('Content-Type', $attachment->getMimeType());
122+
$response->headers->set('Content-Length', $attachment->getFilesize());
123+
$response->headers->set(
124+
'Content-Disposition',
125+
'attachment; filename="' . rawurlencode($attachment->getFilename()) . '"'
126+
);
127+
$response->headers->set('Content-MD5', $attachment->getRealHash());
128+
129+
$response->send();
121130
} else {
122131
$attachmentErrors[] = Translation::get('msgAttachmentInvalid');
123132
}

phpmyfaq/src/phpMyFAQ/Attachment/AbstractAttachment.php

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,14 @@ abstract class AbstractAttachment
9696
/**
9797
* Constructor.
9898
*
99-
* @param mixed $id attachment id
99+
* @param mixed $attachmentId attachment id
100100
*/
101-
public function __construct(mixed $id = null)
101+
public function __construct(mixed $attachmentId = null)
102102
{
103103
$this->db = Database::getInstance();
104104

105-
if (null !== $id) {
106-
$this->id = $id;
105+
if (null !== $attachmentId) {
106+
$this->id = $attachmentId;
107107
$this->getMeta();
108108
}
109109
}
@@ -173,7 +173,7 @@ public function setKey(?string $key, bool $default = true): void
173173
$this->key = $key;
174174
$this->encrypted = null !== $key;
175175
// Not default means the key was set explicitly
176-
// for this attachment, so lets hash it
176+
// for this attachment, so let's hash it
177177
if (!$this->encrypted) {
178178
return;
179179
}
@@ -204,9 +204,9 @@ public function getId(): int
204204
/**
205205
* Sets attachment id.
206206
*/
207-
public function setId(int $id): void
207+
public function setId(int $attachmentId): void
208208
{
209-
$this->id = $id;
209+
$this->id = $attachmentId;
210210
}
211211

212212
/**
@@ -220,11 +220,11 @@ public function getRecordId(): int
220220
/**
221221
* Set record id.
222222
*
223-
* @param int $id record id
223+
* @param int $recordId record id
224224
*/
225-
public function setRecordId(int $id): void
225+
public function setRecordId(int $recordId): void
226226
{
227-
$this->recordId = $id;
227+
$this->recordId = $recordId;
228228
}
229229

230230
/**
@@ -268,34 +268,33 @@ public function saveMeta(): int
268268
return $this->id;
269269
}
270270

271-
/**
272-
* Returns filename.
273-
*/
274271
public function getFilename(): string
275272
{
276273
return $this->filename;
277274
}
278275

279-
/**
280-
* Returns the MIME type
281-
*/
282276
public function getMimeType(): string
283277
{
284278
return $this->mimeType;
285279
}
286280

281+
public function getFilesize(): int
282+
{
283+
return $this->filesize;
284+
}
285+
286+
public function getRealHash(): string
287+
{
288+
return $this->realHash;
289+
}
290+
287291
/**
288292
* Update several meta things after it was saved.
289293
*/
290294
protected function postUpdateMeta(): void
291295
{
292296
$sql = sprintf(
293-
"
294-
UPDATE
295-
%sfaqattachment
296-
SET virtual_hash = '%s',
297-
mime_type = '%s'
298-
WHERE id = %d",
297+
"UPDATE %sfaqattachment SET virtual_hash = '%s', mime_type = '%s' WHERE id = %d",
299298
Database::getTablePrefix(),
300299
$this->db->escape($this->virtualHash),
301300
$this->readMimeType(),
@@ -320,14 +319,11 @@ protected function readMimeType(): string
320319
/**
321320
* Generate hash based on current conditions.
322321
*
323-
* @return string
324-
*
322+
* @return string|null NOTE The way a file is saved in the filesystem
325323
* NOTE The way a file is saved in the filesystem
326324
* is based on md5 hash. If the file is unencrypted,
327-
* it's md5 hash is used directly, otherwise a
325+
* it md5 hash is used directly, otherwise a
328326
* hash based on several tokens gets generated.
329-
*
330-
* @return string|null
331327
* @throws AttachmentException
332328
*/
333329
protected function mkVirtualHash(): ?string

phpmyfaq/src/phpMyFAQ/Attachment/AttachmentInterface.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,27 @@ interface AttachmentInterface
2828
* Save current attachment to the appropriate storage.
2929
*
3030
* @param string $filePath full path to the attachment file
31-
*
3231
* @return bool
3332
*/
34-
public function save($filePath);
33+
public function save(string $filePath): bool;
3534

3635
/**
3736
* Delete attachment.
3837
*
3938
* @return bool
4039
*/
41-
public function delete();
40+
public function delete(): bool;
4241

4342
/**
4443
* Retrieve file contents into a variable.
4544
*
4645
* @return string
4746
*/
48-
public function get();
47+
public function get(): string;
4948

5049
/**
5150
* Output current file to stdout.
5251
*
53-
* @param bool $headers if headers must be sent
54-
* @param string $disposition disposition type (ignored if $headers false)
5552
*/
56-
public function rawOut($headers = true, $disposition = 'attachment');
53+
public function rawOut(): void;
5754
}

phpmyfaq/src/phpMyFAQ/Attachment/File.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,11 @@ public function isStorageOk(): bool
8888
* filepath given will be processed and moved to appropriate
8989
* location.
9090
*
91-
* @param string $filePath full path to the attachment file
92-
* @param string|null $filename filename to force
91+
* @param string $filePath full path to the attachment file
9392
* @throws FileException|AttachmentException
9493
* @todo rollback if something went wrong
9594
*/
96-
public function save($filePath, string $filename = null): bool
95+
public function save(string $filePath): bool
9796
{
9897
$success = false;
9998

@@ -160,22 +159,11 @@ public function get(): string
160159
/**
161160
* Output current file to stdout.
162161
*
163-
* @param bool $headers if headers must be sent
164-
* @param string $disposition disposition type (ignored if $headers false)
165162
* @throws AttachmentException
166163
*/
167-
public function rawOut($headers = true, $disposition = 'attachment'): void
164+
public function rawOut(): void
168165
{
169166
$file = $this->getFile();
170-
171-
if ($headers) {
172-
$disposition = 'attachment' == $disposition ? 'attachment' : 'inline';
173-
header('Content-Type: ' . $this->mimeType);
174-
header('Content-Length: ' . $this->filesize);
175-
header(sprintf('Content-Disposition: %s; filename="', $disposition) . rawurlencode($this->filename) . '"');
176-
header('Content-MD5: ' . $this->realHash);
177-
}
178-
179167
while (!$file->eof()) {
180168
echo $file->getChunk();
181169
}

0 commit comments

Comments
 (0)