Completed
Push — master ( 5cc303...b83508 )
by cam
04:45
created
ecrire/inc/importer_csv.php 2 patches
Doc Comments   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -34,12 +34,12 @@
 block discarded – undo
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@@@
Please login to merge, or discard this patch.
Indentation   +84 added lines, -84 removed lines patch added patch discarded remove patch
@@ -11,7 +11,7 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 /**
@@ -87,75 +87,75 @@  discard block
 block discarded – undo
87 87
  */
88 88
 function inc_importer_csv_dist($file, $options = []) {
89 89
 
90
-	// support ancienne syntaxe
91
-	// inc_importer_csv_dist($file, $head = false, $delim = ',', $enclos = '"', $len = 10000, $charset_source = '')
92
-	if (is_string($options)) {
93
-		$args = func_get_args();
94
-		$options = [];
95
-		foreach ([1 => 'head', 2 => 'delim', 3 => 'enclos', 4 => 'len', 5 => 'charset_source'] as $k => $option) {
96
-			if (!empty($args[$k])) {
97
-				$options[$option] = $args[$k];
98
-			}
99
-		}
100
-	}
90
+    // support ancienne syntaxe
91
+    // inc_importer_csv_dist($file, $head = false, $delim = ',', $enclos = '"', $len = 10000, $charset_source = '')
92
+    if (is_string($options)) {
93
+        $args = func_get_args();
94
+        $options = [];
95
+        foreach ([1 => 'head', 2 => 'delim', 3 => 'enclos', 4 => 'len', 5 => 'charset_source'] as $k => $option) {
96
+            if (!empty($args[$k])) {
97
+                $options[$option] = $args[$k];
98
+            }
99
+        }
100
+    }
101 101
 
102
-	$default_options = [
103
-		'head' => false,
104
-		'delim' => ',',
105
-		'enclos' => '"',
106
-		'len' => 10000,
107
-		'charset_source' => '',
108
-	];
109
-	$options = array_merge($default_options, $options);
102
+    $default_options = [
103
+        'head' => false,
104
+        'delim' => ',',
105
+        'enclos' => '"',
106
+        'len' => 10000,
107
+        'charset_source' => '',
108
+    ];
109
+    $options = array_merge($default_options, $options);
110 110
 
111 111
 
112
-	$return = false;
113
-	if (@file_exists($file)
114
-		and $handle = fopen($file, 'r')) {
115
-		if ($options['charset_source']) {
116
-			importer_csv_importcharset('', $options['charset_source']);
117
-		}
118
-		if ($options['head']) {
119
-			$header = fgetcsv($handle, $options['len'], $options['delim'], $options['enclos']);
120
-			if ($header) {
121
-				$header = array_map('importer_csv_importcharset', $header);
122
-				$header = array_map('importer_csv_nettoie_key', $header);
123
-				$header_type = array();
124
-				foreach ($header as $heading) {
125
-					if (!isset($header_type[$heading])) {
126
-						$header_type[$heading] = "scalar";
127
-					} else {
128
-						$header_type[$heading] = "array";
129
-					}
130
-				}
131
-			}
132
-		}
112
+    $return = false;
113
+    if (@file_exists($file)
114
+        and $handle = fopen($file, 'r')) {
115
+        if ($options['charset_source']) {
116
+            importer_csv_importcharset('', $options['charset_source']);
117
+        }
118
+        if ($options['head']) {
119
+            $header = fgetcsv($handle, $options['len'], $options['delim'], $options['enclos']);
120
+            if ($header) {
121
+                $header = array_map('importer_csv_importcharset', $header);
122
+                $header = array_map('importer_csv_nettoie_key', $header);
123
+                $header_type = array();
124
+                foreach ($header as $heading) {
125
+                    if (!isset($header_type[$heading])) {
126
+                        $header_type[$heading] = "scalar";
127
+                    } else {
128
+                        $header_type[$heading] = "array";
129
+                    }
130
+                }
131
+            }
132
+        }
133 133
 
134
-		while (($data = fgetcsv($handle, $options['len'], $options['delim'], $options['enclos'])) !== false) {
135
-			$data = array_map('importer_csv_importcharset', $data);
136
-			if ($options['head'] and isset($header)) {
137
-				$row = array();
138
-				foreach ($header as $key => $heading) {
139
-					if ($header_type[$heading] == "array") {
140
-						if (!isset($row[$heading])) {
141
-							$row[$heading] = array();
142
-						}
143
-						if (isset($data[$key]) and strlen($data[$key])) {
144
-							$row[$heading][] = $data[$key];
145
-						}
146
-					} else {
147
-						$row[$heading] = (isset($data[$key])) ? $data[$key] : '';
148
-					}
149
-				}
150
-				$return[] = $row;
151
-			} else {
152
-				$return[] = $data;
153
-			}
154
-		}
155
-		if ($options['charset_source']) {
156
-			importer_csv_importcharset('', true);
157
-		}
158
-	}
134
+        while (($data = fgetcsv($handle, $options['len'], $options['delim'], $options['enclos'])) !== false) {
135
+            $data = array_map('importer_csv_importcharset', $data);
136
+            if ($options['head'] and isset($header)) {
137
+                $row = array();
138
+                foreach ($header as $key => $heading) {
139
+                    if ($header_type[$heading] == "array") {
140
+                        if (!isset($row[$heading])) {
141
+                            $row[$heading] = array();
142
+                        }
143
+                        if (isset($data[$key]) and strlen($data[$key])) {
144
+                            $row[$heading][] = $data[$key];
145
+                        }
146
+                    } else {
147
+                        $row[$heading] = (isset($data[$key])) ? $data[$key] : '';
148
+                    }
149
+                }
150
+                $return[] = $row;
151
+            } else {
152
+                $return[] = $data;
153
+            }
154
+        }
155
+        if ($options['charset_source']) {
156
+            importer_csv_importcharset('', true);
157
+        }
158
+    }
159 159
 
160
-	return $return;
160
+    return $return;
161 161
 }
Please login to merge, or discard this patch.
ecrire/inc/exporter_csv.php 3 patches
Indentation   +114 added lines, -114 removed lines patch added patch discarded remove patch
@@ -17,7 +17,7 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 /**
@@ -55,15 +55,15 @@  discard block
 block discarded – undo
55 55
  * @return string
56 56
  */
57 57
 function exporter_csv_ligne_numerotee($nb, $ligne, $delim = ',', $importer_charset = null, $callback=null) {
58
-	if ($callback) {
59
-		$ligne = call_user_func($callback, $nb, $ligne, $delim, $importer_charset);
60
-	}
61
-	$output = join($delim, array_map('exporter_csv_champ', $ligne)) . "\r\n";
62
-	if ($importer_charset) {
63
-		$output = str_replace('’', '\'', $output);
64
-		$output = unicode2charset(html2unicode(charset2unicode($output)), $importer_charset);
65
-	}
66
-	return $output;
58
+    if ($callback) {
59
+        $ligne = call_user_func($callback, $nb, $ligne, $delim, $importer_charset);
60
+    }
61
+    $output = join($delim, array_map('exporter_csv_champ', $ligne)) . "\r\n";
62
+    if ($importer_charset) {
63
+        $output = str_replace('’', '\'', $output);
64
+        $output = unicode2charset(html2unicode(charset2unicode($output)), $importer_charset);
65
+    }
66
+    return $output;
67 67
 }
68 68
 
69 69
 /**
@@ -75,7 +75,7 @@  discard block
 block discarded – undo
75 75
  * @return string
76 76
  */
77 77
 function exporter_csv_ligne($ligne, $delim = ',', $importer_charset = null){
78
-	return exporter_csv_ligne_numerotee(null, $ligne, $delim, $importer_charset);
78
+    return exporter_csv_ligne_numerotee(null, $ligne, $delim, $importer_charset);
79 79
 }
80 80
 
81 81
 /**
@@ -101,101 +101,101 @@  discard block
 block discarded – undo
101 101
  */
102 102
 function inc_exporter_csv_dist($titre, $resource, $options = []) {
103 103
 
104
-	// support ancienne syntaxe
105
-	// inc_exporter_csv_dist($titre, $resource, $delim = ', ', $entetes = null, $envoyer = true)
106
-	if (is_string($options)) {
107
-		$args = func_get_args();
108
-		$options = [];
109
-		foreach ([2 => 'delim', 3 => 'entetes', 4 => 'envoyer'] as $k => $option) {
110
-			if (!empty($args[$k])) {
111
-				$options[$option] = $args[$k];
112
-			}
113
-		}
114
-	}
115
-
116
-	$default_options = [
117
-		'delim' => ', ',
118
-		'entetes' => null,
119
-		'envoyer' => true,
120
-		'charset' => null,
121
-		'callback' => null,
122
-	];
123
-	$options = array_merge($default_options, $options);
124
-
125
-	$filename = preg_replace(',[^-_\w]+,', '_', translitteration(textebrut(typo($titre))));
126
-
127
-	if ($options['delim'] == 'TAB') {
128
-		$options['delim'] = "\t";
129
-	}
130
-	if (!in_array($options['delim'], array(',', ';', "\t"))) {
131
-		$options['delim'] = ',';
132
-	}
133
-
134
-	$charset = $GLOBALS['meta']['charset'];
135
-	$importer_charset = null;
136
-	if ($options['delim'] == ',') {
137
-		$extension = 'csv';
138
-	} else {
139
-		$extension = 'xls';
140
-		# Excel n'accepte pas l'utf-8 ni les entites html... on transcode tout ce qu'on peut
141
-		$charset = 'iso-8859-1';
142
-	}
143
-	// mais si une option charset est explicite, elle a la priorite
144
-	if (!empty($options['charset'])) {
145
-		$charset = $options['charset'];
146
-	}
147
-
148
-	$importer_charset = (($charset === $GLOBALS['meta']['charset']) ? null : $charset);
149
-
150
-	$filename = "$filename.$extension";
151
-
152
-	$output = '';
153
-	$nb = 0;
154
-	if (!empty($options['entetes']) and is_array($options['entetes'])) {
155
-		$output = exporter_csv_ligne_numerotee($nb, $options['entetes'], $options['delim'], $importer_charset, $options['callback']);
156
-	}
157
-	// les donnees commencent toujours a la ligne 1, qu'il y ait ou non des entetes
158
-	$nb++;
159
-
160
-	if ($options['envoyer']) {
161
-		$disposition = ($options['envoyer'] === 'attachment' ? 'attachment' : 'inline');
162
-		header("Content-Type: text/comma-separated-values; charset=$charset");
163
-		header("Content-Disposition: $disposition; filename=$filename");
164
-
165
-		// Vider tous les tampons
166
-		$level = @ob_get_level();
167
-		while ($level--) {
168
-			@ob_end_flush();
169
-		}
170
-	}
171
-
172
-	// si envoyer=='attachment' on passe par un fichier temporaire
173
-	// sinon on ecrit directement sur stdout
174
-	if ($options['envoyer'] and $options['envoyer'] !== 'attachment') {
175
-		$fichier = "php://output";
176
-	}
177
-	else {
178
-	$fichier = sous_repertoire(_DIR_CACHE, 'export') . $filename;
179
-	}
180
-
181
-	$fp = fopen($fichier, 'w');
182
-	$length = fwrite($fp, $output);
183
-
184
-	while ($row = is_array($resource) ? array_shift($resource) : sql_fetch($resource)) {
185
-		$output = exporter_csv_ligne_numerotee($nb, $row, $options['delim'], $importer_charset, $options['callback']);
186
-		$length += fwrite($fp, $output);
187
-		$nb++;
188
-	}
189
-	fclose($fp);
190
-
191
-	if ($options['envoyer']) {
192
-		if ($options['envoyer'] === 'attachment') {
193
-			header("Content-Length: $length");
194
-			readfile($fichier);
195
-		}
196
-		// si on a envoye inline, c'est deja tout bon
197
-		exit;
198
-	}
199
-
200
-	return $fichier;
104
+    // support ancienne syntaxe
105
+    // inc_exporter_csv_dist($titre, $resource, $delim = ', ', $entetes = null, $envoyer = true)
106
+    if (is_string($options)) {
107
+        $args = func_get_args();
108
+        $options = [];
109
+        foreach ([2 => 'delim', 3 => 'entetes', 4 => 'envoyer'] as $k => $option) {
110
+            if (!empty($args[$k])) {
111
+                $options[$option] = $args[$k];
112
+            }
113
+        }
114
+    }
115
+
116
+    $default_options = [
117
+        'delim' => ', ',
118
+        'entetes' => null,
119
+        'envoyer' => true,
120
+        'charset' => null,
121
+        'callback' => null,
122
+    ];
123
+    $options = array_merge($default_options, $options);
124
+
125
+    $filename = preg_replace(',[^-_\w]+,', '_', translitteration(textebrut(typo($titre))));
126
+
127
+    if ($options['delim'] == 'TAB') {
128
+        $options['delim'] = "\t";
129
+    }
130
+    if (!in_array($options['delim'], array(',', ';', "\t"))) {
131
+        $options['delim'] = ',';
132
+    }
133
+
134
+    $charset = $GLOBALS['meta']['charset'];
135
+    $importer_charset = null;
136
+    if ($options['delim'] == ',') {
137
+        $extension = 'csv';
138
+    } else {
139
+        $extension = 'xls';
140
+        # Excel n'accepte pas l'utf-8 ni les entites html... on transcode tout ce qu'on peut
141
+        $charset = 'iso-8859-1';
142
+    }
143
+    // mais si une option charset est explicite, elle a la priorite
144
+    if (!empty($options['charset'])) {
145
+        $charset = $options['charset'];
146
+    }
147
+
148
+    $importer_charset = (($charset === $GLOBALS['meta']['charset']) ? null : $charset);
149
+
150
+    $filename = "$filename.$extension";
151
+
152
+    $output = '';
153
+    $nb = 0;
154
+    if (!empty($options['entetes']) and is_array($options['entetes'])) {
155
+        $output = exporter_csv_ligne_numerotee($nb, $options['entetes'], $options['delim'], $importer_charset, $options['callback']);
156
+    }
157
+    // les donnees commencent toujours a la ligne 1, qu'il y ait ou non des entetes
158
+    $nb++;
159
+
160
+    if ($options['envoyer']) {
161
+        $disposition = ($options['envoyer'] === 'attachment' ? 'attachment' : 'inline');
162
+        header("Content-Type: text/comma-separated-values; charset=$charset");
163
+        header("Content-Disposition: $disposition; filename=$filename");
164
+
165
+        // Vider tous les tampons
166
+        $level = @ob_get_level();
167
+        while ($level--) {
168
+            @ob_end_flush();
169
+        }
170
+    }
171
+
172
+    // si envoyer=='attachment' on passe par un fichier temporaire
173
+    // sinon on ecrit directement sur stdout
174
+    if ($options['envoyer'] and $options['envoyer'] !== 'attachment') {
175
+        $fichier = "php://output";
176
+    }
177
+    else {
178
+    $fichier = sous_repertoire(_DIR_CACHE, 'export') . $filename;
179
+    }
180
+
181
+    $fp = fopen($fichier, 'w');
182
+    $length = fwrite($fp, $output);
183
+
184
+    while ($row = is_array($resource) ? array_shift($resource) : sql_fetch($resource)) {
185
+        $output = exporter_csv_ligne_numerotee($nb, $row, $options['delim'], $importer_charset, $options['callback']);
186
+        $length += fwrite($fp, $output);
187
+        $nb++;
188
+    }
189
+    fclose($fp);
190
+
191
+    if ($options['envoyer']) {
192
+        if ($options['envoyer'] === 'attachment') {
193
+            header("Content-Length: $length");
194
+            readfile($fichier);
195
+        }
196
+        // si on a envoye inline, c'est deja tout bon
197
+        exit;
198
+    }
199
+
200
+    return $fichier;
201 201
 }
Please login to merge, or discard this patch.
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -38,7 +38,7 @@  discard block
 block discarded – undo
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
 /**
@@ -54,11 +54,11 @@  discard block
 block discarded – undo
54 54
  * @param callable $callback
55 55
  * @return string
56 56
  */
57
-function exporter_csv_ligne_numerotee($nb, $ligne, $delim = ',', $importer_charset = null, $callback=null) {
57
+function exporter_csv_ligne_numerotee($nb, $ligne, $delim = ',', $importer_charset = null, $callback = null) {
58 58
 	if ($callback) {
59 59
 		$ligne = call_user_func($callback, $nb, $ligne, $delim, $importer_charset);
60 60
 	}
61
-	$output = join($delim, array_map('exporter_csv_champ', $ligne)) . "\r\n";
61
+	$output = join($delim, array_map('exporter_csv_champ', $ligne))."\r\n";
62 62
 	if ($importer_charset) {
63 63
 		$output = str_replace('’', '\'', $output);
64 64
 		$output = unicode2charset(html2unicode(charset2unicode($output)), $importer_charset);
@@ -74,7 +74,7 @@  discard block
 block discarded – undo
74 74
  * @param null $importer_charset
75 75
  * @return string
76 76
  */
77
-function exporter_csv_ligne($ligne, $delim = ',', $importer_charset = null){
77
+function exporter_csv_ligne($ligne, $delim = ',', $importer_charset = null) {
78 78
 	return exporter_csv_ligne_numerotee(null, $ligne, $delim, $importer_charset);
79 79
 }
80 80
 
@@ -175,7 +175,7 @@  discard block
 block discarded – undo
175 175
 		$fichier = "php://output";
176 176
 	}
177 177
 	else {
178
-	$fichier = sous_repertoire(_DIR_CACHE, 'export') . $filename;
178
+	$fichier = sous_repertoire(_DIR_CACHE, 'export').$filename;
179 179
 	}
180 180
 
181 181
 	$fp = fopen($fichier, 'w');
Please login to merge, or discard this patch.
Braces   +1 added lines, -2 removed lines patch added patch discarded remove patch
@@ -173,8 +173,7 @@
 block discarded – undo
173 173
 	// sinon on ecrit directement sur stdout
174 174
 	if ($options['envoyer'] and $options['envoyer'] !== 'attachment') {
175 175
 		$fichier = "php://output";
176
-	}
177
-	else {
176
+	} else {
178 177
 	$fichier = sous_repertoire(_DIR_CACHE, 'export') . $filename;
179 178
 	}
180 179
 
Please login to merge, or discard this patch.