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