Completed
Push — master ( b6fed2...ffb22c )
by Yannick
07:47
created

Common::getHeading()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 4
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
require_once(dirname(__FILE__).'/libs/simple_html_dom.php');
3
require_once(dirname(__FILE__).'/libs/uagent/uagent.php');
4
5
class Common {
6
	protected $cookies = array();
7
	
8
	/**
9
	* Get data from form result
10
	* @param String $url form URL
11
	* @param String $type type of submit form method (get or post)
12
	* @param String or Array $data values form post method
13
	* @param Array $headers header to submit with the form
14
	* @return String the result
15
	*/
16
	public function getData($url, $type = 'get', $data = '', $headers = '',$cookie = '',$referer = '',$timeout = '',$useragent = '') {
17
		$ch = curl_init();
18
		curl_setopt($ch, CURLOPT_URL, $url);
19
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
20
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
21
		curl_setopt($ch, CURLINFO_HEADER_OUT, true); 
22
		curl_setopt($ch,CURLOPT_ENCODING , "gzip");
23
		//curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5');
24
//		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64; rv:42.0) Gecko/20100101 Firefox/42.0');
25
		if ($useragent == '') {
26
			curl_setopt($ch, CURLOPT_USERAGENT, UAgent::random());
27
		} else {
28
			curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
29
		}
30
		if ($timeout == '') curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
31
		else curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); 
32
		curl_setopt($ch, CURLOPT_HEADERFUNCTION, array('Common',"curlResponseHeaderCallback"));
33
		if ($type == 'post') {
34
			curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
35
			if (is_array($data)) {
36
				curl_setopt($ch, CURLOPT_POST, count($data));
37
				$data_string = '';
38
				foreach($data as $key=>$value) { $data_string .= $key.'='.$value.'&'; }
39
				rtrim($data_string, '&');
40
				curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
41
			} else {
42
				curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
43
			}
44
		}
45
		if ($headers != '') {
46
			curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
47
		}
48
		if ($cookie != '') {
49
			if (is_array($cookie)) {
50
				curl_setopt($ch, CURLOPT_COOKIE, implode($cookie,';'));
51
			} else {
52
				curl_setopt($ch, CURLOPT_COOKIE, $cookie);
53
			}
54
		}
55
		if ($referer != '') {
56
			curl_setopt($ch, CURLOPT_REFERER, $referer);
57
		}
58
		$result = curl_exec($ch);
59
		$info = curl_getinfo($ch);
60
		curl_close($ch);
61
		if ($info['http_code'] == '503' && strstr($result,'DDoS protection by CloudFlare')) {
62
			echo "Cloudflare Detected\n";
63
			require_once(dirname(__FILE__).'/libs/cloudflare-bypass/libraries/cloudflareClass.php');
64
			$useragent = UAgent::random();
65
			cloudflare::useUserAgent($useragent);
66
			if ($clearanceCookie = cloudflare::bypass($url)) {
67
				return $this->getData($url,'get',$data,$headers,$clearanceCookie,$referer,$timeout,$useragent);
0 ignored issues
show
Bug introduced by
It seems like $data can also be of type array; however, Common::getData() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
68
			}
69
		} else {
70
		    return $result;
71
		}
72
	}
73
	
74
	private function curlResponseHeaderCallback($ch, $headerLine) {
75
		global $cookies;
76
		if (preg_match('/^Set-Cookie:\s*([^;]*)/mi', $headerLine, $cookie) == 1)
77
			$cookies[] = $cookie;
78
		return strlen($headerLine); // Needed by curl
79
	}
80
	
81
	/**
82
	* Convert a HTML table to an array
83
	* @param String $data HTML page
84
	* @return Array array of the tables in HTML page
85
	*/
86
	public function table2array($data) {
87
		if (!is_string($data)) return array();
88
		if ($data == '') return array();
89
		$html = str_get_html($data);
90
		if ($html === false) return array();
91
		$tabledata=array();
92 View Code Duplication
		foreach($html->find('tr') as $element)
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
		{
94
			$td = array();
95
			foreach( $element->find('th') as $row)
96
			{
97
				$td [] = trim($row->plaintext);
98
			}
99
			$td=array_filter($td);
100
			$tabledata[] = $td;
101
102
			$td = array();
103
			$tdi = array();
104
			foreach( $element->find('td') as $row)
105
			{
106
				$td [] = trim($row->plaintext);
107
				$tdi [] = trim($row->innertext);
108
			}
109
			$td=array_filter($td);
110
			$tdi=array_filter($tdi);
111
			$tabledata[]=array_merge($td,$tdi);
112
		}
113
		$html->clear();
114
		unset($html);
115
		return(array_filter($tabledata));
116
	}
117
	
118
	/**
119
	* Convert <p> part of a HTML page to an array
120
	* @param String $data HTML page
121
	* @return Array array of the <p> in HTML page
122
	*/
123
	public function text2array($data) {
124
		$html = str_get_html($data);
125
		if ($html === false) return array();
126
		$tabledata=array();
127
		foreach($html->find('p') as $element)
128
		{
129
			$tabledata [] = trim($element->plaintext);
130
		}
131
		$html->clear();
132
		unset($html);
133
		return(array_filter($tabledata));
134
	}
135
136
	/**
137
	* Give distance between 2 coordonnates
138
	* @param Float $lat latitude of first point
139
	* @param Float $lon longitude of first point
140
	* @param Float $latc latitude of second point
141
	* @param Float $lonc longitude of second point
142
	* @param String $unit km else no unit used
143
	* @return Float Distance in $unit
144
	*/
145
	public function distance($lat, $lon, $latc, $lonc, $unit = 'km') {
146
		if ($lat == $latc && $lon == $lonc) return 0;
147
		$dist = rad2deg(acos(sin(deg2rad(floatval($lat)))*sin(deg2rad(floatval($latc)))+ cos(deg2rad(floatval($lat)))*cos(deg2rad(floatval($latc)))*cos(deg2rad(floatval($lon)-floatval($lonc)))))*60*1.1515;
148
		if ($unit == "km") {
149
			return round($dist * 1.609344);
150
		} elseif ($unit == "m") {
151
			return round($dist * 1.609344 * 1000);
152
		} elseif ($unit == "mile" || $unit == "mi") {
153
			return round($dist);
154
		} elseif ($unit == "nm") {
155
			return round($dist*0.868976);
156
		} else {
157
			return round($dist);
158
		}
159
	}
160
161
	/**
162
	* Check is distance realistic
163
	* @param int $timeDifference the time between the reception of both messages
164
	* @param float $distance distance covered
165
	* @return whether distance is realistic
166
	*/
167
	public function withinThreshold ($timeDifference, $distance) {
168
		$x = abs($timeDifference);
169
		$d = abs($distance);
170
		if ($x == 0 || $d == 0) return true;
171
		// may be due to Internet jitter; distance is realistic
172
		if ($x < 0.7 && $d < 2000) return true;
173
		else return $d/$x < 1500*0.27778; // 1500 km/h max
174
	}
175
176
177
	// Check if an array is assoc
178
	public function isAssoc($array)
179
	{
180
		return ($array !== array_values($array));
181
	}
182
183
	public function isInteger($input){
184
	    return(ctype_digit(strval($input)));
185
	}
186
187
188
	public function convertDec($dms,$latlong) {
189
		if ($latlong == 'latitude') {
190
			$deg = substr($dms, 0, 2);
191
			$min = substr($dms, 2, 4);
192
		} else {
193
			$deg = substr($dms, 0, 3);
194
			$min = substr($dms, 3, 5);
195
		}
196
		return $deg+(($min*60)/3600);
197
	}
198
	
199
	/**
200
	* Copy folder contents
201
	* @param       string   $source    Source path
202
	* @param       string   $dest      Destination path
203
	* @return      bool     Returns true on success, false on failure
204
	*/
205
	public function xcopy($source, $dest, $permissions = 0755)
0 ignored issues
show
Unused Code introduced by
The parameter $permissions 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...
206
	{
207
		$files = glob($source.'*.*');
208
		foreach($files as $file){
209
			$file_to_go = str_replace($source,$dest,$file);
210
			copy($file, $file_to_go);
211
		}
212
		return true;
213
	}
214
	
215
	/**
216
	* Check if an url exist
217
	* @param	String $url url to check
218
	* @return	bool Return true on succes false on failure
219
	*/
220
	public function urlexist($url){
221
		$headers=get_headers($url);
222
		return stripos($headers[0],"200 OK")?true:false;
223
	}
224
	
225
	/**
226
	* Convert hexa to string
227
	* @param	String $hex data in hexa
228
	* @return	String Return result
229
	*/
230
	public function hex2str($hex) {
231
		$str = '';
232
		$hexln = strlen($hex);
233
		for($i=0;$i<$hexln;$i+=2) $str .= chr(hexdec(substr($hex,$i,2)));
234
		return $str;
235
	}
236
	
237
	
238
	public function getHeading($lat1, $lon1, $lat2, $lon2) {
239
		//difference in longitudinal coordinates
240
		$dLon = deg2rad($lon2) - deg2rad($lon1);
241
		//difference in the phi of latitudinal coordinates
242
		$dPhi = log(tan(deg2rad($lat2) / 2 + pi() / 4) / tan(deg2rad($lat1) / 2 + pi() / 4));
243
		//we need to recalculate $dLon if it is greater than pi
244
		if(abs($dLon) > pi()) {
245
			if($dLon > 0) {
246
				$dLon = (2 * pi() - $dLon) * -1;
247
			} else {
248
				$dLon = 2 * pi() + $dLon;
249
			}
250
		}
251
		//return the angle, normalized
252
		return (rad2deg(atan2($dLon, $dPhi)) + 360) % 360;
253
	}
254
	
255
	public function checkLine($lat1,$lon1,$lat2,$lon2,$lat3,$lon3,$approx = 0.1) {
256
		$a = ($lon2-$lon1)*$lat3+($lat2-$lat1)*$lon3+($lat1*$lon2+$lat2*$lon1);
0 ignored issues
show
Unused Code introduced by
$a 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...
257
		$a = -($lon2-$lon1);
258
		$b = $lat2 - $lat1;
259
		$c = -($a*$lat1+$b*$lon1);
260
		$d = $a*$lat3+$b*$lon3+$c;
261
		if ($d > -$approx && $d < $approx) return true;
262
		else return false;
263
	}
264
	
265
	public function array_merge_noappend() {
266
		$output = array();
267
		foreach(func_get_args() as $array) {
268
			foreach($array as $key => $value) {
269
				$output[$key] = isset($output[$key]) ?
270
				array_merge($output[$key], $value) : $value;
271
			}
272
		}
273
		return $output;
274
	}
275
	
276
	/**
277
	* Returns list of available locales
278
	*
279
	* @return array
280
	 */
281 View Code Duplication
	public function listLocaleDir()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
282
	{
283
		$result = array('en');
284
		if (!is_dir('./locale')) {
285
			return $result;
286
		}
287
		$handle = @opendir('./locale');
288
		if ($handle === false) return $result;
289
		while (false !== ($file = readdir($handle))) {
290
			$path = './locale'.'/'.$file.'/LC_MESSAGES/fam.mo';
291
			if ($file != "." && $file != ".." && @file_exists($path)) {
292
				$result[] = $file;
293
			}
294
		}
295
		closedir($handle);
296
		return $result;
297
	}
298
299
	function nextcoord($latitude, $longitude, $speed, $heading, $archivespeed = 1){
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
300
		global $globalMapRefresh;
301
		$distance = ($speed*0.514444*$globalMapRefresh*$archivespeed)/1000;
302
		$r = 6378;
303
		$latitude = deg2rad($latitude);
304
		$longitude = deg2rad($longitude);
305
		$bearing = deg2rad($heading); 
306
		$latitude2 =  asin( (sin($latitude) * cos($distance/$r)) + (cos($latitude) * sin($distance/$r) * cos($bearing)) );
307
		$longitude2 = $longitude + atan2( sin($bearing)*sin($distance/$r)*cos($latitude), cos($distance/$r)-(sin($latitude)*sin($latitude2)) );
308
		return array('latitude' => number_format(rad2deg($latitude2),5,'.',''),'longitude' => number_format(rad2deg($longitude2),5,'.',''));
309
	}
310
	
311
	function getCoordfromDistanceBearing($latitude,$longitude,$bearing,$distance) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
312
		// distance in meter
313
		$R = 6378.14;
314
		$latitude1 = $latitude * (M_PI/180);
315
		$longitude1 = $longitude * (M_PI/180);
316
		$brng = $bearing * (M_PI/180);
317
		$d = $distance;
318
319
		$latitude2 = asin(sin($latitude1)*cos($d/$R) + cos($latitude1)*sin($d/$R)*cos($brng));
320
		$longitude2 = $longitude1 + atan2(sin($brng)*sin($d/$R)*cos($latitude1),cos($d/$R)-sin($latitude1)*sin($latitude2));
321
322
		$latitude2 = $latitude2 * (180/M_PI);
323
		$longitude2 = $longitude2 * (180/M_PI);
324
325
		$flat = round ($latitude2,6);
326
		$flong = round ($longitude2,6);
327
/*
328
		$dx = $distance*cos($bearing);
329
		$dy = $distance*sin($bearing);
330
		$dlong = $dx/(111320*cos($latitude));
331
		$dlat = $dy/110540;
332
		$flong = $longitude + $dlong;
333
		$flat = $latitude + $dlat;
334
*/
335
		return array('latitude' => $flat,'longitude' => $flong);
336
	}
337
}
338
?>
1 ignored issue
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...