Skip to content

Commit

Permalink
Merge pull request #934 from nexcess/devel
Browse files Browse the repository at this point in the history
Devel
  • Loading branch information
miguelbalparda committed Sep 14, 2015
2 parents c44ebcf + 08dbebe commit f89ae64
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 37 deletions.
Expand Up @@ -299,8 +299,14 @@ public function banCacheType( $eventObject ) {
*/
public function banProductReview( $eventObject ) {
$patterns = array();
/* @var $review \Mage_Review_Model_Review*/
$review = $eventObject->getObject();
$products = $review->getProductCollection()->getItems();

/* @var $productCollection \Mage_Review_Model_Resource_Review_Product_Collection*/
$productCollection = $review->getProductCollection();

$products = $productCollection->addEntityFilter((int)$review->getEntityPkValue())->getItems();

$productIds = array_unique( array_map(
create_function( '$p', 'return $p->getEntityId();' ),
$products ) );
Expand Down
Expand Up @@ -345,7 +345,9 @@ protected function _connect() {
$banner['text'] );
}

$this->_version = $this->_determineVersion($banner['text']);
if ($this->_version == null) { // If autodetecting
$this->_version = $this->_determineVersion($banner['text']);
}

return $this->isConnected();
}
Expand Down
Expand Up @@ -39,10 +39,10 @@ static public function getFromSocket( $socket ) {
return null;
}
switch( $version ) {
case '4.0':
return Mage::getModel(
'turpentine/varnish_configurator_version4',
array( 'socket' => $socket ) );
case '4.0':
return Mage::getModel(
'turpentine/varnish_configurator_version4',
array( 'socket' => $socket ) );

case '3.0':
return Mage::getModel(
Expand Down Expand Up @@ -209,17 +209,33 @@ protected function _getNormalizeHostTarget() {
* @return string
*/
public function getAllowedHostsRegex() {
$hosts = array();
foreach( Mage::app()->getStores() as $store ) {
$hosts[] = parse_url( $store->getBaseUrl( Mage_Core_Model_Store::URL_TYPE_WEB , false ), PHP_URL_HOST );
}
$hosts = array_values(array_unique( $hosts ));
$hosts = array();
foreach( Mage::app()->getStores() as $store ) {
$hosts[] = parse_url( $store->getBaseUrl( Mage_Core_Model_Store::URL_TYPE_WEB , false ), PHP_URL_HOST );
}

$hosts = array_values(array_unique( $hosts ));

$pattern = '('.implode('|', array_map("preg_quote", $hosts)).')';
return $pattern;
return $pattern;
}


/**
* Get the Host normalization sub routine
*
* @return string
*/
protected function _vcl_sub_allowed_hosts_regex() {
$tpl = <<<EOS
# if host is not allowed in magento pass to backend
if (req.http.host !~ "{{allowed_hosts_regex}}") {
return (pass);
}
EOS;
return $this->_formatTemplate( $tpl, array(
'allowed_hosts_regex' => $this->getAllowedHostsRegex() ) );
}

/**
* Get the base url path regex
*
Expand Down Expand Up @@ -292,7 +308,7 @@ protected function _getDefaultBackend() {
'first_byte_timeout' => $timeout . 's',
'between_bytes_timeout' => $timeout . 's',
);
if ( Mage::getStoreConfigFlag( 'turpentine_vcl/backend/load_balancing' ) ) {
if ( Mage::getStoreConfig( 'turpentine_vcl/backend/load_balancing' ) != 'no' ) {
return $this->_vcl_director( 'default', $default_options );
} else {
return $this->_vcl_backend( 'default',
Expand All @@ -313,7 +329,7 @@ protected function _getAdminBackend() {
'first_byte_timeout' => $timeout . 's',
'between_bytes_timeout' => $timeout . 's',
);
if ( Mage::getStoreConfigFlag( 'turpentine_vcl/backend/load_balancing' ) ) {
if ( Mage::getStoreConfig( 'turpentine_vcl/backend/load_balancing' ) != 'no' ) {
return $this->_vcl_director( 'admin', $admin_options );
} else {
return $this->_vcl_backend( 'admin',
Expand Down Expand Up @@ -821,7 +837,7 @@ protected function _getTemplateVars() {
'admin_frontname' => $this->_getAdminFrontname(),
'normalize_host_target' => $this->_getNormalizeHostTarget(),
'url_base_regex' => $this->getBaseUrlPathRegex(),
'allowed_hosts_regex' => $this->getAllowedHostsRegex(),
'allowed_hosts_regex' => $this->getAllowedHostsRegex(),
'url_excludes' => $this->_getUrlExcludes(),
'get_param_excludes' => $this->_getGetParamExcludes(),
'get_param_ignored' => $this->_getIgnoreGetParameters(),
Expand Down Expand Up @@ -857,6 +873,11 @@ protected function _getTemplateVars() {
'esi_private_ttl' => Mage::helper( 'turpentine/esi' )
->getDefaultEsiTtl(),
);

if( (bool)Mage::getStoreConfig( 'turpentine_vcl/urls/bypass_cache_store_url') ) {
$vars['allowed_hosts'] = $this->_vcl_sub_allowed_hosts_regex();
}

if( Mage::getStoreConfig( 'turpentine_vcl/normalization/encoding' ) ) {
$vars['normalize_encoding'] = $this->_vcl_sub_normalize_encoding();
}
Expand All @@ -872,7 +893,7 @@ protected function _getTemplateVars() {
if( Mage::getStoreConfig( 'turpentine_vcl/normalization/cookie_target' ) ) {
$vars['normalize_cookie_target'] = $this->_getNormalizeCookieTarget();
}

$customIncludeFile = $this->_getCustomIncludeFilename();
if( is_readable( $customIncludeFile ) ) {
$vars['custom_vcl_include'] = file_get_contents( $customIncludeFile );
Expand Down
2 changes: 1 addition & 1 deletion app/code/community/Nexcessnet/Turpentine/etc/config.xml
Expand Up @@ -20,7 +20,7 @@
<config>
<modules>
<Nexcessnet_Turpentine>
<version>0.6.5</version>
<version>0.6.6</version>
</Nexcessnet_Turpentine>
</modules>
<default>
Expand Down
10 changes: 10 additions & 0 deletions app/code/community/Nexcessnet/Turpentine/etc/system.xml
Expand Up @@ -503,6 +503,16 @@
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
</url_blacklist>
<bypass_cache_store_url translate="label" module="turpentine">
<label>Bypass Varnish if base URL is not found within a store?</label>
<comment>If enabled any URL in the client that does not start with a store URL will bypass the cache</comment>
<frontend_type>select</frontend_type>
<source_model>turpentine/config_select_toggle</source_model>
<sort_order>11</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
</bypass_cache_store_url>
</fields>
</urls>
<params translate="label" module="turpentine">
Expand Down
5 changes: 1 addition & 4 deletions app/code/community/Nexcessnet/Turpentine/misc/version-3.vcl
Expand Up @@ -161,10 +161,7 @@ sub vcl_recv {
error 403 "External ESI requests are not allowed";
}
}
# if host is not allowed in magento pass to backend
if (req.http.host !~ "{{allowed_hosts_regex}}") {
return (pass);
}
{{allowed_hosts}}
# no frontend cookie was sent to us AND this is not an ESI or AJAX call
if (req.http.Cookie !~ "frontend=" && !req.http.X-Varnish-Esi-Method) {
if (client.ip ~ crawler_acl ||
Expand Down
5 changes: 1 addition & 4 deletions app/code/community/Nexcessnet/Turpentine/misc/version-4.vcl
Expand Up @@ -159,10 +159,7 @@ sub vcl_recv {
return (synth(403, "External ESI requests are not allowed"));
}
}
# if host is not allowed in magento pass to backend
if (req.http.host !~ "{{allowed_hosts_regex}}") {
return (pass);
}
{{allowed_hosts}}
# no frontend cookie was sent to us AND this is not an ESI or AJAX call
if (req.http.Cookie !~ "frontend=" && !req.http.X-Varnish-Esi-Method) {
if (client.ip ~ crawler_acl ||
Expand Down
36 changes: 27 additions & 9 deletions contrib/tools/esi-decoder.php
Expand Up @@ -7,13 +7,21 @@
*
* Make sure you place this utility on a protected spot on your web server where only authorized users can use it.
*
* If the URLs you see in "varnishlog" or "varnishncsa", read this FAQ item:
* If the URLs you see in "varnishlog" or "varnishncsa" are not working because they are cut off, read this FAQ item:
* https://github.com/nexcess/magento-turpentine/wiki/FAQ#im-using-varnishncsa-to-generate-logs-and-the-esi-urls-are-cut-off-how-do-i-get-the-full-url-in-the-logs
*
*/

// You might need to fix the path to your app/Mage.php on the line below.
require_once dirname(__FILE__).'/../../app/Mage.php';
$tries = 5;
$dir = dirname(__FILE__);
while( $tries-- && ! file_exists( $dir . '/app/Mage.php' ) ) {
$dir = dirname($dir); // go one dir up
}
if ( file_exists( $dir . '/app/Mage.php' ) ) {
require_once $dir . '/app/Mage.php';
} else {
die( "Could not find 'app/Mage.php'. Please edit esi-decoder.php and 'require' it manually." );
}

Mage::app();
$data = ( empty($_REQUEST['data']) ) ? '' : $_REQUEST['data'];
Expand Down Expand Up @@ -69,7 +77,12 @@
}
$dataHelper = Mage::helper( 'turpentine/data' );
$esiDataArray = $dataHelper->thaw( $processData );
?>
$showContentUrl = Mage::getUrl( 'turpentine/esi/getBlock',
array( 'method' => 'esi',
'ttl' => 0,
'hmac' => $dataHelper->getHmac( $processData ),
'data' => $processData ) );
?>
<div class="center">=&nbsp; DATA &nbsp;=</div>
<div class="result">
<pre><?php echo htmlentities( var_export( $esiDataArray, 1 ) ); ?></pre>
Expand All @@ -78,14 +91,19 @@
$refPreg = preg_quote( $esiHelper->getEsiReferrerParam(), '|' );
if ( preg_match('|'.$refPreg.'/([\w\.\-]+),*|', $data, $matches) ):
$processData = $matches[1];
?>
<div class="center">=&nbsp; REFERRER &nbsp;=</div>
<div class="result">
<pre><?php echo htmlentities( $dataHelper->urlBase64Decode( $processData ) ); ?></pre>
</div>
<?php
endif; // if preg_match referrer
?>
<div class="center">=&nbsp; REFERRER &nbsp;=</div>
<div class="result">
<pre><?php echo htmlentities( $dataHelper->urlBase64Decode( $processData ) ); ?></pre>
<div class="center">
<input type="button" value="SHOW CONTENT" onclick='window.open(<?php echo json_encode($showContentUrl); ?>,"_blank");' />
</div>
<?php
endif;
endif;
endif; // if $data
?>
</body>
</html>

0 comments on commit f89ae64

Please sign in to comment.