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);
152
	if(DEBUG) fwrite(STDERR, "curl \"".$URL ."\" : {$sess[1]} / {$id[1]}\n");
1768 raphael 153
	if(isset($sess[1])) {
154
		define('COOKIE', $sess[1]);
1802 raphael 155
		if(!defined('USER')) {
156
			if(!isset($id[1])) return FALSE;
157
			define('USER', $id[1]);
158
		}
159
		return TRUE;
1768 raphael 160
	}
1802 raphael 161
	return FALSE;
1768 raphael 162
}
163
 
164
function genCSV($d = Array()) {
165
	$out = fopen("php://temp", 'r+');
166
	// don't filter-out unknown field (for testing)
167
	// $d = array_intersect_key($d, $GLOBALS['champs']);
168
	$head = array();
169
	foreach($d as $k => $v) {
170
		if(isset($GLOBALS['champs'][$k])) $head[$GLOBALS['champs'][$k]] = $v;
171
		else $head[$k] = $v;
172
	}
173
	fputcsv($out, array_keys($head) , ',', '"');
174
	fputcsv($out, array_values($head) , ',', '"');
175
	rewind($out);
176
	$csv = stream_get_contents($out);
177
	fclose($out);
178
	return $csv;
179
}
180
 
181
function import($d) {
182
	$URL = DOMAIN . '/jrest/ImportXLS';
183
	if(!is_file($d)) return FALSE;
184
	$ch = curl_init();
185
 
186
	// curl does not support setting filename="X.csv";type=application/octet-stream'
187
	// with plain POST variables
188
 
189
	curl_setopt($ch,CURLOPT_URL, $URL);
190
	curl_setopt($ch,CURLOPT_POSTFIELDS, array(
191
		'upload' => '@' . $d . ';filename=test.csv',
192
		'utilisateur' => USER
193
	));
194
 
195
	curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
1802 raphael 196
 
197
	if(DEBUG) fwrite(STDERR, "curl -F \"upload=@$d;filename=test.csv\" -F utilisateur=" . USER . " \"$URL\"\n");
198
 
1768 raphael 199
	//execute post
200
	$result = curl_exec($ch);
201
	curl_close($ch);
202
	return $result;
203
}
204
 
1773 raphael 205
function export($cols = "standard,avance") {
1768 raphael 206
	if(!defined('COOKIE')) return FALSE;
207
 
208
	$URL = DOMAIN . '/jrest/CelWidgetExport/export?';
1773 raphael 209
	$URL .= http_build_query(array('colonnes' => $cols,
1768 raphael 210
								   'id_utilisateur' => USER,
211
								   'format' => 'csv'));
212
	$ch = curl_init();
213
 
214
	curl_setopt($ch,CURLOPT_URL, $URL);
215
	curl_setopt($ch,CURLOPT_COOKIE, 'PHPSESSID=' . COOKIE);
216
 
1773 raphael 217
	if(DEBUG) fwrite(STDERR, "curl -b PHPSESSID=" . COOKIE . ' "' . $URL ."\"\n");
218
 
1768 raphael 219
	curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
220
	//execute post
221
	$result = curl_exec($ch);
222
	curl_close($ch);
223
	return $result;
224
}
225
 
226
function getCSV_line($csv, $line) {
227
	$line = max(0, intval($line - 1)); // -1 à cause du header
228
 
229
	$out = fopen("php://temp", 'rw');
230
	fwrite($out, $csv);
231
	rewind($out);
232
 
233
	$head = champsLongToShort(fgetcsv($out));
234
	while ($line--) fgetcsv($out);
235
	$l = fgetcsv($out);
236
	fclose($out);
1793 raphael 237
	if(!$l) return FALSE;
1768 raphael 238
	return array_combine($head, $l);
239
}
240
 
241
 
242
function champsLongToShort($a) {
243
	$r = array();
244
	$x = array_flip($GLOBALS['champs']);
245
	foreach($a as $k => $v) {
246
		if(isset($x[$v])) $r[$k] = $x[$v];
247
		else $r[$k] = -1;
248
	}
249
	return $r;
250
}
251
 
1769 raphael 252
function champsLongToShort2($a) {
253
	$r = array();
254
	$x = array_flip($GLOBALS['champs']);
255
	foreach($a as $k => $v) {
256
		if(isset($x[$k])) $r[$x[$k]] = $v;
257
		else $r[$k] = $v;
258
	}
259
	return $r;
260
}