Subversion Repositories eFlore/Applications.cel

Rev

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