Source for file plazes.php

Documentation is available at plazes.php

  1. <?php
  2. /**
  3. * @package org.routamc.positioning
  4. * @author The Midgard Project, http://www.midgard-project.org
  5. * @version $Id: plazes.php 3757 2006-07-27 14:32:42Z bergie $
  6. * @copyright The Midgard Project, http://www.midgard-project.org
  7. *
  8. * Based on PlazesWhereAmIPhp by
  9. * @author Peter Rukavina <peter@rukavina.net>
  10. * @author Olle Jonsson <olle@olleolleolle.dk>
  11. * @copyright Reinvented Inc., 2005
  12. * @license http://creativecommons.org/licenses/by-sa/2.0/ca
  13. */
  14.  
  15. // PEAR XML_RPC package
  16. require_once 'XML/RPC.php';
  17.  
  18. /**
  19. * Importer for fetching position data for Plazes users
  20. *
  21. * @package org.routamc.positioning
  22. */
  23. class org_routamc_positioning_importer_plazes extends org_routamc_positioning_importer
  24. {
  25. /**
  26. * Initializes the class. The real startup is done by the initialize() call.
  27. */
  28. function org_routamc_positioning_importer_plazes()
  29. {
  30. parent::org_routamc_positioning_importer();
  31. }
  32. /**
  33. * Seek users with Plazes account settings set
  34. *
  35. * @return Array
  36. */
  37. function seek_plazes_users()
  38. {
  39. // TODO: With 1.8 we can query parameters more efficiently
  40. $qb = new MidgardQueryBuilder('midgard_parameter');
  41. $qb->add_constraint('domain', '=','org.routamc.positioning:plazes');
  42. $qb->add_constraint('name', '=','username');
  43. $qb->add_constraint('tablename', '=', 'person');
  44. $accounts = $qb->execute();
  45. if (count($accounts) > 0)
  46. {
  47. foreach ($accounts as $account_param)
  48. {
  49. $user = new midcom_db_person($account_param->oid);
  50. $this->get_plazes_location($user, true);
  51. }
  52. }
  53. }
  54.  
  55. function _fetch_plazes_position($plazes_username, $plazes_password)
  56. {
  57. $plazes_password_md5 = md5("PLAZES{$plazes_password}");
  58. // These are the required XML-RPC parameters
  59. $params = array
  60. (
  61. new XML_RPC_Value($plazes_username, 'string'),
  62. new XML_RPC_Value($plazes_password_md5, 'string')
  63. );
  64. // Name of the XML-RPC method to be called
  65. $msg = new XML_RPC_Message('plazes.whereami', $params);
  66. // URI of the XML-RPC stub
  67. $cli = new XML_RPC_Client('/xmlrpc/whereami.php', 'http://www.plazes.com');
  68. $resp = $cli->send($msg);
  69.  
  70. if (!$resp)
  71. {
  72. $this->error = 'POSITIONING_PLAZES_CONNECTION_FAILED';
  73. return null;
  74. }
  75.  
  76. if (!$resp->faultCode())
  77. {
  78. $results = $resp->value();
  79. if (isset($results))
  80. {
  81. $plaze_lat = $results->structmem('plazelat');
  82. $plaze_lat = $plaze_lat->scalarval();
  83. $plaze_lon = $results->structmem('plazelon');
  84. $plaze_lon = $plaze_lon->scalarval();
  85. /*
  86. $plaze_url = $results->structmem('plazeurl')->scalarval();
  87. $plaze_name = $results->structmem('plazename')->scalarval();
  88. $plaze_username = $results->structmem('username')->scalarval();
  89. $plaze_state = $results->structmem('state')->scalarval();
  90. */
  91. $plaze_country = $results->structmem('plazecountry');
  92. $plaze_country = $plaze_country->scalarval();
  93. $plaze_city = $results->structmem('plazecity');
  94. $plaze_city = $plaze_city->scalarval();
  95. $position = array
  96. (
  97. 'latitude' => $plaze_lat,
  98. 'longitude' => $plaze_lon,
  99. 'country' => $plaze_country,
  100. 'city' => $plaze_city,
  101. );
  102. return $position;
  103. }
  104. else
  105. {
  106. $this->error = 'POSITIONING_PLAZES_CONNECTION_NORESULTS';
  107. return null;
  108. }
  109. }
  110. else
  111. {
  112. $this->error = 'POSITIONING_PLAZES_FAULT_' . $resp->faultCode();
  113. return null;
  114. }
  115. }
  116. /**
  117. * Get plazes location for an user
  118. *
  119. * @param midcom_db_person $user Person to fetch Plazes data for
  120. * @param boolean $cache Whether to cache the position to a log object
  121. * @return Array
  122. */
  123. function get_plazes_location($user, $cache = true)
  124. {
  125. $plazes_username = $user->parameter('org.routamc.positioning:plazes', 'username');
  126. $plazes_password = $user->parameter('org.routamc.positioning:plazes', 'password');
  127. if ( $plazes_username
  128. && $plazes_password)
  129. {
  130. $position = $this->_fetch_plazes_position($plazes_username, $plazes_password);
  131. if (is_null($position))
  132. {
  133. return null;
  134. }
  135. if ($cache)
  136. {
  137. $this->import($position, $user->id);
  138. }
  139. return $position;
  140. }
  141. else
  142. {
  143. $this->error = 'POSITIONING_PLAZES_NO_ACCOUNT';
  144. }
  145. return null;
  146. }
  147. /**
  148. * Import plazes log entry. The entries are associative arrays containing
  149. * all of the following keys:
  150. *
  151. * - latitude
  152. * - longitude
  153. *
  154. * @param Array $log Log entry in Array format specific to importer
  155. * @param integer $person_id ID of the person to import logs for
  156. * @return bool Indicating success.
  157. */
  158. function import($position, $person_id)
  159. {
  160. $this->log = new org_routamc_positioning_log_dba();
  161. $this->log->importer = 'plazes';
  162. $this->log->person = $person_id;
  163. $this->log->date = time();
  164. $this->log->latitude = (float) $position['latitude'];
  165. $this->log->longitude = (float) $position['longitude'];
  166. $this->log->altitude = 0;
  167. $this->log->accuracy = ORG_ROUTAMC_POSITIONING_ACCURACY_PLAZES;
  168.  
  169. // Try to create the entry
  170. $stat = $this->log->create();
  171. $this->error = mgd_errstr();
  172. return $stat;
  173. }
  174. }

Documentation generated on Tue, 15 Aug 2006 12:51:40 +0300 by phpDocumentor 1.3.0RC3