Location: PHPKode > scripts > Avatar Tools > helpers/optimize.php
<?php
/**
 * @version		$Id: coolfeed.php 100 2012-04-14 17:42:51Z hide@address.com $
 * @copyright	JoomAvatar.com
 * @author		Nguyen Quang Trung
 * @link		http://joomavatar.com
 * @license		License GNU General Public License version 2 or later http://www.gnu.org/licenses/gpl-2.0.html
 * @package		Avatar Dream Framework Template
 * @facebook 	http://www.facebook.com/pages/JoomAvatar/120705031368683
 * @twitter	    https://twitter.com/#!/JoomAvatar
 * @support 	http://joomavatar.com/forum/
 */

// no direct access
defined('_JEXEC') or die;

class PlgAvatarOptimize {
	
	public static $_cacheGroup = 'plg_avatar_tool';
	
	public static function scanScripts ($subject) 
	{
		  $pattern = '/(\\s*)
		  				(<script\\b[^>]*?[^>]*src=["\']([^>]*\.js\b)[^>]*>)
		  				([\\s\\S]*?)
		  					<\\/script>
		  				(\\s*)/i';
		   
		  preg_match_all($pattern, $subject, $matches);
		  
		  $uri = JURI::getInstance();
		  $files = array();
		  
		  foreach ($matches[3] as $key => $file) 
		  {
		  		if ($uri->isInternal($file)) {
		  			$files[] = $file;
				} else {
		  			unset($matches[2][$key]);
		  		}	
				
		  }
		  
		  return array ('pattern' => $matches[2] , 'files' => $files);
	}
	
	public static function scanStyles ($subject) 
	{
		$pattern =  '/\\s*(<link\\b[^>]*?[^>]*href=["\']([^>]*\.css\b)[^>]*>)\\s*/i';
		
		preg_match_all($pattern, $subject, $matches);
		
		$mediaLinks = array();
		
		foreach ($matches[1] as $key => $file) 
		{
			$result = preg_match('~media\s?=\s?["\']([^"\']+?)["\']~i', $file, $mediaTypes);
			
			if ($result) 
			{
				$mediaLinks[$key] = $mediaTypes[1];
			}
		}
		
		$uri = JURI::getInstance();
		$files = array();
		  
		foreach ($matches[2] as $key => $file) 
		{
			if ($uri->isInternal($file)) {
		  		$files[] = $file;
			} else {
		  		unset($matches[1][$key]);
		  	}	
		}
		
		return array ('pattern' => $matches[1] , 'files' => $files, 'mediaLinks' => $mediaLinks);
	}	
	
	public static function removeScripts ($subject , $arrayScripts) 
	{
		foreach ($arrayScripts as $script) {
			$subject = str_replace($script, '', $subject);
		}
		
		return $subject;
	}
	
	public function getContentCSS($arrayFiles) 
	{
		$content = '';
		
		$options = array('prependRelativePath' => '../');
		$oURI = JURI::getInstance();
		$path = $oURI->getPath(); 
		
		foreach ($arrayFiles['files'] as $key => $file) 
		{
			$file = dirname(JPATH_ROOT).$path.str_replace($path, '', $file);
			
			if(file_exists($file)) 
			{
				$options 	= array('currentDir' => dirname($file));
				$source 	= file_get_contents($file);
				
				if (isset($arrayFiles['mediaLinks'][$key])) {
					$source = '@media '.$arrayFiles['mediaLinks'][$key]. '{' . $source . '}';
				}
				
				$content 	.= Minify_CSS::minify($source, $options);
			}		
		}
		
		return $content;
	}

	public function getContentJS($arrayFiles) 
	{
		$content = '';
		$oURI = JURI::getInstance();
		$path = $oURI->getPath(); 
		
		foreach ($arrayFiles as $file) 
		{
			$file = dirname(JPATH_ROOT).$path.str_replace($path, '', $file);
			
			if (file_exists($file)) {
				$source 	= file_get_contents($file);
				$content 	.= JSMin::minify($source);	
			}
		}
		
		return $content;
	}
	
	public static function combine($obj) 
	{
		$js	 = '';
		$css = '';
		$url = JURI::base().'index.php?controller=plgavatartools';
		$gzip = 'gzip=0&';
		$expire = 'expire='.$obj->params->get('lifetime', 30);
		$document = JFactory::getDocument();
		$sLnEnd = $document->_getLineEnd();
        $sTab = $document->_getTab();
		
		if ($obj->optimizeGzip) {
			$gzip = 'gzip=1&';
		}

		if ($obj->optimizeJS) 
		{
			unset($cached);
			$args 		= array($obj->_scripts['files']);
	        $function 	= array('PlgAvatarOptimize', 'getContentJS');
			$idJS 		= md5(serialize(implode('', $obj->_scripts['files'] ) . $obj->params));
			$cached 	= $obj->cache->get($function, $args, $idJS);
			$fileNameJS 	= self::_getCacheFileName($obj->cache, $idJS, PlgAvatarOptimize::$_cacheGroup);
			
        	$js  = $sTab.'<script type="text/javascript" src="'.$url.'&'.$gzip.$expire.'&type=js&f='.base64_encode($fileNameJS).'"></script>'.$sLnEnd;	
		}
		
		if ($obj->optimizeCSS) 
		{
			unset($cached);
			$args 		= array($obj->_styles);
	        $function 	= array('PlgAvatarOptimize', 'getContentCSS');
			$idCSS 		= md5(serialize(implode('', $obj->_styles['files'] ) . $obj->params));
			$cached 	= $obj->cache->get($function, $args, $idCSS);
			$fileNameCSS 	= self::_getCacheFileName($obj->cache, $idCSS, PlgAvatarOptimize::$_cacheGroup);
			
			$css = $sTab.'<link rel="stylesheet" type="text/css" href="'.$url.'&'.$gzip.$expire.'&type=css&f='.base64_encode($fileNameCSS).'" />'.$sLnEnd;
        }
		
		$obj->body = str_replace('</title>', '</title>'.$css.$js, $obj->body);
		return $obj->body;
	}
	
	protected function _getCacheFileName($cache, $id, $group) 
	{
		$storage = $cache->_getStorage();
        $name = md5($storage->_application . '-' . $id . '-' . $storage->_language);
        $name = $storage->_hash . '-cache-' . $group . '-' . $name;
        return $name;
    }
	
	public static function loadjQuery($content)
	{
		$pattern = '/<script\\b[^>]*?[^>]*src=["\'][^>]*jquery[^>]*\.js\b[^>]*>[\\s\\S]*?<\\/script>/i';
		preg_match_all($pattern, $content, $matches);
		
		return count($matches[0]);
	}
}
Return current item: Avatar Tools