Completed
Push — master ( 2224b9...adb4a8 )
by Yannick
34:48 queued 06:58
created

Common::array_merge_noappend()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 0
dl 0
loc 10
rs 9.2
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
require_once(dirname(__FILE__).'/settings.php');
5
6
class Common {
7
	//protected $cookies = array();
8
	
9
	/**
10
	* Get data from form result
11
	* @param String $url form URL
12
	* @param String $type type of submit form method (get or post)
13
	* @param String|Array $data values form post method
14
	* @param Array $headers header to submit with the form
15
	* @return String the result
16
	*/
17
	public function getData($url, $type = 'get', $data = '', $headers = '',$cookie = '',$referer = '',$timeout = '',$useragent = '') {
18
		global $globalProxy, $globalForceIPv4;
19
		$ch = curl_init();
20
		curl_setopt($ch, CURLOPT_URL, $url);
21
		if (isset($globalForceIPv4) && $globalForceIPv4) {
22
			if (defined('CURLOPT_IPRESOLVE') && defined('CURL_IPRESOLVE_V4')){
23
				curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
24
			}
25
		}
26
		if (isset($globalProxy) && $globalProxy != '') {
27
			curl_setopt($ch, CURLOPT_PROXY, $globalProxy);
28
		}
29
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
30
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
31
		curl_setopt($ch, CURLINFO_HEADER_OUT, true); 
32
		curl_setopt($ch,CURLOPT_ENCODING , "gzip");
33
		//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');
34
//		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64; rv:42.0) Gecko/20100101 Firefox/42.0');
35
		if ($useragent == '') {
36
			curl_setopt($ch, CURLOPT_USERAGENT, UAgent::random());
37
		} else {
38
			curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
39
		}
40
		if ($timeout == '') curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
41
		else curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); 
42
		curl_setopt($ch, CURLOPT_HEADERFUNCTION, array('Common',"curlResponseHeaderCallback"));
43
		if ($type == 'post') {
44
			curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
45
			if (is_array($data)) {
46
				curl_setopt($ch, CURLOPT_POST, count($data));
47
				$data_string = '';
48
				foreach($data as $key=>$value) { $data_string .= $key.'='.$value.'&'; }
49
				rtrim($data_string, '&');
50
				curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
51
			} else {
52
				curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
53
			}
54
		}
55
		if ($headers != '') {
56
			curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
57
		}
58
		if ($cookie != '') {
59
			if (is_array($cookie)) {
60
				curl_setopt($ch, CURLOPT_COOKIE, implode($cookie,';'));
61
			} else {
62
				curl_setopt($ch, CURLOPT_COOKIE, $cookie);
63
			}
64
		}
65
		if ($referer != '') {
66
			curl_setopt($ch, CURLOPT_REFERER, $referer);
67
		}
68
		$result = curl_exec($ch);
69
		$info = curl_getinfo($ch);
70
		//var_dump($info);
71
		curl_close($ch);
72
		if ($info['http_code'] == '503' && strstr($result,'DDoS protection by CloudFlare')) {
73
			echo "Cloudflare Detected\n";
74
			require_once(dirname(__FILE__).'/libs/cloudflare-bypass/libraries/cloudflareClass.php');
75
			$useragent = UAgent::random();
76
			cloudflare::useUserAgent($useragent);
77
			if ($clearanceCookie = cloudflare::bypass($url)) {
78
				return $this->getData($url,'get',$data,$headers,$clearanceCookie,$referer,$timeout,$useragent);
79
			}
80
		} else {
81
			return $result;
82
		}
83
	}
84
	
85
	private function curlResponseHeaderCallback($ch, $headerLine) {
86
		//global $cookies;
87
		$cookies = array();
88
		if (preg_match('/^Set-Cookie:\s*([^;]*)/mi', $headerLine, $cookie) == 1)
89
			$cookies[] = $cookie;
90
		return strlen($headerLine); // Needed by curl
91
	}
92
93
	public static function download($url, $file, $referer = '') {
94
		global $globalDebug;
95
		$fp = fopen($file, 'w');
96
		$ch = curl_init();
97
		curl_setopt($ch, CURLOPT_URL, $url);
98
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
99
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
100
		if ($referer != '') curl_setopt($ch, CURLOPT_REFERER, $referer);
101
		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');
102
		curl_setopt($ch, CURLOPT_FILE, $fp);
103
		curl_exec($ch);
104
		if (curl_errno($ch) && $globalDebug) echo 'Download error: '.curl_error($ch);
105
		curl_close($ch);
106
		fclose($fp);
107
	}
108
109
	public static function gunzip($in_file,$out_file_name = '') {
110
		//echo $in_file.' -> '.$out_file_name."\n";
111
		$buffer_size = 4096; // read 4kb at a time
112
		if ($out_file_name == '') $out_file_name = str_replace('.gz', '', $in_file); 
113
		if ($in_file != '' && file_exists($in_file)) {
114
			// PHP version of Ubuntu use gzopen64 instead of gzopen
115
			if (function_exists('gzopen')) $file = gzopen($in_file,'rb');
116
			elseif (function_exists('gzopen64')) $file = gzopen64($in_file,'rb');
117
			else {
118
				echo 'gzopen not available';
119
				die;
120
			}
121
			$out_file = fopen($out_file_name, 'wb'); 
122
			while(!gzeof($file)) {
123
				fwrite($out_file, gzread($file, $buffer_size));
124
			}  
125
			fclose($out_file);
126
			gzclose($file);
127
		}
128
	}
129
130
	public static function bunzip2($in_file,$out_file_name = '') {
131
		//echo $in_file.' -> '.$out_file_name."\n";
132
		$buffer_size = 4096; // read 4kb at a time
133
		if ($out_file_name == '') $out_file_name = str_replace('.bz2', '', $in_file); 
134
		if ($in_file != '' && file_exists($in_file)) {
135
			// PHP version of Ubuntu use gzopen64 instead of gzopen
136
			if (function_exists('bzopen')) $file = bzopen($in_file,'rb');
137
			else {
138
				echo 'bzopen not available';
139
				die;
140
			}
141
			$out_file = fopen($out_file_name, 'wb'); 
142
			while(!feof($file)) {
143
				fwrite($out_file, bzread($file, $buffer_size));
144
			}  
145
			fclose($out_file);
146
			bzclose($file);
147
		}
148
	}
149
150
	/**
151
	* Convert a HTML table to an array
152
	* @param String $data HTML page
153
	* @return Array array of the tables in HTML page
154
	*/
155
	public function table2array($data) {
156
		if (!is_string($data)) return array();
157
		if ($data == '') return array();
158
		$html = str_get_html($data);
159
		if ($html === false) return array();
160
		$tabledata=array();
161
		foreach($html->find('tr') as $element)
162
		{
163
			$td = array();
164
			foreach( $element->find('th') as $row)
165
			{
166
				$td [] = trim($row->plaintext);
167
			}
168
			$td=array_filter($td);
169
			$tabledata[] = $td;
170
171
			$td = array();
172
			$tdi = array();
173
			foreach( $element->find('td') as $row)
174
			{
175
				$td [] = trim($row->plaintext);
176
				$tdi [] = trim($row->innertext);
177
			}
178
			$td=array_filter($td);
179
			$tdi=array_filter($tdi);
180
			$tabledata[]=array_merge($td,$tdi);
181
		}
182
		$html->clear();
183
		unset($html);
184
		return(array_filter($tabledata));
185
	}
186
	
187
	/**
188
	* Convert <p> part of a HTML page to an array
189
	* @param String $data HTML page
190
	* @return Array array of the <p> in HTML page
191
	*/
192
	public function text2array($data) {
193
		$html = str_get_html($data);
194
		if ($html === false) return array();
195
		$tabledata=array();
196
		foreach($html->find('p') as $element)
197
		{
198
			$tabledata [] = trim($element->plaintext);
199
		}
200
		$html->clear();
201
		unset($html);
202
		return(array_filter($tabledata));
203
	}
204
205
	/**
206
	* Give distance between 2 coordonnates
207
	* @param Float $lat latitude of first point
208
	* @param Float $lon longitude of first point
209
	* @param Float $latc latitude of second point
210
	* @param Float $lonc longitude of second point
211
	* @param String $unit km else no unit used
212
	* @return Float Distance in $unit
213
	*/
214
	public function distance($lat, $lon, $latc, $lonc, $unit = 'km') {
215
		if ($lat == $latc && $lon == $lonc) return 0;
216
		$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;
217
		if ($unit == "km") {
218
			return round($dist * 1.609344);
219
		} elseif ($unit == "m") {
220
			return round($dist * 1.609344 * 1000);
221
		} elseif ($unit == "mile" || $unit == "mi") {
222
			return round($dist);
223
		} elseif ($unit == "nm") {
224
			return round($dist*0.868976);
225
		} else {
226
			return round($dist);
227
		}
228
	}
229
230
	/**
231
	* Check is distance realistic
232
	* @param int $timeDifference the time between the reception of both messages
233
	* @param float $distance distance covered
234
	* @return whether distance is realistic
235
	*/
236
	public function withinThreshold ($timeDifference, $distance) {
237
		$x = abs($timeDifference);
238
		$d = abs($distance);
239
		if ($x == 0 || $d == 0) return true;
240
		// may be due to Internet jitter; distance is realistic
241
		if ($x < 0.7 && $d < 2000) return true;
242
		else return $d/$x < 1500*0.27778; // 1500 km/h max
243
	}
244
245
246
	// Check if an array is assoc
247
	public function isAssoc($array)
248
	{
249
		return ($array !== array_values($array));
250
	}
251
252
	public function isInteger($input){
253
	    return(ctype_digit(strval($input)));
254
	}
255
256
257
	public function convertDec($dms,$latlong) {
258
		if ($latlong == 'latitude') {
259
			$deg = substr($dms, 0, 2);
260
			$min = substr($dms, 2, 4);
261
		} else {
262
			$deg = substr($dms, 0, 3);
263
			$min = substr($dms, 3, 5);
264
		}
265
		return $deg+(($min*60)/3600);
266
	}
267
	
268
	public function convertDM($coord,$latlong) {
269
		if ($latlong == 'latitude') {
270
			if ($coord < 0) $NSEW = 'S';
271
			else $NSEW = 'N';
272
		} else {
273
			if ($coord < 0) $NSEW = 'W';
274
			else $NSEW = 'E';
275
		}
276
		$coord = abs($coord);
277
		$deg = floor($coord);
278
		$coord = ($coord-$deg)*60;
279
		$min = $coord;
280
		return array('deg' => $deg,'min' => $min,'NSEW' => $NSEW);
281
	}
282
	
283
	/**
284
	* Copy folder contents
285
	* @param       string   $source    Source path
286
	* @param       string   $dest      Destination path
287
	* @return      bool     Returns true on success, false on failure
288
	*/
289
	public function xcopy($source, $dest)
290
	{
291
		$files = glob($source.'*.*');
292
		foreach($files as $file){
293
			$file_to_go = str_replace($source,$dest,$file);
294
			copy($file, $file_to_go);
295
		}
296
		return true;
297
	}
298
	
299
	/**
300
	* Check if an url exist
301
	* @param	String $url url to check
302
	* @return	bool Return true on succes false on failure
303
	*/
304
	public function urlexist($url){
305
		$headers=get_headers($url);
306
		return stripos($headers[0],"200 OK")?true:false;
307
	}
308
	
309
	/**
310
	* Convert hexa to string
311
	* @param	String $hex data in hexa
312
	* @return	String Return result
313
	*/
314
	public function hex2str($hex) {
315
		$str = '';
316
		$hexln = strlen($hex);
317
		for($i=0;$i<$hexln;$i+=2) $str .= chr(hexdec(substr($hex,$i,2)));
318
		return $str;
319
	}
320
	
321
	/**
322
	* Convert hexa color to rgb
323
	* @param	String $hex data in hexa
324
	* @return	String Return result
325
	*/
326
	public function hex2rgb($hex) {
327
		$hex = str_replace('#','',$hex);
328
		return sscanf($hex, "%02x%02x%02x"); 
329
	}
330
	
331
	public function getHeading($lat1, $lon1, $lat2, $lon2) {
332
		//difference in longitudinal coordinates
333
		$dLon = deg2rad($lon2) - deg2rad($lon1);
334
		//difference in the phi of latitudinal coordinates
335
		$dPhi = log(tan(deg2rad($lat2) / 2 + pi() / 4) / tan(deg2rad($lat1) / 2 + pi() / 4));
336
		//we need to recalculate $dLon if it is greater than pi
337
		if(abs($dLon) > pi()) {
338
			if($dLon > 0) {
339
				$dLon = (2 * pi() - $dLon) * -1;
340
			} else {
341
				$dLon = 2 * pi() + $dLon;
342
			}
343
		}
344
		//return the angle, normalized
345
		return (rad2deg(atan2($dLon, $dPhi)) + 360) % 360;
346
	}
347
	
348
	public function checkLine($lat1,$lon1,$lat2,$lon2,$lat3,$lon3,$approx = 0.2) {
349
		//$a = ($lon2-$lon1)*$lat3+($lat2-$lat1)*$lon3+($lat1*$lon2+$lat2*$lon1);
350
		$a = -($lon2-$lon1);
351
		$b = $lat2 - $lat1;
352
		$c = -($a*$lat1+$b*$lon1);
353
		$d = $a*$lat3+$b*$lon3+$c;
354
		if ($d > -$approx && $d < $approx) return true;
355
		else return false;
356
	}
357
	
358
	public function array_merge_noappend() {
359
		$output = array();
360
		foreach(func_get_args() as $array) {
361
			foreach($array as $key => $value) {
362
				$output[$key] = isset($output[$key]) ?
363
				array_merge($output[$key], $value) : $value;
364
			}
365
		}
366
		return $output;
367
	}
368
	
369
370
	public function arr_diff($arraya, $arrayb) {
371
		foreach ($arraya as $keya => $valuea) {
372
			if (in_array($valuea, $arrayb)) {
373
				unset($arraya[$keya]);
374
			}
375
		}
376
		return $arraya;
377
	}
378
379
	/*
380
	* Check if a key exist in an array
381
	* Come from http://stackoverflow.com/a/19420866
382
	* @param Array array to check
383
	* @param String key to check
384
	* @return Bool true if exist, else false
385
	*/
386
	public function multiKeyExists(array $arr, $key) {
387
		// is in base array?
388
		if (array_key_exists($key, $arr)) {
389
			return true;
390
		}
391
		// check arrays contained in this array
392
		foreach ($arr as $element) {
393
			if (is_array($element)) {
394
				if ($this->multiKeyExists($element, $key)) {
395
					return true;
396
				}
397
			}
398
		}
399
		return false;
400
	}
401
	
402
	/**
403
	* Returns list of available locales
404
	*
405
	* @return array
406
	 */
407
	public function listLocaleDir()
408
	{
409
		$result = array('en');
410
		if (!is_dir('./locale')) {
411
			return $result;
412
		}
413
		$handle = @opendir('./locale');
414
		if ($handle === false) return $result;
415
		while (false !== ($file = readdir($handle))) {
416
			$path = './locale'.'/'.$file.'/LC_MESSAGES/fam.mo';
417
			if ($file != "." && $file != ".." && @file_exists($path)) {
418
				$result[] = $file;
419
			}
420
		}
421
		closedir($handle);
422
		return $result;
423
	}
424
425
	public function nextcoord($latitude, $longitude, $speed, $heading, $archivespeed = 1){
426
		global $globalMapRefresh;
427
		$distance = ($speed*0.514444*$globalMapRefresh*$archivespeed)/1000;
428
		$r = 6378;
429
		$latitude = deg2rad($latitude);
430
		$longitude = deg2rad($longitude);
431
		$bearing = deg2rad($heading); 
432
		$latitude2 =  asin( (sin($latitude) * cos($distance/$r)) + (cos($latitude) * sin($distance/$r) * cos($bearing)) );
433
		$longitude2 = $longitude + atan2( sin($bearing)*sin($distance/$r)*cos($latitude), cos($distance/$r)-(sin($latitude)*sin($latitude2)) );
434
		return array('latitude' => number_format(rad2deg($latitude2),5,'.',''),'longitude' => number_format(rad2deg($longitude2),5,'.',''));
435
	}
436
	
437
	public function getCoordfromDistanceBearing($latitude,$longitude,$bearing,$distance) {
438
		// distance in meter
439
		$R = 6378.14;
440
		$latitude1 = $latitude * (M_PI/180);
441
		$longitude1 = $longitude * (M_PI/180);
442
		$brng = $bearing * (M_PI/180);
443
		$d = $distance;
444
445
		$latitude2 = asin(sin($latitude1)*cos($d/$R) + cos($latitude1)*sin($d/$R)*cos($brng));
446
		$longitude2 = $longitude1 + atan2(sin($brng)*sin($d/$R)*cos($latitude1),cos($d/$R)-sin($latitude1)*sin($latitude2));
447
448
		$latitude2 = $latitude2 * (180/M_PI);
449
		$longitude2 = $longitude2 * (180/M_PI);
450
451
		$flat = round ($latitude2,6);
452
		$flong = round ($longitude2,6);
453
/*
454
		$dx = $distance*cos($bearing);
455
		$dy = $distance*sin($bearing);
456
		$dlong = $dx/(111320*cos($latitude));
457
		$dlat = $dy/110540;
458
		$flong = $longitude + $dlong;
459
		$flat = $latitude + $dlat;
460
*/
461
		return array('latitude' => $flat,'longitude' => $flong);
462
	}
463
464
	/**
465
	 * GZIPs a file on disk (appending .gz to the name)
466
	 *
467
	 * From http://stackoverflow.com/questions/6073397/how-do-you-create-a-gz-file-using-php
468
	 * Based on function by Kioob at:
469
	 * http://www.php.net/manual/en/function.gzwrite.php#34955
470
	 * 
471
	 * @param string $source Path to file that should be compressed
472
	 * @param integer $level GZIP compression level (default: 9)
473
	 * @return string New filename (with .gz appended) if success, or false if operation fails
474
	 */
475
	public function gzCompressFile($source, $level = 9){ 
476
		$dest = $source . '.gz'; 
477
		$mode = 'wb' . $level; 
478
		$error = false; 
479
		if ($fp_out = gzopen($dest, $mode)) { 
480
			if ($fp_in = fopen($source,'rb')) { 
481
				while (!feof($fp_in)) 
482
					gzwrite($fp_out, fread($fp_in, 1024 * 512)); 
483
				fclose($fp_in); 
484
			} else {
485
				$error = true; 
486
			}
487
			gzclose($fp_out); 
488
		} else {
489
			$error = true; 
490
		}
491
		if ($error)
492
			return false; 
493
		else
494
			return $dest; 
495
	} 
496
	
497
	public function remove_accents($string) {
498
		if ( !preg_match('/[\x80-\xff]/', $string) ) return $string;
499
		$chars = array(
500
		    // Decompositions for Latin-1 Supplement
501
		    chr(195).chr(128) => 'A', chr(195).chr(129) => 'A',
502
		    chr(195).chr(130) => 'A', chr(195).chr(131) => 'A',
503
		    chr(195).chr(132) => 'A', chr(195).chr(133) => 'A',
504
		    chr(195).chr(135) => 'C', chr(195).chr(136) => 'E',
505
		    chr(195).chr(137) => 'E', chr(195).chr(138) => 'E',
506
		    chr(195).chr(139) => 'E', chr(195).chr(140) => 'I',
507
		    chr(195).chr(141) => 'I', chr(195).chr(142) => 'I',
508
		    chr(195).chr(143) => 'I', chr(195).chr(145) => 'N',
509
		    chr(195).chr(146) => 'O', chr(195).chr(147) => 'O',
510
		    chr(195).chr(148) => 'O', chr(195).chr(149) => 'O',
511
		    chr(195).chr(150) => 'O', chr(195).chr(153) => 'U',
512
		    chr(195).chr(154) => 'U', chr(195).chr(155) => 'U',
513
		    chr(195).chr(156) => 'U', chr(195).chr(157) => 'Y',
514
		    chr(195).chr(159) => 's', chr(195).chr(160) => 'a',
515
		    chr(195).chr(161) => 'a', chr(195).chr(162) => 'a',
516
		    chr(195).chr(163) => 'a', chr(195).chr(164) => 'a',
517
		    chr(195).chr(165) => 'a', chr(195).chr(167) => 'c',
518
		    chr(195).chr(168) => 'e', chr(195).chr(169) => 'e',
519
		    chr(195).chr(170) => 'e', chr(195).chr(171) => 'e',
520
		    chr(195).chr(172) => 'i', chr(195).chr(173) => 'i',
521
		    chr(195).chr(174) => 'i', chr(195).chr(175) => 'i',
522
		    chr(195).chr(177) => 'n', chr(195).chr(178) => 'o',
523
		    chr(195).chr(179) => 'o', chr(195).chr(180) => 'o',
524
		    chr(195).chr(181) => 'o', chr(195).chr(182) => 'o',
525
		    chr(195).chr(182) => 'o', chr(195).chr(185) => 'u',
526
		    chr(195).chr(186) => 'u', chr(195).chr(187) => 'u',
527
		    chr(195).chr(188) => 'u', chr(195).chr(189) => 'y',
528
		    chr(195).chr(191) => 'y',
529
		    // Decompositions for Latin Extended-A
530
		    chr(196).chr(128) => 'A', chr(196).chr(129) => 'a',
531
		    chr(196).chr(130) => 'A', chr(196).chr(131) => 'a',
532
		    chr(196).chr(132) => 'A', chr(196).chr(133) => 'a',
533
		    chr(196).chr(134) => 'C', chr(196).chr(135) => 'c',
534
		    chr(196).chr(136) => 'C', chr(196).chr(137) => 'c',
535
		    chr(196).chr(138) => 'C', chr(196).chr(139) => 'c',
536
		    chr(196).chr(140) => 'C', chr(196).chr(141) => 'c',
537
		    chr(196).chr(142) => 'D', chr(196).chr(143) => 'd',
538
		    chr(196).chr(144) => 'D', chr(196).chr(145) => 'd',
539
		    chr(196).chr(146) => 'E', chr(196).chr(147) => 'e',
540
		    chr(196).chr(148) => 'E', chr(196).chr(149) => 'e',
541
		    chr(196).chr(150) => 'E', chr(196).chr(151) => 'e',
542
		    chr(196).chr(152) => 'E', chr(196).chr(153) => 'e',
543
		    chr(196).chr(154) => 'E', chr(196).chr(155) => 'e',
544
		    chr(196).chr(156) => 'G', chr(196).chr(157) => 'g',
545
		    chr(196).chr(158) => 'G', chr(196).chr(159) => 'g',
546
		    chr(196).chr(160) => 'G', chr(196).chr(161) => 'g',
547
		    chr(196).chr(162) => 'G', chr(196).chr(163) => 'g',
548
		    chr(196).chr(164) => 'H', chr(196).chr(165) => 'h',
549
		    chr(196).chr(166) => 'H', chr(196).chr(167) => 'h',
550
		    chr(196).chr(168) => 'I', chr(196).chr(169) => 'i',
551
		    chr(196).chr(170) => 'I', chr(196).chr(171) => 'i',
552
		    chr(196).chr(172) => 'I', chr(196).chr(173) => 'i',
553
		    chr(196).chr(174) => 'I', chr(196).chr(175) => 'i',
554
		    chr(196).chr(176) => 'I', chr(196).chr(177) => 'i',
555
		    chr(196).chr(178) => 'IJ',chr(196).chr(179) => 'ij',
556
		    chr(196).chr(180) => 'J', chr(196).chr(181) => 'j',
557
		    chr(196).chr(182) => 'K', chr(196).chr(183) => 'k',
558
		    chr(196).chr(184) => 'k', chr(196).chr(185) => 'L',
559
		    chr(196).chr(186) => 'l', chr(196).chr(187) => 'L',
560
		    chr(196).chr(188) => 'l', chr(196).chr(189) => 'L',
561
		    chr(196).chr(190) => 'l', chr(196).chr(191) => 'L',
562
		    chr(197).chr(128) => 'l', chr(197).chr(129) => 'L',
563
		    chr(197).chr(130) => 'l', chr(197).chr(131) => 'N',
564
		    chr(197).chr(132) => 'n', chr(197).chr(133) => 'N',
565
		    chr(197).chr(134) => 'n', chr(197).chr(135) => 'N',
566
		    chr(197).chr(136) => 'n', chr(197).chr(137) => 'N',
567
		    chr(197).chr(138) => 'n', chr(197).chr(139) => 'N',
568
		    chr(197).chr(140) => 'O', chr(197).chr(141) => 'o',
569
		    chr(197).chr(142) => 'O', chr(197).chr(143) => 'o',
570
		    chr(197).chr(144) => 'O', chr(197).chr(145) => 'o',
571
		    chr(197).chr(146) => 'OE',chr(197).chr(147) => 'oe',
572
		    chr(197).chr(148) => 'R',chr(197).chr(149) => 'r',
573
		    chr(197).chr(150) => 'R',chr(197).chr(151) => 'r',
574
		    chr(197).chr(152) => 'R',chr(197).chr(153) => 'r',
575
		    chr(197).chr(154) => 'S',chr(197).chr(155) => 's',
576
		    chr(197).chr(156) => 'S',chr(197).chr(157) => 's',
577
		    chr(197).chr(158) => 'S',chr(197).chr(159) => 's',
578
		    chr(197).chr(160) => 'S', chr(197).chr(161) => 's',
579
		    chr(197).chr(162) => 'T', chr(197).chr(163) => 't',
580
		    chr(197).chr(164) => 'T', chr(197).chr(165) => 't',
581
		    chr(197).chr(166) => 'T', chr(197).chr(167) => 't',
582
		    chr(197).chr(168) => 'U', chr(197).chr(169) => 'u',
583
		    chr(197).chr(170) => 'U', chr(197).chr(171) => 'u',
584
		    chr(197).chr(172) => 'U', chr(197).chr(173) => 'u',
585
		    chr(197).chr(174) => 'U', chr(197).chr(175) => 'u',
586
		    chr(197).chr(176) => 'U', chr(197).chr(177) => 'u',
587
		    chr(197).chr(178) => 'U', chr(197).chr(179) => 'u',
588
		    chr(197).chr(180) => 'W', chr(197).chr(181) => 'w',
589
		    chr(197).chr(182) => 'Y', chr(197).chr(183) => 'y',
590
		    chr(197).chr(184) => 'Y', chr(197).chr(185) => 'Z',
591
		    chr(197).chr(186) => 'z', chr(197).chr(187) => 'Z',
592
		    chr(197).chr(188) => 'z', chr(197).chr(189) => 'Z',
593
		    chr(197).chr(190) => 'z', chr(197).chr(191) => 's'
594
		);
595
		$string = strtr($string, $chars);
596
		return $string;
597
	}
598
	
599
	/*
600
	* Extract int from a string
601
	* Come from http://php.net/manual/fr/function.intval.php comment by michiel ed thalent nl
602
	*
603
	* @param String Input string
604
	* @return Integer integer from the string
605
	*/
606
	public function str2int($string, $concat = false) {
607
		$length = strlen($string);    
608
		for ($i = 0, $int = '', $concat_flag = true; $i < $length; $i++) {
609
			if (is_numeric($string[$i]) && $concat_flag) {
610
				$int .= $string[$i];
611
			} elseif(!$concat && $concat_flag && strlen($int) > 0) {
612
				$concat_flag = false;
613
			}
614
		}
615
		return (int) $int;
616
	}
617
	
618
	public function create_socket($host, $port, &$errno, &$errstr) {
619
		$ip = gethostbyname($host);
620
		$s = socket_create(AF_INET, SOCK_STREAM, 0);
621
		$r = @socket_connect($s, $ip, $port);
622
		if (!socket_set_nonblock($s)) echo "Unable to set nonblock on socket\n";
623
		if ($r || socket_last_error() == 114 || socket_last_error() == 115) {
624
			return $s;
625
		}
626
		$errno = socket_last_error($s);
627
		$errstr = socket_strerror($errno);
628
		socket_close($s);
629
		return false;
630
	}
631
632
	public function create_socket_udp($host, $port, &$errno, &$errstr) {
633
		$ip = gethostbyname($host);
634
		$s = socket_create(AF_INET, SOCK_DGRAM, 0);
635
		$r = @socket_bind($s, $ip, $port);
636
		if ($r || socket_last_error() == 114 || socket_last_error() == 115) {
637
			return $s;
638
		}
639
		$errno = socket_last_error($s);
640
		$errstr = socket_strerror($errno);
641
		socket_close($s);
642
		return false;
643
	}
644
645
	public function getUserIP() { 
646
		$client = @$_SERVER['HTTP_CLIENT_IP'];
647
		$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
648
		return filter_var($client, FILTER_VALIDATE_IP) ? $client : filter_var($forward, FILTER_VALIDATE_IP) ? $forward : $_SERVER['REMOTE_ADDR']; 
649
	}
650
}
651
?>