activitycontext.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * An activity
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Feed
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @author Zach Copley <zach@status.net>
  26. * @copyright 2010 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. exit(1);
  32. }
  33. class ActivityContext
  34. {
  35. public $replyToID;
  36. public $replyToUrl;
  37. public $location;
  38. public $attention = array(); // 'uri' => 'type'
  39. public $conversation;
  40. public $scope;
  41. const THR = 'http://purl.org/syndication/thread/1.0';
  42. const GEORSS = 'http://www.georss.org/georss';
  43. const OSTATUS = 'http://ostatus.org/schema/1.0';
  44. const INREPLYTO = 'in-reply-to';
  45. const REF = 'ref';
  46. const HREF = 'href';
  47. // OStatus element names with prefixes
  48. const OBJECTTYPE = 'ostatus:object-type'; // FIXME: Undocumented!
  49. const CONVERSATION = 'ostatus:conversation';
  50. const POINT = 'point';
  51. const MENTIONED = 'mentioned';
  52. const ATTN_PUBLIC = 'http://activityschema.org/collection/public';
  53. function __construct($element = null)
  54. {
  55. if (empty($element)) {
  56. return;
  57. }
  58. $replyToEl = ActivityUtils::child($element, self::INREPLYTO, self::THR);
  59. if (!empty($replyToEl)) {
  60. $this->replyToID = $replyToEl->getAttribute(self::REF);
  61. $this->replyToUrl = $replyToEl->getAttribute(self::HREF);
  62. }
  63. $this->location = $this->getLocation($element);
  64. $convs = $element->getElementsByTagNameNS(self::OSTATUS, self::CONVERSATION);
  65. foreach ($convs as $conv) {
  66. $this->conversation = $conv->textContent;
  67. }
  68. if (empty($this->conversation)) {
  69. // fallback to the atom:link rel="ostatus:conversation" element
  70. $this->conversation = ActivityUtils::getLink($element, self::CONVERSATION);
  71. }
  72. // Multiple attention links allowed
  73. $links = $element->getElementsByTagNameNS(ActivityUtils::ATOM, ActivityUtils::LINK);
  74. for ($i = 0; $i < $links->length; $i++) {
  75. $link = $links->item($i);
  76. $linkRel = $link->getAttribute(ActivityUtils::REL);
  77. $linkHref = $link->getAttribute(self::HREF);
  78. if ($linkRel == self::MENTIONED && $linkHref !== '') {
  79. $this->attention[$linkHref] = $link->getAttribute(ActivityContext::OBJECTTYPE);
  80. }
  81. }
  82. }
  83. /**
  84. * Parse location given as a GeoRSS-simple point, if provided.
  85. * http://www.georss.org/simple
  86. *
  87. * @param feed item $entry
  88. * @return mixed Location or false
  89. */
  90. function getLocation($dom)
  91. {
  92. $points = $dom->getElementsByTagNameNS(self::GEORSS, self::POINT);
  93. for ($i = 0; $i < $points->length; $i++) {
  94. $point = $points->item($i)->textContent;
  95. return self::locationFromPoint($point);
  96. }
  97. return null;
  98. }
  99. // XXX: Move to ActivityUtils or Location?
  100. static function locationFromPoint($point)
  101. {
  102. $point = str_replace(',', ' ', $point); // per spec "treat commas as whitespace"
  103. $point = preg_replace('/\s+/', ' ', $point);
  104. $point = trim($point);
  105. $coords = explode(' ', $point);
  106. if (count($coords) == 2) {
  107. list($lat, $lon) = $coords;
  108. if (is_numeric($lat) && is_numeric($lon)) {
  109. common_log(LOG_INFO, "Looking up location for $lat $lon from georss point");
  110. return Location::fromLatLon($lat, $lon);
  111. }
  112. }
  113. common_log(LOG_ERR, "Ignoring bogus georss:point value $point");
  114. return null;
  115. }
  116. /**
  117. * Returns context (StatusNet stuff) as an array suitable for serializing
  118. * in JSON. Right now context stuff is an extension to Activity.
  119. *
  120. * @return array the context
  121. */
  122. function asArray()
  123. {
  124. $context = array();
  125. $context['inReplyTo'] = $this->getInReplyToArray();
  126. $context['conversation'] = $this->conversation;
  127. return array_filter($context);
  128. }
  129. /**
  130. * Returns an array of arrays representing Activity Objects (intended to be
  131. * serialized in JSON) that represent WHO the Activity is supposed to
  132. * be received by. This is not really specified but appears in an example
  133. * of the current spec as an extension. We might want to figure out a JSON
  134. * serialization for OStatus and use that to express mentions instead.
  135. *
  136. * XXX: People's ideas on how to do this are all over the place
  137. *
  138. * @return array the array of recipients
  139. */
  140. function getToArray()
  141. {
  142. $tos = array();
  143. foreach ($this->attention as $attnUrl => $attnType) {
  144. $to = array(
  145. 'objectType' => $attnType, // can be empty
  146. 'id' => $attnUrl,
  147. );
  148. $tos[] = $to;
  149. }
  150. return $tos;
  151. }
  152. /**
  153. * Return an array for the notices this notice is a reply to
  154. * suitable for serializing as JSON note objects.
  155. *
  156. * @return array the array of notes
  157. */
  158. function getInReplyToArray()
  159. {
  160. if (empty($this->replyToID) && empty($this->replyToUrl)) {
  161. return null;
  162. }
  163. $replyToObj = array('objectType' => 'note');
  164. // XXX: Possibly shorten this to just the numeric ID?
  165. // Currently, it's the full URI of the notice.
  166. if (!empty($this->replyToID)) {
  167. $replyToObj['id'] = $this->replyToID;
  168. }
  169. if (!empty($this->replyToUrl)) {
  170. $replyToObj['url'] = $this->replyToUrl;
  171. }
  172. return $replyToObj;
  173. }
  174. }