PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/org/routamc/positioning/importer/fireeagle.php

https://github.com/nemein/openpsa
PHP | 145 lines | 89 code | 20 blank | 36 comment | 9 complexity | 72bd939401992a190a7d6653df5decbb MD5 | raw file
  1. <?php
  2. /**
  3. * @package org.routamc.positioning
  4. * @author The Midgard Project, http://www.midgard-project.org
  5. * @copyright The Midgard Project, http://www.midgard-project.org
  6. */
  7. /**
  8. * Importer for fetching position data for Yahoo! Fire Eagle users
  9. *
  10. * @package org.routamc.positioning
  11. */
  12. class org_routamc_positioning_importer_fireeagle extends org_routamc_positioning_importer
  13. {
  14. /**
  15. * Seek users with Plazes account settings set
  16. *
  17. * @return Array
  18. */
  19. function seek_fireeagle_users()
  20. {
  21. // TODO: With 1.8 we can query parameters more efficiently
  22. $qb = new midgard_query_builder('midgard_parameter');
  23. $qb->add_constraint('domain', '=', 'net.yahoo.fireeagle');
  24. $qb->add_constraint('name', '=', 'access_key');
  25. $accounts = $qb->execute();
  26. if (count($accounts) > 0)
  27. {
  28. foreach ($accounts as $account_param)
  29. {
  30. $user = new midcom_db_person($account_param->parentguid);
  31. $this->get_fireeagle_location($user, true);
  32. }
  33. }
  34. }
  35. private function _fetch_fireeagle_positions($fireeagle_access_key, $fireeagle_access_secret)
  36. {
  37. $position = array();
  38. require_once(MIDCOM_ROOT . '/external/fireeagle.php');
  39. $fireeagle = new FireEagle($this->_config->get('fireeagle_consumer_key'), $this->_config->get('fireeagle_consumer_secret'), $fireeagle_access_key, $fireeagle_access_secret);
  40. // Note: this must be C so we get floats correctly from JSON. See http://bugs.php.net/bug.php?id=41403
  41. setlocale(LC_NUMERIC, 'C');
  42. $user_data = $fireeagle->user();
  43. if ( !$user_data
  44. || !$user_data->user
  45. || empty($user_data->user->location_hierarchy))
  46. {
  47. return $position;
  48. }
  49. $best_position = $user_data->user->location_hierarchy[0];
  50. switch ($best_position->level_name)
  51. {
  52. case 'exact':
  53. $position['accuracy'] = 10;
  54. break;
  55. case 'postal':
  56. $position['accuracy'] = 20;
  57. break;
  58. case 'city':
  59. $position['accuracy'] = 30;
  60. break;
  61. default:
  62. $position['accuracy'] = 60;
  63. break;
  64. }
  65. $position['latitude'] = $best_position->latitude;
  66. $position['longitude'] = $best_position->longitude;
  67. $position['date'] = strtotime($best_position->located_at);
  68. return $position;
  69. }
  70. /**
  71. * Get fireeagle location for a user
  72. *
  73. * @param midcom_db_person $user Person to fetch Plazes data for
  74. * @param boolean $cache Whether to cache the position to a log object
  75. * @return Array
  76. */
  77. function get_fireeagle_location($user, $cache = true)
  78. {
  79. $fireeagle_access_key = $user->get_parameter('net.yahoo.fireeagle', 'access_key');
  80. $fireeagle_access_secret = $user->get_parameter('net.yahoo.fireeagle', 'access_secret');
  81. if ( $fireeagle_access_key
  82. && $fireeagle_access_secret)
  83. {
  84. $position = $this->_fetch_fireeagle_positions($fireeagle_access_key, $fireeagle_access_secret);
  85. if ( is_null($position)
  86. && !is_array($position))
  87. {
  88. return null;
  89. }
  90. $this->import($position, $user->id);
  91. return $position;
  92. }
  93. else
  94. {
  95. $this->error = 'POSITIONING_FIREEAGLE_NO_ACCOUNT';
  96. }
  97. return null;
  98. }
  99. /**
  100. * Import fireeagle log entry. The entries are associative arrays containing
  101. * all of the following keys:
  102. *
  103. * - latitude
  104. * - longitude
  105. *
  106. * @param Array $log Log entry in Array format specific to importer
  107. * @param integer $person_id ID of the person to import logs for
  108. * @return boolean Indicating success.
  109. */
  110. function import($position, $person_id)
  111. {
  112. $this->log = new org_routamc_positioning_log_dba();
  113. $this->log->importer = 'fireeagle';
  114. $this->log->person = $person_id;
  115. $this->log->date = (int) $position['date'];
  116. $this->log->latitude = (float) $position['latitude'];
  117. $this->log->longitude = (float) $position['longitude'];
  118. $this->log->altitude = 0;
  119. $this->log->accuracy = $position['accuracy'];
  120. // Try to create the entry
  121. $stat = $this->log->create();
  122. $this->error = midcom_connection::get_error_string();
  123. return $stat;
  124. }
  125. }