Max F 5 anni fa
parent
commit
5aa24f56ef

+ 10 - 2
src/App/Middlewares/MinimifierHtml.php

@@ -13,13 +13,21 @@ class MinimifierHtml
     {
         $response = $next($request, $response);
 
-        $initial_content_length = $response->getContentLength();
+        if (false) {
+            // modify css link files
+            $content = $response->getContent();
+            preg_match_all('#<link [^>]+"([^>]).css"#', $content, $regs);
+            pre($regs, 1);
+            $js_files = $regs[1];
+        }
 
+        // minimify html
+        $initial_content_length = $response->getContentLength();
         $content_minimified = self::minify_html($response->getContent());
         $response->setContent($content_minimified);
-        
         $final_content_length = $response->getContentLength();
 
+        // add information headers
         $response->addHeader('X-Unminimified-Content-Length', $initial_content_length);
         $response->addHeader('X-Minimified-Content-Length', $final_content_length);
 

+ 88 - 1
src/Controllers/MinimifierController.php

@@ -4,6 +4,7 @@ namespace KarmaFW\Controllers;
 
 use \KarmaFW\Routing\Controllers\WebAppController;
 use \KarmaFW\App\Middlewares\MinimifierJs;
+use \KarmaFW\App\Middlewares\MinimifierCss;
 
 
 class MinimifierController extends WebAppController
@@ -19,7 +20,7 @@ class MinimifierController extends WebAppController
 
 		if (! is_dir($document_root)) {
 			if (! empty($_SERVER['DOCUMENT_ROOT'])) {
-				$document_root = $_SERVER['DOCUMENT_ROOT'];
+				$document_root = realpath($_SERVER['DOCUMENT_ROOT']);
 
 			} else {
 				$document_root = '';
@@ -29,14 +30,29 @@ class MinimifierController extends WebAppController
 		if ($document_root) {
 			$file_path = $document_root . $file_url;
 
+			if ($file_path != realpath($file_path) || substr($file_path, -3) != '.js') {
+				// file path invalid or not a js file
+				redirect($file_url);
+				/*
+				header('HTTP/1.1 404 Not Found');
+				echo "ERROR 404";
+				exit;
+				*/
+			}
+
 			if (! is_file($file_path)) {
+				// file not found
+				redirect($file_url);
+				/*
 				header('HTTP/1.1 404 Not Found');
 				echo "ERROR 404";
 				exit;
+				*/
 
 			} else {
 				header('Content-type: text/javascript');
 
+				//if (ENV == 'dev' && ! (defined('MINIMIFY_JS') && MINIMIFY_JS ) ) {
 				if (false) {
 					// NO minimification
 					readfile($file_path);
@@ -46,10 +62,81 @@ class MinimifierController extends WebAppController
 					$content = file_get_contents($file_path);
 					$content = MinimifierJs::minify_js($content);
 					echo $content;
+
+					// TODO: gerer cache-expire, expires, ...
+				}
+			}
+		}
+	}
+
+	
+	public function minimifier_css($arguments=[])
+	{
+		//pre($arguments, 1);
+
+		$file_url = $arguments['file_url'];
+
+		$document_root = APP_DIR . '/public';
+
+		if (! is_dir($document_root)) {
+			if (! empty($_SERVER['DOCUMENT_ROOT'])) {
+				$document_root = realpath($_SERVER['DOCUMENT_ROOT']);
+
+			} else {
+				$document_root = '';
+			}
+		}
+
+		if ($document_root) {
+			$file_path = $document_root . $file_url;
+
+			if ($file_path != realpath($file_path) || substr($file_path, -4) != '.css') {
+				// file path invalid or not a css file
+				redirect($file_url);
+				/*
+				header('HTTP/1.1 404 Not Found');
+				echo "ERROR 404";
+				exit;
+				*/
+			}
+
+			if (! is_file($file_path)) {
+				// file not found
+				redirect($file_url);
+				/*
+				header('HTTP/1.1 404 Not Found');
+				echo "ERROR 404";
+				exit;
+				*/
+
+			} else {
+				header('Content-type: text/css');
+
+				if (false) {
+					// NO minimification
+					readfile($file_path);
+
+				} else {
+					// minimification
+					$content = file_get_contents($file_path);
+					$content = MinimifierCss::minify_css($content);
+					echo $content;
+
+					// TODO: gerer cache-expire, expires, ...
 				}
 			}
+
+		} else {
+			// Error document root not found
+			redirect($file_url);
 		}
+	}
 
+	protected function redirectToUrl($url)
+	{
+		$continue_url = "";
+		redirect($continue_url);
+		//return new ResponseRedirect($continue_url, 302);
 	}
 
 }