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