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|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); |
68
|
|
|
} |
69
|
|
|
} else { |
70
|
|
|
return $result; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private function curlResponseHeaderCallback($ch, $headerLine) { |
75
|
|
|
//global $cookies; |
76
|
|
|
$cookies = array(); |
77
|
|
|
if (preg_match('/^Set-Cookie:\s*([^;]*)/mi', $headerLine, $cookie) == 1) |
78
|
|
|
$cookies[] = $cookie; |
79
|
|
|
return strlen($headerLine); // Needed by curl |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public static function download($url, $file, $referer = '') { |
83
|
|
|
$fp = fopen($file, 'w+'); |
84
|
|
|
$ch = curl_init(); |
85
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
86
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
87
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
88
|
|
|
if ($referer != '') curl_setopt($ch, CURLOPT_REFERER, $referer); |
89
|
|
|
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'); |
90
|
|
|
curl_setopt($ch, CURLOPT_FILE, $fp); |
91
|
|
|
curl_exec($ch); |
92
|
|
|
curl_close($ch); |
93
|
|
|
fclose($fp); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Convert a HTML table to an array |
98
|
|
|
* @param String $data HTML page |
99
|
|
|
* @return Array array of the tables in HTML page |
100
|
|
|
*/ |
101
|
|
|
public function table2array($data) { |
102
|
|
|
if (!is_string($data)) return array(); |
103
|
|
|
if ($data == '') return array(); |
104
|
|
|
$html = str_get_html($data); |
105
|
|
|
if ($html === false) return array(); |
106
|
|
|
$tabledata=array(); |
107
|
|
|
foreach($html->find('tr') as $element) |
108
|
|
|
{ |
109
|
|
|
$td = array(); |
110
|
|
|
foreach( $element->find('th') as $row) |
111
|
|
|
{ |
112
|
|
|
$td [] = trim($row->plaintext); |
113
|
|
|
} |
114
|
|
|
$td=array_filter($td); |
115
|
|
|
$tabledata[] = $td; |
116
|
|
|
|
117
|
|
|
$td = array(); |
118
|
|
|
$tdi = array(); |
119
|
|
|
foreach( $element->find('td') as $row) |
120
|
|
|
{ |
121
|
|
|
$td [] = trim($row->plaintext); |
122
|
|
|
$tdi [] = trim($row->innertext); |
123
|
|
|
} |
124
|
|
|
$td=array_filter($td); |
125
|
|
|
$tdi=array_filter($tdi); |
126
|
|
|
$tabledata[]=array_merge($td,$tdi); |
127
|
|
|
} |
128
|
|
|
$html->clear(); |
129
|
|
|
unset($html); |
130
|
|
|
return(array_filter($tabledata)); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Convert <p> part of a HTML page to an array |
135
|
|
|
* @param String $data HTML page |
136
|
|
|
* @return Array array of the <p> in HTML page |
137
|
|
|
*/ |
138
|
|
|
public function text2array($data) { |
139
|
|
|
$html = str_get_html($data); |
140
|
|
|
if ($html === false) return array(); |
141
|
|
|
$tabledata=array(); |
142
|
|
|
foreach($html->find('p') as $element) |
143
|
|
|
{ |
144
|
|
|
$tabledata [] = trim($element->plaintext); |
145
|
|
|
} |
146
|
|
|
$html->clear(); |
147
|
|
|
unset($html); |
148
|
|
|
return(array_filter($tabledata)); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Give distance between 2 coordonnates |
153
|
|
|
* @param Float $lat latitude of first point |
154
|
|
|
* @param Float $lon longitude of first point |
155
|
|
|
* @param Float $latc latitude of second point |
156
|
|
|
* @param Float $lonc longitude of second point |
157
|
|
|
* @param String $unit km else no unit used |
158
|
|
|
* @return Float Distance in $unit |
159
|
|
|
*/ |
160
|
|
|
public function distance($lat, $lon, $latc, $lonc, $unit = 'km') { |
161
|
|
|
if ($lat == $latc && $lon == $lonc) return 0; |
162
|
|
|
$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; |
163
|
|
|
if ($unit == "km") { |
164
|
|
|
return round($dist * 1.609344); |
165
|
|
|
} elseif ($unit == "m") { |
166
|
|
|
return round($dist * 1.609344 * 1000); |
167
|
|
|
} elseif ($unit == "mile" || $unit == "mi") { |
168
|
|
|
return round($dist); |
169
|
|
|
} elseif ($unit == "nm") { |
170
|
|
|
return round($dist*0.868976); |
171
|
|
|
} else { |
172
|
|
|
return round($dist); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Check is distance realistic |
178
|
|
|
* @param int $timeDifference the time between the reception of both messages |
179
|
|
|
* @param float $distance distance covered |
180
|
|
|
* @return whether distance is realistic |
181
|
|
|
*/ |
182
|
|
|
public function withinThreshold ($timeDifference, $distance) { |
183
|
|
|
$x = abs($timeDifference); |
184
|
|
|
$d = abs($distance); |
185
|
|
|
if ($x == 0 || $d == 0) return true; |
186
|
|
|
// may be due to Internet jitter; distance is realistic |
187
|
|
|
if ($x < 0.7 && $d < 2000) return true; |
188
|
|
|
else return $d/$x < 1500*0.27778; // 1500 km/h max |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
|
192
|
|
|
// Check if an array is assoc |
193
|
|
|
public function isAssoc($array) |
194
|
|
|
{ |
195
|
|
|
return ($array !== array_values($array)); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function isInteger($input){ |
199
|
|
|
return(ctype_digit(strval($input))); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
|
203
|
|
|
public function convertDec($dms,$latlong) { |
204
|
|
|
if ($latlong == 'latitude') { |
205
|
|
|
$deg = substr($dms, 0, 2); |
206
|
|
|
$min = substr($dms, 2, 4); |
207
|
|
|
} else { |
208
|
|
|
$deg = substr($dms, 0, 3); |
209
|
|
|
$min = substr($dms, 3, 5); |
210
|
|
|
} |
211
|
|
|
return $deg+(($min*60)/3600); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Copy folder contents |
216
|
|
|
* @param string $source Source path |
217
|
|
|
* @param string $dest Destination path |
218
|
|
|
* @return bool Returns true on success, false on failure |
219
|
|
|
*/ |
220
|
|
|
public function xcopy($source, $dest) |
221
|
|
|
{ |
222
|
|
|
$files = glob($source.'*.*'); |
223
|
|
|
foreach($files as $file){ |
224
|
|
|
$file_to_go = str_replace($source,$dest,$file); |
225
|
|
|
copy($file, $file_to_go); |
226
|
|
|
} |
227
|
|
|
return true; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Check if an url exist |
232
|
|
|
* @param String $url url to check |
233
|
|
|
* @return bool Return true on succes false on failure |
234
|
|
|
*/ |
235
|
|
|
public function urlexist($url){ |
236
|
|
|
$headers=get_headers($url); |
237
|
|
|
return stripos($headers[0],"200 OK")?true:false; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Convert hexa to string |
242
|
|
|
* @param String $hex data in hexa |
243
|
|
|
* @return String Return result |
244
|
|
|
*/ |
245
|
|
|
public function hex2str($hex) { |
246
|
|
|
$str = ''; |
247
|
|
|
$hexln = strlen($hex); |
248
|
|
|
for($i=0;$i<$hexln;$i+=2) $str .= chr(hexdec(substr($hex,$i,2))); |
249
|
|
|
return $str; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
|
253
|
|
|
public function getHeading($lat1, $lon1, $lat2, $lon2) { |
254
|
|
|
//difference in longitudinal coordinates |
255
|
|
|
$dLon = deg2rad($lon2) - deg2rad($lon1); |
256
|
|
|
//difference in the phi of latitudinal coordinates |
257
|
|
|
$dPhi = log(tan(deg2rad($lat2) / 2 + pi() / 4) / tan(deg2rad($lat1) / 2 + pi() / 4)); |
258
|
|
|
//we need to recalculate $dLon if it is greater than pi |
259
|
|
|
if(abs($dLon) > pi()) { |
260
|
|
|
if($dLon > 0) { |
261
|
|
|
$dLon = (2 * pi() - $dLon) * -1; |
262
|
|
|
} else { |
263
|
|
|
$dLon = 2 * pi() + $dLon; |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
//return the angle, normalized |
267
|
|
|
return (rad2deg(atan2($dLon, $dPhi)) + 360) % 360; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
public function checkLine($lat1,$lon1,$lat2,$lon2,$lat3,$lon3,$approx = 0.1) { |
271
|
|
|
//$a = ($lon2-$lon1)*$lat3+($lat2-$lat1)*$lon3+($lat1*$lon2+$lat2*$lon1); |
272
|
|
|
$a = -($lon2-$lon1); |
273
|
|
|
$b = $lat2 - $lat1; |
274
|
|
|
$c = -($a*$lat1+$b*$lon1); |
275
|
|
|
$d = $a*$lat3+$b*$lon3+$c; |
276
|
|
|
if ($d > -$approx && $d < $approx) return true; |
277
|
|
|
else return false; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
public function array_merge_noappend() { |
281
|
|
|
$output = array(); |
282
|
|
|
foreach(func_get_args() as $array) { |
283
|
|
|
foreach($array as $key => $value) { |
284
|
|
|
$output[$key] = isset($output[$key]) ? |
285
|
|
|
array_merge($output[$key], $value) : $value; |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
return $output; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
|
292
|
|
|
function arr_diff($arraya, $arrayb) { |
|
|
|
|
293
|
|
|
foreach ($arraya as $keya => $valuea) { |
294
|
|
|
if (in_array($valuea, $arrayb)) { |
295
|
|
|
unset($arraya[$keya]); |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
return $arraya; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Returns list of available locales |
303
|
|
|
* |
304
|
|
|
* @return array |
305
|
|
|
*/ |
306
|
|
|
public function listLocaleDir() |
307
|
|
|
{ |
308
|
|
|
$result = array('en'); |
309
|
|
|
if (!is_dir('./locale')) { |
310
|
|
|
return $result; |
311
|
|
|
} |
312
|
|
|
$handle = @opendir('./locale'); |
313
|
|
|
if ($handle === false) return $result; |
314
|
|
|
while (false !== ($file = readdir($handle))) { |
315
|
|
|
$path = './locale'.'/'.$file.'/LC_MESSAGES/fam.mo'; |
316
|
|
|
if ($file != "." && $file != ".." && @file_exists($path)) { |
317
|
|
|
$result[] = $file; |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
closedir($handle); |
321
|
|
|
return $result; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
public function nextcoord($latitude, $longitude, $speed, $heading, $archivespeed = 1){ |
325
|
|
|
global $globalMapRefresh; |
326
|
|
|
$distance = ($speed*0.514444*$globalMapRefresh*$archivespeed)/1000; |
327
|
|
|
$r = 6378; |
328
|
|
|
$latitude = deg2rad($latitude); |
329
|
|
|
$longitude = deg2rad($longitude); |
330
|
|
|
$bearing = deg2rad($heading); |
331
|
|
|
$latitude2 = asin( (sin($latitude) * cos($distance/$r)) + (cos($latitude) * sin($distance/$r) * cos($bearing)) ); |
332
|
|
|
$longitude2 = $longitude + atan2( sin($bearing)*sin($distance/$r)*cos($latitude), cos($distance/$r)-(sin($latitude)*sin($latitude2)) ); |
333
|
|
|
return array('latitude' => number_format(rad2deg($latitude2),5,'.',''),'longitude' => number_format(rad2deg($longitude2),5,'.','')); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
public function getCoordfromDistanceBearing($latitude,$longitude,$bearing,$distance) { |
337
|
|
|
// distance in meter |
338
|
|
|
$R = 6378.14; |
339
|
|
|
$latitude1 = $latitude * (M_PI/180); |
340
|
|
|
$longitude1 = $longitude * (M_PI/180); |
341
|
|
|
$brng = $bearing * (M_PI/180); |
342
|
|
|
$d = $distance; |
343
|
|
|
|
344
|
|
|
$latitude2 = asin(sin($latitude1)*cos($d/$R) + cos($latitude1)*sin($d/$R)*cos($brng)); |
345
|
|
|
$longitude2 = $longitude1 + atan2(sin($brng)*sin($d/$R)*cos($latitude1),cos($d/$R)-sin($latitude1)*sin($latitude2)); |
346
|
|
|
|
347
|
|
|
$latitude2 = $latitude2 * (180/M_PI); |
348
|
|
|
$longitude2 = $longitude2 * (180/M_PI); |
349
|
|
|
|
350
|
|
|
$flat = round ($latitude2,6); |
351
|
|
|
$flong = round ($longitude2,6); |
352
|
|
|
/* |
353
|
|
|
$dx = $distance*cos($bearing); |
354
|
|
|
$dy = $distance*sin($bearing); |
355
|
|
|
$dlong = $dx/(111320*cos($latitude)); |
356
|
|
|
$dlat = $dy/110540; |
357
|
|
|
$flong = $longitude + $dlong; |
358
|
|
|
$flat = $latitude + $dlat; |
359
|
|
|
*/ |
360
|
|
|
return array('latitude' => $flat,'longitude' => $flong); |
361
|
|
|
} |
362
|
|
|
} |
363
|
|
|
?> |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.