diff --git a/smartoptimizer/.htaccess b/smartoptimizer/.htaccess
index e25e2f9..e2a356f 100644
--- a/smartoptimizer/.htaccess
+++ b/smartoptimizer/.htaccess
@@ -1,3 +1,3 @@
php_flag zlib.output_compression Off
php_flag output_buffering Off
-php_value output_handler NULL
+php_value output_handler None
diff --git a/smartoptimizer/config.php b/smartoptimizer/config.php
index dff4f69..92b3b98 100644
--- a/smartoptimizer/config.php
+++ b/smartoptimizer/config.php
@@ -1,7 +1,7 @@
array(MY_JS_DIR.'test.js', MY_JS_DIR.'test2.js'),
-// 'test.css' => array(MY_JS_DIR.'style.css', MY_JS_DIR.'print.css')
-//);
\ No newline at end of file
+/*
+$settings['groups'] = array(
+ 'alljs.js' => array(MY_JS_DIR.'js/getDataService.js',
+ MY_JS_DIR.'js/recettes_directive.js',
+ MY_JS_DIR.'js/search_directive.js',
+ MY_JS_DIR.'js/cloud_directive.js',
+ MY_JS_DIR.'js/paginate_directive3.js'
+ ),
+ 'test.css' => array(MY_JS_DIR.'style.css', MY_JS_DIR.'print.css')
+);*/
+
+$settings['myrootpath']=implode('/',array_slice(explode('/',$_SERVER["SCRIPT_FILENAME"]),0, -2));
+
+$smartoptimizerPath=$settings['myrootpath'].'/smartoptimizer';
+
+$settings['js_group_name'] = 'group.alljs.js';
+$settings['alljs'] = $smartoptimizerPath."/".$settings['cacheDir']."alljs.txt";
+
+$settings['css_group_name'] = 'group.allcss.css';
+$settings['allcss'] = $smartoptimizerPath."/".$settings['cacheDir']."allcss.txt";
diff --git a/smartoptimizer/index.php b/smartoptimizer/index.php
index 43f1399..d67ab76 100644
--- a/smartoptimizer/index.php
+++ b/smartoptimizer/index.php
@@ -1,5 +1,5 @@
"application/x-shockwave-flash",
"ico" => "image/x-icon",
);
-/**Processs a time in a string
- *
- * @param datetime $time
- * @return string Date formatted
- */
-
-function gmdatestr($time = null) {
- if (is_null($time)){ $time = time();}
- return gmdate("D, d M Y H:i:s", $time) . " GMT";
-}
-/**Adds a status header and then exits
- *
- * @param string $status
- */
function headerExit($status) {
header("HTTP/1.0 $status");
exit();
}
-/**
- * Adds Headers to a returning HTTP response that tells a server not to cache the file ever
- **/
-function headerNoCache()
- {
+
+function headerNoCache() {
// already expired
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
@@ -97,124 +74,113 @@ function headerNoCache()
//generate a unique Etag each time
header('Etag: '.microtime());
}
-/**
- * Adds header to the response informing that the file never expires cache it forever.
- */
-function headerNeverExpire() {
- $exptime = time() + 31557600;
- $curtime = time();
- header("Expires: " . gmdatestr($exptime));
- header("Cache-Control: public, max-age=31557600");
- header("Last-Modified: " . gmdatestr($curtime));
- header("Vary: Accept-Encoding");
+function headerNeverExpire(){
+ header("Expires: " . gmdatestr(time() + 315360000));
+ header("Cache-Control: max-age=315360000");
}
-/**
- * Adds Headers to a file pls echo a Error that smartoptimizer couldnt process the file
- * If Debug is set true this will 404 the file else it will return the file with a Script header.
- *
- * @global array $settings
- * @param string $msg The message to ruturn if Debug is false
- */
-function debugExit($msg) {
- global $settings;
- if (!$settings['debug']) {
- headerExit('404 Not Found');
- }
- headerNoCache();
- header('Content-Type: text/html; charset=' . $settings['charSet']);
- header("Content-Encoding: none");
- echo "//\n";
- exit();
+function debugExit($msg){
+ global $settings;
+ if (!$settings['debug']) {
+ headerExit('404 Not Found');
+ }
+ headerNoCache();
+ header('Content-Type: text/html; charset='.$settings['charSet']);
+ header("Content-Encoding: none");
+ echo "//\n";
+ exit();
}
-/**Gets the input files modification time and compares it to the modification time of /smartoptimizer/index.php and config.php
- * and returns the larges of the 3 (ie the most recent time)
- *
- * @global array $files
- * @global string $fileType
- * @staticvar datetime $filesmtime
- * @return datetime
- **/
+function gmdatestr($time = null) {
+ if (is_null($time)) $time = time();
+ return gmdate("D, d M Y H:i:s", $time) . " GMT";
+}
function filesmtime() {
global $files, $fileMinifier;
static $filesmtime;
- if ($filesmtime){return $filesmtime;}
- $filesmtime = max(filemtime("minifiers/$fileMinifier.php"), filemtime('index.php'), filemtime('config.php'));
+ if ($filesmtime) return $filesmtime;
+ $filesmtime = max(@filemtime("minifiers/$fileMinifier.php"), filemtime('index.php'), filemtime('config.php'));
foreach ($files as $file) {
- if (!file_exists($file)) {debugExit("File not found ($file).");}
+ if (!file_exists($file)) debugExit("File not found ($file).");
$filesmtime = max(filemtime($file), $filesmtime);
}
return $filesmtime;
}
+function getFileList($listFile)
+{
+ global $settings;
+
+ $fileNames=array();
+ if(file_exists($listFile)) {
+
+ $fp=fopen($listFile,'r');
+
+ while (($buffer = fgets($fp, 4096)) !== false) {
+ $fileNames[]=str_replace("\n","",$settings['myrootpath'].'/'.$buffer);
+ }
+ if (!feof($fp)) {
+ echo "Erreur: fgets() a échoué\n";
+ }
+ fclose($fp);
+ }
+
+ return $fileNames;
+}
+
require 'config.php';
list($query) = explode('?', urldecode($_SERVER['QUERY_STRING']));
-//if(isset($settings['groups']) && substr($query, 0, 6) == 'group.'){
-// //we have a pre-defined group to include
-// $group_name = str_replace('group.','',$query);
-// if(isset($settings['groups'][$group_name])){
-// $fileNames = $settings['groups'][$group_name];
-// $fileDir = '';
-// }
-// else {debugExit("Group ($group_name) not set. Please edit config.");}
-//} else {
-if (preg_match('/^\/?(.+\/)?(.+)$/', $query, $matchResult)) {
- $fileNames = $matchResult[2];
- $fileDir = $settings['baseDir'] . $matchResult[1];
-} else {
- debugExit("Invalid file name ($query)");
-}
-//}
-//check the file exists and is relative to the base directory.
-if (strpos(realpath($fileDir), realpath($settings['baseDir'])) !== 0) {debugExit("File is out of base directory.");}
-
-if ($settings['concatenate']) {
- if(!is_array($fileNames)){
- $files = explode($settings['separator'], $fileNames);
- } else {
- $files = $fileNames;
- }
- $settings['concatenate'] = count($files) > 1;
-} else {$files = array($fileNames);}
+if ($settings['concatenate'] && $query == $settings['js_group_name'] ) {
+ $fileDir = '';
+ $files=getFileList($settings['alljs']);
+ // $files=array_merge($files,getFileList($settings['alljs']));
+
+} elseif ($settings['concatenate'] && $query == $settings['css_group_name'] ) {
+ $fileDir = '';
+ $files=getFileList($settings['allcss']);
+}else {
+ if (preg_match('/^\/?(.+\/)?(.+)$/', $query, $matchResult)) {
+ $fileNames = $matchResult[2];
+ $fileDir = $settings['baseDir'].$matchResult[1];
+ } else debugExit("Invalid file name ($query)");
+ $files = array($fileNames);
+}
foreach ($files as $key => $file) {
if (preg_match('/^[^\x00]+\.([a-z0-9]+)$/i', $file, $matchResult)) {
$fileTypes[] = strtolower($matchResult[1]);
- } else {debugExit("Unsupported file ($file)");}
+ } else debugExit("Unsupported file ($file)");
$files[$key] = $fileDir.$file;
}
if ($settings['concatenate']) {
- if (count(array_unique($fileTypes)) > 1) {debugExit("Files must be of the same type.");}
+ if (count(array_unique($fileTypes)) > 1) debugExit("Files must be of the same type.");
}
-//check we have a minifier
+
$fileType = $fileTypes[0];
$fileMinifier = $settings[$fileType.'Minifier'];
-if(!file_exists('minifiers/'.$fileMinifier.'.php')) {debugExit($fileType.'Minifier not found. Please create "minifiers/'.$fileMinifier.'.php" or change minifier in config.php');}
-//check the file meets our filetypes list or
-if (!isset($mimeTypes[$fileType])){ debugExit("Unsupported file type ($fileType)");}
+if(!file_exists('minifiers/'.$fileMinifier.'.php')) debugExit($fileType.'Minifier not found. Please create "minifiers/'.$fileMinifier.'.php" or change minifier in config.php');
+
+if (!isset($mimeTypes[$fileType])) debugExit("Unsupported file type ($fileType)");
header("Content-Type: {$mimeTypes[$fileType]}; charset=".$settings['charSet']);
-// lets check and see if the client accepts compression and change our variable if it doesnt also check the filetype to ensure we want to compress.
+
$settings['gzip'] =
($settings['gzip'] &&
!in_array($fileType, $settings['gzipExceptions']) &&
- in_array('gzip', array_map('trim', explode(',' , htmlspecialchars($_SERVER['HTTP_ACCEPT_ENCODING'], ENT_QUOTES)))) &&
+ in_array('gzip', array_map('trim', explode(',' , @$_SERVER['HTTP_ACCEPT_ENCODING']))) &&
function_exists('gzencode'));
-// ok client likes compression - lets tell it we are sending a compressed file.
-if ($settings['gzip']){ header("Content-Encoding: gzip");}
-//check we have a minifier
+
+if ($settings['gzip']) header("Content-Encoding: gzip");
+
$settings['minify'] = $settings['minify'] && file_exists('minifiers/'.$fileMinifier.'.php');
-//if its css and we have an opera browser embed the css
-$settings['embed'] = $settings['embed'] && $fileType == 'css' && (!preg_match('/msie/i', htmlspecialchars($_SERVER['HTTP_USER_AGENT'])) || preg_match('/msie 8|opera/i', htmlspecialchars($_SERVER['HTTP_USER_AGENT'])));
-//check if we want to call the cache.
+$settings['embed'] = $settings['embed'] && $fileType == 'css' && (!preg_match('/msie/i', $_SERVER['HTTP_USER_AGENT']) || preg_match('/msie 8|opera/i', $_SERVER['HTTP_USER_AGENT']));
$settings['serverCache'] = $settings['serverCache'] && ($settings['minify'] || $settings['gzip'] || $settings['concatenate'] || $settings['embed']);
if ($settings['serverCache']) {
@@ -225,9 +191,9 @@ function_exists('gzencode'));
($settings['serverCache'] && (!file_exists($cachedFile) || ($settings['serverCacheCheck'] && filesmtime() > filemtime($cachedFile)))));
if ($settings['clientCache'] && $settings['clientCacheCheck']) {
- if ($settings['serverCache'] && !$generateContent) {$mtime = filemtime($cachedFile);}
-elseif ($settings['serverCache']) {$mtime = time();}
- else {$mtime = filesmtime();}
+ if ($settings['serverCache'] && !$generateContent) $mtime = filemtime($cachedFile);
+ elseif ($settings['serverCache']) $mtime = time();
+ else $mtime = filesmtime();
$mtimestr = gmdatestr($mtime);
}
@@ -237,7 +203,7 @@ function_exists('gzencode'));
header("Cache-Control: must-revalidate");
} elseif ($settings['clientCache']) {
headerNeverExpire();
- } else { headerNoCache();}
+ } else headerNoCache();
if ($generateContent) {
if ($settings['minify']){
@@ -245,10 +211,10 @@ function_exists('gzencode'));
}
$content = array();
- foreach ($files as $file){(($content[] = file_get_contents($file)) !== false) || debugExit("File not found ($file).");}
+ foreach ($files as $file) (($content[] = @file_get_contents($file)) !== false) || debugExit("File not found ($file).");
$content = implode("\n", $content);
- if ($settings['minify']) {$content = call_user_func('minify_' . $fileType, $content);}
- if ($settings['gzip']){ $content = gzencode($content, $settings['compressionLevel']);}
+ if ($settings['minify']) $content = call_user_func('minify_' . $fileType, $content);
+ if ($settings['gzip']) $content = gzencode($content, $settings['compressionLevel']);
if ($settings['serverCache']) {
$handle = @fopen($cachedFile, 'w') or debugExit("Could not create cache file($cachedFile).");
fwrite($handle, $content);
@@ -260,4 +226,5 @@ function_exists('gzencode'));
header('Content-Length: ' . filesize($cachedFile));
readfile($cachedFile);
}
-} else {headerExit('304 Not Modified');}
\ No newline at end of file
+} else headerExit('304 Not Modified');
+?>
diff --git a/smartoptimizer/minifiers/css.php b/smartoptimizer/minifiers/css.php
index 533dc98..e8cf072 100644
--- a/smartoptimizer/minifiers/css.php
+++ b/smartoptimizer/minifiers/css.php
@@ -14,12 +14,9 @@ function convertUrl($url, $count)
if (preg_match('@^[^/]+:@', $url)) return $url;
$fileType = substr(strrchr($url, '.'), 1);
- if (isset($mimeTypes[$fileType]))
- $mimeType = $mimeTypes[$fileType];
- elseif (function_exists('mime_content_type') && file_exists($fileDir.$url))
- $mimeType = mime_content_type($fileDir.$url);
- else
- $mimeType = null;
+ if (isset($mimeTypes[$fileType])) $mimeType = $mimeTypes[$fileType];
+ elseif (function_exists('mime_content_type')) $mimeType = mime_content_type($url);
+ else $mimeType = null;
if (!$settings['embed'] ||
!file_exists($fileDir.$url) ||
@@ -37,7 +34,7 @@ function convertUrl($url, $count)
$contents = file_get_contents($fileDir.$url);
- if ($fileType == 'css') {
+ if ($fileType === 'css') {
$oldFileDir = $fileDir;
$fileDir = rtrim(dirname($fileDir.$url), '\/').'/';
$oldBaseUrl = $baseUrl;
@@ -52,60 +49,61 @@ function convertUrl($url, $count)
}
function minify_css($str) {
+ $strsize=strlen($str);
$res = '';
$i=0;
$inside_block = false;
$current_char = '';
- while ($i+1+\n\r\t ]{2}$/';
if (strlen($res) && preg_match($pattern, $res[strlen($res)-1].$str[$i+1]))
$res .= $current_char;
@@ -113,7 +111,7 @@ function minify_css($str) {
$i++;
}
- if ($i
diff --git a/smartoptimizer/minifiers/html.php b/smartoptimizer/minifiers/html.php
new file mode 100644
index 0000000..3b1099b
--- /dev/null
+++ b/smartoptimizer/minifiers/html.php
@@ -0,0 +1,49 @@
+|', $buffer, $out);
+ $jsScripts=$out[1];
+
+ $fp=fopen($settings['alljs'],"w");
+ foreach ($jsScripts as $value) fwrite($fp, $value."\n");
+ fclose($fp);
+
+ $html_modified=preg_replace('||', '',$buffer );
+ $buffer=preg_replace('|
|', '',$html_modified );
+
+ /* here we take automatically all css files found in HTML and we concatenate them. */
+ preg_match_all('||', $buffer, $out);
+ $cssScripts=$out[1];
+
+ $fp=fopen($settings['allcss'],"w");
+ foreach ($cssScripts as $value) fwrite($fp, $value."\n");
+ fclose($fp);
+
+ $html_modified=preg_replace('||', '',$buffer );
+ $buffer=preg_replace('|