params= new JRegistry(); $this->params->loadString($plugin->params, 'JSON'); $this->_cacheEnabled = $this->params->get('cache_enabled'); if ($this->_cacheEnabled === null) $this->_cacheEnabled == 1; $this->_autoflush = $this->params->get('autoFlush'); if ($this->_autoflush === null) $this->_autoflush = 1; $this->_autoflush3rdParty = $this->params->get('autoFlush-ThirdParty'); if ($this->_autoflush3rdParty === null) $this->_autoflush3rdParty = 1; $this->_autoflushClientSide = $this->params->get('autoFlush-ClientSide'); if ($this->_autoflushClientSide === null) $this->_autoflushClientSide = 0; } /** * Heartbeat cache checking function. Will also monitor $_GET for the jWECache parameter * (pressing the purge cache button in admin) * * * @access public * @return null */ public function onAfterInitialise() { // Revisamos si tiene plugin de cookies habilitado, este es el plugin de Cookies compatible.. // http://www.channeldigital.co.uk/resources/joomla-cookie-directive-plugin.html // http://extensions.joomla.org/extension/eu-cookie-directive-pro /* // // Finalmente esto lo hacemos desde Vanrish que funciona mejor, tapo esta parte en el plugin de Joomla // $db =& JFactory::getDBO(); $query = "select name from #__extensions where type = 'plugin' and state = 'active' and name LIKE '%EUCookieDirective%' "; $db->setQuery( $query ); $items = $db->loadObjectList(); if(count($items) >= 1){ if ( isset($_COOKIE['cookieAcceptanceCookie'] ) ){ unset($_COOKIE['cookieAcceptanceCookie']); } $document = JFactory::getDocument(); $document->addScriptDeclaration(' function getCookie(cname) { var name = cname + "="; var ca = document.cookie.split(";"); for(var i = 0; i guest){ JResponse::setHeader('X-Logged-In','False',true); }else{ JResponse::setHeader('X-Logged-In','True',true); } if (!$this->_cacheEnabled || $this->_isBlacklisted($this->_applicationPath)) { JResponse::setHeader('X-Cache-Enabled','False',true); return; } if ($this->_cacheEnabled) { JResponse::setHeader('X-Cache-Enabled','True',true); } //Init the application url $this->_applicationPath = str_replace(array('administrator/index.php','index.php'),'',str_replace($_SERVER['DOCUMENT_ROOT'],'',$_SERVER['SCRIPT_FILENAME'])); $wepath = preg_replace ("/[^\/]+\.php$/", "" , $_SERVER['REQUEST_URI']) ; $this->_we_URL_Path = preg_replace ("/\/+/", "/" , str_replace(array('administrator','index.php'),'', $wepath ) ); //Check for any admin action and proceed to flushMonitor and 3rd party plugins if ( isset($_POST['task']) || isset($_GET['task']) || isset($_GET['cart_virtuemart_product_id'])) { $this->_flushMonitor(); if ($this->_autoflush3rdParty){ $this->_monitorThirdPartyPlugins(); } if ( ( $_REQUEST['task'] == "addJS" || $_REQUEST['task'] == "add" ) && $_REQUEST['option'] == "com_virtuemart" ){ if( isset($_REQUEST['task']) ){ $_POST[JSession::getFormToken()] = 1; //Force the correct token, since the login box on the page is cached with the 1st visitors' token } //Enable the cache bypass for logged users by setting a cache bypass cookie setcookie('wevarnishpass',1,time() + 6000,'/'); JResponse::setHeader('X-Cache-Enabled','False',true); } } //Check if we have a logged in user and enable cache bypass cookie 'task' => string 'user.login' // $user = JFactory::getUser(); // Ya lo instanciamos mas arriba // if (!$user->guest || (isset($_REQUEST['task']) && preg_match('/login/i', $_REQUEST['task']))) if ( !$user->guest || (isset($_REQUEST['task']) && preg_match('/login/i', $_REQUEST['task']) ) || (isset($_REQUEST['bttask']) && preg_match('/register/i', $_REQUEST['bttask']) ) || (isset($_REQUEST['bttask']) && preg_match('/login/i', $_REQUEST['bttask']) ) ) { $_POST[JSession::getFormToken()] = 1; //Force the correct token, since the login box on the page is cached with the 1st visitors' token //Enable the cache bypass for logged users by setting a cache bypass cookie setcookie('wevarnishpass',1,time() + 6000,'/'); JResponse::setHeader('X-Cache-Enabled','False',true); } if ( isset($_REQUEST['task']) && preg_match('/logout/i', $_REQUEST['task']) ) { //JResponse::setHeader('X-Cache-Enabled','False',true); //Remove the bypass cookie if not a logged user if ( !$_REQUEST['task'] == "addJS" && !$_REQUEST['task'] == "add" ){ if (isset($_COOKIE['wevarnishpass'])){ setcookie('wevarnishpass',0, time() - 3600,'/'); } } } // Handle purge button press when get has jWECache=purge, but only in admin with a logged user if(isset($_GET['jWECache']) && $_GET['jWECache'] == 'purge' && JFactory::getApplication()->isAdmin() && !$user->guest ) $this->_purgeCache(true); } /** * Admin panel icon display * * @access public * @param string $context * @return array */ public function onGetIcons( $context ) { return array(array( 'link'=>'?jWECache=purge', 'image'=>'header/icon-48-purge.png', 'text'=>JText::_('Vaciar cache de Varnish'), 'id'=>'jWECache' )); } /** * Calls the cache server to purge the cache * * @access public * @param string|bool $message Message to be displayed if purge is successful. If this param is false no output would be done * @return null */ private function _purgeCache( $message = true , $purge_method = "BAN" , $request = null ) { if($request == "wepath"){ $purgeRequest = $this->_we_URL_Path . '(.*)'; }else{ $purgeRequest = $this->_applicationPath . '(.*)'; } // Check if caching server is varnish or nginx. $hostname = $_SERVER['SERVER_ADDR']; // $purge_method = "BAN"; $cacheServerSocket = fsockopen($hostname, 80, $errno, $errstr, 2); if(!$cacheServerSocket) { JError::raise(E_ERROR,500,JText::_('Connection to cache server failed!')); JError::raise(E_ERROR,500,JText::_($errstr ($errno))); return; } $request = "$purge_method {$purgeRequest} HTTP/1.0\r\nHost: {$_SERVER['SERVER_NAME']}\r\nConnection: Close\r\n\r\n"; if (preg_match('/^www\./',$_SERVER['SERVER_NAME'])) { $domain_no_www = preg_replace('/^www\./', '', $_SERVER['SERVER_NAME']); $request2 = "$purge_method {$purgeRequest} HTTP/1.0\r\nHost: {$domain_no_www}\r\nConnection: Close\r\n\r\n"; }else{ $request2 = "$purge_method {$purgeRequest} HTTP/1.0\r\nHost: www.{$_SERVER['SERVER_NAME']}\r\nConnection: Close\r\n\r\n"; } fwrite($cacheServerSocket, $request); $response = fgets($cacheServerSocket); fclose($cacheServerSocket); $cacheServerSocket = fsockopen($hostname, 80, $errno, $errstr, 2); fwrite($cacheServerSocket, $request2); fclose($cacheServerSocket); /* $file = 'varnishlogN.txt'; // Ouvre un fichier pour lire un contenu existant $current = file_get_contents($file); // Ajoute une personne $current .= date("Y-m-d H:i:s") . "\n $request \n \n $request2 \n ================================== \n"; // Écrit le résultat dans le fichier file_put_contents($file, $current); */ if($message !== false) { if(preg_match('/200/',$response)) { if ($message === true) JFactory::getApplication()->enqueueMessage(JText::_('WE Varnish Cache Successfully Purged!')); else JFactory::getApplication()->enqueueMessage(JText::_( $message )); }else{ JError::raise(E_NOTICE,501, JText::_('WE Varnish Cache: Purge was not successful!')); JError::raise(E_NOTICE,501, jText::_('Error: ' . $response)); } } } private function _purgeCacheOld( $message = true ) { $purgeRequest = $this->_applicationPath . '(.*)'; // Check if caching server is varnish or nginx. $hostname = $_SERVER['SERVER_ADDR']; $purge_method = "PURGE"; if (file_exists($sgcache_ip)) { $hostname = trim( file_get_contents( $sgcache_ip, true ) ); $purge_method = "BAN"; } $cacheServerSocket = fsockopen($hostname, 80, $errno, $errstr, 2); if(!$cacheServerSocket) { JError::raise(E_ERROR,500,JText::_('Connection to Varnish cache server failed!')); JError::raise(E_ERROR,500,JText::_($errstr ($errno))); return; } $request = "$purge_method {$purgeRequest} HTTP/1.0\r\nHost: {$_SERVER['SERVER_NAME']}\r\nConnection: Close\r\n\r\n"; if (preg_match('/^www\./',$_SERVER['SERVER_NAME'])) { $domain_no_www = preg_replace('/^www\./', '', $_SERVER['SERVER_NAME']); $request2 = "$purge_method {$purgeRequest} HTTP/1.0\r\nHost: {$domain_no_www}\r\nConnection: Close\r\n\r\n"; } else $request2 = "$purge_method {$purgeRequest} HTTP/1.0\r\nHost: www.{$_SERVER['SERVER_NAME']}\r\nConnection: Close\r\n\r\n"; fwrite($cacheServerSocket, $request); $response = fgets($cacheServerSocket); fclose($cacheServerSocket); $cacheServerSocket = fsockopen($hostname, 80, $errno, $errstr, 2); fwrite($cacheServerSocket, $request2); fclose($cacheServerSocket); if($message !== false) { if(preg_match('/200/',$response)) { if ($message === true) JFactory::getApplication()->enqueueMessage(JText::_('WE Varnish Cache Successfully Purged!')); else JFactory::getApplication()->enqueueMessage(JText::_( $message )); } else { JError::raise(E_NOTICE,501, JText::_('WE Varnish Cache: Purge was not successful!')); JError::raise(E_NOTICE,501, jText::_('Error: ' . $response)); } } } /** * Check if url is in caching blacklist * * @param string $applicationPath * * @return bool */ private function _isBlacklisted($applicationPath) { $blacklistArray = explode("\n",$this->params->get('blacklist')); $blacklistRegexArray = array(); $indexIsBlacklisted = false; foreach($blacklistArray as $key=>$row) { $row = trim($row); if ($row != '/' && $quoted = preg_quote($row,'/')) $blacklistRegexArray[$key] = $quoted; if ($row == '/') $indexIsBlacklisted = true; } if ($indexIsBlacklisted && $_SERVER['REQUEST_URI'] == $applicationPath) return true; if (empty($blacklistRegexArray)) return false; $blacklistRegex = '/('.implode('|',$blacklistRegexArray) . ')/i'; return preg_match($blacklistRegex, $_SERVER['REQUEST_URI']); } /** * 3rd party plugin monitor * * @access private * @return null */ private function _monitorThirdPartyPlugins() { // Kunena & K2 if ($this->params->get('autoFlush-ThirdParty') == 1 && isset($_POST['option']) && ($_POST['option']=='com_k2' || $_POST['option' ]== 'com_kunena')) { $this->_purgeCache(false); } // VirtueMart if ( (isset($_POST['option']) && $_POST['option'] == 'com_virtuemart') || ( isset($_GET['option']) && $_GET['option'] == 'com_virtuemart' ) || isset($_GET['cart_virtuemart_product_id']) ) { if($this->params->get('autoFlush-ThirdParty') == 1) $this->_purgeCache(false); } // Hacemos autoflush si desde fuera de la administracion se modifican cosas de Kunena // Kunena if (isset($_POST['task'])) { if($this->params->get('autoFlush-ThirdParty') == 1){ if($_POST['task'] == "post"){ $this->_purgeCache(false, "BAN" , "wepath"); } } } } /** * Action monitor * * @access private * @return null */ private function _flushMonitor() { $user = JFactory::getUser(); if ((!JFactory::getApplication()->isAdmin() && !$this->_autoflushClientSide) || $user->guest) return; $autoflush = $this->params->get('autoFlush'); if ($autoflush === null) $autoflush = 1; // Se vacía la cache cada vez que se hace POST (desde fuera o dentro de la administracion) de algun task que no sea login/logout ni user.login/user.logout if (isset($_POST['task']) && $_POST['task'] && !in_array($_POST['task'],self::$_ignoreTasks) && $autoflush == 1) $this->_purgeCache(false); } } Pulseras de cristales