]> git.agnieray.net Git - galette.git/commitdiff
Rework CSV routes responses to fix a strange content added in contents
authorJohan Cwiklinski <jcwiklinski@teclib.com>
Sat, 5 Oct 2019 05:42:42 +0000 (07:42 +0200)
committerJohan Cwiklinski <jcwiklinski@teclib.com>
Sat, 5 Oct 2019 05:42:42 +0000 (07:42 +0200)
Fix a wrong data type

galette/includes/routes/management.routes.php
galette/includes/routes/members.routes.php
galette/lib/Galette/IO/CsvOut.php

index 8335f865db6fe7f2f3f8d033e7f5e4d423527584..4ed337472cce5bcfda3226391919e8347d2779e3 100644 (file)
@@ -1366,9 +1366,18 @@ $app->get(
                 $response = $this->response->withHeader('Content-Description', 'File Transfer')
                     ->withHeader('Content-Type', 'text/csv')
                     ->withHeader('Content-Disposition', 'attachment;filename="' . $filename . '"')
-                    ->withHeader('Pragma', 'no-cache');
-                $response->write(readfile($filepath));
-                return $response;
+                    ->withHeader('Pragma', 'no-cache')
+                    ->withHeader('Content-Transfer-Encoding', 'binary')
+                    ->withHeader('Expires', '0')
+                    ->withHeader('Cache-Control', 'must-revalidate')
+                    ->withHeader('Pragma', 'public')
+                    ->withHeader('Content-Length', filesize($filepath));
+
+                $stream = fopen('php://memory', 'r+');
+                fwrite($stream, file_get_contents($filepath));
+                rewind($stream);
+
+                return $response->withBody(new \Slim\Http\Stream($stream));
             } else {
                 Analog::log(
                     'A request has been made to get an ' . $args['type'] . ' file named `' .
@@ -1608,9 +1617,17 @@ $app->get(
         $response = $this->response->withHeader('Content-Description', 'File Transfer')
             ->withHeader('Content-Type', 'text/csv')
             ->withHeader('Content-Disposition', 'attachment;filename="' . $filename . '"')
-            ->withHeader('Pragma', 'no-cache');
-        $response->write($res);
-        return $response;
+            ->withHeader('Pragma', 'no-cache')
+            ->withHeader('Content-Transfer-Encoding', 'binary')
+            ->withHeader('Expires', '0')
+            ->withHeader('Cache-Control', 'must-revalidate')
+            ->withHeader('Pragma', 'public');
+
+        $stream = fopen('php://memory', 'r+');
+        fwrite($stream, $res);
+        rewind($stream);
+
+        return $response->withBody(new \Slim\Http\Stream($stream));
     }
 )->setName('getImportModel')->add($authenticate);
 
index 3a518a6468ebafd5cdbda9bd2574263b7dd3c398..d962c9386d0d31c1700904ec9a60af476f432566 100644 (file)
@@ -303,13 +303,23 @@ $app->get(
             );
         }
 
-        if (file_exists(CsvOut::DEFAULT_DIRECTORY . $filename)) {
+        $filepath = CsvOut::DEFAULT_DIRECTORY . $filename;
+        if (file_exists($filepath)) {
             $response = $this->response->withHeader('Content-Description', 'File Transfer')
                 ->withHeader('Content-Type', 'text/csv')
                 ->withHeader('Content-Disposition', 'attachment;filename="' . $filename . '"')
-                ->withHeader('Pragma', 'no-cache');
-            $response->write(readfile(CsvOut::DEFAULT_DIRECTORY . $filename));
-            return $response;
+                ->withHeader('Pragma', 'no-cache')
+                ->withHeader('Content-Transfer-Encoding', 'binary')
+                ->withHeader('Expires', '0')
+                ->withHeader('Cache-Control', 'must-revalidate')
+                ->withHeader('Pragma', 'public')
+                ->withHeader('Content-Length', filesize($filepath));
+
+            $stream = fopen('php://memory', 'r+');
+            fwrite($stream, file_get_contents($filepath));
+            rewind($stream);
+
+            return $response->withBody(new \Slim\Http\Stream($stream));
         } else {
             Analog::log(
                 'A request has been made to get an exported file named `' .
index 1b707f444321c72eebcf9753d20c0ccda4e1b045..e59e2f95db283f707035b0b028d500eeef816ca1 100644 (file)
@@ -110,7 +110,7 @@ class CsvOut extends Csv
         $this->current_line = 0;
 
         $fields = array();
-        if ($titles && !count($titles)>1) {
+        if ($titles && !is_array($titles)) {
             foreach (array_key($this->rs) as $field) {
                 $fields[] = $this->quote . str_replace(
                     $this->quote,