Official Addons for the Friendica Communications Platform. (please note that this is a clone of the repository at github, issues are handled there)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.0 KiB

  1. <?php
  2. /**
  3. * Name: Geocoordinates
  4. * Description: Use the OpenCage Geocoder http://geocoder.opencagedata.com to resolve nearest populated location for given latitude, longitude. Derived from "geonames"
  5. * Version: 0.1
  6. * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
  7. */
  8. use Friendica\Core\Hook;
  9. use Friendica\Core\Logger;
  10. use Friendica\Core\Renderer;
  11. use Friendica\DI;
  12. use Friendica\Util\Strings;
  13. function geocoordinates_install()
  14. {
  15. Hook::register('post_local', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
  16. Hook::register('post_remote', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
  17. }
  18. function geocoordinates_resolve_item(&$item)
  19. {
  20. if((!$item["coord"]) || ($item["location"]))
  21. return;
  22. $key = DI::config()->get("geocoordinates", "api_key");
  23. if ($key == "")
  24. return;
  25. $language = DI::config()->get("geocoordinates", "language");
  26. if ($language == "")
  27. $language = "de";
  28. $coords = explode(' ',$item["coord"]);
  29. if (count($coords) < 2)
  30. return;
  31. $coords[0] = round($coords[0], 5);
  32. $coords[1] = round($coords[1], 5);
  33. $result = DI::cache()->get("geocoordinates:".$language.":".$coords[0]."-".$coords[1]);
  34. if (!is_null($result)) {
  35. $item["location"] = $result;
  36. return;
  37. }
  38. $s = DI::httpRequest()->fetch("https://api.opencagedata.com/geocode/v1/json?q=" . $coords[0] . "," . $coords[1] . "&key=" . $key . "&language=" . $language);
  39. if (!$s) {
  40. Logger::log("API could not be queried", Logger::DEBUG);
  41. return;
  42. }
  43. $data = json_decode($s);
  44. if ($data->status->code != "200") {
  45. Logger::log("API returned error ".$data->status->code." ".$data->status->message, Logger::DEBUG);
  46. return;
  47. }
  48. if (($data->total_results == 0) || (count($data->results) == 0)) {
  49. Logger::log("No results found for coordinates ".$item["coord"], Logger::DEBUG);
  50. return;
  51. }
  52. $item["location"] = $data->results[0]->formatted;
  53. Logger::log("Got location for coordinates ".$coords[0]."-".$coords[1].": ".$item["location"], Logger::DEBUG);
  54. if ($item["location"] != "")
  55. DI::cache()->set("geocoordinates:".$language.":".$coords[0]."-".$coords[1], $item["location"]);
  56. }
  57. function geocoordinates_post_hook($a, &$item)
  58. {
  59. geocoordinates_resolve_item($item);
  60. }
  61. function geocoordinates_addon_admin(&$a, &$o)
  62. {
  63. $t = Renderer::getMarkupTemplate("admin.tpl", "addon/geocoordinates/");
  64. $o = Renderer::replaceMacros($t, [
  65. '$submit' => DI::l10n()->t('Save Settings'),
  66. '$api_key' => ['api_key', DI::l10n()->t('API Key'), DI::config()->get('geocoordinates', 'api_key'), ''],
  67. '$language' => ['language', DI::l10n()->t('Language code (IETF format)'), DI::config()->get('geocoordinates', 'language'), ''],
  68. ]);
  69. }
  70. function geocoordinates_addon_admin_post(&$a)
  71. {
  72. $api_key = (!empty($_POST['api_key']) ? Strings::escapeTags(trim($_POST['api_key'])) : '');
  73. DI::config()->set('geocoordinates', 'api_key', $api_key);
  74. $language = (!empty($_POST['language']) ? Strings::escapeTags(trim($_POST['language'])) : '');
  75. DI::config()->set('geocoordinates', 'language', $language);
  76. }