From f0670b6675875620a0a92c57e96d943de3eb6e1b Mon Sep 17 00:00:00 2001 From: Shikha Mishra Date: Thu, 27 Dec 2018 22:12:21 +0530 Subject: [PATCH 01/11] Updated Download.php --- app/code/Magento/Downloadable/Helper/Download.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/code/Magento/Downloadable/Helper/Download.php b/app/code/Magento/Downloadable/Helper/Download.php index 910eaa42f0e84..50f8d757d5537 100644 --- a/app/code/Magento/Downloadable/Helper/Download.php +++ b/app/code/Magento/Downloadable/Helper/Download.php @@ -200,6 +200,9 @@ public function getContentType() return $this->_downloadableFile->getFileType($this->_resourceFile); } } elseif ($this->_linkType == self::LINK_TYPE_URL) { + if(is_array($this->_handle->stat($this->_resourceFile)['type'])){ + return end($this->_handle->stat($this->_resourceFile)['type']); + }else return $this->_handle->stat($this->_resourceFile)['type']; } return $this->_contentType; From 04f6cfedfd6ec39fcbe0d8ae8ae298c2c62f734d Mon Sep 17 00:00:00 2001 From: Shikha Mishra Date: Thu, 27 Dec 2018 22:17:42 +0530 Subject: [PATCH 02/11] Updated Http.php --- lib/internal/Magento/Framework/Filesystem/Driver/Http.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/internal/Magento/Framework/Filesystem/Driver/Http.php b/lib/internal/Magento/Framework/Filesystem/Driver/Http.php index 5c7fdb0630186..c4350c970f9e6 100644 --- a/lib/internal/Magento/Framework/Filesystem/Driver/Http.php +++ b/lib/internal/Magento/Framework/Filesystem/Driver/Http.php @@ -39,6 +39,11 @@ public function isExists($path) if (strpos($status, '302 Found') !== false && isset($headers[1])) { $status = $headers[1]; } + + /* Handling 301 redirection */ + if (strpos($status, '301 Moved Permanently') !== false && isset($headers[1])) { + $status = $headers[1]; + } if (strpos($status, '200 OK') === false) { $result = false; From a89e6ed21e7044fa29d75866d7041af9b209fb7c Mon Sep 17 00:00:00 2001 From: Milind Singh Date: Sat, 29 Dec 2018 15:00:45 +0530 Subject: [PATCH 03/11] Update Download.php --- app/code/Magento/Downloadable/Helper/Download.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Downloadable/Helper/Download.php b/app/code/Magento/Downloadable/Helper/Download.php index 50f8d757d5537..0da899dcbce84 100644 --- a/app/code/Magento/Downloadable/Helper/Download.php +++ b/app/code/Magento/Downloadable/Helper/Download.php @@ -200,10 +200,11 @@ public function getContentType() return $this->_downloadableFile->getFileType($this->_resourceFile); } } elseif ($this->_linkType == self::LINK_TYPE_URL) { - if(is_array($this->_handle->stat($this->_resourceFile)['type'])){ + if (is_array($this->_handle->stat($this->_resourceFile)['type'])) { return end($this->_handle->stat($this->_resourceFile)['type']); - }else - return $this->_handle->stat($this->_resourceFile)['type']; + } else { + return $this->_handle->stat($this->_resourceFile)['type']; + } } return $this->_contentType; } From 7058adb9fba8fb58c6d62ee7f4e1f27f2a7f58fc Mon Sep 17 00:00:00 2001 From: Chris Snedaker Date: Mon, 31 Dec 2018 04:45:25 -0500 Subject: [PATCH 04/11] Updated to reduce overall complexity of function --- .../Magento/Downloadable/Helper/Download.php | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/app/code/Magento/Downloadable/Helper/Download.php b/app/code/Magento/Downloadable/Helper/Download.php index 0da899dcbce84..adb3378f23e46 100644 --- a/app/code/Magento/Downloadable/Helper/Download.php +++ b/app/code/Magento/Downloadable/Helper/Download.php @@ -185,26 +185,23 @@ public function getFileSize() * * @return string */ - public function getContentType() + public function getContentType() { $this->_getHandle(); - if ($this->_linkType == self::LINK_TYPE_FILE) { - if (function_exists( - 'mime_content_type' - ) && ($contentType = mime_content_type( - $this->_workingDirectory->getAbsolutePath($this->_resourceFile) - )) + if ($this->_linkType === self::LINK_TYPE_FILE) { + if (function_exists('mime_content_type') + && ($contentType = mime_content_type( + $this->_workingDirectory->getAbsolutePath($this->_resourceFile) + )) ) { return $contentType; - } else { - return $this->_downloadableFile->getFileType($this->_resourceFile); - } - } elseif ($this->_linkType == self::LINK_TYPE_URL) { - if (is_array($this->_handle->stat($this->_resourceFile)['type'])) { - return end($this->_handle->stat($this->_resourceFile)['type']); - } else { - return $this->_handle->stat($this->_resourceFile)['type']; } + return $this->_downloadableFile->getFileType($this->_resourceFile); + } + if ($this->_linkType === self::LINK_TYPE_URL) { + return (is_array($this->_handle->stat($this->_resourceFile)['type']) + ? end($this->_handle->stat($this->_resourceFile)['type']) + : $this->_handle->stat($this->_resourceFile)['type']); } return $this->_contentType; } From dac6687ed0b2eba02bd3e67913b2929afc940c9c Mon Sep 17 00:00:00 2001 From: Chris Snedaker Date: Mon, 31 Dec 2018 04:51:05 -0500 Subject: [PATCH 05/11] Merged logic and reduced overall complexity of function Note: if() constructions that return boolean values based on conditions, can be simplified to return the condition instead. --- .../Framework/Filesystem/Driver/Http.php | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/lib/internal/Magento/Framework/Filesystem/Driver/Http.php b/lib/internal/Magento/Framework/Filesystem/Driver/Http.php index c4350c970f9e6..55935f2f149ce 100644 --- a/lib/internal/Magento/Framework/Filesystem/Driver/Http.php +++ b/lib/internal/Magento/Framework/Filesystem/Driver/Http.php @@ -27,31 +27,18 @@ class Http extends File * * @param string $path * @return bool - * @throws FileSystemException */ public function isExists($path) { $headers = array_change_key_case(get_headers($this->getScheme() . $path, 1), CASE_LOWER); - $status = $headers[0]; - /* Handling 302 redirection */ - if (strpos($status, '302 Found') !== false && isset($headers[1])) { - $status = $headers[1]; - } - - /* Handling 301 redirection */ - if (strpos($status, '301 Moved Permanently') !== false && isset($headers[1])) { + /* Handling 301 or 302 redirection */ + if (isset($headers[1]) && preg_match('/^30[12]/', $status)) { $status = $headers[1]; } - if (strpos($status, '200 OK') === false) { - $result = false; - } else { - $result = true; - } - - return $result; + return !(strpos($status, '200 OK') === false); } /** From fc90509ec4cac3785de7f2cc537476a87e89e611 Mon Sep 17 00:00:00 2001 From: Shikha Mishra Date: Wed, 2 Jan 2019 21:23:35 +0530 Subject: [PATCH 06/11] Updated Http.php --- lib/internal/Magento/Framework/Filesystem/Driver/Http.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/Filesystem/Driver/Http.php b/lib/internal/Magento/Framework/Filesystem/Driver/Http.php index 55935f2f149ce..46ad773cff201 100644 --- a/lib/internal/Magento/Framework/Filesystem/Driver/Http.php +++ b/lib/internal/Magento/Framework/Filesystem/Driver/Http.php @@ -34,7 +34,7 @@ public function isExists($path) $status = $headers[0]; /* Handling 301 or 302 redirection */ - if (isset($headers[1]) && preg_match('/^30[12]/', $status)) { + if (isset($headers[1]) && preg_match('/30[12]/', $status)) { $status = $headers[1]; } From 71a5f5d302088d638cec73dd7835358beba84f15 Mon Sep 17 00:00:00 2001 From: Shikha Mishra Date: Tue, 22 Jan 2019 00:23:36 +0530 Subject: [PATCH 07/11] updated Download.php --- app/code/Magento/Downloadable/Helper/Download.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Downloadable/Helper/Download.php b/app/code/Magento/Downloadable/Helper/Download.php index adb3378f23e46..58ae9a1050c9d 100644 --- a/app/code/Magento/Downloadable/Helper/Download.php +++ b/app/code/Magento/Downloadable/Helper/Download.php @@ -255,10 +255,19 @@ public function setResource($resourceFile, $linkType = self::LINK_TYPE_FILE) ); } } + + /** + * check header + */ $this->_resourceFile = $resourceFile; + $headers = array_change_key_case(get_headers($this->_resourceFile, 1), CASE_LOWER); + if(isset($headers['location'])){ + $this->_resourceFile = is_array($headers['location']) ? current($headers['location']): + $headers['location']; + } + $this->_linkType = $linkType; - return $this; } From 0fc730bb0c3545b3ff241d097efb74ad160c8e6d Mon Sep 17 00:00:00 2001 From: Prince Patel Date: Wed, 23 Jan 2019 12:25:44 +0530 Subject: [PATCH 08/11] Update Download.php --- app/code/Magento/Downloadable/Helper/Download.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Downloadable/Helper/Download.php b/app/code/Magento/Downloadable/Helper/Download.php index 58ae9a1050c9d..349a22c383eb5 100644 --- a/app/code/Magento/Downloadable/Helper/Download.php +++ b/app/code/Magento/Downloadable/Helper/Download.php @@ -185,7 +185,7 @@ public function getFileSize() * * @return string */ - public function getContentType() + public function getContentType() { $this->_getHandle(); if ($this->_linkType === self::LINK_TYPE_FILE) { From be3a5456339f66f98d2aaa42eebafbe9fcfa389f Mon Sep 17 00:00:00 2001 From: Shikha Mishra Date: Mon, 28 Jan 2019 23:22:46 +0530 Subject: [PATCH 09/11] updated Download.php --- .../Magento/Downloadable/Helper/Download.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/app/code/Magento/Downloadable/Helper/Download.php b/app/code/Magento/Downloadable/Helper/Download.php index 349a22c383eb5..4832773f13d49 100644 --- a/app/code/Magento/Downloadable/Helper/Download.php +++ b/app/code/Magento/Downloadable/Helper/Download.php @@ -256,15 +256,17 @@ public function setResource($resourceFile, $linkType = self::LINK_TYPE_FILE) } } - /** - * check header - */ - $this->_resourceFile = $resourceFile; - $headers = array_change_key_case(get_headers($this->_resourceFile, 1), CASE_LOWER); - if(isset($headers['location'])){ - $this->_resourceFile = is_array($headers['location']) ? current($headers['location']): - $headers['location']; + + /** + * check header for urls + */ + if ($this->_linkType === self::LINK_TYPE_URL) { + $headers = array_change_key_case(get_headers($this->_resourceFile, 1), CASE_LOWER); + if(isset($headers['location'])){ + $this->_resourceFile = is_array($headers['location']) ? current($headers['location']): + $headers['location']; + } } $this->_linkType = $linkType; From e5d04fa7171ad496e6d2836f56e6eff4d8a2e44d Mon Sep 17 00:00:00 2001 From: Shikha Mishra Date: Mon, 28 Jan 2019 23:46:54 +0530 Subject: [PATCH 10/11] Updated Download.php --- app/code/Magento/Downloadable/Helper/Download.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Downloadable/Helper/Download.php b/app/code/Magento/Downloadable/Helper/Download.php index 4832773f13d49..9b91c18a33cbd 100644 --- a/app/code/Magento/Downloadable/Helper/Download.php +++ b/app/code/Magento/Downloadable/Helper/Download.php @@ -261,7 +261,7 @@ public function setResource($resourceFile, $linkType = self::LINK_TYPE_FILE) /** * check header for urls */ - if ($this->_linkType === self::LINK_TYPE_URL) { + if ($linkType === self::LINK_TYPE_URL) { $headers = array_change_key_case(get_headers($this->_resourceFile, 1), CASE_LOWER); if(isset($headers['location'])){ $this->_resourceFile = is_array($headers['location']) ? current($headers['location']): From 9817c50262fcdd45246bf59475795689e7cbca30 Mon Sep 17 00:00:00 2001 From: nmalevanec Date: Mon, 11 Feb 2019 12:36:21 +0200 Subject: [PATCH 11/11] Fix static test. --- app/code/Magento/Downloadable/Helper/Download.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Downloadable/Helper/Download.php b/app/code/Magento/Downloadable/Helper/Download.php index 9b91c18a33cbd..e98d400794dd1 100644 --- a/app/code/Magento/Downloadable/Helper/Download.php +++ b/app/code/Magento/Downloadable/Helper/Download.php @@ -15,6 +15,7 @@ /** * Downloadable Products Download Helper * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + * @SuppressWarnings(PHPMD.CookieAndSessionMisuse) */ class Download extends \Magento\Framework\App\Helper\AbstractHelper { @@ -263,9 +264,9 @@ public function setResource($resourceFile, $linkType = self::LINK_TYPE_FILE) */ if ($linkType === self::LINK_TYPE_URL) { $headers = array_change_key_case(get_headers($this->_resourceFile, 1), CASE_LOWER); - if(isset($headers['location'])){ - $this->_resourceFile = is_array($headers['location']) ? current($headers['location']): - $headers['location']; + if (isset($headers['location'])) { + $this->_resourceFile = is_array($headers['location']) ? current($headers['location']) + : $headers['location']; } }