Completed
Push — master ( 6c4f55...213543 )
by Yannick
10:31
created

Image::addMarineImage()   C

Complexity

Conditions 13
Paths 65

Size

Total Lines 34
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 27
nc 65
nop 3
dl 0
loc 34
rs 5.1234
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
require_once(dirname(__FILE__).'/class.Spotter.php');
3
require_once(dirname(__FILE__).'/class.Common.php');
4
require_once(dirname(__FILE__).'/settings.php');
5
6
class Image {
7
	public $db;
8
9
	public function __construct($dbc = null) {
10
		$Connection = new Connection($dbc);
11
		$this->db = $Connection->db();
12
	}
13
14
	/**
15
	* Gets the images based on the aircraft registration
16
	*
17
	* @return Array the images list
18
	*
19
	*/
20
	public function getSpotterImage($registration,$aircraft_icao = '', $airline_icao = '')
21
	{
22
		$registration = filter_var($registration,FILTER_SANITIZE_STRING);
23
		$aircraft_icao = filter_var($aircraft_icao,FILTER_SANITIZE_STRING);
24
		$airline_icao = filter_var($airline_icao,FILTER_SANITIZE_STRING);
25
		$reg = $registration;
26
		if ($reg == '' && $aircraft_icao != '') $reg = $aircraft_icao.$airline_icao;
27
		$reg = trim($reg);
28
		$query  = "SELECT spotter_image.image, spotter_image.image_thumbnail, spotter_image.image_source, spotter_image.image_source_website,spotter_image.image_copyright, spotter_image.registration 
29
			FROM spotter_image 
30
			WHERE spotter_image.registration = :registration LIMIT 1";
31
		$sth = $this->db->prepare($query);
32
		$sth->execute(array(':registration' => $reg));
33
		$result = $sth->fetchAll(PDO::FETCH_ASSOC);
34
		if (!empty($result)) return $result;
35
		elseif ($registration != '') return $this->getSpotterImage('',$aircraft_icao,$airline_icao);
36
		else return array();
37
	}
38
39
	/**
40
	* Gets the images based on the ship name
41
	*
42
	* @return Array the images list
43
	*
44
	*/
45
	public function getMarineImage($mmsi,$imo = '',$name = '')
46
	{
47
		$mmsi = filter_var($mmsi,FILTER_SANITIZE_STRING);
48
		$imo = filter_var($imo,FILTER_SANITIZE_STRING);
49
		$name = filter_var($name,FILTER_SANITIZE_STRING);
50
		$name = trim($name);
51
		$query  = "SELECT marine_image.image, marine_image.image_thumbnail, marine_image.image_source, marine_image.image_source_website,marine_image.image_copyright, marine_image.mmsi, marine_image.imo, marine_image.name 
52
			FROM marine_image 
53
			WHERE marine_image.mmsi = :mmsi";
54
		$query_data = array(':mmsi' => $mmsi);
55
		if ($imo != '') {
56
			$query .= " AND marine_image.imo = :imo";
57
			$query_data = array_merge($query_data,array(':imo' => $imo));
58
		}
59
		if ($name != '') {
60
			$query .= " AND marine_image.name = :name";
61
			$query_data = array_merge($query_data,array(':name' => $name));
62
		}
63
		$query .= " LIMIT 1";
64
		$sth = $this->db->prepare($query);
65
		$sth->execute($query_data);
66
		$result = $sth->fetchAll(PDO::FETCH_ASSOC);
67
		return $result;
68
	}
69
70
	/**
71
	* Gets the image copyright based on the Exif data
72
	*
73
	* @return String image copyright
74
	*
75
	*/
76
	public function getExifCopyright($url) {
77
		$exif = exif_read_data($url);
78
		$copyright = '';
79
		if (isset($exif['COMPUTED']['copyright'])) $copyright = $exif['COMPUTED']['copyright'];
80
		elseif (isset($exif['copyright'])) $copyright = $exif['copyright'];
81
		if ($copyright != '') {
82
			$copyright = str_replace('Copyright ','',$copyright);
83
			$copyright = str_replace('© ','',$copyright);
84
			$copyright = str_replace('(c) ','',$copyright);
85
		}
86
		return $copyright;
87
	}
88
89
	/**
90
	* Adds the images based on the aircraft registration
91
	*
92
	* @return String either success or error
93
	*
94
	*/
95
	public function addSpotterImage($registration,$aircraft_icao = '', $airline_icao = '')
96
	{
97
		global $globalDebug,$globalAircraftImageFetch;
98
		if (isset($globalAircraftImageFetch) && !$globalAircraftImageFetch) return '';
99
		$registration = filter_var($registration,FILTER_SANITIZE_STRING);
100
		$registration = trim($registration);
101
		//getting the aircraft image
102
		if ($globalDebug && $registration != '') echo 'Try to find an aircraft image for '.$registration.'...';
103
		elseif ($globalDebug && $aircraft_icao != '') echo 'Try to find an aircraft image for '.$aircraft_icao.'...';
104
		elseif ($globalDebug && $airline_icao != '') echo 'Try to find an aircraft image for '.$airline_icao.'...';
105
		$image_url = $this->findAircraftImage($registration,$aircraft_icao,$airline_icao);
106
		if ($registration == '' && $aircraft_icao != '') $registration = $aircraft_icao.$airline_icao;
107
		if ($image_url['original'] != '') {
108
			if ($globalDebug) echo 'Found !'."\n";
109
			$query  = "INSERT INTO spotter_image (registration, image, image_thumbnail, image_copyright, image_source,image_source_website) VALUES (:registration,:image,:image_thumbnail,:copyright,:source,:source_website)";
110
			try {
111
				$sth = $this->db->prepare($query);
112
				$sth->execute(array(':registration' => $registration,':image' => $image_url['original'],':image_thumbnail' => $image_url['thumbnail'], ':copyright' => $image_url['copyright'],':source' => $image_url['source'],':source_website' => $image_url['source_website']));
113
			} catch(PDOException $e) {
114
				echo $e->getMessage()."\n";
115
				return "error";
116
			}
117
		} elseif ($globalDebug) echo "Not found :'(\n";
118
		return "success";
119
	}
120
121
	/**
122
	* Adds the images based on the marine name
123
	*
124
	* @return String either success or error
125
	*
126
	*/
127
	public function addMarineImage($mmsi,$imo = '',$name = '')
128
	{
129
		global $globalDebug,$globalMarineImageFetch;
130
		if (isset($globalMarineImageFetch) && !$globalMarineImageFetch) return '';
131
		$mmsi = filter_var($mmsi,FILTER_SANITIZE_STRING);
132
		$imo = filter_var($imo,FILTER_SANITIZE_STRING);
133
		$name = filter_var($name,FILTER_SANITIZE_STRING);
134
		$name = trim($name);
135
		$Marine = new Marine($this->db);
136
		if ($imo == '' || $name == '') {
137
			$identity = $Marine->getIdentity($mmsi);
138
			if (isset($identity[0]['mmsi'])) {
139
				$imo = $identity[0]['imo'];
140
				if ($identity[0]['ship_name'] != '') $name = $identity[0]['ship_name'];
141
			}
142
		}
143
		unset($Marine);
144
145
		//getting the aircraft image
146
		if ($globalDebug && $name != '') echo 'Try to find an vessel image for '.$name.'...';
147
		$image_url = $this->findMarineImage($mmsi,$imo,$name);
148
		if ($image_url['original'] != '') {
149
			if ($globalDebug) echo 'Found !'."\n";
150
			$query  = "INSERT INTO marine_image (mmsi,imo,name, image, image_thumbnail, image_copyright, image_source,image_source_website) VALUES (:mmsi,:imo,:name,:image,:image_thumbnail,:copyright,:source,:source_website)";
151
			try {
152
				$sth = $this->db->prepare($query);
153
				$sth->execute(array(':mmsi' => $mmsi,':imo' => $imo,':name' => $name,':image' => $image_url['original'],':image_thumbnail' => $image_url['thumbnail'], ':copyright' => $image_url['copyright'],':source' => $image_url['source'],':source_website' => $image_url['source_website']));
154
			} catch(PDOException $e) {
155
				echo $e->getMessage()."\n";
156
				return "error";
157
			}
158
		} elseif ($globalDebug) echo "Not found :'(\n";
159
		return "success";
160
	}
161
162
	/**
163
	* Gets the aircraft image
164
	*
165
	* @param String $aircraft_registration the registration of the aircraft
166
	* @return Array the aircraft thumbnail, orignal url and copyright
167
	*
168
	*/
169
	public function findAircraftImage($aircraft_registration, $aircraft_icao = '', $airline_icao = '')
170
	{
171
		global $globalAircraftImageSources, $globalIVAO, $globalAircraftImageCheckICAO;
172
		$Spotter = new Spotter($this->db);
173
		if (!isset($globalIVAO)) $globalIVAO = FALSE;
174
		$aircraft_registration = filter_var($aircraft_registration,FILTER_SANITIZE_STRING);
175
		if ($aircraft_registration != '') {
176
			if (strpos($aircraft_registration,'/') !== false) return array('thumbnail' => '','original' => '', 'copyright' => '','source' => '','source_website' => '');
177
			$aircraft_registration = urlencode(trim($aircraft_registration));
178
			$aircraft_info = $Spotter->getAircraftInfoByRegistration($aircraft_registration);
179
			if (isset($aircraft_info[0]['aircraft_name'])) $aircraft_name = $aircraft_info[0]['aircraft_name'];
180
			else $aircraft_name = '';
181
			if (isset($aircraft_info[0]['aircraft_icao'])) $aircraft_name = $aircraft_info[0]['aircraft_icao'];
182
			else $aircraft_icao = '';
183
			if (isset($aircraft_info[0]['airline_icao'])) $airline_icao = $aircraft_info[0]['airline_icao'];
184
			else $airline_icao = '';
185
		} elseif ($aircraft_icao != '') {
186
			$aircraft_info = $Spotter->getAllAircraftInfo($aircraft_icao);
187
			if (isset($aircraft_info[0]['type'])) $aircraft_name = $aircraft_info[0]['type'];
188
			else $aircraft_name = '';
189
			$aircraft_registration = $aircraft_icao;
190
		} else return array('thumbnail' => '','original' => '', 'copyright' => '', 'source' => '','source_website' => '');
191
		unset($Spotter);
192
		if (!isset($globalAircraftImageSources)) $globalAircraftImageSources = array('ivaomtl','wikimedia','airportdata','deviantart','flickr','bing','jetphotos','planepictures','planespotters');
193
		foreach ($globalAircraftImageSources as $source) {
194
			$source = strtolower($source);
195
			if ($source == 'ivaomtl' && $globalIVAO && $aircraft_icao != '' && $airline_icao != '') $images_array = $this->fromIvaoMtl('aircraft',$aircraft_icao,$airline_icao);
196
			if ($source == 'planespotters' && !$globalIVAO) $images_array = $this->fromPlanespotters('aircraft',$aircraft_registration,$aircraft_name);
197
			if ($source == 'flickr') $images_array = $this->fromFlickr('aircraft',$aircraft_registration,$aircraft_name);
198
			if ($source == 'bing') $images_array = $this->fromBing('aircraft',$aircraft_registration,$aircraft_name);
199
			if ($source == 'deviantart') $images_array = $this->fromDeviantart('aircraft',$aircraft_registration,$aircraft_name);
200
			if ($source == 'wikimedia') $images_array = $this->fromWikimedia('aircraft',$aircraft_registration,$aircraft_name);
201
			if ($source == 'jetphotos' && !$globalIVAO) $images_array = $this->fromJetPhotos('aircraft',$aircraft_registration,$aircraft_name);
202
			if ($source == 'planepictures' && !$globalIVAO) $images_array = $this->fromPlanePictures('aircraft',$aircraft_registration,$aircraft_name);
203
			if ($source == 'airportdata' && !$globalIVAO) $images_array = $this->fromAirportData('aircraft',$aircraft_registration,$aircraft_name);
204
			if ($source == 'customsources') $images_array = $this->fromCustomSource('aircraft',$aircraft_registration,$aircraft_name);
205
			if (isset($images_array) && $images_array['original'] != '') return $images_array;
206
		}
207
		if ((!isset($globalAircraftImageCheckICAO) || $globalAircraftImageCheckICAO === TRUE) && isset($aircraft_icao)) return $this->findAircraftImage($aircraft_icao);
208
		return array('thumbnail' => '','original' => '', 'copyright' => '','source' => '','source_website' => '');
209
	}
210
211
	/**
212
	* Gets the vessel image
213
	*
214
	* @param String $mmsi the vessel mmsi
215
	* @param String $imo the vessel imo
216
	* @param String $name the vessel name
217
	* @return Array the aircraft thumbnail, orignal url and copyright
218
	*
219
	*/
220
	public function findMarineImage($mmsi,$imo = '',$name = '')
221
	{
222
		global $globalMarineSources;
223
		$mmsi = filter_var($mmsi,FILTER_SANITIZE_STRING);
224
		$imo = filter_var($imo,FILTER_SANITIZE_STRING);
0 ignored issues
show
Unused Code introduced by
$imo is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
225
		$name = filter_var($name,FILTER_SANITIZE_STRING);
226
		$name = trim($name);
227
		if (strlen($name) < 4) return array('thumbnail' => '','original' => '', 'copyright' => '', 'source' => '','source_website' => '');
228
		/*
229
		$Marine = new Marine($this->db);
230
		if ($imo == '' || $name == '') {
231
			$identity = $Marine->getIdentity($mmsi);
232
			if (isset($identity[0]['mmsi'])) {
233
				$imo = $identity[0]['imo'];
234
				$name = $identity[0]['ship_name'];
235
			}
236
		}
237
		unset($Marine);
238
		*/
239
		if (!isset($globalMarineImageSources)) $globalMarineImageSources = array('wikimedia','deviantart','flickr','bing');
0 ignored issues
show
Bug introduced by
The variable $globalMarineImageSources seems only to be defined at a later point. As such the call to isset() seems to always evaluate to false.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
240
		foreach ($globalMarineImageSources as $source) {
241
			$source = strtolower($source);
242
			if ($source == 'flickr') $images_array = $this->fromFlickr('marine',$mmsi,$name);
243
			if ($source == 'bing') $images_array = $this->fromBing('marine',$mmsi,$name);
244
			if ($source == 'deviantart') $images_array = $this->fromDeviantart('marine',$mmsi,$name);
245
			if ($source == 'wikimedia') $images_array = $this->fromWikimedia('marine',$mmsi,$name);
246
			if ($source == 'customsources') $images_array = $this->fromCustomSource('marine',$mmsi,$name);
247
			if (isset($images_array) && $images_array['original'] != '') return $images_array;
248
		}
249
		return array('thumbnail' => '','original' => '', 'copyright' => '','source' => '','source_website' => '');
250
	}
251
252
	/**
253
	* Gets the aircraft image from Planespotters
254
	*
255
	* @param String $aircraft_registration the registration of the aircraft
256
	* @param String $aircraft_name type of the aircraft
257
	* @return Array the aircraft thumbnail, orignal url and copyright
258
	*
259
	*/
260
	public function fromPlanespotters($type,$aircraft_registration, $aircraft_name='') {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
261
		$Common = new Common();
262
		// If aircraft registration is only number, also check with aircraft model
263
		if (preg_match('/^[[:digit]]+$/',$aircraft_registration) && $aircraft_name != '') {
264
			$url= 'http://www.planespotters.net/Aviation_Photos/search.php?tag='.$aircraft_registration.'&actype=s_'.urlencode($aircraft_name).'&output=rss';
265
		} else {
266
			//$url= 'http://www.planespotters.net/Aviation_Photos/search.php?tag='.$airline_aircraft_type.'&output=rss';
267
			$url= 'http://www.planespotters.net/Aviation_Photos/search.php?reg='.$aircraft_registration.'&output=rss';
268
		}
269
		$data = $Common->getData($url);
270
		if ($xml = simplexml_load_string($data)) {
271
			if (isset($xml->channel->item)) {
272
				$image_url = array();
273
				$thumbnail_url = trim((string)$xml->channel->item->children('http://search.yahoo.com/mrss/')->thumbnail->attributes()->url);
274
				$image_url['thumbnail'] = $thumbnail_url;
275
				$image_url['original'] = str_replace('thumbnail','original',$thumbnail_url);
276
				$image_url['copyright'] = trim((string)$xml->channel->item->children('http://search.yahoo.com/mrss/')->copyright);
277
				$image_url['source_website'] = trim((string)$xml->channel->item->link);
278
				$image_url['source'] = 'planespotters';
279
				return $image_url;
280
			}
281
		} 
282
		return false;
283
	}
284
285
	/**
286
	* Gets the aircraft image from Deviantart
287
	*
288
	* @param String $registration the registration of the aircraft
289
	* @param String $name type of the aircraft
290
	* @return Array the aircraft thumbnail, orignal url and copyright
291
	*
292
	*/
293
	public function fromDeviantart($type,$registration, $name='') {
294
		$Common = new Common();
295
		if ($type == 'aircraft') {
296
			// If aircraft registration is only number, also check with aircraft model
297
			if (preg_match('/^[[:digit]]+$/',$registration) && $name != '') {
298
				$url= 'http://backend.deviantart.com/rss.xml?type=deviation&q='.$registration.'%20'.urlencode($name);
299
			} else {
300
				$url= 'http://backend.deviantart.com/rss.xml?type=deviation&q=aircraft%20'.$registration;
301
			}
302
		} elseif ($type == 'marine') {
303
			$url= 'http://backend.deviantart.com/rss.xml?type=deviation&q="'.urlencode($name).'"';
304
		}
305
306
		$data = $Common->getData($url);
0 ignored issues
show
Bug introduced by
The variable $url does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
307
		if ($xml = simplexml_load_string($data)) {
308
			if (isset($xml->channel->item->link)) {
309
				$image_url = array();
310
				$thumbnail_url = trim((string)$xml->channel->item->children('http://search.yahoo.com/mrss/')->thumbnail->attributes()->url);
311
				$image_url['thumbnail'] = $thumbnail_url;
312
				$original_url = trim((string)$xml->channel->item->children('http://search.yahoo.com/mrss/')->content->attributes()->url);
313
				$image_url['original'] = $original_url;
314
				$image_url['copyright'] = str_replace('Copyright ','',trim((string)$xml->channel->item->children('http://search.yahoo.com/mrss/')->copyright));
315
				$image_url['source_website'] = trim((string)$xml->channel->item->link);
316
				$image_url['source'] = 'deviantart';
317
				return $image_url;
318
			}
319
		} 
320
		return false;
321
	}
322
323
	/**
324
	* Gets the aircraft image from JetPhotos
325
	*
326
	* @param String $aircraft_registration the registration of the aircraft
327
	* @param String $aircraft_name type of the aircraft
328
	* @return Array the aircraft thumbnail, orignal url and copyright
329
	*
330
	*/
331
	public function fromJetPhotos($type,$aircraft_registration, $aircraft_name='') {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $aircraft_name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
332
		$Common = new Common();
333
		$url= 'http://jetphotos.net/showphotos.php?displaymode=2&regsearch='.$aircraft_registration;
334
		$data = $Common->getData($url);
335
		$dom = new DOMDocument();
336
		@$dom->loadHTML($data);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
337
		$all_pics = array();
338
		foreach($dom->getElementsByTagName('img') as $image) {
339
			if ($image->getAttribute('itemprop') == "http://schema.org/image") {
340
				$all_pics[] = $image->getAttribute('src');
341
			}
342
		}
343
		$all_authors = array();
344
		foreach($dom->getElementsByTagName('meta') as $author) {
345
			if ($author->getAttribute('itemprop') == "http://schema.org/author") {
346
				$all_authors[] = $author->getAttribute('content');
347
			}
348
		}
349
		$all_ref = array();
350
		foreach($dom->getElementsByTagName('a') as $link) {
351
			$all_ref[] = $link->getAttribute('href');
352
		}
353
		if (isset($all_pics[0])) {
354
			$image_url = array();
355
			$image_url['thumbnail'] = $all_pics[0];
356
			$image_url['original'] = str_replace('_tb','',$all_pics[0]);
357
			$image_url['copyright'] = $all_authors[0];
358
			$image_url['source_website'] = 'http://jetphotos.net'.$all_ref[8];
359
			$image_url['source'] = 'JetPhotos';
360
			return $image_url;
361
		}
362
		return false;
363
	}
364
365
	/**
366
	* Gets the aircraft image from PlanePictures
367
	*
368
	* @param String $aircraft_registration the registration of the aircraft
369
	* @param String $aircraft_name type of the aircraft
370
	* @return Array the aircraft thumbnail, orignal url and copyright
371
	*
372
	*/
373
	public function fromPlanePictures($type,$aircraft_registration, $aircraft_name='') {
0 ignored issues
show
Unused Code introduced by
The parameter $aircraft_name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
374
		$Common = new Common();
375
		$url= 'http://www.planepictures.net/netsearch4.cgi?srch='.$aircraft_registration.'&stype=reg&srng=2';
376
		$data = $Common->getData($url);
377
		$dom = new DOMDocument();
378
		@$dom->loadHTML($data);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
379
		$all_pics = array();
380
		foreach($dom->getElementsByTagName('img') as $image) {
381
			$all_pics[] = $image->getAttribute('src');
382
		}
383
		$all_links = array();
384
		foreach($dom->getElementsByTagName('a') as $link) {
385
			$all_links[] = array('text' => $link->textContent,'href' => $link->getAttribute('href'));
386
		}
387
		if (isset($all_pics[1]) && !preg_match('/bit.ly/',$all_pics[1]) && !preg_match('/flagge/',$all_pics[1])) {
388
			$image_url = array();
389
			$image_url['thumbnail'] = 'http://www.planepictures.net/'.$all_pics[1];
390
			$image_url['original'] = 'http://www.planepictures.net/'.str_replace('_TN','',$all_pics[1]);
391
			$image_url['copyright'] = $all_links[6]['text'];
392
			$image_url['source_website'] = 'http://www.planepictures.net/'.$all_links[2]['href'];
393
			$image_url['source'] = 'PlanePictures';
394
			return $image_url;
395
		}
396
		return false;
397
	}
398
399
	/**
400
	* Gets the aircraft image from Flickr
401
	*
402
	* @param String $registration the registration of the aircraft
403
	* @param String $name type of the aircraft
404
	* @return Array the aircraft thumbnail, orignal url and copyright
405
	*
406
	*/
407
	public function fromFlickr($type,$registration,$name='') {
408
		$Common = new Common();
409
		if ($type == 'aircraft') {
410
			if ($name != '') $url = 'https://api.flickr.com/services/feeds/photos_public.gne?format=rss2&license=1,2,3,4,5,6,7&per_page=1&tags='.$registration.','.urlencode($name);
411
			else $url = 'https://api.flickr.com/services/feeds/photos_public.gne?format=rss2&license=1,2,3,4,5,6,7&per_page=1&tags='.$registration.',aircraft';
412
		} elseif ($type == 'marine') {
413
			if ($name != '') $url = 'https://api.flickr.com/services/feeds/photos_public.gne?format=rss2&license=1,2,3,4,5,6,7&per_page=1&tags='.urlencode($name);
414
			else $url = 'https://api.flickr.com/services/feeds/photos_public.gne?format=rss2&license=1,2,3,4,5,6,7&per_page=1&tags='.$registration.',vessel';
415
		}
416
		$data = $Common->getData($url);
0 ignored issues
show
Bug introduced by
The variable $url does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
417
		if ($xml = simplexml_load_string($data)) {
418
			if (isset($xml->channel->item)) {
419
				$original_url = trim((string)$xml->channel->item->enclosure->attributes()->url);
420
				$image_url = array();
421
				$image_url['thumbnail'] = $original_url;
422
				$image_url['original'] = $original_url;
423
				$image_url['copyright'] = trim((string)$xml->channel->item->children('http://search.yahoo.com/mrss/')->credit);
424
				$image_url['source_website'] = trim((string)$xml->channel->item->link);
425
				$image_url['source'] = 'flickr';
426
				return $image_url;
427
			}
428
		} 
429
		return false;
430
	}
431
432
	public function fromIvaoMtl($type,$aircraft_icao,$airline_icao) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
433
		$Common = new Common();
434
		//echo "\n".'SEARCH IMAGE : http://mtlcatalog.ivao.aero/images/aircraft/'.$aircraft_icao.$airline_icao.'.jpg';
435
		if ($Common->urlexist('http://mtlcatalog.ivao.aero/images/aircraft/'.$aircraft_icao.$airline_icao.'.jpg')) {
436
			$image_url = array();
437
			$image_url['thumbnail'] = 'http://mtlcatalog.ivao.aero/images/aircraft/'.$aircraft_icao.$airline_icao.'.jpg';
438
			$image_url['original'] = 'http://mtlcatalog.ivao.aero/images/aircraft/'.$aircraft_icao.$airline_icao.'.jpg';
439
			$image_url['copyright'] = 'IVAO';
440
			$image_url['source_website'] = 'http://mtlcatalog.ivao.aero/';
441
			$image_url['source'] = 'ivao.aero';
442
			return $image_url;
443
		} else {
444
			return false;
445
		}
446
	}
447
448
	/**
449
	* Gets the aircraft image from Bing
450
	*
451
	* @param String $aircraft_registration the registration of the aircraft
452
	* @param String $aircraft_name type of the aircraft
453
	* @return Array the aircraft thumbnail, orignal url and copyright
454
	*
455
	*/
456
	public function fromBing($type,$aircraft_registration,$aircraft_name='') {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
457
		global $globalImageBingKey;
458
		$Common = new Common();
459
		if (!isset($globalImageBingKey) || $globalImageBingKey == '') return false;
460
		if ($aircraft_name != '') $url = 'https://api.datamarket.azure.com/Bing/Search/v1/Image?$format=json&$top=1&Query=%27'.$aircraft_registration.'%20'.urlencode($aircraft_name).'%20-site:planespotters.com%20-site:flickr.com%27';
461
		else $url = 'https://api.datamarket.azure.com/Bing/Search/v1/Image?$format=json&$top=1&Query=%27%2B'.$aircraft_registration.'%20%2Baircraft%20-site:planespotters.com%20-site:flickr.com%27';
462
		$headers = array("Authorization: Basic " . base64_encode("ignored:".$globalImageBingKey));
463
		$data = $Common->getData($url,'get','',$headers);
464
		$result = json_decode($data);
465
		if (isset($result->d->results[0]->MediaUrl)) {
466
			$image_url = array();
467
			$image_url['original'] = $result->d->results[0]->MediaUrl;
468
			$image_url['source_website'] = $result->d->results[0]->SourceUrl;
469
			// Thumbnail can't be used this way...
470
			// $image_url['thumbnail'] = $result->d->results[0]->Thumbnail->MediaUrl;
471
			$image_url['thumbnail'] = $result->d->results[0]->MediaUrl;
472
			$url = parse_url($image_url['source_website']);
473
			$image_url['copyright'] = $url['host'];
474
			$image_url['source'] = 'bing';
475
			return $image_url;
476
		}
477
		return false;
478
	}
479
480
	/**
481
	* Gets the aircraft image from airport-data
482
	*
483
	* @param String $aircraft_registration the registration of the aircraft
484
	* @param String $aircraft_name type of the aircraft
485
	* @return Array the aircraft thumbnail, orignal url and copyright
486
	*
487
	*/
488
	public function fromAirportData($type,$aircraft_registration,$aircraft_name='') {
0 ignored issues
show
Unused Code introduced by
The parameter $aircraft_name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
489
		$Common = new Common();
490
		$url = 'http://www.airport-data.com/api/ac_thumb.json?&n=1&r='.$aircraft_registration;
491
		$data = $Common->getData($url);
492
		$result = json_decode($data);
493
		if (isset($result->count) && $result->count > 0) {
494
			$image_url = array();
495
			$image_url['original'] = str_replace('thumbnails','large',$result->data[0]->image);
496
			$image_url['source_website'] = $result->data[0]->link;
497
			$image_url['thumbnail'] = $result->data[0]->image;
498
			$image_url['copyright'] = $result->data[0]->photographer;
499
			$image_url['source'] = 'AirportData';
500
			return $image_url;
501
		}
502
		return false;
503
	}
504
505
	/**
506
	* Gets image from WikiMedia
507
	*
508
	* @param String $registration the registration of the aircraft/mmsi
509
	* @param String $name name
510
	* @return Array the aircraft thumbnail, orignal url and copyright
511
	*
512
	*/
513
	public function fromWikimedia($type,$registration,$name='') {
514
		$Common = new Common();
515
		if ($type == 'aircraft') {
516
			if ($name != '') $url = 'https://commons.wikimedia.org/w/api.php?action=query&list=search&format=json&srlimit=1&srnamespace=6&continue&srsearch="'.$registration.'"%20'.urlencode($name);
517
			else $url = 'https://commons.wikimedia.org/w/api.php?action=query&list=search&format=json&srlimit=1&srnamespace=6&continue&srsearch="'.$registration.'"%20aircraft';
518
		} elseif ($type == 'marine') {
519
			if ($name != '') $url = 'https://commons.wikimedia.org/w/api.php?action=query&list=search&format=json&srlimit=1&srnamespace=6&continue&srsearch="'.urlencode($name).'"';
520
			else return false;
521
		}
522
		$data = $Common->getData($url);
0 ignored issues
show
Bug introduced by
The variable $url does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
523
		$result = json_decode($data);
524
		if (isset($result->query->search[0]->title)) {
525
			$fileo = $result->query->search[0]->title;
526
			if (substr($fileo,-3) == 'pdf') return false;
527
			$file = urlencode($fileo);
528
			$url2 = 'https://commons.wikimedia.org/w/api.php?action=query&format=json&continue&iilimit=500&prop=imageinfo&iiprop=user|url|size|mime|sha1|timestamp&iiurlwidth=200%27&titles='.$file;
529
			$data2 = $Common->getData($url2);
530
			$result2 = json_decode($data2);
531
			if (isset($result2->query->pages)) {
532
				foreach ($result2->query->pages as $page) {
533
					if (isset($page->imageinfo[0]->user)) {
534
						$image_url = array();
535
						$image_url['copyright'] = 'Wikimedia, '.$page->imageinfo[0]->user;
536
						$image_url['original'] = $page->imageinfo[0]->url;
537
						$image_url['thumbnail'] = $page->imageinfo[0]->thumburl;
538
						$image_url['source'] = 'wikimedia';
539
						$image_url['source_website'] = 'http://commons.wikimedia.org/wiki/'.$fileo;
540
						//return $image_url;
541
					}
542
				}
543
			}
544
			if (isset($image_url['original'])) {
545
				$url2 = 'https://commons.wikimedia.org/w/api.php?action=query&prop=imageinfo&iiprop=extmetadata&format=json&continue&titles='.$file;
546
				$data2 = $Common->getData($url2);
547
				$result2 = json_decode($data2);
548
				if (isset($result2->query->pages)) {
549
					foreach ($result2->query->pages as $page) {
550
						if (isset($page->imageinfo[0]->extmetadata->Artist)) {
551
							$image_url['copyright'] = preg_replace('/ from(.*)/','',strip_tags($page->imageinfo[0]->extmetadata->Artist->value));
552
							if (isset($page->imageinfo[0]->extmetadata->License->value)) {
553
								$image_url['copyright'] = $image_url['copyright'].' (under '.$page->imageinfo[0]->extmetadata->License->value.')';
554
							}
555
							$image_url['copyright'] = trim(str_replace('\n','',$image_url['copyright']));
556
							return $image_url;
557
						}
558
					}
559
				}
560
				return $image_url;
561
			}
562
		}
563
		return false;
564
	}
565
566
	/**
567
	* Gets the aircraft image from custom url
568
	*
569
	* @param String $aircraft_registration the registration of the aircraft
570
	* @param String $aircraft_name type of the aircraft
571
	* @return Array the aircraft thumbnail, orignal url and copyright
572
	*
573
	*/
574
	public function fromCustomSource($type,$aircraft_registration,$aircraft_name='') {
0 ignored issues
show
Unused Code introduced by
The parameter $aircraft_name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
575
		global $globalAircraftImageCustomSources, $globalDebug;
576
		//$globalAircraftImageCustomSource[] = array('thumbnail' => '','original' => '', 'copyright' => '', 'source_website' => '', 'source' => '','exif' => true);
577
		if (!empty($globalAircraftImageCustomSources)) {
578
			$customsources = array();
579
			if (!isset($globalAircraftImageCustomSources[0])) {
580
				$customsources[] = $globalAircraftImageCustomSources;
581
			} else {
582
				$customsources = $globalAircraftImageCustomSources;
583
			}
584
			foreach ($customsources as $source) {
585
				$Common = new Common();
586
				if (!isset($source['original']) && $globalDebug) {
587
					echo 'original entry not found for $globalAircraftImageCustomSources.';
588
					print_r($source);
589
					print_r($customsources);
590
				}
591
				$url = str_replace('{registration}',$aircraft_registration,$source['original']);
592
				$url_thumbnail = str_replace('{registration}',$aircraft_registration,$source['thumbnail']);
593
				if ($Common->urlexist($url)) {
594
					$image_url = array();
595
					$image_url['thumbnail'] = $url_thumbnail;
596
					$image_url['original'] = $url;
597
					if ($source['exif'] && exif_imagetype($url) == IMAGETYPE_JPEG) $exifCopyright = $this->getExifCopyright($url);
598
					else $exifCopyright = '';
599
					if ($exifCopyright  != '') $image_url['copyright'] = $exifCopyright;
600
					elseif (isset($source['copyright'])) $image_url['copyright'] = $source['copyright'];
601
					else $image_url['copyright'] = $source['source_website'];
602
					$image_url['source_website'] = $source['source_website'];
603
					$image_url['source'] = $source['source'];
604
					return $image_url;
605
				}
606
			}
607
			return false;
608
		} else return false;
609
	}
610
}
611
612
//$Image = new Image();
613
//print_r($Image->fromAirportData('F-GZHM'));
614
615
?>