Skip to content

Commit f0831d2

Browse files
committed
Merge remote-tracking branch 'upstream/2.4-develop' into B2B-2024
2 parents cc510a3 + fea0bba commit f0831d2

File tree

20 files changed

+1109
-325
lines changed

20 files changed

+1109
-325
lines changed

app/code/Magento/AwsS3/Driver/AwsS3.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* Driver for AWS S3 IO operations.
2626
*
2727
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
28+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2829
*/
2930
class AwsS3 implements RemoteDriverInterface
3031
{
@@ -258,6 +259,7 @@ public function filePutContents($path, $content, $mode = null): int
258259
$path = $this->normalizeRelativePath($path, true);
259260
$config = self::CONFIG;
260261

262+
// phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
261263
if (false !== ($imageSize = @getimagesizefromstring($content))) {
262264
$config['Metadata'] = [
263265
'image-width' => $imageSize[0],
@@ -295,19 +297,20 @@ public function readDirectory($path): array
295297
*/
296298
public function getRealPathSafety($path)
297299
{
300+
//Removing redundant directory separators
301+
$path = preg_replace(
302+
'~(?<!:)\/\/+~',
303+
'/',
304+
$path
305+
);
306+
298307
if (strpos($path, '/.') === false) {
299308
return $path;
300309
}
301310

302311
$isAbsolute = strpos($path, $this->normalizeAbsolutePath('')) === 0;
303312
$path = $this->normalizeRelativePath($path);
304313

305-
//Removing redundant directory separators.
306-
$path = preg_replace(
307-
'/\\/\\/+/',
308-
'/',
309-
$path
310-
);
311314
$pathParts = explode('/', $path);
312315
if (end($pathParts) === '.') {
313316
$pathParts[count($pathParts) - 1] = '';
@@ -488,8 +491,7 @@ public function getRelativePath($basePath, $path = null): string
488491
$basePath = (string)$basePath;
489492
$path = (string)$path;
490493

491-
if (
492-
($basePath && $path)
494+
if ($basePath && $path
493495
&& ($basePath === $path . '/' || strpos($path, $basePath) === 0)
494496
) {
495497
$result = substr($path, strlen($basePath));
@@ -722,6 +724,7 @@ public function fileGetCsv($resource, $length = 0, $delimiter = ',', $enclosure
722724
*/
723725
public function fileTell($resource): int
724726
{
727+
// phpcs:ignore Magento2.Functions.DiscouragedFunction, Generic.PHP.NoSilencedErrors.Discouraged
725728
$result = @ftell($resource);
726729
if ($result === null) {
727730
throw new FileSystemException(
@@ -737,6 +740,7 @@ public function fileTell($resource): int
737740
*/
738741
public function fileSeek($resource, $offset, $whence = SEEK_SET): int
739742
{
743+
// phpcs:ignore Magento2.Functions.DiscouragedFunction, Generic.PHP.NoSilencedErrors.Discouraged
740744
$result = @fseek($resource, $offset, $whence);
741745
if ($result === -1) {
742746
throw new FileSystemException(
@@ -755,6 +759,7 @@ public function fileSeek($resource, $offset, $whence = SEEK_SET): int
755759
*/
756760
public function endOfFile($resource): bool
757761
{
762+
// phpcs:ignore Magento2.Functions.DiscouragedFunction.DiscouragedWithAlternative
758763
return feof($resource);
759764
}
760765

@@ -772,6 +777,7 @@ public function filePutCsv($resource, array $data, $delimiter = ',', $enclosure
772777
*/
773778
public function fileFlush($resource): bool
774779
{
780+
// phpcs:ignore Magento2.Functions.DiscouragedFunction, Generic.PHP.NoSilencedErrors.Discouraged
775781
$result = @fflush($resource);
776782
if (!$result) {
777783
throw new FileSystemException(
@@ -790,6 +796,7 @@ public function fileFlush($resource): bool
790796
*/
791797
public function fileLock($resource, $lockMode = LOCK_EX): bool
792798
{
799+
// phpcs:ignore Magento2.Functions.DiscouragedFunction, Generic.PHP.NoSilencedErrors.Discouraged
793800
$result = @flock($resource, $lockMode);
794801
if (!$result) {
795802
throw new FileSystemException(
@@ -808,6 +815,7 @@ public function fileLock($resource, $lockMode = LOCK_EX): bool
808815
*/
809816
public function fileUnlock($resource): bool
810817
{
818+
// phpcs:ignore Magento2.Functions.DiscouragedFunction, Generic.PHP.NoSilencedErrors.Discouraged
811819
$result = @flock($resource, LOCK_UN);
812820
if (!$result) {
813821
throw new FileSystemException(
@@ -851,13 +859,14 @@ public function fileClose($resource): bool
851859
//phpcs:enable
852860

853861
foreach ($this->streams as $path => $stream) {
854-
// phpcs:ignore Magento2.Functions.DiscouragedFunction
862+
// phpcs:ignore
855863
if (stream_get_meta_data($stream)['uri'] === $resourcePath) {
856864
$this->adapter->writeStream($path, $resource, new Config(self::CONFIG));
857865

858866
// Remove path from streams after
859867
unset($this->streams[$path]);
860868

869+
// phpcs:ignore Magento2.Functions.DiscouragedFunction.DiscouragedWithAlternative
861870
return fclose($stream);
862871
}
863872
}
@@ -931,6 +940,7 @@ private function readPath(string $path, $isRecursive = false): array
931940
if (!empty($path)
932941
&& $path !== $relativePath
933942
&& (!$relativePath || strpos($path, $relativePath) === 0)) {
943+
//phpcs:ignore Magento2.Functions.DiscouragedFunction
934944
$itemsList[] = $this->getAbsolutePath(dirname($path), $path);
935945
}
936946
}

app/code/Magento/AwsS3/Test/Unit/Driver/AwsS3Test.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public function testGetAbsolutePath($basePath, $path, string $expected): void
6464

6565
/**
6666
* @return array
67+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
6768
*/
6869
public function getAbsolutePathDataProvider(): array
6970
{
@@ -407,6 +408,18 @@ public function getRealPathSafetyDataProvider(): array
407408
[
408409
'test/test/../test.txt',
409410
'test/test.txt'
411+
],
412+
[
413+
'test//test/../test.txt',
414+
'test/test.txt'
415+
],
416+
[
417+
'test1///test2/..//test3//test.txt',
418+
'test1/test3/test.txt'
419+
],
420+
[
421+
self::URL . '/test1///test2/..//test3//test.txt',
422+
self::URL . 'test1/test3/test.txt'
410423
]
411424
];
412425
}

app/code/Magento/Cron/Observer/ProcessCronQueueObserver.php

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@
99
*/
1010
namespace Magento\Cron\Observer;
1111

12+
use Exception;
13+
use Magento\Cron\Model\DeadlockRetrierInterface;
1214
use Magento\Cron\Model\ResourceModel\Schedule\Collection as ScheduleCollection;
1315
use Magento\Cron\Model\Schedule;
1416
use Magento\Framework\App\State;
1517
use Magento\Framework\Console\Cli;
18+
use Magento\Framework\Event\Observer;
1619
use Magento\Framework\Event\ObserverInterface;
1720
use Magento\Framework\Exception\CronException;
21+
use Magento\Framework\Exception\LocalizedException;
1822
use Magento\Framework\Profiler\Driver\Standard\Stat;
1923
use Magento\Framework\Profiler\Driver\Standard\StatFactory;
20-
use Magento\Cron\Model\DeadlockRetrierInterface;
24+
use Throwable;
2125

2226
/**
2327
* The observer for processing cron jobs.
@@ -226,13 +230,15 @@ public function __construct(
226230
* Generate tasks schedule
227231
* Cleanup tasks schedule
228232
*
229-
* @param \Magento\Framework\Event\Observer $observer
233+
* @param Observer $observer
234+
*
230235
* @return void
236+
* @throws LocalizedException
231237
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
232238
* @SuppressWarnings(PHPMD.NPathComplexity)
233239
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
234240
*/
235-
public function execute(\Magento\Framework\Event\Observer $observer)
241+
public function execute(Observer $observer)
236242
{
237243
$currentTime = $this->dateTime->gmtTimestamp();
238244
$jobGroupsRoot = $this->_config->getJobs();
@@ -311,8 +317,9 @@ private function lockGroup(string $groupId, callable $callback): void
311317
* @param string[] $jobConfig
312318
* @param Schedule $schedule
313319
* @param string $groupId
320+
*
314321
* @return void
315-
* @throws \Exception
322+
* @throws Exception|Throwable
316323
*/
317324
protected function _runJob($scheduledTime, $currentTime, $jobConfig, $schedule, $groupId)
318325
{
@@ -322,25 +329,29 @@ protected function _runJob($scheduledTime, $currentTime, $jobConfig, $schedule,
322329
if ($scheduledTime < $currentTime - $scheduleLifetime) {
323330
$schedule->setStatus(Schedule::STATUS_MISSED);
324331
// phpcs:ignore Magento2.Exceptions.DirectThrow
325-
throw new \Exception(sprintf('Cron Job %s is missed at %s', $jobCode, $schedule->getScheduledAt()));
332+
throw new Exception(sprintf('Cron Job %s is missed at %s', $jobCode, $schedule->getScheduledAt()));
326333
}
327334

328335
if (!isset($jobConfig['instance'], $jobConfig['method'])) {
329336
$schedule->setStatus(Schedule::STATUS_ERROR);
330337
// phpcs:ignore Magento2.Exceptions.DirectThrow
331-
throw new \Exception(sprintf('No callbacks found for cron job %s', $jobCode));
338+
throw new Exception(sprintf('No callbacks found for cron job %s', $jobCode));
332339
}
333340
$model = $this->_objectManager->create($jobConfig['instance']);
334341
$callback = [$model, $jobConfig['method']];
335342
if (!is_callable($callback)) {
336343
$schedule->setStatus(Schedule::STATUS_ERROR);
337344
// phpcs:ignore Magento2.Exceptions.DirectThrow
338-
throw new \Exception(
339-
sprintf('Invalid callback: %s::%s can\'t be called', $jobConfig['instance'], $jobConfig['method'])
345+
throw new Exception(
346+
sprintf(
347+
'Invalid callback: %s::%s can\'t be called',
348+
$jobConfig['instance'],
349+
$jobConfig['method']
350+
)
340351
);
341352
}
342353

343-
$schedule->setExecutedAt(strftime('%Y-%m-%d %H:%M:%S', $this->dateTime->gmtTimestamp()));
354+
$schedule->setExecutedAt(date('Y-m-d H:i:s', $this->dateTime->gmtTimestamp()));
344355
$this->retrier->execute(
345356
function () use ($schedule) {
346357
$schedule->save();
@@ -355,7 +366,7 @@ function () use ($schedule) {
355366
$this->logger->info(sprintf('Cron Job %s is run', $jobCode));
356367
//phpcs:ignore Magento2.Functions.DiscouragedFunction
357368
call_user_func_array($callback, [$schedule]);
358-
} catch (\Throwable $e) {
369+
} catch (Throwable $e) {
359370
$schedule->setStatus(Schedule::STATUS_ERROR);
360371
$this->logger->error(
361372
sprintf(
@@ -380,8 +391,8 @@ function () use ($schedule) {
380391
$schedule->setStatus(
381392
Schedule::STATUS_SUCCESS
382393
)->setFinishedAt(
383-
strftime(
384-
'%Y-%m-%d %H:%M:%S',
394+
date(
395+
'Y-m-d H:i:s',
385396
$this->dateTime->gmtTimestamp()
386397
)
387398
);
@@ -607,14 +618,16 @@ protected function getConfigSchedule($jobConfig)
607618
* @param string $cronExpression
608619
* @param int $timeInterval
609620
* @param array $exists
621+
*
610622
* @return void
623+
* @throws Exception
611624
*/
612625
protected function saveSchedule($jobCode, $cronExpression, $timeInterval, $exists)
613626
{
614627
$currentTime = $this->dateTime->gmtTimestamp();
615628
$timeAhead = $currentTime + $timeInterval;
616629
for ($time = $currentTime; $time < $timeAhead; $time += self::SECONDS_IN_MINUTE) {
617-
$scheduledAt = strftime('%Y-%m-%d %H:%M:00', $time);
630+
$scheduledAt = date('Y-m-d H:i:00', $time);
618631
$alreadyScheduled = !empty($exists[$jobCode . '/' . $scheduledAt]);
619632
$schedule = $this->createSchedule($jobCode, $cronExpression, $time);
620633
$valid = $schedule->trySchedule();
@@ -648,8 +661,8 @@ protected function createSchedule($jobCode, $cronExpression, $time)
648661
->setCronExpr($cronExpression)
649662
->setJobCode($jobCode)
650663
->setStatus(Schedule::STATUS_PENDING)
651-
->setCreatedAt(strftime('%Y-%m-%d %H:%M:%S', $this->dateTime->gmtTimestamp()))
652-
->setScheduledAt(strftime('%Y-%m-%d %H:%M', $time));
664+
->setCreatedAt(date('Y-m-d H:i:s', $this->dateTime->gmtTimestamp()))
665+
->setScheduledAt(date('Y-m-d H:i', $time));
653666

654667
return $schedule;
655668
}

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

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Magento\Framework\Exception\LocalizedException;
2323
use Magento\Framework\File\UploaderFactory;
2424
use Magento\Framework\Filesystem;
25+
use Magento\Framework\Filesystem\Directory\ReadInterface;
2526
use Magento\Framework\Filesystem\Directory\WriteFactory;
2627
use Magento\Framework\Filesystem\Directory\WriteInterface;
2728
use Magento\Framework\Filesystem\Io\File as IoFileSystem;
@@ -48,10 +49,15 @@ class Image extends File
4849
*/
4950
private $ioFileSystem;
5051

52+
/**
53+
* @var ReadInterface
54+
*/
55+
private $mediaEntityTmpReadDirectory;
56+
5157
/**
5258
* @var WriteInterface
5359
*/
54-
private $mediaEntityTmpDirectory;
60+
private $mediaWriteDirectory;
5561

5662
/**
5763
* @param TimezoneInterface $localeDate
@@ -71,6 +77,7 @@ class Image extends File
7177
* @param DirectoryList|null $directoryList
7278
* @param WriteFactory|null $writeFactory
7379
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
80+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
7481
* @throws FileSystemException
7582
*/
7683
public function __construct(
@@ -109,12 +116,10 @@ public function __construct(
109116
->get(ImageContentInterfaceFactory::class);
110117
$this->ioFileSystem = $ioFileSystem ?: ObjectManager::getInstance()
111118
->get(IoFileSystem::class);
112-
$writeFactory = $writeFactory ?? ObjectManager::getInstance()->get(WriteFactory::class);
113-
$directoryList = $directoryList ?? ObjectManager::getInstance()->get(DirectoryList::class);
114-
$this->mediaEntityTmpDirectory = $writeFactory->create(
115-
$directoryList->getPath($directoryList::MEDIA)
116-
. '/' . $this->_entityTypeCode
117-
. '/' . FileProcessor::TMP_DIR
119+
$this->mediaWriteDirectory = $fileSystem->getDirectoryWrite(DirectoryList::MEDIA);
120+
$this->mediaEntityTmpReadDirectory = $fileSystem->getDirectoryReadByPath(
121+
$this->mediaWriteDirectory->getAbsolutePath() . $this->_entityTypeCode
122+
. DIRECTORY_SEPARATOR . FileProcessor::TMP_DIR . DIRECTORY_SEPARATOR
118123
);
119124
}
120125

@@ -135,6 +140,7 @@ protected function _validateByRules($value)
135140
$rules = $this->getAttribute()->getValidationRules();
136141

137142
try {
143+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
138144
$imageProp = getimagesize($value['tmp_name']);
139145
} catch (\Throwable $e) {
140146
$imageProp = false;
@@ -224,18 +230,18 @@ protected function processUiComponentValue(array $value)
224230
*/
225231
protected function processCustomerAddressValue(array $value)
226232
{
227-
$fileName = $this->mediaEntityTmpDirectory
233+
$fileName = $this->mediaWriteDirectory
228234
->getDriver()
229235
->getRealPathSafety(
230-
$this->mediaEntityTmpDirectory->getAbsolutePath(
236+
$this->mediaEntityTmpReadDirectory->getAbsolutePath(
231237
ltrim(
232238
$value['file'],
233239
'/'
234240
)
235241
)
236242
);
237243
return $this->getFileProcessor()->moveTemporaryFile(
238-
$this->mediaEntityTmpDirectory->getRelativePath($fileName)
244+
$this->mediaEntityTmpReadDirectory->getRelativePath($fileName)
239245
);
240246
}
241247

@@ -249,7 +255,7 @@ protected function processCustomerAddressValue(array $value)
249255
protected function processCustomerValue(array $value)
250256
{
251257
$file = ltrim($value['file'], '/');
252-
if ($this->mediaEntityTmpDirectory->isExist($file)) {
258+
if ($this->mediaEntityTmpReadDirectory->isExist($file)) {
253259
$temporaryFile = FileProcessor::TMP_DIR . '/' . $file;
254260
$base64EncodedData = $this->getFileProcessor()->getBase64EncodedData($temporaryFile);
255261
/** @var ImageContentInterface $imageContentDataObject */

0 commit comments

Comments
 (0)