Skip to content

Commit 83745d1

Browse files
merge magento/2.3.6-develop into magento-tsg/2.3.6-develop-pr120
2 parents f528d64 + 6a5ada5 commit 83745d1

File tree

7 files changed

+487
-63
lines changed

7 files changed

+487
-63
lines changed

app/code/Magento/Backend/App/AbstractAction.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
*/
66
namespace Magento\Backend\App;
77

8+
use Magento\Framework\Encryption\Helper\Security;
9+
810
/**
911
* Generic backend controller
1012
*
13+
* phpcs:disable Magento2.Classes.AbstractApi
1114
* @api
1215
* @SuppressWarnings(PHPMD.NumberOfChildren)
1316
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -101,6 +104,8 @@ public function __construct(Action\Context $context)
101104
}
102105

103106
/**
107+
* Checking if the user has access to requested component.
108+
*
104109
* @return bool
105110
*/
106111
protected function _isAllowed()
@@ -119,6 +124,8 @@ protected function _getSession()
119124
}
120125

121126
/**
127+
* Get message manager.
128+
*
122129
* @return \Magento\Framework\Message\ManagerInterface
123130
*/
124131
protected function getMessageManager()
@@ -146,6 +153,8 @@ protected function _setActiveMenu($itemId)
146153
}
147154

148155
/**
156+
* Prepare breadcrumbs.
157+
*
149158
* @param string $label
150159
* @param string $title
151160
* @param string|null $link
@@ -158,6 +167,8 @@ protected function _addBreadcrumb($label, $title, $link = null)
158167
}
159168

160169
/**
170+
* Add content to specified block.
171+
*
161172
* @param \Magento\Framework\View\Element\AbstractBlock $block
162173
* @return $this
163174
*/
@@ -167,6 +178,8 @@ protected function _addContent(\Magento\Framework\View\Element\AbstractBlock $bl
167178
}
168179

169180
/**
181+
* Move block to left container.
182+
*
170183
* @param \Magento\Framework\View\Element\AbstractBlock $block
171184
* @return $this
172185
*/
@@ -176,6 +189,8 @@ protected function _addLeft(\Magento\Framework\View\Element\AbstractBlock $block
176189
}
177190

178191
/**
192+
* Add js to specified block.
193+
*
179194
* @param \Magento\Framework\View\Element\AbstractBlock $block
180195
* @return $this
181196
*/
@@ -200,6 +215,8 @@ private function _moveBlockToContainer(\Magento\Framework\View\Element\AbstractB
200215
}
201216

202217
/**
218+
* Dispatch request.
219+
*
203220
* @param \Magento\Framework\App\RequestInterface $request
204221
* @return \Magento\Framework\App\ResponseInterface
205222
*/
@@ -286,8 +303,7 @@ public function _processUrlKeys()
286303
}
287304

288305
/**
289-
* Set session locale,
290-
* process force locale set through url params
306+
* Set session locale, process force locale set through url params.
291307
*
292308
* @return $this
293309
*/
@@ -309,8 +325,8 @@ protected function _processLocaleSettings()
309325
* Set redirect into response
310326
*
311327
* @TODO MAGETWO-28356: Refactor controller actions to new ResultInterface
312-
* @param string $path
313-
* @param array $arguments
328+
* @param string $path
329+
* @param array $arguments
314330
* @return \Magento\Framework\App\ResponseInterface
315331
*/
316332
protected function _redirect($path, $arguments = [])
@@ -333,7 +349,7 @@ protected function _redirect($path, $arguments = [])
333349
protected function _forward($action, $controller = null, $module = null, array $params = null)
334350
{
335351
$this->_getSession()->setIsUrlNotice($this->_actionFlag->get('', self::FLAG_IS_URLS_CHECKED));
336-
return parent::_forward($action, $controller, $module, $params);
352+
parent::_forward($action, $controller, $module, $params);
337353
}
338354

339355
/**
@@ -360,7 +376,7 @@ protected function _validateSecretKey()
360376
}
361377

362378
$secretKey = $this->getRequest()->getParam(\Magento\Backend\Model\UrlInterface::SECRET_KEY_PARAM_NAME, null);
363-
if (!$secretKey || $secretKey != $this->_backendUrl->getSecretKey()) {
379+
if (!$secretKey || !Security::compareStrings($secretKey, $this->_backendUrl->getSecretKey())) {
364380
return false;
365381
}
366382
return true;

app/code/Magento/Customer/Model/Metadata/Form/Image.php

Lines changed: 88 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,33 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
7+
declare(strict_types=1);
8+
69
namespace Magento\Customer\Model\Metadata\Form;
710

811
use Magento\Customer\Api\AddressMetadataInterface;
912
use Magento\Customer\Api\CustomerMetadataInterface;
13+
use Magento\Customer\Api\Data\AttributeMetadataInterface;
1014
use Magento\Customer\Model\FileProcessor;
15+
use Magento\Customer\Model\FileProcessorFactory;
1116
use Magento\Framework\Api\ArrayObjectSearch;
1217
use Magento\Framework\Api\Data\ImageContentInterface;
1318
use Magento\Framework\Api\Data\ImageContentInterfaceFactory;
1419
use Magento\Framework\App\ObjectManager;
20+
use Magento\Framework\Exception\FileSystemException;
21+
use Magento\Framework\Exception\LocalizedException;
1522
use Magento\Framework\File\UploaderFactory;
1623
use Magento\Framework\Filesystem;
24+
use Magento\Framework\Filesystem\Directory\WriteInterface;
25+
use Magento\Framework\Filesystem\Io\File as IoFileSystem;
26+
use Magento\Framework\App\Filesystem\DirectoryList;
27+
use Magento\Framework\Filesystem\Directory\WriteFactory;
28+
use Magento\Framework\Locale\ResolverInterface;
29+
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
30+
use Magento\Framework\Url\EncoderInterface;
31+
use Magento\MediaStorage\Model\File\Validator\NotProtectedExtension;
32+
use Psr\Log\LoggerInterface;
1733

1834
/**
1935
* Metadata for form image field
@@ -27,38 +43,55 @@ class Image extends File
2743
*/
2844
private $imageContentFactory;
2945

46+
/**
47+
* @var IoFileSystem
48+
*/
49+
private $ioFileSystem;
50+
51+
/**
52+
* @var WriteInterface
53+
*/
54+
private $mediaEntityTmpDirectory;
55+
3056
/**
3157
* Constructor
3258
*
33-
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
34-
* @param \Psr\Log\LoggerInterface $logger
35-
* @param \Magento\Customer\Api\Data\AttributeMetadataInterface $attribute
36-
* @param \Magento\Framework\Locale\ResolverInterface $localeResolver
59+
* @param TimezoneInterface $localeDate
60+
* @param LoggerInterface $logger
61+
* @param AttributeMetadataInterface $attribute
62+
* @param ResolverInterface $localeResolver
3763
* @param null|string $value
3864
* @param string $entityTypeCode
3965
* @param bool $isAjax
40-
* @param \Magento\Framework\Url\EncoderInterface $urlEncoder
41-
* @param \Magento\MediaStorage\Model\File\Validator\NotProtectedExtension $fileValidator
66+
* @param EncoderInterface $urlEncoder
67+
* @param NotProtectedExtension $fileValidator
4268
* @param Filesystem $fileSystem
4369
* @param UploaderFactory $uploaderFactory
44-
* @param \Magento\Customer\Model\FileProcessorFactory|null $fileProcessorFactory
45-
* @param \Magento\Framework\Api\Data\ImageContentInterfaceFactory|null $imageContentInterfaceFactory
70+
* @param FileProcessorFactory|null $fileProcessorFactory
71+
* @param ImageContentInterfaceFactory|null $imageContentInterfaceFactory
72+
* @param IoFileSystem|null $ioFileSystem
73+
* @param DirectoryList|null $directoryList
74+
* @param WriteFactory|null $writeFactory
4675
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
76+
* @throws FileSystemException
4777
*/
4878
public function __construct(
49-
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
50-
\Psr\Log\LoggerInterface $logger,
51-
\Magento\Customer\Api\Data\AttributeMetadataInterface $attribute,
52-
\Magento\Framework\Locale\ResolverInterface $localeResolver,
79+
TimezoneInterface $localeDate,
80+
LoggerInterface $logger,
81+
AttributeMetadataInterface $attribute,
82+
ResolverInterface $localeResolver,
5383
$value,
5484
$entityTypeCode,
5585
$isAjax,
56-
\Magento\Framework\Url\EncoderInterface $urlEncoder,
57-
\Magento\MediaStorage\Model\File\Validator\NotProtectedExtension $fileValidator,
86+
EncoderInterface $urlEncoder,
87+
NotProtectedExtension $fileValidator,
5888
Filesystem $fileSystem,
5989
UploaderFactory $uploaderFactory,
60-
\Magento\Customer\Model\FileProcessorFactory $fileProcessorFactory = null,
61-
\Magento\Framework\Api\Data\ImageContentInterfaceFactory $imageContentInterfaceFactory = null
90+
FileProcessorFactory $fileProcessorFactory = null,
91+
ImageContentInterfaceFactory $imageContentInterfaceFactory = null,
92+
IoFileSystem $ioFileSystem = null,
93+
?DirectoryList $directoryList = null,
94+
?WriteFactory $writeFactory = null
6295
) {
6396
parent::__construct(
6497
$localeDate,
@@ -75,7 +108,16 @@ public function __construct(
75108
$fileProcessorFactory
76109
);
77110
$this->imageContentFactory = $imageContentInterfaceFactory ?: ObjectManager::getInstance()
78-
->get(\Magento\Framework\Api\Data\ImageContentInterfaceFactory::class);
111+
->get(ImageContentInterfaceFactory::class);
112+
$this->ioFileSystem = $ioFileSystem ?: ObjectManager::getInstance()
113+
->get(IoFileSystem::class);
114+
$writeFactory = $writeFactory ?? ObjectManager::getInstance()->get(WriteFactory::class);
115+
$directoryList = $directoryList ?? ObjectManager::getInstance()->get(DirectoryList::class);
116+
$this->mediaEntityTmpDirectory = $writeFactory->create(
117+
$directoryList->getPath($directoryList::MEDIA)
118+
. '/' . $this->_entityTypeCode
119+
. '/' . FileProcessor::TMP_DIR
120+
);
79121
}
80122

81123
/**
@@ -85,6 +127,7 @@ public function __construct(
85127
*
86128
* @param array $value
87129
* @return string[]
130+
* @throws LocalizedException
88131
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
89132
* @SuppressWarnings(PHPMD.NPathComplexity)
90133
*/
@@ -93,7 +136,11 @@ protected function _validateByRules($value)
93136
$label = $value['name'];
94137
$rules = $this->getAttribute()->getValidationRules();
95138

96-
$imageProp = @getimagesize($value['tmp_name']);
139+
try {
140+
$imageProp = getimagesize($value['tmp_name']);
141+
} catch (\Throwable $e) {
142+
$imageProp = false;
143+
}
97144

98145
if (!$this->_isUploadedFile($value['tmp_name']) || !$imageProp) {
99146
return [__('"%1" is not a valid file.', $label)];
@@ -106,9 +153,11 @@ protected function _validateByRules($value)
106153
}
107154

108155
// modify image name
109-
$extension = pathinfo($value['name'], PATHINFO_EXTENSION);
156+
$extension = $this->ioFileSystem->getPathInfo($value['name'])['extension'];
110157
if ($extension != $allowImageTypes[$imageProp[2]]) {
111-
$value['name'] = pathinfo($value['name'], PATHINFO_FILENAME) . '.' . $allowImageTypes[$imageProp[2]];
158+
$value['name'] = $this->ioFileSystem->getPathInfo($value['name'])['filename']
159+
. '.'
160+
. $allowImageTypes[$imageProp[2]];
112161
}
113162

114163
$maxFileSize = ArrayObjectSearch::getArrayElementByName(
@@ -153,6 +202,7 @@ protected function _validateByRules($value)
153202
*
154203
* @param array $value
155204
* @return bool|int|ImageContentInterface|string
205+
* @throws LocalizedException
156206
*/
157207
protected function processUiComponentValue(array $value)
158208
{
@@ -174,32 +224,43 @@ protected function processUiComponentValue(array $value)
174224
*
175225
* @param array $value
176226
* @return string
227+
* @throws LocalizedException
177228
*/
178229
protected function processCustomerAddressValue(array $value)
179230
{
180-
$result = $this->getFileProcessor()->moveTemporaryFile($value['file']);
181-
return $result;
231+
$fileName = $this->mediaEntityTmpDirectory
232+
->getDriver()
233+
->getRealPathSafety(
234+
$this->mediaEntityTmpDirectory->getAbsolutePath(
235+
ltrim(
236+
$value['file'],
237+
'/'
238+
)
239+
)
240+
);
241+
return $this->getFileProcessor()->moveTemporaryFile(
242+
$this->mediaEntityTmpDirectory->getRelativePath($fileName)
243+
);
182244
}
183245

184246
/**
185247
* Process file uploader UI component data for customer entity
186248
*
187249
* @param array $value
188250
* @return bool|int|ImageContentInterface|string
251+
* @throws LocalizedException
189252
*/
190253
protected function processCustomerValue(array $value)
191254
{
192-
$temporaryFile = FileProcessor::TMP_DIR . '/' . ltrim($value['file'], '/');
193-
194-
if ($this->getFileProcessor()->isExist($temporaryFile)) {
255+
$file = ltrim($value['file'], '/');
256+
if ($this->mediaEntityTmpDirectory->isExist($file)) {
257+
$temporaryFile = FileProcessor::TMP_DIR . '/' . $file;
195258
$base64EncodedData = $this->getFileProcessor()->getBase64EncodedData($temporaryFile);
196-
197259
/** @var ImageContentInterface $imageContentDataObject */
198260
$imageContentDataObject = $this->imageContentFactory->create()
199261
->setName($value['name'])
200262
->setBase64EncodedData($base64EncodedData)
201263
->setType($value['type']);
202-
203264
// Remove temporary file
204265
$this->getFileProcessor()->removeUploadedFile($temporaryFile);
205266

0 commit comments

Comments
 (0)