forked from leonardoxc/leonardoxc
/
syncWXC2.php
393 lines (328 loc) · 12 KB
/
syncWXC2.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
<?php
//************************************************************************
// 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: syncWXC2.php,v 1.10 2010/11/23 15:05:42 manolis Exp $
// patched by AR Version 2010/04/06 12:00:00, implemented commentInternal, extend invalid/disabled/non-public flight handling
// patched by PW,AR Version 2010/07/08 13:18:00 adding "approved" handling at admin airspaceCheckMsg in case some airspaces are individual ok
//************************************************************************
/********** implements CIVL WXC synchronization protocol ***************/
/************** Mon Jan 14 18:54:15 CET 2008, by mk *********************/
// Rewritten from scratch by Durval Henke
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();
if ($CONF_use_utf) $db->sql_query("SET NAMES utf8");
$maxCountHardLimit=500;
$ServerId=$CONF_server_id;
$count=$_GET['c']+0;
if (!$count) $count=$maxCountHardLimit;
if ( $count>$maxCountHardLimit) $count=$maxCountHardLimit;
$fromTm=$_GET['from']+0;
if (!$fromTm) $fromTm=gmmktime(0, 0, 0, 1, 1, 2007);
$toTm=$_GET['to']+0;
// if (!$toTm) $toTm=time();
$zerocivl=0;
if (isset($_GET['zerocivl'])) {
$zerocivl=makeSane($_GET['zerocivl'],1);
}
// Authentication
$clientID = makeSane($_GET['clientID'],1);
$clientPass = makeSane($_GET['clientPass'],0);
if ( !Server::checkServerPass($clientID,$clientPass)) {
echo "<?xml version=\"1.0\" encoding=\"$encoding\" ?>\n";
echo "<error>Not authorized $clientID,$clientPass </error>\n";
die();
}
$where_clause='';
// check which servers to give / dont give
$dont_give_servers=$CONF['servers']['list'][$clientID]['dont_give_servers'];
if ( is_array($dont_give_servers) ) {
if (count($dont_give_servers) >0 ){
replaceCurrentServerInArray($dont_give_servers);
$where_clause.=" AND serverID NOT IN ( ";
foreach($dont_give_servers as $tmpServerID) {
$where_clause.=$tmpServerID.', ';
}
$where_clause.=$clientID .' )';
}
}
$give_only_servers=$CONF['servers']['list'][$clientID]['give_only_servers'];
if ( is_array($give_only_servers) ) {
if (count($give_only_servers) >0 ){
replaceCurrentServerInArray($give_only_servers);
$where_clause.=" AND serverID IN ( ";
foreach($give_only_servers as $tmpServerID) {
$where_clause.=$tmpServerID.', ';
}
$where_clause=substr($where_clause,0,-2);
$where_clause.=' )';
}
}
if ( $_GET['getOnlyServersList'] ) {
$getOnlyServersList=$_GET['getOnlyServersList'];
$getOnlyServersList=preg_replace("/[^,\d]/","",$getOnlyServersList);
$getOnlyServersList=replaceCurrentServer($getOnlyServersList);
$getOnlyServersListClause = " AND serverID IN ( ".$_GET['getOnlyServersList']." ) " ;
} else {
$getOnlyServersListClause = '';
}
$where_clause.= $getOnlyServersListClause;
$tableNames=array("active"=>$flightsTable,"deleted"=>$deletedFlightsTable);
$xRow=array();
$lastActionTm=0;
foreach($tableNames as $tableType=>$tableName ) {
// echo "lastActionTm:$lastActionTm<HR>";
if ($tableType=='deleted') {
$NacStatusStr="d";
$toTm=$lastActionTm;
if (! $toTm) $toTm=$fromTm;
$limit=""; // no limit , we must have all the delete actions within this 'chunk'
$count=99999999; // a big number
} else {
$NacStatusStr="o";
$limit=" LIMIT ".($count+200);
}
// echo "toTm:$toTm<HR>";
$where_clause ="AND UNIX_TIMESTAMP($tableName.dateUpdated) >= $fromTm ";
if ($toTm) {
$where_clause.=" AND UNIX_TIMESTAMP($tableName.dateUpdated) <=$toTm ";
}
$query="SELECT
$pilotsTable.CIVL_ID as CivlId,
$pilotsTable.FirstName as PilotFirstName,
$pilotsTable.LastName as PilotLastName ,
$pilotsTable.countryCode as PilotNation ,
$pilotsTable.Sex as PilotGender,
$tableName.originalURL,
$tableName.hash AS IgcMd5,
UNIX_TIMESTAMP($tableName.dateUpdated) as LastTimestamp,
UNIX_TIMESTAMP($tableName.dateAdded) as FirstTimestamp,
'$NacStatusStr' as NacStatus,
$pilotsTable.pilotID as NacPilotId,
$tableName.ID as NacFlightId,
$tableName.glider AS Glider,
(UNIX_TIMESTAMP($tableName.DATE) + $tableName.START_TIME) AS TakeoffTime,
(UNIX_TIMESTAMP($tableName.DATE) + $tableName.END_TIME) AS LandingTime,
$waypointsTable.intName as TakeoffName,
$waypointsTable.countryCode as TakeoffCountry,
$tableName.comments,
$tableName.filename,
$tableName.userID,
$tableName.userServerID,
$tableName.gliderBrandID,
$tableName.cat,
$tableName.subcat,
$tableName.category,
$tableName.DATE,
$tableName.dateAdded,
$tableName.dateUpdated,
$tableName.serverID,
$tableName.original_ID,
$tableName.checkedBy,
$tableName.private,
$tableName.airspaceCheckFinal,
$tableName.airspaceCheckMsg,
DATE_FORMAT($tableName.DATE,'%Y') as Year
FROM $tableName,$pilotsTable,$waypointsTable
WHERE
$tableName.userID =$pilotsTable.pilotID AND
$tableName.userServerID =$pilotsTable.serverID AND
$waypointsTable.ID = $tableName.takeoffID
$where_clause
ORDER BY $tableName.dateAdded $limit
";
// print $query; // exit;
$res= $db->sql_query($query);
if($res <= 0){
echo("<H3> Error in query! <br>".$query." </H3>\n");
exit();
}
$gotEntries=0;
while($row = mysql_fetch_assoc($res)) {
//if ($row['userServerID']) $extra_prefix=$row['userServerID'].'_';
//else $extra_prefix='';
//$filename=$flightsAbsPath.'/'.$extra_prefix.$row['userID'].'/flights/'.$row['Year'].'/'.$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 ($tableType=='active' && !file_exists($filename) ){
// echo" igc not found :$actionTm <BR>";
continue;
}
//echo" igc FOUND !!!!!:$actionTm <BR>";
$actionTimeStr=$row['dateUpdated'];
$actionTm=fulldate2tmUTC($actionTimeStr);
$addedTimeStr=$row['dateAdded'];
$addedTm=fulldate2tmUTC($addedTimeStr);
if ($gotEntries>=$count) { // we are over the limit
// if this $actionTm differs from the last one
// we have forfilled RULE #1 , time to break the loop
if ($actionTm!=$lastActionTm ) break;
}
$xRow[]=$row;
$gotEntries++;
$item_num++;
}
// print_r($xRow);
if ($tableType!='deleted') {
$lastActionTm=$actionTm;
$lastActionTimeStr=$actionTimeStr;
}
}
$item_num=0;
$item_ok=0;
$max_time=0;
foreach ($xRow as $row) {
if(makeSane($row['CivlId'],1)>0 || $zerocivl) {
if($max_time < $row['LastTimestamp']){
$max_time=$row['LastTimestamp'];
}
$row['CommentInternal']=htmlspecialchars(html_entity_decode($row['checkedBy'],ENT_NOQUOTES,"UTF-8") );
// this should be filled from a $CONF[''] variable... defined in site/config_custom.php
$row['CivlIdApprovedBy']=$CONF['WXC']['CivlIdApprovedBy']+0;
// disable any private/disabled and airspace invalid flight from WXC score
if($row['private'] > 0) {
$row['NacStatus'] = "d";
}
//in case of detected airspace violation, pilot might still have approval for that airspace... so we allow sync when word "approved" is set at comment first
if(($row['airspaceCheckFinal'] == "-1") && (strtolower(substr($row['airspaceCheckMsg'],0,8)!="approved")) ) {
$row['NacStatus'] = "d";
}
$row['GliderCat']=convertCat($row['cat'],$row['category']);
if ( $row['serverID']==0 || $row['serverID']==$CONF_server_id ) $isLocal=1;
else $isLocal=0;
if ($isLocal) {
$row['FlightUrl']="http://".$_SERVER['SERVER_NAME'].getLeonardoLink(array('op'=>'show_flight','flightID'=>$row['NacFlightId']));
} else {
if ( $row['serverID'] != 0 && ! $row['originalURL']) {
if ( $CONF['servers']['list'][$row['serverID']]['isLeo'] ==1 ) {
if (! $row['originalURL'] ) {
$row['originalURL']='http://'.$CONF['servers']['list'][$row['serverID']]['url'].'&op=show_flight&flightID='.$row['original_ID'];
}
}
}
$row['FlightUrl']= $row['originalURL'];
}
$row['FlightUrl']=htmlspecialchars($row['FlightUrl']);
$trackPath=getRelMainDir().str_replace("%PILOTID%",getPilotID($row["userServerID"],$row["userID"]),str_replace("%YEAR%",substr($row['DATE'],0,4),$CONF['paths']['igc']) ).'/'.rawurlencode($row['filename']);
$row['IgcUrl']="http://".$_SERVER['SERVER_NAME'].$trackPath;
// the pilot id is serverid_pilotid to be able to distinguish ids across servers
//0_12345
//5_12345
//27_12345
$row['NacPilotId']= $row["userServerID"].'_'.$row["userID"];
$WxcFields=Array('CivlId','PilotFirstName','PilotLastName','PilotNation','PilotGender',
'CivlIdApprovedBy','IgcUrl','IgcMd5','LastTimestamp','FirstTimestamp','NacStatus','NacPilotId','NacFlightId',
'GliderCat','Glider','TakeoffTime','LandingTime','TakeoffName','TakeoffCountry','CommentInternal','FlightUrl');
foreach($row as $key => $value){
if(in_array($key,$WxcFields)){
$valueSafe=$value;
if (in_array($key,array('PilotFirstName','PilotLastName','Glider','TakeoffName','CommentInternal') ) ) {
$valueSafe="<![CDATA[$value]]>";
}
$FlighsXML[$item_ok].= " <$key>$valueSafe</$key>\n";
}
}
$item_ok++;
} // END IF
}
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');
header ('Content-Type: text/xml;');
echo '<?xml version="1.0" encoding="utf-8" ?>'."\n";
echo '<!DOCTYPE FlightSync PUBLIC "-//XCC Nonprofit Organisation//DTD FlightSync Library//EN" "http://www.xccomp.org/spec/xcc-flightsync.dtd">'."\n";
echo '<FlightSync version="0.7">'."\n";
echo '<SyncInfo>'."\n";
echo ' <ServerId>'.$ServerId.'</ServerId>'."\n";
echo ' <NumberOfItems>'.$item_ok.'</NumberOfItems>'."\n";
// echo ' <LastTimestamp>'.$lastActionTm.'</LastTimestamp>'."\n";
echo '</SyncInfo>'."\n";
if($item_ok>0){
$idx=0;
foreach($FlighsXML as $flightxml){
$idx++;
echo '<FlightInfo Id="'.$idx.'">'."\n";
echo $flightxml;
echo "</FlightInfo>"."\n";
}
}
echo '</FlightSync>'."\n";
function convertCat($cat,$category) {
switch ($cat){
case "0":
$GliderCat="0";
break;
case "1":
$GliderCat="3";
break;
case "2":
$GliderCat="1";
break;
case "4":
$GliderCat="5";
break;
default:
$GliderCat="9"; // non free glider
}
switch ($category){
case "1":
$GliderCat.="201";
break;
case "2":
$GliderCat.="901";
break;
case "3":
$GliderCat.="902";
break;
default :
$GliderCat.="901";
break;
}
return $GliderCat;
}
// search if the cussrent server id is in the list, and replace it by 0,
// (0 is used in the DB for serverID for local flights)
function replaceCurrentServer($getOnlyServersList) {
global $CONF_server_id;
$getOnlyServersList=preg_replace("/[^,\d]/","",$getOnlyServersList);
$tmpList=split(",",$getOnlyServersList);
$getOnlyServersList='';
foreach($tmpList as $tmpServerItem) {
$tmpServerItem +=0;
if ( ! $tmpServerItem )
continue;
if ($tmpServerItem ==$CONF_server_id )
$tmpServerItem ="0";
$getOnlyServersList.=$tmpServerItem.',';
}
$getOnlyServersList=substr($getOnlyServersList,0,-1);
return $getOnlyServersList;
}
function replaceCurrentServerInArray(&$ServersList) {
global $CONF_server_id;
foreach($ServersList as $i=>$tmpServerItem) {
$tmpServerItem +=0;
if ($tmpServerItem ==$CONF_server_id )
$ServersList[$i]=0;
}
return $ServersList;
}
?>