Tunnel.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Backend\Controller\Adminhtml\Dashboard;
  8. use Magento\Backend\App\Action;
  9. use Magento\Framework\Controller\Result;
  10. use Magento\Framework\Encryption\Helper\Security;
  11. class Tunnel extends \Magento\Backend\Controller\Adminhtml\Dashboard
  12. {
  13. /**
  14. * @var \Magento\Framework\Controller\Result\RawFactory
  15. */
  16. protected $resultRawFactory;
  17. /**
  18. * @param Action\Context $context
  19. * @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
  20. */
  21. public function __construct(
  22. Action\Context $context,
  23. Result\RawFactory $resultRawFactory
  24. ) {
  25. parent::__construct($context);
  26. $this->resultRawFactory = $resultRawFactory;
  27. }
  28. /**
  29. * Forward request for a graph image to the web-service
  30. *
  31. * This is done in order to include the image to a HTTPS-page regardless of web-service settings
  32. *
  33. * @return \Magento\Framework\Controller\Result\Raw
  34. */
  35. public function execute()
  36. {
  37. $error = __('invalid request');
  38. $httpCode = 400;
  39. $gaData = $this->_request->getParam('ga');
  40. $gaHash = $this->_request->getParam('h');
  41. /** @var \Magento\Framework\Controller\Result\Raw $resultRaw */
  42. $resultRaw = $this->resultRawFactory->create();
  43. if ($gaData && $gaHash) {
  44. /** @var $helper \Magento\Backend\Helper\Dashboard\Data */
  45. $helper = $this->_objectManager->get(\Magento\Backend\Helper\Dashboard\Data::class);
  46. $newHash = $helper->getChartDataHash($gaData);
  47. if (Security::compareStrings($newHash, $gaHash)) {
  48. $params = null;
  49. $paramsJson = base64_decode(urldecode($gaData));
  50. if ($paramsJson) {
  51. $params = json_decode($paramsJson, true);
  52. }
  53. if ($params) {
  54. try {
  55. /** @var $httpClient \Magento\Framework\HTTP\ZendClient */
  56. $httpClient = $this->_objectManager->create(\Magento\Framework\HTTP\ZendClient::class);
  57. $response = $httpClient->setUri(
  58. \Magento\Backend\Block\Dashboard\Graph::API_URL
  59. )->setParameterGet(
  60. $params
  61. )->setConfig(
  62. ['timeout' => 5]
  63. )->request(
  64. 'GET'
  65. );
  66. $headers = $response->getHeaders();
  67. $resultRaw->setHeader('Content-type', $headers['Content-type'])
  68. ->setContents($response->getBody());
  69. return $resultRaw;
  70. } catch (\Exception $e) {
  71. $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
  72. $error = __('see error log for details');
  73. $httpCode = 503;
  74. }
  75. }
  76. }
  77. }
  78. $resultRaw->setHeader('Content-Type', 'text/plain; charset=UTF-8')
  79. ->setHttpResponseCode($httpCode)
  80. ->setContents(__('Service unavailable: %1', $error));
  81. return $resultRaw;
  82. }
  83. }