'delim', 3 => 'entetes', 4 => 'envoyer'] as $k => $option) { if (!empty($args[$k])) { $options[$option] = $args[$k]; } } } $default_options = [ 'delim' => ', ', 'entetes' => null, 'envoyer' => true, 'charset' => null, 'callback' => null, ]; $options = array_merge($default_options, $options); $filename = preg_replace(',[^-_\w]+,', '_', translitteration(textebrut(typo($titre)))); if ($options['delim'] == 'TAB') { $options['delim'] = "\t"; } if (!in_array($options['delim'], [',', ';', "\t"])) { $options['delim'] = ','; } $charset = $GLOBALS['meta']['charset']; $importer_charset = null; $fonction_exporter_champ = null; if ($options['delim'] == ',') { $extension = 'csv'; } else { $extension = 'xls'; # Excel n'accepte pas l'utf-8 ni les entites html... on transcode tout ce qu'on peut $charset = 'iso-8859-1'; # Excel n'accepte pas les retours ligne dans les CSV $fonction_exporter_champ = 'exporter_csv_champ_no_lf'; } // mais si une option charset est explicite, elle a la priorite if (!empty($options['charset'])) { $charset = $options['charset']; } $importer_charset = (($charset === $GLOBALS['meta']['charset']) ? null : $charset); $filename = "$filename.$extension"; $output = ''; $nb = 0; if (!empty($options['entetes']) and is_array($options['entetes'])) { $output = exporter_csv_ligne_numerotee($nb, $options['entetes'], $options['delim'], $importer_charset, $options['callback'], $fonction_exporter_champ); } // les donnees commencent toujours a la ligne 1, qu'il y ait ou non des entetes $nb++; if ($options['envoyer']) { $disposition = ($options['envoyer'] === 'attachment' ? 'attachment' : 'inline'); header("Content-Type: text/comma-separated-values; charset=$charset"); header("Content-Disposition: $disposition; filename=$filename"); // Vider tous les tampons $level = @ob_get_level(); while ($level--) { @ob_end_flush(); } } // si envoyer=='attachment' on passe par un fichier temporaire // sinon on ecrit directement sur stdout if ($options['envoyer'] and $options['envoyer'] !== 'attachment') { $fichier = 'php://output'; } else { $fichier = sous_repertoire(_DIR_CACHE, 'export') . $filename; } $fp = fopen($fichier, 'w'); $length = fwrite($fp, $output); while ($row = is_array($resource) ? array_shift($resource) : sql_fetch($resource)) { $output = exporter_csv_ligne_numerotee($nb, $row, $options['delim'], $importer_charset, $options['callback'], $fonction_exporter_champ); $length += fwrite($fp, $output); $nb++; } fclose($fp); if ($options['envoyer']) { if ($options['envoyer'] === 'attachment') { header("Content-Length: $length"); readfile($fichier); } // si on a envoye inline, c'est deja tout bon exit; } return $fichier; }