Completed
Push — master ( 3f174d...72d96f )
by Yannick
12:45
created

require/class.Scheduler.php (8 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
require_once(dirname(__FILE__).'/libs/simple_html_dom.php');
3
require_once(dirname(__FILE__).'/settings.php');
4
require_once(dirname(__FILE__).'/class.Connection.php');
5
require_once(dirname(__FILE__).'/class.Translation.php');
6
require_once(dirname(__FILE__).'/class.Common.php');
7
require_once(dirname(__FILE__).'/libs/uagent/uagent.php');
8
9
class Schedule {
10
	protected $cookies = array();
11
        public $db;
12
	function __construct($dbc = null) {
13
		$Connection = new Connection($dbc);
14
		$this->db = $Connection->db;
15
        }
16
	
17
	/**
18
	* Add schedule data to database
19
	* @param String $ident aircraft ident
20
	* @param String $departure_airport_icao departure airport icao
21
	* @param String $departure_airport_time departure airport time
22
	* @param String $arrival_airport_icao arrival airport icao
23
	* @param String $arrival_airport_time arrival airport time
24
	/ @param String $source source of data
25
	*/
26
	
27
	public function addSchedule($ident,$departure_airport_icao,$departure_airport_time,$arrival_airport_icao,$arrival_airport_time,$source = 'website') {
28
		date_default_timezone_set('UTC');
29
		$date = date("Y-m-d H:i:s",time());
30
	        //if ($departure_airport_time == '' && $arrival_airport_time == '') exit;
31
	        //$query = "SELECT COUNT(*) FROM schedule WHERE ident = :ident";
32
	        $query = "SELECT COUNT(*) FROM routes WHERE CallSign = :ident";
33
	        $query_values = array(':ident' => $ident);
34
		 try {
35
			$sth = $this->db->prepare($query);
36
			$sth->execute($query_values);
37
		} catch(PDOException $e) {
38
			return "error : ".$e->getMessage();
39
		}
40
		if ($sth->fetchColumn() > 0) {
41
			if ($departure_airport_time == '' && $arrival_airport_time == '') {
42
			    $query = "SELECT COUNT(*) FROM routes WHERE CallSign = :ident AND FromAirport_ICAO = :departure_airport_icao AND ToAirport_ICAO = :arrival_airport_icao";
43
			    $query_values = array(':ident' => $ident,':departure_airport_icao' => $departure_airport_icao,':arrival_airport_icao' => $arrival_airport_icao);
44
			} elseif ($arrival_airport_time == '') {
45
			    $query = "SELECT COUNT(*) FROM routes WHERE CallSign = :ident AND FromAirport_ICAO = :departure_airport_icao AND FromAirport_Time = :departure_airport_time AND ToAirport_ICAO = :arrival_airport_icao";
46
			    $query_values = array(':ident' => $ident,':departure_airport_icao' => $departure_airport_icao,':departure_airport_time' => $departure_airport_time,':arrival_airport_icao' => $arrival_airport_icao);
47
			} elseif ($departure_airport_time == '') {
48
			    $query = "SELECT COUNT(*) FROM routes WHERE CallSign = :ident AND FromAirport_ICAO = :departure_airport_icao AND ToAirport_ICAO = :arrival_airport_icao AND ToAirport_Time = :arrival_airport_time";
49
			    $query_values = array(':ident' => $ident,':departure_airport_icao' => $departure_airport_icao,':arrival_airport_icao' => $arrival_airport_icao,':arrival_airport_time' => $arrival_airport_time);
50
			} else {
51
			    //$query = "SELECT COUNT(*) FROM schedule WHERE ident = :ident AND departure_airport_icao = :departure_airport_icao AND departure_airport_time = :departure_airport_time AND arrival_airport_icao = :arrival_airport_icao AND arrival_airport_time = :arrival_airport_time";
52
			    $query = "SELECT COUNT(*) FROM routes WHERE CallSign = :ident AND FromAirport_ICAO = :departure_airport_icao AND FromAirport_Time = :departure_airport_time AND ToAirport_ICAO = :arrival_airport_icao AND ToAirport_Time = :arrival_airport_time";
53
			    $query_values = array(':ident' => $ident,':departure_airport_icao' => $departure_airport_icao,':departure_airport_time' => $departure_airport_time,':arrival_airport_icao' => $arrival_airport_icao,':arrival_airport_time' => $arrival_airport_time);
54
			}
55
			try {
56
				$sth = $this->db->prepare($query);
57
				$sth->execute($query_values);
58
			} catch(PDOException $e) {
59
				return "error : ".$e->getMessage();
60
			}
61
			if ($sth->fetchColumn() == 0) {
62
				//$query = 'UPDATE schedule SET departure_airport_icao = :departure_airport_icao, departure_airport_time = :departure_airport_time, arrival_airport_icao = :arrival_airport_icao, arrival_airport_time = :arrival_airport_time, date_modified = :date, source = :source WHERE ident = :ident';
63
				if ($departure_airport_time == '' && $arrival_airport_time == '') {
64
                            	    $query = 'UPDATE routes SET FromAirport_ICAO = :departure_airport_icao, ToAirport_ICAO = :arrival_airport_icao, date_modified = :date, Source = :source WHERE CallSign = :ident';
65
				    $query_values = array(':ident' => $ident,':departure_airport_icao' => $departure_airport_icao,':arrival_airport_icao' => $arrival_airport_icao, ':date' => $date, ':source' => $source);
66 View Code Duplication
				} elseif ($arrival_airport_time == '') {
67
                            	    $query = 'UPDATE routes SET FromAirport_ICAO = :departure_airport_icao, FromAiport_Time = :departure_airport_time, ToAirport_ICAO = :arrival_airport_icao, date_modified = :date, Source = :source WHERE CallSign = :ident';
68
				    $query_values = array(':ident' => $ident,':departure_airport_icao' => $departure_airport_icao,':departure_airport_time' => $departure_airport_time,':arrival_airport_icao' => $arrival_airport_icao, ':date' => $date, ':source' => $source);
69
				} elseif ($departure_airport_time == '') {
70
                            	    $query = 'UPDATE routes SET FromAirport_ICAO = :departure_airport_icao, ToAirport_ICAO = :arrival_airport_icao, ToAirport_Time = :arrival_airport_time, date_modified = :date, Source = :source WHERE CallSign = :ident';
71
				    $query_values = array(':ident' => $ident,':departure_airport_icao' => $departure_airport_icao,':arrival_airport_icao' => $arrival_airport_icao,':arrival_airport_time' => $arrival_airport_time, ':date' => $date, ':source' => $source);
72 View Code Duplication
				} else {
73
                            	    $query = 'UPDATE routes SET FromAirport_ICAO = :departure_airport_icao, FromAiport_Time = :departure_airport_time, ToAirport_ICAO = :arrival_airport_icao, ToAirport_Time = :arrival_airport_time, date_modified = :date, Source = :source WHERE CallSign = :ident';
74
				    $query_values = array(':ident' => $ident,':departure_airport_icao' => $departure_airport_icao,':departure_airport_time' => $departure_airport_time,':arrival_airport_icao' => $arrival_airport_icao,':arrival_airport_time' => $arrival_airport_time, ':date' => $date, ':source' => $source);
75
				}
76
				 try {
77
					$sth = $this->db->prepare($query);
78
					$sth->execute($query_values);
79
				} catch(PDOException $e) {
80
					return "error : ".$e->getMessage();
81
				}
82
			} else {
83
				//$query = 'UPDATE schedule SET date_lastseen = :date WHERE ident = :ident';
84
				$query = 'UPDATE routes SET date_lastseen = :date WHERE CallSign = :ident';
85
				$query_values = array(':ident' => $ident,':date' => $date);
86
				 try {
87
					$sth = $this->db->prepare($query);
88
					$sth->execute($query_values);
89
				} catch(PDOException $e) {
90
					return "error : ".$e->getMessage();
91
				}
92
			}
93
		} else {
94
			$query = 'INSERT INTO  routes (CallSign,FromAirport_ICAO, FromAirport_Time, ToAirport_ICAO, ToAirport_Time,date_added,source)  VALUES (:ident,:departure_airport_icao,:departure_airport_time,:arrival_airport_icao,:arrival_airport_time,:date,:source)';
95
			$query_values = array(':ident' => $ident,':departure_airport_icao' => $departure_airport_icao,':departure_airport_time' => $departure_airport_time,':arrival_airport_icao' => $arrival_airport_icao,':arrival_airport_time' => $arrival_airport_time, ':date' => $date, ':source' => $source);
96
			 try {
97
				$sth = $this->db->prepare($query);
98
				$sth->execute($query_values);
99
			} catch(PDOException $e) {
100
				return "error : ".$e->getMessage();
101
			}
102
		}
103
        
104
	}
105
106
	public function getSchedule($ident) {
107
	        $Translation = new Translation($this->db);
108
	        $operator = $Translation->checkTranslation($ident,false);
109
	        if ($ident != $operator) {
110
	    		$query = "SELECT FromAirport_ICAO as departure_airport_icao, ToAirport_ICAO as arrival_airport_icao, FromAirport_Time as departure_airport_time, ToAirport_Time as arrival_airport_time FROM routes WHERE CallSign = :operator OR CallSign = :ident LIMIT 1";
111
	    		$query_values = array(':ident' => $ident,'operator' => $operator);
112
	    	} else {
113
		        $query = "SELECT FromAirport_ICAO as departure_airport_icao, ToAirport_ICAO as arrival_airport_icao, FromAirport_Time as departure_airport_time, ToAirport_Time as arrival_airport_time FROM routes WHERE CallSign = :ident LIMIT 1";
114
	    		$query_values = array(':ident' => $ident);
115
	    	}
116
		 try {
117
			$sth = $this->db->prepare($query);
118
			$sth->execute($query_values);
119
		} catch(PDOException $e) {
120
			return "error : ".$e->getMessage();
121
		}
122
		$row = $sth->fetch(PDO::FETCH_ASSOC);
123
		if (count($row) > 0) {
124
			return $row;
125
		} else return array();
126
	}
127
128 View Code Duplication
	public function checkSchedule($ident) {
129
		global $globalDBdriver;
130
	        //$query = "SELECT COUNT(*) as nb FROM schedule WHERE ident = :ident AND date_added > DATE_SUB(CURDATE(), INTERVAL 8 DAY) - 8 LIMIT 1";
131
	        if ($globalDBdriver == 'mysql') {
132
			$query = "SELECT COUNT(*) as nb FROM routes WHERE CallSign = :ident AND ((date_added BETWEEN DATE(DATE_SUB(CURDATE(), INTERVAL 15 DAY)) AND DATE(NOW()) and date_modified IS NULL) OR (date_modified BETWEEN DATE(DATE_SUB(CURDATE(), INTERVAL 15 DAY)) AND DATE(NOW()))) LIMIT 1";
133
		} else {
134
			$query = "SELECT COUNT(*) as nb FROM routes WHERE CallSign = :ident 
135
			AND ((date_added::timestamp BETWEEN CURRENT_TIMESTAMP - INTERVAL '15 DAYS' AND CURRENT_TIMESTAMP) and date_modified::timestamp IS NULL)
136
			     OR (date_modified::timestamp BETWEEN CURRENT_TIMESTAMP - INTERVAL '15 DAYS' AND CURRENT_TIMESTAMP) LIMIT 1";
137
		}
138
	        $query_values = array(':ident' => $ident);
139
		 try {
140
			$sth = $this->db->prepare($query);
141
			$sth->execute($query_values);
142
		} catch(PDOException $e) {
143
			return "error : ".$e->getMessage();
144
		}
145
		$row = $sth->fetch(PDO::FETCH_ASSOC);
146
		return $row['nb'];
147
	}
148
149
	/**
150
	* Get flight info from Air France
151
	* @param String $callsign The callsign
152
	* @param String $date date we want flight number info
153
	* @param String $carrier IATA code
154
	* @return Flight departure and arrival airports and time
155
	*/
156
	private function getAirFrance($callsign, $date = 'NOW',$carrier = 'AF') {
157
		$Common = new Common();
158
		$check_date = new Datetime($date);
159
		$numvol = preg_replace('/^[A-Z]*/','',$callsign);
160
		if (!filter_var($numvol,FILTER_VALIDATE_INT)) return array();
161
		$url = "http://www.airfrance.fr/cgi-bin/AF/FR/fr/local/resainfovol/infovols/detailsVolJson.do?codeCompagnie[0]=".$carrier."&numeroVol[0]=".$numvol."&dayFlightDate=".$check_date->format('d')."&yearMonthFlightDate=".$check_date->format('Ym');
162
		$json = $Common->getData($url);
163
	
164
		$parsed_json = json_decode($json);
165
		if (property_exists($parsed_json,'errors') === false) {
166
			$originLong = $parsed_json->{'flightsList'}[0]->{'segmentsList'}[0]->{'originLong'};
167
			$originShort = $parsed_json->{'flightsList'}[0]->{'segmentsList'}[0]->{'originShort'};
168
			$departureDateMedium = $parsed_json->{'flightsList'}[0]->{'segmentsList'}[0]->{'departureDateMedium'};
169
			$departureTime = $parsed_json->{'flightsList'}[0]->{'segmentsList'}[0]->{'departureTime'};
170
			$destinationLong = $parsed_json->{'flightsList'}[0]->{'segmentsList'}[0]->{'destinationLong'};
171
			$destinationShort = $parsed_json->{'flightsList'}[0]->{'segmentsList'}[0]->{'destinationShort'};
172
			$arrivalDateMedium = $parsed_json->{'flightsList'}[0]->{'segmentsList'}[0]->{'arrivalDateMedium'};
173
			$arrivalTime = $parsed_json->{'flightsList'}[0]->{'segmentsList'}[0]->{'arrivalTime'};
174
175
			preg_match('/\((.*?)\)/',$originShort,$originiata);
176
			$DepartureAirportIata = $originiata[1];
177
			preg_match('/\((.*?)\)/',$destinationShort,$destinationiata);
178
			$ArrivalAirportIata = $destinationiata[1];
179
180
			/*
1 ignored issue
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
181
			date_default_timezone_set('Europe/Paris');
182
			$departureTime = gmdate('H:i',strtotime($departureTime));
183
			$arrivalTime = gmdate('H:i',strtotime($arrivalTime));
184
			*/
185
		
186
			return array('DepartureAirportIATA' => $DepartureAirportIata,'DepartureTime' => $departureTime,'ArrivalAirportIATA' => $ArrivalAirportIata,'ArrivalTime' => $arrivalTime,'Source' => 'website_airfrance');
187
		} else return array();
188
	}
189
190
	/**
191
	* Get flight info from EasyJet
192
	* @param String $callsign The callsign
193
	* @param String $date date we want flight number info
194
	* @return Flight departure and arrival airports and time
195
	*/
196
	private function getEasyJet($callsign, $date = 'NOW') {
197
		global $globalTimezone;
198
		$Common = new Common();
199
		date_default_timezone_set($globalTimezone);
200
		$check_date = new Datetime($date);
201
		$numvol = preg_replace('/^[A-Z]*/','',$callsign);
202
		if (!filter_var($numvol,FILTER_VALIDATE_INT)) return array();
203
		$url = "http://www.easyjet.com/ft/api/flights?date=".$check_date->format('Y-m-d')."&fn=".$callsign;
204
		$json = $Common->getData($url);
205
		$parsed_json = json_decode($json);
206
207
		$flights = $parsed_json->{'flights'};
208
		if (count($flights) > 0) {
209
			$DepartureAirportIata = $parsed_json->{'flights'}[0]->{'airports'}->{'pda'}->{'iata'}; //name
210
			$ArrivalAirportIata = $parsed_json->{'flights'}[0]->{'airports'}->{'paa'}->{'iata'}; //name
211
			$departureTime = $parsed_json->{'flights'}[0]->{'dates'}->{'fstd'};
212
			$arrivalTime = $parsed_json->{'flights'}[0]->{'dates'}->{'fsta'};
213
214
			return array('DepartureAirportIATA' => $DepartureAirportIata,'DepartureTime' => $departureTime,'ArrivalAirportIATA' => $ArrivalAirportIata,'ArrivalTime' => $arrivalTime,'Source' => 'website_easyjet');
215
		} else return array();
216
	}
217
218
	/**
219
	* Get flight info from Ryanair
220
	* @param String $callsign The callsign
221
	* @return Flight departure and arrival airports and time
222
	*/
223
	private function getRyanair($callsign) {
224
		$Common = new Common();
225
		$numvol = preg_replace('/^[A-Z]*/','',$callsign);
226
		if (!filter_var($numvol,FILTER_VALIDATE_INT)) return array();
227
		$url = "http://www.ryanair.com/fr/api/2/flight-info/0/50/";
228
		$post = '{"flight":"'.$numvol.'","minDepartureTime":"00:00","maxDepartureTime":"23:59"}';
229
		$headers = array('Content-Type: application/json','Content-Length: ' . strlen($post));
230
		$json = $Common->getData($url,'post',$post,$headers);
231
		$parsed_json = json_decode($json);
232
		if (isset($parsed_json->{'flightInfo'})) {
233
			$flights = $parsed_json->{'flightInfo'};
234
			if (count($flights) > 0) {
235
				$DepartureAirportIata = $parsed_json->{'flightInfo'}[0]->{'departureAirport'}->{'iata'}; //name
236
				$ArrivalAirportIata = $parsed_json->{'flightInfo'}[0]->{'arrivalAirport'}->{'iata'}; //name
237
				$departureTime = $parsed_json->{'flightInfo'}[0]->{'departureTime'};
238
				$arrivalTime = $parsed_json->{'flightInfo'}[0]->{'arrivalTime'};
239
				return array('DepartureAirportIATA' => $DepartureAirportIata,'DepartureTime' => $departureTime,'ArrivalAirportIATA' => $ArrivalAirportIata,'ArrivalTime' => $arrivalTime, 'Source' => 'website_ryanair');
240
			} else return array();
241
		} else return array();
242
	}
243
244
	/**
245
	* Get flight info from Swiss
246
	* @param String $callsign The callsign
247
	* @return Flight departure and arrival airports and time
248
	*/
249
	private function getSwiss($callsign) {
250
		$Common = new Common();
251
		$numvol = preg_replace('/^[A-Z]*/','',$callsign);
252
		if (!filter_var($numvol,FILTER_VALIDATE_INT)) return array();
253
		$url = "http://www.world-of-swiss.com/fr/routenetwork.json";
254
		$json = $Common->getData($url);
255
		$parsed_json = json_decode($json);
256
257
258
		$flights = $parsed_json->{'flights'};
259
		if (count($flights) > 0) {
260
			foreach ($flights as $flight) {
261
				if ($flight->{'no'} == "Vol LX ".$numvol) {
262
					$DepartureAirportIata = $flight->{'from'}->{'code'}; //city
263
					$ArrivalAirportIata = $flight->{'to'}->{'code'}; //city
264
					$departureTime = substr($flight->{'from'}->{'hour'},0,5);
265
					$arrivalTime = substr($flight->{'to'}->{'hour'},0,5);
266
				}
267
			}
268 View Code Duplication
			if (isset($DepartureAirportIata)) {
269
				return array('DepartureAirportIATA' => $DepartureAirportIata,'DepartureTime' => $departureTime,'ArrivalAirportIATA' => $ArrivalAirportIata,'ArrivalTime' => $arrivalTime,'Source' => 'website_swiss');
270
			} else return array();
271
		} else return array();
272
	}
273
	
274
	/**
275
	* Get flight info from British Airways API
276
	* @param String $callsign The callsign
277
	* @param String $date date we want flight number info
278
	* @return Flight departure and arrival airports and time
279
	*/
280
	public function getBritishAirways($callsign, $date = 'NOW') {
281
		global $globalBritishAirwaysKey;
282
		$Common = new Common();
283
		$check_date = new Datetime($date);
284
		$numvol = sprintf('%04d',preg_replace('/^[A-Z]*/','',$callsign));
285
		if (!filter_var(preg_replace('/^[A-Z]*/','',$callsign),FILTER_VALIDATE_INT)) return array();
286
		if ($globalBritishAirwaysKey == '') return array();
287
		$url = "https://api.ba.com/rest-v1/v1/flights;flightNumber=".$numvol.";scheduledDepartureDate=".$check_date->format('Y-m-d').".json";
288
		$headers = array('Client-Key: '.$globalBritishAirwaysKey);
289
		$json = $Common->getData($url,'get','',$headers);
290
		if ($json == '') return array();
291
		$parsed_json = json_decode($json);
292
		$flights = $parsed_json->{'FlightsResponse'};
293
		if (count($flights) > 0) {
294
			$DepartureAirportIata = $parsed_json->{'FlightsResponse'}->{'Flight'}->{'Sector'}->{'DepartureAirport'};
295
			$ArrivalAirportIata = $parsed_json->{'FlightsResponse'}->{'Flight'}->{'Sector'}->{'ArrivalAirport'};
296
			$departureTime = date('H:i',strtotime($parsed_json->{'FlightsResponse'}->{'Flight'}->{'Sector'}->{'ScheduledDepartureDateTime'}));
297
			$arrivalTime = date('H:i',strtotime($parsed_json->{'FlightsResponse'}->{'Flight'}->{'Sector'}->{'ScheduledArrivalDateTime'}));
298
			return array('DepartureAirportIATA' => $DepartureAirportIata,'DepartureTime' => $departureTime,'ArrivalAirportIATA' => $ArrivalAirportIata,'ArrivalTime' => $arrivalTime,'Source' => 'website_britishairways');
299
		} else return array();
300
	}
301
302
	/**
303
	* Get flight info from Lutfhansa API
304
	* @param String $callsign The callsign
305
	* @param String $date date we want flight number info
306
	* @return Flight departure and arrival airports and time
307
	*/
308
	public function getLufthansa($callsign, $date = 'NOW') {
309
		global $globalLufthansaKey;
310
		$Common = new Common();
311
		$check_date = new Datetime($date);
312
		$numvol = sprintf('%04d',preg_replace('/^[A-Z]*/','',$callsign));
313
		if (!filter_var(preg_replace('/^[A-Z]*/','',$callsign),FILTER_VALIDATE_INT)) return array();
314
		if (!isset($globalLufthansaKey) || $globalLufthansaKey == '' || !isset($globalLufthansaKey['key']) || $globalLufthansaKey['key'] == '') return array();
315
		$url = "https://api.lufthansa.com/v1/oauth/token";
316
		$post = array('client_id' => $globalLufthansaKey['key'],'client_secret' => $globalLufthansaKey['secret'],'grant_type' => 'client_credentials');
317
		$data = $Common->getData($url,'post',$post);
318
		$parsed_data = json_decode($data);
319
		if (!isset($parsed_data->{'access_token'})) return array();
320
		$token = $parsed_data->{'access_token'};
321
		
322
		$url = "https://api.lufthansa.com/v1/operations/flightstatus/LH".$numvol."/".$check_date->format('Y-m-d');
323
		$headers = array('Authorization: Bearer '.$token,'Accept: application/json');
324
		$json = $Common->getData($url,'get','',$headers);
325
		if ($json == '') return array();
326
		$parsed_json = json_decode($json);
327
		if (isset($parsed_json->{'FlightStatusResource'}) && count($parsed_json->{'FlightStatusResource'}) > 0) {
328
			$DepartureAirportIata = $parsed_json->{'FlightStatusResource'}->{'Flights'}->{'Flight'}->{'Departure'}->{'AirportCode'};
329
			$departureTime = date('H:i',strtotime($parsed_json->{'FlightStatusResource'}->{'Flights'}->{'Flight'}->{'Departure'}->{'ScheduledTimeLocal'}->{'DateTime'}));
330
			$ArrivalAirportIata = $parsed_json->{'FlightStatusResource'}->{'Flights'}->{'Flight'}->{'Arrival'}->{'AirportCode'};
331
			$arrivalTime = date('H:i',strtotime($parsed_json->{'FlightStatusResource'}->{'Flights'}->{'Flight'}->{'Arrival'}->{'ScheduledTimeLocal'}->{'DateTime'}));
332
			return array('DepartureAirportIATA' => $DepartureAirportIata,'DepartureTime' => $departureTime,'ArrivalAirportIATA' => $ArrivalAirportIata,'ArrivalTime' => $arrivalTime,'Source' => 'website_lufthansa');
333
		} else return array();
334
	}
335
336
	/**
337
	* Get flight info from Transavia API
338
	* @param String $callsign The callsign
339
	* @param String $date date we want flight number info
340
	* @return Flight departure and arrival airports and time
341
	*/
342
	private function getTransavia($callsign, $date = 'NOW') {
343
		global $globalTransaviaKey;
344
		$Common = new Common();
345
		$check_date = new Datetime($date);
346
		$numvol = sprintf('%04d',preg_replace('/^[A-Z]*/','',$callsign));
347
		if (!filter_var(preg_replace('/^[A-Z]*/','',$callsign),FILTER_VALIDATE_INT)) return array();
348
		if ($globalTransaviaKey == '') return array();
349
		$url = "https://tst.api.transavia.com/v1/flightstatus/departuredate/".$check_date->format('Ymd').'/flightnumber/HV'.$numvol;
350
		$headers = array('apikey: '.$globalTransaviaKey);
351
		$json = $Common->getData($url,'get','',$headers);
352
		if ($json == '') return array();
353
		$parsed_json = json_decode($json);
354
		
355
		if (isset($parsed_json->{'data'}[0])) {
356
			$DepartureAirportIata = $parsed_json->{'data'}[0]->{'flight'}->{'departureAirport'}->{'locationCode'};
357
			$departureTime = date('H:i',strtotime($parsed_json->{'data'}[0]->{'flight'}->{'departureDateTime'}));
358
			$ArrivalAirportIata = $parsed_json->{'data'}[0]->{'flight'}->{'arrivalAirport'}->{'locationCode'};
359
			$arrivalTime = date('H:i',strtotime($parsed_json->{'data'}[0]->{'flight'}->{'arrivalDateTime'}));
360
			return array('DepartureAirportIATA' => $DepartureAirportIata,'DepartureTime' => $departureTime,'ArrivalAirportIATA' => $ArrivalAirportIata,'ArrivalTime' => $arrivalTime,'Source' => 'website_transavia');
361
		} else return array();
362
	}
363
364
	/**
365
	* Get flight info from Tunisair
366
	* @param String $callsign The callsign
367
	* @return Flight departure and arrival airports and time
368
	*/
369
	private function getTunisair($callsign) {
370
		$Common = new Common();
371
		$numvol = preg_replace('/^[A-Z]*/','',$callsign);
372
		if (!filter_var($numvol,FILTER_VALIDATE_INT)) return array();
373
		$url = "http://www.tunisair.com/site/publish/module/Volj/fr/Flight_List.asp";
374
		$data = $Common->getData($url);
375
		$table = $Common->table2array($data);
376
		foreach ($table as $flight) {
377
			if (isset($flight[1]) && $flight[1] == "TU ".sprintf('%04d',$numvol)) {
378
				return array('DepartureAirportIATA' => $flight[2],'DepartureTime' => str_replace('.',':',$flight[5]),'ArrivalAirportIATA' => $flight[3],'ArrivalTime' => str_replace('.',':',$flight[6]),'Source' => 'website_tunisair');
379
			}
380
		}
381
		return array();
382
	}
383
384
	/**
385
	* Get flight info from Vueling
386
	* @param String $callsign The callsign
387
	* @return Flight departure and arrival airports and time
388
	*/
389
	private function getVueling($callsign) {
390
		$Common = new Common();
391
		$numvol = preg_replace('/^[A-Z]*/','',$callsign);
392
		if (!filter_var($numvol,FILTER_VALIDATE_INT)) return array();
393
		$url = "https://www.vueling.com/Base/BaseProxy/RenderMacro/?macroalias=DailyFlights&OriginSelected=&DestinationSelected=&idioma=en-GB&pageid=30694&ItemsByPage=50&FlightNumberFilter=".$numvol;
394
		$data = $Common->getData($url);
395
		if ($data != '') {
396
			$table = $Common->table2array($data);
397
			foreach ($table as $flight) {
398
				if (count($flight) > 0 && $flight[0] == "VY".$numvol && isset($flight[13])) {
399
					preg_match('/flightOri=[A-Z]{3}/',$flight[13],$result);
400
					$DepartureAirportIata = str_replace('flightOri=','',$result[0]);
401
					preg_match('/flightDest=[A-Z]{3}/',$flight[13],$result);
402
					$ArrivalAirportIata = str_replace('flightDest=','',$result[0]);
403
					return array('DepartureAirportIATA' => $DepartureAirportIata,'DepartureTime' => $flight[3],'ArrivalAirportIATA' => $ArrivalAirportIata,'ArrivalTime' => $flight[4],'Source' => 'website_vueling');
404
				}
405
			}
406
		}
407
		return array();
408
	}
409
410
	/**
411
	* Get flight info from Iberia
412
	* @param String $callsign The callsign
413
	* @param String $date date we want flight number info
414
	* @return Flight departure and arrival airports and time
415
	*/
416
	private function getIberia($callsign, $date = 'NOW') {
417
		$Common = new Common();
418
		$numvol = preg_replace('/^[A-Z]*/','',$callsign);
419
		$check_date = new Datetime($date);
420
		if (!filter_var($numvol,FILTER_VALIDATE_INT)) return array();
421
		$url = "https://www.iberia.com/web/flightDetail.do";
422
		$post = array('numvuelo' => $numvol,'fecha' => $check_date->format('Ymd'),'airlineID' => 'IB');
423
		$data = $Common->getData($url,'post',$post);
424
		if ($data != '') {
425
			$table = $Common->table2array($data);
426
			//print_r($table);
427
			if (count($table) > 0) {
428
				$flight = $table;
429
				preg_match('/([A-Z]{3})/',$flight[3][0],$DepartureAirportIataMatch);
430
				preg_match('/([A-Z]{3})/',$flight[5][0],$ArrivalAirportIataMatch);
431
				$DepartureAirportIata = $DepartureAirportIataMatch[0];
432
				$ArrivalAirportIata = $ArrivalAirportIataMatch[0];
433
				$departureTime = substr(trim(str_replace(' lunes','',str_replace('&nbsp;','',$flight[3][2]))),0,5);
434
				$arrivalTime = trim(str_replace(' lunes','',str_replace('&nbsp;','',$flight[5][1])));
435
				if ($arrivalTime == 'Hora estimada de llegada') {
436
					$arrivalTime = substr(trim(str_replace(' lunes','',str_replace('&nbsp;','',$flight[5][2]))),0,5);
437
				} else $arrivalTime = substr($arrivalTime,0,5);
438
				return array('DepartureAirportIATA' => $DepartureAirportIata,'DepartureTime' => $departureTime,'ArrivalAirportIATA' => $ArrivalAirportIata,'ArrivalTime' => $arrivalTime,'Source' => 'website_iberia');
439
			}
440
		}
441
		return array();
442
	}
443
444
	/**
445
	* Get flight info from Star Alliance
446
	* @param String $callsign The callsign
447
	* @param String $date date we want flight number info
448
	* @return Flight departure and arrival airports and time
449
	*/
450
	private function getStarAlliance($callsign, $date = 'NOW',$carrier = '') {
451
		$Common = new Common();
452
		$numvol = preg_replace('/^[A-Z]*/','',$callsign);
453
		$check_date = new Datetime($date);
454
		if (!filter_var($numvol,FILTER_VALIDATE_INT)) return array();
455
		$url = "http://www.staralliance.com/flifoQueryAction.do?myAirline=&airlineCode=".$carrier."&flightNo=".$numvol."&day=".$check_date->format('d')."&month=".$check_date->format('m')."&year=".$check_date->format('Y')."&departuredate=".$check_date->format('d-M-Y');
456
		$data = $Common->getData($url);
457
		if ($data != '') {
458
			$table = $Common->table2array($data);
459
			if (count($table) > 0) {
460
				$flight = $table;
461
				//print_r($table);
462
				if (isset($flight[25]) && isset($flight[29])) {
463
					preg_match('/([A-Z]{3})/',$flight[25][1],$DepartureAirportIataMatch);
464
					preg_match('/([A-Z]{3})/',$flight[25][3],$ArrivalAirportIataMatch);
465
					$DepartureAirportIata = $DepartureAirportIataMatch[0];
466
					$ArrivalAirportIata = $ArrivalAirportIataMatch[0];
467
					$departureTime = substr(trim(str_replace('Scheduled: ','',$flight[29][0])),0,5);
468
					$arrivalTime = substr(trim(str_replace('Scheduled: ','',$flight[29][1])),0,5);
469
					return array('DepartureAirportIATA' => $DepartureAirportIata,'DepartureTime' => $departureTime,'ArrivalAirportIATA' => $ArrivalAirportIata,'ArrivalTime' => $arrivalTime,'Source' => 'website_staralliance');
470
				} else return array();
471
			}
472
			
473
474
		}
475
		return array();
476
	}
477
478
	/**
479
	* Get flight info from Alitalia
480
	* @param String $callsign The callsign
481
	* @param String $date date we want flight number info
482
	* @return Flight departure and arrival airports and time
483
	*/
484
	private function getAlitalia($callsign, $date = 'NOW') {
485
		$Common = new Common();
486
		$numvol = preg_replace('/^[A-Z]*/','',$callsign);
487
		$check_date = new Datetime($date);
488
		$url= "http://booking.alitalia.com/FlightStatus/fr_fr/FlightInfo?Brand=az&NumeroVolo=".$numvol."&DataCompleta=".$check_date->format('d/m/Y');
489
		if (!filter_var($numvol,FILTER_VALIDATE_INT)) return array();
490
		$data = $Common->getData($url);
491
		if ($data != '') {
492
			$table = $Common->text2array($data);
493
			$DepartureAirportIata = '';
494
			$ArrivalAirportIata = '';
495
			$departureTime = $table[4];
496
			$arrivalTime = $table[5];
497
			return array('DepartureAirportIATA' => $DepartureAirportIata,'DepartureTime' => $departureTime,'ArrivalAirportIATA' => $ArrivalAirportIata,'ArrivalTime' => $arrivalTime,'Source' => 'website_alitalia');
498
		}
499
	}
500
501
	/**
502
	* Get flight info from Brussels airlines
503
	* @param String $callsign The callsign
504
	* @param String $date date we want flight number info
505
	* @return Flight departure and arrival airports and time
506
	*/
507
	private function getBrussels($callsign, $date = 'NOW') {
508
		$Common = new Common();
509
		$numvol = preg_replace('/^[A-Z]*/','',$callsign);
510
		$check_date = new Datetime($date);
511
		$url= "http://www.brusselsairlines.com/api/flightstatus/getresults?from=NA&to=NA&date=".$check_date->format('d/m/Y')."&hour=NA&lookup=flightnumber&flightnumber=".$numvol."&publicationID=302";
512
		//http://www.brusselsairlines.com/fr-fr/informations-pratiques/statut-de-votre-vol/resultat.aspx?flightnumber=".$numvol."&date=".$check_date->format('d/m/Y')."&lookup=flightnumber";
513
		if (!filter_var($numvol,FILTER_VALIDATE_INT)) return array();
514
		$data = $Common->getData($url);
515
		if ($data != '') {
516
		    //echo $data;
517
		    $parsed_json = json_decode($data);
518
		    if (isset($parsed_json[0]->FromAirportCode)) {
519
			$DepartureAirportIata = $parsed_json[0]->FromAirportCode;
520
			$ArrivalAirportIata = $parsed_json[0]->ToAirportCode;
521
			$departureTime = date('H:i',strtotime($parsed_json[0]->ScheduledDepatureDate));
522
			$arrivalTime = date('H:i',strtotime($parsed_json[0]->ScheduledArrivalDate));
523
			return array('DepartureAirportIATA' => $DepartureAirportIata,'DepartureTime' => $departureTime,'ArrivalAirportIATA' => $ArrivalAirportIata,'ArrivalTime' => $arrivalTime,'Source' => 'website_brussels');
524
		    }
525
		}
526
	}
527
528
	/**
529
	* Get flight info from FlightRadar24
530
	* @param String $callsign The callsign
531
	* @param String $date date we want flight number info
532
	* @return Flight departure and arrival airports and time
533
	*/
534
/*
1 ignored issue
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
535
	public function getFlightRadar24($callsign, $date = 'NOW') {
536
		$Common = new Common();
537
		$url= "http://arn.data.fr24.com/zones/fcgi/feed.js?flight=".$callsign;
538
		$data = $Common->getData($url);
539
		if ($data != '') {
540
			$parsed_json = get_object_vars(json_decode($data));
541
			if (count($parsed_json) > 2) {
542
				$info = array_splice($parsed_json,2,1);
543
				$fr24id = current(array_keys($info));
544
				$urldata = "http://krk.data.fr24.com/_external/planedata_json.1.4.php?f=".$fr24id;
545
				$datapl = $Common->getData($urldata);
546
				if ($datapl != '') {
547
					$parsed_jsonpl = json_decode($datapl);
548
					if (isset($parsed_jsonpl->from_iata)) {
549
						$DepartureAirportIata = $parsed_jsonpl->from_iata;
550
						$ArrivalAirportIata = $parsed_jsonpl->to_iata;
551
						$departureTime = date('H:i',$parsed_jsonpl->dep_schd);
552
						$arrivalTime = date('H:i',$parsed_jsonpl->arr_schd);
553
						return array('DepartureAirportIATA' => $DepartureAirportIata,'DepartureTime' => $departureTime,'ArrivalAirportIATA' => $ArrivalAirportIata,'ArrivalTime' => $arrivalTime,'Source' => 'website_flightradar24');
554
					}
555
				}
556
			}
557
		}
558
		return array();
559
	}
560
  */
561
	/**
562
	* Get flight info from Lufthansa
563
	* @param String $callsign The callsign
564
	* @param String $date date we want flight number info
565
	* @return Flight departure and arrival airports and time
566
	*/
567
568
/*	private function getLufthansa($callsign, $date = 'NOW') {
569
		$Common = new Common();
570
		*/
571
		//$numvol = preg_replace('/^[A-Z]*/','',$callsign);
572
/*
573
		$url= "http://www.lufthansa.com/fr/fr/Arrivees-Departs-fonction";
574
		$check_date = new Datetime($date);
575
		if (!filter_var($numvol,FILTER_VALIDATE_INT)) return array();
576
577
		$post = array('flightNumber' => $numvol, 'date' => $check_date->format('Y-m-d'),'time' => '12:00','timezoneOffset' => '0','selection' => '0','arrivalDeparture' => 'D');
578
		$data = $Common->getData($url,'post',$post);
579
		if ($data != '') {
580
			$table = $Common->table2array($data);
581
			$departureTime = trim(str_replace($check_date->format('d.m.Y'),'',$table[25][3]));
582
		}
583
584
		$post = array('flightNumber' => $numvol, 'date' => $check_date->format('Y-m-d'),'time' => '12:00','timezoneOffset' => '0','selection' => '0','arrivalDeparture' => 'A');
585
		$data = $Common->getData($url,'post',$post);
586
		if ($data != '') {
587
			$table = $Common->table2array($data);
588
			$arrivalTime = trim(str_replace($check_date->format('d.m.Y'),'',$table[25][3]));
589
		}
590
		return array('DepartureAirportIATA' => '','DepartureTime' => $departureTime,'ArrivalAirportIATA' => '','ArrivalTime' => $arrivalTime,'Source' => 'website_lufthansa');
591
	}
592
  */
593
	/**
594
	* Get flight info from flytap
595
	* @param String $callsign The callsign
596
	* @param String $date date we want flight number info
597
	* @return Flight departure and arrival airports and time
598
	*/
599
	private function getFlyTap($callsign, $date = 'NOW') {
600
		$Common = new Common();
601
		$numvol = preg_replace('/^[A-Z]*/','',$callsign);
602
		$url= "http://www.flytap.com/France/fr/PlanifierEtReserver/Outils/DepartsEtArrivees";
603
		$check_date = new Datetime($date);
604
		if (!filter_var($numvol,FILTER_VALIDATE_INT)) return array();
605
		$post = array('arrivalsdepartures_content' => 'number','arrivalsdepartures_tp' => $numvol,'arrivalsdepartures_trk' => 'ARR','arrivalsdepartures_date_trk' => '1','aptCode' => '','arrivalsdepartures' => 'DEP','arrivalsdepartures_date' => '1','aptCodeFrom' => '','aptCodeTo' => '','arrivalsdepartures2' => 'DEP','arrivalsdepartures_date2' => '1');
606
		$data = $Common->getData($url,'post',$post);
607
		if ($data != '') {
608
			$table = $Common->table2array($data);
609
			$departureTime = trim(substr($table[15][0],0,5));
610
			$arrivalTime = trim(substr($table[35][0],0,5));
611
			preg_match('/([A-Z]{3})/',$table[11][0],$DepartureAirportIataMatch);
612
			preg_match('/([A-Z]{3})/',$table[31][0],$ArrivalAirportIataMatch);
613
			$DepartureAirportIata = $DepartureAirportIataMatch[0];
614
			$ArrivalAirportIata = $ArrivalAirportIataMatch[0];
615
			return array('DepartureAirportIATA' => $DepartureAirportIata,'DepartureTime' => $departureTime,'ArrivalAirportIATA' => $ArrivalAirportIata,'ArrivalTime' => $arrivalTime,'Source' => 'website_flytap');
616
		}
617
		return array();
618
	}
619
620
	/**
621
	* Get flight info from flightmapper
622
	* @param String $callsign The callsign
623
	* @param String $date date we want flight number info
624
	* @return Flight departure and arrival airports and time
625
	*/
626
	public function getFlightMapper($callsign, $date = 'NOW') {
627
		$Common = new Common();
628 View Code Duplication
		if (!is_numeric(substr($callsign, 0, 3)))
629
		{
630
			if (is_numeric(substr(substr($callsign, 0, 3), -1, 1))) {
631
				$airline_icao = substr($callsign, 0, 2);
632
			} elseif (is_numeric(substr(substr($callsign, 0, 4), -1, 1))) {
633
				$airline_icao = substr($callsign, 0, 3);
634
			} 
635
		}
636
		$numvol = preg_replace('/^[A-Z]*/','',$callsign);
637
		$url= "http://info.flightmapper.net/flight/".$airline_icao.'_'.$numvol;
638
		$check_date = new Datetime($date);
639
		if (!filter_var($numvol,FILTER_VALIDATE_INT)) return array();
640
		$data = $Common->getData($url);
641
		if ($data != '') {
642
			$table = $Common->table2array($data);
643
			if (isset($table[5][0])) {
644
				$sched = $table[5][0];
645
				$n = sscanf($sched,'%*s %5[0-9:] %*[^()] (%3[A-Z]) %5[0-9:] %*[^()] (%3[A-Z])',$dhour,$darr,$ahour,$aarr);
646
				if ($n == 7) {
647
				    $departureTime = $dhour;
648
				    $arrivalTime = $ahour;
649
				    $DepartureAirportIata = str_replace(array('(',')'),'',$darr);
650
				    $ArrivalAirportIata = str_replace(array('(',')'),'',$aarr);
651
				    return array('DepartureAirportIATA' => $DepartureAirportIata,'DepartureTime' => $departureTime,'ArrivalAirportIATA' => $ArrivalAirportIata,'ArrivalTime' => $arrivalTime,'Source' => 'website_flightmapper');
652
				}
653
			}
654
		}
655
		return array();
656
	}
657
658
	/**
659
	* Get flight info from flightaware
660
	* @param String $callsign The callsign
661
	* @param String $date date we want flight number info
662
	* @return Flight departure and arrival airports and time
663
	*/
664
	public function getFlightAware($callsign, $date = 'NOW') {
665
		$Common = new Common();
666 View Code Duplication
		if (!is_numeric(substr($callsign, 0, 3)))
667
		{
668
			if (is_numeric(substr(substr($callsign, 0, 3), -1, 1))) {
669
				$airline_icao = substr($callsign, 0, 2);
670
			} elseif (is_numeric(substr(substr($callsign, 0, 4), -1, 1))) {
671
				$airline_icao = substr($callsign, 0, 3);
672
			} 
673
		}
674
		$numvol = preg_replace('/^[A-Z]*/','',$callsign);
675
		$url= "http://fr.flightaware.com/live/flight/".$callsign;
676
		$check_date = new Datetime($date);
677
		if (!filter_var($numvol,FILTER_VALIDATE_INT)) return array();
678
		$data = $Common->getData($url);
679
		if ($data != '') {
680
			$table = $Common->table2array($data);
681
			if (isset($table[11][0])) {
682
				$departureTime = str_replace('h',':',substr($table[5][0],0,5));
683
				$arrivalTime = str_replace('h',':',substr($table[5][1],0,5));
684
				echo $table[3][0];
685
				sscanf($table[3][0],'%*[^(] (%3[A-Z] / %*4[A-Z])',$DepartureAirportIata);
686
				sscanf($table[3][1],'%*[^(] (%3[A-Z] / %*4[A-Z])',$ArrivalAirportIata);
687
				return array('DepartureAirportIATA' => $DepartureAirportIata,'DepartureTime' => $departureTime,'ArrivalAirportIATA' => $ArrivalAirportIata,'ArrivalTime' => $arrivalTime,'Source' => 'website_flightaware');
688
			}
689
		}
690
		return array();
691
	}
692
693
	/**
694
	* Get flight info from CostToTravel
695
	* @param String $callsign The callsign
696
	* @param String $date date we want flight number info
697
	* @return Flight departure and arrival airports and time
698
	*/
699
	public function getCostToTravel($callsign, $date = 'NOW') {
700
		$Common = new Common();
701
		$url= "http://www.costtotravel.com/flight-number/".$callsign;
702
		$check_date = new Datetime($date);
703
		//if (!filter_var($numvol,FILTER_VALIDATE_INT)) return array();
704
		$data = $Common->getData($url);
705
		if ($data != '') {
706
			$table = $Common->table2array($data);
707
			//print_r($table);
708
			if (isset($table[11][1])) {
709
				$departureTime = substr($table[11][1],0,5);
710
				$arrivalTime = substr($table[17][1],0,5);
711
				$DepartureAirportIata = substr($table[13][1],0,3);
712
				$ArrivalAirportIata = substr($table[15][1],0,3);
713
				return array('DepartureAirportIATA' => $DepartureAirportIata,'DepartureTime' => $departureTime,'ArrivalAirportIATA' => $ArrivalAirportIata,'ArrivalTime' => $arrivalTime,'Source' => 'website_costtotravel');
714
			}
715
		}
716
		return array();
717
	}
718
719
	/**
720
	* Get flight info from Air Canada
721
	* @param String $callsign The callsign
722
	* @param String $date date we want flight number info
723
	* @return Flight departure and arrival airports and time
724
	*/
725
	private function getAirCanada($callsign,$date = 'NOW') {
726
		$Common = new Common();
727
		date_default_timezone_set('UTC');
728
		$check_date = new Datetime($date);
729
		$numvol = preg_replace('/^[A-Z]*/','',$callsign);
730
		$url= "http://services.aircanada.com/portal/rest/getFlightsByFlightNumber?forceTimetable=true&flightNumber=".$numvol."&carrierCode=AC&date=".$check_date->format('m-d-Y')."&app_key=AE919FDCC80311DF9BABC975DFD72085&cache=74249";
731
		if (!filter_var($numvol,FILTER_VALIDATE_INT)) return array();
732
		$data = $Common->getData($url);
733
		$dom = new DomDocument();
734
		$dom->loadXML($data);
735
		if ($dom->getElementsByTagName('DepartureStationInfo')->length == 0) return array();
736
		$departure = $dom->getElementsByTagName('DepartureStationInfo')->item(0);
737
		if (isset($departure->getElementsByTagName('Airport')->item(0)->firstChild->nodeValue)) {
738
			$DepartureAirportIata = $departure->getElementsByTagName('Airport')->item(0)->firstChild->nodeValue;
739
			$departureTime = date('H:i',strtotime($departure->getElementsByTagName('ScheduledTime')->item(0)->firstChild->nodeValue));
740
			$arrival = $dom->getElementsByTagName('ArrivalStationInfo')->item(0);
741
			$ArrivalAirportIata = $arrival->getElementsByTagName('Airport')->item(0)->firstChild->nodeValue;
742
			$arrivalTime = date('H:i',strtotime($arrival->getElementsByTagName('ScheduledTime')->item(0)->firstChild->nodeValue));
743
			return array('DepartureAirportIATA' => $DepartureAirportIata,'DepartureTime' => $departureTime,'ArrivalAirportIATA' => $ArrivalAirportIata,'ArrivalTime' => $arrivalTime,'Source' => 'website_aircanada');
744
		} else return array();
745
	}
746
747
	/**
748
	* Get flight info from Vietnam Airlines
749
	* @param String $callsign The callsign
750
	* @param String $date date we want flight number info
751
	* @return Flight departure and arrival airports and time
752
	*/
753
	private function getVietnamAirlines($callsign, $date = 'NOW') {
754
		$Common = new Common();
755
		$numvol = preg_replace('/^[A-Z]*/','',$callsign);
756
		$check_date = new Datetime($date);
757
		$url= "https://cat.sabresonicweb.com/SSWVN/meridia?posid=VNVN&page=flifoFlightInfoDetailsMessage_learn&action=flightInfoDetails&airline=VN&language=fr&depDay=".$check_date->format('j')."&depMonth=".strtoupper($check_date->format('M'))."&=&flight=".$numvol."&";
758
		if (!filter_var($numvol,FILTER_VALIDATE_INT)) return array();
759
		$data = $Common->getData($url);
760
		if ($data != '') {
761
			$table = $Common->table2array($data);
762
			$flight = $table;
763
			preg_match('/([A-Z]{3})/',$flight[3][0],$DepartureAirportIataMatch);
764
			preg_match('/([A-Z]{3})/',$flight[21][0],$ArrivalAirportIataMatch);
765
			$DepartureAirportIata = $DepartureAirportIataMatch[0];
766
			$ArrivalAirportIata = $ArrivalAirportIataMatch[0];
767
			$departureTime = $flight[5][1];
768
			$arrivalTime = $flight[23][1];
769
			return array('DepartureAirportIATA' => $DepartureAirportIata,'DepartureTime' => $departureTime,'ArrivalAirportIATA' => $ArrivalAirportIata,'ArrivalTime' => $arrivalTime,'Source' => 'website_vietnamairlines');
770
		}
771
	}
772
773
	/**
774
	* Get flight info from Air Berlin
775
	* @param String $callsign The callsign
776
	* @param String $date date we want flight number info
777
	* @param String $carrier IATA code
778
	* @return Flight departure and arrival airports and time
779
	*/
780
	private function getAirBerlin($callsign, $date = 'NOW', $carrier = 'AB') {
781
		$Common = new Common();
782
		date_default_timezone_set('UTC');
783
		//AB = airberlin, HG/NLY = NIKI, 4T/BHP = Belair 
784
		$numvol = preg_replace('/^[A-Z]*/','',$callsign);
785
		$check_date = new Datetime($date);
786
		$url= "http://www.airberlin.com/en-US/site/aims.php";
787
		if (!filter_var($numvol,FILTER_VALIDATE_INT)) return array();
788
		$post = array('type' => 'departure','searchFlightNo' => '1','requestsent' => 'true', 'flightno' => $numvol,'date' => $check_date->format('Y-m-d'),'carrier' => 'AB');
789
		$data = $Common->getData($url,'post',$post);
790
		//echo $data;
791
		$DepartureAirportIata = '';
792
		$ArrivalAirportIata = '';
793
		
794
		if ($data != '') {
795
			$table = $Common->table2array($data);
796
			$flight = $table;
797
			if (isset($flight[5][4])) $departureTime = $flight[5][4];
798
			else $departureTime = '';
799
			if (isset($flight[5][2])) $departureAirport = $flight[5][2];
800
			else $departureAirport = '';
801
		}
802
		$post = array('type' => 'arrival','searchFlightNo' => '1','requestsent' => 'true', 'flightno' => $numvol,'date' => $check_date->format('Y-m-d'),'carrier' => 'AB');
803
		$data = $Common->getData($url,'post',$post);
804
		if ($data != '') {
805
			$table = $Common->table2array($data);
806
			$flight = $table;
807
			if (isset($flight[5][4])) {
808
			    $arrivalTime = $flight[5][4];
809
			    $arrivalAirport = $flight[5][3];
810
			} else {
811
			    $arrivalTime = '';
812
			    $arrivalAirport = '';
813
			}
814
		}
815
		$url = 'http://www.airberlin.com/en-US/site/json/suggestAirport.php?searchfor=departures&searchflightid=0&departures%5B%5D=&suggestsource%5B0%5D=activeairports&withcountries=0&withoutroutings=0&promotion%5Bid%5D=&promotion%5Btype%5D=&routesource%5B0%5D=airberlin&routesource%5B1%5D=partner';
816
		$json = $Common->getData($url);
817
		if ($json == '') return array();
818
		$parsed_json = json_decode($json);
819
		$airports = $parsed_json->{'suggestList'};
820
		if (count($airports) > 0) {
821
			foreach ($airports as $airinfo) {
822
				if ($airinfo->{'name'} == $departureAirport) {
823
					$DepartureAirportIata = $airinfo->{'code'};
824
				}
825
				if ($airinfo->{'name'} == $arrivalAirport) {
826
					$ArrivalAirportIata = $airinfo->{'code'};
827
				}
828
			}
829
		}
830 View Code Duplication
		if (isset($DepartureAirportIata)) {
831
			return array('DepartureAirportIATA' => $DepartureAirportIata,'DepartureTime' => $departureTime,'ArrivalAirportIATA' => $ArrivalAirportIata,'ArrivalTime' => $arrivalTime,'Source' => 'website_airberlin');
832
		} else return array();
833
	}
834
835
836
	
837
	public function fetchSchedule($ident,$date = 'NOW') {
838
		global $globalSchedulesSources, $globalSchedulesFetch;
839
		$Common = new Common();
840
		if (!$globalSchedulesFetch) return array();
841
		$airline_icao = '';
842 View Code Duplication
		if (!is_numeric(substr($ident, 0, 3)))
843
		{
844
			if (is_numeric(substr(substr($ident, 0, 3), -1, 1))) {
845
				$airline_icao = substr($ident, 0, 2);
846
			} elseif (is_numeric(substr(substr($ident, 0, 4), -1, 1))) {
847
				$airline_icao = substr($ident, 0, 3);
848
			} 
849
		}
850
		if ($airline_icao != '') {
851
			switch ($airline_icao) {
852
/*
1 ignored issue
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
853
				// Adria Airways
854
				case "ADR":
855
				case "JP":
856
					return Schedule->getStarAlliance($ident,$date,'JP');
857
					break;
858
				// Aegean Airlines
859
				case "AEE":
860
				case "A3":
861
					return Schedule->getStarAlliance($ident,$date,'A3');
862
					break;
863
				// Air Canada
864
				case "ACA":
865
				case "AC":
866
					return Schedule->getStarAlliance($ident,$date,'AC');
867
					break;
868
				// Air China
869
				case "CCA":
870
				case "CA":
871
					return Schedule->getStarAlliance($ident,$date,'CA');
872
					break;
873
				// Air India
874
				case "AIC":
875
				case "AI":
876
					return Schedule->getStarAlliance($ident,$date,'AI');
877
					break;
878
				// Air New Zealand
879
				case "ANZ":
880
				case "NZ":
881
					return Schedule->getStarAlliance($ident,$date,'NZ');
882
					break;
883
				// All Nippon Airways
884
				case "ANA":
885
				case "NH":
886
					return Schedule->getStarAlliance($ident,$date,'NH');
887
					break;
888
				// Asiana Airlines
889
				case "AAR":
890
				case "OZ":
891
					return Schedule->getStarAlliance($ident,$date,'OZ');
892
					break;
893
				// Austrian
894
				case "AUA":
895
				case "OS":
896
					return Schedule->getStarAlliance($ident,$date,'OS');
897
					break;
898
				// Avianca
899
				case "AVA":
900
				case "AV":
901
					return Schedule->getStarAlliance($ident,$date,'AV');
902
					break;
903
*/
904
				// Brussels Airlines
905
				case "BEL":
906
				case "SN":
907
					return $this->getBrussels($ident,$date,'SN');
908
					break;
909
/*
1 ignored issue
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
910
				// Copa Airlines
911
				case "CMP":
912
				case "CM":
913
					return Schedule->getStarAlliance($ident,$date,'CM');
914
					break;
915
				// Croatia Airlines
916
				case "CTN":
917
				case "OU":
918
					return Schedule->getStarAlliance($ident,$date,'OU');
919
					break;
920
				// Egyptair
921
				case "MSR":
922
				case "MS":
923
					return Schedule->getStarAlliance($ident,$date,'MS');
924
					break;
925
				// Ethiopian Airlines
926
				case "ETH":
927
				case "ET":
928
					return Schedule->getStarAlliance($ident,$date,'ET');
929
					break;
930
				// Eva Air
931
				case "EVA":
932
				case "BR":
933
					return Schedule->getStarAlliance($ident,$date,'BR');
934
					break;
935
				// LOT Polish Airlines
936
				case "LOT":
937
				case "LO":
938
					return Schedule->getStarAlliance($ident,$date,'LO');
939
					break;
940
				// Scandinavian Airlines
941
				case "SAS":
942
				case "SK":
943
					return Schedule->getStarAlliance($ident,$date,'SK');
944
					break;
945
				// Shenzhen Airlines
946
				case "CSZ":
947
				case "ZH":
948
					return Schedule->getStarAlliance($ident,$date,'ZH');
949
					break;
950
				// Singapore Airlines
951
				case "SIA":
952
				case "SQ":
953
					return Schedule->getStarAlliance($ident,$date,'SQ');
954
					break;
955
				// South African Airways
956
				case "SAA":
957
				case "SA":
958
					return Schedule->getStarAlliance($ident,$date,'SA');
959
					break;
960
*/
961
				// SWISS
962
				case "SWR":
963
				case "LX":
964
					return $this->getSwiss($ident);
965
					break;
966
967
				// TAP Portugal
968
				case "TAP":
969
				case "TP":
970
					return $this->getFlyTap($ident,$date);
971
					break;
972
/*
1 ignored issue
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
973
				// Thai Airways International
974
				case "THA":
975
				case "TG":
976
					return Schedule->getStarAlliance($ident,$date,'TG');
977
					break;
978
				// Turkish Airlines
979
				case "THY":
980
				case "TK":
981
					return Schedule->getStarAlliance($ident,$date,'TK');
982
					break;
983
				// United
984
				case "UAL":
985
				case "UA":
986
					return Schedule->getStarAlliance($ident,$date,'UA');
987
					break;
988
*/
989
				// Air France
990
				case "AF":
991
				case "AFR":
992
					return $this->getAirFrance($ident,$date,'AF');
993
					break;
994
				// HOP
995
				case "A5":
996
				case "HOP":
997
					return $this->getAirFrance($ident,$date,'A5');
998
					break;
999
				// EasyJet
1000
				case "U2":
1001
				case "DS":
1002
				case "EZY":
1003
				case "EZS":
1004
					return $this->getEasyJet($ident,$date);
1005
					break;
1006
				// Ryanair
1007
				case "FR":
1008
				case "RYR":
1009
					return $this->getRyanair($ident);
1010
					break;
1011
				// British Airways
1012
				case "BA":
1013
				case "SHT":
1014
				case "BAW":
1015
					return $this->getBritishAirways($ident);
1016
					break;
1017
				// Tunisair
1018
				case "TUI":
1019
				case "TAR":
1020
				case "TU":
1021
					return $this->getTunisair($ident);
1022
					break;
1023
				// Vueling
1024
				case "VLG":
1025
				case "VY":
1026
					return $this->getVueling($ident);
1027
					break;
1028
				// Alitalia
1029
				case "AZ":
1030
				case "AZA":
1031
					return $this->getAlitalia($ident);
1032
					break;
1033
				// Air Canada
1034
				case "ACA":
1035
				case "AC":
1036
					return $this->getAirCanada($ident);
1037
					break;
1038
				// Lufthansa
1039
				case "DLH":
1040
				case "LH":
1041
					return $this->getLufthansa($ident);
1042
					break;
1043
				// Transavia
1044
				case "TRA":
1045
				case "HV":
1046
					return $this->getTransavia($ident);
1047
					break;
1048
					
1049
/*
1 ignored issue
show
Unused Code Comprehensibility introduced by
71% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1050
				case "DLH":
1051
				case "LH":
1052
					return $this->getStarAlliance($ident,$date,'LH');
1053
					break;
1054
*/
1055
				// Iberia
1056
				case "IBE":
1057
				case "IB":
1058
					return $this->getIberia($ident);
1059
					break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
1060
				// Vietnam Airlines
1061
				case "HVN":
1062
					return $this->getVietnamAirlines($ident,$date);
1063
					break;
1064
				// Air Berlin
1065
				case "AB":
1066
				case "BER":
1067
					return $this->getAirBerlin($ident,$date,'AB');
1068
					break;
1069
				// NIKI
1070
				case "HG":
1071
				case "NLY":
1072
					return $this->getAirBerlin($ident,$date,'HG');
1073
					break;
1074
				// BelAir
1075
				case "4T":
1076
				case "BHP":
1077
					return $this->getAirBerlin($ident,$date,'4T');
1078
					break;
1079
				default:
1080
					// Randomly use a generic function to get hours
1081
					if (strlen($airline_icao) == 2) {
1082
						if (!isset($globalSchedulesSources)) $globalSchedulesSources = array('flightmapper','costtotravel','flightradar24','flightaware');
1083
						if (count($globalSchedulesSources) > 0) {
1084
							$rand = mt_rand(0,count($globalSchedulesSources)-1);
1085
							$source = $globalSchedulesSources[$rand];
1086
							if ($source == 'flightmapper') return $this->getFlightMapper($ident,$date);
1087
							elseif ($source == 'costtotravel') return $this->getCostToTravel($ident,$date);
1088
							//elseif ($source == 'flightradar24') return $this->getFlightRadar24($ident,$date);
1089
							elseif ($source == 'flightaware') return $this->getFlightAware($ident,$date);
1090
						}
1091
					}
1092
			}
1093
		}
1094
	        return array();
1095
	}
1096
}
1097
1098
  /*
1 ignored issue
show
Unused Code Comprehensibility introduced by
68% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1099
$Schedule = new Schedule();
1100
//print_r($Schedule->fetchSchedule('HV5661'));
1101
//print_r($Schedule->getFlightAware('AF1179'));
1102
//print_r($Schedule->getBritishAirways('BAW551'));
1103
print_r($Schedule->getLufthansa('LH551'));
1104
*/
1105
?>