/lib/global.php

https://github.com/aussieian/sunflightmap · PHP · 124 lines · 89 code · 21 blank · 14 comment · 27 complexity · bb0f015f43b9db1a7c1abb62de1bf381 MD5 · raw file

  1. <?php
  2. include_once("config.php");
  3. include_once("airport_codes.php"); // todo: could move this to load on demand since its a large file
  4. function getAirport($airport_code) {
  5. global $openflights_airports;
  6. if (array_key_exists($airport_code, $openflights_airports)) {
  7. return $openflights_airports[$airport_code];
  8. }
  9. // not found
  10. return null;
  11. }
  12. // detect chrome
  13. function isChrome() {
  14. $ischrome = (preg_match("/Chrome/i", $_SERVER['HTTP_USER_AGENT']) > 0);
  15. return $ischrome;
  16. }
  17. // detect a mobile device
  18. function isMobile() {
  19. $iphone = (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),"iphone") !== false);
  20. $android = (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),"android") > 0);
  21. $palmpre = (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),"webos") !== false);
  22. $ipod = (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),"ipod") !== false);
  23. if($iphone || $android || $palmpre || $ipod) {
  24. // it's a mobile device
  25. return true;
  26. }
  27. return false;
  28. }
  29. // detect iPhone
  30. function isiPhone() {
  31. $isiPhone = (bool) strpos(strtolower($_SERVER['HTTP_USER_AGENT']),"iphone");
  32. return $isiPhone;
  33. }
  34. // detect ipad
  35. function isiPad() {
  36. $isiPad = (bool) strpos($_SERVER['HTTP_USER_AGENT'],'iPad');
  37. return $isiPad;
  38. }
  39. // detect IE 6
  40. function isIE6() {
  41. $ua=getBrowser();
  42. $is_ie7 = ($ua["name"] == 'Internet Explorer' && $ua['version'] == 6);
  43. return $is_ie7;
  44. }
  45. // detect IE 7
  46. function isIE7() {
  47. $ua=getBrowser();
  48. $is_ie7 = ($ua["name"] == 'Internet Explorer' && $ua['version'] == 7);
  49. return $is_ie7;
  50. }
  51. // detect IE 8
  52. function isIE8() {
  53. $ua=getBrowser();
  54. $is_ie8 = ($ua["name"] == 'Internet Explorer' && $ua['version'] == 8);
  55. return $is_ie8;
  56. }
  57. // detect IE 9
  58. function isIE9() {
  59. $ua=getBrowser();
  60. $is_ie9 = ($ua["name"] == 'Internet Explorer' && $ua['version'] == 9);
  61. return $is_ie9;
  62. }
  63. // detect any IE
  64. function isIE() {
  65. $ua=getBrowser();
  66. $is_ie = ($ua["name"] == 'Internet Explorer');
  67. return $is_ie;
  68. }
  69. // list recent searched flights from cache
  70. function showRecentSearches() {
  71. global $cfg;
  72. $dir = $cfg['CACHE_STORAGE_PATH'] . "/cache.storage/files"; // '/var/www/sunflight.net/lib/cache.storage/files/';
  73. $cache_files = @scandir($dir);
  74. //print_r($cache_files);
  75. if ($cache_files == null) {
  76. print("No recent results.");
  77. return;
  78. }
  79. foreach ($cache_files as $cache_file) {
  80. if (strstr($cache_file, "sunflight_") !== false) {
  81. $parts = explode("_", $cache_file);
  82. $url = "flightcode=" . strtoupper($parts[1] . $parts[2]) . "&date=" . $parts[3] . "-" . $parts[4] . "-" . $parts[5];
  83. print("<a rel='external' href='/?" . $url . "'>" . strtoupper($parts[1] . $parts[2]) . " departing " . $parts[3] . "-" . $parts[4] . "-" . $parts[5] . "</a><br>");
  84. }
  85. }
  86. }
  87. // get distance between two locations
  88. function calcDistance($lat1, $lon1, $lat2, $lon2, $unit) {
  89. $theta = $lon1 - $lon2;
  90. $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
  91. $dist = acos($dist);
  92. $dist = rad2deg($dist);
  93. $miles = $dist * 60 * 1.1515;
  94. $unit = strtoupper($unit);
  95. if ($unit == "K") {
  96. return ($miles * 1.609344);
  97. } else if ($unit == "N") {
  98. return ($miles * 0.8684);
  99. } else {
  100. return $miles;
  101. }
  102. }
  103. ?>