Skip to content

Commit 3b1fb97

Browse files
authored
Merge pull request #3312 from magento-qwerty/2.3-bugfixes-161018
[Qwerty] Bugfixes
2 parents 27b3d48 + bbf59f3 commit 3b1fb97

File tree

12 files changed

+154
-624
lines changed

12 files changed

+154
-624
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Catalog\Model\Product\Option\Type\File;
10+
11+
/**
12+
* Validator for existing (already saved) files.
13+
*/
14+
class ExistingValidate extends \Zend_Validate
15+
{
16+
/**
17+
* @inheritDoc
18+
*
19+
* @param string $value File's full path.
20+
* @param string|null $originalName Original file's name (when uploaded).
21+
*/
22+
public function isValid($value, string $originalName = null)
23+
{
24+
$this->_messages = [];
25+
$this->_errors = [];
26+
27+
if (!is_string($value)) {
28+
$this->_messages[] = __('Full file path is expected.')->render();
29+
return false;
30+
}
31+
32+
$result = true;
33+
$fileInfo = null;
34+
if ($originalName) {
35+
$fileInfo = ['name' => $originalName];
36+
}
37+
foreach ($this->_validators as $element) {
38+
$validator = $element['instance'];
39+
if ($validator->isValid($value, $fileInfo)) {
40+
continue;
41+
}
42+
$result = false;
43+
$messages = $validator->getMessages();
44+
$this->_messages = array_merge($this->_messages, $messages);
45+
$this->_errors = array_merge($this->_errors, array_keys($messages));
46+
if ($element['breakChainOnFailure']) {
47+
break;
48+
}
49+
}
50+
return $result;
51+
}
52+
}

app/code/Magento/Catalog/Model/Product/Option/Type/File/ValidateFactory.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@
66

77
namespace Magento\Catalog\Model\Product\Option\Type\File;
88

9+
/**
10+
* Class ValidateFactory. Creates Validator with type "ExistingValidate"
11+
*/
912
class ValidateFactory
1013
{
1114
/**
15+
* Main factory method
16+
*
1217
* @return \Zend_Validate
1318
*/
1419
public function create()
1520
{
16-
return new \Zend_Validate();
21+
return new ExistingValidate();
1722
}
1823
}

app/code/Magento/Catalog/Model/Product/Option/Type/File/ValidatorFile.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@
1010
use Magento\Framework\App\Filesystem\DirectoryList;
1111
use Magento\Catalog\Model\Product\Exception as ProductException;
1212
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\Math\Random;
14+
use Magento\Framework\App\ObjectManager;
1315

1416
/**
17+
* Validator class. Represents logic for validation file given from product option
18+
*
1519
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1620
*/
1721
class ValidatorFile extends Validator
@@ -63,28 +67,41 @@ class ValidatorFile extends Validator
6367
protected $isImageValidator;
6468

6569
/**
70+
* @var Random
71+
*/
72+
private $random;
73+
74+
/**
75+
* Constructor method
76+
*
6677
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
6778
* @param \Magento\Framework\Filesystem $filesystem
6879
* @param \Magento\Framework\File\Size $fileSize
6980
* @param \Magento\Framework\HTTP\Adapter\FileTransferFactory $httpFactory
7081
* @param \Magento\Framework\Validator\File\IsImage $isImageValidator
82+
* @param Random|null $random
7183
* @throws \Magento\Framework\Exception\FileSystemException
7284
*/
7385
public function __construct(
7486
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
7587
\Magento\Framework\Filesystem $filesystem,
7688
\Magento\Framework\File\Size $fileSize,
7789
\Magento\Framework\HTTP\Adapter\FileTransferFactory $httpFactory,
78-
\Magento\Framework\Validator\File\IsImage $isImageValidator
90+
\Magento\Framework\Validator\File\IsImage $isImageValidator,
91+
Random $random = null
7992
) {
8093
$this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
8194
$this->filesystem = $filesystem;
8295
$this->httpFactory = $httpFactory;
8396
$this->isImageValidator = $isImageValidator;
97+
$this->random = $random
98+
?? ObjectManager::getInstance()->get(Random::class);
8499
parent::__construct($scopeConfig, $filesystem, $fileSize);
85100
}
86101

87102
/**
103+
* Setter method for the product
104+
*
88105
* @param Product $product
89106
* @return $this
90107
*/
@@ -95,6 +112,8 @@ public function setProduct(Product $product)
95112
}
96113

97114
/**
115+
* Validation method
116+
*
98117
* @param \Magento\Framework\DataObject $processingParams
99118
* @param \Magento\Catalog\Model\Product\Option $option
100119
* @return array
@@ -154,16 +173,15 @@ public function validate($processingParams, $option)
154173
$userValue = [];
155174

156175
if ($upload->isUploaded($file) && $upload->isValid($file)) {
157-
$extension = pathinfo(strtolower($fileInfo['name']), PATHINFO_EXTENSION);
158-
159176
$fileName = \Magento\MediaStorage\Model\File\Uploader::getCorrectFileName($fileInfo['name']);
160177
$dispersion = \Magento\MediaStorage\Model\File\Uploader::getDispersionPath($fileName);
161178

162179
$filePath = $dispersion;
163180

164181
$tmpDirectory = $this->filesystem->getDirectoryRead(DirectoryList::SYS_TMP);
165182
$fileHash = md5($tmpDirectory->readFile($tmpDirectory->getRelativePath($fileInfo['tmp_name'])));
166-
$filePath .= '/' . $fileHash . '.' . $extension;
183+
$fileRandomName = $this->random->getRandomString(32);
184+
$filePath .= '/' .$fileRandomName;
167185
$fileFullPath = $this->mediaDirectory->getAbsolutePath($this->quotePath . $filePath);
168186

169187
$upload->addFilter(new \Zend_Filter_File_Rename(['target' => $fileFullPath, 'overwrite' => true]));
@@ -243,6 +261,8 @@ protected function initFilesystem()
243261
}
244262

245263
/**
264+
* Validate contents length method
265+
*
246266
* @return bool
247267
* @todo need correctly name
248268
*/

app/code/Magento/Catalog/Model/Product/Option/Type/File/ValidatorInfo.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
namespace Magento\Catalog\Model\Product\Option\Type\File;
88

9+
/**
10+
* Validator for existing files.
11+
*/
912
class ValidatorInfo extends Validator
1013
{
1114
/**
@@ -34,6 +37,8 @@ class ValidatorInfo extends Validator
3437
protected $fileRelativePath;
3538

3639
/**
40+
* Construct method
41+
*
3742
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
3843
* @param \Magento\Framework\Filesystem $filesystem
3944
* @param \Magento\Framework\File\Size $fileSize
@@ -53,6 +58,8 @@ public function __construct(
5358
}
5459

5560
/**
61+
* Setter method for property "useQuotePath"
62+
*
5663
* @param mixed $useQuotePath
5764
* @return $this
5865
*/
@@ -63,6 +70,8 @@ public function setUseQuotePath($useQuotePath)
6370
}
6471

6572
/**
73+
* Validate method for the option value depends on an option
74+
*
6675
* @param array $optionValue
6776
* @param \Magento\Catalog\Model\Product\Option $option
6877
* @return bool
@@ -90,7 +99,7 @@ public function validate($optionValue, $option)
9099
}
91100

92101
$result = false;
93-
if ($validatorChain->isValid($this->fileFullPath)) {
102+
if ($validatorChain->isValid($this->fileFullPath, $optionValue['title'])) {
94103
$result = $this->rootDirectory->isReadable($this->fileRelativePath)
95104
&& isset($optionValue['secret_key'])
96105
&& $this->buildSecretKey($this->fileRelativePath) == $optionValue['secret_key'];
@@ -109,6 +118,8 @@ public function validate($optionValue, $option)
109118
}
110119

111120
/**
121+
* Method for creation secret key for the given file
122+
*
112123
* @param string $fileRelativePath
113124
* @return string
114125
*/
@@ -118,6 +129,8 @@ protected function buildSecretKey($fileRelativePath)
118129
}
119130

120131
/**
132+
* Calculates path for the file
133+
*
121134
* @param array $optionValue
122135
* @return void
123136
*/

app/code/Magento/Config/App/Config/Type/System.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ public function __construct(
101101
}
102102

103103
/**
104+
* Get config value by path.
105+
*
104106
* System configuration is separated by scopes (default, websites, stores). Configuration of a scope is inherited
105107
* from its parent scope (store inherits website).
106108
*
@@ -121,7 +123,7 @@ public function __construct(
121123
public function get($path = '')
122124
{
123125
if ($path === '') {
124-
$this->data = array_replace_recursive($this->loadAllData(), $this->data);
126+
$this->data = array_replace_recursive($this->data, $this->loadAllData());
125127

126128
return $this->data;
127129
}
@@ -142,7 +144,7 @@ private function getWithParts($path)
142144
if (count($pathParts) === 1 && $pathParts[0] !== ScopeInterface::SCOPE_DEFAULT) {
143145
if (!isset($this->data[$pathParts[0]])) {
144146
$data = $this->readData();
145-
$this->data = array_replace_recursive($data, $this->data);
147+
$this->data = array_replace_recursive($this->data, $this->postProcessor->process($data));
146148
}
147149

148150
return $this->data[$pathParts[0]];
@@ -152,7 +154,12 @@ private function getWithParts($path)
152154

153155
if ($scopeType === ScopeInterface::SCOPE_DEFAULT) {
154156
if (!isset($this->data[$scopeType])) {
155-
$this->data = array_replace_recursive($this->loadDefaultScopeData($scopeType), $this->data);
157+
$this->data = array_replace_recursive(
158+
$this->data,
159+
$scopeData = $this->loadDefaultScopeData($scopeType)
160+
);
161+
$scopeData = $this->postProcessor->process($scopeData);
162+
$this->data = array_replace_recursive($this->data, $scopeData);
156163
}
157164

158165
return $this->getDataByPathParts($this->data[$scopeType], $pathParts);
@@ -162,10 +169,9 @@ private function getWithParts($path)
162169

163170
if (!isset($this->data[$scopeType][$scopeId])) {
164171
$scopeData = $this->loadScopeData($scopeType, $scopeId);
165-
166-
if (!isset($this->data[$scopeType][$scopeId])) {
167-
$this->data = array_replace_recursive($scopeData, $this->data);
168-
}
172+
$this->data = array_replace_recursive($this->data, $scopeData);
173+
$scopeData = $this->postProcessor->process($scopeData);
174+
$this->data = array_replace_recursive($this->data, $scopeData);
169175
}
170176

171177
return isset($this->data[$scopeType][$scopeId])
@@ -186,9 +192,10 @@ private function loadAllData()
186192
$data = $this->readData();
187193
} else {
188194
$data = $this->serializer->unserialize($cachedData);
195+
$this->data = $data;
189196
}
190197

191-
return $data;
198+
return $this->postProcessor->process($data);
192199
}
193200

194201
/**
@@ -243,6 +250,7 @@ private function loadScopeData($scopeType, $scopeId)
243250

244251
/**
245252
* Cache configuration data.
253+
*
246254
* Caches data per scope to avoid reading data for all scopes on every request
247255
*
248256
* @param array $data
@@ -308,9 +316,6 @@ private function getDataByPathParts($data, $pathParts)
308316
private function readData(): array
309317
{
310318
$this->data = $this->reader->read();
311-
$this->data = $this->postProcessor->process(
312-
$this->data
313-
);
314319

315320
return $this->data;
316321
}

0 commit comments

Comments
 (0)