Skip to content

Commit

Permalink
Merge pull request #12 from yugeshramteke/master
Browse files Browse the repository at this point in the history
Resolved issue with HTTP headers
  • Loading branch information
yugeshramteke authored Nov 16, 2016
2 parents 10581f3 + be5883e commit 3e3186a
Show file tree
Hide file tree
Showing 2 changed files with 251 additions and 12 deletions.
217 changes: 217 additions & 0 deletions src/app/code/community/Radial/FraudInsight/Helper/Http.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
<?php
/**
* Copyright (c) 2013-2016 Radial, Inc.
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* @copyright Copyright (c) 2013-2016 Radial, Inc. (http://www.radial.com/)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

class Radial_FraudInsight_Helper_Http extends Mage_Core_Helper_Http
{
const COOKIES_DELIMITER = ';';

/** @var Mage_Core_Model_Cookie */
protected $_cookie;

/**
* inject dependencies
* @param array
*/
public function __construct(array $args = [])
{
list($this->_cookie) =
$this->_checkTypes(
$this->_nullCoalesce('cookie', $args, Mage::getSingleton('core/cookie'))
);
}

/**
* return $ar[$key] if it exists otherwise return $default
* @param string
* @param array
* @param mixed
* @return mixed
*/
protected function _nullCoalesce($key, array $ar, $default)
{
return isset($ar[$key]) ? $ar[$key] : $default;
}

/**
* ensure correct types
* @param Mage_Core_Model_Cookie
* @return array
*/
protected function _checkTypes(Mage_Core_Model_Cookie $cookie)
{
return [$cookie];
}

public function getHttpHost($clean = true)
{
return $this->_getHttpCleanValue('HTTP_HOST', $clean);
}

public function getHttpOrigin($clean = true)
{
return $this->_getHttpCleanValue('HTTP_ORIGIN', $clean);
}

public function getHttpXPrototypeVersion($clean = true )
{
return $this->_getHttpCleanValue('HTTP_X_PROTOTYPE_VERSION', $clean);
}

public function getHttpXRequestedWith($clean = true )
{
return $this->_getHttpCleanValue('HTTP_X_REQUESTED_WITH', $clean);
}

public function getHttpUserAgent($clean = true )
{
return $this->_getHttpCleanValue('HTTP_USER_AGENT', $clean);
}

public function getHttpCookie($clean = true )
{
return $this->_getHttpCleanValue('HTTP_COOKIE', $clean);
}

public function getHttpXForwardedProto($clean = true )
{
return $this->_getHttpCleanValue('HTTP_X_FORWARDED_PROTO', $clean);
}

public function getHttpXForwardedFor($clean = true )
{
return $this->_getHttpCleanValue('HTTP_X_FORWARDED_FOR', $clean);
}

public function getHttpContentType($clean = true )
{
return $this->_getHttpCleanValue('HTTP_CONTENT_TYPE', $clean);
}

/**
* Retrieve HTTP Accept header
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
* @param bool $clean clean non UTF-8 characters
* @return string
*/
public function getHttpAccept($clean = true)
{
return $this->_getHttpCleanValue('HTTP_ACCEPT', $clean);
}

/**
* Retrieve HTTP Accept-Encoding header
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
* @param bool $clean clean non UTF-8 characters
* @return string
*/
public function getHttpAcceptEncoding($clean = true)
{
return $this->_getHttpCleanValue('HTTP_ACCEPT_ENCODING', $clean);
}

/**
* Retrieve HTTP Accept-Language header
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
* @param bool $clean clean non UTF-8 characters
* @return string
*/
public function getHttpAcceptLanguage($clean = true)
{
return $this->_getHttpCleanValue('HTTP_ACCEPT_LANGUAGE', $clean);
}

/**
* Retrieve HTTP Connection header
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.10
* @param bool $clean clean non UTF-8 characters
* @return string
*/
public function getHttpConnection($clean = true)
{
return $this->_getHttpCleanValue('HTTP_CONNECTION', $clean);
}

public function getHttpReferrer($clean = true )
{
return $this->_getHttpCleanValue('HTTP_REFERER', $clean);
}

public function getHttpAcceptCharset($clean = true)
{
return $this->_getHttpCleanValue('HTTP_ACCEPT_CHARSET', $clean);
}

/**
* Retrieve the remote client's host name
*
* @return string
*/
public function getRemoteHost()
{
return gethostbyaddr($this->getRemoteAddr(false));
}

/**
* return a string representation of the given cookie array
* @return string
*/
public function getCookiesString()
{
$cookies = $this->_cookie->get();
return implode(self::COOKIES_DELIMITER, array_map(function ($key, $value) {
return "$key=$value";
}, array_keys($cookies), $cookies));
}

/**
* get url to our JavaScript
* @return string
*/
public function getJscUrl()
{
return $this->_jscUrl;
}

/**
* Find the generated JS data from the given request's POST data. This uses
* a known form field in the POST data, self::JSC_FIELD_NAME, to find the
* form field populated by the JS collector. As the form field populated is
* selected at random, this mapping is the only way to find the data
* populated by the collector.
* @param Mage_Core_Controller_Request_Http
* @return string
*/
public function getJavaScriptFraudData()
{
$request = $this->_getRequest();
return $request->getPost($request->getPost(static::JSC_FIELD_NAME, ''), '');
}

/**
* Get all header data.
*
* @return array
*/
public function getHeaderData()
{
$headers = array();
foreach ($_SERVER as $name => $value) {
if (substr($name, 0, 5) == 'HTTP_') {
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
return $headers;
}
}
46 changes: 34 additions & 12 deletions src/app/code/community/Radial/FraudInsight/Model/Build/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class Radial_FraudInsight_Model_Build_Request
protected $_quote;
/** @var Radial_FraudInsight_Helper_Data */
protected $_helper;
/** @var Radial_FraudInsight_Helper_Http */
protected $_httpHelper;
/** @var Radial_FraudInsight_Helper_Config */
protected $_config;
/** @var Mage_Catalog_Model_Product */
Expand All @@ -43,17 +45,19 @@ class Radial_FraudInsight_Model_Build_Request
* - 'order' => Mage_Sales_Model_Order
* - 'quote' => Mage_Sales_Model_Quote
* - 'helper' => Radial_FraudInsight_Helper_Data
* - 'httpHelper' => Radial_FraudInsight_Helper_HTTP
* - 'config' => Radial_FraudInsight_Helper_Config
* - 'product' => Mage_Catalog_Model_Product
*/
public function __construct(array $initParams=array())
{
list($this->_request, $this->_insight, $this->_order, $this->_quote, $this->_helper, $this->_config, $this->_product) = $this->_checkTypes(
list($this->_request, $this->_insight, $this->_order, $this->_quote, $this->_helper, $this->_httpHelper, $this->_config, $this->_product) = $this->_checkTypes(
$this->_nullCoalesce($initParams, 'request', $this->_getNewSdkInstance('Radial_FraudInsight_Sdk_Request')),
$this->_nullCoalesce($initParams, 'insight', Mage::getModel('radial_fraudinsight/risk_insight')),
$this->_nullCoalesce($initParams, 'order', $initParams['order']),
$this->_nullCoalesce($initParams, 'quote', Mage::getModel('sales/quote')),
$this->_nullCoalesce($initParams, 'helper', Mage::helper('radial_fraudinsight')),
$this->_nullCoalesce($initParams, 'httpHelper', Mage::helper('radial_fraudinsight/http')),
$this->_nullCoalesce($initParams, 'config', Mage::helper('radial_fraudinsight/config')),
$this->_nullCoalesce($initParams, 'product', Mage::getModel('catalog/product'))
);
Expand All @@ -67,6 +71,7 @@ public function __construct(array $initParams=array())
* @param Mage_Sales_Model_Order
* @param Mage_Sales_Model_Quote
* @param Radial_FraudInsight_Helper_Data
* @param Radial_FraudInsight_Helper_Http
* @param Radial_FraudInsight_Helper_Config
* @param Mage_Catalog_Model_Product
* @return array
Expand All @@ -77,10 +82,11 @@ protected function _checkTypes(
Mage_Sales_Model_Order $order,
Mage_Sales_Model_Quote $quote,
Radial_FraudInsight_Helper_Data $helper,
Radial_FraudInsight_Helper_Http $httpHelper,
Radial_FraudInsight_Helper_Config $config,
Mage_Catalog_Model_Product $product
) {
return array($request, $insight, $order, $quote, $helper, $config, $product);
return array($request, $insight, $order, $quote, $helper, $httpHelper, $config, $product);
}

public function build()
Expand Down Expand Up @@ -620,12 +626,31 @@ protected function _buildTransactionResponse(
*/
protected function _buildHttpHeaders(Radial_FraudInsight_Sdk_Http_IHeaders $subPayloadHttpHeaders)
{
foreach ($this->_getHttpHeaders() as $name => $message) {
$subPayloadHttpHeader = $subPayloadHttpHeaders->getEmptyHttpHeader();
$this->_buildHttpHeader($subPayloadHttpHeader, $name, $message);
$subPayloadHttpHeaders->offsetSet($subPayloadHttpHeader);
}
return $this;
$httpHeaderZend = array(
array( 'name' => 'host', 'message' => $this->_httpHelper->getHttpHost()),
array( 'name' => 'origin', 'message' => $this->_httpHelper->getHttpOrigin()),
array( 'name' => 'x-prototype-version', 'message' => $this->_httpHelper->getHttpXPrototypeVersion()),
array( 'name' => 'x-requested-with', 'message' => $this->_httpHelper->getHttpXRequestedWith()),
array( 'name' => 'user-agent', 'message' => $this->_httpHelper->getHttpUserAgent()),
array( 'name' => 'accept', 'message' => $this->_httpHelper->getHttpAccept()),
array( 'name' => 'accept-language', 'message' => $this->_httpHelper->getHttpAcceptLanguage()),
array( 'name' => 'accept-encoding', 'message' => $this->_httpHelper->getHttpAcceptEncoding()),
array( 'name' => 'cookie', 'message' => $this->_httpHelper->getCookiesString()),
array( 'name' => 'x-forwarded-proto', 'message' => $this->_httpHelper->getHttpXForwardedProto()),
array( 'name' => 'x-forwarded-for', 'message' => $this->_httpHelper->getHttpXForwardedFor()),
array( 'name' => 'content-type', 'message' => $this->_httpHelper->getHttpContentType()),
array( 'name' => 'connection', 'message' => $this->_httpHelper->getHttpConnection()),
array( 'name' => 'accept-charset', 'message' => $this->_httpHelper->getHttpAcceptCharset()),
array( 'name' => 'referer', 'message' => $this->_httpHelper->getHttpReferrer())
);
foreach ($httpHeaderZend as $headerProperty) {
if (isset($headerProperty['message']) && $headerProperty['message'] != null) {
$subPayloadHttpHeader = $subPayloadHttpHeaders->getEmptyHttpHeader();
$this->_buildHttpHeader($subPayloadHttpHeader, $headerProperty['name'], $headerProperty['message']);
$subPayloadHttpHeaders->offsetSet($subPayloadHttpHeader);
}
}
return $this;
}

/**
Expand All @@ -636,10 +661,7 @@ protected function _buildHttpHeaders(Radial_FraudInsight_Sdk_Http_IHeaders $subP
*/
protected function _buildHttpHeader(Radial_FraudInsight_Sdk_Http_IHeader $subPayloadHttpHeader, $name, $message)
{
if ($name == 'Referer') {
$message = '<![CDATA[' . $message . ']]>';
}
$subPayloadHttpHeader->setHeader($message)
$subPayloadHttpHeader->setHeader($message)
->setName($name);
return $this;
}
Expand Down

0 comments on commit 3e3186a

Please sign in to comment.