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