forked from leonardoxc/leonardoxc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.php
427 lines (347 loc) · 13.2 KB
/
sync.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<?
//************************************************************************
// Leonardo XC Server, http://www.leonardoxc.net
//
// Copyright (c) 2004-2010 by Andreadakis Manolis
//
// This program is free software. You can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License.
//
// $Id: sync.php,v 1.32 2012/01/16 07:21:23 manolis Exp $
//
//************************************************************************
if ($_GET['version']==2 && ( $_GET['op']=="latest" || !$_GET['op']) ) {
require_once dirname(__FILE__)."/sync_new.php";
exit;
}
if ($_GET['version']!=2 && ( $_GET['op']=="latest" || !$_GET['op']) ) {
echo "Sync protocol version 1 is inactive, please use version 2";
exit;
}
require_once dirname(__FILE__)."/EXT_config_pre.php";
require_once "config.php";
require_once "EXT_config.php";
require_once "CL_flightData.php";
require_once "FN_functions.php";
require_once "FN_UTM.php";
require_once "FN_waypoint.php";
require_once "FN_output.php";
require_once "FN_pilot.php";
require_once "CL_server.php";
setDEBUGfromGET();
// the requesting server must authenticate first
$op=makeSane($_REQUEST['op']);
$type = $_GET['type']+0;
$startID = $_GET['startID']+0;
$count = $_GET['c']+0;
$format = makeSane($_GET['format'],0);
if ( ! $format ) $format=$CONF['sync']['protocol']['format'];
$format=strtoupper($format);
$clientID = makeSane($_GET['clientID'],1);
$clientPass = makeSane($_GET['clientPass'],0);
if ( $_GET['getOnlyServersList'] ) {
$getOnlyServersListClause = " AND ServerItemID IN ( ".$_GET['getOnlyServersList']." ) " ;
} else {
$getOnlyServersListClause = '';
}
if ($count) $limit="LIMIT $count";
else $limit="";
$where_clause='';
if ($type) $where_clause=" AND ItemType=$type ";
$dont_give_servers=$CONF['servers']['list'][$clientID]['dont_give_servers'];
$where_clause.=" AND ServerItemID NOT IN ( ";
if ( is_array($dont_give_servers) ) {
foreach($dont_give_servers as $tmpServerID) {
$where_clause.=$tmpServerID.', ';
}
}
$where_clause.=$clientID .' )';
$where_clause.= $getOnlyServersListClause;
if (!$op) $op="latest";
if (!in_array($op,array("latest","get_hash","get_igc","pilot_info")) ) return;
$encoding="utf-8";
if ($format=='XML')
$RSS_str="<?xml version=\"1.0\" encoding=\"$encoding\" ?>\n<log>";
else if ($format=='JSON')
$RSS_str='{ "log": [ ';
// authentication stuff
// the client must be in the leonardo_servers table and the password he provided must match the serverPass field we have for him.
if ( !Server::checkServerPass($clientID,$clientPass)) {
$op='error';
if ($format=='XML') {
$RSS_str="<?xml version=\"1.0\" encoding=\"$encoding\" ?>\n<log>";
$RSS_str.="<error>Not authorized $clientID,$clientPass </error>\n";
} else if ($format=='JSON') {
$RSS_str='{ "error": "Not authorized '.$clientID.','.$clientPass.'" }';
}
}
set_time_limit( 60 + floor($count/4) );
if ($op=="pilot_info") {
if (!$CONF_use_utf) {
require_once dirname(__FILE__).'/lib/ConvertCharset/ConvertCharset.class.php';
}
$pilotListStr=preg_replace("/[^_,\d]/","",$_GET['pilots']);
$pilotList=split(",",$pilotListStr);
if (!count($pilotList)) {
echo '{ "error": "No pilot id" }';
exit;
}
//echo "*".$pilotList."**";
// print_r($pilotList);
$where_clause='';
foreach($pilotList as $pIDstr) {
list($sID,$pID)=explode('_',$pIDstr);
// echo "@ $sID,$pID @";
if (!$pID && !$sID ) {
continue;
}
if (!$pID) {
$pID=$sID;
$sID=0;
}
$where_clause.=" OR ( pilotID=$pID AND serverID=$sID) ";
}
$format='JSON';
$query="SELECT * FROM $pilotsTable WHERE (1=0) $where_clause ";
//$query.=" LIMIT 1000 ";
$res= $db->sql_query($query);
if($res <= 0){
$RSS_str='{ "error": "Error in query!" }';
} else {
$res_str='';
$item_num=0;
while ($row = mysql_fetch_assoc($res)) {
$pilotID=$row['pilotID'].'_'.$row['serverID'];
if ( ! $pilotNames[$pilotID]){
$pilotInfo=getPilotInfoEn($row['pilotID'],$row['serverID'] );
if (!$CONF_use_utf ) {
$NewEncoding = new ConvertCharset;
$lName=$NewEncoding->Convert($pilotInfo[0],$langEncodings[$nativeLanguage], "utf-8", $Entities);
$fName=$NewEncoding->Convert($pilotInfo[1],$langEncodings[$nativeLanguage], "utf-8", $Entities);
} else {
$lName=$pilotInfo[0];
$fName=$pilotInfo[1];
}
$pilotNames[$pilotID]['lname']=$lName;
$pilotNames[$pilotID]['fname']=$fName;
$pilotNames[$pilotID]['lnameEn']=$pilotInfo[2];
$pilotNames[$pilotID]['fnameEn']=$pilotInfo[3];
$pilotNames[$pilotID]['country']=$pilotInfo[4];
$pilotNames[$pilotID]['sex']=$pilotInfo[5];
$pilotNames[$pilotID]['birthdate']=$pilotInfo[6];
$pilotNames[$pilotID]['CIVL_ID']=$pilotInfo[7];
}
if ($item_num>0) $RSS_str.=' , ';
$res_str.=' { "item": {
"pilotID":'.$row['pilotID'].',
"serverID":'.($row['serverID']?$row['serverID']:$CONF_server_id).',
"LastName":"'.$pilotNames[$pilotID]['lname'].'",
"FirstName":"'.$pilotNames[$pilotID]['fname'].'",
"LastNameEn":"'.$pilotNames[$pilotID]['lnameEn'].'",
"FirstNameEn":"'.$pilotNames[$pilotID]['fnameEn'].'",
"countryCode": "'.$pilotNames[$pilotID]['country'].'",
"Sex": "'.$pilotNames[$pilotID]['sex'].'",
"Birthdate": "'.$pilotNames[$pilotID]['birthdate'].'",
"CIVL_ID": "'.$pilotNames[$pilotID]['CIVL_ID'].'"
}} ';
$item_num++;
}
}
$RSS_str='{ "log_item_num": '.$item_num.', ';
// $RSS_str.=' "query": "'.$query.'", ';
$RSS_str.=' "log": [ '.$res_str.' ] } ';
} else if ($op=="get_hash") {
if (!$CONF_use_utf) {
require_once dirname(__FILE__).'/lib/ConvertCharset/ConvertCharset.class.php';
}
$format='JSON';
$query="SELECT ID, userID, serverID,hash, userServerID,originalUserID ,original_ID FROM $flightsTable WHERE hash<>'' ";
//$query.=" LIMIT 1000 ";
$res= $db->sql_query($query);
if($res <= 0){
$RSS_str='{ "error": "Error in query!" }';
} else {
$RSS_str='';
$item_num=0;
while ($row = mysql_fetch_assoc($res)) {
$pilotID=$row['userID'].'_'.$row['userServerID'];
if ( ! $pilotNames[$pilotID]){
$pilotInfo=getPilotInfo($row['userID'],$row['userServerID'] );
if (!$CONF_use_utf ) {
$NewEncoding = new ConvertCharset;
$lName=$NewEncoding->Convert($pilotInfo[0],$langEncodings[$nativeLanguage], "utf-8", $Entities);
$fName=$NewEncoding->Convert($pilotInfo[1],$langEncodings[$nativeLanguage], "utf-8", $Entities);
} else {
$lName=$pilotInfo[0];
$fName=$pilotInfo[1];
}
$pilotNames[$pilotID]['lname']=$lName;
$pilotNames[$pilotID]['fname']=$fName;
$pilotNames[$pilotID]['country']=$pilotInfo[2];
$pilotNames[$pilotID]['sex']=$pilotInfo[3];
$pilotNames[$pilotID]['birthdate']=$pilotInfo[4];
$pilotNames[$pilotID]['CIVL_ID']=$pilotInfo[5];
}
if ($item_num>0) $RSS_str.=' , ';
$RSS_str.=' { "item": {
"ID": '.$row['ID'].',
"serverID": '.$row['serverID'].',
"hash": "'.$row['hash'].'", '.
'
"pilot" : {
"userID":'.$row['userID'].',
"userServerID":'.$row['userServerID'].',
"lName":"'.$pilotNames[$pilotID]['lname'].'",
"fName":"'.$pilotNames[$pilotID]['fname'].'",
} '.
/*
'
"pilot" : {
"userID": '.$row['userID'].',
"userServerID": '.$row['userServerID'].',
"lName": "'.$pilotNames[$pilotID]['lname'].'",
"fName": "'.$pilotNames[$pilotID]['fname'].'",
"country": "'.$pilotNames[$pilotID]['country'].'",
"sex": "'.$pilotNames[$pilotID]['sex'].'",
"birthdate": "'.$pilotNames[$pilotID]['birthdate'].'",
"CIVL_ID": "'.$pilotNames[$pilotID]['CIVL_ID'].'"
} '.
*/
' }} ';
$item_num++;
}
}
$RSS_str='{ "log_item_num": '.$item_num.', "log": [ '.$RSS_str.' ] } ';
} else if ($op=="latest") {
$sync_type = makeSane($_GET['sync_type'],1);
$getIGCfiles = $sync_type & SYNC_INSERT_FLIGHT_LOCAL ;
$zip = makeSane($_GET['use_zip'],1);
if ($getIGCfiles) $zip=1;
$query="SELECT * FROM $logTable WHERE transactionID>=$startID AND result=1 $where_clause ORDER BY transactionID $limit";
// echo $query;
$res= $db->sql_query($query);
if($res <= 0){
echo("<H3> Error in query! </H3>\n");
exit();
}
$item_num=0;
$flightsToServe =array();
while ($row = mysql_fetch_assoc($res)) {
$desc=htmlspecialchars ($desc);
$actionTime=$row['actionTime']-date('Z');
$actionTimeStr=tm2fulldate($actionTime);
// prepare an array of files to send as well
if ($getIGCfiles) {
if ( $row['ItemType']==1 && $row['ActionID']==1 ) { // type is flight and action is add
if (! in_array($row['ItemID'],$flightsToServe) )
array_push($flightsToServe,$row['ItemID'] );
}
}
if ($format=='JSON') {
if ($item_num>0) $RSS_str.=' , ';
$RSS_str.=' { "item": {
"transactionID": "'.sprintf("%020d",$row['transactionID']).'",
"actionTimeUTC": "'.$actionTimeStr.'",
"serverUTCoffset": "'.date('Z').'",
"type": '.$row['ItemType'].',
"id": '.$row['ItemID'].',
"serverID": '.$row['ServerItemID'].',
"action": '.$row['ActionID'].',
"userID": '.$row['userID'].',
"actionData": '.$row['ActionXML'].'
}} ';
} else if ($format=='XML') {
$RSS_str.="<item>
<transactionID>".sprintf("%020d",$row['transactionID'])."</transactionID>
<actionTimeUTC>".$actionTimeStr."</actionTimeUTC>
<serverUTCoffset>".date('Z')."</serverUTCoffset>
<type>".$row['ItemType']."</type>
<id>".$row['ItemID']."</id>
<serverId>".$row['ServerItemID']."</serverId>
<action>".$row['ActionID']."</action>
<userID>".$row['userID']."</userID>
<ActionXML>".$row['ActionXML']."</ActionXML>
</item>\n";
}
$item_num++;
} // end while
if ($format=='XML') $RSS_str.="</log>\n";
else if ($format=='JSON') $RSS_str.=' ] } ';
//$RSS_str=htmlspecialchars($RSS_str);
if (!$CONF_use_utf) {
require_once dirname(__FILE__).'/lib/ConvertCharset/ConvertCharset.class.php';
$NewEncoding = new ConvertCharset;
$RSS_str = $NewEncoding->Convert($RSS_str,$langEncodings[$nativeLanguage], "utf-8", $Entities);
}
if ($zip) {
$filesToServe=array();
require_once dirname(__FILE__)."/lib/pclzip/pclzip.lib.php";
if ($getIGCfiles && count($flightsToServe) ) {
$sql="select ID, DATE, userID, filename from $flightsTable WHERE ID IN ( ";
for($i=0;$i<count($flightsToServe);$i++) {
if ($i>0) $sql.=' , ';
$sql.=$flightsToServe[$i];
}
$sql.=" ) ";
$res= $db->sql_query($sql);
# Error checking
if($res <= 0){
echo("<H3> Error in sync - get igc filenames query! $sql</H3>\n");
exit();
}
while ( $row = $db->sql_fetchrow($res) ) {
// $filename=dirname(__FILE__).'/flights/'.$row['userID'].'/flights/'.substr($row['DATE'],0,4).'/'.$row['filename'];
$filename=LEONARDO_ABS_PATH.'/'.str_replace("%PILOTID%",getPilotID($row["userServerID"],$row["userID"]),str_replace("%YEAR%",substr($row['DATE'],0,4),$CONF['paths']['igc']) ).'/'.$row['filename'];
if (is_file($filename ))
array_push($filesToServe,
array( PCLZIP_ATT_FILE_NAME => $row['ID'].".igc",
PCLZIP_ATT_FILE_CONTENT => implode("",file($filename ) )
)
);
if (is_file($filename.'saned.igc' ))
array_push($filesToServe,
array( PCLZIP_ATT_FILE_NAME => $row['ID'].".saned.igc",
PCLZIP_ATT_FILE_CONTENT => implode("",file($filename.'saned.igc' ) )
)
);
}
}
array_push($filesToServe,
array( PCLZIP_ATT_FILE_NAME => "sync.txt",
PCLZIP_ATT_FILE_CONTENT => $RSS_str )
) ;
$tmpZipFile="sync_".$clientID."_".time().".zip";
$archive = new PclZip($CONF_tmp_path.'/'.$tmpZipFile);
$v_list = $archive->create( $filesToServe,PCLZIP_OPT_REMOVE_ALL_PATH);
$outputStr=implode("", file($CONF_tmp_path.'/'.$tmpZipFile) );
@unlink($CONF_tmp_path.'/'.$tmpZipFile);
$outputFilename=$tmpZipFile;
$attachmentMIME ='application/octet-stream';
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-type: $attachmentMIME");
header('Content-Disposition: inline; filename="'.$outputFilename.'"');
header("Content-Transfer-Encoding: binary");
header("Content-length: ".strlen($outputStr));
echo $outputStr;
return;
}
//$RSS_str=htmlspecialchars($RSS_str);
//$NewEncoding = new ConvertCharset;
//$FromCharset=$langEncodings[$currentlang];
//$RSS_str = $NewEncoding->Convert($RSS_str, $FromCharset, "utf-8", $Entities);
}
if (!empty($HTTP_SERVER_VARS['SERVER_SOFTWARE']) && strstr($HTTP_SERVER_VARS['SERVER_SOFTWARE'], 'Apache/2')) {
header ('Cache-Control: no-cache, pre-check=0, post-check=0, max-age=0');
} else {
header ('Cache-Control: private, pre-check=0, post-check=0, max-age=0');
}
header ('Expires: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
header ('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
if ($format=='XML') header ('Content-Type: text/xml');
else if ($format=='JSON') header ('Content-Type: text/plain');
echo $RSS_str;
?>