@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | { |
| 25 | 25 | $jaxon = jaxon(); |
| 26 | 26 | $di = $jaxon->di(); |
| 27 | - if($di->h(UploadHandler::class)) |
|
| 27 | + if ($di->h(UploadHandler::class)) |
|
| 28 | 28 | { |
| 29 | 29 | return; |
| 30 | 30 | } |
@@ -76,7 +76,7 @@ discard block |
||
| 76 | 76 | $xUploadHandler = $di->g(UploadHandler::class); |
| 77 | 77 | // The HTTP request |
| 78 | 78 | $xRequest = $di->getRequest(); |
| 79 | - if($xUploadHandler->canProcessRequest($xRequest)) |
|
| 79 | + if ($xUploadHandler->canProcessRequest($xRequest)) |
|
| 80 | 80 | { |
| 81 | 81 | $xUploadHandler->processRequest($xRequest); |
| 82 | 82 | } |
@@ -92,7 +92,7 @@ discard block |
||
| 92 | 92 | { |
| 93 | 93 | $jaxon = jaxon(); |
| 94 | 94 | $jaxon->callback()->boot(function() use($jaxon) { |
| 95 | - if($jaxon->getOption('core.upload.enabled')) |
|
| 95 | + if ($jaxon->getOption('core.upload.enabled')) |
|
| 96 | 96 | { |
| 97 | 97 | registerUpload(); |
| 98 | 98 | } |
@@ -102,7 +102,7 @@ discard block |
||
| 102 | 102 | function register() |
| 103 | 103 | { |
| 104 | 104 | // Do nothing if running in cli. |
| 105 | - if(php_sapi_name() !== 'cli') |
|
| 105 | + if (php_sapi_name() !== 'cli') |
|
| 106 | 106 | { |
| 107 | 107 | _register(); |
| 108 | 108 | }; |
@@ -22,63 +22,63 @@ discard block |
||
| 22 | 22 | */ |
| 23 | 23 | function registerUpload(): void |
| 24 | 24 | { |
| 25 | - $jaxon = jaxon(); |
|
| 26 | - $di = $jaxon->di(); |
|
| 27 | - if($di->h(UploadHandler::class)) |
|
| 28 | - { |
|
| 29 | - return; |
|
| 30 | - } |
|
| 25 | +$jaxon = jaxon(); |
|
| 26 | +$di = $jaxon->di(); |
|
| 27 | +if($di->h(UploadHandler::class)) |
|
| 28 | +{ |
|
| 29 | +return; |
|
| 30 | +} |
|
| 31 | 31 | |
| 32 | - // Upload file and dir name generator |
|
| 33 | - $di->set(FileNameInterface::class, function() { |
|
| 34 | - return new class implements FileNameInterface |
|
| 35 | - { |
|
| 36 | - public function random(int $nLength): string |
|
| 37 | - { |
|
| 38 | - return bin2hex(random_bytes((int)($nLength / 2))); |
|
| 39 | - } |
|
| 40 | - }; |
|
| 41 | - }); |
|
| 42 | - // Upload validator |
|
| 43 | - $di->set(Validator::class, function($c) { |
|
| 44 | - return new Validator($c->g(ConfigManager::class), $c->g(Translator::class)); |
|
| 45 | - }); |
|
| 46 | - // File storage |
|
| 47 | - $di->set(FileStorage::class, function($c) { |
|
| 48 | - return new FileStorage($c->g(ConfigManager::class), $c->g(Translator::class)); |
|
| 49 | - }); |
|
| 50 | - // File upload manager |
|
| 51 | - $di->set(UploadManager::class, function($c) { |
|
| 52 | - // Translation directory |
|
| 53 | - $sTranslationDir = dirname(__DIR__) . '/translations'; |
|
| 54 | - // Load the upload translations |
|
| 55 | - $xTranslator = $c->g(Translator::class); |
|
| 56 | - $xTranslator->loadTranslations("$sTranslationDir/en/upload.php", 'en'); |
|
| 57 | - $xTranslator->loadTranslations("$sTranslationDir/fr/upload.php", 'fr'); |
|
| 58 | - $xTranslator->loadTranslations("$sTranslationDir/es/upload.php", 'es'); |
|
| 32 | +// Upload file and dir name generator |
|
| 33 | +$di->set(FileNameInterface::class, function() { |
|
| 34 | +return new class implements FileNameInterface |
|
| 35 | +{ |
|
| 36 | +public function random(int $nLength): string |
|
| 37 | +{ |
|
| 38 | + return bin2hex(random_bytes((int)($nLength / 2))); |
|
| 39 | +} |
|
| 40 | +}; |
|
| 41 | +}); |
|
| 42 | +// Upload validator |
|
| 43 | +$di->set(Validator::class, function($c) { |
|
| 44 | +return new Validator($c->g(ConfigManager::class), $c->g(Translator::class)); |
|
| 45 | +}); |
|
| 46 | +// File storage |
|
| 47 | +$di->set(FileStorage::class, function($c) { |
|
| 48 | +return new FileStorage($c->g(ConfigManager::class), $c->g(Translator::class)); |
|
| 49 | +}); |
|
| 50 | +// File upload manager |
|
| 51 | +$di->set(UploadManager::class, function($c) { |
|
| 52 | +// Translation directory |
|
| 53 | +$sTranslationDir = dirname(__DIR__) . '/translations'; |
|
| 54 | +// Load the upload translations |
|
| 55 | +$xTranslator = $c->g(Translator::class); |
|
| 56 | +$xTranslator->loadTranslations("$sTranslationDir/en/upload.php", 'en'); |
|
| 57 | +$xTranslator->loadTranslations("$sTranslationDir/fr/upload.php", 'fr'); |
|
| 58 | +$xTranslator->loadTranslations("$sTranslationDir/es/upload.php", 'es'); |
|
| 59 | 59 | |
| 60 | - return new UploadManager($c->g(LoggerInterface::class), $c->g(Validator::class), |
|
| 61 | - $xTranslator, $c->g(FileStorage::class), |
|
| 62 | - $c->g(FileNameInterface::class), $c->g(ConfigManager::class)); |
|
| 63 | - }); |
|
| 64 | - // File upload plugin |
|
| 65 | - $di->set(UploadHandler::class, function($c) { |
|
| 66 | - return new UploadHandler($c->g(FileStorage::class), $c->g(UploadManager::class)); |
|
| 67 | - }); |
|
| 68 | - // Set alias on the interface |
|
| 69 | - $di->alias(UploadHandlerInterface::class, UploadHandler::class); |
|
| 60 | +return new UploadManager($c->g(LoggerInterface::class), $c->g(Validator::class), |
|
| 61 | +$xTranslator, $c->g(FileStorage::class), |
|
| 62 | +$c->g(FileNameInterface::class), $c->g(ConfigManager::class)); |
|
| 63 | +}); |
|
| 64 | +// File upload plugin |
|
| 65 | +$di->set(UploadHandler::class, function($c) { |
|
| 66 | +return new UploadHandler($c->g(FileStorage::class), $c->g(UploadManager::class)); |
|
| 67 | +}); |
|
| 68 | +// Set alias on the interface |
|
| 69 | +$di->alias(UploadHandlerInterface::class, UploadHandler::class); |
|
| 70 | 70 | |
| 71 | - // Set a callback to process uploaded files in the incoming requests. |
|
| 72 | - $jaxon->callback()->before(function() use($di) { |
|
| 73 | - /** @var UploadHandler */ |
|
| 74 | - $xUploadHandler = $di->g(UploadHandler::class); |
|
| 75 | - // The HTTP request |
|
| 76 | - $xRequest = $di->getRequest(); |
|
| 77 | - if($xUploadHandler->canProcessRequest($xRequest)) |
|
| 78 | - { |
|
| 79 | - $xUploadHandler->processRequest($xRequest); |
|
| 80 | - } |
|
| 81 | - }); |
|
| 71 | +// Set a callback to process uploaded files in the incoming requests. |
|
| 72 | +$jaxon->callback()->before(function() use($di) { |
|
| 73 | +/** @var UploadHandler */ |
|
| 74 | +$xUploadHandler = $di->g(UploadHandler::class); |
|
| 75 | +// The HTTP request |
|
| 76 | +$xRequest = $di->getRequest(); |
|
| 77 | +if($xUploadHandler->canProcessRequest($xRequest)) |
|
| 78 | +{ |
|
| 79 | +$xUploadHandler->processRequest($xRequest); |
|
| 80 | +} |
|
| 81 | +}); |
|
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | /** |
@@ -88,22 +88,22 @@ discard block |
||
| 88 | 88 | */ |
| 89 | 89 | function _register(): void |
| 90 | 90 | { |
| 91 | - $jaxon = jaxon(); |
|
| 92 | - $jaxon->callback()->boot(function() use($jaxon) { |
|
| 93 | - if($jaxon->getOption('core.upload.enabled')) |
|
| 94 | - { |
|
| 95 | - registerUpload(); |
|
| 96 | - } |
|
| 97 | - }); |
|
| 91 | +$jaxon = jaxon(); |
|
| 92 | +$jaxon->callback()->boot(function() use($jaxon) { |
|
| 93 | +if($jaxon->getOption('core.upload.enabled')) |
|
| 94 | +{ |
|
| 95 | +registerUpload(); |
|
| 96 | +} |
|
| 97 | +}); |
|
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 100 | function register() |
| 101 | 101 | { |
| 102 | - // Do nothing if running in cli. |
|
| 103 | - if(php_sapi_name() !== 'cli') |
|
| 104 | - { |
|
| 105 | - _register(); |
|
| 106 | - }; |
|
| 102 | +// Do nothing if running in cli. |
|
| 103 | +if(php_sapi_name() !== 'cli') |
|
| 104 | +{ |
|
| 105 | +_register(); |
|
| 106 | +}; |
|
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | register(); |
@@ -97,7 +97,7 @@ discard block |
||
| 97 | 97 | {
|
| 98 | 98 | $xDefault = $this->xConfigManager->getOption('upload.default.' . $sProperty);
|
| 99 | 99 | $aAllowed = $this->xConfigManager->getOption('upload.files.' . $sVarName . '.' . $sProperty, $xDefault);
|
| 100 | - if(is_array($aAllowed) && !in_array($sValue, $aAllowed)) |
|
| 100 | + if (is_array($aAllowed) && !in_array($sValue, $aAllowed)) |
|
| 101 | 101 | {
|
| 102 | 102 | $this->sErrorMessage = $this->xTranslator->trans('errors.upload.' . $sField, [$sField => $sValue]);
|
| 103 | 103 | return false; |
@@ -118,7 +118,7 @@ discard block |
||
| 118 | 118 | {
|
| 119 | 119 | $xDefault = $this->xConfigManager->getOption('upload.default.' . $sProperty, 0);
|
| 120 | 120 | $nSize = $this->xConfigManager->getOption('upload.files.' . $sVarName . '.' . $sProperty, $xDefault);
|
| 121 | - if($nSize > 0 && ( |
|
| 121 | + if ($nSize > 0 && ( |
|
| 122 | 122 | ($sProperty == 'max-size' && $nFileSize > $nSize) || |
| 123 | 123 | ($sProperty == 'min-size' && $nFileSize < $nSize))) |
| 124 | 124 | {
|
@@ -140,25 +140,25 @@ discard block |
||
| 140 | 140 | {
|
| 141 | 141 | $this->sErrorMessage = ''; |
| 142 | 142 | // Verify the file extension |
| 143 | - if(!$this->validateFileProperty($sVarName, $xFile->type(), 'types', 'type')) |
|
| 143 | + if (!$this->validateFileProperty($sVarName, $xFile->type(), 'types', 'type')) |
|
| 144 | 144 | {
|
| 145 | 145 | return false; |
| 146 | 146 | } |
| 147 | 147 | |
| 148 | 148 | // Verify the file extension |
| 149 | - if(!$this->validateFileProperty($sVarName, $xFile->extension(), 'extensions', 'extension')) |
|
| 149 | + if (!$this->validateFileProperty($sVarName, $xFile->extension(), 'extensions', 'extension')) |
|
| 150 | 150 | {
|
| 151 | 151 | return false; |
| 152 | 152 | } |
| 153 | 153 | |
| 154 | 154 | // Verify the max size |
| 155 | - if(!$this->validateFileSize($sVarName, $xFile->size(), 'max-size')) |
|
| 155 | + if (!$this->validateFileSize($sVarName, $xFile->size(), 'max-size')) |
|
| 156 | 156 | {
|
| 157 | 157 | return false; |
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | // Verify the min size |
| 161 | - if(!$this->validateFileSize($sVarName, $xFile->size(), 'min-size')) |
|
| 161 | + if (!$this->validateFileSize($sVarName, $xFile->size(), 'min-size')) |
|
| 162 | 162 | {
|
| 163 | 163 | return false; |
| 164 | 164 | } |
@@ -30,46 +30,46 @@ discard block |
||
| 30 | 30 | |
| 31 | 31 | class Validator |
| 32 | 32 | {
|
| 33 | - /** |
|
| 33 | +/** |
|
| 34 | 34 | * The config manager |
| 35 | 35 | * |
| 36 | 36 | * @var ConfigManager |
| 37 | 37 | */ |
| 38 | - protected $xConfigManager; |
|
| 38 | +protected $xConfigManager; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 40 | +/** |
|
| 41 | 41 | * The translator |
| 42 | 42 | * |
| 43 | 43 | * @var Translator |
| 44 | 44 | */ |
| 45 | - protected $xTranslator; |
|
| 45 | +protected $xTranslator; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 47 | +/** |
|
| 48 | 48 | * The last error message |
| 49 | 49 | * |
| 50 | 50 | * @var string |
| 51 | 51 | */ |
| 52 | - protected $sErrorMessage; |
|
| 52 | +protected $sErrorMessage; |
|
| 53 | 53 | |
| 54 | - public function __construct(ConfigManager $xConfigManager, Translator $xTranslator) |
|
| 55 | - {
|
|
| 56 | - // Set the config manager |
|
| 57 | - $this->xConfigManager = $xConfigManager; |
|
| 58 | - // Set the translator |
|
| 59 | - $this->xTranslator = $xTranslator; |
|
| 60 | - } |
|
| 54 | +public function __construct(ConfigManager $xConfigManager, Translator $xTranslator) |
|
| 55 | +{
|
|
| 56 | +// Set the config manager |
|
| 57 | +$this->xConfigManager = $xConfigManager; |
|
| 58 | +// Set the translator |
|
| 59 | +$this->xTranslator = $xTranslator; |
|
| 60 | +} |
|
| 61 | 61 | |
| 62 | - /** |
|
| 62 | +/** |
|
| 63 | 63 | * Get the last error message |
| 64 | 64 | * |
| 65 | 65 | * @return string |
| 66 | 66 | */ |
| 67 | - public function getErrorMessage(): string |
|
| 68 | - {
|
|
| 69 | - return $this->sErrorMessage; |
|
| 70 | - } |
|
| 67 | +public function getErrorMessage(): string |
|
| 68 | +{
|
|
| 69 | +return $this->sErrorMessage; |
|
| 70 | +} |
|
| 71 | 71 | |
| 72 | - /** |
|
| 72 | +/** |
|
| 73 | 73 | * Validate a property of an uploaded file |
| 74 | 74 | * |
| 75 | 75 | * @param string $sVarName The uploaded file variable name |
@@ -79,19 +79,19 @@ discard block |
||
| 79 | 79 | * |
| 80 | 80 | * @return bool |
| 81 | 81 | */ |
| 82 | - private function validateFileProperty(string $sVarName, string $sValue, string $sProperty, string $sField): bool |
|
| 83 | - {
|
|
| 84 | - $xDefault = $this->xConfigManager->getOption('upload.default.' . $sProperty);
|
|
| 85 | - $aAllowed = $this->xConfigManager->getOption('upload.files.' . $sVarName . '.' . $sProperty, $xDefault);
|
|
| 86 | - if(is_array($aAllowed) && !in_array($sValue, $aAllowed)) |
|
| 87 | - {
|
|
| 88 | - $this->sErrorMessage = $this->xTranslator->trans('errors.upload.' . $sField, [$sField => $sValue]);
|
|
| 89 | - return false; |
|
| 90 | - } |
|
| 91 | - return true; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 82 | +private function validateFileProperty(string $sVarName, string $sValue, string $sProperty, string $sField): bool |
|
| 83 | +{
|
|
| 84 | +$xDefault = $this->xConfigManager->getOption('upload.default.' . $sProperty);
|
|
| 85 | +$aAllowed = $this->xConfigManager->getOption('upload.files.' . $sVarName . '.' . $sProperty, $xDefault);
|
|
| 86 | +if(is_array($aAllowed) && !in_array($sValue, $aAllowed)) |
|
| 87 | +{
|
|
| 88 | +$this->sErrorMessage = $this->xTranslator->trans('errors.upload.' . $sField, [$sField => $sValue]);
|
|
| 89 | +return false; |
|
| 90 | +} |
|
| 91 | +return true; |
|
| 92 | +} |
|
| 93 | + |
|
| 94 | +/** |
|
| 95 | 95 | * Validate the size of an uploaded file |
| 96 | 96 | * |
| 97 | 97 | * @param string $sVarName The uploaded file variable name |
@@ -100,21 +100,21 @@ discard block |
||
| 100 | 100 | * |
| 101 | 101 | * @return bool |
| 102 | 102 | */ |
| 103 | - private function validateFileSize(string $sVarName, int $nFileSize, string $sProperty): bool |
|
| 104 | - {
|
|
| 105 | - $xDefault = $this->xConfigManager->getOption('upload.default.' . $sProperty, 0);
|
|
| 106 | - $nSize = $this->xConfigManager->getOption('upload.files.' . $sVarName . '.' . $sProperty, $xDefault);
|
|
| 107 | - if($nSize > 0 && ( |
|
| 108 | - ($sProperty == 'max-size' && $nFileSize > $nSize) || |
|
| 109 | - ($sProperty == 'min-size' && $nFileSize < $nSize))) |
|
| 110 | - {
|
|
| 111 | - $this->sErrorMessage = $this->xTranslator->trans('errors.upload.' . $sProperty, ['size' => $nFileSize]);
|
|
| 112 | - return false; |
|
| 113 | - } |
|
| 114 | - return true; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 103 | +private function validateFileSize(string $sVarName, int $nFileSize, string $sProperty): bool |
|
| 104 | +{
|
|
| 105 | +$xDefault = $this->xConfigManager->getOption('upload.default.' . $sProperty, 0);
|
|
| 106 | +$nSize = $this->xConfigManager->getOption('upload.files.' . $sVarName . '.' . $sProperty, $xDefault);
|
|
| 107 | +if($nSize > 0 && ( |
|
| 108 | +($sProperty == 'max-size' && $nFileSize > $nSize) || |
|
| 109 | +($sProperty == 'min-size' && $nFileSize < $nSize))) |
|
| 110 | +{
|
|
| 111 | +$this->sErrorMessage = $this->xTranslator->trans('errors.upload.' . $sProperty, ['size' => $nFileSize]);
|
|
| 112 | +return false; |
|
| 113 | +} |
|
| 114 | +return true; |
|
| 115 | +} |
|
| 116 | + |
|
| 117 | +/** |
|
| 118 | 118 | * Validate an uploaded file |
| 119 | 119 | * |
| 120 | 120 | * @param string $sVarName The uploaded file variable name |
@@ -122,33 +122,33 @@ discard block |
||
| 122 | 122 | * |
| 123 | 123 | * @return bool |
| 124 | 124 | */ |
| 125 | - public function validateUploadedFile(string $sVarName, File $xFile): bool |
|
| 126 | - {
|
|
| 127 | - $this->sErrorMessage = ''; |
|
| 128 | - // Verify the file extension |
|
| 129 | - if(!$this->validateFileProperty($sVarName, $xFile->type(), 'types', 'type')) |
|
| 130 | - {
|
|
| 131 | - return false; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - // Verify the file extension |
|
| 135 | - if(!$this->validateFileProperty($sVarName, $xFile->extension(), 'extensions', 'extension')) |
|
| 136 | - {
|
|
| 137 | - return false; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - // Verify the max size |
|
| 141 | - if(!$this->validateFileSize($sVarName, $xFile->size(), 'max-size')) |
|
| 142 | - {
|
|
| 143 | - return false; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - // Verify the min size |
|
| 147 | - if(!$this->validateFileSize($sVarName, $xFile->size(), 'min-size')) |
|
| 148 | - {
|
|
| 149 | - return false; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - return true; |
|
| 153 | - } |
|
| 125 | +public function validateUploadedFile(string $sVarName, File $xFile): bool |
|
| 126 | +{
|
|
| 127 | +$this->sErrorMessage = ''; |
|
| 128 | +// Verify the file extension |
|
| 129 | +if(!$this->validateFileProperty($sVarName, $xFile->type(), 'types', 'type')) |
|
| 130 | +{
|
|
| 131 | +return false; |
|
| 132 | +} |
|
| 133 | + |
|
| 134 | +// Verify the file extension |
|
| 135 | +if(!$this->validateFileProperty($sVarName, $xFile->extension(), 'extensions', 'extension')) |
|
| 136 | +{
|
|
| 137 | +return false; |
|
| 138 | +} |
|
| 139 | + |
|
| 140 | +// Verify the max size |
|
| 141 | +if(!$this->validateFileSize($sVarName, $xFile->size(), 'max-size')) |
|
| 142 | +{
|
|
| 143 | +return false; |
|
| 144 | +} |
|
| 145 | + |
|
| 146 | +// Verify the min size |
|
| 147 | +if(!$this->validateFileSize($sVarName, $xFile->size(), 'min-size')) |
|
| 148 | +{
|
|
| 149 | +return false; |
|
| 150 | +} |
|
| 151 | + |
|
| 152 | +return true; |
|
| 153 | +} |
|
| 154 | 154 | } |
@@ -22,56 +22,56 @@ discard block |
||
| 22 | 22 | |
| 23 | 23 | class File implements FileInterface |
| 24 | 24 | {
|
| 25 | - /** |
|
| 25 | +/** |
|
| 26 | 26 | * The uploaded file type |
| 27 | 27 | * |
| 28 | 28 | * @var string |
| 29 | 29 | */ |
| 30 | - protected $sType; |
|
| 30 | +protected $sType; |
|
| 31 | 31 | |
| 32 | - /** |
|
| 32 | +/** |
|
| 33 | 33 | * The uploaded file name, without the extension and sanitized |
| 34 | 34 | * |
| 35 | 35 | * @var string |
| 36 | 36 | */ |
| 37 | - protected $sName; |
|
| 37 | +protected $sName; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 39 | +/** |
|
| 40 | 40 | * The uploaded file name, with the extension |
| 41 | 41 | * |
| 42 | 42 | * @var string |
| 43 | 43 | */ |
| 44 | - protected $sFilename; |
|
| 44 | +protected $sFilename; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 46 | +/** |
|
| 47 | 47 | * The uploaded file path |
| 48 | 48 | * |
| 49 | 49 | * @var string |
| 50 | 50 | */ |
| 51 | - protected $sPath; |
|
| 51 | +protected $sPath; |
|
| 52 | 52 | |
| 53 | - /** |
|
| 53 | +/** |
|
| 54 | 54 | * The uploaded file size |
| 55 | 55 | * |
| 56 | 56 | * @var int |
| 57 | 57 | */ |
| 58 | - protected $nSize; |
|
| 58 | +protected $nSize; |
|
| 59 | 59 | |
| 60 | - /** |
|
| 60 | +/** |
|
| 61 | 61 | * The uploaded file extension |
| 62 | 62 | * |
| 63 | 63 | * @var string |
| 64 | 64 | */ |
| 65 | - protected $sExtension; |
|
| 65 | +protected $sExtension; |
|
| 66 | 66 | |
| 67 | - /** |
|
| 67 | +/** |
|
| 68 | 68 | * The filesystem where the file is stored |
| 69 | 69 | * |
| 70 | 70 | * @var Filesystem |
| 71 | 71 | */ |
| 72 | - protected $xFilesystem; |
|
| 72 | +protected $xFilesystem; |
|
| 73 | 73 | |
| 74 | - /** |
|
| 74 | +/** |
|
| 75 | 75 | * Create an instance of this class using data from an uploaded file. |
| 76 | 76 | * |
| 77 | 77 | * @param Filesystem $xFilesystem |
@@ -81,87 +81,87 @@ discard block |
||
| 81 | 81 | * |
| 82 | 82 | * @return File |
| 83 | 83 | */ |
| 84 | - public static function fromHttpFile(Filesystem $xFilesystem, |
|
| 85 | - UploadedFile $xHttpFile, string $sUploadDir, string $sName): File |
|
| 86 | - {
|
|
| 87 | - $xFile = new File(); |
|
| 88 | - $xFile->xFilesystem = $xFilesystem; |
|
| 89 | - $xFile->sType = $xHttpFile->getClientMediaType(); |
|
| 90 | - $xFile->sName = $sName; |
|
| 91 | - $xFile->sFilename = $xHttpFile->getClientFilename(); |
|
| 92 | - $xFile->sExtension = pathinfo($xFile->sFilename, PATHINFO_EXTENSION); |
|
| 93 | - $xFile->nSize = $xHttpFile->getSize(); |
|
| 94 | - $xFile->sPath = $sUploadDir . $xFile->sName . '.' . $xFile->sExtension; |
|
| 95 | - return $xFile; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 84 | +public static function fromHttpFile(Filesystem $xFilesystem, |
|
| 85 | +UploadedFile $xHttpFile, string $sUploadDir, string $sName): File |
|
| 86 | +{
|
|
| 87 | +$xFile = new File(); |
|
| 88 | +$xFile->xFilesystem = $xFilesystem; |
|
| 89 | +$xFile->sType = $xHttpFile->getClientMediaType(); |
|
| 90 | +$xFile->sName = $sName; |
|
| 91 | +$xFile->sFilename = $xHttpFile->getClientFilename(); |
|
| 92 | +$xFile->sExtension = pathinfo($xFile->sFilename, PATHINFO_EXTENSION); |
|
| 93 | +$xFile->nSize = $xHttpFile->getSize(); |
|
| 94 | +$xFile->sPath = $sUploadDir . $xFile->sName . '.' . $xFile->sExtension; |
|
| 95 | +return $xFile; |
|
| 96 | +} |
|
| 97 | + |
|
| 98 | +/** |
|
| 99 | 99 | * Get the filesystem where the file is stored |
| 100 | 100 | * |
| 101 | 101 | * @return Filesystem |
| 102 | 102 | */ |
| 103 | - public function filesystem(): Filesystem |
|
| 104 | - {
|
|
| 105 | - return $this->xFilesystem; |
|
| 106 | - } |
|
| 103 | +public function filesystem(): Filesystem |
|
| 104 | +{
|
|
| 105 | +return $this->xFilesystem; |
|
| 106 | +} |
|
| 107 | 107 | |
| 108 | - /** |
|
| 108 | +/** |
|
| 109 | 109 | * Get the uploaded file type |
| 110 | 110 | * |
| 111 | 111 | * @return string |
| 112 | 112 | */ |
| 113 | - public function type(): string |
|
| 114 | - {
|
|
| 115 | - return $this->sType; |
|
| 116 | - } |
|
| 113 | +public function type(): string |
|
| 114 | +{
|
|
| 115 | +return $this->sType; |
|
| 116 | +} |
|
| 117 | 117 | |
| 118 | - /** |
|
| 118 | +/** |
|
| 119 | 119 | * Get the uploaded file name, without the extension and slugified |
| 120 | 120 | * |
| 121 | 121 | * @return string |
| 122 | 122 | */ |
| 123 | - public function name(): string |
|
| 124 | - {
|
|
| 125 | - return $this->sName; |
|
| 126 | - } |
|
| 123 | +public function name(): string |
|
| 124 | +{
|
|
| 125 | +return $this->sName; |
|
| 126 | +} |
|
| 127 | 127 | |
| 128 | - /** |
|
| 128 | +/** |
|
| 129 | 129 | * Get the uploaded file name, with the extension |
| 130 | 130 | * |
| 131 | 131 | * @return string |
| 132 | 132 | */ |
| 133 | - public function filename(): string |
|
| 134 | - {
|
|
| 135 | - return $this->sFilename; |
|
| 136 | - } |
|
| 133 | +public function filename(): string |
|
| 134 | +{
|
|
| 135 | +return $this->sFilename; |
|
| 136 | +} |
|
| 137 | 137 | |
| 138 | - /** |
|
| 138 | +/** |
|
| 139 | 139 | * Get the uploaded file path |
| 140 | 140 | * |
| 141 | 141 | * @return string |
| 142 | 142 | */ |
| 143 | - public function path(): string |
|
| 144 | - {
|
|
| 145 | - return $this->sPath; |
|
| 146 | - } |
|
| 143 | +public function path(): string |
|
| 144 | +{
|
|
| 145 | +return $this->sPath; |
|
| 146 | +} |
|
| 147 | 147 | |
| 148 | - /** |
|
| 148 | +/** |
|
| 149 | 149 | * Get the uploaded file size |
| 150 | 150 | * |
| 151 | 151 | * @return int |
| 152 | 152 | */ |
| 153 | - public function size(): int |
|
| 154 | - {
|
|
| 155 | - return $this->nSize; |
|
| 156 | - } |
|
| 153 | +public function size(): int |
|
| 154 | +{
|
|
| 155 | +return $this->nSize; |
|
| 156 | +} |
|
| 157 | 157 | |
| 158 | - /** |
|
| 158 | +/** |
|
| 159 | 159 | * Get the uploaded file extension |
| 160 | 160 | * |
| 161 | 161 | * @return string |
| 162 | 162 | */ |
| 163 | - public function extension(): string |
|
| 164 | - {
|
|
| 165 | - return $this->sExtension; |
|
| 166 | - } |
|
| 163 | +public function extension(): string |
|
| 164 | +{
|
|
| 165 | +return $this->sExtension; |
|
| 166 | +} |
|
| 167 | 167 | } |
@@ -4,12 +4,12 @@ |
||
| 4 | 4 | |
| 5 | 5 | interface FileNameInterface |
| 6 | 6 | { |
| 7 | - /** |
|
| 7 | +/** |
|
| 8 | 8 | * Generate a random name for a file or dir |
| 9 | 9 | * |
| 10 | 10 | * @param int $nLength |
| 11 | 11 | * |
| 12 | 12 | * @return string |
| 13 | 13 | */ |
| 14 | - public function random(int $nLength): string; |
|
| 14 | +public function random(int $nLength): string; |
|
| 15 | 15 | } |
@@ -138,8 +138,7 @@ discard block |
||
| 138 | 138 | throw new RequestException($this->xTranslator->trans('errors.upload.access'));
|
| 139 | 139 | } |
| 140 | 140 | return $sUploadDir; |
| 141 | - } |
|
| 142 | - catch(FilesystemException $e) |
|
| 141 | + } catch(FilesystemException $e) |
|
| 143 | 142 | {
|
| 144 | 143 | $this->xLogger->error('Filesystem error.', ['message' => $e->getMessage()]);
|
| 145 | 144 | throw new RequestException($this->xTranslator->trans('errors.upload.access'));
|
@@ -238,8 +237,7 @@ discard block |
||
| 238 | 237 | {
|
| 239 | 238 | $aFiles['user']->filesystem()->write($aFiles['user']->path(), $aFiles['temp']->getStream()); |
| 240 | 239 | } |
| 241 | - } |
|
| 242 | - catch(FilesystemException $e) |
|
| 240 | + } catch(FilesystemException $e) |
|
| 243 | 241 | {
|
| 244 | 242 | $this->xLogger->error('Filesystem error.', ['message' => $e->getMessage()]);
|
| 245 | 243 | throw new RequestException($this->xTranslator->trans('errors.upload.access'));
|
@@ -133,13 +133,13 @@ discard block |
||
| 133 | 133 | try |
| 134 | 134 | {
|
| 135 | 135 | $xFilesystem->createDirectory($sUploadDir); |
| 136 | - if(!$xFilesystem->directoryExists($sUploadDir)) |
|
| 136 | + if (!$xFilesystem->directoryExists($sUploadDir)) |
|
| 137 | 137 | {
|
| 138 | 138 | throw new RequestException($this->xTranslator->trans('errors.upload.access'));
|
| 139 | 139 | } |
| 140 | 140 | return $sUploadDir; |
| 141 | 141 | } |
| 142 | - catch(FilesystemException $e) |
|
| 142 | + catch (FilesystemException $e) |
|
| 143 | 143 | {
|
| 144 | 144 | $this->xLogger->error('Filesystem error.', ['message' => $e->getMessage()]);
|
| 145 | 145 | throw new RequestException($this->xTranslator->trans('errors.upload.access'));
|
@@ -173,7 +173,7 @@ discard block |
||
| 173 | 173 | {
|
| 174 | 174 | // Check the uploaded file validity |
| 175 | 175 | $nErrorCode = $xHttpFile->getError(); |
| 176 | - if($nErrorCode !== UPLOAD_ERR_OK) |
|
| 176 | + if ($nErrorCode !== UPLOAD_ERR_OK) |
|
| 177 | 177 | {
|
| 178 | 178 | $this->xLogger->error('File upload error.', [
|
| 179 | 179 | 'code' => $nErrorCode, |
@@ -184,7 +184,7 @@ discard block |
||
| 184 | 184 | |
| 185 | 185 | // Filename without the extension. Needs to be sanitized. |
| 186 | 186 | $sName = pathinfo($xHttpFile->getClientFilename(), PATHINFO_FILENAME); |
| 187 | - if($this->cNameSanitizer !== null) |
|
| 187 | + if ($this->cNameSanitizer !== null) |
|
| 188 | 188 | {
|
| 189 | 189 | $sName = (string)call_user_func($this->cNameSanitizer, $sName, $sField, $this->sUploadFieldId); |
| 190 | 190 | } |
@@ -192,7 +192,7 @@ discard block |
||
| 192 | 192 | // Set the user file data |
| 193 | 193 | $xFile = File::fromHttpFile($this->xFileStorage->filesystem($sField), $xHttpFile, $sUploadDir, $sName); |
| 194 | 194 | // Verify file validity (format, size) |
| 195 | - if(!$this->xValidator->validateUploadedFile($sField, $xFile)) |
|
| 195 | + if (!$this->xValidator->validateUploadedFile($sField, $xFile)) |
|
| 196 | 196 | {
|
| 197 | 197 | throw new RequestException($this->xValidator->getErrorMessage()); |
| 198 | 198 | } |
@@ -217,16 +217,16 @@ discard block |
||
| 217 | 217 | |
| 218 | 218 | $aUserFiles = []; |
| 219 | 219 | $this->aAllFiles = []; // A flat list of all uploaded files |
| 220 | - foreach($aTempFiles as $sField => $aFiles) |
|
| 220 | + foreach ($aTempFiles as $sField => $aFiles) |
|
| 221 | 221 | {
|
| 222 | 222 | $aUserFiles[$sField] = []; |
| 223 | 223 | // Get the path to the upload dir |
| 224 | 224 | $sUploadDir = $this->getUploadDir($sField); |
| 225 | - if(!is_array($aFiles)) |
|
| 225 | + if (!is_array($aFiles)) |
|
| 226 | 226 | {
|
| 227 | 227 | $aFiles = [$aFiles]; |
| 228 | 228 | } |
| 229 | - foreach($aFiles as $xHttpFile) |
|
| 229 | + foreach ($aFiles as $xHttpFile) |
|
| 230 | 230 | {
|
| 231 | 231 | $aUserFiles[$sField][] = $this->makeUploadedFile($xHttpFile, $sUploadDir, $sField); |
| 232 | 232 | } |
@@ -234,12 +234,12 @@ discard block |
||
| 234 | 234 | // Copy the uploaded files from the temp dir to the user dir |
| 235 | 235 | try |
| 236 | 236 | {
|
| 237 | - foreach($this->aAllFiles as $aFiles) |
|
| 237 | + foreach ($this->aAllFiles as $aFiles) |
|
| 238 | 238 | {
|
| 239 | 239 | $aFiles['user']->filesystem()->write($aFiles['user']->path(), $aFiles['temp']->getStream()); |
| 240 | 240 | } |
| 241 | 241 | } |
| 242 | - catch(FilesystemException $e) |
|
| 242 | + catch (FilesystemException $e) |
|
| 243 | 243 | {
|
| 244 | 244 | $this->xLogger->error('Filesystem error.', ['message' => $e->getMessage()]);
|
| 245 | 245 | throw new RequestException($this->xTranslator->trans('errors.upload.access'));
|
@@ -31,42 +31,42 @@ discard block |
||
| 31 | 31 | |
| 32 | 32 | class UploadManager |
| 33 | 33 | {
|
| 34 | - /** |
|
| 34 | +/** |
|
| 35 | 35 | * The id of the upload field in the form |
| 36 | 36 | * |
| 37 | 37 | * @var string |
| 38 | 38 | */ |
| 39 | - protected $sUploadFieldId = ''; |
|
| 39 | +protected $sUploadFieldId = ''; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 41 | +/** |
|
| 42 | 42 | * A user defined function to transform uploaded file names |
| 43 | 43 | * |
| 44 | 44 | * @var Closure |
| 45 | 45 | */ |
| 46 | - protected $cNameSanitizer = null; |
|
| 46 | +protected $cNameSanitizer = null; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 48 | +/** |
|
| 49 | 49 | * A flat list of all uploaded files |
| 50 | 50 | * |
| 51 | 51 | * @var array |
| 52 | 52 | */ |
| 53 | - private $aAllFiles = []; |
|
| 53 | +private $aAllFiles = []; |
|
| 54 | 54 | |
| 55 | - /** |
|
| 55 | +/** |
|
| 56 | 56 | * @var array |
| 57 | 57 | */ |
| 58 | - private $errorMessages = [ |
|
| 59 | - 0 => 'There is no error, the file uploaded with success', |
|
| 60 | - 1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini', |
|
| 61 | - 2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form', |
|
| 62 | - 3 => 'The uploaded file was only partially uploaded', |
|
| 63 | - 4 => 'No file was uploaded', |
|
| 64 | - 6 => 'Missing a temporary folder', |
|
| 65 | - 7 => 'Failed to write file to disk.', |
|
| 66 | - 8 => 'A PHP extension stopped the file upload.', |
|
| 67 | - ]; |
|
| 68 | - |
|
| 69 | - /** |
|
| 58 | +private $errorMessages = [ |
|
| 59 | +0 => 'There is no error, the file uploaded with success', |
|
| 60 | +1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini', |
|
| 61 | +2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form', |
|
| 62 | +3 => 'The uploaded file was only partially uploaded', |
|
| 63 | +4 => 'No file was uploaded', |
|
| 64 | +6 => 'Missing a temporary folder', |
|
| 65 | +7 => 'Failed to write file to disk.', |
|
| 66 | +8 => 'A PHP extension stopped the file upload.', |
|
| 67 | +]; |
|
| 68 | + |
|
| 69 | +/** |
|
| 70 | 70 | * The constructor |
| 71 | 71 | * |
| 72 | 72 | * @param LoggerInterface $xLogger |
@@ -76,50 +76,50 @@ discard block |
||
| 76 | 76 | * @param FileNameInterface $xFileName |
| 77 | 77 | * @param ConfigManager $xConfigManager |
| 78 | 78 | */ |
| 79 | - public function __construct(private LoggerInterface $xLogger, |
|
| 80 | - private Validator $xValidator, private Translator $xTranslator, |
|
| 81 | - private FileStorage $xFileStorage, private FileNameInterface $xFileName, |
|
| 82 | - private ConfigManager $xConfigManager) |
|
| 83 | - {
|
|
| 84 | - // This feature is not yet implemented |
|
| 85 | - $this->setUploadFieldId('');
|
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 79 | +public function __construct(private LoggerInterface $xLogger, |
|
| 80 | +private Validator $xValidator, private Translator $xTranslator, |
|
| 81 | +private FileStorage $xFileStorage, private FileNameInterface $xFileName, |
|
| 82 | +private ConfigManager $xConfigManager) |
|
| 83 | +{
|
|
| 84 | +// This feature is not yet implemented |
|
| 85 | +$this->setUploadFieldId('');
|
|
| 86 | +} |
|
| 87 | + |
|
| 88 | +/** |
|
| 89 | 89 | * Generate a random name |
| 90 | 90 | * |
| 91 | 91 | * @return string |
| 92 | 92 | */ |
| 93 | - protected function randomName(): string |
|
| 94 | - {
|
|
| 95 | - return $this->xFileName->random(16); |
|
| 96 | - } |
|
| 93 | +protected function randomName(): string |
|
| 94 | +{
|
|
| 95 | +return $this->xFileName->random(16); |
|
| 96 | +} |
|
| 97 | 97 | |
| 98 | - /** |
|
| 98 | +/** |
|
| 99 | 99 | * Set the id of the upload field in the form |
| 100 | 100 | * |
| 101 | 101 | * @param string $sUploadFieldId |
| 102 | 102 | * |
| 103 | 103 | * @return void |
| 104 | 104 | */ |
| 105 | - public function setUploadFieldId(string $sUploadFieldId): void |
|
| 106 | - {
|
|
| 107 | - $this->sUploadFieldId = $sUploadFieldId; |
|
| 108 | - } |
|
| 105 | +public function setUploadFieldId(string $sUploadFieldId): void |
|
| 106 | +{
|
|
| 107 | +$this->sUploadFieldId = $sUploadFieldId; |
|
| 108 | +} |
|
| 109 | 109 | |
| 110 | - /** |
|
| 110 | +/** |
|
| 111 | 111 | * Filter uploaded file name |
| 112 | 112 | * |
| 113 | 113 | * @param Closure $cNameSanitizer The closure which filters filenames |
| 114 | 114 | * |
| 115 | 115 | * @return void |
| 116 | 116 | */ |
| 117 | - public function setNameSanitizer(Closure $cNameSanitizer): void |
|
| 118 | - {
|
|
| 119 | - $this->cNameSanitizer = $cNameSanitizer; |
|
| 120 | - } |
|
| 117 | +public function setNameSanitizer(Closure $cNameSanitizer): void |
|
| 118 | +{
|
|
| 119 | +$this->cNameSanitizer = $cNameSanitizer; |
|
| 120 | +} |
|
| 121 | 121 | |
| 122 | - /** |
|
| 122 | +/** |
|
| 123 | 123 | * Make sure the upload dir exists and is writable |
| 124 | 124 | * |
| 125 | 125 | * @param Filesystem $xFilesystem |
@@ -128,25 +128,25 @@ discard block |
||
| 128 | 128 | * @return string |
| 129 | 129 | * @throws RequestException |
| 130 | 130 | */ |
| 131 | - private function _makeUploadDir(Filesystem $xFilesystem, string $sUploadDir): string |
|
| 132 | - {
|
|
| 133 | - try |
|
| 134 | - {
|
|
| 135 | - $xFilesystem->createDirectory($sUploadDir); |
|
| 136 | - if(!$xFilesystem->directoryExists($sUploadDir)) |
|
| 137 | - {
|
|
| 138 | - throw new RequestException($this->xTranslator->trans('errors.upload.access'));
|
|
| 139 | - } |
|
| 140 | - return $sUploadDir; |
|
| 141 | - } |
|
| 142 | - catch(FilesystemException $e) |
|
| 143 | - {
|
|
| 144 | - $this->xLogger->error('Filesystem error.', ['message' => $e->getMessage()]);
|
|
| 145 | - throw new RequestException($this->xTranslator->trans('errors.upload.access'));
|
|
| 146 | - } |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 131 | +private function _makeUploadDir(Filesystem $xFilesystem, string $sUploadDir): string |
|
| 132 | +{
|
|
| 133 | +try |
|
| 134 | +{
|
|
| 135 | +$xFilesystem->createDirectory($sUploadDir); |
|
| 136 | +if(!$xFilesystem->directoryExists($sUploadDir)) |
|
| 137 | +{
|
|
| 138 | + throw new RequestException($this->xTranslator->trans('errors.upload.access'));
|
|
| 139 | +} |
|
| 140 | +return $sUploadDir; |
|
| 141 | +} |
|
| 142 | +catch(FilesystemException $e) |
|
| 143 | +{
|
|
| 144 | +$this->xLogger->error('Filesystem error.', ['message' => $e->getMessage()]);
|
|
| 145 | +throw new RequestException($this->xTranslator->trans('errors.upload.access'));
|
|
| 146 | +} |
|
| 147 | +} |
|
| 148 | + |
|
| 149 | +/** |
|
| 150 | 150 | * Get the path to the upload dir |
| 151 | 151 | * |
| 152 | 152 | * @param string $sField |
@@ -154,12 +154,12 @@ discard block |
||
| 154 | 154 | * @return string |
| 155 | 155 | * @throws RequestException |
| 156 | 156 | */ |
| 157 | - private function getUploadDir(string $sField): string |
|
| 158 | - {
|
|
| 159 | - return $this->_makeUploadDir($this->xFileStorage->filesystem($sField), $this->randomName() . '/'); |
|
| 160 | - } |
|
| 157 | +private function getUploadDir(string $sField): string |
|
| 158 | +{
|
|
| 159 | +return $this->_makeUploadDir($this->xFileStorage->filesystem($sField), $this->randomName() . '/'); |
|
| 160 | +} |
|
| 161 | 161 | |
| 162 | - /** |
|
| 162 | +/** |
|
| 163 | 163 | * Check uploaded files |
| 164 | 164 | * |
| 165 | 165 | * @param UploadedFile $xHttpFile |
@@ -169,40 +169,40 @@ discard block |
||
| 169 | 169 | * @return File |
| 170 | 170 | * @throws RequestException |
| 171 | 171 | */ |
| 172 | - private function makeUploadedFile(UploadedFile $xHttpFile, string $sUploadDir, string $sField): File |
|
| 173 | - {
|
|
| 174 | - // Check the uploaded file validity |
|
| 175 | - $nErrorCode = $xHttpFile->getError(); |
|
| 176 | - if($nErrorCode !== UPLOAD_ERR_OK) |
|
| 177 | - {
|
|
| 178 | - $this->xLogger->error('File upload error.', [
|
|
| 179 | - 'code' => $nErrorCode, |
|
| 180 | - 'message' => $this->errorMessages[$nErrorCode], |
|
| 181 | - ]); |
|
| 182 | - throw new RequestException($this->xTranslator->trans('errors.upload.failed', ['name' => $sField]));
|
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - // Filename without the extension. Needs to be sanitized. |
|
| 186 | - $sName = pathinfo($xHttpFile->getClientFilename(), PATHINFO_FILENAME); |
|
| 187 | - if($this->cNameSanitizer !== null) |
|
| 188 | - {
|
|
| 189 | - $sName = (string)call_user_func($this->cNameSanitizer, $sName, $sField, $this->sUploadFieldId); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - // Set the user file data |
|
| 193 | - $xFile = File::fromHttpFile($this->xFileStorage->filesystem($sField), $xHttpFile, $sUploadDir, $sName); |
|
| 194 | - // Verify file validity (format, size) |
|
| 195 | - if(!$this->xValidator->validateUploadedFile($sField, $xFile)) |
|
| 196 | - {
|
|
| 197 | - throw new RequestException($this->xValidator->getErrorMessage()); |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - // All's right, save the file for copy. |
|
| 201 | - $this->aAllFiles[] = ['temp' => $xHttpFile, 'user' => $xFile]; |
|
| 202 | - return $xFile; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 172 | +private function makeUploadedFile(UploadedFile $xHttpFile, string $sUploadDir, string $sField): File |
|
| 173 | +{
|
|
| 174 | +// Check the uploaded file validity |
|
| 175 | +$nErrorCode = $xHttpFile->getError(); |
|
| 176 | +if($nErrorCode !== UPLOAD_ERR_OK) |
|
| 177 | +{
|
|
| 178 | +$this->xLogger->error('File upload error.', [
|
|
| 179 | + 'code' => $nErrorCode, |
|
| 180 | + 'message' => $this->errorMessages[$nErrorCode], |
|
| 181 | +]); |
|
| 182 | +throw new RequestException($this->xTranslator->trans('errors.upload.failed', ['name' => $sField]));
|
|
| 183 | +} |
|
| 184 | + |
|
| 185 | +// Filename without the extension. Needs to be sanitized. |
|
| 186 | +$sName = pathinfo($xHttpFile->getClientFilename(), PATHINFO_FILENAME); |
|
| 187 | +if($this->cNameSanitizer !== null) |
|
| 188 | +{
|
|
| 189 | +$sName = (string)call_user_func($this->cNameSanitizer, $sName, $sField, $this->sUploadFieldId); |
|
| 190 | +} |
|
| 191 | + |
|
| 192 | +// Set the user file data |
|
| 193 | +$xFile = File::fromHttpFile($this->xFileStorage->filesystem($sField), $xHttpFile, $sUploadDir, $sName); |
|
| 194 | +// Verify file validity (format, size) |
|
| 195 | +if(!$this->xValidator->validateUploadedFile($sField, $xFile)) |
|
| 196 | +{
|
|
| 197 | +throw new RequestException($this->xValidator->getErrorMessage()); |
|
| 198 | +} |
|
| 199 | + |
|
| 200 | +// All's right, save the file for copy. |
|
| 201 | +$this->aAllFiles[] = ['temp' => $xHttpFile, 'user' => $xFile]; |
|
| 202 | +return $xFile; |
|
| 203 | +} |
|
| 204 | + |
|
| 205 | +/** |
|
| 206 | 206 | * Read uploaded files info from HTTP request data |
| 207 | 207 | * |
| 208 | 208 | * @param ServerRequestInterface $xRequest |
@@ -210,40 +210,40 @@ discard block |
||
| 210 | 210 | * @return array |
| 211 | 211 | * @throws RequestException |
| 212 | 212 | */ |
| 213 | - public function readFromHttpData(ServerRequestInterface $xRequest): array |
|
| 214 | - {
|
|
| 215 | - // Get the uploaded files |
|
| 216 | - $aTempFiles = $xRequest->getUploadedFiles(); |
|
| 217 | - |
|
| 218 | - $aUserFiles = []; |
|
| 219 | - $this->aAllFiles = []; // A flat list of all uploaded files |
|
| 220 | - foreach($aTempFiles as $sField => $aFiles) |
|
| 221 | - {
|
|
| 222 | - $aUserFiles[$sField] = []; |
|
| 223 | - // Get the path to the upload dir |
|
| 224 | - $sUploadDir = $this->getUploadDir($sField); |
|
| 225 | - if(!is_array($aFiles)) |
|
| 226 | - {
|
|
| 227 | - $aFiles = [$aFiles]; |
|
| 228 | - } |
|
| 229 | - foreach($aFiles as $xHttpFile) |
|
| 230 | - {
|
|
| 231 | - $aUserFiles[$sField][] = $this->makeUploadedFile($xHttpFile, $sUploadDir, $sField); |
|
| 232 | - } |
|
| 233 | - } |
|
| 234 | - // Copy the uploaded files from the temp dir to the user dir |
|
| 235 | - try |
|
| 236 | - {
|
|
| 237 | - foreach($this->aAllFiles as $aFiles) |
|
| 238 | - {
|
|
| 239 | - $aFiles['user']->filesystem()->write($aFiles['user']->path(), $aFiles['temp']->getStream()); |
|
| 240 | - } |
|
| 241 | - } |
|
| 242 | - catch(FilesystemException $e) |
|
| 243 | - {
|
|
| 244 | - $this->xLogger->error('Filesystem error.', ['message' => $e->getMessage()]);
|
|
| 245 | - throw new RequestException($this->xTranslator->trans('errors.upload.access'));
|
|
| 246 | - } |
|
| 247 | - return $aUserFiles; |
|
| 248 | - } |
|
| 213 | +public function readFromHttpData(ServerRequestInterface $xRequest): array |
|
| 214 | +{
|
|
| 215 | +// Get the uploaded files |
|
| 216 | +$aTempFiles = $xRequest->getUploadedFiles(); |
|
| 217 | + |
|
| 218 | +$aUserFiles = []; |
|
| 219 | +$this->aAllFiles = []; // A flat list of all uploaded files |
|
| 220 | +foreach($aTempFiles as $sField => $aFiles) |
|
| 221 | +{
|
|
| 222 | +$aUserFiles[$sField] = []; |
|
| 223 | +// Get the path to the upload dir |
|
| 224 | +$sUploadDir = $this->getUploadDir($sField); |
|
| 225 | +if(!is_array($aFiles)) |
|
| 226 | +{
|
|
| 227 | + $aFiles = [$aFiles]; |
|
| 228 | +} |
|
| 229 | +foreach($aFiles as $xHttpFile) |
|
| 230 | +{
|
|
| 231 | + $aUserFiles[$sField][] = $this->makeUploadedFile($xHttpFile, $sUploadDir, $sField); |
|
| 232 | +} |
|
| 233 | +} |
|
| 234 | +// Copy the uploaded files from the temp dir to the user dir |
|
| 235 | +try |
|
| 236 | +{
|
|
| 237 | +foreach($this->aAllFiles as $aFiles) |
|
| 238 | +{
|
|
| 239 | + $aFiles['user']->filesystem()->write($aFiles['user']->path(), $aFiles['temp']->getStream()); |
|
| 240 | +} |
|
| 241 | +} |
|
| 242 | +catch(FilesystemException $e) |
|
| 243 | +{
|
|
| 244 | +$this->xLogger->error('Filesystem error.', ['message' => $e->getMessage()]);
|
|
| 245 | +throw new RequestException($this->xTranslator->trans('errors.upload.access'));
|
|
| 246 | +} |
|
| 247 | +return $aUserFiles; |
|
| 248 | +} |
|
| 249 | 249 | } |
@@ -25,58 +25,58 @@ discard block |
||
| 25 | 25 | |
| 26 | 26 | class UploadHandler implements UploadHandlerInterface |
| 27 | 27 | { |
| 28 | - /** |
|
| 28 | +/** |
|
| 29 | 29 | * The uploaded files copied in the user dir |
| 30 | 30 | * |
| 31 | 31 | * @var array |
| 32 | 32 | */ |
| 33 | - private $aUserFiles = []; |
|
| 33 | +private $aUserFiles = []; |
|
| 34 | 34 | |
| 35 | - /** |
|
| 35 | +/** |
|
| 36 | 36 | * The constructor |
| 37 | 37 | * |
| 38 | 38 | * @param FileStorage $xFileStorage |
| 39 | 39 | * @param UploadManager $xUploadManager |
| 40 | 40 | */ |
| 41 | - public function __construct(private FileStorage $xFileStorage, |
|
| 42 | - private UploadManager $xUploadManager) |
|
| 43 | - {} |
|
| 41 | +public function __construct(private FileStorage $xFileStorage, |
|
| 42 | +private UploadManager $xUploadManager) |
|
| 43 | +{} |
|
| 44 | 44 | |
| 45 | - /** |
|
| 45 | +/** |
|
| 46 | 46 | * Set the uploaded file name sanitizer |
| 47 | 47 | * |
| 48 | 48 | * @param Closure $cSanitizer The closure |
| 49 | 49 | * |
| 50 | 50 | * @return void |
| 51 | 51 | */ |
| 52 | - public function sanitizer(Closure $cSanitizer): void |
|
| 53 | - { |
|
| 54 | - $this->xUploadManager->setNameSanitizer($cSanitizer); |
|
| 55 | - } |
|
| 52 | +public function sanitizer(Closure $cSanitizer): void |
|
| 53 | +{ |
|
| 54 | +$this->xUploadManager->setNameSanitizer($cSanitizer); |
|
| 55 | +} |
|
| 56 | 56 | |
| 57 | - /** |
|
| 57 | +/** |
|
| 58 | 58 | * Get the uploaded files |
| 59 | 59 | * |
| 60 | 60 | * @return array |
| 61 | 61 | */ |
| 62 | - public function files(): array |
|
| 63 | - { |
|
| 64 | - return $this->aUserFiles; |
|
| 65 | - } |
|
| 62 | +public function files(): array |
|
| 63 | +{ |
|
| 64 | +return $this->aUserFiles; |
|
| 65 | +} |
|
| 66 | 66 | |
| 67 | - /** |
|
| 67 | +/** |
|
| 68 | 68 | * Check if the current request contains uploaded files |
| 69 | 69 | * |
| 70 | 70 | * @param ServerRequestInterface $xRequest |
| 71 | 71 | * |
| 72 | 72 | * @return bool |
| 73 | 73 | */ |
| 74 | - public function canProcessRequest(ServerRequestInterface $xRequest): bool |
|
| 75 | - { |
|
| 76 | - return count($xRequest->getUploadedFiles()) > 0; |
|
| 77 | - } |
|
| 74 | +public function canProcessRequest(ServerRequestInterface $xRequest): bool |
|
| 75 | +{ |
|
| 76 | +return count($xRequest->getUploadedFiles()) > 0; |
|
| 77 | +} |
|
| 78 | 78 | |
| 79 | - /** |
|
| 79 | +/** |
|
| 80 | 80 | * Process the uploaded files in the HTTP request |
| 81 | 81 | * |
| 82 | 82 | * @param ServerRequestInterface $xRequest |
@@ -84,21 +84,21 @@ discard block |
||
| 84 | 84 | * @return bool |
| 85 | 85 | * @throws RequestException |
| 86 | 86 | */ |
| 87 | - public function processRequest(ServerRequestInterface $xRequest): bool |
|
| 88 | - { |
|
| 89 | - // Copy the uploaded files from the HTTP request. |
|
| 90 | - $this->aUserFiles = $this->xUploadManager->readFromHttpData($xRequest); |
|
| 91 | - return true; |
|
| 92 | - } |
|
| 87 | +public function processRequest(ServerRequestInterface $xRequest): bool |
|
| 88 | +{ |
|
| 89 | +// Copy the uploaded files from the HTTP request. |
|
| 90 | +$this->aUserFiles = $this->xUploadManager->readFromHttpData($xRequest); |
|
| 91 | +return true; |
|
| 92 | +} |
|
| 93 | 93 | |
| 94 | - /** |
|
| 94 | +/** |
|
| 95 | 95 | * @param string $sStorage |
| 96 | 96 | * @param Closure $cFactory |
| 97 | 97 | * |
| 98 | 98 | * @return void |
| 99 | 99 | */ |
| 100 | - public function registerStorageAdapter(string $sStorage, Closure $cFactory): void |
|
| 101 | - { |
|
| 102 | - $this->xFileStorage->registerAdapter($sStorage, $cFactory); |
|
| 103 | - } |
|
| 100 | +public function registerStorageAdapter(string $sStorage, Closure $cFactory): void |
|
| 101 | +{ |
|
| 102 | +$this->xFileStorage->registerAdapter($sStorage, $cFactory); |
|
| 103 | +} |
|
| 104 | 104 | } |
@@ -50,7 +50,7 @@ |
||
| 50 | 50 | */ |
| 51 | 51 | protected function validateName(string $sName): void |
| 52 | 52 | { |
| 53 | - if(preg_match('/^[a-zA-Z][a-zA-Z0-9_\-\.]*$/', $sName) > 0) |
|
| 53 | + if (preg_match('/^[a-zA-Z][a-zA-Z0-9_\-\.]*$/', $sName) > 0) |
|
| 54 | 54 | { |
| 55 | 55 | return; |
| 56 | 56 | } |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | */ |
| 55 | 55 | protected function validateMethod(string $sMethod): void |
| 56 | 56 | { |
| 57 | - if(preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $sMethod) > 0) |
|
| 57 | + if (preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $sMethod) > 0) |
|
| 58 | 58 | { |
| 59 | 59 | return; |
| 60 | 60 | } |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | public function encode(string $sVarName): array |
| 82 | 82 | { |
| 83 | 83 | $aCalls = []; |
| 84 | - foreach($this->aCalls as $sMethod => $aParams) |
|
| 84 | + foreach ($this->aCalls as $sMethod => $aParams) |
|
| 85 | 85 | { |
| 86 | 86 | $sParams = addslashes(json_encode($aParams)); |
| 87 | 87 | $aCalls[] = "{$sVarName}->addCall('$sMethod', json_decode(\"$sParams\", true));"; |
@@ -45,7 +45,7 @@ |
||
| 45 | 45 | $aFiles = scandir($this->sCacheDir); |
| 46 | 46 | foreach ($aFiles as $sFile) |
| 47 | 47 | { |
| 48 | - if($sFile !== '.' && $sFile !== '..') |
|
| 48 | + if ($sFile !== '.' && $sFile !== '..') |
|
| 49 | 49 | { |
| 50 | 50 | @unlink($this->sCacheDir . DIRECTORY_SEPARATOR . $sFile); |
| 51 | 51 | } |