Subversion Repositories eFlore/Applications.cel

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1768 raphael 1
<?php
2
 
3
global $champs;
4
$champs = Array(
5
	'nom_sel' => 'Espèce',
6
	'nom_sel_nn' => 'Numéro nomenclatural',
7
	'nom_ret' => 'Nom retenu',
8
	'nom_ret_nn' => 'Numéro nomenclatural nom retenu',
9
	'nt' => 'Numéro taxonomique',
10
	'famille' => 'Famille',
11
	'nom_referentiel' => 'Referentiel taxonomique',
12
	'zone_geo' => 'Commune',
13
	'ce_zone_geo' => 'Identifiant Commune',
14
	'date_observation' => 'Date',
15
	'lieudit' => 'Lieu-dit',
16
	'station' => 'Station',
17
	'milieu' => 'Milieu',
18
	'commentaire' => 'Notes',
19
	'latitude' => 'Latitude',
20
	'longitude' => 'Longitude',
21
	'altitude' => 'Altitude',
22
	'geodatum' => 'Référentiel Géographique',
23
 
24
	'ordre' => 'Ordre',
25
	'id_observation' => 'Identifiant',
26
 
27
	'mots_cles_texte' => 'Mots Clés',
28
 
29
	'date_creation' => 'Date Création',
30
	'date_modification' => 'Date Modification',
31
 
32
	'transmission' => 'Transmis',
33
	'date_transmission' => 'Date Transmission',
34
	'abondance' => 'Abondance',
35
	'certitude' => 'Certitude',
36
	'phenologie' => 'Phénologie',
37
	'images' => 'Image(s)',
38
	'nom_commun' => 'Nom Commun',
1850 raphael 39
 
40
	'observateur' => 'Observateur',
1768 raphael 41
);
42
 
43
 
44
function cel_get_obs() {
45
	$URL = DOMAIN . '/jrest/InventoryObservationList/' . USER;
1932 raphael 46
	// 5000 (memory_limit)
47
	return json_decode(file_get_contents($URL . '/?limite=5000&numero_page=0'));
1768 raphael 48
}
49
 
50
function cel_get_images() {
51
	$URL = DOMAIN . '/jrest/InventoryImageList/' . USER;
52
	return json_decode(file_get_contents($URL . '/?limite=100000&numero_page=0'));
53
}
54
 
55
function cel_delete_obs($obs) {
56
	$URL = DOMAIN . '/jrest/InventoryObservationList/' . USER;
57
	$ch = curl_init();
58
 
59
	//set the url, number of POST vars, POST data
60
	curl_setopt($ch,CURLOPT_URL, $URL . '/' . implode(',', $obs));
61
	curl_setopt($ch,CURLOPT_POSTFIELDS, array('action' => 'DELETE'));
62
	curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
1932 raphael 63
 
64
	if(DEBUG) @fwrite(STDERR, sprintf("curl -F action=DELETE '%s/%s'\n", $URL, implode(',', $obs)));
1768 raphael 65
	//execute post
66
	$result = curl_exec($ch);
67
	curl_close($ch);
68
	return $result;
69
}
70
 
71
function cel_delete_image($img) {
72
	$URL = DOMAIN . '/jrest/InventoryImageList/' . USER;
73
	$ch = curl_init();
74
 
75
	//set the url, number of POST vars, POST data
76
	curl_setopt($ch,CURLOPT_URL, $URL . '/' . implode(',', $img));
77
	curl_setopt($ch,CURLOPT_POSTFIELDS, array('action' => 'DELETE'));
78
	curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
79
	//execute post
80
	$result = curl_exec($ch);
81
	curl_close($ch);
82
	return $result;
83
}
84
 
85
 
86
function cel_delete_all_obs() {
1932 raphael 87
    while( ($obs = cel_get_obs()) ) {
1768 raphael 88
	$ordres = array_map(function($item) { return $item->ordre; }, $obs);
1932 raphael 89
	// les chunks permettent d'éviter des REQUEST-URI too long (500 id * 4 digits + 500 (virgules)) < 4k
90
	while( $ordres_chunk = array_splice($ordres, 0, 500)) {
91
	    cel_delete_obs($ordres_chunk);
92
	}
93
    }
1768 raphael 94
}
95
 
96
 
97
function cel_delete_all_images() {
98
	$img = cel_get_images();
99
	$ids = array_map(function($item) { return $item->id_image; }, $img);
100
	cel_delete_image($ids);
101
}
102
 
103
function cel_upload_image($file) {
104
	if(! file_exists($file)) return NULL;
105
	$URL = DOMAIN . '/jrest/InventoryImage';
106
 
107
	$ch = curl_init();
108
 
109
	//set the url, number of POST vars, POST data
110
	curl_setopt($ch,CURLOPT_URL, $URL);
111
	curl_setopt($ch,CURLOPT_POSTFIELDS, array(
112
		'file' => '@' . $file,
113
		'ce_utilisateur' => USER
114
	));
115
 
1907 raphael 116
	//curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
1768 raphael 117
	//execute post
1907 raphael 118
	if(DEBUG) @fwrite(STDERR, sprintf("curl -F file=@%s '%s'\n", $file, $URL));
119
 
1768 raphael 120
	$result = curl_exec($ch);
121
	curl_close($ch);
122
	return $result;
123
}
124
 
125
function cel_link_images($user, $id_image, $id_obs) {
126
	$URL = DOMAIN . '/jrest/inventoryImageLink/';
127
	$ch = curl_init();
128
 
129
	//set the url, number of POST vars, POST data
130
	curl_setopt($ch,CURLOPT_URL, $URL);
131
	curl_setopt($ch,CURLOPT_POSTFIELDS, array(
132
		'id_image' => $id_obs,
133
		'id_observation' => $id_obs,
134
		'ce_utilisateur' => $user
135
	));
136
 
137
	curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
138
	//execute post
139
	$result = curl_exec($ch);
140
	curl_close($ch);
141
	return $result;
142
}
143
 
144
 
145
function auth() {
146
	// TODO: CURLOPT_NETRC + Cel API support
147
	if(!defined('EMAIL') || !defined('PASS') || ! EMAIL || ! PASS) return FALSE;
1802 raphael 148
	if(defined('COOKIE') && COOKIE) return TRUE;
1768 raphael 149
	$URL = DOMAIN . '/jrest/User/' . EMAIL . '/' . PASS;
150
 
151
	$ch = curl_init();
152
 
153
	//set the url, number of POST vars, POST data
154
	curl_setopt($ch,CURLOPT_URL, $URL);
155
	curl_setopt($ch,CURLOPT_HEADER, true);
156
	curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
157
 
158
	$result = curl_exec($ch);
159
	curl_close($ch);
1802 raphael 160
	$sess = $id = array();
161
	preg_match('/.*PHPSESSID=(\w+)/', $result, $sess);
162
	preg_match('/"id_utilisateur":"(\d+)"/', $result, $id);
1803 raphael 163
 
164
	if(DEBUG) @fwrite(STDERR, "curl \"".$URL ."\" : {$sess[1]} / {$id[1]}\n");
1768 raphael 165
	if(isset($sess[1])) {
166
		define('COOKIE', $sess[1]);
1802 raphael 167
		if(!defined('USER')) {
168
			if(!isset($id[1])) return FALSE;
169
			define('USER', $id[1]);
170
		}
171
		return TRUE;
1768 raphael 172
	}
1802 raphael 173
	return FALSE;
1768 raphael 174
}
175
 
176
function genCSV($d = Array()) {
177
	$out = fopen("php://temp", 'r+');
178
	// don't filter-out unknown field (for testing)
179
	// $d = array_intersect_key($d, $GLOBALS['champs']);
180
	$head = array();
181
	foreach($d as $k => $v) {
182
		if(isset($GLOBALS['champs'][$k])) $head[$GLOBALS['champs'][$k]] = $v;
183
		else $head[$k] = $v;
184
	}
185
	fputcsv($out, array_keys($head) , ',', '"');
186
	fputcsv($out, array_values($head) , ',', '"');
187
	rewind($out);
188
	$csv = stream_get_contents($out);
189
	fclose($out);
190
	return $csv;
191
}
192
 
193
function import($d) {
194
	$URL = DOMAIN . '/jrest/ImportXLS';
195
	if(!is_file($d)) return FALSE;
196
	$ch = curl_init();
197
 
198
	// curl does not support setting filename="X.csv";type=application/octet-stream'
199
	// with plain POST variables
200
 
201
	curl_setopt($ch,CURLOPT_URL, $URL);
202
	curl_setopt($ch,CURLOPT_POSTFIELDS, array(
203
		'upload' => '@' . $d . ';filename=test.csv',
204
		'utilisateur' => USER
205
	));
206
 
207
	curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
1802 raphael 208
 
209
	if(DEBUG) fwrite(STDERR, "curl -F \"upload=@$d;filename=test.csv\" -F utilisateur=" . USER . " \"$URL\"\n");
210
 
1768 raphael 211
	//execute post
212
	$result = curl_exec($ch);
213
	curl_close($ch);
214
	return $result;
215
}
216
 
1803 raphael 217
function importXLS($d) {
218
	$URL = DOMAIN . '/jrest/ImportXLS';
219
	if(!is_file($d)) return FALSE;
220
	exec("csvtool -o /tmp/a.csv col 1- $d");
221
	exec(__DIR__ . "/bin/csv2xls -v 0 -f -o /tmp/a /tmp/a.csv &>/dev/null");
222
	if(DEBUG) fwrite(STDERR,
223
					 "csvtool -o /tmp/a.csv col 1- $d;\n" .
224
					 __DIR__ . "/bin/csv2xls -v 0 -f -o /tmp/a /tmp/a.csv\n");
225
	$d = "/tmp/a.xls";
226
	if(!is_file($d)) { die('error: ' . __FILE__ . ':' . __LINE__); }
227
 
228
	$ch = curl_init();
229
 
230
	// curl does not support setting filename="X.csv";type=application/octet-stream'
231
	// with plain POST variables
232
 
233
	curl_setopt($ch,CURLOPT_URL, $URL);
234
	curl_setopt($ch,CURLOPT_POSTFIELDS, array(
235
		'upload' => '@' . $d . ';filename=test.xls',
236
		'utilisateur' => USER
237
	));
238
 
239
	curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
240
 
241
	if(DEBUG) fwrite(STDERR, "curl -F \"upload=@$d;filename=test.xls\" -F utilisateur=" . USER . " \"$URL\"\n");
242
 
243
	//execute post
244
	$result = curl_exec($ch);
245
	curl_close($ch);
246
	return $result;
247
}
248
 
1773 raphael 249
function export($cols = "standard,avance") {
1768 raphael 250
	if(!defined('COOKIE')) return FALSE;
251
 
252
	$URL = DOMAIN . '/jrest/CelWidgetExport/export?';
1773 raphael 253
	$URL .= http_build_query(array('colonnes' => $cols,
1768 raphael 254
								   'id_utilisateur' => USER,
255
								   'format' => 'csv'));
256
	$ch = curl_init();
257
 
258
	curl_setopt($ch,CURLOPT_URL, $URL);
259
	curl_setopt($ch,CURLOPT_COOKIE, 'PHPSESSID=' . COOKIE);
260
 
1803 raphael 261
	curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
262
 
1773 raphael 263
	if(DEBUG) fwrite(STDERR, "curl -b PHPSESSID=" . COOKIE . ' "' . $URL ."\"\n");
1768 raphael 264
	//execute post
265
	$result = curl_exec($ch);
266
	curl_close($ch);
267
	return $result;
268
}
269
 
1803 raphael 270
function exportXLS($cols = "standard,avance") {
271
	if(!defined('COOKIE')) return FALSE;
272
 
273
	$file = "/tmp/b.xls";
274
 
275
	$URL = DOMAIN . '/jrest/CelWidgetExport/export?';
276
	$URL .= http_build_query(array('colonnes' => $cols,
277
								   'id_utilisateur' => USER,
278
								   'format' => 'xls'));
279
	$ch = curl_init();
280
 
281
	curl_setopt($ch,CURLOPT_URL, $URL);
282
	curl_setopt($ch,CURLOPT_COOKIE, 'PHPSESSID=' . COOKIE);
283
 
284
	$fh = fopen($file, 'w');
285
	curl_setopt($ch,CURLOPT_FILE, $fh);
286
 
287
	if(DEBUG) fwrite(STDERR, "curl -o $file -b PHPSESSID=" . COOKIE . ' "' . $URL ."\"\n");
288
	//execute post
289
	curl_exec($ch);
290
 
291
	fflush($fh);
292
	fclose($fh);
293
	curl_close($ch);
294
 
295
	// if(DEBUG) fwrite(STDERR, "xls2csv $file\n");
296
	// exec("xls2csv $file", $result, $ret);
297
	// return implode("\n", $result);
298
	if(DEBUG) fwrite(STDERR, __DIR__ . "/bin/xls2csv-separate-sheets.pl -i $file\n");
299
	exec(__DIR__ . "/bin/xls2csv-separate-sheets.pl -i $file &>/dev/null", $result, $ret);
300
	$newfile = substr($file, 0, -4) . '-Liste' . '.csv';
301
 
302
	// because xls2csv-separate-sheets.pl does latin9
303
	if(DEBUG) fwrite(STDERR, "iconv -f latin9 -t utf8 $newfile >| /tmp/.x; mv -f /tmp/.x $newfile\n");
304
	exec("iconv -f latin9 -t utf8 -o /tmp/.x $newfile; mv -f /tmp/.x $newfile");
305
	if($ret) die('error: ' . __FILE__ . ':' . __LINE__);
306
 
307
	return file_get_contents($newfile);
308
}
309
 
1768 raphael 310
function getCSV_line($csv, $line) {
311
	$line = max(0, intval($line - 1)); // -1 à cause du header
312
 
313
	$out = fopen("php://temp", 'rw');
314
	fwrite($out, $csv);
315
	rewind($out);
316
 
317
	$head = champsLongToShort(fgetcsv($out));
318
	while ($line--) fgetcsv($out);
319
	$l = fgetcsv($out);
320
	fclose($out);
1793 raphael 321
	if(!$l) return FALSE;
1803 raphael 322
	// xls2csv-separate-sheets.pl supprime les trailing champs vides
323
	if(count($l) != count($head)) {
324
		$l += array_fill(count($l), count($head) - count($l), "");
325
	}
1768 raphael 326
	return array_combine($head, $l);
327
}
328
 
329
 
330
function champsLongToShort($a) {
331
	$r = array();
332
	$x = array_flip($GLOBALS['champs']);
333
	foreach($a as $k => $v) {
334
		if(isset($x[$v])) $r[$k] = $x[$v];
335
		else $r[$k] = -1;
336
	}
337
	return $r;
338
}
339
 
1769 raphael 340
function champsLongToShort2($a) {
341
	$r = array();
342
	$x = array_flip($GLOBALS['champs']);
343
	foreach($a as $k => $v) {
344
		if(isset($x[$k])) $r[$x[$k]] = $v;
345
		else $r[$k] = $v;
346
	}
347
	return $r;
348
}