<?
//************************************************************************
// 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: EXT_config_pre.php,v 1.12 2010/03/14 20:56:10 manolis Exp $                                                                 
//
//************************************************************************
//	define('EXT_CONFIG',1);
	
	//$tmpDir=dirname(__FILE__);
	//$tmpParts=explode("/",str_replace("\\","/",$tmpDir));
	//$module_name=$tmpParts[count($tmpParts)-1]; 

//	$moduleAbsPath=dirname(__FILE__);
//	$moduleRelPath=".";

	$isExternalFile=1;	
    require_once dirname(__FILE__)."/site/config_op_mode.php";
	if ($opMode==5 && $CONF_use_own_template==1) { // Joomla
		define( '_JEXEC', 1 );
		define( 'DS', DIRECTORY_SEPARATOR );		
		
		require_once JPATH_BASE.DS.'includes'.DS.'defines.php';
		require_once JPATH_BASE.DS.'includes'.DS.'framework.php';
		$mainframe =& JFactory::getApplication('site');
		$user   =& JFactory::getUser();   
	}
/*

	if (!$baseInstallationPathSet) {
		$baseInstallationPath="";
		$baseInstallationPathSet=1;
		$parts=explode("/",$_SERVER['REQUEST_URI']);
	
		if ( count($parts)>1 )  {
			for($i=1;$i<count($parts);$i++) {
			   // if ($parts[$i-1]=="modules") break;
			   if ($parts[$i-1]!='') $baseInstallationPath.="/".$parts[$i-1];	
			}
		}
	}
*/
?><?
//************************************************************************
// 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: config.php,v 1.164 2012/11/05 07:37:13 manolis Exp $                                                                 
//
//************************************************************************

error_reporting(E_ERROR);
// set this to always use . in sprintf 
setlocale(LC_NUMERIC, 'en_US') ;
@ini_set('auto_detect_line_endings',true);

// This file contains default values and is overwritten on new updates -installs
// Dont edit this file, edit site/config_custom.php instead

  $CONF_version="5.0.52";
  $CONF_releaseDate="2023/03/31";
  $CONF_release_notes="http://trac.pgxc.pl/report/6?max=600";
// opMode 
// 1 = PHPnuke module
// 2 = phbb2 module
// 3 = standalone -- still work in progress
 $opMode= 2; 
 require dirname(__FILE__)."/site/config_op_mode.php";

 // Here we define which server Id is the master server of the leonardo Network
 $CONF_master_server_id=1;

 // Our server ID -> usually 0 for non network operation
 $CONF_server_id=0;
 
 // various display parameters
 $CONF['display']['blocks']['right_side']=1;

 // if it is a phpbb module we can use our own template 
 // and not the onw of the forum 
 if ( !isset($CONF_use_own_template) ) $CONF_use_own_template=0;
 
 // for phpbb when  $CONF_use_own_template=1;
 if ( !isset($CONF_use_own_login) )  $CONF_use_own_login=0;
 
 // rely on  leonardo_pilits name flields  entirely
 $CONF_use_leonardo_names=0;
 
 // admin_users 
 // put in this array the userID of the users you want ot grand admin status
 // YOU MUST PUT AT LEAST ONE ADMIN USER

 $admin_users=array();
 // mod_users 
 // put in this array the userID of the users you want ot grand mod status
 // You can leave this blank
 $mod_users=array();

 // ***********************************************************
 // put your admin/mod users in this file !!!!!!!!!!!!!!!!!
 // ***********************************************************
 @include_once dirname(__FILE__)."/site/config_admin_users.php"; 

 $CONF_admin_email="your_mail@nowhere.com";
 require_once dirname(__FILE__)."/site/config_admin_email.php"; 
 
 $CONF_main_page="index_full";

 // club config
 $clubsList=array();
 @include_once dirname(__FILE__)."/site/config_clubs.php"; 

 // define the path where the maps reside
 // use this if you have the maps stored 
 // on the local server in a directory called maps/
 $mapsPath=dirname(__FILE__)."/maps";

 // use this if you have them on another windows drive
 // $mapsPath="E:\\maps";

 // or you can use an url if they are in a other server
 // $mapsPath="http://www.mapserver.com/my_maps";

 // CAUTION this will not work for importing the maps into the 
 // database, you should have already an workiing map db 
 // in order to use a url

 // this is for the  DEM - 3d maps, to compute ground height 
 $CONF_DEMpath=dirname(__FILE__)."/maps/hgt/";


 // the native language of the server
 $nativeLanguage="english";
 
 // if you intend to use google maps API to display fancy maps of takeoffs 
 // get one key for your server at 
 // http://www.google.com/apis/maps/signup.html
 // else leave it blank
 //$CONF_google_maps_api_key="AIzaSyAYfsSCuHMIYEamXdKok34VpXStqc06j1E";
 $CONF_google_maps_api_key= rtrim(file_get_contents("/run/secrets/google_maps_api_key")); 
 $CONF_google_api_key= rtrim(file_get_contents("/run/secrets/google_maps_api_key")); 

//AIzaSyDZzLpNolyAyslC9i8QiQb3kyC5ZL2qppg

 // use utf language files 
 // not yet ready !! dont use !
 $CONF_use_utf=1;
 
 // Available translations
// $availableLanguages=array("english","french","german","dutch","italian","spanish","mexican","portuguese","brazilian",
//				    "greek","turkish","danish","swedish","finnish","russian","bulgarian","croatian","slovenian","polish","czech","hungarian","romanian","chinese","hebrew");

 $availableLanguages=array("english","polish");
 $langEncodings=array(
	"albanian"=>"iso-8859-2","arabic"=>"iso-8859-6","bulgarian"=>"windows-1251","brazilian"=>"iso-8859-1",
	"catalan"=>"iso-8859-1", "chinese"=>"gb2312",
	"croatian"=>"windows-1250","czech"=>"iso-8859-2","danish"=>"iso-8859-1","dutch"=>"iso-8859-1",
	"english"=>"iso-8859-1","estonian"=>"iso-8859-15","finnish"=>"iso-8859-1","french"=>"iso-8859-1",
	"german"=>"iso-8859-1","greek"=>"iso-8859-7","hebrew"=>"iso-8859-8-i","hungarian"=>"iso-8859-2",
	"icelandic"=>"iso-8859-1","italian"=>"iso-8859-1","latvian"=>"iso-8859-13","lithuanian"=>"iso-8859-13",
	"macedonian"=>"iso-8859-5","mexican"=>"iso-8859-1","norwegian"=>"iso-8859-1","polish"=>"iso-8859-2","portuguese"=>"iso-8859-1",
	"romanian"=>"iso-8859-2","russian"=>"windows-1251","serbian"=>"iso-8859-5","serbo-croatian"=>"iso-8859-2",
	"slovak"=>"iso-8859-2","slovenian"=>"iso-8859-2","spanish"=>"iso-8859-1","swedish"=>"iso-8859-1",
	"turkish"=>"iso-8859-9");

 $lang2iso=array("english"=>"en","german"=>"de","dutch"=>"nl","french"=>"fr", "italian"=>"it",
 			"spanish"=>"es","portuguese"=>"pt","brazilian"=>"br","greek"=>"gr","turkish"=>"tr",
			"swedish"=>"se","finnish"=>"fi","polish"=>"pl","bulgarian"=>"bg","romanian"=>"ro","russian"=>"ru","serbian"=>"cs",
			"croatian"=>"hr","mexican"=>"mx","polish"=>"pl" ,"czech"=>"cz" ,"hungarian"=>"hu","slovenian"=>"si",
			"danish"=>"dk","chinese"=>"cn","hebrew"=>"il");

 $CONF['lang']['lang2countryFlag']=array(
			"english"=>"us","german"=>"de","dutch"=>"nl","french"=>"fr", "italian"=>"it",
 			"spanish"=>"es","portuguese"=>"pt","brazilian"=>"br","greek"=>"gr","turkish"=>"tr",
			"swedish"=>"se","finnish"=>"fi","polish"=>"pl","bulgarian"=>"bg","romanian"=>"ro","russian"=>"ru","serbian"=>"cs",
			"croatian"=>"hr","mexican"=>"mx","polish"=>"pl" ,"czech"=>"cz" ,"hungarian"=>"hu","slovenian"=>"si",
			"danish"=>"dk","chinese"=>"cn","hebrew"=>"il");


 $lang2isoEditor=array("english"=>"en","german"=>"de","dutch"=>"nl","french"=>"fr", "italian"=>"it",
 			"spanish"=>"es","portuguese"=>"pt","brazilian"=>"pt-br","greek"=>"el","turkish"=>"tr",
			"swedish"=>"sv","finnish"=>"fi","polish"=>"pl","bulgarian"=>"bg","romanian"=>"ro","russian"=>"ru","serbian"=>"cs",
			"croatian"=>"hr","mexican"=>"es","polish"=>"pl" ,"czech"=>"cz" ,"hungarian"=>"hu","slovenian"=>"si",
			"danish"=>"da","chinese"=>"zh","hebrew"=>"he");

 $lang2isoGoogle=array("english"=>"en","german"=>"de","dutch"=>"nl","french"=>"fr", "italian"=>"it",
 			"spanish"=>"es","portuguese"=>"pt","brazilian"=>"pt","greek"=>"el","turkish"=>"tr",
			"swedish"=>"sv","finnish"=>"fi","polish"=>"pl","bulgarian"=>"bg","romanian"=>"ro","russian"=>"ru","serbian"=>"sr",
			"croatian"=>"hr","mexican"=>"es","polish"=>"pl" ,"czech"=>"cs" ,"hungarian"=>"hu","slovenian"=>"sl",
			"danish"=>"da","chinese"=>"zh","hebrew"=>"he");
			
  $CONFIG_langsSpoken=array(
	"albanian"=>array("al"),"arabic"=>array("eg"),"bulgarian"=>array("bg"),
	"catalan"=>array("es"),"chinese"=>array("cn"),"czech"=>array("cz"),"danish"=>array("dk"),
	"estonian"=>array("ee"),"finnish"=>array("fi"),"hebrew"=>array("il"),
	"hungarian"=>array("hu"),"icelandic"=>array("is"),"italian"=>array("it"),
	"latvian"=>array("lv"),"lithuanian"=>array("lt"),"macedonian"=>array("mk"),
	"norwegian"=>array("no"),"polish"=>array("pl"),"romanian"=>array("ro"),
	"russian"=>array("ru"),"serbian"=>array("cs"),"slovak"=>array("sk"),
	"slovenian"=>array("si"),"swedish"=>array("se"),"turkish"=>array("tr"),
	
	"croatian"=>array("hr","ba"),
	"dutch"=>array("be","nl"),
	"portuguese"=>array("br","pt"),
	"english"=>array("au","ca","gb","ie","nz","us"),
	"french"=>array("be","ca","fr","ch","re"),
	"german"=>array("at","de","ch"),
	"greek"=>array("cy","gr"),
	"spanish"=>array("ar","bo","cl","co","cr","ec","sv","gt","mx","ni","pa","py","pe","es","uy","ve")
  );		

 // If you have a waypoint database that has names in another language
 // than english but you want to diplay waypoint names in english 
 // set this to 1 to force that
 // if you have $nativeLanguage="english"; you will most probably want to
 // set it to 1 , else set it to 0
 $CONFIG_forceIntl=0;

 // set to 1 if you have an map server running
 // set to 0 if you dont know what a map server is ...
 // ... but you will not have the track plotted on the map 
 $mapServerActive=1;

 // dont use DEM for ground elevation by default
 $CONF['maps']['3d']=0;

 // Use google maps to display flight track ( needs a google maps key : see above  $CONF_google_maps_api_key )
 $CONF_google_maps_track=0;
 $CONF_google_maps_track_order=2; // will apear in the second tab
 $CONF_google_maps_track_only=0;  // use only google maps,  discard the local map server


 // set to 1 if you have an scoring server running
 // set to 0 if you dont know what a scoring server is ...
 // ... but you will not have the OLC score
 $scoringServerActive=1;

 // set to 1 if you have an working GD 2.0
 // set to 0 else 
 // ... but you will not have the flight's charts
 $chartsActive=1;

  // the OLC path 
 // this is needed for the automatic submission to OLC
 $olcServerURL = "http://olc.onlinecontest.org/olc-cgi/2006/holc-i/olc";

 // set this to 0 if you dont want to give the functionality of OLC submits
 // OLC scoring will be done even if you set this to 0. 
 // set $scoringServerActive=0 to disable the scoring.
 $enableOLCsubmission=0;


 // validate against the G-record
 $CONF_use_validation=1;
 $CONF['validation']['user_internal_server']=0;
 //$CONF['validation']['server_url']="http://wxc.fai.org/vali/leonardoVali.php";
 $CONF['validation']['server_url']="https://leonardo.pgxc.pl/vali/leonardoVali.php";
 //$CONF['validation']['server_url']="";
 
 
 // Membership of NAC (National Airsport Control, also referred as National Aero Club)
 $CONF_NAC_list=array();
 $CONF_use_NAC=0;
 // Allow pilots to assgin a different NAC club to each of their flights
 $CONF['NAC']['clubPerFlight'] =0;
 
 // set this to 0 if you dont want to give the functionality of OLC submits
 // OLC scoring will be done even if you set this to 0. 
 // set $scoringServerActive=0 to disable the scoring.
 $enablePrivateFlights=1;


 // flightsTable
 // the name of the table holding the flights....
 // you should probably not change that unless you have 
 // many leonardo servers sharing the same database
 $CONF_tables_prefix="leonardo";
 $flightsTable	=  $CONF_tables_prefix."_flights";
 $deletedFlightsTable=	$CONF_tables_prefix."_flights_deleted";
 $pilotsTable	=  $CONF_tables_prefix."_pilots";
 $pilotsInfoTable	=  $CONF_tables_prefix."_pilots_info";
 $mapsTable		=  $CONF_tables_prefix."_maps";
 $waypointsTable=  $CONF_tables_prefix."_waypoints";
 $clubsTable	=  $CONF_tables_prefix."_clubs";
 $NACclubsTable =  $CONF_tables_prefix."_nac_clubs";
 $clubsPilotsTable	=  $CONF_tables_prefix."_clubs_pilots";
 $clubsFlightsTable	=  $CONF_tables_prefix."_clubs_flights";
 $serversTable	=  $CONF_tables_prefix."_servers";
 $logTable		=  $CONF_tables_prefix."_log";
 $statsTable		=  $CONF_tables_prefix."_stats";
 $airspaceTable		=  $CONF_tables_prefix."_airspace";

 $areasTable	=		$CONF_tables_prefix."_areas";
 $areasTakeoffsTable=	$CONF_tables_prefix."_areas_takeoffs";
 $photosTable  	=		$CONF_tables_prefix."_photos";
 $remotePilotsTable =	$CONF_tables_prefix."_remote_pilots";
 $scoresTable		=  $CONF_tables_prefix."_flights_score";

 $thermalsTable		=  $CONF_tables_prefix."_thermals";
 $commentsTable		=  $CONF_tables_prefix."_comments";

 $jobsTable			= $CONF_tables_prefix."_queue";

 $bannedPilotsTable = $CONF_tables_prefix."_rank_banned_pilots";
 $rankPilotsTable = $CONF_tables_prefix."_rank_pilots";
 $bannedFlightsTable = $CONF_tables_prefix."_rank_banned_flights";
 $rankTakeoffsTable = $CONF_tables_prefix."_rank_takeoffs";
 
 // Mrsid tiles config
 $maxMrSidResolution=28.5; // m/pixel Smaller is better.
 $minMrSidResolution=28.5; // m/pixel

 // ------------------------
 //  various config
 // ------------------------
 $takeoffRadious= 1000 ; // in m
 $landingRadious= 1000 ; // in m
 $CONF_itemsPerPage=100 ;
 $CONF_compItemsPerPage=100;
 $CONF_defaultThemeName="pgxc";
 $CONF_metricSystem=1; //  1=km,m     2=miles,feet

 // Debug leave it 0
 $CONF_show_DBG_XML=1;

 $CONF_countHowManyFlightsInComp=6;

 $CONF_showProfilesToGuests=1;

 // Max is 6 
 $CONF_photosPerFlight=6;
 $CONF_max_photo_size=3000; // 3000 Kb

 // Available Types and subtypes of Gliders
 $CONF_glider_types=array(1=>"Paraglider",2=>"Flex wing FAI1",4=>"Rigid wing FAI5",8=>"Glider",
				16=>"Paramotor",32=>"Trike", 64=>"Powered flight",256=>"HG Mosquito" );
// $CONF_glider_types=array(1=>"Paraglider");


 $CONF['startTypes']=array(1=>"Klasyczny",2=>"Wyciągarka",4=>"Microlight tow",8=>"E-motor" );
 $CONF['defaultStartType']=1;


 $CONF_glider_certification_categories =array(
 		1=>"LTF 1",2=>"LTF 1/2",4=>"LFT 2", 8=>"LFT 2/3" , 16=>"LFT 3" , 
 		32=>"EN A",64=>"EN B",128=>"EN C", 256=>"EN D" , 
 		1024=>"Proto");
 
 $CONF_cert_avalable_categories=array(
	0=>array(1,2,3,4), // ALL
 	1=>array(1,2,3,4), // ltf 1
 	2=>array(1,2,3,4),
 	4=>array(1,2,3),
 	8=>array(2,3),   // ltf 2/3
 	16=>array(2,3),  // ltf 3
 	32=>array(1,2,3,4), //en a
 	64=>array(1,2,3,4),  
 	128=>array(2,3),
 	256=>array(2,3),
 	1024=>array(2,3),
  );
 
 $CONF_xc_types=array(1=>"3 Turnpoints XC",2=>"Open Triangle",4=>"Closed Triangle");
 $CONF_xc_types_db=array(1=>"FREE_FLIGHT",2=>"FREE_TRIANGLE",4=>"FAI_TRIANGLE");				

 $CONF['gliderClasses']=array(
    // pg
 	1=>array( 
		'default_class'=>2,
		'classes'=>array(1=>"Sport",2=>"Open",3=>"Tandem"),		
	),
	2=>array( 
		'default_class'=>2,
		'classes'=>array(1=>"Kingpost",2=>"Topless"),
	),
 );	


 // these are no longer used! 
 //$CONF_category_types =array(1=>"Sport",2=>"Open",3=>"Tandem");
 //$gliderClassList=$CONF_category_types;

 // defaul values 
 $xcTypesList=$CONF_xc_types;

 $CONF_default_cat_view=0; // pg
//20180730 - from sport (1) to Open (2)
 $CONF_default_cat_add=1; //  the default category for submitting new flights 
 $CONF_default_cat_pilots=1; // the default cat to display pilots for
 
 // The top 'dates' menu  will have years starting from $CONF_StartYear
 // Deprecated -  not use it anywherenow
 $CONF_StartYear=1998;


 // flights that were submitted these days ago will have a "new" icon
 // set to -1 to disable
 $CONF_new_flights_days_threshold=60;

 // Flights older than these days will not be accepted.
 // set to 0 to deactivate
 // Admins cat submit flights either way for any day in the past!
 $CONF_new_flights_submit_window=0;

 // this will allow the use of pre-calculated turnpoints for OLC score optimization
 // This is done by uploading an .OLC file
 $CONF_allow_olc_files=1;
 
 // this will allow an external program to upload a flight to this server.
 // the program must post a form to http://serveraddess/modules/leonardo/flight_submit.php
 $CONF_allow_direct_upload=1;

 // this will enable a calendar in flights_list to select individual days
 $CONF_use_calendar=1;
 
	// SEASON MOD
	// start of seasons config (2007-09-27)
	require_once dirname(__FILE__)."/CL_dates.php";
	
	$CONF['years']=array(
		'use_calendar_years'=>1,
		'start_year'=>1998,
		'end_year'=>date("Y")
		);
	
	
	// set this to 1 to enable seasons
	$CONF['seasons']['use_season_years']=0;	
	
	// IF this is set then all info on which sesons to display in hte menu
	//     will be taken forh the $CONF['seasons']['seasons']
	// ELSE 
	//    the menu will display season starting from $CONF['seasons']['start_season'] to $CONF['seasons']['end_season']
	//    BOTH VALUES MUST BE defined
	//    In this case also the season start/end will be defined from 
	//    $CONF['seasons']['season_default_start'] and $CONF['seasons']['season_default_end']	
	$CONF['seasons']['use_defined_seasons']=0;

	// Defined seasons
	// The next array will be used in case of 	$CONF['seasons']['show_only_defined_seasons']=1
	$CONF['seasons']['seasons']=array(
		2021=>array('start'=>'2020-10-1','end'=>'2021-09-30'),
		2020=>array('start'=>'2019-10-1','end'=>'2020-09-30'),
		2008=>array('start'=>'2018-10-1','end'=>'2019-09-30'),
		2008=>array('start'=>'2017-10-1','end'=>'2018-09-30'),
		2008=>array('start'=>'2016-10-1','end'=>'2017-09-30'),
		2008=>array('start'=>'2015-10-1','end'=>'2016-09-30'),
		2008=>array('start'=>'2014-10-1','end'=>'2015-09-30'),
		2008=>array('start'=>'2013-10-1','end'=>'2014-09-30'),
		2008=>array('start'=>'2012-10-1','end'=>'2013-09-30'),
		2008=>array('start'=>'2011-10-1','end'=>'2012-09-30'),
		2008=>array('start'=>'2010-10-1','end'=>'2011-09-30'),
		2008=>array('start'=>'2009-10-1','end'=>'2010-09-30'),
		2008=>array('start'=>'2008-10-1','end'=>'2009-09-30'),
		2008=>array('start'=>'2007-10-1','end'=>'2008-09-30'),
		2007=>array('start'=>'2006-10-1','end'=>'2007-9-30',
					'subseasons'=>array(
						'winter'=>array('start'=>'2006-10-1','end'=>'2007-3-31','localName'=>'winterInLocalLanguage'),
						'summer'=>array('start'=>'2007-3-1','end'=>'2007-9-30','localName'=>'summerInLocalLanguage'),
					)					
				),
	
	);				

	// The next 4 values will be used in case of 	$CONF['seasons']['show_only_defined_seasons']=0
	$CONF['seasons']['season_default_start']='10-1';
	$CONF['seasons']['season_default_end']='9-31';		
	
	// ONLY ONE of the 3 next varibles canbe set to TRUE !!!
	// if the season 2007 is 2006-10-1 till  2007-9-30
	$CONF['seasons']['season_start_year_diff']=-1;
	$CONF['seasons']['season_end_year_diff']=0;

	// else if season 2007 is 2007-4-1 till  2008-3-31
	//$CONF['seasons']['season_start_year_diff']=0;
	//$CONF['seasons']['season_end_year_diff']=1;

	// else if season 2007 is 2007-1-1 till  2007-12-31
	//$CONF['seasons']['season_start_year_diff']=0;
	//$CONF['seasons']['season_end_year_diff']=0;

	$CONF['seasons']['start_season']=2006;
	$CONF['seasons']['end_season']= dates::getCurrentSeason(0);

 // end of seasons config
 				

 // start of BRANDS MOD
	$CONF['brands']['filter_brands'] = 0;
	$CONF['brands']['showAll']=1;
	$CONF['brands']['list']=array();
	$CONF['brands']['filterList']=array(1,2,4);

 // end of BRANDS MOD
	
 // this is the default setting for the user's -> "My settings"
 $CONF_googleMapsShow=1;
 
 // 1- > western -> firstName - LastName
 // 2- > eastern -> LastName - firstName 
 $CONF_defaultNameOrder=1; 
 
 // USE airspace checking
 $CONF_airspaceChecks=0; 
 // airspace infrigments will be show to admins and the owner of the track
 if (isset($CONF['airspace'])){
 $CONF['airspace']['view']=='own';
 // display airspace in google maps
 $CONF['airspace']['enable']=1;
 
 $CONF['aispace']['list']['colors']=array(
	'CLASSC'=>'FF0008',
	'CLASSD'=>'FF0008',
 	'ALLOTHER'=>'FFFF00',
 );
 }					
 // Will use the date('0') together with the timezone name for TZ detection
 // This is buggy in php 4.4.1 and before .
 $CONF_use_date_for_TZ_detection=1;


  // use htc files for ie mouse over TR elements
  // May not work on servers behind firewalls
  $CONF_use_htc_ie_hack=1;

  $CONF['list_flights']['fields']['scoring'][0]='LINEAR_DISTANCE'; // LINEAR_DISTANCE or SCORE_SPEED

  $CONF['sync']['protocol']['format']='JSON';

  // dont enable statistics by default, it uses a lot of space !!!
  $CONF['stats']['enable']=0;

  // use a WYSIWYG editor for editing takeoff information
  $CONF['editor']['use_wysiwyg']['global']=1;
  $CONF['editor']['use_wysiwyg']['takeoff_description']=1;
  $CONF['editor']['use_wysiwyg']['flight_comments']=1;

  // default user prefs 
  $CONF['default_user_prefs']['useEditor']=1;
  
  //  live thumbnails 
  $CONF['photos']['mid']['max_width']=800;
  $CONF['photos']['mid']['max_height']=800;

  $CONF['photos']['carousel']['max_width']=1300;
  $CONF['photos']['carousel']['max_height']=1300;

  $CONF['photos']['normal']['max_width']=3840;
  $CONF['photos']['normal']['max_height']=3840;
  //$CONF['photos']['normal']['max_width']=1920;
  //$CONF['photos']['normal']['max_height']=1920;
  
  $CONF['photos']['thumbs']['max_width']=130;
  $CONF['photos']['thumbs']['max_height']=130;
  $CONF['photos']['thumbs']['max_width']=400;
  $CONF['photos']['thumbs']['max_height']=400;
  
  $CONF['photos']['tiny']['max_width']=60;
  $CONF['photos']['tiny']['max_height']=60;
  
  $CONF['photos']['compression']=20;
  
  // the scoring co-efficients 
  $CONF['scoring']['default_set']=1;
  $CONF['scoring']['sets']=array(
		1=>array('name'=>'XC scoring',
				 'code'=>'OLC',
				 'types'=>array('FREE_FLIGHT'=>1.5,
								'FREE_TRIANGLE'=>1.75,
								'FAI_TRIANGLE'=>2,
						)
			),

/*
Triangle conform to the FAI definition 
(the shortest leg of the triangle must be at least 28% of the total triangle)
*/
		2=>array('name'=>'FAI scoring',
				 'code'=>'FAI',
				 'types'=>array('FREE_FLIGHT'=>1,
								'FREE_TRIANGLE'=>1.2,
								'FAI_TRIANGLE'=>1.4,
						)
			),

		3=>array('name'=>'SPBW scoring',
				 'code'=>'SPBW',
				 'types'=>array('FREE_FLIGHT'=>1,
								'FREE_TRIANGLE'=>1.4,
								'FAI_TRIANGLE'=>1.6,
						)
			),
	);
	
	
	if ( strpos(strtolower(PHP_OS), 'win')  === false ) $CONF['os']='linux';
	else $CONF['os']='windows';
		
	$CONF['thermals']['enable']=false;

	// choose how to send mail to users from the system
	
	$CONF['mail']['method']='system'; // use mail() function
	//20171205 zmiana smtp
/*	$CONF['mail']['method']='smtp';  // use an external mail server
	$CONF['mail']['smtp']['host']='127.0.0.1';
	$CONF['mail']['smtp']['port']=25;
	$CONF['mail']['smtp']['ssl']=false;
	$CONF['mail']['smtp']['username']='';
	$CONF['mail']['smtp']['password']='';
	
*/	
	$CONF['site']['name']="LeonardoXC PHP UPGRADE";
	
	$CONF_force_civlid=0;
	
    $CONF['NAC']['display_club_details_on_pilot_list']=0;
	
	$CONF['db_browser']['maxTrackNum']=100;
	
	$CONF['pdf']['pdfcreator']=dirname(__FILE__).'/lib/pdf/wkhtmltopdf   ';
	$CONF['pdf']['tmpPath']=dirname(__FILE__).'/data/pdf/tmp';
	$CONF['pdf']['tmpPathRel']="data/pdf/tmp";
	$CONF['pdf']['maxflightsPerPrint']=20;
	$CONF['pdf']['daysTokeepPdfOnServer']=3;
//-----------------------------------------------------------------------------
// DONT EDIT BELOW THIS LINE --- EDIT last lines only
//-----------------------------------------------------------------------------

function setVarFromRequest($varname,$def_value,$isNumeric=0) {
	if ($varname=='day' || $varname=='month' || $varname=='year' ) {
			if ( isset($_SESSION[$varname.'leo']))  $_SESSION[$varname]=$_SESSION[$varname.'leo'];
	}

	global $$varname;
	 // echo "SES:".$_SESSION[$varname].", REQ:".$_REQUEST[$varname].", varname:".$varname.", def_value: $def_value<BR>";
	 // first we see if this was passed as an arguemnt
	if (isset($_REQUEST[$varname])) {
	  $$varname=$_REQUEST[$varname];
	  $_SESSION[$varname]=$$varname;
	// then if it already existed in the SESSION 
	} else if ( isset($_SESSION[$varname])  ) {
	  $$varname=$_SESSION[$varname];
	// if nothing of the above, put the default value  
	} else { // default value
	  $$varname=$def_value;
	  $_SESSION[$varname]=$$varname;
	}
	
	// sanitize the variable!
	if ($isNumeric) { // just add 0, this should do the trick
	  $$varname=$$varname+0;
	} else { // is string : allow only  a-zA-Z0-9_
	  $$varname=preg_replace("/[^\w_\.]/","",$$varname);
	}
	$_SESSION[$varname]=$$varname;
	if ($varname=='day' || $varname=='month' || $varname=='year' )   $_SESSION[$varname.'leo']=$$varname;
	
	// echo "$varname=".$$varname."#";
}

function makeSane($str,$type=0) {
	if ($type==1) { // 1=> numeric just add 0, this should do the trick
		return $str+0;
	} else 	if ($type==0) { // is "strict" string : allow only  a-zA-Z0-9_-,.
		return preg_replace("/[^\w_\-\,\.]/","",$str);
	} else 	if ($type==2) { // is "relaxed" string : allow only  a-zA-Z0-9_
		return preg_replace('/\\\/',"",$str);
	} else {	// no sanitation
		return $str; 
	}

}

function setVar($varname,$value) {
	global $$varname; 
    //echo "SES:".$_SESSION[$varname]."#REQ:".$_REQUEST[$varname]."#".$varname."<BR>";
	  $$varname=$value;
	  $_SESSION[$varname]=$$varname;
	  if ($varname=='day' || $varname=='month' || $varname=='year' )   $_SESSION[$varname.'leo']=$$varname;
	 // $_GET[$varname]=$$varname;
	 // $_POST[$varname]=$$varname;
	 // $_REQUEST[$varname]=$$varname;
	 // echo "SETVAR: $varname=".$$varname."#";
}

// you should probably set  $OLCScoringServerPath to the same server 
// you have leonardo
$OLCScoringServerUseInternal=1;
// These are not needed if $OLCScoringServerUseInternal=1;
$OLCScoringServerPath="http://".$_SERVER['SERVER_NAME'].getRelMainDir()."/server/scoreOLC.php";
$OLCScoringServerPassword="mypasswd";


$CONF['paths_versions'][2]['tmpigc']='data/tmp';
$CONF['paths_versions'][2]['config']['pathsVersion']=2;

// the rss map thumbs
$CONF['paths_versions'][2]['map_thumbs']='data/cache/map_thumbs';

// the pilot dir
$CONF['paths_versions'][2]['pilot']	='data/pilots/%PILOTID%';
// the main IGC file
$CONF['paths_versions'][2]['igc']	='data/flights/tracks/%YEAR%/%PILOTID%';
// photo filenames
$CONF['paths_versions'][2]['photos']='data/flights/photos/%YEAR%/%PILOTID%';
// The rest can be ommited from Backup!!!
// *.jpg
$CONF['paths_versions'][2]['map']	='data/flights/maps/%YEAR%/%PILOTID%';
// *.png 16 files / flight
$CONF['paths_versions'][2]['charts']='data/flights/charts/%YEAR%/%PILOTID%';
// *.kmz
// *.man.kmz
// *.igc2kmz.[version].kmz
$CONF['paths_versions'][2]['kml']	='data/flights/kml/%YEAR%/%PILOTID%';
// *.json.js
$CONF['paths_versions'][2]['js']	='data/flights/js/%YEAR%/%PILOTID%';
// *.1.txt
// *.poly.txt
// *.saned.full.igc
// *.saned.igc
$CONF['paths_versions'][2]['intermediate']	='data/flights/intermediate/%YEAR%/%PILOTID%';


// the paths for pathsVersion=1
$CONF['paths_versions'][1]['tmpigc']='files/tmp';
// the rss map thumbs
$CONF['paths_versions'][1]['map_thumbs']='tmp_map_thumbs';

$CONF['paths_versions'][1]['config']['pathsVersion']=1;
// the pilot dir
$CONF['paths_versions'][1]['pilot']	='flights/%PILOTID%';
// the main IGC file
$CONF['paths_versions'][1]['igc']	='flights/%PILOTID%/flights/%YEAR%';
// photo filenames
$CONF['paths_versions'][1]['photos']='flights/%PILOTID%/photos/%YEAR%';
// *.jpg
$CONF['paths_versions'][1]['map']	='flights/%PILOTID%/maps/%YEAR%';
// *.png 16 files / flight
$CONF['paths_versions'][1]['charts']='flights/%PILOTID%/charts/%YEAR%';
// *.kmz
// *.man.kmz
// *.igc2kmz.[version].kmz
$CONF['paths_versions'][1]['kml']	='flights/%PILOTID%/flights/%YEAR%';
// *.json.js
$CONF['paths_versions'][1]['js']	='flights/%PILOTID%/flights/%YEAR%';
// *.1.txt
// *.poly.txt
// *.saned.full.igc
// *.saned.igc
$CONF['paths_versions'][1]['intermediate']	='flights/%PILOTID%/flights/%YEAR%';


$CONF['paths']=$CONF['paths_versions'][1];

$CONF['userPrefs']['defaults']=array(
	'showNews'=>1,
);


//---------------------------------------------------
// user profile options
//---------------------------------------------------

// the way to fill in the CIVL ID of the pilot
$CONF['profile']['CIVL_ID']['enter_url']='';
// example  for custom method : 
// $CONF['profile']['CIVL_ID_enter_url']=
// 			getRelMainDir()."/site/CIVL_ID_enter.php?id=check_membership&callingfield=CIVL_ID";
$CONF['profile']['CIVL_ID']['window_width']=700;
$CONF['profile']['CIVL_ID']['window_height']=550;

$CONF['takeoffAreas']['defaultCountry']='';

$CONF['db_browser']['default_area']=1;
$CONF['db_browser']['areas']=array(
	1=>array(
		'name'=>"Polska",
		'queryString'=>"",
		'min_lat'=>13,
		'max_lat'=>24,
		'min_lon'=>48,
		'max_lon'=>54,
		'radius'=>400,
	),
	2=>array(
		'name'=>"Europe",
		'queryString'=>"",
		'min_lat'=>35,
		'max_lat'=>64,
		'min_lon'=>-11,
		'max_lon'=>30.5,
		'radius'=>1800,
	),

	3=>array(
		'name'=>"Alps",
		'queryString'=>"",
		'min_lat'=>44,
		'max_lat'=>47,
		'min_lon'=>6,
		'max_lon'=>14,
		'radius'=>350,
	),
	
	4=>array(
		'name'=>"Germany",
		'queryString'=>"",
		'min_lat'=>47,
		'max_lat'=>55,
		'min_lon'=>4.5,
		'max_lon'=>15,
		'radius'=>400,
	),
);



define("LEONARDO_ABS_PATH",dirname(__FILE__));
$CONF_abs_path=LEONARDO_ABS_PATH;

// news
$CONF['news']['items']=array();
@include_once dirname(__FILE__)."/site/config_news.php";

// The readonly fields on profile edit page - override in site/config_custom.php
$CONF['profile']['edit']['readonlyFields']=array();
	
// dont force the users to enter a civl id
$CONF['profile']['edit']['force_civl_id']=false;


$CONF['log']['logActions']=array(
// 1 => "Flight",
2 => "Pilot",
4 => "Waypoint",
8 => "NAC / Club  / Group",
16=> "League / Event ",
32=> "Area  ",
);

$CONF['log']['logActionsType']=array(
1  => "Add",
2  => "Edit",
4  => "Delete",
8  => "Score",
16 => "Rename TrackLog",
32 => "Create Map",
);
	
$CONF['comments']['guestComments']=true;

	
// we over ride the config values with our custom ones here 
@include_once dirname(__FILE__)."/site/config_custom.php";
@include_once dirname(__FILE__)."/site/config_ranks.php";
@include_once dirname(__FILE__)."/site/config_servers.php";

$CONF_tmp_path=$CONF_abs_path.'/'.$CONF['paths']['tmpigc']; // was  '/files/tmp';

// this loads predefined settings for userDB and  settings 
// to bridge to the users table of different forum/portal/cms systems
require_once dirname(__FILE__)."/site/predefined/$opMode/config.php";
setLeonardoPaths();

// time to load the user prefs from cookie
require_once dirname(__FILE__)."/CL_user_prefs.php";
$PREFS=new userPrefs();
if (! $PREFS->getFromCookie() || !$PREFS->themeName  || !$PREFS->itemsPerPage ) {
	// echo "NO user prefs cookie found : puting default values<br>";
	$PREFS->themeName= $CONF_defaultThemeName;
	$PREFS->language=$nativeLanguage;
	$PREFS->viewCat=$CONF_default_cat_view;
	$PREFS->viewCountry=0;
	$PREFS->itemsPerPage=$CONF_itemsPerPage;
	$PREFS->metricSystem=$CONF_metricSystem;

	$PREFS->nameOrder=$CONF_defaultNameOrder;
	$PREFS->googleMaps=$CONF_googleMapsShow;
	$PREFS->useEditor=$CONF['default_user_prefs']['useEditor'];
	
	
}

if ($PREFS->showNews==-1) {
	$PREFS->showNews=$CONF['userPrefs']['defaults']['showNews'];
}

if (isset($_REQUEST['updatePrefs1'])) {// submit form 	   		
	$PREFS->themeName= $_POST['PREFS_themeName'];
	$PREFS->itemsPerPage=$_POST['PREFS_itemsPerPage']+0;
	$PREFS->metricSystem=$_POST['PREFS_metricSystem']+0;
	$PREFS->googleMaps=$_POST['PREFS_googleMaps']+0;
	$PREFS->nameOrder=$_POST['PREFS_nameOrder']+0;
	$PREFS->useEditor=$_POST['PREFS_useEditor']+0;
	
	$PREFS->language=$_POST['PREFS_language'];
	$_SESSION["lng"]= $PREFS->language;
	$PREFS->viewCat=$_POST['PREFS_viewCat'];
	$_SESSION["cat"]= $PREFS->viewCat;
	$PREFS->viewCountry=$_POST['PREFS_viewCountry'];
	$_SESSION["country"]= $PREFS->viewCountry;
}

if ( !is_dir(dirname(__FILE__)."/templates/".$PREFS->themeName) || !$PREFS->themeName )
	$PREFS->themeName=$CONF_defaultThemeName;

$PREFS->setToCookie();

$themeRelPath=$moduleRelPath."/templates/".$PREFS->themeName;
$themeAbsPath=dirname(__FILE__)."/templates/".$PREFS->themeName;


	function getRelMainFileName() {
		global $baseInstallationPath, $CONF_mainfile ;		
		return  str_replace('//','/',"/$baseInstallationPath/$CONF_mainfile".CONF_MODULE_ARG);
	}

	function getArgList($str) {
		global  $module_name, $CONF_arg_name;		
		return "?$CONF_arg_name=$module_name$str";	
	}
	
	function getRelMainDir($noLeadingSlash=0,$noTrailingSlash=0) {
		global $baseInstallationPath, $moduleRelPath, $CONF;
		
		// / + modules/leonardo/
		// / + components/com_leonardo/
		// /leonardo + /./
		if ( $CONF['links']['type']==3 ) {
			return $CONF['links']['baseURL'].'/';
		}

		if ( $noLeadingSlash) 
			return str_replace('//','/',"$baseInstallationPath/$moduleRelPath/");
		else 
			return  str_replace('//','/',"/$baseInstallationPath/$moduleRelPath/");
	}

function setLeonardoPaths () {
	global	$baseInstallationPath,$baseInstallationPathSet;
	global 	$module_name,$moduleAbsPath,$moduleRelPath;
	global 	$waypointsRelPath,	$waypointsAbsPath,	$waypointsWebPath;
	global 	$flightsRelPath; 
	global  $CONF_arg_name,$CONF_mainfile,$CONF,$opMode;
	global 	$isExternalFile;


	if (!isset($module_name))  {
		if (isset($_GET[$CONF_arg_name])) $module_name=makeSane($_GET[$CONF_arg_name]);
		else {
			$tmpDir=dirname(__FILE__);
			$tmpParts=explode("/",str_replace("\\","/",$tmpDir));
			$module_name=$tmpParts[count($tmpParts)-1]; 
			// $module_name="leonardo";
		}
	}
	
	$moduleRelPath=moduleRelPath($isExternalFile);
	// if ($opMode==3 || $opMode==4) $moduleRelPath="./";



	// detect if the installation in not on the root
	$baseInstallationPath="";
	$moduleRelPathTemp=moduleRelPath(!$isExternalFile);		

	// compute baseInstallationPath
	$parts=explode("/", str_replace($moduleRelPathTemp,'',dirname($_SERVER['SCRIPT_NAME']) )   );	
	// print_r($parts);	
	if ( count($parts)>1 )  {
		for($i=0;$i<count($parts);$i++) 
		   if ($parts[$i]!='') $baseInstallationPath.="/".$parts[$i];	
	}

	if (!defined('CONF_MODULE_ARG') ){
		if ($CONF['links']['type']==3 && $opMode!=2){		
			$preDir=$CONF['links']['baseURL'];
		} else {
			$preDir=$baseInstallationPath;
		}
		$lnk=$preDir.'/'.$CONF_mainfile."?$CONF_arg_name=$module_name";				
		define('CONF_MODULE_ARG',$lnk);
		//	setModuleArg();
	}

	if ( $CONF['links']['type']==3 ) {
		$baseInstallationPath="";
	}
		
if (0) {
	echo "@".substr($_SERVER['SCRIPT_NAME'],0,-$queryLen)."@";
	echo "queryLen : $queryLen#";
	echo "&& _SERVER['SCRIPT_NAME'] : ".$_SERVER['SCRIPT_NAME']."&&";
	echo dirname($_SERVER['SCRIPT_NAME']);
	echo "#moduleRelPath=$moduleRelPath#moduleRelPathTemp=$moduleRelPathTemp#";
	echo "baseInstallationPath=$baseInstallationPath#<BR>";
	echo "CONF_MODULE_ARG=".CONF_MODULE_ARG."<BR>";
}

	$moduleAbsPath=dirname(__FILE__);	
	
	$flightsRelPath="flights";
	$waypointsRelPath="waypoints";

	$waypointsAbsPath=dirname(__FILE__)."/".$waypointsRelPath;
	$waypointsWebPath=$moduleRelPath."/".$waypointsRelPath;

	$flightsAbsPath=dirname(__FILE__)."/".$flightsRelPath;
	// $flightsWebPath=$moduleRelPath."/".$flightsRelPath;
	

}

define('SYNC_INSERT_FLIGHT_LINK',1);
define('SYNC_INSERT_FLIGHT_LOCAL',2);

define('SYNC_INSERT_FLIGHT_REPROCESS_LOCALLY',64);

define('SYNC_INSERT_PILOT_LINK',4);
define('SYNC_INSERT_PILOT_LOCAL',8); 

define('SYNC_INSERT_WAYPOINT_LINK',16);
define('SYNC_INSERT_WAYPOINT_LOCAL',32);


if (!function_exists('str_ireplace')) {
    function str_ireplace($needle, $str, $haystack) {
        $needle = preg_quote($needle, '/');
        return preg_replace("/$needle/i", $str, $haystack);
    }
} 

//$CONF['sprites']

// list of takeoffs that will make all flights from them PRIVATE!
$CONF['takeoffs']['private']=array() ;

if ($db) {
	$db->sql_query("set sql_mode = ''");
}

//$cdnURL='https://files.leonardo.pgxc.pl';
//$CONF['cdnURL']='https://files.leonardo.pgxc.pl';
$cdnURL='https://leonardo.pgxc.pl';
$CONF['cdnURL']='https://leonardo.pgxc.pl';
$CONF['protocol']='https';
$CONF['menu']['googleearth']=0; //0 hide google earth from menu
$CONF['menu']['googlemaps']=1; //0 hide show in google maps from menu
$CONF['takeoffmap']['dateselect']=0; //0 hide dateselect on EXT_google_maps_browser.php
$CONF['timeToDelete']=14; //time for users to delete flight

$CONF['links']['type']=3;
$CONF['links']['baseURL']=$CONF['protocol'].'://'.'leonardo.pgxc.pl';

$CONF['mapIconsDir'] = $CONF['paths_versions'][2]['map_thumbs'].'/waypoints';
$CONF['mapUsersDir'] = $CONF['paths_versions'][2]['map_thumbs'].'/users';
$CONF['flightMapThumbsDir'] = $CONF['paths_versions'][2]['map_thumbs'].'/flights';
//zmienne majace znaczenie w GUI_header

$board_config['meta_copyright']='https://leonardo.pgxc.pl';
$board_config['meta_distribution']='global';
$board_config['meta_category']='Sports';
$board_config['meta_revisit_after']='14 days';
$CONF_noreply_email='no-reply@pgxc.pl';
$CONF['images']['directions']=dirname(__FILE__).'/data/cache/directions';
$CONF['images']['directionsRel']='/data/cache/directions';
$CONF['weatherapi']='http://weather:8080';
$CONF['recaptcha_site_key']=  rtrim(file_get_contents("/run/secrets/recaptcha_site_key")); 
$CONF['recaptcha_secret_key']=  rtrim(file_get_contents("/run/secrets/recaptcha_secret_key"));
$thisSeason=2025;
//20201203 until resolved
$CONF['menu']['googlemaps']=0; //0 hide show in google maps from menu

//20210129 hide airspace violations from public
$CONF_showAirspaceViolationsToPublic=0;

$CONF['googleEarth']['igc2kmz']['python'] = '/usr/bin/python2.7';
$CONF['adsFreeUsers'] = Array(1,14,13,310);
?>
<?
//************************************************************************
// 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: EXT_config.php,v 1.22 2010/03/14 20:56:10 manolis Exp $                                                                 
//
//************************************************************************

	@session_start();
	setVarFromRequest("lng",$PREFS->language); 

	$currentlang=$lng;
	$lang=$currentlang;
	$language=$currentlang;

	if ( $opMode==1) {
		$newlang=$currentlang;
		$inside_mod =1;
		define('INSIDE_MOD',1);
		require_once("../../mainfile.php");

		// re-set $module_name;
		$tmpDir=dirname(__FILE__);
		$tmpParts=explode("/",str_replace("\\","/",$tmpDir));
		$module_name=$tmpParts[count($tmpParts)-1];
	} else if ( $opMode==2) {
		// require_once "language/lang-".$currentlang.".php";
		define('IN_PHPBB', true);
		$phpbb_root_path = dirname(__FILE__).'/../../';		
		$prefix="phpbb";
		require($phpbb_root_path . 'extension.inc');
		require($phpbb_root_path . 'common.'.$phpEx);
	}


	if ( strlen($currentlang)==2) { 
		$currentlang=array_search($currentlang,$lang2iso);
		if (!$currentlang) $currentlang=$PREFS->language;
	}


	if ($CONF_use_utf) {
		define('CONF_LANG_ENCODING_TYPE','utf8');
		$CONF_ENCODING='utf-8';
	} else  {
		define('CONF_LANG_ENCODING_TYPE','iso');
		$CONF_ENCODING=$langEncodings[$currentlang];
	}
	if (is_array($lang) )
		$lang['ENCODING']=$CONF_ENCODING;

	require_once dirname(__FILE__)."/language/".CONF_LANG_ENCODING_TYPE."/lang-".$currentlang.".php";

	session_start();
	$userID = $_SESSION['userID'];

	if ($CONF_use_utf) $db->sql_query("SET NAMES utf8");
?><?
//************************************************************************
// 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: CL_flightData.php,v 1.196 2012/09/17 22:33:48 manolis Exp $
//
//************************************************************************

require_once dirname(__FILE__)."/CL_gpsPoint.php";
require_once dirname(__FILE__)."/CL_flightPhotos.php";
require_once dirname(__FILE__).'/CL_flightScore.php';
require_once dirname(__FILE__).'/CL_comments.php';
require_once dirname(__FILE__)."/CL_brands.php";
require_once dirname(__FILE__)."/CL_dates.php";
require_once dirname(__FILE__)."/FN_pilot.php";

class flight {
	var $cat=1;
	var $subcat=1;
	var $category=2; // 1=sport , 2=open ,3=tandem
	var $active;
	var $timesViewed;
	var $dateAdded;

	var $flightID;
	var $private=0;
    var $filename;
    var $userID;
	var $userName="";

    var $comments="";
    var $commentsNum=0;
    var $glider="";
	var $gliderBrandID=0;

	var $startType=0;
	
	var $linkURL="";
	var $takeoffID=0;
	var $takeoffVinicity=0;
	var $landingID=0;
	var $landingVinicity=0;

	var $forceBounds=0;

	var $validated=0;
	var $grecord=0;
	var $validationMessage="";
	var $NACclubID=0;
	/// Martin Jursa, 17.05.2007
	var $NACid=0;

	var $airspaceCheck=0; // not yet processed
	var $airspaceCheckFinal=0; // not yet processed
	var $airspaceCheckMsg="";
	var $checkedBy="";

	var $hash='';

	var $serverID=0;
	var $excludeFrom=0;

	var $originalURL='';
	var $originalKML='';

	var $original_ID=0;

	var $originalUserID =0;
	var $userServerID=0;

var $timezone=0;

var $DATE;
var $MAX_SPEED =0;
var $MEAN_SPEED=0 ;
var $MAX_ALT =0;
var $MIN_ALT= 0 ;
var $TAKEOFF_ALT=0;
var $LANDING_ALT=0;
var $MAX_VARIO=0 ;
var $MIN_VARIO=0 ;
var $LINEAR_DISTANCE=0 ;

var $START_TIME=0;
var $END_TIME=0;
var $DURATION=0;

//var $MAX_LINEAR_DISTANCE=0 ;
//var $BEST_FLIGHT_TYPE="";
//var $FLIGHT_KM=0;
//var $FLIGHT_POINTS=0;

//var $FIRST_POINT="";
//var $LAST_POINT="";

var $turnpoint1="";
var $turnpoint2="";
var $turnpoint3="";
var $turnpoint4="";
var $turnpoint5="";

var $autoScore=0;

var $olcRefNum="";
var $olcFilename="";
var $olcDateSubmited;

var $flightScore;
var $flightComments;
var $commentsEnabled=1;

/*
#externalFlightType
#	0 -> local flight
#	1- > SYNC_INSERT_FLIGHT_LINK (extllink  linked to external disply , only scroing info into DB)
#	2 -> SYNC_INSERT_FLIGHT_LOCAL (extflight IGC imported locally)
#isLive
#	0 -> not a live flight
#	1 -> in progress liveflight in progress, only basic info into db and link to actual display/server
#	2 -> finalized , liveflight finilized IGC imported locally
#
#forceBounds
#	1-> auto detect start/end
#	0-> set by user /admin
*/
var	$externalFlightType=0;
var $isLive=0;
var $firstPointTM=0;
var $firstLat=0;
var $firstLon=0;
var	$lastPointTM=0;
var $lastLat=0;
var $lastLon=0;

	var $hasPhotos=0;
	var $photos=array();

// CONSTANTS
var	$maxAllowedSpeed=100;
var	$maxAllowedVario=13;
var $maxAllowedHeight=9000;

var $max_allowed_time_gap=1800; //  30 mins

var $maxPointNum=1000;
//---------------
// CONSTRUCTOR
    function flight() {
		global $CONF;
		$this->startType=$CONF['defaultStartType'];
		
		$this->commentsEnabled=1;
		
    	# Martin Jursa, 30.05.2007: make time gap configurable
		global $CONF_max_allowed_time_gap;
		if (!empty($CONF_max_allowed_time_gap)) {
			$this->max_allowed_time_gap=$CONF_max_allowed_time_gap;
		}
    }

	function belongsToUser($userID) {
		global $CONF_server_id ;
		if ( 	$this->userID == $userID &&
				($this->userServerID==$CONF_server_id || $this->userServerID==0 )
			) return 1;
		else return 0;

	}

	function checkGliderBrand($gliderBrand='') {

		if (! $this->gliderBrandID ) {

			if ($gliderBrand) {
				$gliderBrandID=brands::guessBrandID($gliderBrand );
			}	else {
				$gliderBrandID=brands::guessBrandID($this->glider );
			}

			if ($gliderBrandID){
				global $CONF;
				if (!$gliderBrand) {
					if (!function_exists('str_ireplace')) {
						function str_ireplace($needle, $str, $haystack) {
							$needle = preg_quote($needle, '/');
							return preg_replace("/$needle/i", $str, $haystack);
						}
					}  else {
						$gliderName=str_ireplace($CONF['brands']['list'][$gliderBrandID],'',$this->glider);
					}
				} else {
					$gliderName=$this->glider;
				}

				$gliderName=brands::sanitizeGliderName($gliderName);
				$this->glider=$gliderName;
				$this->gliderBrandID=$gliderBrandID;
			}
		}
	}

	function checkDirs($userDir='',$year=0) {
		global $CONF;
		
		if ($userDir=='' || $year==0){
			$userDir=$this->getPilotID();
			$year=$this->getYear();
		}
		
		$d=0;
		$dirs=array();
		$dirs[$d++]=str_replace("%PILOTID%","$userDir",str_replace("%YEAR%","$year",$CONF['paths']['igc']) );
		$dirs[$d++]=str_replace("%PILOTID%","$userDir",str_replace("%YEAR%","$year",$CONF['paths']['photos']) );
		$dirs[$d++]=str_replace("%PILOTID%","$userDir",str_replace("%YEAR%","$year",$CONF['paths']['map']) );
		$dirs[$d++]=str_replace("%PILOTID%","$userDir",str_replace("%YEAR%","$year",$CONF['paths']['charts']) );
		$dirs[$d++]=str_replace("%PILOTID%","$userDir",str_replace("%YEAR%","$year",$CONF['paths']['kml']) );
		$dirs[$d++]=str_replace("%PILOTID%","$userDir",str_replace("%YEAR%","$year",$CONF['paths']['js']) );
		$dirs[$d++]=str_replace("%PILOTID%","$userDir",str_replace("%YEAR%","$year",$CONF['paths']['intermediate']) );
		
		foreach($dirs as $dir) {
			$dirPath=LEONARDO_ABS_PATH.'/'.$dir;
			if (!is_dir($dirPath))  	makeDir($dirPath);
		}
	}

	function toXML($forceProtocol=''){
		/*	maybe also include these
		"forceBounds"		"autoScore"
		*/
		global $CONF_server_id,$CONF_photosPerFlight, $CONF;

		if ($CONF['sync']['protocol']['format']=='JSON' && $forceProtocol!='XML' || $forceProtocol=='JSON') {
			$useJSON=1;
			require_once dirname(__FILE__).'/lib/json/CL_json.php';
		} else $useJSON=0;

		if ($this->flightScore) {
			$flightScore=&$this->flightScore;
			if (!$flightScore->gotValues) {
				$flightScore->getFromDB();
			}
		}  else {
			$flightScore=new flightScore($this->flightID);
			$flightScore->getFromDB();
		}

		$defaultMethodID= $CONF['scoring']['default_set'];
		$scoreDetails=$flightScore->scores[$defaultMethodID][ $flightScore->bestScoreType ];

		$tpNum=0;
		$tpStr='';
		for($i=1;$i<=7;$i++) {
			if ($scoreDetails['tp'][$i]) {
				$newPoint=new gpsPoint($scoreDetails['tp'][$i]);
				if ($tpNum>0) $tpStr.=" ,\n		";
				$tpStr.=' {"id": '.$i.' , "lat": '.$newPoint->lat().', "lon": '.$newPoint->lon().', "UTCsecs": '.($newPoint->gpsTime+0).' } ';
				$tpNum++;
			}
		}


		$takeoff=new waypoint($this->takeoffID);
		$takeoff->getFromDB();

//		$firstPoint=new gpsPoint($this->FIRST_POINT,$this->timezone);
//		$lastPoint=new gpsPoint($this->LAST_POINT,$this->timezone);

		$firstPoint=new gpsPoint('',$this->timezone);
		$firstPoint->setLat($this->firstLat);
		$firstPoint->setLon($this->firstLon);
		$firstPoint->gpsTime=$this->firstPointTM;

		$lastPoint=new gpsPoint('',$this->timezone);
		$lastPoint->setLat($this->lastLat);
		$lastPoint->setLon($this->lastLon);
		$lastPoint->gpsTime=$this->lastPointTM;

		list($lastName,$firstName,$pilotCountry,$Sex,$Birthdate,$CIVL_ID)=getPilotInfo($this->userID,$this->userServerID);

		$userServerID=$this->userServerID;
		if ($userServerID==0) $userServerID=$CONF_server_id;

		// will be changed when dateAdded will be UTC
		$dateAdded=$this->dateAdded;

		// We changed this, dateAdded is in UTC from now on !!!!
		// So the conversion is not needed
		//$dateAdded=tm2fulldate(fulldate2tm($dateAdded)-date('Z')); // convert to UTC


		if (  $this->serverID==0 || $this->serverID==$CONF_server_id ) $isLocal=1;
		else $isLocal=0;

		if (!$useJSON) {
		$resStr=
"<flight>
<serverID>$CONF_server_id</serverID>
<id>$this->flightID</id>
<dateAdded>$dateAdded</dateAdded>
<filename>$this->filename</filename>
<linkIGC>".$this->getIGC_URL()."</linkIGC>
<linkIGCzip>".$this->getZippedIGC_URL()."</linkIGCzip>
<linkDisplay>".htmlspecialchars($this->getFlightLinkURL())."</linkDisplay>
<linkGE>".htmlspecialchars($this->getFlightKML(0))."</linkGE>

<info>
	<glider>$this->glider</glider>
	<gliderBrandID>$this->gliderBrandID</gliderBrandID>
	<gliderCat>$this->cat</gliderCat>
	<cat>$this->category</cat>
	<linkURL>$this->linkURL</linkURL>
	<private>$this->private</private>
	<comments>$this->comments</comments>
</info>

<time>
	<date>$this->DATE</date>
	<Timezone>$this->timezone</Timezone>
	<StartTime>$this->START_TIME</StartTime>
	<Duration>$this->DURATION</Duration>
</time>

<pilot>
	<userID>$this->userID</userID>
	<serverID>$userServerID</serverID>
	<civlID>$CIVL_ID</civlID>
	<userName>$this->userName</userName>
	<pilotFirstName>$firstName</pilotFirstName>
	<pilotLastName>$lastName</pilotLastName>
	<pilotCountry>$pilotCountry</pilotCountry>
	<pilotBirthdate>$Birthdate</pilotBirthdate>
	<pilotSex>$Sex</pilotSex>
</pilot>

<location>
	<firstLat>$firstPoint->lat</firstLat>
	<firstLon>".$firstPoint->lon()."</firstLon>
	<takeoffID>$this->takeoffID</takeoffID>
	<serverID>$CONF_server_id</serverID>
	<takeoffVinicity>$this->takeoffVinicity</takeoffVinicity>
	<takeoffName>$takeoff->name</takeoffName>
	<takeoffNameInt>$takeoff->intName</takeoffNameInt>
	<takeoffCountry>$takeoff->countryCode</takeoffCountry>
	<takeoffLocation>$takeoff->location</takeoffLocation>
	<takeoffLocationInt>$takeoff->intlocation</takeoffLocationInt>
	<takeoffLat>$takeoff->lat</takeoffLat>
	<takeoffLon>".$takeoff->lon()."</takeoffLon>
</location>

<stats>
	<FlightType>$this->BEST_FLIGHT_TYPE</FlightType>
	<StraightDistance>$this->MAX_LINEAR_DISTANCE</StraightDistance>
	<XCdistance>$this->FLIGHT_KM</XCdistance>
	<XCscore>$this->FLIGHT_POINTS</XCscore>
	<MaxSpeed>$this->MAX_SPEED</MaxSpeed>
	<MaxVario>$this->MAX_VARIO</MaxVario>
	<MinVario>$this->MIN_VARIO</MinVario>
	<MaxAltASL>$this->MAX_ALT</MaxAltASL>
	<MinAltASL>$this->MIN_ALT</MinAltASL>
	<TakeoffAlt>$this->TAKEOFF_ALT</TakeoffAlt>
</stats>

<validation>
	<validated>$this->validated</validated>
	<grecord>$this->grecord</grecord>
	<hash>$this->hash</hash>
	<validationMessage>$this->validationMessage</validationMessage>
	<airspaceCheck>$this->airspaceCheck</airspaceCheck>
	<airspaceCheckFinal>$this->airspaceCheckFinal</airspaceCheckFinal>
	<airspaceCheckMsg>$this->airspaceCheckMsg</airspaceCheckMsg>
</validation>


</flight>\n";
		} else {
$resStr='{
"flight": {
	"serverID": '. ( $this->serverID?$this->serverID:$CONF_server_id).',
	"id": '.($isLocal ? $this->flightID : $this->original_ID  ).',
	"dateAdded": "'.$dateAdded.'",
	"filename": "'.json::prepStr($this->filename).'",
	"linkIGC": "'.json::prepStr($this->getIGC_URL()).'",
	"linkIGCzip": "'.json::prepStr($this->getZippedIGC_URL()).'",
	"linkDisplay": "'.($isLocal ? 	json::prepStr($this->getFlightLinkURL()):
									json::prepStr($this->getOriginalURL()) ).'",
	"linkGE": "'.($isLocal ? json::prepStr($this->getFlightKML(0)) :
							 json::prepStr($this->getOriginalKML()) ).'",
	"isLive": '.$this->isLive.',

	"info": {
		"glider": "'.json::prepStr($this->glider).'",
		"gliderBrandID": '.$this->gliderBrandID.',
		"gliderBrand": "'.json::prepStr($CONF['brands']['list'][$this->gliderBrandID]).'",
		"gliderCat": '.$this->cat.',
		"cat": '.$this->category.',
		"linkURL": "'.json::prepStr($this->linkURL).'",
		"private": '.$this->private.',
		"comments": "'.json::prepStr($this->comments).'"
	},

	"time": {
		"date": "'.json::prepStr($this->DATE).'",
		"Timezone": "'.$this->timezone.'",
		"StartTime": "'.$this->START_TIME.'",
		"Duration": "'.$this->DURATION.'"
	},

	"bounds": {
		"forceBounds": '.$this->forceBounds.',
		"firstLat": '.$firstPoint->lat().',
		"firstLon": '.$firstPoint->lon().',
		"firstTM": '.($firstPoint->gpsTime+0).',
		"lastLat": '.$lastPoint->lat().',
		"lastLon": '.$lastPoint->lon().',
		"lastTM": '.($lastPoint->gpsTime+0).'
	},

	"pilot": {
		"userID": "'.$this->userID.'",
		"serverID": "'.$userServerID.'",
		"civlID": "'.$CIVL_ID.'",
		"userName": "'.json::prepStr($lastName.' '.$firstName).'",
		"pilotFirstName": "'.json::prepStr($firstName).'",
		"pilotLastName": "'.json::prepStr($lastName).'",
		"pilotCountry": "'.$pilotCountry.'",
		"pilotBirthdate": "'.json::prepStr($Birthdate).'",
		"pilotSex": "'.$Sex.'"
	},

	"location": {
		"takeoffID": "'.($this->takeoffID+0).'",
		"serverID": "'.$CONF_server_id.'",
		"takeoffVinicity": "'.$this->takeoffVinicity.'",
		"takeoffName": "'.json::prepStr($takeoff->name).'",
		"takeoffNameInt": "'.json::prepStr($takeoff->intName).'",
		"takeoffCountry": "'.$takeoff->countryCode.'",
		"takeoffLocation": "'.json::prepStr($takeoff->location).'",
		"takeoffLocationInt": "'.json::prepStr($takeoff->intlocation).'",
		"takeoffLat": "'.$takeoff->lat().'",
		"takeoffLon": "'.$takeoff->lon().'"
	},

	"stats":  {
		"FlightType": "'.$this->BEST_FLIGHT_TYPE.'",
		"MaxStraightDistance": '.($this->MAX_LINEAR_DISTANCE+0).',
		"StraightDistance": '.$this->LINEAR_DISTANCE.',
		"XCdistance": "'.($this->FLIGHT_KM+0).'",
		"XCscore": "'.($this->FLIGHT_POINTS+0).'",

		"MaxSpeed": "'.$this->MAX_SPEED.'",
		"MeanGliderSpeed": "'.$this->MEAN_SPEED.'",
		"MaxVario": "'.$this->MAX_VARIO.'",
		"MinVario": "'.$this->MIN_VARIO.'",
		"MaxAltASL": "'.$this->MAX_ALT.'",
		"MinAltASL": "'.$this->MIN_ALT.'",
		"TakeoffAlt": "'.$this->TAKEOFF_ALT.'"
	},

	'.$flightScore->toSyncJSON().' ,


	"turnpoints": [
		'.$tpStr.'
	] ,

	"validation": {
		"validated": "'.$this->validated.'",
		"grecord": "'.$this->grecord.'",
		"hash": "'.$this->hash.'",
		"validationMessage": "'.json::prepStr($this->validationMessage).'",
		"airspaceCheck": "'.json::prepStr($this->airspaceCheck).'",
		"airspaceCheckFinal": "'.json::prepStr($this->airspaceCheckFinal).'",
		"airspaceCheckMsg": "'.json::prepStr($this->airspaceCheckMsg).'"
	}


}
}';
		}
		return $resStr;
	}

	function setAllowedParams() {
		if ($this->cat==1 ) { // PG
			$this->maxAllowedSpeed=140;
			$this->maxAllowedVario=13;
			$this->maxAllowedHeight=9000;
		} else	if ($this->cat==2 ) { // flex wing HG
			$this->maxAllowedSpeed=200;
			$this->maxAllowedVario=15;
			$this->maxAllowedHeight=9000;
		} else	if ($this->cat==4 ) { // fixed wing HG
			$this->maxAllowedSpeed=200;
			$this->maxAllowedVario=17;
			$this->maxAllowedHeight=9000;
		} else	if ($this->cat==8 ) { // glider
			$this->maxAllowedSpeed=350;
			$this->maxAllowedVario=20;
			$this->maxAllowedHeight=9000;
		} else if ($this->cat==16 ) { // paramotor
			$this->maxAllowedSpeed=150;
			$this->maxAllowedVario=13;
			$this->maxAllowedHeight=9000;
		} else if ($this->cat==32 ) { // trike
			$this->maxAllowedSpeed=150;
			$this->maxAllowedVario=13;
			$this->maxAllowedHeight=9000;
		} else if ($this->cat==64 ) { // other powered flight
			$this->maxAllowedSpeed=500;
			$this->maxAllowedVario=20;
			$this->maxAllowedHeight=15000;
		}

	}

    function resetData() {
		$this->DATE="";
		$this->MAX_SPEED =0;
		$this->MEAN_SPEED=0 ;
		$this->MAX_ALT =0;
		$this->MIN_ALT= 0 ;
		$this->TAKEOFF_ALT=0;
		$this->LANDING_ALT=0;
		$this->MAX_VARIO=0 ;
		$this->MIN_VARIO=0 ;
		$this->LINEAR_DISTANCE=0 ;
		//$this->MAX_LINEAR_DISTANCE=0 ;
		$this->START_TIME=0;
		$this->END_TIME=0;
		$this->DURATION=0;
		//$this->BEST_FLIGHT_TYPE="";
		//$this->FLIGHT_KM=0;
		//$this->FLIGHT_POINTS=0;

		//$this->FIRST_POINT="";
		//$this->LAST_POINT="";

		$this->firstPointTM=0;
		$this->firstLat=0;
		$this->firstLat=0;
		$this->firstLon=0;
		$this->lastPointTM=0;
		$this->lastLat=0;
		$this->lastLat=0;

	}

	function checkBound($time){
		if ($time >= $this->START_TIME &&  $time <= $this->END_TIME ) return 1;
		return 0;
	}

	function is3D() {
		if ( $this->MAX_ALT ==0 && $this->MIN_ALT == 0 ) return false;
		else return true;
	}

    function getTakeoffName() {
  	    return	getWaypointName($this->takeoffID);
    }

    function getLandingName() {
  	    return	getWaypointName($this->landingID);
    }

	function getYear() {
		return substr($this->DATE,0,4);
	}

	function getOLCYear() {
		$m=$this->getMonth();
		$d=$this->getDay();
		$y=$this->getYear();

		if ( ($m==10 && $d >=12) || $m>10 ) return $y+1;
		else return $y;
	}

	function getMonth() {
		return substr($this->DATE,5,2);
	}
	function getDay() {
		return substr($this->DATE,8,2);
	}

	function getMapWindowSize() {
		$dist=max($this->LINEAR_DISTANCE ,$this->MAX_LINEAR_DISTANCE );
		if ($dist > 50 ) $mult=1.5;
		else $mult=1.7;
	    $window_size=($dist * $mult)/1000;
	    if ( $window_size < 20 ) $window_size=20;
	    return $window_size;
	}


	function getPilotID() {
		if ($this->userServerID) $extra_prefix=$this->userServerID.'_';
		else $extra_prefix='';
		return $extra_prefix.$this->userID;
	}

	//---------------------------------
	// external flights

	function getOriginalKML() {
		if ( $this->serverID  != 0 && ! $this->originalURL) {
			global $CONF;
			if ( $CONF['servers']['list'][$this->serverID]['isLeo'] ==1  ) {
			 	if (! $this->originalKML )  {
					$this->originalKML='http://'.$CONF['servers']['list'][$this->serverID]['url_base'].
										'/download.php?type=kml_trk&flightID='.$this->original_ID;
				}

			}
		}
		return $this->originalKML;
	}

	function getOriginalURL() {
		if ( $this->serverID  != 0 && ! $this->originalURL) {
			global $CONF;
			if ( $CONF['servers']['list'][$this->serverID]['isLeo'] ==1  ) {
			 	if (! $this->originalURL )  {
					$url_flight=$CONF['servers']['list'][$this->serverID]['url_flight'];
					if ($url_flight) {
						$this->originalURL='http://'.str_replace("%FLIGHT_ID%",$this->original_ID,$url_flight);
					} else {
						$this->originalURL='http://'.$CONF['servers']['list'][$this->serverID]['url'].
										'&op=show_flight&flightID='.$this->original_ID;
					}					
				}
			}
		}
		return $this->originalURL;
	}

	// ---------------------------------
	// Relative (web) paths
	// ---------------------------------
	/* 
	 // not used
	 function getPilotRelDir() {		
		global $flightsWebPath;
		if ($this->userServerID) $extra_prefix=$this->userServerID.'_';
		else $extra_prefix='';

		return $flightsWebPath."/".$extra_prefix.$this->userID;
	}
	*/
	
	function getIGCRelPath($saned=0) {
		global $moduleRelPath,$CONF;
		if ($saned) {
			if ($saned==2) $suffix=".saned.full.igc";
			else $suffix=".saned.igc";
			
			return $moduleRelPath.'/'.str_replace("%PILOTID%",$this->getPilotID(),str_replace("%YEAR%",$this->getYear(),$CONF['paths']['intermediate']) ).'/'.
				rawurlencode($this->filename).$suffix;
		} else { 
			$suffix="";
			return $moduleRelPath.'/'.str_replace("%PILOTID%",$this->getPilotID(),str_replace("%YEAR%",$this->getYear(),$CONF['paths']['igc']) ).'/'.
				rawurlencode($this->filename).$suffix;
		}
				
		// return $this->getPilotRelDir()."/flights/".$this->getYear()."/".rawurlencode($this->filename).$suffix;
	}

	function getKMLRelPath($method=0) {
		global $moduleRelPath,$CONF;
						
		if ($method==0) $suffix=".kmz";
		else if ($method==1) $suffix=".man.kmz";
		else if ($method==2) $suffix=".igc2kmz";
		
		return $moduleRelPath.'/'.str_replace("%PILOTID%",$this->getPilotID(),str_replace("%YEAR%",$this->getYear(),$CONF['paths']['kml']) ).'/'.
				rawurlencode($this->filename).$suffix;		
		//return $this->getPilotRelDir()."/flights/".$this->getYear()."/".rawurlencode($this->filename).".kml";
	}

	function getGPXRelPath() {
		global $moduleRelPath,$CONF;
		return $moduleRelPath.'/'.str_replace("%PILOTID%",$this->getPilotID(),str_replace("%YEAR%",$this->getYear(),$CONF['paths']['kml']) ).'/'.
				rawurlencode($this->filename).".xml";
		//return $this->getPilotRelDir()."/flights/".$this->getYear()."/".rawurlencode($this->filename).".xml";
	}
	function getPolylineRelPath() {
		global $moduleRelPath,$CONF;
		return $moduleRelPath.'/'.str_replace("%PILOTID%",$this->getPilotID(),str_replace("%YEAR%",$this->getYear(),$CONF['paths']['intermediate']) ).'/'.
				rawurlencode($this->filename).".poly.txt";
		// return $this->getPilotRelDir()."/flights/".$this->getYear()."/".rawurlencode($this->filename).".poly.txt";
	}

	function getMapRelPath($num=0) {
		global $moduleRelPath,$CONF;
		if ($num) $suffix="3d";
		else $suffix="";
		return $moduleRelPath.'/'.str_replace("%PILOTID%",$this->getPilotID(),str_replace("%YEAR%",$this->getYear(),$CONF['paths']['map']) ).'/'.
				rawurlencode($this->filename).$suffix.".jpg";
		//return $this->getPilotRelDir()."/maps/".$this->getYear()."/".rawurlencode($this->filename).$suffix.".jpg";
	}

	function getChartRelPath($chartType,$unitSystem=1,$rawChart=0) {
		global $moduleRelPath,$CONF;
		if ($unitSystem==2) $suffix="_2";
		else $suffix="";
		if ($rawChart) $suffix.=".raw";
		else $suffix.="";
				
		return $moduleRelPath.'/'.str_replace("%PILOTID%",$this->getPilotID(),str_replace("%YEAR%",$this->getYear(),$CONF['paths']['charts']) ).'/'.
				rawurlencode($this->filename).".".$chartType.$suffix.".png";				
		// return $this->getPilotRelDir()."/charts/".$this->getYear()."/".rawurlencode($this->filename).".".$chartType.$suffix.".png";
	}
	function getPhotoRelPath($photoNum) {
		global $moduleRelPath,$CONF;
		$t="photo".$photoNum."Filename";
		return $moduleRelPath.'/'.str_replace("%PILOTID%",$this->getPilotID(),str_replace("%YEAR%",$this->getYear(),$CONF['paths']['photos']) ).'/'.
				rawurlencode($this->filename).".".$chartType.$suffix.".png";				
		// return $this->getPilotRelDir()."/photos/".$this->getYear()."/".$this->$t;
	}
	function getRelPointsFilename($timeStep=0) { // values > 0 mean 1-> first level of timestep, usually 20 secs, 2-> less details usually 30-40 secs
		global $moduleRelPath,$CONF;
		if ($timeStep) $suffix=".".$timeStep;
		else $suffix="";
		return $moduleRelPath.'/'.str_replace("%PILOTID%",$this->getPilotID(),str_replace("%YEAR%",$this->getYear(),$CONF['paths']['intermediate']) ).'/'.
				rawurlencode($this->filename)."$suffix.txt";
		// return  $this->getPilotRelDir()."/flights/".$this->getYear()."/".$this->filename."$suffix.txt";
	}
	function getJsRelPath($timeStep=0) { // values > 0 mean 1-> first level of timestep, usually 20 secs, 2-> less details usually 30-40 secs
		global $moduleRelPath,$CONF;
		if ($timeStep) $suffix=".".$timeStep;
		else $suffix="";
		return $moduleRelPath.'/'.str_replace("%PILOTID%",$this->getPilotID(),str_replace("%YEAR%",$this->getYear(),$CONF['paths']['js']) ).'/'.
				rawurlencode($this->filename)."$suffix.js";
		//return $this->getPilotRelDir()."/flights/".$this->getYear()."/".$this->filename."$suffix.js";
	}

	function getJsonRelPath() { // values > 0 mean 1-> first level of timestep, usually 20 secs, 2-> less details usually 30-40 secs
		global $moduleRelPath,$CONF;
		return $moduleRelPath.'/'.str_replace("%PILOTID%",$this->getPilotID(),str_replace("%YEAR%",$this->getYear(),$CONF['paths']['js']) ).'/'.
				rawurlencode($this->filename).".json.js";
				
		// return str_replace('.//','',$this->getPilotRelDir())."/flights/".$this->getYear()."/".rawurlencode($this->filename).".json.js";
	}
	// ---------------------------------
	// now absolute filenames
	// ---------------------------------
	function getPilotAbsDir() {
		global $CONF;		
		return LEONARDO_ABS_PATH.'/'.str_replace("%PILOTID%",$this->getPilotID(),$CONF['paths']['pilot']);			
	}
	
	function getIGCFilename($saned=0) {
		global $CONF;
		if ($saned) {
			if ($saned==2) $suffix=".saned.full.igc";
			else $suffix=".saned.igc"; // $saned==1
			
			return LEONARDO_ABS_PATH.'/'.str_replace("%PILOTID%",$this->getPilotID(),str_replace("%YEAR%",$this->getYear(),$CONF['paths']['intermediate']) ).'/'.
			$this->filename.$suffix;
		} else { 
			$suffix="";
			return LEONARDO_ABS_PATH.'/'.str_replace("%PILOTID%",$this->getPilotID(),str_replace("%YEAR%",$this->getYear(),$CONF['paths']['igc']) ).'/'.
				$this->filename.$suffix;
		}
		//return $this->getPilotAbsDir()."/flights/".$this->getYear()."/".$this->filename.$suffix;
	}
	
	function getKMLFilename($method=0) {
		global $CONF;
		if ($method==0) $suffix=".kmz";
		else if ($method==1) $suffix=".man.kmz";
		else if ($method==2) $suffix=".igc2kmz";
		
		return LEONARDO_ABS_PATH.'/'.str_replace("%PILOTID%",$this->getPilotID(),str_replace("%YEAR%",$this->getYear(),$CONF['paths']['kml']) ).'/'.$this->filename.$suffix;
		//return $this->getPilotAbsDir()."/flights/".$this->getYear()."/".$this->filename.".kml";
	}
	function getGPXFilename() {
		global $CONF;
		return LEONARDO_ABS_PATH.'/'.str_replace("%PILOTID%",$this->getPilotID(),str_replace("%YEAR%",$this->getYear(),$CONF['paths']['kml']) ).'/'.$this->filename.".xml";
		//return $this->getPilotAbsDir()."/flights/".$this->getYear()."/".$this->filename.".xml";
	}
	function getPolylineFilename() {
		global $CONF;
		return LEONARDO_ABS_PATH.'/'.str_replace("%PILOTID%",$this->getPilotID(),str_replace("%YEAR%",$this->getYear(),$CONF['paths']['intermediate']) ).'/'.$this->filename.".poly.txt";
		//return $this->getPilotAbsDir()."/flights/".$this->getYear()."/".$this->filename.".poly.txt";
	}
	function getMapFilename($num=0) {
		global $CONF;
		if ($num) $suffix="3d";
		else $suffix="";
		return LEONARDO_ABS_PATH.'/'.str_replace("%PILOTID%",$this->getPilotID(),str_replace("%YEAR%",$this->getYear(),$CONF['paths']['map']) ).'/'.$this->filename.$suffix.".jpg";
		//return $this->getPilotAbsDir()."/maps/".$this->getYear()."/".$this->filename.$suffix.".jpg";
	}
	function getChartFilename($chartType,$unitSystem=1,$rawChart=0) {
		global $CONF;
		if ($unitSystem==2) $suffix="_2";
		else $suffix="";

		if ($rawChart) $suffix.=".raw";
		else $suffix.="";

		return LEONARDO_ABS_PATH.'/'.str_replace("%PILOTID%",$this->getPilotID(),str_replace("%YEAR%",$this->getYear(),$CONF['paths']['charts']) ).'/'.$this->filename.".".$chartType.$suffix.".png";
		// return $this->getPilotAbsDir()."/charts/".$this->getYear()."/".$this->filename.".".$chartType.$suffix.".png";
	}
	function getPhotoFilename($photoNum) {
		global $CONF;
		$t="photo".$photoNum."Filename";
		return LEONARDO_ABS_PATH.'/'.str_replace("%PILOTID%",$this->getPilotID(),str_replace("%YEAR%",$this->getYear(),$CONF['paths']['photos']) ).'/'.$this->$t;
		// return $this->getPilotAbsDir()."/photos/".$this->getYear()."/".$this->$t;
	}
	function getPointsFilename($timeStep=0) { // values > 0 mean 1-> first level of timestep, usually 20 secs, 2-> less details usually 30-40 secs
		global $CONF;
		if ($timeStep) $suffix=".".$timeStep;
		else $suffix="";
		return LEONARDO_ABS_PATH.'/'.str_replace("%PILOTID%",$this->getPilotID(),str_replace("%YEAR%",$this->getYear(),$CONF['paths']['intermediate']) ).'/'.$this->filename."$suffix.txt";
		//return $this->getPilotAbsDir()."/flights/".$this->getYear()."/".$this->filename."$suffix.txt";
	}
	function getJsFilename($timeStep=0) { // values > 0 mean 1-> first level of timestep, usually 20 secs, 2-> less details usually 30-40 secs
		global $CONF;
		if ($timeStep) $suffix=".".$timeStep;
		else $suffix="";
		return LEONARDO_ABS_PATH.'/'.str_replace("%PILOTID%",$this->getPilotID(),str_replace("%YEAR%",$this->getYear(),$CONF['paths']['js']) ).'/'.$this->filename."$suffix.js";
		//return $this->getPilotAbsDir()."/flights/".$this->getYear()."/".$this->filename."$suffix.js";
	}

	function getJsonFilename() {
		global $CONF;
		return LEONARDO_ABS_PATH.'/'.str_replace("%PILOTID%",$this->getPilotID(),str_replace("%YEAR%",$this->getYear(),$CONF['paths']['js']) ).'/'.$this->filename.$suffix.".json.js";
		//return $this->getPilotAbsDir()."/flights/".$this->getYear()."/".$this->filename.".json.js";
	}
	//---------------------------------------
	// Full url functions
	//----------------------------------------
	function getFlightLinkURL() {
		return getLeonardoLink(array('op'=>'show_flight','flightID'=>$this->flightID));
	}

	function getIGC_URL($saned=0) {
		global $baseInstallationPath;
		return "http://".$_SERVER['SERVER_NAME'].$baseInstallationPath."/".$this->getIGCRelPath($saned);
	}

	function getZippedIGC_URL() {
		return getDownloadLink(array('type'=>'igc','zip'=>'1','flightID'=>$this->flightID)) ;
		// getRelMainDir()."download.php?type=igc&zip=1&flightID=".$this->flightID;
	}

	function getFlightKML($includeLang=1) {
		global $currentlang;
		if ($includeLang) $langArray=array("lng">=$currentlang);
		else  $langArray=array();

		return 	getDownloadLink(array('type'=>'kml_trk','flightID'=>$this->flightID)+$langArray) ;
		// getRelMainDir()."download.php?type=kml_trk&flightID=".$this->flightID.$langStr;
	}

	function getFlightGPX() {
		return 	getDownloadLink(array('type'=>'gpx_trk','flightID'=>$this->flightID)) ;
		//getRelMainDir()."download.php?type=gpx_trk&flightID=".$this->flightID;
	}

	//------------------------------
	// End of path functions
	//------------------------------


	function kmlGetDescription($ext,$getFlightKML,$loadTrackLink=0) {
		if ($this->takeoffVinicity > $takeoffRadious )
			$location=getWaypointName($this->takeoffID,0)." [~".sprintf("%.1f",$this->takeoffVinicity/1000)." km]";
		else $location=getWaypointName($this->takeoffID,0);

		if ($this->landingVinicity > $landingRadious )
			$location_land=getWaypointName($this->landingID,0)." [~".sprintf("%.1f",$this->landingVinicity/1000)." km]";
		else $location_land=getWaypointName($this->landingID,0);

		$fl_url=$this->getFlightLinkURL();
		//$fl_url=str_replace("&","&#38;",$this->getFlightLinkURL());
		//$fl_url=str_replace("&nbsp;"," ",$fl_url);

		$str="<description><![CDATA[<table cellpadding=0 cellspacing=0>";
		
		if ($loadTrackLink) 
			$str.="<tr bgcolor='#D7E1EE'><td></td><td><div align='right'><a href='$getFlightKML'><b>Load Track</b></a></div></td></tr>";					
		else 
			$str.="<tr bgcolor='#D7E1EE'><td></td><td><div align='right'><a href='$fl_url'><b>"._See_more_details."</b></a></div></td></tr>";
		
		$str.="<tr bgcolor='#CCCCCC'><td>"._PILOT."</td><td>  ".htmlspecialchars($this->userName)."</td></tr>".
			"<tr><td>"._TAKEOFF_LOCATION."</td><td> ".$location."</td></tr>".
			"<tr bgcolor='#CCCCCC'><td>"._TAKEOFF_TIME."</td><td>    ".formatDate($this->DATE,false)." - ".sec2Time($this->START_TIME,1)."</td></tr>".
			"<tr><td>"._LANDING_LOCATION."</td><td> ".$location_land."</td></tr>".
			"<tr bgcolor='#CCCCCC'><td>"._OPEN_DISTANCE."</td><td> ".formatDistance($this->LINEAR_DISTANCE,1)."</td></tr>".
			"<tr><td>"._DURATION."</td><td> ".sec2Time($this->DURATION,1)."</td></tr>".
			"<tr bgcolor='#CCCCCC'><td>"._OLC_SCORE_TYPE."</td><td> ".htmlspecialchars(formatOLCScoreType($this->BEST_FLIGHT_TYPE,false))."</td></tr>".
			"<tr><td>"._OLC_DISTANCE."</td><td> ".formatDistance($this->FLIGHT_KM,1)."</td></tr>".
			"<tr bgcolor='#CCCCCC'><td>"._OLC_SCORING."</td><td> ".sprintf("%.1f",$this->FLIGHT_POINTS)."</td></tr>".
			"<tr><td>"._MAX_SPEED."</td><td> ".formatSpeed($this->MAX_SPEED)."</td></tr>".
			"<tr bgcolor='#CCCCCC'><td>"._MAX_VARIO."</td><td> ".formatVario($this->MAX_VARIO)."</td></tr>".
			"<tr><td>"._MIN_VARIO."</td><td> ".formatVario($this->MIN_VARIO)."</td></tr>".
			"<tr bgcolor='#CCCCCC'><td>"._MAX_ALTITUDE."</td><td> ".formatAltitude($this->MAX_ALT)."</td></tr>".
			"<tr><td>"._MIN_ALTITUDE."</td><td> ".formatAltitude($this->MIN_ALT)."</td></tr>".
			"<tr bgcolor='#CCCCCC'><td>"._TAKEOFF_ALTITUDE."</td><td> ".formatAltitude($this->TAKEOFF_ALT)."</td></tr>".
			"<tr><td>"._GLIDER."</td><td>".$this->glider."</td></tr>".
			"<tr bgcolor='#D7E1EE'><td></td><td><div align='right'>"._KML_file_made_by." <a href='https://leonardo.pgxc.pl'>Leonardo</a></div></td></tr>";
			if($ext) $str.=	"<tr bgcolor='#D7E1EE'><td></td><td><div align='right'>Extra analysis module by  Man\'s <a href='http://www.parawing.net'>GPS2GE V2.0</a></div></td></tr>";
			$str.="<tr bgcolor='#D7E1EE'><td></td><td><div align='right'><a href='$getFlightKML&show_url=1'>URL of this KML file</div></td></tr>";

			$str.="</table>]]></description>";
		return $str;
	}


	function gMapsGetTaskJS($useJSapi=0){
		global  $CONF;

		$tpStr=' { "turnpoints":  [ ';

		$flightScore=new flightScore($this->flightID);
		$flightScore->getFromDB();

		$defaultMethodID= $CONF['scoring']['default_set'];
		$scoreDetails=$flightScore->scores[$defaultMethodID][ $flightScore->bestScoreType ];

		$tpNum=0;
		for($i=1;$i<=7;$i++) {
			if ($scoreDetails['tp'][$i]) {
				$newPoint=new gpsPoint($scoreDetails['tp'][$i],$this->timezone);
				if ($tpNum>0) $tpStr.=" ,\n		";
				$tpStr.=' {"id": "'.$i.'" , "name": "TP'.$i.'" , "lat": '.$newPoint->lat().', "lon": '.$newPoint->lon().', "secs": '.$newPoint->getTime().' } ';
				$tpNum++;
			}
		}

		/*
		$j=0;
		for($i=1;$i<=5;$i++) {
			$varname="turnpoint$i";
			if ($this->{$varname} ) {
				$pointString=explode(" ",$this->{$varname});
				// make this string
				// B1256514029151N02310255EA0000000486
				// from N40:29.151 E23:10.255
				preg_match("/([NS])(\d+):(\d+)\.(\d+) ([EW])(\d+):(\d+)\.(\d+)/",$this->{$varname},$matches);

				$lat=preg_replace("/[NS](\d+):(\d+)\.(\d+)/","\\1\\2\\3",$pointString[0]);
				$lon=preg_replace("/[EW](\d+):(\d+)\.(\d+)/","\\1\\2\\3",$pointString[1]);

				$pointStringFaked=sprintf("B125959%02d%02d%03d%1s%03d%02d%03d%1sA0000000500",
							$matches[2],$matches[3],$matches[4],$matches[1],
							$matches[6],$matches[7],$matches[8],$matches[5] );

				$newPoint=new gpsPoint( $pointStringFaked ,$this->timezone );

				$name=$i;

				if ($i> 1 ) $jsonStr.=' , ';
				$jsonStr.=' { "lat": '.$newPoint->lat().' , "lon": '.$newPoint->lon().
							', "name": "TP'.$name.'", "id": "'.$name.'"  } '."\n";

				$j++;

			}

		}
*/

		$tpStr.=' ] } ';
		return $tpStr;
	}


	function kmlGetTask($completeKMLfile=0){
		if ($completeKMLfile) {
			$kml_file_contents="<?xml version='1.0' encoding='UTF-8'?>\n".
								"<kml xmlns=\"http://earth.google.com/kml/2.1\">\n";
		} else {
			$kml_file_contents="";
		}

		$kml_file_contents.="<Folder>
		<name>XC Task</name>

		";


		$kml_file_contents.="<Placemark>
		<name>XC Task</name>
		<Style id='taskLine'>
			<LineStyle><color>ffffffff</color></LineStyle>
		</Style>
		<LineString>
			<altitudeMode>extrude</altitudeMode>
			<coordinates>
		";

		$icons=array(
		1=>array("http://maps.google.com/mapfiles/kml/paddle/grn-circle.png",32,1),
		2=>array("http://maps.google.com/mapfiles/kml/paddle/1.png",32,1),
		3=>array("http://maps.google.com/mapfiles/kml/paddle/2.png",32,1),
		4=>array("http://maps.google.com/mapfiles/kml/paddle/3.png",32,1),
		5=>array("http://maps.google.com/mapfiles/kml/paddle/purple-square.png",32,1) );


		global  $CONF;
		$tpStr=' { "turnpoints":  [ ';

		$flightScore=new flightScore($this->flightID);
		$flightScore->getFromDB();

		$defaultMethodID= $CONF['scoring']['default_set'];
		$scoreDetails=$flightScore->scores[$defaultMethodID][ $flightScore->bestScoreType ];

		$j=0;
		for($i=1;$i<=7;$i++) {
			if ($scoreDetails['tp'][$i]) {
				$newPoint=new gpsPoint($scoreDetails['tp'][$i],$this->timezone);
				$kml_file_contents.=$newPoint->lon().",".$newPoint->lat().",0 ";
				$turnpointPlacemark[$j]="
		<Placemark>
				 <Style>
				  <IconStyle>
					<scale>1.0</scale>
					<Icon>
					  <href>".$icons[$j+1][0]."</href>					 
					</Icon>
					<hotSpot x=\"32\" y=\"1\" xunits=\"pixels\" yunits=\"pixels\"/>
				  </IconStyle>
				</Style>
		 <Point>
			<coordinates>".$newPoint->lon().",".$newPoint->lat().",0</coordinates>
		 </Point>
		</Placemark>";
				$j++;

			}
		}

		$kml_file_contents.="
		</coordinates>
		</LineString>
		</Placemark>";

		for ($i=0;$i<$j;$i++)
			$kml_file_contents.=$turnpointPlacemark[$i];
		$kml_file_contents.="</Folder>";

		if ($completeKMLfile) {
			$kml_file_contents="</kml>\n";
		}
		return $kml_file_contents;
	}

	function kmlGetTrack($lineColor="ff0000",$exaggeration=1,$lineWidth=2,$extended=1) {
		global $takeoffRadious,$landingRadious;
		global $moduleRelPath,$baseInstallationPath;
		global $langEncodings,$currentlang;

		$getFlightKML=$this->getFlightKML()."&c=$lineColor&w=$lineWidth&an=$extended";

		// do some basic check for saned igc file
		if (! $this->checkSanedFiles() ) {
			return "";
		}

		if ($extended==1) {
			//$kml_file_contents.="<Placemark >\n<name>".$this->filename."</name>";
			// $kml_file_contents.=$this->kmlGetDescription($extended,$getFlightKML);
			//$kml_file_contents.="</Placemark>";
			require_once dirname(__FILE__)."/FN_kml.php";
			kmlGetTrackAnalysis($this->getKMLFilename(1),$this->getIGCFilename(2),1);
			$kml_file_contents="
<NetworkLink>
  <name>Extended analysis</name>
  <visibility>1</visibility>
  <open>1</open>
  <description> Extra analysis generation by  Man\'s GPS2GE V2.0 (http://www.parawing.net)</description>
  <refreshVisibility>0</refreshVisibility>
  <flyToView>0</flyToView>
  <Link>
	<href>http://".$_SERVER['SERVER_NAME']."/$baseInstallationPath/".$this->getKMLRelPath(1)."</href>
  </Link>
</NetworkLink>";

			return $kml_file_contents;
		}


		$KMLlineColor="ff".substr($lineColor,4,2).substr($lineColor,2,2).substr($lineColor,0,2);
		$kmzFile=$this->getKMLFilename(0);
		$kmlTempFile=$kmzFile.".tmp.kml";

// DEBUG MANOLIS force creation
		if ( !file_exists($kmzFile)  ) { // create the kmz file containg the points only

			$filename=$this->getIGCFilename(2);
			$lines = file ($filename);
			if ( $lines) {
				$i=0;

/*
new ge features

http://googlegeodevelopers.blogspot.com/2010/07/making-tracks-new-kml-extensions-in.html
http://google-latlong.blogspot.com/2010/06/relive-your-hiking-biking-and-other.html

****** !!!! ****
http://code.google.com/intl/el-GR/apis/kml/documentation/kmlreference.html#trackexample

http://sketchup.google.com/3dwarehouse/cldetails?mid=c166a0a48065f4403a426bad1ca64772&ct=mdcc&prevstart=0
*/

	$str='<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2"
 xmlns:gx="http://www.google.com/kml/ext/2.2">
 ';

		$str.="<Document>
		<name>Tracklog File</name>
		<Schema id=\"schema\">
		  <gx:SimpleArrayField name=\"Climb\" type=\"float\">
			  <displayName>CLimb (m/s)</displayName>
		  </gx:SimpleArrayField>
		</Schema>";
		
$strXXX.='<!-- Normal multiTrack style -->
    <Style id="Track_n">
      <LineStyle>
			<color>ff0000ff</color>
			<width>2</width>
      </LineStyle>
      <IconStyle>
        <Icon>
          <href>http://earth.google.com/images/kml-icons/track-directional/track-0.png</href>
        </Icon>
      </IconStyle>
    </Style>
<!-- Highlighted multiTrack style -->
    <Style id="Track_h">
      <LineStyle>
			<color>ff0000ff</color>
			<width>1</width>
      </LineStyle>
      <IconStyle>
        <scale>1.2</scale>
        <Icon>
          <href>http://earth.google.com/images/kml-icons/track-directional/track-0.png</href>
        </Icon>
      </IconStyle>
    </Style>
	
    <StyleMap id="TrackStyle">
      <Pair>
        <key>normal</key>
        <styleUrl>#Track_n</styleUrl>
      </Pair>
      <Pair>
        <key>highlight</key>
        <styleUrl>#Track_h</styleUrl>
      </Pair>
    </StyleMap>
	
	';

		$str.="<Placemark id='timeTrack'>
					<name>Tracklog</name>
					    <Style id='Track1'>
      <LineStyle>
			<color>ff0000ff</color>
			<width>2</width>
      </LineStyle>
      <IconStyle>
        <Icon>
          <href>http://earth.google.com/images/kml-icons/track-directional/track-0.png</href>
        </Icon>
      </IconStyle>
    </Style>";
	

				$str.="<gx:MultiTrack id=\"MultiTrack\">
				<gx:Track>					
				<altitudeMode>absolute</altitudeMode>\n\n";


				//$kml_file_contents=str_replace("&","&#38;",$kml_file_contents);
				// $kml_file_contents=utf8_encode(str_replace("&nbsp;"," ",$kml_file_contents));
				$str=str_replace("&nbsp;"," ",$str);
				$timeStampStr='';
				foreach($lines as $line) {
					$line=trim($line);
					if  (strlen($line)==0) continue;
					if ($line{0}=='B') {
							if  ( strlen($line) < 23 ) 	continue;
							// also check for bad points
							// 012345678901234567890123456789
							// B1522144902558N00848090EV0058400000
							if ($line{24}=='V') continue;

							$thisPoint=new gpsPoint($line,$this->timezone);
							$alt=$thisPoint->getAlt();
							$lon=-$thisPoint->lon;
							$lat=$thisPoint->lat;
							// $data_alt[$i]=$thisPoint->getAlt();
							if ( $alt > $this->maxAllowedHeight ) continue;
							// $str.=$lon.",".$lat.",".($alt*$exaggeration)." ";
							
							if ($tm0) {
								$dalt=$alt-$alt0;
								$dt=$thisPoint->getTime()-$tm0;
								$climb=sprintf("%.1f",$dalt/$dt);
								if (abs($climb) > 20 ) $climb=0;
							} else {
								$climb=0;
							} 							
							$tm0=$thisPoint->getTime();
							$alt0=$alt;
							
							$strWhen.="<when>".$this->DATE."T".sec2Time24h( ( $thisPoint->getTime() )%(3600*24) )."Z</when>\n";
					        $strCord.="<gx:coord>".sprintf("%.5f",$lon)." ".sprintf("%.5f",$lat)." ".round($alt)."</gx:coord>\n";
							$strClimb.="<gx:value>$climb</gx:value>\n";

							$simpleTrackStr.=sprintf("%.5f",$lon).",".sprintf("%.5f",$lat).",".round($alt)." ";
							if($i % 50==0) $simpleTrackStr.="\n";
							
							$i++;
					}
				}

				//$str.="</coordinates>\n</LineString>\n";
				$str.=$strWhen."\n\n".$strCord.
				"<ExtendedData>
		            <SchemaData schemaUrl=\"#schema\">
		              <gx:SimpleArrayData name=\"Climb\">
						".$strClimb."
						 </gx:SimpleArrayData>
					</SchemaData>
				  </ExtendedData>
				  ";

				$str.="	
					</gx:Track>
				</gx:MultiTrack>
						";
							
				$str.="</Placemark>";
				
				$str.=' <Placemark id="simpleTrack">
    <name>Simple Track</name>
	 <Style id="Track2">
      <LineStyle>
			<color>ff0000ff</color>
			<width>2</width>
      </LineStyle>
    </Style>
	
	<LineString >
  <extrude>0</extrude>
  <altitudeMode>absolute</altitudeMode> 
  <coordinates>'.$simpleTrackStr.'</coordinates>
</LineString>
</Placemark>';

$str.="
		<Style id='s_camera'>
		<IconStyle>
			<color>ff00ff00</color>
			<scale>0.8</scale>
			<Icon>
				<href>http://maps.google.com/mapfiles/kml/shapes/camera.png</href>
			</Icon>
			<hotSpot x='0.5' y='0' xunits='fraction' yunits='fraction'/>
		</IconStyle>
		<ListStyle>
		</ListStyle>
		</Style>";
		
// now the photos
if ($this->hasPhotos) {
	require_once dirname(__FILE__)."/CL_flightPhotos.php";
	global $CONF;

	$flightPhotos=new flightPhotos($this->flightID);
	$flightPhotos->getFromDB();

	// get geoinfo
	$flightPhotos->computeGeoInfo();

	$imagesHtml="";
	foreach ( $flightPhotos->photos as $photoNum=>$photoInfo) {
		
		if ($photoInfo['lat'] && $photoInfo['lon'] ) {
			$imgIconRel=$flightPhotos->getPhotoRelPath($photoNum).".icon.jpg";
			$imgBigRel=$flightPhotos->getPhotoRelPath($photoNum);
	
			$imgIcon=$flightPhotos->getPhotoAbsPath($photoNum).".icon.jpg";
			$imgBig=$flightPhotos->getPhotoAbsPath($photoNum);

			if (file_exists($imgBig) ) {
				list($width, $height, $type, $attr) = getimagesize($imgBig);
				list($width, $height)=CLimage::getJPG_NewSize($CONF['photos']['mid']['max_width'], $CONF['photos']['mid']['max_height'], $width, $height);
				$imgTarget=$imgBigRel;
			} else 	if (file_exists($imgIcon) ) {
				list($width, $height, $type, $attr) = getimagesize($imgIcon);
				list($width, $height)=CLimage::getJPG_NewSize($CONF['photos']['mid']['max_width'], $CONF['photos']['mid']['max_height'], $width, $height);
				$imgTarget=$imgIconRel;
			} 
			$imgIconRel="http://".$_SERVER['SERVER_NAME'].$baseInstallationPath."/".$imgIconRel;
			$imgTarget="http://".$_SERVER['SERVER_NAME'].str_replace('//','/',$baseInstallationPath."/".$imgTarget) ;

			$altitudeMode="absolute";
			if ($photoInfo['alt']==0) $altitudeMode="clampToGround";
			
			$str.="
			<Placemark>
		    <name>Photo</name>
			<styleUrl>#s_camera</styleUrl>
			<Snippet maxLines='0' ></Snippet>
			<description><![CDATA[<a href='$imgTarget'><img src='$imgTarget' width='".$CONF['photos']['mid']['max_width']."' border='0'></a>
]]></description>
			<Point>
				<altitudeMode>$altitudeMode</altitudeMode>  	    
				<coordinates>".$photoInfo['lon'].",".$photoInfo['lat'].",".$photoInfo['alt']."</coordinates>
			</Point>
			</Placemark>";
			 	
		}		
	}
}
	

				$str.="\n</Document>\n</kml>";

				writeFile($kmlTempFile,$str);
				// zip the file
				require_once dirname(__FILE__)."/lib/pclzip/pclzip.lib.php";
				$archive = new PclZip($kmzFile);
				$v_list = $archive->create($kmlTempFile, PCLZIP_OPT_REMOVE_ALL_PATH);
				$this->deleteFile($kmlTempFile);
			}
		}

		$getFlightKMLcolorUpdater=$this->getFlightKML()."&c=$lineColor&w=$lineWidth&an=$extended&updateColor=1";


		$kml_file_contents='';
		$kml_file_contents.="
<NetworkLink>
  <name>Tracklog</name>
  <visibility>1</visibility>
  <open>1</open>
  <refreshVisibility>1</refreshVisibility>
  <flyToView>1</flyToView>
  <Link>
	<href>http://".str_replace('//','/',$_SERVER['SERVER_NAME']."/$baseInstallationPath/".$this->getKMLRelPath(0) )."</href>
  </Link>
</NetworkLink>";

$kml_file_contents.="
<NetworkLink>
  <name>UpdateColor</name>
  <Link>
    <href>".
	//str_replace("&","&#38;",str_replace('kml_trk','kml_trk_color',$getFlightKML) ).
	str_replace("&","&#38;",$getFlightKMLcolorUpdater ).
	"</href>
	</Link>
</NetworkLink>
";
//echo "## $getFlightKMLcolorUpdater ## ";
//exit;

		return $kml_file_contents;
	}

	function getBounds() {
		$filename=$this->getIGCFilename(1);
		$lines = file ($filename);
		if (!$lines) return array(0,0,0,0);
		$i=0;

		$min_lat=1000;
		$max_lat=-1000;
		$min_lon=1000;
		$max_lon=-1000;

		foreach($lines as $line) {
			$line=trim($line);
			if  (strlen($line)==0) continue;
			if ($line{0}=='B') {
					if  ( strlen($line) < 23 ) 	continue;
					// also check for bad points
					// 012345678901234567890123456789
					// B1522144902558N00848090EV0058400000
					if ($line{24}=='V') continue;

					$thisPoint=new gpsPoint($line,$this->timezone);
					if ( $thisPoint->lat  > $max_lat )  $max_lat =$thisPoint->lat  ;
					if ( $thisPoint->lat  < $min_lat )  $min_lat =$thisPoint->lat  ;
					if ( -$thisPoint->lon  > $max_lon )  $max_lon =-$thisPoint->lon  ;
					if ( -$thisPoint->lon  < $min_lon )  $min_lon =-$thisPoint->lon  ;

					$i++;
			}
		}
		return array($min_lat,$min_lon,$max_lat,$max_lon);
	}

	function gpxGetTrack() {
		global $takeoffRadious,$landingRadious;
		global $moduleRelPath,$baseInstallationPath;
		global $langEncodings,$currentlang;

		// $getFlightKML=$this->getFlightKML()."c=$lineColor&ex=$exaggeration&w=$lineWidth&an=$extended";

		$filename=$this->getIGCFilename(0);
		$lines = file ($filename);
		if (!$lines) return;
		$i=0;
		$kml_file_contents="<trk>\n
<name>".htmlspecialchars($this->userName)."</name>
<desc>Leonardo track</desc>
	<trkseg>\n";

		$min_lat=1000;
		$max_lat=-1000;
		$min_lon=1000;
		$max_lon=-1000;


		foreach($lines as $line) {
			$line=trim($line);
			if  (strlen($line)==0) continue;
			if ($line{0}=='B') {
					if  ( strlen($line) < 23 ) 	continue;
					// also check for bad points
					// 012345678901234567890123456789
					// B1522144902558N00848090EV0058400000
					if ($line{24}=='V') continue;

					$thisPoint=new gpsPoint($line,$this->timezone);
					if ( $thisPoint->lat  > $max_lat )  $max_lat =$thisPoint->lat  ;
					if ( $thisPoint->lat  < $min_lat )  $min_lat =$thisPoint->lat  ;
					if ( -$thisPoint->lon  > $max_lon )  $max_lon =-$thisPoint->lon  ;
					if ( -$thisPoint->lon  < $min_lon )  $min_lon =-$thisPoint->lon  ;

					$data_alt[$i]=$thisPoint->getAlt();
					if ( $thisPoint->getAlt() > $this->maxAllowedHeight ) continue;
					$kml_file_contents.='<trkpt lat="'.$thisPoint->lat.'" lon="'.-$thisPoint->lon.'">'."\n";
					$kml_file_contents.="  <ele>".$thisPoint->getAlt()."</ele>\n</trkpt>\n";
					$i++;
					//	if($i % 50==0) $kml_file_contents.="\n";
			}
		}

		$kml_file_contents.="</trkseg>\n</trk>\n<bounds minlat=\"$min_lat\" minlon=\"".$min_lon."\" maxlat=\"$max_lat\" maxlon=\"".$max_lon."\" />\n";
		//echo "</trkseg>\n</trk>\n<bounds minLat=\"$min_lat\" minlon=\"$min_lon\" maxLat=\"$max_lat\" maxlon=\"$max_lon\" />\n";
		return $kml_file_contents;
	}

	function createEncodedPolyline($forceRefresh=0) {
		global $takeoffRadious,$landingRadious;
		global $moduleRelPath,$baseInstallationPath;
		global $langEncodings,$currentlang;

		if ( is_file($this->getPolylineFilename())  && !$forceRefresh ) return ;

		$filename=$this->getIGCFilename(1);
		$lines = file ($filename);
		if (!$lines) return;
		$i=0;
		$kml_file_contents="";

		$min_lat=1000;
		$max_lat=-1000;
		$min_lon=1000;
		$max_lon=-1000;

		$prevLat=0;
		$prevLon=0;

		// compute num of B lines
		$numLines=0;
		foreach($lines as $line) {
			if ($line{0}=='B' && strlen($line) >= 23 && $line{24}!='V' ) $numLines++;
		}

		if ($numLines<200 ) {
			$mod=1;
			$lvlArray="3"; // always 3
		} else 	if ($numLines>=200 && $numLines<400) {
			$mod=8; // max visible every 8 points
			$lvlArray="30102010"; // for mod 8
		} else 	if ($numLines>=400 && $numLines<600) {
			$mod=12;
			$lvlArray="300100200100";
		} else 	if ($numLines>=600) {
			$mod=18;
			$lvlArray="300100200100200100";
		}

		$lvlEncArray=array(0=>'?','@','A','B' );

		foreach($lines as $line) {
			$line=trim($line);
			if  (strlen($line)==0) continue;
			if ($line{0}=='B') {
					if  ( strlen($line) < 23 ) 	continue;
					// also check for bad points
					// 012345678901234567890123456789
					// B1522144902558N00848090EV0058400000
					if ($line{24}=='V') continue;

					$thisPoint=new gpsPoint($line,$this->timezone);
					$lat=$thisPoint->lat;
					$lon=-$thisPoint->lon;
					if ($i==0) $ln="$lat|$lon|Takeoff|Takeoff\n";

					if ( $lat  > $max_lat )  $max_lat =$lat  ;
					if ( $lat  < $min_lat )  $min_lat =$lat  ;
					if ( $lon  > $max_lon )  $max_lon =$lon  ;
					if ( $lon  < $min_lon )  $min_lon =$lon  ;

					$kml_file_contents.=encodeNumber($lat-$prevLat).encodeNumber($lon-$prevLon);


					$thisLvl=$lvlEncArray[ $lvlArray[$i%$mod]+0 ];
//					$levels.="B";
					$levels.=$thisLvl;


					$prevLat=$lat;
					$prevLon=$lon;

					$i++;
			}
		}
		$ln.="$lat|$lon|Landing|Landing\n";
		$ln.="$min_lat,$max_lat,$min_lon,$max_lon\n";
		// write to file
		$handle = fopen($this->getPolylineFilename(), "w");
		fwrite($handle, $ln.$kml_file_contents."\n".$levels);
		fclose($handle);
		return 1;
	}

	function createKMLfile($lineColor="ff0000",$exaggeration=1,$lineWidth=2,$extendedInfo=0) {
		global $takeoffRadious,$landingRadious;
		global $moduleRelPath,$baseInstallationPath;
		global $langEncodings,$currentlang,	$CONF_use_utf;

		$exaggeration=1;
		//if (file_exists($this->getKMLFilename())) return;

		$getFlightKML=$this->getFlightKML()."&c=$lineColor&w=$lineWidth&an=$extendedInfo";
		//UTF-8 or
		//".$langEncodings[$currentlang]."
$kml_file_contents=
//"<?xml version='1.0' encoding='".$langEncodings[$currentlang]."'? >".
"<?xml version='1.0' encoding='UTF-8'?>\n".
"<kml xmlns=\"http://earth.google.com/kml/2.1\">
<Document>
<name>".$this->filename."</name>".$this->kmlGetDescription($extendedInfo,$getFlightKML)."
<open>1</open>";

		/*<LookAt>
			<longitude>-3.10135108046447</longitude>
			<latitude>52.9733850011146</latitude>
			<range>3000</range>
			<tilt>65</tilt>
			<heading>227.735584972338</heading>
		</LookAt>*/

	    $kml_file_contents.=$this->kmlGetTrack($lineColor,$exaggeration,$lineWidth,$extendedInfo);


		// create the start and finish points
		$kml_file_contents.=makeWaypointPlacemark($this->takeoffID);
		if ( $this->takeoffID!=$this->landingID)
			$kml_file_contents.=makeWaypointPlacemark($this->landingID);

		// create the XC task
		$kml_file_contents.=$this->kmlGetTask();

		$kml_file_contents.="</Document>\n</kml>";

		if (! $CONF_use_utf ) {
			require_once dirname(__FILE__)."/lib/ConvertCharset/ConvertCharset.class.php";
			$NewEncoding = new ConvertCharset;
			$FromCharset=$langEncodings[$currentlang];
			$kml_file_contents = $NewEncoding->Convert($kml_file_contents, $FromCharset, "utf-8", $Entities);
		}
		return $kml_file_contents;
	}

	function createKMZfile($c, $ex, $w, $an) {
		global $CONF;

		// do some basic check for saned igc file
		if (! $this->checkSanedFiles() ) {
			return "";
		}

		require_once dirname(__FILE__)."/FN_igc2kmz.php";
		$igc2kmzVersion=igc2kmz($this->getIGCFilename(0),$this->getKMLFilename(3),$this->timezone,$this->flightID);
		$version=$CONF['googleEarth']['igc2kmz']['version'];
		$file_name=$this->getKMLFilename(3).'.'.$version.'.kmz';
		return file_get_contents($file_name);
	}

	function createGPXfile($returnAlsoXML=0) {

		global $takeoffRadious,$landingRadious;
		global $moduleRelPath,$baseInstallationPath;
		global $langEncodings,$currentlang;

		if (file_exists($this->getGPXFilename()) && 0 ) {
			if (!$returnAlsoXML) return 1;
			$xml_file_contents=file_get_contents( $this->getGPXFilename() );
			return $xml_file_contents;
		}

		$xml_file_contents=
'<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" creator="byHand" version="1.1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
';

	    $xml_file_contents.=$this->gpxGetTrack();


		// create the start and finish points
//		$xml_file_contents.=makeWaypointPlacemark($this->takeoffID);
//		if ( $this->takeoffID!=$this->landingID)
//			$xml_file_contents.=makeWaypointPlacemark($this->landingID);

		// create the OLC task
		// $xml_file_contents.=$this->kmlGetTask();

		$xml_file_contents.="</gpx>";

		// write to file
		$handle = fopen($this->getGPXFilename(), "w");
		fwrite($handle, $xml_file_contents);
		fclose($handle);

		// return the xml
		if ($returnAlsoXML)		return $xml_file_contents;
		else return 1;
	}

	function getAltValues() {
		$this->setAllowedParams();

		$max_allowed_alt_diff=400;
		$max_allowed_speed_diff=70;
		$max_allowed_vario_diff=15;


		$data_time =array();
		$data_alt =array();
		$data_speed =array();
		$data_vario =array();
		$data_takeoff_distance=array();

		$filename=$this->getIGCFilename(1);
		$lines = @file ($filename);
		if (!$lines) return;
		$i=0;
		$day_offset =0;

		
		$duration=$this->DURATION; // in secs
		$intervalSecs=floor($duration/600);
		if ($intervalSecs>20) $intervalSecs=20;
					
		foreach($lines as $line) {
			$line=trim($line);
			if  (strlen($line)==0) continue;
			if ($line{0}=='B') {
					if  ( strlen($line) < 23 ) 	continue;
					// also check for bad points
					// 012345678901234567890123456789
					// B1522144902558N00848090EV0058400000
					if ($line{24}=='V') continue;

					$thisPoint=new gpsPoint($line,$this->timezone);
					$thisPoint->gpsTime+=$day_offset;

					// check for start of flight
					if ($thisPoint->getTime()<$this->START_TIME) continue;
					// check for end of flight
					if ($thisPoint->getTime()>$this->END_TIME) continue;

					$data_time[$i]=sec2Time($thisPoint->getTime(),1);
					$data_alt[$i]=$thisPoint->getAlt();
					if ( $data_alt[$i] > $this->maxAllowedHeight ) continue;

					if ($i>0) {
						$tmp = $lastPoint->calcDistance($thisPoint);
						if ( ($lastPoint->getTime() - $thisPoint->getTime()  ) > 3600 )  { // more than 1 hour
                    		$day_offset = 86400; // if time seems to have gone backwards, add a day
		                    $thisPoint->gpsTime += 86400;
        		        } else if ( ($lastPoint->getTime() - $thisPoint->getTime()  ) > 0 ) {
							$lastPoint=new gpsPoint($line,$this->timezone);
							$lastPoint->gpsTime+=$day_offset;
							 continue;
						}
						
						if ( ($thisPoint->getTime() - $lastPoint->getTime() ) < $intervalSecs  ) {
							continue;						
						}
						        		        	
						$deltaseconds = $thisPoint->getTime() - $lastPoint->getTime() ;
						if ($deltaseconds) {
							$speed = ($deltaseconds)?$tmp*3.6/($deltaseconds):0.0; /* in km/h */
							$data_vario[$i] =($thisPoint->getAlt() - $lastPoint->getAlt() ) / $deltaseconds;
						} else {
							$speed =0;
							$data_vario[$i]=0;
						}
						// sanity checks
						if ( $speed > $this->maxAllowedSpeed ) continue;
						if ( $data_vario[$i]  > $this->maxAllowedVario ) continue;

						$data_speed[$i]=$speed;
						$data_takeoff_distance[$i] = $takeoffPoint->calcDistance($thisPoint) /1000;
					}	else  {
						$data_speed[$i]=0;
						$data_vario[$i]=0;
						$takeoffPoint=new gpsPoint($line,$this->timezone);
						$data_takeoff_distance[$i]=0;
					}
					$lastPoint=new gpsPoint($line,$this->timezone);
					$lastPoint->gpsTime+=$day_offset;
					$i++;
			}
		}

		return array($data_time,$data_alt,$data_speed,$data_vario,$data_takeoff_distance);
	}


	function makeJSON($forceRefresh=0) {
		global  $CONF;

		// $forceRefresh=1;

		$filename=$this->getJsonFilename();
		if ( is_file($filename) && ! $forceRefresh ) {
			return;
		}

		// if no file exists do the proccess now
		if ( ! is_file($this->getPointsFilename(1) ) || $forceRefresh ) $this->storeIGCvalues();

		$lines = file($this->getPointsFilename(1)); // get the normalized with constant time step points array
		if (!$lines) return;
		$i = 0;

		$jsTrack['max_alt']=0;
		$jsTrack['min_alt']=100000;

		// first 3 lines of pointsFile is reserved for info
		for($k=3;$k< count($lines);$k++){
			$line = trim($lines[$k]);
			if (strlen($line) == 0) continue;

			eval($line);

			//	if ($alt > $this->maxAllowedHeight)  continue;
			//    if ($speed > $this->maxAllowedSpeed) continue;
			//    if (abs($vario) > $this->maxAllowedVario) continue;

			if  ( $time<$lastPointTime ) continue;
			$lastPointTime=$time;

			//if (! $time_in_secs ) {
			//	$time = sec2Time($time, 1);
			//}

			$jsTrack['time'][$i]	=$time;
			$jsTrack['elev'][$i]	=$alt;
			$jsTrack['elevV'][$i] 	= $altV;
			$jsTrack['lat'][$i] 	=sprintf('%0.6f',$lat);
			$jsTrack['lon'][$i]		= sprintf('%0.6f',-$lon);
			$jsTrack['speed'][$i] 	= sprintf('%.2f',$speed);
			$jsTrack['vario'][$i] 	= sprintf('%.2f',$vario);
			
			
			



			if ( $CONF['maps']['3d'] ) {
				require_once dirname(__FILE__).'/CL_hgt.php';
				$jsTrack['elevGnd'][$i] = $elevGnd[$i] = hgt::getHeight($lat,-$lon);
			} else  {
				$jsTrack['elevGnd'][$i] = 0 ;
			}

			if ( $jsTrack['elevGnd'][$i] > $jsTrack['max_alt'] ) $jsTrack['max_alt']=$jsTrack['elevGnd'][$i];
			if ( $jsTrack['elev'][$i]    > $jsTrack['max_alt'] ) $jsTrack['max_alt']=$jsTrack['elev'][$i];
			if ( $jsTrack['elevGnd'][$i] < $jsTrack['min_alt'] ) $jsTrack['min_alt']=$jsTrack['elevGnd'][$i];
			if ( $jsTrack['elev'][$i]    < $jsTrack['min_alt']) $jsTrack['min_alt']=$jsTrack['elev'][$i];


			if ($i == 0) {
				$dis =0;
				$firstLat=$lat;
				$firstLon=$lon;
			}
			$jsTrack['distance'][$i] =sprintf('%.3f',$dis);

			$i ++;
		}  //end for loop

		/*
		 Change the number of points to CHART_NBPTS
		for ($i = 0, $idx = 0, $step = ($nbPts - 1) / (CHART_NBPTS - 1); $i < CHART_NBPTS; $i++, $idx += $step) {
			$jsTrack['elev'][$i] = $track['elev'][$idx];
			$jsTrack['time']['hour'][$i] = $track['time']['hour'][$idx];
			$jsTrack['time']['min'][$i] = $track['time']['min'][$idx];
			$jsTrack['time']['sec'][$i] = $track['time']['sec'][$idx];
		}

		*/

		$nbPts=count( $jsTrack['time']);
		$label_num=8;
		for ($i = 0, $idx = 0, $step = ($nbPts - 1) / ($label_num - 1); $i < $label_num; $i++, $idx += $step) {
			$jsTrack['labels'][$i] = $jsTrack['time'][$idx];
		}
		// 			$jsTrack['labels']=array("11h40","12h6","12h29","12h52","13h15");

		$jsTrack['points_num'] = $nbPts;
		$jsTrack['nbChartPt'] = $nbPts;
		$jsTrack['label_num'] = $label_num;
		$jsTrack['date'] = $this->DATE;

		require_once dirname(__FILE__).'/lib/json/CL_json.php';

		$JSONstr = json::encode($jsTrack);

		writeFile( $filename, 'var flightArray='.$JSONstr );
	}


  function getRawValues($forceRefresh=0, $getAlsoXY=0) {
    $this->setAllowedParams();

    $data_time = array ();
    $data_alt = array ();
    $data_speed = array ();
    $data_vario = array ();
    $data_takeoff_distance = array ();
    $data_X =array();
    $data_Y =array();

	if ( ! is_file($this->getPointsFilename(1) ) ||  $forceRefresh )
		 	$this->storeIGCvalues(); // if no file exists do the proccess now

    $lines = file($this->getPointsFilename(1)); // get the normalized with constant time step points array
    if (!$lines) return;
    $i = 0;

	// first 3 lines of pointsFile is reserved for info
	for($k=3;$k< count($lines);$k++){
		$line = trim($lines[$k]);
		if (strlen($line) == 0) continue;

		eval($line);

	//	if ($alt > $this->maxAllowedHeight)  continue;
    //    if ($speed > $this->maxAllowedSpeed) continue;
    //    if (abs($vario) > $this->maxAllowedVario) continue;

		if  ( $time<$lastPointTime ) continue;
		$lastPointTime=$time;

        if ($time_in_secs) $data_time[$i] = $time;
        else $data_time[$i] = sec2Time($time, 1);

        $data_alt[$i] = $alt;
        $data_speed[$i] = $speed;
        $data_vario[$i] = $vario;
        if ($getAlsoXY) {
          $data_X[$i]=-$lon;
          $data_Y[$i]=$lat;
        }

        if ($i > 0) {
			// $t_dis=gpsPoint::calc_distance($lat,$lon,$firstLat,$firstLon) ;
			// $data_takeoff_distance[$i] = $t_dis/1000; //gpsPoint::calc_distance($lat,$lon,$firstLat,$firstLon) /1000;
			$data_takeoff_distance[$i]=$dis;
        } else {
			$data_takeoff_distance[$i] = 0;
			$firstLat=$lat;
			$firstLon=$lon;
		}

        $i ++;
    } //end for loop

    if ($getAlsoXY)
      return array($data_time,$data_alt,$data_speed,$data_vario,$data_takeoff_distance,$data_X,$data_Y);
    else
      return array ($data_time, $data_alt, $data_speed, $data_vario, $data_takeoff_distance);

  }

	function getXYValues($tmAlso=0,$altAlso=0) {		
		$data_X =array();
		$data_Y =array();

		$data_alt=array();
		$data_tm=array();
		 
		$filename=$this->getIGCFilename(1);
		$lines = file ($filename);
		$i=0;


		foreach($lines as $line) {
			$line=trim($line);
			if  (strlen($line)==0) continue;
			if ($line{0}=='B' && $line{24}!='V') {
					$thisPoint=new gpsPoint($line,$this->timezone);
					$data_X[$i]=$thisPoint->lat;
					$data_Y[$i]=$thisPoint->lon;
					if ($tmAlso) $data_tm[$i]=$thisPoint->gpsTime;
					if ($altAlso) $data_alt[$i]=$thisPoint->getAlt();
					
					$i++;
			}
		}
		if ($tmAlso && $altAlso ) 
			return array($data_X,$data_Y,$data_tm, $data_alt);
		else if ($tmAlso) 
			return array($data_X,$data_Y,$data_tm);
		else 
			return array($data_X,$data_Y);
	}

	function getRawHeader(){
		$handle = fopen($this->getPointsFilename(1), "r");
		$l=array();
		$l[0]= fgets($handle, 4096);
		$l[1]= fgets($handle, 4096);
		$l[2]= fgets($handle, 4096);
		fclose ($handle);
		return $l;
	}

	function getPolyHeader(){
		$handle = fopen($this->getPolylineFilename(), "r");
		$l=array();
		$l[0]= fgets($handle, 4096);
		$l[1]= fgets($handle, 4096);
		$l[2]= fgets($handle, 4096);
		fclose ($handle);
		return $l;
	}

	function storeIGCvalues() {
		$data_time =array();
		$data_alt =array();
		$data_speed =array();
		$data_vario =array();
		$data_takeoff_distance=array();

		if (! $this->checkSanedFiles() ) {
			return;
		}

		$filename=$this->getIGCFilename(2);
		$lines = @file ($filename);
		if (!$lines) return;

		$i=0;
		$day_offset =0;

		$currentDate=0;
		$prevDate=0;

		foreach($lines as $line) {
			$line=trim($line);
			if  (strlen($line)==0) continue;

			if (strtoupper(substr($line,0,5)) =="HFDTE"  || strtoupper(substr($line,0,5)) =="HPDTE"  ) {  // HFDTE170104  OR HPDTE310805
//					$dt=substr($line,5,6);
					$line = str_replace('DATE:', '', strtoupper($line));
					$dt=substr($line,5,6);
					$yr_last=substr($dt,4,2);
					// case of YY=0 (1 digit) HFDTE08070
					if ($yr_last=="0") $yr_last="00";
					if ($yr_last > 80 ) $yr="19".$yr_last;
					else $yr="20".$yr_last;

					$prevDate=$currentDate;
					$currentDate=mktime(0,0,0,substr($dt,2,2),substr($dt,0,2),$yr);
					//echo "DATE cahnge : $currentDate  $prevDate <BR>";
					if ($prevDate>0 && ($currentDate-$prevDate)>86400) { // more than one day
						break;
					}
			}

			if ($line{0}=='B') {
					if  ( strlen($line) < 23 ) 	continue;
					// also check for bad points
					// 012345678901234567890123456789
					// B1522144902558N00848090EV0058400000
					if ($line{24}=='V') continue;

					$thisPoint=new gpsPoint($line,$this->timezone);
					$thisPoint->gpsTime+=$day_offset;
					// $goodPoints[$i]['time']=sec2Time($thisPoint->getTime(),1);
					$goodPoints[$i]['time']=$thisPoint->getTime();
					$goodPoints[$i]['lon']=$thisPoint->lon;
					$goodPoints[$i]['lat']=$thisPoint->lat;

					$goodPoints[$i]['alt']=$thisPoint->getAlt();
					$goodPoints[$i]['altV']=$thisPoint->getAlt(true); // prefer vario Alt

					if ($i>0) {
						$tmp = $lastPoint->calcDistance($thisPoint);

					$time_diff=  $thisPoint->getTime() - $lastPoint->getTime()  ;

					if (  $time_diff < 0 && $time_diff > -36000  )  { // if the time is less than 10 hours in the past  we just ignore it
						continue;
        		    } else 	if ( $time_diff < 0  )  {  // CHANGING DAY , means the flight is at night
						array_pop($goodPoints);
						break;
        		    } else 	if (  $time_diff > $this->max_allowed_time_gap *4 )  {  // found time gap
						array_pop($goodPoints);
						break;
					}

					/*	if ( ($lastPoint->getTime() - $thisPoint->getTime()  ) > 3600 )  { // more than 1 hour
                    		$day_offset = 86400; // if time seems to have gone backwards, add a day
		                    $thisPoint->gpsTime += 86400;
        		        } else if ( ($lastPoint->getTime() - $thisPoint->getTime()  ) > 0 ) {
							$lastPoint=new gpsPoint($line,$this->timezone);
							$lastPoint->gpsTime+=$day_offset;
							continue;
						}
						*/

						$deltaseconds = $thisPoint->getTime() - $lastPoint->getTime() ;
						if ($deltaseconds) {
							$speed = ($deltaseconds)?$tmp*3.6/($deltaseconds):0.0; /* in km/h */
							$goodPoints[$i]['vario'] =($thisPoint->getAlt() - $lastPoint->getAlt() ) / $deltaseconds;
						} else {
							$speed =0;
							$data_vario[$i]=0;
						}

						$goodPoints[$i]['speed']=$speed;
						$goodPoints[$i]['dis']=  $takeoffPoint->calcDistance($thisPoint) /1000;
					}	else  {
						$goodPoints[$i]['speed']=0;
						$goodPoints[$i]['vario']=0;
						$takeoffPoint=new gpsPoint($line,$this->timezone);
						$goodPoints[$i]['dis']=0;
					}
					$lastPoint=new gpsPoint($line,$this->timezone);
					$lastPoint->gpsTime+=$day_offset;

					$i++;
			}
		}

		$pointsNum=count($goodPoints);

		//echo "pointsNum : $pointsNum<br>";
		$min_time=floor($goodPoints[0]['time']/60)*60;
		$max_time=ceil($goodPoints[$pointsNum-1]['time']/60)*60;
		// echo "max / min time: $max_time $min_time<BR>";

		$interval=20;
		$k=0;
		for($i=$min_time;$i<=$max_time;$i=$i+$interval) {
			$normPoints[$k]=array();
			$normPoints[$k]['timeText']=sec2time($i);
			$normPoints[$k]['time']=$i;
			$k++;
		}

		$org_array_pos=0;
		foreach($normPoints as $norm_array_pos=>$normPoint) { // for each point  in the timeline
			$timeval=$normPoint['time'];
			if ( $goodPoints[$org_array_pos]['time'] >= $timeval && $org_array_pos< count($goodPoints)  &&
				!($org_array_pos==0 && $goodPoints[0]['time'] < $timeval)   ) {

				$normPoints[$norm_array_pos]['lon']=$goodPoints[$org_array_pos]['lon'];
				$normPoints[$norm_array_pos]['lat']=$goodPoints[$org_array_pos]['lat'];

				$normPoints[$norm_array_pos]['speed']=$goodPoints[$org_array_pos]['speed'];
				$normPoints[$norm_array_pos]['vario']=$goodPoints[$org_array_pos]['vario'];
				$normPoints[$norm_array_pos]['alt']=$goodPoints[$org_array_pos]['alt'];
				$normPoints[$norm_array_pos]['altV']=$goodPoints[$org_array_pos]['altV'];
				$normPoints[$norm_array_pos]['dis']=$goodPoints[$org_array_pos]['dis'];
				// if ($normPoints[$norm_array_pos]['alt']==0) $normPoints[$norm_array_pos]['alt']="-";

				while ($goodPoints[$org_array_pos]['time'] <  $timeval + $interval  && ($org_array_pos)< count($goodPoints) ) {
					$org_array_pos++;
				}
			} else {
				if ($norm_array_pos ) {
					$normPoints[$norm_array_pos]['lon']=$normPoints[$norm_array_pos-1]['lon'];
					$normPoints[$norm_array_pos]['lat']=$normPoints[$norm_array_pos-1]['lat'];
					$normPoints[$norm_array_pos]['speed']=$normPoints[$norm_array_pos-1]['speed'];
					$normPoints[$norm_array_pos]['vario']=$normPoints[$norm_array_pos-1]['vario'];
					$normPoints[$norm_array_pos]['alt']=$normPoints[$norm_array_pos-1]['alt'];
					$normPoints[$norm_array_pos]['altV']=$normPoints[$norm_array_pos-1]['altV'];
					$normPoints[$norm_array_pos]['dis']=$normPoints[$norm_array_pos-1]['dis'];
				}	else {
					$normPoints[$norm_array_pos]['lon']=$goodPoints[0]['lon'];
					$normPoints[$norm_array_pos]['lat']=$goodPoints[0]['lat'];
					$normPoints[$norm_array_pos]['speed']=$goodPoints[0]['speed'];
					$normPoints[$norm_array_pos]['vario']=$goodPoints[0]['vario'];
					$normPoints[$norm_array_pos]['alt']=$goodPoints[0]['alt'];
					$normPoints[$norm_array_pos]['altV']=$goodPoints[0]['altV'];
					$normPoints[$norm_array_pos]['dis']=$goodPoints[0]['dis'];
				}
	/*
				if ($org_array_pos>0) {
					$normPoints[$norm_array_pos]['speed']="-";
					$normPoints[$norm_array_pos]['vario']="-";
					$normPoints[$norm_array_pos]['alt']="-";
				}
	*/
			}


		} // end foreach

		// now write it to file
		$outputBuffer='$min_time='.$min_time.';$max_time='.$max_time.";\n\n\n";

		// $jsOutput="";
		foreach ($normPoints as $point) {
			$outputBuffer.='$time='.$point['time'].'; $lat='.$point['lat'].'; $lon='.$point['lon'].
			'; $dis='.$point['dis'].'; $alt='.$point['alt'].'; $altV='.$point['altV'].'; $speed='.$point['speed'].'; $vario='.$point['vario'].";\n";
			$this_tm=$point['time']-$min_time;

//$jsOutput.=sprintf("lt[$this_tm]=%.6f;ln[$this_tm]=%.6f;d[$this_tm]=%d;a[$this_tm]=%d;s[$this_tm]=%0.1f;v[$this_tm]=%.2f;\n"
	//							,$point['lat'],-$point['lon'],$point['dis'],$point['alt'],$point['speed'],$point['vario']);
		}
		$path_igc = dirname($this->getPointsFilename(1));
		if (!is_dir($path_igc)) makeDir($path_igc, 0755);

		// write saned js file
		// writeFile($this->getJsFilename(1),$jsOutput);

		// write saned IGC file
		writeFile($this->getPointsFilename(1),$outputBuffer);

		return 1;

	}

	function getNextPointPos($pointArray,$currentPos){
		for($i=$currentPos+1; $i < count($pointArray);$i++) {
			if ($pointArray[$i]{0}=='B') return $i;
		}
		return $i;
	}

	function getPrevPointPos($pointArray,$currentPos){
		for($i=$currentPos-1;$i>0;$i--) {
			if ($pointArray[$i]{0}=='B') return $i;
		}
		return $i;
	}

	function checkForOLCfile($filename) {
		$lines = file ($filename);
		$line=$lines[0];
		if (  strtoupper(substr($line,0,3))!='OLC' ) return 0; // not OLC file

		$mindist=0;
		if ( preg_match("/&mindist=(\d+)&/",$lines[0],$m) ) {
			$mindist=$m[1];
		}
		// echo "mindist: $mindist		<BR>";

		DEBUG("OLC",255,"First line starts with OLC, will search further");
		// if the first line begins with OLC we have an olc file
		// we got an olc optimized file , get the 5 turnpoints
		if (preg_match(
	   "/&w0bh=([NnSs])&w0bg=(\d+)&w0bm=(\d+)&w0bmd=(\d+)&w0lh=([EeWw])&w0lg=(\d+)&w0lm=(\d+)&w0lmd=(\d+)".
		"&w1bh=([NnSs])&w1bg=(\d+)&w1bm=(\d+)&w1bmd=(\d+)&w1lh=([EeWw])&w1lg=(\d+)&w1lm=(\d+)&w1lmd=(\d+)".
		"&w2bh=([NnSs])&w2bg=(\d+)&w2bm=(\d+)&w2bmd=(\d+)&w2lh=([EeWw])&w2lg=(\d+)&w2lm=(\d+)&w2lmd=(\d+)".
		"&w3bh=([NnSs])&w3bg=(\d+)&w3bm=(\d+)&w3bmd=(\d+)&w3lh=([EeWw])&w3lg=(\d+)&w3lm=(\d+)&w3lmd=(\d+)".
		"&w4bh=([NnSs])&w4bg=(\d+)&w4bm=(\d+)&w4bmd=(\d+)&w4lh=([EeWw])&w4lg=(\d+)&w4lm=(\d+)&w4lmd=(\d+)/",$lines[0],$matches)  ||

		preg_match(
	   "/&fai0bh=([NnSs])&fai0bg=(\d+)&fai0bm=(\d+)&fai0bmd=(\d+)&fai0lh=([EeWw])&fai0lg=(\d+)&fai0lm=(\d+)&fai0lmd=(\d+)".
		"&fai1bh=([NnSs])&fai1bg=(\d+)&fai1bm=(\d+)&fai1bmd=(\d+)&fai1lh=([EeWw])&fai1lg=(\d+)&fai1lm=(\d+)&fai1lmd=(\d+)".
		"&fai2bh=([NnSs])&fai2bg=(\d+)&fai2bm=(\d+)&fai2bmd=(\d+)&fai2lh=([EeWw])&fai2lg=(\d+)&fai2lm=(\d+)&fai2lmd=(\d+)".
		"&fai3bh=([NnSs])&fai3bg=(\d+)&fai3bm=(\d+)&fai3bmd=(\d+)&fai3lh=([EeWw])&fai3lg=(\d+)&fai3lm=(\d+)&fai3lmd=(\d+)".
		"&fai4bh=([NnSs])&fai4bg=(\d+)&fai4bm=(\d+)&fai4bmd=(\d+)&fai4lh=([EeWw])&fai4lg=(\d+)&fai4lm=(\d+)&fai4lmd=(\d+)/",$lines[0],$matches)

		) {
			for($i1=0;$i1<=4;$i1++)  {
				$opt_point[$i1]=sprintf("%02d%02d%03d%s%03d%02d%03d%s",$matches[1+$i1*8+1],$matches[1+$i1*8+2],$matches[1+$i1*8+3],$matches[1+$i1*8+0],
								$matches[1+$i1*8+5],$matches[1+$i1*8+6],$matches[1+$i1*8+7],$matches[1+$i1*8+4]);
			}
			$manual_optimize=1;
			// echo "manual_optimize";
			// print_r($matches);
			// print_r($p);

			//now confirm that the supplied points are indeed in the track.
			$opt_point_num=0;
			foreach($lines as $line) {
				if (strtoupper($line[0])!='B') continue;
				$done=0;
				while(! $done){
					if ( substr($line,7,17)==$opt_point[$opt_point_num] ) { // found the point inside the track
						$opt_point_num++; // search for next turnpoint
						if ($opt_point_num==5) break;
					} else {
						$done=1;
					}
				}
				if ($opt_point_num==5) break;
			}

			//for($i=0;$i<=4;$i++)  {
			//	echo "#".$opt_point[$i]."<br>";
			//}
			if ($opt_point_num==5) {
				//echo "OPTIMIZATION POINTS confirmed";
				DEBUG("OLC",255,"OPTIMIZATION POINTS confirmed");
				// now compute best score
				for($i=0;$i<=4;$i++)  {
					$points[$i]=new gpsPoint("B120000".$opt_point[$i]."A0000000000");
				}
				/*
					Explained by Thomas Kuhlmann

					You have 5 Points:
					Startpoint, First Waypoint, Second Waypoint, Third Waypoint, Endpoint
					given following distances
					a : distance from First to Second Waypoint
					b : distance from Second the Third Waypoint
					c : distance from Third to First Waypoint
					d : distance from End- To Startpoint
					Rule to have a triangle (it may be 20% open):

					  d <= 20% ( a+b+c )
					this is equivalent to:
					  d*5 <= a+b+c

					Now you have to check if this triangle fullfills the 28% FAI rule:
					  ( a >= 28% (a+b+c) )  AND  (b >= 28% (a+b+c))  AND (c>= 28% (a+b+c))
					this is quivalent to (28% = 7/25):
					  (25*a >= 7*(a+b+c))  AND (25*b >= 7*(a+b+c)) AND (25*c >= 7*(a+b+c))
					if using integers for calculation of distances in decimeters,
					 this formular generates no integer to float concerning problems.
				*/
				$a=$points[1]->calcDistance($points[2]);
				$b=$points[2]->calcDistance($points[3]);
				$c=$points[3]->calcDistance($points[1]);
				$d=$points[4]->calcDistance($points[0]);

				// echo "<br> triangle : $a $b $c $d<BR>";
				$u=$a+$b+$c;
				if ($d*5 <= ($u) ) { // we have a triangle
					if ( ( 25*$a >= 7*$u )  && ( 25*$b >= 7*$u ) && ( 25*$c >= 7*$u ) ) {
						$factor1=2; // fai triangle
					} else {
						$factor1=1.75; // open triangle
					}
					$km_triangle=($u-$d)/1000;
					$score_triangle=$km_triangle*$factor1;
				} else { // no triangle
					$km_triangle=0;
					$score_triangle=0;
				}

				//now for straight distance
				$a=$points[0]->calcDistance($points[1]);
				$b=$points[1]->calcDistance($points[2]);
				$c=$points[2]->calcDistance($points[3]);
				$d=$points[3]->calcDistance($points[4]);

				$factor=1.5;
				$km_straight=($a+$b+$c+$d)/1000;
				$score_straight=$km_straight*$factor;
				//echo "$a $b $c $d<BR>";
				//echo "km_straight:$km_straight  score_straight:$score_straight <BR>";
				//echo "km_triangle:$km_triangle  score_triangle:$score_triangle <BR>";

				$str="";
				for($i=0;$i<=4;$i++)
					$str.=$opt_point[$i]."\n";

				// check if $mindist>0 and  the triangle km are < $mindist, then we check
				// if the straight distance km are > $mindist and use that instead
				if ($km_triangle<$mindist) {
					if ($km_straight>=$mindist || $km_triangle==0) $str.="$km_straight\n$score_straight\n$factor\n";
					else $str.="$km_triangle\n$score_triangle\n$factor1\n";
				} else {
					if ($score_triangle>=$score_straight)
						$str.="$km_triangle\n$score_triangle\n$factor1\n";
					else
						$str.="$km_straight\n$score_straight\n$factor\n";
				}

				$str.="\n$mindist";
				writeFile($filename.".olc",$str);
			} // end if confirmed the 5 turnpoints

		} // end if the preg match

		// now write the IGC file stripping the first line
		// there might be a &IGCigcIGC= (maxpunkte) or not (compeGPS)
		// $lines[0]
		if ( preg_match("/&IGCigcIGC=(.*)/",$lines[0],$matches) ) {
			$lines[0]=$matches[1]."\n";
		} else { // just remove the line[0] for the array
			array_shift($lines);
		}

		// done write the stripped file to disk
		$str=implode("",$lines);
		writeFile($filename,$str);

		return 1;
	}


	/****************************************************************************************
	****************************************************************************************

		Core function of analizing an igc

	****************************************************************************************
	****************************************************************************************/
	function getFlightFromIGC($filename) {
		if ( !is_file($filename) ) {
			DEBUG("IGC",1,"getFlightFromIGC: File was not found:$filename<br>");
			return 0;
		}

		set_time_limit (100);
		if ($this->forceBounds) {
			$startTime=$this->START_TIME;
			$endTime=$this->END_TIME;
		}
		$this->resetData();
		if ($this->forceBounds) {
			$this->START_TIME=$startTime;
			$this->END_TIME=$endTime;
		}
		$this->setAllowedParams();
		$this->filename=basename($filename);

		$this->checkForOLCfile($filename);

		$done=0;
		$try_no_takeoff_detection=0;
		
		$tm1=time()+3600*24*30;
		$tm2=0;
		
		while(!$done) {

			$lines = file ($filename);
			$linesNum =count($lines);
			DEBUG("IGC",1,"File has total $linesNum lines<br>");
			$points=0;
			$outputBuffer="";

			// process points
			// filter bad ones
			$p=0;
			$Brecords=0;
			$getPointsNum=5 ; // + 4 points + this one
			for($i=0;$i< count($lines)-10 ;$i++) {
				$pointOK=1;
				$line=trim($lines[$i]);

				if  (strlen($line)==0) continue;
				if  ( $line{0}!='B' ) continue;
				$Brecords++;

				// also check for bad points
				// 012345678901234567890123456789
				// B1522144902558N00848090EV0058400000

				if  ( strlen($line)  < 23  || $line{24}=='V') {
					$lines[$i]{1}='X';
					continue;
				}
				$neighboors=array();
				$nextPointPos=$i;
				for ($t1= 0 ;$t1 <  $getPointsNum ;$t1++ ) {
					$thisPoint=new gpsPoint( trim($lines[$nextPointPos]) ,$this->timezone );
					$neighboors[$t1] = $thisPoint;

					$nextPointPos=$this->getNextPointPos($lines,$nextPointPos);
				} // got all next points

				// find mean values
				$mean_speed=0;
				$mean_vario=0;
				for ($t1= 1 ;$t1 < $getPointsNum ;$t1++ ) {  // for 4 (5-1) points in a row
						// create arrays
						$T_distance[$t1] = $neighboors[$t1]->calcDistance($neighboors[$t1-1]);
						$T_alt[$t1] = $neighboors[$t1]->getAlt();
						$T_deltaseconds[$t1] = $neighboors[$t1]->getTime() -  $neighboors[$t1-1]->getTime() ;
						$T_speed[$t1] = ($T_deltaseconds[$t1])?$T_distance[$t1]*3.6/($T_deltaseconds[$t1]):0.0; /* in km/h */
						if ($T_deltaseconds[$t1]) $T_vario[$t1]=($T_alt[$t1]-$neighboors[$t1-1]->getAlt() ) / $T_deltaseconds[$t1];

						$mean_speed+=$T_speed[$t1];
						$mean_vario+=$T_vario[$t1];
				}
				$mean_speed = $mean_speed/($getPointsNum-1);
				$mean_vario=( $neighboors[$getPointsNum-1]->getAlt() - $neighboors[0]->getAlt() ) / 
							( $neighboors[$getPointsNum-1]->getTime() - $neighboors[0]->getTime()  )	;
				//if ($mean_vario<0) {
				//		DEBUG("IGC",8,"[$Brecords-$p] mean_vario :$mean_vario <0 <br>");
				//}
							
				$data_speed[$i]=$mean_speed; 
				$data_vario[$i]=$mean_vario;			
				// $mean_vario = $mean_vario/($getPointsNum-1); // mean vario is wrong

				if ($neighboors[0]->getTime() < $tm1 ) $tm1=$neighboors[0]->getTime();
				if ($neighboors[0]->getTime() > $tm2 ) $tm2=$neighboors[0]->getTime();
				
				if ( ($neighboors[0]->getTime() - $neighboors[1]->getTime()  ) > 0   )  {  // the next point is more than one hour in the past
						// echo "#"; $pointOK=0;
				}

				if ($T_distance[1] < 0.5 && $T_deltaseconds[1] >2 ) {  // less than 0.5 m distance
					$pointOK=0;
					DEBUG("IGC",8,"[$Brecords-$p] Distance <0.5 m <br>");
					$REJ_T_distance++;

					// we dont go through the other tests
					//echo "{$lines[$i]} =>";
					$lines[$i]{1}='X';
					//echo "{$lines[$i]}<br>";
					continue;
					//echo $T_distance[1]."*<br>";
				}
				if ( abs ($mean_speed - $T_speed[1] ) > 40 ) { // diff more than 40 km/h
					$pointOK=0;
					$REJ_T_mean_speed++;
					DEBUG("IGC",8,"[$Brecords-$p] Mean speed > 40 km/h <br>");
					//echo "@";
				}
				//if ( abs ($mean_vario - $T_vario[1] ) > 6 ) {  // diff more than 6 m/sec
				//	$pointOK=0;
					//echo "#";
				//}
				if ( $T_deltaseconds[1] == 0 ) {
					$pointOK=0;
					$REJ_T_zero_time_diff++;
					DEBUG("IGC",8,"[$Brecords-$p] No time Diff<br>");
				}
				if ( $T_alt[1]   > $this->maxAllowedHeight ) {  $pointOK=0;	$REJ_max_alt++; }
				if ( abs($T_speed[1])  > $this->maxAllowedSpeed ) {
					 $pointOK=0;
					 $REJ_max_speed++;
					 DEBUG("IGC",8,"[$Brecords-$p] > ".abs($T_speed[1])."km/h max allowed speed<br>");
					// echo "S";
				}
				if ( abs($T_vario[1])  > $this->maxAllowedVario ) {  $pointOK=0; $REJ_max_vario++;
					// echo "V";
					 DEBUG("IGC",8,"[$Brecords-$p] > ".abs($T_vario[1]) ."m/sec  > max allowed vario<br>");
				}
				if ( $p<5 && ! $try_no_takeoff_detection && ! $this->forceBounds ) { // first 5 points need special care
					$takeoffMaxSpeed=$this->maxAllowedSpeed *0.5;
					DEBUG("IGC",8,"[$Brecords-$p] TAKEOFF sequence SPEED: ".abs($T_speed[1])." max:$takeoffMaxSpeed<br>");
					if ( abs($T_speed[1])  > $takeoffMaxSpeed ) {
						$pointOK=0;
						$REJ_max_speed_start++;
						// echo "s";
					}
					if ( abs($T_vario[1])  > ($this->maxAllowedVario *0.4) ) {
						$pointOK=0;
						$REJ_max_vario_start++;
						//echo "v";
					}
				}
				if (!$pointOK)  {
					$lines[$i]{1}='X';
				} else  {
					$p++;
				
					if ($p==5) DEBUG("IGC",1,"Passed the strict testing (p=5)<br>");
				}

			}


			DEBUG("IGC",1,"REJ: [$REJ_T_distance] <0.5 distance<br>");
			DEBUG("IGC",1,"REJ: [$REJ_T_zero_time_diff] zero_time_diff<br>");
			DEBUG("IGC",1,"REJ: [$REJ_T_mean_speed] mean_speed diff >40km/h<br>");
			DEBUG("IGC",1,"REJ: [$REJ_max_alt] >max_alt<br>");
			DEBUG("IGC",1,"REJ: [$REJ_max_speed] >max_speed<br>");
			DEBUG("IGC",1,"REJ: [$REJ_max_vario] >max_vario<br>");
			DEBUG("IGC",1,"REJ: [$REJ_max_speed_start] >max_speed_start<br>");
			DEBUG("IGC",1,"REJ: [$REJ_max_vario_start] >max_vario_start<br>");

			DEBUG("IGC",1,"Found $p valid B records out of $Brecords total<br>");

			if ($p>0) {
				$done=1;
			} else if ($Brecords>0 && ( $REJ_T_zero_time_diff/$Brecords > 0.9 ) ) { // more than 90% stopped points
				$lines = file ($filename);
				$done=1;
				$garminSpecialCase=1;
				$p=$Brecords;
				DEBUG("IGC",1,"Many Stopped points, it is a Garmin Special Case<br>");
			} else if (	!$try_no_takeoff_detection ) {
				$try_no_takeoff_detection=1;
				DEBUG("IGC",1,"Will try no_takeoff_detection<br>");
			} else {
				$done=1;
			}

		} // while not done

		//
		if ($p==0)  {
			DEBUG("IGC",1,"NO VALID POINTS FOUND");
			return 0; // no valid points found
		}

		$mod=0;
		if ($p > $this->maxPointNum ){
			$reduceArray=getReduceArray($p ,$this->maxPointNum);
			// print_r($recudeArray);
			$mod=count($reduceArray);
			// $mod= ceil( $p / $this->maxPointNum );
		}
		DEBUG("IGC",1,"will use a reduce array of length $mod<br>");
		
		$duration=$tm2-$tm1;
		$intervalSecs=round($duration/$p);
		
		// echo "<hr>good points: $p duration:$duration, intervalSecs:$intervalSecs<hr>";
		
		$pointsNeededForTakeoff=5;
		
		if ($intervalSecs>=8) {			
			$pointsNeededForTakeoff=2;
		}
		
		
		$alreadyInPoints=0;
		$stopReadingPoints=0;
		$this->timezone=1000;
		$day_offset =0;
		$foundNewTrack=0;

		$slow_points=0;
		$slow_points_dt=0;
		$stillOnGround=1;

		$tmpDate=0;

		foreach($lines as $iii=>$line) {
			if ($foundNewTrack) break;
			$outputLine=$line;
			$line=trim($line);
			if  (strlen($line)==0) continue;

			// if (strtoupper(substr($line,0,1)) !="B" ) echo "@$line<BR>";

			if (strtoupper(substr($line,0,3)) =="OLC"  ) continue; // it is an olc file , dont put the OLC... line in to the saned file

			if (strtoupper(substr($line,0,5)) =="HFDTE"  || strtoupper(substr($line,0,5)) =="HPDTE"  ) {  // HFDTE170104  OR HPDTE310805
				$line = str_replace('DATE:', '', strtoupper($line));
				if ( $alreadyInPoints && $points>0 ) {
					if ( $prevPoint->gpsTime < 86200 ) {
						// if last good point is > 86200 (200 secs before day change at 86400) we dont treat this as a new track
						$stopReadingPoints=1;
						DEBUG("IGC",1,"[$points] $line<br>");
						DEBUG("IGC",1,"[$points] Found a new track (NEW HFDTE)<br>");
					}
				} else {

					$this->DATE=substr($line,5,6);
					$yr_last=substr($this->DATE,4,2);
					// case of YY=0 (1 digit) HFDTE08070
					if ($yr_last=="0") $yr_last="00";
					if ($yr_last > 80 ) $yr="19".$yr_last;
					else $yr="20".$yr_last;
					$this->DATE=$yr."-".substr($this->DATE,2,2)."-".substr($this->DATE,0,2);

					$alreadyInPoints=1;
				}
			} else if (strtoupper(substr($line,0,13)) =="HFTZOTIMEZONE" ) {  // HFTZOTimezone:3 OR HFTZOTimezone:-8
				$this->timezone=substr($line,14)+0;
				// echo $this->timezone."#^^";
			} else if (strtoupper(substr($line,2,13)) =="GTYGLIDERTYPE" ) {
				// HOGTYGLIDERTYPE: Gradient Bliss 26  OR  HPGTYGliderType:Gradient Nevada
				if (!$this->glider ) $this->glider=trim(substr($line,16));
				// HFGTYGLIDERTYPE
				// HOGTYGLIDERTYPE
			} else 	if ($line{0}=='B' ) {
				if ($stopReadingPoints ) continue;
				if ($line{1}=='X') {
					DEBUG("IGC",1,"[$points] BAD : $line<br>");
					continue ; // MARKED BAD from BEFORE
				}
				if  ( strlen($line) < 23 || strlen($line) > 100  ) continue;


				if  ($points==0)  { // first point
					//				echo "######## first point <br>";
					$firstPoint=new gpsPoint($line,$this->timezone);
					if ($this->timezone==1000) { // no timezone in the file
						// echo "calc timezone<br>";
						$this->timezone= getUTMtimeOffset( $firstPoint->lat,$firstPoint->lon, $this->DATE );
						$this->timezone= getTZ( $firstPoint->lat,$firstPoint->lon, $this->DATE );
						// echo 	$this->timezone;
						$firstPoint->timezone=$this->timezone;

					}

					$tmpTime=$firstPoint->gpsTime + $this->timezone*3600;
					// Now also check if we are one day minus (for US flights )
					if ( $tmpTime < 0  && $tmpDate==0 ) { // one day before!
						$this->DATE=dates::moveDaysFromDate($this->DATE,-1);
						$tmpDate=1;
					}
					// take care of day change for timezones in australia/nz etc
					if (  $tmpTime > 86400  && $tmpDate==0 )  { // UTC date in the igc file  needs to be +1
						$this->DATE=dates::moveDaysFromDate($this->DATE,1);
						$tmpDate=1;
					}

					// sanity checks
					if ( $firstPoint->getAlt()  > $this->maxAllowedHeight ) continue;
					// echo "times: ".$firstPoint->gpsTime.", ".$firstPoint->getTime()." start_time: ".$this->START_TIME ."<BR> ";
					if ( $this->forceBounds && ! $this->checkBound($firstPoint->getTime() ) ) continue; // not inside time window

					//$this->FIRST_POINT=$line;
					$this->firstPointTM= $firstPoint->gpsTime;
					$this->firstLat=$firstPoint->lat();
					$this->firstLon=$firstPoint->lon();

					$this->TAKEOFF_ALT= $firstPoint->getAlt();
					$this->MIN_ALT= $firstPoint->getAlt();
					if ( ! $this->forceBounds) $this->START_TIME = $firstPoint->getTime();
					$prevPoint=new gpsPoint($line,$this->timezone);
					$prevPoint2=new gpsPoint($line,$this->timezone);
				} else  {
					$lastPoint=new gpsPoint($line,$this->timezone);
					$lastPoint->gpsTime+=$day_offset;

					if ( $this->forceBounds && ! $this->checkBound($lastPoint->getTime() ) ) {
						 $lastPoint=$prevPoint;
						 continue; // not inside time window
					}

					$time_diff= $lastPoint->getTime() - $prevPoint->getTime() ;
					$time_diff2= $lastPoint->getTime() - $prevPoint2->getTime() ;
					
					// echo "time diff: $time_diff # $line<br>";
					if (  $time_diff < 0 && $time_diff > -36000  )  { // if the time is less than 10 hours in the past  we just ignore it
                    		// $day_offset = 86400; // if time seems to have gone backwards, add a day
							DEBUG("IGC",1,"[$points] $line<br>");
							DEBUG("IGC",1,"[$points] Point in the past<br>");
							continue;
        		    } else 	if ( $time_diff < 0  )  {  // CHANGING DAY , means the flight is at night
						$lastPoint=$prevPoint;
						$foundNewTrack=1;
						DEBUG("IGC",1,"[$points] $line<br>");
						DEBUG("IGC",1,"[$points] Flight at night ????<br>");
						continue;
        		    } else 	if (  $time_diff > $this->max_allowed_time_gap  )  {  // found time gap
						// if we are forceBounds check to see if inside window
						if ( ( $this->forceBounds && ! $this->checkBound($lastPoint->getTime() )  ) || !$this->forceBounds ) {
							// if we are still on the ground  we dont care about time gap
							if ( !$stillOnGround ) {
								// not inside time window  OR not checking go ahead
								$lastPoint=$prevPoint;
								$foundNewTrack=1;
								DEBUG("IGC",1,"[$points] $line<br>");
								DEBUG("IGC",1,"[$points] Found a new track (Time diff of $time_diff secs)<br>");
								continue;
							}
						} else { // inside the window, forced to continue

						}
					}

					$this->LAST_POINT=$line;

					// compute some things
					$tmp = $lastPoint->calcDistance($prevPoint);
					$alt = $lastPoint->getAlt();
					$deltaseconds = $lastPoint->getTime() - $prevPoint->getTime() ;
					$speed = ($deltaseconds)?$tmp*3.6/($deltaseconds):0.0; /* in km/h */
					if ($deltaseconds) $vario=($alt-$prevPoint->getAlt() ) / $deltaseconds;

					if (!$garminSpecialCase && ! $this->forceBounds) {
						if ( ($fast_points>=$pointsNeededForTakeoff || $fast_points_dt>30) && $stillOnGround) { // found 5 flying points or 30 secs
							$stillOnGround=0;
							DEBUG("IGC",1,"[$points] $line<br>");
							DEBUG("IGC",1,"[$points] Found Takeoff <br>");
						}

						if ($stillOnGround) { //takeoff scan
							// either speed >= 15 or if we already have 2 fast points settle with speed>=10
							if ($speed >= 15 ||  ( $speed >= 10 && $fast_points>=2 ) ) {
								$fast_points++;
								$fast_points_dt+=$deltaseconds;
								DEBUG("IGC",1,"[$points] $line<br>");
								DEBUG("IGC",1,"[$points] Found a fast speed point <br>");
							} else { // reset takeoff scan
								DEBUG("IGC",1,"[$points] $line<br>");
								DEBUG("IGC",1,"[$points] takeoff scan: speed: $speed  time_diff: $time_diff<br>");

								$fast_points=0;
								$fast_points_dt=0;
							}
							$points=0;
							continue;
						} else { //landing  scan
							if ($speed < 5 ) {
								$slow_points++;
								$slow_points_dt+=$deltaseconds;
								DEBUG("IGC",1,"[$points] $line<br>");
								DEBUG("IGC",1,"[$points] Found a slow speed point (speed, dt)=($speed,$deltaseconds)<br>");
							} else {
								$slow_points=0;
								$slow_points_dt=0;
							}
						}

						// found landing (5 stopped points and >2mins) or 5 mins (300 secs)
						if ( ($slow_points>$pointsNeededForTakeoff && $slow_points_dt>180)  || $slow_points_dt>300) {
							$foundNewTrack=1;
							DEBUG("IGC",1,"[$points] $line<br>");
							DEBUG("IGC",1,"[$points] Found a new track  /landing <br>");
						}

					}

					// sanity checks
					if ( $deltaseconds == 0 && !$garminSpecialCase) {  continue; }
					if ( $alt    > $this->maxAllowedHeight ) {  continue; }
					if ( abs($speed)  > $this->maxAllowedSpeed ) {  continue; }
					if ( abs($vario)  > $this->maxAllowedVario ) {  continue; }

					$takeoffDistance=$lastPoint->calcDistance($firstPoint);
					if ($takeoffDistance > $this->LINEAR_DISTANCE )  $this->LINEAR_DISTANCE=$takeoffDistance;

					if ($time_diff2>10) {
						$tmp = $lastPoint->calcDistance($prevPoint2);
						$alt = $lastPoint->getAlt();
						$deltaseconds = $time_diff2;
						$speed = ($deltaseconds)?$tmp*3.6/($deltaseconds):0.0; /* in km/h */
						if ($deltaseconds) $vario=($alt-$prevPoint2->getAlt() ) / $deltaseconds;
						$prevPoint2=new gpsPoint($line,$this->timezone);
						$prevPoint2->gpsTime+=$day_offset;
						
						// update maximum speed
						if ($speed > $this->MAX_SPEED)  $this->MAX_SPEED=$speed;
						$this->MEAN_SPEED +=$speed;
						$MEAN_SPEED_POINTS++;
						// UPDATE MIN-MAX VARIO
						if ($vario > $this->MAX_VARIO) $this->MAX_VARIO=$vario;
						if ($vario < $this->MIN_VARIO) $this->MIN_VARIO=$vario;
						
					}
					
					//$speed=$data_speed[$iii-1]+0; 
					// $vario=$data_vario[$iii-1]+0;					
					
					// UPDATE MIN-MAX ALT
					if ($alt> $this->MAX_ALT) $this->MAX_ALT=$alt;
					if ($alt< $this->MIN_ALT) $this->MIN_ALT=$alt;

					
					// end computing
					$prevPoint=new gpsPoint($line,$this->timezone);
					$prevPoint->gpsTime+=$day_offset;
					if ($mod>=1)  {
						if ( $reduceArray[ $points % $mod]  == 0  ) $outputLine="";
					}
				}
				$points++;
			}  // end else
			$outputBuffer.=$outputLine;

		} // end main loop

		// echo "<HR>MIN VARIO".$this->MIN_VARIO."<HR>";
		//
		if ($stillOnGround && $this->LINEAR_DISTANCE < 50 )  {
			DEBUG("IGC",1,"NO TAKEOFF FOUND: ");
			return 0; // no valid points found
		}

		$path_igc=dirname($this->getIGCFilename(1));
		if ( !is_dir($path_igc) ) {
			makeDir($path_igc,0755);
		}

		/*write the full saned file */
		$fullSanedFile='';
		foreach($lines as $line) {
			$line=trim($line);
			if  (strlen($line)==0) continue;
			if ($line{0}=='B' && $line{1}=='X') continue ; // MARKED BAD from BEFORE
			// if  ( strlen($line) < 23 || strlen($line) > 100  ) continue;
			$fullSanedFile.=$line."\n";
		}


		if (! writeFile($this->getIGCFilename(2),$fullSanedFile) ) {
		   echo "Problem writing to file (".$this->getIGCFilename(2).")";
		}
		// echo "<HR><HR>". $this->getIGCFilename(2) .strlen($fullSanedFile)."<HR><HR>";
		/* done wrting the full saned file */

		// write saned IGC file
		if (! writeFile($this->getIGCFilename(1),$outputBuffer) ) {
		   echo "Problem writing to file (".$this->getIGCFilename(1).")";
		}
		// done write saned IGC file

		if ($lastPoint) {
			$this->lastPointTM=$lastPoint->gpsTime;
			$this->lastLon=$lastPoint->lon();
			$this->lastLat=$lastPoint->lat();

			$this->LANDING_ALT= $lastPoint->getAlt();
			$this->END_TIME =   $lastPoint->getTime();
		} else {
			$this->lastPointTM=0;
			$this->lastLon=0;
			$this->lastLat=0;

			$this->LANDING_ALT= 0;
			$this->END_TIME =   0;
		}

		$this->DURATION =   $this->END_TIME - $this->START_TIME ;
		$this->MEAN_SPEED = $this->MEAN_SPEED / $MEAN_SPEED_POINTS;

		return 1;
	} // end function getFlightFromIGC()

	function checkAirspace($updateFlightInDB=0) {
		global $CONF_airspaceChecks;
		if (!$CONF_airspaceChecks) return 1;

		require_once dirname(__FILE__).'/FN_airspace.php';
		set_time_limit (140);

		$resStr=checkAirspace($this->getIGCFilename());
		DEBUG("checkAirspace",1,"Airspace check:");
		if (!$resStr) {
			DEBUG("checkAirspace",1,"CLEAR");
			$this->airspaceCheck=1;
			if ( $this->airspaceCheckFinal==0) $this->airspaceCheckFinal=1; // only set it if undefined
			$this->airspaceCheckMsg="";
		} else  {
			DEBUG("checkAirspace",1,$resStr);
			$this->airspaceCheck=-1;
			if ( $this->airspaceCheckFinal==0) $this->airspaceCheckFinal=-1; // only set it if undefined
			$this->airspaceCheckMsg=$resStr;
		}

		if ( $updateFlightInDB ) $this->putFlightToDB(1);
	}

	function validate($updateFlightInDB=1) {
		global $CONF_validation_server_url, $CONF_use_custom_validation, $DBGlvl;
		global $baseInstallationPath,$CONF_abs_path,$CONF;

		global $alreadyValidatedInPage;
		if ($alreadyValidatedInPage) return;
		$alreadyValidatedInPage=1;

		set_time_limit (240);
		$customValidationCodeFile=dirname(__FILE__)."/site/CODE_validate_custom.php";
		if (  $CONF_use_custom_validation && file_exists($customValidationCodeFile) ) { // we expect the result on $ok
			include $customValidationCodeFile;
		} else { //standard leoanrdo validation -> the server not yet working
	// trac#40
	//		$IGCwebPath=urlencode($baseInstallationPath."/").$this->getIGCRelPath(0); // validate original file
			$IGCwebPath=$this->getIGCRelPath(0); // validate original file
			if(strpos($IGCwebPath,"http")=== FALSE){
				$IGCwebPath=urlencode('https://'.$_SERVER['SERVER_NAME'].$baseInstallationPath."/").$this->getIGCRelPath(0);
			}
			//error_log(print_r($IGCwebPath,true));
			$fl=  $CONF['validation']['server_url']."?file=".$IGCwebPath;
			if ($DBGlvl) $fl.="&dbg=1";
			DEBUG("VALIDATE_IGC",1,"Will use URL: $fl<BR>");
			$contents =	explode("\n",fetchURL($fl,30));
			if (!$contents) return 0;

			$ok=-1;
			foreach ( $contents as $lnum=>$line) {
				DEBUG("VALIDATE",64,"$line");
			
				if (trim($contents[$lnum])=="VALI:OK") {
					$ok=1;
					$valStr=trim($contents[$lnum+1]);
				}
				if (trim($contents[$lnum])=="VALI:NOK") {
					$ok=-1;
					$valStr=trim($contents[$lnum+1]);
				}
				if (trim($contents[$lnum])=="VALI:NNOK") {
					$ok=-2;
					$valStr=trim($contents[$lnum+1]);
				}
			}
		}

		//	 -1 => invalid 0 => not yet proccessed  1 => valid
		// force ok=1 till we have a validation server ready
		// $ok=1;

		$this->grecord=$ok;
		$this->validated=$ok;
		$this->validationMessage=$valStr;
		if ( $updateFlightInDB ) $this->putFlightToDB(1);

		return $ok;
	}

	function checkSanedFiles() {
		// do some basic check for saned igc file
		if (!is_file($this->getIGCFilename(1)) || !is_file($this->getIGCFilename(2))   ) {
			// we have to create it
			DEBUG('checkSanedFiles',1,"Saned files are missing for flight: ".$this->flightID.", we will create it<BR>");
			if (!is_file($this->getIGCFilename(0) ) ) {
				DEBUG('checkSanedFiles',1,"Original file is missing for flight: ".$this->flightID." <BR>".$this->getIGCFilename(0)."<BR>");
				require_once dirname(__FILE__).'/CL_actionLogger.php';
				$log=new Logger();
				$log->userID  	=$this->userID;
				$log->ItemType	=1 ; // flight;
				$log->ItemID	= ( ( $this->serverID && $this->serverID!=$CONF_server_id ) ?$this->original_ID:$this->flightID ); // 0 at start will fill in later if successfull
				$log->ServerItemID	=  ( $this->serverID?$this->serverID:$CONF_server_id);
				$log->ActionID  = 8 ;  //1  => add  2  => edit , 8=score flight;
				$log->ActionXML	= "{ }";
				$log->Modifier	= 0;
				$log->ModifierID= 0;
				$log->ServerModifierID =0;
				$log->Result = 0;
				if (!$log->Result) $log->ResultDescription ="IGC file misssing";
				if (!$log->put()) echo "Problem in logger<BR>";
				return 0;
			}
			$this->getFlightFromIGC($this->getIGCFilename(0) ) ;
			if (!is_file($this->getIGCFilename(1)) || !is_file($this->getIGCFilename(2)) ) {
				 //saned file could no be created!!!
				echo "Saned files could no be created for flight: ".$this->flightID." <BR>".$this->getIGCFilename(0)."<BR>";
				require_once dirname(__FILE__).'/CL_actionLogger.php';
				$log=new Logger();
				$log->userID  	=$this->userID;
				$log->ItemType	=1 ; // flight;
				$log->ItemID	= ( ( $this->serverID && $this->serverID!=$CONF_server_id ) ?$this->original_ID:$this->flightID ); // 0 at start will fill in later if successfull
				$log->ServerItemID	=  ( $this->serverID?$this->serverID:$CONF_server_id);
				$log->ActionID  = 8 ;  //1  => add  2  => edit , 8=score flight;
				$log->ActionXML	= "{ }";
				$log->Modifier	= 0;
				$log->ModifierID= 0;
				$log->ServerModifierID =0;
				$log->Result = 0;
				if (!$log->Result) $log->ResultDescription ="Saned file could no be created";
				if (!$log->put()) echo "Problem in logger<BR>";
				return 0;
			}
		}

		return 1;
	}

	function computeScore() {
		global $OLCScoringServerUseInternal,$OLCScoringServerPath, $scoringServerActive , $OLCScoringServerPassword;
		global $baseInstallationPath,$CONF_allow_olc_files,$CONF,$CONF_server_id;

		if (! $scoringServerActive) return 0;

		set_time_limit (240);

		// do some basic check for saned igc file
		if (! $this->checkSanedFiles() ) {
			return 0;
		}

		$flightScore=new flightScore($this->flightID);
		if ($OLCScoringServerUseInternal ) {
			$results=$flightScore->getScore( $this->getIGCFilename(1) ,1  );
		} else {
			$results=$flightScore->getScore( $this->getIGCRelPath(1) , 0  );
		}
		$flightScore->parseScore($results);

		// make a second pass
		$flightScore->computeSecondPass($this->getIGCFilename(2));

		// now is the time to search for the OLC files, manually optimization
		// and 'inject' these values into the $flightScore object
		$manualScore=0;

		if ($CONF_allow_olc_files) {
			// see if there is an OLC file present
			$OLCfilename=$this->getIGCFilename().".olc";

			if (file_exists($OLCfilename) ) {
				DEBUG("OLC_SCORE",1,"OLC pre-optimized is used instead");

				$lines=file($OLCfilename);
	/*
	4624328N00805128E
	4632522N00819212E
	4617978N00733326E
	4609910N00755225E
	4609910N00755225E
	118.68765887809
	207.70340303666
	1.75
	*/
				$this->FLIGHT_KM=$lines[5]*1000;
				$this->FLIGHT_POINTS=$lines[6]+0;

				//$manualScoreInfo['FLIGHT_KM']=$lines[5]*1000;
				//$manualScoreInfo['FLIGHT_POINTS']=$lines[6]+0;

				$factor=$lines[7]+0;
				if ($factor==1.5) 	$this->BEST_FLIGHT_TYPE="FREE_FLIGHT";
				if ($factor==1.75) 	$this->BEST_FLIGHT_TYPE="FREE_TRIANGLE";
				if ($factor==2) 	$this->BEST_FLIGHT_TYPE="FAI_TRIANGLE";

				DEBUG('OLC_SCORE',1,"MANUAL FLIGHT_KM: ".$this->FLIGHT_KM."<BR>");
				DEBUG('OLC_SCORE',1,"MANUAL FLIGHT_POINTS: ".$this->FLIGHT_POINTS."<BR>");
				DEBUG('OLC_SCORE',1,"MANUAL BEST_FLIGHT_TYPE: ".$this->BEST_FLIGHT_TYPE."<BR>");

				$manualScoreTP=array();
				for($i=0;$i<5;$i++) {
				    //              01234567890123456
					// must convert 4624328N00805128E to N54:54.097 W02:40.375
					$str=substr($lines[$i],7,1) .substr($lines[$i],0,2).':'.substr($lines[$i],2,2).'.'.substr($lines[$i],4,3).' '.
						 substr($lines[$i],16,1).substr($lines[$i],8,3).':'.substr($lines[$i],11,2).'.'.substr($lines[$i],13,3);
					DEBUG('OLC_SCORE',1,"MANUAL TurnPoint ".($i+1).": $str<BR>");
					// $manualScoreTP[$i+1]=$str;
					$manualScoreTP[$i+1]=sprintf("B%02d%02d%02d%sA0000000000",00,00,00,$lines[$i]);

				}

				$defaultMethodID= $CONF['scoring']['default_set'];
				$defaultScore=$flightScore->scores[$defaultMethodID];
				$this->autoScore=$defaultScore['bestScore'];
				//echo "<pre>";
				//print_r($flightScore->scores);
				//echo "</pre>";

				foreach ( $flightScore->scores as $methodID=>$scoreForMethod) {
					$distance=$this->FLIGHT_KM/1000;
					$flightScore->scores[$methodID][$this->BEST_FLIGHT_TYPE]['distance']=$distance;
					$flightScore->scores[$methodID][$this->BEST_FLIGHT_TYPE]['score']=
							$distance * $CONF['scoring']['sets'][$methodID]['types'][$this->BEST_FLIGHT_TYPE] ;
					for($j=1;$j<=5;$j++) {
						$flightScore->scores[$methodID][$this->BEST_FLIGHT_TYPE]['tp'][$j]=$manualScoreTP[$j];
					}

					// update the best score, in case we have a new bestScoreType
					foreach($flightScore->scores[$methodID] as $scoreType=>$scoreDetails ) {
						//echo "<h1>$scoreType</h1>";
						if (!is_array($scoreDetails) ) { continue; }
						//echo $scoreDetails['score']." > ".$scoreForMethod['bestScore']." <BR>";
						if ($scoreDetails['score'] > $scoreForMethod['bestScore'] ) {
							//echo "FOUND BETTER SCORE !!!!!!!!!<BR><BR>";
							$flightScore->scores[$methodID]['bestScore']=$scoreDetails['score'];
							$flightScore->scores[$methodID]['bestScoreType']=$this->BEST_FLIGHT_TYPE;
							$flightScore->scores[$methodID]['bestDistance']=$scoreDetails['distance'];

						}
					}
				}
				//echo "<pre>";
				//print_r($flightScore->scores);
				//echo "</pre>";

				$manualScore=1;

			}
		}

		//put also in scores table, the flight is sure to be present in flights table
		$flightScore->putToDB(1,1);

		$defaultMethodID= $CONF['scoring']['default_set'];
		$defaultScore=$flightScore->scores[$defaultMethodID];

		$this->MAX_LINEAR_DISTANCE=$flightScore->MAX_LINEAR_DISTANCE;
		if ($manualScore) {
			// $this->autoScore=$defaultScore['bestScore'];
		} else {
			$this->BEST_FLIGHT_TYPE	=$defaultScore['bestScoreType'];
			$this->FLIGHT_POINTS	=$defaultScore['bestScore'];
			$this->FLIGHT_KM		=$defaultScore[ $defaultScore['bestScoreType'] ]['distance']*1000;
		}

		require_once dirname(__FILE__).'/CL_actionLogger.php';
		$log=new Logger();
		$log->userID  	=$this->userID;
		$log->ItemType	=1 ; // flight;
		$log->ItemID	= ( ( $this->serverID && $this->serverID!=$CONF_server_id ) ?$this->original_ID:$this->flightID ); // 0 at start will fill in later if successfull
		$log->ServerItemID	=  ( $this->serverID?$this->serverID:$CONF_server_id);
		$log->ActionID  = 8 ;  //1  => add  2  => edit , 8=score flight;
		$log->ActionXML	= "{\n". $flightScore->toSyncJSON()."\n}";
		$log->Modifier	= 0;
		$log->ModifierID= 0;
		$log->ServerModifierID =0;
		$log->Result = 1;
		// if (!$log->Result) $log->ResultDescription ="Problem in puting flight to DB $query";
		if (!$log->put()) echo "Problem in logger<BR>";
/*

		if ($OLCScoringServerUseInternal ) {
			$file=$this->getIGCFilename(1);

			$path=dirname( __FILE__ ).'/server';
			$igcFilename=tempnam($path."/tmpFiles","IGC.");  //urlencode($basename)
			@unlink($igcFilename);

			$lines=file($file);

			$cont="";
			foreach($lines as $line) {
				$cont.=$line;
			}


			if (!$handle = fopen($igcFilename, 'w')) exit;
			if (!fwrite($handle, $cont))    exit;
			fclose ($handle);

			@chmod ($path."/olc", 0755);
			if ($CONF['os']=='windows') $olcEXE='olc.exe';
			else $olcEXE='olc';

			$cmd=$path."/$olcEXE $igcFilename";
			DEBUG('OLC_SCORE',1,"cmd=$cmd");
			exec($cmd,$res);

			DEBUG('OLC_SCORE',1,"result has ".count($res)." lines<BR>");
			$contents=array();
			foreach($res as $line) {
					DEBUG('OLC_SCORE',1,$line.'<BR>');
					if (substr($line,0,4)=="OUT ") {
						// echo substr($line,4)."\n";
						$contents[]=substr($line,4);
					}
			}

			@unlink($igcFilename);

		} else {
			$IGCwebPath=urlencode("http://".$_SERVER['SERVER_NAME'].$baseInstallationPath."/").$this->getIGCRelPath(1); // score saned file

			$fl= $OLCScoringServerPath."?pass=".$OLCScoringServerPassword."&file=".$IGCwebPath;
			DEBUG("OLC_SCORE",1,"Will use URL: $fl<BR>");
			//$contents = file($fl);
			$contents =	explode("\n",fetchURL($fl,40));
			if (!$contents) return;
		}

		$turnpointNum=1;
		foreach(  $contents  as $line) {
			if (!$line) continue;
			DEBUG("OLC_SCORE",1,"LINE: $line<BR>\n");
			$var_name  = strtok($line,' ');
			$var_value = strtok(' ');
			if ($var_name{0}=='p' ) {
				// sample line
				// p0181 12:45:43 N53:20.898 W 1:48.558
				// p0181 12:45:43 N53:20.898 W12:48.558
				preg_match("/.+ .+ ([NS][ \d][^ ]+) ([WE][ \d][^ ]+)/i",$line,$line_parts);
				$var_name="turnpoint".$turnpointNum;
				$lat= str_replace(" ","0",trim($line_parts[1]));
				$lon= str_replace(" ","0",trim($line_parts[2]));
				$var_value =$lat." ".$lon;
				$turnpointNum++;
			}

			if (! $manualScore) {
				$this->$var_name=trim($var_value);
			} else {
				if ($var_name=='MAX_LINEAR_DISTANCE') $this->$var_name=trim($var_value);
				if ($var_name=='FLIGHT_POINTS') $this->autoScore=trim($var_value);
			}

			DEBUG("OLC_SCORE",1,"#".$var_name."=".$var_value."#<br>\n");
		}
		*/


	//		if (! $manualScore) $this->FLIGHT_KM=$this->FLIGHT_KM*1000;

	}

	function makeScoreLogEntry(){
		global $CONF_server_id;

		$flightScore=new flightScore($this->flightID);
		$flightScore->getFromDB();

		require_once dirname(__FILE__).'/CL_actionLogger.php';
		$log=new Logger();
		$log->userID  	=$this->userID;
		$log->ItemType	=1 ; // flight;
		$log->ItemID	= ( ( $this->serverID && $this->serverID!=$CONF_server_id ) ?$this->original_ID:$this->flightID ); // 0 at start will fill in later if successfull
		$log->ServerItemID	=  ( $this->serverID?$this->serverID:$CONF_server_id);
		$log->ActionID  = 8 ;  //1  => add  2  => edit , 8=score flight;
		$log->ActionXML	= "{\n". $flightScore->toSyncJSON()."\n}";
		$log->Modifier	= 0;
		$log->ModifierID= 0;
		$log->ServerModifierID =0;
		$log->Result = 1;
		// if (!$log->Result) $log->ResultDescription ="Problem in puting flight to DB $query";
		if (!$log->put()) echo "Problem in logger<BR>";
	}

	function getMapFromServerDEM($num=0) {
		global  $CONF;

		 $forceRefresh=1;

		$filename=$this->getMapFilename();
		if ( is_file($filename) && ! $forceRefresh ) {
			return;
		}

		// if no file exists do the proccess now
		if ( ! is_file($this->getPointsFilename(1) ) || $forceRefresh ) $this->storeIGCvalues();

		$lines = file($this->getPointsFilename(1)); // get the normalized with constant time step points array
		if (!$lines) return;
		$i = 0;

		$jsTrack['max_alt']=0;
		$jsTrack['min_alt']=100000;

		$min_lat=1000;
		$max_lat=-1000;
		$min_lon=1000;
		$max_lon=-1000;

		for($k=3;$k< count($lines);$k++){
			$line = trim($lines[$k]);
			if (strlen($line) == 0) continue;
			eval($line);
			$lon=-$lon;
			if ( $lat  > $max_lat )  $max_lat =$lat  ;
			if ( $lat  < $min_lat )  $min_lat =$lat  ;
			if ( $lon  > $max_lon )  $max_lon =$lon  ;
			if ( $lon  < $min_lon )  $min_lon =$lon  ;
			$lat_arr[]=$lat;
			$lon_arr[]=$lon;
		}
/*
		$max_lat+=0.01;
		$max_lon+=0.01;

		$min_lat-=0.01;
		$min_lon-=0.01;
*/
		$totalWidth1=calc_distance($min_lat, $min_lon,$min_lat, $max_lon);
		$totalWidth2=calc_distance($max_lat, $min_lon,$max_lat, $max_lon);
		$totalWidth=max($totalWidth1,$totalWidth2);

		$totalHeight1=calc_distance($min_lat, $min_lon,$max_lat, $min_lon);
		$totalHeight2=calc_distance($min_lat, $max_lon,$max_lat, $max_lon);
		$totalHeight=max($totalHeight1,$totalHeight2);

		if ($totalWidth> $totalHeight ) {
			// Landscape  style
			DEBUG("MAP",1,"Landscape style <BR>");
			DEBUG("MAP",1,"totalWidth: $totalWidth, totalHeight: $totalHeight, totalHeight/totalWidth: ".( $totalHeight / $totalWidth)."<br>");
			// if ( $totalHeight / $totalWidth < 3/4 ) $totalHeight = (3/4) *  $totalWidth ;
			$mapWidthPixels=600;
			$mapHeightPixels=$mapWidthPixels * ( $totalHeight / $totalWidth);

		} else {
			// portait style
			DEBUG("MAP",1,"Portait style <BR>");
			DEBUG("MAP",1,"totalWidth: $totalWidth, totalHeight: $totalHeight, totalWidth/totalHeight: ".( $totalWidth / $totalHeight)."<br>");
			// if ( $totalWidth  / $totalHeight < 3/4 )  $totalWidth  = (3/4) * $totalHeight ;
			$mapWidthPixels=300;
			$mapHeightPixels=$mapWidthPixels * ( $totalHeight / $totalWidth);
		}

		DEBUG("MAP",1,"MAP  min_lat: $min_lat, min_lon: $min_lon, max_lat: $max_lat, max_lon: $max_lon <BR>");

		require_once dirname(__FILE__).'/CL_hgt.php';


		$latStep= ($max_lat-$min_lat) /  $mapHeightPixels;
		$lonStep= ($max_lon-$min_lon) /  $mapWidthPixels ;


		$max_ground_elev  =0;

		$lon=$min_lon ;
		for ($x=0;$x<$mapWidthPixels;$x++) {

			$lat=$min_lat ;
			for ($y=0;$y<$mapHeightPixels;$y++) {

				// echo "lat:$lat lon:$lon<BR>";

				// $lon=$min_lon+ $x * ($max_lon-$min_lon) /  $mapWidthPixels;
				// $lat=$min_lat+ $y * ($max_lat-$min_lat) / $mapHeightPixels;
				$alt=hgt::getHeight($lat,$lon);
				//echo "$alt#";
				if ($alt >$max_ground_elev  ) $max_ground_elev  =$alt;
				$mapEvelGnd[$x][$y]=$alt;

				// $mapLatLon[$x][$y]="$lat,$lon";
				$lat+=$latStep;
			}
			$lon+=$lonStep;
		}

		// print_r($mapEvelGnd);
		// print_r($mapLatLon);

  		$img=imagecreatetruecolor($mapWidthPixels,$mapHeightPixels);

		$backColor =imagecolorallocate ($img,214,223,209);
		// $backColor =imagecolorallocate ($this->img,73,76,50);
		if ($backColor==-1) echo "FAILED TO allocate new color<br>";
		// imagefill($this->img,0,0, 1 );

		imagefilledrectangle($img, 0, 0, $mapWidthPixels-1, $mapHeightPixels-1, $backColor);

        // Allocate the color map
        InterpolateRGB($colorScale, RGB(  0,   0, 110), RGB(  0,   0, 110),   0, 255);
        InterpolateRGB($colorScale, RGB(  0, 100,   0), RGB(180, 180,  50),   1,  60);
        InterpolateRGB($colorScale, RGB(180, 180,  50), RGB(150, 110,  50),  60, 100);
        InterpolateRGB($colorScale, RGB(150, 110,  50), RGB(150, 150, 150), 100, 150);
        InterpolateRGB($colorScale, RGB(150, 150, 150), RGB(255, 255, 255), 150, 200);
        InterpolateRGB($colorScale, RGB(255, 255, 255), RGB(255, 255, 255), 200, 253);
        InterpolateRGB($colorScale, RGB(  0,   0,   0), RGB(  0,   0,   0), 254, 255);

        AllocateColorMap($img, $colorScale, $cMap);

		$factor= 256/$max_ground_elev;
		for ($x=0;$x<$mapWidthPixels;$x++) {
			for ($y=0;$y<$mapHeightPixels;$y++) {
				$color =$cMap[$mapEvelGnd[$x][$mapHeightPixels-$y] * $factor  - 1];
				imagesetpixel ( $img, $x, $y, $color );
			}
		}

		$track_color = imagecolorallocate($img, 255,0,0);

		for ($i=0;$i<count($lat_arr);$i++) {
			$lat=$lat_arr[$i];
			$lon=$lon_arr[$i];

			$x= ( $lon - $min_lon) * $mapWidthPixels/ ($max_lon-$min_lon) ;
			$y= ( $lat - $min_lat) * $mapHeightPixels/ ($max_lat-$min_lat) ;
			imagesetpixel ( $img, $x, $mapHeightPixels-$y, $track_color  );
		}

		imagefilter($img, IMG_FILTER_SMOOTH,200);

		imagejpeg($img, $filename , 85);
		imagedestroy($img);
		return;

	}

	function getMapFromServer($num=0) {
		global $moduleRelPath,$mapServerActive;
		if (!$mapServerActive) return;

		require_once dirname(__FILE__)."/CL_map.php";

		$filename=$this->getIGCFilename(1);
		$lines = @file ($filename);
		if (!$lines ) return;

		$i=0;

		$min_lat=1000;
		$max_lat=-1000;
		$min_lon=1000;
		$max_lon=-1000;

		foreach($lines as $line) {
			$line=trim($line);
			if  (strlen($line)==0) continue;
			if ($line{0}=='B') {
					$thisPoint=new gpsPoint($line,$this->timezone);
					if ( $thisPoint->lat  > $max_lat )  $max_lat =$thisPoint->lat  ;
					if ( $thisPoint->lat  < $min_lat )  $min_lat =$thisPoint->lat  ;
					if ( $thisPoint->lon  > $max_lon )  $max_lon =$thisPoint->lon  ;
					if ( $thisPoint->lon  < $min_lon )  $min_lon =$thisPoint->lon  ;
					$i++;
			}
		}
		if ($i==0) return; // no B records found

		$lat_diff=$max_lat-$min_lat;
		$lon_diff=$max_lon-$min_lon;

		DEBUG("MAP",1,"MAP  min_lat: $min_lat, min_lon: $min_lon, max_lat: $max_lat, max_lon: $max_lon <BR>");

		if ($lat_diff > 20 || $lon_diff > 20 ) return; // too much

		list($MAP_LEFT,$MAP_TOP,$UTMzone,$UTMlatZone)=utm(-$max_lon,$max_lat);
		list($MAP_RIGHT,$MAP_BOTTOM,$UTMzone2,$UTMlatZone2)=utm(-$min_lon,$min_lat);

		$totalWidth1=calc_distance($min_lat, $min_lon,$min_lat, $max_lon);
		$totalWidth2=calc_distance($max_lat, $min_lon,$max_lat, $max_lon);
		$totalWidth=max($totalWidth1,$totalWidth2);
		$totalWidth_initial=$totalWidth;
		$totalHeight=$MAP_TOP-$MAP_BOTTOM;

		DEBUG("MAP",1,"MAP (right, left) :".$MAP_RIGHT." [".$UTMzone2."] ,".$MAP_LEFT."[".$UTMzone."]<BR>");
		DEBUG("MAP",1,"MAP (top, bottom) :".$MAP_TOP." ,".$MAP_BOTTOM."<BR>");
		DEBUG("MAP",1,"MAP (witdh,height) :".$totalWidth.",".$totalHeight."<BR>");

		if ($totalWidth> $totalHeight ) {
			// Landscape  style
			DEBUG("MAP",1,"Landscape style <BR>");
			DEBUG("MAP",1,"totalWidth: $totalWidth, totalHeight: $totalHeight, totalHeight/totalWidth: ".( $totalHeight / $totalWidth)."<br>");
			if ( $totalHeight / $totalWidth < 3/4 ) $totalHeight = (3/4) *  $totalWidth ;
		} else {
			// portait style
			DEBUG("MAP",1,"Portait style <BR>");
			DEBUG("MAP",1,"totalWidth: $totalWidth, totalHeight: $totalHeight, totalWidth/totalHeight: ".( $totalWidth / $totalHeight)."<br>");
			if ( $totalWidth  / $totalHeight < 3/4 )  $totalWidth  = (3/4) * $totalHeight ;
		}

		$marginHor=2000  + floor ( $totalWidth / 20000 ) * 1000 +  ($totalWidth - ($totalWidth_initial))/2 ;   //in meters
		$marginVert=2000 + floor ( $totalHeight / 20000 ) * 1000 + ($totalHeight - ($MAP_TOP-$MAP_BOTTOM))/2;   //in meters

		if ($marginHor > $marginVert ) {
			// landscape style ...
			if ( $marginVert / $marginHor < 3/4 ) $marginVert = (3/4) *  $marginHor  ;
		} else {
			// portait style
			if ( $marginHor / $marginVert < 3/4 )  $marginHor = (3/4) * $marginVert ;
		}

		DEBUG("MAP",1,"marginHor: $marginHor, marginVert:$marginVert <br>");

		$flMap=new flightMap($UTMzone,$UTMlatZone, $MAP_TOP + $marginVert, $MAP_LEFT - $marginHor,$UTMzone2, $UTMlatZone2, $MAP_BOTTOM - $marginVert ,$MAP_RIGHT +$marginHor  , 600,800,$this->getIGCFilename(1),$this->getMapFilename(0),$this->is3D() );
		DEBUG("MAP",1,"MAP Required m/pixel = ".$flMap->metersPerPixel."<br>");
		$flMap->drawFlightMap();

	}

	function getFlightFromDB($flightID,$updateTakeoff=1,$row=array()) {
	  global $db,$CONF_photosPerFlight;
  	  global $flightsTable;
	  global $nativeLanguage,$currentlang;

	  if ( count($row) ) $useData=true;
	  else $useData=false;

	  if (!$useData) {
		  $query="SELECT * FROM $flightsTable  WHERE ID=".$flightID." ";

		  $res= $db->sql_query($query);
		  # Error checking
		  if($res <= 0){
			 echo("<H3> Error in getFlightFromDB() query! $query</H3>\n");
			 exit();
		  }

		  if (! $row = $db->sql_fetchrow($res) ) {
		  echo "###### ERRROR ####$query###";
			  return 0;
		  }
	  }

		$this->flightID=$flightID;
		$name=getPilotRealName($row["userID"],$row["userServerID"]);
		$this->userName=$name;

		$this->serverID=$row["serverID"];
		$this->excludeFrom=$row["excludeFrom"];

		$this->originalURL=$row["originalURL"];
		$this->originalKML=$row["originalKML"];

		$this->original_ID=$row["original_ID"];

		$this->originalUserID=$row["originalUserID"];
		$this->userServerID=$row["userServerID"];

		$this->NACclubID=$row["NACclubID"];
		/// Martin Jursa, 17.05.2007
		$this->NACid=$row["NACid"];
		$this->cat=$row["cat"];
		$this->subcat=$row["subcat"];
		$this->category=$row["category"];
		$this->active=$row["active"];
		$this->private=$row["private"];

		$this->gliderCertCategory=$row["gliderCertCategory"];
		$this->startType=$row["startType"];

		$this->validated=$row["validated"];
		$this->grecord=$row["grecord"];
		$this->validationMessage=$row["validationMessage"];

		$this->airspaceCheck=$row["airspaceCheck"]+0;
		$this->airspaceCheckFinal=$row["airspaceCheckFinal"]+0;
		$this->airspaceCheckMsg=$row["airspaceCheckMsg"];
		$this->checkedBy=$row["checkedBy"];

		$this->timesViewed=$row["timesViewed"];
		$this->dateAdded=$row["dateAdded"];
		$this->dateUpdated=$row["dateUpdated"];
		$this->timezone=$row["timezone"];

		$this->filename=$row["filename"];
		$this->userID=$row["userID"];
		$this->comments=$row["comments"];
		$this->commentsNum=$row["commentsNum"]+0;
		$this->commentsEnabled=$row["commentsEnabled"]+0;

		$this->glider=$row["glider"];
		$this->gliderBrandID =$row["gliderBrandID"];
		$this->linkURL=$row["linkURL"];

		$this->hasPhotos=$row["hasPhotos"];
/*
		for($i=1;$i<=$CONF_photosPerFlight;$i++) {
			$var_name="photo".$i."Filename";
			$this->$var_name=$row[$var_name];
		}
*/

		$this->takeoffID=$row["takeoffID"];
		$this->takeoffVinicity=$row["takeoffVinicity"];
		$this->landingID=$row["landingID"];
		$this->landingVinicity=$row["landingVinicity"];

		$this->DATE=$row["DATE"];
		$this->MAX_SPEED =$row["MAX_SPEED"];
		$this->MEAN_SPEED =$row["MEAN_SPEED"];
		$this->MAX_ALT =$row["MAX_ALT"];
		$this->MIN_ALT =$row["MIN_ALT"];
		$this->TAKEOFF_ALT=$row["TAKEOFF_ALT"];
		$this->MAX_VARIO =$row["MAX_VARIO"];
		$this->MIN_VARIO =$row["MIN_VARIO"];
		$this->LINEAR_DISTANCE =$row["LINEAR_DISTANCE"];

		$this->START_TIME=$row["START_TIME"];
		$this->END_TIME=$row["END_TIME"];
		$this->DURATION=$row["DURATION"];

		$this->MAX_LINEAR_DISTANCE =$row["MAX_LINEAR_DISTANCE"];
		$this->BEST_FLIGHT_TYPE=$row["BEST_FLIGHT_TYPE"];
		$this->FLIGHT_KM=$row["FLIGHT_KM"];
		$this->FLIGHT_POINTS=$row["FLIGHT_POINTS"];

		$this->autoScore=$row["autoScore"];

		$this->externalFlightType=$row["externalFlightType"];
		$this->isLive=$row["isLive"];
		$this->firstPointTM=$row["firstPointTM"];
		$this->firstLat=$row["firstLat"];
		$this->firstLon=$row["firstLon"];
		$this->lastPointTM=$row["lastPointTM"];
		$this->lastLat=$row["lastLat"];
		$this->lastLon=$row["lastLon"];
		$this->forceBounds=$row["forceBounds"];

		$this->hash=$row["hash"];

//to be deleted START
//		$this->FIRST_POINT=$row["FIRST_POINT"];
//		$this->LAST_POINT=$row["LAST_POINT"];

/*
		$this->turnpoint1=$row["turnpoint1"];
		$this->turnpoint2=$row["turnpoint2"];
		$this->turnpoint3=$row["turnpoint3"];
		$this->turnpoint4=$row["turnpoint4"];
		$this->turnpoint5=$row["turnpoint5"];
*/


//		$this->olcRefNum=$row["olcRefNum"];
//		$this->olcFilename=$row["olcFilename"];
//		$this->olcDateSubmited =$row["olcDateSubmited"];
//to be deleted END


		// now recompute the
		// $this->originalURL
		// $this->originalKML
		$this->getOriginalKML();
		$this->getOriginalURL();

		if (!$useData) {
	  		$db->sql_freeresult($res);
		}

		if ($this->filename && $updateTakeoff) $this->updateTakeoffLanding();
		return 1;
	}

	function updateTakeoffLanding() {
		global $db;
		global $flightsTable, $waypoints;

		$firstPoint=new gpsPoint('',$this->timezone);
		$firstPoint->setLat($this->firstLat);
		$firstPoint->setLon($this->firstLon);
		$firstPoint->gpsTime=$this->firstPointTM;

		$lastPoint=new gpsPoint('',$this->timezone);
		$lastPoint->setLat($this->lastLat);
		$lastPoint->setLon($this->lastLon);
		$lastPoint->gpsTime=$this->lastPointTM;

		//$firstPoint=new gpsPoint($this->FIRST_POINT,$this->timezone);
		//$lastPoint=new gpsPoint($this->LAST_POINT,$this->timezone);

		// calc TAKEOFF - LANDING PLACES
		if (count((array)$waypoints)==0)
			$waypoints=getWaypoints();

		$takeoffIDTmp=0;
		$minTakeoffDistance=1000000;
		$landingIDTmp=0;
		$minLandingDistance=1000000;

		foreach($waypoints as $waypoint) {
		   $takeoff_distance = $firstPoint->calcDistance($waypoint);
		   $landing_distance = $lastPoint->calcDistance($waypoint);
		   if ( $takeoff_distance < $minTakeoffDistance ) {
				$minTakeoffDistance = $takeoff_distance;
				$takeoffIDTmp=$waypoint->waypointID;
		   }
		   if ( $landing_distance < $minLandingDistance ) {
				$minLandingDistance = $landing_distance;
				$landingIDTmp=$waypoint->waypointID;
		   }
		}

		if ( $this->takeoffID!=$takeoffIDTmp || $this->takeoffVinicity!=$minTakeoffDistance ||
	   		 $this->landingID!=$landingIDTmp || $this->landingVinicity!=$minLandingDistance )
		{
			  $query="UPDATE $flightsTable SET takeoffID='".$takeoffIDTmp."',  takeoffVinicity=".$minTakeoffDistance.",
					  landingID='".$landingIDTmp."', landingVinicity=".$minLandingDistance."  WHERE ID=".$this->flightID." ";
		   	  // echo $query;

			  $res= $db->sql_query($query);
			  # Error checking
			  if($res <= 0){
				 echo("<H3> Error in Update Takeoff - landing  query! </H3>\n");
				 exit();
			  }

		}

		$this->takeoffID=$takeoffIDTmp;
		$this->takeoffVinicity=$minTakeoffDistance;
		$this->landingID=$landingIDTmp;
		$this->landingVinicity=$minLandingDistance;

	}

	function deletePhoto($photoNum) {
		$var_name="photo".$photoNum."Filename";
		if ( is_file($this->getPhotoFilename($photoNum) )  ) {
			# martin jursa 28.05.2008: delete using the deleteFile() method to avoid log flooding
			$this->deleteFile($this->getPhotoFilename($photoNum) );
			$this->deleteFile($this->getPhotoFilename($photoNum).".icon.jpg" );
		}
		$this->$var_name="";
	}

	function renameTracklog($newName,$oldName='') {
		global $db;
		global $flightsTable;
		global $CONF_server_id;

		if ($oldName) {
			$orgFilename=$this->filename;
			$this->filename=$oldName;
		}

		$this->deleteSecondaryFiles();

		$this->deleteFile($this->getMapFilename() );

		for ($metric_system=1;$metric_system<=2;$metric_system++) {
			for ($raw=0;$raw<=1;$raw++) {
				$this->deleteFile($this->getChartFilename("alt",$metric_system,$raw) );
				$this->deleteFile($this->getChartFilename("speed",$metric_system,$raw) );
				$this->deleteFile($this->getChartFilename("vario",$metric_system,$raw) );
				$this->deleteFile($this->getChartFilename("takeoff_distance",$metric_system,$raw) );
				$this->deleteFile($this->getChartFilename("alt",$metric_system,$raw).".txt" );
				$this->deleteFile($this->getChartFilename("speed",$metric_system,$raw).".txt" );
				$this->deleteFile($this->getChartFilename("vario",$metric_system,$raw).".txt" );
				$this->deleteFile($this->getChartFilename("takeoff_distance",$metric_system,$raw).".txt" );
			}
		}

		$oldFilename=$this->getIGCFilename() ;
		$oldFilenameSaned=$this->getIGCFilename(1) ;
		$oldFilenameSanedFull=$this->getIGCFilename(2) ;
		$oldOLCfile=$this->getIGCFilename(0).".olc";

		$this->filename=$newName;

		@rename($oldFilename,$this->getIGCFilename() );
		@rename($oldFilenameSaned,$this->getIGCFilename(1) );
		@rename($oldFilenameSanedFull,$this->getIGCFilename(2) );
		@rename($oldOLCfile,$this->getIGCFilename(0).".olc");


		$query="UPDATE $flightsTable SET filename='".$this->filename."' WHERE ID=".$this->flightID;
		// echo $query."<HR>";
		$res= $db->sql_query($query );
		if($res <= 0){
			 echo "Error renaming IGC file for flight ".$this->flightID." : $query<BR>";
		}

		require_once dirname(__FILE__).'/lib/json/CL_json.php';
		require_once dirname(__FILE__).'/CL_actionLogger.php';
		$log=new Logger();
		$log->userID  	=$this->userID;
		$log->ItemType	=1 ; // flight;
		$log->ItemID	= ( ( $this->serverID && $this->serverID!=$CONF_server_id ) ?$this->original_ID:$this->flightID ); // 0 at start will fill in later if successfull
		$log->ServerItemID	= ( $this->serverID?$this->serverID:$CONF_server_id) ;
		$log->ActionID  = 16 ;  //1  => add  2  => edit; 4  => delete ; 16 -> rename trackog
		$log->ActionXML	=
'{
	"serverID": '. ( $this->serverID?$this->serverID:$CONF_server_id).',
	"id": '.($isLocal ? $this->flightID : $this->original_ID  ).',
	"linkIGC": "'.$this->getIGC_URL().'",
	"linkIGCzip": "'.$this->getZippedIGC_URL().'",
	"newFilename": "'.json::prepStr($this->filename).'",
	"oldFilename": "'.json::prepStr($oldName).'"
}';
		$log->Modifier	= 0;
		$log->ModifierID= 0;
		$log->ServerModifierID =0;
		$log->Result = 1;
		if (!$log->Result) $log->ResultDescription ="Problem in deleting flight  $query";
		if (!$log->put()) echo "Problem in logger<BR>";
	}

	function deleteSecondaryFiles(){
		# martin jursa 28.05.2008: delete using the deleteFile() method to avoid log flooding		
		$this->deleteFile($this->getJsonFilename() ) ; // json.js
		$this->deleteFile($this->getPolylineFilename() ) ;// *.poly.txt
		$this->deleteFile($this->getPointsFilename(1) ) ;// *.1.txt
		$this->deleteFile($this->getKMLFilename(0) ); // kmz
		$this->deleteFile($this->getKMLFilename(1) ); // man.kmz
		require_once dirname(__FILE__).'/FN_igc2kmz.php';			
		deleteOldKmzFiles($this->getKMLFilename(3),'xxx'); // delete all versions igc2kmz
	}


/**
 * Martin Jursa 28.05.2008
 * Delete function avoiding the @unlink expression which throws warnings if error handling is turned on
 *
 * @param string $filename
 */
	function deleteFile($filename) {
		if (file_exists($filename)) {
			unlink($filename);
		}
	}

	function deleteFlight() {
 		global $db;
		global $flightsTable,$deletedFlightsTable;
		global $CONF_photosPerFlight,$CONF_server_id;

		$query="INSERT INTO $deletedFlightsTable SELECT $flightsTable.* from $flightsTable WHERE $flightsTable.ID=".$this->flightID." ";
		$res= $db->sql_query($query);

        $query="UPDATE $deletedFlightsTable Set dateUpdated='".gmdate("Y-m-d H:i:s")."' where ID=".$this->flightID." ";
	    $res= $db->sql_query($query);

		$query="DELETE from $flightsTable  WHERE ID=".$this->flightID." ";
		// echo $query;
		$res= $db->sql_query($query);
	    # Error checking
	    if($res <= 0){
		  echo("<H3> Error in delete Flight query! </H3>\n");
		  exit();
 	    }

		// save a copy
		$this->flightScore=new flightScore($this->flightID);
		$this->flightScore->getFromDB();

		$flightScore=new flightScore($this->flightID);
		$flightScore->deleteFromDB();
		// Now delete the files

		$this->deleteFile($this->getIGCFilename(0) );
		$this->deleteFile($this->getIGCFilename(0).".olc" );
		
		$this->deleteFile($this->getIGCFilename(1) );
		$this->deleteFile($this->getIGCFilename(2) );


		$this->deleteSecondaryFiles();

		$this->deleteFile($this->getMapFilename());

		for ($metric_system=1;$metric_system<=2;$metric_system++) {
			for ($raw=0;$raw<=1;$raw++) {
				# martin jursa 28.05.2008: delete using the deleteFile() method to avoid log flooding
				$this->deleteFile($this->getChartFilename("alt",$metric_system,$raw) );
				$this->deleteFile($this->getChartFilename("speed",$metric_system,$raw) );
				$this->deleteFile($this->getChartFilename("vario",$metric_system,$raw) );
				$this->deleteFile($this->getChartFilename("takeoff_distance",$metric_system,$raw) );
				$this->deleteFile($this->getChartFilename("alt",$metric_system,$raw).".txt" );
				$this->deleteFile($this->getChartFilename("speed",$metric_system,$raw).".txt" );
				$this->deleteFile($this->getChartFilename("vario",$metric_system,$raw).".txt" );
				$this->deleteFile($this->getChartFilename("takeoff_distance",$metric_system,$raw).".txt" );
			}
		}

		if ($this->hasPhotos) {
			$flightPhotos=new flightPhotos($this->flightID);
			$flightPhotos->deleteAllPhotos(0);
		}
		
		if ($this->commentsNum) {
			require_once dirname(__FILE__).'/CL_comments.php';
			$comments=new flightComments($this->flightID);
			$comments->deleteAllComments(0); // dont update the flights table
		}
		
		// Now also hide/unhide same flights
		$this->hideSameFlights();

		require_once dirname(__FILE__).'/CL_actionLogger.php';
		$log=new Logger();
		$log->userID  	=$this->userID;
		$log->ItemType	=1 ; // flight;
		$log->ItemID	= ( ( $this->serverID && $this->serverID!=$CONF_server_id ) ?$this->original_ID:$this->flightID ); // 0 at start will fill in later if successfull
		$log->ServerItemID	= ( $this->serverID?$this->serverID:$CONF_server_id) ;
		$log->ActionID  = 4 ;  //1  => add  2  => edit; 4  => delete
		$log->ActionXML	= $this->toXML();
		$log->Modifier	= 0;
		$log->ModifierID= 0;
		$log->ServerModifierID =0;
		$log->Result = 1;
		if (!$log->Result) $log->ResultDescription ="Problem in deleting flight  $query";
		if (!$log->put()) echo "Problem in logger<BR>";
	}

    function putFlightToDB($update=0) {
		global $db;
		global $flightsTable,$CONF_photosPerFlight,$CONF_server_id;

		if ($update) {
			$query="REPLACE INTO ";
			$fl_id_1="ID,dateAdded,";
			$fl_id_2=$this->flightID.",'".$this->dateAdded."',";
			$this->active=1;
		}else {
			$query="INSERT INTO ";
			$fl_id_1="dateAdded,";

			$this->active=0;
			if (!$this->dateAdded )
				$this->dateAdded= gmdate("Y-m-d H:i:s");

			// $fl_id_2="now(),";
			$fl_id_2=" '".$this->dateAdded."',";

			$this->timesViewed=0;
		}

		$this->dateUpdated = gmdate("Y-m-d H:i:s");

	/*
		for($i=1;$i<=$CONF_photosPerFlight;$i++) {
			$var_name="photo".$i."Filename";
			$p1.="$var_name, ";
			$p2.="'".$this->$var_name."',";
		}
	*/

		// we dont store $originalURL $originalKML. for leonardo originated flights...

		$originalURL = $this->originalURL ;
		$originalKML = $this->originalKML ;
		if ( $this->serverID != 0 ) {
			global $CONF;
			if ( $CONF['servers']['list'][$this->serverID]['isLeo'] == 1  ) {
				$originalURL='';
				$originalKML='';
			}
		}

		// make sure it evaluates to something
		$this->gliderCertCategory+=0;

		/// Martin Jursa 17.05.2007: adding NACid
		$query.=" $flightsTable (".$fl_id_1."filename,userID, dateUpdated,
		cat,subcat,category,active, private ,
		gliderCertCategory, startType,
		validated,grecord,validationMessage,
		hash, serverID, originalURL, originalKML, original_ID,
		originalUserID ,userServerID,
		excludeFrom,

		airspaceCheck,airspaceCheckFinal,airspaceCheckMsg,checkedBy,
		NACclubID,NACid,
		comments, commentsNum, commentsEnabled, glider, gliderBrandID, linkURL, timesViewed,

		takeoffID, takeoffVinicity, landingID, landingVinicity,
		DATE,
		timezone,
		hasPhotos,
		MAX_SPEED ,
		MEAN_SPEED ,
		MAX_ALT ,
		MIN_ALT ,
		TAKEOFF_ALT,
		MAX_VARIO ,
		MIN_VARIO ,
		LINEAR_DISTANCE , ".
		"MAX_LINEAR_DISTANCE ,".
		"START_TIME,
		END_TIME,
		DURATION, ".
		"BEST_FLIGHT_TYPE,
		FLIGHT_KM,
		FLIGHT_POINTS,".

		"autoScore,
		forceBounds,
		externalFlightType,	isLive,
	
		firstPointTM, firstLat, firstLon,
		lastPointTM, lastLat, lastLon

		)
		VALUES (".$fl_id_2."'$this->filename',$this->userID, '$this->dateUpdated',
		$this->cat,$this->subcat,$this->category,$this->active, $this->private,
		$this->gliderCertCategory, $this->startType,
		$this->validated, $this->grecord, '".prep_for_DB($this->validationMessage)."',
		'$this->hash',  $this->serverID, '$originalURL', '$originalKML',  $this->original_ID,
		'$this->originalUserID' , $this->userServerID,
		$this->excludeFrom,

		$this->airspaceCheck, $this->airspaceCheckFinal, '".prep_for_DB($this->airspaceCheckMsg)."','".prep_for_DB($this->checkedBy)."',
		$this->NACclubID, $this->NACid,
		'".prep_for_DB($this->comments)."', ".($this->commentsNum+0).", ".($this->commentsEnabled+0).", '".prep_for_DB($this->glider)."',  ".($this->gliderBrandID+0)." , '".prep_for_DB($this->linkURL)."', $this->timesViewed ,

		'$this->takeoffID', $this->takeoffVinicity, '$this->landingID', $this->landingVinicity,
		'$this->DATE',
		$this->timezone,
		$this->hasPhotos,
		$this->MAX_SPEED ,
		$this->MEAN_SPEED ,
		$this->MAX_ALT ,
		$this->MIN_ALT ,
		$this->TAKEOFF_ALT,
		$this->MAX_VARIO ,
		$this->MIN_VARIO ,
		$this->LINEAR_DISTANCE , ".
		($this->MAX_LINEAR_DISTANCE +0).",".
		"$this->START_TIME,
		$this->END_TIME,
		$this->DURATION, ".
		"'$this->BEST_FLIGHT_TYPE',
		".($this->FLIGHT_KM+0).",
		".($this->FLIGHT_POINTS+0).",".
		"$this->autoScore,
		$this->forceBounds,
		$this->externalFlightType,	$this->isLive,
		
		".($this->firstPointTM+0).", $this->firstLat, $this->firstLon,
		".($this->lastPointTM+0).", $this->lastLat, $this->lastLon

		)";

		//echo $query;
		$result = $db->sql_query($query);
		if (!$result) {
			echo "Problem in puting flight to DB $query<BR>";
		}
		//echo "UPDATE / INSERT RESULT ".$result ;
		if (!$update) $this->flightID=$db->sql_nextid();


		require_once dirname(__FILE__).'/CL_actionLogger.php';
		$log=new Logger();
		$log->userID  	=$this->userID;
		$log->ItemType	=1 ; // flight;
		$log->ItemID	= ( ( $this->serverID && $this->serverID!=$CONF_server_id ) ?$this->original_ID:$this->flightID ); // 0 at start will fill in later if successfull
		$log->ServerItemID	=  ( $this->serverID?$this->serverID:$CONF_server_id);
		$log->ActionID  = $update+1 ;  //1  => add  2  => edit;
		$log->ActionXML	= $this->toXML();
		$log->Modifier	= 0;
		$log->ModifierID= 0;
		$log->ServerModifierID =0;
		$log->Result = ($result?1:0);
		if (!$log->Result) $log->ResultDescription ="Problem in puting flight to DB $query";
		if (!$log->put()) echo "Problem in logger<BR>";

		return $result;
	}

	function makeLogEntry() {
		global $CONF_server_id ;
		require_once dirname(__FILE__).'/CL_actionLogger.php';
		$log=new Logger();
		$log->userID  	=$this->userID;
		$log->ItemType	=1 ; // flight;
		$log->ItemID	= ( ( $this->serverID && $this->serverID!=$CONF_server_id ) ?$this->original_ID:$this->flightID ); // 0 at start will fill in later if successfull
		$log->ServerItemID	= ( $this->serverID?$this->serverID:$CONF_server_id);
		$log->ActionID  = 1 ;  //1  => add  2  => edit;
		$log->ActionXML	= $this->toXML();
		$log->Modifier	= 0;
		$log->ModifierID= 0;
		$log->ServerModifierID =0;
		$log->Result = 1;

		if (!$log->put()) echo "Problem in logger<BR>";
	}

	function activateFlight() {
		global $db;
		global $flightsTable;

		$query="UPDATE $flightsTable SET active=1 WHERE ID=$this->flightID";
		// echo $query;
		$result = $db->sql_query($query);

		$this->active=1;
	}

	function incViews() {
		global $db;
		global $flightsTable;

		$query="UPDATE $flightsTable SET  timesViewed=timesViewed+1 WHERE ID=$this->flightID";
		// echo $query;
		// Disable this count!
		// $result = $db->sql_query($query);
		$this->timesViewed++;
	}

	function updateAll($forceRefresh=0) {
		// chack for saned igc in case it wasnt created in the first place or if the flight was synced
	 	if (  !is_file( $this->getIGCFilename(1) ) || $forceRefresh ) {
			if (! $this->getFlightFromIGC($this->getIGCFilename(0) ) ) {
				$this->getFlightFromDB($this->flightID,0);
				return;
			}
		}

 	    if (  !is_file( $this->getMapFilename() ) || $forceRefresh ) {
			 $this->getMapFromServer();
	    }

	    $this->updateCharts($forceRefresh);
 	    $this->updateCharts($forceRefresh,1);
	}


    function updateCharts($forceRefresh=0,$rawCharts=0) {
		global $moduleRelPath,$chartsActive;
		if (!$chartsActive ) return 0;

	 	$alt_img_filename=$this->getChartFilename("alt",1,$rawCharts);
		$speed_img_filename=$this->getChartFilename("speed",1,$rawCharts);
		$vario_img_filename=$this->getChartFilename("vario",1,$rawCharts);
		$takeoff_distance_img_filename=$this->getChartFilename("takeoff_distance",1,$rawCharts);

 		$alt_img_filename2=$this->getChartFilename("alt",2,$rawCharts);
		$speed_img_filename2=$this->getChartFilename("speed",2,$rawCharts);
		$vario_img_filename2=$this->getChartFilename("vario",2,$rawCharts);
		$takeoff_distance_img_filename2=$this->getChartFilename("takeoff_distance",2,$rawCharts);

		if ( !is_file($alt_img_filename.".txt") ||  !is_file($speed_img_filename.".txt") ||
			 !is_file($vario_img_filename.".txt") ||  !is_file($takeoff_distance_img_filename.".txt") ||
			 !is_file($alt_img_filename2.".txt") ||  !is_file($speed_img_filename2.".txt") ||
			 !is_file($vario_img_filename2.".txt") ||  !is_file($takeoff_distance_img_filename2.".txt") ||
			 $forceRefresh) {
            file_put_contents($alt_img_filename.".txt", "1");
            file_put_contents($speed_img_filename.".txt", "1");
            file_put_contents($vario_img_filename.".txt", "1");
            file_put_contents($takeoff_distance_img_filename.".txt", "1");
            file_put_contents($alt_img_filename2.".txt", "1");
            file_put_contents($speed_img_filename2.".txt", "1");
            file_put_contents($vario_img_filename2.".txt", "1");
            file_put_contents($takeoff_distance_img_filename2.".txt", "1");
            
			// list ($data_time,$data_alt,$data_speed,$data_vario,$data_takeoff_distance)=$this->getAltValues();

			if ($rawCharts)  {
				list ($data_time,$data_alt,$data_speed,$data_vario,$data_takeoff_distance)=$this->getRawValues($forceRefresh);
				//list ($data_time,$data_alt,$data_speed,$data_vario,$data_takeoff_distance,$data_X,$data_Y)=$this->getRawValues($forceRefresh,1);
			} else
				list ($data_time,$data_alt,$data_speed,$data_vario,$data_takeoff_distance)=$this->getAltValues();

			if (!count($data_time) ) return; // empty timeseries

			require_once dirname(__FILE__)."/lib/graph/jpgraph_gradient.php";
			require_once dirname(__FILE__)."/lib/graph/jpgraph_plotmark.inc.php" ;

			require_once dirname(__FILE__)."/lib/graph/jpgraph.php";
			require_once dirname(__FILE__)."/lib/graph/jpgraph_line.php";

/*
require_once dirname(__FILE__)."/CL_dem.php";
$ground=array();
foreach ($data_time as $i=>$tm) {
	$ground[$i]=DEM::getAlt($data_Y[$i],$data_X[$i]);
	echo $ground[$i]."#";

}
*/
		 	$this->plotGraph("Wysokosc (m)",$data_time,$data_alt,$alt_img_filename,$rawCharts);
			//$this->plotGraph("Height (m)",$data_time,$ground,$alt_img_filename,$rawCharts);
			$this->plotGraph("Predkosc (km/h)",$data_time,$data_speed,$speed_img_filename,$rawCharts);
			$this->plotGraph("Noszenia (m/s)",$data_time,$data_vario,$vario_img_filename,$rawCharts);
			$this->plotGraph("Odleglosc od startowiska (km)",$data_time,$data_takeoff_distance,$takeoff_distance_img_filename,$rawCharts);

			// now make the miles/ feet versions!!!
			// convert the arrays
			// 1 kilometer = 0.62 mil
			// 1 meter  =  3.28 feet
			foreach($data_time as $idx=>$val) {
				$data_alt[$idx]=$data_alt[$idx]*3.28;
				$data_speed[$idx]=$data_speed[$idx]*0.62;
				$data_vario[$idx]=$data_vario[$idx]*3.28*60;
				$data_takeoff_distance[$idx]=$data_takeoff_distance[$idx]*0.62;
			}

			$this->plotGraph("Wysokosc (stopy)",$data_time,$data_alt,$alt_img_filename2,$rawCharts);
			$this->plotGraph("Predkosc (mph)",$data_time,$data_speed,$speed_img_filename2,$rawCharts);
			$this->plotGraph("Noszenia (fpm)",$data_time,$data_vario,$vario_img_filename2,$rawCharts);
			$this->plotGraph("Odleglosc od startowiska (mile)",$data_time,$data_takeoff_distance,$takeoff_distance_img_filename2,$rawCharts);

		}

	}

	function plotGraph($title,$data_time,$yvalues,$img_filename,$raw=0) {

		if (count($data_time)==0) {
			$data_time[0]=0;
			$yvalues[0]=0;
		}
		if ($raw) {
			$graphWidth=1200;
			$graphHeight=240;
			$mLeft=40;
			$mRight=20;
			$mTop=13;
			$mBottom=23;
			$titleSize=8;
			$labelSize=6;

		}	else {
			$graphWidth=1200;
			$graphHeight=400;
			$mLeft=40;
			$mRight=20;
			$mTop=20;
			$mBottom=30;

			$titleSize=10;
			$labelSize=8;
		}

		$graph = new Graph($graphWidth,$graphHeight,"auto");
		//$graph->SetScale("textlin",min($yvalues),max($yvalues));
		$graph->SetScale("textlin");

		if ($raw) {
			$graph->title->SetFont(FF_FONT1,FS_NORMAL,$titleSize);
			$graph->legend->SetFont(FF_FONT1,FS_NORMAL,$titleSize);
			$graph->xaxis->SetFont(FF_FONT1,FS_NORMAL,$labelSize);
		}

		$graph->SetMarginColor("#C8C8D4");
		// #DAE4E6

		// if only one point  add it twice !
		// we should nt be here at the first place !
		if (count($data_time)==1) {
			$yvalues[1]=$yvalues[0];
			$data_time[1]=$data_time[0];
		}
		//echo "$title -> max:".max($yvalues)." min: ".min($yvalues)." data_time_num: ".count($data_time)."<BR>";
		$lineplot=new LinePlot($yvalues);
		$graph->Add($lineplot);

		$graph->img->SetMargin($mLeft,$mRight,$mTop,$mBottom);
		if (!$raw) $graph->title->Set($title);

		$textTickInterval=floor((count($data_time)/($graphWidth-$mLeft-$mRight))*60-1);
		if ($textTickInterval<=0 ) $textTickInterval=1;

		$graph->xaxis->SetTextTickInterval($textTickInterval);
		$graph->xaxis->SetTextLabelInterval(1);
		$graph->xaxis->SetTickLabels($data_time);
		$graph->xaxis->SetPos("min");

		$graph->xgrid->Show();
		$graph->xgrid->SetLineStyle('dashed');


		$graph->Stroke($img_filename);
	}

	function findSameFilename($filename) {
		global $db;
		global $flightsTable;
		$query="SELECT ID FROM $flightsTable WHERE userID=".$this->userID." AND userServerID=".$this->userServerID.
						" AND filename=\"".$filename."\" ";
		// echo $query;
		$res= $db->sql_query($query);
		if ($res<=0) return 0; // no duplicate found

		$row = $db->sql_fetchrow($res);
		return $row["ID"]; // found duplicate retrun the ID;
	}

	function findSameTime() {
		global $db;
		global $flightsTable;
		$query="SELECT serverID,ID FROM $flightsTable WHERE userID=".$this->userID." AND userServerID=".$this->userServerID.
					" AND DATE='".$this->DATE."'  AND
						(
							( ".$this->START_TIME." >= START_TIME AND ".$this->START_TIME." <= END_TIME )
							OR
							( ".$this->END_TIME." >= START_TIME AND ".$this->END_TIME." <= END_TIME )
						)
							";
		$res= $db->sql_query($query);
		if ($res<=0) return array(); // no duplicate found

		$i=0;
		$dup=array();
		while  ( $row = $db->sql_fetchrow($res) ) {
			$dup[$i]['ID']=$row["ID"];
			$dup[$i]['serverID']=$row["serverID"];
			$i++;
		}
		return $dup; // found duplicate return the array of IDs;

	}

	function findSameHash($hash,$serverIDtoCheck=0 ) {
		global $db;
		global $flightsTable;

		$where_clause='';
		if ($serverIDtoCheck) $where_clause=" AND serverID=$serverIDtoCheck ";

		$query="SELECT serverID,ID,userID,userServerID FROM $flightsTable WHERE hash='$hash' $where_clause ";
		// echo $query;
		$res= $db->sql_query($query);
		if ($res<=0) return array(); // no duplicate found

		$i=0;
		$dup=array();
		while  ( $row = $db->sql_fetchrow($res) ) {
			$dup[$i]['ID']=$row["ID"];
			$dup[$i]['serverID']=$row["serverID"];
			$dup[$i]['userServerID']=$row["userServerID"];
			$dup[$i]['userID']=$row["userID"];
			$i++;
		}
		return $dup; // found duplicate return the array of IDs;

	}

	function hideSameFlights() {
		// now is a good time to disable duplicate flights we have found from other servers
		// AND are from the same user (using pilot's mapping table to find that out)

		// addition: 2008/07/21 we search for all flight no only from same user/server
		global $db,$flightsTable;

		$query="SELECT serverID,ID,externalFlightType, FROM $flightsTable
					WHERE hash='".$this->hash."' AND userID=".$this->userID." AND userServerID=".$this->userServerID.
					" ORDER BY serverID ASC, ID ASC";

		$query="SELECT serverID,ID,externalFlightType,userID,userServerID FROM $flightsTable
			WHERE hash='".$this->hash."' ORDER BY serverID ASC, ID ASC";

		// echo $query;
		$res= $db->sql_query($query);
		if ($res<=0) {
			DEBUG("FLIGHT",1,"flightData: Error in query: $query<br>");
			return array(); // no duplicate found
		}

		// we must disable all flights BUT one
		// rules:
		// 1. locally submitted flights have priority
		// 2. between external flights , the full synced have priority over simple links
		// 3. between equal cases the first submitted has priority.

		$i=0;
		while  ( $row = $db->sql_fetchrow($res) ) {
			$fList[$i]=$row;
			$i++;
		}

		if ($i==0) {
			return array(); // no duplicate found
		}

		usort($fList, "sameFlightsCmp");

		$i=0;
		$msg='';
		foreach($fList as $i=>$fEntry) {
			if (0) {
				echo "<pre>";
				echo "-------------------------<BR>";
				print_r($fEntry);
				echo "-------------------------<BR>";
				echo "</pre>";
			}

			if ($i==0)  {// enable
				$msg.= " Enabling ";
				$query="UPDATE $flightsTable SET private = private & (~0x02 & 0xff ) WHERE  ID=".$fEntry['ID'];
			} else  {// disable
				$msg.= " Disabling ";
				$query="UPDATE $flightsTable SET private = private | 0x02 WHERE  ID=".$fEntry['ID'];
			}
			$msg.= " <a href='http://".$_SERVER['SERVER_NAME'].
				getLeonardoLink(array('op'=>'show_flight','flightID'=>$fEntry['ID'])).
				"'>Flight ".$fEntry['ID'].
			"</a> from <a href='http://".$_SERVER['SERVER_NAME'].
				getLeonardoLink(array('op'=>'pilot_profile','pilotIDview'=>$fEntry['userServerID'].'_'.$fEntry['userID'])).
				"'>PILOT ".
			$fEntry['userServerID'].'_'.$fEntry['userID']."</a><BR>\n";

			$res= $db->sql_query($query);
			# Error checking
			if($res <= 0){
				echo("<H3> Error in query: $query</H3>\n");
			}

			$i++;
		}

		// now also make a test to see if all flights are from same user (alien mapped  to local)
		// if not , send a mail to admin to warn him and suggest a new mapping
		$pList=array();
		foreach($fList as $i=>$fEntry) {
			$pList[$fEntry['userServerID'].'_'.$fEntry['userID']]++;
		}

		if ( count($pList) > 1  ) { // more than one pilot involved in this
			sendMailToAdmin("Duplicate flights",$msg);
			//echo "Duplicate flights".$msg;
		}

		/*
		foreach ($disableFlightsList as $dFlightID=>$num) {
			$query="UPDATE $flightsTable SET private = private | 0x02 WHERE  ID=$dFlightID ";
			$res= $db->sql_query($query);
			# Error checking
			if($res <= 0){
				echo("<H3> Error in query: $query</H3>\n");
			}
		}
		foreach ($enableFlightsList as $dFlightID=>$num) {
			$query="UPDATE $flightsTable SET private = private & (~0x02 & 0xff ) WHERE  ID=$dFlightID ";
			$res= $db->sql_query($query);
			# Error checking
			if($res <= 0){
				echo("<H3> Error in query: $query</H3>\n");
			}
		}*/

	}

	function numOfFlightsSubmitedThisDay() {
		global $db;
		global $flightsTable;

		$query="SELECT count(*) as num FROM $flightsTable WHERE userID=".$this->userID." AND DATE='".
				$this->DATE."' AND ID <> ".$this->flightID." AND olcRefNum <> '' ";

		$res= $db->sql_query($query);
		if ($res<=0) return 0; // no duplicate found

		$row = $db->sql_fetchrow($res);
		return $row["num"]; // found duplicate retrun the ID;
	}

	function insideOLCsubmitWindow() {
		// submit deadline
		//	24:00 UTC on the Tuesday following a week after the flight
		$submitDeadline=mktime(11,59,0,$this->getMonth(),$this->getDay(),$this->getYear() );
		$submitDeadline+= 7*24*60*60 ; // one week after
		$dayOfWeek=date("w",$tm); // 0=sunday ; 1=mon 2=tuesday 3=wensday

		if ( $dayOfWeek <= 2 )  $submitDeadline+= (2-$dayOfWeek)* 24*60*60;
		else $submitDeadline+= (6-$dayOfWeek +3 )* 24*60*60;

		// TO BE FIXED !!!!
		// should cater for UTC time
		$now=mktime();
		if ($submitDeadline > $now ) return 1;
		else return 0;
	}

	// assigns the flight to a new user
	function changeUser($newUserID,$newUserServerID) {
		global $CONF,$CONF_photosPerFlight;

		$pilotDir=$this->getPilotAbsDir();

		if ($newUserServerID) $extra_prefix=$newUserServerID.'_';
		else $extra_prefix='';
		$newPilotDir=LEONARDO_ABS_PATH.'/'.str_replace("%PILOTID%",$extra_prefix.$newUserID,$CONF['paths']['pilot']);			
		

		// delete non critical files
		$this->deleteFile($this->getIGCFilename(1) );
		$this->deleteFile($this->getIGCFilename(2) );
		$this->deleteSecondaryFiles();
		$this->deleteFile($this->getMapFilename());
		for ($metric_system=1;$metric_system<=2;$metric_system++) {
			for ($raw=0;$raw<=1;$raw++) {
				# martin jursa 28.05.2008: delete using the deleteFile() method to avoid log flooding
				$this->deleteFile($this->getChartFilename("alt",$metric_system,$raw) );
				$this->deleteFile($this->getChartFilename("speed",$metric_system,$raw) );
				$this->deleteFile($this->getChartFilename("vario",$metric_system,$raw) );
				$this->deleteFile($this->getChartFilename("takeoff_distance",$metric_system,$raw) );
			}
		}
		
		
		$flightYear=$this->getYear();
		$subdirs=array('flights','charts','maps');
		
		// create all dirs on the target user as well in case they are missing
		$this->checkDirs($extra_prefix.$newUserID,$flightYear); 
		
		/*
		foreach ($subdirs as $subdir){
			$sourceDir="$pilotDir/$subdir/$flightYear";
			$targetDir="$newPilotDir/$subdir/$flightYear";

			if ($handle = opendir($sourceDir)) {
				while (false !== ($file = readdir($handle))) {
					if (  substr( $file,0,strlen($this->filename) )==$this->filename  ) {
						// echo "$file\n";
						$filesToMove[$sourceDir.'/'.$file]=$targetDir.'/'.$file;
					}
				}
				closedir($handle);
			}
		}

		//	this is old code, does not work nay more!
		for($i=1;$i<=$CONF_photosPerFlight;$i++) {
			$var_name="photo".$i."Filename";
			$file=$this->$var_name;
			if ($file) {
				$sourceDir="$pilotDir/photos/$flightYear";
				$targetDir="$newPilotDir/photos/$flightYear";
				$filesToMove[$sourceDir.'/'.$file]=$targetDir.'/'.$file;
			}
		}

		array_push($subdirs,'photos');
		
		foreach ($subdirs as $subdir){
			makeDir("$newPilotDir/$subdir");
		}

		foreach ($filesToMove as $src=>$target){
			makeDir($target);
			@rename($src,$target);
		}
		*/
		
		// Take care of photos!
		if ($this->hasPhotos) {		
			$flightPhotos=new flightPhotos($this->flightID);
			$flightPhotos->getFromDB();
			
			// print_r($flightPhotos->photos );
			
			foreach ( $flightPhotos->photos as $photoNum=>$photoInfo) {			 
				$flightPhotos->changeUser($photoNum,getPilotID($newUserServerID,$newUserID) );
			}
			$flightPhotos->putToDB(0);
		}
		
		
		// store the original paths of the files
		$igcOrg=$this->getIGCFilename(0);		
				
		// store away the original userID
		$this->originalUserID=($this->userServerID+0).'_'.$this->userID;

		$this->userID=$newUserID;
		$this->userServerID=$newUserServerID;
		
		// now move the igc file (and optionally the olc if it exists
		@rename($igcOrg,$this->getIGCFilename(0));
		@rename($igcOrg.".olc",$this->getIGCFilename(0).".olc");
		//  echo "will put to db $newUserServerID $newUserID<BR>";
		$this->putFlightToDB(1);

		// take care of same flights (hide /unhide)
		$this->hideSameFlights();

		// now also log this action

	}


 } // end of class
?>
<?
//************************************************************************
// 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: FN_functions.php,v 1.84 2012/10/17 09:45:24 manolis Exp $                                                                 
//
//************************************************************************

require_once dirname(__FILE__)."/CL_auth.php"; 

function replace_spaces($str) {
	return str_replace(" ","&nbsp;",$str);
}

function htmlDecode($encoded) {
	return strtr($encoded,array_flip(get_html_translation_table(HTML_ENTITIES)));
}

function displayWeatherLegend(){
	return _CHANCES_TO_FLY." &#9730; - "._NONE." &#9729; - "._LOW." &#9728; - "._HIGH;   
}


if( !function_exists('str_ireplace') ){
 function str_ireplace($search,$replace,$subject){
   $token = chr(1);
   $haystack = strtolower($subject);
   $needle = strtolower($search);
   while (($pos=strpos($haystack,$needle))!==FALSE){
     $subject = substr_replace($subject,$token,$pos,strlen($search));
     $haystack = substr_replace($haystack,$token,$pos,strlen($search));
   }
   $subject = str_replace($token,$replace,$subject);
   return $subject;
 }
} 

function safeFilename($str){
	$str=str_replace('"','_',$str);
	$str=str_replace("'",'_',$str);
	$str=str_replace("`",'_',$str);
	$str=str_replace('/','_',$str);
	$str=str_replace('\\','_',$str);
	return $str;
}

function toLatin1($str,$enc=""){
	if ( ! preg_match("/[^\w\.\-\@\!\#\$\%\^\&\*\?\[\]\{\}\.\+\/]/",$str) ) {
		return $str;
	}
	//echo "non latin char in name<BR>";

	$orgNum=substr_count($str,"?");

	// check utf
	require_once dirname(__FILE__)."/lib/utf8_to_ascii/utf8_to_ascii.php";
	$newString=utf8_to_ascii($str);

	$newNum=substr_count($newString,"?");
	if ($newNum<=$orgNum) { // no extra ? were added, this was a valid utf string
		return $newString;
	}

	global $langEncodings,$nativeLanguage;
	if ($enc=="" ) $enc=$langEncodings[$nativeLanguage];

	require_once dirname(__FILE__)."/lib/ConvertCharset/ConvertCharset.class.php";
	$NewEncoding = new ConvertCharset;
	$str_utf8 = $NewEncoding->Convert($str, $enc, "utf-8", $Entities);	

	return utf8_to_ascii($str_utf8);

}

function fetchURL( $url, $timeout=5) {
	$url_parsed = parse_url(str_replace(' ','%20',$url) );
   $host = $url_parsed["host"];
   $port = $url_parsed["port"];
   if ($port==0)
       $port = 80;
   $path =$url_parsed["path"];
   if ($url_parsed["query"] != "")
       $path .= "?".$url_parsed["query"];

	/* this breaks things if the string is already rawurlencode
		$dirName=dirname($path);
		$fileName=basename($path);
		//echo "% $dirName%$fileName %";
		$path="$dirName/".rawurlencode($fileName);
		echo "@$path@";
	*/
	
   $out = "GET $path HTTP/1.0\r\nHost: $host\r\n";
   $out.= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0\r\n\r\n";

   $fp = @fsockopen($host, $port, $errno, $errstr, $timeout);
   if (!$fp) { return 0; }

   stream_set_timeout($fp,$timeout);

   if (fwrite($fp, $out)) {
	   $body = false;
	   while (!feof($fp)) {
		   if ( ! $s = fgets($fp, 1024) ) { 
				//echo "#"; 
				break; 
			}
		   if ( $body )
			   $in .= $s;
		   if ( $s == "\r\n" )
			   $body = true;
	   }
   }  else {
	//echo "$";
   }

   fclose($fp);
  
   return $in;
}

/**
 * Get a web file (HTML, XHTML, XML, image, etc.) from a URL.  Return an
 * array containing the HTTP server response header fields and content.
 */
function getWebpage( $url )
{
    $options = array(
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => false,    // don't return headers
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_ENCODING       => "",       // handle all encodings
        CURLOPT_USERAGENT      => "spider", // who am i
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
        CURLOPT_TIMEOUT        => 120,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
    );

    $ch      = curl_init( $url );
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );
    $err     = curl_errno( $ch );
    $errmsg  = curl_error( $ch );
    $header  = curl_getinfo( $ch );
    curl_close( $ch );

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;
    $header['content'] = $content;
    return $header;
}

function splitLines($line) {
 $max_line_len=35;
 $words=explode(" ",$line);
 $sline="";
 $i=0;
 foreach($words as $word) {
    if ($i==0) $sep=""; else $sep=" ";
	$i++;
 	$tmp_sline = $sline.$sep.$word;
	if ($nl_pos=strrchr($tmp_sline, 10)) $tmp_sline = substr($tmp_sline ,$nl_pos ); 

	if (strlen($tmp_sline) > $max_line_len ) {
	   $sline=$sline."\n".$word;	
	   $i=0;  
	} else {
	   $sline=$sline.$sep.$word;
	}

 }
 
 return $sline;
}

if( !function_exists('delDir') ){
	function delDir($dir){
	 if ( !is_dir($dir) ) return;
	 $current_dir = opendir($dir);
	 while($entryname = readdir($current_dir)){
		if(is_dir("$dir/$entryname") and ($entryname != "." and $entryname!="..")){
		   deldir("${dir}/${entryname}");
		}elseif($entryname != "." and $entryname!=".."){
		   unlink("${dir}/${entryname}");
		}
	 }
	 closedir($current_dir);
	 @rmdir(${dir});
	}
}

function makeDir($path, $rights = 0777) {
  	$folder_path = array( strstr($path, '.') ? dirname($path) : $path);

	if (version_compare(PHP_VERSION, '5.0.0', '>=')) {
		return @mkdir($path,$rights,true);
	}
	
	// else for php 4
	while(!@is_dir(dirname(end($folder_path)))
          && dirname(end($folder_path)) != '/'
          && dirname(end($folder_path)) != '.'
          && dirname(end($folder_path)) != '') {
    	array_push($folder_path, dirname(end($folder_path)));
	}
	while($parent_folder_path = array_pop($folder_path)) {
    	// if(!@mkdir($parent_folder_path, $rights)) return 0;
		@mkdir($parent_folder_path, $rights);
		//user_error("Can't create folder \"$parent_folder_path\".");
	}

	return 1;
}


function getBrowser() {
	//
	// Determine the Browser the User is using, because of some nasty incompatibilities.
	// Most of the methods used in this function are from phpMyAdmin. :)
	//
	if (!empty($_SERVER['HTTP_USER_AGENT'])) 
	{
		$HTTP_USER_AGENT = $_SERVER['HTTP_USER_AGENT'];
	} 
	else if (!empty($HTTP_SERVER_VARS['HTTP_USER_AGENT'])) 
	{
		$HTTP_USER_AGENT = $HTTP_SERVER_VARS['HTTP_USER_AGENT'];
	}
	else if (!isset($HTTP_USER_AGENT))
	{
		$HTTP_USER_AGENT = '';
	}

	if (preg_match('/Opera(\/| )([0-9].[0-9]{1,2})/', $HTTP_USER_AGENT, $log_version)) 
	{
		$browser_version = $log_version[2];
		$browser_agent = 'opera';
	} 
	else if (preg_match('/MSIE ([0-9].[0-9]{1,2})/', $HTTP_USER_AGENT, $log_version)) 
	{
		$browser_version = $log_version[1];
		$browser_agent = 'ie';
	} 
	else if (preg_match('/OmniWeb\/([0-9].[0-9]{1,2})/', $HTTP_USER_AGENT, $log_version)) 
	{
		$browser_version = $log_version[1];
		$browser_agent = 'omniweb';
	} 
	else if (preg_match('/Netscape([0-9]{1})/', $HTTP_USER_AGENT, $log_version)) 
	{
		$browser_version = $log_version[1];
		$browser_agent = 'netscape';
	} 
	else if (preg_match('/Mozilla\/([0-9].[0-9]{1,2})/', $HTTP_USER_AGENT, $log_version)) 
	{
		$browser_version = $log_version[1];
		$browser_agent = 'mozilla';
	} 
	else if (preg_match('/Konqueror\/([0-9].[0-9]{1,2})/', $HTTP_USER_AGENT, $log_version)) 
	{
		$browser_version = $log_version[1];
		$browser_agent = 'konqueror';
	} 
	else 
	{
		$browser_version = 0;
		$browser_agent = 'other';
	}
	return array($browser_agent,$browser_version);
}


/*****************************************************************
    File name: browser.php
    Author: Gary White
    Last modified: November 10, 2003
    
    **************************************************************

    Copyright (C) 2003  Gary White
    
    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, or (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details at:
    http://www.gnu.org/copyleft/gpl.html

    **************************************************************

    Browser class
    
    Identifies the user's Operating system, browser and version
    by parsing the HTTP_USER_AGENT string sent to the server
    
    Typical Usage:
    
        require_once($_SERVER['DOCUMENT_ROOT'].'/include/browser.php');
        $br = new Browser;
        echo "$br->Platform, $br->Name version $br->Version";
    
    For operating systems, it will correctly identify:
        Microsoft Windows
        MacIntosh
        Linux

    Anything not determined to be one of the above is considered to by Unix
    because most Unix based browsers seem to not report the operating system.
    The only known problem here is that, if a HTTP_USER_AGENT string does not
    contain the operating system, it will be identified as Unix. For unknown
    browsers, this may not be correct.
    
    For browsers, it should correctly identify all versions of:
        Amaya
        Galeon
        iCab
        Internet Explorer
            For AOL versions it will identify as Internet Explorer (AOL) and the version
            will be the AOL version instead of the IE version.
        Konqueror
        Lynx
        Mozilla
        Netscape Navigator/Communicator
        OmniWeb
        Opera
        Pocket Internet Explorer for handhelds
        Safari
        WebTV
*****************************************************************/

function findBrowserOS(){
	$agent = $_SERVER['HTTP_USER_AGENT'];

	// initialize properties
	$bd['platform'] = "Unknown";
	$bd['browser'] = "Unknown";
	$bd['version'] = "Unknown";


	// find operating system
	if (preg_match("/win/", $agent))
		$bd['platform'] = "Windows";
	elseif (preg_match("/mac/", $agent))
		$bd['platform'] = "MacIntosh";
	elseif (preg_match("/linux/", $agent))
		$bd['platform'] = "Linux";
	elseif (preg_match("/OS\/2/", $agent))
		$bd['platform'] = "OS/2";
	elseif (preg_match("/BeOS/", $agent))
		$bd['platform'] = "BeOS";

	// test for Opera        
	if (preg_match("/opera/",$agent)){
		$val = stristr($agent, "opera");
		if (preg_match("/\//", $val)){
			$val = explode("/",$val);
			$bd['browser'] = $val[0];
			$val = explode(" ",$val[1]);
			$bd['version'] = $val[0];
		}else{
			$val = explode(" ",stristr($val,"opera"));
			$bd['browser'] = $val[0];
			$bd['version'] = $val[1];
		}

	// test for WebTV
	}elseif(preg_match("/webtv/",$agent)){
		$val = explode("/",stristr($agent,"webtv"));
		$bd['browser'] = $val[0];
		$bd['version'] = $val[1];
	
	// test for MS Internet Explorer version 1
	}elseif(preg_match("/microsoft internet explorer/", $agent)){
		$bd['browser'] = "MSIE";
		$bd['version'] = "1.0";
		$var = stristr($agent, "/");
		if (preg_match("/308|425|426|474|0b1/", $var)){
			$bd['version'] = "1.5";
		}

	// test for NetPositive
	}elseif(preg_match("/NetPositive/", $agent)){
		$val = explode("/",stristr($agent,"NetPositive"));
		$bd['platform'] = "BeOS";
		$bd['browser'] = $val[0];
		$bd['version'] = $val[1];

	// test for MS Internet Explorer
	}elseif(preg_match("/msie/",$agent) && !preg_match("/opera/",$agent)){
		$val = explode(" ",stristr($agent,"msie"));
		$bd['browser'] = $val[0];
		$bd['version'] = $val[1];
	
	// test for MS Pocket Internet Explorer
	}elseif(preg_match("/mspie/",$agent) || preg_match('/pocket/', $agent)){
		$val = explode(" ",stristr($agent,"mspie"));
		$bd['browser'] = "MSPIE";
		$bd['platform'] = "WindowsCE";
		if (preg_match("/mspie/", $agent))
			$bd['version'] = $val[1];
		else {
			$val = explode("/",$agent);
			$bd['version'] = $val[1];
		}
		
	// test for Galeon
	}elseif(preg_match("/galeon/",$agent)){
		$val = explode(" ",stristr($agent,"galeon"));
		$val = explode("/",$val[0]);
		$bd['browser'] = $val[0];
		$bd['version'] = $val[1];
		
	// test for Konqueror
	}elseif(preg_match("/Konqueror/",$agent)){
		$val = explode(" ",stristr($agent,"Konqueror"));
		$val = explode("/",$val[0]);
		$bd['browser'] = $val[0];
		$bd['version'] = $val[1];
		
	// test for iCab
	}elseif(preg_match("/icab/",$agent)){
		$val = explode(" ",stristr($agent,"icab"));
		$bd['browser'] = $val[0];
		$bd['version'] = $val[1];

	// test for OmniWeb
	}elseif(preg_match("/omniweb/",$agent)){
		$val = explode("/",stristr($agent,"omniweb"));
		$bd['browser'] = $val[0];
		$bd['version'] = $val[1];

	// test for Phoenix
	}elseif(preg_match("/Phoenix/", $agent)){
		$bd['browser'] = "Phoenix";
		$val = explode("/", stristr($agent,"Phoenix/"));
		$bd['version'] = $val[1];
	
	// test for Firebird
	}elseif(preg_match("/firebird/", $agent)){
		$bd['browser']="Firebird";
		$val = stristr($agent, "Firebird");
		$val = explode("/",$val);
		$bd['version'] = $val[1];
		
	// test for Firefox
	}elseif(preg_match("/Firefox/", $agent)){
		$bd['browser']="Firefox";
		$val = stristr($agent, "Firefox");
		$val = explode("/",$val);
		$bd['version'] = $val[1];
		
  // test for Mozilla Alpha/Beta Versions
	}elseif(preg_match("/mozilla/",$agent) && 
		preg_match("/rv:[0-9].[0-9][a-b]/",$agent) && !preg_match("/netscape/",$agent)){
		$bd['browser'] = "Mozilla";
		$val = explode(" ",stristr($agent,"rv:"));
		preg_match("/rv:[0-9].[0-9][a-b]/",$agent,$val);
		$bd['version'] = str_replace("rv:","",$val[0]);
		
	// test for Mozilla Stable Versions
	}elseif(preg_match("/mozilla/",$agent) &&
		preg_match("/rv:[0-9]\.[0-9]/",$agent) && !preg_match("/netscape/",$agent)){
		$bd['browser'] = "Mozilla";
		$val = explode(" ",stristr($agent,"rv:"));
		preg_match("/rv:[0-9]\.[0-9]\.[0-9]/",$agent,$val);
		$bd['version'] = str_replace("rv:","",$val[0]);
	
	// test for Lynx & Amaya
	}elseif(preg_match("/libwww/", $agent)){
		if (preg_match("/amaya/", $agent)){
			$val = explode("/",stristr($agent,"amaya"));
			$bd['browser'] = "Amaya";
			$val = explode(" ", $val[1]);
			$bd['version'] = $val[0];
		} else {
			$val = explode("/",$agent);
			$bd['browser'] = "Lynx";
			$bd['version'] = $val[1];
		}
	
	// test for Safari
	}elseif(preg_match("/safari/", $agent)){
		$bd['browser'] = "Safari";
		$bd['version'] = "";

	// remaining two tests are for Netscape
	}elseif(preg_match("/netscape/",$agent)){
		$val = explode(" ",stristr($agent,"netscape"));
		$val = explode("/",$val[0]);
		$bd['browser'] = $val[0];
		$bd['version'] = $val[1];
	}elseif(preg_match("/mozilla/",$agent) && !preg_match("/rv:[0-9]\.[0-9]\.[0-9]/",$agent)){
		$val = explode(" ",stristr($agent,"mozilla"));
		$val = explode("/",$val[0]);
		$bd['browser'] = "Netscape";
		$bd['version'] = $val[1];
	}
	
	// clean up extraneous garbage that may be in the name
	$bd['browser'] = preg_replace("[^a-z,A-Z]", "", $bd['browser']);
	// clean up extraneous garbage that may be in the version        
	$bd['version'] = preg_replace("[^0-9,.,a-z,A-Z]", "", $bd['version']);
	
	// check for AOL
	if (preg_match("/AOL/", $agent)){
		$var = stristr($agent, "AOL");
		$var = explode(" ", $var);
		$bd['aol'] = preg_replace("[^0-9,.,a-z,A-Z]", "", $var[1]);
	}
	
	// finally assign our properties
	return array( $bd['browser'],$bd['version'],$bd['platform'],$bd['aol']);
}

	function prep_for_DB($str) {
		return		str_replace("'","&#039;",$str);
	}
	function prep_from_DB($str) {
		return		str_replace("&#039;","'",$str);
	}


	function fill_year_month_array($first_month ,$last_month){
		$year_month_array=array();

		$start_year=substr($first_month,0,4);
		$start_month=substr($first_month,5,2);
		$end_year=substr($last_month,0,4);
		$end_month=substr($last_month,5,2);


		for($y=$start_year;$y<=$end_year;$y++) {
			for($m=1;$m<=12;$m++) {
				array_push($year_month_array,sprintf("%04d-%02d",$y,$m));
			}
		}
		return 		$year_month_array;
	}

	function DEBUG($debugCat,$debugLevel,$msg ) {
		global $DBGcat,$DBGlvl,$DEBUG_OUTPUT;
		if ( ($DBGcat == $debugCat || $DBGcat=='' ) && $DBGlvl & $debugLevel  ) 
			$DEBUG_OUTPUT.=$msg;
	}
	
	function DEBUG_END() {
		global $DEBUG_OUTPUT;
		if ($DEBUG_OUTPUT) { 
			echo "<div id='debugTitleDiv'><STRONG><a href='javascript:toggleVisibility(\"debugDiv\");'>DEBUG OUTPUT</a></STRONG></div>";
			echo "<div id='debugDiv'>$DEBUG_OUTPUT</div>";
			$DEBUG_OUTPUT="";
		}
	}

	function setDEBUGfromGET(){
		global $DBGcat,$DBGlvl;
		if ( $_GET['DBGcat'] ) $DBGcat=makeSane($_GET['DBGcat'] );
		if ( $_GET['DBGlvl'] ) $DBGlvl=makeSane($_GET['DBGlvl'],1) ;		
	}

	function getAvailableThemes() {
		 $res=array();
		 $dir=dirname(__FILE__)."/templates";
		 $current_dir = opendir($dir);
		 while($entryname = readdir($current_dir)){
			if( is_dir($dir ."/".$entryname) && ($entryname != "." and $entryname!=".." and strtolower($entryname)!="cvs" )){
			   array_push($res,$entryname);
			}
		 }
		 closedir($current_dir);
		 return $res;
	}

	# Compares versions of software
	# versions must must use the format ' x.y.z... ' 
	# where (x, y, z) are numbers in [0-9]
	function check_version($currentversion, $requiredversion)
	{
		list($majorC, $minorC, $editC) = explode('[/.-]', $currentversion);
		list($majorR, $minorR, $editR) = explode('[/.-]', $requiredversion);
		
		if ($majorC > $majorR) return true;
		if ($majorC < $majorR) return false;
		// same major - check ninor
		if ($minorC > $minorR) return true;
		if ($minorC < $minorR) return false;
		// and same minor
		if ($editC  >= $editR)  return true;
		return false;
	}

	/*  gets as string the normal screen output of php file  */
	function get_include_contents($filename) {
		if (is_file($filename)) {
			ob_start();
			include $filename;
			$contents = ob_get_contents();
			ob_end_clean();
			return $contents;
		}
		return "";
	}
	
	function getExtrernalServerTakeoffs($serverID,$lat,$lon,$limitKm,$limitNum ) {
		global $xmlSites;
		$takeoffServers=array(
			1=>array(
				/* calling  method */
				"callUrl"=>"http://www.paraglidingearth.com/api/getAroundLatLngSites.php?",
				"latArg"=>"lat",
				"lonArg"=>"lng",
				"limitKmArg"=>"distance",
				"limitNumArg"=>"limit",
				/* parsing of results */
				"XML_name"=>"name",
				"XML_distance"=>"distance",
				"XML_area"=>"area",
				"XML_countryCode"=>"countryCode",
				//"XML_url"=>"pe_link",
				"XML_url"=>"pge_link",
				"XML_lat"=>"lat",
				"XML_lon"=>"lng",
				),
			2=>array(
				/* calling  method */
				"callUrl"=>"http://www.paragliding365.com/paragliding_sites_xml.html?type=mini&",
				"latArg"=>"latitude",
				"lonArg"=>"longitude",
				"limitKmArg"=>"radius",
				"limitNumArg"=>"notused",
				/* parsing of results */
				"XML_name"=>"name",
				"XML_distance"=>"distance",
				"XML_area"=>"location",
				"XML_countryCode"=>"iso",
				"XML_url"=>"link",						
				"XML_lat"=>"lat",
				"XML_lon"=>"lng",
			)
			

		);	
				
		
		$getXMLurl=$takeoffServers[$serverID]["callUrl"].
					$takeoffServers[$serverID]["latArg"]."=$lat&".
					$takeoffServers[$serverID]["lonArg"]."=$lon&".
					$takeoffServers[$serverID]["limitKmArg"]."=$limitKm&".
					$takeoffServers[$serverID]["limitNumArg"]."=$limitNum";
		
		//echo 	$getXMLurl;
		
		$xmlSites=fetchURL($getXMLurl);		
		if ($xmlSites) {
			require_once dirname(__FILE__).'/lib/miniXML/minixml.inc.php';
			$xmlDoc = new MiniXMLDoc();
			$xmlDoc->fromString($xmlSites);
			$xmlArray = $xmlDoc->toArray();

			$takeoffsNum=0;
			$takoffsList=array();
			// print_r($xmlArray);
			
			if ($serverID==1) { // paraglidingearth.com
				if (is_array($xmlArray['search'])) {
					if (is_array($xmlArray['search']['takeoff'][0])) 
						$arrayToUse=$xmlArray['search']['takeoff'];
					else
						$arrayToUse=$xmlArray['search'];
				} else {
					$arrayToUse=0;
				}
			} else if ($serverID==2) { //paragliding365.com
				if ($xmlArray['root']['flightareas']['flightarea']) {
					if ( is_array($xmlArray['root']['flightareas']['flightarea'][0] ) )
						$arrayToUse=$xmlArray['root']['flightareas']['flightarea'];
					else
						$arrayToUse=$xmlArray['root']['flightareas'];
				} else $arrayToUse=0;
			} else {
				$arrayToUse=0;
			}
	
			$takeoffsList=array();
			$takeoffsNum=0;
			if ($arrayToUse) {
				//echo "#";
				//print_r($arrayToUse);
				foreach ($arrayToUse as $flightareaNum=>$flightarea) {
					$XML_name=$takeoffServers[$serverID]["XML_name"];
					$XML_distance=$takeoffServers[$serverID]["XML_distance"];
					$XML_area=$takeoffServers[$serverID]["XML_area"];
					$XML_countryCode=$takeoffServers[$serverID]["XML_countryCode"];
					$XML_url=$takeoffServers[$serverID]["XML_url"];
					$XML_lat=$takeoffServers[$serverID]["XML_lat"];
					$XML_lon=$takeoffServers[$serverID]["XML_lon"];
					if ( $flightareaNum!=="_num" && $flightarea[$XML_name]) {							
							$distance=$flightarea[$XML_distance]; 
							if ($distance>$limitKm*1000) continue;
							$takeoffsList[$takeoffsNum]['distance']=$flightarea[$XML_distance]; 
							$takeoffsList[$takeoffsNum]['name']=$flightarea[$XML_name]; 
							$takeoffsList[$takeoffsNum]['area']=$flightarea[$XML_area]; 
							$takeoffsList[$takeoffsNum]['countryCode']=$flightarea[$XML_countryCode]; 
							$takeoffsList[$takeoffsNum]['url']=$flightarea[$XML_url]; 
							$takeoffsList[$takeoffsNum]['lat']=$flightarea[$XML_lat]; 
							$takeoffsList[$takeoffsNum]['lon']=$flightarea[$XML_lon]; 
							$takeoffsNum++;
							if ($takeoffsNum==$limitNum) break;
					}
				}
		  }

		  return $takeoffsList;
		} // if we have content
		return array();
	
	}
	
	// google maps polyline encoding
	function encodeNumber($num) {
		//    printf("%f = ", $num);
		$sgn_num = (int)($num * 100000);
		$sgn_num = ($sgn_num<<1);
		if ($num<0) {
			$sgn_num = ~$sgn_num;
		}
	
		while ($sgn_num >= 0x20) {
			$t = ( 0x20 | ($sgn_num & 0x1f)) + 63;
			$res.=sprintf("%c", $t);
			$sgn_num >>= 5;
		}
		$t = $sgn_num + 63;
		return $res.sprintf("%c", $t);
	}

function  writeFile($filename,$str){
	if (! $fp=fopen($filename,"w") ) return 0;
    if (!fwrite($fp,$str)) return 0;
	fclose($fp);
	return 1;
}

function splitServerPilotStr($str) {
	$serverID=0;
	if (  count($pilotPartsArray=explode('_',$str)) >1 ) {
		$serverID=$pilotPartsArray[0];
		$pilotID=$pilotPartsArray[1];
	} else $pilotID=$str+0;

	return array($serverID,$pilotID);
}


// Color handling functions
function RGB($r, $g, $b) { return array($r, $g, $b);}

function AllocateColorMap($image, $array, &$colorMap) {
    for ($index = 0; $index < count($array); $index++) {
        $colorMap[$index] = imagecolorallocate($image,
                                               $array[$index]['R'],
                                               $array[$index]['G'],
                                               $array[$index]['B']);
    }
}

function InterpolateRGB(&$array, $startRGB, $endRGB, $startIdx, $endIdx)
{
    InterpolateArray($rArray, $startRGB[0], $endRGB[0], $startIdx, $endIdx);
    InterpolateArray($gArray, $startRGB[1], $endRGB[1], $startIdx, $endIdx);
    InterpolateArray($bArray, $startRGB[2], $endRGB[2], $startIdx, $endIdx);

    for ($index = $startIdx; $index < $endIdx; $index++) {
        $array[$index]['R'] = $rArray[$index];
        $array[$index]['G'] = $gArray[$index];
        $array[$index]['B'] = $bArray[$index];
    }
}

function InterpolateArray(&$array, $startVal, $endVal, $startIdx, $endIdx)
{
    if ($endIdx <= $startIdx) return;

    $step = ($endVal - $startVal) / ($endIdx - $startIdx);

    for ($index = $startIdx; $index < $endIdx; $index++) {
        $array[$index] = (int)round($startVal);
        $startVal += $step;
    }

    $array[$endIdx] = $endVal;
}



$dec2fracArray=array(
	'0.04'=>array(1,25),
	'0.05'=>array(1,20),
	'0.0666'=>array(1,15),
	'0.083'=>array(1,12),
	'0.111111111'=>array(1,9),
	'0.125'=>array(1,8),
	'0.142857143'=>array(1,7),
	'0.166666667'=>array(1,6),
	'0.2'=>array(1,5),
	'0.222222222'=>array(2,9),
	'0.25'=>array(1,4),
	'0.285714286'=>array(2,7),
	'0.333333333'=>array(1,3),
	'0.375'=>array(3,8),
	'0.4'=>array(2,5),
	'0.428571429'=>array(3,7),
	'0.444444444'=>array(4,9),
	'0.5'=>array(1,2),
	'0.555555556'=>array(5,9),
	'0.571428571'=>array(4,7),
	'0.6'=>array(3,5),
	'0.625'=>array(5,8),
	'0.666666667'=>array(2,3),
	'0.714285714'=>array(5,7),
	'0.75'=>array(3,4),
	'0.777777778'=>array(7,9),
	'0.8'=>array(4,5),
	'0.833333333'=>array(5,6),
	'0.857142857'=>array(6,7),
	'0.875'=>array(7,8),
	'0.888888889'=>array(8,9),
	'0.916666667'=>array(11,12),
);


$reduceArray=array();
$reduceArray[2][5]=array(1,0,0,1,0);
$reduceArray[3][5]=array(1,0,1,0,1);
$reduceArray[2][7]=array(1,0,0,0,1,0,0);
$reduceArray[3][7]=array(1,0,0,1,0,1,0);
$reduceArray[4][7]=array(1,0,1,0,1,0,1);
$reduceArray[5][7]=array(1,0,1,1,1,0,1);
$reduceArray[3][8]=array(1,0,0,1,0,0,1,0);
$reduceArray[5][8]=array(1,0,1,1,1,0,1,0);
$reduceArray[2][9]=array(1,0,0,0,1,0,0,0,0);
$reduceArray[4][9]=array(1,0,1,0,1,0,1,0,0);
$reduceArray[5][9]=array(1,0,1,0,1,1,0,1,0);
$reduceArray[7][9]=array(1,0,1,1,1,0,1,1,1);

function dec2frac($dec){
	global $dec2fracArray;
	$minD=1;
	$selD=0;
	foreach($dec2fracArray as $d=>$dArr){		
		//echo $d.'#';
		if ( abs($d-$dec) < $minD) {
			$minD=abs($d-$dec);
			$selD=$d;
		}
	}
	return array($dec2fracArray[$selD][0],$dec2fracArray[$selD][1]);
}

function getReduceArray($pointsNum,$maxPointsNum) {
	global $reduceArray;

	if ($pointsNum<=$maxPointsNum) return array(1);
	$ratio=$maxPointsNum/$pointsNum;
	list($n,$d)=dec2frac($ratio);

	//echo "$pointsNum / $maxPointsNum have ratio=$ratio, we selected ".($n/$d)." with $n/$d<BR>";
	//echo "it will result in ".$pointsNum*($n/$d)." points<BR>";

	$arr=array();
	if ($n==1) { //simple case mod $d
		$arr[0]=1;
		for ($i=1;$i<$d;$i++) {
			$arr[$i]=0;
		}
	} else if ( ($d-$n)==1) { // also simple fill in all slots with 1 except last
		for ($i=0;$i<$d-1;$i++) {
			$arr[$i]=1;
		}
		$arr[$d-1]=0;
	} else {
		if ( is_array($reduceArray[$n][$d]) ) {
			return $reduceArray[$n][$d];
		} else {
			echo "getReduceArray() internal error<BR>";
		}
	}

	return $arr;
}

function pilotServerCmp($a, $b) { 
	global $CONF;
	
	$aPos=$CONF['servers']['pilotServerOrder'][$a];
	if (!$aPos)  $aPos=999;
	$bPos=$CONF['servers']['pilotServerOrder'][$b];
	if (!$bPos)  $bPos=999;

	if ($aPos == $bPos) { 
	   if ($a == $b) return 0;
	   return ($a > $b) ? 1 : -1; 
   } 
   return ($aPos > $bPos) ? 1 : -1; 
} 

function sameFlightsCmp($a, $b) { 
	// we must disable all flights BUT one
	// rules: 
	// 1. locally submitted flights have priority
	// 2. between external flights , the full synced have priority over simple links
	// 3. between equal cases the first submitted has priority.

	// locally vs non-local
	if ($a['serverID'] ==0 && $b['serverID']!=0 ) return -1;	 // local flight  ($a) is better
	if ($b['serverID'] ==0 && $a['serverID']!=0 ) return 1; // local flight  ($b) is better

	// both locals	
	if ($a['serverID'] ==0 && $b['serverID']==0 ) {
		if ( $a['ID'] < $b['ID'] ) return -1; // smallest ID ($a) is better
		else return 1;
	}
	
	// both externals
	
	if ( $a['externalFlightType'] ==2 && $b['externalFlightType']!=2 ) return -1;	 // ext type 2 ($a) is better
	if ( $b['externalFlightType'] ==2 && $a['externalFlightType']!=2 ) return 1;	 // ext type 2 ($b) is better
	
	
	//final compare the ids again 
	if ( $a['ID'] < $b['ID'] ) return -1; // smallest ID ($a) is better
	else return 1;
	
}

function sendMail($to,$subject,$msg) {
	$headers ="MIME-Version: 1.0\r\n";
	$headers.="Content-type: text/html; charset=iso-8859-1\r\n";
	$headers.="From: $CONF_admin_email \r\n";
			
	mail($to,$_SERVER['SERVER_NAME'].": $subject",$msg,$headers);
}


function sendMailToAdmin($subject,$msg) {
	global  $CONF_admin_email;
	sendMail($CONF_admin_email,$subject,$msg) ;
	
}

function makeHash($scriptName)  {
	return md5($CONF_SitePassword.$scriptName);
}


function get_absolute_path($path) {
	$path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path);
	$parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen');
	$absolutes = array();
	foreach ($parts as $part) {
		if ('.' == $part) continue;
		if ('..' == $part) {
			array_pop($absolutes);
		} else {
			$absolutes[] = $part;
		}
	}
	if ($path{0}=='/') 
		return '/'.implode(DIRECTORY_SEPARATOR, $absolutes);
	else
		return implode(DIRECTORY_SEPARATOR, $absolutes);
}
	
function isPrint() {	
	global $RUN;
	
	if( $RUN['view']=='print') return 1;
	else return 0;

}
// the method to use for links in the menu and other parts of leonardo
// 1 -> current old way, using &name=value args + session variables
// 2 -> same as 1 but all sessions vars are in the url, this means that the url 
// 3 -> SEO urls

function getLeonardoLink($argArray,$forcedLinkType=0) {
	global $CONF,$lng, $lang2iso,$op;
	
	$linkType=	$CONF['links']['type'];
	if ($forcedLinkType) $linkType=$forcedLinkType;
	
	if (in_array($argArray['op'],array('login','admin','conf_htaccess') ) ) {
		$linkType=1;
	}
	
	// echo "#".$_SESSION['fltr'];
	$filterArg='';
	if ($_SESSION['fltr'] && !in_array("fltr",$argArray,true )) {
		if ( in_array($argArray['op'],array('comp','competition','list_takeoffs','list_forecasts','list_detailed_forecasts','list_flights','list_pilots','pilot_profile_stats','explore_ge'))  ) {
			$filterArg.="&fltr=".$_SESSION['fltr'];				
		}
	}
	
	if ($linkType==1) {		
		foreach($argArray as $argName=>$argValue){
			if ($argValue!='useCurrent')
				$args.='&'.$argName.'='.($argValue!='skipValue'?$argValue:'');
		}	
		$args.=$filterArg;
		
		return CONF_MODULE_ARG.$args;

	} else 	if ($linkType==2) {
		global $op,$rank,$subrank;
		global $year,$month,$day,$season,$pilotID,$takeoffID,$country,$cat,$clubID;
		global $brandID,$filter01,$xctype,$class;
		
		global $nacid,$nacclubid;

		foreach($argArray as $argName=>$argValue){
			if ($argValue=='useCurrent') {
				global ${$argName};
				$args.='&'.$argName.'='.${$argName};
			} else {
				$args.='&'.$argName.'='.($argValue!='skipValue'?$argValue:'');
			}	
		}	
		$args.=$filterArg;
		
		$thisURL=CONF_MODULE_ARG.$args;
		if ($op=="comp") 
			$thisURL.="&rank=$rank&subrank=$subrank&year=$year&season=$season";
		else
			$thisURL.="&year=$year&month=$month&day=$day&season=$season&pilotID=$pilotID&takeoffID=$takeoffID&country=$country&cat=$cat&clubID=$clubID";
		
		if ($op=="list_flights")
					$thisURL.="&brandID=$brandID&filter01=$filter01&xctype=$xctype&class=$class";
					 			
		# Martin Jursa 25.05.2007: support for nacclub-filtering
		if (!empty($CONF_use_NAC)) {
			if ($nacclubid && $nacid) {
				$thisURL.="&nacid=$nacid&nacclubid=$nacclubid";
			}
		}
		return $thisURL;
		
	} else 	if ($linkType==3) { // SEO URLS 
		global $op,$rank,$subrank;
		global $year,$month,$day,$season,$serverID,$serverIDview,$pilotID,$pilotIDview;
		global $takeoffID,$country,$cat,$class,$xctype,$clubID;
		global $nacid,$nacclubid,$brandID;

		if ($argArray['op']=='useCurrent') {
			$argArray['op']=$op;
		}
			
		if ( isset($argArray['lng']) ){
			if (strpos($_SERVER['REQUEST_URI'],'&lng=') ) 
				return preg_replace('/&lng=(\w+)/','&lng='.$argArray['lng'],$_SERVER['REQUEST_URI']);
			else 
				return $_SERVER['REQUEST_URI'].'&lng='.$argArray['lng'];
			$lngCode= $lang2iso[$argArray['lng']];							
		} else {
			$lngCode= $lang2iso[$lng];
		}	
		
		// $args=$lngCode.'/';
		$args='';
		$opProccessed=1;
		
		//unset($argArray['lng']);
		//unset($argArray['newlang']);
		
		$opTmp=$argArray['op'];
		if ($opTmp=='list_flights') {
			//$args.='tracks/';
			$args.='loty/';
		} else if ($opTmp=='explore_ge') {
			$args.='ge/';
		} else if ($opTmp=='list_pilots') {
//			$args.='pilots/';
			$args.='piloci/';
		} else if ($opTmp=='competition') {
			//$args.='league/';
			$args.='liga/';
		} else if ($opTmp=='comp') {
//			$args.='ranks/';
			$args.='rankingi/';
		} else if ($opTmp=='list_takeoffs') {
			$args.='startowiska/';
		} else if ($opTmp=='list_forecasts') {
			$args.='prognozy/';
		} else if ($opTmp=='list_detailed_forecasts') {
			$args.='prognozy_szczegolowe/';
		} else if ($opTmp=='compare') {
//			$args.='compare/';
			$args.='porownanie/';
			if ($argArray['flightID']!='skipValue') {
				$args.=$argArray['flightID'];
				unset($argArray['flightID']);
			}	
		} else if ($opTmp=='compare3d') { 
//						$args.='compare3d/';
						$args.='porownanie3d/';
			if ($argArray['flightID']!='skipValue') {
				$args.=$argArray['flightID'];
				unset($argArray['flightID']);
			}			
		} else if ($opTmp=='pilot_profile') {			
			// if (!$argArray['pilotIDview']) $argArray['pilotIDview']=$pilotIDview;			
			$args.='pilot/'.$argArray['pilotIDview'];
			return $CONF['links']['baseURL'].'/'.$args;
						
		} else if ($opTmp=='pilot_profile_stats') {
			
			// $args.='pilot/'.($serverIDview+0).'_'.($pilotIDview).'/stats/';
//			$args.='stats/';
			$args.='statystyki/';
			// $args.='pilot/'.$argArray['pilotIDview'].'/stats/';
			
			// return $CONF['links']['baseURL'].'/'.$args;
			
		} else if ($opTmp=='show_waypoint') {
//			$args.='takeoff/'.$argArray['waypointIDview'];
			$args.='startowisko/'.$argArray['waypointIDview'];
			return $CONF['links']['baseURL'].'/'.$args;
			
		} else if ($opTmp=="area_show"){
			$args.='rejon/'.$argArray['areaID'];
			return $CONF['links']['baseURL'].'/'.$args;
		} else if ($opTmp=='show_flight') {	
//			$args.='flight/';	
			$args.='lot/';	
			if ($argArray['flightID']!='skipValue') {
				$args.=$argArray['flightID'];
				unset($argArray['flightID']);
			}	
			// return $CONF['links']['baseURL'].'/'.$args;
		} else {
			$opProccessed=0;
		}
		
		
		$listings=array('list_flights','list_takeoffs','list_forecasts','list_detailed_forecasts','list_pilots','competition','comp','pilot_profile_stats');
		$args2process=array('year','month','day','season','country','rank','subrank','cat','class','xctype','brandID',
							'clubID','nacclubid','nacid','pilotID','takeoffID');
		$args2Array=array();
		
		$isListing=false;
		// add the date, country in the path
		if (in_array($opTmp ,$listings)) {
			$isListing=true;
			//see if we have some variables in the argArray		
			foreach($args2process as $argName) {
				if (isset($argArray[$argName])){
					$args2Array[$argName]=$argArray[$argName];				
				}else {				
					if ( $argName=='pilotID') {
						//if ($opTmp!='pilot_profile_stats' ) {
							$args2Array[$argName]=($serverID+0).'_'.($pilotID);
						//}					
					}else {
						$args2Array[$argName]=${$argName};
					}	
				}
			}

			if ($opTmp=='comp') {
				if ($args2Array['rank']){
					$args.=$args2Array['rank'].'.'.$args2Array['subrank'].'/';
				} else {
					$args.='all/';
				}
				unset($argArray['clubID']);
				unset($argArray['nacid']);
				unset($argArray['nacclubid']);
			} else {
				if ($args2Array['country']){
					$args.=$args2Array['country'].'/';
				} else {
					$args.='world/';
//					$args.='swiat/';
				}			
			}
						
			if ($args2Array['season']) {
				$args.='season'.$args2Array['season'].'/';
			} else {
				if ($args2Array['year'] && $args2Array['month'] && $args2Array['day']) {
					//printf("date : %04d.%02d.%02d ***",
					//	$args2Array['year'],$args2Array['month'],$args2Array['day']);

						//$args.=sprintf("%04d.%02d.%02d",
						//$args2Array['year'],$args2Array['month'],$args2Array['day']).'/';
					$args.=$args2Array['year'].'.'.$args2Array['month'].'.'.$args2Array['day'].'/';
				} else if ($args2Array['year'] && $args2Array['month'])
					$args.=$args2Array['year'].'.'.$args2Array['month'].'/';
				else if ($args2Array['year']  )
					$args.=$args2Array['year'].'/';	
				else 
					$args.='alltimes/';
			}

			$args.='brand:'.($args2Array['brandID']?$args2Array['brandID']:'all').',';
			
			if ($opTmp=='comp') {
			
			} else {
				$args.='cat:'.$args2Array['cat'].',';
			}
			
			if ($opTmp!='list_takeoffs' && $opTmp!='comp' && $opTmp!='list_forecasts' && $opTmp!='list_detailed_forecasts') {
				$args.='class:'.($args2Array['class']?$args2Array['class']:'all').',';
				$args.='xctype:'.($args2Array['xctype']?$args2Array['xctype']:'all').',';
			}
						
			if ( $args2Array['nacid'] && $args2Array['nacclubid'] ) {
				$args.='club:'.$args2Array['nacid'].'.'.$args2Array['nacclubid'];
			} else {
				$args.='club:'.($args2Array['clubID']?'0.'.$args2Array['clubID']:'all');
			}
			
			if ($opTmp!='comp' &&  $opTmp!='competition') {
				
				if ($opTmp!='list_pilots' )
					$args.=',pilot:'.($args2Array['pilotID']?$args2Array['pilotID']:'all');			
				if ( $opTmp!='list_takeoffs' && $opTmp!='list_forecasts' && $opTmp!='list_detailed_forecasts' )	
					$args.=',takeoff:'.($args2Array['takeoffID']?$args2Array['takeoffID']:'all');
			}
						
			unset($argArray['nacclubid']);
			unset($argArray['nacid']);
			unset($argArray['takeoffID']);
			unset($argArray['pilotID']);			
			unset($argArray['clubID']);
			unset($argArray['brandID']);
			unset($argArray['cat']);
			unset($argArray['class']);
			unset($argArray['xctype']);
			unset($argArray['season']);
			unset($argArray['year']);
			unset($argArray['month']);	
			unset($argArray['day']);
			unset($argArray['country']);
			unset($argArray['rank']);
			unset($argArray['subrank']);
		}

		if (!$opProccessed && isset($argArray['op'])  ) {
//			$args.='page/'.$argArray['op'];
			$args.='strona/'.$argArray['op'];
			unset($argArray['op']);
		}
		if ($argArray['print']) {
			$isPrint=1;
			unset($argArray['print']);
		}
		$i=0;
		foreach($argArray as $argName=>$argValue){
			if ($argName!='op' ) {
				// if (! $isListing || $i>0) $args.='&';
				$args.='&'.$argName.'='.($argValue!='skipValue'?($argValue):'');

			}	
		}
		$args.=$filterArg;
		
		if ($isPrint) $args.='&print';
		 
		return $CONF['links']['baseURL'].'/'.$args;
	}
}


// the method to use for download.php
// 1 -> current old way, using &name=value args + session variables
// 2 -> same as 1 but all sessions vars are in the url, this means that the url 
// 3 -> SEO urls

function getDownloadLink($argArray) {
	global $CONF,$lng, $lang2iso;
	
	if ($CONF['links']['type']==1 || $CONF['links']['type']== 2 ) {
	
		foreach($argArray as $argName=>$argValue){
			if ($argName!='type')
				$args.='&'.$argName.'='.($argValue!='skipValue'?$argValue:'');
		}	
		return $CONF['protocol'].'://'.$_SERVER['SERVER_NAME'].getRelMainDir().'download.php?type='.$argArray['type'].$args;
		
	} else 	if ($CONF['links']['type']==3) {


		// $args=$lngCode.'/';
		$args='';
		$opProccessed=1;
		
		// "kml_task","kml_trk","kml_trk_color","kml_wpt","sites","igc"
		$opTmp=$argArray['type'];
		if ($opTmp=='igc') {
			$args.='lot/'.$argArray['flightID'].'/igc/';
			unset($argArray['flightID']);
		} else if ($opTmp=='kml_trk') {		
			$args.='lot/'.$argArray['flightID'].'/kml/';
			unset($argArray['flightID']);
		} else if ($opTmp=='kml_wpt') {		
			$args.='startowisko/'.$argArray['wptID'].'/kml/';
			unset($argArray['wptID']);
		} else {
		
			$opProccessed=0;
		}
			
		
		foreach($argArray as $argName=>$argValue){
			if ($argName!='type' || !$opProccessed )
				$args.='&'.$argName.'='.($argValue!='skipValue'?$argValue:'');
		}
	
		return $CONF['links']['baseURL'].'/'.$args;
	}
}

function getClientIpAddr()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}
?>
<?
//************************************************************************
// 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: FN_UTM.php,v 1.3 2010/03/14 20:56:10 manolis Exp $                                                                 
//
//************************************************************************

   function calc_distance($lat1, $lon1,$lat2, $lon2) { // in metern 
	  // echo "calc_distance : ".$lat1." ".$lon1." ".$lat2." ".$lon2."<br>";
  	  $pi_div_180 = M_PI/180.0;
      $d_fak = 6371000.0;  // FAI Erdradius in Metern 
//      $d_fak = 6378137.0;
 //     $d_fak = 6356752.0;
	  //  Radius nautischen Meilen: ((double)1852.0)*((double)60.0)*((double)180.0)/((double)M_PI); 
      $d2    = 2.0;
      $latx = $lat1 * $pi_div_180; 
	  $lonx = $lon1 * $pi_div_180;
      $laty = $lat2 * $pi_div_180; 
	  $lony = $lon2 * $pi_div_180;
      $sinlat = sin(($latx-$laty)/$d2);
      $sinlon = sin(($lonx-$lony)/$d2);
      return $d2*asin(sqrt( $sinlat*$sinlat + $sinlon*$sinlon*cos($latx)*cos($laty)))*$d_fak;
   }

global  $EQ_RAD, $f, $ECC2, $ECC4, $ECC6, $ECC, $e1, $t_e2, $t_c1, $t_c2, $t_c3, $t_c4, $t_ic1, $t_ic2, $t_ic3, $t_ic4, $central_meridian,$map_scale_factor,$D2R ,$R2D;
$EQ_RAD= $f= $ECC2= $ECC4= $ECC6= $ECC= $e1= $t_e2= $t_c1= $t_c2= $t_c3= $t_c4= $t_ic1= $t_ic2= $t_ic3= $t_ic4= $central_meridian=0 ;

$D2R= M_PI / 180.0;
$R2D = 180.0 / M_PI;
// FOR  WGS84
$EQ_RAD = 6378137; // 	Equatorial Radius, meters 
// $f=1/297.257223563; // flattening;
$f=0.00335281067183099;
$map_scale_factor=0.9996;
$workingUTMzone=-1;

$NORTH=1;

/*
$lat=40.5010914;  // COMPEGPS x=  653978 NORTH => +   MINE => x=654040.35162953 y=4486678.0482354
$lon=22.8171769;  // y= 4484962 EAST => +   WEST =>-
list($x,$y,$zone,$latzone)=utm($lon,$lat);

echo "lon=".$lon." lat=".$lat." --><br>";
echo "x=$x , y=$y, zone=$zone latzone=$latzone <br>";


$x=653978;
$y=4484962;
$zone=34;

$x=768521;
$y=6635652;
$zone=55;
$latzone="J";
list($lon,$lat)=iutm($x,$y,$zone,$latzone);
echo "<br> x=$x , y=$y, zone=$zone    --> <br>";
echo "lon=".$lon." lat=".$lat."<br>";
*/

function d_sqrt($x) {
	return	( ($x<0) ? 0 : sqrt ($x) );
}

function copysign($x,$y) {
	return ( ($y < 0) ? -abs($x) : abs($x)) ;
}

function vtm ($lon0) {
	global $EQ_RAD, $f, $ECC2, $ECC4, $ECC6, $ECC, $e1, $t_e2, $t_c1, $t_c2, $t_c3, $t_c4, $t_ic1, $t_ic2, $t_ic3, $t_ic4, $central_meridian ,$map_scale_factor,$D2R;
	// Set up an TM projection  
	
	$ECC2 = 2 * $f - $f * $f;
	$ECC4 = $ECC2 * $ECC2;
	$ECC6 = $ECC2 * $ECC4;
	$ECC = d_sqrt ($ECC2);

	$e1 = (1.0 - d_sqrt (1.0 - $ECC2)) / (1.0 + d_sqrt (1.0 - $ECC2));
	$t_e2 = $ECC2 / (1.0 - $ECC2);
	$t_c1 = (1.0 - 0.25 * $ECC2 - 3.0 * $ECC4 / 64.0 - 5.0 * $ECC6 / 256.0);
	$t_c2 = (3.0 * $ECC2 / 8.0 + 3.0 * $ECC4 / 32.0 + 45.0 * $ECC6 / 1024.0);
	$t_c3 = (15.0 * $ECC4 / 256.0 + 45.0 * $ECC6 / 1024.0);
	$t_c4 = (35.0 * $ECC6 / 3072.0);
	$t_ic1 = (1.5 * $e1 - 27.0 * pow ($e1, 3.0) / 32.0);
	$t_ic2 = (21.0 * $e1 * $e1 / 16.0 - 55.0 * pow ($e1, 4.0) / 32.0);
	$t_ic3 = (151.0 * pow ($e1, 3.0) / 96.0);
	$t_ic4 = (1097.0 * pow ($e1, 4.0) / 512.0);

	$central_meridian = $lon0;
}

function tm ($lon, $lat) {
	/* Convert lon/lat to TM x/y */
	global $EQ_RAD, $f, $ECC2, $ECC4, $ECC6, $ECC, $e1, $t_e2, $t_c1, $t_c2, $t_c3, $t_c4, $t_ic1, $t_ic2, $t_ic3, $t_ic4, $central_meridian,$map_scale_factor ,$D2R;
	
	$dlon = $lon - $central_meridian;
	if (abs ($dlon) > 360.0) $dlon += copysign (360.0, - $dlon);
	if (abs ($dlon) > 180.0) $dlon = copysign (360.0 - abs ($dlon), -$dlon);
	
	$lat *= $D2R;
	$M = $EQ_RAD * ($t_c1 * $lat - $t_c2 * sin (2.0 * $lat)
		+ $t_c3 * sin (4.0 * $lat) - $t_c4 * sin (6.0 * $lat));

	if ( abs ($lat) == M_PI_2) {
		$x = 0.0;
		$y = $map_scale_factor * $M;
	}
	else {
		$N = $EQ_RAD / d_sqrt (1.0 - $ECC2 * pow (sin ($lat), 2.0));
		$tan_lat = tan ($lat);
		$cos_lat = cos ($lat);
		$T = $tan_lat * $tan_lat;
		$T2 = $T * $T;
		$C = $t_e2 * $cos_lat * $cos_lat;
		$A = $dlon * $D2R * $cos_lat;
		$A2 = $A * $A;	
		$A3 = $A2 * $A;	
		$A5 = $A3 * $A2;
		$x = $map_scale_factor * $N * ($A + (1.0 - $T + $C) * ($A3 * 0.16666666666666666667)
			+ (5.0 - 18.0 * $T + $T2 + 72.0 * $C - 58.0 * $t_e2) * ($A5 * 0.00833333333333333333));
		$A3 *= $A;	$A5 *= $A;
		$y = $map_scale_factor * ($M + $N * tan ($lat) * (0.5 * $A2 + (5.0 - $T + 9.0 * $C + 4.0 * $C * $C) * ($A3 * 0.04166666666666666667)
			+ (61.0 - 58.0 * $T + $T2 + 600.0 * $C - 330.0 * $t_e2) * ($A5 * 0.00138888888888888889)));
	}

	return array($x,$y);
}


function itm ($x, $y) {
	// Convert TM x/y to lon/lat 
	global $EQ_RAD, $f, $ECC2, $ECC4, $ECC6, $ECC, $e1, $t_e2, $t_c1, $t_c2, $t_c3, $t_c4, $t_ic1, $t_ic2, $t_ic3, $t_ic4, $central_meridian,$map_scale_factor ,$D2R ,$R2D ;	
	
	$M = $y / $map_scale_factor;
	$mu = $M / ($EQ_RAD * $t_c1);
	$phi1 = $mu + $t_ic1 * sin (2.0 * $mu) + $t_ic2 * sin (4.0 * $mu)
		+ $t_ic3 * sin (6.0 * $mu) + $t_ic4 * sin (8.0 * $mu);
	$cos_phi1 = cos ($phi1);
	$tan_phi1 = tan ($phi1);
	$C1 = $t_e2 * $cos_phi1 * $cos_phi1;
	$C12 = $C1 * $C1;
	$T1 = $tan_phi1 * $tan_phi1;
	$T12 = $T1 * $T1;
	$tmp = 1.0 - $ECC2 * (1.0 - $cos_phi1 * $cos_phi1);
	$tmp2 = d_sqrt ($tmp);
	$N1 = $EQ_RAD / $tmp2;
	$R1 = $EQ_RAD * (1.0 - $ECC2) / ($tmp * $tmp2);
	$D = $x / ($N1 * $map_scale_factor);
	$D2 = $D * $D;	
	$D3 = $D2 * $D;	
	$D5 = $D3 * $D2;
	
	$lon = $central_meridian + $R2D * ($D - (1.0 + 2.0 * $T1 + $C1) * ($D3 * 0.16666666666666666667) 
		+ (5.0 - 2.0 * $C1 + 28.0 * $T1 - 3.0 * $C12 + 8.0 * $t_e2 + 24.0 * $T12)
		* ($D5 * 0.00833333333333333333)) / cos ($phi1);
	$D3 *= $D;	
	$D5 *= $D;
	$lat = $phi1 - ($N1 * tan ($phi1) / $R1) * (0.5 * $D2 -
		(5.0 + 3.0 * $T1 + 10.0 * $C1 - 4.0 * $C12 - 9.0 * $t_e2) * ($D3 * 0.04166666666666666667)
		+ (61.0 + 90.0 * $T1 + 298 * $C1 + 45.0 * $T12 - 252.0 * $t_e2 - 3.0 * $C12) * ($D5 * 0.00138888888888888889));
	$lat *= $R2D;

	return array($lon,$lat);
}


/*
 *	TRANSFORMATION ROUTINES FOR THE UNIVERSAL TRANSVERSE MERCATOR PROJECTION (UTM)
 */
 
function initUTM ($UTMzone) {
	global $workingUTMzone;
	if ($UTMzone != $workingUTMzone ) {
		$lon0 = 180.0 + 6.0 * $UTMzone - 3.0;
		if ($lon0 >= 360.0) $lon0 -= 360.0;
		vtm($lon0);	/* Central meridian for this zone */
		$workingUTMzone=$UTMzone ;
	}
}

function utm($lon, $lat) {
	/* Convert lon/lat to UTM x/y */
	global $NORTH;
	global $workingUTMzone;

	// compute UTMzone
	list($UTMzone,$UTMlatZone)=getUTMzone($lon, $lat);
	//echo "$".$UTMzone."$";
	if ($UTMzone != $workingUTMzone ) initUTM($UTMzone) ;

	if ($lon < 0.0) $lon += 360.0;
	list($x,$y)= tm($lon, $lat);
	$x += 500000.0;
	if ($lat<0) $y += 10000000.0;	/* For S hemisphere, add 10^6 m */
	return array($x,$y,$UTMzone,$UTMlatZone);
}

function iutm($x, $y, $UTMzone,$UTMlatZone) {
	/* Convert UTM x/y to lon/lat */
	global $NORTH;
	global $workingUTMzone;

	if ($UTMzone != $workingUTMzone ) initUTM($UTMzone) ;

	$x -= 500000.0;

	//if ( $lat<0 ) $y -= 10000000.0;
	if ( ord( strtoupper($UTMlatZone) )  < ord ("N") ) 	$y -= 10000000.0;  // we are south
	list($lon,$lat) =itm ($x, $y);
	return array ($lon,$lat);
}

function getUTMzone($lon, $lat)  {
	if ($lon < 0.0) $lonTmp = 180 + $lon ;
	else $lonTmp = 180 + $lon ;

	$UTMlatZones=array(-9 =>'D' , -8=> 'E' ,-7=> 'F' ,-6=> 'G' ,-5=> 'H' ,-4=> 'J' ,-3=> 'K' ,-2=> 'L', -1=> 'M',0=>'-' ,'N' ,'P' ,'Q' ,'R' ,'S', 'T' ,'U' ,'V' ,'W' );

	if (abs($lat)<=72) { 
		if ( $lat>=0) $latZoneNum=floor($lat/8 + 1);
		else $latZoneNum=ceil($lat/8 - 1);
		$UTMlatZone=$UTMlatZones[$latZoneNum];
	} else $UTMlatZone="";
	$UTMzone=ceil($lonTmp/6);
	return array($UTMzone,$UTMlatZone);
}
function getInvLatUTMzone($letter)  {
	$UTMlatZones=array('D'=>0, 'E'=>1,'F'=>2 ,'G'=>3 ,'H'=>4 ,'J'=>5 ,'K'=>6 ,'L'=>7, 
			   'M'=>8,'N'=>9 ,'P'=>10 ,'Q'=>11 ,'R'=>12 ,'S'=>13, 'T'=>14 ,'U'=>15 ,'V'=>16 ,'W'=>17);
	//echo $letter."#".$UTMlatZones[$letter]."#";
	return $UTMlatZones[$letter]+0;
}

function getUTMZoneWidth($lat) {
// central meridian of zone ?
	$central=0;
	$zone_distance= calc_distance($lat, $central,$lat, $central+6);
	DEBUG("UTM",64,"Zone_distance at lat: $lat = $zone_distance<br>");
    return $zone_distance;
}

?>
<?
//************************************************************************
// 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: FN_waypoint.php,v 1.31 2010/09/09 12:46:40 manolis Exp $                                                                 
//
//************************************************************************
require_once dirname(__FILE__).'/FN_output.php';
//require_once dirname(__FILE__)."/config.php";

function findNearestWaypoint($lat,$lon) {
	global $waypoints;

	$point=new GpsPoint();
	$point->setLat($lat);
	$point->setLon($lon);

	if (count($waypoints)==0) 
		$waypoints=getWaypoints();

	$nearestID=0;
	$minDistance=1000000;

	foreach($waypoints as $waypoint) {
	   $distance = $point->calcDistance($waypoint);
	   if ( $distance < $minDistance ) {
			$minDistance = $distance;
			$nearestID=$waypoint->waypointID;
	   }
	}
	return array($nearestID,$minDistance);
}

function getTakeoffsCountryContinent() {
	global $db;
	global $flightsTable,$waypointsTable;
	require_once dirname(__FILE__)."/FN_areas.php";

  	$query="SELECT DISTINCT takeoffID, $waypointsTable.countryCode 
			FROM $flightsTable,$waypointsTable WHERE takeoffID<>0 AND $waypointsTable.ID=$flightsTable.takeoffID";
	// echo $query;
	$res= $db->sql_query($query);		
    if($res <= 0){
		return array( array (),array ());
    }

	$takeoffsCountry=array();
	$takeoffsContinent=array();
	while ($row = $db->sql_fetchrow($res)) { 
		$id=$row["takeoffID"];
		$countryCode=$row["countryCode"];
		$takeoffsCountry[$id]=$countryCode;
		$takeoffsContinent[$id]=$countries2continent[$countryCode];
	}

	return array($takeoffsCountry,$takeoffsContinent);

}

function getTakeoffList() {
	global $db;
	global $flightsTable;

  	$query="SELECT DISTINCT takeoffID FROM $flightsTable WHERE takeoffID<>0";
	// echo $query;
	$res= $db->sql_query($query);		
    if($res <= 0){
		return array( array (),array () );
    }

	$takeoffs=array();
	$takeoffsID=array();
	while ($row = $db->sql_fetchrow($res)) { 
 		 $tnames[$row["takeoffID"]]=getWaypointName($row["takeoffID"],-1,1);
	}
	if (!empty($tnames)) {
		asort($tnames);
		foreach($tnames as $takeoffID=>$takeoffName) {
				 array_push($takeoffs,$takeoffName );
				 array_push($takeoffsID,$takeoffID );
		}
	}
	return array($takeoffs,$takeoffsID);

}



function getCountriesList($year=0,$month=0,$clubID=0,$pilotID=0) {
	global $db;
	global $flightsTable,$waypointsTable,$pilotsTable,$areasTakeoffsTable;
	global 	$clubsPilotsTable,$clubsFlightsTable,$moduleRelPath,$countries;	
	global $clubsList;

	$where_clause="";
	if ($clubID) {
		 require dirname(__FILE__)."/INC_club_where_clause.php";
		/*	if ( is_array($clubsList[$clubID]['countryCodes']) ) {			
			foreach ($clubsList[$clubID]['countryCodes'] as $cCode ) {
				$where_clause.=" AND countryCode='$cCode' ";
			}
		}	
		*/
	}	
	
  	$query="SELECT DISTINCT countryCode, count(*) as FlightsNum 
			FROM $flightsTable,$waypointsTable $extra_table_str  
			WHERE 
				$flightsTable.takeoffID=$waypointsTable.ID  ".
			//	"AND $flightsTable.userID<>0 ".
				" $where_clause
				GROUP BY countryCode ORDER BY countryCode ASC";	
	// echo $query;
	$res= $db->sql_query($query);		
    if($res <= 0){
		return array( array (),array () );
    }

	$countriesCodes=array();
	$countriesNames=array();
	$countriesFlightsNum=array();
//	while ($row = $db->sql_fetchrow($res)) { 
	while ($row = @mysqli_fetch_array($res)) { 
		$countriesN[$row["countryCode"]]= $countries[$row["countryCode"]];
		$countriesFNum[$row["countryCode"]]= $row["FlightsNum"];
	}
	if (!empty($countriesN) ){
		asort($countriesN);
		foreach($countriesN as $countryCode=>$countryName) {
				 array_push($countriesNames,$countryName );
				 array_push($countriesCodes,$countryCode );
				 array_push($countriesFlightsNum,$countriesFNum[$countryCode] );
				// echo $countriesFNum[$countryCode] ."->".$countryCode."<br>";
		}
	}
	
	return array($countriesCodes,$countriesNames,$countriesFlightsNum);
}

function getAreasList($year=0,$month=0,$clubID=0,$pilotID=0) {
	global $db;
	global $lightsTable,$waypointsTable,$countries;

	$where_clause="";
		
/*	
  	$query="SELECT DISTINCT countryCode, count(*) as FlightsNum 
			FROM $flightsTable,$waypointsTable $extra_table_str  
			WHERE 
				$flightsTable.takeoffID=$waypointsTable.ID  ".
			//	"AND $flightsTable.userID<>0 ".
				" $where_clause
				GROUP BY countryCode ORDER BY countryCode ASC";	
	// echo $query;
*/
	$query = "SELECT ID,name FROM leonardo_areas $where_clause order by id desc";
	$res= $db->sql_query($query);		
    if($res <= 0){
		return array( array (),array () );
    }

	$areasCodes=array();
	$areasNames=array();
	while ($row = @mysqli_fetch_array($res)) { 
		$areasN[$row["ID"]]= $row["name"];
	}
	if (!empty($areasN) ){
		asort($areasN);
		foreach($areasN as $areaCode=>$areaName) {
				 array_push($areasNames,$areaName );
				 array_push($areasCodes,$areaCode );
		}
	}
	
	return array($areasCodes,$areasNames);
}

function getWaypoints($tm=0,$onlyTakeoffs=0,$utf=0) {
	global $db,$waypointsTable;
	set_time_limit(200);
	if ($onlyTakeoffs)
		$query="SELECT * from $waypointsTable WHERE type=1000";
	else 
		$query="SELECT * from $waypointsTable ".(($tm)?" WHERE modifyDate>=FROM_UNIXTIME($tm) AND type=1000 ":"");
	$res= $db->sql_query($query);
		
    if($res <= 0){
	  if (!$tm) {
	      echo("<H3>"._NO_KNOWN_LOCATIONS."</H3>\n");
    	  exit();
	  } else return array();
    }

	$waypoints=array();
	$i=0;
	// while ($row = $db->sql_fetchrow($res)) { 
	// $rows=$db->sql_fetchrowset($res);
	
    while ($row = mysqli_fetch_assoc($res)) { 
	 //foreach($rows as $row) {
	  $waypoints[$i]=new gpsPoint();
 	  $waypoints[$i]->waypointID=$row["ID"];

	  if ($utf ) {
			$waypoints[$i]->name=urlencode(mb_convert_encoding($row["name"] ,'iso-8859-7', "UTF-8"));
			$waypoints[$i]->intName=urlencode(mb_convert_encoding($row["intName"] ,'iso-8859-1', "UTF-8"));

	  } else {
		  $waypoints[$i]->name=urlencode($row["name"]);
		  $waypoints[$i]->intName=urlencode($row["intName"]);
	  }
   	  $waypoints[$i]->lat=$row["lat"];
   	  $waypoints[$i]->lon=$row["lon"];
	  $waypoints[$i]->type=$row["type"];
  	  $i++;	  
    }     

    $db->sql_freeresult($res);
	return $waypoints;
}

function getWaypointFull($ID) {
	global $db,$waypointsTable;
	global $CONFIG_forceIntl;

	$query="SELECT * from $waypointsTable WHERE ID=".$ID;
	$res= $db->sql_query($query);			
	if($res <= 0) return array(0,"UNKNOWN","UNKNOWN","UNKNOWN");
	
	$row = $db->sql_fetchrow($res) ;
	$db->sql_freeresult($res);

	return array($ID,$row["name"],$row["intName"],$row["countryCode"]);
}

function getWaypointName($ID,$forceIntl=-1,$countryFirst=0,$maxChars=0) {
	global $db,$waypointsTable;
	global $CONFIG_forceIntl;

    if ($forceIntl==-1) $forceIntl=$CONFIG_forceIntl	;
		
	$query="SELECT * from $waypointsTable WHERE ID=".$ID;
	$res= $db->sql_query($query);			
	if($res <= 0) return "UNKNOWN";
	
	$row = $db->sql_fetchrow($res) ;
	$db->sql_freeresult($res);

	$tname=selectWaypointName($row["name"],$row["intName"],$row["countryCode"],$forceIntl);
	$tname=trimText($tname,$maxChars);

	if ($countryFirst)	return $row["countryCode"]." - ".$tname;	
	else return $tname." - ".$row["countryCode"];	
}

function getWaypointIntName($ID) {
	global $db,$waypointsTable;
	global $CONFIG_forceIntl;
		
	$query="SELECT * from $waypointsTable WHERE ID=".$ID;
	$res= $db->sql_query($query);			
	if($res <= 0) return "UNKNOWN";
	
	$row = $db->sql_fetchrow($res) ;
	$db->sql_freeresult($res);
	$tname=selectWaypointName($row["name"],$row["intName"],$row["countryCode"],1);
	return $tname;

}

function getWaypointID($IntName) {
	global $db,$waypointsTable;
		
	$query="SELECT ID FROM $waypointsTable WHERE intName = \"".$IntName."\" LIMIT 1";
	$res= $db->sql_query($query);			
	if($res <= 0) return "UNKNOWN";
	
	$row = $db->sql_fetchrow($res) ;
	$db->sql_freeresult($res);
	$id=$row["ID"];
	return $id;

}
function showWaypointDesciptionIcon($ID) {
	global $db,$waypointsTable;
	global $CONFIG_forceIntl;

		
	$query="SELECT * from $waypointsTable WHERE ID=".$ID;
	$res= $db->sql_query($query);			
	if($res <= 0) return "";
	
	$row = $db->sql_fetchrow($res) ;
	$db->sql_freeresult($res);

	$descLength = strlen($row["description"]);
	if ($descLength > 200){	
		return "&#9432; ";	
	}else{
		return "";
	}
}

function selectWaypointName($name,$intName,$countryCode,$forceIntl=-1) {
	global $currentlang,$nativeLanguage;
	global $CONFIG_forceIntl;

    if ($forceIntl==-1) $forceIntl=$CONFIG_forceIntl	;
		
//	if ( $nativeLanguage==$currentlang || countryHasLang($countryCode,$currentlang)   ) $tname=$name;
	if ( countryHasLang($countryCode,$currentlang)  && !$forceIntl ) $tname=$name;
	else $tname=$intName;

	return $tname;
}

function selectWaypointLocation($name,$intName,$countryCode,$forceIntl=-1) {
	global $currentlang,$nativeLanguage;
	global $CONFIG_forceIntl;

    if ($forceIntl==-1) $forceIntl=$CONFIG_forceIntl	;
		
//	if ( $nativeLanguage==$currentlang || countryHasLang($countryCode,$currentlang)   ) $tname=$name;
	if ( countryHasLang($countryCode,$currentlang)  && !$forceIntl ) $tname=$name;
	else $tname=$intName;

	return $tname;
}

function countryCodeToLanguage($countryCode) {
	global $lang2iso;
	if ( ($lang=array_search(strtolower($countryCode),$lang2iso)) === NULL )  $lang='';
	return $lang;
}
function  countryHasLang($countryCode,$language) {
	 global $CONFIG_langsSpoken;

	 $countryCode=	strtolower($countryCode);
	 $language =	strtolower($language);

	 // if ($countryCode==$language) return 1;
	 if  ($CONFIG_langsSpoken[$language])
		 if ( in_array($countryCode, $CONFIG_langsSpoken[$language]) ) return 1;

	 return 0;
}

function  getKMLFilename($waypointID) {	
		global $waypointsAbsPath;
		return $waypointsAbsPath."/".$waypointID.".kml";  
}

function  getKMLrelPathWaypoint($waypointID) {	
		global $waypointsWebPath;
		return $waypointsWebPath."/".$waypointID.".kml";  
}

function  makeKMLwaypoint($waypointID) {	
	global $langEncodings,$currentlang, $CONF_use_utf ;
  
	$placemarkString=makeWaypointPlacemark($waypointID) ;

//	$xml_text='<?xml version="1.0" encoding="'.$langEncodings[$currentlang].'"? >'.
	$xml_text='<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2"
 xmlns:gx="http://www.google.com/kml/ext/2.2">
'.$placemarkString.'
</kml>
';
	if (! $CONF_use_utf ) {
		require_once dirname(__FILE__)."/lib/ConvertCharset/ConvertCharset.class.php";
		$NewEncoding = new ConvertCharset;
		$FromCharset=$langEncodings[$currentlang];
		$xml_text = $NewEncoding->Convert($xml_text, $FromCharset, "utf-8", $Entities);
	}
	return $xml_text;
}

function  makeWaypointPlacemark($waypointID,$returnCountryCode=0,$includeStyle=1,$includeLookat=1,$styleUrl='') {	
	global $db, $waypointsTable;
	global $flightsTable,$countries,$CONF_mainfile,$moduleRelPath;

    $wpInfo =new waypoint($waypointID);
    $wpInfo->getFromDB();

    $wpName= selectWaypointName($wpInfo->name,$wpInfo->intName,$wpInfo->countryCode);
    $wpLocation = selectWaypointLocation($wpInfo->location,$wpInfo->intLocation,$wpInfo->countryCode);

	 $query="SELECT  MAX(MAX_LINEAR_DISTANCE) as record_km, ID FROM $flightsTable  WHERE takeoffID =".$waypointID." GROUP BY ID ORDER BY record_km DESC ";
	
	 $flightNum=0;
	 $res= $db->sql_query($query);
	 if($res > 0){
		$flightNum=mysqli_num_rows($res);
		$row = $db->sql_fetchrow($res);
	
		$siteRecordLink="<a href='".getLeonardoLink(array('op'=>'show_flight','flightID'=>$row['ID']))."'>".
			formatDistance($row['record_km'],1)."</a>";
	 } else $siteRecordLink="";
	
	 $pointFlightsLink="<a href='".getLeonardoLink(array('op'=>'list_flights','takeoffID'=>$waypointID,'year'=>'0','season'=>'0'))
		."'>"._See_flights_near_this_point." [ ".$flightNum." ]</a>";
	 $countryFlightsLink="<a href='".getLeonardoLink(array('op'=>'list_flights','takeoffID'=>'0','year'=>'0','season'=>'0','country'=>$wpInfo->countryCode))
		."'>".$countries[$wpInfo->countryCode]."</a>";
	 if ($wpInfo->link) $siteLink='<a href="'.formatURL($wpInfo->link).'" target="_blank">'.formatURL($wpInfo->link).'</a>';
	 else $siteLink="-";

// "<?xml version='1.0' encoding='".$langEncodings[$currentlang]."'? >
// <?xml version="1.0" encoding="UTF-8"? >
$xml_text='<Placemark>
  <name><![CDATA['.$wpName.' ]]></name>
  <description><![CDATA[<table cellpadding=0 cellspacing=0 width=600>'.
	'<tr bgcolor="#D7E1EE"><td>'._SITE_REGION .': '.$wpLocation.' - '.$countryFlightsLink.'</td></tr>'.
	'<tr bgcolor="#CCCCCC"><td>'.$pointFlightsLink.'</td></tr>'.
	'<tr ><td>'._SITE_RECORD.' : '.$siteRecordLink.'</td></tr>'.
	'<tr bgcolor="#CCCCCC"><td>'._SITE_LINK .' : '.$siteLink.'</td></tr>'.
	'<tr ><td>'.$wpInfo->description.'</td></tr>'.
	'<tr ><td></td></tr>'.
	'</table>	
    ]]>
 </description>';
 
 if ($includeLookat) {
 	$xml_text.='
  <LookAt>
    <longitude>'.(-$wpInfo->lon).'</longitude>
    <latitude>'.$wpInfo->lat.'</latitude>
    <range>10000</range>
    <tilt>0</tilt>
    <heading>0</heading>
  </LookAt>
  ';
  }
  
 if ($includeStyle) {
 	$xml_text.='  
  <styleUrl>root://styleMaps#default+nicon=0x307+hicon=0x317</styleUrl>
  <Style>
    <IconStyle>
      <scale>0.8</scale>
      <Icon>
        <href>root://icons/palette-4.png</href>
        <x>160</x>
        <y>128</y>
        <w>32</w>
        <h>32</h>
      </Icon>
    </IconStyle>
    <LabelStyle>
      <scale>0.8</scale>
    </LabelStyle>
  </Style>
  ';
  }
  
  if ($styleUrl) {
    $xml_text.="<styleUrl>$styleUrl</styleUrl>\n";
  }
  
  $xml_text.='
  <Point>
    <coordinates>'.(-$wpInfo->lon).','.$wpInfo->lat.',0</coordinates>
  </Point>
</Placemark>';
	if ($returnCountryCode) {
		return array($xml_text,$wpInfo->countryCode);
	} else return $xml_text;

}
?>
<?
//************************************************************************
// 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: FN_output.php,v 1.61 2012/10/04 05:59:26 manolis Exp $                                                                 
//
//************************************************************************

require_once dirname(__FILE__)."/CL_html.php";

function sec2Time($secs,$no_seconds=false) {
  if ($no_seconds)
    return sprintf("%d:%02d",$secs/3600,($secs%3600)/60);
  else 
    return '<span class="time_style">'.sprintf("%d:%02d:%02d",$secs/3600,($secs%3600)/60,$secs%60).'</span>';
}

function sec2Time24h($secs,$no_seconds=false) {
	if ($no_seconds)
    	return sprintf("%02d:%02d",$secs/3600,($secs%3600)/60);
	else 
		return sprintf("%02d:%02d:%02d",$secs/3600,($secs%3600)/60,$secs%60);
}


function fulldate2tm($dateStr) {
	// expecting YYYY-MM-DD HH:MM:SS
	$tm=0;
	if (preg_match("/(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)/",$dateStr,$matches)) {
		$tm=mktime($matches[4],$matches[5],$matches[6],$matches[2],$matches[3],$matches[1]);
	}
	return $tm;
}

function fulldate2tmUTC($dateStr) {
	// expecting YYYY-MM-DD HH:MM:SS
	$tm=0;
	if (preg_match("/(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)/",$dateStr,$matches)) {
		$tm=gmmktime($matches[4],$matches[5],$matches[6],$matches[2],$matches[3],$matches[1]);
	}
	return $tm;
}

function tm2fulldate($tm){
	// return YYYY-MM-DD HH:MM:SS
	return date("Y-m-d H:i:s",$tm);
}

function days2YearsMonths($days) {
   $years=floor($days/365);
   $months=ceil( ($days%365) / 30 );
   return array($years,$months);
}

function formatURL($linkURL,$numChars=0) {
	if ($numChars>0) {
		 if (strlen($linkURL) > $numChars - 3 )
		 $linkURL=substr($linkURL,0,$numChars)."...";
	}
	if ( substr($linkURL,0,7) == "http://" || substr($linkURL,0,8) == "https://" ) return $linkURL;
	else return "http://".$linkURL;
}

function trimText($text,$numChars=0) {
	global $CONF_use_utf;
	if ($numChars>0) {
		if ($CONF_use_utf) {
		 if (function_exists('mb_strlen')  ) {
			 if (mb_strlen($text,'UTF-8') > $numChars - 3 )
				 $text=mb_substr($text,0,$numChars,'UTF-8')."...";
		  } else {
			 $numChars*=2;
			 if (strlen($text) > $numChars - 3 )
				 $text=substr($text,0,$numChars)."...";		  
		  }		
		} else {
		 if (strlen($text) > $numChars - 3 )
			 $text=substr($text,0,$numChars)."...";
		}	 
	}
	return $text;	
}

function formatDate($date,$html_output=true) {
  // from 2002-07-14 -> 14/07/2004
  $dt_str=sprintf("%02d/%02d/%4d",substr($date,8,2),substr($date,5,2),substr($date,0,4));
  return $dt_str;
  //if ($html_output)
  //  return '<font color=#224488>'.$dt_str.'</font>';
  //else return $dt_str;
}

function formatOLCScoreType($type,$html_output=true) {
	$ret="#".$type."#";
	if ($type=="FREE_FLIGHT") $ret=_FREE_FLIGHT;
	else if ($type=="FREE_TRIANGLE") $ret=_FREE_TRIANGLE;
	else if ($type=="FAI_TRIANGLE") $ret=_FAI_TRIANGLE;
	if ($html_output)
		return '<span class="score_type_style">'.$ret.'</span>';
	else return $ret;
}

function formatDistance($distance,$showUnits=false) { // in meters
	global $PREFS;
	// 1 kilometer = 0.62 mil
	// 1 meter  =  3.28 feet
	if ($PREFS->metricSystem==2) { 
		$dis=($distance*0.62)/1000; 
		$units=_MI;
	} else { // km
		$dis=$distance/1000;
		$units=_KM;
	}
	// return sprintf("%.2f km",$distance/1000);
	return sprintf("%.1f&nbsp;%s",$dis,($showUnits)?$units:"");
}

function formatDistanceOpen($distance,$showKm=true) { // in meters
	return  formatDistance($distance,$showKm);
	// return '<font color=#4400aa>'.formatDistance($distance,$showKm).'</font>';
}

function formatDistanceOLC($distance,$showKm=true) { // in meters
	return '<span class="distance_style">'.formatDistance($distance,$showKm).'</span>';
}



function formatOLCScore($score,$html_output=true) { 
	return sprintf("%.2f",$score);
	//if ($html_output) return '<font color=#0000ff>'.sprintf("%.1f",$score).'</font>';
	//else return sprintf("%.1f",$score);
}

function formatAltitude($alt) { 
	global $PREFS;
	// 1 kilometer = 0.62 mil
	// 1 meter  =  3.28 feet
	if ($PREFS->metricSystem==2) { 
		$alt=$alt*3.28; //feet
		$units=_FT;
	} else { 
		$units=_M;
	}
	return '<span class="altitude_style">'.sprintf("%d&nbsp;%s",$alt,$units).'</span>';
}

function formatSpeed($speed) { // in km/h
	global $PREFS;
	// 1 kilometer = 0.62 mil
	// 1 meter  =  3.28 feet
	if ($PREFS->metricSystem==2) { 
		$speed=$speed*0.62; // ml/h
		$units=_MPH;
	} else { 
		$units=_KM_PER_HR;
	}
	return '<span class="speed_style">'.sprintf("%.1f&nbsp;%s",$speed,$units).'</span>';
}

function formatVario($vario) { // in m/sec
	global $PREFS;
	// 1 kilometer = 0.62 mil
	// 1 meter  =  3.28 feet
	if ($PREFS->metricSystem==2) { 
		$vario=$vario*3.28*60; // feet /min
		$units=_FPM;
		return '<span class="vario_style">'.sprintf("%.0f&nbsp;%s",$vario,$units).'</span>';
	} else { 
		$units=_M_PER_SEC;
		return '<span class="vario_style">'.sprintf("%.1f&nbsp;%s",$vario,$units).'</span>';
	}

}

function formatLocation($name,$vinicity,$radious) {
	global $PREFS;
	if ($PREFS->metricSystem==2) $dis=($vinicity*0.62)/1000; 
	else $dis=$vinicity/1000;

 if ($vinicity > 300000 ) $res_name="UNKNOWN";
 else if ($vinicity > $radious ) 
		$res_name=$name."&nbsp;[~".sprintf("%.1f",$dis)."]"; 
//		$res_name=$name."&nbsp;[~".sprintf("%.1f",$vinicity/1000)."&nbsp;km]"; 
 else $res_name=$name;

 $res_name=str_replace("&#039;","'",$res_name);
 return $res_name;
}


function datetime2UnixTimestamp($datestamp) {
    if ($datestamp!=0) {
        list($date, $time)=explode(" ", $datestamp);
        list($year, $month, $day)=explode("-", $date);
        list($hour, $minute, $second)=explode(":", $time);		
        $stampeddate=mktime($hour,$minute,$second,$month,$day,$year);    
        return   $stampeddate ;
    }
}



function processIGC($filePath) {
	global $takeoffRadious;
	//echo $filePath."<br>";
	//echo filesize( $filePath)."<br>";
	$lines = file ($filePath); 
	$points=0;
	foreach($lines as $line) {
		$line=trim($line);
		if  (strlen($line)==0) continue;		
		if ($line{0}=='B') {
			$firstPoint=new gpsPoint($line);
			//$res.=_TAKEOFF_COORDS." ";
			//$res.=$firstPoint->lat." ";
			//$res.=$firstPoint->lon."<br>";

			$time=substr($line,1,6);
			$zone= getUTMzoneLocal( $firstPoint->lon,$firstPoint->lat);
			$timezone= ceil(-$firstPoint->lon / (180/12) );
			$res.="<b>UTM zone:</b> ".$zone." ";
			$res.="<b>Timezone:</b> ".$timezone." <b>First point time:</b> $time ";
			break;
		}
	}
	return $res;
}

function getUTMzoneLocal($lon, $lat)  {
	if ($lon < 0.0)  $lonTmp = 180 - $lon ;
	else $lonTmp = 180 - $lon ;

	$UTMzone=ceil($lonTmp/6);
	return $UTMzone;
}

function getUTMtimeOffset($lat,$lon, $theDate ) {
// $lon is the X (negative is EAST positive is WEST
// for now we return a very rough calculation
	DEBUG('flight',1,"getUTMtimeOffset: date-> $theDate <BR>");

	$timezone= ceil( -$lon / (180/12) );
	return $timezone;
}

function getTZ($lat,$lon, $theDate) {
	global $db,$waypointsTable;
	global $CONF_use_date_for_TZ_detection;

	// fall back to simple lon method in case of old php (4.4.1) 
	if (!$CONF_use_date_for_TZ_detection) 	return getUTMtimeOffset($lat,$lon, $theDate );

	$query="SELECT lat,lon,ABS($lat-lat) as dlat , ABS($lon- lon ) as dlon ,countryCode from $waypointsTable 
				WHERE ABS($lat-lat) < 1 AND ABS($lon- lon )  < 1
				ORDER BY dlat,dlon ASC";
	DEBUG('getTZ',128,"getTZ: $query<BR>");
	$res= $db->sql_query($query);
		
    if($res <= 0){
		DEBUG('getTZ',128,"getTZ: no waypont near by will try rough method<BR>");
		return  getUTMtimeOffset($lat,$lon, $theDate );
    }

	$i=0;
	$minTakeoffDistance=1000000;
	while ($row = mysqli_fetch_assoc($res)) { 
		$i++;	  
		$this_distance = gpsPoint::calc_distance($row["lat"],$row["lon"],$lat,$lon);
		DEBUG('getTZ',128,"getTZ: ".$row["lat"]." , ".$row["lon"]." country-> ".$row["countryCode"]." distance-> $this_distance <BR>");
		if ( $this_distance < $minTakeoffDistance ) {
				$minTakeoffDistance = $this_distance;
				$countryCode=$row["countryCode"];
		}
    }     

	if (!$i) {
		DEBUG('getTZ',128,"getTZ: No waypont near by #2. Will try rough method<BR>");
		return  getUTMtimeOffset($lat,$lon, $theDate );
	}

	DEBUG('getTZ',128,"getTZ: Min dist: $minTakeoffDistance , country: $countryCode <BR>");
	if ($minTakeoffDistance > 50000 ) {
		DEBUG('getTZ',128,"getTZ: Nearest waypoint is too far. Will try rough method<BR>");
		return  getUTMtimeOffset($lat,$lon, $theDate );
	}

	// now we will try the getTZoffset()

	// make $tm from YYYY-MM-DD
	$tm=gmmktime(1,0,0,substr($theDate,5,2),substr($theDate,8,2),substr($theDate,0,4));

	// this will return  good results only for countries that have ONE timezone
	// else '' will be returned
	require_once dirname(__FILE__).'/FN_timezones.php';
	// $TZone=getTZforCountry($countryCode);
	global $Countries2timeZones;
	$TZone= $Countries2timeZones[strtoupper($countryCode)];

	if (strtoupper($countryCode)=='AU' ){
		DEBUG('getTZ',128,"getTZ: Australia timezones<BR>");
/* 
australia
http://www.statoids.com/tau.html
central - west is on 129E

central - east is on 
lon > 141E for lat < -26 (S)
lon > 138E for lat > -26 (S)

east - > Australia/Sydney
west
central
*/
		if ( $lon > -129 )  $TZone='Australia/Perth'; // West
		else if ( $lon > -138 && $lat  > -26 ) $TZone='Australia/Darwin';   // central north
		else if ( $lon > -141 && $lat  < -26 ) $TZone='Australia/Adelaide'; // central south
		else if ( $lat > -29) $TZone='Australia/Brisbane';  // queensland - North East
		else $TZone='Australia/Sydney';  //  South-East
	}

	if ($TZone=='') { 
		DEBUG('getTZ',128,"getTZ: Country $countryCode has more than one timezones.. Back to rough method<BR>");
		return  getUTMtimeOffset($lat,$lon, $theDate );
	}

	return getTZoffset($TZone,$tm)/3600;
}

function getTZoffset($TZone,$tm) {
	$oldTZ=getenv("TZ");
	// echo "old:$oldTZ";
	// DEBUG('getTZoffset',128,"getTZoffset: $TZone  ($tm) [server TZ=$oldTZ] ".date("T/Z/I/O")."<BR>");

	putenv("TZ=$TZone");
$offset=date('O',$tm);
	putenv("TZ=$TZone");
$offset=date('O',$tm);
	putenv("TZ=$TZone");
	$offset=date('O',$tm);

	$tTZ=getenv("TZ");
	DEBUG('getTZoffset',128,"getTZoffset: offset from GMT :$offset [getenv TZ= $tTZ] ".date("T/Z/I/O")."<BR>");

	putenv("TZ=$oldTZ");


	if ( preg_match("/([-+])(\d\d)(\d\d)/",$offset,$matches) ) {
		$secs=$matches[2]*3600+$matches[3]*60;
		if ($matches[1]=='-') $secs=-$secs;
	} else  {
		echo "FATAL error in flight offset";
		exit;
	}
	DEBUG('getTZoffset',128,"getTZoffset: $TZone  ($tm) = ".($secs /3600)."<BR>");
		
	return $secs;
}

function generate_flights_pagination($base_url, $num_items, $per_page, $start_item, $add_prevnext_text = TRUE,	$begin_end = 2,	$from_middle = 2)
{


/*
	By default, $begin_end is 3, and $from_middle is 1, so on page 6 in a 12 page view, it will look like this:

	a, d = $begin_end = 3
	b, c = $from_middle = 1

 "begin"        "middle"           "end"
    |              |                 |
    |     a     b  |  c     d        |
    |     |     |  |  |     |        |
    v     v     v  v  v     v        v
    1, 2, 3 ... 5, 6, 7 ... 10, 11, 12

	Change $begin_end and $from_middle to suit your needs appropriately
*/
//	$begin_end = 2;
//	$from_middle = 2;
	
	$total_pages = ceil($num_items/$per_page);

	if ( $total_pages == 1 )
	{
		return '';
	}

	$on_page = floor($start_item / $per_page) + 1;

	$page_string = '';
	if ( $total_pages > ((2*($begin_end + $from_middle)) + 2) )
	{
	//	$init_page_max = ( $total_pages > 3 ) ? 3 : $total_pages;
		$init_page_max = ( $total_pages > $begin_end ) ? $begin_end : $total_pages;


		for($i = 1; $i < $init_page_max + 1; $i++)
		{
			// $page_string .= ( $i == $on_page ) ? '<b>' . $i . '</b>' : '<a href="' . append_sid($base_url . "&amp;start=" . ( ( $i - 1 ) * $per_page ) ) . '">' . $i . '</a>';
			$page_string .= ( $i == $on_page ) ? '<div class="activePageNum">' . $i . '</div>' : '<a href="' . append_sid($base_url . "&amp;page_num=".$i ) . '">' . $i . '</a>';
			if ( $i <  $init_page_max )
			{
				$page_string .= ", ";
			}
		}

		//if ( $total_pages > 3 )
		if ( $total_pages > $begin_end )
		{
			if ( $on_page > 1  && $on_page < $total_pages )	{
				//$page_string .= ( $on_page > 5 ) ? ' ... ' : ', ';
				$page_string .= ( $on_page > ($begin_end + $from_middle + 1) ) ? ' ... ' : ', ';


				//$init_page_min = ( $on_page > 4 ) ? $on_page : 5;
				$init_page_min = ( $on_page > ($begin_end + $from_middle) ) ? $on_page : ($begin_end+ $from_middle + 1);
				//$init_page_max = ( $on_page < $total_pages - 4 ) ? $on_page : $total_pages - 4;
				$init_page_max = ( $on_page < $total_pages - ($begin_end + $from_middle) ) ? $on_page : $total_pages - ($begin_end + $from_middle);

//echo $init_page_min."#".$init_page_max."#".$on_page;
				//for($i = $init_page_min - 1; $i < $init_page_max + 2; $i++)
				for($i = $init_page_min - $from_middle; $i < $init_page_max + ($from_middle + 1); $i++)
				{
					$page_string .= ($i == $on_page) ? '<div class="activePageNum">' . $i . '</div>' : '<a href="' . append_sid($base_url . "&amp;page_num=$i").'">'.$i. '</a>';
					//if ( $i <  $init_page_max + 1 )
					if ( $i <  $init_page_max + $from_middle )
					{
						$page_string .= ', ';
					}
				}

				//$page_string .= ( $on_page < $total_pages - 4 ) ? ' ... ' : ', ';
				$page_string .= ( $on_page < $total_pages - ($begin_end + $from_middle) ) ? ' ... ' : ', ';


			} else {
				$page_string .= ' ... ';
			}

			//for($i = $total_pages - 2; $i < $total_pages + 1; $i++)
			for($i = $total_pages - ($begin_end - 1); $i < $total_pages + 1; $i++)
			{
				$page_string .= ( $i == $on_page ) ? '<div class="activePageNum">' . $i . '</div>'  : '<a href="' . append_sid($base_url . "&amp;page_num=$i").'">' . $i . '</a>';
				if( $i <  $total_pages )
					$page_string .= ", ";
			}
		}
	} else {
		for($i = 1; $i < $total_pages + 1; $i++)
		{
			$page_string .= ( $i == $on_page ) ? '<div class="activePageNum">' . $i . '</div>': '<a href="' . append_sid($base_url . "&amp;page_num=$i").'">'.$i.'</a>';
			if ( $i <  $total_pages )
				$page_string .= ', ';
		}
	}

	if ( $add_prevnext_text )
	{
		global $themeRelPath;
		$imgNext=leoHtml::img("icon_page_next.gif",0,0,'border','','icons1');	
		$imgPrev=leoHtml::img("icon_page_next.gif",0,0,'border','','icons1');
		if ( $on_page > 1 )
			$page_string = ' <a href="' . append_sid($base_url . "&amp;page_num=" . ( $on_page - 1 )  ) . '">'.$imgPrev.'</a>&nbsp;&nbsp;'.$page_string;

		if ( $on_page < $total_pages ) 
			$page_string .= '&nbsp;&nbsp;<a href="' . append_sid($base_url . "&amp;page_num=" . ($on_page+1)   ) . '">'.$imgNext.'</a>';

	}
	$page_string = '<div class="numeration">'.$page_string.'</div>';
	return $page_string;
}

function makeFlightActionsPopup() {
	global $moduleRelPath,$opMode;
	ob_start();

?>
<script language="javascript">
var flightActionTip = new TipObj('flightActionTip');
with (flightActionTip)
{
  template = '<table bgcolor="#000000" cellpadding="0" cellspacing="0" width="%3%" border="0">' +
  '<tr><td class="infoBoxHeader"><? echo "Actions"; ?></td></tr>'+
  '<tr><td class="infoBox">'+
  "<img src='/img/change_icon.png' width='16' height='16' border='0' align='absmiddle'> <a href='</urlset>
