@@ -34,12 +34,12 @@ discard block |
||
| 34 | 34 | * Importe un texte de CSV dans un charset et le passe dans le charset du site (utf8 probablement) |
| 35 | 35 | * Les CSV peuvent sous ms@@@ avoir le charset 'iso-8859-1', mais pasfois aussi 'windows-1252' :/ |
| 36 | 36 | * |
| 37 | - * @param mixed $texte |
|
| 37 | + * @param string $texte |
|
| 38 | 38 | * @param bool|string $definir_charset_source |
| 39 | 39 | * false : ne fait rien |
| 40 | 40 | * string : utilisera pour les prochains imports le charset indiqué |
| 41 | 41 | * true : remet le charset d'import par défaut de la fonction |
| 42 | - * @return array |
|
| 42 | + * @return string |
|
| 43 | 43 | */ |
| 44 | 44 | function importer_csv_importcharset($texte, $definir_charset_source = false) { |
| 45 | 45 | // le plus frequent, en particulier avec les trucs de ms@@@ |
@@ -83,7 +83,7 @@ discard block |
||
| 83 | 83 | * @param int $len |
| 84 | 84 | * @param string $charset_source |
| 85 | 85 | * Permet de définir un charset source du CSV, si différent de utf-8 ou iso-8859-1 |
| 86 | - * @return array |
|
| 86 | + * @return boolean |
|
| 87 | 87 | */ |
| 88 | 88 | function inc_importer_csv_dist($file, $head = false, $delim = ',', $enclos = '"', $len = 10000, $charset_source = '') { |
| 89 | 89 | $return = false; |
@@ -11,7 +11,7 @@ discard block |
||
| 11 | 11 | \***************************************************************************/ |
| 12 | 12 | |
| 13 | 13 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 14 | - return; |
|
| 14 | + return; |
|
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | include_spip('inc/charsets'); |
@@ -42,22 +42,22 @@ discard block |
||
| 42 | 42 | * @return array |
| 43 | 43 | */ |
| 44 | 44 | function importer_csv_importcharset($texte, $definir_charset_source = false) { |
| 45 | - // le plus frequent, en particulier avec les trucs de ms@@@ |
|
| 46 | - static $charset_source = 'iso-8859-1'; |
|
| 47 | - if ($definir_charset_source) { |
|
| 48 | - if ($definir_charset_source === true) { |
|
| 49 | - $charset_source = 'iso-8859-1'; |
|
| 50 | - } else { |
|
| 51 | - $charset_source = $definir_charset_source; |
|
| 52 | - } |
|
| 53 | - } |
|
| 54 | - // mais open-office sait faire mieux, donc mefiance ! |
|
| 55 | - if (is_utf8($texte)) { |
|
| 56 | - $charset = 'utf-8'; |
|
| 57 | - } else { |
|
| 58 | - $charset = $charset_source; |
|
| 59 | - } |
|
| 60 | - return importer_charset($texte, $charset); |
|
| 45 | + // le plus frequent, en particulier avec les trucs de ms@@@ |
|
| 46 | + static $charset_source = 'iso-8859-1'; |
|
| 47 | + if ($definir_charset_source) { |
|
| 48 | + if ($definir_charset_source === true) { |
|
| 49 | + $charset_source = 'iso-8859-1'; |
|
| 50 | + } else { |
|
| 51 | + $charset_source = $definir_charset_source; |
|
| 52 | + } |
|
| 53 | + } |
|
| 54 | + // mais open-office sait faire mieux, donc mefiance ! |
|
| 55 | + if (is_utf8($texte)) { |
|
| 56 | + $charset = 'utf-8'; |
|
| 57 | + } else { |
|
| 58 | + $charset = $charset_source; |
|
| 59 | + } |
|
| 60 | + return importer_charset($texte, $charset); |
|
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | /** |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | * @return string |
| 69 | 69 | */ |
| 70 | 70 | function importer_csv_nettoie_key($key) { |
| 71 | - return translitteration($key); |
|
| 71 | + return translitteration($key); |
|
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | /** |
@@ -86,52 +86,52 @@ discard block |
||
| 86 | 86 | * @return array |
| 87 | 87 | */ |
| 88 | 88 | function inc_importer_csv_dist($file, $head = false, $delim = ',', $enclos = '"', $len = 10000, $charset_source = '') { |
| 89 | - $return = false; |
|
| 90 | - if (@file_exists($file) |
|
| 91 | - and $handle = fopen($file, 'r')) { |
|
| 92 | - if ($charset_source) { |
|
| 93 | - importer_csv_importcharset('', $charset_source); |
|
| 94 | - } |
|
| 95 | - if ($head) { |
|
| 96 | - $header = fgetcsv($handle, $len, $delim, $enclos); |
|
| 97 | - if ($header) { |
|
| 98 | - $header = array_map('importer_csv_importcharset', $header); |
|
| 99 | - $header = array_map('importer_csv_nettoie_key', $header); |
|
| 100 | - $header_type = array(); |
|
| 101 | - foreach ($header as $heading) { |
|
| 102 | - if (!isset($header_type[$heading])) { |
|
| 103 | - $header_type[$heading] = "scalar"; |
|
| 104 | - } else { |
|
| 105 | - $header_type[$heading] = "array"; |
|
| 106 | - } |
|
| 107 | - } |
|
| 108 | - } |
|
| 109 | - } |
|
| 110 | - while (($data = fgetcsv($handle, $len, $delim, $enclos)) !== false) { |
|
| 111 | - $data = array_map('importer_csv_importcharset', $data); |
|
| 112 | - if ($head and isset($header)) { |
|
| 113 | - $row = array(); |
|
| 114 | - foreach ($header as $key => $heading) { |
|
| 115 | - if ($header_type[$heading] == "array") { |
|
| 116 | - if (!isset($row[$heading])) { |
|
| 117 | - $row[$heading] = array(); |
|
| 118 | - } |
|
| 119 | - if (isset($data[$key]) and strlen($data[$key])) { |
|
| 120 | - $row[$heading][] = $data[$key]; |
|
| 121 | - } |
|
| 122 | - } else { |
|
| 123 | - $row[$heading] = (isset($data[$key])) ? $data[$key] : ''; |
|
| 124 | - } |
|
| 125 | - } |
|
| 126 | - $return[] = $row; |
|
| 127 | - } else { |
|
| 128 | - $return[] = $data; |
|
| 129 | - } |
|
| 130 | - } |
|
| 131 | - if ($charset_source) { |
|
| 132 | - importer_csv_importcharset('', true); |
|
| 133 | - } |
|
| 134 | - } |
|
| 89 | + $return = false; |
|
| 90 | + if (@file_exists($file) |
|
| 91 | + and $handle = fopen($file, 'r')) { |
|
| 92 | + if ($charset_source) { |
|
| 93 | + importer_csv_importcharset('', $charset_source); |
|
| 94 | + } |
|
| 95 | + if ($head) { |
|
| 96 | + $header = fgetcsv($handle, $len, $delim, $enclos); |
|
| 97 | + if ($header) { |
|
| 98 | + $header = array_map('importer_csv_importcharset', $header); |
|
| 99 | + $header = array_map('importer_csv_nettoie_key', $header); |
|
| 100 | + $header_type = array(); |
|
| 101 | + foreach ($header as $heading) { |
|
| 102 | + if (!isset($header_type[$heading])) { |
|
| 103 | + $header_type[$heading] = "scalar"; |
|
| 104 | + } else { |
|
| 105 | + $header_type[$heading] = "array"; |
|
| 106 | + } |
|
| 107 | + } |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | + while (($data = fgetcsv($handle, $len, $delim, $enclos)) !== false) { |
|
| 111 | + $data = array_map('importer_csv_importcharset', $data); |
|
| 112 | + if ($head and isset($header)) { |
|
| 113 | + $row = array(); |
|
| 114 | + foreach ($header as $key => $heading) { |
|
| 115 | + if ($header_type[$heading] == "array") { |
|
| 116 | + if (!isset($row[$heading])) { |
|
| 117 | + $row[$heading] = array(); |
|
| 118 | + } |
|
| 119 | + if (isset($data[$key]) and strlen($data[$key])) { |
|
| 120 | + $row[$heading][] = $data[$key]; |
|
| 121 | + } |
|
| 122 | + } else { |
|
| 123 | + $row[$heading] = (isset($data[$key])) ? $data[$key] : ''; |
|
| 124 | + } |
|
| 125 | + } |
|
| 126 | + $return[] = $row; |
|
| 127 | + } else { |
|
| 128 | + $return[] = $data; |
|
| 129 | + } |
|
| 130 | + } |
|
| 131 | + if ($charset_source) { |
|
| 132 | + importer_csv_importcharset('', true); |
|
| 133 | + } |
|
| 134 | + } |
|
| 135 | 135 | |
| 136 | - return $return; |
|
| 136 | + return $return; |
|
| 137 | 137 | } |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | **/ |
| 18 | 18 | |
| 19 | 19 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 20 | - return; |
|
| 20 | + return; |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | include_spip('inc/charsets'); |
@@ -32,13 +32,13 @@ discard block |
||
| 32 | 32 | * @return string |
| 33 | 33 | */ |
| 34 | 34 | function exporter_csv_champ($champ) { |
| 35 | - #$champ = str_replace("\r", "\n", $champ); |
|
| 36 | - #$champ = preg_replace(",[\n]+,ms", "\n", $champ); |
|
| 37 | - #$champ = str_replace("\n", ", ", $champ); |
|
| 38 | - $champ = preg_replace(',[\s]+,ms', ' ', $champ); |
|
| 39 | - $champ = str_replace('"', '""', $champ); |
|
| 35 | + #$champ = str_replace("\r", "\n", $champ); |
|
| 36 | + #$champ = preg_replace(",[\n]+,ms", "\n", $champ); |
|
| 37 | + #$champ = str_replace("\n", ", ", $champ); |
|
| 38 | + $champ = preg_replace(',[\s]+,ms', ' ', $champ); |
|
| 39 | + $champ = str_replace('"', '""', $champ); |
|
| 40 | 40 | |
| 41 | - return '"' . $champ . '"'; |
|
| 41 | + return '"' . $champ . '"'; |
|
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | /** |
@@ -53,11 +53,11 @@ discard block |
||
| 53 | 53 | * @return string |
| 54 | 54 | */ |
| 55 | 55 | function exporter_csv_ligne($ligne, $delim = ',', $importer_charset = null) { |
| 56 | - $output = join($delim, array_map('exporter_csv_champ', $ligne)) . "\r\n"; |
|
| 57 | - if ($importer_charset) { |
|
| 58 | - $output = unicode2charset(html2unicode(charset2unicode($output)), $importer_charset); |
|
| 59 | - } |
|
| 60 | - return $output; |
|
| 56 | + $output = join($delim, array_map('exporter_csv_champ', $ligne)) . "\r\n"; |
|
| 57 | + if ($importer_charset) { |
|
| 58 | + $output = unicode2charset(html2unicode(charset2unicode($output)), $importer_charset); |
|
| 59 | + } |
|
| 60 | + return $output; |
|
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | /** |
@@ -83,53 +83,53 @@ discard block |
||
| 83 | 83 | */ |
| 84 | 84 | function inc_exporter_csv_dist($titre, $resource, $delim = ', ', $entetes = null, $envoyer = true) { |
| 85 | 85 | |
| 86 | - $filename = preg_replace(',[^-_\w]+,', '_', translitteration(textebrut(typo($titre)))); |
|
| 87 | - |
|
| 88 | - if ($delim == 'TAB') { |
|
| 89 | - $delim = "\t"; |
|
| 90 | - } |
|
| 91 | - if (!in_array($delim, array(',', ';', "\t"))) { |
|
| 92 | - $delim = ','; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - $charset = $GLOBALS['meta']['charset']; |
|
| 96 | - $importer_charset = null; |
|
| 97 | - if ($delim == ',') { |
|
| 98 | - $extension = 'csv'; |
|
| 99 | - } else { |
|
| 100 | - $extension = 'xls'; |
|
| 101 | - # Excel n'accepte pas l'utf-8 ni les entites html... on transcode tout ce qu'on peut |
|
| 102 | - $importer_charset = $charset = 'iso-8859-1'; |
|
| 103 | - } |
|
| 104 | - $filename = "$filename.$extension"; |
|
| 105 | - |
|
| 106 | - $output = ''; |
|
| 107 | - if ($entetes and is_array($entetes) and count($entetes)) { |
|
| 108 | - $output = exporter_csv_ligne($entetes, $delim, $importer_charset); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - // on passe par un fichier temporaire qui permet de ne pas saturer la memoire |
|
| 112 | - // avec les gros exports |
|
| 113 | - $fichier = sous_repertoire(_DIR_CACHE, 'export') . $filename; |
|
| 114 | - $fp = fopen($fichier, 'w'); |
|
| 115 | - $length = fwrite($fp, $output); |
|
| 116 | - |
|
| 117 | - while ($row = is_array($resource) ? array_shift($resource) : sql_fetch($resource)) { |
|
| 118 | - $output = exporter_csv_ligne($row, $delim, $importer_charset); |
|
| 119 | - $length += fwrite($fp, $output); |
|
| 120 | - } |
|
| 121 | - fclose($fp); |
|
| 122 | - |
|
| 123 | - if ($envoyer) { |
|
| 124 | - header("Content-Type: text/comma-separated-values; charset=$charset"); |
|
| 125 | - header("Content-Disposition: attachment; filename=$filename"); |
|
| 126 | - //non supporte |
|
| 127 | - //header("Content-Type: text/plain; charset=$charset"); |
|
| 128 | - header("Content-Length: $length"); |
|
| 129 | - ob_clean(); |
|
| 130 | - flush(); |
|
| 131 | - readfile($fichier); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - return $fichier; |
|
| 86 | + $filename = preg_replace(',[^-_\w]+,', '_', translitteration(textebrut(typo($titre)))); |
|
| 87 | + |
|
| 88 | + if ($delim == 'TAB') { |
|
| 89 | + $delim = "\t"; |
|
| 90 | + } |
|
| 91 | + if (!in_array($delim, array(',', ';', "\t"))) { |
|
| 92 | + $delim = ','; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + $charset = $GLOBALS['meta']['charset']; |
|
| 96 | + $importer_charset = null; |
|
| 97 | + if ($delim == ',') { |
|
| 98 | + $extension = 'csv'; |
|
| 99 | + } else { |
|
| 100 | + $extension = 'xls'; |
|
| 101 | + # Excel n'accepte pas l'utf-8 ni les entites html... on transcode tout ce qu'on peut |
|
| 102 | + $importer_charset = $charset = 'iso-8859-1'; |
|
| 103 | + } |
|
| 104 | + $filename = "$filename.$extension"; |
|
| 105 | + |
|
| 106 | + $output = ''; |
|
| 107 | + if ($entetes and is_array($entetes) and count($entetes)) { |
|
| 108 | + $output = exporter_csv_ligne($entetes, $delim, $importer_charset); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + // on passe par un fichier temporaire qui permet de ne pas saturer la memoire |
|
| 112 | + // avec les gros exports |
|
| 113 | + $fichier = sous_repertoire(_DIR_CACHE, 'export') . $filename; |
|
| 114 | + $fp = fopen($fichier, 'w'); |
|
| 115 | + $length = fwrite($fp, $output); |
|
| 116 | + |
|
| 117 | + while ($row = is_array($resource) ? array_shift($resource) : sql_fetch($resource)) { |
|
| 118 | + $output = exporter_csv_ligne($row, $delim, $importer_charset); |
|
| 119 | + $length += fwrite($fp, $output); |
|
| 120 | + } |
|
| 121 | + fclose($fp); |
|
| 122 | + |
|
| 123 | + if ($envoyer) { |
|
| 124 | + header("Content-Type: text/comma-separated-values; charset=$charset"); |
|
| 125 | + header("Content-Disposition: attachment; filename=$filename"); |
|
| 126 | + //non supporte |
|
| 127 | + //header("Content-Type: text/plain; charset=$charset"); |
|
| 128 | + header("Content-Length: $length"); |
|
| 129 | + ob_clean(); |
|
| 130 | + flush(); |
|
| 131 | + readfile($fichier); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + return $fichier; |
|
| 135 | 135 | } |
@@ -38,7 +38,7 @@ discard block |
||
| 38 | 38 | $champ = preg_replace(',[\s]+,ms', ' ', $champ); |
| 39 | 39 | $champ = str_replace('"', '""', $champ); |
| 40 | 40 | |
| 41 | - return '"' . $champ . '"'; |
|
| 41 | + return '"'.$champ.'"'; |
|
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | /** |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | * @return string |
| 54 | 54 | */ |
| 55 | 55 | function exporter_csv_ligne($ligne, $delim = ',', $importer_charset = null) { |
| 56 | - $output = join($delim, array_map('exporter_csv_champ', $ligne)) . "\r\n"; |
|
| 56 | + $output = join($delim, array_map('exporter_csv_champ', $ligne))."\r\n"; |
|
| 57 | 57 | if ($importer_charset) { |
| 58 | 58 | $output = unicode2charset(html2unicode(charset2unicode($output)), $importer_charset); |
| 59 | 59 | } |
@@ -110,7 +110,7 @@ discard block |
||
| 110 | 110 | |
| 111 | 111 | // on passe par un fichier temporaire qui permet de ne pas saturer la memoire |
| 112 | 112 | // avec les gros exports |
| 113 | - $fichier = sous_repertoire(_DIR_CACHE, 'export') . $filename; |
|
| 113 | + $fichier = sous_repertoire(_DIR_CACHE, 'export').$filename; |
|
| 114 | 114 | $fp = fopen($fichier, 'w'); |
| 115 | 115 | $length = fwrite($fp, $output); |
| 116 | 116 | |