activityutils.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. /**
  34. * Utilities for turning DOMish things into Activityish things
  35. *
  36. * Some common functions that I didn't have the bandwidth to try to factor
  37. * into some kind of reasonable superclass, so just dumped here. Might
  38. * be useful to have an ActivityObject parent class or something.
  39. *
  40. * @category OStatus
  41. * @package StatusNet
  42. * @author Evan Prodromou <evan@status.net>
  43. * @copyright 2010 StatusNet, Inc.
  44. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
  45. * @link http://status.net/
  46. */
  47. class ActivityUtils
  48. {
  49. const ATOM = 'http://www.w3.org/2005/Atom';
  50. const LINK = 'link';
  51. const REL = 'rel';
  52. const TYPE = 'type';
  53. const HREF = 'href';
  54. const CONTENT = 'content';
  55. const SRC = 'src';
  56. /**
  57. * Get the permalink for an Activity object
  58. *
  59. * @param DOMElement $element A DOM element
  60. *
  61. * @return string related link, if any
  62. */
  63. static function getPermalink(DOMNode $element)
  64. {
  65. return self::getLink($element, 'alternate', 'text/html');
  66. }
  67. static function getSelfLink(DOMNode $element)
  68. {
  69. return self::getLink($element, 'self', 'application/atom+xml');
  70. }
  71. /**
  72. * Get the permalink for an Activity object
  73. *
  74. * @param DOMElement $element A DOM element
  75. *
  76. * @return string related link, if any
  77. */
  78. static function getLink(DOMNode $element, $rel, $type=null)
  79. {
  80. $els = $element->childNodes;
  81. foreach ($els as $link) {
  82. if (!($link instanceof DOMElement)) {
  83. continue;
  84. }
  85. if ($link->localName == self::LINK && $link->namespaceURI == self::ATOM) {
  86. $linkRel = $link->getAttribute(self::REL);
  87. $linkType = $link->getAttribute(self::TYPE);
  88. // XXX: Am I allowed to do this according to specs? (matching using common_bare_mime)
  89. if ($linkRel == $rel &&
  90. (is_null($type) || common_bare_mime($linkType) == common_bare_mime($type))) {
  91. return $link->getAttribute(self::HREF);
  92. }
  93. }
  94. }
  95. return null;
  96. }
  97. static function getLinks(DOMNode $element, $rel, $type=null)
  98. {
  99. $els = $element->childNodes;
  100. $out = array();
  101. for ($i = 0; $i < $els->length; $i++) {
  102. $link = $els->item($i);
  103. if ($link->localName == self::LINK && $link->namespaceURI == self::ATOM) {
  104. $linkRel = $link->getAttribute(self::REL);
  105. $linkType = $link->getAttribute(self::TYPE);
  106. if ($linkRel == $rel &&
  107. (is_null($type) || $linkType == $type)) {
  108. $out[] = $link;
  109. }
  110. }
  111. }
  112. return $out;
  113. }
  114. /**
  115. * Gets the first child element with the given tag
  116. *
  117. * @param DOMElement $element element to pick at
  118. * @param string $tag tag to look for
  119. * @param string $namespace Namespace to look under
  120. *
  121. * @return DOMElement found element or null
  122. */
  123. static function child(DOMNode $element, $tag, $namespace=self::ATOM)
  124. {
  125. $els = $element->childNodes;
  126. if (empty($els) || $els->length == 0) {
  127. return null;
  128. } else {
  129. for ($i = 0; $i < $els->length; $i++) {
  130. $el = $els->item($i);
  131. if ($el->localName == $tag && $el->namespaceURI == $namespace) {
  132. return $el;
  133. }
  134. }
  135. }
  136. }
  137. /**
  138. * Gets all immediate child elements with the given tag
  139. *
  140. * @param DOMElement $element element to pick at
  141. * @param string $tag tag to look for
  142. * @param string $namespace Namespace to look under
  143. *
  144. * @return array found element or null
  145. */
  146. static function children(DOMNode $element, $tag, $namespace=self::ATOM)
  147. {
  148. $results = array();
  149. $els = $element->childNodes;
  150. if (!empty($els) && $els->length > 0) {
  151. for ($i = 0; $i < $els->length; $i++) {
  152. $el = $els->item($i);
  153. if ($el->localName == $tag && $el->namespaceURI == $namespace) {
  154. $results[] = $el;
  155. }
  156. }
  157. }
  158. return $results;
  159. }
  160. /**
  161. * Grab the text content of a DOM element child of the current element
  162. *
  163. * @param DOMElement $element Element whose children we examine
  164. * @param string $tag Tag to look up
  165. * @param string $namespace Namespace to use, defaults to Atom
  166. *
  167. * @return string content of the child
  168. */
  169. static function childContent(DOMNode $element, $tag, $namespace=self::ATOM)
  170. {
  171. $el = self::child($element, $tag, $namespace);
  172. if (empty($el)) {
  173. return null;
  174. } else {
  175. return $el->textContent;
  176. }
  177. }
  178. static function childHtmlContent(DOMNode $element, $tag, $namespace=self::ATOM)
  179. {
  180. $el = self::child($element, $tag, $namespace);
  181. if (empty($el)) {
  182. return null;
  183. } else {
  184. return self::textConstruct($el);
  185. }
  186. }
  187. /**
  188. * Get the content of an atom:entry-like object
  189. *
  190. * @param DOMElement $element The element to examine.
  191. *
  192. * @return string unencoded HTML content of the element, like "This -&lt; is <b>HTML</b>."
  193. *
  194. * @todo handle remote content
  195. * @todo handle embedded XML mime types
  196. * @todo handle base64-encoded non-XML and non-text mime types
  197. */
  198. static function getContent($element)
  199. {
  200. return self::childHtmlContent($element, self::CONTENT, self::ATOM);
  201. }
  202. static function textConstruct($el)
  203. {
  204. $src = $el->getAttribute(self::SRC);
  205. if (!empty($src)) {
  206. // TRANS: Client exception thrown when there is no source attribute.
  207. throw new ClientException(_("Can't handle remote content yet."));
  208. }
  209. $type = $el->getAttribute(self::TYPE);
  210. // slavishly following http://atompub.org/rfc4287.html#rfc.section.4.1.3.3
  211. if (empty($type) || $type == 'text') {
  212. // We have plaintext saved as the XML text content.
  213. // Since we want HTML, we need to escape any special chars.
  214. return htmlspecialchars($el->textContent);
  215. } else if ($type == 'html') {
  216. // We have HTML saved as the XML text content.
  217. // No additional processing required once we've got it.
  218. $text = $el->textContent;
  219. return $text;
  220. } else if ($type == 'xhtml') {
  221. // Per spec, the <content type="xhtml"> contains a single
  222. // HTML <div> with XHTML namespace on it as a child node.
  223. // We need to pull all of that <div>'s child nodes and
  224. // serialize them back to an (X)HTML source fragment.
  225. $divEl = ActivityUtils::child($el, 'div', 'http://www.w3.org/1999/xhtml');
  226. if (empty($divEl)) {
  227. return null;
  228. }
  229. $doc = $divEl->ownerDocument;
  230. $text = '';
  231. $children = $divEl->childNodes;
  232. for ($i = 0; $i < $children->length; $i++) {
  233. $child = $children->item($i);
  234. $text .= $doc->saveXML($child);
  235. }
  236. return trim($text);
  237. } else if (in_array($type, array('text/xml', 'application/xml')) ||
  238. preg_match('#(+|/)xml$#', $type)) {
  239. // TRANS: Client exception thrown when there embedded XML content is found that cannot be processed yet.
  240. throw new ClientException(_("Can't handle embedded XML content yet."));
  241. } else if (strncasecmp($type, 'text/', 5)) {
  242. return $el->textContent;
  243. } else {
  244. // TRANS: Client exception thrown when base64 encoded content is found that cannot be processed yet.
  245. throw new ClientException(_("Can't handle embedded Base64 content yet."));
  246. }
  247. }
  248. /**
  249. * Is this a valid URI for remote profile/notice identification?
  250. * Does not have to be a resolvable URL.
  251. * @param string $uri
  252. * @return boolean
  253. */
  254. static function validateUri($uri)
  255. {
  256. // Check mailto: URIs first
  257. $validate = new Validate();
  258. if (preg_match('/^mailto:(.*)$/', $uri, $match)) {
  259. return $validate->email($match[1], common_config('email', 'check_domain'));
  260. }
  261. if ($validate->uri($uri)) {
  262. return true;
  263. }
  264. // Possibly an upstream bug; tag: URIs aren't validated properly
  265. // unless you explicitly ask for them. All other schemes are accepted
  266. // for basic URI validation without asking.
  267. if ($validate->uri($uri, array('allowed_schemes' => array('tag')))) {
  268. return true;
  269. }
  270. return false;
  271. }
  272. static function getFeedAuthor(DOMElement $feedEl)
  273. {
  274. // Try old and deprecated activity:subject
  275. $subject = ActivityUtils::child($feedEl, Activity::SUBJECT, Activity::SPEC);
  276. if (!empty($subject)) {
  277. return new ActivityObject($subject);
  278. }
  279. // Try the feed author
  280. $author = ActivityUtils::child($feedEl, Activity::AUTHOR, Activity::ATOM);
  281. if (!empty($author)) {
  282. return new ActivityObject($author);
  283. }
  284. // Sheesh. Not a very nice feed! Let's try fingerpoken in the
  285. // entries.
  286. $entries = $feedEl->getElementsByTagNameNS(Activity::ATOM, 'entry');
  287. if (!empty($entries) && $entries->length > 0) {
  288. $entry = $entries->item(0);
  289. // Try the (deprecated) activity:actor
  290. $actor = ActivityUtils::child($entry, Activity::ACTOR, Activity::SPEC);
  291. if (!empty($actor)) {
  292. return new ActivityObject($actor);
  293. }
  294. // Try the author
  295. $author = ActivityUtils::child($entry, Activity::AUTHOR, Activity::ATOM);
  296. if (!empty($author)) {
  297. return new ActivityObject($author);
  298. }
  299. }
  300. return null;
  301. }
  302. static function compareTypes($type, $objects)
  303. {
  304. $type = self::resolveUri($type, false);
  305. foreach ((array)$objects as $object) {
  306. if ($type === self::resolveUri($object)) {
  307. return true;
  308. }
  309. }
  310. return false;
  311. }
  312. static function compareVerbs($type, $objects)
  313. {
  314. return self::compareTypes($type, $objects);
  315. }
  316. static function resolveUri($uri, $make_relative=false)
  317. {
  318. if (empty($uri)) {
  319. throw new ServerException('No URI to resolve in ActivityUtils::resolveUri');
  320. }
  321. if (!$make_relative && parse_url($uri, PHP_URL_SCHEME) == '') { // relative -> absolute
  322. $uri = Activity::SCHEMA . $uri;
  323. } elseif ($make_relative) { // absolute -> relative
  324. $uri = basename($uri); //preg_replace('/^http:\/\/activitystrea\.ms\/schema\/1\.0\//', '', $uri);
  325. } // absolute schemas pass through unharmed
  326. return $uri;
  327. }
  328. static function findLocalObject(array $uris, $type=ActivityObject::NOTE) {
  329. $obj_class = null;
  330. // TODO: Extend this in plugins etc. and describe in EVENTS.txt
  331. if (Event::handle('StartFindLocalActivityObject', array($uris, $type, &$obj_class))) {
  332. switch (self::resolveUri($type)) {
  333. case ActivityObject::PERSON:
  334. // GROUP will also be here in due time...
  335. $obj_class = 'Profile';
  336. break;
  337. default:
  338. $obj_class = 'Notice';
  339. }
  340. }
  341. $object = null;
  342. $uris = array_unique($uris);
  343. foreach ($uris as $uri) {
  344. try {
  345. // the exception thrown will cancel before reaching $object
  346. $object = call_user_func("{$obj_class}::fromUri", $uri);
  347. break;
  348. } catch (UnknownUriException $e) {
  349. common_debug('Could not find local activity object from uri: '.$e->object_uri);
  350. }
  351. }
  352. if (!$object instanceof Managed_DataObject) {
  353. throw new ServerException('Could not find any activityobject stored locally with given URIs: '.var_export($uris,true));
  354. }
  355. Event::handle('EndFindLocalActivityObject', array($object->getUri(), $object->getObjectType(), $object));
  356. return $object;
  357. }
  358. // Check authorship by supplying a Profile as a default and letting plugins
  359. // set it to something else if the activity's author is actually someone
  360. // else (like with a group or peopletag feed as handled in OStatus).
  361. //
  362. // NOTE: Returned is not necessarily the supplied profile! For example,
  363. // the "feed author" may be a group, but the "activity author" is a person!
  364. static function checkAuthorship(Activity $activity, Profile $profile)
  365. {
  366. if (Event::handle('CheckActivityAuthorship', array($activity, &$profile))) {
  367. // if (empty($activity->actor)), then we generated this Activity ourselves and can trust $profile
  368. $actor_uri = $profile->getUri();
  369. if (!in_array($actor_uri, array($activity->actor->id, $activity->actor->link))) {
  370. // A mismatch between our locally stored URI and the supplied author?
  371. // Probably not more than a blog feed or something (with multiple authors or so)
  372. // but log it for future inspection.
  373. common_log(LOG_WARNING, "Got an actor '{$activity->actor->title}' ({$activity->actor->id}) on single-user feed for " . $actor_uri);
  374. } elseif (empty($activity->actor->id)) {
  375. // Plain <author> without ActivityStreams actor info.
  376. // We'll just ignore this info for now and save the update under the feed's identity.
  377. }
  378. }
  379. if (!$profile instanceof Profile) {
  380. throw new ServerException('Could not get an author Profile for activity');
  381. }
  382. return $profile;
  383. }
  384. static public function typeToTitle($type)
  385. {
  386. return ucfirst(self::resolveUri($type, true));
  387. }
  388. static public function verbToTitle($verb)
  389. {
  390. return ucfirst(self::resolveUri($verb, true));
  391. }
  392. }