activitycontext.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 $conversation_url;
  41. public $scope;
  42. const THR = 'http://purl.org/syndication/thread/1.0';
  43. const GEORSS = 'http://www.georss.org/georss';
  44. const OSTATUS = 'http://ostatus.org/schema/1.0';
  45. const INREPLYTO = 'in-reply-to';
  46. const REF = 'ref';
  47. const HREF = 'href';
  48. // OStatus element names with prefixes
  49. const OBJECTTYPE = 'ostatus:object-type'; // FIXME: Undocumented!
  50. const CONVERSATION = 'conversation';
  51. const POINT = 'point';
  52. const MENTIONED = 'mentioned';
  53. const ATTN_PUBLIC = 'http://activityschema.org/collection/public';
  54. function __construct($element = null)
  55. {
  56. if (empty($element)) {
  57. return;
  58. }
  59. $replyToEl = ActivityUtils::child($element, self::INREPLYTO, self::THR);
  60. if (!empty($replyToEl)) {
  61. $this->replyToID = $replyToEl->getAttribute(self::REF);
  62. $this->replyToUrl = $replyToEl->getAttribute(self::HREF);
  63. }
  64. $this->location = $this->getLocation($element);
  65. foreach ($element->getElementsByTagNameNS(self::OSTATUS, self::CONVERSATION) as $conv) {
  66. if ($conv->hasAttribute('ref')) {
  67. $this->conversation = $conv->getAttribute('ref');
  68. if ($conv->hasAttribute('href')) {
  69. $this->conversation_url = $conv->getAttribute('href');
  70. }
  71. } else {
  72. $this->conversation = $conv->textContent;
  73. }
  74. if (!empty($this->conversation)) {
  75. break;
  76. }
  77. }
  78. if (empty($this->conversation)) {
  79. // fallback to the atom:link rel="ostatus:conversation" element
  80. $this->conversation = ActivityUtils::getLink($element, 'ostatus:'.self::CONVERSATION);
  81. }
  82. // Multiple attention links allowed
  83. $links = $element->getElementsByTagNameNS(ActivityUtils::ATOM, ActivityUtils::LINK);
  84. for ($i = 0; $i < $links->length; $i++) {
  85. $link = $links->item($i);
  86. $linkRel = $link->getAttribute(ActivityUtils::REL);
  87. $linkHref = $link->getAttribute(self::HREF);
  88. if ($linkRel == self::MENTIONED && $linkHref !== '') {
  89. $this->attention[$linkHref] = $link->getAttribute(ActivityContext::OBJECTTYPE);
  90. }
  91. }
  92. }
  93. /**
  94. * Parse location given as a GeoRSS-simple point, if provided.
  95. * http://www.georss.org/simple
  96. *
  97. * @param feed item $entry
  98. * @return mixed Location or false
  99. */
  100. function getLocation($dom)
  101. {
  102. $points = $dom->getElementsByTagNameNS(self::GEORSS, self::POINT);
  103. for ($i = 0; $i < $points->length; $i++) {
  104. $point = $points->item($i)->textContent;
  105. return self::locationFromPoint($point);
  106. }
  107. return null;
  108. }
  109. // XXX: Move to ActivityUtils or Location?
  110. static function locationFromPoint($point)
  111. {
  112. $point = str_replace(',', ' ', $point); // per spec "treat commas as whitespace"
  113. $point = preg_replace('/\s+/', ' ', $point);
  114. $point = trim($point);
  115. $coords = explode(' ', $point);
  116. if (count($coords) == 2) {
  117. list($lat, $lon) = $coords;
  118. if (is_numeric($lat) && is_numeric($lon)) {
  119. common_log(LOG_INFO, "Looking up location for $lat $lon from georss point");
  120. return Location::fromLatLon($lat, $lon);
  121. }
  122. }
  123. common_log(LOG_ERR, "Ignoring bogus georss:point value $point");
  124. return null;
  125. }
  126. /**
  127. * Returns context (StatusNet stuff) as an array suitable for serializing
  128. * in JSON. Right now context stuff is an extension to Activity.
  129. *
  130. * @return array the context
  131. */
  132. function asArray()
  133. {
  134. $context = array();
  135. $context['inReplyTo'] = $this->getInReplyToArray();
  136. $context['conversation'] = $this->conversation;
  137. $context['conversation_url'] = $this->conversation_url;
  138. return array_filter($context);
  139. }
  140. /**
  141. * Returns an array of arrays representing Activity Objects (intended to be
  142. * serialized in JSON) that represent WHO the Activity is supposed to
  143. * be received by. This is not really specified but appears in an example
  144. * of the current spec as an extension. We might want to figure out a JSON
  145. * serialization for OStatus and use that to express mentions instead.
  146. *
  147. * XXX: People's ideas on how to do this are all over the place
  148. *
  149. * @return array the array of recipients
  150. */
  151. function getToArray()
  152. {
  153. $tos = array();
  154. foreach ($this->attention as $attnUrl => $attnType) {
  155. $to = array(
  156. 'objectType' => $attnType, // can be empty
  157. 'id' => $attnUrl,
  158. );
  159. $tos[] = $to;
  160. }
  161. return $tos;
  162. }
  163. /**
  164. * Return an array for the notices this notice is a reply to
  165. * suitable for serializing as JSON note objects.
  166. *
  167. * @return array the array of notes
  168. */
  169. function getInReplyToArray()
  170. {
  171. if (empty($this->replyToID) && empty($this->replyToUrl)) {
  172. return null;
  173. }
  174. $replyToObj = array('objectType' => 'note');
  175. // XXX: Possibly shorten this to just the numeric ID?
  176. // Currently, it's the full URI of the notice.
  177. if (!empty($this->replyToID)) {
  178. $replyToObj['id'] = $this->replyToID;
  179. }
  180. if (!empty($this->replyToUrl)) {
  181. $replyToObj['url'] = $this->replyToUrl;
  182. }
  183. return $replyToObj;
  184. }
  185. }