Max F 5 anos atrás
pai
commit
edb31deb83

+ 2 - 1
src/App/Middlewares/ErrorHandler.php

@@ -37,12 +37,13 @@ class ErrorHandler
             $error_code = $e->getCode();
             $error_message = $e->getMessage();
 
-
+            /*
             $is_response = is_a($e, Response::class);
             if ($is_response) {
                 // exception is in reality a Response
                 return $e;
             }
+            */
 
             /*
             if ($error_code == 404) {

+ 2 - 1
src/App/Middlewares/ForceHttps.php

@@ -26,7 +26,8 @@ class ForceHttps
 			
 			if (empty($this->redirect_domains) || in_array($request->SERVER['SERVER_NAME'], $this->redirect_domains)) {
 				$redirect_url = 'https://' . $request->SERVER['SERVER_NAME'] . $request->SERVER['REQUEST_URI'];
-				return new ResponseRedirect($redirect_url, $this->redirect_status);
+				
+				return $response->redirect($redirect_url, $this->redirect_status);
 			}
 
 		}

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

@@ -11,13 +11,15 @@ class MinimifierHtml
     protected $minimify_html;
     protected $minimify_external_js;
     protected $minimify_external_css;
+    protected $content_types;
 
     
-    public function __construct($minimify_html=true, $minimify_external_js=true, $minimify_external_css=true)
+    public function __construct($minimify_html=true, $minimify_external_js=true, $minimify_external_css=true, $content_types=['text/html'])
     {
         $this->minimify_html = $minimify_html;
         $this->minimify_external_js = $minimify_external_js;
         $this->minimify_external_css = $minimify_external_css;
+        $this->content_types = $content_types;
     }
 
 
@@ -29,7 +31,9 @@ class MinimifierHtml
         $content_type = $response->getContentType();
         $content_type_short = explode(';', $content_type)[0];
 
-        if ($content_type_short !== 'text/html') {
+
+        if (! empty($this->content_types) && ! in_array($content_type_short, $this->content_types)) {
+            // restriction to the selected content_types
             return $response;
         }
 

+ 2 - 1
src/App/Middlewares/RedirectToDomain.php

@@ -26,7 +26,8 @@ class RedirectToDomain
 
 			if (empty($this->redirect_domains) || in_array($this->target_domain, $this->redirect_domains)) {
 				$redirect_url = 'https://' . $this->target_domain . $request->SERVER['REQUEST_URI'];
-				return new ResponseRedirect($redirect_url, $this->redirect_status);
+				
+				return $response->redirect($redirect_url, $this->redirect_status);
 			}
 
 		}

+ 4 - 1
src/App/Middlewares/UrlRouter.php

@@ -48,16 +48,19 @@ class UrlRouter
 			$error_code = $e->getCode();
 			$error_message = $e->getMessage();
 
+			/*
 			$is_response = is_a($e, Response::class);
 			if ($is_response) {
 				// exception is in reality a Response
 				return $e;
 			}
+			*/
 
 			if (in_array($error_code, [301, 302, 310])) {
 				// if $error_code is a redirection
 				$url = $error_message;
-				return new ResponseRedirect($url, $error_code);
+				//return new ResponseRedirect($url, $error_code);
+				return $response->redirect($url, $error_code);
 			}
 
 			// ERROR 404

+ 103 - 2
src/App/Response.php

@@ -7,7 +7,7 @@ use KarmaFW\App;
 // TODO: a remplacer par ou rendre compatible avec GuzzleHttp\Psr7\Response
 
 
-class Response extends \Exception
+class Response /* extends \Exception */
 {
 	protected $headers = [];
 	protected $body = '';
@@ -18,6 +18,9 @@ class Response extends \Exception
 	protected $protocol = null;
 	protected $template_path = null;
 	protected $template_data = [];
+	protected $redirect_url = null;
+	protected $download_file_name = null;
+	protected $download_file_path = null;
 
 
 	/* public */ const http_status_codes = [
@@ -178,8 +181,14 @@ class Response extends \Exception
 				->setStatus($status);
 	}
 
-	public function setCsv($body, $status=200, $content_type='text/csv')
+	public function setCsv($body, $download_file_name=null, $status=200, $content_type='text/csv')
 	{
+		if (is_array($body)) {
+			// transform array to csv
+			$body = get_csv($body);
+		}
+		$this->download_file_name = $download_file_name;
+
 		return $this->setBody($body)
 				->setContentType($content_type)
 				->setStatus($status);
@@ -244,6 +253,11 @@ class Response extends \Exception
 			return;
 		}
 
+		if ($this->status === 200 && empty($this->body)) {
+			// No content
+			$this->setStatus(204);
+		}
+
 		if (! empty($this->status)) {
 			$reasonPhrase = empty($this->reasonPhrase) ? "Unknown http status" : trim($this->reasonPhrase);
 			$this->headers['X-Status'] = $this->status . ' ' . $reasonPhrase;
@@ -251,6 +265,43 @@ class Response extends \Exception
 			header('HTTP/1.0 ' . $this->status . ' ' . $reasonPhrase);
 		}
 
+
+		if ($this->download_file_name) {
+			// Download
+
+			$content_type = $this->getContentType();
+
+			if ($this->download_file_path) {
+				// DOWNLOAD A LOCAL FILE
+
+				if (! is_file($this->download_file_path)) {
+					// File not found
+					return $this->setHtml("File not found", 404);
+
+				} else {
+					// File exists
+					if (empty($content_type)) {
+						$content_type = mime_content_type($this->download_file_path);
+					}
+
+					$this->headers['Content-Length'] = filesize($this->download_file_path);
+				}
+
+			} else {
+				// DOWNLOAD A VIRTUAL FILE
+
+			}
+
+
+			if (empty($content_type)) {
+				$content_type = "application/octet-stream";
+			}
+			$this->setContentType($content_type);
+
+			$this->headers['Content-Transfer-Encoding'] = "Binary";
+			$this->headers['Content-disposition'] = 'attachment; filename="' . basename($this->download_file_name) . '"';
+		}
+
 		if (empty($this->headers['Content-Type']) && ! empty($this->content_type)) {
 			$this->headers['Content-Type'] = $this->content_type;
 		}
@@ -277,11 +328,61 @@ class Response extends \Exception
 			$this->sendHeaders();
 		}
 
+		if ($this->download_file_name) {
+			// Download
+
+			if ($this->download_file_path) {
+				// Download a local file
+				if (is_file($this->download_file_path)) {
+					readfile($this->download_file_path);
+
+				} else {
+					// 404
+					echo "Error 404: file not found";
+				}
+
+			} else {
+				// Download a virtual file
+
+			}
+
+		} else {
+			// Echo HTML
+		}
+
+
+		// Echo HTML or Virtual file
 		if (strlen($this->body) > 0) {
 			echo $this->body;
+
+		} else {
+			// No content
+			// TODO: renvoyer code 204 ?
 		}
 
 		return $this;
 	}
 
+
+	public function redirect($redirect_url, $status=302)
+	{
+		$this->redirect_url = $redirect_url;
+
+		$this->addHeader('Location', $redirect_url)
+			->setStatus($status)
+			->setBody('');
+
+		return $this;
+	}
+
+
+	public function error404($body='', $content_type='text/html')
+	{
+		$this->setStatus(404)
+			->setContentType($content_type)
+			->setBody($body);
+
+		return $this;
+	}
+
 }

+ 6 - 14
src/Controllers/MinimifierController.php

@@ -6,8 +6,6 @@ use \KarmaFW\Routing\Controllers\WebAppController;
 use \KarmaFW\App\Middlewares\MinimifierJs;
 use \KarmaFW\App\Middlewares\MinimifierCss;
 
-use \KarmaFW\App\ResponseRedirect;
-
 
 class MinimifierController extends WebAppController
 {
@@ -32,14 +30,12 @@ class MinimifierController extends WebAppController
 
 			if ($file_path != realpath($file_path) || substr($file_path, -3) != '.js') {
 				// file path invalid or not a js file
-				return $this->response->setStatus(404)->setHtml('');
-				//return new ResponseRedirect($file_url);
+				return $this->response->error404("Invalid file");
 			}
 
 			if (! is_file($file_path)) {
 				// file not found
-				return $this->response->setStatus(404)->setHtml('');
-				//return new ResponseRedirect($file_url);
+				return $this->response->error404("File not found");
 
 			} else {
 				if (false) {
@@ -68,8 +64,7 @@ class MinimifierController extends WebAppController
 
 		} else {
 			// Error document root not found
-			return $this->response->setStatus(404)->setHtml('');
-			//return new ResponseRedirect($file_url);
+			return $this->response->error404("Root not found");
 		}
 	}
 
@@ -94,14 +89,12 @@ class MinimifierController extends WebAppController
 
 			if ($file_path != realpath($file_path) || substr($file_path, -4) != '.css') {
 				// file path invalid or not a css file
-				return $this->response->setStatus(404)->setHtml('');
-				//return new ResponseRedirect($file_url);
+				return $this->response->error404("Invalid file");
 			}
 
 			if (! is_file($file_path)) {
 				// file not found
-				return $this->response->setStatus(404)->setHtml('');
-				//return new ResponseRedirect($file_url);
+				return $this->response->error404("File not found");
 
 			} else {
 				if (false) {
@@ -130,8 +123,7 @@ class MinimifierController extends WebAppController
 
 		} else {
 			// Error document root not found
-			return $this->response->setStatus(404)->setHtml('');
-			//return new ResponseRedirect($file_url);
+			return $this->response->error404("Root not found");
 		}
 	}
 

+ 5 - 3
src/helpers/helpers_array.php

@@ -253,7 +253,7 @@ if (! function_exists('exportToCsvFile')) {
 	function exportToCsvFile($rows, $export_filename=null, $fields=null) {
 		$csv_content = get_csv($rows, $fields);
 
-		/*
+		
 		if (! empty($export_filename)) {
 			// download file
 			header('Content-Type: text/csv');
@@ -267,9 +267,9 @@ if (! function_exists('exportToCsvFile')) {
 
 		echo $csv_content;
 		exit;
-		*/
-
+		
 
+		/*
 		if (! empty($export_filename)) {
 			$content_type = 'text/csv';
 			$headers = [
@@ -284,6 +284,8 @@ if (! function_exists('exportToCsvFile')) {
 		}
 
 		throw new ResponseText($csv_content, 200, $content_type, $headers);
+		*/
+
 	}
 }