Skip to content

Commit

Permalink
refactor: switch to use Symfony HTTP foundation (#3338)
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Jan 16, 2025
1 parent 586754d commit 493634d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 53 deletions.
17 changes: 13 additions & 4 deletions phpmyfaq/attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\StreamedResponse;

if (!defined('IS_VALID_PHPMYFAQ')) {
http_response_code(400);
Expand Down Expand Up @@ -113,11 +114,19 @@
$attachment && $attachment->getRecordId() > 0 && ($faqConfig->get('records.allowDownloadsForGuests') ||
(($groupPermission || ($groupPermission && $userPermission)) && isset($permission['dlattachment'])))
) {
try {
$response = new StreamedResponse(function () use ($attachment) {
$attachment->rawOut();
} catch (AttachmentException|ErrorException $e) {
$attachmentErrors[] = $e->getMessage();
}
});

$response->headers->set('Content-Type', $attachment->getMimeType());
$response->headers->set('Content-Length', $attachment->getFilesize());
$response->headers->set(
'Content-Disposition',
'attachment; filename="' . rawurlencode($attachment->getFilename()) . '"'
);
$response->headers->set('Content-MD5', $attachment->getRealHash());

$response->send();
} else {
$attachmentErrors[] = Translation::get('msgAttachmentInvalid');
}
Expand Down
50 changes: 23 additions & 27 deletions phpmyfaq/src/phpMyFAQ/Attachment/AbstractAttachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ abstract class AbstractAttachment
/**
* Constructor.
*
* @param mixed $id attachment id
* @param mixed $attachmentId attachment id
*/
public function __construct(mixed $id = null)
public function __construct(mixed $attachmentId = null)
{
$this->db = Database::getInstance();

if (null !== $id) {
$this->id = $id;
if (null !== $attachmentId) {
$this->id = $attachmentId;
$this->getMeta();
}
}
Expand Down Expand Up @@ -173,7 +173,7 @@ public function setKey(?string $key, bool $default = true): void
$this->key = $key;
$this->encrypted = null !== $key;
// Not default means the key was set explicitly
// for this attachment, so lets hash it
// for this attachment, so let's hash it
if (!$this->encrypted) {
return;
}
Expand Down Expand Up @@ -204,9 +204,9 @@ public function getId(): int
/**
* Sets attachment id.
*/
public function setId(int $id): void
public function setId(int $attachmentId): void
{
$this->id = $id;
$this->id = $attachmentId;
}

/**
Expand All @@ -220,11 +220,11 @@ public function getRecordId(): int
/**
* Set record id.
*
* @param int $id record id
* @param int $recordId record id
*/
public function setRecordId(int $id): void
public function setRecordId(int $recordId): void
{
$this->recordId = $id;
$this->recordId = $recordId;
}

/**
Expand Down Expand Up @@ -268,34 +268,33 @@ public function saveMeta(): int
return $this->id;
}

/**
* Returns filename.
*/
public function getFilename(): string
{
return $this->filename;
}

/**
* Returns the MIME type
*/
public function getMimeType(): string
{
return $this->mimeType;
}

public function getFilesize(): int
{
return $this->filesize;
}

public function getRealHash(): string
{
return $this->realHash;
}

/**
* Update several meta things after it was saved.
*/
protected function postUpdateMeta(): void
{
$sql = sprintf(
"
UPDATE
%sfaqattachment
SET virtual_hash = '%s',
mime_type = '%s'
WHERE id = %d",
"UPDATE %sfaqattachment SET virtual_hash = '%s', mime_type = '%s' WHERE id = %d",
Database::getTablePrefix(),
$this->db->escape($this->virtualHash),
$this->readMimeType(),
Expand All @@ -320,14 +319,11 @@ protected function readMimeType(): string
/**
* Generate hash based on current conditions.
*
* @return string
*
* @return string|null NOTE The way a file is saved in the filesystem
* NOTE The way a file is saved in the filesystem
* is based on md5 hash. If the file is unencrypted,
* it's md5 hash is used directly, otherwise a
* it md5 hash is used directly, otherwise a
* hash based on several tokens gets generated.
*
* @return string|null
* @throws AttachmentException
*/
protected function mkVirtualHash(): ?string
Expand Down
11 changes: 4 additions & 7 deletions phpmyfaq/src/phpMyFAQ/Attachment/AttachmentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,27 @@ interface AttachmentInterface
* Save current attachment to the appropriate storage.
*
* @param string $filePath full path to the attachment file
*
* @return bool
*/
public function save($filePath);
public function save(string $filePath): bool;

/**
* Delete attachment.
*
* @return bool
*/
public function delete();
public function delete(): bool;

/**
* Retrieve file contents into a variable.
*
* @return string
*/
public function get();
public function get(): string;

/**
* Output current file to stdout.
*
* @param bool $headers if headers must be sent
* @param string $disposition disposition type (ignored if $headers false)
*/
public function rawOut($headers = true, $disposition = 'attachment');
public function rawOut(): void;
}
18 changes: 3 additions & 15 deletions phpmyfaq/src/phpMyFAQ/Attachment/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,11 @@ public function isStorageOk(): bool
* filepath given will be processed and moved to appropriate
* location.
*
* @param string $filePath full path to the attachment file
* @param string|null $filename filename to force
* @param string $filePath full path to the attachment file
* @throws FileException|AttachmentException
* @todo rollback if something went wrong
*/
public function save($filePath, string $filename = null): bool
public function save(string $filePath): bool
{
$success = false;

Expand Down Expand Up @@ -160,22 +159,11 @@ public function get(): string
/**
* Output current file to stdout.
*
* @param bool $headers if headers must be sent
* @param string $disposition disposition type (ignored if $headers false)
* @throws AttachmentException
*/
public function rawOut($headers = true, $disposition = 'attachment'): void
public function rawOut(): void
{
$file = $this->getFile();

if ($headers) {
$disposition = 'attachment' == $disposition ? 'attachment' : 'inline';
header('Content-Type: ' . $this->mimeType);
header('Content-Length: ' . $this->filesize);
header(sprintf('Content-Disposition: %s; filename="', $disposition) . rawurlencode($this->filename) . '"');
header('Content-MD5: ' . $this->realHash);
}

while (!$file->eof()) {
echo $file->getChunk();
}
Expand Down

0 comments on commit 493634d

Please sign in to comment.