@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | **/ |
| 17 | 17 | |
| 18 | 18 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 19 | - return; |
|
| 19 | + return; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | include_spip('inc/charsets'); |
@@ -31,13 +31,13 @@ discard block |
||
| 31 | 31 | * @return string |
| 32 | 32 | */ |
| 33 | 33 | function exporter_csv_champ($champ) { |
| 34 | - #$champ = str_replace("\r", "\n", $champ); |
|
| 35 | - #$champ = preg_replace(",[\n]+,ms", "\n", $champ); |
|
| 36 | - #$champ = str_replace("\n", ", ", $champ); |
|
| 37 | - $champ = preg_replace(',[\s]+,ms', ' ', $champ); |
|
| 38 | - $champ = str_replace('"', '""', $champ); |
|
| 34 | + #$champ = str_replace("\r", "\n", $champ); |
|
| 35 | + #$champ = preg_replace(",[\n]+,ms", "\n", $champ); |
|
| 36 | + #$champ = str_replace("\n", ", ", $champ); |
|
| 37 | + $champ = preg_replace(',[\s]+,ms', ' ', $champ); |
|
| 38 | + $champ = str_replace('"', '""', $champ); |
|
| 39 | 39 | |
| 40 | - return '"' . $champ . '"'; |
|
| 40 | + return '"' . $champ . '"'; |
|
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | /** |
@@ -54,15 +54,15 @@ discard block |
||
| 54 | 54 | * @return string |
| 55 | 55 | */ |
| 56 | 56 | function exporter_csv_ligne_numerotee($nb, $ligne, $delim = ',', $importer_charset = null, ?callable $callback = null) { |
| 57 | - if ($callback) { |
|
| 58 | - $ligne = $callback($nb, $ligne, $delim, $importer_charset); |
|
| 59 | - } |
|
| 60 | - $output = join($delim, array_map('exporter_csv_champ', $ligne)) . "\r\n"; |
|
| 61 | - if ($importer_charset) { |
|
| 62 | - $output = str_replace('’', '\'', $output); |
|
| 63 | - $output = unicode2charset(html2unicode(charset2unicode($output)), $importer_charset); |
|
| 64 | - } |
|
| 65 | - return $output; |
|
| 57 | + if ($callback) { |
|
| 58 | + $ligne = $callback($nb, $ligne, $delim, $importer_charset); |
|
| 59 | + } |
|
| 60 | + $output = join($delim, array_map('exporter_csv_champ', $ligne)) . "\r\n"; |
|
| 61 | + if ($importer_charset) { |
|
| 62 | + $output = str_replace('’', '\'', $output); |
|
| 63 | + $output = unicode2charset(html2unicode(charset2unicode($output)), $importer_charset); |
|
| 64 | + } |
|
| 65 | + return $output; |
|
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | /** |
@@ -74,7 +74,7 @@ discard block |
||
| 74 | 74 | * @return string |
| 75 | 75 | */ |
| 76 | 76 | function exporter_csv_ligne($ligne, $delim = ',', $importer_charset = null) { |
| 77 | - return exporter_csv_ligne_numerotee(null, $ligne, $delim, $importer_charset); |
|
| 77 | + return exporter_csv_ligne_numerotee(null, $ligne, $delim, $importer_charset); |
|
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | /** |
@@ -104,107 +104,107 @@ discard block |
||
| 104 | 104 | */ |
| 105 | 105 | function inc_exporter_csv_dist($titre, $resource, $options = []) { |
| 106 | 106 | |
| 107 | - // support ancienne syntaxe |
|
| 108 | - // inc_exporter_csv_dist($titre, $resource, $delim = ', ', $entetes = null, $envoyer = true) |
|
| 109 | - if (is_string($options)) { |
|
| 110 | - $args = func_get_args(); |
|
| 111 | - $options = []; |
|
| 112 | - foreach ([2 => 'delim', 3 => 'entetes', 4 => 'envoyer'] as $k => $option) { |
|
| 113 | - if (!empty($args[$k])) { |
|
| 114 | - $options[$option] = $args[$k]; |
|
| 115 | - } |
|
| 116 | - } |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - $default_options = [ |
|
| 120 | - 'fichier' => null, // par défaut = $titre |
|
| 121 | - 'extension' => null, // par défaut = choix auto |
|
| 122 | - 'delim' => ',', |
|
| 123 | - 'entetes' => null, |
|
| 124 | - 'envoyer' => true, |
|
| 125 | - 'charset' => null, |
|
| 126 | - 'callback' => null, |
|
| 127 | - ]; |
|
| 128 | - $options = array_merge($default_options, $options); |
|
| 129 | - |
|
| 130 | - // Délimiteur |
|
| 131 | - if ($options['delim'] == 'TAB') { |
|
| 132 | - $options['delim'] = "\t"; |
|
| 133 | - } |
|
| 134 | - if (!in_array($options['delim'], [',', ';', "\t"])) { |
|
| 135 | - $options['delim'] = ','; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - // Nom du fichier : celui indiqué dans les options, sinon le titre |
|
| 139 | - // Normalisation : uniquement les caractères non spéciaux, tirets, underscore et point + remplacer espaces par underscores |
|
| 140 | - $filename = $options['fichier'] ?? translitteration(textebrut(typo($titre))); |
|
| 141 | - $filename = preg_replace([',[^\w\-_\.\s]+,', ',\s+,'], ['', '_'], trim($filename)); |
|
| 142 | - $filename = rtrim($filename, '.'); |
|
| 143 | - |
|
| 144 | - // Extension : celle indiquée en option, sinon choisie selon le délimiteur |
|
| 145 | - // Normalisation : uniquement les charactères non spéciaux |
|
| 146 | - if (!empty($options['extension'])) { |
|
| 147 | - $options['extension'] = preg_replace(',[^\w]+,', '', trim($options['extension'])); |
|
| 148 | - } |
|
| 149 | - $extension = $options['extension'] ?? ($options['delim'] === ',' ? 'csv' : 'xls'); |
|
| 150 | - |
|
| 151 | - // Fichier |
|
| 152 | - $basename = "$filename.$extension"; |
|
| 153 | - |
|
| 154 | - // Charset : celui indiqué en option, sinon celui compatible excel si nécessaire, sinon celui du site |
|
| 155 | - // Excel n'accepte pas l'utf-8 ni les entites html... on transcode tout ce qu'on peut |
|
| 156 | - $charset_site = $GLOBALS['meta']['charset']; |
|
| 157 | - $charset_excel = ($extension === 'xls' ? 'iso-8859-1' : null); |
|
| 158 | - $charset = $options['charset'] ?? $charset_excel ?? $charset_site; |
|
| 159 | - $importer_charset = (($charset === $charset_site) ? null : $charset); |
|
| 160 | - |
|
| 161 | - $output = ''; |
|
| 162 | - $nb = 0; |
|
| 163 | - if (!empty($options['entetes']) and is_array($options['entetes'])) { |
|
| 164 | - $output = exporter_csv_ligne_numerotee($nb, $options['entetes'], $options['delim'], $importer_charset, $options['callback']); |
|
| 165 | - } |
|
| 166 | - // les donnees commencent toujours a la ligne 1, qu'il y ait ou non des entetes |
|
| 167 | - $nb++; |
|
| 168 | - |
|
| 169 | - if ($options['envoyer']) { |
|
| 170 | - $disposition = ($options['envoyer'] === 'attachment' ? 'attachment' : 'inline'); |
|
| 171 | - header("Content-Type: text/comma-separated-values; charset=$charset"); |
|
| 172 | - header("Content-Disposition: $disposition; filename=$basename"); |
|
| 173 | - |
|
| 174 | - // Vider tous les tampons |
|
| 175 | - $level = @ob_get_level(); |
|
| 176 | - while ($level--) { |
|
| 177 | - @ob_end_flush(); |
|
| 178 | - } |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - // si envoyer=='attachment' on passe par un fichier temporaire |
|
| 182 | - // sinon on ecrit directement sur stdout |
|
| 183 | - if ($options['envoyer'] and $options['envoyer'] !== 'attachment') { |
|
| 184 | - $fichier = 'php://output'; |
|
| 185 | - } |
|
| 186 | - else { |
|
| 187 | - $fichier = sous_repertoire(_DIR_CACHE, 'export') . $basename; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - $fp = fopen($fichier, 'w'); |
|
| 191 | - $length = fwrite($fp, $output); |
|
| 192 | - |
|
| 193 | - while ($row = is_array($resource) ? array_shift($resource) : sql_fetch($resource)) { |
|
| 194 | - $output = exporter_csv_ligne_numerotee($nb, $row, $options['delim'], $importer_charset, $options['callback']); |
|
| 195 | - $length += fwrite($fp, $output); |
|
| 196 | - $nb++; |
|
| 197 | - } |
|
| 198 | - fclose($fp); |
|
| 199 | - |
|
| 200 | - if ($options['envoyer']) { |
|
| 201 | - if ($options['envoyer'] === 'attachment') { |
|
| 202 | - header("Content-Length: $length"); |
|
| 203 | - readfile($fichier); |
|
| 204 | - } |
|
| 205 | - // si on a envoye inline, c'est deja tout bon |
|
| 206 | - exit; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - return $fichier; |
|
| 107 | + // support ancienne syntaxe |
|
| 108 | + // inc_exporter_csv_dist($titre, $resource, $delim = ', ', $entetes = null, $envoyer = true) |
|
| 109 | + if (is_string($options)) { |
|
| 110 | + $args = func_get_args(); |
|
| 111 | + $options = []; |
|
| 112 | + foreach ([2 => 'delim', 3 => 'entetes', 4 => 'envoyer'] as $k => $option) { |
|
| 113 | + if (!empty($args[$k])) { |
|
| 114 | + $options[$option] = $args[$k]; |
|
| 115 | + } |
|
| 116 | + } |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + $default_options = [ |
|
| 120 | + 'fichier' => null, // par défaut = $titre |
|
| 121 | + 'extension' => null, // par défaut = choix auto |
|
| 122 | + 'delim' => ',', |
|
| 123 | + 'entetes' => null, |
|
| 124 | + 'envoyer' => true, |
|
| 125 | + 'charset' => null, |
|
| 126 | + 'callback' => null, |
|
| 127 | + ]; |
|
| 128 | + $options = array_merge($default_options, $options); |
|
| 129 | + |
|
| 130 | + // Délimiteur |
|
| 131 | + if ($options['delim'] == 'TAB') { |
|
| 132 | + $options['delim'] = "\t"; |
|
| 133 | + } |
|
| 134 | + if (!in_array($options['delim'], [',', ';', "\t"])) { |
|
| 135 | + $options['delim'] = ','; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + // Nom du fichier : celui indiqué dans les options, sinon le titre |
|
| 139 | + // Normalisation : uniquement les caractères non spéciaux, tirets, underscore et point + remplacer espaces par underscores |
|
| 140 | + $filename = $options['fichier'] ?? translitteration(textebrut(typo($titre))); |
|
| 141 | + $filename = preg_replace([',[^\w\-_\.\s]+,', ',\s+,'], ['', '_'], trim($filename)); |
|
| 142 | + $filename = rtrim($filename, '.'); |
|
| 143 | + |
|
| 144 | + // Extension : celle indiquée en option, sinon choisie selon le délimiteur |
|
| 145 | + // Normalisation : uniquement les charactères non spéciaux |
|
| 146 | + if (!empty($options['extension'])) { |
|
| 147 | + $options['extension'] = preg_replace(',[^\w]+,', '', trim($options['extension'])); |
|
| 148 | + } |
|
| 149 | + $extension = $options['extension'] ?? ($options['delim'] === ',' ? 'csv' : 'xls'); |
|
| 150 | + |
|
| 151 | + // Fichier |
|
| 152 | + $basename = "$filename.$extension"; |
|
| 153 | + |
|
| 154 | + // Charset : celui indiqué en option, sinon celui compatible excel si nécessaire, sinon celui du site |
|
| 155 | + // Excel n'accepte pas l'utf-8 ni les entites html... on transcode tout ce qu'on peut |
|
| 156 | + $charset_site = $GLOBALS['meta']['charset']; |
|
| 157 | + $charset_excel = ($extension === 'xls' ? 'iso-8859-1' : null); |
|
| 158 | + $charset = $options['charset'] ?? $charset_excel ?? $charset_site; |
|
| 159 | + $importer_charset = (($charset === $charset_site) ? null : $charset); |
|
| 160 | + |
|
| 161 | + $output = ''; |
|
| 162 | + $nb = 0; |
|
| 163 | + if (!empty($options['entetes']) and is_array($options['entetes'])) { |
|
| 164 | + $output = exporter_csv_ligne_numerotee($nb, $options['entetes'], $options['delim'], $importer_charset, $options['callback']); |
|
| 165 | + } |
|
| 166 | + // les donnees commencent toujours a la ligne 1, qu'il y ait ou non des entetes |
|
| 167 | + $nb++; |
|
| 168 | + |
|
| 169 | + if ($options['envoyer']) { |
|
| 170 | + $disposition = ($options['envoyer'] === 'attachment' ? 'attachment' : 'inline'); |
|
| 171 | + header("Content-Type: text/comma-separated-values; charset=$charset"); |
|
| 172 | + header("Content-Disposition: $disposition; filename=$basename"); |
|
| 173 | + |
|
| 174 | + // Vider tous les tampons |
|
| 175 | + $level = @ob_get_level(); |
|
| 176 | + while ($level--) { |
|
| 177 | + @ob_end_flush(); |
|
| 178 | + } |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + // si envoyer=='attachment' on passe par un fichier temporaire |
|
| 182 | + // sinon on ecrit directement sur stdout |
|
| 183 | + if ($options['envoyer'] and $options['envoyer'] !== 'attachment') { |
|
| 184 | + $fichier = 'php://output'; |
|
| 185 | + } |
|
| 186 | + else { |
|
| 187 | + $fichier = sous_repertoire(_DIR_CACHE, 'export') . $basename; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + $fp = fopen($fichier, 'w'); |
|
| 191 | + $length = fwrite($fp, $output); |
|
| 192 | + |
|
| 193 | + while ($row = is_array($resource) ? array_shift($resource) : sql_fetch($resource)) { |
|
| 194 | + $output = exporter_csv_ligne_numerotee($nb, $row, $options['delim'], $importer_charset, $options['callback']); |
|
| 195 | + $length += fwrite($fp, $output); |
|
| 196 | + $nb++; |
|
| 197 | + } |
|
| 198 | + fclose($fp); |
|
| 199 | + |
|
| 200 | + if ($options['envoyer']) { |
|
| 201 | + if ($options['envoyer'] === 'attachment') { |
|
| 202 | + header("Content-Length: $length"); |
|
| 203 | + readfile($fichier); |
|
| 204 | + } |
|
| 205 | + // si on a envoye inline, c'est deja tout bon |
|
| 206 | + exit; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + return $fichier; |
|
| 210 | 210 | } |