<?php if (!defined('BASEPATH')) {
exit('No direct script access allowed');
}
class Assets {
var $variable = 0;
function Assets() {
log_message ( 'debug', "Assets Class Initialized" );
$this->_defineHelpers ();
$this->minify = 1;
$this->checkForUpdatedAssetsEvery = 5; // 60*60*24); //cache for 60 secods (so it will check js/css for new and updated files every x seconds)
$this->src = array (
'js' => array (
// May 15, 2011 - Slimbox doesn't work with Mootools 1.2.1 in Mozilla 4 and Chrome 11; needs 1.2.4 and up
// 'mt'=>'js/mootools-1.2.1-core-nc.js', //No Compr
// 'mtm'=>'js/mootools-1.2-more.js;js/mtExtend.js', //No Compr, Sortables, Drag.Move, Drag, Fx.Elements/Scroll/Slide, Accordion, Assets
'mt' => 'js/mootools-1.2.5-core-nc.js', // No Compr
'mtm' => 'js/mootools-1.2.5.1-more.js;js/mtExtend.js', // No Compr, Sortables, Drag.Move, Drag, Fx.Elements/Scroll/Slide, Accordion, Assets
'mtm_ef' => 'js/mootools-more-ef.js',
'cn' => 'js/clientcide-trunk-754.compressed.js', // No Compr, Waiter, StickyWin/Modal, StyleWriter, Modalizer, Element.Shortcuts/Position/Pin/Measure, Date, IframeShim, FixPNG, ToElement, Class.Refactor, dbug, Clientcide (Use setCNETAssetBaseHref('/images/cnet/');)
'ac' => 'js/autocompleter/Autocompleter.js;js/autocompleter/Autocompleter.Local.js;js/autocompleter/Autocompleter.Request.js;js/autocompleter/Observer.js',
'gm' => 'js/googleMaps.js',
'hm' => 'js/HistoryManager.js',
'ls' => 'js/livesearch-1.0.js',
// 'wf'=>'js/wufoo.js;js/TextArea.js', //js/selBoxes.js;js/fileBoxes.js;
'mc' => 'js/MooCrop.js',
'sb' => 'js/slimbox.js', // May 15, 2011: Slimbox upgraded to 1.71
'vc' => 'js/vlaCal-v2.1.js',
'fc' => 'js/FusionCharts.js',
'ss' => 'js/slideshow/slideshow.js',
'sq' => 'js/squeezebox.js',
'jq' => 'js/jquery-1.4.4.min.js',
'jq_fb' => 'js/fancybox/jquery.fancybox-1.3.4.pack.js;js/fancybox/jquery.easing-1.4.pack.js;js/fancybox/jquery.mousewheel-3.0.4.pack.js',
'jq_ag' => 'js/jquery.autogrowtextarea.js',
'ag1' => 'js/autogrow-1.0.js',
'cnse' => 'js/clientcide.2.2.0.simpleEdit.compressed.js',
'st' => 'js/scrollabletable.js'
),
'jsAuto' => array (
'mt',
'mtm',
'cn',
'ac',
'gm',
'ss',
'sq'
),
'jqAuto' => array (
'jq'
),
'css' => array (
's' => 'css/style.css',
'x' => 'css/fix.css',
'ac' => 'css/Autocompleter.css',
'sb' => 'css/slimbox.css',
'vc' => 'css/vlaCal-v2.1.css',
'ss' => 'css/slideshow.css',
'sq' => 'css/squeezebox.css',
'jq_fb' => 'css/jquery.fancybox-1.3.4.css',
'sc' => 'css/style_classifieds.css',
'st' => 'css/scrollabletable.css'
)
,
'cssAuto' => array (
's',
'x',
'ac',
'ss',
'sq'
)
);
}
function _getModules($m, $d) {
if (ereg ( ':', $d )) {
$d = explode ( ':', $d );
$tmp = array ();
foreach ( $d as $v ) {
if (array_key_exists ( $v, $m ) || $v == 'excl') {
$tmp [] = $v;
}
}
$d = $tmp;
$tmp = NULL;
} else if (array_key_exists ( $d, $m )) {
$d = array (
$d
);
} else {
$d = array ();
}
return $d;
}
function _getContents($f) {
$f = addDR ( $f );
if (ereg ( ';', $f )) {
$a = explode ( ';', $f );
$o = '';
foreach ( $a as $f ) {
$o .= $this->_getContents ( $f );
}
return $o;
} else {
return ! is_file ( $f ) ? '' : file_get_contents ( $f ) . "\n\n\n";
}
}
function _compressCSS($buffer) {
if (! $this->minify) {
return $buffer;
}
/* remove comments */
// $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
$buffer = preg_replace ( '/((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))/', "", $buffer );
/* remove tabs, spaces, new lines, etc. */
$buffer = str_replace ( array (
"\r\n",
"\r",
"\n",
"\t",
' ',
' ',
' '
), '', $buffer );
/* remove unnecessary spaces */
$buffer = str_replace ( '{ ', '{', $buffer );
$buffer = str_replace ( ' }', '}', $buffer );
$buffer = str_replace ( '; ', ';', $buffer );
$buffer = str_replace ( ', ', ',', $buffer );
$buffer = str_replace ( ' {', '{', $buffer );
$buffer = str_replace ( '} ', '}', $buffer );
$buffer = str_replace ( ': ', ':', $buffer );
$buffer = str_replace ( ' ,', ',', $buffer );
$buffer = str_replace ( ' ;', ';', $buffer );
return $buffer;
}
function _compressJS($buffer) {
if (! $this->minify) {
return $buffer;
}
$CI = &get_instance ();
$CI->load->library ( 'JSMin' );
return JSMin::minify ( $buffer );
}
function css($modules = 'def', $ver = 1) {
$wp = '/assets/css';
// $ver = '101108_155951';
$fn = $wp . '/styles_' . ereg_replace ( ':', '_', $modules ) . '_' . $ver . '.css';
if (file_exists ( addDR ( $fn ) )) {
return $fn;
}
createDir ( $wp );
$modules = $this->_getModules ( $this->src ['css'], $modules );
if (! in_array ( 'excl', $modules ) && ! in_array ( 'sc', $modules )) {
$modules = array_merge ( $this->src ['cssAuto'], $modules ); // excl for exclude auto
}
$css = "/* CSS Document */\n\n";
foreach ( $modules as $id ) {
$css .= $this->_getContents ( $this->src ['css'] [$id] );
}
$css = $this->_compressCSS ( $css );
$oldCSS = getFiles ( $wp, 'css', 60 * 60 * 24 * 3 ); // get files that are older then 3 days
foreach ( $oldCSS as $cf ) {
@unlink ( addDR ( "$wp/$cf" ) );
}
if (storeFile ( $css, $fn )) {
return $fn;
} else {
return $wp . '/not_found.css';
}
}
function jq($modules = 'def', $ver = 1) {
$wp = '/assets/js';
$fn = $wp . '/JQscripts_' . ereg_replace ( ':', '_', $modules ) . '_' . $ver . '.js';
createDir ( $wp );
$modules = $this->_getModules ( $this->src ['js'], $modules );
if (! in_array ( 'excl', $modules )) {
$modules = array_merge ( $this->src ['jqAuto'], $modules );
} // excl for exclude auto
$js = "// JavaScript Document\n\n";
foreach ( $modules as $id ) {
$js .= $this->_getContents ( $this->src ['js'] [$id] );
}
$js = $this->_compressJS ( $js );
$oldJS = getFiles ( $wp, 'js', 60 * 60 * 24 * 3 ); // get files that are older then 3 days
foreach ( $oldJS as $jsf ) {
@unlink ( addDR ( "$wp/$jsf" ) );
}
if (storeFile ( $js, $fn )) {
return $fn;
} else {
return $wp . '/not_found.js';
}
}
function js($modules = 'def', $ver = 1) {
$wp = '/assets/js';
$fn = $wp . '/scripts_' . ereg_replace ( ':', '_', $modules ) . '_' . $ver . '.js';
if (file_exists ( addDR ( $fn ) )) {
return $fn;
}
createDir ( $wp );
$modules = $this->_getModules ( $this->src ['js'], $modules );
if (! in_array ( 'excl', $modules )) {
$modules = array_merge ( $this->src ['jsAuto'], $modules );
} // excl for exclude auto
$js = "// JavaScript Document\n\n";
foreach ( $modules as $id ) {
$js .= $this->_getContents ( $this->src ['js'] [$id] );
}
$js = $this->_compressJS ( $js );
$oldJS = getFiles ( $wp, 'js', 60 * 60 * 24 * 3 ); // get files that are older then 3 days
foreach ( $oldJS as $jsf ) {
@unlink ( addDR ( "$wp/$jsf" ) );
}
if (storeFile ( $js, $fn )) {
return $fn;
} else {
return $wp . '/not_found.js';
}
}
function _defineHelpers() {
function css($a = '') { // Loads CSS, pass names eg. 'frms:phts', set autoloads in asset controller, update version here
$CI = &get_instance ();
$v = getAssetVersion2 ( '/css', 'css' );
$modules = isEmpty ( $a ) ? 'def' : $a; // modules or default
$fn = $CI->assets->css ( $modules, $v ); // returns: '/asset/css/styles'.$modules.$ver.'.css';
?><link href="<?=$fn?>" rel="stylesheet" type="text/css" /><?
/*
* <!--[if IE 6]><script type="text/javascript" src="/js/png_fix.js"></script><![endif]-->
* <!--[if IE 6]>
* <style type="text/css" media="screen">@import url(/css/ie6.css);</style>
* <![endif]-->
* <!--[if lt IE 8]><style>
* .wraptocenter span {
* display: inline-block;
* height: 100%;
* }
* </style><![endif]-->
* <!--[if lt IE 8]>
* <style type="text/css" media="screen">@import url(/css/ltie8.css);</style>
* <![endif]-->
*/
}
function jq() {
$ps = func_get_args ();
while ( $p = array_shift ( $ps ) ) {
if (ereg ( '.php', $p )) {
$f = $p;
} else {
$a = $p;
}
}
$CI = &get_instance ();
$v = getAssetVersion2 ( '/js', 'js' );
$modules = isEmpty ( $a ) ? 'def' : $a; // modules or default
$fn = $CI->assets->jq ( $modules, $v ); // returns: '/assets/js/script$modules$ver.js'
?><script type="text/javascript" src="<?=$fn?>"></script><?
}
function js() { // $a='',$f=NULL){
$ps = func_get_args ();
while ( $p = array_shift ( $ps ) ) {
if (ereg ( '.php', $p )) {
$f = $p;
} else {
$a = $p;
}
}
$CI = &get_instance ();
$v = getAssetVersion2 ( '/js', 'js' );
$modules = isEmpty ( $a ) ? 'def' : $a; // modules or default
$fn = $CI->assets->js ( $modules, $v ); // returns: '/assets/js/script$modules$ver.js'
?><script type="text/javascript" src="<?=$fn?>"></script><?
}
function getAssetVersion2($webPath, $ext = 'js') { // Gets updated asset version based on last modify date... uses memcache to reduce file checks
$CI = & get_instance ();
$cacheKey = 'asset/' . $ext . 'Ver'; // jsVer or cssVer
$cache = $CI->mp_cache->get ( $cacheKey );
if ($cache === false) {
clearstatcache (); // Clears file status cache
$fls = getFilesRecursive ( $webPath, $ext );
$md = 0;
foreach ( $fls as $f ) {
$mod = filemtime ( addDR ( $f ) );
$md = $mod > $md ? $mod : $md; // get highest mod time
}
$cache = date ( "ymd_His", $md == 0 ? time () : $md ); // 090631230159
$CI->mp_cache->write ( $cache, $cacheKey, $CI->assets->checkForUpdatedAssetsEvery ); // cache for 60 secods (so it will check js/css for new and updated files every x seconds)
}
return $cache;
}
}
}
?>